This is an automated email from the ASF dual-hosted git repository.

rymek pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git


The following commit(s) were added to refs/heads/master by this push:
     new 2da39a1  nimble/gap: Fix storing CCC for bonded devices.
2da39a1 is described below

commit 2da39a1fe53a635252cc04cac3f8df7305af4eca
Author: Ɓukasz Rymanowski <lukasz.rymanow...@codecoup.pl>
AuthorDate: Tue Apr 21 09:48:02 2020 +0200

    nimble/gap: Fix storing CCC for bonded devices.
    
    This PR make sure that ble_gatts_bonding_established is called only when
    bonded.
    
    Note: renamed not used "persist_keys" to "bonded" and make use of it
    
    Bug can be easly reproduced when
    BLE_SM_BONDING: 0
    BLE_HS_DEBUG: 1
    
    Program received signal SIGTRAP, Trace/breakpoint trap.
    ble_gatts_bonding_established (conn_handle=conn_handle@entry=1) at 
repos/apache-mynewt-nimble/nimble/host/src/ble_gatts.c:1681
    1681            BLE_HS_DBG_ASSERT(conn->bhc_sec_state.bonded);
    (gdb)
---
 nimble/host/src/ble_gap.c      | 27 ++++++++++++++++++++-------
 nimble/host/src/ble_gap_priv.h |  2 +-
 nimble/host/src/ble_sm.c       |  5 +++--
 nimble/host/src/ble_sm_priv.h  |  8 ++++----
 4 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/nimble/host/src/ble_gap.c b/nimble/host/src/ble_gap.c
index 53c6bf3..4df3b7b 100644
--- a/nimble/host/src/ble_gap.c
+++ b/nimble/host/src/ble_gap.c
@@ -5654,7 +5654,8 @@ ble_gap_passkey_event(uint16_t conn_handle,
 }
 
 void
-ble_gap_enc_event(uint16_t conn_handle, int status, int security_restored)
+ble_gap_enc_event(uint16_t conn_handle, int status,
+                  int security_restored, int bonded)
 {
 #if NIMBLE_BLE_SM
     struct ble_gap_event event;
@@ -5667,12 +5668,24 @@ ble_gap_enc_event(uint16_t conn_handle, int status, int 
security_restored)
     ble_gap_event_listener_call(&event);
     ble_gap_call_conn_event_cb(&event, conn_handle);
 
-    if (status == 0) {
-        if (security_restored) {
-            ble_gatts_bonding_restored(conn_handle);
-        } else {
-            ble_gatts_bonding_established(conn_handle);
-        }
+    if (status != 0) {
+        return;
+    }
+
+    /* If encryption succeded and encryption has been restored for bonded 
device,
+     * notify gatt server so it has chance to send notification/indication if 
needed.
+     */
+    if (security_restored) {
+        ble_gatts_bonding_restored(conn_handle);
+        return;
+    }
+
+    /* If this is fresh pairing and bonding has been established,
+     * notify gatt server about that so previous subscriptions (before bonding)
+     * can be stored.
+     */
+    if (bonded) {
+        ble_gatts_bonding_established(conn_handle);
     }
 #endif
 }
diff --git a/nimble/host/src/ble_gap_priv.h b/nimble/host/src/ble_gap_priv.h
index c050435..499823b 100644
--- a/nimble/host/src/ble_gap_priv.h
+++ b/nimble/host/src/ble_gap_priv.h
@@ -116,7 +116,7 @@ int ble_gap_rx_l2cap_update_req(uint16_t conn_handle,
                                 struct ble_gap_upd_params *params);
 void ble_gap_rx_phy_update_complete(const struct 
ble_hci_ev_le_subev_phy_update_complete *ev);
 void ble_gap_enc_event(uint16_t conn_handle, int status,
-                       int security_restored);
+                       int security_restored, int bonded);
 void ble_gap_passkey_event(uint16_t conn_handle,
                            struct ble_gap_passkey_params *passkey_params);
 void ble_gap_notify_rx_event(uint16_t conn_handle, uint16_t attr_handle,
diff --git a/nimble/host/src/ble_sm.c b/nimble/host/src/ble_sm.c
index cfd80fc..91afb75 100644
--- a/nimble/host/src/ble_sm.c
+++ b/nimble/host/src/ble_sm.c
@@ -938,7 +938,7 @@ ble_sm_process_result(uint16_t conn_handle, struct 
ble_sm_result *res)
 
         if (res->enc_cb) {
             BLE_HS_DBG_ASSERT(proc == NULL || rm);
-            ble_gap_enc_event(conn_handle, res->app_status, res->restore);
+            ble_gap_enc_event(conn_handle, res->app_status, res->restore, 
res->bonded);
         }
 
         if (res->app_status == 0 &&
@@ -1190,6 +1190,7 @@ ble_sm_enc_event_rx(uint16_t conn_handle, uint8_t 
evt_status, int encrypted)
 
     ble_hs_unlock();
 
+    res.bonded = bonded;
     ble_sm_process_result(conn_handle, &res);
 }
 
@@ -2425,7 +2426,7 @@ ble_sm_timer(void)
      * procedures without reconnect.
      */
     while ((proc = STAILQ_FIRST(&exp_list)) != NULL) {
-        ble_gap_enc_event(proc->conn_handle, BLE_HS_ETIMEOUT, 0);
+        ble_gap_enc_event(proc->conn_handle, BLE_HS_ETIMEOUT, 0, 0);
 
         STAILQ_REMOVE_HEAD(&exp_list, next);
         ble_sm_proc_free(proc);
diff --git a/nimble/host/src/ble_sm_priv.h b/nimble/host/src/ble_sm_priv.h
index 6d5601b..27e75aa 100644
--- a/nimble/host/src/ble_sm_priv.h
+++ b/nimble/host/src/ble_sm_priv.h
@@ -279,10 +279,10 @@ struct ble_sm_result {
     uint8_t sm_err;
     struct ble_gap_passkey_params passkey_params;
     void *state_arg;
-    unsigned execute:1;
-    unsigned enc_cb:1;
-    unsigned persist_keys:1;
-    unsigned restore:1;
+    unsigned execute : 1;
+    unsigned enc_cb : 1;
+    unsigned bonded : 1;
+    unsigned restore : 1;
 };
 
 #if MYNEWT_VAL(BLE_HS_DEBUG)

Reply via email to