@@ -442,73 +442,69 @@ static void wifi_mgmt_event_handler(struct net_mgmt_event_callback *cb,
442
442
}
443
443
}
444
444
445
- static int __wifi_args_to_params (size_t argc , char * argv [],
445
+ static int __wifi_args_to_params (const struct shell * sh , size_t argc , char * argv [],
446
446
struct wifi_connect_req_params * params ,
447
447
enum wifi_iface_mode iface_mode )
448
448
{
449
449
char * endptr ;
450
450
int idx = 1 ;
451
- const struct shell * sh = context .sh ;
451
+ struct getopt_state * state ;
452
+ int opt ;
453
+ bool secure_connection = false;
454
+ static struct option long_options [] = {{"ssid" , required_argument , 0 , 's' },
455
+ {"passphrase" , required_argument , 0 , 'p' },
456
+ {"key-mgmt" , required_argument , 0 , 'k' },
457
+ {"ieee-80211w" , required_argument , 0 , 'w' },
458
+ {"bssid" , required_argument , 0 , 'm' },
459
+ {"band" , required_argument , 0 , 'b' },
460
+ {"channel" , required_argument , 0 , 'c' },
461
+ {"help" , no_argument , 0 , 'h' },
462
+ {0 , 0 , 0 , 0 }};
463
+ int opt_index = 0 ;
464
+ uint8_t band ;
465
+ const uint8_t all_bands [] = {
466
+ WIFI_FREQ_BAND_2_4_GHZ ,
467
+ WIFI_FREQ_BAND_5_GHZ ,
468
+ WIFI_FREQ_BAND_6_GHZ
469
+ };
470
+ bool found = false;
471
+ char bands_str [MAX_BANDS_STR_LEN ] = {0 };
472
+ size_t offset = 0 ;
452
473
453
474
/* Defaults */
454
475
params -> band = WIFI_FREQ_BAND_UNKNOWN ;
455
476
params -> channel = WIFI_CHANNEL_ANY ;
456
477
params -> security = WIFI_SECURITY_TYPE_NONE ;
478
+ params -> mfp = WIFI_MFP_OPTIONAL ;
457
479
458
- /* SSID */
459
- params -> ssid = argv [0 ];
460
- params -> ssid_length = strlen (params -> ssid );
461
- if (params -> ssid_length > WIFI_SSID_MAX_LEN ) {
462
- PR_WARNING ("SSID too long (max %d characters)\n" ,
463
- WIFI_SSID_MAX_LEN );
464
- return - EINVAL ;
465
- }
466
-
467
- /* Channel (optional: STA, mandatory: AP) */
468
- if ((idx < argc ) && (strlen (argv [idx ]) <= 3 )) {
469
- uint8_t band ;
470
- long channel = strtol (argv [idx ], & endptr , 10 );
471
- const uint8_t all_bands [] = {WIFI_FREQ_BAND_2_4_GHZ ,
472
- WIFI_FREQ_BAND_5_GHZ ,
473
- WIFI_FREQ_BAND_6_GHZ };
474
- bool found = false;
475
- char bands_str [MAX_BANDS_STR_LEN ] = {0 };
476
- size_t offset = 0 ;
477
-
478
- if (* endptr != '\0' ) {
479
- PR_ERROR ("Failed to parse channel: %s: endp: %s, err: %s\n" ,
480
- argv [idx ],
481
- endptr ,
482
- strerror (errno ));
483
- return - EINVAL ;
484
- }
485
-
486
- if (iface_mode == WIFI_MODE_INFRA ) {
487
- if (channel < 0 ) {
488
- /* Negative channel means band */
489
- switch (- channel ) {
490
- case 2 :
491
- params -> band = WIFI_FREQ_BAND_2_4_GHZ ;
492
- break ;
493
- case 5 :
494
- params -> band = WIFI_FREQ_BAND_5_GHZ ;
495
- break ;
496
- case 6 :
497
- params -> band = WIFI_FREQ_BAND_6_GHZ ;
498
- break ;
499
- default :
500
- PR_ERROR ("Invalid band: %ld\n" , channel );
501
- return - EINVAL ;
502
- }
503
- }
504
- } else {
505
- if (channel < 0 ) {
506
- PR_ERROR ("Invalid channel: %ld\n" , channel );
480
+ while ((opt = getopt_long (argc , argv , "s:p:k:w:b:c:h" , long_options , & opt_index )) != -1 ) {
481
+ state = getopt_state_get ();
482
+ switch (opt ) {
483
+ case 's' :
484
+ params -> ssid = optarg ;
485
+ params -> ssid_length = strlen (params -> ssid );
486
+ if (params -> ssid_length > WIFI_SSID_MAX_LEN ) {
487
+ PR_WARNING ("SSID too long (max %d characters)\n" ,
488
+ WIFI_SSID_MAX_LEN );
507
489
return - EINVAL ;
508
490
}
509
- }
510
-
511
- if (channel > 0 ) {
491
+ break ;
492
+ case 'k' :
493
+ params -> security = atoi (optarg );
494
+ if (params -> security ) {
495
+ secure_connection = true;
496
+ }
497
+ break ;
498
+ case 'p' :
499
+ if (secure_connection ) {
500
+ params -> psk = optarg ;
501
+ params -> psk_length = strlen (params -> psk );
502
+ } else {
503
+ PR_WARNING ("Passphrase provided without security configuration\n" );
504
+ }
505
+ break ;
506
+ case 'c' :
507
+ long channel = strtol (optarg , & endptr , 10 );
512
508
for (band = 0 ; band < ARRAY_SIZE (all_bands ); band ++ ) {
513
509
offset += snprintf (bands_str + offset ,
514
510
sizeof (bands_str ) - offset ,
@@ -537,58 +533,43 @@ static int __wifi_args_to_params(size_t argc, char *argv[],
537
533
}
538
534
539
535
params -> channel = channel ;
540
- }
541
- idx ++ ;
542
- }
543
-
544
- /* PSK (optional) */
545
- if (idx < argc ) {
546
- params -> psk = argv [idx ];
547
- params -> psk_length = strlen (argv [idx ]);
548
- /* Defaults */
549
- params -> security = WIFI_SECURITY_TYPE_PSK ;
550
- params -> mfp = WIFI_MFP_OPTIONAL ;
551
- idx ++ ;
552
-
553
- /* Security type (optional) */
554
- if (idx < argc ) {
555
- unsigned int security = strtol (argv [idx ], & endptr , 10 );
556
-
557
- if (security <= WIFI_SECURITY_TYPE_MAX ) {
558
- params -> security = security ;
559
- }
560
- idx ++ ;
561
-
562
- /* MFP (optional) */
563
- if (idx < argc ) {
564
- unsigned int mfp = strtol (argv [idx ], & endptr , 10 );
565
-
566
- if (security == WIFI_SECURITY_TYPE_NONE ||
567
- security == WIFI_SECURITY_TYPE_WPA_PSK ) {
568
- PR_ERROR ("MFP not supported for security type %s\n" ,
569
- wifi_security_txt (security ));
536
+ break ;
537
+ case 'b' :
538
+ if (iface_mode == WIFI_MODE_INFRA ) {
539
+ switch (atoi (optarg )) {
540
+ case 2 :
541
+ params -> band = WIFI_FREQ_BAND_2_4_GHZ ;
542
+ break ;
543
+ case 5 :
544
+ params -> band = WIFI_FREQ_BAND_5_GHZ ;
545
+ break ;
546
+ case 6 :
547
+ params -> band = WIFI_FREQ_BAND_6_GHZ ;
548
+ break ;
549
+ default :
550
+ PR_ERROR ("Invalid band: %d\n" , atoi (optarg ));
570
551
return - EINVAL ;
571
552
}
572
-
573
- if (mfp <= WIFI_MFP_REQUIRED ) {
574
- params -> mfp = mfp ;
575
- }
576
- idx ++ ;
577
553
}
578
- }
579
-
580
- if (params -> psk_length < WIFI_PSK_MIN_LEN ||
581
- (params -> security != WIFI_SECURITY_TYPE_SAE &&
582
- params -> psk_length > WIFI_PSK_MAX_LEN ) ||
583
- (params -> security == WIFI_SECURITY_TYPE_SAE &&
584
- params -> psk_length > WIFI_SAE_PSWD_MAX_LEN )) {
585
- PR_ERROR ("Invalid PSK length (%d) for security type %s\n" ,
586
- params -> psk_length ,
587
- wifi_security_txt (params -> security ));
554
+ break ;
555
+ case 'w' :
556
+ if (params -> security == WIFI_SECURITY_TYPE_NONE ||
557
+ params -> security == WIFI_SECURITY_TYPE_WPA_PSK ) {
558
+ PR_ERROR ("MFP not supported for security type %s\n" ,
559
+ wifi_security_txt (params -> security ));
560
+ return - EINVAL ;
561
+ }
562
+ params -> mfp = atoi (optarg );
563
+ break ;
564
+ case 'h' :
565
+ shell_help (sh );
566
+ break ;
567
+ default :
568
+ PR_ERROR ("Invalid option %c\n" , opt );
569
+ shell_help (sh );
588
570
return - EINVAL ;
589
571
}
590
572
}
591
-
592
573
return 0 ;
593
574
}
594
575
@@ -599,7 +580,7 @@ static int cmd_wifi_connect(const struct shell *sh, size_t argc,
599
580
struct wifi_connect_req_params cnx_params = { 0 };
600
581
601
582
context .sh = sh ;
602
- if (__wifi_args_to_params (argc - 1 , & argv [ 1 ] , & cnx_params , WIFI_MODE_INFRA )) {
583
+ if (__wifi_args_to_params (sh , argc , argv , & cnx_params , WIFI_MODE_INFRA )) {
603
584
shell_help (sh );
604
585
return - ENOEXEC ;
605
586
}
@@ -1254,7 +1235,7 @@ static int cmd_wifi_ap_enable(const struct shell *sh, size_t argc,
1254
1235
int ret ;
1255
1236
1256
1237
context .sh = sh ;
1257
- if (__wifi_args_to_params (argc - 1 , & argv [1 ], & cnx_params , WIFI_MODE_AP )) {
1238
+ if (__wifi_args_to_params (sh , argc - 1 , & argv [1 ], & cnx_params , WIFI_MODE_AP )) {
1258
1239
shell_help (sh );
1259
1240
return - ENOEXEC ;
1260
1241
}
@@ -1870,16 +1851,16 @@ SHELL_STATIC_SUBCMD_SET_CREATE(wifi_commands,
1870
1851
SHELL_CMD (ap , & wifi_cmd_ap , "Access Point mode commands.\n" , NULL ),
1871
1852
SHELL_CMD_ARG (connect , NULL ,
1872
1853
"Connect to a Wi-Fi AP\n"
1873
- "\"<SSID>\"\n"
1874
- "[channel number/band: > 0: Channel, 0:any channel, \n"
1875
- "< 0: band (- 2:2.4GHz, - 5:5GHz, - 6:6GHz]\n"
1876
- "[PSK: valid only for secure SSIDs] \n"
1877
- "[Security type: valid only for secure SSIDs] \n"
1854
+ "<-s --ssid \"<SSID>\">: SSID. \n"
1855
+ "[-c --channel]: Channel that needs to be scanned for connection. 0:any channel. \n"
1856
+ "[-b, --band] 0: any band (2:2.4GHz, 5:5GHz, 6:6GHz]\n"
1857
+ "[-p, --psk]: Passphrase ( valid only for secure SSIDs) \n"
1858
+ "[-k, --key-mgmt]: Key Management type ( valid only for secure SSIDs) \n"
1878
1859
"0:None, 1:WPA2-PSK, 2:WPA2-PSK-256, 3:SAE, 4:WAPI, 5:EAP, 6:WEP, 7: WPA-PSK\n"
1879
- "[MFP (optional: needs security type to be specified)] \n"
1860
+ "[-w, --ieee-80211w]: MFP (optional: needs security type to be specified)\n"
1880
1861
": 0:Disable, 1:Optional, 2:Required.\n" ,
1881
1862
cmd_wifi_connect ,
1882
- 2 , 4 ),
1863
+ 2 , 5 ),
1883
1864
SHELL_CMD_ARG (disconnect , NULL , "Disconnect from the Wi-Fi AP.\n" ,
1884
1865
cmd_wifi_disconnect ,
1885
1866
1 , 0 ),
0 commit comments