configServer.c
19.8 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/**@file
* @brief Definitions for the configuration HTTP server
* @author Imanol Barba Sabariego
* @date 08/06/2014
*
* This is where all the methods previously declared in configServer.h are defined.
*/
#include "configServer.h"
//! POST variable for Wi-Fi network name
char arg_networkNameStr[100];
//! POST variable for Wi-Fi network passphrase
char arg_passphraseStr[100];
//! POST variable for Wi-Fi network security type
char arg_securityTypeStr[100];
//! POST variable for RADIUS username
char arg_radUserStr[100];
//! POST variable for RADIUS password
char arg_radPassStr[100];
//! POST variable for geolocalization coordinates (i.e: 41.557255 2.096183)
char arg_geoLocalizationStr[100];
//! Timer for device reboot
wismart_timer_t rebootTimer;
//! This is the array that stores the http resources
wismart_server_resource_t configServerResources[22];
void configServer_start(uint8_t enableApScan)
{
configServer_buildResources();
libwismart_server_start(80, "WismartServer", configServer_dynamicCb, chHeapFree, configServerResources);
}
void configServer_connect()
{
libwismart_server_connect();
}
void configServer_reboot()
{
libwismart_TimerSet(&rebootTimer, 5000, configServer_rebootTimerHandler, NULL);
}
void configServer_rebootTimerHandler(void *arg)
{
libwismart_Reboot();
}
void configServer_setClientParameters()
{
char asciiBuf[20];
libwismart_server_GET("networkName", arg_networkNameStr, sizeof(arg_networkNameStr));
libwismart_server_GET("passphrase", arg_passphraseStr, sizeof(arg_passphraseStr));
libwismart_server_GET("securityType", arg_securityTypeStr, sizeof(arg_securityTypeStr));
libwismart_server_GET("radUser", arg_radUserStr, sizeof(arg_radUserStr));
libwismart_server_GET("radPass", arg_radPassStr, sizeof(arg_radPassStr));
libwismart_server_GET("geoloc", arg_geoLocalizationStr, sizeof(arg_geoLocalizationStr));
CONFIG_SERVER_DBG("The following client settings where retrieved:\r\n");
CONFIG_SERVER_DBG("networkName : %s\r\n",arg_networkNameStr);
CONFIG_SERVER_DBG("securityType : %s\r\n",arg_securityTypeStr);
CONFIG_SERVER_DBG("passphrase : %s\r\n",arg_passphraseStr);
CONFIG_SERVER_DBG("radUser : %s\r\n",arg_radUserStr);
CONFIG_SERVER_DBG("radPass : %s\r\n",arg_radPassStr);
CONFIG_SERVER_DBG("geoloc : %s\r\n",arg_geoLocalizationStr);
config_params_t config;
strcpy(config.user,arg_radUserStr);
strcpy(config.password,arg_radPassStr);
strcpy(config.localization,arg_geoLocalizationStr);
strcpy(config.ssid,arg_networkNameStr);
strcpy(config.wepkey,arg_passphraseStr);
strcpy(config.passphrase,arg_passphraseStr);
/* Value validation */
if((strlen((char*)arg_networkNameStr) > 32) || (strlen((char*)arg_networkNameStr) == 0))
{
CONFIG_SERVER_DBG_WARNING("Invalid network name![%s]\r\n",(char*)arg_networkNameStr);
strcpy(config.ssid,"modularsense");
}
if(strcmp((char*)arg_securityTypeStr, SECURITY_TYPE_OPEN) == 0)
{
/* OPEN ----------------------------------------------------------- */
config.security = (uint16_t) PROFILE_SECURITY_OPEN;
}
else if(strcmp((char*)arg_securityTypeStr, SECURITY_TYPE_WPA) == 0)
{
/* WPA ------------------------------------------------------------- */
if((strlen((char*)arg_passphraseStr) < 8) || (strlen((char*)arg_passphraseStr) > 64))
{
CONFIG_SERVER_DBG_WARNING("Invalid passphrase length![%s]\r\n", arg_passphraseStr);
strcpy((char*)arg_passphraseStr,"");
}
config.security = (uint16_t) PROFILE_SECURITY_WPA_WPA2;
}
else
{
/* WEP ------------------------------------------------------------- */
if(strlen((char*)arg_passphraseStr) == 5)
{
config.security = PROFILE_SECURITY_WEP40;
}
else if(strlen((char*)arg_passphraseStr) == 13)
{
config.security = PROFILE_SECURITY_WEP104;
}
else if(strlen((char*)arg_passphraseStr) == 10)
{
config.security = PROFILE_SECURITY_WEP104;
CONFIG_SERVER_DBG("WEP hex2ascii conversion for '%s' ...\r\n",arg_passphraseStr);
int ret;
ret = configServer_hex2bin((char*)arg_passphraseStr, (char*)asciiBuf, 5);
if(ret == 0)
{
//successful conversion
CONFIG_SERVER_DBG("%d,%d,%d,%d,%d\r\n",asciiBuf[0],asciiBuf[1],asciiBuf[2],asciiBuf[3],asciiBuf[4]);
memset(arg_passphraseStr, 0, sizeof(arg_passphraseStr));
memcpy(arg_passphraseStr, asciiBuf, 5);
}
else
{
//failed conversion
memset(arg_passphraseStr, 0, sizeof(arg_passphraseStr));
CONFIG_SERVER_DBG_WARNING("WEP hex2ascii conversion failed!\r\n");
}
CONFIG_SERVER_DBG("WEP hex2ascii conversion result is '%s'\r\n",arg_passphraseStr);
}
else if(strlen((char*)arg_passphraseStr) == 26)
{
config.security = PROFILE_SECURITY_WEP104;
CONFIG_SERVER_DBG("WEP hex2ascii conversion for '%s' ...\r\n",arg_passphraseStr);
int ret;
ret = configServer_hex2bin((char*)arg_passphraseStr, (char*)asciiBuf, 13);
if(ret == 0)
{
memset(arg_passphraseStr, 0, sizeof(arg_passphraseStr));
memcpy(arg_passphraseStr, asciiBuf, 13);
}
else
{
memset(arg_passphraseStr, 0, sizeof(arg_passphraseStr));
CONFIG_SERVER_DBG_WARNING("WEP hex2ascii conversion failed!\r\n");
}
CONFIG_SERVER_DBG("WEP hex2ascii conversion result is '%s'\r\n",arg_passphraseStr);
}
else
{
CONFIG_SERVER_DBG_WARNING("Invalid WEP key length![%s]\r\n",(char*)arg_passphraseStr);
config.security = PROFILE_SECURITY_WEP104;
strcpy((char*)arg_passphraseStr,"");
}
strcpy(config.passphrase, (char*)arg_passphraseStr);
strcpy(config.wepkey, (char*)arg_passphraseStr);
}
libwismart_RegistrySet(&geo,&config);
}
/*----------------------------------------------------------------------------------------------------------------------------------------------
// DYNAMIC CONTENT
-----------------------------------------------------------------------------------------------------------------------------------------------*/
/*
* This callback function is called everytime the browser requests a page with the 'hasDynamicContent'
* field equal to 1. The user must return only the following values, depending the case:
* - WISMART_SERVER_ERR_MEM : if program is out of memory. This will tell the server to not drop the
* http request, and ask in ~200ms again for the value of the variable.
* After ~5 seconds of failed rerties, the http request will be dropped by the server.
* - WISMART_SERVER_ERR_OK : The program filled the 'varValue' and 'varAllocType' fields,
* and server can use them for the reply.
*
*/
uint32_t configServer_dynamicCb(char* varName, char** varValue, uint8_t* varAllocType)
{
char* value;
config_params_t config;
if(libwismart_RegistryIsValueEmpty(&geo))
{
value = chHeapAlloc(NULL, 100);
strcpy(value,"N/A");
*varAllocType = WISMART_SERVER_ALLOC_DYNAMIC;
(*varValue) = value;
return WISMART_SERVER_ERR_OK;
}
libwismart_RegistryGet(&geo,&config);
CONFIG_SERVER_DBG("Quering configuration server for variable '%s'", varName);
if(strcmp((char*)varName,"WIFI_SECURITY") == 0){
uint16_t security;
/* Alocate space for the value */
value = chHeapAlloc(NULL, 100);
if(value == NULL){
return WISMART_SERVER_ERR_MEM;
}
security = config.security;
if(security == PROFILE_SECURITY_OPEN)
{
strcpy(value,"Open");
}
else if(security == PROFILE_SECURITY_WPA_WPA2)
{
strcpy(value,"WPA/WPA2");
}
else
{
strcpy(value,"WEP");
}
/*
* Instruct server to automatically free the memory by calling the
* appropriate free() function when the request is replied.
*/
*varAllocType = WISMART_SERVER_ALLOC_DYNAMIC;
(*varValue) = value;
return WISMART_SERVER_ERR_OK;
}
if(strcmp((char*)varName,"WIFI_SSID") == 0){
/* Alocate space for the value */
value = chHeapAlloc(NULL, 100);
if(value == NULL){
return WISMART_SERVER_ERR_MEM;
}
strcpy(value,config.ssid);
/*
* Instruct server to automatically free the memory by calling the
* appropriate free() function when the request is replied.
*/
*varAllocType = WISMART_SERVER_ALLOC_DYNAMIC;
(*varValue) = value;
return WISMART_SERVER_ERR_OK;
}
if(strcmp((char*)varName,"WIFI_PASSPHRASE") == 0){
/* Alocate space for the value */
value = chHeapAlloc(NULL, 100);
if(value == NULL){
return WISMART_SERVER_ERR_MEM;
}
strcpy(value,config.passphrase);
/* Display only visible characters */
uint32_t index;
char* ascci = (char*)value;
for(index = 0; index < strlen(ascci); index++){
if((ascci[index] < 32) || (ascci[index] > 126)){
ascci[index] = '?';
}
}
/*
* Instruct server to automatically free the memory by calling the
* appropriate free() function when the request is replied.
*/
*varAllocType = WISMART_SERVER_ALLOC_DYNAMIC;
(*varValue) = value;
return WISMART_SERVER_ERR_OK;
}
if(strcmp((char*)varName,"WIFI_RADUSER") == 0){
/* Alocate space for the value */
value = chHeapAlloc(NULL, 100);
if(value == NULL){
return WISMART_SERVER_ERR_MEM;
}
strcpy(value,config.user);
/*
* Instruct server to automatically free the memory by calling the
* appropriate free() function when the request is replied.
*/
*varAllocType = WISMART_SERVER_ALLOC_DYNAMIC;
(*varValue) = value;
return WISMART_SERVER_ERR_OK;
}
if(strcmp((char*)varName,"WIFI_RADPASS") == 0){
/* Alocate space for the value */
value = chHeapAlloc(NULL, 100);
if(value == NULL){
return WISMART_SERVER_ERR_MEM;
}
strcpy(value,config.password);
/*
* Instruct server to automatically free the memory by calling the
* appropriate free() function when the request is replied.
*/
*varAllocType = WISMART_SERVER_ALLOC_DYNAMIC;
(*varValue) = value;
return WISMART_SERVER_ERR_OK;
}
if(strcmp((char*)varName,"WIFI_GEOLOC") == 0){
/* Alocate space for the value */
value = chHeapAlloc(NULL, 100);
if(value == NULL){
return WISMART_SERVER_ERR_MEM;
}
strcpy(value, config.localization);
/*
* Instruct server to automatically free the memory by calling the
* appropriate free() function when the request is replied.
*/
*varAllocType = WISMART_SERVER_ALLOC_DYNAMIC;
(*varValue) = value;
return WISMART_SERVER_ERR_OK;
}
/* We should never reach here */
CONFIG_SERVER_DBG_WARNING("Unknown dynamic content asked!!");
return WISMART_SERVER_ERR_MEM;
}
/*----------------------------------------------------------------------------------------------------------------------------------------------
HEX2BIN conversion
-----------------------------------------------------------------------------------------------------------------------------------------------*/
/*
* This function converts a string in hex format to a string of ascii format. for
* example it will convert the string "313131" (byte array [3][1][3][1][3][1])
* into "111" (byte array [49][49][49]). This is needed because WEP keys can be
* givent either as HEX strigs or as ASCII strings.
*/
int configServer_hex2bin(char *hex, char *buf, size_t hexLen)
{
size_t i, j = 0;
uint8_t byte;
for(i = 0; i < strlen(hex); i += 2)
{
byte = (hex[i]-48)*16 + (hex[i+1]-48);
buf[j++] = byte;
}
return 0;
}
/*----------------------------------------------------------------------------------------------------------------------------------------------
HTTP RESOURCES
-----------------------------------------------------------------------------------------------------------------------------------------------*/
/*
* This function builds a list of all the files of our http server. The next-to-last
* entry must have the 'name' field point to NULL so the server can understand
* that this was the last entry into the array.
*/
void configServer_buildResources()
{
uint32_t index;
/* --------------------------------------------------------------------------------------------- */
// STATIC FILES
/* --------------------------------------------------------------------------------------------- */
index = 0; /* The root html file can be queried as '/' or as '/en_index.html' */
configServerResources[index].name = "/";
configServerResources[index].mimeType = CONFIG_SERVER_MIME_TYPE_HTML;
configServerResources[index].dataPtr = (uint8_t*)data_en_index_html;
configServerResources[index].dataSize = sizeof(data_en_index_html);
configServerResources[index].hasDynamicContent = 1; /* This page has dynamic content ( $_DYNAMIC[XYZ] code inside the html ) */
configServerResources[index].canBeCached = 0;
configServerResources[index].scriptCb = NULL;
index++;
configServerResources[index].name = "/en_index.html";
configServerResources[index].mimeType = CONFIG_SERVER_MIME_TYPE_HTML;
configServerResources[index].dataPtr = (uint8_t*)data_en_index_html;
configServerResources[index].dataSize = sizeof(data_en_index_html);
configServerResources[index].hasDynamicContent = 1; /* This page has dynamic content ( $_DYNAMIC[XYZ] code inside the html ) */
configServerResources[index].canBeCached = 0;
configServerResources[index].scriptCb = NULL;
/* --------------------------------------------------------------------------------------------- */
index++;
configServerResources[index].name = "/en_client.html";
configServerResources[index].mimeType = CONFIG_SERVER_MIME_TYPE_HTML;
configServerResources[index].dataPtr = (uint8_t*)data_en_client_html;
configServerResources[index].dataSize = sizeof(data_en_client_html);
configServerResources[index].hasDynamicContent = 0;
configServerResources[index].canBeCached = 0;
configServerResources[index].scriptCb = NULL;
/* --------------------------------------------------------------------------------------------- */
index++;
configServerResources[index].name = "/en_reboot.html";
configServerResources[index].mimeType = CONFIG_SERVER_MIME_TYPE_HTML;
configServerResources[index].dataPtr = (uint8_t*)data_en_reboot_html;
configServerResources[index].dataSize = sizeof(data_en_reboot_html);
configServerResources[index].hasDynamicContent = 0;
configServerResources[index].canBeCached = 0;
configServerResources[index].scriptCb = NULL;
/* --------------------------------------------------------------------------------------------- */
index++;
configServerResources[index].name = "/css.css";
configServerResources[index].mimeType = CONFIG_SERVER_MIME_TYPE_CSS;
configServerResources[index].dataPtr = (uint8_t*)data_css_css;
configServerResources[index].dataSize = sizeof(data_css_css);
configServerResources[index].hasDynamicContent = 0;
configServerResources[index].canBeCached = 0;
configServerResources[index].scriptCb = NULL;
/* --------------------------------------------------------------------------------------------- */
index++;
configServerResources[index].name = "/logo.png";
configServerResources[index].mimeType = CONFIG_SERVER_MIME_TYPE_PNG;
configServerResources[index].dataPtr = (uint8_t*)data_logo_png;
configServerResources[index].dataSize = sizeof(data_logo_png);
configServerResources[index].hasDynamicContent = 0;
configServerResources[index].canBeCached = 0;
configServerResources[index].scriptCb = NULL;
/* --------------------------------------------------------------------------------------------- */
// SCRIPTS
/* --------------------------------------------------------------------------------------------- */
index++;
configServerResources[index].name = "/en_clientParams";
configServerResources[index].mimeType = CONFIG_SERVER_MIME_TYPE_HTML;
configServerResources[index].dataPtr = (uint8_t*)data_en_reboot_html;
configServerResources[index].dataSize = sizeof(data_en_reboot_html);
configServerResources[index].hasDynamicContent = 0;
configServerResources[index].canBeCached = 0;
configServerResources[index].scriptCb = configServer_setClientParameters; /* Function to be called when Server receives request for this script */
/* --------------------------------------------------------------------------------------------- */
index++;
configServerResources[index].name = "/en_reboot";
configServerResources[index].mimeType = CONFIG_SERVER_MIME_TYPE_HTML;
configServerResources[index].dataPtr = (uint8_t*)data_en_rebooting_html;
configServerResources[index].dataSize = sizeof(data_en_rebooting_html);
configServerResources[index].hasDynamicContent = 0;
configServerResources[index].canBeCached = 0;
configServerResources[index].scriptCb = configServer_reboot; /* Function to be called when Server receives request for this script */
/* --------------------------------------------------------------------------------------------- */
// END OF LIST INDICATOR
/* --------------------------------------------------------------------------------------------- */
index++;
configServerResources[index].name = NULL;
/* --------------------------------------------------------------------------------------------- */
// RESOURCE NUMBER CHECK
/* --------------------------------------------------------------------------------------------- */
if(index + 1 > sizeof(configServerResources)/sizeof(wismart_server_resource_t)){
CONFIG_SERVER_DBG_WARNING("configServerResources[] size is too small! [%u/%u]",index + 1, sizeof(configServerResources)/sizeof(wismart_server_resource_t));
while(1){}
}
}