Skip to content

Commit 93492e0

Browse files
committed
aioble/security: Add DeviceConnection.pairing_in_progress flag.
Will be True if the pairing process has been started but is waiting for ack from remote device.
1 parent 5b496e9 commit 93492e0

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

micropython/bluetooth/aioble/aioble/device.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,15 @@ class DeviceConnection:
163163
_connected = {}
164164

165165
def __init__(self, device):
166-
self.device = device
166+
self.device: Device = device
167167
device._connection = self
168168

169169
self.encrypted = False
170170
self.authenticated = False
171171
self.bonded = False
172172
self.key_size = False
173173
self.mtu = None
174+
self.pairing_in_progress = False
174175

175176
self._conn_handle = None
176177

micropython/bluetooth/aioble/aioble/security.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import json
88

99
from .core import log_info, log_warn, ble, register_irq_handler
10-
from .device import DeviceConnection
10+
from .device import DeviceConnection, Device
1111

1212
_IRQ_ENCRYPTION_UPDATE = const(28)
1313
_IRQ_GET_SECRET = const(29)
@@ -51,6 +51,15 @@ def load_secrets(path=None):
5151
log_warn("No secrets available")
5252

5353

54+
def _get_connection(key) -> DeviceConnection:
55+
if not key:
56+
return None
57+
addr = bytes(reversed(key[-6:]))
58+
for connection in DeviceConnection._connected.values():
59+
if connection.device.addr == addr:
60+
return connection
61+
62+
5463
# Call this whenever the secrets dict changes.
5564
def _save_secrets(arg=None):
5665
global _modified, _path
@@ -84,6 +93,7 @@ def _security_irq(event, data):
8493
connection.authenticated = authenticated
8594
connection.bonded = bonded
8695
connection.key_size = key_size
96+
connection.pairing_in_progress = False
8797
# TODO: Handle failure.
8898
if encrypted and connection._pair_event:
8999
connection._pair_event.set()
@@ -126,6 +136,11 @@ def _security_irq(event, data):
126136
i += 1
127137
return None
128138
else:
139+
if sec_type in SEC_TYPES_PEER:
140+
if conn := _get_connection(key):
141+
log_info("encryption / pairing started", conn)
142+
conn.pairing_in_progress = True
143+
129144
# Return the secret for this key (or None).
130145
key = sec_type, bytes(key)
131146
return _secrets.get(key, None)

0 commit comments

Comments
 (0)