Changeset 385
- Timestamp:
- 07/27/09 19:35:38 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
vtcross/branches/sriram/dsa.py
r384 r385 13 13 from transmit_path import transmit_path 14 14 from receive_path import receive_path 15 global sync_status,mode,ch,traffic_flag 15 16 global sync_status,mode,ch,traffic_flag,n_rcvd, n_right 16 17 sync_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 21 mode = "sync" #Default mode is sync 18 22 traffic_flag = False 19 23 class my_top_block(gr.top_block): … … 28 32 self.connect(self.rxpath); 29 33 30 global n_rcvd, n_right31 34 32 35 def main(): 33 print "inside main" 36 34 37 global n_rcvd, n_right,sync_status,mode,ch,traffic_flag,n_attempts,return_flag 35 38 n_rcvd = 0 … … 37 40 n_attempts = 5 38 41 return_flag = 0 42 39 43 def send_pkt(self, payload='', eof=False): 40 44 return self.txpath.send_pkt(payload, eof) … … 52 56 53 57 def rx_callback(ok, payload): 54 print "inside rx callback\n"58 55 59 global n_rcvd, n_right,sync_status,mode,ch,traffic_flag 56 ########################## ####################################60 ########################## sync #################################### 57 61 if mode == "sync": 58 62 if ok: … … 60 64 (sync_signal,) = struct.unpack('!s', payload[2]) 61 65 (data_channel,) = struct.unpack('!H', payload[3:5]) 62 print "printing the elements of packet ",sync_signal," !!!!!!!!!!! ",data_channel," !!!!!\n" 63 66 64 67 if str(sync_signal) == 'o' and str(data_channel) == str(ch): 65 #print "inside if2"66 68 69 sync_status = True 67 70 #tb.stop() 68 #print "after tb stop ans sync status is ",sync_status," sync status\n" 69 71 70 72 if str(sync_signal) == 's' and str(data_channel) == str(ch): 73 71 74 sync_status = True 72 print "received a sync packet on channel %s\n" %(data_channel) 73 data = 'o' 75 data = 'o' 74 76 pktno=0 75 77 ack_payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data 76 78 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 ################################################################### 80 82 81 ######################### traffic ############################ 83 ######################### traffic ################################# 82 84 if mode == "traffic": 83 85 if ok: … … 86 88 traffic_flag = True 87 89 comm = struct.unpack('!14s', payload[1:15]) 88 print("received this ", comm)89 90 data = 'dI am fine.....' #Sending this message 90 91 payload = struct.pack('!15s', data) 91 92 send_pkt(tb,payload) 92 93 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" 99 96 ############################################################## 100 97 … … 105 102 #print "ok = %5s pktno = %4d n_rcvd = %4d n_right = %4d" % ( 106 103 # ok, pktno, n_rcvd, n_right) 107 #print "sync_status is ",sync_status," @@\n"104 108 105 109 106 mods = modulation_utils.type_1_mods() 110 #print "printing mods",mods,"$$$$\n"111 107 demods = modulation_utils.type_1_demods() 112 108 113 109 #setting up the tx options parser 110 114 111 parser_tx = OptionParser(option_class=eng_option, conflict_handler="resolve") 115 112 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(), 118 115 default='gmsk', 119 116 help="Select modulation from: %s [default=%%default]" … … 128 125 parser_tx.add_option("","--from-file", default=None, 129 126 help="use file for packet contents") 130 print "printing parser_tx",parser_tx,"printing parser_tx"127 131 128 transmit_path.add_options(parser_tx, expert_grp_tx) 132 129 … … 156 153 if options_tx.from_file is not None: 157 154 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 162 156 parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve") 163 print "printing parser_rx",parser_rx,"!!!!!\n"164 print "after option parser"165 157 expert_grp_rx = parser_rx.add_option_group("Expert_rx") 166 print "printing expert group rx",expert_grp_rx,"@@@@\n"167 158 parser_rx.add_option("-m", "--modulation", type="choice", choices=demods.keys(), 168 159 default='gmsk', 169 160 help="Select modulation from: %s [default=%%default]" 170 161 % (', '.join(demods.keys()),)) 171 print "inside rx option parser"162 172 163 receive_path.add_options(parser_rx, expert_grp_rx) 173 164 … … 193 184 parser_rx.print_help(sys.stderr) 194 185 sys.exit(1) 195 print "printing rx options",options_rx,"&&&&\n" 196 186 197 187 # build the graph 198 188 … … 206 196 207 197 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 210 199 #listening to random frequencies untill a match is found 211 200 running = True 212 global ch213 running = True214 201 ch_energy = tb.rxpath.probe.level() #setting initial value 215 202 hop_freq = options_tx.tx_freq # = options_rx.rx_freq...same for tx and rx side … … 231 218 ch_energy = tb.rxpath.probe.level() #check if primary user is present 232 219 233 print "printing hop frequency ",hop_freq,"and energy is ",ch_energy,"\n"234 220 if int(ch_energy) > 1.5e8: #if primary user is there then dont transmit on this channel 235 #while236 221 continue 237 #print "present"238 222 239 223 nbytes = 5 #int(1e6 * .0003) … … 243 227 while n < nbytes: 244 228 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' 247 230 else: 248 231 data = source_file.read(pkt_size - 2) … … 251 234 252 235 payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data 253 #print "printing payload",payload,"**\n" 254 236 255 237 send_pkt(tb,payload) 256 print "sending this data ",data,"\n" 257 n += len(payload) 238 n += len(payload) 258 239 sys.stderr.write('.') 259 240 if options_tx.discontinuous and pktno % 5 == 4: … … 261 242 pktno += 1 262 243 time.sleep(0.1) 263 #resend_count += 1244 264 245 else: 265 246 print "sync channel found..channel ",ch,"\n" … … 274 255 ################################################Communications mode################################# 275 256 if mode == "traffic": 276 print "Inside traffic mode" 277 257 278 258 nbytes = 15 279 259 pkt_size = 15 … … 281 261 n = 0 282 262 while n < nbytes: 283 #print >> myfile, "inside while"263 284 264 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 289 267 else: 290 268 data = source_file.read(pkt_size - 2) … … 294 272 295 273 payload = struct.pack('!15s', data) 296 #print "printing payload",payload,"**\n" 297 274 298 275 send_pkt(tb,payload) 299 print "printing payload",data,"**\n"276 #print "printing payload",data,"**\n" 300 277 n += len(payload) 301 278 sys.stderr.write('.') … … 303 280 time.sleep(1) 304 281 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]))) 307 283 308 284 if traffic_flag != True: … … 313 289 314 290 ch_energy = tb.rxpath.probe.level() #check if primary user is present 315 print "channel energy is ",ch_energy,"\n"291 316 292 if int(ch_energy) > 1.5e8: #if primary user is there then dont transmit on this channel 317 293 stop_time = datetime.now() 318 294 _elapsed_time = start_time - stop_time 319 295 elapsed_time = _elapsed_time.seconds 296 print "primary user detected..moving out of this channel\n" 320 297 mode = "sync" 321 298 return_flag = 1 322 299 323 324 300 325 301 if __name__ == '__main__':