How to get pairing/bonding to work in e.g. ble_app_uart

   Hello List,

First some context: I am working with SDK 17.1.0 on a nrf52840 (arduino nano ble).

Created a variant of the ble_app_uart example and want to add pairing/bonding with it. Started looking at ble_app_hrs to start.

Basically I added functions "peer_manager_init" and "pm_evt_handler" and some changes to "sdk_config.h", but now am stuck. Please see below for that code.

With this code it just works, but without pairing/bonding.

If I remove the "pm_conn_sec_params_reply" call from "pm_evt_handler" the smartphone pops up a window and asks whether there should be a pairing, which then fails as can be seen using the RTT-log:

<error> peer_manager_sm: Could not perform security procedure. smd_params_reply() or smd_link_secure() returned NRF_ERROR_INVALID_ADDR. conn_handle: 0
<error> peer_manager_handler: Unexpected fatal error occurred: error: NRF_ERROR_INVALID_ADDR
<error> peer_manager_handler: Asserting.
<error> app: Fatal error

One thing that is missing is setting the pin code in the program, how is that to be done?

Hopefully someone can help me in the right direction.

Thanks in advance, Sietse

Here the code snipped.

// peer_manager stuff in main.c
      
#include "peer_manager.h"
#include "peer_manager_handler.h"
#include "fds.h"

#define SEC_PARAM_BOND                      1                                       /**< Perform bonding. */
#define SEC_PARAM_MITM                      0                                       /**< Man In The Middle protection not required. */
#define SEC_PARAM_LESC                      1                                       /**< LE Secure Connections enabled. */
#define SEC_PARAM_KEYPRESS                  0                                       /**< Keypress notifications not enabled. */
#define SEC_PARAM_IO_CAPABILITIES           BLE_GAP_IO_CAPS_NONE                    /**< No I/O capabilities. */
#define SEC_PARAM_OOB                       0                                       /**< Out Of Band data not available. */
#define SEC_PARAM_MIN_KEY_SIZE              7                                       /**< Minimum encryption key size. */
#define SEC_PARAM_MAX_KEY_SIZE              16                                      /**< Maximum encryption key size. */


static void pm_evt_handler(pm_evt_t const * p_evt)
{
    pm_handler_on_pm_evt(p_evt);
    pm_handler_disconnect_on_sec_failure(p_evt);
    pm_handler_flash_clean(p_evt);

    switch (p_evt->evt_id)
      {
      case PM_EVT_PEERS_DELETE_SUCCEEDED:
	advertising_start();
	break;

      case  PM_EVT_CONN_SEC_SUCCEEDED:
	NRF_LOG_INFO("--> PM_EVT_CONN_SEC_SUCCEEDED");
	break;
      case  PM_EVT_CONN_SEC_PARAMS_REQ:
	NRF_LOG_INFO("--> PM_EVT_CONN_SEC_PARAMS_REQ");
	pm_conn_sec_params_reply(p_evt->conn_handle,
				 p_evt->params.conn_sec_params_req.p_peer_params,
				 p_evt->params.conn_sec_params_req.p_context);
	break;
      default:
	NRF_LOG_INFO("--> pm_evt_handler %d", p_evt->evt_id);
	break;
    }
}



/**@brief Function for the Peer Manager initialization.
 */

static void peer_manager_init(void)
{
    ble_gap_sec_params_t sec_param;
    ret_code_t           err_code;

    err_code = pm_init();
    APP_ERROR_CHECK(err_code);

    memset(&sec_param, 0, sizeof(ble_gap_sec_params_t));

    // Security parameters to be used for all security procedures.
    sec_param.bond           = SEC_PARAM_BOND;
    sec_param.mitm           = SEC_PARAM_MITM;
    sec_param.lesc           = SEC_PARAM_LESC;
    sec_param.keypress       = SEC_PARAM_KEYPRESS;
    sec_param.io_caps        = SEC_PARAM_IO_CAPABILITIES;
    sec_param.oob            = SEC_PARAM_OOB;
    sec_param.min_key_size   = SEC_PARAM_MIN_KEY_SIZE;
    sec_param.max_key_size   = SEC_PARAM_MAX_KEY_SIZE;
    sec_param.kdist_own.enc  = 1;
    sec_param.kdist_own.id   = 1;
    sec_param.kdist_peer.enc = 1;
    sec_param.kdist_peer.id  = 1;

    err_code = pm_sec_params_set(&sec_param);
    APP_ERROR_CHECK(err_code);

    err_code = pm_register(pm_evt_handler);
    APP_ERROR_CHECK(err_code);
}


Parents Reply Children
No Data
Related