def tx_data(self): """ Put messages from input into tx_queue. """ #TODO: Enable multi-hop transmissions -> less overhead! msg = self.queue.get() msg_byte_count = len(pmt.blob_data(msg.value)) + self.overhead if msg_byte_count >= self.bytes_per_slot: print "ERROR: Message too long!" else: #self.got_cts = False self.state = IDLE time_object = int(math.floor(self.antenna_start)), (self.antenna_start % 1) more_frames = 0 data = numpy.concatenate([HAS_DATA, self._to_byte_array(self.own_adr), self._to_byte_array(self.dst_adr), pmt.blob_data(msg.value)]) #print "DATA-SEND: %s" % data tx_object = time_object, data, more_frames #print "DEBUG: Sending DATA at", time_object #print "-----fre_list %s - hop-index %s" % (self.freq_list, self.hop_index) #print self.freq_msg self.post_msg(TO_FRAMER_PORT, pmt.string_to_symbol('full'), pmt.from_python(tx_object), pmt.string_to_symbol('tdma'))
def tx_data(self): """ Put messages from input into tx_queue. """ #TODO: Enable multi-hop transmissions -> less overhead! msg = self.queue.get() msg_byte_count = len(pmt.blob_data(msg.value)) + self.overhead if msg_byte_count >= self.bytes_per_slot: print "ERROR: Message too long!" else: #self.got_cts = False self.state = IDLE time_object = int(math.floor( self.antenna_start)), (self.antenna_start % 1) more_frames = 0 data = numpy.concatenate([ HAS_DATA, self._to_byte_array(self.own_adr), self._to_byte_array(self.dst_adr), pmt.blob_data(msg.value) ]) #print "DATA-SEND: %s" % data tx_object = time_object, data, more_frames #print "DEBUG: Sending DATA at", time_object #print "-----fre_list %s - hop-index %s" % (self.freq_list, self.hop_index) #print self.freq_msg self.post_msg(TO_FRAMER_PORT, pmt.string_to_symbol('full'), pmt.from_python(tx_object), pmt.string_to_symbol('tdma'))
def work(self, input_items, output_items): if self.rx_state == RX_INIT: for usrp in ['uhd_source', 'uhd_sink']: self.post_msg(CTRL_PORT, pmt.string_to_symbol(usrp + '.set_center_freq'), pmt.from_python(((self.freq_list[self.hop_index], ), {})), pmt.string_to_symbol('fhss')) #print "DEBUG: Set frequency" 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 # Check for pkts from higher layer (pkts to transmit) if msg.offset == OUTGOING_PKT_PORT: dst = int(pmt.blob_data(msg.value).tostring()[0]) if dst > self.max_neighbors: print "ERROR: DST-adr > number of channels!" elif self.neighbors[dst - 1] and dst != self.own_adr: self.dst_adr = dst self.queue.put(msg) # if outgoing, put in queue for processing else: print "ERROR: DST Node not in known neighborhood or own adr!" # Check for received pkts from deframer elif msg.offset == INCOMING_PKT_PORT: pkt = pmt.blob_data(msg.value) pkt_type, pkt_src, pkt_dst = pkt[0:3] handle_pkts = {HAS_DATA[0]: self.received_data, IS_RTS[0]: self.received_rts, IS_CTS[0]: self.received_cts, IS_BCN[0]: self.received_bcn} #print "DEBUG: MSG from ", pkt[1], " - to ", pkt[2], " type: ", pkt[0] if pkt_src != self.own_adr and pkt_dst in [self.own_adr, self.bcst_adr]: try: handle_pkts[pkt_type](pkt) except KeyError: print "ERROR: Wrong packet type detected!" #else: # print "Not addressed to this station - adr to: ", pkt[2] nread = self.nitems_read(0) # number of items read on port 0 ninput_items = len(input_items[0]) if not self.know_time: print "Waiting for time..." #process streaming samples and tags here #read all tags associated with port 0 for items tags = self.get_tags_in_range(0, nread, nread + ninput_items) #find all of our tags, making the adjustments to our timing for tag in tags: key_string = pmt.symbol_to_string(tag.key) if key_string == "rx_time": self.current_integer, self.current_fractional = pmt.to_python(tag.value) self.time_update = self.current_integer + self.current_fractional self.found_time = True print repr(self.time_update) elif key_string == "rx_rate": self.rate = pmt.to_python(tag.value) self.sample_period = 1.0 / self.rate self.found_rate = True if self.found_time and self.found_rate: self.know_time = True else: #get/update current time self.time_update += (self.sample_period * ninput_items) #print "DEBUG: time_update:", self.time_update, " - input_items:", ninput_items, " - samp-period", self.sample_period # Set first tuning time 20 sec in future (hope that we receive beacon # pkg within this time for sync -> assume that we're the only node if not) if self.time_tune_start == 0: print "Searching for neighbors..." self.interval_start = self.time_update + self.discovery_time self.time_tune_start = self.interval_start - (10 * self.post_guard) #determine if it's time for us to start tx'ing, start process #10 * self.post_guard before our slot actually begins (deal with latency) if self.time_update > self.time_tune_start: # Check for neighbors -> get free address if not self.discovery_finished: self.discovery_finished = True i = 0 while self.neighbors[i]: i += 1 self.own_adr = i + 1 print "Set own address to:", self.own_adr if self.own_adr != 1: # Wait another 20 sec for synchronization print "Waiting for synchronization..." self.interval_start = self.time_update + self.sync_time else: self.antenna_start = self.interval_start + self.pre_guard self.hop() # TODO: MOve most of the following stuff before # time_tune_start! handle_state = {IDLE: self.idle, GOT_RTS: self.got_rts, GOT_CTS: self.got_cts, WAITING_FOR_CTS: self.waiting_for_cts, WAITING_FOR_DATA: self.waiting_for_data} handle_state[self.state]() self.hops_to_beacon -= 1 self.interval_start += self.hop_interval self.time_tune_start = self.interval_start - (10 * self.post_guard) #print "Next Hop: ", int(math.floor(self.interval_start)), " - ", self.interval_start % 1, " ----- INDEX: ", self.hop_index return ninput_items
def work(self, input_items, output_items): if self.rx_state == RX_INIT: for usrp in ['uhd_source', 'uhd_sink']: self.post_msg( CTRL_PORT, pmt.string_to_symbol(usrp + '.set_center_freq'), pmt.from_python(((self.freq_list[self.hop_index], ), {})), pmt.string_to_symbol('fhss')) #print "DEBUG: Set frequency" 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 # Check for pkts from higher layer (pkts to transmit) if msg.offset == OUTGOING_PKT_PORT: dst = int(pmt.blob_data(msg.value).tostring()[0]) if dst > self.max_neighbors: print "ERROR: DST-adr > number of channels!" elif self.neighbors[dst - 1] and dst != self.own_adr: self.dst_adr = dst self.queue.put( msg) # if outgoing, put in queue for processing else: print "ERROR: DST Node not in known neighborhood or own adr!" # Check for received pkts from deframer elif msg.offset == INCOMING_PKT_PORT: pkt = pmt.blob_data(msg.value) pkt_type, pkt_src, pkt_dst = pkt[0:3] handle_pkts = { HAS_DATA[0]: self.received_data, IS_RTS[0]: self.received_rts, IS_CTS[0]: self.received_cts, IS_BCN[0]: self.received_bcn } #print "DEBUG: MSG from ", pkt[1], " - to ", pkt[2], " type: ", pkt[0] if pkt_src != self.own_adr and pkt_dst in [ self.own_adr, self.bcst_adr ]: try: handle_pkts[pkt_type](pkt) except KeyError: print "ERROR: Wrong packet type detected!" #else: # print "Not addressed to this station - adr to: ", pkt[2] nread = self.nitems_read(0) # number of items read on port 0 ninput_items = len(input_items[0]) if not self.know_time: print "Waiting for time..." #process streaming samples and tags here #read all tags associated with port 0 for items tags = self.get_tags_in_range(0, nread, nread + ninput_items) #find all of our tags, making the adjustments to our timing for tag in tags: key_string = pmt.symbol_to_string(tag.key) if key_string == "rx_time": self.current_integer, self.current_fractional = pmt.to_python( tag.value) self.time_update = self.current_integer + self.current_fractional self.found_time = True print repr(self.time_update) elif key_string == "rx_rate": self.rate = pmt.to_python(tag.value) self.sample_period = 1.0 / self.rate self.found_rate = True if self.found_time and self.found_rate: self.know_time = True else: #get/update current time self.time_update += (self.sample_period * ninput_items) #print "DEBUG: time_update:", self.time_update, " - input_items:", ninput_items, " - samp-period", self.sample_period # Set first tuning time 20 sec in future (hope that we receive beacon # pkg within this time for sync -> assume that we're the only node if not) if self.time_tune_start == 0: print "Searching for neighbors..." self.interval_start = self.time_update + self.discovery_time self.time_tune_start = self.interval_start - (10 * self.post_guard) #determine if it's time for us to start tx'ing, start process #10 * self.post_guard before our slot actually begins (deal with latency) if self.time_update > self.time_tune_start: # Check for neighbors -> get free address if not self.discovery_finished: self.discovery_finished = True i = 0 while self.neighbors[i]: i += 1 self.own_adr = i + 1 print "Set own address to:", self.own_adr if self.own_adr != 1: # Wait another 20 sec for synchronization print "Waiting for synchronization..." self.interval_start = self.time_update + self.sync_time else: self.antenna_start = self.interval_start + self.pre_guard self.hop() # TODO: MOve most of the following stuff before # time_tune_start! handle_state = { IDLE: self.idle, GOT_RTS: self.got_rts, GOT_CTS: self.got_cts, WAITING_FOR_CTS: self.waiting_for_cts, WAITING_FOR_DATA: self.waiting_for_data } handle_state[self.state]() self.hops_to_beacon -= 1 self.interval_start += self.hop_interval self.time_tune_start = self.interval_start - (10 * self.post_guard) #print "Next Hop: ", int(math.floor(self.interval_start)), " - ", self.interval_start % 1, " ----- INDEX: ", self.hop_index return ninput_items