Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

ble_app_multirole_lesc on SDK17.1.0 vs SDK15.2.0

Hi,

I want to use a single firmware to be flashed on two devices that can be used as a ble peripheral with DK Button 3 and as a central with DK Button 4.

I chose ble_app_multirole_lesc for this purpose. I made the following changes:

diff --git a/examples/ble_central_and_peripheral/experimental/ble_app_multirole_lesc/main.c b/examples/ble_central_and_peripheral/experimental/ble_app_multirole_lesc/main.c
index e384712..84442ed 100644
--- a/examples/ble_central_and_peripheral/experimental/ble_app_multirole_lesc/main.c
+++ b/examples/ble_central_and_peripheral/experimental/ble_app_multirole_lesc/main.c
@@ -280,25 +280,6 @@ static void scan_start(void)
 }
 
 
-/**@brief Function for initializing the advertising and the scanning.
- */
-static void adv_scan_start(void)
-{
-    ret_code_t err_code;
-
-    scan_start();
-
-    // Turn on the LED to signal scanning.
-    bsp_board_led_on(CENTRAL_SCANNING_LED);
-
-    // Start advertising.
-    err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
-    APP_ERROR_CHECK(err_code);
-
-    NRF_LOG_INFO("Advertising");
-}
-
-
 /**@brief Function for handling Peer Manager events.
  *
  * @param[in] p_evt  Peer Manager event.
@@ -312,7 +293,6 @@ static void pm_evt_handler(pm_evt_t const * p_evt)
     switch (p_evt->evt_id)
     {
         case PM_EVT_PEERS_DELETE_SUCCEEDED:
-            adv_scan_start();
             break;
 
         default:
@@ -588,8 +568,6 @@ static void on_ble_central_evt(ble_evt_t const * p_ble_evt)
                                                    &target_uuid);
                 APP_ERROR_CHECK(err_code);
             }
-            
-            scan_start();
         } break; // BLE_GAP_EVT_DISCONNECTED
 
         case BLE_GAP_EVT_TIMEOUT:
@@ -693,8 +671,6 @@ static void on_adv_evt(ble_adv_evt_t ble_adv_evt)
 
         case BLE_ADV_EVT_IDLE:
         {
-            ret_code_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
-            APP_ERROR_CHECK(err_code);
         } break;
 
         default:
@@ -897,6 +873,21 @@ static void bsp_event_handler(bsp_event_t event)
             on_num_comp_button_press(false);
             break;
 
+      case BSP_EVENT_KEY_2:
+            // Start advertising.
+            err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
+            APP_ERROR_CHECK(err_code);
+
+            NRF_LOG_INFO("Advertising");
+            break;
+
+      case BSP_EVENT_KEY_3:
+            scan_start();
+
+            // Turn on the LED to signal scanning.
+            bsp_board_led_on(CENTRAL_SCANNING_LED);
+            break;
+
         default:
             break;
     }
@@ -1161,6 +1152,7 @@ int main(void)
     // Start execution.
     NRF_LOG_INFO("LE Secure Connections example started.");
 
+    erase_bonds = true;
     if (erase_bonds == true)
     {
         delete_bonds();
@@ -1168,7 +1160,6 @@ int main(void)
     }
     else
     {
-        adv_scan_start();
     }
 
     // Enter main loop.

After bonding, it used to work on SDK15.2.0 such that pressing Button 1 on the peripheral side would print 

<info> app: CENTRAL: Heart Rate = 87
on the central side.

But on SDK17.1.0 it does not print anything! What other changes do I need to make it work on the latest SDK?

Parents
  • Hi,

    I have narrowed down the problem. The issue is that nrf_ble_gq module does not handle BLE_GATT_STATUS_ATTERR_INSUF_ENCRYPTION response from the peer when your device writes to its CCCD (with MITM security flag) before the security procedure is complete. In older SDK this was handled differently where tx_buffer_process  in ble_hrs_c.c does not delete the item from the transmit queue until it got a success response from the peer for the write request.

    The solution is to add this below in ble_hrs_c.c

    static void on_write_rsp(ble_hrs_c_t * p_ble_hrs_c, const ble_evt_t * p_ble_evt)
    {
        // Check if the event if on the link for this instance
        if (p_ble_hrs_c->conn_handle != p_ble_evt->evt.gattc_evt.conn_handle)
        {
            return;
        }
    
        if ((p_ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_ATTERR_INSUF_AUTHENTICATION) ||
            (p_ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_ATTERR_INSUF_ENCRYPTION))
        {
             // write req rejected most likely due to AUTH setup delays, retry until AUTH setup is complete
             ble_hrs_c_hrm_notif_enable(p_ble_hrs_c);
    
        }
    }

    and handle the write response from the peer in the ble_hrs_c_on_ble_evt as below

            case BLE_GATTC_EVT_WRITE_RSP:
                on_write_rsp(p_ble_hrs_c, p_ble_evt);
                break;

Reply
  • Hi,

    I have narrowed down the problem. The issue is that nrf_ble_gq module does not handle BLE_GATT_STATUS_ATTERR_INSUF_ENCRYPTION response from the peer when your device writes to its CCCD (with MITM security flag) before the security procedure is complete. In older SDK this was handled differently where tx_buffer_process  in ble_hrs_c.c does not delete the item from the transmit queue until it got a success response from the peer for the write request.

    The solution is to add this below in ble_hrs_c.c

    static void on_write_rsp(ble_hrs_c_t * p_ble_hrs_c, const ble_evt_t * p_ble_evt)
    {
        // Check if the event if on the link for this instance
        if (p_ble_hrs_c->conn_handle != p_ble_evt->evt.gattc_evt.conn_handle)
        {
            return;
        }
    
        if ((p_ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_ATTERR_INSUF_AUTHENTICATION) ||
            (p_ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_ATTERR_INSUF_ENCRYPTION))
        {
             // write req rejected most likely due to AUTH setup delays, retry until AUTH setup is complete
             ble_hrs_c_hrm_notif_enable(p_ble_hrs_c);
    
        }
    }

    and handle the write response from the peer in the ble_hrs_c_on_ble_evt as below

            case BLE_GATTC_EVT_WRITE_RSP:
                on_write_rsp(p_ble_hrs_c, p_ble_evt);
                break;

Children
No Data
Related