Tue, 26 Sep 2023 15:10:25 +0200
Migrated to isp-idf v5.1
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 | 13 | wifiStation_t wifiStation; |
14 | strConfig_t config; | |
0
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_config() { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
18 | uint8_t *dst = (uint8_t *)&config; |
74 | 19 | FILE *f = fopen("/spiffs/config.conf", "w+"); |
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 | 22 | ESP_LOGE(TAG, "write /spiffs/config.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 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
26 | size_t bytes = fwrite(dst, 1, sizeof(config), f); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
27 | fclose(f); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
28 | if (bytes != sizeof(config)) { |
74 | 29 | ESP_LOGE(TAG, "/spiffs/config.conf written %d/%d bytes", bytes, sizeof(config)); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
30 | } else { |
74 | 31 | ESP_LOGD(TAG, "/spiffs/config.conf written %d bytes", bytes); |
0
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 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
37 | void read_config() { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
38 | uint8_t *dst; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
39 | uint8_t mac_addr[8] = {0}; |
74 | 40 | FILE *f = fopen("/spiffs/config.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 configuration yet, create it. |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
44 | esp_efuse_mac_get_default(mac_addr); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
45 | config.Version = 1; |
1 | 46 | sprintf(config.hostname, "co2meter-%02x%02x%02x", mac_addr[3], mac_addr[4], mac_addr[5]); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
47 | config.mqtt_server[0] = '\0'; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
48 | config.mqtt_port = 1883; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
49 | config.mqtt_user[0] = '\0'; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
50 | config.mqtt_pwd[0] = '\0'; |
74 | 51 | sprintf(config.uuid, "c0ffeeee-dead-beef-cafe-%02x%02x%02x%02x%02x%02x", |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
52 | mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
53 | write_config(); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
54 | } else { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
55 | dst = (uint8_t*)&config; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
56 | size_t bytes = fread(dst, 1, sizeof(config), f); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
57 | fclose(f); |
74 | 58 | ESP_LOGD(TAG, "/spiffs/config.conf read %d bytes", bytes); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
59 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
60 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
61 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
62 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
63 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
64 | void write_units() { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
65 | uint8_t *dst = (uint8_t *)&units; |
74 | 66 | FILE *f = fopen("/spiffs/units.conf", "r+"); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
67 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
68 | if (f == NULL) { |
74 | 69 | 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
|
70 | return; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
71 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
72 | 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
|
73 | fclose(f); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
74 | if (bytes != sizeof(units)) { |
74 | 75 | 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
|
76 | } else { |
74 | 77 | 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
|
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 | |
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 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
83 | 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
|
84 | uint8_t *dst = (uint8_t *)&units; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
85 | 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
|
86 | size_t bytes; |
74 | 87 | FILE *f = fopen("/spiffs/units.conf", "r"); |
0
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 | if (f == NULL) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
90 | // No units yet, create them. |
74 | 91 | 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
|
92 | goto u_error; |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
93 | } 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
|
94 | bytes = fread(dst, 1, sizeof(units), f); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
95 | 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
|
96 | if (bytes != sizeof(units)) { |
74 | 97 | 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
|
98 | 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
|
99 | } |
74 | 100 | 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
|
101 | for (int i = 0; i < 3; i++) |
74 | 102 | 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
|
103 | } |
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
|
104 | 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
|
105 | |
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
|
106 | 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
|
107 | 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
|
108 | 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
|
109 | 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
|
110 | 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
|
111 | 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
|
112 | 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
|
113 | 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
|
114 | } |
74 | 115 | 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
|
116 | 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
|
117 | 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
|
118 | if (bytes != sizeof(units)) { |
74 | 119 | 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
|
120 | } else { |
74 | 121 | 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
|
122 | } |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
123 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
124 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
125 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
126 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
127 | 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
|
128 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
129 | FILE *f; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
130 | uint8_t *dst = (uint8_t *)&wifiStation; |
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 | if (read_station(SSID) >= 0) { |
74 | 133 | 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
|
134 | return -1; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
135 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
136 | |
74 | 137 | f = fopen("/spiffs/stations.conf", "a+"); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
138 | if (f == NULL) { |
74 | 139 | 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
|
140 | return -1; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
141 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
142 | memset(dst, 0, sizeof(wifiStation)); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
143 | sprintf(wifiStation.SSID, "%s", (char *)SSID); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
144 | sprintf(wifiStation.Password, "%s", (char *)Password); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
145 | fwrite(dst, 1, sizeof(wifiStation), f); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
146 | fclose(f); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
147 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
148 | 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
|
149 | /* Return the record number */ |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
150 | return read_station(SSID); |
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 | |
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 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
155 | int read_station(uint8_t *SSID) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
156 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
157 | uint8_t *dst = (uint8_t *)&wifiStation; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
158 | static int rc; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
159 | FILE *f; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
160 | size_t bytes; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
161 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
162 | if ((SSID == NULL) || (strlen((char *)SSID) == 0)) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
163 | ESP_LOGI(TAG, "read_station(NULL)"); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
164 | return -1; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
165 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
166 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
167 | memset(dst, 0, sizeof(wifiStation)); |
74 | 168 | f = fopen("/spiffs/stations.conf", "r+"); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
169 | if (f == NULL) { |
74 | 170 | f = fopen("/spiffs/stations.conf", "w+"); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
171 | fclose(f); |
74 | 172 | 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
|
173 | return -1; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
174 | } |
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 | rc = 0; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
177 | fseek(f, 0, SEEK_SET); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
178 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
179 | while (1) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
180 | bytes = fread(dst, 1, sizeof(wifiStation), f); |
74 | 181 | 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
|
182 | if (bytes < sizeof(wifiStation)) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
183 | fclose(f); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
184 | memset(dst, 0, sizeof(wifiStation)); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
185 | return -1; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
186 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
187 | if (strcmp(wifiStation.SSID, (char *)SSID) == 0) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
188 | // Fount it |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
189 | fclose(f); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
190 | return rc; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
191 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
192 | rc++; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
193 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
194 | return -1; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
195 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
196 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
197 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
198 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
199 | void remove_station(uint8_t *SSID) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
200 | { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
201 | FILE *n, *o; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
202 | uint8_t *dst; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
203 | size_t bytes; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
204 | |
74 | 205 | n = fopen("/spiffs/stations.new", "a"); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
206 | if (n == NULL) { |
74 | 207 | 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
|
208 | return; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
209 | } |
74 | 210 | o = fopen("/spiffs/stations.conf", "r"); |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
211 | if (o == NULL) { |
74 | 212 | 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
|
213 | fclose(n); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
214 | return; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
215 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
216 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
217 | dst = (uint8_t*)&wifiStation; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
218 | while (true) { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
219 | bytes = fread(dst, 1, sizeof(wifiStation), o); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
220 | if (bytes == 0) |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
221 | break; |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
222 | |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
223 | if ((strcmp((char *)SSID, wifiStation.SSID) == 0) || (strlen(wifiStation.SSID) == 0)) { |
33 | 224 | 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
|
225 | } else { |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
226 | fwrite(dst, 1, sizeof(wifiStation), n); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
227 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
228 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
229 | fclose(o); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
230 | fclose(n); |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
231 | |
74 | 232 | rename("/spiffs/stations.conf", "/spiffs/stations.old"); |
233 | rename("/spiffs/stations.new", "/spiffs/stations.conf"); | |
234 | unlink("/spiffs/stations.old"); | |
0
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
235 | } |
88d965579617
Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
236 |