def test_endian(self): self.emitter = pdu_utils.message_emitter() #self.endi = pdu_utils.pdu_binary_tools(pdu_utils.pdu_binary_tools.ENDIAN_SWAP8) self.endi = pdu_utils.pdu_binary_tools(4) self.debug = blocks.message_debug() self.tb.msg_connect((self.emitter, 'msg'), (self.endi, 'pdu_in')) self.tb.msg_connect((self.endi, 'pdu_out'), (self.debug, 'store')) i_vec = pmt.init_u8vector( 16, [1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0]) e_vec = pmt.init_u8vector( 16, [1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1]) in_pdu = pmt.cons(pmt.make_dict(), i_vec) e_pdu = pmt.cons(pmt.make_dict(), e_vec) self.tb.start() time.sleep(.001) self.emitter.emit(in_pdu) time.sleep(.01) self.tb.stop() self.tb.wait() print "test endian_swap8:" print "pdu expected: " + repr(pmt.car(e_pdu)) print "pdu got: " + repr(pmt.car(self.debug.get_message(0))) print "data expected: " + repr(pmt.u8vector_elements(pmt.cdr(e_pdu))) print "data got: " + repr( pmt.u8vector_elements(pmt.cdr(self.debug.get_message(0)))) print self.assertTrue(pmt.equal(self.debug.get_message(0), e_pdu))
def test_001_n4_u8(self): in_data = [0, 1, 2, 4, 8, 16, 32] expected_data1 = [ 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0, 8, 0, 0, 0, 16, 0, 0, 0, 32, 0, 0, 0 ] expected_data2 = [ 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 8, 8, 8, 8, 16, 16, 16, 16, 32, 32, 32, 32 ] in_pdu = pmt.cons(pmt.make_dict(), pmt.init_u8vector(len(in_data), in_data)) expected_pdu1 = pmt.cons( pmt.make_dict(), pmt.init_u8vector(len(expected_data1), expected_data1)) expected_pdu2 = pmt.cons( pmt.make_dict(), pmt.init_u8vector(len(expected_data2), expected_data2)) self.tb.start() time.sleep(.001) self.emitter.emit(pmt.intern("MALFORMED PDU")) time.sleep(.001) self.emitter.emit(in_pdu) time.sleep(.001) self.upsample.set_repeat(True) time.sleep(.001) self.emitter.emit(in_pdu) time.sleep(.05) self.tb.stop() self.tb.wait() self.assertTrue(pmt.equal(self.debug.get_message(0), expected_pdu1)) self.assertTrue(pmt.equal(self.debug.get_message(1), expected_pdu2))
def test_001_t(self): # set up fg msg_meta = pmt.make_dict() msg_meta = pmt.dict_add(msg_meta, pmt.to_pmt("freq"), pmt.to_pmt("val")) vec1 = [ 0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00 ] msg_vector = pmt.init_u8vector(len(vec1), vec1) msg = pmt.cons(msg_meta, msg_vector) src = blocks.message_strobe(msg, 10) dut = capture_tools.bit_sniffer(fade_out=500, hexadecimal=True) self.tb.msg_connect((src, "strobe"), (dut, "packets")) self.tb.start() time.sleep(5) vec1 = [0x01, 0x00, 0x01, 0x00, 0x01, 0x00] msg_vector = pmt.init_u8vector(len(vec1), vec1) msg = pmt.cons(msg_meta, msg_vector) src.set_msg(msg) time.sleep(5) vec1 = [0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x01] msg_vector = pmt.init_u8vector(len(vec1), vec1) msg = pmt.cons(msg_meta, msg_vector) src.set_msg(msg) time.sleep(5) self.tb.stop()
def test_longer(self): self.down = pdu_utils.pdu_downsample(2, 1) self.connectUp() i_vec = pmt.init_u8vector(7, [0, 1, 2, 3, 4, 5, 6]) e_vec = pmt.init_u8vector(3, [1, 3, 5]) in_pdu = pmt.cons(pmt.make_dict(), i_vec) e_pdu = pmt.cons(pmt.make_dict(), e_vec) self.tb.start() time.sleep(.001) self.emitter.emit(in_pdu) time.sleep(.01) self.tb.stop() self.tb.wait() print("test long:") print("pdu expected: " + repr(pmt.car(e_pdu))) print("pdu got: " + repr(pmt.car(self.debug.get_message(0)))) print("data expected: " + repr(pmt.u8vector_elements(pmt.cdr(e_pdu)))) print("data got: " + repr(pmt.u8vector_elements(pmt.cdr(self.debug.get_message(0))))) print self.assertTrue(pmt.equal(self.debug.get_message(0), e_pdu))
def test_longer(self): self.emitter = pdu_utils.message_emitter() self.down = pdu_utils.pdu_downsample(2, 1) self.debug = blocks.message_debug() self.tb.msg_connect((self.emitter, 'msg'), (self.down, 'pdu_in')) self.tb.msg_connect((self.down, 'pdu_out'), (self.debug, 'store')) i_vec = pmt.init_u8vector(7, [0, 1, 2, 3, 4, 5, 6]) e_vec = pmt.init_u8vector(3, [1, 3, 5]) in_pdu = pmt.cons(pmt.make_dict(), i_vec) e_pdu = pmt.cons(pmt.make_dict(), e_vec) self.tb.start() time.sleep(.001) self.emitter.emit(in_pdu) time.sleep(.01) self.tb.stop() self.tb.wait() print "test long:" print "pdu expected: " + repr(pmt.car(e_pdu)) print "pdu got: " + repr(pmt.car(self.debug.get_message(0))) print "data expected: " + repr(pmt.u8vector_elements(pmt.cdr(e_pdu))) print "data got: " + repr( pmt.u8vector_elements(pmt.cdr(self.debug.get_message(0)))) print self.assertTrue(pmt.equal(self.debug.get_message(0), e_pdu))
def test_007_match_errors2(self): ''' Ensure specification of the number of errors in an alignment sequence is properly checked ''' self.dut = pdu_utils.pdu_align('10101010', 2, 0, pdu_utils.ALIGN_EMPTY) self.connectUp() in_data = [1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1] expected_data = [0, 1, 0, 1, 0, 1, 0, 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(.1) self.tb.stop() self.tb.wait() self.assertTrue(pmt.equal(self.debug.get_message(0), expected_pdu))
def test_001_split(self): emitter = pdu_utils.message_emitter() split = pdu_utils.pdu_split() d1 = blocks.message_debug() d2 = blocks.message_debug() self.tb.msg_connect((emitter, 'msg'), (split, 'pdu_in')) self.tb.msg_connect((split, 'dict'), (d1, 'store')) self.tb.msg_connect((split, 'data'), (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() time.sleep(.001) emitter.emit(pmt.intern("MALFORMED PDU")) time.sleep(.001) emitter.emit(pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(2, range(2)))) time.sleep(.001) emitter.emit(pmt.cons(in_meta2, pmt.init_u8vector(0, []))) time.sleep(.001) emitter.emit(in_pdu) time.sleep(.01) self.tb.stop() self.tb.wait() self.assertTrue(pmt.equal(d1.get_message(0), in_meta2)) self.assertTrue(pmt.equal(d1.get_message(1), 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(6, range(6))))
def test_002_timing(self): self.tb.start() self.add_sys_time.to_basic_block()._post(pmt.intern("pdu"), pmt.intern("BAD PDU")) self.add_sys_time.to_basic_block()._post( pmt.intern("pdu"), pmt.cons(pmt.make_dict(), pmt.init_u8vector(1, [0]))) time.sleep( 1.0 ) # wait for one second to provide a time difference between messages self.add_sys_time.to_basic_block()._post( pmt.intern("pdu"), pmt.cons(pmt.make_dict(), pmt.init_u8vector(1, [0]))) self.waitFor(lambda: self.debug.num_messages() == 2, timeout=1.0, poll_interval=0.01) self.tb.stop() self.tb.wait() t0 = pmt.to_double( pmt.dict_ref(pmt.car(self.debug.get_message(0)), pmt.intern("systime"), pmt.from_double(0.0))) t1 = pmt.to_double( pmt.dict_ref(pmt.car(self.debug.get_message(1)), pmt.intern("systime"), pmt.from_double(0.0))) self.assertTrue( ((t1 - t0) - 1) < 0.05) # should be sufficient tolerance
def test_errorCorrected(self): encoded1 = copy.copy(ENCODED1) encoded1[3] = 1 - encoded1[3] encoded1[12] = 1 - encoded1[12] encoded1[17] = 1 - encoded1[17] encoded2 = copy.copy(ENCODED2) encoded2[7] = 1 - encoded2[7] encoded2[10] = 1 - encoded2[10] encoded2[11] = 1 - encoded2[11] 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_005_match_error0b(self): self.dut = pdu_utils.pdu_align('10101010', 1, 0, pdu_utils.ALIGN_DROP) self.connectUp() in_data = [1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1] expected_data = [1, 1, 1, 1, 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()) print("\nEXPECTED: " + repr(pmt.car(expected_pdu))) print("GOT: " + repr(pmt.car(self.debug.get_message(0)))) print("\nEXPECTED: " + repr(pmt.u8vector_elements(pmt.cdr(expected_pdu)))) print("GOT: " + repr(pmt.u8vector_elements(pmt.cdr(self.debug.get_message(0))))) self.assertTrue(pmt.equal(self.debug.get_message(0), expected_pdu))
def test_003_normal_passa(self): self.dut = pdu_utils.pdu_range_filter(pmt.intern("start_time"), 1, 5, False) self.connectUp() in_data = [0, 0, 0, 0, 0, 0] i_meta = pmt.dict_add(pmt.make_dict(), pmt.intern("start_time"), pmt.from_double(1.0)) in_pdu = pmt.cons(i_meta, pmt.init_u8vector(len(in_data), in_data)) expected_data = [0, 0, 0, 0, 0, 0] e_meta = pmt.dict_add(pmt.make_dict(), pmt.intern("start_time"), pmt.from_double(1.0)) expected_pdu = pmt.cons( e_meta, 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_callbacks (self): in_data = [0, 0, 0, 0] in_meta = pmt.make_dict() expected_meta1 = pmt.dict_add(in_meta, pmt.intern('num'), pmt.from_long(4)) expected_meta2 = pmt.dict_add(pmt.dict_add(in_meta, pmt.intern('num'), pmt.from_long(4)), pmt.intern('name'), pmt.intern('param1')) expected_meta3 = pmt.dict_add(pmt.dict_add(in_meta, pmt.intern('name'), pmt.intern('param1')), pmt.intern('num'), pmt.from_long(1)) in_pdu = pmt.cons(in_meta, pmt.init_u8vector(len(in_data), in_data)) expected_pdu1 = pmt.cons(expected_meta1, pmt.init_u8vector(len(in_data), in_data)) expected_pdu2 = pmt.cons(expected_meta2, pmt.init_u8vector(len(in_data), in_data)) expected_pdu3 = pmt.cons(expected_meta3, 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(in_pdu) time.sleep(.001) self.set.set_key(pmt.intern('name')) self.set.set_val(pmt.intern('param1')) time.sleep(.001) self.emitter.emit(self.debug.get_message(0)) time.sleep(.001) self.set.set_kv(pmt.intern('num'), pmt.from_long(1)) time.sleep(.001) self.emitter.emit(self.debug.get_message(1)) time.sleep(.01) self.tb.stop() self.tb.wait() self.assertTrue(pmt.equal(self.debug.get_message(0), expected_pdu1)) self.assertTrue(pmt.equal(self.debug.get_message(1), expected_pdu2)) self.assertTrue(pmt.equal(self.debug.get_message(2), expected_pdu3))
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 test_002_larger(self): self.ht.set_length(32) in_data = [ 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1 ] in_pdu = pmt.cons(pmt.make_dict(), pmt.init_u8vector(len(in_data), in_data)) expected_head = [ 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 ] e_head_pdu = pmt.cons( pmt.make_dict(), pmt.init_u8vector(len(expected_head), expected_head)) expected_tail = [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1 ] e_tail_pdu = pmt.cons( pmt.make_dict(), pmt.init_u8vector(len(expected_tail), expected_tail)) self.tb.start() time.sleep(.001) self.emitter.emit(pmt.intern("MALFORMED PDU")) time.sleep(.001) self.emitter.emit(in_pdu) time.sleep(.01) self.tb.stop() self.tb.wait() self.assertTrue(pmt.equal(self.d_head.get_message(0), e_head_pdu)) self.assertTrue(pmt.equal(self.d_tail.get_message(0), e_tail_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 test_bit_flip(self): self.emitter = pdu_utils.message_emitter() #self.flip = pdu_utils.pdu_binary_tools(pdu_utils.pdu_binary_tools.BIT_FLIP) self.flip = pdu_utils.pdu_binary_tools(0) self.debug = blocks.message_debug() self.tb.msg_connect((self.emitter, 'msg'), (self.flip, 'pdu_in')) self.tb.msg_connect((self.flip, 'pdu_out'), (self.debug, 'store')) i_vec = pmt.init_u8vector(6, [1, 0, 0, 1, 0, 1]) e_vec = pmt.init_u8vector(6, [0, 1, 1, 0, 1, 0]) in_pdu = pmt.cons(pmt.make_dict(), i_vec) e_pdu = pmt.cons(pmt.make_dict(), e_vec) self.tb.start() time.sleep(.001) self.emitter.emit(in_pdu) time.sleep(.01) self.tb.stop() self.tb.wait() print("test bit_flip:") print("pdu expected: " + repr(pmt.car(e_pdu))) print("pdu got: " + repr(pmt.car(self.debug.get_message(0)))) print("data expected: " + repr(pmt.u8vector_elements(pmt.cdr(e_pdu)))) print("data got: " + repr(pmt.u8vector_elements(pmt.cdr(self.debug.get_message(0))))) print() self.assertTrue(pmt.equal(self.debug.get_message(0), e_pdu))
def send_pkt(self, payload): data = pmt.init_u8vector(len(payload), [ord(c) for c in payload]) meta_dict = {} meta_dict['dst_addr'] = pmt.init_u8vector(len(self.mac_addr), self.mac_addr) meta = pmt.to_pmt(meta_dict) self.message_port_pub(pmt.intern('to_mac'), pmt.cons(meta, data))
def test_001_uint8(self): ''' uint8_t input data, no decimation, no filter ''' self.dut = pdu_utils.pdu_fir_filter(1, [1.0]) self.connectUp() i_data = [1, 0, 0, 0] * 5 i_meta = pmt.dict_add(pmt.make_dict(), pmt.intern("sample_rate"), pmt.from_float(1000.0)) in_pdu = pmt.cons(i_meta, pmt.init_u8vector(len(i_data), i_data)) e_data = [1, 0, 0, 0] * 5 e_meta = pmt.dict_add(pmt.make_dict(), pmt.intern("sample_rate"), pmt.from_float(1000.0)) e_pdu = pmt.cons(e_meta, pmt.init_u8vector(len(e_data), e_data)) self.tb.start() time.sleep(.01) self.emitter.emit(in_pdu) time.sleep(.1) self.tb.stop() self.tb.wait() print("test_001:") print("pdu expected: " + repr(pmt.car(e_pdu))) print("pdu got: " + repr(pmt.car(self.debug.get_message(0)))) print("data expected: " + repr(pmt.to_python(pmt.cdr(e_pdu)))) print("data got: " + repr(pmt.to_python(pmt.cdr(self.debug.get_message(0))))) print self.assertTrue(pmt.equal(self.debug.get_message(0), e_pdu))
def test_008_match_leading_zeros (self): ''' Leading zeros at the beginning of the alignment sequence previously caused invalid alignments due to not checking to ensure that the entire alignment sequence was checked before emitting the message For an alignment sequence of: '00000000'*3 + '10110111' a message was kicked out with the following contents '00000000'*3 + '10110111' + ... if the first 8 bits of a sequence were within the threshold of the last 8 bits of the alignment sequence, which has a 1 in 256 chance of occuring statistically anyway. ''' preamble = '00000000' * 3 + '10110111' self.dut = pdu_utils.pdu_align(preamble, 0, 0, pdu_utils.ALIGN_EMPTY) self.connectUp() in_data = [int(a) for a in ('10110111' + '00000000' * 3 + '10110111' + '10100101')] expected_data = [1, 0, 1, 0, 0, 1, 0, 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(.1) self.tb.stop() self.tb.wait() self.assertTrue(pmt.equal(self.debug.get_message(0), expected_pdu))
def test_001_t(self): # set up data srcdata = (1, 2, 3) meta = pmt.to_pmt({'dissector': 'rds'}) srcpdu = pmt.cons(meta, pmt.init_u8vector(len(srcdata), srcdata)) outdata = (82, 70, 116, 97, 4, 0, 0, 0, 16, 0, 3, 255, 114, 100, 115, 0) + srcdata outpdu = pmt.cons(meta, pmt.init_u8vector(len(outdata), outdata)) # set up flowgraph encap = rftap_encap(1, -1, "") sink = blocks.message_debug() tb = gr.top_block() tb.msg_connect(encap, "out", sink, "store") # run flowgraph tb.start() t = time.time() encap.to_basic_block()._post(pmt.intern("in"), srcpdu) while time.time() - t < 2: # timeout if sink.num_messages() > 0: break # got msg time.sleep(0.1) tb.stop() tb.wait() # check data recpdu = sink.get_message(0) #print 'expected:', pmt.u8vector_elements(pmt.cdr(outpdu)) #print 'actual :', pmt.u8vector_elements(pmt.cdr(recpdu)) self.assertTrue(pmt.equal(recpdu, outpdu))
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 control = [numpy.uint8(0)] if self.control_byte else [] frame = bytes([FEND] + control + kiss_escape(pmt.u8vector_elements(msg)) + [FEND]) if self.include_timestamp: timestamp_frame = bytes([FEND] + kiss_escape(self.create_timestamp()) + [FEND]) self.message_port_pub(pmt.intern('out'), pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(len(timestamp_frame), timestamp_frame))) self.message_port_pub(pmt.intern('out'), pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(len(frame), frame)))
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 test_001_t (self): data = [ord('t'),ord('e'),ord('s'),ord('t')] msg = pmt.list1(pmt.list2(pmt.string_to_symbol("msg_clear"),pmt.init_u8vector(len(data),data))) filename_sk = "secret.key" filename_pk = "public.key" nacl.generate_keypair(filename_sk,filename_pk) strobe = blocks.message_strobe(msg, 100) encrypt_public = nacl.encrypt_public(filename_pk,filename_sk) debug = blocks.message_debug() self.tb.msg_connect(strobe,"strobe",encrypt_public,"Msg clear") self.tb.msg_connect(encrypt_public,"Msg encrypted",debug,"store") self.tb.start() sleep(0.15) self.tb.stop() self.tb.wait() # check results msg_stored = debug.get_message(0) nonce = pmt.nth(0,msg_stored) msg_encrypted = pmt.nth(1,msg_stored) print pmt.symbol_to_string(pmt.nth(0,nonce)), pmt.u8vector_elements(pmt.nth(1,nonce)) print pmt.symbol_to_string(pmt.nth(0,msg_encrypted)), pmt.u8vector_elements(pmt.nth(1,msg_encrypted))
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 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 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 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_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 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 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_006_basic_nrz(self): self.dut = pdu_utils.pdu_preamble([], [], 1, 0, True) self.connectUp() input_data = pmt.init_u8vector(8, [1, 0, 1, 1, 0, 0, 1, 0]) input_dict = pmt.dict_add(pmt.make_dict(), pmt.intern("KEY"), pmt.intern("VALUE")) input_pdu = pmt.cons(input_dict, input_data) expected_data = [1, -1, 1, 1, -1, -1, 1, -1] expected_dict = pmt.dict_add(pmt.make_dict(), pmt.intern("KEY"), pmt.intern("VALUE")) expected_pdu = pmt.cons( expected_dict, pmt.init_f32vector(len(expected_data), expected_data)) self.tb.start() time.sleep(.001) self.emitter.emit(input_pdu) time.sleep(.01) self.tb.stop() self.tb.wait() self.assertEqual(1, self.debug.num_messages()) print("test_006_basic_nrz:") print("pdu expected: " + repr(pmt.car(expected_pdu))) print("pdu got: " + repr(pmt.car(self.debug.get_message(0)))) print("data expected: " + repr(pmt.to_python(pmt.cdr(expected_pdu)))) print("data got: " + repr(pmt.to_python(pmt.cdr(self.debug.get_message(0))))) print self.assertTrue(pmt.equal(self.debug.get_message(0), expected_pdu))
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_pdu_length_filter(self): tb = gr.top_block() dbg = blocks.message_debug() sizes = [25, 50, 100] test_data = list(range(np.max(sizes))) msgs = [test_data[:x] for x in sizes] pdus = [pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(len(m), m)) for m in msgs] length_filter = pdu_length_filter(40, 60) tb.msg_connect((length_filter, 'out'), (dbg, 'store')) for pdu in pdus: length_filter.to_basic_block()._post(pmt.intern('in'), pdu) length_filter.to_basic_block()._post( pmt.intern('system'), pmt.cons(pmt.intern('done'), pmt.from_long(1))) tb.start() tb.wait() self.assertEqual( dbg.num_messages(), 1, 'Incorrect number of messages passed by PDU Length Filter') out = pmt.u8vector_elements(pmt.cdr(dbg.get_message(0))) self.assertEqual( len(out), 50, 'PDU Length Filter output does not match expected')
def test_encoder_decoder(self): """Connects a PDU to KISS and KISS to PDU and sends PDUs through""" pdu2kiss = pdu_to_kiss(include_timestamp=True) kiss2pdu = kiss_to_pdu() pdu2tag = blocks.pdu_to_tagged_stream(byte_t) dbg = blocks.message_debug() self.tb.connect(pdu2tag, kiss2pdu) self.tb.msg_connect((pdu2kiss, 'out'), (pdu2tag, 'pdus')) self.tb.msg_connect((kiss2pdu, 'out'), (dbg, 'store')) test_size = 150 test_number_frames = 7 test_data = [ np.random.randint(0, 256, test_size, dtype='uint8') for _ in range(test_number_frames) ] for td in test_data: test_frame = pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(test_size, td)) pdu2kiss.to_basic_block()._post(pmt.intern('in'), test_frame) pdu2kiss.to_basic_block()._post( pmt.intern('system'), pmt.cons(pmt.intern('done'), pmt.from_long(1))) self.tb.start() self.tb.wait() for j, td in enumerate(test_data): result_data = pmt.u8vector_elements(pmt.cdr(dbg.get_message(j))) np.testing.assert_equal( td, result_data, 'KISS to PDU output does not match expected frame')
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 _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 = 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 packetise(self, msg): data = pmt.cdr(msg) meta = pmt.car(msg) if not pmt.is_u8vector(data): #raise NameError("Data is no u8 vector") return "Message data is not u8vector" buf = pmt.u8vector_elements(data) buf_str = "".join(map(chr, buf)) # FIXME: Max: 4096-header-crc meta_dict = pmt.to_python(meta) if not (type(meta_dict) is dict): meta_dict = {} pkt = "" pkt += self.preamble pkt += packet_utils.make_packet( buf_str, 0, #self._samples_per_symbol, 0, #self._bits_per_symbol, #preamble=<default> access_code=self.access_code, pad_for_usrp=False, #pad_for_usrp, whitener_offset=self.whitener_offset, whitening=self.whiten) pkt += self.postamble pkt = map(ord, list(pkt)) if self.rotate_whitener_offset: self.whitener_offset = (self.whitener_offset + 1) % 16 meta = pmt.to_pmt(meta_dict) data = pmt.init_u8vector(len(pkt), pkt) self.message_port_pub(pmt.intern('out'), pmt.cons(meta, data)) '''
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 test_001_t(self): data = [ord("a"), ord("b"), ord("c"), ord("d")] msg = pmt.list1(pmt.list2(pmt.string_to_symbol("msg_clear"), pmt.init_u8vector(len(data), data))) filename_key = "secret.a" nacl.generate_key(filename_key) strobe = blocks.message_strobe(msg, 100) encrypt_secret = nacl.encrypt_secret(filename_key) decrypt_secret = nacl.decrypt_secret(filename_key) debug = blocks.message_debug() self.tb.msg_connect(strobe, "strobe", encrypt_secret, "Msg clear") self.tb.msg_connect(encrypt_secret, "Msg encrypted", decrypt_secret, "Msg encrypted") self.tb.msg_connect(decrypt_secret, "Msg decrypted", debug, "store") self.tb.start() sleep(0.15) self.tb.stop() self.tb.wait() # check results msg_out = debug.get_message(0) msg_symbol = pmt.symbol_to_string(pmt.nth(0, pmt.nth(0, msg_out))) msg_decrypted = pmt.u8vector_elements(pmt.nth(1, pmt.nth(0, msg_out))) print msg_symbol, msg_decrypted print "msg_clear", data for k in range(len(data)): self.assertEqual(data[k], msg_decrypted[k])
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 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 buff = list() buff.append(FEND) buff.append(numpy.uint8(0)) for x in pmt.u8vector_elements(msg): if x == FESC: buff.append(FESC) buff.append(TFESC) elif x == FEND: buff.append(FESC) buff.append(TFEND) else: buff.append(numpy.uint8(x)) buff.append(FEND) buff = array.array('B', buff) self.message_port_pub( pmt.intern('out'), pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(len(buff), buff)))
def work(self, input_items, output_items): self.kiss.extend(input_items[0]) while self.kiss: c = self.kiss.popleft() if c == FEND: if self.pdu and (not self.control_byte or not self.pdu[0] & 0x0f): msg = array.array( 'B', self.pdu[1:] if self.control_byte else self.pdu) 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) return len(input_items[0])
def handle_msg(self, arg): msg = pmt.write_string(arg); fullmsg = self.header + msg; ba = bitarray.bitarray(); ba.fromstring(fullmsg); fsl = ba.tolist(); pdu = pmt.cons(pmt.PMT_NIL,pmt.init_u8vector(len(fsl), fsl)); # pdu = pmt.cons(pmt.PMT_NIL,pmt.to_pmt(map(lambda x: int(x), ba.tolist()))); self.message_port_pub(pmt.intern("pdu"), pdu);
def output_user_data(self,pdu_tuple): if (len(pdu_tuple[0]) > 5): data = pdu_tuple[0][5:] 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 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)) header = packet[:4] header.reverse() packet = header + packet[4:] msg_pmt = pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(len(packet), bytearray(packet))) self.message_port_pub(pmt.intern('out'), msg_pmt)
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 post_data(self, data, type=None, arg1=None, arg2=None): ok, payload = packet_utils.unmake_packet(data, int(arg1), self.dewhiten) if not ok: #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} meta = pmt.to_pmt(meta_dict) self.message_port_pub(pmt.intern('pdu'), pmt.cons(meta, buf))
def output_user_data(self, pdu_tuple): self.message_port_pub(pmt.intern('to_app'), \ # meta_dic pmt.cons(pmt.to_pmt(pdu_tuple[1]), \ # encapsulated data pmt.init_u8vector(len(pdu_tuple[0][self.DATA_PKT_MIN_LENGTH:]), \ pdu_tuple[0][self.DATA_PKT_MIN_LENGTH:]))) # write packet to standard output sys.stdout.write(time.asctime(time.localtime(time.time())) + " : "); # print data for i in range(0, len(pdu_tuple[0])): sys.stdout.write("%d " % pdu_tuple[0][i]) sys.stdout.write("\n")
def _demux(self, buf, meta_dict): header = [] chan_flags = buf[0] # Added manually to reconstructed header buf = buf[1:] chan_id = None frag_id, frag_idx, frag_num = None, -1, 0 try: if chan_flags & CHAN_FLAG_ID: chan_id = buf[0] header += buf[:1] buf = buf[1:] if chan_flags & CHAN_FLAG_FRAG: frag_id, frag_idx, frag_num = buf[0], buf[1], buf[2] # Ignore for header reconstruction buf = buf[3:] except: print "Exception parsing packet channel header" return if frag_id is not None: self.clean_frags(frag_id) frag_result = self.add_frag(frag_id, frag_idx, frag_num, buf) if frag_result is not None: chan_flags = (chan_flags & ~CHAN_FLAG_FRAG) & 0xFF buf = [chan_flags] + header + frag_result self._demux(buf, meta_dict) return if chan_id in self.channel_map.keys(): out_name = self.channel_map[chan_id] else: out_name = 'out%d' % ((self.channel_count - 1)) #lets append some metadata to the pdu meta_dict['EM_CHAN_ID'] = chan_id #print chan_id, out_name, meta_dict #print "[virtual_channel_decoder]", meta_dict #convert dictionary back to a pmt meta = pmt.to_pmt(meta_dict) data = pmt.init_u8vector(len(buf),buf) self.message_port_pub(pmt.intern(out_name), pmt.cons(meta, data))
def test_001_t (self): # set up fg msg_meta = pmt.make_dict() msg_meta = pmt.dict_add(msg_meta, pmt.to_pmt("freq"), pmt.to_pmt("val")) vec1 = [0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00]; msg_vector = pmt.init_u8vector(len(vec1), vec1) msg = pmt.cons(msg_meta, msg_vector) src = blocks.message_strobe(msg, 10) dut = capture_tools.bit_sniffer(fade_out=500, hexadecimal=True) self.tb.msg_connect((src, "strobe"), (dut, "packets")) self.tb.start () time.sleep(5) vec1 = [0x01, 0x00, 0x01, 0x00, 0x01, 0x00]; msg_vector = pmt.init_u8vector(len(vec1), vec1) msg = pmt.cons(msg_meta, msg_vector) src.set_msg(msg); time.sleep(5) vec1 = [0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x01]; msg_vector = pmt.init_u8vector(len(vec1), vec1) msg = pmt.cons(msg_meta, msg_vector) src.set_msg(msg); time.sleep(5) self.tb.stop()
def send_notification_radio(self, source, seq_num): # receiver notification structure data = [self.NOTI_PROTO, self.addr, source, seq_num] # debug mode enabled? if self.debug_stderr: # yes! log the packet sys.stderr.write( "%d:in send_notification_radio(): sending notification:\n" % self.addr) # 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 send_pkt_radio(self,pdu_tuple,pkt_cnt,protocol_id,control): #create header, merge with payload, convert to pmt for message_pub data = (pkt_cnt,self.addr,pdu_tuple[1]['EM_DEST_ADDR'],protocol_id,control) + pdu_tuple[0] 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_radio'),pdu) print >> sys.stderr, "@MAC.send_pkt_radio" return
def handler(self, msg): meta = pmt.car(msg) packed_data = pmt.cdr(msg) # convert pmt -> int list -> string -> bit array ba = bitarray.bitarray() ba.frombytes(array.array("B", pmt.u8vector_elements(packed_data)).tostring()) # convert the unpacked bits to a list and back into a pmt u8vector unpacked_data = pmt.init_u8vector(len(ba), ba.tolist()) # make the new pdu pdu = pmt.cons(meta, unpacked_data) # send it on its way self.message_port_pub(pmt.intern("unpacked_pdus"), pdu)
def send_pkt_radio(self, pdu_tuple, pkt_cnt, protocol_id, control, allow_dummy=True): meta_data = pdu_tuple[1] if allow_dummy: prepend_dummy = self.prepend_dummy or ('EM_PREPEND_DUMMY' in meta_data.keys() and meta_data['EM_PREPEND_DUMMY']) if prepend_dummy: #meta_data['EM_PREPEND_DUMMY'] = False #sys.stdout.write('.') #sys.stdout.flush() self.send_pkt_radio(pdu_tuple, pkt_cnt, DUMMY_PROTOCOL_ID, control, False) 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.addr, 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_radio'),pdu) with self.lock: self.last_tx_time = time.time()
def send_sink_pkt(self, addr, seq_num, hop_count): # beacon packet structure data = [self.SINK_PROTO, self.addr, addr, seq_num, hop_count] # debug mode enabled? if self.debug_stderr: # Yes! # log the packet sys.stderr.write( "%d:in send_sink_pkt(): sending sink packet:\n" % self.addr) self.print_sink_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) # save current transmit time with self.lock: self.sink_pkt_xmit_time = time.time()
def handle_binary(self,msg): data = pmt.cdr(msg) if not pmt.is_u8vector(data): raise NameError("Data is not u8 vector") payload = array.array("B",pmt.u8vector_elements(data)) buf = payload #print buf for i in buf: if self.rx_state == GET_FEND: if i == FEND: self.rx_state = GET_LEN self.count = 0 self.incoming_len = 0 elif self.rx_state == GET_LEN: self.incoming_len |= ( ( i & 0xFF ) << ( 8 * (3 - self.count ) ) ) self.count += 1 if self.count == 4: #print "PDU Link(TTY): Receiving Payload Frame of Length:",self.incoming_len,self.last_time - time.time() print "<binary_to_pdu> Receiving Payload Frame of Length:", self.incoming_len self.last_time = time.time() if self.incoming_len < 2048: self.count = 0 self.rx_state = GET_PAYLOAD else: print "Received unreasonable payload length: ",self.incoming_len, "Aborting!" self.rx_state = GET_FEND elif self.rx_state == GET_PAYLOAD: #print self.count,len(self.out_buf),self.incoming_len self.out_buf[self.count] = i self.count += 1 if self.count == self.incoming_len: #print buf self.rx_state = GET_FEND packet = self.out_buf[0:self.incoming_len] print "<binary_to_pdu>", packet self.message_port_pub(pmt.intern('pdu_out'),pmt.cons(pmt.PMT_NIL,pmt.init_u8vector(len(packet),packet))) self.count = 0
def handle_msg(self, msg): # decrypted data plaintext = [] # consume encrypted data from encoder ciphertext = pmt.to_python(msg) for i in range(len(ciphertext)): # use one key bit k = self.key_bit.pop(0) # post decrypted data bit t = ciphertext[i]^k plaintext.append(t) if self.debug: sys.stderr.write("decoder.handle_msg():plain text: " + \ str(plaintext) + \ " cipher text: " + str(ciphertext) + "\n") # post encrypted data, converted to the PDU PMT self.message_port_pub(pmt.intern('plaintext'), \ pmt.cons(pmt.to_pmt({}), \ pmt.init_u8vector(len(plaintext),plaintext)))
def handler(self, msg): meta = pmt.car(msg); packed_data = pmt.cdr(msg); data = array.array('B', pmt.u8vector_elements(packed_data)) # break up into blocks nbits = len(data); # 32 for the payload crc final_len = int(math.ceil( 1.0*nbits / self.blocksize) * self.blocksize); padding = final_len - nbits; # add some padding for i in range(0,padding): data.append(0); padded_bits = pmt.init_u8vector(len(data), data); pdu = pmt.cons(meta, padded_bits); # send it on its way self.message_port_pub(pmt.intern("pdus"), pdu);