|
1 /** |
|
2 * @file config.c |
|
3 * @brief BrewBoard configuration files. |
|
4 */ |
|
5 |
|
6 #include "config.h" |
|
7 |
|
8 static const char *TAG = "config"; |
|
9 |
|
10 extern unit_t units[3]; |
|
11 |
|
12 |
|
13 void write_config() { |
|
14 uint8_t *dst = (uint8_t *)&config; |
|
15 FILE *f = fopen("/spiffs/etc/config.conf", "w+"); |
|
16 |
|
17 if (f == NULL) { |
|
18 ESP_LOGE(TAG, "write /spiffs/etc/config.conf failed"); |
|
19 return; |
|
20 } |
|
21 |
|
22 size_t bytes = fwrite(dst, 1, sizeof(config), f); |
|
23 fclose(f); |
|
24 if (bytes != sizeof(config)) { |
|
25 ESP_LOGE(TAG, "/spiffs/etc/config.conf written %d/%d bytes", bytes, sizeof(config)); |
|
26 } else { |
|
27 ESP_LOGI(TAG, "/spiffs/etc/config.conf written %d bytes", bytes); |
|
28 } |
|
29 } |
|
30 |
|
31 |
|
32 |
|
33 void read_config() { |
|
34 uint8_t *dst; |
|
35 uint8_t mac_addr[8] = {0}; |
|
36 FILE *f = fopen("/spiffs/etc/config.conf", "r"); |
|
37 |
|
38 if (f == NULL) { |
|
39 // No configuration yet, create it. |
|
40 esp_efuse_mac_get_default(mac_addr); |
|
41 config.Version = 1; |
|
42 sprintf(config.hostname, "pressure-%02x%02x%02x", mac_addr[3], mac_addr[4], mac_addr[5]); |
|
43 config.mqtt_server[0] = '\0'; |
|
44 config.mqtt_port = 1883; |
|
45 config.mqtt_user[0] = '\0'; |
|
46 config.mqtt_pwd[0] = '\0'; |
|
47 sprintf(config.uuid, "c0ffeeee-dead-beef-cafe-%02x%02x%02x%02x%02x%02x", |
|
48 mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); |
|
49 write_config(); |
|
50 } else { |
|
51 dst = (uint8_t*)&config; |
|
52 size_t bytes = fread(dst, 1, sizeof(config), f); |
|
53 fclose(f); |
|
54 ESP_LOGI(TAG, "/spiffs/etc/config.conf read %d bytes", bytes); |
|
55 } |
|
56 } |
|
57 |
|
58 |
|
59 |
|
60 void write_units() { |
|
61 uint8_t *dst = (uint8_t *)&units; |
|
62 FILE *f = fopen("/spiffs/etc/units.conf", "w+"); |
|
63 |
|
64 if (f == NULL) { |
|
65 ESP_LOGE(TAG, "write /spiffs/etc/units.conf failed"); |
|
66 return; |
|
67 } |
|
68 size_t bytes = fwrite(dst, 1, sizeof(units), f); |
|
69 fclose(f); |
|
70 if (bytes != sizeof(units)) { |
|
71 ESP_LOGE(TAG, "/spiffs/etc/units.conf written %d/%d bytes", bytes, sizeof(units)); |
|
72 } else { |
|
73 ESP_LOGI(TAG, "/spiffs/etc/units.conf written %d bytes", bytes); |
|
74 } |
|
75 } |
|
76 |
|
77 |
|
78 |
|
79 void read_units() { |
|
80 uint8_t *dst; |
|
81 uint8_t mac_addr[8] = {0}; |
|
82 FILE *f = fopen("/spiffs/etc/units.conf", "r"); |
|
83 |
|
84 if (f == NULL) { |
|
85 // No units yet, create them. |
|
86 esp_efuse_mac_get_default(mac_addr); |
|
87 for (int i = 0; i < 3; i++) { |
|
88 memset(&units[i], 0, sizeof(unit_t)); |
|
89 sprintf(units[i].uuid, "c0ffeeee-dead-beef-caf%d-%02x%02x%02x%02x%02x%02x", i & 3, |
|
90 mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); |
|
91 sprintf(units[i].alias, "unit%d", (i + 1) & 3); |
|
92 } |
|
93 write_units(); |
|
94 } else { |
|
95 dst = (uint8_t*)&units; |
|
96 size_t bytes = fread(dst, 1, sizeof(units), f); |
|
97 fclose(f); |
|
98 ESP_LOGI(TAG, "/spiffs/etc/units.conf read %d bytes", bytes); |
|
99 ESP_LOGI(TAG, "%s %s", units[0].uuid, units[0].alias); |
|
100 } |
|
101 } |
|
102 |
|
103 |
|
104 |
|
105 int add_station(uint8_t *SSID, uint8_t *Password) |
|
106 { |
|
107 FILE *f; |
|
108 uint8_t *dst = (uint8_t *)&wifiStation; |
|
109 |
|
110 if (read_station(SSID) >= 0) { |
|
111 ESP_LOGE(TAG, "add_station %s already excists", SSID); |
|
112 return -1; |
|
113 } |
|
114 |
|
115 f = fopen("/spiffs/etc/stations.conf", "a+"); |
|
116 if (f == NULL) { |
|
117 ESP_LOGE(TAG, "write /spiffs/etc/stations.conf failed"); |
|
118 return -1; |
|
119 } |
|
120 memset(dst, 0, sizeof(wifiStation)); |
|
121 sprintf(wifiStation.SSID, "%s", (char *)SSID); |
|
122 sprintf(wifiStation.Password, "%s", (char *)Password); |
|
123 wifiStation.hide = false; |
|
124 fwrite(dst, 1, sizeof(wifiStation), f); |
|
125 fclose(f); |
|
126 |
|
127 ESP_LOGI(TAG, "add_station %s record: %d", (char *)SSID, read_station(SSID)); |
|
128 /* Return the record number */ |
|
129 return read_station(SSID); |
|
130 } |
|
131 |
|
132 |
|
133 |
|
134 int read_station(uint8_t *SSID) |
|
135 { |
|
136 uint8_t *dst = (uint8_t *)&wifiStation; |
|
137 static int rc; |
|
138 FILE *f; |
|
139 size_t bytes; |
|
140 |
|
141 if ((SSID == NULL) || (strlen((char *)SSID) == 0)) { |
|
142 ESP_LOGI(TAG, "read_station(NULL)"); |
|
143 return -1; |
|
144 } |
|
145 |
|
146 memset(dst, 0, sizeof(wifiStation)); |
|
147 f = fopen("/spiffs/etc/stations.conf", "r+"); |
|
148 if (f == NULL) { |
|
149 f = fopen("/spiffs/etc/stations.conf", "w+"); |
|
150 fclose(f); |
|
151 ESP_LOGI(TAG, "/spiffs/etc/stations.conf created, return -1"); |
|
152 return -1; |
|
153 } |
|
154 |
|
155 rc = 0; |
|
156 fseek(f, 0, SEEK_SET); |
|
157 |
|
158 while (1) { |
|
159 bytes = fread(dst, 1, sizeof(wifiStation), f); |
|
160 if (bytes < sizeof(wifiStation)) { |
|
161 fclose(f); |
|
162 memset(dst, 0, sizeof(wifiStation)); |
|
163 return -1; |
|
164 } |
|
165 if (strcmp(wifiStation.SSID, (char *)SSID) == 0) { |
|
166 // Fount it |
|
167 fclose(f); |
|
168 return rc; |
|
169 } |
|
170 rc++; |
|
171 } |
|
172 return -1; |
|
173 } |
|
174 |
|
175 |
|
176 |
|
177 void remove_station(uint8_t *SSID) |
|
178 { |
|
179 FILE *n, *o; |
|
180 uint8_t *dst; |
|
181 size_t bytes; |
|
182 |
|
183 n = fopen("/spiffs/etc/stations.new", "a"); |
|
184 if (n == NULL) { |
|
185 ESP_LOGE(TAG, "cannot create /spiffs/etc/stations.new"); |
|
186 return; |
|
187 } |
|
188 o = fopen("/spiffs/etc/stations.conf", "r"); |
|
189 if (o == NULL) { |
|
190 ESP_LOGE(TAG, "cannot open spiffs/etc/stations.conf for reading"); |
|
191 fclose(n); |
|
192 return; |
|
193 } |
|
194 |
|
195 dst = (uint8_t*)&wifiStation; |
|
196 while (true) { |
|
197 bytes = fread(dst, 1, sizeof(wifiStation), o); |
|
198 if (bytes == 0) |
|
199 break; |
|
200 |
|
201 if ((strcmp((char *)SSID, wifiStation.SSID) == 0) || (strlen(wifiStation.SSID) == 0)) { |
|
202 // Record to delete, don't copy |
|
203 } else { |
|
204 fwrite(dst, 1, sizeof(wifiStation), n); |
|
205 } |
|
206 } |
|
207 fclose(o); |
|
208 fclose(n); |
|
209 |
|
210 rename("/spiffs/etc/stations.conf", "/spiffs/etc/stations.old"); |
|
211 rename("/spiffs/etc/stations.new", "/spiffs/etc/stations.conf"); |
|
212 unlink("/spiffs/etc/stations.old"); |
|
213 } |
|
214 |