This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Connecting a peripheral to specific device or app.

Hi,

I have a nrf52810 board working as a peripheral, I want my board to connect to specific central application to protect it with malicious attack by other devices.

Please suggest how t proceed.

 Board >> NRF52810

Sample >> BLE_app_uart

Mode >> Peripheral

Thanks 

Raj

Parents
  • Hi,

    Since you have a peripheral, you need to advertise and wait for the peer to connect to you. I would recommend that you have two (or more) advertisement modes. One that allows all connection request, in case you do not have a bond, or in case you want to create a new bond. Then a second mode, that you use once the device has bonded where you enable the whitelist function to only allow connection requests from known devices.

    In addition to this you need to configure the security requirements correctly, both for bonding and for the characteristic. Depending on your application you might be fine with using just works bonding if you only allow bonds after user interaction, if not you should consider adding a passkey or using OOB bonding.

    Finaly, if you are new to our product and bluetooth low energy you might want to take a look at our tutorials section, here: https://devzone.nordicsemi.com/nordic/short-range-guides/b/bluetooth-low-energy

  • Hi run_ar,

    i just want to adding a passkey in my Nordic peripheral so that only the device(central) who knows exact passkey of my peripheral can initiate a connection.

    Thanks

    Raj

  • Hi run_ar,

    Thanks for the response,

    I follow the steps suggested in the link provided by you but still i am not able to implement the Passkey in my code.

    I am using SDK17

    Sample is BLE_APP_UART

    Controller NRF52810

    Thanks 

    Raj

  • Hi run_ar,

    I implemented the paring and bonding concept in my code, My nrf52810 board is advertising as advertising led is blinking but the problem here is  I am not getting my device name in the mobile scanning list.

    Here is the implementation I followed

    ///////////Added for bonding////////////
    #include "peer_manager.h"
    #include "peer_manager_handler.h"
    
    #define SEC_PARAM_BOND 1 /**< Perform bonding. */
    #define SEC_PARAM_MITM 1 /**< Man In The Middle protection is enabled. */
    #define SEC_PARAM_LESC 0 /**< LE Secure Connections not enabled. */
    #define SEC_PARAM_KEYPRESS 0 /**< Keypress notifications not enabled. */
    #define SEC_PARAM_IO_CAPABILITIES BLE_GAP_IO_CAPS_DISPLAY_ONLY /**< Display Only. */
    #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. */
    ///////////Added for bonding////////////
    
    
    ////////////Added for bonding///////////
    // Static passkey
    #define STATIC_PASSKEY "123456"
    static ble_opt_t m_static_pin_option;
    uint8_t passkey[] = STATIC_PASSKEY;
    ////////////Added for bonding///////////
    
    
    ////////////Added for bonding///////////
    static void advertising_start(bool erase_bonds);
    ////////////Added for bonding///////////
    
    ///////////Added for bonding/////////////
    /**@brief Function for handling Peer Manager events.
    *
    * @param[in] p_evt Peer Manager event.
    */
    static void pm_evt_handler(pm_evt_t const * p_evt)
    {
    pm_handler_on_pm_evt(p_evt);
    pm_handler_flash_clean(p_evt);
    
    switch (p_evt->evt_id)
    {
    case PM_EVT_PEERS_DELETE_SUCCEEDED:
    advertising_start(false);
    break;
    
    default:
    break;
    }
    }
    /**@brief Function for the Peer Manager initialization.
    */
    static void peer_manager_init(void)
    {
    ret_code_t err_code;
    
    err_code = pm_init();
    APP_ERROR_CHECK(err_code);
    
    err_code = pm_register(pm_evt_handler);
    APP_ERROR_CHECK(err_code);
    
    ble_gap_sec_params_t sec_param;
    
    memset(&sec_param, 0, sizeof(ble_gap_sec_params_t));
    
    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);
    }
    /**@brief Clear bond information from persistent storage.
    */
    static void delete_bonds(void)
    {
    ret_code_t err_code;
    
    NRF_LOG_INFO("Erase bonds");
    
    err_code = pm_peers_delete();
    APP_ERROR_CHECK(err_code);
    }
    
    
    /**@brief Function for the GAP initialization.
    *
    * @details This function will set up all the necessary GAP (Generic Access Profile) parameters of
    * the device. It also sets the permissions and appearance.
    */
    /**@brief Function for the GAP initialization.
    *
    * @details This function will set up all the necessary GAP (Generic Access Profile) parameters of
    * the device. It also sets the permissions and appearance.
    */
    static void gap_params_init(void)
    {
    uint32_t err_code;
    ble_gap_conn_params_t gap_conn_params;
    ble_gap_conn_sec_mode_t sec_mode;
    
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
    
    err_code = sd_ble_gap_device_name_set(&sec_mode,
    (const uint8_t *) DEVICE_NAME,
    strlen(DEVICE_NAME));
    APP_ERROR_CHECK(err_code);
    
    memset(&gap_conn_params, 0, sizeof(gap_conn_params));
    
    gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
    gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
    gap_conn_params.slave_latency = SLAVE_LATENCY;
    gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;
    
    err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
    APP_ERROR_CHECK(err_code);
    
    ////////Added for bonding ///////////
    m_static_pin_option.gap_opt.passkey.p_passkey = &passkey[0];
    err_code = sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &m_static_pin_option);
    APP_ERROR_CHECK(err_code);
    ////////Added for bonding ///////////
    }
    
    /**@brief Function for handling BLE events.
    *
    * @param[in] p_ble_evt Bluetooth stack event.
    * @param[in] p_context Unused.
    */
    static void ble_evt_handler(ble_evt_t const *p_ble_evt, void *p_context) {
    uint32_t err_code;
    
    switch (p_ble_evt->header.evt_id) {
    case BLE_GAP_EVT_CONNECTED:
    NRF_LOG_INFO("Connected");
    err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
    APP_ERROR_CHECK(err_code);
    m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
    err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
    APP_ERROR_CHECK(err_code);
    // start_timer();
    break;
    
    case BLE_GAP_EVT_DISCONNECTED:
    NRF_LOG_INFO("Disconnected");
    // LED indication will be changed when advertising starts.
    m_conn_handle = BLE_CONN_HANDLE_INVALID;
    break;
    
    case BLE_GAP_EVT_PHY_UPDATE_REQUEST: {
    NRF_LOG_DEBUG("PHY update request.");
    ble_gap_phys_t const phys =
    {
    .rx_phys = BLE_GAP_PHY_AUTO,
    .tx_phys = BLE_GAP_PHY_AUTO,
    };
    err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
    APP_ERROR_CHECK(err_code);
    } break;
    
    /*case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
    // Pairing not supported
    err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
    APP_ERROR_CHECK(err_code);
    break;*/
    
    /*case BLE_GATTS_EVT_SYS_ATTR_MISSING:
    // No system attributes have been stored.
    err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
    APP_ERROR_CHECK(err_code);
    break;*/
    
    case BLE_GATTC_EVT_TIMEOUT:
    // Disconnect on GATT Client timeout event.
    err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
    BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
    APP_ERROR_CHECK(err_code);
    break;
    
    case BLE_GATTS_EVT_TIMEOUT:
    // Disconnect on GATT Server timeout event.
    err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
    BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
    APP_ERROR_CHECK(err_code);
    break;
    
    default:
    // No implementation needed.
    break;
    }
    }
    
    /**@brief Function for starting advertising.
    */
    /*static void advertising_start(void) {
    uint32_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);
    }*/
    ////////////Added for bonding//////////////
    /**@brief Function for starting advertising.
    */
    static void advertising_start(bool erase_bonds)
    {
    if (erase_bonds == true)
    {
    delete_bonds();
    // Advertising is started by PM_EVT_PEERS_DELETE_SUCCEEDED event.
    }
    else
    {
    ret_code_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
    
    APP_ERROR_CHECK(err_code);
    }
    }
    ////////////Added for bonding//////////////
    
    
    /**@brief Application main function.
    */
    int main(void) {
    
    bool erase_bonds;
    ret_code_t rc;
    uart_init();
    leds_init();
    log_init();
    timers_init();
    buttons_leds_init(&erase_bonds);
    power_management_init();
    ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();
    /////////Added for bonding//////////
    peer_manager_init();
    /////////Added for bonding//////////
    
    advertising_start(erase_bonds);
    
    }

    Can you pleasse suggest where i am doing wrong?

    Thanks

    Raj.

Reply
  • Hi run_ar,

    I implemented the paring and bonding concept in my code, My nrf52810 board is advertising as advertising led is blinking but the problem here is  I am not getting my device name in the mobile scanning list.

    Here is the implementation I followed

    ///////////Added for bonding////////////
    #include "peer_manager.h"
    #include "peer_manager_handler.h"
    
    #define SEC_PARAM_BOND 1 /**< Perform bonding. */
    #define SEC_PARAM_MITM 1 /**< Man In The Middle protection is enabled. */
    #define SEC_PARAM_LESC 0 /**< LE Secure Connections not enabled. */
    #define SEC_PARAM_KEYPRESS 0 /**< Keypress notifications not enabled. */
    #define SEC_PARAM_IO_CAPABILITIES BLE_GAP_IO_CAPS_DISPLAY_ONLY /**< Display Only. */
    #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. */
    ///////////Added for bonding////////////
    
    
    ////////////Added for bonding///////////
    // Static passkey
    #define STATIC_PASSKEY "123456"
    static ble_opt_t m_static_pin_option;
    uint8_t passkey[] = STATIC_PASSKEY;
    ////////////Added for bonding///////////
    
    
    ////////////Added for bonding///////////
    static void advertising_start(bool erase_bonds);
    ////////////Added for bonding///////////
    
    ///////////Added for bonding/////////////
    /**@brief Function for handling Peer Manager events.
    *
    * @param[in] p_evt Peer Manager event.
    */
    static void pm_evt_handler(pm_evt_t const * p_evt)
    {
    pm_handler_on_pm_evt(p_evt);
    pm_handler_flash_clean(p_evt);
    
    switch (p_evt->evt_id)
    {
    case PM_EVT_PEERS_DELETE_SUCCEEDED:
    advertising_start(false);
    break;
    
    default:
    break;
    }
    }
    /**@brief Function for the Peer Manager initialization.
    */
    static void peer_manager_init(void)
    {
    ret_code_t err_code;
    
    err_code = pm_init();
    APP_ERROR_CHECK(err_code);
    
    err_code = pm_register(pm_evt_handler);
    APP_ERROR_CHECK(err_code);
    
    ble_gap_sec_params_t sec_param;
    
    memset(&sec_param, 0, sizeof(ble_gap_sec_params_t));
    
    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);
    }
    /**@brief Clear bond information from persistent storage.
    */
    static void delete_bonds(void)
    {
    ret_code_t err_code;
    
    NRF_LOG_INFO("Erase bonds");
    
    err_code = pm_peers_delete();
    APP_ERROR_CHECK(err_code);
    }
    
    
    /**@brief Function for the GAP initialization.
    *
    * @details This function will set up all the necessary GAP (Generic Access Profile) parameters of
    * the device. It also sets the permissions and appearance.
    */
    /**@brief Function for the GAP initialization.
    *
    * @details This function will set up all the necessary GAP (Generic Access Profile) parameters of
    * the device. It also sets the permissions and appearance.
    */
    static void gap_params_init(void)
    {
    uint32_t err_code;
    ble_gap_conn_params_t gap_conn_params;
    ble_gap_conn_sec_mode_t sec_mode;
    
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
    
    err_code = sd_ble_gap_device_name_set(&sec_mode,
    (const uint8_t *) DEVICE_NAME,
    strlen(DEVICE_NAME));
    APP_ERROR_CHECK(err_code);
    
    memset(&gap_conn_params, 0, sizeof(gap_conn_params));
    
    gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
    gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
    gap_conn_params.slave_latency = SLAVE_LATENCY;
    gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;
    
    err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
    APP_ERROR_CHECK(err_code);
    
    ////////Added for bonding ///////////
    m_static_pin_option.gap_opt.passkey.p_passkey = &passkey[0];
    err_code = sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &m_static_pin_option);
    APP_ERROR_CHECK(err_code);
    ////////Added for bonding ///////////
    }
    
    /**@brief Function for handling BLE events.
    *
    * @param[in] p_ble_evt Bluetooth stack event.
    * @param[in] p_context Unused.
    */
    static void ble_evt_handler(ble_evt_t const *p_ble_evt, void *p_context) {
    uint32_t err_code;
    
    switch (p_ble_evt->header.evt_id) {
    case BLE_GAP_EVT_CONNECTED:
    NRF_LOG_INFO("Connected");
    err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
    APP_ERROR_CHECK(err_code);
    m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
    err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
    APP_ERROR_CHECK(err_code);
    // start_timer();
    break;
    
    case BLE_GAP_EVT_DISCONNECTED:
    NRF_LOG_INFO("Disconnected");
    // LED indication will be changed when advertising starts.
    m_conn_handle = BLE_CONN_HANDLE_INVALID;
    break;
    
    case BLE_GAP_EVT_PHY_UPDATE_REQUEST: {
    NRF_LOG_DEBUG("PHY update request.");
    ble_gap_phys_t const phys =
    {
    .rx_phys = BLE_GAP_PHY_AUTO,
    .tx_phys = BLE_GAP_PHY_AUTO,
    };
    err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
    APP_ERROR_CHECK(err_code);
    } break;
    
    /*case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
    // Pairing not supported
    err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
    APP_ERROR_CHECK(err_code);
    break;*/
    
    /*case BLE_GATTS_EVT_SYS_ATTR_MISSING:
    // No system attributes have been stored.
    err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
    APP_ERROR_CHECK(err_code);
    break;*/
    
    case BLE_GATTC_EVT_TIMEOUT:
    // Disconnect on GATT Client timeout event.
    err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
    BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
    APP_ERROR_CHECK(err_code);
    break;
    
    case BLE_GATTS_EVT_TIMEOUT:
    // Disconnect on GATT Server timeout event.
    err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
    BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
    APP_ERROR_CHECK(err_code);
    break;
    
    default:
    // No implementation needed.
    break;
    }
    }
    
    /**@brief Function for starting advertising.
    */
    /*static void advertising_start(void) {
    uint32_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);
    }*/
    ////////////Added for bonding//////////////
    /**@brief Function for starting advertising.
    */
    static void advertising_start(bool erase_bonds)
    {
    if (erase_bonds == true)
    {
    delete_bonds();
    // Advertising is started by PM_EVT_PEERS_DELETE_SUCCEEDED event.
    }
    else
    {
    ret_code_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
    
    APP_ERROR_CHECK(err_code);
    }
    }
    ////////////Added for bonding//////////////
    
    
    /**@brief Application main function.
    */
    int main(void) {
    
    bool erase_bonds;
    ret_code_t rc;
    uart_init();
    leds_init();
    log_init();
    timers_init();
    buttons_leds_init(&erase_bonds);
    power_management_init();
    ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();
    /////////Added for bonding//////////
    peer_manager_init();
    /////////Added for bonding//////////
    
    advertising_start(erase_bonds);
    
    }

    Can you pleasse suggest where i am doing wrong?

    Thanks

    Raj.

Children
Related