main/config.c

changeset 60
07a1a07fdc8c
parent 59
52d9405474e1
child 74
34da2d2b12d5
--- a/main/config.c	Mon Jun 15 12:08:21 2020 +0200
+++ b/main/config.c	Mon Jun 15 13:38:46 2020 +0200
@@ -62,7 +62,7 @@
 
 void write_units() {
     uint8_t     *dst = (uint8_t *)&units;
-    FILE        *f = fopen("/spiffs/etc/units.conf", "w+");
+    FILE        *f = fopen("/spiffs/etc/units.conf", "r+");
 
     if (f == NULL) {
         ESP_LOGE(TAG, "write /spiffs/etc/units.conf failed");
@@ -80,8 +80,9 @@
 
 
 void read_units() {
-    uint8_t     *dst;
+    uint8_t     *dst = (uint8_t *)&units;
     uint8_t     mac_addr[8] = {0};
+    size_t	bytes;
     FILE        *f = fopen("/spiffs/etc/units.conf", "r");
 
     if (f == NULL) {
@@ -89,8 +90,7 @@
 	ESP_LOGE(TAG, "/spiffs/etc/units.conf not found, create new");
 	goto u_error;
     } else {
-        dst = (uint8_t*)&units;
-        size_t bytes = fread(dst, 1, sizeof(units), f);
+        bytes = fread(dst, 1, sizeof(units), f);
         fclose(f);
 	if (bytes != sizeof(units)) {
 	    ESP_LOGE(TAG, "/spiffs/etc/units.conf read %d of %d bytes", bytes, sizeof(units));
@@ -112,6 +112,13 @@
         units[i].pressure_zero = 110;
     }
     f = fopen("/spiffs/etc/units.conf", "w+");
+    bytes = fwrite(dst, 1, sizeof(units), f);
+    fclose(f);
+    if (bytes != sizeof(units)) {
+        ESP_LOGE(TAG, "/spiffs/etc/units.conf written %d/%d bytes", bytes, sizeof(units));
+    } else {
+        ESP_LOGI(TAG, "/spiffs/etc/units.conf written %d bytes", bytes);
+    }
 }
 
 

mercurial