Skip to content

Commit 539b9fa

Browse files
committed
aioble/security: Add support for CCCD bonding.
1 parent c8a0f4c commit 539b9fa

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

micropython/bluetooth/aioble/aioble/security.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
limit_peers = None
3636

3737
SEC_TYPES_SELF = (10, )
38-
SEC_TYPES_PEER = (1, 2, 3, 4)
39-
38+
SEC_TYPES_PEER = (1, 2, 4)
39+
SEC_TYPES_CCCD = (3, )
4040

4141
# Must call this before stack startup.
4242
def load_secrets(path=None):
@@ -105,14 +105,16 @@ def _save_secrets(arg=None):
105105
_modified = False
106106

107107

108-
def _remove_entry(sec_type, key):
108+
def _remove_entry(sec_type, key, value=None):
109109
secrets = _secrets[sec_type]
110110

111111
# Delete existing secrets matching the type and key.
112112
deleted = False
113113
for to_delete in [
114114
entry for entry in secrets if entry[0] == key
115115
]:
116+
if value and to_delete[1] != value:
117+
continue
116118
log_info("Removing existing secret matching key")
117119
secrets.remove(to_delete)
118120
deleted = True
@@ -124,7 +126,7 @@ def _log_peers(heading=""):
124126
if core.log_level <= 2:
125127
return
126128
log_info("secrets:", heading)
127-
for sec_type in SEC_TYPES_PEER:
129+
for sec_type in SEC_TYPES_PEER + SEC_TYPES_CCCD:
128130
log_info("-", sec_type)
129131

130132
if sec_type not in _secrets:
@@ -171,8 +173,13 @@ def _security_irq(event, data):
171173
_secrets[sec_type] = []
172174
secrets = _secrets[sec_type]
173175

174-
# Delete existing secrets matching the type and key.
175-
removed = _remove_entry(sec_type, key)
176+
# Delete existing secrets matching the type and key as required.
177+
# There should only every be one SEC_TYPES_PEER per addr, but
178+
# multiple entries are allowed for SEC_TYPES_CCCD
179+
del_by_value = None
180+
if not is_deleting and sec_type in SEC_TYPES_CCCD:
181+
del_by_value = value
182+
removed = _remove_entry(sec_type, key, del_by_value)
176183

177184
if is_deleting and not removed:
178185
# Delete mode, but no entries were deleted
@@ -216,6 +223,9 @@ def _security_irq(event, data):
216223

217224
for k, v in secrets:
218225
if k == key:
226+
if index:
227+
index -= 1
228+
continue
219229
return v
220230
return None
221231

0 commit comments

Comments
 (0)