main/config.c

Tue, 03 Oct 2023 17:24:06 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 03 Oct 2023 17:24:06 +0200
changeset 77
15dc572a7fcb
parent 74
34da2d2b12d5
child 80
715785443a95
permissions
-rw-r--r--

Version 0.3.0. Backported network code from experimental roaming project. Will now connect after reset to the strongest AP. Id the signal level drops below -67, extra scans are done to see for a better AP. Nothing is done yet. Removed config.conf file, all info is taken from the project menu and live tests. Better log the board type and send it via json mqtt. Send bssid and current channel too.

0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 /**
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
2 * @file config.c
41
d327e0aff62f Updated doxygen comments. Removed some development debug logs. Increased user inactivity time to 4 minutes. More Dutch translations in the OTA update screens.
Michiel Broek <mbroek@mbse.eu>
parents: 34
diff changeset
3 * @brief The 'co2meter' configuration data. These are stored in the
d327e0aff62f Updated doxygen comments. Removed some development debug logs. Increased user inactivity time to 4 minutes. More Dutch translations in the OTA update screens.
Michiel Broek <mbroek@mbse.eu>
parents: 34
diff changeset
4 * spiffs filesystem in a flash partition.
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7 #include "config.h"
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9 static const char *TAG = "config";
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10
4
2a57c466bf45 FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
11 unit_t units[3]; ///< Pressure test units
2a57c466bf45 FreeRTOS scheduler from 100 Hz to 200 Hz. All units data is now protected with a semaphore.
Michiel Broek <mbroek@mbse.eu>
parents: 3
diff changeset
12 SemaphoreHandle_t xSemaphoreUnits = NULL; ///< Semaphore Units records
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
13 wifiStation_t wifiStation;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
15
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
16
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
17 void write_units() {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
18 uint8_t *dst = (uint8_t *)&units;
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
19 FILE *f = fopen("/spiffs/units.conf", "r+");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
20
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
21 if (f == NULL) {
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
22 ESP_LOGE(TAG, "write /spiffs/units.conf failed");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
23 return;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
25 size_t bytes = fwrite(dst, 1, sizeof(units), f);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
26 fclose(f);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27 if (bytes != sizeof(units)) {
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
28 ESP_LOGE(TAG, "/spiffs/units.conf written %d/%d bytes", bytes, sizeof(units));
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
29 } else {
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
30 ESP_LOGD(TAG, "/spiffs/units.conf written %d bytes", bytes);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
31 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
32 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
33
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
34
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
35
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
36 void read_units() {
60
07a1a07fdc8c write_units now overwrites instead of truncate/write the records. Add ssid to the node mqtt message. Lower rotary log messages. Removed most menu log messages.
Michiel Broek <mbroek@mbse.eu>
parents: 59
diff changeset
37 uint8_t *dst = (uint8_t *)&units;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
38 uint8_t mac_addr[8] = {0};
60
07a1a07fdc8c write_units now overwrites instead of truncate/write the records. Add ssid to the node mqtt message. Lower rotary log messages. Removed most menu log messages.
Michiel Broek <mbroek@mbse.eu>
parents: 59
diff changeset
39 size_t bytes;
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
40 FILE *f = fopen("/spiffs/units.conf", "r");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
41
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
42 if (f == NULL) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
43 // No units yet, create them.
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
44 ESP_LOGE(TAG, "/spiffs/units.conf not found, create new");
59
52d9405474e1 Better check for a corrupted units file. Added a small boot delay to prevent units file corruption.
Michiel Broek <mbroek@mbse.eu>
parents: 57
diff changeset
45 goto u_error;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
46 } else {
60
07a1a07fdc8c write_units now overwrites instead of truncate/write the records. Add ssid to the node mqtt message. Lower rotary log messages. Removed most menu log messages.
Michiel Broek <mbroek@mbse.eu>
parents: 59
diff changeset
47 bytes = fread(dst, 1, sizeof(units), f);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
48 fclose(f);
59
52d9405474e1 Better check for a corrupted units file. Added a small boot delay to prevent units file corruption.
Michiel Broek <mbroek@mbse.eu>
parents: 57
diff changeset
49 if (bytes != sizeof(units)) {
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
50 ESP_LOGE(TAG, "/spiffs/units.conf read %d of %d bytes", bytes, sizeof(units));
59
52d9405474e1 Better check for a corrupted units file. Added a small boot delay to prevent units file corruption.
Michiel Broek <mbroek@mbse.eu>
parents: 57
diff changeset
51 goto u_error;
52d9405474e1 Better check for a corrupted units file. Added a small boot delay to prevent units file corruption.
Michiel Broek <mbroek@mbse.eu>
parents: 57
diff changeset
52 }
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
53 ESP_LOGI(TAG, "/spiffs/units.conf read %d bytes", bytes);
21
043ae27633f8 Moved the user interface into a separate task. Added a real seconds timer to this task.
Michiel Broek <mbroek@mbse.eu>
parents: 16
diff changeset
54 for (int i = 0; i < 3; i++)
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
55 ESP_LOGI(TAG, "%d %s %d %4lu %3lu", i, units[i].alias, units[i].pressure_channel, units[i].pressure_voltage, units[i].pressure_zero);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
56 }
59
52d9405474e1 Better check for a corrupted units file. Added a small boot delay to prevent units file corruption.
Michiel Broek <mbroek@mbse.eu>
parents: 57
diff changeset
57 return;
52d9405474e1 Better check for a corrupted units file. Added a small boot delay to prevent units file corruption.
Michiel Broek <mbroek@mbse.eu>
parents: 57
diff changeset
58
52d9405474e1 Better check for a corrupted units file. Added a small boot delay to prevent units file corruption.
Michiel Broek <mbroek@mbse.eu>
parents: 57
diff changeset
59 u_error:
52d9405474e1 Better check for a corrupted units file. Added a small boot delay to prevent units file corruption.
Michiel Broek <mbroek@mbse.eu>
parents: 57
diff changeset
60 esp_efuse_mac_get_default(mac_addr);
52d9405474e1 Better check for a corrupted units file. Added a small boot delay to prevent units file corruption.
Michiel Broek <mbroek@mbse.eu>
parents: 57
diff changeset
61 for (int i = 0; i < 3; i++) {
52d9405474e1 Better check for a corrupted units file. Added a small boot delay to prevent units file corruption.
Michiel Broek <mbroek@mbse.eu>
parents: 57
diff changeset
62 memset(&units[i], 0, sizeof(unit_t));
52d9405474e1 Better check for a corrupted units file. Added a small boot delay to prevent units file corruption.
Michiel Broek <mbroek@mbse.eu>
parents: 57
diff changeset
63 sprintf(units[i].uuid, "c0ffeeee-dead-beef-caf%d-%02x%02x%02x%02x%02x%02x", i & 3,
52d9405474e1 Better check for a corrupted units file. Added a small boot delay to prevent units file corruption.
Michiel Broek <mbroek@mbse.eu>
parents: 57
diff changeset
64 mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
52d9405474e1 Better check for a corrupted units file. Added a small boot delay to prevent units file corruption.
Michiel Broek <mbroek@mbse.eu>
parents: 57
diff changeset
65 sprintf(units[i].alias, "unit%d", (i + 1) & 3);
52d9405474e1 Better check for a corrupted units file. Added a small boot delay to prevent units file corruption.
Michiel Broek <mbroek@mbse.eu>
parents: 57
diff changeset
66 units[i].pressure_zero = 110;
52d9405474e1 Better check for a corrupted units file. Added a small boot delay to prevent units file corruption.
Michiel Broek <mbroek@mbse.eu>
parents: 57
diff changeset
67 }
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
68 f = fopen("/spiffs/units.conf", "w+");
60
07a1a07fdc8c write_units now overwrites instead of truncate/write the records. Add ssid to the node mqtt message. Lower rotary log messages. Removed most menu log messages.
Michiel Broek <mbroek@mbse.eu>
parents: 59
diff changeset
69 bytes = fwrite(dst, 1, sizeof(units), f);
07a1a07fdc8c write_units now overwrites instead of truncate/write the records. Add ssid to the node mqtt message. Lower rotary log messages. Removed most menu log messages.
Michiel Broek <mbroek@mbse.eu>
parents: 59
diff changeset
70 fclose(f);
07a1a07fdc8c write_units now overwrites instead of truncate/write the records. Add ssid to the node mqtt message. Lower rotary log messages. Removed most menu log messages.
Michiel Broek <mbroek@mbse.eu>
parents: 59
diff changeset
71 if (bytes != sizeof(units)) {
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
72 ESP_LOGE(TAG, "/spiffs/units.conf written %d/%d bytes", bytes, sizeof(units));
60
07a1a07fdc8c write_units now overwrites instead of truncate/write the records. Add ssid to the node mqtt message. Lower rotary log messages. Removed most menu log messages.
Michiel Broek <mbroek@mbse.eu>
parents: 59
diff changeset
73 } else {
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
74 ESP_LOGI(TAG, "/spiffs/units.conf written %d bytes", bytes);
60
07a1a07fdc8c write_units now overwrites instead of truncate/write the records. Add ssid to the node mqtt message. Lower rotary log messages. Removed most menu log messages.
Michiel Broek <mbroek@mbse.eu>
parents: 59
diff changeset
75 }
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
76 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
77
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
78
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
79
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
80 int add_station(uint8_t *SSID, uint8_t *Password)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
81 {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
82 FILE *f;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
83 uint8_t *dst = (uint8_t *)&wifiStation;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
84
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
85 if (read_station(SSID) >= 0) {
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
86 ESP_LOGE(TAG, "add_station %s already exists", SSID);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
87 return -1;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
88 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
89
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
90 f = fopen("/spiffs/stations.conf", "a+");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
91 if (f == NULL) {
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
92 ESP_LOGE(TAG, "write /spiffs/stations.conf failed");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
93 return -1;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
94 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
95 memset(dst, 0, sizeof(wifiStation));
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
96 sprintf(wifiStation.SSID, "%s", (char *)SSID);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
97 sprintf(wifiStation.Password, "%s", (char *)Password);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
98 fwrite(dst, 1, sizeof(wifiStation), f);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
99 fclose(f);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
100
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
101 ESP_LOGI(TAG, "add_station %s record: %d", (char *)SSID, read_station(SSID));
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
102 /* Return the record number */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
103 return read_station(SSID);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
104 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
105
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
106
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
107
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
108 int read_station(uint8_t *SSID)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
109 {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
110 uint8_t *dst = (uint8_t *)&wifiStation;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
111 static int rc;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
112 FILE *f;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
113 size_t bytes;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
114
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
115 if ((SSID == NULL) || (strlen((char *)SSID) == 0)) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
116 ESP_LOGI(TAG, "read_station(NULL)");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
117 return -1;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
118 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
119
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
120 memset(dst, 0, sizeof(wifiStation));
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
121 f = fopen("/spiffs/stations.conf", "r+");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
122 if (f == NULL) {
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
123 f = fopen("/spiffs/stations.conf", "w+");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
124 fclose(f);
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
125 ESP_LOGI(TAG, "/spiffs/stations.conf created, return -1");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
126 return -1;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
127 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
128
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
129 rc = 0;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
130 fseek(f, 0, SEEK_SET);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
131
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
132 while (1) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
133 bytes = fread(dst, 1, sizeof(wifiStation), f);
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
134 ESP_LOGI(TAG, " read_station bytes %d size %d", bytes, sizeof(wifiStation));
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
135 if (bytes < sizeof(wifiStation)) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
136 fclose(f);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
137 memset(dst, 0, sizeof(wifiStation));
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
138 return -1;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
139 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
140 if (strcmp(wifiStation.SSID, (char *)SSID) == 0) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
141 // Fount it
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
142 fclose(f);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
143 return rc;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
144 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
145 rc++;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
146 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
147 return -1;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
148 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
149
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
150
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
151
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
152 void remove_station(uint8_t *SSID)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
153 {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
154 FILE *n, *o;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
155 uint8_t *dst;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
156 size_t bytes;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
157
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
158 n = fopen("/spiffs/stations.new", "a");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
159 if (n == NULL) {
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
160 ESP_LOGE(TAG, "cannot create /spiffs/stations.new");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
161 return;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
162 }
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
163 o = fopen("/spiffs/stations.conf", "r");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
164 if (o == NULL) {
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
165 ESP_LOGE(TAG, "cannot open spiffs/stations.conf for reading");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
166 fclose(n);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
167 return;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
168 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
169
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
170 dst = (uint8_t*)&wifiStation;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
171 while (true) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
172 bytes = fread(dst, 1, sizeof(wifiStation), o);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
173 if (bytes == 0)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
174 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
175
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
176 if ((strcmp((char *)SSID, wifiStation.SSID) == 0) || (strlen(wifiStation.SSID) == 0)) {
33
331e7f700971 Added WiFi AP editor.
Michiel Broek <mbroek@mbse.eu>
parents: 21
diff changeset
177 ESP_LOGI(TAG, "remove station %s", (char *)SSID);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
178 } else {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
179 fwrite(dst, 1, sizeof(wifiStation), n);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
180 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
181 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
182 fclose(o);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
183 fclose(n);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
184
74
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
185 rename("/spiffs/stations.conf", "/spiffs/stations.old");
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
186 rename("/spiffs/stations.new", "/spiffs/stations.conf");
34da2d2b12d5 Migrated to isp-idf v5.1
Michiel Broek <mbroek@mbse.eu>
parents: 60
diff changeset
187 unlink("/spiffs/stations.old");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
188 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
189

mercurial