#!/usr/bin/env python3 # BFi53ER0 Battery-Free Bluetooth Gateway -- example: receive telegrams over UDP. # # python3 udp_receive.py # every telegram, one line each # python3 udp_receive.py --json # the JSON exactly as received (with both # # families on, each telegram twice) # python3 udp_receive.py --key HEX # encrypted output: the 64-hex-digit key # # from Forwarding -> Encrypted output; needs # # the 'cryptography' package # # The gateway sends one JSON object per telegram to UDP port 9001, by default # to every computer on the local segment (IPv6 ff02::1 and IPv4 broadcast). # One socket receives both families. With both on, each telegram arrives # twice, once per family; src and seq together tell the copies apart. # # Python 3, standard library only. Allow inbound UDP 9001 in the computer's # firewall. On Wi-Fi, set this computer's address as the gateway's unicast # destination: access points drop part of the broadcast and multicast. # # (c) 2026 BANFi Semiconductor Co., Ltd. and FL Semiconductor LLC. You may # use, copy and modify this example to integrate with BFi53ER0 products. import base64 import json import socket import sys import time PORT = 9001 def opener(keyhex): """Encrypted output (firmware 0.57.105 and later): open an AES-256-GCM envelope. Needs the 'cryptography' package (pip install cryptography); only this option does. Returns a function that gives the telegram, or None for an envelope already seen (the other family's copy, or a replay).""" from cryptography.hazmat.primitives.ciphers.aead import AESGCM gcm, last = AESGCM(bytes.fromhex(keyhex)), {} def open_(e): aad = ("A256GCM|%s|%d|%d|%d" % (e["gw"], e["kid"], e["b"], e["n"])).encode() try: t = json.loads(gcm.decrypt(base64.b64decode(e["iv"]), base64.b64decode(e["ct"]), aad)) except Exception: print("an envelope did not authenticate (wrong key, or altered): dropped", file=sys.stderr) return None if last.get(e["gw"], (-1, -1)) >= (e["b"], e["n"]): return None last[e["gw"]] = (e["b"], e["n"]) return t return open_ s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0) # IPv4 arrives too s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) if hasattr(socket, "SO_REUSEPORT"): # share the port s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) s.bind(("::", PORT)) print("listening on UDP %d (IPv6 and IPv4); Ctrl-C to stop" % PORT, file=sys.stderr) KEY = sys.argv[sys.argv.index("--key") + 1] if "--key" in sys.argv else None open_ = opener(KEY) if KEY else None recent = [] # (src, seq) of the last few hundred telegrams while True: data, peer = s.recvfrom(2048) if "--json" in sys.argv: print(data.decode().rstrip(), flush=True) continue t = json.loads(data) if "enc" in t: if not open_: print("encrypted output: run with --key", file=sys.stderr) continue t = open_(t) if t is None: continue if "io" in t: # analog data, when chosen on the gateway (0.57.117) key = json.dumps(t, sort_keys=True) if key not in recent: recent = (recent + [key])[-500:] print("%s %s %s" % (time.strftime("%H:%M:%S"), t["io"], json.dumps({k: v for k, v in t.items() if k != "io"})), flush=True) continue key = (t["src"], t["seq"]) if key in recent: # the same telegram over the other family continue recent = (recent + [key])[-500:] print("%s %s seq %-9d rssi %4d dBm missed %-3d payload %s" % (time.strftime("%H:%M:%S"), t["src"], t["seq"], t["rssi"], t["gaps"], t["d"]), flush=True)