main/config.c

changeset 60
07a1a07fdc8c
parent 59
52d9405474e1
child 74
34da2d2b12d5
equal deleted inserted replaced
59:52d9405474e1 60:07a1a07fdc8c
60 60
61 61
62 62
63 void write_units() { 63 void write_units() {
64 uint8_t *dst = (uint8_t *)&units; 64 uint8_t *dst = (uint8_t *)&units;
65 FILE *f = fopen("/spiffs/etc/units.conf", "w+"); 65 FILE *f = fopen("/spiffs/etc/units.conf", "r+");
66 66
67 if (f == NULL) { 67 if (f == NULL) {
68 ESP_LOGE(TAG, "write /spiffs/etc/units.conf failed"); 68 ESP_LOGE(TAG, "write /spiffs/etc/units.conf failed");
69 return; 69 return;
70 } 70 }
78 } 78 }
79 79
80 80
81 81
82 void read_units() { 82 void read_units() {
83 uint8_t *dst; 83 uint8_t *dst = (uint8_t *)&units;
84 uint8_t mac_addr[8] = {0}; 84 uint8_t mac_addr[8] = {0};
85 size_t bytes;
85 FILE *f = fopen("/spiffs/etc/units.conf", "r"); 86 FILE *f = fopen("/spiffs/etc/units.conf", "r");
86 87
87 if (f == NULL) { 88 if (f == NULL) {
88 // No units yet, create them. 89 // No units yet, create them.
89 ESP_LOGE(TAG, "/spiffs/etc/units.conf not found, create new"); 90 ESP_LOGE(TAG, "/spiffs/etc/units.conf not found, create new");
90 goto u_error; 91 goto u_error;
91 } else { 92 } else {
92 dst = (uint8_t*)&units; 93 bytes = fread(dst, 1, sizeof(units), f);
93 size_t bytes = fread(dst, 1, sizeof(units), f);
94 fclose(f); 94 fclose(f);
95 if (bytes != sizeof(units)) { 95 if (bytes != sizeof(units)) {
96 ESP_LOGE(TAG, "/spiffs/etc/units.conf read %d of %d bytes", bytes, sizeof(units)); 96 ESP_LOGE(TAG, "/spiffs/etc/units.conf read %d of %d bytes", bytes, sizeof(units));
97 goto u_error; 97 goto u_error;
98 } 98 }
110 mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); 110 mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
111 sprintf(units[i].alias, "unit%d", (i + 1) & 3); 111 sprintf(units[i].alias, "unit%d", (i + 1) & 3);
112 units[i].pressure_zero = 110; 112 units[i].pressure_zero = 110;
113 } 113 }
114 f = fopen("/spiffs/etc/units.conf", "w+"); 114 f = fopen("/spiffs/etc/units.conf", "w+");
115 bytes = fwrite(dst, 1, sizeof(units), f);
116 fclose(f);
117 if (bytes != sizeof(units)) {
118 ESP_LOGE(TAG, "/spiffs/etc/units.conf written %d/%d bytes", bytes, sizeof(units));
119 } else {
120 ESP_LOGI(TAG, "/spiffs/etc/units.conf written %d bytes", bytes);
121 }
115 } 122 }
116 123
117 124
118 125
119 int add_station(uint8_t *SSID, uint8_t *Password) 126 int add_station(uint8_t *SSID, uint8_t *Password)

mercurial