def rx_work(self, ser): while 1: try: msg = self.pop_msg_queue() except: return -1 if not pmt.pmt_is_blob(msg.value): continue blob = pmt.pmt_blob_data(msg.value) tx_string = pmt.pmt_blob_data(msg.value).tostring() ser.write(tx_string)
def rx_work(self, ser): while (1): try: msg = self.pop_msg_queue() except: return -1 if not pmt.pmt_is_blob(msg.value): continue blob = pmt.pmt_blob_data(msg.value) tx_string = pmt.pmt_blob_data(msg.value).tostring() ser.write(tx_string)
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 self.addr print "not a blob - simple mac" continue self.key_string = pmt.pmt_symbol_to_string(msg.key) self.value = pmt.pmt_blob_data(msg.value) if(msg.offset == 0): if (len(self.value) > 0): if(self.value[0] == 0): self.channel_state = READY else: self.channel_state = NOT_READY if(msg.offset == 1): self.q.put(msg) if(self.channel_state == NOT_READY): print 'hold back' if (self.channel_state == READY): while not self.q.empty(): self.q.get() self.post_msg(0, msg)
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 self.addr print "not a blob - simple mac" continue self.key_string = pmt.pmt_symbol_to_string(msg.key) self.value = pmt.pmt_blob_data(msg.value) if (msg.offset == 0): if (len(self.value) > 0): if (self.value[0] == 0): self.channel_state = READY else: self.channel_state = NOT_READY if (msg.offset == 1): self.q.put(msg) if (self.channel_state == NOT_READY): print 'hold back' if (self.channel_state == READY): while not self.q.empty(): self.q.get() self.post_msg(0, msg)
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(self, msg, pkt_cnt, dest, protocol_id, control): pkt_str = chr(pkt_cnt) + chr(self.addr) + chr(dest) + chr( protocol_id) + chr(control) + pmt.pmt_blob_data( msg.value).tostring() 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): try: msg = self.pop_msg_queue() except: return -1 print if not pmt.pmt_is_blob(msg.value): print "BTW, this not a blob" print "Key: ",pmt.pmt_symbol_to_string(msg.key),"Value: ",pmt.pmt_symbol_to_string(msg.key) else: print "Key: ",pmt.pmt_symbol_to_string(msg.key),"Value: ",pmt.pmt_blob_data(msg.value).tostring()
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): 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): try: msg = self.pop_msg_queue() except: return -1 print if not pmt.pmt_is_blob(msg.value): print "BTW, this not a blob" print "Key: ", pmt.pmt_symbol_to_string( msg.key), "Value: ", pmt.pmt_symbol_to_string(msg.key) else: print "Key: ", pmt.pmt_symbol_to_string( msg.key), "Value: ", pmt.pmt_blob_data( msg.value).tostring()
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): continue blob = pmt.pmt_blob_data(msg.value) if self.arq == ARQ_TRUE: arq_char = 'U' else: arq_char = 'V' key_str = arq_char + chr(self.channel) self.post_msg(0, pmt.pmt_string_to_symbol(key_str), msg.value)
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): while not len(self._pkt): try: msg = self.pop_msg_queue() except: return -1 if not pmt.pmt_is_blob(msg.value): continue pkt = packet_utils.make_packet( pmt.pmt_blob_data(msg.value).tostring(), self._samples_per_symbol, self._bits_per_symbol, self._access_code, False, #pad_for_usrp, self._whitener_offset, ) self._pkt = numpy.fromstring(pkt, numpy.uint8) if self._use_whitener_offset: self._whitener_offset = (self._whitener_offset + 1) % 16 num_items = min(len(self._pkt), len(output_items[0])) output_items[0][:num_items] = self._pkt[:num_items] self._pkt = self._pkt[num_items:] #residue for next work() return num_items
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: 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 send_pkt_radio(self,msg,pkt_cnt,dest,protocol_id,control): pkt_str = chr(pkt_cnt) + chr(self.addr) + chr(dest) + chr(protocol_id) + chr(control) + pmt.pmt_blob_data(msg.value).tostring() 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 not len(self._pkt): try: msg = self.pop_msg_queue() except: return -1 if not pmt.pmt_is_blob(msg.value): self.tx_time,data,self.more_frame_cnt = pmt.to_python(msg.value) self.has_tx_time = True #print data #print tx_time #print data.tostring() else: data = pmt.pmt_blob_data(msg.value) #print data self.has_tx_time = False pkt = packet_utils.make_packet( data.tostring(), self._samples_per_symbol, self._bits_per_symbol, self._access_code, False, #pad_for_usrp, self._whitener_offset, ) pkt += "".join(map(chr, [0x55] * 64)) self._pkt = numpy.fromstring(pkt, numpy.uint8) if self._use_whitener_offset: self._whitener_offset = (self._whitener_offset + 1) % 16 #shouldn't really need to send start of burst #only need to do sob if looking for timed transactions num_items = min(len(self._pkt), len(output_items[0])) output_items[0][:num_items] = self._pkt[:num_items] self._pkt = self._pkt[num_items:] #residue for next work() if len(self._pkt) == 0 : item_index = num_items #which output item gets the tag? offset = self.nitems_written(0) + item_index source = pmt.pmt_string_to_symbol("framer") #print 'frame cnt',self.more_frame_cnt if self.has_tx_time: key = pmt.pmt_string_to_symbol("tx_sob") self.add_item_tag(0, self.nitems_written(0), key, pmt.PMT_T, source) key = pmt.pmt_string_to_symbol("tx_time") self.add_item_tag(0, self.nitems_written(0), key, pmt.from_python(self.tx_time), source) #if self.keep: # print 'bad order' #self.keep = True if self.more_frame_cnt == 0: key = pmt.pmt_string_to_symbol("tx_eob") self.add_item_tag(0, offset - 1, key, pmt.PMT_T, source) #if self.keep: # print 'good order' #self.keep = False else: self.more_frame_cnt -= 1 return num_items
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 self.addr print "not a blob - simple mac" continue self.key_string = pmt.pmt_symbol_to_string(msg.key) #receive data from application port to be transmitter to radio if msg.offset == APP_PORT: if(ord(self.key_string[KEY_INDEX_CTRL]) == ARQ_REQ): self.queue.put(msg) if(ord(self.key_string[KEY_INDEX_CTRL]) == ARQ_NO_REQ): self.tx_no_arq(msg,ord(self.key_string[KEY_INDEX_DEST_ADDR]),USER_IO_PROTOCOL_ID) #TODO: Oh my goodness, this section is ugly. I need to clean this. #process the packet received on radio if(msg.offset == RADIO_PORT): incoming_pkt = pmt.pmt_blob_data(msg.value) #get data if ( len(incoming_pkt) > 5 ): #check for weird header only stuff if( ( incoming_pkt[PKT_INDEX_DEST] == self.addr or incoming_pkt[PKT_INDEX_DEST] == 255) and not incoming_pkt[PKT_INDEX_SRC] == self.addr): #for us? #check to see if we must ACK this packet if(incoming_pkt[PKT_INDEX_CTRL] == ARQ_REQ): #TODO, stuff CTRL and Protocol in one field self.send_ack(incoming_pkt,incoming_pkt[PKT_INDEX_SRC],incoming_pkt[PKT_INDEX_CNT]) #Then send ACK then if not (self.arq_expected_sequence_number == incoming_pkt[PKT_INDEX_CNT]): self.arq_sequence_error_cnt += 1 self.throw_away = True else: self.throw_away = False self.arq_expected_sequence_number = ( incoming_pkt[PKT_INDEX_CNT] + 1 ) % 255 else: if not (self.no_arq_expected_sequence_number == incoming_pkt[PKT_INDEX_CNT]): self.no_arq_sequence_error_cnt += 1 #print self.no_arq_sequence_error_cnt #print self.no_arq_sequence_error_cnt,incoming_pkt[PKT_INDEX_CNT],self.no_arq_expected_sequence_number self.no_arq_expected_sequence_number = ( incoming_pkt[PKT_INDEX_CNT] + 1 ) % 255 incoming_protocol_id = incoming_pkt[PKT_INDEX_PROT_ID] #check to see if this is an ACK packet if(incoming_protocol_id == ARQ_PROTOCOL_ID): if incoming_pkt[5] == self.expected_arq_id: self.arq_channel_state = ARQ_CHANNEL_IDLE self.pkt_cnt_arq = ( self.pkt_cnt_arq + 1 ) % 255 else: print 'received out of sequence ack',incoming_pkt[5],self.expected_arq_id #do something with incoming user data elif(incoming_protocol_id == USER_IO_PROTOCOL_ID): if not self.throw_away: self.output_user_data(incoming_pkt) self.throw_away = False else: print 'unknown protocol' if(msg.offset == CTRL_PORT): if(self.key_string[KEY_INT_MSG_TYPE] == HEARTBEAT): #TODO: something useful when we receive heartbeat? a = '0' #check to see if we have any outgoing messages from arq buffer we should send #or pending re-transmissions if self.arq_channel_state == ARQ_CHANNEL_IDLE: #channel ready for next arq msg if not self.queue.empty(): #we have an arq msg to send, so lets send it #print self.queue.qsize() self.outgoing_msg = self.queue.get() #get msg self.expected_arq_id = self.pkt_cnt_arq #store it for re-use self.key_string = pmt.pmt_symbol_to_string(self.outgoing_msg.key) #pull key self.tx_arq(self.outgoing_msg,ord(self.key_string[KEY_INDEX_DEST_ADDR]),USER_IO_PROTOCOL_ID) #transmit it self.time_of_tx = time.time() # note time for arq timeout recognition self.arq_channel_state = ARQ_CHANNEL_BUSY #remember that the channel is busy self.arq_pkts_txed += 1 self.retries = 0 else: #if channel is busy, lets check to see if its time to re-transmit if ( time.time() - self.time_of_tx ) > self.timeout: #check for ack timeout if self.retries == self.max_attempts: #know when to quit self.retries = 0 self.arq_channel_state = ARQ_CHANNEL_IDLE self.failed_arq += 1 self.pkt_cnt_arq = ( self.pkt_cnt_arq + 1 ) % 255 #start on next pkt print 'pkt failed arq' else: self.key_string = pmt.pmt_symbol_to_string(self.outgoing_msg.key) #reset key self.tx_arq(self.outgoing_msg,ord(self.key_string[KEY_INDEX_DEST_ADDR]),USER_IO_PROTOCOL_ID) #tx again self.time_of_tx = time.time() self.arq_retxed += 1 self.retries += 1
def work(self, input_items, output_items): while not len(self._pkt): try: msg = self.pop_msg_queue() except: return -1 if not pmt.pmt_is_blob(msg.value): self.tx_time, data, self.more_frame_cnt = pmt.to_python( msg.value) self.has_tx_time = True #print data #print tx_time #print data.tostring() else: data = pmt.pmt_blob_data(msg.value) #print data self.has_tx_time = False pkt = packet_utils.make_packet( data.tostring(), self._samples_per_symbol, self._bits_per_symbol, self._access_code, False, #pad_for_usrp, self._whitener_offset, ) self._pkt = numpy.fromstring(pkt, numpy.uint8) if self._use_whitener_offset: self._whitener_offset = (self._whitener_offset + 1) % 16 #shouldn't really need to send start of burst #only need to do sob if looking for timed transactions num_items = min(len(self._pkt), len(output_items[0])) output_items[0][:num_items] = self._pkt[:num_items] self._pkt = self._pkt[num_items:] #residue for next work() if len(self._pkt) == 0: item_index = num_items #which output item gets the tag? offset = self.nitems_written(0) + item_index source = pmt.pmt_string_to_symbol("framer") #print 'frame cnt',self.more_frame_cnt if self.has_tx_time: key = pmt.pmt_string_to_symbol("tx_sob") self.add_item_tag(0, self.nitems_written(0), key, pmt.PMT_T, source) key = pmt.pmt_string_to_symbol("tx_time") self.add_item_tag(0, self.nitems_written(0), key, pmt.from_python(self.tx_time), source) #if self.keep: # print 'bad order' #self.keep = True if self.more_frame_cnt == 0: key = pmt.pmt_string_to_symbol("tx_eob") self.add_item_tag(0, offset - 1, key, pmt.PMT_T, source) #if self.keep: # print 'good order' #self.keep = False else: self.more_frame_cnt -= 1 return num_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
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 self.addr print "not a blob - simple mac" continue self.key_string = pmt.pmt_symbol_to_string(msg.key) #receive data from application port to be transmitter to radio if msg.offset == APP_PORT: if (ord(self.key_string[KEY_INDEX_CTRL]) == ARQ_REQ): self.queue.put(msg) if (ord(self.key_string[KEY_INDEX_CTRL]) == ARQ_NO_REQ): self.tx_no_arq(msg, ord(self.key_string[KEY_INDEX_DEST_ADDR]), USER_IO_PROTOCOL_ID) #TODO: Oh my goodness, this section is ugly. I need to clean this. #process the packet received on radio if (msg.offset == RADIO_PORT): incoming_pkt = pmt.pmt_blob_data(msg.value) #get data if (len(incoming_pkt) > 5): #check for weird header only stuff if ((incoming_pkt[PKT_INDEX_DEST] == self.addr or incoming_pkt[PKT_INDEX_DEST] == 255) and not incoming_pkt[PKT_INDEX_SRC] == self.addr): #for us? #check to see if we must ACK this packet if (incoming_pkt[PKT_INDEX_CTRL] == ARQ_REQ ): #TODO, stuff CTRL and Protocol in one field self.send_ack(incoming_pkt, incoming_pkt[PKT_INDEX_SRC], incoming_pkt[PKT_INDEX_CNT] ) #Then send ACK then if not (self.arq_expected_sequence_number == incoming_pkt[PKT_INDEX_CNT]): self.arq_sequence_error_cnt += 1 self.throw_away = True else: self.throw_away = False self.arq_expected_sequence_number = ( incoming_pkt[PKT_INDEX_CNT] + 1) % 255 else: if not (self.no_arq_expected_sequence_number == incoming_pkt[PKT_INDEX_CNT]): self.no_arq_sequence_error_cnt += 1 #print self.no_arq_sequence_error_cnt #print self.no_arq_sequence_error_cnt,incoming_pkt[PKT_INDEX_CNT],self.no_arq_expected_sequence_number self.no_arq_expected_sequence_number = ( incoming_pkt[PKT_INDEX_CNT] + 1) % 255 incoming_protocol_id = incoming_pkt[PKT_INDEX_PROT_ID] #check to see if this is an ACK packet if (incoming_protocol_id == ARQ_PROTOCOL_ID): if incoming_pkt[5] == self.expected_arq_id: self.arq_channel_state = ARQ_CHANNEL_IDLE self.pkt_cnt_arq = (self.pkt_cnt_arq + 1) % 255 else: print 'received out of sequence ack', incoming_pkt[ 5], self.expected_arq_id #do something with incoming user data elif (incoming_protocol_id == USER_IO_PROTOCOL_ID): if not self.throw_away: self.output_user_data(incoming_pkt) self.throw_away = False else: print 'unknown protocol' if (msg.offset == CTRL_PORT): if (self.key_string[KEY_INT_MSG_TYPE] == HEARTBEAT): #TODO: something useful when we receive heartbeat? a = '0' #check to see if we have any outgoing messages from arq buffer we should send #or pending re-transmissions if self.arq_channel_state == ARQ_CHANNEL_IDLE: #channel ready for next arq msg if not self.queue.empty( ): #we have an arq msg to send, so lets send it #print self.queue.qsize() self.outgoing_msg = self.queue.get() #get msg self.expected_arq_id = self.pkt_cnt_arq #store it for re-use self.key_string = pmt.pmt_symbol_to_string( self.outgoing_msg.key) #pull key self.tx_arq(self.outgoing_msg, ord(self.key_string[KEY_INDEX_DEST_ADDR]), USER_IO_PROTOCOL_ID) #transmit it self.time_of_tx = time.time( ) # note time for arq timeout recognition self.arq_channel_state = ARQ_CHANNEL_BUSY #remember that the channel is busy self.arq_pkts_txed += 1 self.retries = 0 else: #if channel is busy, lets check to see if its time to re-transmit if (time.time() - self.time_of_tx ) > self.timeout: #check for ack timeout if self.retries == self.max_attempts: #know when to quit self.retries = 0 self.arq_channel_state = ARQ_CHANNEL_IDLE self.failed_arq += 1 self.pkt_cnt_arq = (self.pkt_cnt_arq + 1) % 255 #start on next pkt print 'pkt failed arq' else: self.key_string = pmt.pmt_symbol_to_string( self.outgoing_msg.key) #reset key self.tx_arq(self.outgoing_msg, ord(self.key_string[KEY_INDEX_DEST_ADDR]), USER_IO_PROTOCOL_ID) #tx again self.time_of_tx = time.time() self.arq_retxed += 1 self.retries += 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