浏览代码

bluetooth-fw/nimble: only notify about completed pairing if needed

NimBLE stack reports the BLE_GAP_EVENT_PAIRING_COMPLETE in every
connection after a pairing is done, not just after a successful
BLE_GAP_EVENT_PASSKEY_ACTION. This means we are sending unnecessary
events if pairing was performed before.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
Gerard Marull-Paretas 1 月之前
父节点
当前提交
943c0179e6
共有 1 个文件被更改,包括 10 次插入0 次删除
  1. 10 0
      src/bluetooth-fw/nimble/advert.c

+ 10 - 0
src/bluetooth-fw/nimble/advert.c

@@ -31,6 +31,7 @@
 
 
 static const ble_uuid16_t s_device_name_chr_uuid = BLE_UUID16_INIT(0x2A00);
 static const ble_uuid16_t s_device_name_chr_uuid = BLE_UUID16_INIT(0x2A00);
 static char s_device_name[BT_DEVICE_NAME_BUFFER_SIZE];
 static char s_device_name[BT_DEVICE_NAME_BUFFER_SIZE];
+static bool s_pairing_in_progress;
 
 
 static int prv_device_name_read_event_cb(uint16_t conn_handle, const struct ble_gatt_error *error,
 static int prv_device_name_read_event_cb(uint16_t conn_handle, const struct ble_gatt_error *error,
                                          struct ble_gatt_attr *attr, void *arg) {
                                          struct ble_gatt_attr *attr, void *arg) {
@@ -115,6 +116,9 @@ static void prv_handle_connection_event(struct ble_gap_event *event) {
 
 
   nimble_conn_params_to_pebble(&desc, &complete_event.conn_params);
   nimble_conn_params_to_pebble(&desc, &complete_event.conn_params);
   nimble_addr_to_pebble_device(&desc.peer_id_addr, &complete_event.peer_address);
   nimble_addr_to_pebble_device(&desc.peer_id_addr, &complete_event.peer_address);
+
+  s_pairing_in_progress = false;
+
   bt_driver_handle_le_connection_complete_event(&complete_event);
   bt_driver_handle_le_connection_complete_event(&complete_event);
 }
 }
 
 
@@ -185,12 +189,18 @@ static void prv_handle_passkey_event(struct ble_gap_event *event) {
 
 
   snprintf(passkey_str, sizeof(passkey_str), "%06lu", passkey);
   snprintf(passkey_str, sizeof(passkey_str), "%06lu", passkey);
   bt_driver_cb_pairing_confirm_handle_request(ctx, device_name, passkey_str);
   bt_driver_cb_pairing_confirm_handle_request(ctx, device_name, passkey_str);
+  s_pairing_in_progress = true;
 }
 }
 
 
 static void prv_handle_pairing_complete_event(struct ble_gap_event *event) {
 static void prv_handle_pairing_complete_event(struct ble_gap_event *event) {
+  if (!s_pairing_in_progress) {
+    return;
+  }
+
   PairingUserConfirmationCtx *ctx =
   PairingUserConfirmationCtx *ctx =
       (PairingUserConfirmationCtx *)((uintptr_t)event->pairing_complete.conn_handle);
       (PairingUserConfirmationCtx *)((uintptr_t)event->pairing_complete.conn_handle);
   bt_driver_cb_pairing_confirm_handle_completed(ctx, event->pairing_complete.status == 0);
   bt_driver_cb_pairing_confirm_handle_completed(ctx, event->pairing_complete.status == 0);
+  s_pairing_in_progress = false;
 }
 }
 
 
 static void prv_handle_identity_resolved_event(struct ble_gap_event *event) {
 static void prv_handle_identity_resolved_event(struct ble_gap_event *event) {