def work(self, input_items, output_items): while (1): #for simplicty, we'll loop. Blocks on self.pop call try: msg = self.pop_msg_queue() except: return -1 #test to make sure this is a blob if not pmt.pmt_is_blob(msg.value): continue if msg.offset == DATA_IN: #recall that a pmt includes a key, source, offset, and value key = pmt.pmt_symbol_to_string(msg.key) #print "Key: ",key #now lets get the actual data blob = pmt.pmt_blob_data(msg.value) if blob[0] == 0: tx_str = "Emitter off\n\r" else: tx_str = "Emitter detected\n\r" #print "Blob Value: ",blob.tostring() blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(tx_str)) pmt.pmt_blob_rw_data(blob)[:] = numpy.fromstring(tx_str, dtype='uint8') self.post_msg(0, pmt.pmt_string_to_symbol("n/a"), blob, pmt.pmt_string_to_symbol("rpt")) else: pkt_str = "I've seen an event" key_str = "event_report" src_str = "my_first_msg_block" blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(pkt_str)) pmt.pmt_blob_rw_data(blob)[:] = numpy.fromstring(pkt_str, dtype='uint8') self.post_msg(0, pmt.pmt_string_to_symbol(key_str), msg.value, pmt.pmt_string_to_symbol(src_str))
def work(self, input_items, output_items): """ For each message we receive, we generate for each of its bits a uniformly distributed random number and see if it is smaller than the threshold given by BER. """ while(1): try: msg = self.pop_msg_queue() except: return -1 if not pmt.pmt_is_blob(msg.value): print '[%s] not a blob' % self.name() continue if msg.offset == 0: data = pmt.pmt_blob_data(msg.value) for b in range(self.bits_per_byte): if random.random() < self.ber: data ^= (0x01 << b) blob = self.mgr.acquire(True) pmt.pmt_blob_resize(blob, len(data)) pmt.pmt_blob_rw_data(blob)[:] = data self.post_msg(0, pmt.pmt_string_to_symbol('U'), blob) else: print '[%s] w00t, weird msg offset' % self.name()
def send_pkt_radio_2(self,msg,pkt_cnt,dest,protocol_id,control): pkt_str = chr(pkt_cnt) + chr(self.addr) + chr(dest) + chr(protocol_id) + chr(control) + msg blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(pkt_str)) pmt.pmt_blob_rw_data(blob)[:] = numpy.fromstring(pkt_str, dtype='uint8') self.post_msg(0, pmt.pmt_string_to_symbol('U'), blob) return
def work(self, input_items, output_items): while(1): blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(self.value)) pmt.pmt_blob_rw_data(blob)[:] = numpy.fromstring(self.value,dtype="uint8") self.post_msg(0, pmt.pmt_string_to_symbol(self.key), blob) time.sleep(self.period)
def output_user_data(self,pkt): pkt_len = len(pkt) if (pkt_len > 5): payload = pkt[5:] blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(payload)) pmt.pmt_blob_rw_data(blob)[:] = payload self.post_msg(1, pmt.pmt_string_to_symbol('U'), blob)
def output_user_data(self, pkt): pkt_len = len(pkt) if (pkt_len > 5): payload = pkt[5:] blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(payload)) pmt.pmt_blob_rw_data(blob)[:] = payload self.post_msg(1, pmt.pmt_string_to_symbol('U'), blob)
def send_pkt_radio_2(self, msg, pkt_cnt, dest, protocol_id, control): pkt_str = chr(pkt_cnt) + chr( self.addr) + chr(dest) + chr(protocol_id) + chr(control) + msg blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(pkt_str)) pmt.pmt_blob_rw_data(blob)[:] = numpy.fromstring(pkt_str, dtype='uint8') self.post_msg(0, pmt.pmt_string_to_symbol('U'), blob) return
def work(self, input_items, output_items): while (1): blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(self.value)) pmt.pmt_blob_rw_data(blob)[:] = numpy.fromstring(self.value, dtype="uint8") self.post_msg(0, pmt.pmt_string_to_symbol(self.key), blob) time.sleep(self.period)
def tx_work(self, ser): while 1: if self.wait_for_newline: rx_buf = ser.readline() else: rx_buf = ser.read() blob = self.mgr.acquire(True) # block pmt.pmt_blob_resize(blob, len(rx_buf)) pmt.pmt_blob_rw_data(blob)[:] = numpy.fromstring(rx_buf, dtype="uint8") self.post_msg(0, pmt.pmt_string_to_symbol("serial"), blob)
def work(self, input_items, output_items): while(1):#for simplicty, we'll loop. Blocks on self.pop call try: msg = self.pop_msg_queue() except: return -1 #test to make sure this is a blob if not pmt.pmt_is_blob(msg.value): continue if msg.offset == DATA_IN: #recall that a pmt includes a key, source, offset, and value key = pmt.pmt_symbol_to_string(msg.key) #print "Key: ",key #now lets get the actual data blob = pmt.pmt_blob_data(msg.value) if blob[0] == 0: tx_str = "Emitter off\n\r" else: tx_str = "Emitter detected\n\r" #print "Blob Value: ",blob.tostring() blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(tx_str)) pmt.pmt_blob_rw_data(blob)[:] = numpy.fromstring(tx_str, dtype='uint8') self.post_msg(0,pmt.pmt_string_to_symbol("n/a"), blob, pmt.pmt_string_to_symbol("rpt") ) else: pkt_str = "I've seen an event" key_str = "event_report" src_str = "my_first_msg_block" blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(pkt_str)) pmt.pmt_blob_rw_data(blob)[:] = numpy.fromstring(pkt_str, dtype='uint8') self.post_msg(0, pmt.pmt_string_to_symbol(key_str), msg.value, pmt.pmt_string_to_symbol(src_str))
def tx_work(self, ser): while (1): if (self.wait_for_newline): rx_buf = ser.readline() else: rx_buf = ser.read() blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(rx_buf)) pmt.pmt_blob_rw_data(blob)[:] = numpy.fromstring(rx_buf, dtype='uint8') self.post_msg(0, pmt.pmt_string_to_symbol('serial'), blob)
def work(self, input_items, output_items): in0 = input_items[0] for x in range(len(in0)): if self.old_result != in0[x]: self.value = [in0[x]] self.trans_count += 1 blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(self.value)) pmt.pmt_blob_rw_data(blob)[:] = self.value self.post_msg(0, pmt.pmt_string_to_symbol("W"), blob) self.old_result = in0[x] self.consume(0, len(in0)) return 0
def work(self, input_items, output_items): while True: try: msg = self._msgq.delete_head() except: return -1 ok, payload = packet_utils.unmake_packet(msg.to_string(), int(msg.arg1())) if ok: payload = numpy.fromstring(payload, numpy.uint8) try: blob = self._mgr.acquire(True) #block except: return -1 pmt.pmt_blob_resize(blob, len(payload)) pmt.pmt_blob_rw_data(blob)[:] = payload self.post_msg(0, pmt.pmt_string_to_symbol("ok"), blob) else: a = 0
def work(self, input_items, output_items): while True: try: msg = self._msgq.delete_head() except: return -1 ok, payload = packet_utils.unmake_packet(msg.to_string(), int(msg.arg1()), False) #remove the whitening for channel estimation #print 'pkt deframer get a payload' self.display_pkt_stats(payload, ok) if ok: payload = numpy.fromstring(payload, numpy.uint8) try: blob = self._mgr.acquire(True) #block except: return -1 pmt.pmt_blob_resize(blob, len(payload)) pmt.pmt_blob_rw_data(blob)[:] = payload #self.post_msg(0, pmt.pmt_string_to_symbol("ok"), blob) else: a = 0
def work(self, input_items, output_items): while(1): try: msg = self.pop_msg_queue() except: return -1 if not pmt.pmt_is_blob(msg.value): print "not a blob - virtual mux" continue #insert header byte so demux can route to appropriate output data = numpy.concatenate([numpy.array([msg.offset]),pmt.pmt_blob_data(msg.value)]) blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(data)) pmt.pmt_blob_rw_data(blob)[:] = data self.post_msg(0,msg.key,blob,pmt.pmt_string_to_symbol('mux')) #pass incoming key for transparency
def work(self, input_items, output_items): while True: try: msg = self._msgq.delete_head() except: return -1 ok, payload = packet_utils.unmake_packet(msg.to_string(), int(msg.arg1())) if ok: payload = numpy.fromstring(payload, numpy.uint8) try: blob = self._mgr.acquire(True) #block except: return -1 pmt.pmt_blob_resize(blob, len(payload)) pmt.pmt_blob_rw_data(blob)[:] = payload self.post_msg(0, pmt.pmt_string_to_symbol("ok"), blob) else: pass
def work(self, input_items, output_items): while True: try: msg = self._msgq.delete_head() except: return -1 ok, payload = packet_utils.unmake_packet( msg.to_string(), int(msg.arg1()), False) #remove the whitening for channel estimation #print 'pkt deframer get a payload' self.display_pkt_stats(payload, ok) if ok: payload = numpy.fromstring(payload, numpy.uint8) try: blob = self._mgr.acquire(True) #block except: return -1 pmt.pmt_blob_resize(blob, len(payload)) pmt.pmt_blob_rw_data(blob)[:] = payload #self.post_msg(0, pmt.pmt_string_to_symbol("ok"), blob) else: a = 0
def work(self, input_items, output_items): while (1): try: msg = self.pop_msg_queue() except: return -1 if not pmt.pmt_is_blob(msg.value): print "not a blob - virtual mux" continue #insert header byte so demux can route to appropriate output data = numpy.concatenate( [numpy.array([msg.offset]), pmt.pmt_blob_data(msg.value)]) blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(data)) pmt.pmt_blob_rw_data(blob)[:] = data self.post_msg(0, msg.key, blob, pmt.pmt_string_to_symbol( 'mux')) #pass incoming key for transparency
def work(self, input_items, output_items): while(1):#for simplicty, we'll loop. Blocks on self.pop call try: msg = self.pop_msg_queue() except: return -1 #test to make sure this is a blob if not pmt.pmt_is_blob(msg.value): continue if msg.offset == DATA_IN: #recall that a pmt includes a key, source, offset, and value key = pmt.pmt_symbol_to_string(msg.key) print "Key: ",key #now lets get the actual data blob = pmt.pmt_blob_data(msg.value) print "Blob Value: ",blob.tostring() else: pkt_str = "I've seen an event" key_str = "event_report" src_str = "my_first_msg_block" blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(pkt_str)) pmt.pmt_blob_rw_data(blob)[:] = numpy.fromstring(pkt_str, dtype='uint8') self.post_msg(DATA_OUT, pmt.pmt_string_to_symbol(key_str), msg.value, pmt.pmt_string_to_symbol(src_str)) print self.freq_list[self.index] self.post_msg(CTRL_OUT,pmt.pmt_string_to_symbol('usrp_source.set_center_freq'),pmt.from_python( ( ( self.freq_list[self.index] , ), { } ) ),pmt.pmt_string_to_symbol('2nd_block')) self.index = ( self.index + 1 ) % self.freq_list_len
def work(self, input_items, output_items): while(1): try: msg = self.pop_msg_queue() except: return -1 if not pmt.pmt_is_blob(msg.value): print "not a blob - virtual demux" continue incoming_array = pmt.pmt_blob_data(msg.value) outgoing_array = incoming_array[1:] output_port = int(incoming_array[0]) #check to make sure the byte header is within our valid range if ( output_port > ( self.port_count - 1 ) ): print 'received msg outside of port range' continue blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(outgoing_array)) pmt.pmt_blob_rw_data(blob)[:] = outgoing_array self.post_msg(output_port,pmt.pmt_string_to_symbol('n/a'),blob,pmt.pmt_string_to_symbol('demux'))
def tx_frames(self): #send_sob #self.post_msg(TO_FRAMER_PORT, pmt.pmt_string_to_symbol('tx_sob'), pmt.PMT_T, pmt.pmt_string_to_symbol('tx_sob')) #get all of the packets we want to send total_byte_count = 0 frame_count = 0 #put residue from previous execution if self.has_old_msg: length = len(pmt.pmt_blob_data(self.old_msg.value)) + self.overhead total_byte_count += length self.tx_queue.put(self.old_msg) frame_count += 1 self.has_old_msg = False print 'old msg' #fill outgoing queue until empty or maximum bytes queued for slot while(not self.queue.empty()): msg = self.queue.get() length = len(pmt.pmt_blob_data(msg.value)) + self.overhead total_byte_count += length if total_byte_count >= self.bytes_per_slot: self.has_old_msg = True self.old_msg = msg print 'residue' continue else: self.has_old_msg = False self.tx_queue.put(msg) frame_count += 1 time_object = int(math.floor(self.antenna_start)),(self.antenna_start % 1) #if no data, send a single pad frame #TODO: add useful pad data, i.e. current time of SDR if frame_count == 0: data = self.pad_data more_frames = 0 tx_object = time_object,data,more_frames self.post_msg(TO_FRAMER_PORT,pmt.pmt_string_to_symbol('full'),pmt.from_python(tx_object),pmt.pmt_string_to_symbol('tdma')) else: #print frame_count,self.queue.qsize(), self.tx_queue.qsize() #send first frame w tuple for tx_time and number of frames to put in slot blob = self.mgr.acquire(True) #block more_frames = frame_count - 1 msg = self.tx_queue.get() data = pmt.pmt_blob_data(msg.value) tx_object = time_object,data,more_frames self.post_msg(TO_FRAMER_PORT,pmt.pmt_string_to_symbol('full'),pmt.from_python(tx_object),pmt.pmt_string_to_symbol('tdma')) frame_count -= 1 old_data = [] #print 'frame count: ',frame_count #send remining frames, blob only while(frame_count > 0): msg = self.tx_queue.get() data = pmt.pmt_blob_data(msg.value) blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(data)) pmt.pmt_blob_rw_data(blob)[:] = data self.post_msg(TO_FRAMER_PORT,pmt.pmt_string_to_symbol('d_only'),blob,pmt.pmt_string_to_symbol('tdma')) frame_count -= 1
def tx_frames(self): #send_sob #self.post_msg(TO_FRAMER_PORT, pmt.pmt_string_to_symbol('tx_sob'), pmt.PMT_T, pmt.pmt_string_to_symbol('tx_sob')) #get all of the packets we want to send total_byte_count = 0 frame_count = 0 #put residue from previous execution if self.has_old_msg: length = len(pmt.pmt_blob_data(self.old_msg.value)) + self.overhead total_byte_count += length self.tx_queue.put(self.old_msg) frame_count += 1 self.has_old_msg = False print 'old msg' #fill outgoing queue until empty or maximum bytes queued for slot while(not self.queue.empty()): msg = self.queue.get() length = len(pmt.pmt_blob_data(msg.value)) + self.overhead total_byte_count += length if total_byte_count >= self.bytes_per_slot: self.has_old_msg = True self.old_msg = msg print 'residue' continue else: self.has_old_msg = False self.tx_queue.put(msg) frame_count += 1 time_object = int(math.floor(self.antenna_start)),(self.antenna_start % 1) #if no data, send a single pad frame #TODO: add useful pad data, i.e. current time of SDR if frame_count == 0: #pad_d = struct.pack('!H', self.pktno & 0xffff) + (self.bytes_per_slot - 100) * chr(self.pktno & 0xff) #zeros = 64*chr(0x00) if self.initial_slot == 0: prefix = pn511s[0] else: prefix = pn511s[1] #for i in range(self.prefix_len): # if i == self.prefix_loc: # seg = zeros + pn511s[i] #put the PN code to the prefix # else: # seg = 128*chr(0x00) # the prefix looks like 0000000...0000PPPPPP...PPPP0000000.....000000 # |___512bit_||____512bit_||___M*1024bit_____| # M+N+1 := num_slots # N+1 := prefix_loc # prefix = prefix + seg rdata = '' if self.from_file == 1 and self.sfile != 0: rdata = self.sfile.read(self.bytes_per_slot - 64*self.prefix_len -100) if len(rdata) > 0: pad_d = rdata elif self.from_file == 2 and self.sfile != 0: #repeated sending the same packets for Virtual MIMO testing self.sfile.seek(0) #go the beginning of file rdata = self.sfile.read(self.bytes_per_slot - 64*self.prefix_len -100) if len(rdata) > 0: pad_d = rdata else: if self.initial_slot == 0: # use the unique PN code to specify the first slot pad_d = 16*pn511_0 #+ (self.bytes_per_slot - 64) * chr(self.pktno & 0xff) else: pad_d = 16*pn511_1 pad_d = prefix + pad_d #send PN and data at different slots for MIMO postmsg = True if self.mimo == True: postmsg = True if self.pktno % 3 == self.prefix_loc: pad_d = pn511_0 elif self.pktno % 3 == 2: pad_d = rdata else: postmsg = False data = numpy.fromstring(pad_d, dtype='uint8') #data = self.pad_data #data = pad_d more_frames = 0 tx_object = time_object,data,more_frames print 'prefix_loc = %d' %(self.prefix_loc) print 'antenna_start = %7f' %(self.antenna_start) if postmsg: self.post_msg(TO_FRAMER_PORT,pmt.pmt_string_to_symbol('full'),pmt.from_python(tx_object),pmt.pmt_string_to_symbol('tdma')) self.pktno += 1 #print 'tx_frames:post message from the pad data' else: #print frame_count,self.queue.qsize(), self.tx_queue.qsize() #send first frame w tuple for tx_time and number of frames to put in slot blob = self.mgr.acquire(True) #block more_frames = frame_count - 1 msg = self.tx_queue.get() data = pmt.pmt_blob_data(msg.value) tx_object = time_object,data,more_frames self.post_msg(TO_FRAMER_PORT,pmt.pmt_string_to_symbol('full'),pmt.from_python(tx_object),pmt.pmt_string_to_symbol('tdma')) frame_count -= 1 old_data = [] #print 'frame count: ',frame_count #send remining frames, blob only while(frame_count > 0): msg = self.tx_queue.get() data = pmt.pmt_blob_data(msg.value) blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(data)) pmt.pmt_blob_rw_data(blob)[:] = data self.post_msg(TO_FRAMER_PORT,pmt.pmt_string_to_symbol('d_only'),blob,pmt.pmt_string_to_symbol('tdma')) frame_count -= 1
def tx_frames(self): #send_sob #self.post_msg(TO_FRAMER_PORT, pmt.pmt_string_to_symbol('tx_sob'), pmt.PMT_T, pmt.pmt_string_to_symbol('tx_sob')) #get all of the packets we want to send total_byte_count = 0 frame_count = 0 #put residue from previous execution if self.has_old_msg: length = len(pmt.pmt_blob_data(self.old_msg.value)) + self.overhead total_byte_count += length self.tx_queue.put(self.old_msg) frame_count += 1 self.has_old_msg = False print 'old msg' #fill outgoing queue until empty or maximum bytes queued for slot while (not self.queue.empty()): msg = self.queue.get() length = len(pmt.pmt_blob_data(msg.value)) + self.overhead total_byte_count += length if total_byte_count >= self.bytes_per_slot: self.has_old_msg = True self.old_msg = msg print 'residue' continue else: self.has_old_msg = False self.tx_queue.put(msg) frame_count += 1 time_object = int(math.floor( self.antenna_start)), (self.antenna_start % 1) #if no data, send a single pad frame #TODO: add useful pad data, i.e. current time of SDR if frame_count == 0: data = self.pad_data more_frames = 0 tx_object = time_object, data, more_frames self.post_msg(TO_FRAMER_PORT, pmt.pmt_string_to_symbol('full'), pmt.from_python(tx_object), pmt.pmt_string_to_symbol('tdma')) else: #print frame_count,self.queue.qsize(), self.tx_queue.qsize() #send first frame w tuple for tx_time and number of frames to put in slot blob = self.mgr.acquire(True) #block more_frames = frame_count - 1 msg = self.tx_queue.get() data = pmt.pmt_blob_data(msg.value) tx_object = time_object, data, more_frames self.post_msg(TO_FRAMER_PORT, pmt.pmt_string_to_symbol('full'), pmt.from_python(tx_object), pmt.pmt_string_to_symbol('tdma')) frame_count -= 1 old_data = [] #print 'frame count: ',frame_count #send remining frames, blob only while (frame_count > 0): msg = self.tx_queue.get() data = pmt.pmt_blob_data(msg.value) blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(data)) pmt.pmt_blob_rw_data(blob)[:] = data self.post_msg(TO_FRAMER_PORT, pmt.pmt_string_to_symbol('d_only'), blob, pmt.pmt_string_to_symbol('tdma')) frame_count -= 1
def work(self, input_items, output_items): if self.rx_state == RX_INIT: self.rx_hop_index = 0 self.rx_state = RX_SEARCH self.post_msg( CTRL_PORT, pmt.pmt_string_to_symbol("usrp_source.set_center_freq"), pmt.from_python(((self.rx_freq_list[self.rx_hop_index],), {})), pmt.pmt_string_to_symbol("fhss"), ) self.rx_hop_index = (self.rx_hop_index + 1) % self.rx_freq_list_length print "Initialized to channel 0. Searching..." # check for msg inputs when work function is called if self.check_msg_queue(): try: msg = self.pop_msg_queue() except: return -1 if msg.offset == INCOMING_PKT_PORT: pkt = pmt.pmt_blob_data(msg.value) if pkt[0]: blob = self.mgr.acquire(True) # block pmt.pmt_blob_resize(blob, len(pkt) - 1) pmt.pmt_blob_rw_data(blob)[:] = pkt[1:] self.post_msg(APP_PORT, pmt.pmt_string_to_symbol("rx"), blob, pmt.pmt_string_to_symbol("fhss")) if self.know_time: if self.rx_state == RX_SEARCH: self.rx_state = RX_FOUND self.pkt_received = True self.next_tune_time = self.time_update + self.hop_interval - self.tune_lead self.start_hop = self.next_tune_time - self.lead_limit print "Received packet. Locked. Hopping initialized." else: self.pkt_received = True # print 'pkt_rcved_2',self.time_update,self.start_hop,self.next_tune_time else: a = 0 # CONTROL port # process streaming samples and tags here in0 = input_items[0] nread = self.nitems_read(0) # number of items read on port 0 ninput_items = len(input_items[0]) # read all tags associated with port 0 for items in this work function tags = self.get_tags_in_range(0, nread, nread + ninput_items) # lets find all of our tags, making the appropriate adjustments to our timing for tag in tags: key_string = pmt.pmt_symbol_to_string(tag.key) if key_string == "rx_time": self.samples_since_last_rx_time = 0 self.current_integer, self.current_fractional = pmt.to_python(tag.value) self.time_update = self.current_integer + self.current_fractional self.found_time = True elif key_string == "rx_rate": self.rate = pmt.to_python(tag.value) self.sample_period = 1 / self.rate self.found_rate = True # determine first transmit slot when we learn the time if not self.know_time: if self.found_time and self.found_rate: self.know_time = True else: # get current time self.time_update += self.sample_period * ninput_items if self.rx_state == RX_FOUND: if self.time_update > self.start_hop: # print 'set: ', self.rx_freq_list[self.rx_hop_index], self.time_update, self.next_tune_time self.post_msg( CTRL_PORT, pmt.pmt_string_to_symbol("usrp_source.set_command_time"), pmt.from_python(((self.next_tune_time,), {})), pmt.pmt_string_to_symbol("fhss"), ) self.post_msg( CTRL_PORT, pmt.pmt_string_to_symbol("usrp_source.set_center_freq"), pmt.from_python(((self.rx_freq_list[self.rx_hop_index],), {})), pmt.pmt_string_to_symbol("fhss"), ) self.post_msg( CTRL_PORT, pmt.pmt_string_to_symbol("usrp_source.clear_command_time"), pmt.from_python(((0,), {})), pmt.pmt_string_to_symbol("fhss"), ) self.rx_hop_index = (self.rx_hop_index + 1) % self.rx_freq_list_length self.start_hop += self.hop_interval self.next_tune_time += self.hop_interval # self.next_rx_interval += self.hop_interval - self.tune_lead if self.pkt_received: self.consecutive_miss = 0 else: self.consecutive_miss += 1 if self.consecutive_miss > LOST_SYNC_THRESHOLD: self.consecutive_miss = 0 self.rx_state = RX_INIT print "Lost Sync: Re-Initializing" self.pkt_received = False return ninput_items
def work(self, input_items, output_items): if self.rx_state == RX_INIT: self.rx_hop_index = 0 self.rx_state = RX_SEARCH self.post_msg( CTRL_PORT, pmt.pmt_string_to_symbol('usrp_source.set_center_freq'), pmt.from_python( ((self.rx_freq_list[self.rx_hop_index], ), {})), pmt.pmt_string_to_symbol('fhss')) self.rx_hop_index = (self.rx_hop_index + 1) % self.rx_freq_list_length print 'Initialized to channel 0. Searching...' #check for msg inputs when work function is called if self.check_msg_queue(): try: msg = self.pop_msg_queue() except: return -1 if msg.offset == INCOMING_PKT_PORT: pkt = pmt.pmt_blob_data(msg.value) if pkt[0]: blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(pkt) - 1) pmt.pmt_blob_rw_data(blob)[:] = pkt[1:] self.post_msg(APP_PORT, pmt.pmt_string_to_symbol('rx'), blob, pmt.pmt_string_to_symbol('fhss')) if self.know_time: if self.rx_state == RX_SEARCH: self.rx_state = RX_FOUND self.pkt_received = True self.next_tune_time = self.time_update + self.hop_interval - self.tune_lead self.start_hop = self.next_tune_time - self.lead_limit print 'Received packet. Locked. Hopping initialized.' else: self.pkt_received = True #print 'pkt_rcved_2',self.time_update,self.start_hop,self.next_tune_time else: a = 0 #CONTROL port #process streaming samples and tags here in0 = input_items[0] nread = self.nitems_read(0) #number of items read on port 0 ninput_items = len(input_items[0]) #read all tags associated with port 0 for items in this work function tags = self.get_tags_in_range(0, nread, nread + ninput_items) #lets find all of our tags, making the appropriate adjustments to our timing for tag in tags: key_string = pmt.pmt_symbol_to_string(tag.key) if key_string == "rx_time": self.samples_since_last_rx_time = 0 self.current_integer, self.current_fractional = pmt.to_python( tag.value) self.time_update = self.current_integer + self.current_fractional self.found_time = True elif key_string == "rx_rate": self.rate = pmt.to_python(tag.value) self.sample_period = 1 / self.rate self.found_rate = True #determine first transmit slot when we learn the time if not self.know_time: if self.found_time and self.found_rate: self.know_time = True else: #get current time self.time_update += (self.sample_period * ninput_items) if self.rx_state == RX_FOUND: if self.time_update > self.start_hop: #print 'set: ', self.rx_freq_list[self.rx_hop_index], self.time_update, self.next_tune_time self.post_msg( CTRL_PORT, pmt.pmt_string_to_symbol('usrp_source.set_command_time'), pmt.from_python(((self.next_tune_time, ), {})), pmt.pmt_string_to_symbol('fhss')) self.post_msg( CTRL_PORT, pmt.pmt_string_to_symbol('usrp_source.set_center_freq'), pmt.from_python( ((self.rx_freq_list[self.rx_hop_index], ), {})), pmt.pmt_string_to_symbol('fhss')) self.post_msg( CTRL_PORT, pmt.pmt_string_to_symbol('usrp_source.clear_command_time'), pmt.from_python(((0, ), {})), pmt.pmt_string_to_symbol('fhss')) self.rx_hop_index = (self.rx_hop_index + 1) % self.rx_freq_list_length self.start_hop += self.hop_interval self.next_tune_time += self.hop_interval #self.next_rx_interval += self.hop_interval - self.tune_lead if self.pkt_received: self.consecutive_miss = 0 else: self.consecutive_miss += 1 if self.consecutive_miss > LOST_SYNC_THRESHOLD: self.consecutive_miss = 0 self.rx_state = RX_INIT print 'Lost Sync: Re-Initializing' self.pkt_received = False return ninput_items
def tx_frames(self): #send_sob #self.post_msg(TO_FRAMER_PORT, pmt.pmt_string_to_symbol('tx_sob'), pmt.PMT_T, pmt.pmt_string_to_symbol('tx_sob')) #get all of the packets we want to send total_byte_count = 0 frame_count = 0 #put residue from previous execution if self.has_old_msg: length = len(pmt.pmt_blob_data(self.old_msg.value)) + self.overhead total_byte_count += length self.tx_queue.put(self.old_msg) frame_count += 1 self.has_old_msg = False print 'old msg' #fill outgoing queue until empty or maximum bytes queued for slot while (not self.queue.empty()): msg = self.queue.get() length = len(pmt.pmt_blob_data(msg.value)) + self.overhead total_byte_count += length if total_byte_count >= self.bytes_per_slot: self.has_old_msg = True self.old_msg = msg print 'residue' continue else: self.has_old_msg = False self.tx_queue.put(msg) frame_count += 1 time_object = int(math.floor( self.antenna_start)), (self.antenna_start % 1) #if no data, send a single pad frame #TODO: add useful pad data, i.e. current time of SDR if frame_count == 0: #pad_d = struct.pack('!H', self.pktno & 0xffff) + (self.bytes_per_slot - 100) * chr(self.pktno & 0xff) #zeros = 64*chr(0x00) if self.initial_slot == 0: prefix = pn511s[0] else: prefix = pn511s[1] #for i in range(self.prefix_len): # if i == self.prefix_loc: # seg = zeros + pn511s[i] #put the PN code to the prefix # else: # seg = 128*chr(0x00) # the prefix looks like 0000000...0000PPPPPP...PPPP0000000.....000000 # |___512bit_||____512bit_||___M*1024bit_____| # M+N+1 := num_slots # N+1 := prefix_loc # prefix = prefix + seg rdata = '' if self.from_file == 1 and self.sfile != 0: rdata = self.sfile.read(self.bytes_per_slot - 64 * self.prefix_len - 100) if len(rdata) > 0: pad_d = rdata elif self.from_file == 2 and self.sfile != 0: #repeated sending the same packets for Virtual MIMO testing self.sfile.seek(0) #go the beginning of file rdata = self.sfile.read(self.bytes_per_slot - 64 * self.prefix_len - 100) if len(rdata) > 0: pad_d = rdata else: if self.initial_slot == 0: # use the unique PN code to specify the first slot pad_d = 16 * pn511_0 #+ (self.bytes_per_slot - 64) * chr(self.pktno & 0xff) else: pad_d = 16 * pn511_1 pad_d = prefix + pad_d #send PN and data at different slots for MIMO postmsg = True if self.mimo == True: postmsg = True if self.pktno % 3 == self.prefix_loc: pad_d = pn511_0 elif self.pktno % 3 == 2: pad_d = rdata else: postmsg = False data = numpy.fromstring(pad_d, dtype='uint8') #data = self.pad_data #data = pad_d more_frames = 0 tx_object = time_object, data, more_frames print 'prefix_loc = %d' % (self.prefix_loc) print 'antenna_start = %7f' % (self.antenna_start) if postmsg: self.post_msg(TO_FRAMER_PORT, pmt.pmt_string_to_symbol('full'), pmt.from_python(tx_object), pmt.pmt_string_to_symbol('tdma')) self.pktno += 1 #print 'tx_frames:post message from the pad data' else: #print frame_count,self.queue.qsize(), self.tx_queue.qsize() #send first frame w tuple for tx_time and number of frames to put in slot blob = self.mgr.acquire(True) #block more_frames = frame_count - 1 msg = self.tx_queue.get() data = pmt.pmt_blob_data(msg.value) tx_object = time_object, data, more_frames self.post_msg(TO_FRAMER_PORT, pmt.pmt_string_to_symbol('full'), pmt.from_python(tx_object), pmt.pmt_string_to_symbol('tdma')) frame_count -= 1 old_data = [] #print 'frame count: ',frame_count #send remining frames, blob only while (frame_count > 0): msg = self.tx_queue.get() data = pmt.pmt_blob_data(msg.value) blob = self.mgr.acquire(True) #block pmt.pmt_blob_resize(blob, len(data)) pmt.pmt_blob_rw_data(blob)[:] = data self.post_msg(TO_FRAMER_PORT, pmt.pmt_string_to_symbol('d_only'), blob, pmt.pmt_string_to_symbol('tdma')) frame_count -= 1