def process_d(self): try: message = self.s.recv(8192) timestamp_d = time() page = message.split("\n") for record in page: if len(record) > 0: bits = hex_to_bits(record) in_bytes = numpy.packbits(bits) raw_hex = "".join( ["{0:02x}".format(byte) for byte in in_bytes]) if (in_bytes[3] == 0x20 or (in_bytes[3] == 0x22)): key = make_key(int(timestamp_d), (in_bytes[1] << 16) | (in_bytes[0] << 8)) #print "Key: " + str(key) bytes = decrypt_packet(in_bytes, key) #print bytes icao, lat, lon, alt, vs, no_track, stealth, typ, ns, ew, status, unk = extract_values( bytes[0:24]) parity = 0 for sym in bytes: parity ^= parityOf(sym) if (parity & 0x1) == 0: lat = recover_lat(lat, int(self.mylat * 1e7)) lon = recover_lon(lon, int(self.mylon * 1e7)) if False: # True: print "Timestamp: " + str(timestamp_d), print "ICAO: " + icao, print "Lat: " + str(lat), print "Lon: " + str(lon), print "Alt: " + str(alt) + "m", print "VS: " + str(vs), print "No-track: " + str(no_track), print "Stealth: " + str(stealth), print "Type: " + str(typ), print "GPS status: " + str(status), print "North/South speeds: {0},{1},{2},{3}".format( *ns), print "East/West speeds: {0},{1},{2},{3}".format( *ew), print "Unknown: {0:04x}".format(unk), print "Raw: {0:02x}".format(bytes[3]), print "{0:02x}{1:02x}{2:02x}{3:02x}{4:02x}{5:02x}{6:02x}{7:02x}".format( *bytes[4:12]), print "{0:02x}{1:02x}{2:02x}{3:02x}{4:02x}{5:02x}{6:02x}{7:02x}".format( *bytes[12:20]), print "{0:02x}{1:02x}{2:02x}{3:02x}".format( *bytes[20:24]), print #else: # print raw_hex trlat = lat / 1e7 trlgt = lon / 1e7 return (icao, trlat, trlgt, alt) else: print "Don't know how to decrypt packet type {0:02x}".format( in_bytes[3]) icao, _, _, _, _, _, _, _, _, _, _, _ = extract_values( in_bytes[0:24]) lat, lon, alt = -1, -1, -1 return (0, 0, 0, 0) # "" except error, e: if e.args[0] == errno.EWOULDBLOCK: #print 'EWOULDBLOCK' #time.sleep(1) # short delay, no tight loops return (0, 0, 0, 0) # "" else: print e return (0, 0, 0, 0) # "" # "$FLM\r\n"
session.tx_cnt = session.tx_cnt + 1 (icao, lat, lon, alt) = session.process_d() if icao != 0: print "R", session.rx_cnt, session.mytstamp, icao, "%.4f" % lat, "%.4f" % lon, int( alt) export_nmea(session, icao, lat, lon, alt) # Demo data # lat = 43.97446 # lon = -88.6032945 # alt = 1000 tcall = 'FLARM' icao_bytes = numpy.packbits(hex_to_bits(icao)) taddr = (icao_bytes[0] << 16) | ( icao_bytes[1] << 8) | icao_bytes[2] # Traffic Report buf = session.gdl90_encoder.msgTrafficReport(latitude=lat, longitude=lon, altitude=alt, callSign=tcall, address=taddr) if platform_name( ) != 'Android': # workaround against broadcast UDP packets issue session.g.sendto(buf, (destAddr, destPort)) session.rx_cnt = session.rx_cnt + 1