Skip to content

Commit 6223c4a

Browse files
krish2718kartben
authored andcommitted
net: l2: wifi: Convert reg_domain to use get_opt
In order to prepare for extending the options, convert to getopt long for easier parsing of options. Signed-off-by: Chaitanya Tata <[email protected]>
1 parent a97f640 commit 6223c4a

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

subsys/net/l2/wifi/wifi_shell.c

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,37 +1940,45 @@ static int cmd_wifi_reg_domain(const struct shell *sh, size_t argc,
19401940
struct net_if *iface = net_if_get_wifi_sta();
19411941
struct wifi_reg_domain regd = {0};
19421942
int ret, chan_idx = 0;
1943+
int opt;
1944+
bool force = false;
1945+
int opt_index = 0;
1946+
static const struct option long_options[] = {
1947+
{"force", no_argument, 0, 'f'},
1948+
{NULL, 0, NULL, 0}
1949+
};
19431950

1944-
if (argc == 1) {
1951+
while ((opt = getopt_long(argc, argv, "f", long_options, &opt_index)) != -1) {
1952+
switch (opt) {
1953+
case 'f':
1954+
force = true;
1955+
break;
1956+
default:
1957+
return -ENOEXEC;
1958+
}
1959+
}
1960+
1961+
if (optind == argc) {
19451962
regd.chan_info = &chan_info[0];
19461963
regd.oper = WIFI_MGMT_GET;
1947-
} else if (argc >= 2 && argc <= 3) {
1948-
regd.oper = WIFI_MGMT_SET;
1949-
if (strlen(argv[1]) != 2) {
1964+
} else if (optind == argc - 1) {
1965+
if (strlen(argv[optind]) != 2) {
19501966
PR_WARNING("Invalid reg domain: Length should be two letters/digits\n");
19511967
return -ENOEXEC;
19521968
}
19531969

19541970
/* Two letter country code with special case of 00 for WORLD */
1955-
if (((argv[1][0] < 'A' || argv[1][0] > 'Z') ||
1956-
(argv[1][1] < 'A' || argv[1][1] > 'Z')) &&
1957-
(argv[1][0] != '0' || argv[1][1] != '0')) {
1958-
PR_WARNING("Invalid reg domain %c%c\n", argv[1][0], argv[1][1]);
1971+
if (((argv[optind][0] < 'A' || argv[optind][0] > 'Z') ||
1972+
(argv[optind][1] < 'A' || argv[optind][1] > 'Z')) &&
1973+
(argv[optind][0] != '0' || argv[optind][1] != '0')) {
1974+
PR_WARNING("Invalid reg domain %c%c\n", argv[optind][0], argv[optind][1]);
19591975
return -ENOEXEC;
19601976
}
1961-
regd.country_code[0] = argv[1][0];
1962-
regd.country_code[1] = argv[1][1];
1963-
1964-
if (argc == 3) {
1965-
if (strncmp(argv[2], "-f", 2) == 0) {
1966-
regd.force = true;
1967-
} else {
1968-
PR_WARNING("Invalid option %s\n", argv[2]);
1969-
return -ENOEXEC;
1970-
}
1971-
}
1977+
regd.country_code[0] = argv[optind][0];
1978+
regd.country_code[1] = argv[optind][1];
1979+
regd.force = force;
1980+
regd.oper = WIFI_MGMT_SET;
19721981
} else {
1973-
shell_help(sh);
19741982
return -ENOEXEC;
19751983
}
19761984

0 commit comments

Comments
 (0)