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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
|
/*
* File : wlan_cmd.c
* Wi-Fi common commands
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2016, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2016-03-12 Bernard first version
*/
#include <rtthread.h>
#include <wlan_dev.h>
#include <finsh.h>
#include <lwip/dhcp.h>
#include "wlan_cmd.h"
#ifdef LWIP_USING_DHCPD
#include <dhcp_server.h>
#endif
struct rt_wlan_info info;
static char wifi_ssid[32] = {0};
static char wifi_key[32] = {0};
static int network_mode = WIFI_STATION;
#ifndef WIFI_SETTING_FN
#define WIFI_SETTING_FN "/appfs/setting.json"
#endif
#ifndef WIFI_DEVICE_STA_NAME
#define WIFI_DEVICE_STA_NAME "w0"
#endif
#ifndef WIFI_DEVICE_AP_NAME
#define WIFI_DEVICE_AP_NAME "ap"
#endif
#ifdef RT_USING_DFS
#include <dfs_posix.h>
#ifdef PKG_USING_CJSON
#include <cJSON_util.h>
#endif
int wifi_get_mode(void)
{
return network_mode;
}
int wifi_set_mode(int mode)
{
network_mode = mode;
return network_mode;
}
int wifi_set_setting(const char* ssid, const char* pwd)
{
if (!ssid) return -1;
strncpy(wifi_ssid, ssid, sizeof(wifi_ssid));
wifi_ssid[sizeof(wifi_ssid) - 1] = '\0';
if (pwd)
{
strncpy(wifi_key, pwd, sizeof(wifi_key));
wifi_key[sizeof(wifi_key) - 1] = '\0';
}
else wifi_key[0] = '\0';
return 0;
}
#ifdef PKG_USING_CJSON
int wifi_read_cfg(const char* filename)
{
int fd;
cJSON *json = RT_NULL;
fd = open(filename,O_RDONLY, 0);
if(fd < 0)
{
/* no setting file */
return -1;
}
if (fd >= 0)
{
int length;
length = lseek(fd, 0, SEEK_END);
if (length)
{
char *json_str = (char *) rt_malloc (length);
if (json_str)
{
lseek(fd, 0, SEEK_SET);
read(fd, json_str, length);
json = cJSON_Parse(json_str);
rt_free(json_str);
}
}
close(fd);
}
if (json)
{
cJSON *wifi = cJSON_GetObjectItem(json, "wifi");
cJSON *ssid = cJSON_GetObjectItem(wifi, "SSID");
cJSON *key = cJSON_GetObjectItem(wifi, "Key");
cJSON *mode = cJSON_GetObjectItem(wifi, "Mode");
if (ssid)
{
memset(wifi_ssid, 0x0, sizeof(wifi_ssid));
rt_strncpy(wifi_ssid, ssid->valuestring, sizeof(wifi_ssid) - 1);
}
if (key)
{
memset(wifi_key, 0x0, sizeof(wifi_key));
rt_strncpy(wifi_key, key->valuestring, sizeof(wifi_key) - 1);
}
if (mode)
{
network_mode = mode->valueint;
}
cJSON_Delete(json);
}
return 0;
}
int wifi_save_cfg(const char* filename)
{
int fd;
cJSON *json = RT_NULL;
fd = open(filename, O_RDONLY, 0);
if (fd >= 0)
{
int length;
length = lseek(fd, 0, SEEK_END);
if (length)
{
char *json_str = (char *) rt_malloc (length);
if (json_str)
{
lseek(fd, 0, SEEK_SET);
read(fd, json_str, length);
json = cJSON_Parse(json_str);
rt_free(json_str);
}
}
close(fd);
}
else
{
/* create a new setting.json */
fd = open(filename, O_WRONLY | O_TRUNC, 0);
if (fd >= 0)
{
json = cJSON_CreateObject();
if (json)
{
cJSON *wifi = cJSON_CreateObject();
if (wifi)
{
char *json_str;
cJSON_AddItemToObject(json, "wifi", wifi);
cJSON_AddStringToObject(wifi, "SSID", wifi_ssid);
cJSON_AddStringToObject(wifi, "Key", wifi_key);
cJSON_AddNumberToObject(wifi, "Mode", network_mode);
json_str = cJSON_Print(json);
if (json_str)
{
write(fd, json_str, rt_strlen(json_str));
cJSON_free(json_str);
}
}
}
}
close(fd);
return 0;
}
if (json)
{
cJSON *wifi = cJSON_GetObjectItem(json, "wifi");
if (!wifi)
{
wifi = cJSON_CreateObject();
cJSON_AddItemToObject(json, "wifi", wifi);
}
if (cJSON_GetObjectItem(wifi, "SSID"))cJSON_ReplaceItemInObject(wifi, "SSID", cJSON_CreateString(wifi_ssid));
else cJSON_AddStringToObject(wifi, "SSID", wifi_ssid);
if (cJSON_GetObjectItem(wifi, "Key")) cJSON_ReplaceItemInObject(wifi, "Key", cJSON_CreateString(wifi_key));
else cJSON_AddStringToObject(wifi, "Key", wifi_key);
if (cJSON_GetObjectItem(wifi, "Mode")) cJSON_ReplaceItemInObject(wifi, "Mode", cJSON_CreateNumber(network_mode));
else cJSON_AddNumberToObject(wifi, "Mode", network_mode);
fd = open(filename, O_WRONLY | O_TRUNC, 0);
if (fd >= 0)
{
char *json_str = cJSON_Print(json);
if (json_str)
{
write(fd, json_str, rt_strlen(json_str));
cJSON_free(json_str);
}
close(fd);
}
cJSON_Delete(json);
}
return 0;
}
#endif
int wifi_save_setting(void)
{
#ifdef PKG_USING_CJSON
wifi_save_cfg(WIFI_SETTING_FN);
#endif
return 0;
}
#endif
int wifi_softap_setup_netif(struct netif *netif)
{
if (netif)
{
ip_addr_t *ip;
ip_addr_t addr;
#ifdef RT_LWIP_DHCP
/* Stop DHCP Client */
dhcp_stop(netif);
#endif
/* set ipaddr, gw, netmask */
ip = (ip_addr_t *)&addr;
/* set ip address */
if (ipaddr_aton("192.168.169.1", &addr))
{
netif_set_ipaddr(netif, ip);
}
/* set gateway address */
if ( ipaddr_aton("192.168.169.1", &addr))
{
netif_set_gw(netif, ip);
}
/* set netmask address */
if ( ipaddr_aton("255.255.255.0", &addr))
{
netif_set_netmask(netif, ip);
}
netif_set_up(netif);
#ifdef LWIP_USING_DHCPD
{
char name[8];
memset(name, 0, sizeof(name));
strncpy(name, netif->name, sizeof(name)>sizeof(netif->name)? sizeof(netif->name) : sizeof(name));
dhcpd_start(name);
}
#endif
}
return 0;
}
int wifi_default(void)
{
int result = 0;
struct rt_wlan_device *wlan;
#ifdef PKG_USING_CJSON
/* read default setting for wifi */
wifi_read_cfg(WIFI_SETTING_FN);
#endif
if (network_mode == WIFI_STATION)
{
/* get wlan device */
wlan = (struct rt_wlan_device*)rt_device_find(WIFI_DEVICE_STA_NAME);
if (!wlan)
{
rt_kprintf("no wlan:%s device\n", WIFI_DEVICE_STA_NAME);
return -1;
}
/* wifi station */
rt_wlan_info_init(&info, WIFI_STATION, SECURITY_WPA2_MIXED_PSK, wifi_ssid);
result =rt_wlan_init(wlan, WIFI_STATION);
if (result == RT_EOK)
{
result = rt_wlan_connect(wlan, &info, wifi_key);
}
}
else
{
/* wifi AP */
/* get wlan device */
wlan = (struct rt_wlan_device*)rt_device_find(WIFI_DEVICE_AP_NAME);
if (!wlan)
{
rt_kprintf("no wlan:%s device\n", WIFI_DEVICE_AP_NAME);
return -1;
}
rt_wlan_info_init(&info, WIFI_AP, SECURITY_WPA2_AES_PSK, wifi_ssid);
info.channel = 11;
/* wifi soft-AP */
result =rt_wlan_init(wlan, WIFI_AP);
if (result == RT_EOK)
{
result = rt_wlan_softap(wlan, &info, wifi_key);
}
}
return result;
}
static void wifi_usage(void)
{
rt_kprintf("wifi help - Help information\n");
rt_kprintf("wifi cfg SSID PASSWORD - Setting your router AP ssid and pwd\n");
rt_kprintf("wifi - Do the default wifi action\n");
rt_kprintf("wifi wlan_dev scan\n");
rt_kprintf("wifi wlan_dev join SSID PASSWORD\n");
rt_kprintf("wifi wlan_dev ap SSID [PASSWORD]\n");
rt_kprintf("wifi wlan_dev up\n");
rt_kprintf("wifi wlan_dev down\n");
rt_kprintf("wifi wlan_dev rssi\n");
rt_kprintf("wifi wlan_dev status\n");
}
int wifi(int argc, char** argv)
{
struct rt_wlan_device *wlan;
if (argc == 1)
{
wifi_default();
return 0;
}
if (strcmp(argv[1], "help") == 0)
{
wifi_usage();
return 0;
}
if (strcmp(argv[1], "cfg") == 0)
{
/* configure wifi setting */
memset(wifi_ssid, 0x0, sizeof(wifi_ssid));
rt_strncpy(wifi_ssid, argv[2], sizeof(wifi_ssid) - 1);
memset(wifi_key, 0x0, sizeof(wifi_key));
rt_strncpy(wifi_key, argv[3], sizeof(wifi_key) - 1);
network_mode = WIFI_STATION;
#ifdef PKG_USING_CJSON
wifi_save_cfg(WIFI_SETTING_FN);
#endif
return 0;
}
/* get wlan device */
wlan = (struct rt_wlan_device*)rt_device_find(argv[1]);
if (!wlan)
{
rt_kprintf("no wlan:%s device\n", argv[1]);
return 0;
}
if (argc < 3)
{
wifi_usage();
return 0;
}
if (strcmp(argv[2], "join") == 0)
{
rt_wlan_init(wlan, WIFI_STATION);
network_mode = WIFI_STATION;
/* TODO: use easy-join to replace */
rt_wlan_info_init(&info, WIFI_STATION, SECURITY_WPA2_MIXED_PSK, argv[3]);
rt_wlan_connect(wlan, &info, argv[4]);
}
else if (strcmp(argv[2], "up") == 0)
{
/* the key was saved in wlan device */
rt_wlan_connect(wlan, RT_NULL, wlan->key);
}
else if (strcmp(argv[2], "down") == 0)
{
rt_wlan_disconnect(wlan);
}
else if (strcmp(argv[2], "scan") == 0)
{
struct rt_wlan_info *infos;
infos = (struct rt_wlan_info*)rt_malloc(sizeof(struct rt_wlan_info) * 12);
if (infos)
{
int index, num;
memset(infos, 0x0, sizeof(struct rt_wlan_info) * 12);
num = rt_wlan_scan(wlan, infos, 12);
for (index = 0; index < num; index ++)
{
rt_kprintf("----Wi-Fi AP[%d] Information----\n", index);
rt_kprintf("SSID: %-.32s\n", infos[index].ssid);
rt_kprintf("rssi: %d\n", infos[index].rssi);
rt_kprintf(" chn: %d\n", infos[index].channel);
rt_kprintf("rate: %d\n", infos[index].datarate);
rt_kprintf("\n");
}
/* de-initialize info */
for (index = 0; index < num; index ++)
{
rt_wlan_info_deinit(&infos[index]);
}
rt_free(infos);
}
}
else if (strcmp(argv[2], "rssi") == 0)
{
int rssi;
rssi = rt_wlan_get_rssi(wlan);
rt_kprintf("rssi=%d\n", rssi);
}
else if (strcmp(argv[2], "ap") == 0)
{
rt_err_t result = RT_EOK;
if (argc == 4)
{
// open soft-AP
rt_wlan_info_init(&info, WIFI_AP, SECURITY_OPEN, argv[3]);
info.channel = 11;
result =rt_wlan_init(wlan, WIFI_AP);
/* start soft ap */
result = rt_wlan_softap(wlan, &info, NULL);
if (result == RT_EOK)
{
network_mode = WIFI_AP;
}
}
else if (argc == 5)
{
// WPA2 with password
rt_wlan_info_init(&info, WIFI_AP, SECURITY_WPA2_AES_PSK, argv[3]);
info.channel = 11;
result =rt_wlan_init(wlan, WIFI_AP);
/* start soft ap */
result = rt_wlan_softap(wlan, &info, argv[4]);
if (result == RT_EOK)
{
network_mode = WIFI_AP;
}
}
else
{
wifi_usage();
}
if (result != RT_EOK)
{
rt_kprintf("wifi start failed! result=%d\n", result);
}
}
else if (strcmp(argv[2], "status") == 0)
{
int rssi;
if (netif_is_link_up(wlan->parent.netif))
{
rssi = rt_wlan_get_rssi(wlan);
rt_kprintf("Wi-Fi AP: %-.32s\n", wlan->info->ssid);
rt_kprintf("MAC Addr: %02x:%02x:%02x:%02x:%02x:%02x\n", wlan->info->bssid[0],
wlan->info->bssid[1],
wlan->info->bssid[2],
wlan->info->bssid[3],
wlan->info->bssid[4],
wlan->info->bssid[5]);
rt_kprintf(" Channel: %d\n", wlan->info->channel);
rt_kprintf("DataRate: %dMbps\n", wlan->info->datarate/1000);
rt_kprintf(" RSSI: %d\n", rssi);
}
else
{
rt_kprintf("wifi disconnected!\n");
}
return 0;
}
return 0;
}
MSH_CMD_EXPORT(wifi, wifi command);
|