SingingCat 0
application
config.c
1#include "main-header.h"
2// #include "bare-metal.h"
3
4// this is my in-ram copy
5struct cnw_config config;
6static byte valid;
7static byte *destadr = 0;
8int config_is_ram_valid();
9//extern void *app_flash_adr; // its the ADRESS of the label !!
10
11static int flash_read(void *buf, const int size) {
12 byte *ram = buf;
13
14 printf("[config] Config at %p\r\n", destadr);
15 int i;
16
17 for (i = 0; i < size; i++) {
18 ram[i] = destadr[i];
19 }
20 return 1;
21}
25int flash_config_init() {
26 struct cnw_config *cfg;
27
28 destadr = get_flash_config_addr();
29
30 valid = 0;
31 printf("[config] Reading config from flash\r\n");
32 flash_read(config_get_ram_struct(), sizeof(_cnw_config));
33
34 if (config_is_ram_valid()) {
35 printf("[config] Config in FLASH is valid\r\n");
36 } else {
37 printf("[config] Config in FLASH is not valid!\r\n");
38 cfg = config_get_ram_struct();
39 memset(cfg, 0, sizeof(_cnw_config));
40 cfg->size = sizeof(_cnw_config);
41 memset(&cfg->apcredentials, 0, ESP_AP_NAME_SIZE);
42 cfg->wifidetected = 0;
43 cfg->flags1 = 0;
44 cfg->flags2 = 0;
45 cfg->nodeid = 0;
46 config_commit();
47 return 1;
48 }
49 printf("[config] Done\r\n");
50 return 0;
51}
52
53struct cnw_config *config_get_ram_struct() {
54 return &config;
55}
56
57static int toflash() {
58 int size = sizeof(_cnw_config);
59 int res;
60
61 if ((res = mculib_flash_unlock(MAIN_MCULIBHANDLE))) {
62 printf("[config] Unlock flash failed: %i\r\n", res);
63 mculib_flash_lock(MAIN_MCULIBHANDLE);
64 return res;
65 }
66 if ((res = mculib_flash_erase(MAIN_MCULIBHANDLE, destadr, size))) {
67 printf("[config] Save config failed: %i\r\n", res);
68 mculib_flash_lock(MAIN_MCULIBHANDLE);
69 return res;
70 }
71 int fw = mculib_flash_write(MAIN_MCULIBHANDLE, config_get_ram_struct(), destadr, size);
72
73 mculib_flash_lock(MAIN_MCULIBHANDLE);
74 printf("[config] Saved %i bytes to flash. Flash result: %i\r\n", size, fw);
75 if (!config_check_crc((struct cnw_config *)destadr)) {
76 printf("[configConfig in flash is STILL not valid!\r\n");
77 }
78 return 0;
79}
80int config_commit() {
81 valid = 0;
82 config_get_ram_struct()->crc = config_calc_crc(config_get_ram_struct());
83 config_get_ram_struct()->size = sizeof(_cnw_config);
84
85 // check if in_ram is identical to in_flash, if so, we do not need to write
86 uint8_t *a;
87 uint8_t *b;
88
89 a = (uint8_t *)config_get_ram_struct();
90 b = (uint8_t *)destadr;
91 int i;
92 uint8_t mustwrite = 0;
93
94 for (i = 0; i < config_get_ram_struct()->size; i++) {
95 if (a[i] != b[i]) {
96 mustwrite = 1;
97 break;
98 }
99 }
100 if (mustwrite == 0) {
101 printf("[config] no need to write\r\n");
102 return 0;
103 }
104 printf("[config] Saving config to flash\r\n");
105 return toflash();
106}
107
108// return >0 if ok
109int config_is_ram_valid() {
110 if (valid == 1) {
111 return 1;
112 } else if (valid == 2) {
113 return 0;
114 }
115 if (config_check_crc(&config)) {
116 //printf("[configConfig in ram is valid!\r\n");
117 valid = 1;
118 return 1;
119 }
120 //printf("[configConfig in ram is NOT valid!\r\n");
121 valid = 2;
122 return 0;
123}
124
125uint32_t config_calc_crc(struct cnw_config *cfg) {
126 int crcsize = sizeof(config) - (((void *)&config.crc_start) - ((void *)&config));
127 uint8_t *buf = (void *)&cfg->crc_start;
128
129 //printf("crc size: %i\r\n", crcsize);
130 int i;
131 uint32_t crc = 0;
132
133 for (i = 0; i < crcsize; i++) {
134 crc = crc + buf[i];
135 }
136 crc = 0xFFFFFFFF - crc;
137 return crc;
138}
142int config_check_crc(struct cnw_config *cfg) {
143 uint32_t i = config_calc_crc(cfg);
144
145 //printf("Config: crc value: %i, calculated: %i\r\n",cfg->crc,i);
146 if ((cfg->crc & 0xFFFFFFFF) == i) {
147 return 1;
148 }
149
150 return 0;
151}
152
153void config_set_wifidetected(int b) {
154 valid = 0;
155 if (b) {
156 config.wifidetected &= ~2;
157 } else {
158 config.wifidetected |= 2;
159 }
160}
161
162void config_set_wifidisable(int b) {
163 valid = 0;
164 if (b) {
165 config_set_flag(CONFIG_FLAGS_WIFI_DISABLE, 1);
166 } else {
167 config_set_flag(CONFIG_FLAGS_WIFI_DISABLE, 0);
168 }
169}
170int config_get_wifidisable() {
171 return config_get_flag(CONFIG_FLAGS_WIFI_DISABLE);
172}
173
174void config_set_flashmode(byte b) {
175 valid = 0;
176 config.flashmode = b;
177}
178
179void config_set_ti1101disable(int b) {
180 config_set_flag(CONFIG_FLAGS_RADIODISABLE, b ? 1 : 0);
181}
182int config_get_ti1101disable() {
183 return config_get_flag(CONFIG_FLAGS_RADIODISABLE);
184}
185
186void config_erase() {
187 valid = 0;
188 if (config_is_ram_valid()) {
189 config_get_ram_struct()->crc++; // force invalidate
190 }
191 int r = toflash();
192
193 if (r != 0) {
194 printf("[config] Config erase from flash failed (%i)!\r\n", r);
195 }
196}
197
198int config_get_sensors_enabled() {
199 return (config_get_flag(CONFIG_FLAGS_SENSORS_DISABLED) == 0) ? 1 : 0;
200}
201void config_set_sensors_enabled(int b) {
202 config_set_flag(CONFIG_FLAGS_SENSORS_DISABLED, (b == 0) ? 1 : 0);
203}
204
205const char *config_get_cloud_token() {
206 if (!config_is_ram_valid()) {
207 return "";
208 }
209 return config.cloudtoken;
210}
211void config_set_cloud_token(const char *token) {
212 valid = 0;
213 int i;
214
215 for (i = 0; i < strlen(token); i++) {
216 config.cloudtoken[i] = token[i];
217 if (i >= (sizeof(config.cloudtoken) - 1)) {
218 config.cloudtoken[i] = 0;
219 printf("[config] CLOUDTOKEN TOO LARGE!\r\n");
220 return;
221 }
222 }
223 config.cloudtoken[i] = 0;
224}
225
226const char *config_get_sensor_server() {
227 if (!config_is_ram_valid()) {
228 return "";
229 }
230 return config.sensorserver;
231}
232void config_set_sensor_server(const char *name) {
233 valid = 0;
234 int i;
235
236 for (i = 0; i < strlen(name); i++) {
237 config.sensorserver[i] = name[i];
238 config.sensorserver[i + 1] = 0;
239 if (i >= (sizeof(config.sensorserver) - 1)) {
240 config.sensorserver[i] = 0;
241 printf("[config] SENSORSERVERNAME TOO LARGE!\r\n");
242 return;
243 }
244 }
245}
246const char *config_get_cloud_server() {
247 if (!config_is_ram_valid()) {
248 return NULL;
249 }
250 return config.cloudserver;
251}
252void config_set_cloud_server(const char *name) {
253 valid = 0;
254 int i;
255
256 for (i = 0; i < strlen(name); i++) {
257 config.cloudserver[i] = name[i];
258 config.cloudserver[i + 1] = 0;
259 if (i >= (sizeof(config.cloudserver) - 1)) {
260 config.cloudserver[i] = 0;
261 printf("[config] CLOUDSERVERNAME TOO LARGE!\r\n");
262 return;
263 }
264 }
265 config_commit();
266}
267
268long config_get_log_node() {
269 if (!config_is_ram_valid()) {
270 return 0;
271 }
272 return config.lognode;
273}
274void config_set_log_node(long nodeid) {
275 valid = 0;
276 config.lognode = nodeid;
277}
278byte config_get_flag(int flag) {
279 if (!config_is_ram_valid()) {
280 return 0;
281 }
282 int r = 0;
283
284 if (flag > 7) {
285 r = config.flags2 & (1 << (flag - 8));
286 } else {
287 r = config.flags1 & (1 << flag);
288 }
289 return r ? 1 : 0;
290}
291
292void config_set_flag(int flag, byte value) {
293 int r;
294
295 byte *f = &config.flags1;
296 char const *s = "flags1";
297
298 if (flag > 7) {
299 s = "flags2";
300 f = &config.flags2;
301 flag = flag - 8;
302 }
303 byte z = *f;
304
305 IPLOG("[config] Setting flag %i in %s to %i\r\n", flag, s, value);
306 if (value == 0) {
307 r = 0xFF & ~(1 << flag);
308 *f = *f & r;
309 } else {
310 *f = *f | (1 << flag);
311 }
312 IPLOG("[config] New config flag: %x\r\n", *f);
313 if (z != *f) {
314 valid = 0;
315 }
316}
317
318uint32_t config_get_flags() {
319 uint32_t res = config.flags1;
320
321 res = res | (((uint32_t)config.flags2) << 8);
322 return res;
323}
324
325void config_flag_line(const char *line) {
326 printf("[config] got flag line: %s\r\n", line);
327 int i;
328 int a = atoi(line);
329 int b = 0;
330
331 for (i = 0; i < strlen(line); i++) {
332 if (line[i] == ' ') {
333 b = atoi(&line[i + 1]);
334 }
335 }
336 config_set_flag(a, b);
337 config_commit();
338}
339void config_set_moduleid(uint64_t modid) {
340 if (config.moduleid == modid) {
341 return;
342 }
343 config.moduleid = modid;
344 config_commit();
345}
346uint64_t config_get_moduleid() {
347 if (!config_is_ram_valid()) {
348 return 0;
349 }
350
351 return config.moduleid;
352}
353void config_set_userapp(uint32_t userapp_base) {
354 if (config.userapp == userapp_base) {
355 return;
356 }
357 config.userapp = userapp_base;
358 config_commit();
359}
360struct userapp_info *config_get_userapp() {
361 if (!config_is_ram_valid()) {
362 return 0;
363 }
364 return (void *)config.userapp;
365}
366uint32_t get_user_config_flags() {
367 if (!config_is_ram_valid()) {
368 return 0;
369 }
370 return config.userflags;
371}
372int get_user_config_flag(int flag) {
373 if ((flag < 0) || (flag > 31)) {
374 return 0;
375 }
376 if (!config_is_ram_valid()) {
377 return 0;
378 }
379 if (config.userflags & (1 << flag)) {
380 return 1;
381 }
382 return 0;
383}
384void set_user_config_flag(int flag, int value) {
385 if ((flag < 0) || (flag > 31)) {
386 return;
387 }
388 if (value) {
389 config.userflags |= (1 << flag);
390 } else {
391 config.userflags &= ~(1 << flag);
392 }
393 config_commit();
394}
395void set_user_config_flags(uint32_t flags) {
396 config.userflags = flags;
397 config_commit();
398}
399
400uint32_t config_get_clock_mul() {
401 if (!config_is_ram_valid()) {
402 return 0;
403 }
404 return config.clock_mul;
405}
406uint32_t config_get_clock_div() {
407 if (!config_is_ram_valid()) {
408 return 0;
409 }
410 return config.clock_div;
411}
412void config_set_clock_mul(uint32_t mul) {
413 config.clock_mul = mul;
414 config_commit();
415}
416void config_set_clock_div(uint32_t div) {
417 config.clock_div = div;
418 config_commit();
419}
420void config_set_timezone(uint32_t tz) {
421 config.tz = tz;
422 config_commit();
423}
424uint32_t config_get_timezone() {
425 if (!config_is_ram_valid()) {
426 return 0;
427 }
428 return config.tz;
429}
430void config_set_powermode_flags(uint32_t new_flags) {
431 config.powermodeflags = new_flags;
432 config_commit();
433}
434uint32_t config_get_powermode_flags() {
435 if (!config_is_ram_valid()) {
436 return 0;
437 }
438 return config.powermodeflags;
439}
440int config_get_powermode_flag(int b) {
441 if (!config_is_ram_valid()) {
442 return 0;
443 }
444 if (config.powermodeflags & (1 << b)) {
445 return 1;
446 }
447 return 0;
448}
449uint16_t config_get_radiooff_duration() {
450 if (!config_is_ram_valid()) {
451 return 0;
452 }
453 return config.radiooff_duration;
454}
455void config_set_radiooff_duration(uint16_t dur) {
456 config.radiooff_duration = dur;
457}
458uint16_t config_get_wifioff_duration() {
459 if (!config_is_ram_valid()) {
460 return 0;
461 }
462 return config.wifioff_duration;
463}
464void config_set_wifioff_duration(uint16_t wifioff) {
465 config.wifioff_duration = wifioff;
466}
this must be implemented by the userapp, stored in flash at the beginning of the file (offset 0)