update credits
[librepilot.git] / flight / tests / logfs / pios_flash_ut.c
blob1c42a31fd95c7d8137d31e2b230e71de7cd59659
1 #include <stdlib.h> /* abort */
2 #include <stdio.h> /* fopen/fread/fwrite/fseek */
3 #include <assert.h> /* assert */
4 #include <string.h> /* memset */
5 #include <unistd.h>
6 #include <stdbool.h>
7 #include "pios_flash_ut_priv.h"
9 enum flash_ut_magic {
10 FLASH_UT_MAGIC = 0x321dabc1,
13 struct flash_ut_dev {
14 enum flash_ut_magic magic;
15 const struct pios_flash_ut_cfg *cfg;
16 bool transaction_in_progress;
17 FILE *flash_file;
20 static struct flash_ut_dev *PIOS_Flash_UT_Alloc(void)
22 struct flash_ut_dev *flash_dev = malloc(sizeof(struct flash_ut_dev));
24 flash_dev->magic = FLASH_UT_MAGIC;
26 return flash_dev;
29 int32_t PIOS_Flash_UT_Init(uintptr_t *flash_id, const struct pios_flash_ut_cfg *cfg)
31 /* Check inputs */
32 assert(flash_id);
33 assert(cfg);
34 assert(cfg->size_of_flash);
35 assert(cfg->size_of_sector);
36 assert((cfg->size_of_flash % cfg->size_of_sector) == 0);
38 struct flash_ut_dev *flash_dev = PIOS_Flash_UT_Alloc();
39 assert(flash_dev);
41 flash_dev->cfg = cfg;
42 flash_dev->transaction_in_progress = false;
44 flash_dev->flash_file = fopen(FLASH_IMAGE_FILE, "rb+");
45 if (flash_dev->flash_file == NULL) {
46 return -1;
49 if (fseek(flash_dev->flash_file, flash_dev->cfg->size_of_flash, SEEK_SET) != 0) {
50 return -2;
53 *flash_id = (uintptr_t)flash_dev;
55 return 0;
58 int32_t PIOS_Flash_UT_Destroy(uintptr_t flash_id)
60 /* Check inputs */
61 assert(flash_id);
62 struct flash_ut_dev *flash_dev = (void *)flash_id;
64 if (flash_dev->flash_file == NULL) {
65 return -1;
68 fclose(flash_dev->flash_file);
70 free(flash_dev);
72 unlink(FLASH_IMAGE_FILE);
74 return 0;
78 /**********************************
80 * Provide a PIOS flash driver API
82 *********************************/
83 #include "pios_flash.h"
85 static int32_t PIOS_Flash_UT_StartTransaction(uintptr_t flash_id)
87 struct flash_ut_dev *flash_dev = (struct flash_ut_dev *)flash_id;
89 assert(!flash_dev->transaction_in_progress);
91 flash_dev->transaction_in_progress = true;
93 return 0;
96 static int32_t PIOS_Flash_UT_EndTransaction(uintptr_t flash_id)
98 struct flash_ut_dev *flash_dev = (struct flash_ut_dev *)flash_id;
100 assert(flash_dev->transaction_in_progress);
102 flash_dev->transaction_in_progress = false;
104 return 0;
107 static int32_t PIOS_Flash_UT_EraseSector(uintptr_t flash_id, uint32_t addr)
109 struct flash_ut_dev *flash_dev = (struct flash_ut_dev *)flash_id;
111 assert(flash_dev->transaction_in_progress);
113 if (fseek(flash_dev->flash_file, addr, SEEK_SET) != 0) {
114 assert(0);
117 unsigned char *buf = malloc(flash_dev->cfg->size_of_sector);
118 assert(buf);
119 memset((void *)buf, 0xFF, flash_dev->cfg->size_of_sector);
121 size_t s;
122 s = fwrite(buf, 1, flash_dev->cfg->size_of_sector, flash_dev->flash_file);
124 assert(s == flash_dev->cfg->size_of_sector);
126 return 0;
129 static int32_t PIOS_Flash_UT_WriteData(uintptr_t flash_id, uint32_t addr, uint8_t *data, uint16_t len)
131 /* Check inputs */
132 assert(data);
134 struct flash_ut_dev *flash_dev = (struct flash_ut_dev *)flash_id;
136 assert(flash_dev->transaction_in_progress);
138 if (fseek(flash_dev->flash_file, addr, SEEK_SET) != 0) {
139 assert(0);
142 size_t s;
143 s = fwrite(data, 1, len, flash_dev->flash_file);
145 assert(s == len);
147 return 0;
150 static int32_t PIOS_Flash_UT_ReadData(uintptr_t flash_id, uint32_t addr, uint8_t *data, uint16_t len)
152 /* Check inputs */
153 assert(data);
155 struct flash_ut_dev *flash_dev = (struct flash_ut_dev *)flash_id;
157 assert(flash_dev->transaction_in_progress);
159 if (fseek(flash_dev->flash_file, addr, SEEK_SET) != 0) {
160 assert(0);
163 size_t s;
164 s = fread(data, 1, len, flash_dev->flash_file);
166 assert(s == len);
168 return 0;
171 /* Provide a flash driver to external drivers */
172 const struct pios_flash_driver pios_ut_flash_driver = {
173 .start_transaction = PIOS_Flash_UT_StartTransaction,
174 .end_transaction = PIOS_Flash_UT_EndTransaction,
175 .erase_sector = PIOS_Flash_UT_EraseSector,
176 .write_data = PIOS_Flash_UT_WriteData,
177 .read_data = PIOS_Flash_UT_ReadData,