nRF5 SDK for Mesh v2.1.1
flash_manager.h
1 /* Copyright (c) 2010 - 2018, Nordic Semiconductor ASA
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form, except as embedded into a Nordic
11  * Semiconductor ASA integrated circuit in a product or a software update for
12  * such product, must reproduce the above copyright notice, this list of
13  * conditions and the following disclaimer in the documentation and/or other
14  * materials provided with the distribution.
15  *
16  * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
17  * contributors may be used to endorse or promote products derived from this
18  * software without specific prior written permission.
19  *
20  * 4. This software, with or without modification, must only be used with a
21  * Nordic Semiconductor ASA integrated circuit.
22  *
23  * 5. Any software provided in binary form under this license must not be reverse
24  * engineered, decompiled, modified and/or disassembled.
25  *
26  * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
27  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
29  * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
32  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
35  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 #ifndef FLASH_MANAGER_H__
38 #define FLASH_MANAGER_H__
39 
40 #include <stdint.h>
41 #include <stdbool.h>
42 
43 #include "packet_buffer.h"
44 #include "nrf_mesh_config_core.h"
45 #include "queue.h"
46 #include "bearer_event.h"
47 
65 #define FLASH_MANAGER_DATA_PER_PAGE (PAGE_SIZE - sizeof(flash_manager_metadata_t))
66 
73 #define FLASH_MANAGER_PAGE_COUNT_MINIMUM(DATA_SIZE, LARGEST_ENTRY_SIZE) (1 + ((DATA_SIZE) / (FLASH_MANAGER_DATA_PER_PAGE - LARGEST_ENTRY_SIZE)))
74 
75 #define FLASH_MANAGER_HANDLE_MAX (0x7EFF)
76 #define FLASH_MANAGER_HANDLE_INVALID (0x0000)
78 #define FLASH_MANAGER_ENTRY_LEN_OVERHEAD (sizeof(fm_header_t) / WORD_SIZE)
89 typedef uint16_t fm_handle_t;
90 
92 typedef struct
93 {
94  uint16_t len_words;
96 } fm_header_t;
97 
102 typedef struct
103 {
107 
109 typedef struct
110 {
111  fm_header_t header;
112  uint32_t data[];
113 } fm_entry_t;
114 
116 typedef enum
117 {
118  FM_STATE_UNINITIALIZED,
119  FM_STATE_BUILDING,
120  FM_STATE_READY,
121  FM_STATE_DEFRAG,
122  FM_STATE_REMOVING
123 } fm_state_t;
124 
126 typedef struct
127 {
128  uint8_t metadata_len;
133  uint8_t pages_in_area;
134  uint8_t page_index;
135  uint16_t _padding;
137 
139 typedef union
140 {
142  uint8_t raw[PAGE_SIZE];
144 
145 typedef struct flash_manager flash_manager_t;
146 
148 typedef enum
149 {
154 } fm_result_t;
155 
168 typedef void (*flash_manager_write_complete_cb_t)(const flash_manager_t * p_manager,
169  const fm_entry_t * p_entry,
170  fm_result_t result);
171 
180 typedef void (*flash_manager_invalidate_complete_cb_t)(const flash_manager_t * p_manager,
181  fm_handle_t handle,
182  fm_result_t result);
183 
192 typedef void (*flash_manager_remove_complete_cb_t)(const flash_manager_t * p_manager);
193 
198 typedef void (*flash_manager_queue_empty_cb_t)(void);
199 
203 typedef struct
204 {
206  uint32_t page_count;
213 
215 typedef struct
216 {
218  uint32_t invalid_bytes;
219  const fm_entry_t * p_seal;
221 
223 {
226 };
227 
233 typedef void (*flash_manager_mem_listener_cb_t)(void * p_args);
234 
236 typedef struct
237 {
238  queue_elem_t queue_elem;
240  void * p_args;
242 
248 void flash_manager_init(void);
249 
264 uint32_t flash_manager_add(flash_manager_t * p_manager,
265  const flash_manager_config_t * p_config);
266 
280 uint32_t flash_manager_remove(flash_manager_t * p_manager);
281 
290 const fm_entry_t * flash_manager_entry_get(const flash_manager_t * p_manager, fm_handle_t handle);
291 
308 const fm_entry_t * flash_manager_entry_next_get(const flash_manager_t * p_manager,
309  const fm_handle_filter_t * p_filter,
310  const fm_entry_t * p_start);
311 
320 uint32_t flash_manager_entry_count_get(const flash_manager_t * p_manager, const fm_handle_filter_t * p_filter);
321 
339 fm_entry_t * flash_manager_entry_alloc(flash_manager_t * p_manager,
340  fm_handle_t handle,
341  uint32_t data_length);
342 
351 void flash_manager_entry_commit(const fm_entry_t * p_entry);
352 
362 uint32_t flash_manager_entry_invalidate(flash_manager_t * p_manager, fm_handle_t handle);
363 
370 
382 
389 bool flash_manager_is_stable(void);
390 
400 
406 const void * flash_manager_recovery_page_get(void);
407 
408 
410 static inline void flash_manager_wait(void)
411 {
412 #if !defined(HOST)
413  while (!flash_manager_is_stable())
414  {
415  /* Temporary hack to make sure that bearer events are handled while waiting for
416  * the flash manager to finish.
417  * TODO: Find a solution for this that does not include busy-waiting. */
418  if (bearer_event_handler())
419  {
420  __WFE();
421  }
422  }
423 #endif
424 }
425 
426 
429 #endif /* FLASH_MANAGER_H__ */
void(* flash_manager_queue_empty_cb_t)(void)
Action queue empty callback, indicating that the flash manager is finished processing all its ongoing...
void flash_manager_action_queue_empty_cb_set(flash_manager_queue_empty_cb_t queue_empty_cb)
Sets a function to call when the flash manager&#39;s action queue is empty.
uint8_t metadata_len
Length of manager metadata in bytes.
void flash_manager_entry_release(fm_entry_t *p_entry)
Release an allocated entry buffer that won&#39;t be committed after all.
flash_manager_write_complete_cb_t write_complete_cb
Callback called after every completed write action, or NULL.
The action completed successfully.
void(* flash_manager_write_complete_cb_t)(const flash_manager_t *p_manager, const fm_entry_t *p_entry, fm_result_t result)
Write complete callback, to give the user the result of an entry write action.
The managed flash area is full, and cannot accept any new entries.
uint8_t entry_type_length_bits
Length of entry type field in bits.
fm_entry_t * flash_manager_entry_alloc(flash_manager_t *p_manager, fm_handle_t handle, uint32_t data_length)
Allocate a buffer for a flash entry write.
void flash_manager_init(void)
Initialize the flash manager.
flash_manager_invalidate_complete_cb_t invalidate_complete_cb
Callback called after every completed entry invalidation, or NULL.
flash_manager_config_t config
Manager configuration, as set by the user.
void(* flash_manager_invalidate_complete_cb_t)(const flash_manager_t *p_manager, fm_handle_t handle, fm_result_t result)
Invalidate complete callback, to give the user the result of an entry invalidation action...
flash_manager_metadata_t metadata
Metadata at the start of every managed page.
void flash_manager_mem_listener_register(fm_mem_listener_t *p_listener)
Register a call back to be notified once memory has been made available in the internal buffer...
const fm_entry_t * flash_manager_entry_next_get(const flash_manager_t *p_manager, const fm_handle_filter_t *p_filter, const fm_entry_t *p_start)
Get the next entry matching the given filter.
Metadata structure denoting properties of a flash manager page.
static void flash_manager_wait(void)
Waits for the flash manager to complete all its operations.
Memory listener.
fm_result_t
Flash action result, returned in complete-callback.
uint16_t fm_handle_t
Flash manager handle type, used to identify entries in a flash manager area.
Definition: flash_manager.h:89
const void * flash_manager_recovery_page_get(void)
Get the address of the recovery page.
fm_handle_t match
Value that the masked portion of the handle has to match.
uint32_t min_available_space
Number of bytes that should always be left available during normal operation.
fm_handle_t mask
Mask to apply to the match value when comparing with a handle.
fm_handle_t handle
Entry handle.
Definition: flash_manager.h:95
uint32_t page_count
Number of pages in the area.
Flash manager configuration structure, defines the user-configurable parts of the flash manager...
Internal flash manager state, managed and used internally.
uint8_t page_index
Index of this page in its area.
The flash HW malfunctioned, and the operation did not finish correctly.
uint32_t flash_manager_entry_invalidate(flash_manager_t *p_manager, fm_handle_t handle)
Invalidate the entry with the given handle.
fm_state_t
Valid state of a flash manager instance.
uint32_t flash_manager_remove(flash_manager_t *p_manager)
Remove the given flash manager, and erase all of its contents.
void(* flash_manager_remove_complete_cb_t)(const flash_manager_t *p_manager)
Remove complete callback, indicating that the requested remove operation has been completed...
uint8_t pages_in_area
Total number of pages in this area.
flash_manager_remove_complete_cb_t remove_complete_cb
Callback called after the manager has been successfully removed.
uint32_t flash_manager_entry_count_get(const flash_manager_t *p_manager, const fm_handle_filter_t *p_filter)
Get the number of entries matching the given filter.
uint8_t entry_header_length
Length of each entry header in bytes.
uint32_t invalid_bytes
Bytes invalidated in the area.
Single flash manager page.
fm_state_t state
State of the manager.
uint8_t entry_len_length_bits
Length of entry len field in bits.
bool flash_manager_is_stable(void)
Checks whether the module is in the progress of flashing anything.
const flash_manager_page_t * p_area
Start of area owned by this flash manager.
The entry wasn&#39;t present in the manager.
Handle filter, used to search through all handles to find matches.
void(* flash_manager_mem_listener_cb_t)(void *p_args)
Memory listener callback, that will be called once the flash manager has some memory available...
void flash_manager_entry_commit(const fm_entry_t *p_entry)
Commit the given write buffer for flashing.
uint32_t flash_manager_add(flash_manager_t *p_manager, const flash_manager_config_t *p_config)
Add a flash manager instance.
const fm_entry_t * flash_manager_entry_get(const flash_manager_t *p_manager, fm_handle_t handle)
Get a pointer to the entry with the given index.
flash_manager_mem_listener_cb_t callback
Callback to call when there&#39;s memory available.
const fm_entry_t * p_seal
Pointer to the seal entry.
queue_elem_t queue_elem
Used for linked list operation, should not be altered by the user.
void * p_args
Arguments pointer, set by the user and returned in the callback.
Single flash manager entry.
Header prepending every flash entry in a flash manager.
Definition: flash_manager.h:92
uint16_t len_words
Length of entry in words, including header.
Definition: flash_manager.h:94

Documentation feedback | Developer Zone | Subscribe | Updated