|
15 | 15 | register_irq_handler, |
16 | 16 | GattError, |
17 | 17 | ) |
18 | | -from .device import DeviceConnection, DeviceTimeout |
| 18 | +from .device import DeviceConnection, DeviceDisconnectedError, DeviceTimeout |
19 | 19 |
|
20 | 20 | _registered_characteristics = {} |
21 | 21 |
|
@@ -56,6 +56,10 @@ def _server_irq(event, data): |
56 | 56 |
|
57 | 57 | def _server_shutdown(): |
58 | 58 | 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 |
59 | 63 | _registered_characteristics = {} |
60 | 64 | if hasattr(BaseCharacteristic, "_capture_task"): |
61 | 65 | BaseCharacteristic._capture_task.cancel() |
@@ -84,7 +88,6 @@ def _register(self, value_handle): |
84 | 88 | _registered_characteristics[value_handle] = self |
85 | 89 | if self._initial is not None: |
86 | 90 | self.write(self._initial) |
87 | | - self._initial = None |
88 | 91 |
|
89 | 92 | # Read value from local db. |
90 | 93 | def read(self): |
@@ -152,6 +155,9 @@ async def written(self, timeout_ms=None): |
152 | 155 | with DeviceTimeout(None, timeout_ms): |
153 | 156 | await self._write_event.wait() |
154 | 157 |
|
| 158 | + if self._value_handle is None: |
| 159 | + raise DeviceDisconnectedError() |
| 160 | + |
155 | 161 | # Return the write data and clear the stored copy. |
156 | 162 | # In default usage this will be just the connection handle. |
157 | 163 | # In capture mode this will be a tuple of (connection_handle, received_data) |
@@ -338,3 +344,8 @@ def register_services(*services): |
338 | 344 | for descriptor in characteristic.descriptors: |
339 | 345 | descriptor._register(service_handles[n]) |
340 | 346 | 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