From 10ec742baa12881bb9b8227c6729b99631f6be6f Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 11 Nov 2021 11:04:00 +1100 Subject: [PATCH] aioble: Add a shutdown handler for cleanup. This allows `aioble.stop()` to reset all internal state. Signed-off-by: Jim Mussared --- .../bluetooth/aioble/aioble/central.py | 8 +++++- micropython/bluetooth/aioble/aioble/client.py | 2 +- micropython/bluetooth/aioble/aioble/core.py | 25 ++++++++++++------- micropython/bluetooth/aioble/aioble/device.py | 2 +- micropython/bluetooth/aioble/aioble/l2cap.py | 7 +++++- .../bluetooth/aioble/aioble/peripheral.py | 8 +++++- .../bluetooth/aioble/aioble/security.py | 9 ++++++- micropython/bluetooth/aioble/aioble/server.py | 7 +++++- 8 files changed, 52 insertions(+), 16 deletions(-) diff --git a/micropython/bluetooth/aioble/aioble/central.py b/micropython/bluetooth/aioble/aioble/central.py index 374fc6ea..46da907a 100644 --- a/micropython/bluetooth/aioble/aioble/central.py +++ b/micropython/bluetooth/aioble/aioble/central.py @@ -86,7 +86,13 @@ def _central_irq(event, data): connection._event.set() -register_irq_handler(_central_irq) +def _central_shutdown(): + global _active_scanner, _connecting + _active_scanner = None + _connecting = set() + + +register_irq_handler(_central_irq, _central_shutdown) # Cancel an in-progress scan. diff --git a/micropython/bluetooth/aioble/aioble/client.py b/micropython/bluetooth/aioble/aioble/client.py index 963c0e32..35e33a52 100644 --- a/micropython/bluetooth/aioble/aioble/client.py +++ b/micropython/bluetooth/aioble/aioble/client.py @@ -78,7 +78,7 @@ def _client_irq(event, data): ClientCharacteristic._on_indicate(conn_handle, value_handle, bytes(indicate_data)) -register_irq_handler(_client_irq) +register_irq_handler(_client_irq, None) # Async generator for discovering services, characteristics, descriptors. diff --git a/micropython/bluetooth/aioble/aioble/core.py b/micropython/bluetooth/aioble/aioble/core.py index 44302443..8daa2446 100644 --- a/micropython/bluetooth/aioble/aioble/core.py +++ b/micropython/bluetooth/aioble/aioble/core.py @@ -43,17 +43,24 @@ def config(*args, **kwargs): return ble.config(*args, **kwargs) +# Because different functionality is enabled by which files are available the +# different modules can register their IRQ handlers and shutdown handlers +# dynamically. +_irq_handlers = [] +_shutdown_handlers = [] + + +def register_irq_handler(irq, shutdown): + if irq: + _irq_handlers.append(irq) + if shutdown: + _shutdown_handlers.append(shutdown) + + def stop(): ble.active(False) - - -# Because different functionality is enabled by which files are available -# the different modules can register their IRQ handlers dynamically. -_irq_handlers = [] - - -def register_irq_handler(handler): - _irq_handlers.append(handler) + for handler in _shutdown_handlers: + handler() # Dispatch IRQs to the registered sub-modules. diff --git a/micropython/bluetooth/aioble/aioble/device.py b/micropython/bluetooth/aioble/aioble/device.py index 9634f6d6..ea87be35 100644 --- a/micropython/bluetooth/aioble/aioble/device.py +++ b/micropython/bluetooth/aioble/aioble/device.py @@ -26,7 +26,7 @@ def _device_irq(event, data): device._mtu_event.set() -register_irq_handler(_device_irq) +register_irq_handler(_device_irq, None) # Context manager to allow an operation to be cancelled by timeout or device diff --git a/micropython/bluetooth/aioble/aioble/l2cap.py b/micropython/bluetooth/aioble/aioble/l2cap.py index 06c2a6cd..4d8fd927 100644 --- a/micropython/bluetooth/aioble/aioble/l2cap.py +++ b/micropython/bluetooth/aioble/aioble/l2cap.py @@ -54,7 +54,12 @@ def _l2cap_irq(event, data): channel._event.set() -register_irq_handler(_l2cap_irq) +def _l2cap_shutdown(): + global _listening + _listening = False + + +register_irq_handler(_l2cap_irq, _l2cap_shutdown) # The channel was disconnected during a send/recvinto/flush. diff --git a/micropython/bluetooth/aioble/aioble/peripheral.py b/micropython/bluetooth/aioble/aioble/peripheral.py index 61beebf0..0b89c487 100644 --- a/micropython/bluetooth/aioble/aioble/peripheral.py +++ b/micropython/bluetooth/aioble/aioble/peripheral.py @@ -63,7 +63,13 @@ def _peripheral_irq(event, data): connection._event.set() -register_irq_handler(_peripheral_irq) +def _peripheral_shutdown(): + global _incoming_connection, _connect_event + _incoming_connection = None + _connect_event = None + + +register_irq_handler(_peripheral_irq, _peripheral_shutdown) # Advertising payloads are repeated packets of the following form: diff --git a/micropython/bluetooth/aioble/aioble/security.py b/micropython/bluetooth/aioble/aioble/security.py index 9ca4651d..a0b46e6d 100644 --- a/micropython/bluetooth/aioble/aioble/security.py +++ b/micropython/bluetooth/aioble/aioble/security.py @@ -149,7 +149,14 @@ def _security_irq(event, data): # log_warn("unknown passkey action") -register_irq_handler(_security_irq) +def _security_shutdown(): + global _secrets, _modified, _path + _secrets = {} + _modified = False + _path = None + + +register_irq_handler(_security_irq, _security_shutdown) # Use device.pair() rather than calling this directly. diff --git a/micropython/bluetooth/aioble/aioble/server.py b/micropython/bluetooth/aioble/aioble/server.py index f87e4732..b537638e 100644 --- a/micropython/bluetooth/aioble/aioble/server.py +++ b/micropython/bluetooth/aioble/aioble/server.py @@ -56,7 +56,12 @@ def _server_irq(event, data): Characteristic._indicate_done(conn_handle, value_handle, status) -register_irq_handler(_server_irq) +def _server_shutdown(): + global _registered_characteristics + _registered_characteristics = {} + + +register_irq_handler(_server_irq, _server_shutdown) class Service: