1#include "main-header.h"
7static byte *destadr = 0;
8int config_is_ram_valid();
11static int flash_read(
void *buf,
const int size) {
14 printf(
"[config] Config at %p\r\n", destadr);
17 for (i = 0; i < size; i++) {
25int flash_config_init() {
28 destadr = get_flash_config_addr();
31 printf(
"[config] Reading config from flash\r\n");
32 flash_read(config_get_ram_struct(),
sizeof(
_cnw_config));
34 if (config_is_ram_valid()) {
35 printf(
"[config] Config in FLASH is valid\r\n");
37 printf(
"[config] Config in FLASH is not valid!\r\n");
38 cfg = config_get_ram_struct();
41 memset(&cfg->apcredentials, 0, ESP_AP_NAME_SIZE);
42 cfg->wifidetected = 0;
49 printf(
"[config] Done\r\n");
61 if ((res = mculib_flash_unlock(MAIN_MCULIBHANDLE))) {
62 printf(
"[config] Unlock flash failed: %i\r\n", res);
63 mculib_flash_lock(MAIN_MCULIBHANDLE);
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);
71 int fw = mculib_flash_write(MAIN_MCULIBHANDLE, config_get_ram_struct(), destadr, size);
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");
82 config_get_ram_struct()->crc = config_calc_crc(config_get_ram_struct());
83 config_get_ram_struct()->size =
sizeof(
_cnw_config);
89 a = (uint8_t *)config_get_ram_struct();
90 b = (uint8_t *)destadr;
92 uint8_t mustwrite = 0;
94 for (i = 0; i < config_get_ram_struct()->size; i++) {
100 if (mustwrite == 0) {
101 printf(
"[config] no need to write\r\n");
104 printf(
"[config] Saving config to flash\r\n");
109int config_is_ram_valid() {
112 }
else if (valid == 2) {
115 if (config_check_crc(&config)) {
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;
133 for (i = 0; i < crcsize; i++) {
136 crc = 0xFFFFFFFF - crc;
143 uint32_t i = config_calc_crc(cfg);
146 if ((cfg->crc & 0xFFFFFFFF) == i) {
153void config_set_wifidetected(
int b) {
156 config.wifidetected &= ~2;
158 config.wifidetected |= 2;
162void config_set_wifidisable(
int b) {
165 config_set_flag(CONFIG_FLAGS_WIFI_DISABLE, 1);
167 config_set_flag(CONFIG_FLAGS_WIFI_DISABLE, 0);
170int config_get_wifidisable() {
171 return config_get_flag(CONFIG_FLAGS_WIFI_DISABLE);
174void config_set_flashmode(
byte b) {
176 config.flashmode = b;
179void config_set_ti1101disable(
int b) {
180 config_set_flag(CONFIG_FLAGS_RADIODISABLE, b ? 1 : 0);
182int config_get_ti1101disable() {
183 return config_get_flag(CONFIG_FLAGS_RADIODISABLE);
188 if (config_is_ram_valid()) {
189 config_get_ram_struct()->crc++;
194 printf(
"[config] Config erase from flash failed (%i)!\r\n", r);
198int config_get_sensors_enabled() {
199 return (config_get_flag(CONFIG_FLAGS_SENSORS_DISABLED) == 0) ? 1 : 0;
201void config_set_sensors_enabled(
int b) {
202 config_set_flag(CONFIG_FLAGS_SENSORS_DISABLED, (b == 0) ? 1 : 0);
205const char *config_get_cloud_token() {
206 if (!config_is_ram_valid()) {
209 return config.cloudtoken;
211void config_set_cloud_token(
const char *token) {
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");
223 config.cloudtoken[i] = 0;
226const char *config_get_sensor_server() {
227 if (!config_is_ram_valid()) {
230 return config.sensorserver;
232void config_set_sensor_server(
const char *name) {
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");
246const char *config_get_cloud_server() {
247 if (!config_is_ram_valid()) {
250 return config.cloudserver;
252void config_set_cloud_server(
const char *name) {
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");
268long config_get_log_node() {
269 if (!config_is_ram_valid()) {
272 return config.lognode;
274void config_set_log_node(
long nodeid) {
276 config.lognode = nodeid;
278byte config_get_flag(
int flag) {
279 if (!config_is_ram_valid()) {
285 r = config.flags2 & (1 << (flag - 8));
287 r = config.flags1 & (1 << flag);
292void config_set_flag(
int flag,
byte value) {
295 byte *f = &config.flags1;
296 char const *s =
"flags1";
305 IPLOG(
"[config] Setting flag %i in %s to %i\r\n", flag, s, value);
307 r = 0xFF & ~(1 << flag);
310 *f = *f | (1 << flag);
312 IPLOG(
"[config] New config flag: %x\r\n", *f);
318uint32_t config_get_flags() {
319 uint32_t res = config.flags1;
321 res = res | (((uint32_t)config.flags2) << 8);
325void config_flag_line(
const char *line) {
326 printf(
"[config] got flag line: %s\r\n", line);
331 for (i = 0; i < strlen(line); i++) {
332 if (line[i] ==
' ') {
333 b = atoi(&line[i + 1]);
336 config_set_flag(a, b);
339void config_set_moduleid(uint64_t modid) {
340 if (config.moduleid == modid) {
343 config.moduleid = modid;
346uint64_t config_get_moduleid() {
347 if (!config_is_ram_valid()) {
351 return config.moduleid;
353void config_set_userapp(uint32_t userapp_base) {
354 if (config.userapp == userapp_base) {
357 config.userapp = userapp_base;
361 if (!config_is_ram_valid()) {
364 return (
void *)config.userapp;
366uint32_t get_user_config_flags() {
367 if (!config_is_ram_valid()) {
370 return config.userflags;
372int get_user_config_flag(
int flag) {
373 if ((flag < 0) || (flag > 31)) {
376 if (!config_is_ram_valid()) {
379 if (config.userflags & (1 << flag)) {
384void set_user_config_flag(
int flag,
int value) {
385 if ((flag < 0) || (flag > 31)) {
389 config.userflags |= (1 << flag);
391 config.userflags &= ~(1 << flag);
395void set_user_config_flags(uint32_t flags) {
396 config.userflags = flags;
400uint32_t config_get_clock_mul() {
401 if (!config_is_ram_valid()) {
404 return config.clock_mul;
406uint32_t config_get_clock_div() {
407 if (!config_is_ram_valid()) {
410 return config.clock_div;
412void config_set_clock_mul(uint32_t mul) {
413 config.clock_mul = mul;
416void config_set_clock_div(uint32_t div) {
417 config.clock_div = div;
420void config_set_timezone(uint32_t tz) {
424uint32_t config_get_timezone() {
425 if (!config_is_ram_valid()) {
430void config_set_powermode_flags(uint32_t new_flags) {
431 config.powermodeflags = new_flags;
434uint32_t config_get_powermode_flags() {
435 if (!config_is_ram_valid()) {
438 return config.powermodeflags;
440int config_get_powermode_flag(
int b) {
441 if (!config_is_ram_valid()) {
444 if (config.powermodeflags & (1 << b)) {
449uint16_t config_get_radiooff_duration() {
450 if (!config_is_ram_valid()) {
453 return config.radiooff_duration;
455void config_set_radiooff_duration(uint16_t dur) {
456 config.radiooff_duration = dur;
458uint16_t config_get_wifioff_duration() {
459 if (!config_is_ram_valid()) {
462 return config.wifioff_duration;
464void config_set_wifioff_duration(uint16_t wifioff) {
465 config.wifioff_duration = wifioff;
this must be implemented by the userapp, stored in flash at the beginning of the file (offset 0)