-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathwlan.c
988 lines (809 loc) · 23.2 KB
/
wlan.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
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
#include "wlan.h"
#include "util.h"
#include "config.h"
#include "stats.h"
#include "sys_time.h"
#include "string.h"
#include "lwip-interface.h"
#include <stdbool.h>
roflash static const char *const phy[] = {
"unknown",
"802.11b",
"802.11g",
"802.11n",
"unknown"
};
roflash static const char *const slp[] =
{
"none",
"light",
"modem",
"unknown"
};
roflash static const char status_msg[][16] =
{
"OK",
"FAIL",
"PENDING",
"BUSY",
"CANCEL"
};
static unsigned int association_state_time;
static struct attr_packed
{
unsigned int bootstrap_mode:1;
unsigned int reconnect_mode:1;
unsigned int recovery_mode:1;
unsigned int associated:1;
} flags;
assert_size(flags, 1);
typedef struct
{
mac_addr_t mac;
int channel;
int rssi;
} access_point_t;
assert_size(access_point_t, 16);
enum
{
access_points_size = 5,
};
typedef struct
{
int entries;
int selected;
int scanning;
char ssid[32];
char password[32];
access_point_t ap[access_points_size];
} access_points_t;
static access_points_t access_points;
assert_size(access_points_t, 156);
roflash const char help_description_stats_wlan[] = "statistics from the wlan subsystem";
roflash const char help_description_wlan_ap_config[] = "configure access point mode wlan params, supply ssid, passwd and channel";
roflash const char help_description_wlan_client_config[] = "configure client mode wlan params, supply ssid and passwd";
roflash const char help_description_wlan_mode[] = "set wlan mode: client or ap";
roflash const char help_description_wlan_scan[] = "scan wlan, see log to retrieve the results";
static void wlan_event_handler(System_Event_t *event)
{
switch(event->event)
{
case(EVENT_STAMODE_CONNECTED):
{
if(stat_init_associate_time_us == 0)
stat_init_associate_time_us = time_get_us();
break;
}
case(EVENT_STAMODE_GOT_IP):
{
if(stat_init_ip_time_us == 0)
stat_init_ip_time_us = time_get_us();
wlan_multicast_init_groups();
time_sntp_start();
flags.reconnect_mode = 0;
flags.recovery_mode = 0;
flags.associated = 1;
association_state_time = 0;
[[fallthrough]];
}
case(EVENT_SOFTAPMODE_STACONNECTED):
{
dispatch_post_task(task_prio_medium, task_alert_association, 0, 0, 0);
break;
}
case(EVENT_STAMODE_DISCONNECTED):
{
if(flags.associated)
{
log("[wlan] deassociate\n");
association_state_time = 0;
flags.associated = 0;
}
else
log("[wlan] deassociate from unassociated state\n");
[[fallthrough]];
}
case(EVENT_SOFTAPMODE_STADISCONNECTED):
{
dispatch_post_task(task_prio_high, task_alert_disassociation, 0, 0, 0);
break;
}
}
}
void wlan_init(void)
{
unsigned int mode_int;
config_wlan_mode_t mode;
access_points.entries = 0;
access_points.selected = 0;
access_points.scanning = 0;
flags.bootstrap_mode = 1;
flags.reconnect_mode = 0;
flags.recovery_mode = 0;
flags.associated = 0;
association_state_time = 0;
power_save_enable(config_flags_match(flag_wlan_power_save));
if(config_get_uint("wlan.mode", &mode_int, -1, -1))
mode = (config_wlan_mode_t)mode_int;
else
mode = config_wlan_mode_client;
if(mode == config_wlan_mode_client)
wifi_set_opmode(STATION_MODE);
else
wifi_set_opmode(SOFTAP_MODE);
if(!wifi_station_ap_number_set(1))
log("wlan: wifi_station_ap_number_set failed\n");
wifi_station_set_auto_connect(true);
wifi_station_set_reconnect_policy(true);
wifi_set_phy_mode(PHY_MODE_11N);
wifi_set_event_handler_cb(wlan_event_handler);
}
void wlan_periodic(void)
{
// called once per second
association_state_time++;
if(flags.associated)
{
if(flags.bootstrap_mode && (association_state_time > 60))
{
flags.bootstrap_mode = 0;
dispatch_post_task(task_prio_high, task_wlan_reconnect, 0, 0, 0);
}
}
else
{
// when not associated
// - after 15 seconds request explicity for a reconnect
// - after 60 seconds to into ap configuration mode
// - after 5 minutes do a complete reset
if(!flags.recovery_mode && !flags.reconnect_mode && (association_state_time > 15))
{
flags.reconnect_mode = 1;
wlan_reconnect();
}
if(!flags.recovery_mode && (association_state_time > 60))
dispatch_post_task(task_prio_high, task_wlan_recovery, 0, 0, 0);
if(association_state_time > 300)
dispatch_post_task(task_prio_high, task_reset, 0, 0, 0);
}
}
static void wlan_associate_callback(void *arg, STATUS status)
{
struct bss_info *bss;
char status_string[32];
struct station_config cconf;
access_point_t *ap;
int ix;
if(access_points.scanning > 0)
access_points.scanning--;
flash_to_dram(true, status <= CANCEL ? status_msg[status] : "<invalid>", status_string, sizeof(status_string));
log("[wlan] scan callback result: %s\n", status_string);
for(bss = arg; bss; bss = bss->next.stqe_next)
{
ap = &access_points.ap[access_points.entries];
ap->channel = bss->channel;
ap->rssi = bss->rssi;
ap->mac[0] = bss->bssid[0];
ap->mac[1] = bss->bssid[1];
ap->mac[2] = bss->bssid[2];
ap->mac[3] = bss->bssid[3];
ap->mac[4] = bss->bssid[4];
ap->mac[5] = bss->bssid[5];
if(bss->rssi > access_points.ap[access_points.selected].rssi)
access_points.selected = access_points.entries;
access_points.entries++;
}
if(access_points.entries == 0)
{
log("[wlan] no access points found, giving up\n");
return;
}
log("[wlan] access points found: \n");
for(ix = 0; ix < access_points.entries; ix++)
{
string_new(, line, 64);
ap = &access_points.ap[ix];
string_format(&line, "%c ch: %2d, rssi: %3d, bssid: %02x:%02x:%02x:%02x:%02x:%02x\n",
ix == access_points.selected ? '*' : ' ',
ap->channel,
ap->rssi,
ap->mac[0],
ap->mac[1],
ap->mac[2],
ap->mac[3],
ap->mac[4],
ap->mac[5]);
log("%s", string_to_cstr(&line));
}
ap = &access_points.ap[access_points.selected];
if(flags.associated && (wifi_get_channel() == ap->channel))
log("[wlan] already associated to optimal access point\n");
else
{
log("[wlan] reconnecting to optimal access point\n");
memset(&cconf, 0, sizeof(cconf));
strecpy((char *)cconf.ssid, access_points.ssid, sizeof(cconf.ssid));
strecpy((char *)cconf.password, access_points.password, sizeof(cconf.password));
cconf.bssid_set = 1;
cconf.bssid[0] = ap->mac[0];
cconf.bssid[1] = ap->mac[1];
cconf.bssid[2] = ap->mac[2];
cconf.bssid[3] = ap->mac[3];
cconf.bssid[4] = ap->mac[4];
cconf.bssid[5] = ap->mac[5];
cconf.channel = ap->channel;
cconf.all_channel_scan = 0;
cconf.threshold.rssi = 0;
cconf.threshold.authmode = AUTH_OPEN;
wifi_station_set_config(&cconf);
wifi_station_connect();
}
}
static bool wlan_associate(string_t *ssid, string_t *password)
{
struct scan_config sc =
{
.ssid = (char *)0,
.bssid = (char *)0,
.channel = 0,
.show_hidden = 0,
.scan_type = WIFI_SCAN_TYPE_ACTIVE,
.scan_time.active.min = 500,
.scan_time.active.max = 2000,
};
access_points.scanning++;
if(access_points.scanning > 1)
{
access_points.scanning--;
log("[wlan] duplicate association request\n");
return(true);
}
if(!wifi_set_opmode(STATION_MODE))
return(false);
access_points.entries = 0;
access_points.selected = 0;
strecpy(access_points.ssid, string_to_cstr(ssid), sizeof(access_points.ssid));
strecpy(access_points.password, string_to_cstr(password), sizeof(access_points.password));
string_to_cstr(ssid);
sc.ssid = string_buffer_nonconst(ssid);
log("[wlan] start scan for %s\n", access_points.ssid);
if(!wifi_station_scan(&sc, wlan_associate_callback))
return(false);
return(true);
}
static bool wlan_access_point(string_t *ssid, string_t *password, unsigned int channel)
{
struct softap_config saconf;
memset(&saconf, 0, sizeof(saconf));
strecpy(saconf.ssid, string_to_cstr(ssid), sizeof(saconf.ssid));
strecpy(saconf.password, string_to_cstr(password), sizeof(saconf.password));
saconf.ssid_len = strlen(saconf.ssid);
saconf.channel = channel;
saconf.authmode = AUTH_WPA_WPA2_PSK;
saconf.ssid_hidden = 0;
saconf.max_connection = 1;
saconf.beacon_interval = 100;
wifi_station_disconnect();
if(!wifi_set_opmode(SOFTAP_MODE))
return(false);
if(!wifi_softap_set_config_current(&saconf))
return(false);
return(true);
}
bool wlan_reconnect(void)
{
unsigned int mode_int;
config_wlan_mode_t mode;
string_new(, ssid, 64);
string_new(, password, 64);
unsigned int channel;
log("[wlan] start reconnect\n");
if(config_get_uint("wlan.mode", &mode_int, -1, -1))
mode = (config_wlan_mode_t)mode_int;
else
mode = config_wlan_mode_client;
channel = -1;
switch(mode)
{
case(config_wlan_mode_client):
{
if(!config_get_string("wlan.client.ssid", &ssid, -1, -1) ||
!config_get_string("wlan.client.passwd", &password, -1, -1))
return(false);
flags.recovery_mode = 0;
return(wlan_associate(&ssid, &password));
}
case(config_wlan_mode_ap):
{
if(!config_get_string("wlan.ap.ssid", &ssid, -1, -1) ||
!config_get_string("wlan.ap.passwd", &password, -1, -1) ||
!config_get_uint("wlan.ap.channel", &channel, -1, -1))
return(false);
flags.recovery_mode = 0;
return(wlan_access_point(&ssid, &password, channel));
}
}
return(false);
}
void wlan_start_recovery(void)
{
string_init(, wlan_default_ssid, "esp");
string_init(, wlan_default_password, "espespesp");
config_flag_change_nosave(flag_log_to_uart, true);
config_flag_change_nosave(flag_log_to_buffer, true);
config_flag_change_nosave(flag_cmd_from_uart, true);
uart_invert(0, uart_dir_tx, false);
uart_invert(0, uart_dir_rx, false);
uart_loopback(0, false);
flags.bootstrap_mode = 0;
flags.reconnect_mode = 0;
flags.recovery_mode = 1;
flags.associated = 0;
association_state_time = 0;
wlan_access_point(&wlan_default_ssid, &wlan_default_password, 1);
log("* WLAN CAN'T CONNECT, entering recovery mode. *\n");
msleep(10);
log(" configure wlan parameters\n");
msleep(10);
log(" - EITHER associate to SSID \"%s\" using passwd \"%s\"\n", string_to_cstr(&wlan_default_ssid), string_to_cstr(&wlan_default_password));
msleep(10);
log(" and then connect to 192.168.4.1:22 using telnet or browser\n");
msleep(10);
log(" - OR connect to UART\n");
msleep(10);
log(" - THEN issue these commands:\n");
msleep(10);
log(" wcc <ssid> <passwd>\n");
msleep(10);
log(" wm client\n");
msleep(10);
log(" after that, issue a reset command to restore temporarily changed flags.\n");
}
void wlan_multicast_init_groups(void)
{
string_new(, ip, 32);
unsigned int entry;
ip_addr_to_bytes_t addr;
for(entry = 0; entry < 8; entry++)
{
string_clear(&ip);
if(config_get_string("multicast-group.%u", &ip, entry, -1))
{
addr.ip_addr = ip_addr(string_to_cstr(&ip));
if((addr.byte[0] > 0) &&
(addr.byte[1] > 0) &&
(addr.byte[2] > 0) &&
(addr.byte[3] > 0))
if(!lwip_if_join_mc(addr.ip_addr))
log("[wlan] join mc group failed\n");
}
}
}
bool wlan_client_configure(string_t *error, const char *ssid, const char *password)
{
struct station_config cconf;
const char *eptr = "";
if(!config_open_write())
{
static roflash const char e[] = "cannot set config (open)";
eptr = e;
goto e2;
}
if(!config_set_string("wlan.client.ssid", ssid, -1, -1))
{
static roflash const char e[] = "cannot set config (write ssid)";
eptr = e;
goto e1;
}
if(!config_set_string("wlan.client.passwd", password, -1, -1))
{
static roflash const char e[] = "cannot set config (write passwd)";
eptr = e;
goto e1;
}
if(!config_close_write())
{
static roflash const char e[] = "cannot set config (close)";
eptr = e;
goto e2;
}
wifi_set_opmode(STATION_MODE);
wifi_station_disconnect();
memset(&cconf, 0, sizeof(cconf));
strecpy((char *)cconf.ssid, ssid, sizeof(cconf.ssid));
strecpy((char *)cconf.password, password, sizeof(cconf.password));
cconf.all_channel_scan = 1;
wifi_station_set_config(&cconf);
wifi_station_connect();
return(true);
e1:
config_abort_write();
e2:
{
string_new(, logstr, 64);
string_append(&logstr, "wlan client configure: ");
string_append_cstr_flash(&logstr, eptr);
string_append(&logstr, "\n");
if(error)
string_append_string(error, &logstr);
else
log("%s", string_to_cstr(&logstr));
return(false);
}
}
void stats_wlan(string_t *dst)
{
sdk_mac_addr_t mac_addr;
struct ip_info ip_addr_info;
struct station_config config;
struct station_config sc[5], *scp;
unsigned int scn, scni, scnc;
wifi_country_t wc;
int ix;
roflash static const char auth_mode[][20] =
{
"OTHER",
"WEP",
"WPA PSK",
"WPA2 PSK",
"WPA PSK + WPA2 PSK"
};
wifi_station_get_config_default(&config);
wifi_get_country(&wc);
string_append(dst, "> current wlan state: ");
if(flags.reconnect_mode)
string_append(dst, "in reconnect mode");
else
if(flags.recovery_mode)
string_append(dst, "in recovery mode");
else
if(flags.associated)
string_append(dst, "associated");
else
string_append(dst, "not associated");
if(flags.bootstrap_mode)
string_append(dst, ", in bootstrap mode");
string_format(dst, " for %u sec\n", association_state_time);
string_format(dst, "> ssid in flash: \"%s\", passwd: \"%s\"\n",
config.ssid, config.password);
wifi_station_get_config(&config);
wc.cc[2] = '\0';
string_format(dst, "> ssid active: \"%s\", passwd: \"%s\"\n",
config.ssid, config.password);
string_format(dst,
"> channel: %u\n"
"> phy mode: %s\n"
"> sleep mode: %s\n"
"> max sleep level: %s\n"
"> listen interval: %d\n"
"> signal strength: %d dB\n"
"> country: %s [%d - %d]\n"
">\n",
wifi_get_channel(),
phy[wifi_get_phy_mode()],
slp[wifi_get_sleep_type()],
onoff(wifi_get_sleep_level()),
wifi_get_listen_interval(),
wifi_station_get_rssi(),
wc.cc, wc.schan, wc.schan + wc.nchan);
wifi_get_ip_info(SOFTAP_IF, &ip_addr_info);
string_append(dst, "> ap mac address: ");
wifi_get_macaddr(SOFTAP_IF, mac_addr);
mac_to_string(dst, mac_addr);
string_append(dst, "\n");
string_append(dst, "> ap ip address: ");
string_ip(dst, ip_addr_info.ip);
string_append(dst, "\n");
string_append(dst, "> ap gateway: ");
string_ip(dst, ip_addr_info.gw);
string_append(dst, "\n");
string_append(dst, "> ap ip netmask: ");
string_ip(dst, ip_addr_info.netmask);
string_append(dst, "\n");
string_append(dst, ">\n");
wifi_get_ip_info(STATION_IF, &ip_addr_info);
string_append(dst, "> station mac address: ");
wifi_get_macaddr(STATION_IF, mac_addr);
mac_to_string(dst, mac_addr);
string_append(dst, "\n");
string_append(dst, "> station ip address: ");
string_ip(dst, ip_addr_info.ip);
string_append(dst, "\n");
string_append(dst, "> station gateway: ");
string_ip(dst, ip_addr_info.gw);
string_append(dst, "\n");
string_append(dst, "> station ip netmask: ");
string_ip(dst, ip_addr_info.netmask);
string_append(dst, "\n>\n");
memset(sc, 0, sizeof(sc));
if((scn = wifi_station_get_ap_info(sc)) < 1)
string_append(dst, "> no ap info\n");
else
{
scnc = wifi_station_get_current_ap_id();
for(scni = 0; scni < scn; scni++)
{
scp = &sc[scni];
string_format(dst, "> ap %c#%u: %s/%s@%d, %02x:%02x:%02x:%02x:%02x:%02x %d, %d, %d, %d, ",
scnc == scni ? '*' : ' ',
scni,
scp->ssid, scp->password,
scp->channel,
scp->bssid[0], scp->bssid[1], scp->bssid[2],
scp->bssid[3], scp->bssid[4], scp->bssid[5],
scp->threshold.rssi,
scp->bssid_set,
scp->open_and_wep_mode_disable,
scp->all_channel_scan);
string_append_cstr_flash(dst, auth_mode[scp->threshold.authmode]);
string_format(dst, "\n");
}
}
string_append(dst, ">\n> access points selection\n>\n");
for(ix = 0; ix < access_points.entries; ix++)
{
const access_point_t *ap = &access_points.ap[ix];
string_format(dst, "> %c ch: %2d, rssi: %3d, bssid: %02x:%02x:%02x:%02x:%02x:%02x\n",
(ix == access_points.selected) ? '*' : ' ',
ap->channel,
ap->rssi,
ap->mac[0],
ap->mac[1],
ap->mac[2],
ap->mac[3],
ap->mac[4],
ap->mac[5]);
}
}
app_action_t application_function_stats_wlan(app_params_t *parameters)
{
stats_wlan(parameters->dst);
return(app_action_normal);
}
static void wlan_scan_done_callback(void *arg, STATUS status)
{
roflash static const char auth_mode_msg[][16] =
{
"OTHER",
"WEP",
"WPA1-PSK",
"WPA2-PSK",
"WPA1/2-PSK"
};
roflash static const char cipher_type[][16] =
{
"NONE",
"WEP40",
"WEP104",
"TKIP",
"AES",
"TKIP/AES",
"UNKNOWN",
};
roflash static const char fmt_string_1[] = "> %-16s %-3s %-4s %-10s %-9s %-9s %-4s %s\n";
roflash static const char fmt_string_2[] = "> %c%-16s %3u %4d %-10s %-9s %-9s %4d %02x:%02x:%02x:%02x:%02x:%02x\n";
struct bss_info *bss;
const char *ssid;
char status_string[32];
char auth_mode_string[32];
char pairwise_cipher_string[32];
char groupwise_cipher_string[32];
bool selected;
flash_to_dram(true, status <= CANCEL ? status_msg[status] : "<invalid>", status_string, sizeof(status_string));
logbuffer_clear(); // make sure as much room is available as is possible
log("wlan scan result: %s\n", status_string);
log_from_flash_n(fmt_string_1, "SSID", "CHN", "RSSI", "AUTH", "PAIR", "GROUP", "OFFS", "BSSID");
for(bss = arg; bss; bss = bss->next.stqe_next)
{
if(strcmp(access_points.ssid, "") && !strncmp((const char *)bss->ssid, access_points.ssid, sizeof(access_points.ssid)))
selected = true;
else
selected = false;
if(!strcmp((const char *)bss->ssid, ""))
ssid = "<hidden>";
else
ssid = (const char *)bss->ssid;
flash_to_dram(true, bss->authmode < AUTH_MAX ? auth_mode_msg[bss->authmode] : "<invalid>", auth_mode_string, sizeof(auth_mode_string));
flash_to_dram(true, cipher_type[bss->pairwise_cipher], pairwise_cipher_string, sizeof(pairwise_cipher_string));
flash_to_dram(true, cipher_type[bss->group_cipher], groupwise_cipher_string, sizeof(groupwise_cipher_string));
log_from_flash_n(fmt_string_2,
selected ? '*' : ' ',
ssid,
bss->channel,
bss->rssi,
auth_mode_string,
pairwise_cipher_string, groupwise_cipher_string,
bss->freq_offset,
bss->bssid[0], bss->bssid[1], bss->bssid[2], bss->bssid[3], bss->bssid[4], bss->bssid[5]);
}
}
app_action_t application_function_wlan_scan(app_params_t *parameters)
{
struct scan_config sc =
{
.ssid = (char *)0,
.bssid = (char *)0,
.channel = 0,
.show_hidden = 1,
.scan_type = WIFI_SCAN_TYPE_ACTIVE,
.scan_time.active.min = 100,
.scan_time.active.max = 1000,
};
string_new(, ssid, 64);
string_clear(&ssid);
if(parse_string(1, parameters->src, &ssid, ' ') == parse_ok)
{
if(string_match_cstr(&ssid, "-"))
sc.ssid = access_points.ssid;
else
{
string_to_cstr(&ssid); // ensure \0
sc.ssid = string_buffer_nonconst(&ssid);
}
}
wifi_station_scan(&sc, wlan_scan_done_callback);
string_append(parameters->dst, "wlan scan started, see log to retrieve the results\n");
return(app_action_normal);
}
app_action_t application_function_wlan_ap_configure(app_params_t *parameters)
{
unsigned int channel;
string_new(, ssid, 64);
string_new(, passwd, 64);
if((parse_string(1, parameters->src, &ssid, ' ') == parse_ok) && (parse_string(2, parameters->src, &passwd, ' ') == parse_ok) &&
(parse_uint(3, parameters->src, &channel, 0, ' ') == parse_ok))
{
if((channel < 1) || (channel > 13))
{
string_format(parameters->dst, "> channel %u out of range (1-13)\n", channel);
return(app_action_error);
}
if(string_length(&passwd) < 8)
{
string_format(parameters->dst, "> passwd \"%s\" too short (length must be >= 8)\n",
string_to_cstr(&passwd));
return(app_action_error);
}
if(!config_open_write())
{
string_append(parameters->dst, "> cannot set config (open)\n");
return(app_action_error);
}
if(!config_set_string("wlan.ap.ssid", string_to_cstr(&ssid), -1, -1))
{
config_abort_write();
string_append(parameters->dst, "> cannot set config (set ssid)\n");
return(app_action_error);
}
if(!config_set_string("wlan.ap.passwd", string_to_cstr(&passwd), -1, -1))
{
config_abort_write();
string_append(parameters->dst, "> cannot set config (passwd)\n");
return(app_action_error);
}
if(!config_set_int("wlan.ap.channel", channel, -1, -1))
{
config_abort_write();
string_append(parameters->dst, "> cannot set config (channel)\n");
return(app_action_error);
}
if(!config_open_write())
{
string_append(parameters->dst, "> cannot set config (close)\n");
return(app_action_error);
}
}
string_clear(&ssid);
string_clear(&passwd);
if(!config_get_string("wlan.ap.ssid", &ssid, -1, -1))
{
string_clear(&ssid);
string_append(&ssid, "<empty>");
}
if(!config_get_string("wlan.ap.passwd", &passwd, -1, -1))
{
string_clear(&passwd);
string_append(&passwd, "<empty>");
}
if(!config_get_uint("wlan.ap.channel", &channel, -1, -1))
channel = 0;
string_format(parameters->dst, "> ssid: \"%s\", passwd: \"%s\", channel: %u\n",
string_to_cstr(&ssid), string_to_cstr(&passwd), channel);
return(app_action_normal);
}
app_action_t application_function_wlan_client_configure(app_params_t *parameters)
{
string_new(, ssid, 64);
string_new(, passwd, 64);
if((parse_string(1, parameters->src, &ssid, ' ') == parse_ok) && (parse_string(2, parameters->src, &passwd, ' ') == parse_ok))
{
if(string_length(&passwd) < 8)
{
string_format(parameters->dst, "> passwd \"%s\" too short (length must be >= 8)\n", string_to_cstr(&passwd));
return(app_action_error);
}
if(!wlan_client_configure(parameters->dst, string_to_cstr(&ssid), string_to_cstr(&passwd)))
return(app_action_error);
}
string_clear(&ssid);
string_clear(&passwd);
if(!config_get_string("wlan.client.ssid", &ssid, -1, -1))
{
string_clear(&ssid);
string_append(&ssid, "<empty>");
}
if(!config_get_string("wlan.client.passwd", &passwd, -1, -1))
{
string_clear(&passwd);
string_append(&passwd, "<empty>");
}
string_format(parameters->dst, "> ssid: \"%s\", passwd: \"%s\"\n",
string_to_cstr(&ssid), string_to_cstr(&passwd));
return(app_action_normal);
}
app_action_t application_function_wlan_mode(app_params_t *parameters)
{
unsigned int int_mode;
config_wlan_mode_t mode;
if(parse_string(1, parameters->src, parameters->dst, ' ') == parse_ok)
{
if(string_match_cstr(parameters->dst, "client"))
{
string_clear(parameters->dst);
if(!config_open_write() ||
!config_set_int("wlan.mode", config_wlan_mode_client, -1, -1) ||
!config_close_write())
{
config_abort_write();
string_append(parameters->dst, "> cannot set config\n");
return(app_action_error);
}
wlan_reconnect();
return(app_action_disconnect);
}
if(string_match_cstr(parameters->dst, "ap"))
{
string_clear(parameters->dst);
if(!config_open_write() ||
!config_set_int("wlan.mode", config_wlan_mode_ap, -1, -1) ||
!config_close_write())
{
config_abort_write();
string_append(parameters->dst, "> cannot set config\n");
return(app_action_error);
}
wlan_reconnect();
return(app_action_disconnect);
}
string_append(parameters->dst, ": invalid wlan mode\n");
return(app_action_error);
}
string_clear(parameters->dst);
string_append(parameters->dst, "> current mode: ");
if(config_get_uint("wlan.mode", &int_mode, -1, -1))
{
mode = (config_wlan_mode_t)int_mode;
switch(mode)
{
case(config_wlan_mode_client):
{
string_append(parameters->dst, "client mode");
break;
}
case(config_wlan_mode_ap):
{
string_append(parameters->dst, "ap mode");
break;
}
default:
{
string_append(parameters->dst, "unknown mode");
break;
}
}
}
else
string_append(parameters->dst, "mode unset");
string_append(parameters->dst, "\n");
return(app_action_normal);
}