def msg_handler(self, p): length = pmt.length(p) for i in range(0,length): element = pmt.nth(i, p) key = pmt.nth(0, element) value = pmt.nth(1, element) if str(key) == "power": output = pmt.f32vector_elements(value)[0] output = 10 * math.log(output, 10) output = pmt.make_f32vector(1, output) if i==0: outpmt = pmt.list1(pmt.list2(pmt.string_to_symbol(str(key) + "_dB"), output)) else: outpmt = pmt.list_add(outpmt, pmt.list2(pmt.string_to_symbol(str(key) + "_dB"), output)) output = pmt.nth(1, element) if i==0: outpmt = pmt.list1(pmt.list2(key, output)) else: outpmt = pmt.list_add(outpmt, pmt.list2(key, output)) self.message_port_pub(pmt.string_to_symbol("out"), outpmt)
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_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 int_list_to_pmt(self, items): if len(items) == 0: return pmt.from_bool(pmt.PMT_F) else: pmtl = pmt.list1(pmt.from_long(items[0])) for i in range(len(items) - 1): pmtl = pmt.list_add(pmtl, pmt.from_long(items[i + 1])) return pmtl
def complex_list_to_pmt(self, items): if len(items) == 0: return pmt.from_bool(pmt.PMT_F) else: pmtl = pmt.list1(pmt.from_complex(items[0])) for i in range(len(items) - 1): pmtl = pmt.list_add(pmtl, pmt.from_complex(items[i + 1])) return pmtl
def handle_msg(self, msg): cell_id = pmt.to_long(msg) if self.cell_id == cell_id: return else: self.cell_id = cell_id print "received cell_id = " + str(cell_id) seqs = [] seq = lte.encode_nrz(lte.generate_pn_sequence(1920, cell_id)) pmt_list = pmt.list1(pmt.from_double(seq[0])) for i in range(len(seq) - 1): pmt_list = pmt.list_add(pmt_list, pmt.from_double(seq[i + 1])) self.message_port_pub(self.msg_buf_out, pmt_list)
def handle_msg(self, msg): cell_id = pmt.to_long(msg) if self.cell_id == cell_id: return else: self.cell_id = cell_id print "received cell_id = " + str(cell_id) seqs = [] seq = lte.encode_nrz(lte.generate_pn_sequence(1920, cell_id)) pmt_list = pmt.list1(pmt.from_double(seq[0])) for i in range(len(seq) - 1): pmt_list = pmt.list_add(pmt_list, pmt.from_double(seq[i+1])) self.message_port_pub(self.msg_buf_out, pmt_list)
def msg_handler(self, p): length = pmt.length(p) # iterate over all elements for i in range(0, length): element = pmt.nth(i, p) key = pmt.nth(0, element) value = pmt.nth(1, element) initial_phase = None # when we found the phase key if str(key) == "phase" and pmt.length(value) >= 1: # print "length of vector: ", pmt.length(value) value = pmt.f32vector_elements(value)[0] self.last_val = value # save that value initial_phase = value value = value - self.subtract while value < -numpy.pi: value = value + numpy.pi while value > numpy.pi: value = value - numpy.pi initial_phase = pmt.make_f32vector(1, initial_phase) initial_phase = pmt.list2(pmt.string_to_symbol("phase_initial"), initial_phase) outvalue = pmt.make_f32vector(1, value) output = pmt.list2(pmt.string_to_symbol(str(key)), outvalue) else: output = element if i == 0: outpmt = pmt.list1(output) else: outpmt = pmt.list_add(outpmt, output) if initial_phase != None: outpmt = pmt.list_add(outpmt, initial_phase) self.message_port_pub(pmt.string_to_symbol("out"), outpmt)
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_sk_a = "secret.a" filename_pk_a = "public.a" nacl.generate_keypair(filename_sk_a, filename_pk_a) filename_sk_b = "secret.b" filename_pk_b = "public.b" nacl.generate_keypair(filename_sk_b, filename_pk_b) strobe = blocks.message_strobe(msg, 100) encrypt_public = nacl.encrypt_public(filename_pk_b, filename_sk_a) decrypt_public = nacl.decrypt_public(filename_pk_a, filename_sk_b) debug = blocks.message_debug() self.tb.msg_connect(strobe, "strobe", encrypt_public, "Msg clear") self.tb.msg_connect(encrypt_public, "Msg encrypted", decrypt_public, "Msg encrypted") self.tb.msg_connect(decrypt_public, "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 rs_pos_to_pmt(self, rs_poss): pmt_rs = pmt.list1(self.int_list_to_pmt(rs_poss[0])) for i in range(len(rs_poss) - 1): pmt_rs = pmt.list_add(pmt_rs, self.int_list_to_pmt(rs_poss[i + 1])) return pmt_rs
def rs_val_to_pmt(self, rs_vals): pmt_vals = pmt.list1(self.complex_list_to_pmt(rs_vals[0])) for i in range(len(rs_vals) - 1): pmt_vals = pmt.list_add(pmt_vals, self.complex_list_to_pmt(rs_vals[i + 1])) return pmt_vals
def msg_handler(self, p): verbose = False length = pmt.length(p) if verbose: print "PMT contrains " + str(length) + " key/value pairs" for i in range(0,length): element = pmt.nth(i, p) key = pmt.nth(0, element) value = pmt.nth(1, element) if verbose: print "Key of " + str(i) + "th element: " + str(key) print "Value of " + str(i) + "th element: " + str(value) print found = False for j in range(0, len(self.n)): if str(key) == self.kk[j]: found = True if verbose: print "found the key " + str(key) #rotate the values, the latest one disappears self.prev_values[j] = numpy.roll(self.prev_values[j], -1) #set the vector accordingly self.prev_values[j][-1] = pmt.f32vector_elements(value)[0] output = sum(self.prev_values[j])/len(self.prev_values[j]) output = pmt.make_f32vector(1, output) if i==0: outpmt = pmt.list1(pmt.list2(pmt.string_to_symbol(str(key) + "_filtered"), output)) else: outpmt = pmt.list_add(outpmt, pmt.list2(pmt.string_to_symbol(str(key) + "_filtered"), output)) output = pmt.nth(1, element) if i==0: outpmt = pmt.list1(pmt.list2(key, output)) else: outpmt = pmt.list_add(outpmt, pmt.list2(key, output)) if verbose: print #iterate over all keys for i in range(0, len(self.kk)): minimum = self.prev_values[i][0] maximum = self.prev_values[i][0] #iterate over all saved values for j in range(0, self.n[i]): if self.prev_values[i][j] < minimum: minimum = self.prev_values[i][j] if self.prev_values[i][j] > maximum: maximum = self.prev_values[i][j] #print out a min, diff, max for every key difference = maximum-minimum outpmt = pmt.list_add(outpmt, pmt.list2(pmt.string_to_symbol("min"), pmt.make_f32vector(1, minimum))) outpmt = pmt.list_add(outpmt, pmt.list2(pmt.string_to_symbol("max"), pmt.make_f32vector(1, maximum))) outpmt = pmt.list_add(outpmt, pmt.list2(pmt.string_to_symbol("diff"), pmt.make_f32vector(1, difference))) self.message_port_pub(pmt.string_to_symbol("out"), outpmt)