forked from nrfconnect/sdk-zephyr
/
wifi_shell.c
1931 lines (1645 loc) · 54.2 KB
/
wifi_shell.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
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
/** @file
* @brief WiFi shell module
*/
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(net_wifi_shell, LOG_LEVEL_INF);
#include <zephyr/kernel.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zephyr/shell/shell.h>
#include <zephyr/sys/printk.h>
#include <zephyr/init.h>
#include <zephyr/net/net_if.h>
#include <zephyr/net/net_event.h>
#include <zephyr/net/wifi_mgmt.h>
#include <zephyr/net/wifi_utils.h>
#include <zephyr/posix/unistd.h>
#include <zephyr/sys/slist.h>
#include "net_private.h"
#define WIFI_SHELL_MODULE "wifi"
#define WIFI_SHELL_MGMT_EVENTS_COMMON (NET_EVENT_WIFI_SCAN_DONE |\
NET_EVENT_WIFI_CONNECT_RESULT |\
NET_EVENT_WIFI_DISCONNECT_RESULT | \
NET_EVENT_WIFI_TWT |\
NET_EVENT_WIFI_RAW_SCAN_RESULT |\
NET_EVENT_WIFI_AP_ENABLE_RESULT |\
NET_EVENT_WIFI_AP_DISABLE_RESULT |\
NET_EVENT_WIFI_AP_STA_CONNECTED |\
NET_EVENT_WIFI_AP_STA_DISCONNECTED)
#ifdef CONFIG_WIFI_MGMT_RAW_SCAN_RESULTS_ONLY
#define WIFI_SHELL_MGMT_EVENTS (WIFI_SHELL_MGMT_EVENTS_COMMON)
#else
#define WIFI_SHELL_MGMT_EVENTS (WIFI_SHELL_MGMT_EVENTS_COMMON |\
NET_EVENT_WIFI_SCAN_RESULT)
#endif /* CONFIG_WIFI_MGMT_RAW_SCAN_RESULTS_ONLY */
static struct {
const struct shell *sh;
union {
struct {
uint8_t connecting : 1;
uint8_t disconnecting : 1;
uint8_t _unused : 6;
};
uint8_t all;
};
} context;
static uint32_t scan_result;
static struct net_mgmt_event_callback wifi_shell_mgmt_cb;
static K_MUTEX_DEFINE(wifi_ap_sta_list_lock);
struct wifi_ap_sta_node {
bool valid;
struct wifi_ap_sta_info sta_info;
};
static struct wifi_ap_sta_node sta_list[CONFIG_WIFI_SHELL_MAX_AP_STA];
#define print(sh, level, fmt, ...) \
do { \
if (sh) { \
shell_fprintf(sh, level, fmt, ##__VA_ARGS__); \
} else { \
printk(fmt, ##__VA_ARGS__); \
} \
} while (false)
static bool parse_number(const struct shell *sh, long *param, char *str, long min, long max)
{
char *endptr;
char *str_tmp = str;
long num = 0;
if ((str_tmp[0] == '0') && (str_tmp[1] == 'x')) {
/* Hexadecimal numbers take base 0 in strtol */
num = strtol(str_tmp, &endptr, 0);
} else {
num = strtol(str_tmp, &endptr, 10);
}
if (*endptr != '\0') {
print(sh, SHELL_ERROR, "Invalid number: %s", str_tmp);
return false;
}
if ((num) < (min) || (num) > (max)) {
print(sh, SHELL_WARNING, "Value out of range: %s, (%ld-%ld)", str_tmp, min, max);
return false;
}
*param = num;
return true;
}
static void handle_wifi_scan_result(struct net_mgmt_event_callback *cb)
{
const struct wifi_scan_result *entry =
(const struct wifi_scan_result *)cb->info;
uint8_t mac_string_buf[sizeof("xx:xx:xx:xx:xx:xx")];
scan_result++;
if (scan_result == 1U) {
print(context.sh, SHELL_NORMAL,
"\n%-4s | %-32s %-5s | %-13s | %-4s | %-15s | %-17s | %-8s\n",
"Num", "SSID", "(len)", "Chan (Band)", "RSSI", "Security", "BSSID", "MFP");
}
print(context.sh, SHELL_NORMAL,
"%-4d | %-32s %-5u | %-4u (%-6s) | %-4d | %-15s | %-17s | %-8s\n",
scan_result, entry->ssid, entry->ssid_length, entry->channel,
wifi_band_txt(entry->band),
entry->rssi,
wifi_security_txt(entry->security),
((entry->mac_length) ?
net_sprint_ll_addr_buf(entry->mac, WIFI_MAC_ADDR_LEN,
mac_string_buf,
sizeof(mac_string_buf)) : ""),
wifi_mfp_txt(entry->mfp));
}
#ifdef CONFIG_WIFI_MGMT_RAW_SCAN_RESULTS
static int wifi_freq_to_channel(int frequency)
{
int channel = 0;
if (frequency == 2484) { /* channel 14 */
channel = 14;
} else if ((frequency <= 2472) && (frequency >= 2412)) {
channel = ((frequency - 2412) / 5) + 1;
} else if ((frequency <= 5320) && (frequency >= 5180)) {
channel = ((frequency - 5180) / 5) + 36;
} else if ((frequency <= 5720) && (frequency >= 5500)) {
channel = ((frequency - 5500) / 5) + 100;
} else if ((frequency <= 5895) && (frequency >= 5745)) {
channel = ((frequency - 5745) / 5) + 149;
} else {
channel = frequency;
}
return channel;
}
static enum wifi_frequency_bands wifi_freq_to_band(int frequency)
{
enum wifi_frequency_bands band = WIFI_FREQ_BAND_2_4_GHZ;
if ((frequency >= 2401) && (frequency <= 2495)) {
band = WIFI_FREQ_BAND_2_4_GHZ;
} else if ((frequency >= 5170) && (frequency <= 5895)) {
band = WIFI_FREQ_BAND_5_GHZ;
} else {
band = WIFI_FREQ_BAND_6_GHZ;
}
return band;
}
static void handle_wifi_raw_scan_result(struct net_mgmt_event_callback *cb)
{
struct wifi_raw_scan_result *raw =
(struct wifi_raw_scan_result *)cb->info;
int channel;
int band;
int rssi;
int i = 0;
uint8_t mac_string_buf[sizeof("xx:xx:xx:xx:xx:xx")];
scan_result++;
if (scan_result == 1U) {
print(context.sh, SHELL_NORMAL,
"\n%-4s | %-13s | %-4s | %-15s | %-15s | %-32s\n",
"Num", "Channel (Band)", "RSSI", "BSSID", "Frame length", "Frame Body");
}
rssi = raw->rssi;
channel = wifi_freq_to_channel(raw->frequency);
band = wifi_freq_to_band(raw->frequency);
print(context.sh, SHELL_NORMAL, "%-4d | %-4u (%-6s) | %-4d | %s | %-4d ",
scan_result,
channel,
wifi_band_txt(band),
rssi,
net_sprint_ll_addr_buf(raw->data + 10, WIFI_MAC_ADDR_LEN, mac_string_buf,
sizeof(mac_string_buf)), raw->frame_length);
for (i = 0; i < 32; i++) {
print(context.sh, SHELL_NORMAL, "%02X ", *(raw->data + i));
}
print(context.sh, SHELL_NORMAL, "\n");
}
#endif /* CONFIG_WIFI_MGMT_RAW_SCAN_RESULTS */
static void handle_wifi_scan_done(struct net_mgmt_event_callback *cb)
{
const struct wifi_status *status =
(const struct wifi_status *)cb->info;
if (status->status) {
print(context.sh, SHELL_WARNING,
"Scan request failed (%d)\n", status->status);
} else {
print(context.sh, SHELL_NORMAL, "Scan request done\n");
}
scan_result = 0U;
}
static void handle_wifi_connect_result(struct net_mgmt_event_callback *cb)
{
const struct wifi_status *status =
(const struct wifi_status *) cb->info;
if (status->status) {
print(context.sh, SHELL_WARNING,
"Connection request failed (%d)\n", status->status);
} else {
print(context.sh, SHELL_NORMAL, "Connected\n");
}
context.connecting = false;
}
static void handle_wifi_disconnect_result(struct net_mgmt_event_callback *cb)
{
const struct wifi_status *status =
(const struct wifi_status *) cb->info;
if (context.disconnecting) {
print(context.sh,
status->status ? SHELL_WARNING : SHELL_NORMAL,
"Disconnection request %s (%d)\n",
status->status ? "failed" : "done",
status->status);
context.disconnecting = false;
} else {
print(context.sh, SHELL_NORMAL, "Disconnected\n");
}
}
static void print_twt_params(uint8_t dialog_token, uint8_t flow_id,
enum wifi_twt_negotiation_type negotiation_type,
bool responder, bool implicit, bool announce,
bool trigger, uint32_t twt_wake_interval,
uint64_t twt_interval)
{
print(context.sh, SHELL_NORMAL, "TWT Dialog token: %d\n",
dialog_token);
print(context.sh, SHELL_NORMAL, "TWT flow ID: %d\n",
flow_id);
print(context.sh, SHELL_NORMAL, "TWT negotiation type: %s\n",
wifi_twt_negotiation_type_txt(negotiation_type));
print(context.sh, SHELL_NORMAL, "TWT responder: %s\n",
responder ? "true" : "false");
print(context.sh, SHELL_NORMAL, "TWT implicit: %s\n",
implicit ? "true" : "false");
print(context.sh, SHELL_NORMAL, "TWT announce: %s\n",
announce ? "true" : "false");
print(context.sh, SHELL_NORMAL, "TWT trigger: %s\n",
trigger ? "true" : "false");
print(context.sh, SHELL_NORMAL, "TWT wake interval: %d us\n",
twt_wake_interval);
print(context.sh, SHELL_NORMAL, "TWT interval: %lld us\n",
twt_interval);
print(context.sh, SHELL_NORMAL, "========================\n");
}
static void handle_wifi_twt_event(struct net_mgmt_event_callback *cb)
{
const struct wifi_twt_params *resp =
(const struct wifi_twt_params *)cb->info;
if (resp->operation == WIFI_TWT_TEARDOWN) {
print(context.sh, SHELL_NORMAL, "TWT teardown received for flow ID %d\n",
resp->flow_id);
return;
}
if (resp->resp_status == WIFI_TWT_RESP_RECEIVED) {
print(context.sh, SHELL_NORMAL, "TWT response: %s\n",
wifi_twt_setup_cmd_txt(resp->setup_cmd));
print(context.sh, SHELL_NORMAL, "== TWT negotiated parameters ==\n");
print_twt_params(resp->dialog_token,
resp->flow_id,
resp->negotiation_type,
resp->setup.responder,
resp->setup.implicit,
resp->setup.announce,
resp->setup.trigger,
resp->setup.twt_wake_interval,
resp->setup.twt_interval);
} else {
print(context.sh, SHELL_NORMAL, "TWT response timed out\n");
}
}
static void handle_wifi_ap_enable_result(struct net_mgmt_event_callback *cb)
{
const struct wifi_status *status =
(const struct wifi_status *)cb->info;
if (status->status) {
print(context.sh, SHELL_WARNING,
"AP enable request failed (%d)\n", status->status);
} else {
print(context.sh, SHELL_NORMAL, "AP enabled\n");
}
}
static void handle_wifi_ap_disable_result(struct net_mgmt_event_callback *cb)
{
const struct wifi_status *status =
(const struct wifi_status *)cb->info;
if (status->status) {
print(context.sh, SHELL_WARNING,
"AP disable request failed (%d)\n", status->status);
} else {
print(context.sh, SHELL_NORMAL, "AP disabled\n");
}
k_mutex_lock(&wifi_ap_sta_list_lock, K_FOREVER);
memset(&sta_list, 0, sizeof(sta_list));
k_mutex_unlock(&wifi_ap_sta_list_lock);
}
static void handle_wifi_ap_sta_connected(struct net_mgmt_event_callback *cb)
{
const struct wifi_ap_sta_info *sta_info =
(const struct wifi_ap_sta_info *)cb->info;
uint8_t mac_string_buf[sizeof("xx:xx:xx:xx:xx:xx")];
int i;
print(context.sh, SHELL_NORMAL, "Station connected: %s\n",
net_sprint_ll_addr_buf(sta_info->mac, WIFI_MAC_ADDR_LEN,
mac_string_buf, sizeof(mac_string_buf)));
k_mutex_lock(&wifi_ap_sta_list_lock, K_FOREVER);
for (i = 0; i < CONFIG_WIFI_SHELL_MAX_AP_STA; i++) {
if (!sta_list[i].valid) {
sta_list[i].sta_info = *sta_info;
sta_list[i].valid = true;
break;
}
}
if (i == CONFIG_WIFI_SHELL_MAX_AP_STA) {
print(context.sh, SHELL_WARNING, "No space to store station info: "
"Increase CONFIG_WIFI_SHELL_MAX_AP_STA\n");
}
k_mutex_unlock(&wifi_ap_sta_list_lock);
}
static void handle_wifi_ap_sta_disconnected(struct net_mgmt_event_callback *cb)
{
const struct wifi_ap_sta_info *sta_info =
(const struct wifi_ap_sta_info *)cb->info;
uint8_t mac_string_buf[sizeof("xx:xx:xx:xx:xx:xx")];
print(context.sh, SHELL_NORMAL, "Station disconnected: %s\n",
net_sprint_ll_addr_buf(sta_info->mac, WIFI_MAC_ADDR_LEN,
mac_string_buf, sizeof(mac_string_buf)));
k_mutex_lock(&wifi_ap_sta_list_lock, K_FOREVER);
for (int i = 0; i < CONFIG_WIFI_SHELL_MAX_AP_STA; i++) {
if (!sta_list[i].valid) {
continue;
}
if (!memcmp(sta_list[i].sta_info.mac, sta_info->mac,
WIFI_MAC_ADDR_LEN)) {
sta_list[i].valid = false;
break;
}
}
k_mutex_unlock(&wifi_ap_sta_list_lock);
}
static void wifi_mgmt_event_handler(struct net_mgmt_event_callback *cb,
uint32_t mgmt_event, struct net_if *iface)
{
switch (mgmt_event) {
case NET_EVENT_WIFI_SCAN_RESULT:
handle_wifi_scan_result(cb);
break;
case NET_EVENT_WIFI_SCAN_DONE:
handle_wifi_scan_done(cb);
break;
case NET_EVENT_WIFI_CONNECT_RESULT:
handle_wifi_connect_result(cb);
break;
case NET_EVENT_WIFI_DISCONNECT_RESULT:
handle_wifi_disconnect_result(cb);
break;
case NET_EVENT_WIFI_TWT:
handle_wifi_twt_event(cb);
break;
#ifdef CONFIG_WIFI_MGMT_RAW_SCAN_RESULTS
case NET_EVENT_WIFI_RAW_SCAN_RESULT:
handle_wifi_raw_scan_result(cb);
break;
#endif /* CONFIG_WIFI_MGMT_RAW_SCAN_RESULTS */
case NET_EVENT_WIFI_AP_ENABLE_RESULT:
handle_wifi_ap_enable_result(cb);
break;
case NET_EVENT_WIFI_AP_DISABLE_RESULT:
handle_wifi_ap_disable_result(cb);
break;
case NET_EVENT_WIFI_AP_STA_CONNECTED:
handle_wifi_ap_sta_connected(cb);
break;
case NET_EVENT_WIFI_AP_STA_DISCONNECTED:
handle_wifi_ap_sta_disconnected(cb);
break;
default:
break;
}
}
static int __wifi_args_to_params(size_t argc, char *argv[],
struct wifi_connect_req_params *params)
{
char *endptr;
int idx = 1;
if (argc < 1) {
return -EINVAL;
}
/* Defaults */
params->band = WIFI_FREQ_BAND_UNKNOWN;
params->channel = WIFI_CHANNEL_ANY;
params->security = WIFI_SECURITY_TYPE_NONE;
/* SSID */
params->ssid = argv[0];
params->ssid_length = strlen(params->ssid);
if (params->ssid_length > WIFI_SSID_MAX_LEN) {
return -EINVAL;
}
/* Channel (optional) */
if ((idx < argc) && (strlen(argv[idx]) <= 3)) {
params->channel = strtol(argv[idx], &endptr, 10);
if (*endptr != '\0') {
return -EINVAL;
}
if (params->channel == 0U) {
params->channel = WIFI_CHANNEL_ANY;
}
idx++;
}
/* PSK (optional) */
if (idx < argc) {
params->psk = argv[idx];
params->psk_length = strlen(argv[idx]);
/* Defaults */
params->security = WIFI_SECURITY_TYPE_PSK;
params->mfp = WIFI_MFP_OPTIONAL;
idx++;
/* Security type (optional) */
if (idx < argc) {
unsigned int security = strtol(argv[idx], &endptr, 10);
if (security <= WIFI_SECURITY_TYPE_MAX) {
params->security = security;
}
idx++;
/* MFP (optional) */
if (idx < argc) {
unsigned int mfp = strtol(argv[idx], &endptr, 10);
if (mfp <= WIFI_MFP_REQUIRED) {
params->mfp = mfp;
}
idx++;
}
}
if (params->psk_length < WIFI_PSK_MIN_LEN ||
(params->security != WIFI_SECURITY_TYPE_SAE &&
params->psk_length > WIFI_PSK_MAX_LEN) ||
(params->security == WIFI_SECURITY_TYPE_SAE &&
params->psk_length > WIFI_SAE_PSWD_MAX_LEN)) {
return -EINVAL;
}
}
return 0;
}
static int cmd_wifi_connect(const struct shell *sh, size_t argc,
char *argv[])
{
struct net_if *iface = net_if_get_first_wifi();
struct wifi_connect_req_params cnx_params = { 0 };
if (__wifi_args_to_params(argc - 1, &argv[1], &cnx_params)) {
shell_help(sh);
return -ENOEXEC;
}
context.connecting = true;
context.sh = sh;
if (net_mgmt(NET_REQUEST_WIFI_CONNECT, iface,
&cnx_params, sizeof(struct wifi_connect_req_params))) {
shell_fprintf(sh, SHELL_WARNING,
"Connection request failed\n");
context.connecting = false;
return -ENOEXEC;
}
shell_fprintf(sh, SHELL_NORMAL, "Connection requested\n");
return 0;
}
static int cmd_wifi_disconnect(const struct shell *sh, size_t argc,
char *argv[])
{
struct net_if *iface = net_if_get_first_wifi();
int status;
context.disconnecting = true;
context.sh = sh;
status = net_mgmt(NET_REQUEST_WIFI_DISCONNECT, iface, NULL, 0);
if (status) {
context.disconnecting = false;
if (status == -EALREADY) {
shell_fprintf(sh, SHELL_INFO,
"Already disconnected\n");
} else {
shell_fprintf(sh, SHELL_WARNING,
"Disconnect request failed\n");
return -ENOEXEC;
}
} else {
shell_fprintf(sh, SHELL_NORMAL,
"Disconnect requested\n");
}
return 0;
}
static int wifi_scan_args_to_params(const struct shell *sh,
size_t argc,
char *argv[],
struct wifi_scan_params *params,
bool *do_scan)
{
struct getopt_state *state;
int opt;
static struct option long_options[] = {{"type", required_argument, 0, 't'},
{"bands", required_argument, 0, 'b'},
{"dwell_time_active", required_argument, 0, 'a'},
{"dwell_time_passive", required_argument, 0, 'p'},
{"ssid", required_argument, 0, 's'},
{"max_bss", required_argument, 0, 'm'},
{"chans", required_argument, 0, 'c'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}};
int opt_index = 0;
int val;
int opt_num = 0;
*do_scan = true;
while ((opt = getopt_long(argc, argv, "t:b:a:p:s:m:c:h", long_options, &opt_index)) != -1) {
state = getopt_state_get();
switch (opt) {
case 't':
if (!strcmp(optarg, "passive")) {
params->scan_type = WIFI_SCAN_TYPE_PASSIVE;
} else if (!strcmp(optarg, "active")) {
params->scan_type = WIFI_SCAN_TYPE_ACTIVE;
} else {
shell_fprintf(sh, SHELL_ERROR, "Invalid scan type %s\n", optarg);
return -ENOEXEC;
}
opt_num++;
break;
case 'b':
if (wifi_utils_parse_scan_bands(optarg, ¶ms->bands)) {
shell_fprintf(sh, SHELL_ERROR, "Invalid band value(s)\n");
return -ENOEXEC;
}
opt_num++;
break;
case 'a':
val = atoi(optarg);
if ((val < 5) || (val > 1000)) {
shell_fprintf(sh, SHELL_ERROR, "Invalid dwell_time_active val\n");
return -ENOEXEC;
}
params->dwell_time_active = val;
opt_num++;
break;
case 'p':
val = atoi(optarg);
if ((val < 10) || (val > 1000)) {
shell_fprintf(sh, SHELL_ERROR, "Invalid dwell_time_passive val\n");
return -ENOEXEC;
}
params->dwell_time_passive = val;
opt_num++;
break;
case 's':
if (wifi_utils_parse_scan_ssids(optarg,
params->ssids,
ARRAY_SIZE(params->ssids))) {
shell_fprintf(sh, SHELL_ERROR, "Invalid SSID(s)\n");
return -ENOEXEC;
}
opt_num++;
break;
case 'm':
val = atoi(optarg);
if ((val < 0) || (val > 65535)) {
shell_fprintf(sh, SHELL_ERROR, "Invalid max_bss val\n");
return -ENOEXEC;
}
params->max_bss_cnt = val;
opt_num++;
break;
case 'c':
if (wifi_utils_parse_scan_chan(optarg,
params->band_chan,
ARRAY_SIZE(params->band_chan))) {
shell_fprintf(sh,
SHELL_ERROR,
"Invalid band or channel value(s)\n");
return -ENOEXEC;
}
opt_num++;
break;
case 'h':
shell_help(sh);
*do_scan = false;
opt_num++;
break;
case '?':
default:
shell_fprintf(sh, SHELL_ERROR, "Invalid option or option usage: %s\n",
argv[opt_index + 1]);
return -ENOEXEC;
}
}
return opt_num;
}
static int cmd_wifi_scan(const struct shell *sh, size_t argc, char *argv[])
{
struct net_if *iface = net_if_get_first_wifi();
struct wifi_scan_params params = { 0 };
bool do_scan = true;
int opt_num;
context.sh = sh;
if (argc > 1) {
opt_num = wifi_scan_args_to_params(sh, argc, argv, ¶ms, &do_scan);
if (opt_num < 0) {
shell_help(sh);
return -ENOEXEC;
} else if (!opt_num) {
shell_fprintf(sh, SHELL_WARNING, "No valid option(s) found\n");
do_scan = false;
}
}
if (do_scan) {
if (net_mgmt(NET_REQUEST_WIFI_SCAN, iface, ¶ms, sizeof(params))) {
shell_fprintf(sh, SHELL_WARNING, "Scan request failed\n");
return -ENOEXEC;
}
shell_fprintf(sh, SHELL_NORMAL, "Scan requested\n");
return 0;
}
shell_fprintf(sh, SHELL_WARNING, "Scan not initiated\n");
return -ENOEXEC;
}
static int cmd_wifi_status(const struct shell *sh, size_t argc, char *argv[])
{
struct net_if *iface = net_if_get_first_wifi();
struct wifi_iface_status status = { 0 };
context.sh = sh;
if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, iface, &status,
sizeof(struct wifi_iface_status))) {
shell_fprintf(sh, SHELL_WARNING, "Status request failed\n");
return -ENOEXEC;
}
shell_fprintf(sh, SHELL_NORMAL, "Status: successful\n");
shell_fprintf(sh, SHELL_NORMAL, "==================\n");
shell_fprintf(sh, SHELL_NORMAL, "State: %s\n", wifi_state_txt(status.state));
if (status.state >= WIFI_STATE_ASSOCIATED) {
uint8_t mac_string_buf[sizeof("xx:xx:xx:xx:xx:xx")];
shell_fprintf(sh, SHELL_NORMAL, "Interface Mode: %s\n",
wifi_mode_txt(status.iface_mode));
shell_fprintf(sh, SHELL_NORMAL, "Link Mode: %s\n",
wifi_link_mode_txt(status.link_mode));
shell_fprintf(sh, SHELL_NORMAL, "SSID: %.32s\n", status.ssid);
shell_fprintf(sh, SHELL_NORMAL, "BSSID: %s\n",
net_sprint_ll_addr_buf(status.bssid,
WIFI_MAC_ADDR_LEN, mac_string_buf,
sizeof(mac_string_buf))
);
shell_fprintf(sh, SHELL_NORMAL, "Band: %s\n",
wifi_band_txt(status.band));
shell_fprintf(sh, SHELL_NORMAL, "Channel: %d\n", status.channel);
shell_fprintf(sh, SHELL_NORMAL, "Security: %s\n",
wifi_security_txt(status.security));
shell_fprintf(sh, SHELL_NORMAL, "MFP: %s\n",
wifi_mfp_txt(status.mfp));
if (status.iface_mode == WIFI_MODE_INFRA) {
shell_fprintf(sh, SHELL_NORMAL, "RSSI: %d\n", status.rssi);
}
shell_fprintf(sh, SHELL_NORMAL, "Beacon Interval: %d\n", status.beacon_interval);
shell_fprintf(sh, SHELL_NORMAL, "DTIM: %d\n", status.dtim_period);
shell_fprintf(sh, SHELL_NORMAL, "TWT: %s\n",
status.twt_capable ? "Supported" : "Not supported");
}
return 0;
}
#if defined(CONFIG_NET_STATISTICS_WIFI) && \
defined(CONFIG_NET_STATISTICS_USER_API)
static void print_wifi_stats(struct net_if *iface, struct net_stats_wifi *data,
const struct shell *sh)
{
shell_fprintf(sh, SHELL_NORMAL, "Statistics for Wi-Fi interface %p [%d]\n", iface,
net_if_get_by_iface(iface));
shell_fprintf(sh, SHELL_NORMAL, "Bytes received : %u\n", data->bytes.received);
shell_fprintf(sh, SHELL_NORMAL, "Bytes sent : %u\n", data->bytes.sent);
shell_fprintf(sh, SHELL_NORMAL, "Packets received : %u\n", data->pkts.rx);
shell_fprintf(sh, SHELL_NORMAL, "Packets sent : %u\n", data->pkts.tx);
shell_fprintf(sh, SHELL_NORMAL, "Receive errors : %u\n", data->errors.rx);
shell_fprintf(sh, SHELL_NORMAL, "Send errors : %u\n", data->errors.tx);
shell_fprintf(sh, SHELL_NORMAL, "Bcast received : %u\n", data->broadcast.rx);
shell_fprintf(sh, SHELL_NORMAL, "Bcast sent : %u\n", data->broadcast.tx);
shell_fprintf(sh, SHELL_NORMAL, "Mcast received : %u\n", data->multicast.rx);
shell_fprintf(sh, SHELL_NORMAL, "Mcast sent : %u\n", data->multicast.tx);
shell_fprintf(sh, SHELL_NORMAL, "Beacons received : %u\n", data->sta_mgmt.beacons_rx);
shell_fprintf(sh, SHELL_NORMAL, "Beacons missed : %u\n",
data->sta_mgmt.beacons_miss);
}
#endif /* CONFIG_NET_STATISTICS_WIFI && CONFIG_NET_STATISTICS_USER_API */
static int cmd_wifi_stats(const struct shell *sh, size_t argc, char *argv[])
{
#if defined(CONFIG_NET_STATISTICS_WIFI) && \
defined(CONFIG_NET_STATISTICS_USER_API)
struct net_if *iface = net_if_get_first_wifi();
struct net_stats_wifi stats = { 0 };
int ret;
ret = net_mgmt(NET_REQUEST_STATS_GET_WIFI, iface,
&stats, sizeof(stats));
if (!ret) {
print_wifi_stats(iface, &stats, sh);
}
#else
ARG_UNUSED(argc);
ARG_UNUSED(argv);
shell_fprintf(sh, SHELL_INFO, "Set %s to enable %s support.\n",
"CONFIG_NET_STATISTICS_WIFI and CONFIG_NET_STATISTICS_USER_API",
"statistics");
#endif /* CONFIG_NET_STATISTICS_WIFI && CONFIG_NET_STATISTICS_USER_API */
return 0;
}
static int cmd_wifi_ps(const struct shell *sh, size_t argc, char *argv[])
{
struct net_if *iface = net_if_get_first_wifi();
struct wifi_ps_params params = { 0 };
context.sh = sh;
if (argc > 2) {
shell_fprintf(sh, SHELL_WARNING, "Invalid number of arguments\n");
return -ENOEXEC;
}
if (argc == 1) {
struct wifi_ps_config config = { 0 };
if (net_mgmt(NET_REQUEST_WIFI_PS_CONFIG, iface,
&config, sizeof(config))) {
shell_fprintf(sh, SHELL_WARNING, "Failed to get PS config\n");
return -ENOEXEC;
}
shell_fprintf(sh, SHELL_NORMAL, "PS status: %s\n",
wifi_ps_txt(config.ps_params.enabled));
if (config.ps_params.enabled) {
shell_fprintf(sh, SHELL_NORMAL, "PS mode: %s\n",
wifi_ps_mode_txt(config.ps_params.mode));
}
shell_fprintf(sh, SHELL_NORMAL, "PS listen_interval: %d\n",
config.ps_params.listen_interval);
shell_fprintf(sh, SHELL_NORMAL, "PS wake up mode: %s\n",
config.ps_params.wakeup_mode ? "Listen interval" : "DTIM");
if (config.ps_params.timeout_ms) {
shell_fprintf(sh, SHELL_NORMAL, "PS timeout: %d ms\n",
config.ps_params.timeout_ms);
} else {
shell_fprintf(sh, SHELL_NORMAL, "PS timeout: disabled\n");
}
if (config.num_twt_flows == 0) {
shell_fprintf(sh, SHELL_NORMAL, "No TWT flows\n");
} else {
for (int i = 0; i < config.num_twt_flows; i++) {
print_twt_params(
config.twt_flows[i].dialog_token,
config.twt_flows[i].flow_id,
config.twt_flows[i].negotiation_type,
config.twt_flows[i].responder,
config.twt_flows[i].implicit,
config.twt_flows[i].announce,
config.twt_flows[i].trigger,
config.twt_flows[i].twt_wake_interval,
config.twt_flows[i].twt_interval);
}
}
return 0;
}
if (!strncmp(argv[1], "on", 2)) {
params.enabled = WIFI_PS_ENABLED;
} else if (!strncmp(argv[1], "off", 3)) {
params.enabled = WIFI_PS_DISABLED;
} else {
shell_fprintf(sh, SHELL_WARNING, "Invalid argument\n");
return -ENOEXEC;
}
params.type = WIFI_PS_PARAM_STATE;
if (net_mgmt(NET_REQUEST_WIFI_PS, iface, ¶ms, sizeof(params))) {
shell_fprintf(sh, SHELL_WARNING,
"PS %s failed. Reason: %s\n",
params.enabled ? "enable" : "disable",
wifi_ps_get_config_err_code_str(params.fail_reason));
return -ENOEXEC;
}
shell_fprintf(sh, SHELL_NORMAL, "%s\n", wifi_ps_txt(params.enabled));
return 0;
}
static int cmd_wifi_ps_mode(const struct shell *sh, size_t argc, char *argv[])
{
struct net_if *iface = net_if_get_first_wifi();
struct wifi_ps_params params = { 0 };
context.sh = sh;
if (!strncmp(argv[1], "legacy", 6)) {
params.mode = WIFI_PS_MODE_LEGACY;
} else if (!strncmp(argv[1], "wmm", 3)) {
params.mode = WIFI_PS_MODE_WMM;
} else {
shell_fprintf(sh, SHELL_WARNING, "Invalid PS mode\n");
return -ENOEXEC;
}
params.type = WIFI_PS_PARAM_MODE;
if (net_mgmt(NET_REQUEST_WIFI_PS, iface, ¶ms, sizeof(params))) {
shell_fprintf(sh, SHELL_WARNING, "%s failed Reason : %s\n",
wifi_ps_mode_txt(params.mode),
wifi_ps_get_config_err_code_str(params.fail_reason));
return -ENOEXEC;
}
shell_fprintf(sh, SHELL_NORMAL, "%s\n", wifi_ps_mode_txt(params.mode));
return 0;
}
static int cmd_wifi_ps_timeout(const struct shell *sh, size_t argc, char *argv[])
{
struct net_if *iface = net_if_get_first_wifi();
struct wifi_ps_params params = { 0 };
long timeout_ms = 0;
int err = 0;
context.sh = sh;
timeout_ms = shell_strtol(argv[1], 10, &err);
if (err) {
shell_error(sh, "Unable to parse input (err %d)", err);
return err;
}
params.timeout_ms = timeout_ms;
params.type = WIFI_PS_PARAM_TIMEOUT;
if (net_mgmt(NET_REQUEST_WIFI_PS, iface, ¶ms, sizeof(params))) {
shell_fprintf(sh, SHELL_WARNING,
"Setting PS timeout failed. Reason : %s\n",
wifi_ps_get_config_err_code_str(params.fail_reason));
return -ENOEXEC;
}
if (params.timeout_ms) {
shell_fprintf(sh, SHELL_NORMAL, "PS timeout: %d ms\n", params.timeout_ms);
} else {
shell_fprintf(sh, SHELL_NORMAL, "PS timeout: disabled\n");
}
return 0;
}
static int cmd_wifi_twt_setup_quick(const struct shell *sh, size_t argc,
char *argv[])
{
struct net_if *iface = net_if_get_first_wifi();
struct wifi_twt_params params = { 0 };
int idx = 1;
long value;
context.sh = sh;
if (argc != 3) {
shell_fprintf(sh, SHELL_WARNING, "Invalid number of arguments\n");
shell_help(sh);
return -ENOEXEC;
}
/* Sensible defaults */
params.operation = WIFI_TWT_SETUP;
params.negotiation_type = WIFI_TWT_INDIVIDUAL;
params.setup_cmd = WIFI_TWT_SETUP_CMD_REQUEST;
params.dialog_token = 1;
params.flow_id = 0;
params.setup.responder = 0;
params.setup.implicit = 1;