Changeset 385

Show
Ignore:
Timestamp:
07/27/09 19:35:38 (15 years ago)
Author:
sriram
Message:

Cleaning up the code a little..need to test this more for bugs.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • vtcross/branches/sriram/dsa.py

    r384 r385  
    1313from transmit_path import transmit_path 
    1414from receive_path import receive_path 
    15 global sync_status,mode,ch,traffic_flag 
     15 
     16global sync_status,mode,ch,traffic_flag,n_rcvd, n_right 
    1617sync_status = False 
    17 mode = "sync"#default mode is sync 
     18#Defining modes of operation 
     19# sync: the two nodes are trying to rendezvous on a common channel 
     20# traffic: the two node are communicating information to each other 
     21mode = "sync" #Default mode is sync 
    1822traffic_flag = False 
    1923class my_top_block(gr.top_block): 
     
    2832                self.connect(self.rxpath); 
    2933 
    30 global n_rcvd, n_right 
    3134 
    3235def main(): 
    33         print "inside main" 
     36 
    3437        global n_rcvd, n_right,sync_status,mode,ch,traffic_flag,n_attempts,return_flag 
    3538        n_rcvd = 0 
     
    3740        n_attempts = 5 
    3841        return_flag = 0 
     42 
    3943        def send_pkt(self, payload='', eof=False): 
    4044                return self.txpath.send_pkt(payload, eof) 
     
    5256 
    5357        def rx_callback(ok, payload): 
    54                 print "inside rx callback\n" 
     58                 
    5559                global n_rcvd, n_right,sync_status,mode,ch,traffic_flag 
    56                 ############################################################## 
     60                ########################## sync #################################### 
    5761                if mode == "sync": 
    5862                        if ok: 
     
    6064                                (sync_signal,) = struct.unpack('!s', payload[2])  
    6165                                (data_channel,) = struct.unpack('!H', payload[3:5]) 
    62                                 print "printing the elements of packet  ",sync_signal,"  !!!!!!!!!!!  ",data_channel," !!!!!\n"   
    63                                    
     66                                                                   
    6467                                if str(sync_signal) == 'o' and str(data_channel) == str(ch): 
    65                                         #print "inside if2" 
    66                                         sync_status = True 
     68                                         
     69                                        sync_status = True 
    6770                                        #tb.stop() 
    68                                         #print "after tb stop ans sync status is ",sync_status," sync status\n" 
    69                                  
     71                                                                         
    7072                                if str(sync_signal) == 's' and str(data_channel) == str(ch):  
     73                                         
    7174                                        sync_status = True 
    72                                         print "received a sync packet on channel %s\n" %(data_channel) 
    73                                         data = 'o' 
     75                                        data = 'o' 
    7476                                        pktno=0 
    7577                                        ack_payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data 
    7678                                        send_pkt(tb,ack_payload) #sending back the acknowledgement 
    77                         else: 
    78                                 print "sync packet not ok\n" 
    79                 ############################################################## 
     79                        #else: 
     80                        #       print "sync packet not ok\n" 
     81                ################################################################### 
    8082                 
    81                 ######################### traffic ############################ 
     83                ######################### traffic ################################# 
    8284                if mode == "traffic": 
    8385                        if ok:  
     
    8688                                        traffic_flag = True 
    8789                                        comm = struct.unpack('!14s', payload[1:15]) 
    88                                         print("received this ", comm)  
    8990                                        data = 'dI am fine.....' #Sending this message 
    9091                                        payload = struct.pack('!15s', data) 
    9192                                        send_pkt(tb,payload) 
    9293                                 
    93                                 #(data_header,) = struct.unpack('!s', payload[0]) 
    94                                 #if data_header == 'd': 
    95                                 #       comm = struct.unpack('!14s', payload[1:15]) 
    96                                 #       print("received this ", comm)  
    97                         else: 
    98                                 print "data packet not ok\n" 
     94                        #else: 
     95                        #       print "data packet not ok\n" 
    9996                ############################################################## 
    10097 
     
    105102                #print "ok = %5s  pktno = %4d  n_rcvd = %4d  n_right = %4d" % ( 
    106103                #    ok, pktno, n_rcvd, n_right) 
    107                 #print "sync_status is ",sync_status," @@\n" 
     104                 
    108105 
    109106        mods = modulation_utils.type_1_mods() 
    110         #print "printing mods",mods,"$$$$\n" 
    111107        demods = modulation_utils.type_1_demods() 
    112108 
    113109        #setting up the tx options parser 
     110 
    114111        parser_tx = OptionParser(option_class=eng_option, conflict_handler="resolve") 
    115112        expert_grp_tx = parser_tx.add_option_group("Expert_tx") 
    116         #print "printing Expert_tx group",expert_grp_tx,"printing Expert_tx group" 
    117         parser_tx.add_option("-m", "--modulation", type="choice", choices=mods.keys(), 
     113 
     114        parser_tx.add_option("-m", "--modulation", type="choice", choices=mods.keys(), 
    118115                        default='gmsk', 
    119116                        help="Select modulation from: %s [default=%%default]" 
     
    128125        parser_tx.add_option("","--from-file", default=None, 
    129126                        help="use file for packet contents") 
    130         print "printing parser_tx",parser_tx,"printing parser_tx" 
     127         
    131128        transmit_path.add_options(parser_tx, expert_grp_tx) 
    132129 
     
    156153        if options_tx.from_file is not None: 
    157154                source_file = open(options_tx.from_file, 'r') 
    158         print "printing modulation type",mods[options_tx.modulation],"**" 
    159         # build the graph 
    160         print "printing tx options",options_tx,"&&&&\n" 
    161      
     155             
    162156        parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve") 
    163         print "printing parser_rx",parser_rx,"!!!!!\n" 
    164         print "after option parser" 
    165157        expert_grp_rx = parser_rx.add_option_group("Expert_rx") 
    166         print "printing expert group rx",expert_grp_rx,"@@@@\n" 
    167158        parser_rx.add_option("-m", "--modulation", type="choice", choices=demods.keys(), 
    168159                        default='gmsk', 
    169160                        help="Select modulation from: %s [default=%%default]" 
    170161                                % (', '.join(demods.keys()),)) 
    171         print "inside rx option parser" 
     162         
    172163        receive_path.add_options(parser_rx, expert_grp_rx) 
    173164 
     
    193184                parser_rx.print_help(sys.stderr) 
    194185                sys.exit(1) 
    195         print "printing rx options",options_rx,"&&&&\n" 
    196  
     186         
    197187        # build the graph 
    198188 
     
    206196     
    207197        tb.start() 
    208         #tb.rxpath.disconnect(tb.rxpath.u, tb.rxpath.s2v,tb.rxpath.fft, tb.rxpath.c2mag, tb.rxpath.stats) 
    209         #tb.rxpath.connect(tb.rxpath.u, tb.rxpath.s2v) 
     198 
    210199        #listening to random frequencies untill a match is found 
    211200        running = True 
    212         global ch 
    213         running = True 
    214201        ch_energy = tb.rxpath.probe.level() #setting initial value 
    215202        hop_freq = options_tx.tx_freq #  = options_rx.rx_freq...same for tx and rx side 
     
    231218                                ch_energy = tb.rxpath.probe.level() #check if primary user is present 
    232219                                 
    233                                 print "printing hop frequency ",hop_freq,"and energy is ",ch_energy,"\n" 
    234220                                if int(ch_energy) > 1.5e8: #if primary user is there then dont transmit on this channel 
    235                                         #while  
    236221                                        continue 
    237                                         #print "present" 
    238222                                 
    239223                                nbytes = 5 #int(1e6 * .0003) 
     
    243227                                while n < nbytes: 
    244228                                        if options_tx.from_file is None: 
    245                                                 #data = (pkt_size - 2) * chr(pktno & 0xff) #0xff is 255 
    246                                                 data = 's'#+'s'+str(ch)+str(ch) #adding redundant bits for sync and channel 
     229                                                data = 's' 
    247230                                        else: 
    248231                                                data = source_file.read(pkt_size - 2) 
     
    251234 
    252235                                        payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data 
    253                                         #print "printing payload",payload,"**\n" 
    254          
     236                                                 
    255237                                        send_pkt(tb,payload) 
    256                                         print "sending this data ",data,"\n" 
    257                                         n += len(payload) 
     238                                        n += len(payload) 
    258239                                        sys.stderr.write('.') 
    259240                                        if options_tx.discontinuous and pktno % 5 == 4: 
     
    261242                                                pktno += 1 
    262243                                time.sleep(0.1) 
    263                         #resend_count += 1  
     244                         
    264245                        else: 
    265246                                print "sync channel found..channel ",ch,"\n"   
     
    274255                ################################################Communications mode################################# 
    275256                if mode == "traffic": 
    276                         print "Inside traffic mode" 
    277                                          
     257                                                         
    278258                        nbytes = 15 
    279259                        pkt_size = 15 
     
    281261                        n = 0 
    282262                        while n < nbytes: 
    283                                 #print >> myfile, "inside while" 
     263                                 
    284264                                if options_tx.from_file is None: 
    285                                         #data = (pkt_size - 2) * chr(data_pktno & 0xff) #0xff is 255 
    286                                         data = 'dHi how are you' #Sending this message 
    287                                         #print >> myfile, data 
    288                                         #print "printing data",data,"****\n" 
     265                                        data = 'dHi how are you' #Sending this message 
     266                                         
    289267                                else: 
    290268                                        data = source_file.read(pkt_size - 2) 
     
    294272                         
    295273                                payload = struct.pack('!15s', data) 
    296                                 #print "printing payload",payload,"**\n" 
    297                                  
     274                                                                 
    298275                                send_pkt(tb,payload) 
    299                                 print "printing payload",data,"**\n" 
     276                                #print "printing payload",data,"**\n" 
    300277                                n += len(payload) 
    301278                                sys.stderr.write('.') 
     
    303280                                        time.sleep(1) 
    304281                                data_pktno += 1 
    305                                 #print "before sleeping for 10 seconds and value of resend count is",resend_count 
    306                                 time.sleep(0.2 + 0.05*int(random.choice([0,1,2,3]))) 
     282                                time.sleep(0.2 + 0.05*int(random.choice([0,1,2,3]))) 
    307283 
    308284                                if traffic_flag != True: 
     
    313289 
    314290                                ch_energy = tb.rxpath.probe.level() #check if primary user is present 
    315                                 print "channel energy is ",ch_energy,"\n" 
     291                                 
    316292                                if int(ch_energy) > 1.5e8: #if primary user is there then dont transmit on this channel 
    317293                                        stop_time = datetime.now()       
    318294                                        _elapsed_time  = start_time - stop_time 
    319295                                        elapsed_time = _elapsed_time.seconds 
     296                                        print "primary user detected..moving out of this channel\n" 
    320297                                        mode = "sync" 
    321298                                        return_flag = 1 
    322299                 
    323  
    324300 
    325301if __name__ == '__main__':