uint32_t conn_mw_ble_gap_auth_key_reply(uint8_t const * const p_rx_buf, uint32_t rx_buf_len, uint8_t * const p_tx_buf, uint32_t * const p_tx_buf_len) { SER_ASSERT_NOT_NULL(p_rx_buf); SER_ASSERT_NOT_NULL(p_tx_buf); SER_ASSERT_NOT_NULL(p_tx_buf_len); uint32_t err_code = NRF_SUCCESS; uint32_t sd_err_code; uint16_t conn_handle; uint8_t key_type; uint8_t key[BLE_GAP_SEC_KEY_LEN]; uint8_t * p_key = key; err_code = ble_gap_auth_key_reply_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &key_type, &p_key); SER_ASSERT(err_code == NRF_SUCCESS, err_code); sd_err_code = sd_ble_gap_auth_key_reply(conn_handle, key_type, p_key); err_code = ble_gap_auth_key_reply_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len); SER_ASSERT(err_code == NRF_SUCCESS, err_code); return err_code; }
/**@brief Function for handling BLE Stack events common to both the central and peripheral roles. * @param[in] conn_handle Connection Handle. * @param[in] p_ble_evt Bluetooth stack event. */ static void on_ble_evt(uint16_t conn_handle, ble_evt_t * p_ble_evt) { ret_code_t err_code; uint8_t passkey[BLE_GAP_PASSKEY_LEN + 1]; switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_SEC_PARAMS_REQUEST: NRF_LOG_PRINTF("BLE_GAP_EVT_SEC_PARAMS_REQUEST\n"); break; case BLE_GAP_EVT_PASSKEY_DISPLAY: memcpy(passkey, p_ble_evt->evt.gap_evt.params.passkey_display.passkey, BLE_GAP_PASSKEY_LEN); passkey[BLE_GAP_PASSKEY_LEN] = 0x00; NRF_LOG_PRINTF("BLE_GAP_EVT_PASSKEY_DISPLAY: passkey=%s match_req=%d\n", passkey, p_ble_evt->evt.gap_evt.params.passkey_display.match_request); err_code = sd_ble_gap_auth_key_reply(conn_handle, BLE_GAP_AUTH_KEY_TYPE_PASSKEY, NULL); APP_ERROR_CHECK(err_code); /* Due to DRGN-7235, dhkey_reply() must come after auth_key_reply() */ APP_ERROR_CHECK_BOOL(m_dhkey_req); err_code = sd_ble_gap_lesc_dhkey_reply(conn_handle, &m_lesc_dhkey); APP_ERROR_CHECK(err_code); m_dhkey_req = 0; break; case BLE_GAP_EVT_AUTH_KEY_REQUEST: NRF_LOG_PRINTF("BLE_GAP_EVT_AUTH_KEY_REQUEST\n"); break; case BLE_GAP_EVT_LESC_DHKEY_REQUEST: NRF_LOG_PRINTF("BLE_GAP_EVT_LESC_DHKEY_REQUEST\n"); err_code = ecc_p256_shared_secret_compute(&m_lesc_sk.sk[0], &p_ble_evt->evt.gap_evt.params.lesc_dhkey_request.p_pk_peer->pk[0], &m_lesc_dhkey.key[0]); APP_ERROR_CHECK(err_code); m_dhkey_req = 1; break; case BLE_GAP_EVT_AUTH_STATUS: NRF_LOG_PRINTF("BLE_GAP_EVT_AUTH_STATUS: status=0x%x bond=0x%x lv4: %d kdist_own:0x%x kdist_peer:0x%x\n", p_ble_evt->evt.gap_evt.params.auth_status.auth_status, p_ble_evt->evt.gap_evt.params.auth_status.bonded, p_ble_evt->evt.gap_evt.params.auth_status.sm1_levels.lv4, *((uint8_t *)&p_ble_evt->evt.gap_evt.params.auth_status.kdist_own), *((uint8_t *)&p_ble_evt->evt.gap_evt.params.auth_status.kdist_peer)); break; default: // No implementation needed. break; } }
/**@brief Function for handling the Application's BLE Stack events. * * @param[in] p_ble_evt Bluetooth stack event. */ static void on_ble_evt(ble_evt_t * p_ble_evt) { uint32_t err_code; switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: NRF_LOG_INFO("Connected\r\n"); err_code = bsp_indication_set(BSP_INDICATE_CONNECTED); APP_ERROR_CHECK(err_code); m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle; /* Disable advertising, so when disconnected it does not restart. */ advertising_disable(); break; // BLE_GAP_EVT_CONNECTED case BLE_GAP_EVT_DISCONNECTED: NRF_LOG_INFO("Disonnected\r\n"); m_conn_handle = BLE_CONN_HANDLE_INVALID; m_advertising_flag = 0; if (m_is_wl_changed) { // The whitelist has been modified, update it in the Peer Manager. err_code = pm_whitelist_set(m_whitelist_peers, m_whitelist_peer_cnt); APP_ERROR_CHECK(err_code); err_code = pm_device_identities_list_set(m_whitelist_peers, m_whitelist_peer_cnt); if (err_code != NRF_ERROR_NOT_SUPPORTED) { APP_ERROR_CHECK(err_code); } m_is_wl_changed = false; } break; // BLE_GAP_EVT_DISCONNECTED case BLE_GAP_EVT_AUTH_KEY_REQUEST: err_code = sd_ble_gap_auth_key_reply(p_ble_evt->evt.gap_evt.conn_handle, BLE_GAP_AUTH_KEY_TYPE_OOB, m_oob_auth_key.tk); APP_ERROR_CHECK(err_code); break; // BLE_GAP_EVT_AUTH_KEY_REQUEST case BLE_GAP_EVT_AUTH_STATUS: { if (p_ble_evt->evt.gap_evt.params.auth_status.auth_status == BLE_GAP_SEC_STATUS_SUCCESS) { /* Configure NFC launchapp data (requires stopping NFC tag emulation) */ err_code = nfc_t2t_emulation_stop(); APP_ERROR_CHECK(err_code); nfc_launchapp_data_set(); err_code = nfc_t2t_emulation_start(); APP_ERROR_CHECK(err_code); } }break; // BLE_GAP_EVT_AUTH_STATUS case BLE_GATTC_EVT_TIMEOUT: // Disconnect on GATT Client timeout event. NRF_LOG_DEBUG("GATT Client Timeout.\r\n"); 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; // BLE_GATTC_EVT_TIMEOUT case BLE_GATTS_EVT_TIMEOUT: // Disconnect on GATT Server timeout event. NRF_LOG_DEBUG("GATT Server Timeout.\r\n"); 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; // BLE_GATTS_EVT_TIMEOUT case BLE_EVT_USER_MEM_REQUEST: err_code = sd_ble_user_mem_reply(m_conn_handle, NULL); APP_ERROR_CHECK(err_code); break; // BLE_EVT_USER_MEM_REQUEST case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST: { ble_gatts_evt_rw_authorize_request_t req; ble_gatts_rw_authorize_reply_params_t auth_reply; req = p_ble_evt->evt.gatts_evt.params.authorize_request; if (req.type != BLE_GATTS_AUTHORIZE_TYPE_INVALID) { if ((req.request.write.op == BLE_GATTS_OP_PREP_WRITE_REQ) || (req.request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_NOW) || (req.request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL)) { if (req.type == BLE_GATTS_AUTHORIZE_TYPE_WRITE) { auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE; } else { auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_READ; } auth_reply.params.write.gatt_status = APP_FEATURE_NOT_SUPPORTED; err_code = sd_ble_gatts_rw_authorize_reply(p_ble_evt->evt.gatts_evt.conn_handle, &auth_reply); APP_ERROR_CHECK(err_code); } } } break; // BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST #if (NRF_SD_BLE_API_VERSION == 3) case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST: err_code = sd_ble_gatts_exchange_mtu_reply(p_ble_evt->evt.gatts_evt.conn_handle, NRF_BLE_MAX_MTU_SIZE); APP_ERROR_CHECK(err_code); break; // BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST #endif default: // No implementation needed. break; } }