Changeset 493

Show
Ignore:
Timestamp:
09/28/09 17:12:26 (15 years ago)
Author:
bhilburn
Message:

Fixing whitespace issues in benchmark_dsa.py, updated contact info.

Location:
vtcross/trunk/src/cross-examples/python/gnuradio-examples
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • vtcross/trunk/src/cross-examples/python/gnuradio-examples/README

    r416 r493  
    6060the Virginia Tech Cognitive Radio Open Source System. 
    6161 
    62 For questions or comments related to CROSS:  
    63 cross-discuss@listserv.vt.edu 
    64 or 
    65 trnewman@vt.edu 
     62For questions or comments related to CROSS, please get in touch with us! 
     63http://www.cornet.wireless.vt.edu/trac/wiki/Cross/Contact 
     64 
  • vtcross/trunk/src/cross-examples/python/gnuradio-examples/benchmark_dsa.py

    r464 r493  
    4545global sync_status,mode,ch,traffic_flag,n_rcvd, n_right 
    4646sync_status = False 
     47 
    4748#Defining modes of operation 
    4849# sync: the two nodes are trying to rendezvous on a common channel 
     
    5051mode = "sync" #Default mode is sync 
    5152traffic_flag = False 
     53 
    5254class my_top_block(gr.top_block): 
    5355 
    54         def __init__(self, mod_class, demod_class, 
    55                  rx_callback, options_tx,options_rx): 
    56  
    57                 gr.top_block.__init__(self) 
    58                 self.rxpath = usrp_receive_path.usrp_receive_path(demod_class, rx_callback, options_rx) 
    59                 self.txpath = usrp_transmit_path.usrp_transmit_path(mod_class, options_tx) 
    60                 self.connect(self.txpath); 
    61                 self.connect(self.rxpath); 
     56    def __init__(self, mod_class, demod_class, 
     57            rx_callback, options_tx,options_rx): 
     58 
     59        gr.top_block.__init__(self) 
     60        self.rxpath = usrp_receive_path.usrp_receive_path(demod_class, rx_callback, options_rx) 
     61        self.txpath = usrp_transmit_path.usrp_transmit_path(mod_class, options_tx) 
     62        self.connect(self.txpath); 
     63        self.connect(self.rxpath); 
    6264 
    6365 
    6466def main(): 
    6567 
    66         global stats_array, count_array, time_array, n_rcvd, n_right,sync_status,mode,ch,traffic_flag,n_attempts,return_flag 
    67         n_rcvd = 0 
    68         n_right = 0 
    69         n_attempts = 5 
    70         return_flag = 0 
    71  
    72         count_array = [ 0, 0, 0, 0, 0] 
    73         time_array = [ 0, 0, 0, 0, 0] 
    74         stats_array = [ 0, 0, 0, 0, 0] 
    75  
    76  
    77         def send_pkt(self, payload='', eof=False): 
    78                 return self.txpath.send_pkt(payload, eof) 
    79  
    80         def get_real_channel(channel): 
    81  
    82                 real_channel = 1; 
    83  
    84                 get_channel = { 
    85                         1: 1, 
    86                         2: 7, 
    87                         3: 8, 
    88                         4: 14  
    89                         } 
    90  
    91                 real_channel = get_channel[channel] 
    92  
    93                 return real_channel 
    94  
    95         def get_average_time(channel,absent_time): 
    96  
    97                 global count_array, time_array 
    98  
    99                 count_array[channel] = count_array[channel] + 1 
    100                 average_time = (time_array[channel] + absent_time) / count_array[channel]        
    101          
    102                 return average_time 
    103  
    104         def get_freq(hop_freq,probe_level,absent_time,cross): 
    105  
    106                 # Convert hop_freq to our unique channel list 
    107  
    108                 if cross == True: 
    109  
    110                         freq_channel = { 
    111                                 462562500: 1, 
    112                                 462712500: 2, 
    113                                 467562500: 3, 
    114                                 467712500: 4, 
    115                         } 
    116                         currentParameters = Parameter(1) 
    117                         currentParameters[0].name = "channel" 
    118                         currentParameters[0].value = freq_channel[hop_freq]  
    119  
    120                         o = Observable(2) 
    121                         o[0].value = probe_level 
    122                         o[0].name = "energy" 
    123          
    124                         o[1].value = absent_time  
    125                         o[1].name = "communication_time" 
    126                  
    127                         # If time == 0 then we are scanning and we dont want to  
    128                         #  use this time in the averaging process. 
    129                         if absent_time != 0: 
    130                                 try: 
    131                                         UpdateParameterPerformance(currentParameters,1,o,1) 
    132                                 except: 
    133                                         print "fail" 
    134                         else: 
    135                                 # Get the average communication time 
    136                                 average_time = get_average_time(freq_channel[hop_freq], absent_time) 
    137                                 o[1].value = average_time  
    138                                 o[1].name = "communication_time" 
    139                                 try: 
    140                                         UpdateParameterPerformance(currentParameters,1,o,2) 
    141                                 except: 
    142                                         print "fail" 
    143  
    144                         p = Parameter(1) 
    145                         p = GetOptimalParameters(o,2,currentParameters,1); 
    146                  
    147                         channel = get_real_channel(int(p[0].value)) 
    148                 else: 
    149                         channel = int(random.choice([1,7,8,14])) 
    150                          
    151                 if channel < 8: 
    152                         hop_freq = float(1e6 * (462.5625+(channel-1)*0.025))#setting the centre freq frequency for sending packets 
    153                 else: 
    154                         hop_freq = float(1e6 * (467.5625+(channel-8)*0.025))#setting the centre freq frequency for sending packets       
    155                  
    156                 return channel,hop_freq #returning the channel number and hop frequency 
    157          
    158  
    159         def rx_callback(ok, payload): 
    160                  
    161                 global n_rcvd, n_right,sync_status,mode,ch,traffic_flag 
    162                 ########################## sync #################################### 
    163                 if mode == "sync": 
    164                         if ok: 
    165                                 (pktno,) = struct.unpack('!H', payload[0:2]) 
    166                                 (sync_signal,) = struct.unpack('!s', payload[2])  
    167                                 (data_channel,) = struct.unpack('!H', payload[3:5]) 
    168                                                                    
    169                                 if str(sync_signal) == 'o' and str(data_channel) == str(ch): 
    170                                          
    171                                         sync_status = True 
    172                                         #tb.stop() 
    173                                                                          
    174                                 if str(sync_signal) == 's' and str(data_channel) == str(ch):  
    175                                          
    176                                         sync_status = True 
    177                                         data = 'o' 
    178                                         pktno=0 
    179                                         ack_payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data 
    180                                         send_pkt(tb,ack_payload) #sending back the acknowledgement 
    181                 ################################################################### 
    182                  
    183                 ######################### traffic ################################# 
    184                 if mode == "traffic": 
    185                         if ok:  
    186                                 (data_header,) = struct.unpack('!s', payload[0]) 
    187                                 if data_header == 'd': 
    188                                         traffic_flag = True 
    189                                         comm = struct.unpack('!14s', payload[1:15]) 
    190                                         data = 'dI am fine.....' #Sending this message 
    191                                         payload = struct.pack('!15s', data) 
    192                                         send_pkt(tb,payload) 
    193                                  
    194                 ############################################################## 
    195  
    196                 n_rcvd += 1 
    197                 if ok: 
    198                         n_right += 1 
    199  
    200         mods = modulation_utils.type_1_mods() 
    201         demods = modulation_utils.type_1_demods() 
    202  
    203         #setting up the tx options parser 
    204  
    205         parser_tx = OptionParser(option_class=eng_option, conflict_handler="resolve") 
    206          
    207         parser_tx.add_option("-m", "--modulation", type="choice", choices=mods.keys(), 
    208                         default='gmsk', 
    209                         help="Select modulation from: %s [default=%%default]" 
    210                                 % (', '.join(mods.keys()),)) 
    211         parser_tx.add_option("-s", "--size", type="eng_float", default=1500, 
    212                         help="set packet size [default=%default]") 
    213         parser_tx.add_option("-M", "--megabytes", type="eng_float", default=1.0, 
    214                         help="set megabytes to transmit [default=%default]") 
    215         parser_tx.add_option("","--discontinuous", action="store_true", default=False, 
    216                         help="enable discontinous transmission (bursts of 5 packets)")  
    217         parser_tx.add_option("","--from-file", default=None, 
    218                         help="use file for packet contents") 
     68    global stats_array, count_array, time_array, n_rcvd, n_right,sync_status,mode,ch,traffic_flag,n_attempts,return_flag 
     69    n_rcvd = 0 
     70    n_right = 0 
     71    n_attempts = 5 
     72    return_flag = 0 
     73 
     74    count_array = [ 0, 0, 0, 0, 0] 
     75    time_array = [ 0, 0, 0, 0, 0] 
     76    stats_array = [ 0, 0, 0, 0, 0] 
     77 
     78 
     79    def send_pkt(self, payload='', eof=False): 
     80        return self.txpath.send_pkt(payload, eof) 
     81 
     82 
     83    def get_real_channel(channel): 
     84 
     85        real_channel = 1; 
     86 
     87        get_channel = { 
     88            1: 1, 
     89            2: 7, 
     90            3: 8, 
     91            4: 14  
     92        } 
     93 
     94        real_channel = get_channel[channel] 
     95 
     96        return real_channel 
     97 
     98 
     99    def get_average_time(channel,absent_time): 
     100 
     101        global count_array, time_array 
     102 
     103        count_array[channel] = count_array[channel] + 1 
     104        average_time = (time_array[channel] + absent_time) / count_array[channel]     
     105     
     106        return average_time 
     107 
     108 
     109    def get_freq(hop_freq,probe_level,absent_time,cross): 
     110 
     111        # Convert hop_freq to our unique channel list 
     112 
     113        if cross == True: 
     114 
     115            freq_channel = { 
     116                462562500: 1, 
     117                462712500: 2, 
     118                467562500: 3, 
     119                467712500: 4, 
     120            } 
     121 
     122            currentParameters = Parameter(1) 
     123            currentParameters[0].name = "channel" 
     124            currentParameters[0].value = freq_channel[hop_freq]  
     125 
     126            o = Observable(2) 
     127            o[0].value = probe_level 
     128            o[0].name = "energy" 
     129     
     130            o[1].value = absent_time  
     131            o[1].name = "communication_time" 
     132         
     133            # If time == 0 then we are scanning and we dont want to  
     134            #  use this time in the averaging process. 
     135            if absent_time != 0: 
     136                try: 
     137                    UpdateParameterPerformance(currentParameters,1,o,1) 
     138                except: 
     139                    print "fail" 
     140            else: 
     141                # Get the average communication time 
     142                average_time = get_average_time(freq_channel[hop_freq], absent_time) 
     143                o[1].value = average_time  
     144                o[1].name = "communication_time" 
     145                try: 
     146                    UpdateParameterPerformance(currentParameters,1,o,2) 
     147                except: 
     148                    print "fail" 
     149 
     150            p = Parameter(1) 
     151            p = GetOptimalParameters(o,2,currentParameters,1); 
     152         
     153            channel = get_real_channel(int(p[0].value)) 
     154        else: 
     155            channel = int(random.choice([1,7,8,14])) 
     156             
     157        if channel < 8: 
     158            hop_freq = float(1e6 * (462.5625+(channel-1)*0.025))#setting the centre freq frequency for sending packets 
     159        else: 
     160            hop_freq = float(1e6 * (467.5625+(channel-8)*0.025))#setting the centre freq frequency for sending packets     
     161         
     162        return channel,hop_freq #returning the channel number and hop frequency 
     163     
     164 
     165    def rx_callback(ok, payload): 
     166         
     167        global n_rcvd, n_right,sync_status,mode,ch,traffic_flag 
     168        ########################## sync #################################### 
     169        if mode == "sync": 
     170            if ok: 
     171                (pktno,) = struct.unpack('!H', payload[0:2]) 
     172                   (sync_signal,) = struct.unpack('!s', payload[2])  
     173                (data_channel,) = struct.unpack('!H', payload[3:5]) 
     174                                   
     175                if str(sync_signal) == 'o' and str(data_channel) == str(ch): 
     176                    sync_status = True 
     177                    #tb.stop() 
     178                                                
     179                if str(sync_signal) == 's' and str(data_channel) == str(ch):  
     180                    sync_status = True 
     181                    data = 'o' 
     182                    pktno=0 
     183                    ack_payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data 
     184                    send_pkt(tb,ack_payload) #sending back the acknowledgement 
     185        ################################################################### 
     186             
     187        ######################### traffic ################################# 
     188        if mode == "traffic": 
     189            if ok:  
     190                (data_header,) = struct.unpack('!s', payload[0]) 
     191                if data_header == 'd': 
     192                    traffic_flag = True 
     193                    comm = struct.unpack('!14s', payload[1:15]) 
     194                    data = 'dI am fine.....' #Sending this message 
     195                    payload = struct.pack('!15s', data) 
     196                    send_pkt(tb,payload) 
     197                 
     198        ############################################################## 
     199 
     200       n_rcvd += 1 
     201       if ok: 
     202           n_right += 1 
     203 
     204    mods = modulation_utils.type_1_mods() 
     205    demods = modulation_utils.type_1_demods() 
     206 
     207    #setting up the tx options parser 
     208 
     209    parser_tx = OptionParser(option_class=eng_option, conflict_handler="resolve") 
     210     
     211    parser_tx.add_option("-m", "--modulation", type="choice", choices=mods.keys(), 
     212            default='gmsk',help="Select modulation from: %s [default=%%default]"  
     213            % (', '.join(mods.keys()),)) 
     214    parser_tx.add_option("-s", "--size", type="eng_float", default=1500, 
     215                      help="set packet size [default=%default]") 
     216    parser_tx.add_option("-M", "--megabytes", type="eng_float", default=1.0, 
     217                      help="set megabytes to transmit [default=%default]") 
     218    parser_tx.add_option("","--discontinuous", action="store_true", default=False, 
     219                      help="enable discontinous transmission (bursts of 5 packets)")  
     220    parser_tx.add_option("","--from-file", default=None, 
     221                          help="use file for packet contents") 
    219222  
    220         expert_grp_tx = parser_tx.add_option_group("Expert_tx") 
    221         dsa_grp = parser_tx.add_option_group("DSA Options") 
    222         dsa_grp.add_option("-c", "--cross", action="store_true", default=False, 
    223                                 help="Use the CROSS CBR Cognitive Engine for DSA channel decisions [default=off (random selection)].") 
    224          
    225         dsa_grp.add_option("-T", "--threshold", type="eng_float", default=1.5e8, 
    226                         help="set primary user sensing energy threshold [default=%default]") 
     223    expert_grp_tx = parser_tx.add_option_group("Expert_tx") 
     224    dsa_grp = parser_tx.add_option_group("DSA Options") 
     225    dsa_grp.add_option("-c", "--cross", action="store_true", default=False, 
     226            help="Use the CROSS CBR Cognitive Engine for DSA channel decisions [default=off (random selection)].") 
     227         
     228    dsa_grp.add_option("-T", "--threshold", type="eng_float", default=1.5e8, 
     229                          help="set primary user sensing energy threshold [default=%default]") 
    227230   
    228231 
    229         usrp_transmit_path.add_options(parser_tx, expert_grp_tx) 
    230         parser_tx.remove_option('-f'); 
    231         parser_tx.remove_option('--tx-freq'); 
    232  
    233         for mod in mods.values(): 
    234                 mod.add_options(expert_grp_tx) 
    235  
    236  
    237         (options_tx, args_tx) = parser_tx.parse_args () 
    238  
    239         if len(args_tx) != 0: 
    240                 parser_tx.print_help() 
    241                 sys.exit(1) 
    242          
    243         ############# Setting some default values for tx side of the block 
    244         options_tx.tx_freq = 462.5625e6 
    245         options_tx.samples_per_symbol =  2 
    246         options_tx.modulation = 'dbpsk' 
    247         options_tx.fusb_block_size = 4096 
    248         options_tx.fusb_nblocks = 16 
    249         options_tx.bitrate = 0.0125e6 
    250         ############# 
    251  
    252         if options_tx.tx_freq is None: 
    253                 sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") 
    254                 parser_tx.print_help(sys.stderr) 
    255                 sys.exit(1) 
    256  
    257         parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve") 
    258         expert_grp_rx = parser_rx.add_option_group("Expert_rx") 
    259         usrp_receive_path.add_options(parser_rx, expert_grp_rx) 
    260          
    261         parser_rx.remove_option('-f'); 
    262          
    263         parser_rx.add_option("-c", "--cross", action="store_true", default=False, 
    264                                 help="Use the CROSS engine for DSA decisions: default is random.") 
    265         parser_rx.add_option("-T", "--threshold", type="eng_float", default=1.5e8, 
    266                         help="set primary user sensing energy threshold [default=%default]") 
    267  
    268  
    269  
    270         (options_rx, args_rx) = parser_rx.parse_args () 
    271  
    272         ############# Setting some default values for rx side of the block 
    273         options_rx.rx_freq = 462.5625e6 #setting default rx_freq value 
    274         options_rx.samples_per_symbol =  2 
    275         options_rx.modulation = 'dbpsk' 
    276         options_rx.fusb_block_size = 4096 
    277         options_rx.fusb_nblocks = 16 
    278         options_rx.bitrate = 0.0125e6 
    279         ############# 
    280  
    281  
    282  
    283         if options_rx.cross == True: 
    284                 if cross_import == False: 
    285                         print "\n\n[[ CROSS Import failed..  Defaulting to RANDOM channel selection ]]" 
    286                         print "[[ Using the RANDOM channel selection algorithm ]]\n\n" 
    287                         options_rx.cross = False 
    288                 else: 
    289                         print "[[ Using the CROSS DSA Cognitive Engine ]]" 
    290         else: 
    291                 print "[[ Using the RANDOM channel selection algorithm ]]\n\n" 
    292                  
    293         # build the graph 
    294  
    295         tb = my_top_block(mods[options_tx.modulation], 
    296                         demods[options_rx.modulation], 
    297                         rx_callback,options_tx, 
    298                         options_rx) 
    299         r = gr.enable_realtime_scheduling() 
    300         if r != gr.RT_OK: 
    301                 print "Warning: failed to enable realtime scheduling" 
    302      
    303         tb.start() 
    304  
    305         #listening to random frequencies untill a match is found 
    306         running = True 
    307         ch_energy = tb.rxpath.probe.level() #setting initial value 
    308         hop_freq = options_tx.tx_freq #  = options_rx.rx_freq...same for tx and rx side 
    309          
    310         # Scan all channels first for inital data 
    311         time.sleep(0.1) 
    312  
    313         print "\n[[ Scanning channels for network nodes ]]\n" 
    314         while running: 
    315  
    316                 ################################################sync mode#################################### 
    317                 if mode == "sync": 
    318                         if sync_status != True: 
    319                                  
    320                                 if return_flag == 0: 
    321                                         ch,hop_freq = get_freq(hop_freq,ch_energy,0,options_rx.cross) 
    322                                 else: 
    323                                         ch,hop_freq = get_freq(hop_freq,ch_energy,elapsed_time,options_rx.cross) 
    324                                         return_flag = 0 
    325  
    326                                 tb.txpath.u.set_center_freq(hop_freq) 
    327                                 tb.rxpath.u.set_center_freq(hop_freq) 
    328                  
    329                                 ch_energy = tb.rxpath.probe.level() #check if primary user is present 
    330                                  
    331                                 if int(ch_energy) > 1.5e8: #if primary user is there then dont transmit on this channel 
    332                                         continue 
    333                                  
    334                                 nbytes = 5 #int(1e6 * .0003) 
    335                                 pkt_size = 5 
    336                                 n = 0 
    337                                 pktno = 0 
    338                                 while n < nbytes: 
    339                                         if options_tx.from_file is None: 
    340                                                 data = 's' 
    341                                         else: 
    342                                                 data = source_file.read(pkt_size - 2) 
    343                                                 if data == '': 
    344                                                         break; 
    345  
    346                                         payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data 
    347                                                  
    348                                         send_pkt(tb,payload) 
    349                                         n += len(payload) 
    350                                         sys.stderr.write('.') 
    351                                         if options_tx.discontinuous and pktno % 5 == 4: 
    352                                                 time.sleep(1) 
    353                                                 pktno += 1 
    354                                 time.sleep(0.1) 
    355                          
    356                         else: 
    357                                 print "\n\n[[ Network Node Found: Commencing communications on CHANNEL ", ch, " ]]\n"; 
    358                                 n_attempts_counter = 0 
    359                                 mode = "traffic" 
    360                                 traffic_flag = False 
    361                                 sync_status="False" 
    362                                 start_time = datetime.now() #measuring the time for which the primary user is away 
    363          
    364                 ################################################end of sync mode#################################### 
    365  
    366                 ################################################Communications mode################################# 
    367                 if mode == "traffic": 
    368                                                          
    369                         nbytes = 15 
    370                         pkt_size = 15 
    371                         data_pktno = 0 
    372                         n = 0 
    373                         while n < nbytes: 
    374                                  
    375                                 if options_tx.from_file is None: 
    376                                         data = 'dHi how are you' #Sending this message 
    377                                          
    378                                 else: 
    379                                         data = source_file.read(pkt_size - 2) 
    380                                         if data == '': 
    381                                                 break; 
    382          
    383                          
    384                                 payload = struct.pack('!15s', data) 
    385                                                                  
    386                                 send_pkt(tb,payload) 
    387                                 n += len(payload) 
    388                                 sys.stderr.write('.') 
    389                                 if options_tx.discontinuous and data_pktno % 5 == 4: 
    390                                         time.sleep(1) 
    391                                 data_pktno += 1 
    392                                 time.sleep(0.2 + 0.05*int(random.choice([0,1,2,3]))) 
    393  
    394                                 if traffic_flag != True: 
    395                                         n_attempts_counter += 1 
    396                                         if n_attempts_counter  > n_attempts: #get out of the data channel as it seems that the other node is still trying to rendezvous 
    397                                                 mode = "sync" 
    398                                                 continue 
    399  
    400                                 ch_energy = tb.rxpath.probe.level() #check if primary user is present 
    401                                  
    402                                 if int(ch_energy) > 1.5e8: #if primary user is there then dont transmit on this channel 
    403                                         stop_time = datetime.now()       
    404                                         _elapsed_time  = start_time - stop_time 
    405                                         elapsed_time = _elapsed_time.seconds 
    406                                         print "\n[[ Primary User Detected:  Evacuating Current Channel ]]\n" 
    407                                         print "\n[[ Scanning channels for network nodes ]]\n" 
    408                                         mode = "sync" 
    409                                         return_flag = 1 
     232    usrp_transmit_path.add_options(parser_tx, expert_grp_tx) 
     233    parser_tx.remove_option('-f'); 
     234    parser_tx.remove_option('--tx-freq'); 
     235 
     236    for mod in mods.values(): 
     237        mod.add_options(expert_grp_tx) 
     238 
     239 
     240    (options_tx, args_tx) = parser_tx.parse_args () 
     241 
     242    if len(args_tx) != 0: 
     243        parser_tx.print_help() 
     244        sys.exit(1) 
     245     
     246    ############# Setting some default values for tx side of the block 
     247    options_tx.tx_freq = 462.5625e6 
     248    options_tx.samples_per_symbol =  2 
     249    options_tx.modulation = 'dbpsk' 
     250    options_tx.fusb_block_size = 4096 
     251    options_tx.fusb_nblocks = 16 
     252    options_tx.bitrate = 0.0125e6 
     253    ############# 
     254 
     255    if options_tx.tx_freq is None: 
     256        sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") 
     257        parser_tx.print_help(sys.stderr) 
     258        sys.exit(1) 
     259 
     260    parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve") 
     261    expert_grp_rx = parser_rx.add_option_group("Expert_rx") 
     262    usrp_receive_path.add_options(parser_rx, expert_grp_rx) 
     263     
     264    parser_rx.remove_option('-f'); 
     265         
     266    parser_rx.add_option("-c", "--cross", action="store_true", default=False, 
     267            help="Use the CROSS engine for DSA decisions: default is random.") 
     268    parser_rx.add_option("-T", "--threshold", type="eng_float", default=1.5e8, 
     269                          help="set primary user sensing energy threshold [default=%default]") 
     270 
     271 
     272 
     273    (options_rx, args_rx) = parser_rx.parse_args () 
     274 
     275    ############# Setting some default values for rx side of the block 
     276    options_rx.rx_freq = 462.5625e6 #setting default rx_freq value 
     277    options_rx.samples_per_symbol =  2 
     278    options_rx.modulation = 'dbpsk' 
     279    options_rx.fusb_block_size = 4096 
     280    options_rx.fusb_nblocks = 16 
     281    options_rx.bitrate = 0.0125e6 
     282    ############# 
     283 
     284 
     285    if options_rx.cross == True: 
     286        if cross_import == False: 
     287            print "\n\n[[ CROSS Import failed..  Defaulting to RANDOM channel selection ]]" 
     288            print "[[ Using the RANDOM channel selection algorithm ]]\n\n" 
     289            options_rx.cross = False 
     290        else: 
     291            print "[[ Using the CROSS DSA Cognitive Engine ]]" 
     292    else: 
     293        print "[[ Using the RANDOM channel selection algorithm ]]\n\n" 
     294         
     295    # build the graph 
     296 
     297    tb = my_top_block(mods[options_tx.modulation], 
     298                      demods[options_rx.modulation], 
     299                      rx_callback,options_tx, 
     300                      options_rx) 
     301    r = gr.enable_realtime_scheduling() 
     302    if r != gr.RT_OK: 
     303        print "Warning: failed to enable realtime scheduling" 
     304     
     305    tb.start() 
     306 
     307    #listening to random frequencies untill a match is found 
     308    running = True 
     309    ch_energy = tb.rxpath.probe.level() #setting initial value 
     310    hop_freq = options_tx.tx_freq #  = options_rx.rx_freq...same for tx and rx side 
     311        
     312    # Scan all channels first for inital data 
     313    time.sleep(0.1) 
     314 
     315    print "\n[[ Scanning channels for network nodes ]]\n" 
     316    while running: 
     317        ################################################sync mode#################################### 
     318        if mode == "sync": 
     319            if sync_status != True: 
     320                     
     321                if return_flag == 0: 
     322                    ch,hop_freq = get_freq(hop_freq,ch_energy,0,options_rx.cross) 
     323                else: 
     324                    ch,hop_freq = get_freq(hop_freq,ch_energy,elapsed_time,options_rx.cross) 
     325                    return_flag = 0 
     326 
     327                tb.txpath.u.set_center_freq(hop_freq) 
     328                tb.rxpath.u.set_center_freq(hop_freq) 
     329               
     330                ch_energy = tb.rxpath.probe.level() #check if primary user is present 
     331                 
     332                if int(ch_energy) > 1.5e8: #if primary user is there then dont transmit on this channel 
     333                    continue 
     334                 
     335                nbytes = 5 #int(1e6 * .0003) 
     336                pkt_size = 5 
     337                n = 0 
     338                pktno = 0 
     339                while n < nbytes: 
     340                    if options_tx.from_file is None: 
     341                        data = 's' 
     342                    else: 
     343                        data = source_file.read(pkt_size - 2) 
     344                        if data == '': 
     345                            break; 
     346 
     347                    payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data 
     348                             
     349                    send_pkt(tb,payload) 
     350                    n += len(payload) 
     351                    sys.stderr.write('.') 
     352                    if options_tx.discontinuous and pktno % 5 == 4: 
     353                        time.sleep(1) 
     354                        pktno += 1 
     355                time.sleep(0.1) 
     356                     
     357            else: 
     358                print "\n\n[[ Network Node Found: Commencing communications on CHANNEL ", ch, " ]]\n"; 
     359                n_attempts_counter = 0 
     360                mode = "traffic" 
     361                traffic_flag = False 
     362                sync_status="False" 
     363                start_time = datetime.now() #measuring the time for which the primary user is away 
     364     
     365        ################################################end of sync mode#################################### 
     366 
     367        ################################################Communications mode################################# 
     368        if mode == "traffic": 
     369            nbytes = 15 
     370            pkt_size = 15 
     371            data_pktno = 0 
     372            n = 0 
     373            while n < nbytes: 
     374                if options_tx.from_file is None: 
     375                    data = 'dHi how are you' #Sending this message 
     376                else: 
     377                    data = source_file.read(pkt_size - 2) 
     378                    if data == '': 
     379                        break; 
     380     
     381             
     382                payload = struct.pack('!15s', data) 
     383                                        
     384                send_pkt(tb,payload) 
     385                n += len(payload) 
     386                sys.stderr.write('.') 
     387                if options_tx.discontinuous and data_pktno % 5 == 4: 
     388                    time.sleep(1) 
     389                data_pktno += 1 
     390                time.sleep(0.2 + 0.05*int(random.choice([0,1,2,3]))) 
     391 
     392                if traffic_flag != True: 
     393                    n_attempts_counter += 1 
     394                    if n_attempts_counter > n_attempts: #get out of the data channel as it seems that the other node is still trying to rendezvous 
     395                        mode = "sync" 
     396                        continue 
     397 
     398                ch_energy = tb.rxpath.probe.level() #check if primary user is present 
     399                 
     400                if int(ch_energy) > 1.5e8: #if primary user is there then dont transmit on this channel 
     401                    stop_time = datetime.now()     
     402                    _elapsed_time  = start_time - stop_time 
     403                    elapsed_time = _elapsed_time.seconds 
     404                    print "\n[[ Primary User Detected:  Evacuating Current Channel ]]\n" 
     405                    print "\n[[ Scanning channels for network nodes ]]\n" 
     406                    mode = "sync" 
     407                    return_flag = 1 
    410408                 
    411409 
     
    416414        pass 
    417415 
    418  
    419  
    420  
    421