Skip to content

Commit 10ec742

Browse files
jimmodpgeorge
authored andcommitted
aioble: Add a shutdown handler for cleanup.
This allows `aioble.stop()` to reset all internal state. Signed-off-by: Jim Mussared <[email protected]>
1 parent a61bfc1 commit 10ec742

File tree

8 files changed

+50
-14
lines changed

8 files changed

+50
-14
lines changed

micropython/bluetooth/aioble/aioble/central.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,13 @@ def _central_irq(event, data):
8686
connection._event.set()
8787

8888

89-
register_irq_handler(_central_irq)
89+
def _central_shutdown():
90+
global _active_scanner, _connecting
91+
_active_scanner = None
92+
_connecting = set()
93+
94+
95+
register_irq_handler(_central_irq, _central_shutdown)
9096

9197

9298
# Cancel an in-progress scan.

micropython/bluetooth/aioble/aioble/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def _client_irq(event, data):
7878
ClientCharacteristic._on_indicate(conn_handle, value_handle, bytes(indicate_data))
7979

8080

81-
register_irq_handler(_client_irq)
81+
register_irq_handler(_client_irq, None)
8282

8383

8484
# Async generator for discovering services, characteristics, descriptors.

micropython/bluetooth/aioble/aioble/core.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,24 @@ def config(*args, **kwargs):
4343
return ble.config(*args, **kwargs)
4444

4545

46-
def stop():
47-
ble.active(False)
46+
# Because different functionality is enabled by which files are available the
47+
# different modules can register their IRQ handlers and shutdown handlers
48+
# dynamically.
49+
_irq_handlers = []
50+
_shutdown_handlers = []
4851

4952

50-
# Because different functionality is enabled by which files are available
51-
# the different modules can register their IRQ handlers dynamically.
52-
_irq_handlers = []
53+
def register_irq_handler(irq, shutdown):
54+
if irq:
55+
_irq_handlers.append(irq)
56+
if shutdown:
57+
_shutdown_handlers.append(shutdown)
5358

5459

55-
def register_irq_handler(handler):
56-
_irq_handlers.append(handler)
60+
def stop():
61+
ble.active(False)
62+
for handler in _shutdown_handlers:
63+
handler()
5764

5865

5966
# Dispatch IRQs to the registered sub-modules.

micropython/bluetooth/aioble/aioble/device.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def _device_irq(event, data):
2626
device._mtu_event.set()
2727

2828

29-
register_irq_handler(_device_irq)
29+
register_irq_handler(_device_irq, None)
3030

3131

3232
# Context manager to allow an operation to be cancelled by timeout or device

micropython/bluetooth/aioble/aioble/l2cap.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ def _l2cap_irq(event, data):
5454
channel._event.set()
5555

5656

57-
register_irq_handler(_l2cap_irq)
57+
def _l2cap_shutdown():
58+
global _listening
59+
_listening = False
60+
61+
62+
register_irq_handler(_l2cap_irq, _l2cap_shutdown)
5863

5964

6065
# The channel was disconnected during a send/recvinto/flush.

micropython/bluetooth/aioble/aioble/peripheral.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ def _peripheral_irq(event, data):
6363
connection._event.set()
6464

6565

66-
register_irq_handler(_peripheral_irq)
66+
def _peripheral_shutdown():
67+
global _incoming_connection, _connect_event
68+
_incoming_connection = None
69+
_connect_event = None
70+
71+
72+
register_irq_handler(_peripheral_irq, _peripheral_shutdown)
6773

6874

6975
# Advertising payloads are repeated packets of the following form:

micropython/bluetooth/aioble/aioble/security.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,14 @@ def _security_irq(event, data):
149149
# log_warn("unknown passkey action")
150150

151151

152-
register_irq_handler(_security_irq)
152+
def _security_shutdown():
153+
global _secrets, _modified, _path
154+
_secrets = {}
155+
_modified = False
156+
_path = None
157+
158+
159+
register_irq_handler(_security_irq, _security_shutdown)
153160

154161

155162
# Use device.pair() rather than calling this directly.

micropython/bluetooth/aioble/aioble/server.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ def _server_irq(event, data):
5656
Characteristic._indicate_done(conn_handle, value_handle, status)
5757

5858

59-
register_irq_handler(_server_irq)
59+
def _server_shutdown():
60+
global _registered_characteristics
61+
_registered_characteristics = {}
62+
63+
64+
register_irq_handler(_server_irq, _server_shutdown)
6065

6166

6267
class Service:

0 commit comments

Comments
 (0)