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 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): while(1): try: msg = self.pop_msg_queue() except: return -1 if not pmt.pmt_is_blob(msg.value): continue self.post_msg(0, pmt.pmt_string_to_symbol(self.key), msg.value)
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 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):#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: a = 0 else: b = 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): 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): 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: a = 0 else: b = 0
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 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, ) 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 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 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