|
7 | 7 | import json
|
8 | 8 |
|
9 | 9 | from .core import log_info, log_warn, ble, register_irq_handler
|
10 |
| -from .device import DeviceConnection |
| 10 | +from .device import DeviceConnection, Device |
11 | 11 |
|
12 | 12 | _IRQ_ENCRYPTION_UPDATE = const(28)
|
13 | 13 | _IRQ_GET_SECRET = const(29)
|
@@ -51,6 +51,15 @@ def load_secrets(path=None):
|
51 | 51 | log_warn("No secrets available")
|
52 | 52 |
|
53 | 53 |
|
| 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 | + |
54 | 63 | # Call this whenever the secrets dict changes.
|
55 | 64 | def _save_secrets(arg=None):
|
56 | 65 | global _modified, _path
|
@@ -84,6 +93,7 @@ def _security_irq(event, data):
|
84 | 93 | connection.authenticated = authenticated
|
85 | 94 | connection.bonded = bonded
|
86 | 95 | connection.key_size = key_size
|
| 96 | + connection.pairing_in_progress = False |
87 | 97 | # TODO: Handle failure.
|
88 | 98 | if encrypted and connection._pair_event:
|
89 | 99 | connection._pair_event.set()
|
@@ -126,6 +136,11 @@ def _security_irq(event, data):
|
126 | 136 | i += 1
|
127 | 137 | return None
|
128 | 138 | 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 | + |
129 | 144 | # Return the secret for this key (or None).
|
130 | 145 | key = sec_type, bytes(key)
|
131 | 146 | return _secrets.get(key, None)
|
|
0 commit comments