Skip to content

Commit 5d3101b

Browse files
committed
aioble/security: Defer saving of CCCD bond information.
1 parent 81591ae commit 5d3101b

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

micropython/bluetooth/aioble/aioble/device.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ async def disconnect(self, timeout_ms=2000):
216216
await self.disconnected(timeout_ms, disconnect=True)
217217

218218
async def disconnected(self, timeout_ms=60000, disconnect=False):
219+
from . import security
219220
if not self.is_connected():
220221
return
221222

@@ -231,6 +232,9 @@ async def disconnected(self, timeout_ms=60000, disconnect=False):
231232
with DeviceTimeout(None, timeout_ms):
232233
await self._task
233234

235+
# Ensure any cached secrets are written to disk
236+
security.flush()
237+
234238
# Retrieve a single service matching this uuid.
235239
async def service(self, uuid, timeout_ms=2000):
236240
result = None

micropython/bluetooth/aioble/aioble/security.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ def load_secrets(path=None):
8282
log_warn("No secrets available")
8383

8484

85+
# Writes secrets to disk if needed
86+
def flush():
87+
if _modified:
88+
schedule(_save_secrets, None)
89+
else:
90+
print("security flush not needed")
91+
92+
8593
# Call this whenever the secrets dict changes.
8694
def _save_secrets(arg=None):
8795
global _modified, _path
@@ -189,8 +197,13 @@ def _security_irq(event, data):
189197
log_info("Removed:", addr)
190198
# Add new value to database
191199
secrets.append((key, value))
192-
193-
_log_peers("set_secret")
200+
201+
if sec_type in SEC_TYPES_CCCD:
202+
# Don't write immediately for CCCD, queue up for later flush when needed
203+
log_info("set_cccd")
204+
_modified = True
205+
else:
206+
_log_peers("set_secret")
194207

195208
# Queue up a save (don't synchronously write to flash).
196209
_modified = True
@@ -248,6 +261,7 @@ def _security_irq(event, data):
248261

249262
def _security_shutdown():
250263
global _secrets, _modified, _path
264+
flush()
251265
_secrets = {}
252266
_modified = False
253267
_path = None

0 commit comments

Comments
 (0)