Skip to content

Commit cc88a7c

Browse files
committed
tools: gateway: broadcast ConnectionDropped event
If the connected basestation reports that a connection has dropped, forward that event to connected clients. Signed-off-by: Jordan Yates <jordan@embeint.com>
1 parent de09034 commit cc88a7c

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

src/infuse_iot/epacket/interface.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing_extensions import Self
77

88
import infuse_iot.generated.rpc_definitions as rpc_defs
9+
import infuse_iot.generated.tdf_definitions as tdf_defs
910
from infuse_iot.epacket.common import Serializable
1011
from infuse_iot.util.ctypes import bytes_to_uint8
1112

@@ -83,6 +84,12 @@ def from_rpc_struct(cls, struct: rpc_defs.rpc_struct_bt_addr_le):
8384

8485
return cls(struct.type, int.from_bytes(struct.val, "little"))
8586

87+
@classmethod
88+
def from_tdf_struct(cls, struct: tdf_defs.structs.tdf_struct_bt_addr_le):
89+
"""Create instance from the common TDF address structure"""
90+
91+
return cls(struct.type, struct.val)
92+
8693
def __init__(self, val):
8794
self.val = val
8895

src/infuse_iot/tools/gateway.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
import infuse_iot.epacket.interface as interface
2323
import infuse_iot.generated.rpc_definitions as defs
24-
from infuse_iot import rpc
24+
import infuse_iot.generated.tdf_definitions as tdf_defs
25+
from infuse_iot import rpc, tdf
2526
from infuse_iot.commands import InfuseCommand
2627
from infuse_iot.common import InfuseID, InfuseType
2728
from infuse_iot.database import (
@@ -39,6 +40,7 @@
3940
from infuse_iot.socket_comms import (
4041
ClientNotification,
4142
ClientNotificationConnectionCreated,
43+
ClientNotificationConnectionDropped,
4244
ClientNotificationConnectionFailed,
4345
ClientNotificationEpacketReceived,
4446
GatewayRequestConnectionRelease,
@@ -153,6 +155,7 @@ def __init__(self, common: CommonThreadState, log: io.TextIOWrapper):
153155
self._line = ""
154156
self._log = log
155157
self._next_ping = 0.0
158+
self._tdf_decoder = tdf.TDF()
156159
super().__init__(self._iter)
157160

158161
def _iter(self) -> None:
@@ -190,6 +193,17 @@ class memfault_chunk_header(ctypes.LittleEndianStructure):
190193
p = p[3 + hdr.len :]
191194
print(f"Memfault Chunk {hdr.cnt:3d}: {base64.b64encode(chunk).decode('utf-8')}")
192195

196+
def _handle_local_tdf(self, pkt: PacketReceived):
197+
if self._common.server is None:
198+
# No-one to broadcast events to
199+
return
200+
for reading in self._tdf_decoder.decode(pkt.payload):
201+
if isinstance(reading.data[0], tdf_defs.readings.bluetooth_connection) and reading.data[0].connected == 0:
202+
if_addr = interface.Address.BluetoothLeAddr.from_tdf_struct(reading.data[0].address)
203+
infuse_id = self._common.ddb.infuse_id_from_bluetooth(if_addr)
204+
if infuse_id:
205+
self._common.server.broadcast(ClientNotificationConnectionDropped(infuse_id))
206+
193207
def _handle_serial_frame(self, frame: bytearray):
194208
try:
195209
# Decode the serial packet
@@ -216,6 +230,9 @@ def _handle_serial_frame(self, frame: bytearray):
216230
# Iterate over all contained subpackets
217231
for pkt in decoded:
218232
Console.log_rx(pkt.ptype, len(frame))
233+
# Handle any local TDFs
234+
if len(pkt.route) == 1 and pkt.ptype == InfuseType.TDF:
235+
self._handle_local_tdf(pkt)
219236
# Handle any local RPC responses
220237
self._common.rpc.handle(pkt)
221238
# Handle any Memfault chunks

0 commit comments

Comments
 (0)