Contiki 3.x
store.h
Go to the documentation of this file.
1 /**
2  * @file
3  * Convenience layer for storing readings and the config.
4  * Implements locking, and SPI sharing with the CC1120 driver.
5  *
6  * Every sample is assigned a unique id for it's lifetime on flash.
7  * The id of a sample may be reused once it has been deleted.
8  * The store allows deleting any given sample, by gracefully dealing with files that do not exist.
9  *
10  * Callers are always responsible for allocating the required memory.
11  *
12  * @author Arthur Fabre <af1g12@ecs.soton.ac.uk>
13  */
14 
15 #ifndef Z1_COAP_STORE_H
16 #define Z1_COAP_STORE_H
17 
18 #include "contiki.h"
19 #include <stdint.h>
20 #include <stdbool.h>
21 #include "settings.pb.h"
22 #include "readings.pb.h"
23 
24 /**
25  * Initialize the data store.
26  * Includes finding the latest reading.
27  */
28 void store_init(void);
29 
30 /**
31  * Store a sample in the flash.
32  * This will also set sample's id field.
33  * @param *sample The sample to save.
34  * @return A unique id for the sample on success, `false` on failure.
35  */
36 uint16_t store_save_sample(Sample *sample);
37 
38 /**
39  * Get a given sample from the flash,
40  * @param id The id of the sample.
41  * @param *sample The Sample to write the sample to.
42  * @return `true` on success, `false` otherwise.
43  */
44 bool store_get_sample(uint16_t id, Sample *sample);
45 
46 /**
47  * Get a given sample from the flash,
48  * in the form of an encoded protocol buffer.
49  * @param id The id of the sample.
50  * @param buffer An allocated buffer at least Samle_size big to which the sample protocol buffer will be written.
51  * @return The number of bytes written to the buffer on success, `false` otherwise.
52  */
53 uint8_t store_get_raw_sample(uint16_t id, uint8_t buffer[Sample_size]);
54 
55 /**
56  * Get the most recent sample from the flash.
57  * @param *sample The Sample to write the sample to.
58  * @return `true` on success, `false` otherwise.
59  */
60 bool store_get_latest_sample(Sample *sample);
61 
62 /**
63  * Get the most recent sample from the flash,
64  * in the form of an encoded protocol buffer.
65  * @param buffer An allocated buffer at last Sample_size big to which the sample protocol buffer will be written.
66  * @return The number of bytes written to the buffer on success, `false` otherwise.
67  */
68 uint8_t store_get_latest_raw_sample(uint8_t buffer[Sample_size]);
69 
70 /**
71  * Get the identifer of the most recent sample stored.
72  * @return The identifier of the latest sample.
73  */
74 uint16_t store_get_latest_sample_id(void);
75 
76 /**
77  * Delete a given sample from the flash.
78  * @param id The id of the sample to delete.
79  * @return `true` on success, `false` otherwise.
80  */
81 bool store_delete_sample(uint16_t id);
82 
83 /**
84  * Save the configuration to flash.
85  * @param *config The configuration to save.
86  * @return `true` on success, `false` otherwise.
87  */
88 bool store_save_config(SensorConfig *config);
89 
90 /**
91  * Get the configuration from the flash.
92  * @param *config. The configuration to write to.
93  * @return `true` on success, `false` otherwise.
94  */
95 bool store_get_config(SensorConfig *config);
96 
97 /**
98  * Get the configuration from the flash.
99  * @param An allocated buffer at least SensorConfig_size big to which the config protocol buffer will be written.
100  * @return The number of bytes written to the buffer on success, `false` otherwise.
101  */
102 uint8_t store_get_raw_config(uint8_t buffer[SensorConfig_size]);
103 
104 #endif // ifndef Z1_COAP_STORE_H
bool store_get_latest_sample(Sample *sample)
Get the most recent sample from the flash.
Definition: store.c:131
bool store_save_config(SensorConfig *config)
Save the configuration to flash.
Definition: store.c:220
bool store_get_config(SensorConfig *config)
Get the configuration from the flash.
Definition: store.c:249
uint8_t store_get_raw_sample(uint16_t id, uint8_t buffer[Sample_size])
Get a given sample from the flash, in the form of an encoded protocol buffer.
Definition: store.c:155
uint16_t store_save_sample(Sample *sample)
Store a sample in the flash.
Definition: store.c:100
bool store_get_sample(uint16_t id, Sample *sample)
Get a given sample from the flash,.
Definition: store.c:139
bool store_delete_sample(uint16_t id)
Delete a given sample from the flash.
Definition: store.c:174
void store_init(void)
Initialize the data store.
Definition: store.c:339
uint8_t store_get_raw_config(uint8_t buffer[SensorConfig_size])
Get the configuration from the flash.
Definition: store.c:262
uint8_t store_get_latest_raw_sample(uint8_t buffer[Sample_size])
Get the most recent sample from the flash, in the form of an encoded protocol buffer.
Definition: store.c:135
uint16_t store_get_latest_sample_id(void)
Get the identifer of the most recent sample stored.
Definition: store.c:170