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 vectors_to_packets(data, tags, lengthtagname, vlen=1): lengthtags = [t for t in tags if pmt.pmt_symbol_to_string(t.key) == lengthtagname] lengths = {} for tag in lengthtags: if tag.offset in lengths: raise ValueError( "More than one tags with key {0} with the same offset={1}." .format(lengthtagname, tag.offset)) lengths[tag.offset] = pmt.pmt_to_long(tag.value)*vlen if 0 not in lengths: raise ValueError("There is no tag with key {0} and an offset of 0" .format(lengthtagname)) pos = 0 packets = [] while pos < len(data): if pos not in lengths: raise ValueError("There is no tag with key {0} and an offset of {1}." "We were expecting one." .format(lengthtagname, pos)) length = lengths[pos] if length == 0: raise ValueError("Packets cannot have zero length.") if pos+length > len(data): raise ValueError("The final packet is incomplete.") packets.append(data[pos: pos+length]) pos += length return packets
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 pre_hook(val): key_pmt = es.event_field( val.msg, key_sym ); key = pmt.pmt_symbol_to_string( key_pmt ); print key; if(key == "*"): key = "10"; elif(key == "#"): key = "12"; elif(key=="0"): ival = 11; ival = int(key); row = (ival - 1)/3; col = (ival - 1)%3; rowfreqs = [697.0, 770.0, 852.0, 941.0]; colfreqs = [1209.0, 1336.0, 1477.0, 1633.0]; assert(row < len(rowfreqs)); assert(col < len(colfreqs)); blocks = val.handler.pb2(); blocks["src_L"].set_frequency(rowfreqs[row]); blocks["src_R"].set_frequency(colfreqs[col]); print "set freq %s"%( str((rowfreqs[row], colfreqs[col])) ); r = es.es_hook_rval(); return r;
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): #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 #set know_time True for useful state machines if not self.know_time: if self.found_time and self.found_rate: self.know_time = True print 'know time' else: self.time_update += (self.sample_period * ninput_items) print self.time_update return ninput_items
def work(self, input_items, output_items): in0 = input_items[0] out = output_items[0] nread = self.nitems_read(0) #number of items read on port 0 ninput_items = len(input_items[0]) noutput_items = len(output_items[0]) nitems_to_consume = min(ninput_items, noutput_items) out[:nitems_to_consume] = in0[:nitems_to_consume] # output any tags left over from the last iteration if they're ready ready_tags = [x for x in self.tag_residues if x[0] < self.nitems_written(0) + ninput_items] # test if we're writing past what we're allowed for tag in ready_tags: if tag[0] >= self.nitems_written(0) + nitems_to_consume: self.dev_logger.error("writing tags out of range. bad idea") for offset, key, value, srcid in ready_tags: # self.dev_logger.debug("adding key %s value %s source %s at offset %s", # key,value,srcid, offset) self.add_item_tag(0,offset, pmt.from_python(key), pmt.from_python(value), pmt.from_python(srcid)) # keep tags in residues that aren't ready yet self.tag_residues = [x for x in self.tag_residues if x[0] >= self.nitems_written(0) + ninput_items] #read all tags associated with port 0 for items in this work function tags = self.get_tags_in_range(0, nread, nread+nitems_to_consume) for tag in tags: if pmt.pmt_symbol_to_string(tag.key) == self.eob_key: new_offset = tag.offset + self.upsamp_factor-1 # if the new offset is still in this work block, shift the tag. # Otherwise store the tag tuple for the next call if new_offset < self.nitems_written(0) + ninput_items: # self.dev_logger.debug("adding key %s value %s source %s at offset %s", # pmt.to_python(tag.key),pmt.to_python(tag.value),pmt.to_python(tag.srcid), new_offset) self.add_item_tag(0,new_offset, tag.key, tag.value, tag.srcid) else: # turning into native python types in case seg fault issue is due to memory management self.tag_residues.append( (new_offset, pmt.to_python(tag.key), pmt.to_python(tag.value), pmt.to_python(tag.srcid)) ) else: # self.dev_logger.debug("adding key %s value %s source %s at offset %s", # pmt.to_python(tag.key),pmt.to_python(tag.value),pmt.to_python(tag.srcid), tag.offset) self.add_item_tag(0,tag.offset, tag.key, tag.value, tag.srcid) return nitems_to_consume
def pre_hook(val): payload = es.event_field( val.msg, key_sym ); payload = pmt.pmt_symbol_to_string( payload ); v = numpy.fromstring( payload, dtype=numpy.byte ); blocks = val.handler.pb2(); blocks["src"].set_data( es.StrVector([payload]), len(v) ); r = es.es_hook_rval(); return r;
def work(self, input_items, output_items): #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: pass #TODO:something intelligent for incoming time bcast pkts 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 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 #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.interval_start = self.time_transmit_start + self.lead_limit self.antenna_start = self.interval_start + self.guard_interval self.tx_frames() #do more than this? self.time_transmit_start += self.frame_period #TODO: add intelligence to handle slot changes safely return ninput_items
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): try: msg = self.pop_msg_queue() key = pmt.pmt_symbol_to_string(msg.key) txt = pmt.to_python(msg.value) if key == pocsag.POCSAG_ID: self.pocsag_pagermsg.emit(txt) return 1 return 0 except: return -1
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): #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) if len(tags) > 0: self._dev_logger.debug("beacon consumer found new tags") for tag in tags: key_string = pmt.pmt_symbol_to_string(tag.key) if key_string == "rx_time": current_integer, current_fractional = pmt.to_python(tag.value) self.timestamp = time_spec_t(current_integer + current_fractional) self.floored_timestamp = time_spec_t(current_integer) self.time_offset = tag.offset self.found_time = True #print "rx time found: %s at offset %ld" %(self.timestamp, self.time_offset) elif key_string == "rx_rate": #print "rx rate found" self.rate = pmt.to_python(tag.value) self.sample_period = 1 / self.rate self.found_rate = True # only clear out old packets if the time and rate are known if self.found_rate & self.found_time: #print "nread: %ld ninput_items: %ld self.time_offset %ld" % (nread, ninput_items, self.time_offset) t_end = (nread + ninput_items - self.time_offset) * self.sample_period + self.timestamp #print "t_end is %s" % t_end self._beacon_lock.acquire() self.cull_stale_beacons(t_end) num_beacons = len(self._beacon_list) self._beacon_lock.release() # if there aren't any valid beacons left in the queue, declare the sync was # lost if num_beacons == 0: self.sync_lost(t_end) return ninput_items
def work(self, input_items, output_items): #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) if len(tags) > 0: self._dev_logger.debug("beacon consumer found new tags") for tag in tags: key_string = pmt.pmt_symbol_to_string(tag.key) if key_string == "rx_time": current_integer,current_fractional = pmt.to_python(tag.value) self.timestamp = time_spec_t(current_integer + current_fractional) self.floored_timestamp = time_spec_t(current_integer) self.time_offset = tag.offset self.found_time = True #print "rx time found: %s at offset %ld" %(self.timestamp, self.time_offset) elif key_string == "rx_rate": #print "rx rate found" self.rate = pmt.to_python(tag.value) self.sample_period = 1/self.rate self.found_rate = True # only clear out old packets if the time and rate are known if self.found_rate & self.found_time: #print "nread: %ld ninput_items: %ld self.time_offset %ld" % (nread, ninput_items, self.time_offset) t_end = (nread + ninput_items - self.time_offset)*self.sample_period + self.timestamp #print "t_end is %s" % t_end self._beacon_lock.acquire() self.cull_stale_beacons(t_end) num_beacons = len(self._beacon_list) self._beacon_lock.release() # if there aren't any valid beacons left in the queue, declare the sync was # lost if num_beacons == 0: self.sync_lost(t_end) return ninput_items
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):#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): 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): 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): # g = open('./work.txt', 'ab') # g.write("FrameToFileSink start\n") # g.close nread = self.nitems_read(0) #number of items read on port 0 ninput_items = len(input_items[0]) input = np.array(input_items[0], dtype=np.int16) input.byteswap(True) #read all tags associated with port 0 for items in this work function tags = self.get_tags_in_range(0, nread, nread+ninput_items) frame_starts = [] frame_cnt = np.array([], dtype=np.uint32) threshold_events = [] for tag in tags: key_string = pmt.pmt_symbol_to_string(tag.key) if key_string == "Frame counter" : frame_start = tag.offset - nread if frame_start in frame_starts : continue frame_starts.append( frame_start ) # frame_cnt = np.append(frame_cnt, np.uint32(int(pmt.pmt_symbol_to_string(tag.value))) ) frame_cnt = np.append(frame_cnt, np.uint32(int(pmt.pmt_to_long(tag.value))) ) elif key_string == "Threshold event" : threshold_event = tag.offset - nread if threshold_event in threshold_events : continue threshold_events.append( threshold_event ) # f = open('./frame_temp.txt', 'ab') # f.write('BEGINBEGINBEGIN\n') # f.write('Threshold event len: %d\n'%len(threshold_events)) # for ii in xrange(len(threshold_events)): # f.write('Threshold event: %d\n'%threshold_events[ii]) # f.write('Frame start len: %d\n'%len(frame_starts)) # f.write('Frame cnt len: %d\n'%frame_cnt.size) # for ii in xrange(len(frame_starts)): # f.write('Frame start: %d, Frame cnt: %d\n'%(frame_starts[ii] , frame_cnt[ii])) # f.write('ENDENDENDEND\n\n') # f.close() ##################################### # Write data to file beginning = 0 while 1 : if len(threshold_events) == 0 : ending = ninput_items else : ending = threshold_events[0] threshold_events.pop(0) #LINUX compatibility # threshold_events = threshold_events[1:] if beginning != ending : if self.f == None : self.f = open('./collect_%d.bin'%self.file_counter, 'wb') self.file_counter += 1 start_idx = beginning while 1 : if frame_cnt.size == 0 or frame_starts[0] >= ending : input[start_idx:ending].tofile(self.f) break input[start_idx:frame_starts[0]].tofile(self.f) self.FrameConf.START_OF_FRAME.tofile(self.f) self.FrameConf.DATA_FRAME_ID.tofile(self.f) frame_cnt[0].newbyteorder('B').tofile(self.f) start_idx = frame_starts[0] frame_cnt = np.delete(frame_cnt, [0]) frame_starts.pop(0) #LINUX compatibility # frame_cnt = frame_cnt[1:] # frame_starts = frame_starts[1:] beginning = ending if beginning >= ninput_items : break if self.f is not None: self.f.close() self.f = None # g = open('./work.txt', 'ab') # g.write("FrameToFileSink stop\n") # g.close return ninput_items
def work(self, input_items, output_items): #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: a = 0 #TODO:something intelligent for incoming time bcast pkts 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 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 #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.interval_start = self.time_transmit_start + self.lead_limit self.antenna_start = self.interval_start + self.guard_interval self.tx_frames() #do more than this? self.time_transmit_start += self.frame_period #TODO: add intelligence to handle slot changes safely return ninput_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 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): #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]) #print "nread in logger is " + str(nread) #print "ninput_items is " + str(ninput_items) #out = output_items[0] #out[:] = in0[:] #read all tags associated with port 0 for items in this work function tags = self.get_tags_in_range(0, nread, nread + ninput_items) #sort by key first so that this order is preserved for same offset when sorting by offset tags = sorted(tags, key=lambda tag: pmt.pmt_symbol_to_string(tag.key), reverse=True) #sort by tag.offset tags = sorted(tags, key=lambda tag: tag.offset) #call lincoln log if self._logging != -1: #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) #print "found key : " + key_string + " at offset " + str(tag.offset) if key_string == 'tx_sob': self._sob_found = True if key_string == 'tx_time': tx_time_offset = tag.offset tx_time_tuple = pmt.to_python(tag.value) tx_time_spec_t = time_spec_t(tx_time_tuple[0], tx_time_tuple[1]) #tx_time_value = tx_time_spec_t.__float__() self._tx_time_offset = tx_time_offset #self._tx_time_value = tx_time_value self._tx_time_spec_t = tx_time_spec_t #print "tx_time_value is " + str(tx_time_value) if key_string == 'tx_rate': tx_rate_offset = tag.offset tx_rate_value = float(pmt.to_python(tag.value)) self._tx_rate_value = tx_rate_value * self._upsample_factor if key_string == 'packetlog': packetlog_offset = tag.offset packetlog_value = pmt.to_python(tag.value) if self._sob_found: offset_from_sob = packetlog_offset - self._tx_time_offset time_from_sob = offset_from_sob / float( self._tx_rate_value) #packetlog_time = self._tx_time_value + time_from_sob packetlog_time_spec_t = self._tx_time_spec_t + time_spec_t( time_from_sob) #packetlog_value['timestamp'] = packetlog_time packetlog_value['timestamp'] = str( packetlog_time_spec_t) #print packetlog_value self._logging.packet(packetlog_value) #print "--------------logger------------------------" return ninput_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 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): #print "tdma controller work" #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]) # update the starting timestamp for this block start_timestamp = self.ref_timestamp + (nread - self.ref_time_offset)/self.fs #read all tags associated with port 0 for items in this work function tags = self.get_tags_in_range(0, nread, nread+ninput_items) #print "tdma controller start of tag loop" #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.ref_time_offset = tag.offset self.ref_timestamp = time_spec_t(pmt.to_python(tag.value)) # only set host offset at the start if not self.found_time: current_time = time.time() current_time_ahead = time_spec_t(current_time) - self.ref_timestamp self.mac_sm.cq_manager.set_current_time_ahead(float(current_time_ahead)) self.dev_logger.debug("for rx time %s and host time %s, setting time ahead to %s", self.ref_timestamp, current_time, current_time_ahead) self.found_time = True self.dev_logger.debug("tdma_controller found new rx time of %s at offset %ld", self.ref_timestamp, self.ref_time_offset) #print "mobile controller found rate" # if this tag occurs at the start of the sample block, update the # starting timestamp if tag.offset == nread: start_timestamp = self.ref_timestamp + (nread - self.ref_time_offset)/self.fs elif key_string == "rx_rate": self.fs = pmt.to_python(tag.value) self.found_rate = True #print "mobile controller found time" # if this tag occurs at the start of the sample block, update the # starting timestamp if tag.offset == nread: start_timestamp = self.ref_timestamp + float(nread - self.ref_time_offset)/self.fs # self.dev_logger.debug("tag processing complete") if not (self.current_sched is None): start_timestamp = start_timestamp.round_to_sample(self.fs, self.current_sched["t0"]) #determine first transmit slot when we learn the time if not self.know_time: if self.found_time and self.found_rate: #print "mobile controller knows time" self.know_time = True # if the state machine has a command queue manager, send out a time cal # message if hasattr(self.mac_sm, "cq_manager"): # calibrate the command queue to uhd timing errors cal_ts = self.ref_timestamp + float(nread + ninput_items - self.ref_time_offset)/self.fs self.mac_sm.cq_manager.add_command_to_queue([(cal_ts, 0, "time_cal")]) if self.know_time: # set the mac to generate packets if the start of the frame occurs at any # point between now and the end of the current block plus the lead limit. # This should guarantee that packets are always submitted at least one lead # limit ahead of their transmit time end_timestamp = self.ref_timestamp + self.mac_config["lead_limit"] + float(nread + ninput_items - self.ref_time_offset)/self.fs else: end_timestamp = start_timestamp if not (self.current_sched is None): end_timestamp = end_timestamp.round_to_sample(self.fs, self.current_sched["t0"]) # only update the current timestamp if it is further along than the state machine if self.current_timestamp < start_timestamp: self.current_timestamp = start_timestamp # use this to detect endless loops loop_counter = 0 loop_max = 100 last_ts = self.current_timestamp # grab the latest schedule updates from the thread safe data struct num_scheds = len(self.in_sched_update_q) # self.dev_logger.debug("processing schedule updates") for k in range(num_scheds): self.sched_seq.append(self.in_sched_update_q.popleft()) # self.dev_logger.debug("schedule updates all appended to schedule sequence") # run the state machine if time cal is complete if self.time_cal_complete: # if self.current_sched is not None: # self.last_frame = deepcopy(self.current_sched) # handle any incoming packets self.process_raw_incoming_queue() # start timers if self.monitor_timing == True: wall_start_ts = time.time() state_start_ts = self.current_timestamp outp = None #print "mobile controller state machine loop" # iterate state machine until the current timestamp exceeds the ending timestamp while self.current_timestamp < end_timestamp: # self.dev_logger.debug("iterating state machine") last_ts = self.current_timestamp rf_in = [] while( len(self.incoming_q) > 0): rf_in.append(self.incoming_q.popleft()) inp = { "app_in":self.app_in_q, "current_ts":self.current_timestamp, "end_ts":end_timestamp, "frame_config":self.frame_config, "frame_count":self.frame_count, "mac_config":self.mac_config, "packet_count":self.packet_count, "pkt_switch_queues":self.pkt_switch_queues, "plot_lock":self.plot_lock, "rf_in":rf_in, "sched_seq":self.sched_seq, } #print "current timestamp is %s, end timestamp is %s" %(self.current_timestamp, end_timestamp) #print "iterating state machine" outp = self.mac_sm.step( (inp, False) ) # handle outputs #print "sending tx frames" self.tx_frames(**outp) #print "sending commands" self.send_commands(**outp) #print "sending application packets" self.send_app_pkts(**outp) self.log_dropped_pkts(**outp) self.log_mac_behavior(inp,outp) #print "output handling complete" # update node state with results self.current_timestamp = time_spec_t(outp["current_ts"]) self.packet_count = outp["packet_count"] self.pkt_switch_queues = outp["pkt_switch_queues"] self.frame_count = outp["frame_count"] self.frame_config = outp["frame_config"] self.sched_seq = outp["sched_seq"] # self.schedule_valid = outp["schedule_valid"] #bers = [self.active_rx_slots[num]["ber"] for num in self.active_rx_slots] #self.dev_logger.debug("active slot bers are %s", bers) if last_ts == self.current_timestamp: loop_counter+=1 else: loop_counter = 0 if loop_counter > loop_max: self.dev_logger.warn("INFINITE (PROBABLY) LOOP DETECTED - breaking out after %d loops",loop_counter) self.dev_logger.warn("current timestamp is: %s end timestamp is %s",self.current_timestamp, end_timestamp) break #print "tdma controller work complete" # self.dev_logger.debug("iteration complete") # do timer calcs at end of work function if self.monitor_timing == True: wall_end_ts = time.time() # if state machine wasn't executed at least once, outp won't be defined, # so assign something reasonable to state_end_ts if not (outp is None): state_end_ts = time_spec_t(outp["current_ts"]) else: state_end_ts = state_start_ts wall_delta_ts = wall_end_ts - wall_start_ts state_delta_ts = float(state_end_ts - state_start_ts) self.state_time_deltas += state_delta_ts self.wall_time_deltas += wall_delta_ts if self.state_time_deltas >= self.poll_interval: self.dev_logger.info("runtime ratio was %f wall seconds per state second",self.wall_time_deltas/self.state_time_deltas) self.state_time_deltas = 0 self.wall_time_deltas = 0 # we're still in time cal elif self.do_time_cal: if not self.know_time: self.dev_logger.error(("The base station does not know it's own time. " + "Cannot calibrate")) elif not self.mac_sm.is_base(): self.dev_logger.error("Only base nodes can send time calibration beacons") else: # send out cal beacon frames for k in range(self.num_cal_beacons): packet_count = self.packet_count frame_count = 0 frame_ts = (self.current_timestamp + self.mac_config["lead_limit"] + k*self.cal_frame_config["frame_len"]) # round fractional part to an integer sample so we don't break the # slot selector frame_ts = time_spec_t(frame_ts.int_s(), round(frame_ts.frac_s()*self.fs)/self.fs ) config = self.mac_config mobile_queues=defaultdict(deque) # make mac beacon frames outs = self.manage_slots.send_frame(self.mac_config, self.cal_frame_config, self.cal_schedule, frame_count, packet_count, frame_ts, mobile_queues, ) frame_count, packet_count, tx_list, mobile_queues, dropped_pkts = outs # handle outputs self.packet_count = packet_count # filter out anything that's not a beacon tx_list = [x for x in tx_list if x[0]["pktCode"] == self.mac_sm._types_to_ints["beacon"]] # add tdma headers to all the packets in the tx list tx_list = [ (meta, self.mac_sm.pack_tdma_header(data, **meta)) for meta, data in tx_list ] # send packets self.tx_frames(tx_list) self.current_timestamp = (end_timestamp + self.mac_config["lead_limit"] + self.num_cal_beacons*self.cal_frame_config["frame_len"]) self.do_time_cal = False return ninput_items
def print_tag(self, tag): my_string = "key = " + pmt.pmt_symbol_to_string(tag.key) + "\tsrcid = " + pmt.pmt_symbol_to_string(tag.srcid) my_string = my_string + "\tvalue = " + str(pmt.pmt_to_long(tag.value)) my_string = my_string + "\toffset = " + str(tag.offset) print my_string
def work(self, input_items, output_items): in0 = input_items[0] out = output_items[0] nread = self.nitems_read(0) #number of items read on port 0 ninput_items = len(input_items[0]) noutput_items = len(output_items[0]) nitems_to_consume = min(ninput_items, noutput_items) out[:nitems_to_consume] = in0[:nitems_to_consume] # output any tags left over from the last iteration if they're ready ready_tags = [ x for x in self.tag_residues if x[0] < self.nitems_written(0) + ninput_items ] # test if we're writing past what we're allowed for tag in ready_tags: if tag[0] >= self.nitems_written(0) + nitems_to_consume: self.dev_logger.error("writing tags out of range. bad idea") for offset, key, value, srcid in ready_tags: # self.dev_logger.debug("adding key %s value %s source %s at offset %s", # key,value,srcid, offset) self.add_item_tag(0, offset, pmt.from_python(key), pmt.from_python(value), pmt.from_python(srcid)) # keep tags in residues that aren't ready yet self.tag_residues = [ x for x in self.tag_residues if x[0] >= self.nitems_written(0) + ninput_items ] #read all tags associated with port 0 for items in this work function tags = self.get_tags_in_range(0, nread, nread + nitems_to_consume) for tag in tags: if pmt.pmt_symbol_to_string(tag.key) == self.eob_key: new_offset = tag.offset + self.upsamp_factor - 1 # if the new offset is still in this work block, shift the tag. # Otherwise store the tag tuple for the next call if new_offset < self.nitems_written(0) + ninput_items: # self.dev_logger.debug("adding key %s value %s source %s at offset %s", # pmt.to_python(tag.key),pmt.to_python(tag.value),pmt.to_python(tag.srcid), new_offset) self.add_item_tag(0, new_offset, tag.key, tag.value, tag.srcid) else: # turning into native python types in case seg fault issue is due to memory management self.tag_residues.append( (new_offset, pmt.to_python(tag.key), pmt.to_python(tag.value), pmt.to_python(tag.srcid))) else: # self.dev_logger.debug("adding key %s value %s source %s at offset %s", # pmt.to_python(tag.key),pmt.to_python(tag.value),pmt.to_python(tag.srcid), tag.offset) self.add_item_tag(0, tag.offset, tag.key, tag.value, tag.srcid) return nitems_to_consume
def work(self, input_items, output_items): #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]) #print "nread in logger is " + str(nread) #print "ninput_items is " + str(ninput_items) #out = output_items[0] #out[:] = in0[:] #read all tags associated with port 0 for items in this work function tags = self.get_tags_in_range(0, nread, nread+ninput_items) #sort by key first so that this order is preserved for same offset when sorting by offset tags = sorted(tags, key=lambda tag : pmt.pmt_symbol_to_string(tag.key), reverse=True) #sort by tag.offset tags = sorted(tags, key=lambda tag : tag.offset) #call lincoln log if self._logging != -1: #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) #print "found key : " + key_string + " at offset " + str(tag.offset) if key_string == 'tx_sob': self._sob_found = True if key_string == 'tx_time': tx_time_offset = tag.offset tx_time_tuple = pmt.to_python(tag.value) tx_time_spec_t = time_spec_t(tx_time_tuple[0],tx_time_tuple[1]) #tx_time_value = tx_time_spec_t.__float__() self._tx_time_offset = tx_time_offset #self._tx_time_value = tx_time_value self._tx_time_spec_t = tx_time_spec_t #print "tx_time_value is " + str(tx_time_value) if key_string == 'tx_rate': tx_rate_offset = tag.offset tx_rate_value = float(pmt.to_python(tag.value)) self._tx_rate_value = tx_rate_value*self._upsample_factor if key_string == 'packetlog': packetlog_offset = tag.offset packetlog_value = pmt.to_python(tag.value) if self._sob_found: offset_from_sob = packetlog_offset - self._tx_time_offset time_from_sob = offset_from_sob / float(self._tx_rate_value) #packetlog_time = self._tx_time_value + time_from_sob packetlog_time_spec_t = self._tx_time_spec_t + time_spec_t(time_from_sob) #packetlog_value['timestamp'] = packetlog_time packetlog_value['timestamp'] = str(packetlog_time_spec_t) #print packetlog_value self._logging.packet(packetlog_value) #print "--------------logger------------------------" 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