2121
2222import infuse_iot .epacket .interface as interface
2323import 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
2526from infuse_iot .commands import InfuseCommand
2627from infuse_iot .common import InfuseID , InfuseType
2728from infuse_iot .database import (
3940from 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