-
Notifications
You must be signed in to change notification settings - Fork 44
/
config.c
326 lines (284 loc) · 6.83 KB
/
config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <os_type.h>
#include <osapi.h>
#include <user_interface.h>
#include "config.h"
#include "display.h"
#include "graphics.h"
#include "retain.h"
#include "conv.h"
#include "debug.h"
Config config;
#define VALID_MAGIC_NUMBER 0xAABBCCDD
void ICACHE_FLASH_ATTR configInit(Config *config)
{
os_memset(config, 0, sizeof(Config));
config->magic = VALID_MAGIC_NUMBER;
os_strcpy(config->ssid, DEFAULT_SSID);
os_strcpy(config->pass, DEFAULT_PASS);
os_strcpy(config->city, DEFAULT_CITY);
os_strcpy(config->cityDisplayed, DEFAULT_CITY);
os_strcpy(config->appid, DEFAULT_APPID);
os_strcpy(config->tsApiKey, DEFAULT_TS_KEY);
config->utcoffset = 0;
config->tempoffset = 0;
config->interval = 30UL*60UL*1000000UL;
config->chart = 0;
config->fahrenheit = FALSE;
config->clock24 = TRUE;
config->debugEn = FALSE;
}
void ICACHE_FLASH_ATTR configRead(Config *config)
{
spi_flash_read(CONFIG_SAVE_FLASH_ADDR, (uint*)config, sizeof(Config));
if (config->magic != VALID_MAGIC_NUMBER)
{
os_printf("no valid config in flash\n");
configInit(config);
configWrite(config);
}
else
{
os_printf("valid config found\n");
}
}
void ICACHE_FLASH_ATTR configWrite(Config *config)
{
spi_flash_erase_sector(CONFIG_SAVE_FLASH_SECTOR);
spi_flash_write(CONFIG_SAVE_FLASH_ADDR, (uint*)config, sizeof(Config));
}
LOCAL int ICACHE_FLASH_ATTR setParam(char *param, uint paramSize, const char *value, uint valueLen)
{
if (!value || !*value || valueLen == 0 || valueLen > paramSize-1)
{
return ERROR;
}
os_memset(param, 0, paramSize);
os_memcpy(param, value, valueLen);
return OK;
}
LOCAL int ICACHE_FLASH_ATTR setSsid(const char *value, uint valueLen)
{
// also renew ip on ssid change
os_memset(&retain.ipConfig, 0, sizeof(retain.ipConfig));
retain.dns1.addr = 0;
retain.dns2.addr = 0;
retainWrite(&retain);
return setParam(config.ssid, sizeof(config.ssid), value, valueLen);
}
LOCAL int ICACHE_FLASH_ATTR setPass(const char *value, uint valueLen)
{
if (valueLen == 0)
{
os_memset(config.pass, 0, sizeof(config.pass));
return OK;
}
return setParam(config.pass, sizeof(config.pass), value, valueLen);
}
LOCAL int ICACHE_FLASH_ATTR setCity(const char *value, uint valueLen)
{
if (setParam(config.city, sizeof(config.city), value, valueLen) == OK)
{
os_memset(config.cityDisplayed, 0, sizeof(config.cityDisplayed));
int i;
for (i = 0; i < valueLen; i++)
{
if (value[i] == '_')
{
config.cityDisplayed[i] = ' ';
}
else if (value[i] == ',')
{
config.cityDisplayed[i] = '\0';
break;
}
else
{
config.cityDisplayed[i] = value[i];
}
}
os_memset(retain.cityId, 0, sizeof(retain.cityId));
retainWrite(&retain);
return OK;
}
return ERROR;
}
LOCAL int ICACHE_FLASH_ATTR setCitydisp(const char *value, uint valueLen)
{
if (!value || !*value || valueLen == 0)
{
config.cityDisplayed[0] = '\0';
return OK;
}
return setParam(config.cityDisplayed, sizeof(config.cityDisplayed), value, valueLen);
}
LOCAL int ICACHE_FLASH_ATTR setAppid(const char *value, uint valueLen)
{
return setParam(config.appid, sizeof(config.appid), value, valueLen);
}
LOCAL int ICACHE_FLASH_ATTR setThingSpeakKey(const char *value, uint valueLen)
{
return setParam(config.tsApiKey, sizeof(config.tsApiKey), value, valueLen);
}
LOCAL int ICACHE_FLASH_ATTR setUtcoffset(const char *value, uint valueLen)
{
if (!value || !*value || !valueLen)
return ERROR;
int offset = strtoint(value);
if (offset < -720 || offset > 840)
return ERROR;
config.utcoffset = offset*60;
return OK;
}
LOCAL int ICACHE_FLASH_ATTR setTempoffset(const char *value, uint valueLen)
{
if (!value || !*value || !valueLen)
return ERROR;
config.tempoffset = strtofloat(value);
return OK;
}
LOCAL int ICACHE_FLASH_ATTR setInterval(const char *value, uint valueLen)
{
if (!value || !*value || !valueLen)
return ERROR;
uint interval = strtoint(value);
if (interval < 1 || interval > 71)
return ERROR;
config.interval = interval*60UL*1000000UL;
return OK;
}
LOCAL int ICACHE_FLASH_ATTR setChart(const char *value, uint valueLen)
{
if (!value || !*value || !valueLen)
return ERROR;
uint chart = strtoint(value);
if (chart < 0 || chart > 3)
return ERROR;
config.chart = chart;
return OK;
}
LOCAL int ICACHE_FLASH_ATTR setFahrenheit(const char *value, uint valueLen)
{
if (!value || !*value || !valueLen)
return ERROR;
switch (value[0])
{
case '0':
config.fahrenheit = FALSE;
return OK;
case '1':
config.fahrenheit = TRUE;
return OK;
default:
return ERROR;
}
}
LOCAL int ICACHE_FLASH_ATTR setClock24(const char *value, uint valueLen)
{
if (!value || !*value || !valueLen)
return ERROR;
switch (value[0])
{
case '0':
config.clock24 = FALSE;
return OK;
case '1':
config.clock24 = TRUE;
return OK;
default:
return ERROR;
}
}
LOCAL int ICACHE_FLASH_ATTR setDebug(const char *value, uint valueLen)
{
if (!value || !*value || !valueLen)
return ERROR;
switch (value[0])
{
case '0':
config.debugEn = FALSE;
return OK;
case '1':
config.debugEn = TRUE;
return OK;
default:
return ERROR;
}
}
LOCAL int ICACHE_FLASH_ATTR resetConfig(const char *value, uint valueLen)
{
configInit(&config);
retainInit(&retain);
retainWrite(&retain);
return OK;
}
LOCAL int ICACHE_FLASH_ATTR clearDisp(const char *value, uint valueLen)
{
if (gfxMem)
{
gfxMemFill(0x00);
}
else
{
if (gfxMemAlloc() != OK)
{
return ERROR;
}
}
dispUpdate(eDispTopPart);
dispUpdate(eDispBottomPart);
while (1); // let wdt reset
return OK;
}
typedef struct
{
const char *cmd;
int (*func)(const char *value, uint valueLen);
}CmdEntry;
CmdEntry commands[] = {
{"ssid", setSsid},
{"pass", setPass},
{"city", setCity},
{"citydisp", setCitydisp},
{"appid", setAppid},
{"thingspeak", setThingSpeakKey},
{"utc", setUtcoffset},
{"temp", setTempoffset},
{"interval", setInterval},
{"chart", setChart},
{"fahrenheit", setFahrenheit},
{"clock24", setClock24},
{"debug", setDebug},
{"reset", resetConfig},
{"clear", clearDisp}
};
void ICACHE_FLASH_ATTR onUartCmdReceived(char* command, int length)
{
if (length < 5)
return;
char *sep = os_strchr(command, ':');
if (!sep)
return;
*sep = '\0';
char *value = sep+1;
int valueLen = os_strlen(value);
uint i;
uint nrCmds = sizeof(commands)/sizeof(commands[0]);
for (i = 0; i < nrCmds; i++)
{
if (!os_strcmp(command, commands[i].cmd))
{
if (commands[i].func(value, valueLen) == OK)
{
os_printf("OK\n");
configWrite(&config);
system_restart();
}
else
{
os_printf("invalid parameter\n");
}
return;
}
}
os_printf("command not supported\n");
}