def read_single_header(handle): nread = handle.tell() header_str = handle.read(parse_file_metadata.HEADER_LENGTH) if len(header_str) == 0: sys.stderr.write("Empty Header, quitting.\n") sys.exit(1) # Convert from string to PMT (should be a dictionary) try: header = pmt.deserialize_str(header_str) except RuntimeError: sys.stderr.write( "Could not deserialize header: invalid or \ corrupt data file.\n" ) sys.exit(1) info = parse_file_metadata.parse_header(header, False) extras_str = handle.read(info["extra_len"]) try: extras_hdr = pmt.deserialize_str(extras_str) except RuntimeError: sys.stderr.write("Could not deserialize extras\n") sys.exit(1) nread += parse_file_metadata.HEADER_LENGTH + info["extra_len"] handle.seek(nread, 0) return header, extras_hdr, handle
def parse_gnuradio_header(header_file, verbose=False): headers = [] index = 0 rx_time = datetime.timedelta(seconds=0) with open(header_file, "rb") as handle: file_length = os.path.getsize(header_file) while True: if file_length - handle.tell() < parse_file_metadata.HEADER_LENGTH: break header_str = handle.read(parse_file_metadata.HEADER_LENGTH) try: header = pmt.deserialize_str(header_str) except RuntimeError: break info = parse_file_metadata.parse_header(header, verbose) if info["nbytes"] == 0: break if info["extra_len"] > 0: extra_str = handle.read(info["extra_len"]) if len(extra_str) == 0: break try: extra = pmt.deserialize_str(extra_str) except RuntimeError: break parse_file_metadata.parse_extra_dict(extra, info, verbose) if len(headers) > 0: last_rx_time = headers[-1]["rx_time"] samples_delta = headers[-1]["nitems"] / headers[-1]["rx_rate"] samples_delta = datetime.timedelta(seconds=samples_delta) info["rx_time"] = last_rx_time + samples_delta info["index"] = index index = index + info["nitems"] else: info["rx_time"] = datetime.timedelta(seconds=0.0) info["index"] = 0 index = info["nitems"] headers.append(info) return headers
def parse_metadata(self, filepath): head_file = filepath+".hdr" hlen = parse_file_metadata.HEADER_LENGTH headers = [] extra_tags = [] overs = [] if not os.path.isfile(head_file): return {'total power':{}}, [] with open(head_file,'rb') as fd: for h_str in iter(partial(fd.read, hlen), ''): h_pmt = pmt.deserialize_str(h_str) h_parsed=parse_file_metadata.parse_header(h_pmt,False) headers.append(h_parsed) if(h_parsed["extra_len"] > 0): extra_str = fd.read(h_parsed["extra_len"]) if(len(extra_str) == 0): break extra = pmt.deserialize_str(extra_str) e_parsed = parse_file_metadata.parse_extra_dict(extra, h_parsed, False) extra_tags.append(e_parsed) # Load the extra data into the tagging system for the LightCurve. tags = pd.DataFrame({'total power':[{} for _ in xrange(self.data_len)]}) nums_done = 0 segment_start_time = headers[0]['rx_time'] segments = 1 for i in xrange(len(extra_tags)): j = int(nums_done + extra_tags[i]['nitems']) if not extra_tags[i]['rx_time'] == segment_start_time: should = segment_start_time + j/extra_tags[i]['rx_rate'] miss_sec = extra_tags[i]['rx_time']-should overs.append({'new_seg':j, 'new_time':extra_tags[i]['rx_time']}) segment_start_time = extra_tags[i]['rx_time'] segments += 1 j = int(nums_done + extra_tags[i]['nitems']) tags['total power'][j] = extra_tags[i] nums_done += extra_tags[i]['nitems'] new = self.import_tags(extra_tags, 'total power') return {'total power': headers[0]}, overs
def test14(self): const = self.MAXINT32 - 1 x_pmt = pmt.from_long(const) s = pmt.serialize_str(x_pmt) deser = pmt.deserialize_str(s) self.assertTrue(pmt.equal(x_pmt, deser)) self.assertEqual(const,pmt.to_long(deser))
def test17(self): const = self.MININT32 + 1 x_pmt = pmt.from_long(const) s = pmt.serialize_str(x_pmt) deser = pmt.deserialize_str(s) self.assertTrue(pmt.equal(x_pmt, deser)) x_long = pmt.to_long(deser) self.assertEqual(const, x_long)
def request(self, id_str, args=None): socks = dict(self.poller_req_out.poll(10)) if socks.get(self.req_socket) == zmq.POLLOUT: self.req_socket.send(pmt.serialize_str(pmt.to_pmt((id_str,args)))) socks = dict(self.poller_req_in.poll(10)) if socks.get(self.req_socket) == zmq.POLLIN: reply = pmt.to_python(pmt.deserialize_str(self.req_socket.recv())) print("[RPC] reply:", reply) return reply
def watcher(self): self.keep_running = True while self.keep_running: # poll for calls socks = dict(self.poller_rep.poll(10)) if socks.get(self.rep_socket) == zmq.POLLIN: # receive call msg = self.rep_socket.recv() (id_str, args) = pmt.to_python(pmt.deserialize_str(msg)) print("[RPC] request:", id_str, ", args:", args) reply = self.callback(id_str, args) self.rep_socket.send(pmt.serialize_str(pmt.to_pmt(reply)))
def test19(self): max_key = pmt.intern("MAX") _max = pmt.from_long(self.MAXINT32) min_key = pmt.intern("MIN") _min = pmt.from_long(self.MININT32) d = pmt.make_dict() d = pmt.dict_add(d, max_key, _max) d = pmt.dict_add(d, min_key, _min) s = pmt.serialize_str(d) deser = pmt.deserialize_str(s) self.assertTrue(pmt.equal(d, deser)) p_dict = pmt.to_python(deser) self.assertEqual(self.MAXINT32, p_dict["MAX"]) self.assertEqual(self.MININT32, p_dict["MIN"])
def background_thread(): # Establish ZMQ context and socket context = zmq.Context() socket = context.socket(zmq.SUB) socket.setsockopt(zmq.SUBSCRIBE, "") socket.connect("tcp://0.0.0.0:%d" % (ZMQ_PORT)) while True: # Receive decoded ADS-B message from the decoder over ZMQ pdu_bin = socket.recv() pdu = pmt.deserialize_str(pdu_bin) plane = pmt.to_python(pmt.car(pdu)) socketio.emit("updatePlane", plane) time.sleep(0.010)
def work(self, queue, listener_address, label): self.q = queue self.label = label self.listener_address = listener_address self.logger.log("init: " + self.label) context = zmq.Context() socket = context.socket(zmq.SUB) socket.setsockopt(zmq.SUBSCRIBE, '') socket.connect(listener_address) while True: # Important improvement opportunity 1: The zmq socket that are transmitting to the collector # is sending data as json (send_json), but here the data is being received as normal string. # This is getenrating some inconsistency and the code should be reviewed in future. # Important improvement opportunity 2: The information transported by zmq between applications # may use linux socket instead of tcp socket. k = socket.recv() if self.from_gnuradio == True: # this part of code seams to be consuming alot of processing, needs verification try: p = pmt.deserialize_str(k) except Exception: p = None self.logger.log('Error: problem deserializing message') if p is not None: python_numpy_array = pmt.pmt_to_python.pmt_to_python(p) tosend = str(python_numpy_array[0]) msg = (self.label, tosend) with self.thread_lock: self.q.put(msg) else: tosend = k msg = (self.label, tosend) with self.thread_lock: self.q.put(msg) sleep(0.01) # test
def test10(self): v = pmt.init_s32vector(3, [11, -22, 33]) s = pmt.serialize_str(v) d = pmt.deserialize_str(s) self.assertTrue(pmt.equal(v, d))
def test07(self): v = pmt.init_u16vector(3, [11, 22, 33]) s = pmt.serialize_str(v) d = pmt.deserialize_str(s) self.assertTrue(pmt.equal(v, d))
def forward(self): data, addr = self.input_sock.recvfrom(4096) print "Received Something", pmt.deserialize_str(data) self.output_sock.sendto(data, (self.dest_addr, self.dest_port))
def watcher(self, reception_complete): poll = dict(self.poller.poll(100)) for i in self.interfaces: if not reception_complete[i[0]]: # i = (socket, data_type, callback_func) if poll.get(i[1]) == zmq.POLLIN: print "receiving data" # receive data msg_packed = i[1].recv() # use numpy to unpack header and data header_magic = numpy.fromstring(msg_packed[:2], numpy.dtype('uint16')) #print "header_magic",header_magic if header_magic != 0x5FF0: raise ValueError('Error: gr-zmq header magic does not match!') header_version = numpy.fromstring(msg_packed[2:3], numpy.dtype('uint8')) #print "header_version",header_version if header_version != 1: raise ValueError('Error: gr-zmq header version too high!') offset_out = numpy.fromstring(msg_packed[3:11], numpy.dtype('uint64')) #print "offset_out",offset_out rcv_ntags = numpy.fromstring(msg_packed[11:19], numpy.dtype('uint64')) if rcv_ntags == 3: # extract all the tags offset = numpy.fromstring(msg_packed[19:27], numpy.dtype('uint64')) #print "offset", offset key = str(pmt.deserialize_str(msg_packed[27:37])) #print "key",key value = pmt.to_python(pmt.deserialize_str(msg_packed[37:60])) value = value[0] + value[1] #print "value", value srcid = str(pmt.deserialize_str(msg_packed[60:82])) #print "srcid",srcid #print "in probe_manager:" key_2 = str(pmt.deserialize_str(msg_packed[90:100])) #print "key_2",key_2 value_2 = pmt.to_python(pmt.deserialize_str(msg_packed[100:109])) #print "value_2", value_2 srcid_2 = str(pmt.deserialize_str(msg_packed[109:131])) #print "srcid_2",srcid_2 key_3 = str(pmt.deserialize_str(msg_packed[139:149])) #print "key_3",key_3 value_3 = pmt.to_python(pmt.deserialize_str(msg_packed[149:158])) #print "value_3", value_3 srcid_3 = str(pmt.deserialize_str(msg_packed[158:180])) #print "srcid_3",srcid_3 # Reverse engineering (brute force) #for n in range(39,39+1*64): #for m in range(145,1): # for n in range(160,180): # try: # key = pmt.deserialize_str(msg_packed[m:n]) # print "value_3",key # print m,n # except: # pass samples = numpy.fromstring(msg_packed[180:], numpy.dtype(i[3])) tags = {key:value,key_2:value_2,key_3:value_3} else: samples = numpy.fromstring(msg_packed[19:], numpy.dtype(i[3])) tags = None #print "\n!!!!!!fatal error in rx ",i[0],"!!!!!!" print "rcv_ntags",rcv_ntags print len(samples) #print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" # invoke callback function i[4](samples, tags)
def test12(self): v = pmt.init_c64vector(3, [11 + -101j, -22 + 202j, 33 + -303j]) s = pmt.serialize_str(v) d = pmt.deserialize_str(s) self.assertTrue(pmt.equal(v, d))
def test11(self): v = pmt.init_c32vector(3, [11 + -101j, -22 + 202j, 33 + -303j]) s = pmt.serialize_str(v) d = pmt.deserialize_str(s) self.assertTrue(pmt.equal(v, d)) self.assertEqual(pmt.uniform_vector_itemsize(v), 8)
def test16(self): const = self.MININT32 x_pmt = pmt.from_long(const) s = pmt.serialize_str(x_pmt) deser = pmt.deserialize_str(s) self.assertTrue(pmt.equal(x_pmt, deser))
def test03(self): v = pmt.init_f32vector(3, [11, -22, 33]) s = pmt.serialize_str(v) d = pmt.deserialize_str(s) self.assertTrue(pmt.equal(v, d)) self.assertEqual(pmt.uniform_vector_itemsize(v), 4)
def test_001(self): N = 1000 outfile = "test_out.dat" detached = False samp_rate = 200000 key = pmt.intern("samp_rate") val = pmt.from_double(samp_rate) extras = pmt.make_dict() extras = pmt.dict_add(extras, key, val) data = sig_source_c(samp_rate, 1000, 1, N) src = blocks.vector_source_c(data) fsnk = blocks.file_meta_sink(gr.sizeof_gr_complex, outfile, samp_rate, 1, blocks.GR_FILE_FLOAT, True, 1000000, extras, detached) fsnk.set_unbuffered(True) self.tb.connect(src, fsnk) self.tb.run() fsnk.close() handle = open(outfile, "rb") header_str = handle.read(parse_file_metadata.HEADER_LENGTH) if (len(header_str) == 0): self.assertFalse() try: header = pmt.deserialize_str(header_str) except RuntimeError: self.assertFalse() info = parse_file_metadata.parse_header(header, False) extra_str = handle.read(info["extra_len"]) self.assertEqual(len(extra_str) > 0, True) handle.close() try: extra = pmt.deserialize_str(extra_str) except RuntimeError: self.assertFalse() extra_info = parse_file_metadata.parse_extra_dict(extra, info, False) self.assertEqual(info['rx_rate'], samp_rate) self.assertEqual(pmt.to_double(extra_info['samp_rate']), samp_rate) # Test file metadata source src.rewind() fsrc = blocks.file_meta_source(outfile, False) vsnk = blocks.vector_sink_c() tsnk = blocks.tag_debug(gr.sizeof_gr_complex, "QA") ssnk = blocks.vector_sink_c() self.tb.disconnect(src, fsnk) self.tb.connect(fsrc, vsnk) self.tb.connect(fsrc, tsnk) self.tb.connect(src, ssnk) self.tb.run() fsrc.close() # Test to make sure tags with 'samp_rate' and 'rx_rate' keys # were generated and received correctly. tags = tsnk.current_tags() for t in tags: if (pmt.eq(t.key, pmt.intern("samp_rate"))): self.assertEqual(pmt.to_double(t.value), samp_rate) elif (pmt.eq(t.key, pmt.intern("rx_rate"))): self.assertEqual(pmt.to_double(t.value), samp_rate) # Test that the data portion was extracted and received correctly. self.assertComplexTuplesAlmostEqual(vsnk.data(), ssnk.data(), 5) os.remove(outfile)
def test_001(self): N = 1000 outfile = "test_out.dat" detached = False samp_rate = 200000 key = pmt.intern("samp_rate") val = pmt.from_double(samp_rate) extras = pmt.make_dict() extras = pmt.dict_add(extras, key, val) extras_str = pmt.serialize_str(extras) data = sig_source_c(samp_rate, 1000, 1, N) src = blocks.vector_source_c(data) fsnk = blocks.file_meta_sink(gr.sizeof_gr_complex, outfile, samp_rate, 1, blocks.GR_FILE_FLOAT, True, 1000000, extras_str, detached) fsnk.set_unbuffered(True) self.tb.connect(src, fsnk) self.tb.run() fsnk.close() handle = open(outfile, "rb") header_str = handle.read(parse_file_metadata.HEADER_LENGTH) if(len(header_str) == 0): self.assertFalse() try: header = pmt.deserialize_str(header_str) except RuntimeError: self.assertFalse() info = parse_file_metadata.parse_header(header, False) extra_str = handle.read(info["extra_len"]) self.assertEqual(len(extra_str) > 0, True) handle.close() try: extra = pmt.deserialize_str(extra_str) except RuntimeError: self.assertFalse() extra_info = parse_file_metadata.parse_extra_dict(extra, info, False) self.assertEqual(info['rx_rate'], samp_rate) self.assertEqual(pmt.to_double(extra_info['samp_rate']), samp_rate) # Test file metadata source src.rewind() fsrc = blocks.file_meta_source(outfile, False) vsnk = blocks.vector_sink_c() tsnk = blocks.tag_debug(gr.sizeof_gr_complex, "QA") ssnk = blocks.vector_sink_c() self.tb.disconnect(src, fsnk) self.tb.connect(fsrc, vsnk) self.tb.connect(fsrc, tsnk) self.tb.connect(src, ssnk) self.tb.run() fsrc.close() # Test to make sure tags with 'samp_rate' and 'rx_rate' keys # were generated and received correctly. tags = tsnk.current_tags() for t in tags: if(pmt.eq(t.key, pmt.intern("samp_rate"))): self.assertEqual(pmt.to_double(t.value), samp_rate) elif(pmt.eq(t.key, pmt.intern("rx_rate"))): self.assertEqual(pmt.to_double(t.value), samp_rate) # Test that the data portion was extracted and received correctly. self.assertComplexTuplesAlmostEqual(vsnk.data(), ssnk.data(), 5) os.remove(outfile)
parser = argparse.ArgumentParser() parser.add_argument("--server", "-s", default="127.0.0.1", help="remote server") parser.add_argument("--port", "-p", default=6000, type=int, help="remote port") args = parser.parse_args() # Socket to talk to server context = zmq.Context() socket = context.socket(zmq.SUB) print "Collecting updates from radio server at {} port {}...".format(args.server, args.port) socket.connect("tcp://{}:{}".format(args.server, args.port)) socket.setsockopt(zmq.SUBSCRIBE, "") data = RBDSData() try: while True: gnr_message_pmt = pmt.deserialize_str(socket.recv()) if pmt.is_tuple(gnr_message_pmt): msg_type = pmt.to_long(pmt.tuple_ref(gnr_message_pmt, 0)) msg = pmt.symbol_to_string(pmt.tuple_ref(gnr_message_pmt, 1)) data.update(msg_type, msg) print ansi_erase_display(2) + repr(data) + ansi_move_to(1, 1) else: print "Encountered Data I Did Not Understand" except KeyboardInterrupt: print ansi_erase_display(2) + ansi_move_to(1, 1) + "Shutdown requested...exiting" except Exception: traceback.print_exc(file=sys.stdout) sys.exit(0)
except EOFError: pass tb.stop() tb.wait() fileToWrite = open("signal.txt", 'w') fileToRead = open("meta_signal.bin", "rb") posInFile = 0 while(True): header_str = fileToRead.read(parse_file_metadata.HEADER_LENGTH) if(len(header_str) == 0): break try: header = pmt.deserialize_str(header_str) except RuntimeError: sys.stderr.write("Could not deserialize header: invalid or corrupt data file.\n") sys.exit(1) info = parse_file_metadata.parse_header(header, False) if(info["extra_len"] > 0): extra_str = fileToRead.read(info["extra_len"]) if(len(extra_str) == 0): break try: extra = pmt.deserialize_str(extra_str) except RuntimeError: sys.stderr.write("Could not deserialize extras: invalid or corrupt data file.\n")
help='remote port') args = parser.parse_args() # Socket to talk to server context = zmq.Context() socket = context.socket(zmq.SUB) print 'Collecting updates from radio server at {} port {}...'.format( args.server, args.port) socket.connect('tcp://{}:{}'.format(args.server, args.port)) socket.setsockopt(zmq.SUBSCRIBE, '') data = RBDSData() try: while True: gnr_message_pmt = pmt.deserialize_str(socket.recv()) if pmt.is_tuple(gnr_message_pmt): msg_type = pmt.to_long(pmt.tuple_ref(gnr_message_pmt, 0)) msg = pmt.symbol_to_string(pmt.tuple_ref(gnr_message_pmt, 1)) data.update(msg_type, msg) print ansi_erase_display(2) + repr(data) + ansi_move_to(1, 1) else: print 'Encountered Data I Did Not Understand' except KeyboardInterrupt: print ansi_erase_display(2) + ansi_move_to( 1, 1) + "Shutdown requested...exiting" except Exception: traceback.print_exc(file=sys.stdout) sys.exit(0)