Skip to content

Commit 75ab7fd

Browse files
committed
aioble/server: Make singleton service re-registration safe.
Preserve _initial across re-registration instead of consuming it. Invalidate characteristic handles and wake blocked written() callers on server shutdown. Auto-rebuild capture infrastructure in register_services() when capture-enabled characteristics are present. Signed-off-by: Andrew Langmeier <andrew.langmeier@planetinnovation.com>
1 parent 3eaf027 commit 75ab7fd

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

micropython/bluetooth/aioble/aioble/server.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
register_irq_handler,
1616
GattError,
1717
)
18-
from .device import DeviceConnection, DeviceTimeout
18+
from .device import DeviceConnection, DeviceDisconnectedError, DeviceTimeout
1919

2020
_registered_characteristics = {}
2121

@@ -56,6 +56,10 @@ def _server_irq(event, data):
5656

5757
def _server_shutdown():
5858
global _registered_characteristics
59+
for characteristic in _registered_characteristics.values():
60+
if hasattr(characteristic, "_write_event"):
61+
characteristic._write_event.set()
62+
characteristic._value_handle = None
5963
_registered_characteristics = {}
6064
if hasattr(BaseCharacteristic, "_capture_task"):
6165
BaseCharacteristic._capture_task.cancel()
@@ -84,7 +88,6 @@ def _register(self, value_handle):
8488
_registered_characteristics[value_handle] = self
8589
if self._initial is not None:
8690
self.write(self._initial)
87-
self._initial = None
8891

8992
# Read value from local db.
9093
def read(self):
@@ -152,6 +155,9 @@ async def written(self, timeout_ms=None):
152155
with DeviceTimeout(None, timeout_ms):
153156
await self._write_event.wait()
154157

158+
if self._value_handle is None:
159+
raise DeviceDisconnectedError()
160+
155161
# Return the write data and clear the stored copy.
156162
# In default usage this will be just the connection handle.
157163
# In capture mode this will be a tuple of (connection_handle, received_data)
@@ -338,3 +344,8 @@ def register_services(*services):
338344
for descriptor in characteristic.descriptors:
339345
descriptor._register(service_handles[n])
340346
n += 1
347+
348+
for characteristic in _registered_characteristics.values():
349+
if characteristic.flags & _FLAG_WRITE_CAPTURE:
350+
BaseCharacteristic._init_capture()
351+
break

0 commit comments

Comments
 (0)