From 6399c5fb04b9f6e3d07c6c2d7cea1e1bec5e9d05 Mon Sep 17 00:00:00 2001 From: Josh Wu Date: Wed, 2 Aug 2023 19:08:51 +0800 Subject: [PATCH] Auto add device to resolving list after pairing --- bumble/smp.py | 15 ++++----------- tests/self_test.py | 31 +++++++++++++++---------------- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/bumble/smp.py b/bumble/smp.py index c93ee9c..9588a5a 100644 --- a/bumble/smp.py +++ b/bumble/smp.py @@ -1272,7 +1272,7 @@ class Session: keys.link_key = PairingKeys.Key( value=self.link_key, authenticated=authenticated ) - self.manager.on_pairing(self, peer_address, keys) + await self.manager.on_pairing(self, peer_address, keys) def on_pairing_failure(self, reason: int) -> None: logger.warning(f'pairing failure ({error_name(reason)})') @@ -1827,20 +1827,13 @@ class Manager(EventEmitter): def on_session_start(self, session: Session) -> None: self.device.on_pairing_start(session.connection) - def on_pairing( + async def on_pairing( self, session: Session, identity_address: Optional[Address], keys: PairingKeys ) -> None: # Store the keys in the key store if self.device.keystore and identity_address is not None: - - async def store_keys(): - try: - assert self.device.keystore - await self.device.keystore.update(str(identity_address), keys) - except Exception as error: - logger.warning(f'!!! error while storing keys: {error}') - - self.device.abort_on('flush', store_keys()) + await self.device.keystore.update(str(identity_address), keys) + await self.device.refresh_resolving_list() # Notify the device self.device.on_pairing(session.connection, identity_address, keys, session.sc) diff --git a/tests/self_test.py b/tests/self_test.py index 4c35045..98ce5e8 100644 --- a/tests/self_test.py +++ b/tests/self_test.py @@ -68,13 +68,16 @@ class TwoDevices: ), ] - self.paired = [None, None] + self.paired = [ + asyncio.get_event_loop().create_future(), + asyncio.get_event_loop().create_future(), + ] def on_connection(self, which, connection): self.connections[which] = connection - def on_paired(self, which, keys): - self.paired[which] = keys + def on_paired(self, which: int, keys: PairingKeys): + self.paired[which].set_result(keys) # ----------------------------------------------------------------------------- @@ -323,8 +326,8 @@ async def _test_self_smp_with_configs(pairing_config1, pairing_config2): # Pair await two_devices.devices[0].pair(connection) assert connection.is_encrypted - assert two_devices.paired[0] is not None - assert two_devices.paired[1] is not None + assert await two_devices.paired[0] is not None + assert await two_devices.paired[1] is not None # ----------------------------------------------------------------------------- @@ -527,16 +530,12 @@ async def test_self_smp_over_classic(): two_devices.connections[0].encryption = 1 two_devices.connections[1].encryption = 1 - paired = [ - asyncio.get_event_loop().create_future(), - asyncio.get_event_loop().create_future(), - ] - - def on_pairing(which: int, keys: PairingKeys): - paired[which].set_result(keys) - - two_devices.connections[0].on('pairing', lambda keys: on_pairing(0, keys)) - two_devices.connections[1].on('pairing', lambda keys: on_pairing(1, keys)) + two_devices.connections[0].on( + 'pairing', lambda keys: two_devices.on_paired(0, keys) + ) + two_devices.connections[1].on( + 'pairing', lambda keys: two_devices.on_paired(1, keys) + ) # Mock SMP with patch('bumble.smp.Session', spec=True) as MockSmpSession: @@ -547,7 +546,7 @@ async def test_self_smp_over_classic(): # Start CTKD await two_devices.connections[0].pair() - await asyncio.gather(*paired) + await asyncio.gather(*two_devices.paired) # Phase 2 commands should not be invoked MockSmpSession.send_pairing_confirm_command.assert_not_called()