def start(self): self.count = self.count_init self.thread.start() reg = pmt.cons(pmt.intern("timer_event"), self.message_subscribers(pmt.intern("timer_event"))) reg_msg = pmt.cons(pmt.intern("ES_REGISTER_HANDLER"), reg) print "subscribers: %s" % (str(self.message_subscribers(pmt.intern("timer_event")))) print "registration message: %s" % (str(reg_msg)) self.message_port_pub(pmt.intern("which_stream"), reg_msg)
def set_freqs(self, freq0, freq1, freq2, freq3): self.top4[0] = freq0 self.top4[1] = freq1 self.top4[2] = freq2 self.top4[3] = freq3 self.message_out0.set_msg(pmt.cons( pmt.to_pmt("freq"), pmt.to_pmt(freq0-self.tune_freq) )) #send differencial frequency self.message_out1.set_msg(pmt.cons( pmt.to_pmt("freq"), pmt.to_pmt(freq1-self.tune_freq) )) self.message_out2.set_msg(pmt.cons( pmt.to_pmt("freq"), pmt.to_pmt(freq2-self.tune_freq) )) self.message_out3.set_msg(pmt.cons( pmt.to_pmt("freq"), pmt.to_pmt(freq3-self.tune_freq) ))
def test_000(self): # Just run some data through and make sure it doesn't puke. src_data = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) src = blocks.pdu_to_tagged_stream(blocks.byte_t) snk3 = blocks.tagged_stream_to_pdu(blocks.byte_t) snk2 = blocks.vector_sink_b() snk = blocks.tag_debug(1, "test") snk.set_display(False) dbg = blocks.message_debug() # Test that the right number of ports exist. pi = snk3.message_ports_in() po = snk3.message_ports_out() self.assertEqual(pmt.length(pi), 1) #system port is defined automatically self.assertEqual(pmt.length(po), 1) self.tb.connect(src, snk) self.tb.connect(src, snk2) self.tb.connect(src, snk3) self.tb.msg_connect(snk3, "pdus", dbg, "store") # make our reference and message pmts port = pmt.intern("pdus") msg = pmt.cons( pmt.PMT_NIL, pmt.make_u8vector(16, 0xFF)) # post the message src.to_basic_block()._post(port, msg) src.to_basic_block()._post(pmt.intern("system"), pmt.cons(pmt.intern("done"), pmt.from_long(1))) self.tb.start() self.tb.wait() # Get the vector of data from the vector sink result_data = snk2.data() # Get the vector of data from the message sink # Convert the message PMT as a pair into its vector result_msg = dbg.get_message(0) msg_vec = pmt.cdr(result_msg) #pmt.print(msg_vec) # Convert the PMT vector into a Python list msg_data = [] for i in range(16): msg_data.append(pmt.u8vector_ref(msg_vec, i)) actual_data = 16*[0xFF,] self.assertEqual(actual_data, list(result_data)) self.assertEqual(actual_data, msg_data)
def encode_decode_test(self, payload_str="TEST", whitening=False, encode_crc=False, decode_crc=False): preamble = "01010101" sync1 = 0x2 sync2 = 0x3 sync_length = 2 payload = [ord(c) for c in payload_str] strobe = blocks.message_strobe(pmt.cons(pmt.PMT_NIL, pmt.to_pmt(payload)), 200) encoder = cc11xx_encoder.cc11xx_encoder(preamble=[int(preamble, 2)], syncword=[sync1, sync2], whitening=whitening, crc=encode_crc) pdu_to_stream = blocks.pdu_to_tagged_stream(blocks.byte_t, "packet_len") debug = blocks.message_debug() self.tb.msg_connect(strobe, "strobe", encoder, "in") self.tb.msg_connect(encoder, "out", pdu_to_stream, "pdus") unpack = blocks.packed_to_unpacked_bb(1, 0) acc_code_block = digital.correlate_access_code_tag_bb(preamble, 0, "preamble") deframer = cc11xx.cc11xx_deframer_bb(sync1 ,sync2, whitening, decode_crc, sync_length) self.tb.connect(pdu_to_stream,unpack) self.tb.connect(unpack, acc_code_block) self.tb.connect(acc_code_block, deframer) self.tb.msg_connect((deframer, 'out'), (debug, 'store')) self.tb.start() time.sleep(1) self.tb.stop() #Please get rid of this sleep if you know how! time.sleep(0.1) self.tb.stop() result_data = [i for i in pmt.to_python(pmt.cdr(debug.get_message(0)))] self.assertEqual(payload, result_data)
def send_response(self, response): response_msg = response.strip() response = numpy.fromstring(response, dtype=numpy.uint8) response = pmt.to_pmt(response) response = pmt.cons(pmt.to_pmt(None), response) # self.debug_msg('Responding with {}'.format(response_msg)) self.message_port_pub(pmt.intern('out'), response)
def handle_msg(self, msg): meta = pmt.car(msg) data = pmt.cdr(msg) if not pmt.is_u8vector(data): raise NameError("Data is no u8 vector") packet = pmt.u8vector_elements(data) packet = array.array("B", packet) # TODO - add ax25 depacket try: string, payload, pid, ctrl, src, dest = ax25.printpacket(packet.tostring()) if self.verbose: print "Packet: ", packet.tostring() print "Payload: ", payload print "Payload(hex): ", self.hex_string(array.array("B", payload)) print "PID: %x" % pid print "CTRL: %x" % ctrl print "SRC: ", src print "DEST: ", dest payload = array.array("B", payload) # print outstream self.message_port_pub(pmt.intern("out"), pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(len(payload), payload))) print "********Deframer: ", time.time() except: print ("Bad packet") return 0
def __init__(self): gr.top_block.__init__(self, "Strobe") ################################################## # Blocks ################################################## self.blocks_tagged_stream_to_pdu_0 = blocks.tagged_stream_to_pdu(blocks.byte_t, "packet_len") self.blocks_pdu_to_tagged_stream_0 = blocks.pdu_to_tagged_stream(blocks.byte_t, "packet_len") self.blocks_message_strobe_0_0 = blocks.message_strobe(pmt.cons( pmt.PMT_NIL, pmt.make_u8vector(64,0) ), 750) self.blocks_message_strobe_0 = blocks.message_strobe(pmt.intern("TEST"), 1000) self.blocks_message_debug_0 = blocks.message_debug() self.blocks_copy_0 = blocks.copy(gr.sizeof_char*1) self.blocks_copy_0.set_enabled(True) ################################################## # Connections ################################################## self.connect((self.blocks_copy_0, 0), (self.blocks_tagged_stream_to_pdu_0, 0)) self.connect((self.blocks_pdu_to_tagged_stream_0, 0), (self.blocks_copy_0, 0)) ################################################## # Asynch Message Connections ################################################## self.msg_connect(self.blocks_message_strobe_0, "strobe", self.blocks_message_debug_0, "print") self.msg_connect(self.blocks_message_strobe_0_0, "strobe", self.blocks_pdu_to_tagged_stream_0, "pdus") self.msg_connect(self.blocks_tagged_stream_to_pdu_0, "pdus", self.blocks_message_debug_0, "print_pdu")
def handler(self, msg): ba = bitarray.bitarray(); meta = pmt.car(msg); packed_data = pmt.cdr(msg); # convert pmt -> int list (of packed bytes) data = array.array('B', pmt.u8vector_elements(packed_data)) # add header on front header = struct.pack('hh', 0x1337, 8*len(data)); ba.frombytes(header); # compute header crc c2 = binascii.crc32(ba.tobytes()); hcrc = struct.pack('i', c2); ba.frombytes(hcrc); # add the data payload ba.frombytes(data.tostring()) # compute payload crc c2 = binascii.crc32(ba.tobytes()); pcrc = struct.pack('i', c2); ba.frombytes(pcrc); # convert the unpacked bits to a list and back into a pmt u8vector burst_bits = pmt.init_u8vector(len(ba), ba.tolist()); print "Tx Packet: " + ":".join("{:02x}".format(ord(c)) for c in ba.tobytes()[0:8]) # make the new pdu pdu = pmt.cons(meta, burst_bits); # send it on its way self.message_port_pub(pmt.intern("unpacked_pdus"), pdu);
def test_000 (self): if enable_vw_testing: return print "test_000" payload_sizes = [28, 60, 92, 124, 156, 188, 220] codeword_sizes = [47, 79, 111, 159, 191, 223 ,255] enc = nuts.ngham_encoder(self.tsb_key) dec = nuts.ngham_decoder(verbose=True) dbg = blocks.message_debug() self.tb.connect(enc,dec) self.tb.msg_connect(dec, "out", dbg, "store") port_in = pmt.intern("in") print "starting up" self.tb.start() i = 0 #for i in range(len(payload_sizes)*0 + 1): src_data = [x for x in range(payload_sizes[i])] src_vec = pmt.init_u8vector(len(src_data), src_data) msg = pmt.cons(pmt.PMT_NIL, src_vec) print "posting msg" enc.to_basic_block()._post(port_in, msg) #while dbg.num_messages() < 1: #print "waiting..." time.sleep(1) self.tb.stop() self.tb.wait() result_msg = dbg.get_message(0) vector = pmt.u8vector_elements(pmt.cdr(result_msg)) print metadata print vector
def test_000 (self): if enable_vw_testing: return # this function will check that the encoder produces correct packet lengths payload_sizes = [28, 60, 92, 124, 156, 188, 220] codeword_sizes = [47, 79, 111, 159, 191, 223 ,255] enc = nuts.ngham_encoder(self.tsb_key, scramble=False, pad_for_usrp=False) snk = blocks.vector_sink_b() self.tb.connect(enc, snk) port = pmt.intern("in") self.tb.start() for i in range(7): snk.reset() src_data = [x for x in range(payload_sizes[i])] src_vec = pmt.init_u8vector(len(src_data), src_data) msg = pmt.cons( pmt.PMT_NIL, src_vec ) enc.to_basic_block()._post(port, msg) time.sleep(0.1) res_data = snk.data() print len(src_data), "=>", len(res_data), "(", codeword_sizes[i] + 11,")" self.assertEqual(len(res_data), codeword_sizes[i]+11) self.tb.stop() self.tb.wait()
def test_004 (self): # Test that the TCP server can stream PDUs <= the MTU size. port = str(random.Random().randint(0, 30000) + 10000) mtu = 10000 srcdata = tuple(x % 256 for x in range(mtu)) data = pmt.init_u8vector(srcdata.__len__(), srcdata) pdu_msg = pmt.cons(pmt.PMT_NIL, data) self.pdu_source = blocks.message_strobe(pdu_msg, 500) self.pdu_send = blocks.socket_pdu("TCP_SERVER", "localhost", port, mtu) self.pdu_recv = blocks.socket_pdu("TCP_CLIENT", "localhost", port, mtu) self.pdu_sink = blocks.message_debug() self.tb.msg_connect(self.pdu_source, "strobe", self.pdu_send, "pdus") self.tb.msg_connect(self.pdu_recv, "pdus", self.pdu_sink, "store") self.tb.start() time.sleep(1) self.tb.stop() self.tb.wait() received = self.pdu_sink.get_message(0) received_data = pmt.cdr(received) msg_data = [] for i in range(mtu): msg_data.append(pmt.u8vector_ref(received_data, i)) self.assertEqual(srcdata, tuple(msg_data))
def test_010_t (self): # PHR prefixer to removal data_in = pmt.cons(pmt.PMT_NIL, pmt.make_u8vector(2*self.c.phy_packetsize_bytes,170)) self.src = blocks.message_strobe(data_in,100) self.snk = blocks.message_debug() self.tb.msg_connect(self.src, "strobe", self.fragmentation, "in") self.tb.msg_connect(self.fragmentation, "out", self.phr_prefixer, "in") self.tb.msg_connect(self.phr_prefixer, "out", self.zeropadding, "in") self.tb.connect(self.zeropadding, self.demux) self.tb.connect((self.demux,0), self.codeword_mapper_I, self.interleaver_I, self.preamble_sfd_prefixer_I, (self.qpsk_mapper,0)) self.tb.connect((self.demux,1), self.codeword_mapper_Q, self.interleaver_Q, self.preamble_sfd_prefixer_Q, (self.qpsk_mapper,1)) self.tb.connect(self.qpsk_mapper, self.dqpsk_mapper, self.dqcsk_mapper, self.dqcsk_demapper, self.dqpsk_demapper, self.qpsk_demapper) self.tb.connect((self.qpsk_demapper,0), self.preamble_sfd_removal_I, self.deinterleaver_I, self.codeword_demapper_I, (self.mux,0)) self.tb.connect((self.qpsk_demapper,1), self.preamble_sfd_removal_Q, self.deinterleaver_Q, self.codeword_demapper_Q, (self.mux,1)) self.tb.connect(self.mux, self.zeropadding_removal) self.tb.msg_connect(self.zeropadding_removal, "out", self.phr_removal, "in") self.tb.msg_connect(self.phr_removal, "out", self.snk, "store") self.tb.start() time.sleep(0.3) self.tb.stop() msg_out1 = self.snk.get_message(0) msg_out2 = self.snk.get_message(1) data_out1 = pmt.to_python(msg_out1)[1] data_out2 = pmt.to_python(msg_out2)[1] ref = pmt.to_python(data_in)[1] self.assertTrue((data_out1==ref[:len(data_out1)]).all() and (data_out2==ref[:len(data_out2)]).all())
def make_NPDU(self,msg_pmt): if pmt.is_blob(msg_pmt): blob = msg_pmt #print "is blob" elif pmt.is_pair(msg_pmt): blob = pmt.cdr(msg_pmt) #print "is pair" else: print "Formato desconocido" return # Toma Pmt pair a numpyarray y luego a list data_np = pmt.to_python(blob) #numpy.ndarray apdu = data_np.tolist() #python list Src_Ieee = self.SrcIeeeN # la dir del USRP de 64 bit puede ser cualquiera global Seq_Num Seq_Num[0] = Seq_Num[0] +1 if Seq_Num[0] > 254: Seq_Num[0] = 0 Radio = [0x1E] # Saltos Max del mensaje Src_Add = self.SrcAddN # Puede ser cualquiera Dst_Add = [0xFF,0xFF] # Coordinador 00 00 / #broadcast FF FF FC_N = [0x08,0x10] # Frame Control LSB-MSB NPDU = FC_N + Dst_Add + Src_Add + Radio + Seq_Num + Src_Ieee + apdu # Crea PMT vacio send_pmt = pmt.make_u8vector(len(NPDU), ord(' ')) # Copia Caracteres a u8vector: for i in range(len(NPDU)): pmt.u8vector_set(send_pmt, i, NPDU[i]) # Envia mensaje: self.message_port_pub(pmt.intern('out'), pmt.cons(pmt.PMT_NIL, send_pmt))
def test_002 (self): # Send a PDU through a pair of UDP sockets port = str(random.Random().randint(0, 30000) + 10000) srcdata = (0x64, 0x6f, 0x67, 0x65) data = pmt.init_u8vector(srcdata.__len__(), srcdata) pdu_msg = pmt.cons(pmt.PMT_NIL, data) self.pdu_source = blocks.message_strobe(pdu_msg, 500) self.pdu_recv = blocks.socket_pdu("UDP_SERVER", "localhost", port) self.pdu_send = blocks.socket_pdu("UDP_CLIENT", "localhost", port) self.dbg = blocks.message_debug() self.tb.msg_connect(self.pdu_source, "strobe", self.pdu_send, "pdus") self.tb.msg_connect(self.pdu_recv, "pdus", self.dbg, "store") self.tb.start () time.sleep(1) self.tb.stop() self.tb.wait() self.pdu_send = None self.pdu_recv = None received = self.dbg.get_message(0) received_data = pmt.cdr(received) msg_data = [] for i in range(4): msg_data.append(pmt.u8vector_ref(received_data, i)) self.assertEqual(srcdata, tuple(msg_data))
def start(self): self.F = open(self.filename, 'rb') self.F.seek(0, os.SEEK_END) (s,l) = (0,self.F.tell()/self.itemsize) self.message_port_pub(pmt.intern("file_range"), pmt.cons(pmt.from_long(s),pmt.from_long(l))) return True
def make_MPDU(self, msg_pmt): if pmt.is_blob(msg_pmt): blob = msg_pmt # print "is blob" elif pmt.is_pair(msg_pmt): blob = pmt.cdr(msg_pmt) # print "is pair" else: print "Formato desconocido" return # Toma Pmt pair a numpyarray y luego a list data_np = pmt.to_python(blob) # numpy.ndarray npdu = data_np.tolist() # python list Src_Add = self.SrcAddM # Dir 16 bit id de la red Dst_Add = [0xFF, 0xFF] # Coordinador 00 00 / #broadcast FF FF Pan_Id = self.PanId # PanID que estabece el coordinador LSB -MSB global Seq_M Seq_M[0] = Seq_M[0] + 1 if Seq_M[0] > 254: Seq_M[0] = 0 FC_M = [0x41, 0x88] # Frame control Mac LSB-MSB to_fcs = FC_M + Seq_M + Pan_Id + Dst_Add + Src_Add + npdu s_to_fcs = struct.pack("B" * len(to_fcs), *to_fcs) # Convierte List a String a = self.crc16(s_to_fcs) # Calcula FCS FCS = [a & 0xFF, (a >> 8) & 0xFF] MPDU = FC_M + Seq_M + Pan_Id + Dst_Add + Src_Add + npdu + FCS send_pmt = pmt.make_u8vector(len(MPDU), ord(" ")) # Crea PMT vacio for i in range(len(MPDU)): pmt.u8vector_set(send_pmt, i, MPDU[i]) # Copia Caracteres a u8vector: # Envia mensaje: self.message_port_pub(pmt.intern("out"), pmt.cons(pmt.PMT_NIL, send_pmt))
def to_spec_sense(self, meta, data): meta = pmt.to_pmt(meta) data = pmt.to_pmt(data) pdu = pmt.cons(meta, data) #make the PDU #publish PDU to msg port self.message_port_pub(pmt.intern('to_spect_sens'),pdu) return None
def deframe_MSJ(self, msg_pmt): if pmt.is_blob(msg_pmt): blob = msg_pmt # print "is blob" elif pmt.is_pair(msg_pmt): blob = pmt.cdr(msg_pmt) # print "is pair" else: print "Formato desconocido" return data_np = pmt.to_python(blob) # numpy.ndarray data_py = data_np.tolist() # python list # print "Paquete",data_py,data_py.__class__ index = 8 del data_py[:index] # FC (1), DesEP(1),Cluster(2),Profile(2),SourEP(1),Counter(1) # print "DEPACKED-->",data_py # Crea un PMT vacio: send_pmt = pmt.make_u8vector(len(data_py), ord(" ")) # Copy all characters to the u8vector: for i in range(len(data_py)): pmt.u8vector_set(send_pmt, i, data_py[i]) # Send the message: self.message_port_pub(pmt.intern("out"), pmt.cons(pmt.PMT_NIL, send_pmt))
def post_message(self, msg_str): """ Take a string, remove all non-printable characters, prepend the prefix and post to the next block. """ # Do string sanitization: #msg_str = filter(lambda x: x in string.printable, msg_str) #send_str = "[{}] {}".format(self.prefix, msg_str) send_str = msg_str # prepend AC to sent packet if self.access_code: (packed_access_code, padded) = digital.packet_utils.conv_1_0_string_to_packed_binary_string(self.access_code) print self.access_code print map(ord, packed_access_code) print padded send_str = packed_access_code + send_str if self.AESkey: send_str = self.crypter.encrypt(send_str) #print len(send_str) # Create an empty PMT (contains only spaces): send_pmt = pmt.make_u8vector(len(send_str), ord(' ')) # Copy all characters to the u8vector: for i in range(len(send_str)): pmt.u8vector_set(send_pmt, i, ord(send_str[i])) # Send the message: self.message_port_pub(pmt.intern('out'), pmt.cons(pmt.PMT_NIL, send_pmt))
def _to_ofdm_radio(self, pdu_tuple, pkt_cnt, protocol_id, control): meta_data = pdu_tuple[1] payload = pdu_tuple[0] if payload is None: payload = [] elif isinstance(payload, str): payload = map(ord, list(payload)) elif not isinstance(payload, list): payload = list(payload) dest_addr = meta_data['EM_DEST_ADDR'] if dest_addr == -1: dest_addr = BROADCAST_ADDR elif dest_addr < -1 or dest_addr > BROADCAST_ADDR: print "Invalid address:", dest_addr return #create header, merge with payload, convert to pmt for message_pub data = [pkt_cnt, self.address, dest_addr, protocol_id, control] data += payload data = pmt.init_u8vector(len(data), data) meta = pmt.to_pmt({}) #construct pdu and publish to radio port pdu = pmt.cons(meta, data) #publish to msg port self.message_port_pub(pmt.intern('to_ofdm_radio'),pdu)
def work(self, input_items, output_items): in0 = input_items[0] for x in in0: if x: self.ones += 1 self.bits.append(x) else: if self.ones == 5: # destuff = do nothing None elif self.ones > 5: # should be ones == 6 unless packet is corrupted # flag received # prepare to send frame for _ in range(min(7, len(self.bits))): self.bits.pop() # remove 7 previous flag bits if len(self.bits) % 8: # pad on the left with 0's self.bits.extendleft([0] * (8 - len(self.bits) % 8)) frame = pack(self.bits) self.bits.clear() if frame and (not self.check or fcs_ok(frame)): # send frame buff = array.array('B', frame[:-2]) # trim fcs self.message_port_pub(pmt.intern('out'), pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(len(buff), buff))) else: self.bits.append(x) self.ones = 0 return len(input_items[0])
def handle_msg(self, msg_pmt): msg = pmt.cdr(msg_pmt) if not pmt.is_u8vector(msg): print "[ERROR] Received invalid message type. Expected u8vector" return data = list(pmt.u8vector_elements(msg)) crc = hdlc.crc_ccitt(data) data.append(crc & 0xff) data.append((crc >> 8) & 0xff) buff = hdlc.flag * self.preamble_bytes ones = 0 # number of consecutive ones for byte in data: for _ in range(8): # transmit byte LSB first x = byte & 1 buff.append(x) if x: ones += 1 else: ones = 0 if ones == 5: # bit-stuff buff.append(0) ones = 0 byte >>= 1 buff.extend(hdlc.flag * self.postamble_bytes) buff = array.array('B', buff) self.message_port_pub(pmt.intern('out'), pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(len(buff), buff)))
def send_pkt_radio(self, payload, meta_dict, pkt_cnt): # sink in Sink table? if self.SINK_ADDR not in self.sinkTable.keys(): if self.debug_stderr: # yes! log the packet sys.stderr.write( "%d:in send_pkt_radio(): dropping packet\n" % self.addr) # drop the packet return # yes! get the value paired with the sink key aSinkVal = self.sinkTable[self.SINK_ADDR] # yes! data packet header structure data = [self.DATA_PROTO, self.addr, self.addr, pkt_cnt, aSinkVal.min_dx_to_sink, self.SINK_ADDR, aSinkVal.min_dx_to_sink + self.R] # add payload if payload is None: payload = [] elif isinstance(payload, str): payload = map(ord, list(payload)) elif not isinstance(payload, list): payload = list(payload) data += payload # debug mode enabled? if self.debug_stderr: # yes! log the packet sys.stderr.write( "%d:in send_pkt_radio(): sending packet:\n" % self.addr) self.print_pkt(data) # conversion to PMT PDU (meta data, data) pdu = pmt.cons( pmt.to_pmt({}), pmt.init_u8vector(len(data), data)) # push to radio msg port self.message_port_pub(pmt.intern('to_radio'), pdu)
def handler(self, pdu): meta = pmt.car(pdu) vec = pmt.to_python(pmt.cdr(pdu)) idx = numpy.argmax(numpy.abs(vec) > self.max_item) if(not idx == 0): vec = vec[0:idx] self.message_port_pub(pmt.intern("pdus"), pmt.cons( meta, pmt.to_pmt(vec) ) );
def deframe_APDU(self, msg_pmt): if pmt.is_blob(msg_pmt): blob = msg_pmt # print "is blob" elif pmt.is_pair(msg_pmt): blob = pmt.cdr(msg_pmt) # print "is pair" else: print "Formato desconocido" return data_np = pmt.to_python(blob) # numpy.ndarray data_py = data_np.tolist() # python list # print "Paquete",data_py,data_py.__class__ if data_py[2:4] == [0xFF, 0xFF]: index = 16 else: index = 24 del data_py[:index] # FC(2),Dst_Add(2),Src_Add(2),Radio(1),Sec_N(1),X->Dst_IEEE(8), Src_IEEE(8) # print "DEPACKED-->",data_py # Crea un PMT vacio: send_pmt = pmt.make_u8vector(len(data_py), ord(" ")) # Copy all characters to the u8vector: for i in range(len(data_py)): pmt.u8vector_set(send_pmt, i, data_py[i]) # Send the message: self.message_port_pub(pmt.intern("out"), pmt.cons(pmt.PMT_NIL, send_pmt))
def handle_msg(self, msg_pmt): msg = pmt.cdr(msg_pmt) if not pmt.is_u8vector(msg): print "[ERROR] Received invalid message type. Expected u8vector" return packet = str(bytearray(pmt.u8vector_elements(msg))) data = None try: if self.verbose: print "Trying to decode as long packet: 250 FEC bytes, 92 data bytes" (data, bit_corr, byte_corr) = self.ec.decode(packet[1:]) except Exception as ex: if self.verbose: print(ex) try: if self.verbose: print "Trying to decode as short packet: 128 FEC bytes, 31 data bytes" (data, bit_corr, byte_corr) = self.ec.decode(packet[1:1 + 128]) except Exception as ex: if self.verbose: print(ex) if data: if self.verbose: print "FEC decoded OK. Bit errors: {}. Byte errors {}".format(bit_corr, byte_corr) data = data[:-2] # strip out HMAC self.message_port_pub(pmt.intern('out'), pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(len(data), bytearray(data))))
def test_004_es_source_pdus(self): print "test_004_es_source_pdus" msg = pmt.cons( pmt.to_pmt( {"somekey":"val2", "somekey2":"someval2" } ), pmt.to_pmt( numpy.array( [0,1,2,3,4,5,6,7,8,9] , dtype=numpy.float32) ) ); src = es.source([gr.sizeof_float], 8, 2); stb = blocks.message_strobe( msg, 100.0 ); tb = gr.top_block(); tb.msg_connect(stb, "strobe", src, "schedule_event"); th = blocks.throttle(gr.sizeof_float, 1000*100); hd = blocks.head(gr.sizeof_float, 1000*100); snk = blocks.vector_sink_f(); tb.connect(src,th,hd,snk); # TODO: this can not use run because it is # subject to GNU Radio's shutdown msg block bug # for remaining upstream msg blocks ... #tb.run(); # workaround tb.start(); time.sleep(1); tb.stop(); tb.wait(); self.assertEqual( sum(snk.data())>0, True );
def handle_msg(self, msg_pmt): msg = pmt.cdr(msg_pmt) if not pmt.is_u8vector(msg): print "[ERROR] Received invalid message type. Expected u8vector" return self.kiss.extend(pmt.u8vector_elements(msg)) while self.kiss: c = self.kiss.popleft() if c == FEND: if self.pdu and not self.pdu[0] & 0x0f: msg = array.array('B', self.pdu[1:]) self.message_port_pub(pmt.intern('out'), pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(len(msg), msg))) self.pdu = list() elif self.transpose: if c == TFEND: self.pdu.append(FEND) elif c == TFESC: self.pdu.append(FESC) self.transpose = False elif c == FESC: self.transpose = True else: self.pdu.append(c)
def get_pmt_from_data_str(self, msg_str): send_pmt = pmt.make_u8vector(len(msg_str), ord(' ')) for i in range(len(msg_str)): pmt.u8vector_set(send_pmt, i, ord(msg_str[i])) send_pmt = pmt.cons(pmt.PMT_NIL, send_pmt) return send_pmt
def test_001(self): #test complementary operation of framer & deframer #want to frame some random data that has enough consecutive bits to #stuff at least a few bits npkts = 20 src_data = [0xFE, 0xDA, 0xAC, 0x29, 0x7F, 0xA2, 0x90, 0x0F, 0xF8] frame = digital.hdlc_framer_pb("wat") corr = digital.correlate_access_code_tag_bb("01111110", 0, "frame") deframe = digital.hdlc_deframer_bp("frame", 32, 500) debug = blocks.message_debug() self.tb.connect(frame, corr, deframe) self.tb.msg_connect(deframe, "out", debug, "store") self.tb.start() msg = pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(len(src_data),src_data)) for i in xrange(npkts): frame.to_basic_block()._post(pmt.intern("in"), msg) sleep(0.2) self.tb.stop() self.tb.wait() rxmsg = debug.get_message(0) result_len = pmt.blob_length(pmt.cdr(rxmsg)) msg_data = [] for j in xrange(result_len): msg_data.append(pmt.u8vector_ref(pmt.cdr(rxmsg), j)) self.assertEqual(src_data, msg_data)
def test_001_null_dict (self): in_data = [0, 0, 0, 0] in_meta = pmt.make_dict() expected_meta = pmt.dict_add(in_meta, pmt.intern('num'), pmt.from_long(4)) in_pdu = pmt.cons(in_meta, pmt.init_u8vector(len(in_data), in_data)) expected_pdu = pmt.cons(expected_meta, pmt.init_u8vector(len(in_data), in_data)) self.tb.start() time.sleep(.001) self.emitter.emit(pmt.intern("MALFORMED PDU")) time.sleep(.001) self.emitter.emit(pmt.cons(pmt.from_long(0), pmt.PMT_T)) time.sleep(.001) self.emitter.emit(in_pdu) time.sleep(.01) self.tb.stop() self.tb.wait() self.assertTrue(pmt.equal(self.debug.get_message(0), expected_pdu))
def test_003_match_offseta (self): self.dut = pdu_utils.pdu_align('01010101', 0, 4, pdu_utils.ALIGN_DROP) self.connectUp() in_data = [0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1] expected_data = [ 1, 1, 1, 1 ] in_pdu = pmt.cons(pmt.make_dict(), pmt.init_u8vector(len(in_data), in_data)) expected_pdu = pmt.cons(pmt.make_dict(), pmt.init_u8vector(len(expected_data), expected_data)) self.tb.start() time.sleep(.001) self.emitter.emit(in_pdu) time.sleep(.01) self.tb.stop() self.tb.wait() self.assertEqual(1, self.debug.num_messages()) self.assertTrue(pmt.equal(self.debug.get_message(0), expected_pdu))
def test_002_pack_LSB (self): self.pack.set_mode(pdu_utils.MODE_PACK_BYTE) self.pack.set_bit_order(pdu_utils.BIT_ORDER_LSB_FIRST) in_data = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0] expected_data = [0, 255, 170, 0] in_pdu = pmt.cons(pmt.make_dict(), pmt.init_u8vector(len(in_data), in_data)) expected_pdu = pmt.cons(pmt.make_dict(), pmt.init_u8vector(len(expected_data), expected_data)) self.tb.start() time.sleep(.001) self.emitter.emit(pmt.cons(pmt.PMT_T, pmt.intern("ANOTHER MALFORMED PDU"))) time.sleep(.001) self.emitter.emit(in_pdu) time.sleep(.01) self.tb.stop() self.tb.wait() self.assertTrue(pmt.equal(self.debug.get_message(0), expected_pdu))
def handle_msg(self, msg_pmt): msg = pmt.cdr(msg_pmt) if not pmt.is_u8vector(msg): print "[ERROR] Received invalid message type. Expected u8vector" return packet = array.array("B", pmt.u8vector_elements(msg)) crc = crc32c.crc(packet if self.include_header else packet[4:]) packet += array.array("B", struct.pack(">I", crc)) self.message_port_pub(pmt.intern('out'), pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(len(packet), packet)))
def sendState(self,state): meta = {} if (state): meta['state'] = 1 else: meta['state'] = 0 self.message_port_pub(pmt.intern("state"),pmt.cons( pmt.to_pmt(meta), pmt.PMT_NIL ))
def test_001_nochange(self): in_data = range(8) expected_data = range(8) in_pdu = pmt.cons(pmt.make_dict(), pmt.init_u8vector(len(in_data), in_data)) expected_pdu = pmt.cons( pmt.make_dict(), pmt.init_u8vector(len(expected_data), expected_data)) self.tb.start() time.sleep(.001) self.emitter.emit(pmt.intern("BAD INPUT")) time.sleep(.001) self.emitter.emit(in_pdu) time.sleep(.01) self.tb.stop() self.tb.wait() self.assertTrue(pmt.equal(self.debug.get_message(0), expected_pdu))
def handler(self, msg): if (random.uniform(0, 1) >= self.p_drop): #print "RANDOM_DROP: PASSING" t = threading.Thread(target=self.run) self.q.put(pmt.cons(pmt.car(msg), pmt.cdr(msg))) t.start() # , args=(msg)).start(); #self.message_port_pub(pmt.intern("pdus"), msg); else: pass
def test_003_double_msg(self): in_data1 = [0, 0, 0, 0, 0, 0, 0, 0] in_data2 = [1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1] in_data = in_data1 + in_data1 + in_data2 tag_time = pmt.make_tuple(pmt.from_uint64(11), pmt.from_double(0.123456)) in_dict = pmt.dict_add(pmt.make_dict(), pmt.intern("tx_time"), tag_time) in_pdu1 = pmt.cons(in_dict, pmt.init_c32vector(len(in_data1), in_data1)) in_pdu2 = pmt.cons(pmt.make_dict(), pmt.init_c32vector(len(in_data2), in_data2)) e_tag_0 = gr.tag_utils.python_to_tag( (len(in_data1), pmt.intern("tx_sob"), pmt.PMT_T, pmt.PMT_NIL)) e_tag_1 = gr.tag_utils.python_to_tag( (len(in_data1), pmt.intern("tx_time"), tag_time, pmt.PMT_NIL)) e_tag_2 = gr.tag_utils.python_to_tag( (len(in_data) - 1, pmt.intern("tx_eob"), pmt.PMT_T, pmt.PMT_NIL)) self.tb.start() time.sleep(.001) self.emitter.emit(pmt.intern("MALFORMED PDU")) time.sleep(.001) self.emitter.emit(in_pdu1) time.sleep(.005) self.emitter.emit(in_pdu1) self.emitter.emit(in_pdu2) time.sleep(.01) self.tb.stop() self.tb.wait() tags = self.vs.tags() self.assertEqual(len(tags), 6) self.assertEqual(tags[3].offset, e_tag_0.offset) self.assertTrue(pmt.equal(tags[3].key, e_tag_0.key)) self.assertTrue(pmt.equal(tags[3].value, e_tag_0.value)) self.assertEqual(tags[4].offset, e_tag_1.offset) self.assertTrue(pmt.equal(tags[4].key, e_tag_1.key)) self.assertTrue(pmt.equal(tags[4].value, e_tag_1.value)) self.assertEqual(tags[5].offset, e_tag_2.offset) self.assertTrue(pmt.equal(tags[5].key, e_tag_2.key)) self.assertTrue(pmt.equal(tags[5].value, e_tag_2.value)) self.assertTrue((in_data == numpy.real(self.vs.data())).all())
def notifyUpdate(self, new_val): if self.callback is not None: if new_val: self.callback(self.pressReleasedDict['Pressed']) else: self.callback(self.pressReleasedDict['Released']) if new_val: if type(self.pressReleasedDict['Pressed']) == bool: self.message_port_pub( pmt.intern("state"), pmt.cons(pmt.intern(self.outputmsgname), pmt.from_bool(self.pressReleasedDict['Pressed']))) elif type(self.pressReleasedDict['Pressed']) == int: self.message_port_pub( pmt.intern("state"), pmt.cons(pmt.intern(self.outputmsgname), pmt.from_long(self.pressReleasedDict['Pressed']))) elif type(self.pressReleasedDict['Pressed']) == float: self.message_port_pub( pmt.intern("state"), pmt.cons( pmt.intern(self.outputmsgname), pmt.from_double(self.pressReleasedDict['Pressed']))) else: self.message_port_pub( pmt.intern("state"), pmt.cons(pmt.intern(self.outputmsgname), pmt.intern(self.pressReleasedDict['Pressed']))) else: if type(self.pressReleasedDict['Released']) == bool: self.message_port_pub( pmt.intern("state"), pmt.cons(pmt.intern(self.outputmsgname), pmt.from_bool( self.pressReleasedDict['Released']))) elif type(self.pressReleasedDict['Released']) == int: self.message_port_pub( pmt.intern("state"), pmt.cons(pmt.intern(self.outputmsgname), pmt.from_long( self.pressReleasedDict['Released']))) elif type(self.pressReleasedDict['Released']) == float: self.message_port_pub( pmt.intern("state"), pmt.cons( pmt.intern(self.outputmsgname), pmt.from_double(self.pressReleasedDict['Released']))) else: self.message_port_pub( pmt.intern("state"), pmt.cons(pmt.intern(self.outputmsgname), pmt.intern(self.pressReleasedDict['Released'])))
def test_003_unpack_MSB (self): self.pack.set_mode(pdu_utils.MODE_UNPACK_BYTE) self.pack.set_bit_order(pdu_utils.BIT_ORDER_MSB_FIRST) in_data = [123, 209, 17, 35] expected_data = [0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1] in_pdu = pmt.cons(pmt.make_dict(), pmt.init_u8vector(len(in_data), in_data)) expected_pdu = pmt.cons(pmt.make_dict(), pmt.init_u8vector(len(expected_data), expected_data)) self.tb.start() time.sleep(.001) self.emitter.emit(pmt.cons(pmt.intern("NON U8 PDU"), pmt.init_u16vector(2,[1,2]))) time.sleep(.001) self.emitter.emit(in_pdu) time.sleep(.01) self.tb.stop() self.tb.wait() self.assertTrue(pmt.equal(self.debug.get_message(0), expected_pdu))
def tearDown(self): self.tb.msg_connect((self.encode, 'out'), (self.decode, 'in')) self.tb.msg_connect((self.decode, 'out'), (self.dbg, 'store')) pdu = pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(len(self.data), self.data)) self.encode.to_basic_block()._post(pmt.intern('in'), pdu) self.encode.to_basic_block()._post( pmt.intern('system'), pmt.cons(pmt.intern('done'), pmt.from_long(1))) self.tb.start() self.tb.wait() result = bytes(pmt.u8vector_elements(pmt.cdr(self.dbg.get_message(0)))) self.assertEqual(self.data, result, 'Decoded data does not match encoder input') self.tb = None
def test_001(self): # Test the overflow buffer in pdu_to_tagged_stream src_data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0] src = blocks.pdu_to_tagged_stream(blocks.float_t) snk = blocks.vector_sink_f() self.tb.connect(src, snk) port = pmt.intern("pdus") msg = pmt.cons(pmt.PMT_NIL, pmt.init_f32vector(10, src_data)) src.to_basic_block()._post(port, msg) src.to_basic_block()._post( pmt.intern("system"), pmt.cons(pmt.intern("done"), pmt.from_long(1))) self.tb.start() self.tb.wait() self.assertEqual(src_data, list(snk.data()))
def test_noError(self): input_vector = pmt.init_u8vector( 56, PREAMBLE + ENCODED1 + ENCODED2 + POSTAMBLE) message_car = pmt.dict_add(pmt.make_dict(), pmt.intern('key'), pmt.intern('value')) self.tb.start() self.decoder._post(pmt.intern('in'), pmt.cons(message_car, input_vector)) time.sleep(0.1) self.tb.stop() self.tb.wait() self.assertEqual(self.snk.num_messages(), 1) expected_vector = pmt.init_u8vector( 32, PREAMBLE + MESSAGE1 + MESSAGE2 + POSTAMBLE) self.assertTrue( pmt.equal(self.snk.get_message(0), pmt.cons(message_car, expected_vector)))
def test_000(self): # Just run some data through and make sure it doesn't puke. src_data = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) src = blocks.pdu_to_tagged_stream(blocks.byte_t) snk3 = blocks.tagged_stream_to_pdu(blocks.byte_t) snk2 = blocks.vector_sink_b() snk = blocks.tag_debug(1, "test") snk.set_display(False) dbg = blocks.message_debug() # Test that the right number of ports exist. pi = snk3.message_ports_in() po = snk3.message_ports_out() self.assertEqual(pmt.length(pi), 0) self.assertEqual(pmt.length(po), 1) self.tb.connect(src, snk) self.tb.connect(src, snk2) self.tb.connect(src, snk3) self.tb.msg_connect(snk3, "pdus", dbg, "store") self.tb.start() # make our reference and message pmts port = pmt.intern("pdus") msg = pmt.cons(pmt.PMT_NIL, pmt.make_u8vector(16, 0xFF)) # post the message src.to_basic_block()._post(port, msg) # eww, what's that smell? while dbg.num_messages() < 1: time.sleep(0.1) self.tb.stop() self.tb.wait() # Get the vector of data from the vector sink result_data = snk2.data() # Get the vector of data from the message sink # Convert the message PMT as a pair into its vector result_msg = dbg.get_message(0) msg_vec = pmt.cdr(result_msg) #pmt.print(msg_vec) # Convert the PMT vector into a Python list msg_data = [] for i in xrange(16): msg_data.append(pmt.u8vector_ref(msg_vec, i)) actual_data = 16 * [ 0xFF, ] self.assertEqual(actual_data, list(result_data)) self.assertEqual(actual_data, msg_data)
def test_002_pass_empty(self): split = pdu.pdu_split(True) d1 = blocks.message_debug() d2 = blocks.message_debug() self.tb.msg_connect((split, 'dict'), (d1, 'store')) self.tb.msg_connect((split, 'vec'), (d2, 'store')) in_meta1 = pmt.dict_add(pmt.make_dict(), pmt.intern('num'), pmt.from_long(4)) in_meta2 = pmt.dict_add(pmt.make_dict(), pmt.intern('n'), pmt.from_long(99)) in_pdu = pmt.cons(in_meta1, pmt.init_u8vector(6, range(6))) self.tb.start() split.to_basic_block()._post(pmt.intern("pdus"), pmt.intern("MALFORMED PDU")) split.to_basic_block()._post( pmt.intern("pdus"), pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(2, range(2)))) split.to_basic_block()._post( pmt.intern("pdus"), pmt.cons(in_meta2, pmt.init_u8vector(0, []))) split.to_basic_block()._post(pmt.intern("pdus"), in_pdu) split.to_basic_block()._post( pmt.intern("system"), pmt.cons(pmt.intern("done"), pmt.from_long(1))) self.waitFor(lambda: d1.num_messages() == 3, timeout=1.0, poll_interval=0.01) self.waitFor(lambda: d2.num_messages() == 3, timeout=1.0, poll_interval=0.01) self.tb.wait() self.assertTrue(pmt.equal(d1.get_message(0), pmt.PMT_NIL)) self.assertTrue(pmt.equal(d1.get_message(1), in_meta2)) self.assertTrue(pmt.equal(d1.get_message(2), in_meta1)) self.assertTrue( pmt.equal(d2.get_message(0), pmt.init_u8vector(2, range(2)))) self.assertTrue(pmt.equal(d2.get_message(1), pmt.init_u8vector(0, []))) self.assertTrue( pmt.equal(d2.get_message(2), pmt.init_u8vector(6, range(6))))
def onBtnClicked(self, pressed): if type(self.msgValue) == int: self.message_port_pub( pmt.intern("pressed"), pmt.cons(pmt.intern(self.msgName), pmt.from_long(self.msgValue))) elif type(self.msgValue) == float: self.message_port_pub( pmt.intern("pressed"), pmt.cons(pmt.intern(self.msgName), pmt.from_float(self.msgValue))) elif type(self.msgValue) == str: self.message_port_pub( pmt.intern("pressed"), pmt.cons(pmt.intern(self.msgName), pmt.intern(self.msgValue))) elif type(self.msgValue) == bool: self.message_port_pub( pmt.intern("pressed"), pmt.cons(pmt.intern(self.msgName), pmt.from_bool(self.msgValue)))
def output_user_data(self, pdu_tuple): data = [] if len(pdu_tuple[0]) > PKT_INDEX_MAX: data = pdu_tuple[0][PKT_INDEX_MAX:] data = pmt.init_u8vector(len(data), data) #pass through metadata if there is any meta = pmt.to_pmt(pdu_tuple[1]) self.message_port_pub(pmt.intern('to_app'), pmt.cons(meta, data))
def post_data(self, data, type=None, arg1=None, arg2=None): ok = True payload = "" if len(data) > 0: ok, payload = packet_utils.unmake_packet(data, int(arg1), self.dewhiten) if not ok: if self.log_invalid: print "Packet of length %d failed CRC" % (len(data)) # Max len is 4095 #if not self.output_invalid: # return payload = map(ord, list(payload)) buf = pmt.init_u8vector(len(payload), payload) meta_dict = {'CRC_OK': ok} if len(data) == 0: meta_dict['EMPTY'] = True meta = pmt.to_pmt(meta_dict) if ok or (self.output_invalid and not ok): self.message_port_pub(pmt.intern('pdu'), pmt.cons(meta, buf)) else: self.message_port_pub(pmt.intern('invalid'), pmt.cons(meta, buf))
def _packetToPDU(self): self.message_port_pub(pmt.intern('rmsg'), pmt.intern('paring packet')) if (len(self._packet)%self._size == 0): numberOfBytes = int(round(len(self._packet)/self._size)) stream = pmt.make_u8vector(numberOfBytes,0) for by in range(numberOfBytes): element = int(self._packet[by*self._size:(by+1)*self._size],2) pmt.u8vector_set(stream, by, element) pdu = pmt.cons(pmt.make_dict(),stream) self.message_port_pub(pmt.intern('rmsg'),pmt.intern('sending pdu')) self.message_port_pub(pmt.intern("PDUout"), pdu)
def handler(self, pdu): # grab float vector from pdu meta = pmt.car(pdu) x = pmt.to_python(pmt.cdr(pdu)) # convert to uint8 vector of mapped bits bindata = numpy.array(map(lambda x: x > 0, x), dtype='uint8') # send it on its way self.message_port_pub(pmt.intern("pdus"), pmt.cons(meta, pmt.to_pmt(bindata)))
def handler(self, msg): meta = pmt.car(msg) data_in = pmt.cdr(msg) data = array.array('B', pmt.u8vector_elements(data_in)) #data = array.array('B', pmt.u8vector_elements(data_in)) pre_data = self.preamble + data # print pre_data[:100]; #burst_bits = pmt.to_pmt(pre_data); burst_bits = pmt.init_u8vector(len(pre_data), pre_data.tolist()) pdu = pmt.cons(meta, burst_bits) self.message_port_pub(pmt.intern("pdus"), pdu)
def send_frame(self, timestamp, center_frequency, confidence): msg_meta = pmt.dict_add(pmt.make_dict(), pmt.intern('timestamp'), pmt.from_uint64(timestamp)) msg_meta = pmt.dict_add(msg_meta, pmt.intern('center_frequency'), pmt.from_float(center_frequency)) msg_meta = pmt.dict_add(msg_meta, pmt.intern('confidence'), pmt.from_long(confidence)) msg = pmt.cons(msg_meta, pmt.init_u8vector(2, range(2))) if timestamp > self.max_timestamp: self.max_timestamp = timestamp self.sorter.to_basic_block()._post(pmt.intern("pdus"), msg)
def handle_msg(self, msg_pmt): msg = pmt.cdr(msg_pmt) if not pmt.is_u8vector(msg): print "[ERROR] Received invalid message type. Expected u8vector" return packet = array.array("B", pmt.u8vector_elements(msg)) if len(packet) <= 16: return packet = packet[16:] self.message_port_pub(pmt.intern('out'), pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(len(packet), packet)))
def handle_msg(self, msg_pmt): msg_pmt = pmt.pmt_to_python.pmt_to_python(msg_pmt) msg = bytearray(msg_pmt[1]) try: pkt = packet.from_bytes(msg) self.message_port_pub(pmt.intern('ax25 out'), pmt.cons(pmt.make_dict(), pmt.pmt_to_python.numpy_to_uvector(np.array([ord(c) for c in (packet.dump(pkt) + '\n')], np.uint8)))) except ValueError as e: print e
def publish_unknown_pdu(self): unknown = dict() unknown["timestamp"] = self.timestamp unknown["datetime"] = self.datetime unknown["df"] = self.df unknown["snr"] = self.snr meta = pmt.to_pmt(unknown) vector = pmt.to_pmt(self.bits) pdu = pmt.cons(meta, vector) self.message_port_pub(pmt.to_pmt("unknown"), pdu)
def handle_msg(self, msg_pmt): msg = pmt.cdr(msg_pmt) if not pmt.is_u8vector(msg): print("[ERROR] Received invalid message type. Expected u8vector") return packet = pmt.u8vector_elements(msg) self.message_port_pub( pmt.intern('out'), pmt.cons(pmt.car(msg_pmt), pmt.init_u8vector(len(packet) - 1, packet[1:])))
def loop(self): while True: if self.socket.poll(timeout=1) > 0: buff = self.socket.recv() buff = [ord(i) for i in buff] print buff pkt = pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(len(buff), buff)) self.message_port_pub(pmt.intern('out'), pkt)
def test_001_no_change(self): orig_array = np.array([1, 0, 1, 0, 1, 0, 1], dtype=np.uint8) metadata = {'a': 1, 'b': 2} in_pmt = pmt.cons(pmt.to_pmt(metadata), pmt.to_pmt(orig_array)) expected_pmt = pmt.cons(pmt.to_pmt(metadata), pmt.to_pmt(orig_array)) appender = add_sync_pdu(0, 0) snk = blocks.message_debug() self.tb.msg_connect((appender, 'out'), (snk, 'store')) self.tb.start() appender.to_basic_block()._post(pmt.intern('in'), in_pmt) time.sleep(0.1) self.tb.stop() self.tb.wait() # check data self.assertEqual(snk.num_messages(), 1) self.assertTrue(pmt.equal(snk.get_message(0), expected_pmt))
def setUp(self): """Common part of all CRC tests Creates a flowgraph, a Message Debug block, and a PDU containing the numbers 0x00 through 0x0F. """ self.tb = gr.top_block() self.dbg = blocks.message_debug() self.data = list(range(16)) self.pdu = pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(len(self.data), self.data))
def test_005_pack_sparse(self): self.pack.set_mode(pdu_utils.MODE_PACK_BYTE) self.pack.set_bit_order(pdu_utils.BIT_ORDER_MSB_FIRST) in_data = [0, 23, 0, 255, 1, 3] expected_data = [92] in_pdu = pmt.cons(pmt.make_dict(), pmt.init_u8vector(len(in_data), in_data)) expected_pdu = pmt.cons( pmt.make_dict(), pmt.init_u8vector(len(expected_data), expected_data)) self.tb.start() time.sleep(.001) self.emitter.emit(in_pdu) time.sleep(.01) self.tb.stop() self.tb.wait() self.assertTrue(pmt.equal(self.debug.get_message(0), expected_pdu))