2020
2121#include "services/common/bluetooth/bluetooth_persistent_storage.h"
2222#include "services/common/bluetooth/local_addr.h"
23+ #include "kernel/event_loop.h"
24+ #include "kernel/pbl_malloc.h"
2325#include "system/logging.h"
2426
2527#include <bluetooth/bonding_sync.h>
2628#include <bluetooth/bluetooth_types.h>
2729#include <bluetooth/sm_types.h>
2830
31+ typedef struct {
32+ BTBondingID bonding_id ;
33+ BTDeviceAddress addr ;
34+ bool is_gateway ;
35+ } CreateBondingContext ;
36+
37+ static void prv_finalize_create_bonding (BTBondingID bonding_id ,
38+ const BTDeviceAddress * addr ,
39+ bool is_gateway ) {
40+ bt_lock ();
41+ GAPLEConnection * connection = gap_le_connection_by_addr (addr );
42+ if (connection ) {
43+ connection -> bonding_id = bonding_id ;
44+ connection -> is_gateway = is_gateway ;
45+
46+ if (!connection -> is_gateway ) {
47+ PBL_LOG (LOG_LEVEL_DEBUG , "New bonding is not gateway?" );
48+ }
49+
50+ gap_le_device_name_request (& connection -> device );
51+ } else {
52+ PBL_LOG (LOG_LEVEL_ERROR , "Couldn't find connection for bonding!" );
53+ }
54+ bt_unlock ();
55+ }
56+
57+ static void prv_finalize_create_bonding_cb (void * data ) {
58+ CreateBondingContext * context = data ;
59+ prv_finalize_create_bonding (context -> bonding_id , & context -> addr , context -> is_gateway );
60+ kernel_free (context );
61+ }
62+
2963void bt_driver_cb_handle_create_bonding (const BleBonding * bonding ,
3064 const BTDeviceAddress * addr ) {
3165#if !defined(PLATFORM_TINTIN )
@@ -44,24 +78,21 @@ void bt_driver_cb_handle_create_bonding(const BleBonding *bonding,
4478 bonding -> is_gateway , NULL ,
4579 should_pin_address ,
4680 flags );
47- bt_lock ();
48- {
49- GAPLEConnection * connection = gap_le_connection_by_addr (addr );
50- if (connection ) {
51- // Associate the connection with the bonding:
52- connection -> bonding_id = bonding_id ;
53- connection -> is_gateway = bonding -> is_gateway ;
54-
55- if (!connection -> is_gateway ) {
56- PBL_LOG (LOG_LEVEL_DEBUG , "New bonding is not gateway?" );
57- }
81+ if (bonding_id == BT_BONDING_ID_INVALID ) {
82+ return ;
83+ }
5884
59- // Request device name. iOS returns an "anonymized" device name before encryption, like
60- // "iPhone" and only returns the real name i.e. "Martijn's iPhone" after encryption is set up.
61- gap_le_device_name_request (& connection -> device );
62- } else {
63- PBL_LOG (LOG_LEVEL_ERROR , "Couldn't find connection for bonding!" );
64- }
85+ if (launcher_task_is_current_task ()) {
86+ prv_finalize_create_bonding (bonding_id , addr , bonding -> is_gateway );
87+ return ;
6588 }
66- bt_unlock ();
89+
90+ CreateBondingContext * context = kernel_malloc_check (sizeof (* context ));
91+
92+ * context = (CreateBondingContext ) {
93+ .bonding_id = bonding_id ,
94+ .addr = * addr ,
95+ .is_gateway = bonding -> is_gateway ,
96+ };
97+ launcher_task_add_callback (prv_finalize_create_bonding_cb , context );
6798}
0 commit comments