/**@brief This function handles the authentication status event related to a new master. * * @details This function adds the new master to the database and stores the master's Bonding * Information to flash. It also notifies the application when the new bond is created, * and sets the System Attributes to prepare the stack for connection with the new * master. * * @param[in] p_auth_status New authentication status. * * @return NRF_SUCCESS on success, otherwise an error code. */ static uint32_t on_auth_status_from_new_master(ble_gap_evt_auth_status_t * p_auth_status) { uint32_t err_code; if (m_masters_in_db_count >= BLE_BONDMNGR_MAX_BONDED_MASTERS) { return NRF_ERROR_NO_MEM; } // Update master m_master.bond.auth_status = *p_auth_status; m_master.bond.master_id_info.div = p_auth_status->periph_keys.enc_info.div; m_master.sys_attr.sys_attr_size = 0; // Add new master to database m_master.bond.master_handle = m_masters_in_db_count; m_masters_db[m_masters_in_db_count++] = m_master; update_whitelist(); // Clear System Attributes err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0); if (err_code != NRF_SUCCESS) { return err_code; } // Write new master's Bonding Information to flash err_code = bond_info_store(&m_master.bond); if ((err_code == NRF_ERROR_NO_MEM) && (m_bondmngr_config.evt_handler != NULL)) { ble_bondmngr_evt_t evt; evt.evt_type = BLE_BONDMNGR_EVT_BOND_FLASH_FULL; evt.master_handle = m_master.bond.master_handle; evt.master_id = m_master.bond.master_id_info.div; m_bondmngr_config.evt_handler(&evt); } else if (err_code != NRF_SUCCESS) { return err_code; } // Pass event to application if (m_bondmngr_config.evt_handler != NULL) { ble_bondmngr_evt_t evt; evt.evt_type = BLE_BONDMNGR_EVT_NEW_BOND; evt.master_handle = m_master.bond.master_handle; evt.master_id = m_master.bond.master_id_info.div; m_bondmngr_config.evt_handler(&evt); } return NRF_SUCCESS; }
/**@brief Function for handling the Connection Security Update event received from the BLE * stack. * * @param[in] p_ble_evt Event received from the BLE stack. */ static void on_sec_update(ble_gap_evt_conn_sec_update_t * p_sec_update) { uint8_t security_mode = p_sec_update->conn_sec.sec_mode.sm; uint8_t security_level = p_sec_update->conn_sec.sec_mode.lv; if (((security_mode == 1) && (security_level > 1)) || ((security_mode == 2) && (security_level != 0))) { ENCRYPTION_STATUS_SET(); uint32_t err_code = master_sys_attr_set(&m_master); if (err_code != NRF_SUCCESS) { m_bondmngr_config.error_handler(err_code); } else { m_sys_attr_loaded = true; } if (m_bondmngr_config.evt_handler != NULL) { ble_bondmngr_evt_t evt; evt.evt_type = BLE_BONDMNGR_EVT_ENCRYPTED; evt.master_handle = m_master.bond.master_handle; evt.master_id = m_master.bond.master_id_info.div; m_bondmngr_config.evt_handler(&evt); } } }
/**@brief This function handles the connected event received from the BLE stack. * * @param[in] p_ble_evt Event received from the BLE stack. */ static void on_connect(ble_evt_t * p_ble_evt) { m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle; m_master.bond.master_handle = INVALID_MASTER_HANDLE; m_master.bond.master_addr = p_ble_evt->evt.gap_evt.params.connected.peer_addr; m_master.sys_attr.sys_attr_size = 0; if (p_ble_evt->evt.gap_evt.params.connected.irk_match) { uint8_t irk_idx = p_ble_evt->evt.gap_evt.params.connected.irk_match_idx; if ((irk_idx >= MAX_NUM_MASTER_WHITE_LIST) || (m_whitelist_irk[irk_idx].master_handle >= BLE_BONDMNGR_MAX_BONDED_MASTERS)) { m_bondmngr_config.error_handler(NRF_ERROR_INTERNAL); } else { m_master = m_masters_db[m_whitelist_irk[irk_idx].master_handle]; } } else { int i; for (i = 0; i < m_addr_count; i++) { ble_gap_addr_t * p_cur_addr = m_whitelist_addr[i].p_addr; if (memcmp(p_cur_addr->addr, m_master.bond.master_addr.addr, BLE_GAP_ADDR_LEN) == 0) { m_master = m_masters_db[m_whitelist_addr[i].master_handle]; break; } } } if (m_master.bond.master_handle != INVALID_MASTER_HANDLE) { uint32_t err_code = master_sys_attr_set(&m_master); if (err_code != NRF_SUCCESS) { m_bondmngr_config.error_handler(err_code); } if (m_bondmngr_config.evt_handler != NULL) { ble_bondmngr_evt_t evt; evt.evt_type = BLE_BONDMNGR_EVT_CONN_TO_BONDED_MASTER; evt.master_handle = m_master.bond.master_handle; evt.master_id = m_master.bond.master_id_info.div; m_bondmngr_config.evt_handler(&evt); } } }
/**@brief This function handles the new authentication status event, received from the * SoftDevice, related to an already bonded master. * * @details This function also writes the updated Bonding Information to flash and notifies the * application. * * @param[in] p_auth_status Updated authentication status. */ static void auth_status_update(ble_gap_evt_auth_status_t * p_auth_status) { if (!auth_status_equal(&m_master.bond.auth_status, p_auth_status)) { uint32_t err_code; // Authentication status changed, update Bonding Information m_master.bond.auth_status = *p_auth_status; m_master.bond.master_id_info.div = p_auth_status->periph_keys.enc_info.div; // Write updated Bonding Information to flash err_code = bond_info_store(&m_master.bond); if ((err_code == NRF_ERROR_NO_MEM) && (m_bondmngr_config.evt_handler != NULL)) { ble_bondmngr_evt_t evt; evt.evt_type = BLE_BONDMNGR_EVT_BOND_FLASH_FULL; evt.master_handle = m_master.bond.master_handle; evt.master_id = m_master.bond.master_id_info.div; m_bondmngr_config.evt_handler(&evt); } else if (err_code != NRF_SUCCESS) { m_bondmngr_config.error_handler(err_code); } // Pass event to the application if (m_bondmngr_config.evt_handler != NULL) { ble_bondmngr_evt_t evt; evt.evt_type = BLE_BONDMNGR_EVT_AUTH_STATUS_UPDATED; evt.master_handle = m_master.bond.master_handle; evt.master_id = m_master.bond.master_id_info.div; m_bondmngr_config.evt_handler(&evt); } } }
/**@brief Connection Security Update event handler. * * @param[in] p_ble_evt Event received from the BLE stack. */ static void on_sec_update(ble_gap_evt_conn_sec_update_t * p_sec_update) { uint8_t security_mode = p_sec_update->conn_sec.sec_mode.sm; uint8_t security_level = p_sec_update->conn_sec.sec_mode.lv; if (((security_mode == 1) && (security_level > 1)) || ((security_mode == 2) && (security_level != 0))) { if (m_bondmngr_config.evt_handler != NULL) { ble_bondmngr_evt_t evt; evt.evt_type = BLE_BONDMNGR_EVT_ENCRYPTED; evt.master_handle = m_master.bond.master_handle; m_bondmngr_config.evt_handler(&evt); } } }