def __init__( self, ): """ The input is a stream of bytes. Outputs a blob with value of current byte if it is different from last. """ gr.block.__init__( self, name = "transition_detect", in_sig = [numpy.uint8], out_sig = None, has_msg_input = False, num_msg_outputs = 1, ) self.mgr = pmt.pmt_mgr() for i in range(64): self.mgr.set(pmt.pmt_make_blob(10000)) self.old_result = 0; self.key = pmt.pmt_string_to_symbol("example_key") self.value = pmt.pmt_string_to_symbol("example_value") self.trans_count = 0
def generate_tag(self, tag_key, srcid, value, offset): tag = gr.gr_tag_t() tag.key = pmt.pmt_string_to_symbol(tag_key) tag.srcid = pmt.pmt_string_to_symbol(srcid) tag.value = pmt.pmt_from_long(value) tag.offset = offset return tag
def get_tag_list(data_len, tag_key, N_ofdm_symbols): tag_list = [] for i in range(data_len): tag = gr.gr_tag_t() tag.key = pmt.pmt_string_to_symbol(tag_key) tag.srcid = pmt.pmt_string_to_symbol("test_src") tag.value = pmt.pmt_from_long(i%N_ofdm_symbols) tag.offset = i tag_list.append(tag) return tag_list
def work(self, input_items, output_items): # g = open('./work.txt', 'ab') # g.write("CustomTwoChannelThreshold start\n") # g.close ninput_items = min( len(input_items[0]), len(input_items[1]), len(output_items[0]), len(output_items[1]) ) # input1 = np.array(input_items[0][:ninput_items], dtype=np.int16) # input2 = np.array(input_items[1][:ninput_items], dtype=np.int16) input1 = input_items[0] input2 = input_items[1] noutput_items = 0 for ii in xrange(ninput_items) : if np.abs(input1[ii]) > self.Threshold_val or np.abs(input2[ii]) > self.Threshold_val : # f = open('./Th_temp.txt', 'ab') # f.write('ii: %d, th_val: %d, val1: %d, val2: %d\n'%(ii, self.Threshold_val, input1[ii], input2[ii])) # f.close() if self.WO_counter == 0 : offset1 = self.nitems_written(0) + noutput_items offset2 = self.nitems_written(1) + noutput_items key = pmt.pmt_string_to_symbol( "Threshold event" ) value = pmt.pmt_string_to_symbol( "" ) self.add_item_tag(0, offset1, key, value) self.add_item_tag(1, offset2, key, value) self.WO_counter = self.WashOutLength if self.WO_counter : self.WO_counter -= 1 # output_items[0][noutput_items:noutput_items+1] = [input1[ii]] # output_items[1][noutput_items:noutput_items+1] = [input2[ii]] output_items[0][noutput_items] = input1[ii] output_items[1][noutput_items] = input2[ii] noutput_items += 1 self.consume_each(ninput_items) #or shortcut to consume on all inputs # self.cum += ninput_items # g = open('./work.txt', 'ab') # g.write("CustomTwoChannelThreshold stop, in: %d, total: %d, out: %d\n"%(ninput_items, self.cum, noutput_items)) # g.close return noutput_items
def generate_rx_tags(self): #Produce tags offset = self.nitems_written(0) + 0 key_time = pmt.pmt_string_to_symbol("rx_time") #value_time = pmt.from_python(1.0 / #self.samp_rate * self.virtual_counter) value_time = pmt.from_python(self.get_time_now()) key_rate = pmt.pmt_string_to_symbol("rx_rate") value_rate = pmt.from_python(self.samp_rate) self.add_item_tag(0, offset, key_time, value_time) self.add_item_tag(0, offset, key_rate, value_rate)
def work(self, input_items, output_items): #print "DEBUG: Heartbeat work called!" #while(self.counter < self.counter_treshold): while(1): #print "DEBUG: Heartbeat - new run, run no %s" % self.counter #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), pmt.pmt_string_to_symbol(self.value)) # blob) #self.post_msg(0, pmt.pmt_string_to_symbol(self.key), blob) time.sleep(self.period)
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: self.post_msg(0, pmt.pmt_string_to_symbol("fail"), pmt.PMT_NIL)
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 test_001_t (self): """ pretty simple """ fft_len = 6 tx_symbols = (1, 2, 3) pilot_symbols = ((1j,),) occupied_carriers = ((0, 1, 2),) pilot_carriers = ((3,),) expected_result = (1, 2, 3, 1j, 0, 0) mtu = 128 tag_name = "len" tag = gr.gr_tag_t() tag.offset = 0 tag.key = pmt.pmt_string_to_symbol(tag_name) tag.value = pmt.pmt_from_long(len(tx_symbols)) src = gr.vector_source_c(tx_symbols, (tag,), False, 1) alloc = ofdm.carrier_allocator_cvc(fft_len, occupied_carriers, pilot_carriers, pilot_symbols, tag_name) sink = gr.vector_sink_c(fft_len) self.tb.connect(src, alloc, sink) self.tb.run () self.assertEqual(sink.data(), expected_result)
def test_001_t (self): """ more advanced: - 6 symbols per carrier - 2 pilots per carrier - have enough data for nearly 3 OFDM symbols """ tx_symbols = range(1, 16); pilot_symbols = ((1j, 2j), (3j, 4j)) occupied_carriers = ((1, 3, 4, 11, 12, 14), (1, 2, 4, 11, 13, 14),) pilot_carriers = ((2, 13), (3, 12)) expected_result = (0, 1, 1j, 2, 3, 0, 0, 0, 0, 0, 0, 4, 5, 2j, 6, 0, 0, 7, 8, 3j, 9, 0, 0, 0, 0, 0, 0, 10, 4j, 11, 12, 0, 0, 13, 1j, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 2j, 0, 0) fft_len = 16 mtu = 4096 tag_name = "len" tag = gr.gr_tag_t() tag.offset = 0 tag.key = pmt.pmt_string_to_symbol(tag_name) tag.value = pmt.pmt_from_long(len(tx_symbols)) src = gr.vector_source_c(tx_symbols, (tag,), False, 1) alloc = ofdm.carrier_allocator_cvc(fft_len, occupied_carriers, pilot_carriers, pilot_symbols, tag_name) sink = gr.vector_sink_c(fft_len) self.tb.connect(src, alloc, sink) self.tb.run () self.assertEqual(sink.data(), expected_result)
def test_001_t (self): # set up fg src = gr.file_source(2, "/dev/urandom"); ann = gr.annotator_raw(2); head = gr.head(2, 100000); snk = pmtfile.sink("test1.pf", pmtfile.pmtfile.RI16); tb = gr.top_block(); # connections tb.connect(src,ann,head,snk); # add a test tag via the annotator ann.add_tag(0,pmt.pmt_string_to_symbol("test"), pmt.pmt_string_to_symbol("testval")); # run the graph tb.run ()
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 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 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 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 make_lengthtags(lengths, offsets, tagname='length', vlen=1): tags = [] assert(len(offsets) == len(lengths)) for offset, length in zip(offsets, lengths): tag = gr.gr_tag_t() tag.offset = offset/vlen tag.key = pmt.pmt_string_to_symbol(tagname) tag.value = pmt.pmt_from_long(length/vlen) tags.append(tag) return tags
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): 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 evaluate_timestamps(self, nread, ninput_items): tags = self.get_tags_in_range(0, nread, nread + ninput_items, pmt.pmt_string_to_symbol("tx_time")) if tags: full_secs = pmt.pmt_to_uint64(pmt.pmt_tuple_ref(tags[0].value, 0)) frac_secs = pmt.pmt_to_double(pmt.pmt_tuple_ref(tags[0].value, 1)) tx_item = full_secs * self.samp_rate + \ int(frac_secs / (1.0 / self.samp_rate)) self.tags['tx_time'].append(tx_item) sob_tags = self.get_tags_in_range(0, nread, nread + ninput_items, pmt.pmt_string_to_symbol("tx_sob")) if sob_tags: self.tags['tx_sob'].append(sob_tags[0].offset) eob_tags = self.get_tags_in_range(0, nread, nread + ninput_items, pmt.pmt_string_to_symbol("tx_eob")) if eob_tags: self.tags['tx_eob'].append(eob_tags[0].offset)
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 send_txt(self, endofmsg=False): if self.sendmsg and (self.addr != 0 or len(self.txt) > 0): self.post_msg( 0, pmt.pmt_string_to_symbol(POCSAG_ID), pmt.from_python({ "addr": self.addr, "fun": self.fun, "text": self.txt, "num": self.num, "endofmsg": endofmsg, "channel": self.channel_str }))
def packets_to_vectors(packets, lengthtagname, vlen=1): tags = [] data = [] offset = 0 for packet in packets: data.extend(packet) tag = gr.gr_tag_t() tag.offset = offset/vlen tag.key = pmt.pmt_string_to_symbol(lengthtagname) tag.value = pmt.pmt_from_long(len(packet)/vlen) tags.append(tag) offset = offset + len(packet) return data, tags
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(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 send_txt(self, endofmsg = False): if self.sendmsg and (self.addr != 0 or len(self.txt) > 0): self.post_msg(0, pmt.pmt_string_to_symbol(POCSAG_ID), pmt.from_python( { "addr": self.addr, "fun": self.fun, "text": self.txt, "num": self.num, "endofmsg": endofmsg, "channel": self.channel_str }) )
def __init__(self, ): """ The input is a stream of bytes. Outputs a blob with value of current byte if it is different from last. """ gr.block.__init__( self, name="transition_detect", in_sig=[numpy.uint8], out_sig=None, has_msg_input=False, num_msg_outputs=1, ) self.mgr = pmt.pmt_mgr() for i in range(64): self.mgr.set(pmt.pmt_make_blob(10000)) self.old_result = 0 self.key = pmt.pmt_string_to_symbol("example_key") self.value = pmt.pmt_string_to_symbol("example_value") self.trans_count = 0
def work(self, input_items, output_items): self.state = SEARCH_EOB_IN in0 = input_items[0] out = output_items[0] out[:] = in0[:] 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) num_items = min(len(input_items[0]), len(output_items[0])) if len(input_items[0]) > len(output_items[0]): print "Burst Gate: Output items small. Might be bad." source = pmt.pmt_string_to_symbol("") for tag in tags: if pmt.pmt_symbol_to_string(tag.key) == "tx_eob": self.state = FOUND_EOB_IN else: self.add_item_tag(0, tag.offset, tag.key, tag.value, source) if self.state == FOUND_EOB_IN: item_index = num_items # which output item gets the tag? offset = self.nitems_written(0) + item_index key = pmt.pmt_string_to_symbol("tx_eob") value = pmt.pmt_string_to_symbol("") source = pmt.pmt_string_to_symbol("") self.add_item_tag(0, offset - 1, key, pmt.PMT_T, source) return len(out)
def work(self, input_items, output_items): self.state = SEARCH_EOB_IN in0 = input_items[0] out = output_items[0] out[:] = in0[:] 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) num_items = min(len(input_items[0]), len(output_items[0])) if (len(input_items[0]) > len(output_items[0])): print "Burst Gate: Output items small. Might be bad." source = pmt.pmt_string_to_symbol("") for tag in tags: if pmt.pmt_symbol_to_string(tag.key) == "tx_eob": self.state = FOUND_EOB_IN else: self.add_item_tag(0, tag.offset, tag.key, tag.value, source) if self.state == FOUND_EOB_IN: item_index = num_items #which output item gets the tag? offset = self.nitems_written(0) + item_index key = pmt.pmt_string_to_symbol("tx_eob") value = pmt.pmt_string_to_symbol("") source = pmt.pmt_string_to_symbol("") self.add_item_tag(0, offset - 1, key, pmt.PMT_T, source) return len(out)
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 test_crc_len(self): data = range(16) tag_name = "len" tag = gr.gr_tag_t() tag.offset = 0 tag.key = pmt.pmt_string_to_symbol(tag_name) tag.value = pmt.pmt_from_long(len(data)) src = gr.vector_source_b(data, (tag,), False, 1) mtu = 64 crc = ofdm.crc32_bb(False, mtu, tag_name) sink = gr.vector_sink_b() self.tb.connect(src, crc, sink) self.tb.run() # Check that the packets before crc_check are 4 bytes longer that the input. self.assertEqual(len(data) + 4, len(sink.data()))
def test_crc_equal(self): data = (0, 1, 2, 3, 4, 5, 6, 7, 8) tag_name = "len" tag = gr.gr_tag_t() tag.offset = 0 tag.key = pmt.pmt_string_to_symbol(tag_name) tag.value = pmt.pmt_from_long(len(data)) src = gr.vector_source_b(data, (tag,), False, 1) mtu = 64 crc = ofdm.crc32_bb(False, mtu, tag_name) crc_check = ofdm.crc32_bb(True, mtu, tag_name) sink = gr.vector_sink_b() sink2 = gr.vector_sink_b() self.tb.connect(src, crc, crc_check, sink) self.tb.run() # Check that the packets after crc_check are the same as input. self.assertEqual(data, sink.data())
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 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): 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): 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: 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 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): if self.rx_state == RX_INIT: 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_state == RX_SEARCH #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 == OUTGOING_PKT_PORT: self.queue.put(msg) #if outgoing, put in queue for processing elif msg.offset == INCOMING_PKT_PORT: 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 'pkt_rcved', self.time_update, self.start_hop, self.next_tune_time else: self.pkt_received = True print 'pkt_rcved', self.time_update, self.start_hop, self.next_tune_time else: pass #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 #TODO: this stuff is left over from tdma.py, see if we can re-use this somehow #self.frame_period = self.slot_interval * self.num_slots #my_fraction_frame = ( self.initial_slot * 1.0 ) / ( self.num_slots) #frame_count = math.floor(self.time_update / self.frame_period) #current_slot_interval = ( self.time_update % self.frame_period ) / self.frame_period #self.time_transmit_start = (frame_count + 2) * self.frame_period + ( my_fraction_frame * self.frame_period ) - self.lead_limit self.time_transmit_start = self.time_update + ( self.lead_limit * 10.0) self.interval_start = self.time_transmit_start + self.lead_limit #get current time self.time_update += (self.sample_period * ninput_items) #determine if it's time for us to start tx'ing, start process self.lead_limit seconds #before our slot actually begins (i.e. deal with latency) if self.time_update > self.time_transmit_start: self.antenna_start = self.interval_start + self.post_guard self.tx_frames() #do more than this? #print self.interval_start,self.antenna_start self.interval_start += self.hop_interval self.time_transmit_start = self.interval_start - self.lead_limit if self.rx_state == RX_FOUND: if self.time_update > self.start_hop: #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.next_tune_time #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.rx_state = RX_INIT print 'reset' 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