function_name
stringlengths
1
80
function
stringlengths
14
10.6k
comment
stringlengths
12
8.02k
normalized_comment
stringlengths
6
6.55k
update_status_bit_is_set
static __INLINE bool update_status_bit_is_set(uint32_t index) { return ((m_peer_addr_update & (BIT_0 << index)) ? true : false); }
/**@brief Function for providing update status for the device identified by 'index'. * * @param[in] index Device identifier. * * @retval true if the bit is set, false otherwise. */
@brief Function for providing update status for the device identified by 'index'. @param[in] index Device identifier. @retval true if the bit is set, false otherwise.
application_instance_init
static __INLINE void application_instance_init(uint32_t index) { DM_TRC("[DM]: Initializing Application Instance 0x%08X.\r\n", index); m_application_table[index].ntf_cb = NULL; m_application_table[index].state = 0x00; m_application_table[index].service = 0x00; }
/**@brief Function for initialiasing the application instance identified by 'index'. * * @param[in] index Device identifier. */
@brief Function for initialiasing the application instance identified by 'index'. @param[in] index Device identifier.
connection_instance_init
static __INLINE void connection_instance_init(uint32_t index) { DM_TRC("[DM]: Initializing Connection Instance 0x%08X.\r\n", index); m_connection_table[index].state = STATE_IDLE; m_connection_table[index].conn_handle = BLE_CONN_HANDLE_INVALID; m_connection_table[index].bonded_dev_id = DM_INVALID_ID; memset(&m_connection_table[index].peer_addr, 0, sizeof (ble_gap_addr_t)); }
/**@brief Function for initialiasing the connection instance identified by 'index'. * * @param[in] index Device identifier. */
@brief Function for initialiasing the connection instance identified by 'index'. @param[in] index Device identifier.
peer_instance_init
static __INLINE void peer_instance_init(uint32_t index) { DM_TRC("[DM]: Initializing Peer Instance 0x%08X.\r\n", index); memset(m_peer_table[index].peer_id.id_addr_info.addr, 0, BLE_GAP_ADDR_LEN); memset(m_peer_table[index].peer_id.id_info.irk, 0, BLE_GAP_SEC_KEY_LEN); //Initialize the address type to invalid. m_peer_table[index].peer_id.id_addr_info.addr_type = INVALID_ADDR_TYPE; //Initialize the identification bit map to unassigned. m_peer_table[index].id_bitmap = UNASSIGNED; // Initialize diversifier. m_peer_table[index].ediv = EDIV_INIT_VAL; //Reset the status bit. update_status_bit_reset(index); #if (DEVICE_MANAGER_APP_CONTEXT_SIZE != 0) //Initialize the application context for bond device. m_app_context_table[index] = NULL; #endif //DEVICE_MANAGER_APP_CONTEXT_SIZE }
/**@brief Function for initialiasing the peer device instance identified by 'index'. * * @param[in] index Device identifier. */
@brief Function for initialiasing the peer device instance identified by 'index'. @param[in] index Device identifier.
connection_instance_find
static ret_code_t connection_instance_find(uint16_t conn_handle, uint8_t state, uint32_t * p_instance) { ret_code_t err_code; uint32_t index; err_code = NRF_ERROR_INVALID_STATE; for (index = 0; index < DEVICE_MANAGER_MAX_CONNECTIONS; index++) { //Search only based on the state. if (state & m_connection_table[index].state) { //Ignore the connection handle. if ((conn_handle == BLE_CONN_HANDLE_INVALID) || (conn_handle == m_connection_table[index].conn_handle)) { //Search for matching connection handle. (*p_instance) = index; err_code = NRF_SUCCESS; break; } else { err_code = NRF_ERROR_NOT_FOUND; } } } return err_code; }
/**@brief Function for searching connection instance matching the connection handle and the state * requested. * * @details Connection handle and state information is used to get a connection instance, it * is possible to ignore the connection handle by using BLE_CONN_HANDLE_INVALID. * * @param[in] conn_handle Connection handle. * @param[in] state Connection instance state. * @param[out] p_instance Connection instance. * * @retval NRF_SUCCESS Operation success. * @retval NRF_ERROR_INVALID_STATE Operation failure. Invalid state * @retval NRF_ERROR_NOT_FOUND Operation failure. Not found */
@brief Function for searching connection instance matching the connection handle and the state requested. @details Connection handle and state information is used to get a connection instance, it is possible to ignore the connection handle by using BLE_CONN_HANDLE_INVALID. @param[in] conn_handle Connection handle. @param[in] state Connection instance state. @param[out] p_instance Connection instance. @retval NRF_SUCCESS Operation success. @retval NRF_ERROR_INVALID_STATE Operation failure. Invalid state @retval NRF_ERROR_NOT_FOUND Operation failure. Not found
device_instance_free
static __INLINE ret_code_t device_instance_free(uint32_t device_index) { ret_code_t err_code; pstorage_handle_t block_handle; //Get the block handle. err_code = pstorage_block_identifier_get(&m_storage_handle, device_index, &block_handle); if (err_code == NRF_SUCCESS) { DM_TRC("[DM]:[DI 0x%02X]: Freeing Instance.\r\n", device_index); //Request clearing of the block. err_code = pstorage_clear(&block_handle, ALL_CONTEXT_SIZE); if (err_code == NRF_SUCCESS) { peer_instance_init(device_index); } } return err_code; }
/**@brief Function for freeing a device instance allocated for bonded device. * * @param[in] device_index Device index. * * @retval NRF_SUCCESS On success, else an error code indicating reason for failure. */
@brief Function for freeing a device instance allocated for bonded device. @param[in] device_index Device index. @retval NRF_SUCCESS On success, else an error code indicating reason for failure.
app_evt_notify
static __INLINE void app_evt_notify(dm_handle_t const * const p_handle, dm_event_t const * const p_event, uint32_t event_result) { dm_event_cb_t app_cb = m_application_table[0].ntf_cb; DM_MUTEX_UNLOCK(); DM_TRC("[DM]: Notifying application of event 0x%02X\r\n", p_event->event_id); //No need to do any kind of return value processing thus can be supressed. UNUSED_VARIABLE(app_cb(p_handle, p_event, event_result)); DM_MUTEX_LOCK(); }
/**@brief Function for notifying connection manager event to the application. * * @param[in] p_handle Device handle identifying device. * @param[in] p_event Connection manager event details. * @param[in] event_result Event result code. */
@brief Function for notifying connection manager event to the application. @param[in] p_handle Device handle identifying device. @param[in] p_event Connection manager event details. @param[in] event_result Event result code.
connection_instance_allocate
static __INLINE uint32_t connection_instance_allocate(uint32_t * p_instance) { uint32_t err_code; DM_TRC("[DM]: Request to allocation connection instance\r\n"); err_code = connection_instance_find(BLE_CONN_HANDLE_INVALID, STATE_IDLE, p_instance); if (err_code == NRF_SUCCESS) { DM_LOG("[DM]:[%02X]: Connection Instance Allocated.\r\n", (*p_instance)); m_connection_table[*p_instance].state = STATE_CONNECTED; } else { DM_LOG("[DM]: No free connection instances available\r\n"); err_code = NRF_ERROR_NO_MEM; } return err_code; }
/**@brief Function for allocating instance. * * @details The instance identifier is provided in the 'p_instance' parameter if the routine * succeeds. * * @param[out] p_instance Connection instance. * * @retval NRF_SUCCESS Operation success. * @retval NRF_ERROR_NO_MEM Operation failure. No memory. */
@brief Function for allocating instance. @details The instance identifier is provided in the 'p_instance' parameter if the routine succeeds. @param[out] p_instance Connection instance. @retval NRF_SUCCESS Operation success. @retval NRF_ERROR_NO_MEM Operation failure. No memory.
connection_instance_free
static __INLINE void connection_instance_free(uint32_t const * p_instance) { DM_TRC("[DM]:[CI 0x%02X]: Freeing connection instance\r\n", (*p_instance)); if (m_connection_table[*p_instance].state != STATE_IDLE) { DM_LOG("[DM]:[%02X]: Freed connection instance.\r\n", (*p_instance)); connection_instance_init(*p_instance); } }
/**@brief Function for freeing instance. Instance identifier is provided in the parameter * 'p_instance' in case the routine succeeds. * * @param[in] p_instance Connection instance. */
@brief Function for freeing instance. Instance identifier is provided in the parameter 'p_instance' in case the routine succeeds. @param[in] p_instance Connection instance.
storage_operation_dummy_handler
static uint32_t storage_operation_dummy_handler(pstorage_handle_t * p_dest, uint8_t * p_src, pstorage_size_t size, pstorage_size_t offset) { return NRF_SUCCESS; }
/**@brief Function for storage operation dummy handler. * * @param[in] p_dest Destination address where data is to be stored persistently. * @param[in] p_src Source address containing data to be stored. API assumes this to be resident * memory and no intermediate copy of data is made by the API. * @param[in] size Size of data to be stored expressed in bytes. Should be word aligned. * @param[in] offset Offset in bytes to be applied when writing to the block. * For example, if within a block of 100 bytes, application wishes to * write 20 bytes at offset of 12, then this field should be set to 12. * Should be word aligned. * * @retval NRF_SUCCESS Operation success. */
@brief Function for storage operation dummy handler. @param[in] p_dest Destination address where data is to be stored persistently. @param[in] p_src Source address containing data to be stored. API assumes this to be resident memory and no intermediate copy of data is made by the API. @param[in] size Size of data to be stored expressed in bytes. Should be word aligned. @param[in] offset Offset in bytes to be applied when writing to the block. For example, if within a block of 100 bytes, application wishes to write 20 bytes at offset of 12, then this field should be set to 12. Should be word aligned. @retval NRF_SUCCESS Operation success.
no_service_context_store
static __INLINE ret_code_t no_service_context_store(pstorage_handle_t const * p_block_handle, dm_handle_t const * p_handle) { DM_LOG("[DM]: --> no_service_context_store\r\n"); return NRF_SUCCESS; }
/**@brief Function for storing when there is no service registered. * * @param[in] p_block_handle Storage block identifier. * @param[in] p_handle Device handle identifying device that is loaded. * * @retval NRF_SUCCESS */
@brief Function for storing when there is no service registered. @param[in] p_block_handle Storage block identifier. @param[in] p_handle Device handle identifying device that is loaded. @retval NRF_SUCCESS
gattc_context_store
static __INLINE ret_code_t gattc_context_store(pstorage_handle_t const * p_block_handle, dm_handle_t const * p_handle) { DM_LOG("[DM]: --> gattc_context_store\r\n"); return NRF_SUCCESS; }
/**@brief Function for storing GATT Client context. * * @param[in] p_block_handle Storage block identifier. * @param[in] p_handle Device handle identifying device that is stored. * * @retval NRF_SUCCESS Operation success. */
@brief Function for storing GATT Client context. @param[in] p_block_handle Storage block identifier. @param[in] p_handle Device handle identifying device that is stored. @retval NRF_SUCCESS Operation success.
gattsc_context_store
static __INLINE ret_code_t gattsc_context_store(pstorage_handle_t const * p_block_handle, dm_handle_t const * p_handle) { DM_LOG("[DM]: --> gattsc_context_store\r\n"); ret_code_t err_code = gatts_context_store(p_block_handle, p_handle); if (NRF_SUCCESS == err_code) { err_code = gattc_context_store(p_block_handle, p_handle); } return err_code; }
/**@brief Function for storing GATT Server & Client context. * * @param[in] p_block_handle Storage block identifier. * @param[in] p_handle Device handle identifying device that is stored. * * @retval NRF_SUCCESS On success, else an error code indicating reason for failure. */
@brief Function for storing GATT Server & Client context. @param[in] p_block_handle Storage block identifier. @param[in] p_handle Device handle identifying device that is stored. @retval NRF_SUCCESS On success, else an error code indicating reason for failure.
no_service_context_load
static __INLINE ret_code_t no_service_context_load(pstorage_handle_t const * p_block_handle, dm_handle_t const * p_handle) { DM_LOG("[DM]: --> no_service_context_load\r\n"); return NRF_SUCCESS; }
/**@brief Function for loading when there is no service registered. * * @param[in] p_block_handle Storage block identifier. * @param[in] p_handle Device handle identifying device that is loaded. * * @retval NRF_SUCCESS */
@brief Function for loading when there is no service registered. @param[in] p_block_handle Storage block identifier. @param[in] p_handle Device handle identifying device that is loaded. @retval NRF_SUCCESS
gattc_context_load
static __INLINE ret_code_t gattc_context_load(pstorage_handle_t const * p_block_handle, dm_handle_t const * p_handle) { DM_LOG("[DM]: --> gattc_context_load\r\n"); return NRF_SUCCESS; }
/**@brief Function for loading GATT Client context. * * @param[in] p_block_handle Storage block identifier. * @param[in] p_handle Device handle identifying device that is loaded. * * @retval NRF_SUCCESS */
@brief Function for loading GATT Client context. @param[in] p_block_handle Storage block identifier. @param[in] p_handle Device handle identifying device that is loaded. @retval NRF_SUCCESS
gattsc_context_load
static __INLINE ret_code_t gattsc_context_load(pstorage_handle_t const * p_block_handle, dm_handle_t const * p_handle) { DM_LOG("[DM]: --> gattsc_context_load\r\n"); ret_code_t err_code = gatts_context_load(p_block_handle, p_handle); if (NRF_SUCCESS == err_code) { err_code = gattc_context_load(p_block_handle, p_handle); } return err_code; }
/**@brief Function for loading GATT Server & Client context. * * @param[in] p_block_handle Storage block identifier. * @param[in] p_handle Device handle identifying device that is loaded. * * @retval NRF_SUCCESS On success, else an error code indicating reason for failure. */
@brief Function for loading GATT Server & Client context. @param[in] p_block_handle Storage block identifier. @param[in] p_handle Device handle identifying device that is loaded. @retval NRF_SUCCESS On success, else an error code indicating reason for failure.
no_service_context_apply
static __INLINE ret_code_t no_service_context_apply(dm_handle_t * p_handle) { DM_LOG("[DM]: --> no_service_context_apply\r\n"); DM_LOG("[DM]:[CI 0x%02X]: No Service context\r\n", p_handle->connection_id); return NRF_SUCCESS; }
/**@brief Function for applying when there is no service registered. * * @param[in] p_handle Device handle identifying device that is applied. * * @retval NRF_SUCCESS */
@brief Function for applying when there is no service registered. @param[in] p_handle Device handle identifying device that is applied. @retval NRF_SUCCESS
gattc_context_apply
static __INLINE ret_code_t gattc_context_apply(dm_handle_t * p_handle) { DM_LOG("[DM]: --> gattc_context_apply\r\n"); return NRF_SUCCESS; }
/**@brief Function for applying GATT Client context. * * @param[in] p_handle Device handle identifying device that is applied. * * @retval NRF_SUCCESS On success. */
@brief Function for applying GATT Client context. @param[in] p_handle Device handle identifying device that is applied. @retval NRF_SUCCESS On success.
gattsc_context_apply
static __INLINE ret_code_t gattsc_context_apply(dm_handle_t * p_handle) { uint32_t err_code; DM_LOG("[DM]: --> gattsc_context_apply\r\n"); err_code = gatts_context_apply(p_handle); if (err_code == NRF_SUCCESS) { err_code = gattc_context_apply(p_handle); } return err_code; }
/**@brief Function for applying GATT Server & Client context. * * @param[in] p_handle Device handle identifying device that is applied. * * @retval NRF_SUCCESS On success, else an error code indicating reason for failure. */
@brief Function for applying GATT Server & Client context. @param[in] p_handle Device handle identifying device that is applied. @retval NRF_SUCCESS On success, else an error code indicating reason for failure.
flash_page_erase
static void flash_page_erase(uint32_t * p_page) { // Turn on flash erase enable and wait until the NVMC is ready. NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Een << NVMC_CONFIG_WEN_Pos); while (NRF_NVMC->READY == NVMC_READY_READY_Busy) { // Do nothing. } // Erase page. NRF_NVMC->ERASEPAGE = (uint32_t)p_page; while (NRF_NVMC->READY == NVMC_READY_READY_Busy) { // Do nothing. } // Turn off flash erase enable and wait until the NVMC is ready. NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos); while (NRF_NVMC->READY == NVMC_READY_READY_Busy) { // Do nothing } }
/**@brief Function for erasing a page in flash. * * @param[in] p_page Pointer to first word in page to be erased. */
@brief Function for erasing a page in flash. @param[in] p_page Pointer to first word in page to be erased.
flash_word_unprotected_write
static void flash_word_unprotected_write(uint32_t * p_address, uint32_t value) { // Turn on flash write enable and wait until the NVMC is ready. NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos); while (NRF_NVMC->READY == NVMC_READY_READY_Busy) { // Do nothing. } *p_address = value; // Wait flash write to finish while (NRF_NVMC->READY == NVMC_READY_READY_Busy) { // Do nothing. } // Turn off flash write enable and wait until the NVMC is ready. NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos); while (NRF_NVMC->READY == NVMC_READY_READY_Busy) { // Do nothing. } }
/**@brief Function for writing one word to flash. Unprotected write, which can interfere with radio communication. * * @details This function DOES NOT use the m_radio_active variable, but will force the write even * when the radio is active. To be used only from @ref ble_flash_page_write. * * @note Flash location to be written must have been erased previously. * * @param[in] p_address Pointer to flash location to be written. * @param[in] value Value to write to flash. */
@brief Function for writing one word to flash. Unprotected write, which can interfere with radio communication. @details This function DOES NOT use the m_radio_active variable, but will force the write even when the radio is active. To be used only from @ref ble_flash_page_write. @note Flash location to be written must have been erased previously. @param[in] p_address Pointer to flash location to be written. @param[in] value Value to write to flash.
flash_word_write
static void flash_word_write(uint32_t * p_address, uint32_t value) { // If radio is active, wait for it to become inactive. while (m_radio_active) { // Do nothing (just wait for radio to become inactive). (void) sd_app_evt_wait(); } // Turn on flash write enable and wait until the NVMC is ready. NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos); while (NRF_NVMC->READY == NVMC_READY_READY_Busy) { // Do nothing. } *p_address = value; // Wait flash write to finish while (NRF_NVMC->READY == NVMC_READY_READY_Busy) { // Do nothing. } // Turn off flash write enable and wait until the NVMC is ready. NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos); while (NRF_NVMC->READY == NVMC_READY_READY_Busy) { // Do nothing } }
/**@brief Function for writing one word to flash. * * @note Flash location to be written must have been erased previously. * * @param[in] p_address Pointer to flash location to be written. * @param[in] value Value to write to flash. */
@brief Function for writing one word to flash. @note Flash location to be written must have been erased previously. @param[in] p_address Pointer to flash location to be written. @param[in] value Value to write to flash.
nrf_ecb_init
bool nrf_ecb_init(void) { ecb_key = ecb_data; ecb_cleartext = ecb_data + 16; ecb_ciphertext = ecb_data + 32; NRF_ECB->ECBDATAPTR = (uint32_t)ecb_data; return true; }
/*/< Ciphertext: Starts at ecb_data + 32 bytes.*/
/< Ciphertext: Starts at ecb_data + 32 bytes.
nrf_delay_ms
void nrf_delay_ms(uint32_t volatile number_of_ms) { while(number_of_ms != 0) { number_of_ms--; nrf_delay_us(999); } }
/*lint --e{438} "Variable not used" */
lint --e{438} "Variable not used"
command_queue_element_consume
static void command_queue_element_consume(void) { // Initialize/free the element as it is now processed. cmd_queue_element_init(m_cmd_queue.rp); // Adjust command queue state tracking variables. --(m_cmd_queue.count); if (++(m_cmd_queue.rp) == PSTORAGE_CMD_QUEUE_SIZE) { m_cmd_queue.rp = 0; } }
/**@brief Function for consuming a command queue element. * * @details Function for consuming a command queue element, which has been fully processed. */
@brief Function for consuming a command queue element. @details Function for consuming a command queue element, which has been fully processed.
command_end_procedure_run
static void command_end_procedure_run(void) { app_notify(NRF_SUCCESS, &m_cmd_queue.cmd[m_cmd_queue.rp]); command_queue_element_consume(); sm_state_change(STATE_IDLE); }
/**@brief Function for executing the finalization procedure for the command executed. * * @details Function for executing the finalization procedure for command executed, which includes * notifying the application of command completion, consuming the command queue element, * and changing the internal state. */
@brief Function for executing the finalization procedure for the command executed. @details Function for executing the finalization procedure for command executed, which includes notifying the application of command completion, consuming the command queue element, and changing the internal state.
state_idle_entry_run
static void state_idle_entry_run(void) { m_num_of_command_retries = 0; m_num_of_bytes_written = 0; // Schedule any possible queued flash access operation. cmd_queue_dequeue(); }
/**@brief Function for idle state entry actions. * * @details Function for idle state entry actions, which include resetting relevant state data and * scheduling any possible queued flash access operation. */
@brief Function for idle state entry actions. @details Function for idle state entry actions, which include resetting relevant state data and scheduling any possible queued flash access operation.
app_notify_error_state_transit
static void app_notify_error_state_transit(uint32_t result) { app_notify(result, &m_cmd_queue.cmd[m_cmd_queue.rp]); sm_state_change(STATE_ERROR); }
/**@brief Function for notifying an application of command completion and transitioning to an error * state. * * @param[in] result Result code of the operation for the application. */
@brief Function for notifying an application of command completion and transitioning to an error state. @param[in] result Result code of the operation for the application.
flash_api_err_code_process
static void flash_api_err_code_process(uint32_t err_code) { switch (err_code) { case NRF_SUCCESS: break; case NRF_ERROR_BUSY: // Flash access operation was not accepted and must be reissued upon flash operation // complete event. m_flags |= MASK_FLASH_API_ERR_BUSY; break; default: // Complete the operation with appropriate result code and transit to an error state. app_notify_error_state_transit(err_code); break; } }
/**@brief Function for processing flash API error code. * * @param[in] err_code Error code from the flash API. */
@brief Function for processing flash API error code. @param[in] err_code Error code from the flash API.
flash_write
static void flash_write(uint32_t * const p_dst, uint32_t const * const p_src, uint32_t size_in_words) { flash_api_err_code_process(sd_flash_write(p_dst, p_src, size_in_words)); }
/**@brief Function for writing data to flash. * * @param[in] p_dst Pointer to start of flash location to be written. * @param[in] p_src Pointer to buffer with data to be written. * @param[in] size_in_words Number of 32-bit words to write. */
@brief Function for writing data to flash. @param[in] p_dst Pointer to start of flash location to be written. @param[in] p_src Pointer to buffer with data to be written. @param[in] size_in_words Number of 32-bit words to write.
state_store_entry_run
static void state_store_entry_run(void) { store_cmd_flash_write_execute(); }
/**@brief Function for store state entry action. * * @details Function for store state entry action, which includes writing data to a flash page. */
@brief Function for store state entry action. @details Function for store state entry action, which includes writing data to a flash page.
flash_page_erase
static void flash_page_erase(uint32_t page_number) { flash_api_err_code_process(sd_flash_page_erase(page_number)); }
/**@brief Function for erasing flash page. * * @param[in] page_number Page number of the page to be erased. */
@brief Function for erasing flash page. @param[in] page_number Page number of the page to be erased.
state_data_erase_entry_run
static void state_data_erase_entry_run(void) { flash_page_erase(m_current_page_id); }
/**@brief Function for data erase state entry action. * * @details Function for data erase state entry action, which includes erasing the data flash page. */
@brief Function for data erase state entry action. @details Function for data erase state entry action, which includes erasing the data flash page.
state_entry_action_run
static void state_entry_action_run(void) { switch (m_state) { case STATE_IDLE: state_idle_entry_run(); break; case STATE_STORE: state_store_entry_run(); break; case STATE_DATA_ERASE_WITH_SWAP: state_data_erase_swap_entry_run(); break; case STATE_DATA_ERASE: state_data_erase_entry_run(); break; default: // No action needed. break; } }
/**@brief Function for dispatching the correct application main state entry action. */
@brief Function for dispatching the correct application main state entry action.
sm_state_change
static void sm_state_change(pstorage_state_t new_state) { m_state = new_state; state_entry_action_run(); }
/**@brief Function for changing application main state and dispatching state entry action. * * @param[in] new_state New application main state to transit to. */
@brief Function for changing application main state and dispatching state entry action. @param[in] new_state New application main state to transit to.
state_swap_erase_entry_run
static void state_swap_erase_entry_run(void) { flash_page_erase(PSTORAGE_SWAP_ADDR / PSTORAGE_FLASH_PAGE_SIZE); }
/**@brief Function for swap erase state entry action. * * @details Function for swap erase state entry action, which includes erasing swap flash * page. */
@brief Function for swap erase state entry action. @details Function for swap erase state entry action, which includes erasing swap flash page.
state_write_data_swap_entry_run
static void state_write_data_swap_entry_run(void) { // @note: There is room for further optimization here as there is only need to write the // whole flash page to swap area if there is both head and tail area to be restored. In any // other case we can omit some data from the head or end of the page as that is the clear area. flash_write((uint32_t *)(PSTORAGE_SWAP_ADDR), (uint32_t *)(m_current_page_id * PSTORAGE_FLASH_PAGE_SIZE), PSTORAGE_FLASH_PAGE_SIZE / sizeof(uint32_t)); }
/**@brief Function for write data to the swap state entry action. * * @details Function for write data to the swap state entry action, which includes writing the * current data page to the swap flash page. */
@brief Function for write data to the swap state entry action. @details Function for write data to the swap state entry action, which includes writing the current data page to the swap flash page.
state_erase_data_page_entry_run
static void state_erase_data_page_entry_run(void) { flash_page_erase(m_current_page_id); }
/**@brief Function for erase data page state entry action. * * @details Function for erase data page state entry action, which includes erasing the data flash * page. */
@brief Function for erase data page state entry action. @details Function for erase data page state entry action, which includes erasing the data flash page.
state_restore_tail_entry_run
static void state_restore_tail_entry_run(void) { const cmd_queue_element_t * p_cmd = &m_cmd_queue.cmd[m_cmd_queue.rp]; const pstorage_block_t cmd_block_id = p_cmd->storage_addr.block_id; const uint32_t tail_offset = (cmd_block_id + p_cmd->size + p_cmd->offset) % PSTORAGE_FLASH_PAGE_SIZE; flash_write((uint32_t *)(cmd_block_id + p_cmd->size + p_cmd->offset), (uint32_t *)(PSTORAGE_SWAP_ADDR + tail_offset), m_tail_word_size); }
/**@brief Function for restore tail state entry action. * * @details Function for restore tail state entry action, which includes writing the tail section * back from swap to the data page. */
@brief Function for restore tail state entry action. @details Function for restore tail state entry action, which includes writing the tail section back from swap to the data page.
state_restore_head_entry_run
static void state_restore_head_entry_run(void) { flash_write((uint32_t *)((m_current_page_id - 1u) * PSTORAGE_FLASH_PAGE_SIZE), (uint32_t *)PSTORAGE_SWAP_ADDR, m_head_word_size); }
/**@brief Function for restore head state entry action. * * @details Function for restore head state entry action, which includes writing the head section * back from swap to the data page. */
@brief Function for restore head state entry action. @details Function for restore head state entry action, which includes writing the head section back from swap to the data page.
swap_sub_state_entry_action_run
static void swap_sub_state_entry_action_run(void) { static void (* const swap_sub_state_sm_lut[SWAP_SUB_STATE_MAX])(void) = { state_swap_erase_entry_run, state_write_data_swap_entry_run, state_erase_data_page_entry_run, state_restore_tail_entry_run, state_restore_head_entry_run }; swap_sub_state_sm_lut[m_swap_sub_state](); }
/**@brief Function for dispatching the correct swap sub state entry action. */
@brief Function for dispatching the correct swap sub state entry action.
swap_sub_state_state_change
static void swap_sub_state_state_change(flash_swap_sub_state_t new_state) { m_swap_sub_state = new_state; swap_sub_state_entry_action_run(); }
/**@brief Function for changing the swap sub state and dispatching state entry action. * * @param[in] new_state New swap sub state to transit to. */
@brief Function for changing the swap sub state and dispatching state entry action. @param[in] new_state New swap sub state to transit to.
cmd_queue_element_init
static void cmd_queue_element_init(uint32_t index) { // Internal function and checks on range of index can be avoided. m_cmd_queue.cmd[index].op_code = INVALID_OPCODE; m_cmd_queue.cmd[index].size = 0; m_cmd_queue.cmd[index].storage_addr.module_id = PSTORAGE_NUM_OF_PAGES; m_cmd_queue.cmd[index].storage_addr.block_id = 0; m_cmd_queue.cmd[index].p_data_addr = NULL; m_cmd_queue.cmd[index].offset = 0; }
/**@brief Function for initializing the command queue element. * * @param[in] index Index of the element to be initialized. */
@brief Function for initializing the command queue element. @param[in] index Index of the element to be initialized.
cmd_queue_init
static void cmd_queue_init(void) { m_cmd_queue.rp = 0; m_cmd_queue.count = 0; for (uint32_t cmd_index = 0; cmd_index < PSTORAGE_CMD_QUEUE_SIZE; ++cmd_index) { cmd_queue_element_init(cmd_index); } }
/**@brief Function for initializing the command queue. */
@brief Function for initializing the command queue.
cmd_queue_dequeue
static void cmd_queue_dequeue(void) { if ((m_cmd_queue.count != 0)) { cmd_process(); } }
/**@brief Function for dequeing a possible pending flash access operation. */
@brief Function for dequeing a possible pending flash access operation.
app_notify
static void app_notify(uint32_t result, cmd_queue_element_t * p_elem) { pstorage_ntf_cb_t ntf_cb; const uint8_t op_code = p_elem->op_code; #ifdef PSTORAGE_RAW_MODE_ENABLE if (p_elem->storage_addr.module_id == RAW_MODE_APP_ID) { ntf_cb = m_raw_app_table.cb; } else #endif // PSTORAGE_RAW_MODE_ENABLE { ntf_cb = m_app_table[p_elem->storage_addr.module_id].cb; } ntf_cb(&p_elem->storage_addr, op_code, result, p_elem->p_data_addr, m_app_data_size); }
/**@brief Function for notifying an application of command completion. * * @param[in] result Result code of the operation for the application. * @param[in] p_elem Pointer to the command queue element for which this result was received. */
@brief Function for notifying an application of command completion. @param[in] result Result code of the operation for the application. @param[in] p_elem Pointer to the command queue element for which this result was received.
is_tail_data_page_swap_required
static bool is_tail_data_page_swap_required(void) { bool ret_value; // Extract id of the last page command is executed upon. const cmd_queue_element_t * p_cmd = &m_cmd_queue.cmd[m_cmd_queue.rp]; const pstorage_block_t cmd_block_id = p_cmd->storage_addr.block_id; const uint32_t last_page_id = (cmd_block_id + p_cmd->size + p_cmd->offset - 1u) / PSTORAGE_FLASH_PAGE_SIZE; // If tail section area exists and the current page is the last page then tail data page swap is // required. if ((m_tail_word_size != 0) && (m_current_page_id == last_page_id)) { ret_value = true; } else { ret_value = false; } return ret_value; }
/**@brief Function for evaluating if a data page swap is required for the tail section on the * current page. * * @retval true If data page swap is required. * @retval false If data page swap is not required. */
@brief Function for evaluating if a data page swap is required for the tail section on the current page. @retval true If data page swap is required. @retval false If data page swap is not required.
clear_post_processing_run
static void clear_post_processing_run(void) { const cmd_queue_element_t * p_cmd = &m_cmd_queue.cmd[m_cmd_queue.rp]; if (p_cmd->op_code != PSTORAGE_UPDATE_OP_CODE) { command_end_procedure_run(); } else { store_operation_execute(); } }
/**@brief Function for performing post processing for the update and clear commands. * * @details Function for performing post processing for the update and clear commands, which implies * executing the correct execution path depending on the command. */
@brief Function for performing post processing for the update and clear commands. @details Function for performing post processing for the update and clear commands, which implies executing the correct execution path depending on the command.
swap_sub_sm_exit_action_run
static void swap_sub_sm_exit_action_run(void) { clear_post_processing_run(); }
/**@brief Function for doing swap sub state exit action. */
@brief Function for doing swap sub state exit action.
is_page_erase_required
static bool is_page_erase_required(void) { bool ret; const cmd_queue_element_t * p_cmd = &m_cmd_queue.cmd[m_cmd_queue.rp]; const pstorage_block_t cmd_block_id = p_cmd->storage_addr.block_id; const uint32_t id_last_page_to_be_cleared = (cmd_block_id + p_cmd->size + p_cmd->offset - 1u) / PSTORAGE_FLASH_PAGE_SIZE; // True if: // - current page is not the last page OR // - current page is the last page AND no tail exists if ((m_current_page_id < id_last_page_to_be_cleared) || ((m_current_page_id == id_last_page_to_be_cleared) && (m_tail_word_size == 0))) { ret = true; } else { ret = false; } return ret; }
/**@brief Function for evaluating if the page erase operation is required for the current page. * * @retval true If page erase is required. * @retval false If page erase is not required. */
@brief Function for evaluating if the page erase operation is required for the current page. @retval true If page erase is required. @retval false If page erase is not required.
swap_sub_state_err_busy_process
static void swap_sub_state_err_busy_process(void) { // Reissue the request by doing a self transition to the current state. m_flags &= ~MASK_FLASH_API_ERR_BUSY; swap_sub_state_state_change(m_swap_sub_state); }
/**@brief Function for reissuing the last flash operation request, which was rejected by the flash * API, in swap sub sate. */
@brief Function for reissuing the last flash operation request, which was rejected by the flash API, in swap sub sate.
head_restore_state_run
static void head_restore_state_run(void) { if (!(m_flags & MASK_FLASH_API_ERR_BUSY)) { if (is_tail_data_page_swap_required()) { // Additional data page needs to be swapped for tail section as we are clearing a block, // which is shared between 2 flash pages. // Adjust variables to ensure correct state transition path is taken after the tail // section swap has completed. m_head_word_size = 0; m_flags |= MASK_TAIL_SWAP_DONE; swap_sub_state_state_change(STATE_ERASE_SWAP); } else if (is_page_erase_required()) { // Additional page erase operation is required. // Adjust variable to ensure correct state transition path is taken after the additional // page erase operation has completed. m_head_word_size = 0; swap_sub_state_state_change(STATE_ERASE_DATA_PAGE); } else if (m_tail_word_size != 0) { // Proceed with restoring tail from swap to data page. swap_sub_state_state_change(STATE_RESTORE_TAIL); } else { // Swap statemachine execution end reached. swap_sub_sm_exit_action_run(); } } else { // As operation request was rejected by the flash API reissue the request. swap_sub_state_err_busy_process(); } }
/**@brief Function for doing restore head state action upon flash operation success event. * * @details Function for doing restore head state action upon flash operation success event, which * includes making a state transition depending on the current state. */
@brief Function for doing restore head state action upon flash operation success event. @details Function for doing restore head state action upon flash operation success event, which includes making a state transition depending on the current state.
tail_restore_state_run
static void tail_restore_state_run(void) { if (!(m_flags & MASK_FLASH_API_ERR_BUSY)) { swap_sub_sm_exit_action_run(); } else { // As operation request was rejected by the flash API reissue the request. swap_sub_state_err_busy_process(); } }
/**@brief Function for doing restore tail state action upon flash operation success event. */
@brief Function for doing restore tail state action upon flash operation success event.
data_to_swap_write_state_run
static void data_to_swap_write_state_run(void) { if (!(m_flags & MASK_FLASH_API_ERR_BUSY)) { // If the operation is executed only on 1 single flash page it automatically means that tail // area is written to the swap, which we store to flags. if (m_flags & MASK_SINGLE_PAGE_OPERATION) { m_flags |= MASK_TAIL_SWAP_DONE; } swap_sub_state_state_change(STATE_ERASE_DATA_PAGE); } else { // As operation request was rejected by the flash API reissue the request. swap_sub_state_err_busy_process(); } }
/**@brief Function for doing data to swap write state action upon flash operation success event. */
@brief Function for doing data to swap write state action upon flash operation success event.
swap_erase_state_run
static void swap_erase_state_run(void) { if (!(m_flags & MASK_FLASH_API_ERR_BUSY)) { swap_sub_state_state_change(STATE_WRITE_DATA_TO_SWAP); } else { // As operation request was rejected by the flash API reissue the request. swap_sub_state_err_busy_process(); } }
/**@brief Function for doing swap erase state action upon flash operation success event. */
@brief Function for doing swap erase state action upon flash operation success event.
swap_sub_state_sm_run
static void swap_sub_state_sm_run(void) { static void (* const swap_sub_state_sm_lut[SWAP_SUB_STATE_MAX])(void) = { swap_erase_state_run, data_to_swap_write_state_run, data_page_erase_state_run, tail_restore_state_run, head_restore_state_run }; swap_sub_state_sm_lut[m_swap_sub_state](); }
/**@brief Function for dispatching the correct state action for data erase with a swap composite * state upon a flash operation success event. */
@brief Function for dispatching the correct state action for data erase with a swap composite state upon a flash operation success event.
main_state_err_busy_process
static void main_state_err_busy_process(void) { // Reissue the request by doing a self transition to the current state. m_flags &= ~MASK_FLASH_API_ERR_BUSY; sm_state_change(m_state); }
/**@brief Function for reissuing the last flash operation request, which was rejected by the flash * API, in main sate. */
@brief Function for reissuing the last flash operation request, which was rejected by the flash API, in main sate.
erase_sub_state_sm_run
static void erase_sub_state_sm_run(void) { if (!(m_flags & MASK_FLASH_API_ERR_BUSY)) { // Clear operation request has succeeded. ++m_current_page_id; if (!is_page_erase_required()) { clear_post_processing_run(); } else { // All required flash pages have not yet been erased, issue erase by doing a self // transit. sm_state_change(m_state); } } else { // As operation request was rejected by the flash API reissue the request. main_state_err_busy_process(); } }
/**@brief Function for doing erase state action upon flash operation success event. * * @details Function for doing erase state action upon flash operation success event, which includes * making a state transition depending on the current state. */
@brief Function for doing erase state action upon flash operation success event. @details Function for doing erase state action upon flash operation success event, which includes making a state transition depending on the current state.
store_sub_state_sm_run
static void store_sub_state_sm_run(void) { if (!(m_flags & MASK_FLASH_API_ERR_BUSY)) { // As write operation request has succeeded, adjust the size tracking state information // accordingly. cmd_queue_element_t * p_cmd = &m_cmd_queue.cmd[m_cmd_queue.rp]; p_cmd->size -= m_num_of_bytes_written; if (p_cmd->size == 0) { command_end_procedure_run(); } else { store_cmd_flash_write_execute(); } } else { // As operation request was rejected by the flash API reissue the request. main_state_err_busy_process(); } }
/**@brief Function for doing store state action upon flash operation success event. */
@brief Function for doing store state action upon flash operation success event.
flash_operation_success_run
static void flash_operation_success_run(void) { switch (m_state) { case STATE_STORE: store_sub_state_sm_run(); break; case STATE_DATA_ERASE: erase_sub_state_sm_run(); break; case STATE_DATA_ERASE_WITH_SWAP: swap_sub_state_sm_run(); break; default: // No implementation needed. break; } }
/**@brief Function for doing action upon flash operation success event. */
@brief Function for doing action upon flash operation success event.
flash_operation_failure_run
static void flash_operation_failure_run(void) { if (++m_num_of_command_retries != SD_CMD_MAX_TRIES) { // Retry the last operation by doing a self transition to the current state. if (m_state != STATE_DATA_ERASE_WITH_SWAP) { sm_state_change(m_state); } else { swap_sub_state_state_change(m_swap_sub_state); } } else { // Complete the operation with appropriate result code and transit to an error state. app_notify_error_state_transit(NRF_ERROR_TIMEOUT); } }
/**@brief Function for doing action upon flash operation failure event. * * @details Function for doing action upon flash operation failure event, which includes retrying * the last operation or if retry count has been reached completing the operation with * appropriate result code and transitioning to an error state. * * @note The command is not removed from the command queue, which will result to stalling of the * command pipeline and the appropriate application recovery procedure for this is to reset * the system by issuing @ref pstorage_init which will also result to flushing of the * command queue. */
@brief Function for doing action upon flash operation failure event. @details Function for doing action upon flash operation failure event, which includes retrying the last operation or if retry count has been reached completing the operation with appropriate result code and transitioning to an error state. @note The command is not removed from the command queue, which will result to stalling of the command pipeline and the appropriate application recovery procedure for this is to reset the system by issuing @ref pstorage_init which will also result to flushing of the command queue.
pstorage_sys_event_handler
void pstorage_sys_event_handler(uint32_t sys_evt) { if (m_state != STATE_IDLE && m_state != STATE_ERROR) { switch (sys_evt) { case NRF_EVT_FLASH_OPERATION_SUCCESS: flash_operation_success_run(); break; case NRF_EVT_FLASH_OPERATION_ERROR: if (!(m_flags & MASK_FLASH_API_ERR_BUSY)) { flash_operation_failure_run(); } else { // As our last flash operation request was rejected by the flash API reissue the // request by doing same code execution path as for flash operation sucess // event. This will promote code reuse in the implementation. flash_operation_success_run(); } break; default: // No implementation needed. break; } } }
/**@brief Function for handling flash access result events. * * @param[in] sys_evt System event to be handled. */
@brief Function for handling flash access result events. @param[in] sys_evt System event to be handled.
tail_word_size_calculate
static void tail_word_size_calculate(pstorage_size_t cmd_end_of_storage_address, pstorage_size_t end_of_storage_address) { // Two different cases to resolve when calculating correct size for restore tail section: // 1) End of storage area and command end area are in the same page. // 2) End of storage area and command end area are not in the same page. const uint32_t end_of_storage_area_page = end_of_storage_address / PSTORAGE_FLASH_PAGE_SIZE; const uint32_t command_end_of_storage_area_page = cmd_end_of_storage_address / PSTORAGE_FLASH_PAGE_SIZE; if (end_of_storage_area_page == command_end_of_storage_area_page) { //lint -e{573} suppress "Signed-unsigned mix with divide". m_tail_word_size = (end_of_storage_address - cmd_end_of_storage_address) / sizeof(uint32_t); } else { //lint -e{573} suppress "Signed-unsigned mix with divide". m_tail_word_size = (PSTORAGE_FLASH_PAGE_SIZE - (cmd_end_of_storage_address % PSTORAGE_FLASH_PAGE_SIZE)) / sizeof(uint32_t); } }
/**@brief Function for calculating the tail area size in number of 32-bit words. * * @param[in] cmd_end_of_storage_address End of storage area within the scope of the command. * @param[in] end_of_storage_address End of allocated storage area for the application. */
@brief Function for calculating the tail area size in number of 32-bit words. @param[in] cmd_end_of_storage_address End of storage area within the scope of the command. @param[in] end_of_storage_address End of allocated storage area for the application.
store_operation_execute
static void store_operation_execute(void) { sm_state_change(STATE_STORE); }
/**@brief Function for executing the store operation. */
@brief Function for executing the store operation.
update_operation_execute
static void update_operation_execute(void) { clear_operation_execute(); }
/**@brief Function for executing the update operation. */
@brief Function for executing the update operation.
cmd_process
static void cmd_process(void) { const cmd_queue_element_t * p_cmd = &m_cmd_queue.cmd[m_cmd_queue.rp]; m_app_data_size = p_cmd->size; switch (p_cmd->op_code) { case PSTORAGE_STORE_OP_CODE: store_operation_execute(); break; case PSTORAGE_CLEAR_OP_CODE: clear_operation_execute(); break; case PSTORAGE_UPDATE_OP_CODE: update_operation_execute(); break; default: // No action required. break; } }
/**@brief Function for dispatching the flash access operation. */
@brief Function for dispatching the flash access operation.
next_index
static __INLINE uint8_t next_index(uint8_t index) { return (index < m_queue_size) ? (index + 1) : 0; }
/**@brief Function for incrementing a queue index, and handle wrap-around. * * @param[in] index Old index. * * @return New (incremented) index. */
@brief Function for incrementing a queue index, and handle wrap-around. @param[in] index Old index. @return New (incremented) index.
app_sched_event_get
static uint32_t app_sched_event_get(void ** pp_event_data, uint16_t * p_event_data_size, app_sched_event_handler_t * p_event_handler) { uint32_t err_code = NRF_ERROR_NOT_FOUND; if (!APP_SCHED_QUEUE_EMPTY()) { uint16_t event_index; // NOTE: There is no need for a critical region here, as this function will only be called // from app_sched_execute() from inside the main loop, so it will never interrupt // app_sched_event_put(). Also, updating of (i.e. writing to) the start index will be // an atomic operation. event_index = m_queue_start_index; m_queue_start_index = next_index(m_queue_start_index); *pp_event_data = &m_queue_event_data[event_index * m_queue_event_size]; *p_event_data_size = m_queue_event_headers[event_index].event_data_size; *p_event_handler = m_queue_event_headers[event_index].handler; err_code = NRF_SUCCESS; } return err_code; }
/**@brief Function for reading the next event from specified event queue. * * @param[out] pp_event_data Pointer to pointer to event data. * @param[out] p_event_data_size Pointer to size of event data. * @param[out] p_event_handler Pointer to event handler function pointer. * * @return NRF_SUCCESS if new event, NRF_ERROR_NOT_FOUND if event queue is empty. */
@brief Function for reading the next event from specified event queue. @param[out] pp_event_data Pointer to pointer to event data. @param[out] p_event_data_size Pointer to size of event data. @param[out] p_event_handler Pointer to event handler function pointer. @return NRF_SUCCESS if new event, NRF_ERROR_NOT_FOUND if event queue is empty.
dfu_app_reset_prepare
static void dfu_app_reset_prepare(void) { // Reset prepare should be handled by application. // This function can be extended to include default handling if application does not implement // own handler. }
/**@brief Function for reset_prepare handler if the application has not registered a handler. */
@brief Function for reset_prepare handler if the application has not registered a handler.
interrupts_disable
static void interrupts_disable(void) { uint32_t interrupt_setting_mask; uint32_t irq; // Fetch the current interrupt settings. interrupt_setting_mask = NVIC->ISER[0]; // Loop from interrupt 0 for disabling of all interrupts. for (irq = 0; irq < MAX_NUMBER_INTERRUPTS; irq++) { if (interrupt_setting_mask & (IRQ_ENABLED << irq)) { // The interrupt was enabled, hence disable it. NVIC_DisableIRQ((IRQn_Type)irq); } } }
/**@brief Function for disabling all interrupts before jumping from bootloader to application. */
@brief Function for disabling all interrupts before jumping from bootloader to application.
app_error_handler
__WEAK void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name) { // On assert, the system can only recover with a reset. #ifndef DEBUG NVIC_SystemReset(); #else #ifdef BSP_DEFINES_ONLY LEDS_ON(LEDS_MASK); #else UNUSED_VARIABLE(bsp_indication_set(BSP_INDICATE_FATAL_ERROR)); // This call can be used for debug purposes during application development. // @note CAUTION: Activating this code will write the stack to flash on an error. // This function should NOT be used in a final product. // It is intended STRICTLY for development/debugging purposes. // The flash write will happen EVEN if the radio is active, thus interrupting // any communication. // Use with care. Uncomment the line below to use. //ble_debug_assert_handler(error_code, line_num, p_file_name); #endif // BSP_DEFINES_ONLY // The following variable helps Keil keep the call stack visible, in addition, it can be set to // 0 in the debugger to continue executing code after the error check. volatile bool loop = true; UNUSED_VARIABLE(loop); m_error_code = error_code; m_line_num = line_num; m_p_file_name = p_file_name; UNUSED_VARIABLE(m_error_code); UNUSED_VARIABLE(m_line_num); UNUSED_VARIABLE(m_p_file_name); __disable_irq(); while(loop); #endif // DEBUG }
/*lint -save -e14 */
lint -save -e14
sdk_mapped_flags_set_by_index
static __INLINE void sdk_mapped_flags_set_by_index(sdk_mapped_flags_t * p_flags, uint16_t index) { *p_flags |= (1U << index); }
/**@brief Function for setting the state of a flag to true. * * @note This function does not check whether the index is valid. * * @param[in] p_flags The collection of flags to modify. * @param[in] index The index of the flag to modify. */
@brief Function for setting the state of a flag to true. @note This function does not check whether the index is valid. @param[in] p_flags The collection of flags to modify. @param[in] index The index of the flag to modify.
sdk_mapped_flags_clear_by_index
static __INLINE void sdk_mapped_flags_clear_by_index(sdk_mapped_flags_t * p_flags, uint16_t index) { *p_flags &= ~(1U << index); }
/**@brief Function for setting the state of a flag to false. * * @note This function does not check whether the index is valid. * * @param[in] p_flags The collection of flags to modify. * @param[in] index The index of the flag to modify. */
@brief Function for setting the state of a flag to false. @note This function does not check whether the index is valid. @param[in] p_flags The collection of flags to modify. @param[in] index The index of the flag to modify.
sdk_mapped_flags_get_by_index
static __INLINE bool sdk_mapped_flags_get_by_index(sdk_mapped_flags_t flags, uint16_t index) { return ((flags & (1 << index)) != 0); }
/**@brief Function for getting the state of a flag. * * @note This function does not check whether the index is valid. * * @param[in] p_flags The collection of flags to read. * @param[in] index The index of the flag to get. */
@brief Function for getting the state of a flag. @note This function does not check whether the index is valid. @param[in] p_flags The collection of flags to read. @param[in] index The index of the flag to get.
check_config
static bool check_config(fs_config_t const * const config) { if (config == NULL) { return false; } if ((FS_SECTION_VARS_START_ADDR <= (uint32_t)config) && ((uint32_t)config < FS_SECTION_VARS_END_ADDR)) { return true; } else { return false; } }
/**@brief Function to check that the configuration is non-NULL and within * valid section variable memory bounds. * * @param[in] config Configuration to check. */
@brief Function to check that the configuration is non-NULL and within valid section variable memory bounds. @param[in] config Configuration to check.
queue_init
static void queue_init(void) { memset(&m_cmd_queue, 0, sizeof(fs_cmd_queue_t)); }
/**@brief Function to initialize the queue. */
@brief Function to initialize the queue.
cmd_reset
static void cmd_reset(uint32_t index) { memset(&m_cmd_queue.cmd[index], 0, sizeof(fs_cmd_t)); }
/**@brief Function to reset a queue item to its default values. * * @param index Index of the queue element. */
@brief Function to reset a queue item to its default values. @param index Index of the queue element.
cmd_consume
static void cmd_consume(uint32_t result, const fs_cmd_t * p_cmd) { // Consume the current item on the queue. uint8_t rp = m_cmd_queue.rp; m_cmd_queue.count--; if (m_cmd_queue.count == 0) { // There are no elements left. Stop processing the queue. m_flags &= ~FS_FLAG_PROCESSING; } if (++(m_cmd_queue.rp) == FS_CMD_QUEUE_SIZE) { m_cmd_queue.rp = 0; } // Notify upon successful operation. app_notify(result, p_cmd); // Reset the queue element. cmd_reset(rp); }
/**@brief Function to consume queue item and notify the return value of the operation. * * @details This function will report the result and remove the command from the queue after * notification. */
@brief Function to consume queue item and notify the return value of the operation. @details This function will report the result and remove the command from the queue after notification.
store_execute
static __INLINE uint32_t store_execute(fs_cmd_t const * const p_cmd) { // Write in chunks if write-size is larger than FS_MAX_WRITE_SIZE. fs_length_t const length = ((p_cmd->length_words - p_cmd->offset) < FS_MAX_WRITE_SIZE_WORDS) ? (p_cmd->length_words - p_cmd->offset) : FS_MAX_WRITE_SIZE_WORDS; return sd_flash_write((uint32_t*)p_cmd->p_addr + p_cmd->offset /* destination */, (uint32_t*)p_cmd->p_src + p_cmd->offset /* source */, length); }
/**@brief Function to store data to flash. * * @param[in] p_cmd The queue element associated with the operation. * * @retval NRF_SUCCESS Success. The request was sent to the SoftDevice. * @retval Any error returned by the SoftDevice flash API. */
@brief Function to store data to flash. @param[in] p_cmd The queue element associated with the operation. @retval NRF_SUCCESS Success. The request was sent to the SoftDevice. @retval Any error returned by the SoftDevice flash API.
erase_execute
static __INLINE uint32_t erase_execute(fs_cmd_t const * const p_cmd) { // Erase the page. return sd_flash_page_erase((uint32_t)(p_cmd->p_addr + p_cmd->offset) / FS_PAGE_SIZE); }
/**@brief Function to erase a page. * * @param[in] p_cmd The queue element associated with the operation. * * @retval NRF_SUCCESS Success. The request was sent to the SoftDevice. * @retval Any error returned by the SoftDevice flash API. */
@brief Function to erase a page. @param[in] p_cmd The queue element associated with the operation. @retval NRF_SUCCESS Success. The request was sent to the SoftDevice. @retval Any error returned by the SoftDevice flash API.
queue_process_impl
static uint32_t queue_process_impl(void) { uint32_t ret; fs_cmd_t const * const p_cmd = &m_cmd_queue.cmd[m_cmd_queue.rp]; switch (p_cmd->op_code) { case FS_OP_STORE: ret = store_execute(p_cmd); break; case FS_OP_ERASE: ret = erase_execute(p_cmd); break; case FS_OP_NONE: ret = NRF_SUCCESS; break; default: ret = NRF_ERROR_FORBIDDEN; break; } return ret; }
/**@brief Function to process the current element in the queue and return the result. * * @retval NRF_SUCCESS Success. * @retval NRF_ERROR_FORBIDDEN Error. Undefined command. * @retval Any error returned by the SoftDevice flash API. */
@brief Function to process the current element in the queue and return the result. @retval NRF_SUCCESS Success. @retval NRF_ERROR_FORBIDDEN Error. Undefined command. @retval Any error returned by the SoftDevice flash API.
queue_process
static ret_code_t queue_process(void) { ret_code_t ret = NRF_SUCCESS; /** If the queue is not being processed, and there are still * some elements in it, then start processing. */ if ( !(m_flags & FS_FLAG_PROCESSING) && (m_cmd_queue.count > 0)) { m_flags |= FS_FLAG_PROCESSING; ret = queue_process_impl(); /** There is ongoing flash-operation which was not * initiated by fstorage. */ if (ret == NRF_ERROR_BUSY) { // Wait for a system callback. m_flags |= FS_FLAG_FLASH_REQ_PENDING; // Stop processing the queue. m_flags &= ~FS_FLAG_PROCESSING; ret = NRF_SUCCESS; } else if (ret != NRF_SUCCESS) { // Another error has occurred. app_notify(ret, &m_cmd_queue.cmd[m_cmd_queue.rp]); } } // If we are already processing the queue, return immediately. return ret; }
/**@brief Starts processing the queue if there are no pending flash operations * for which we are awaiting a callback. */
@brief Starts processing the queue if there are no pending flash operations for which we are awaiting a callback.
on_operation_success
static __INLINE void on_operation_success(void) { fs_cmd_t * const p_cmd = &m_cmd_queue.cmd[m_cmd_queue.rp]; m_retry_count = 0; switch (p_cmd->op_code) { case FS_OP_STORE: // Update the offset on successful write. p_cmd->offset += FS_MAX_WRITE_SIZE_WORDS; break; case FS_OP_ERASE: // Update the offset to correspond to the page that has been erased. p_cmd->offset += FS_PAGE_SIZE_WORDS; break; } // If offset is equal to or larger than length, then the operation has finished. if (p_cmd->offset >= p_cmd->length_words) { cmd_consume(NRF_SUCCESS, p_cmd); } queue_process(); }
/**@brief Flash operation success callback handler. * * @details This function updates read/write pointers. * This function resets retry count. */
@brief Flash operation success callback handler. @details This function updates read/write pointers. This function resets retry count.
on_operation_failure
static __INLINE void on_operation_failure(uint32_t sys_evt) { const fs_cmd_t * p_cmd; if (++m_retry_count > FS_CMD_MAX_RETRIES) { p_cmd = &m_cmd_queue.cmd[m_cmd_queue.rp]; cmd_consume(NRF_ERROR_TIMEOUT, p_cmd); } queue_process(); }
/**@brief Flash operation failure callback handler. * * @details Function to keep track of retries and notify failures. */
@brief Flash operation failure callback handler. @details Function to keep track of retries and notify failures.
app_notify
static void app_notify(uint32_t result, fs_cmd_t const * const p_cmd) { p_cmd->p_config->cb(p_cmd->op_code, result, p_cmd->p_addr, p_cmd->length_words); }
/**@brief Function to notify users. * * @param[in] result Result of the flash operation. * @param[in] p_cmd The command associated with the callback. */
@brief Function to notify users. @param[in] result Result of the flash operation. @param[in] p_cmd The command associated with the callback.
fs_sys_event_handler
void fs_sys_event_handler(uint32_t sys_evt) { if (m_flags & FS_FLAG_PROCESSING) { /** A flash operation was initiated by this module. * Handle its result. */ switch (sys_evt) { case NRF_EVT_FLASH_OPERATION_SUCCESS: on_operation_success(); break; case NRF_EVT_FLASH_OPERATION_ERROR: on_operation_failure(sys_evt); break; } } else if ((m_flags & FS_FLAG_FLASH_REQ_PENDING)) { /** A flash operation was initiated outside this module. * We have now receveid a callback which indicates it has * finished. Clear the FS_FLAG_FLASH_REQ_PENDING flag. */ m_flags &= ~FS_FLAG_FLASH_REQ_PENDING; // Resume processing the queue, if necessary. queue_process(); } }
/**@brief Function to handle system events from the SoftDevice. * * @details This function should be dispatched system events if any of the modules used by * the application rely on FStorage. Examples include @ref Peer Manager and * @ref Flash Data Storage. * * @param[in] sys_evt System Event received. */
@brief Function to handle system events from the SoftDevice. @details This function should be dispatched system events if any of the modules used by the application rely on FStorage. Examples include @ref Peer Manager and @ref Flash Data Storage. @param[in] sys_evt System Event received.
fs_debug_print
void fs_debug_print() { printf("fs start address: 0x%08lx\r\n", (unsigned long)FS_SECTION_VARS_START_ADDR); printf("fs end address: 0x%08lx\r\n", (unsigned long)FS_SECTION_VARS_END_ADDR); printf("Num items: 0x%08lx\r\n", (unsigned long)FS_SECTION_VARS_COUNT); printf("===== ITEMS %lu =====\r\n", (unsigned long)FS_SECTION_VARS_COUNT); for(int i = 0; i < FS_SECTION_VARS_COUNT; i++) { fs_config_t* config = FS_SECTION_VARS_GET(i); printf( "Address: 0x%08lx, CB: 0x%08lx\r\n", (unsigned long)config, (unsigned long)config->cb ); } printf("\r\n"); }
/* Just for testing out section vars (across many compilers).*/
Just for testing out section vars (across many compilers).
header_is_valid
static __INLINE bool header_is_valid(fds_header_t const * const p_header) { return ((p_header->tl.type != FDS_TYPE_ID_INVALID) && (p_header->ic.instance != FDS_INSTANCE_ID_INVALID)); }
/**@brief Function to check if a header has valid information. */
@brief Function to check if a header has valid information.
page_has_space
static bool page_has_space(uint16_t page, fds_length_t length_words) { if (page >= FDS_MAX_PAGES) { return false; } CRITICAL_SECTION_ENTER(); length_words += m_pages[page].write_offset; length_words += m_pages[page].words_reserved; CRITICAL_SECTION_EXIT(); return (length_words < FS_PAGE_SIZE_WORDS); }
/* NOTE: depends on m_pages.write_offset to function.*/
NOTE: depends on m_pages.write_offset to function.
page_tag_write_swap
static ret_code_t page_tag_write_swap(uint16_t page) { return fs_store(&fs_config, m_pages[page].start_addr, (uint32_t const *)&fds_page_tag_swap, FDS_PAGE_TAG_SIZE); }
/**@brief Tags a page as swap, i.e., reserved for GC. */
@brief Tags a page as swap, i.e., reserved for GC.
page_tag_write_valid
static ret_code_t page_tag_write_valid(uint16_t page) { return fs_store(&fs_config, m_pages[page].start_addr, (uint32_t const *)&fds_page_tag_valid, FDS_PAGE_TAG_SIZE); }
/**@brief Tags a page as valid, i.e, ready for storage. */
@brief Tags a page as valid, i.e, ready for storage.
page_tag_write_gc
static ret_code_t page_tag_write_gc(uint16_t page) { return fs_store(&fs_config, m_pages[page].start_addr + 3, (uint32_t const *)&fds_page_tag_gc, 1 /*Words*/); }
/**@brief Tags a valid page as being garbage collected. */
@brief Tags a valid page as being garbage collected.
queues_init
static void queues_init(void) { memset(&m_cmd_queue, 0, sizeof(fds_cmd_queue_t)); memset(&m_chunk_queue, 0, sizeof(fds_chunk_queue_t)); }
/**@brief Function for initializing the command queue. */
@brief Function for initializing the command queue.
cmd_queue_advance
static bool cmd_queue_advance(void) { // Reset the current element. memset(&m_cmd_queue.cmd[m_cmd_queue.rp], 0, sizeof(fds_cmd_t)); CRITICAL_SECTION_ENTER(); if (m_cmd_queue.count != 0) { // Advance in the queue, wrapping around if necessary. m_cmd_queue.rp = (m_cmd_queue.rp + 1) % FDS_CMD_QUEUE_SIZE; m_cmd_queue.count--; } CRITICAL_SECTION_EXIT(); return m_cmd_queue.count != 0; }
/**@brief Advances one position in the command queue. Returns true if the queue is not empty. */
@brief Advances one position in the command queue. Returns true if the queue is not empty.
chunk_queue_get_and_advance
static bool chunk_queue_get_and_advance(fds_record_chunk_t ** pp_chunk) { bool chunk_popped = false; CRITICAL_SECTION_ENTER(); if (m_chunk_queue.count != 0) { // Point to the current chunk and advance the queue. *pp_chunk = &m_chunk_queue.chunk[m_chunk_queue.rp]; m_chunk_queue.rp = (m_chunk_queue.rp + 1) % FDS_CHUNK_QUEUE_SIZE; m_chunk_queue.count--; chunk_popped = true; } CRITICAL_SECTION_EXIT(); return chunk_popped; }
/**@brief Returns the current chunk, and advances to the next in the queue. */
@brief Returns the current chunk, and advances to the next in the queue.
queue_reserve_cancel
static void queue_reserve_cancel(uint8_t num_cmd, uint8_t num_chunks) { CRITICAL_SECTION_ENTER(); m_cmd_queue.count -= num_cmd; m_chunk_queue.count -= num_chunks; CRITICAL_SECTION_EXIT(); }
/**@brief Cancel the reservation on resources on queues. */
@brief Cancel the reservation on resources on queues.
softdevice_assertion_handler
void softdevice_assertion_handler(uint32_t pc, uint16_t line_num, const uint8_t * file_name) { UNUSED_PARAMETER(pc); assert_nrf_callback(line_num, file_name); }
/**@brief Callback function for asserts in the SoftDevice. * * @details A pointer to this function will be passed to the SoftDevice. This function will be * called if an ASSERT statement in the SoftDevice fails. * * @param[in] pc The value of the program counter when the ASSERT call failed. * @param[in] line_num Line number of the failing ASSERT call. * @param[in] file_name File name of the failing ASSERT call. */
@brief Callback function for asserts in the SoftDevice. @details A pointer to this function will be passed to the SoftDevice. This function will be called if an ASSERT statement in the SoftDevice fails. @param[in] pc The value of the program counter when the ASSERT call failed. @param[in] line_num Line number of the failing ASSERT call. @param[in] file_name File name of the failing ASSERT call.
SOFTDEVICE_EVT_IRQHandler
void SOFTDEVICE_EVT_IRQHandler(void) { if (m_evt_schedule_func != NULL) { uint32_t err_code = m_evt_schedule_func(); APP_ERROR_CHECK(err_code); } else { intern_softdevice_events_execute(); } }
/**@brief Function for handling the Application's BLE Stack events interrupt. * * @details This function is called whenever an event is ready to be pulled. */
@brief Function for handling the Application's BLE Stack events interrupt. @details This function is called whenever an event is ready to be pulled.
Schema_AddModelProperty
SCHEMA_RESULT Schema_AddModelProperty(SCHEMA_MODEL_TYPE_HANDLE modelTypeHandle, const char* propertyName, const char* propertyType) { return AddModelProperty((SCHEMA_MODEL_TYPE_HANDLE_DATA*)modelTypeHandle, propertyName, propertyType); }
/* Codes_SRS_SCHEMA_99_011:[Schema_AddModelProperty shall add one property to the model type identified by modelTypeHandle.] */
Codes_SRS_SCHEMA_99_011:[Schema_AddModelProperty shall add one property to the model type identified by modelTypeHandle.]
Schema_GetModelName
const char* Schema_GetModelName(SCHEMA_MODEL_TYPE_HANDLE modelTypeHandle) { const char* result; if (modelTypeHandle == NULL) { result = NULL; } else { SCHEMA_MODEL_TYPE_HANDLE_DATA* modelType = (SCHEMA_MODEL_TYPE_HANDLE_DATA*)modelTypeHandle; result = modelType->Name; } return result; }
/*Codes_SRS_SCHEMA_99_160: [Schema_GetModelName shall return the name of the model identified by modelTypeHandle. If the name cannot be retrieved, then NULL shall be returned.]*/
Codes_SRS_SCHEMA_99_160: [Schema_GetModelName shall return the name of the model identified by modelTypeHandle. If the name cannot be retrieved, then NULL shall be returned.]