Data Storage Management API
SDK provide simple APIs to manage data storage. The storage APIs can be used to store data on storage media during unavailability of network or for logging purpose.
Example Usage
#include <storage.h>
struct storagecfg_t config;
/* configure data storage */
/* set storage media */
config.media = STORAGE_MEDIA_FS; /* internal flash */
/* set virtual partitions */
config.parts = 1; /* single partition */
/* storage mode */
config.mode = STORAGE_MODE_FIFO; /* FIFO style */
/* action on storage full */
config.on_full = STORAGE_ONFULL_DELETE; /* delete oldest */
/* compression mode */
config.compression = false; /* no compression, sometimes better when using binary */
/* storage packet size, only used for estimation */
config.packet_sz = 64; /* approx data per packet */
/* maximum storage limit */
config.max_storage = 1000; /* maximum storage in terms of data packets */
/* reserved memory */
config.reserved_mem = 16 * 1024; /* minimum amount of memory to keep in storage */
/* init storage */
storage_init(&config);
/* save a packet in data storage partiton 1 */
storage_save(1, &data_packet, sizeof(data_packet));
/* retrive packet from storage partition 1 */
int packet_len;
packet_len = sizeof(data_packet);
storage_read(1, &data_packet, &packet_len);
/* read multiple packets from storage 1 */
for (int i = 0; i < 10; i++) {
int packet_len = sizeof(data_packet[i]);
storage_read_indexed(1, i, &data_packet[i], &packet_len);
}
/* delete 5 packets from storage partition 1 */
storage_delete(1, 5);
/* check if data available in partition 1 */
if (storage_available(1)) {
printf("Data available in storage\n");
} else {
printf("No data available\n");
}
/* get count of data available */
printf("Storage 1 has %d packets\n", storage_getcount(1));
/* delete all data in parition 1 */
storage_clear(1);
/* clear all data in all partitions */
storage_clear_all();
API Reference
Header File
Source: include/storage.h
#include <storage.h>
Functions
-
int storage_init(const struct storagecfg_t *cfg)
Initialize storage management library. This function must be called before calling any storage function.
- Parameters:
cfg – [in] Storage configuration
- Returns:
0 on success, negative value on error
-
int storage_save(int part, const void *buffer, int len)
Save data to storage partition
- Parameters:
part – [in] Partition index, starting from 0
buffer – [in] Buffer to store
len – [in] Length of data in buffer
- Returns:
0 on success, negative value on error
-
int storage_read(int part, void *buffer, int *len)
Read data from storage partition
- Parameters:
part – [in] Partition index, starting from 0
buffer – [out] Pointer to buffer where data is stored
len – [out] Size of buffer, on return actual size read is updated
- Returns:
0 on success, negative value on error. len is updated with actual data read from storage
-
int storage_read_indexed(int part, int index, void *buffer, int *len)
Red data from storage partition indexed. This function can be used to read multiple packets at once. The reading index is 0 based. In FIFO mode 0 points to first packet and in LIFO mode 0 points to last packet. e.g in FIFO mode reading an indexed packet of index 2 will return third packet stored in FIFO order.
- Parameters:
part – [in] Partition index, starting from 0
index – [in] Index to read
buffer – [out] Pointer to buffer where data is stored
len – [out] Size of buffer, on return actual size read is updated
- Returns:
0 on success, negative value on error. len is updated with actual data read from storage
-
int storage_delete(int part, int count)
Delete data from storage partition
- Parameters:
part – [in] Partition index, starting from 0
count – [in] Number of packets to delete
- Returns:
0 on success, negative value on error
-
int storage_available(int part)
Get storage status
- Parameters:
part – [in] Partition index, starting from 0
- Returns:
TRUE if offline is pending, FALSE if not data is available
-
int storage_getcount(int part)
Get storage data count available in partition
- Parameters:
part – [in] Partition index, starting from 0
- Returns:
available count
-
int storage_clear(int part)
Delete data in storage partion
- Parameters:
part – [in] Partition index, starting from 0
- Returns:
0 on success, negative on error
-
int storage_clear_all(void)
Delete all data
- Returns:
int
Structures
-
struct storagecfg_t
Storage Configuration Structure
Public Members
-
uint8_t media
Storage Media storage_media_e
-
uint8_t parts
Number of Storage partition
-
uint8_t mode
Storage mode storage_mode_e
-
uint8_t on_full
Action to be taken when storage is full storage_action_e
-
uint8_t compression
1 to enable compression, 0 for no compression
-
uint8_t noformat_onfail
0 will format storage space when configuration fails to load, Set 1 to disable format
-
uint16_t reserved
reserved for future use
-
uint32_t packet_sz
Approximate Average data packet size, this parameter is only used for storage estimation. does not limit data size for read/write
-
uint32_t max_storage
Maximum number/count of data packets to store, storage full status is based on either max_storage is reached or storage memory <= resvd_mem
-
uint32_t reserved_mem
Amount to reserved memory to keep in storage media, 16Kb minimum
-
uint8_t media
Enumerations
-
enum storage_media_e
Storage media
Values:
-
enumerator STORAGE_MEDIA_FS
Storage media internal flash memory
-
enumerator STORAGE_MEDIA_SPIFS
Storage media external SPI Flash
-
enumerator STORAGE_MEDIA_SD
Storage media SD card
-
enumerator STORAGE_MEDIA_FS
