I am trying to add static passkey pairing to ble_app_uart in SDK_11 s130. To do this, I first implemented static passkey successfully in ble_app_gls using sd_ble_opt_set(). Then, I tried to port peer manager from ble_app_gls to ble_app_uart by copying and pasting the relevant code from ble_app_gls to ble_app_uart. The code compiles but using Android app nRF Uart v2, the app can connect without being prompted for passkey.
I have been struggling with this problem for some time as there is no example for static pairing ble_app_uart for SDK_11. The github example is for SDK_7.
Here are the relevant code that I added to main.c of ble_app_uart. Please tell me where I could have gone wrong. What are some tips for debugging?
Sorry for the long code.
#define SECURITY_REQUEST_DELAY APP_TIMER_TICKS(400, APP_TIMER_PRESCALER) /**< Delay after connection until Security Request is sent, if necessary (ticks). */
#define SEC_PARAM_BOND 1 /**< Perform bonding. */
#define SEC_PARAM_MITM 1 /**< Man In The Middle protection not required. */
#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 /**< 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. */
APP_TIMER_DEF(m_sec_req_timer_id); /**< Security Request timer. */
pm_peer_id_t peer_to_be_deleted = PM_PEER_ID_INVALID;
static void advertising_start(void);
/**@brief Function for handling the Security Request timer timeout.
*
* @details This function will be called each time the Security Request timer expires.
*
* @param[in] p_context Pointer used for passing some arbitrary information (context) from the
* app_start_timer() call to the timeout handler.
*/
static void sec_req_timeout_handler(void * p_context)
{
uint32_t err_code;
if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
{
// Initiate bonding.
NRF_LOG_DEBUG("Start encryption\r\n");
err_code = pm_conn_secure(m_conn_handle, false);
if (err_code != NRF_ERROR_INVALID_STATE)
{
APP_ERROR_CHECK(err_code);
}
}
}
static void ble_evt_dispatch(ble_evt_t * p_ble_evt)
{
#ifdef ADD_FOR_PASSKEY
ble_conn_state_on_ble_evt(p_ble_evt);
pm_on_ble_evt(p_ble_evt);
#endif
ble_conn_params_on_ble_evt(p_ble_evt);
ble_nus_on_ble_evt(&m_nus, p_ble_evt);
on_ble_evt(p_ble_evt);
ble_advertising_on_ble_evt(p_ble_evt);
bsp_btn_ble_on_ble_evt(p_ble_evt);
}
/**@brief Function for dispatching a system event to interested modules.
*
* @details This function is called from the System event interrupt handler after a system
* event has been received.
*
* @param[in] sys_evt System stack event.
*/
static void sys_evt_dispatch(uint32_t sys_evt)
{
fs_sys_event_handler(sys_evt);
ble_advertising_on_sys_evt(sys_evt);
}
static void ble_stack_init(void)
{
uint32_t err_code;
nrf_clock_lf_cfg_t clock_lf_cfg = NRF_CLOCK_LFCLKSRC;
// Initialize SoftDevice.
SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL);
ble_enable_params_t ble_enable_params;
err_code = softdevice_enable_get_default_config(CENTRAL_LINK_COUNT,
PERIPHERAL_LINK_COUNT,
&ble_enable_params);
APP_ERROR_CHECK(err_code);
//Check the ram settings against the used number of links
CHECK_RAM_START_ADDR(CENTRAL_LINK_COUNT,PERIPHERAL_LINK_COUNT);
// Enable BLE stack.
err_code = softdevice_enable(&ble_enable_params);
APP_ERROR_CHECK(err_code);
// Subscribe for BLE events.
err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
APP_ERROR_CHECK(err_code);
#ifdef ADD_FOR_PASSKEY
// Register with the SoftDevice handler module for BLE events.
err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch);
APP_ERROR_CHECK(err_code);
#endif
}
/**@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)
{
ret_code_t err_code;
switch(p_evt->evt_id)
{
case PM_EVT_BONDED_PEER_CONNECTED:
{
NRF_LOG_PRINTF_DEBUG("Connected to previously bonded device\r\n");
// Start Security Request timer.
err_code = app_timer_start(m_sec_req_timer_id, SECURITY_REQUEST_DELAY, NULL);
APP_ERROR_CHECK(err_code);
err_code = pm_peer_rank_highest(p_evt->peer_id);
if (err_code != NRF_ERROR_BUSY)
{
APP_ERROR_CHECK(err_code);
}
}break;//PM_EVT_BONDED_PEER_CONNECTED
case PM_EVT_CONN_SEC_START:
break;//PM_EVT_CONN_SEC_START
case PM_EVT_CONN_SEC_SUCCEEDED:
{
/*Check if the link is authenticated (meaning at least MITM)*/
pm_conn_sec_status_t conn_sec_status;
err_code = pm_conn_sec_status_get(p_evt->conn_handle, &conn_sec_status);
APP_ERROR_CHECK(err_code);
if (!conn_sec_status.mitm_protected)
{
APP_LOG("Collector did not use MITM, disconnecting\r\n");
/*The peer did not use MITM, disconnect*/
err_code = pm_peer_id_get(m_conn_handle, &peer_to_be_deleted);
APP_ERROR_CHECK(err_code);
err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
APP_ERROR_CHECK(err_code);
}
else
{
NRF_LOG_PRINTF_DEBUG("Link secured. Role: %d. conn_handle: %d, Procedure: %d\r\n",
ble_conn_state_role(p_evt->conn_handle),
p_evt->conn_handle,
p_evt->params.conn_sec_succeeded.procedure);
err_code = pm_peer_rank_highest(p_evt->peer_id);
if (err_code != NRF_ERROR_BUSY)
{
APP_ERROR_CHECK(err_code);
}
}
} break;//PM_EVT_CONN_SEC_SUCCEEDED
case PM_EVT_CONN_SEC_FAILED:
{
/** In some cases, when securing fails, it can be restarted directly. Sometimes it can
* be restarted, but only after changing some Security Parameters. Sometimes, it cannot
* be restarted until the link is disconnected and reconnected. Sometimes it is
* impossible, to secure the link, or the peer device does not support it. How to
* handle this error is highly application dependent. */
APP_LOG("link secure failed! ");
switch (p_evt->params.conn_sec_failed.error)
{
case PM_CONN_SEC_ERROR_PIN_OR_KEY_MISSING:
NRF_LOG_DEBUG("error: PM_CONN_SEC_ERROR_PIN_OR_KEY_MISSING");
break;//PM_CONN_SEC_ERROR_PIN_OR_KEY_MISSING
case PM_CONN_SEC_ERROR_MIC_FAILURE:
NRF_LOG_DEBUG("error: PM_CONN_SEC_ERROR_MIC_FAILURE");
break;//PM_CONN_SEC_ERROR_MIC_FAILURE
case PM_CONN_SEC_ERROR_DISCONNECT :
NRF_LOG_DEBUG("error: PM_CONN_SEC_ERROR_DISCONNECT ");
break;//PM_CONN_SEC_ERROR_DISCONNECT
case PM_CONN_SEC_ERROR_SMP_TIMEOUT:
NRF_LOG_DEBUG("error: PM_CONN_SEC_ERROR_SMP_TIMEOUT");
break;//PM_CONN_SEC_ERROR_SMP_TIMEOUT
default:
NRF_LOG_DEBUG("unknown error");
break;
}
APP_LOG("\r\nDisconnecting\r\n");
err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
APP_ERROR_CHECK(err_code);
m_conn_handle = BLE_CONN_HANDLE_INVALID;
}break;//PM_EVT_CONN_SEC_FAILED
case PM_EVT_CONN_SEC_CONFIG_REQ:
{
// Reject pairing request from an already bonded peer.
pm_conn_sec_config_t conn_sec_config = {.allow_repairing = false};
pm_conn_sec_config_reply(p_evt->conn_handle, &conn_sec_config);
}break;//PM_EVT_CONN_SEC_CONFIG_REQ
case PM_EVT_STORAGE_FULL:
{
// Run garbage collection on the flash.
err_code = fds_gc();
if (err_code == FDS_ERR_BUSY || err_code == FDS_ERR_NO_SPACE_IN_QUEUES)
{
// Retry.
}
else
{
APP_ERROR_CHECK(err_code);
}
}break;//PM_EVT_STORAGE_FULL
case PM_EVT_ERROR_UNEXPECTED:
// Assert.
APP_ERROR_CHECK(p_evt->params.error_unexpected.error);
break;//PM_EVT_ERROR_UNEXPECTED
case PM_EVT_PEER_DATA_UPDATE_SUCCEEDED:
break;//PM_EVT_PEER_DATA_UPDATE_SUCCEEDED
case PM_EVT_PEER_DATA_UPDATE_FAILED:
// Assert.
APP_ERROR_CHECK_BOOL(false);
break;//PM_EVT_PEER_DATA_UPDATE_FAILED
case PM_EVT_PEER_DELETE_SUCCEEDED:
break;//PM_EVT_PEER_DELETE_SUCCEEDED
case PM_EVT_PEER_DELETE_FAILED:
// Assert.
APP_ERROR_CHECK(p_evt->params.peer_delete_failed.error);
break;//PM_EVT_PEER_DELETE_FAILED
case PM_EVT_PEERS_DELETE_SUCCEEDED:
advertising_start();
break;
case PM_EVT_PEERS_DELETE_FAILED:
// Assert.
APP_ERROR_CHECK(p_evt->params.peers_delete_failed_evt.error);
break;//PM_EVT_PEERS_DELETE_FAILED
case PM_EVT_LOCAL_DB_CACHE_APPLIED:
break;//PM_EVT_LOCAL_DB_CACHE_APPLIED
case PM_EVT_LOCAL_DB_CACHE_APPLY_FAILED:
// The local database has likely changed, send service changed indications.
pm_local_database_has_changed();
break;//PM_EVT_LOCAL_DB_CACHE_APPLY_FAILED
case PM_EVT_SERVICE_CHANGED_IND_SENT:
break;//PM_EVT_SERVICE_CHANGED_IND_SENT
case PM_EVT_SERVICE_CHANGED_IND_CONFIRMED:
break;//PM_EVT_SERVICE_CHANGED_IND_CONFIRMED
default:
// No implementation needed.
break;
}
}
/**@brief Function for the Timer initialization.
*
* @details Initializes the timer module. This creates and starts application timers.
*/
static void timers_init(void)
{
uint32_t err_code;
// Initialize timer module.
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
// Create timers.
// Create Security Request timer.
err_code = app_timer_create(&m_sec_req_timer_id,
APP_TIMER_MODE_SINGLE_SHOT,
sec_req_timeout_handler);
APP_ERROR_CHECK(err_code);
}
/**@brief Function for the Peer Manager initialization.
*
* @param[in] erase_bonds Indicates whether bonding information should be cleared from
* persistent storage during initialization of the Peer Manager.
*/
static void peer_manager_init(bool erase_bonds)
{
ble_gap_sec_params_t sec_param;
ret_code_t err_code;
err_code = pm_init();
APP_ERROR_CHECK(err_code);
if (erase_bonds)
{
err_code = pm_peers_delete();
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);
}
/**@brief Function for starting advertising.
*/
static void advertising_start(void)
{
uint32_t err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
APP_ERROR_CHECK(err_code);
}
int main(void)
{
uint32_t err_code;
bool erase_bonds;
// Initialize.
//APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
timers_init();
uart_init();
buttons_leds_init(&erase_bonds);
ble_stack_init();
#ifdef ADD_FOR_PASSKEY
peer_manager_init(erase_bonds);
#endif
gap_params_init();
services_init();
advertising_init();
conn_params_init();
printf("\r\nUART Start!\r\n");
err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
APP_ERROR_CHECK(err_code);
// Enter main loop.
for (;;)
{
power_manage();
}
}