def main(): global n_rcvd, n_right n_rcvd = 0 n_right = 0 def rx_callback(ok, payload): global n_rcvd, n_right n_rcvd += 1 if ok: n_right += 1 tb.audio_tx.msgq().insert_tail(gr.message_from_string(payload)) print "ok = %r n_rcvd = %4d n_right = %4d" % (ok, n_rcvd, n_right) demods = modulation_utils.type_1_demods() # Create Options Parser: parser = OptionParser(option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") parser.add_option("-m", "--modulation", type="choice", choices=demods.keys(), default='gmsk', help="Select modulation from: %s [default=%%default]" % (', '.join(demods.keys()), )) parser.add_option("-O", "--audio-output", type="string", default="", help="pcm output device name. E.g., hw:0,0 or /dev/dsp") usrp_receive_path.add_options(parser, expert_grp) for mod in demods.values(): mod.add_options(expert_grp) parser.set_defaults(bitrate=50e3) # override default bitrate default (options, args) = parser.parse_args() if len(args) != 0: parser.print_help(sys.stderr) sys.exit(1) if options.rx_freq is None: sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") parser.print_help(sys.stderr) sys.exit(1) # build the graph tb = my_top_block(demods[options.modulation], rx_callback, options) r = gr.enable_realtime_scheduling() if r != gr.RT_OK: print "Warning: Failed to enable realtime scheduling." tb.run()
def main_loop(sound_obj,count,Freq): global n_rcvd, n_right ,data n_rcvd = 0 n_right = 0 data="" global packet_file packet_file = open('/usr/local/share/gnuradio/examples/audio/sound','a') def rx_callback(ok, payload): global n_rcvd, n_right global data count.value=1 #reset the counter n_rcvd += 1 (pktno,) = struct.unpack('!H', payload[0:2]) if ok: n_right += 1 #if pktno > 19: #print "writing to file\n" #if (len(data)<1024): #print "length of payload",len(payload) #data=data+payload[2:] #else: #data_buff=data[1024:] print "length of data",len(payload[2:]) sound_obj.sound_sink.write(payload[2:]) #data=data_buff print "ok: %r \t pktno: %d \t n_rcvd: %d \t n_right: %d" % (ok, pktno, n_rcvd, n_right) parser = OptionParser(option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") parser.add_option("", "--snr", type="eng_float", default=30, help="set the SNR of the channel in dB [default=%default]") usrp_receive_path.add_options(parser, expert_grp) ofdm.ofdm_demod.add_options(parser, expert_grp) (options, args) = parser.parse_args () if len(args) != 0: parser.print_help(sys.stderr) sys.exit(1) options.rx_freq=Freq.value if options.rx_freq is None: sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") parser.print_help(sys.stderr) sys.exit(1) tb = my_top_block(rx_callback, options) r = gr.enable_realtime_scheduling() if r != gr.RT_OK: print "Warning: failed to enable realtime scheduling" tb.start() # start flow graph tb.wait() # wait for it to finish
def main(): global n_rcvd, n_right n_rcvd = 0 n_right = 0 def rx_callback(ok, payload): global n_rcvd, n_right (pktno, ) = struct.unpack('!H', payload[0:2]) n_rcvd += 1 if ok: n_right += 1 print "ok = %5s pktno = %4d n_rcvd = %4d n_right = %4d" % ( ok, pktno, n_rcvd, n_right) demods = modulation_utils.type_1_demods() # Create Options Parser: parser = OptionParser(option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") parser.add_option("-m", "--modulation", type="choice", choices=demods.keys(), default='gmsk', help="Select modulation from: %s [default=%%default]" % (', '.join(demods.keys()), )) usrp_receive_path.add_options(parser, expert_grp) for mod in demods.values(): mod.add_options(expert_grp) (options, args) = parser.parse_args() if len(args) != 0: parser.print_help(sys.stderr) sys.exit(1) if options.rx_freq is None: sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") parser.print_help(sys.stderr) sys.exit(1) # build the graph tb = my_top_block(demods[options.modulation], rx_callback, options) r = gr.enable_realtime_scheduling() if r != gr.RT_OK: print "Warning: Failed to enable realtime scheduling." tb.start() # start flow graph tb.wait() # wait for it to finish
def main(): global n_rcvd, n_right n_rcvd = 0 n_right = 0 def rx_callback(ok, payload): global n_rcvd, n_right n_rcvd += 1 if ok: n_right += 1 tb.audio_tx.msgq().insert_tail(gr.message_from_string(payload)) print "ok = %r n_rcvd = %4d n_right = %4d" % ( ok, n_rcvd, n_right) demods = modulation_utils.type_1_demods() # Create Options Parser: parser = OptionParser (option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") parser.add_option("-m", "--modulation", type="choice", choices=demods.keys(), default='gmsk', help="Select modulation from: %s [default=%%default]" % (', '.join(demods.keys()),)) parser.add_option("-O", "--audio-output", type="string", default="", help="pcm output device name. E.g., hw:0,0 or /dev/dsp") usrp_receive_path.add_options(parser, expert_grp) for mod in demods.values(): mod.add_options(expert_grp) parser.set_defaults(bitrate=50e3) # override default bitrate default (options, args) = parser.parse_args () if len(args) != 0: parser.print_help(sys.stderr) sys.exit(1) if options.rx_freq is None: sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") parser.print_help(sys.stderr) sys.exit(1) # build the graph tb = my_top_block(demods[options.modulation], rx_callback, options) r = gr.enable_realtime_scheduling() if r != gr.RT_OK: print "Warning: Failed to enable realtime scheduling." tb.run()
def main(): global n_rcvd, n_right n_rcvd = 0 n_right = 0 def rx_callback(ok, payload): global n_rcvd, n_right (pktno,) = struct.unpack('!H', payload[0:2]) n_rcvd += 1 if ok: n_right += 1 print "ok = %5s pktno = %4d n_rcvd = %4d n_right = %4d" % ( ok, pktno, n_rcvd, n_right) #print "payload ",payload demods = modulation_utils.type_1_demods() # Create Options Parser: parser = OptionParser (option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") parser.add_option("-m", "--modulation", type="choice", choices=demods.keys(), default='gmsk', help="Select modulation from: %s [default=%%default]" % (', '.join(demods.keys()),)) usrp_receive_path.add_options(parser, expert_grp) for mod in demods.values(): mod.add_options(expert_grp) (options, args) = parser.parse_args () if len(args) != 0: parser.print_help(sys.stderr) sys.exit(1) if options.rx_freq is None: sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") parser.print_help(sys.stderr) sys.exit(1) # build the graph tb = my_top_block(demods[options.modulation], rx_callback, options) r = gr.enable_realtime_scheduling() if r != gr.RT_OK: print "Warning: Failed to enable realtime scheduling." tb.start() # start flow graph tb.wait() # wait for it to finish
def main(): global n_rcvd, n_right n_rcvd = 0 n_right = 0 global packet_file packet_file = open('/home/kranthi/rx1.txt', 'w') def rx_callback(ok, payload): global n_rcvd, n_right (preamble, ) = struct.unpack('!H', payload[2:4]) if (preamble == 0): n_rcvd += 1 (pktno, ) = struct.unpack('!H', payload[0:2]) if pktno > 19: packet_file.write(payload[4:]) if ok: n_right += 1 print "ok: %r \t pktno: %d \t n_rcvd: %d \t n_right: %d" % ( ok, pktno, n_rcvd, n_right) parser = OptionParser(option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") parser.add_option( "", "--snr", type="eng_float", default=30, help="set the SNR of the channel in dB [default=%default]") usrp_receive_path.add_options(parser, expert_grp) ofdm.ofdm_demod.add_options(parser, expert_grp) (options, args) = parser.parse_args() if len(args) != 0: parser.print_help(sys.stderr) sys.exit(1) if options.rx_freq is None: sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") parser.print_help(sys.stderr) sys.exit(1) tb = my_top_block(rx_callback, options) r = gr.enable_realtime_scheduling() if r != gr.RT_OK: print "Warning: failed to enable realtime scheduling" tb.start() tb.wait()
def main(): global n_rcvd, n_right n_rcvd = 0 n_right = 0 global packet_file packet_file = open('/home/kranthi/rx1.txt','w') def rx_callback(ok, payload): global n_rcvd, n_right (preamble,) = struct.unpack('!H', payload[2:4]) if (preamble == 0): n_rcvd += 1 (pktno,) = struct.unpack('!H', payload[0:2]) if pktno > 19: packet_file.write(payload[4:]) if ok: n_right += 1 print "ok: %r \t pktno: %d \t n_rcvd: %d \t n_right: %d" % (ok, pktno, n_rcvd, n_right) parser = OptionParser(option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") parser.add_option("", "--snr", type="eng_float", default=30, help="set the SNR of the channel in dB [default=%default]") usrp_receive_path.add_options(parser, expert_grp) ofdm.ofdm_demod.add_options(parser, expert_grp) (options, args) = parser.parse_args () if len(args) != 0: parser.print_help(sys.stderr) sys.exit(1) if options.rx_freq is None: sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") parser.print_help(sys.stderr) sys.exit(1) tb = my_top_block(rx_callback, options) r = gr.enable_realtime_scheduling() if r != gr.RT_OK: print "Warning: failed to enable realtime scheduling" tb.start() tb.wait()
def __init__(self): self.macAddr = 0 args = ["-f", "2.4G", "-R", "B", "-T", "B"] demods = modulation_utils.type_1_demods() mods = modulation_utils.type_1_mods() parser = OptionParser(option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") parser.add_option( "-m", "--modulation", type="choice", choices=mods.keys(), default='gmsk', help="Select modulation from: %s [default=%%default]" % (', '.join(mods.keys()), )) usrp_transmit_path.add_options(parser, expert_grp) usrp_receive_path.add_options(parser, expert_grp) for mod in mods.values(): mod.add_options(expert_grp) for demod in demods.values(): demod.add_options(expert_grp) (options, args) = parser.parse_args(args) self.tb = my_top_block(demods[options.modulation], mods[options.modulation], self.callback, options) r = gr.enable_realtime_scheduling() if r != gr.RT_OK: print 'Warning: Failed to enable realtime scheduling.' self.tb.start() self.n_rcvd = 0 self.n_right = 0 self.pkt_no = 0 print "server init ok!"
def add_options(normal, expert): """ Adds usrp-specific options to the Options Parser """ add_freq_option(normal) mods = modulation_utils.type_1_mods() demods = modulation_utils.type_1_demods() normal.add_option("-m", "--modulation", type="choice", choices=mods.keys(), default='gmsk', help="Select modulation from: %s [default=%%default]" % (', '.join(mods.keys()),)) #other necessary options usrp_transmit_path.add_options(normal, expert) usrp_receive_path.add_options(normal, expert) for mod in mods.values(): mod.add_options(expert) for demod in demods.values(): demod.add_options(expert)
def __init__(self): self.macAddr = 1 args = ["-f", "2.4G", "-R", "B", "-T", "B"] demods = modulation_utils.type_1_demods() mods = modulation_utils.type_1_mods() parser = OptionParser(option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") parser.add_option("-m", "--modulation", type="choice", choices=mods.keys(), default='gmsk', help="Select modulation from: %s [default=%%default]" % (', '.join(mods.keys()),)) usrp_transmit_path.add_options(parser, expert_grp) usrp_receive_path.add_options(parser, expert_grp) for mod in mods.values(): mod.add_options(expert_grp) for demod in demods.values(): demod.add_options(expert_grp) (options, args) = parser.parse_args(args) self.tb = my_top_block(demods[options.modulation], mods[options.modulation], self.callback, options) r = gr.enable_realtime_scheduling() if r != gr.RT_OK: print 'Warning: Failed to enable realtime scheduling.' self.tb.start() self.n_rcvd = 0 self.n_right = 0 self.pkt_no = 0 self.reqId = 0 print "client init ok!"
def main(): parser = OptionParser(option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") parser.add_option("-t", "--runtime", type=int, default=900) parser.add_option("-s", "--size", type="intx", default=97, help="set packet size [default=%default]") parser.add_option("-p", "--spacing", type="eng_float", default=1, help="set packet spacing in time [default=%default]") parser.add_option("-r", "--sample_rate", type="eng_float", default=500000, help="limit sample rate to RATE in throttle (%default)") parser.add_option("-l", "--loss", type="eng_float", default=0.2, help="loss rate that white rate tolerates (%default)") parser.add_option("", "--snr", type="eng_float", default=30, help="set the SNR of the channel in dB [default=%default]") parser.add_option("","--sender", action="store_true", default=False) parser.add_option("", "--tx-ip", type="string", default="128.111.52.67", help="Trasmitter's IP, needed for over-Ethernet-ACKs [default=%default]") parser.add_option("", "--rx-ip", type="string", default="128.111.52.70", help="Receiver's IP, needed for over-Ethernet-ACKs [default=%default]") #parser.add_option("", "--rx-ip", type="string", default="2001:4200:7000:112:227:eff:fe1c:2428", help="Receiver's IP, needed for over-Ethernet-ACKs [default=%default]") parser.add_option("", "--rx-port", type="intx", default=2727, help="Receiver's listening port, needed for over-Ethernet-ACKs [default=%default]") parser.add_option("", "--ack-timeout", type="eng_float", default=1, help="ACK timeout in seconds [default=%default]") parser.add_option("-v", "--verbose", action="store_true", default=False) transmit_path.transmit_path.add_options(parser, expert_grp) receive_path.receive_path.add_options(parser, expert_grp) usrp_transmit_path.add_options(parser, expert_grp) usrp_receive_path.add_options(parser, expert_grp) flex_ofdm.ofdm_mod.add_options(parser, expert_grp) flex_ofdm.ofdm_demod.add_options(parser, expert_grp) (options, args) = parser.parse_args () top = top_class(options.loss, options) r = gr.enable_realtime_scheduling() if r != gr.RT_OK: print "Warning: failed to enable realtime scheduling" top.start() # start flow graph if options.sender: top.run(options.runtime) top.wait() # wait for it to finish
def add_options(normal, expert): """ Adds usrp-specific options to the Options Parser """ add_freq_option(normal) mods = modulation_utils.type_1_mods() demods = modulation_utils.type_1_demods() normal.add_option( "-m", "--modulation", type="choice", choices=mods.keys(), default='gmsk', help="Select modulation from: %s [default=%%default]" % (', '.join(mods.keys()), )) #other necessary options usrp_transmit_path.add_options(normal, expert) usrp_receive_path.add_options(normal, expert) for mod in mods.values(): mod.add_options(expert) for demod in demods.values(): demod.add_options(expert)
def main(): mods = modulation_utils.type_1_mods() demods = modulation_utils.type_1_demods() parser = OptionParser (option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") expert_grp.add_option("", "--rx-freq", type="eng_float", default=None, help="set Rx frequency to FREQ [default=%default]", metavar="FREQ") expert_grp.add_option("", "--tx-freq", type="eng_float", default=None, help="set transmit frequency to FREQ [default=%default]", metavar="FREQ") parser.add_option("-m", "--modulation", type="choice", choices=mods.keys(), default='gmsk', help="Select modulation from: %s [default=%%default]" % (', '.join(mods.keys()),)) parser.add_option("-b","--bssid", default="00:00:00:00:00:00", help="set bssid for network in the form xx:xx:xx:xx:xx:xx") #ZLM copy form chenlong parser.add_option("-v","--verbose", action="store_true", default=False) expert_grp.add_option("-c", "--carrier-threshold", type="eng_float", default=30, help="set carrier detect threshold (dB) [default=%default]") parser.add_option("","--tun", action="store_true", default=False, help="use tun device instead of tap to pass packets.") #ZLM copy form chenlong expert_grp.add_option("","--tun-device-filename", default="/dev/net/tun", help="path to tun device file [default=%default]") usrp_transmit_path.add_options(parser, expert_grp) usrp_receive_path.add_options(parser, expert_grp) for mod in mods.values(): mod.add_options(expert_grp) for demod in demods.values(): demod.add_options(expert_grp) (options, args) = parser.parse_args () if len(args) != 0: parser.print_help(sys.stderr) sys.exit(1) bssid = validate_mac_addr(options.bssid) if bssid == 0: print "Invalid BSSID ", options.bssid parser.print_help() sys.exit(1) mod_kwargs = { 'bt' : options.bt, } pkttype = 'eth' tuntype = 'tap' mcache = None if options.tun: pkttype = 'ip' tuntype = 'tun' # open the TUN/TAP interface (tun_fd, tun_ifname) = open_tun_interface(tuntype, options.tun_device_filename) tun_mac = get_mac_for_interface(tun_ifname) mac_addr = validate_mac_addr(tun_mac) if mac_addr == 0: print "Invalid MAC address ", tun_mac, " for interface ", tun_ifname print "exiting." sys.exit(1) if options.verbose: print "Using MAC address ", tun_mac, " for interface ", tun_ifname # Attempt to enable realtime scheduling r = gr.enable_realtime_scheduling() if r == gr.RT_OK: realtime = True else: realtime = False print "Note: failed to enable realtime scheduling" # If the user hasn't set the fusb_* parameters on the command line, # pick some values that will reduce latency. if options.fusb_block_size == 0 and options.fusb_nblocks == 0: if realtime: # be more aggressive options.fusb_block_size = gr.prefs().get_long('fusb', 'rt_block_size', 1024) options.fusb_nblocks = gr.prefs().get_long('fusb', 'rt_nblocks', 16) else: options.fusb_block_size = gr.prefs().get_long('fusb', 'block_size', 4096) options.fusb_nblocks = gr.prefs().get_long('fusb', 'nblocks', 16) #print "fusb_block_size =", options.fusb_block_size #print "fusb_nblocks =", options.fusb_nblocks numchan = 3 # instantiate the MACi DC = [2.810e9,] for i in range(numchan): DC += [DC[i] + 0.002 * 10**9] #gen channel QCH = makeQCH.makeQCH(numchan)[1] #gen QCH number # QCH = [1,1,1,1,1,1,1,1,1] mac = cs_mac(tun_fd, mac_addr, pkttype, bssid, mcache, DC, QCH, verbose=True) # build the graph (PHY) fg = my_graph(mods[options.modulation],demods[options.modulation], mac.phy_rx_callback, options) mac.set_flow_graph(fg) # give the MAC a handle for the PHY if fg.txpath.bitrate() != fg.rxpath.bitrate(): print "WARNING: Transmit bitrate = %sb/sec, Receive bitrate = %sb/sec" % ( eng_notation.num_to_str(fg.txpath.bitrate()), eng_notation.num_to_str(fg.rxpath.bitrate())) print "modulation: %s" % (options.modulation,) print "freq: %s" % (eng_notation.num_to_str(options.tx_freq)) print "bitrate: %sb/sec" % (eng_notation.num_to_str(fg.txpath.bitrate()),) print "samples/symbol: %3d" % (fg.txpath.samples_per_symbol(),) #print "interp: %3d" % (tb.txpath.interp(),) #print "decim: %3d" % (tb.rxpath.decim(),) fg.rxpath.set_carrier_threshold(options.carrier_threshold) print "Carrier sense threshold:", options.carrier_threshold, "dB" print print "Allocated virtual ethernet interface: %s" % (tun_ifname,) print "You must now use ifconfig to set its IP address. E.g.," print print " $ sudo ifconfig %s 192.168.200.1" % (tun_ifname,) print print "Be sure to use a different address in the same subnet for each machine." print fg.start() # Start executing the flow graph (runs in separate threads) f1 = open('reservation_time.txt','w') f2 = open('reservation_slot.txt','w') f3 = open('Qlearn.txt','w') f4 = open('channel.txt','w') # mac.main_loop(f1,f2,f3) mac.main_loop(f1,f2,f3,f4) # don't expect this to return... f1.close() f2.close() f3.close() fg.stop() # but if it does, tell flow graph to stop. fg.wait() # wait for it to finish
def main(): global n_rcvd, n_right n_rcvd = 0 n_right = 0 def rx_callback(ok, payload): global n_rcvd, n_right (pktno, ) = struct.unpack('!H', payload[0:2]) (beacon_ID, ) = struct.unpack('!H', payload[2:4]) #Added to catch t.o.d. from modified beacon (t_mant1, ) = struct.unpack('!H', payload[4:6]) (t_mant2, ) = struct.unpack('!H', payload[6:8]) (t_mant3, ) = struct.unpack('!H', payload[8:10]) (t_frac1, ) = struct.unpack('!H', payload[10:12]) (t_frac2, ) = struct.unpack('!H', payload[12:14]) (t_frac3, ) = struct.unpack('!H', payload[14:16]) (t_frac4, ) = struct.unpack('!H', payload[16:18]) t_str = (str(t_mant1) + str(t_mant2) + str(t_mant3) + '.' + str(t_frac1) + str(t_frac2) + str(t_frac3) + str(t_frac4)) #Added to identify team in file team_id = 1 #Added to collect time of arrival toa = time.time() n_rcvd += 1 if ok: n_right += 1 print "ok = %5s pktno = %4d beacon_ID = %4d n_rcvd = %4d n_right = %4d" % ( ok, pktno, beacon_ID, n_rcvd, n_right) os.system('echo ' + str(pktno) + ',' + str(beacon_ID) + '>> plb_data') f = open('gui_beacon_data', 'w') f.write(str(pktno)) f.close() #new data string action #beacon_pktno:beacon_ID:t.o.d.:field_team_ID:t.o.a. data_string = (str(pktno) + ':' + str(beacon_ID) + ':' + t_str + ':' + str(team_id) + ':' + ("%.15f" % toa) + '\n') #brand new file writing action f1 = open('geolocation_data', 'a') f1.write(data_string) f1.close() demods = modulation_utils.type_1_demods() # Create Options Parser: parser = OptionParser(option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") parser.add_option("-m", "--modulation", type="choice", choices=demods.keys(), default='gmsk', help="Select modulation from: %s [default=%%default]" % (', '.join(demods.keys()), )) usrp_receive_path.add_options(parser, expert_grp) for mod in demods.values(): mod.add_options(expert_grp) (options, args) = parser.parse_args() if len(args) != 0: parser.print_help(sys.stderr) sys.exit(1) if options.rx_freq is None: sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") parser.print_help(sys.stderr) sys.exit(1) # build the graph tb = my_top_block(demods[options.modulation], rx_callback, options) r = gr.enable_realtime_scheduling() if r != gr.RT_OK: print "Warning: Failed to enable realtime scheduling." tb.start() # start flow graph tb.wait() # wait for it to finish
def main(): global n_rcvd, n_right n_rcvd = 0 n_right = 0 def rx_callback(ok, payload): global n_rcvd, n_right (pktno,) = struct.unpack('!H', payload[0:2]) (beacon_ID,) = struct.unpack('!H', payload[2:4]) #Added to catch t.o.d. from modified beacon (t_mant1,) = struct.unpack('!H', payload[4:6]) (t_mant2,) = struct.unpack('!H', payload[6:8]) (t_mant3,) = struct.unpack('!H', payload[8:10]) (t_frac1,) = struct.unpack('!H', payload[10:12]) (t_frac2,) = struct.unpack('!H', payload[12:14]) (t_frac3,) = struct.unpack('!H', payload[14:16]) (t_frac4,) = struct.unpack('!H', payload[16:18]) t_str = (str(t_mant1)+str(t_mant2)+str(t_mant3)+'.'+str(t_frac1)+ str(t_frac2)+str(t_frac3)+str(t_frac4)) #Added to identify team in file team_id = 1 #Added to collect time of arrival toa = time.time() n_rcvd += 1 if ok: n_right += 1 print "ok = %5s pktno = %4d beacon_ID = %4d n_rcvd = %4d n_right = %4d" % ( ok, pktno, beacon_ID, n_rcvd, n_right) os.system('echo ' + str(pktno) + ',' + str(beacon_ID) + '>> plb_data') f = open('gui_beacon_data', 'w') f.write(str(pktno)) f.close() #new data string action #beacon_pktno:beacon_ID:t.o.d.:field_team_ID:t.o.a. data_string = (str(pktno)+':'+str(beacon_ID)+':'+t_str+ ':'+str(team_id)+':'+("%.15f"%toa)+'\n') #brand new file writing action f1 = open('geolocation_data', 'a') f1.write(data_string) f1.close() demods = modulation_utils.type_1_demods() # Create Options Parser: parser = OptionParser (option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") parser.add_option("-m", "--modulation", type="choice", choices=demods.keys(), default='gmsk', help="Select modulation from: %s [default=%%default]" % (', '.join(demods.keys()),)) usrp_receive_path.add_options(parser, expert_grp) for mod in demods.values(): mod.add_options(expert_grp) (options, args) = parser.parse_args () if len(args) != 0: parser.print_help(sys.stderr) sys.exit(1) if options.rx_freq is None: sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") parser.print_help(sys.stderr) sys.exit(1) # build the graph tb = my_top_block(demods[options.modulation], rx_callback, options) r = gr.enable_realtime_scheduling() if r != gr.RT_OK: print "Warning: Failed to enable realtime scheduling." tb.start() # start flow graph tb.wait() # wait for it to finish
def main(): global stats_array, count_array, time_array, n_rcvd global n_right, sync_status, mode, ch, data_packet_delivery_count global n_attempts data_packet_delivery_count_previous = 0 n_rcvd = 0 n_right = 0 n_attempts = 5 threshold = 0.01 count_array = [ 0, 0, 0, 0, 0] time_array = [ 0, 0, 0, 0, 0] stats_array = [ 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,0] def send_pkt(self, payload='', eof=False): return self.txpath.send_pkt(payload, eof) def get_freq_rx(): return 2.44*1e9 def get_freq_tx(): # return 8,900e6 # Convert hop_freq to our unique channel list channel = int(random.choice([1,7])) hop_freq = float(1e6 * (850+(channel-1)*5))#setting the centre freq frequency for sending packets stats_array[channel] = stats_array[channel] + 1 print "\nChannel DSA Selection Statistics (Channel #: Number times selected)" print "1: ", stats_array[1], " 7: ",stats_array[7] return channel,hop_freq #returning the channel number and hop frequency def rx_callback(ok, payload): global n_rcvd, n_right,sync_status,mode,ch,data_packet_delivery_count ########################## sync #################################### if mode == "sync": if ok: print "SYNC:GOT CHANNEL PACKET" (pktno,) = struct.unpack('!H', payload[0:2]) (sync_signal,) = struct.unpack('!s', payload[2]) (data_channel,) = struct.unpack('!H', payload[3:5]) if str(sync_signal) == 'o' and str(data_channel) == str(ch): sync_status = True #tb.stop() print "SYNC:RECEIVE CONFIRM PACKET...LINK ESTABLISHED" if str(sync_signal) == 's' and str(data_channel) == str(ch): print "SYNC:SEND CONFIRM PACKET" sync_status = True data = 'o' pktno=0 ack_payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data send_pkt(tb,ack_payload) #sending back the acknowledgement ################################################################### ######################### traffic ################################# if mode == "traffic": if ok: (data_header,) = struct.unpack('!s', payload[0]) if data_header == 'd': #print "TRAFFIC:SEND ACK" data_packet_delivery_count = data_packet_delivery_count +1 comm = struct.unpack('!14s', payload[1:15]) data = 'dI am fine.....' #Sending this message payload = struct.pack('!15s', data) send_pkt(tb,payload) ############################################################## n_rcvd += 1 if ok: n_right += 1 mods = digital.modulation_utils.type_1_mods() demods = digital.modulation_utils.type_1_demods() #setting up the tx options parser parser_tx = OptionParser(option_class=eng_option, conflict_handler="resolve") parser_tx.add_option("-m", "--modulation", type="choice", choices=mods.keys(), default='gmsk',help="Select modulation from: %s [default=%%default]" % (', '.join(mods.keys()),)) parser_tx.add_option("-s", "--size", type="eng_float", default=1500, help="set packet size [default=%default]") parser_tx.add_option("-M", "--megabytes", type="eng_float", default=1.0, help="set megabytes to transmit [default=%default]") parser_tx.add_option("","--discontinuous", action="store_true", default=False, help="enable discontinous transmission (bursts of 5 packets)") parser_tx.add_option("","--from-file", default=None, help="use file for packet contents") expert_grp_tx = parser_tx.add_option_group("Expert_tx") dsa_grp = parser_tx.add_option_group("DSA Options") dsa_grp.add_option("-T", "--threshold", type="eng_float", default=0.01, help="set primary user sensing energy threshold [default=%default]") usrp_transmit_path.add_options(parser_tx, expert_grp_tx) parser_tx.remove_option('-f'); #parser_tx.remove_option('--tx-freq'); for mod in mods.values(): mod.add_options(expert_grp_tx) (options_tx, args_tx) = parser_tx.parse_args () if len(args_tx) != 0: parser_tx.print_help() sys.exit(1) ############# Setting some default values for tx side of the block options_tx.tx_freq = 900e6 options_tx.samples_per_symbol = 2 options_tx.modulation = 'gmsk' options_tx.fusb_block_size = 4096 options_tx.fusb_nblocks = 16 options_tx.bitrate = 0.0125e6 ############# parser_rx = OptionParser (option_class=eng_option, conflict_handler="resolve") expert_grp_rx = parser_rx.add_option_group("Expert_rx") usrp_receive_path.add_options(parser_rx, expert_grp_rx) parser_rx.remove_option('-f'); (options_rx, args_rx) = parser_rx.parse_args () ############# Setting some default values for rx side of the block options_rx.rx_freq = 2.44e9 #setting default rx_freq value options_rx.samples_per_symbol = 2 options_rx.modulation = 'gmsk' options_rx.fusb_block_size = 4096 options_rx.fusb_nblocks = 16 options_rx.bitrate = 0.0125e6 ############# print "[[ Using the RANDOM channel selection algorithm ]]\n\n" # build the graph tb = my_top_block(mods[options_tx.modulation], demods[options_rx.modulation], rx_callback,options_tx, options_rx) r = gr.enable_realtime_scheduling() if r != gr.RT_OK: print "Warning: failed to enable realtime scheduling" tb.start() #listening to random frequencies untill a match is found running = True # Scan all channels first for inital data #time.sleep(0.1) print "\n[[ Scanning channels for network nodes ]]\n" timeout = time.time() + timer while running: if time.time() > timeout: break ################################################sync mode#################################### if mode == "sync": if sync_status != True: ch,hop_freq = get_freq_tx() hop_freq_rx = get_freq_rx() tb.txpath.sink.set_freq(hop_freq) tb.rxpath.source.set_freq(hop_freq_rx) print "RX_FREQ:",hop_freq_rx," TX_FREQ:",hop_freq ch_energy = level(ch) #check if primary user is present #print ch_energy,"*"*30 if int(ch_energy) > threshold: #if primary user is there then dont transmit on this channel time.sleep(1) continue nbytes = 5 #int(1e6 * .0003) pkt_size = 5 n = 0 pktno = 0 while n < nbytes: if options_tx.from_file is None: data = 's' else: data = source_file.read(pkt_size - 2) if data == '': break; payload = struct.pack('!HsH', pktno & 0xffff,data,ch & 0xffff) #+ data send_pkt(tb,payload) n += len(payload) sys.stderr.write("SEND SYNC PACKET\n") if options_tx.discontinuous and pktno % 5 == 4: time.sleep(1) pktno += 1 time.sleep(1) else: print "\n\n[[ Network Node Found: Commencing communications on CHANNEL ", ch, " ]]\n"; n_attempts_counter = 0 mode = "traffic" data_packet_delivery_count = 0 sync_status="False" start_time = datetime.now() #measuring the time for which the primary user is away ################################################end of sync mode#################################### ################################################Communications mode################################# if mode == "traffic": nbytes = 15 pkt_size = 15 data_pktno = 0 n = 0 while n < nbytes: if options_tx.from_file is None: data = 'dHi how are you' #Sending this message else: data = source_file.read(pkt_size - 2) if data == '': break; payload = struct.pack('!15s', data) send_pkt(tb,payload) n += len(payload) sys.stderr.write("SEND TRAFFIC PACKET\n") if options_tx.discontinuous and data_pktno % 5 == 4: time.sleep(1) data_pktno += 1 time.sleep(0.2 + 0.05*int(random.choice([0,1,2,3]))) if data_packet_delivery_count == data_packet_delivery_count_previous: #checking if the data packet delivery has stagnated n_attempts_counter += 1 if n_attempts_counter > n_attempts: #get out of the data channel as it seems that the other node is still trying to rendezvous mode = "sync" continue else: data_packet_delivery_count_previous = 0 data_packet_delivery_count = 0 data_packet_delivery_count_previous = data_packet_delivery_count ch_energy = level(ch) #check if primary user is present print "CHANNEL ENERGY:",ch_energy,"\n" if int(ch_energy) > threshold: #if primary user is there then dont transmit on this channel stop_time = datetime.now() _elapsed_time = start_time - stop_time print "\n[[ Primary User Detected: Evacuating Current Channel ]]\n" print "\n[[ Scanning channels for network nodes ]]\n" print "\nAbsent time:",_elapsed_time,"\n" mode = "sync"
def main(): global n_rcvd, n_right n_rcvd = 0 n_right = 0 def send_pkt(payload='', eof=False, seqno=0): return tb.txpath.send_pkt(payload, eof, seqno) def rx_callback(ok, payload, int_fo, frac_fo, time_sinr, freq_sinr, ch_gain, ch_gain_full, avg_gain, width_ranking): global n_rcvd, n_right n_rcvd += 1 try: (pktno,) = struct.unpack('!H', payload[0:2]) except: pktno = 1 if ok: n_right += 1 print "ok: %r \t pktno: %d \t n_rcvd: %d \t n_right: %d" % (ok, pktno, n_rcvd, n_right) freq_offset = int_fo+frac_fo/math.pi print "freq offset: %+.2f(subcarriers) \t SINR: %.2f(time domain), %.2f(freq domain)" % (freq_offset, time_sinr, freq_sinr) #print "Channel gain: "+str(ch_gain) #print "Average gain: "+str(avg_gain) if 1: printlst = list() for x in payload[2:]: t = hex(ord(x)).replace('0x', '') if(len(t) == 1): t = '0' + t printlst.append(t) printable = ''.join(printlst) print printable print "\n" pktno = 0 pkt_size = int(options.size) #time.sleep(0.2) #send_pkt(pkt_size * chr(0x05), eof=False, seqno=pktno) #print "SENT!\n" parser = OptionParser(option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") parser.add_option ("-t", "--runtime", type=int, default=4) parser.add_option("-s", "--size", type="intx", default=95, help="set packet size [default=%default]") parser.add_option("-p", "--spacing", type="eng_float", default=1, help="set packet spacing in time [default=%default]") parser.add_option("-r", "--sample_rate", type="eng_float", default=500000, help="limit sample rate to RATE in throttle (%default)") parser.add_option("","--discontinuous", action="store_true", default=False, help="enable discontinuous mode") parser.add_option("", "--snr", type="eng_float", default=30, help="set the SNR of the channel in dB [default=%default]") parser.add_option("","--sender", action="store_true", default=False) my_top_block.add_options(parser, expert_grp) transmit_path.transmit_path.add_options(parser, expert_grp) receive_path.receive_path.add_options(parser, expert_grp) usrp_receive_path.add_options(parser, expert_grp) usrp_transmit_path.add_options(parser, expert_grp) flex_ofdm.ofdm_mod.add_options(parser, expert_grp) flex_ofdm.ofdm_demod.add_options(parser, expert_grp) (options, args) = parser.parse_args () # build the graph tb = my_top_block(rx_callback, options) r = gr.enable_realtime_scheduling() if r != gr.RT_OK: print "Warning: failed to enable realtime scheduling" tb.start() # start flow graph start_time = time.time() # generate and send infinite stream of packets, will be # interrupted by runtime n = 0 pktno = 0 pkt_size = int(options.size) i = 0 while True: #time.sleep(5) # print "Try %d" %i # i += 1 #tb.rxpath.reset_ofdm_params("1111111") time.sleep(2) #tb.stop() #tb.wait() #tb.lock() tb.rxpath.reset_ofdm_params("1100000") #tb.unlock() #tb.start() #time.sleep(5) #tb.rxpath.reset_ofdm_params("1100011") #time.sleep(5) #tb.rxpath.reset_ofdm_params("1100000") #time.sleep(2) #tb.rxpath.reset_ofdm_params("1100000") #while time.time() < start_time + options.runtime: #if True: # while pktno < options.runtime: # send_pkt(pkt_size * chr(0x05), eof=False, seqno=pktno) # n += pkt_size # #sys.stderr.write('.') # pktno += 1 # print "sent %d packets" % (pktno,) # time.sleep(options.spacing) #send_pkt(eof=True) #print "\nfinishing: sent %d packets" % (pktno,) #raise SystemExit tb.wait() # wait for it to finish print "\n\n\n\n\n\nENDED!\n\n\n\n\n"
def run(shm): global n_rcvd, n_right n_rcvd = 0 n_right = 0 def rx_callback(ok, payload): global n_rcvd, n_right (pktno,) = struct.unpack('!H', payload[0:2]) (beacon_pktno,) = struct.unpack('!H', payload[2:4]) (beacon_ID,) = struct.unpack('!H', payload[4:6]) (team_ID,) = struct.unpack('!H', payload[6:8]) (lon_mant,) = struct.unpack('!H', payload[8:10]) (lon_frac1,) = struct.unpack('!H', payload[10:12]) (lon_frac2,) = struct.unpack('!H', payload[12:14]) (lon_frac3,) = struct.unpack('!H', payload[14:16]) (lon_frac4,) = struct.unpack('!H', payload[16:18]) (lat_mant,) = struct.unpack('!H', payload[18:20]) (lat_frac1,) = struct.unpack('!H', payload[20:22]) (lat_frac2,) = struct.unpack('!H', payload[22:24]) (lat_frac3,) = struct.unpack('!H', payload[24:26]) (lat_frac4,) = struct.unpack('!H', payload[26:28]) n_rcvd += 1 if ok: n_right += 1 print "ok = %5s pktno = %4d n_rcvd = %4d n_right = %4d" % ( ok, pktno, n_rcvd, n_right) print "" print "plb beacon number: ", beacon_pktno print "beacon ID: ", beacon_ID print "team ID: ", team_ID print "longitude: -%d.%d%d%d%d" %(lon_mant, lon_frac1, lon_frac2, lon_frac3, lon_frac4) print "latitude: %d.%d%d%d%d" %(lat_mant, lat_frac1, lat_frac2, lat_frac3, lat_frac4) # convert lat/lon from sting to float longitude = '-' + str(lon_mant) + '.'+ str(lon_frac1) + str(lon_frac2) + str(lon_frac3) + str(lon_frac4) latitude = str(lat_mant) + '.'+ str(lat_frac1) + str(lat_frac2) + str(lat_frac3) + str(lat_frac4) coords = longitude + ',' + latitude shm.push(coords) demods = modulation_utils.type_1_demods() # Create Options Parser: parser = OptionParser (option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") parser.add_option("-m", "--modulation", type="choice", choices=demods.keys(), default='gmsk', help="Select modulation from: %s [default=%%default]" % (', '.join(demods.keys()),)) usrp_receive_path.add_options(parser, expert_grp) for mod in demods.values(): mod.add_options(expert_grp) (options, args) = parser.parse_args () options.rx_freq = 468000000 options.bitrate = 125000 options.usrpx = 2 if len(args) != 0: parser.print_help(sys.stderr) sys.exit(1) if options.rx_freq is None: sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") parser.print_help(sys.stderr) sys.exit(1) # build the graph tb = my_top_block(demods[options.modulation], rx_callback, options) r = gr.enable_realtime_scheduling() if r != gr.RT_OK: print "Warning: Failed to enable realtime scheduling." tb.start() # start flow graph tb.wait() # wait for it to finish
def main(): mods = modulation_utils.type_1_mods() demods = modulation_utils.type_1_demods() parser = OptionParser (option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") expert_grp.add_option("", "--rx-freq", type="eng_float", default=None, help="set Rx frequency to FREQ [default=%default]", metavar="FREQ") expert_grp.add_option("", "--tx-freq", type="eng_float", default=None, help="set transmit frequency to FREQ [default=%default]", metavar="FREQ") parser.add_option("-m", "--modulation", type="choice", choices=mods.keys(), default='gmsk', help="Select modulation from: %s [default=%%default]" % (', '.join(mods.keys()),)) parser.add_option("-v","--verbose", action="store_true", default=False) expert_grp.add_option("-c", "--carrier-threshold", type="eng_float", default=30, help="set carrier detect threshold (dB) [default=%default]") expert_grp.add_option("","--tun-device-filename", default="/dev/net/tun", help="path to tun device file [default=%default]") usrp_transmit_path.add_options(parser, expert_grp) usrp_receive_path.add_options(parser, expert_grp) for mod in mods.values(): mod.add_options(expert_grp) for demod in demods.values(): demod.add_options(expert_grp) (options, args) = parser.parse_args () if len(args) != 0: parser.print_help(sys.stderr) sys.exit(1) # open the TUN/TAP interface (tun_fd, tun_ifname) = open_tun_interface(options.tun_device_filename) # Attempt to enable realtime scheduling r = gr.enable_realtime_scheduling() if r == gr.RT_OK: realtime = True else: realtime = False print "Note: failed to enable realtime scheduling" # If the user hasn't set the fusb_* parameters on the command line, # pick some values that will reduce latency. if options.fusb_block_size == 0 and options.fusb_nblocks == 0: if realtime: # be more aggressive options.fusb_block_size = gr.prefs().get_long('fusb', 'rt_block_size', 1024) options.fusb_nblocks = gr.prefs().get_long('fusb', 'rt_nblocks', 16) else: options.fusb_block_size = gr.prefs().get_long('fusb', 'block_size', 4096) options.fusb_nblocks = gr.prefs().get_long('fusb', 'nblocks', 16) #print "fusb_block_size =", options.fusb_block_size #print "fusb_nblocks =", options.fusb_nblocks # instantiate the MAC mac = cs_mac(tun_fd, verbose=True) # build the graph (PHY) tb = my_top_block(mods[options.modulation], demods[options.modulation], mac.phy_rx_callback, options) mac.set_top_block(tb) # give the MAC a handle for the PHY if tb.txpath.bitrate() != tb.rxpath.bitrate(): print "WARNING: Transmit bitrate = %sb/sec, Receive bitrate = %sb/sec" % ( eng_notation.num_to_str(tb.txpath.bitrate()), eng_notation.num_to_str(tb.rxpath.bitrate())) print "modulation: %s" % (options.modulation,) print "freq: %s" % (eng_notation.num_to_str(options.tx_freq)) print "bitrate: %sb/sec" % (eng_notation.num_to_str(tb.txpath.bitrate()),) print "samples/symbol: %3d" % (tb.txpath.samples_per_symbol(),) #print "interp: %3d" % (tb.txpath.interp(),) #print "decim: %3d" % (tb.rxpath.decim(),) tb.rxpath.set_carrier_threshold(options.carrier_threshold) print "Carrier sense threshold:", options.carrier_threshold, "dB" print print "Allocated virtual ethernet interface: %s" % (tun_ifname,) print "You must now use ifconfig to set its IP address. E.g.," print print " $ sudo ifconfig %s 192.168.200.1" % (tun_ifname,) print print "Be sure to use a different address in the same subnet for each machine." print tb.start() # Start executing the flow graph (runs in separate threads) mac.main_loop() # don't expect this to return... tb.stop() # but if it does, tell flow graph to stop. tb.wait() # wait for it to finish
def main(): def send_pkt(payload='', eof=False): return tb.txpath.send_pkt(payload, eof) global n_rcvd, n_right n_rcvd = 0 n_right = 0 def rx_callback(ok, payload): global n_rcvd, n_right (pktno, ) = struct.unpack('!H', payload[0:2]) n_rcvd += 1 if ok: n_right += 1 print "ok = %5s pktno = %4d n_rcvd = %4d n_right = %4d" % ( ok, pktno, n_rcvd, n_right) demods = modulation_utils.type_1_demods() mods = modulation_utils.type_1_mods() parser = OptionParser(option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") parser.add_option("-m", "--modulation", type="choice", choices=mods.keys(), default='gmsk', help="Select modulation from: %s [default=%%default]" % (', '.join(mods.keys()), )) parser.add_option("-s", "--size", type="eng_float", default=1500, help="set packet size [default=%default]") parser.add_option("-M", "--megabytes", type="eng_float", default=1.0, help="set megabytes to transmit [default=%default]") parser.add_option( "", "--discontinuous", action="store_true", default=False, help="enable discontinous transmission (bursts of 5 packets)") parser.add_option("", "--from-file", default=None, help="use file for packet contents") usrp_transmit_path.add_options(parser, expert_grp) usrp_receive_path.add_options(parser, expert_grp) for mod in mods.values(): mod.add_options(expert_grp) for demod in demods.values(): demod.add_options(expert_grp) (options, args) = parser.parse_args() if len(args) != 0: parser.print_help() sys.exit(1) if options.tx_freq is None or options.rx_freq is None: sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") parser.print_help(sys.stderr) sys.exit(1) tb = my_top_block(demods[options.modulation], mods[options.modulation], rx_callback, options) r = gr.enable_realtime_scheduling() if r != gr.RT_OK: print 'Warning: Failed to enable realtime scheduling.' tb.start() # generate and send packets nbytes = int(1e6 * options.megabytes) n = 0 pktno = 0 pkt_size = int(options.size) while n < nbytes: if options.from_file is None: data = (pkt_size - 2) * chr(pktno & 0xff) else: data = source_file.read(pkt_size - 2) if data == '': break payload = struct.pack('!H', pktno & 0xffff) + data send_pkt(payload) n += len(payload) sys.stderr.write('.') time.sleep(0.01) if options.discontinuous and pktno % 5 == 4: time.sleep(1) pktno += 1 send_pkt('', True) tb.wait()
def main(): global n_rcvd, n_right, shift, sync n_rcvd = 0 n_right = 0 shift = 0 sync = 0 no_packets = 50 global packet_file packet_file = open('/home/kranthi/documents/sound_cognition/test_rx.raw', 'w') def rx_callback(ok, payload): global n_rcvd, n_right, shift, parser, tb, sync (preamble, ) = struct.unpack('!H', payload[2:4]) if (preamble == 11111): n_rcvd += 1 shift = 0 (pktno, ) = struct.unpack('!H', payload[0:2]) if pktno == 20: (no_packets, ) = struct.unpack('!H', payload[4:]) print no_packets no_packets += 20 elif pktno < 20: print elif (pktno > 120 and pktno < 150): print elif pktno >= 150: if sync == 1: print "\nReceived Synchronization packet in 920M Frequency" if ok: (freq, ) = struct.unpack( '!L', payload[4:]) #int(payload[4:]) tb.rxpath.u.u.set_center_freq(freq, 0) print "and shifting to ", freq, " Frequency" sync = 0 else: packet_file.write(payload[4:]) if ok: n_right += 1 print "ok: %r \t pktno: %d \t n_rcvd: %d \t n_right: %d" % ( ok, pktno, n_rcvd, n_right) else: shift += 1 if (shift >= 10): freq = 920 * 10**6 sync = 1 print "Shifting to 920M frequency" tb.rxpath.u.u.set_center_freq(freq, 0) global parser parser = OptionParser(option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") parser.add_option( "", "--snr", type="eng_float", default=30, help="set the SNR of the channel in dB [default=%default]") usrp_receive_path.add_options(parser, expert_grp) ofdm.ofdm_demod.add_options(parser, expert_grp) (options, args) = parser.parse_args() if len(args) != 0: parser.print_help(sys.stderr) sys.exit(1) if options.rx_freq is None: sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") parser.print_help(sys.stderr) sys.exit(1) #print "Receiving Packets in", options.rx_freq, "Frequncy" global tb tb = my_top_block(rx_callback, options) r = gr.enable_realtime_scheduling() if r != gr.RT_OK: print "Warning: failed to enable realtime scheduling" tb.start() tb.wait()
def main(): def send_pkt(payload='', eof=False): return tb.txpath.send_pkt(payload, eof) global n_rcvd, n_right n_rcvd = 0 n_right = 0 def rx_callback(ok, payload): global n_rcvd, n_right (pktno,) = struct.unpack('!H', payload[0:2]) n_rcvd += 1 if ok: n_right += 1 print "ok = %5s pktno = %4d n_rcvd = %4d n_right = %4d" % ( ok, pktno, n_rcvd, n_right) demods = modulation_utils.type_1_demods() mods = modulation_utils.type_1_mods() parser = OptionParser(option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") parser.add_option("-m", "--modulation", type="choice", choices=mods.keys(), default='gmsk', help="Select modulation from: %s [default=%%default]" % (', '.join(mods.keys()),)) parser.add_option("-s", "--size", type="eng_float", default=1500, help="set packet size [default=%default]") parser.add_option("-M", "--megabytes", type="eng_float", default=1.0, help="set megabytes to transmit [default=%default]") parser.add_option("","--discontinuous", action="store_true", default=False, help="enable discontinous transmission (bursts of 5 packets)") parser.add_option("","--from-file", default=None, help="use file for packet contents") usrp_transmit_path.add_options(parser, expert_grp) usrp_receive_path.add_options(parser, expert_grp) for mod in mods.values(): mod.add_options(expert_grp) for demod in demods.values(): demod.add_options(expert_grp) (options, args) = parser.parse_args () if len(args) != 0: parser.print_help() sys.exit(1) if options.tx_freq is None or options.rx_freq is None: sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") parser.print_help(sys.stderr) sys.exit(1) tb = my_top_block(demods[options.modulation], mods[options.modulation], rx_callback, options) r = gr.enable_realtime_scheduling() if r != gr.RT_OK: print 'Warning: Failed to enable realtime scheduling.' tb.start() # generate and send packets nbytes = int(1e6 * options.megabytes) n = 0 pktno = 0 pkt_size = int(options.size) while n < nbytes: if options.from_file is None: data = (pkt_size - 2) * chr(pktno & 0xff) else: data = source_file.read(pkt_size - 2) if data == '': break; payload = struct.pack('!H', pktno & 0xffff) + data send_pkt(payload) n += len(payload) sys.stderr.write('.') time.sleep(0.01) if options.discontinuous and pktno % 5 == 4: time.sleep(1) pktno += 1 send_pkt('',True) tb.wait()
def main(): global n_rcvd, n_right, shift, sync n_rcvd = 0 n_right = 0 shift = 0 sync = 1 no_packets = 50 global packet_file packet_file = open('/home/kranthi/rx.txt','w') def rx_callback(ok, payload): global n_rcvd, n_right, shift, parser, tb, sync (preamble,) = struct.unpack('!H', payload[2:4]) if (preamble == 11111): n_rcvd += 1 shift = 0 (pktno,) = struct.unpack('!H', payload[0:2]) if pktno == 20: (no_packets,) = struct.unpack('!H', payload[4:]) print no_packets no_packets += 20 elif pktno < 20: print elif (pktno > 70 and pktno < 150): print elif pktno >= 150: if sync == 1: print "\nReceived Synchronization packet in 920M Frequency" if ok: (freq,) = struct.unpack('!L', payload[4:])#int(payload[4:]) tb.rxpath.u.u.set_center_freq(freq,0) print "and shifting to ",freq," Frequency" sync = 0 else: packet_file.write(payload[4:]) if ok: n_right += 1 print "ok: %r \t pktno: %d \t n_rcvd: %d \t n_right: %d" % (ok, pktno, n_rcvd, n_right) else: shift += 1 if (shift >= 10): freq = 920*10**6 sync = 1 print "Shifting to 920M frequency" tb.rxpath.u.u.set_center_freq(freq,0) global parser parser = OptionParser(option_class=eng_option, conflict_handler="resolve") expert_grp = parser.add_option_group("Expert") parser.add_option("", "--snr", type="eng_float", default=30, help="set the SNR of the channel in dB [default=%default]") usrp_receive_path.add_options(parser, expert_grp) ofdm.ofdm_demod.add_options(parser, expert_grp) (options, args) = parser.parse_args () if len(args) != 0: parser.print_help(sys.stderr) sys.exit(1) if options.rx_freq is None: sys.stderr.write("You must specify -f FREQ or --freq FREQ\n") parser.print_help(sys.stderr) sys.exit(1) #print "Receiving Packets in", options.rx_freq, "Frequncy" global tb tb = my_top_block(rx_callback, options) r = gr.enable_realtime_scheduling() if r != gr.RT_OK: print "Warning: failed to enable realtime scheduling" tb.start() tb.wait()