ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1
16#pragma GCC diagnostic ignored "-Wmissing-prototypes"
17#pragma GCC diagnostic ignored "-Wmissing-declarations"
18
20#include <stdio.h>
21#include <string.h>
22#include <unity.h>
23
24void setUp(void) { fifo_buffer_clear(); }
25
26void tearDown(void) {}
27
29 uint8_t data[] = {1, 2, 3, 4, 5};
30
31 fram_status status = fifo_put(data, sizeof(data));
32
33 TEST_ASSERT_EQUAL(FRAM_OK, status);
34 TEST_ASSERT_EQUAL(1, fifo_buffer_len());
35}
36
38 const int niters = 20;
39
40 // starting values
41 uint8_t data[10] = {0, 1, 2, 3, 4};
42
43 // write 100 times, therefore 1100 bytes (data + len)
44 for (int i = 0; i < niters; i++) {
45 fram_status status = fifo_put(data, sizeof(data));
46 TEST_ASSERT_EQUAL(FRAM_OK, status);
47
48 // increment index of data
49 for (unsigned int j = 0; j < sizeof(data); j++) {
50 data[j]++;
51 }
52 }
53}
54
56 // starting values
57 uint8_t data[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
58
59 const int niters = fifo_size() / (sizeof(data) + 1);
60
61 // write 100 times, therefore 1100 bytes (data + len)
62 for (int i = 0; i < niters; i++) {
63 fram_status status_zero = fifo_put(data, sizeof(data));
64 TEST_ASSERT_EQUAL(FRAM_OK, status_zero);
65 }
66
67 fram_status status_full = fifo_put(data, sizeof(data));
68 TEST_ASSERT_EQUAL(FRAM_BUFFER_FULL, status_full);
69}
70
72 uint8_t data[16] = {};
73 uint8_t data_len = 0;
74
76
77 TEST_ASSERT_EQUAL(FRAM_BUFFER_EMPTY, status);
78}
79
81 unsigned int items = 5;
82 uint8_t put_data[] = {1, 2, 3, 4, 5};
83 fifo_put(put_data, items);
84
85 uint8_t get_data[items];
86 uint8_t get_data_len;
87 fram_status status = fifo_get(get_data, &get_data_len);
88
89 TEST_ASSERT_EQUAL(FRAM_OK, status);
90 TEST_ASSERT_EQUAL_UINT8_ARRAY(put_data, get_data, items);
91}
92
94 const int niters = 20;
95
96 // starting values
97 uint8_t put_data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
98
99 // write 100 times, therefore 1100 bytes (data+len)
100 for (int i = 0; i < niters; i++) {
101 fifo_put(put_data, sizeof(put_data));
102
103 // increment index of data
104 for (unsigned int j = 0; j < sizeof(put_data); j++) {
105 put_data[j]++;
106 }
107 }
108
109 // reset put_data to starting values
110 // was unsure about the behavior of memcpy
111 for (unsigned int i = 0; i < sizeof(put_data); i++) {
112 put_data[i] = i;
113 }
114
115 uint8_t get_data[10] = {0};
116
117 // read back all the data
118 for (int i = 0; i < niters; i++) {
119 uint8_t get_data_len = 0;
120 fram_status status_get = fifo_get(get_data, &get_data_len);
121
122 TEST_ASSERT_EQUAL(FRAM_OK, status_get);
123 TEST_ASSERT_EQUAL_INT(10, get_data_len);
124 TEST_ASSERT_EQUAL_UINT8_ARRAY(put_data, get_data, sizeof(put_data));
125
126 // increment index of data
127 for (unsigned int j = 0; j < sizeof(put_data); j++) {
128 put_data[j]++;
129 }
130 }
131}
132
134 fram_status status = FRAM_OK;
135
136 // starting values
137 uint8_t data[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
138
139 const int niters = (fifo_size() / (sizeof(data) + 1));
140
141 // write 100 times, therefore 1100 bytes (data + len)
142 for (int i = 0; i < niters; i++) {
143 status = fifo_put(data, sizeof(data));
144 TEST_ASSERT_EQUAL(FRAM_OK, status);
145
146 // increment data
147 for (unsigned int j = 0; j < sizeof(data); j++) {
148 data[j] += sizeof(data);
149 }
150 }
151
152 // try writing data
153 status = fifo_put(data, sizeof(data));
154 TEST_ASSERT_EQUAL(FRAM_BUFFER_FULL, status);
155 // reset data
156 for (unsigned int i = 0; i < sizeof(data); i++) {
157 data[i] = i;
158 }
159
160 uint8_t get_data[sizeof(data)] = {0};
161
162 for (int i = 0; i < niters; i++) {
163 uint8_t get_data_len = 0;
164 status = fifo_get(get_data, &get_data_len);
165
166 TEST_ASSERT_EQUAL(FRAM_OK, status);
167 TEST_ASSERT_EQUAL_INT(sizeof(data), get_data_len);
168 TEST_ASSERT_EQUAL_UINT8_ARRAY(data, get_data, sizeof(data));
169
170 // increment data
171 for (unsigned int j = 0; j < sizeof(data); j++) {
172 data[j] += sizeof(data);
173 }
174 }
175}
176
178 // Assuming that the buffer length is initially 0
179 TEST_ASSERT_EQUAL(0, fifo_buffer_len());
180
181 // Put some data into the buffer
182 uint8_t data[] = {1, 2, 3, 4, 5};
183 fifo_put(data, sizeof(data));
184
185 // Now the buffer length should be equal to the size of the data
186 TEST_ASSERT_EQUAL(1, fifo_buffer_len());
187}
188
190 // Put some data into the buffer
191 uint8_t data[] = {1, 2, 3, 4, 5};
192 fram_status status = fifo_put(data, sizeof(data));
193 TEST_ASSERT_EQUAL(FRAM_OK, status);
194
195 TEST_ASSERT_EQUAL(1, fifo_buffer_len());
196
197 // Clear the buffer
199
200 // Now the buffer length should be 0
201 TEST_ASSERT_EQUAL(0, fifo_buffer_len());
202}
203
205 // checks for errors when read addr > write addr
206 fram_status status;
207
208 // Set the memory after the FIFO buffer to a unique character to check OOB
209 // memory write
210 uint8_t oob_before = 0;
211 uint8_t oob_after = 0;
212 const uint8_t oob_check = 0xFF;
213 status = fram_read(FRAM_BUFFER_END + 1, 1,
214 &oob_before); // to be restored afterwards
215 TEST_ASSERT_EQUAL(FRAM_OK, status);
216 status = fram_write(FRAM_BUFFER_END + 1, &oob_check, 1);
217 TEST_ASSERT_EQUAL(FRAM_OK, status);
218
219 // write block size to handle length
220 // block_size+1 must not be a factor of the FIFO's space
221 uint8_t block_size = 7;
222 while (fifo_size() % (block_size + 1) == 0) {
223 block_size += 1;
224 }
225
226 // oob_check is reserved as a special character for determining
227 // if data was written out of bounds
228 TEST_ASSERT_NOT_EQUAL(block_size, oob_check);
229 TEST_ASSERT_NOT_EQUAL(block_size + 1, oob_check);
230
231 uint8_t junk_data[256];
232 for (int i = 0; i < 256; i++) {
233 junk_data[i] = i;
234 }
235
236 uint8_t buffer[256];
237 uint8_t buffer_length;
238
239 // move write to before the end of physical memory in FRAM
240 // if the assigned FRAM memory is [0, 1769], then a block_size of 16 (+1
241 // length byte) means that the 104th block starts at 1768 and must wrap
242 // around. read 0, write 1768
243 status = FRAM_OK;
244 while (status != FRAM_BUFFER_FULL) {
245 TEST_ASSERT_EQUAL(FRAM_OK, status);
246 status = fifo_put(junk_data, block_size);
247 }
248
249 // advance read all the way to the end to make room for the wraparound
250 // read 1768, write 1768
251 while (fifo_buffer_len() != 0) {
252 status = fifo_get(buffer, &buffer_length);
253 TEST_ASSERT_EQUAL(FRAM_OK, status);
254 }
255
256 // write one block for the wraparound
257 // read 1768, write 1768 + 17 = 14
258 // [{68} 69 70 0 1 2 3 4 5 6 7 8 9 10 11 12 13] 14
259 status = fifo_put(junk_data, block_size);
260 TEST_ASSERT_EQUAL(FRAM_OK, status);
261
262 // observe the wraparound
263 // read 14, write 14
264 status = fifo_get(buffer, &buffer_length);
265 TEST_ASSERT_EQUAL(FRAM_OK, status);
266
267 // test that the data was successfully retrieved across the wraparound
268 TEST_ASSERT_EQUAL_UINT8_ARRAY(buffer, junk_data, block_size);
269
270 // test that no data was written out of bounds
271 status = fram_read(FRAM_BUFFER_END + 1, 1, &oob_after);
272 TEST_ASSERT_EQUAL(FRAM_OK, status);
273 TEST_ASSERT_EQUAL(oob_after, oob_check);
274 status = fram_write(FRAM_BUFFER_END + 1, &oob_before, 1); // restore
275 TEST_ASSERT_EQUAL(FRAM_OK, status);
276
277 // status = fifo_put(zeros, block_size);
278 // TEST_ASSERT_EQUAL(FRAM_BUFFER_FULL, status);
279}
280
282 // Clear the buffer and check initial state
284 TEST_ASSERT_EQUAL(FRAM_OK, status);
285
286 // Add some data
287 const uint8_t test_data[] = {0x11, 0x22, 0x33};
288 for (int i = 0; i < 10; i++) {
289 // note that fifo_put() calls fram_save_buffer_state()
290 status = fifo_put(test_data, sizeof(test_data));
291 TEST_ASSERT_EQUAL(FRAM_OK, status);
292 }
293
294 // Load the new buffer state
295 status = fifo_load_buffer_state();
296 TEST_ASSERT_EQUAL(FRAM_OK, status);
297
298 //
299 uint8_t retrieved_data[sizeof(test_data)];
300 uint8_t retrieved_len;
301 for (int i = 0; i < 10; i++) {
302 status = fifo_get(retrieved_data, &retrieved_len);
303 TEST_ASSERT_EQUAL(FRAM_OK, status);
304 TEST_ASSERT_EQUAL_UINT8_ARRAY(test_data, retrieved_data, sizeof(test_data));
305 }
306}
307
308void test_fifo_peek(void) {
309 // Clear the buffer and check initial state
311 TEST_ASSERT_EQUAL(FRAM_OK, status);
312
313 // Add some data
314 const uint8_t test_data[] = {0xDE, 0xAD, 0xBE, 0xEF};
315 for (int i = 0; i < 5; i++) {
316 status = fifo_put(test_data, sizeof(test_data));
317 TEST_ASSERT_EQUAL(FRAM_OK, status);
318 }
319
320 // Peek at each measurement without removing them
321 uint8_t retrieved_data[sizeof(test_data)];
322 uint8_t retrieved_len;
323 for (int i = 0; i < 5; i++) {
324 status = fifo_peek(i, retrieved_data, &retrieved_len);
325 TEST_ASSERT_EQUAL(FRAM_OK, status);
326 TEST_ASSERT_EQUAL_UINT8_ARRAY(test_data, retrieved_data, sizeof(test_data));
327 }
328
329 // Ensure buffer length remains unchanged
330 TEST_ASSERT_EQUAL(5, fifo_buffer_len());
331}
332
333void test_fifo_drop(void) {
334 // Clear the buffer and check initial state
336 TEST_ASSERT_EQUAL(FRAM_OK, status);
337
338 // Add some data
339 const uint8_t test_data[] = {0xAA, 0xBB, 0xCC};
340 for (int i = 0; i < 5; i++) {
341 status = fifo_put(test_data, sizeof(test_data));
342 TEST_ASSERT_EQUAL(FRAM_OK, status);
343 }
344
345 // Drop two measurements
346 for (int i = 0; i < 2; i++) {
347 status = fifo_drop();
348 TEST_ASSERT_EQUAL(FRAM_OK, status);
349 }
350
351 // Check remaining buffer length
352 TEST_ASSERT_EQUAL(3, fifo_buffer_len());
353
354 // Retrieve remaining data and verify correctness
355 uint8_t retrieved_data[sizeof(test_data)];
356 uint8_t retrieved_len;
357 for (int i = 0; i < 3; i++) {
358 status = fifo_get(retrieved_data, &retrieved_len);
359 TEST_ASSERT_EQUAL(FRAM_OK, status);
360 TEST_ASSERT_EQUAL_UINT8_ARRAY(test_data, retrieved_data, sizeof(test_data));
361 }
362
363 // Attempt to drop from an empty buffer
364 status = fifo_drop();
365 TEST_ASSERT_EQUAL(FRAM_BUFFER_EMPTY, status);
366}
367
368int main(void) {
369 UNITY_BEGIN();
370
371 RUN_TEST(test_fifo_buffer_clear);
372 RUN_TEST(test_fifo_buffer_len);
373
374 // test buffer functions
375 RUN_TEST(test_fifo_put_ValidData);
376 RUN_TEST(test_fifo_put_Sequential);
378 RUN_TEST(test_fifo_BufferEmpty);
379 RUN_TEST(test_fifo_ValidData);
380 RUN_TEST(test_fifo_Sequential);
382 RUN_TEST(test_fifo_wraparound);
384 RUN_TEST(test_fifo_peek);
385 RUN_TEST(test_fifo_drop);
386
387 return UNITY_END();
388}
int main()
Definition main.c:4
const size_t data_len
const char data[]
uint8_t buffer[32]
uint8_t i
Circular buffer as FIFO data structure for buffering measurements.
fram_status fifo_get(uint8_t *data, uint8_t *len)
Reads a measurement from the queue.
Definition fifo.c:102
#define FRAM_BUFFER_END
Definition fifo.h:68
int fifo_size(void)
Gets the total number of bytes in the buffer.
Definition fifo.c:346
uint16_t fifo_buffer_len(void)
Get the current number of measurements stored in the buffer.
Definition fifo.c:233
fram_status fifo_buffer_clear(void)
Clears the buffer.
Definition fifo.c:235
fram_status fifo_put(const uint8_t *data, const uint8_t num_bytes)
Puts a measurement into the circular buffer.
Definition fifo.c:55
fram_status fifo_drop(void)
Drops the next measurement in the buffer.
Definition fifo.c:210
fram_status fifo_peek(size_t idx, uint8_t *data, uint8_t *len)
Peeks at the next measurement in the buffer without removing it.
Definition fifo.c:147
fram_status fifo_load_buffer_state(void)
Loads the buffer state (read address, write address, and buffer length) from FRAM.
Definition fifo.c:290
fram_status fram_write(fram_addr addr, const uint8_t *data, size_t len)
Write bytes at the given address.
Definition fram.c:20
fram_status
Definition fram.h:32
fram_status fram_read(fram_addr addr, size_t len, uint8_t *data)
Read bytes at the given address.
Definition fram.c:28
@ FRAM_BUFFER_EMPTY
Definition fram.h:37
@ FRAM_OK
Definition fram.h:33
@ FRAM_BUFFER_FULL
Definition fram.h:36
void test_fifo_wraparound(void)
Definition main.c:204
void test_fifo_put_Sequential(void)
Definition main.c:37
void test_fifo_buffer_len(void)
Definition main.c:177
void test_fifo_peek(void)
Definition main.c:308
void test_fifo_drop(void)
Definition main.c:333
void test_fifo_put_ValidData(void)
Definition main.c:28
void test_fifo_BufferEmpty(void)
Definition main.c:71
void test_fifo_load_buffer_state(void)
Definition main.c:281
void setUp(void)
Definition main.c:24
void tearDown(void)
Definition main.c:26
void test_fifo_ValidData(void)
Definition main.c:80
void test_fifo_buffer_clear(void)
Definition main.c:189
void test_fifo_Sequential_BufferFull(void)
Definition main.c:133
void test_fifo_Sequential(void)
Definition main.c:93
void test_fifo_put_Sequential_BufferFull(void)
Definition main.c:55