Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / config / config_streamer_file.c
blob379155c22e11527213bcc9af0167089303810dca
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #include <string.h>
19 #include <dirent.h>
20 #include "platform.h"
21 #include "drivers/system.h"
22 #include "config/config_streamer.h"
23 #include "common/utils.h"
25 #if defined(CONFIG_IN_FILE)
27 #include <stdio.h>
28 #include <errno.h>
31 #define FLASH_PAGE_SIZE (0x400)
33 static FILE *eepromFd = NULL;
34 static bool streamerLocked = true;
35 static char eepromPath[260] = EEPROM_FILENAME;
37 bool configFileSetPath(char* path)
39 if(!path || strlen(path) > 260) {
40 return false;
43 strcpy(eepromPath, path);
44 return true;
47 void config_streamer_impl_unlock(void)
49 if (eepromFd != NULL) {
50 fprintf(stderr, "[EEPROM] Unable to load %s\n", eepromPath);
51 return;
54 // open or create
55 eepromFd = fopen(eepromPath,"r+");
56 if (eepromFd != NULL) {
57 // obtain file size:
58 fseek(eepromFd , 0 , SEEK_END);
59 size_t size = ftell(eepromFd);
60 rewind(eepromFd);
62 size_t n = fread(eepromData, 1, sizeof(eepromData), eepromFd);
63 if (n == size) {
64 fprintf(stderr,"[EEPROM] Loaded '%s' (%ld of %ld bytes)\n", eepromPath, size, sizeof(eepromData));
65 streamerLocked = false;
66 } else {
67 fprintf(stderr, "[EEPROM] Failed to load '%s'\n", eepromPath);
69 } else {
70 printf("[EEPROM] Created '%s', size = %ld\n", eepromPath, sizeof(eepromData));
71 streamerLocked = false;
72 if ((eepromFd = fopen(eepromPath, "w+")) == NULL) {
73 fprintf(stderr, "[EEPROM] Failed to create '%s'\n", eepromPath);
74 streamerLocked = true;
76 if (fwrite(eepromData, sizeof(eepromData), 1, eepromFd) != 1) {
77 fprintf(stderr, "[EEPROM] Write failed: %s\n", strerror(errno));
78 streamerLocked = true;
83 void config_streamer_impl_lock(void)
85 // flush & close
86 if (eepromFd != NULL) {
87 fseek(eepromFd, 0, SEEK_SET);
88 fwrite(eepromData, 1, sizeof(eepromData), eepromFd);
89 fclose(eepromFd);
90 eepromFd = NULL;
91 fprintf(stderr, "[EEPROM] Saved '%s'\n", eepromPath);
92 streamerLocked = false;
93 } else {
94 fprintf(stderr, "[EEPROM] Unlock error\n");
98 int config_streamer_impl_write_word(config_streamer_t *c, config_streamer_buffer_align_type_t *buffer)
100 if (streamerLocked) {
101 return -1;
104 if ((c->address >= (uintptr_t)eepromData) && (c->address < (uintptr_t)ARRAYEND(eepromData))) {
105 *((uint32_t*)c->address) = *buffer;
106 fprintf(stderr, "[EEPROM] Program word %p = %08x\n", (void*)c->address, *((uint32_t*)c->address));
107 } else {
108 fprintf(stderr, "[EEPROM] Program word %p out of range!\n", (void*)c->address);
111 c->address += CONFIG_STREAMER_BUFFER_SIZE;
112 return 0;
115 #endif