/**@brief Function for updating the current master's entry in the database. */ static uint32_t master_update(void) { uint32_t err_code; int32_t master_handle = m_master.bond.master_handle; if ((master_handle >= 0) && (master_handle < m_masters_in_db_count)) { // Update the database based on whether the bond and system attributes have // been loaded or not to avoid overwriting information that was not used in the // connection session. if (m_bond_loaded) { m_masters_db[master_handle].bond = m_master.bond; } if (m_sys_attr_loaded) { m_masters_db[master_handle].sys_attr = m_master.sys_attr; } update_whitelist(); err_code = NRF_SUCCESS; } else { err_code = NRF_ERROR_INTERNAL; } return err_code; }
/**@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 This function updates the current master's entry in the database. */ static uint32_t master_update(void) { uint32_t err_code; int32_t master_handle = m_master.bond.master_handle; if ((master_handle >= 0) && (master_handle < m_masters_in_db_count)) { m_masters_db[master_handle] = m_master; update_whitelist(); err_code = NRF_SUCCESS; } else { err_code = NRF_ERROR_INTERNAL; } return err_code; }
int procfile_write(struct file *file, const char *buffer, unsigned long count, void *data) { procfs_buffer_size = count; if(procfs_buffer_size >= PROCFS_MAX_SIZE) { printk(KERN_ERR PROC_CONFIG_NAME ": There is no enought space in the procfs buffer, changes will be ignored\n"); return -ENOSPC; } if(copy_from_user(procfs_buffer, buffer, procfs_buffer_size)) { return -EFAULT; } procfs_buffer[procfs_buffer_size - 1] = '\0'; update_whitelist(); return count; }
/**@brief This function loads all Bonding Information and System Attributes from flash. * * @return NRF_SUCCESS on success, otherwise an error code. */ static uint32_t load_all_from_flash(void) { uint32_t err_code; int i; // Load bond information for all masters m_masters_in_db_count = 0; while (m_masters_in_db_count < BLE_BONDMNGR_MAX_BONDED_MASTERS) { master_bond_t master_bond_info; int master_handle; // Load Bonding Information err_code = bonding_info_load_from_flash(&master_bond_info); if (err_code == NRF_ERROR_NOT_FOUND) { // No more bonds in flash break; } else if (err_code != NRF_SUCCESS) { return err_code; } master_handle = master_bond_info.master_handle; if (master_handle > m_masters_in_db_count) { // Master handle value(s) missing in flash. This should never happen. return NRF_ERROR_INVALID_DATA; } else { // Add/update Bonding Information in master array. m_masters_db[master_handle].bond = master_bond_info; if (master_handle == m_masters_in_db_count) { // New master handle, clear System Attributes. m_masters_db[master_handle].sys_attr.sys_attr_size = 0; m_masters_db[master_handle].sys_attr.master_handle = INVALID_MASTER_HANDLE; m_masters_in_db_count++; } else { // Entry was updated, do nothing } } } // Load System Attributes for all previously known masters. for (i = 0; i < m_masters_in_db_count; i++) { master_sys_attr_t master_sys_attr; // Load System Attributes. err_code = sys_attr_load_from_flash(&master_sys_attr); if (err_code == NRF_ERROR_NOT_FOUND) { // No more System Attributes in flash break; } else if (err_code != NRF_SUCCESS) { return err_code; } if (master_sys_attr.master_handle > m_masters_in_db_count) { // Master handle value(s) missing in flash. This should never happen. return NRF_ERROR_INVALID_DATA; } else { // Add/update Bonding Information in master array m_masters_db[master_sys_attr.master_handle].sys_attr = master_sys_attr; } } // Initialize the remaining empty bond entries in the memory. for (i = m_masters_in_db_count; i < BLE_BONDMNGR_MAX_BONDED_MASTERS; i++) { m_masters_db[i].bond.master_handle = INVALID_MASTER_HANDLE; m_masters_db[i].sys_attr.sys_attr_size = 0; m_masters_db[i].sys_attr.master_handle = INVALID_MASTER_HANDLE; } // Update whitelist data structures update_whitelist(); return NRF_SUCCESS; }
uint32_t ble_bondmngr_bonded_master_delete(uint16_t master_id) { if (!m_is_bondmngr_initialized) { return NRF_ERROR_INVALID_STATE; } int8_t master_handle_to_be_deleted = INVALID_MASTER_HANDLE; uint8_t i; // Search for the handle of the master. for (i = 0; i < m_masters_in_db_count; i++) { if (m_masters_db[i].bond.master_id_info.div == master_id) { master_handle_to_be_deleted = i; break; } } if (master_handle_to_be_deleted == INVALID_MASTER_HANDLE) { // Master ID not found. return NRF_ERROR_NOT_FOUND; } // Delete the master in RAM. for (i = master_handle_to_be_deleted; i < (m_masters_in_db_count - 1); i++) { // Overwrite the current master entry with the next one. m_masters_db[i] = m_masters_db[i + 1]; // Decrement the value of handle. m_masters_db[i].bond.master_handle--; m_masters_db[i].sys_attr.master_handle--; } // Clear the last database entry. memset(&(m_masters_db[m_masters_in_db_count - 1]), 0, sizeof(master_t)); m_masters_in_db_count--; uint32_t err_code; // Reinitialize the pointers to the memory where bonding info and System Attributes are stored // in flash. err_code = ble_flash_page_addr(m_bondmngr_config.flash_page_num_bond, &mp_flash_bond_info); if (err_code != NRF_SUCCESS) { return err_code; } err_code = ble_flash_page_addr(m_bondmngr_config.flash_page_num_sys_attr, &mp_flash_sys_attr); if (err_code != NRF_SUCCESS) { return err_code; } // Refresh the data in the flash memory (both Bonding Information and System Attributes). // Erase and rewrite bonding info and System Attributes. err_code = flash_pages_erase(); if (err_code != NRF_SUCCESS) { return err_code; } m_bond_info_in_flash_count = 0; m_sys_attr_in_flash_count = 0; for (i = 0; i < m_masters_in_db_count; i++) { err_code = bond_info_store(&(m_masters_db[i].bond)); if (err_code != NRF_SUCCESS) { return err_code; } } for (i = 0; i < m_masters_in_db_count; i++) { err_code = sys_attr_store(&(m_masters_db[i].sys_attr)); if (err_code != NRF_SUCCESS) { return err_code; } } update_whitelist(); return NRF_SUCCESS; }