From 61250d5516f87b54fafa56d303f0ede1c259a666 Mon Sep 17 00:00:00 2001 From: "chao.an" Date: Thu, 23 Jun 2022 14:44:38 +0800 Subject: [PATCH] wireless/wapi: add get country code support Country command with no args to get the ccode: Get country code: cp> wapi country wlan0 CN Set country code: cp> wapi country wlan0 JP Get country code: cp> wapi country wlan0 JP Signed-off-by: chao.an --- include/wireless/wapi.h | 11 +++++++++++ wireless/wapi/src/wapi.c | 28 ++++++++++++++++++++++++++-- wireless/wapi/src/wireless.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/include/wireless/wapi.h b/include/wireless/wapi.h index fcddf54e7..d4e962234 100644 --- a/include/wireless/wapi.h +++ b/include/wireless/wapi.h @@ -731,6 +731,17 @@ void wapi_scan_coll_free(FAR struct wapi_list_s *aps); int wapi_set_country(int sock, FAR const char *ifname, FAR const char *country); +/**************************************************************************** + * Name: wapi_get_country + * + * Description: + * Get the country code + * + ****************************************************************************/ + +int wapi_get_country(int sock, FAR const char *ifname, + FAR char *country); + /**************************************************************************** * Name: wapi_get_sensitivity * diff --git a/wireless/wapi/src/wapi.c b/wireless/wapi/src/wapi.c index fdc50c534..96c428e69 100644 --- a/wireless/wapi/src/wapi.c +++ b/wireless/wapi/src/wapi.c @@ -123,7 +123,7 @@ static const struct wapi_command_s g_wapi_commands[] = {"ap", 2, 2, wapi_ap_cmd}, {"bitrate", 3, 3, wapi_bitrate_cmd}, {"txpower", 3, 3, wapi_txpower_cmd}, - {"country", 2, 2, wapi_country_cmd}, + {"country", 1, 2, wapi_country_cmd}, {"sense", 1, 1, wapi_sense_cmd}, #ifdef CONFIG_WIRELESS_WAPI_INITCONF {"reconnect", 1, 1, wapi_reconnect_cmd}, @@ -259,6 +259,7 @@ static int wapi_show_cmd(int sock, int argc, FAR char **argv) enum wapi_mode_e mode; struct ether_addr ap; struct in_addr addr; + char country[4]; double tmpfreq; int bitrate; int txpower; @@ -404,6 +405,15 @@ static int wapi_show_cmd(int sock, int argc, FAR char **argv) printf(" Sense: %d\n", sense); } + /* Get Country Code */ + + memset(country, 0, sizeof(country)); + ret = wapi_get_country(sock, ifname, country); + if (ret == 0) + { + printf(" Country: %s\n", country); + } + return 0; } @@ -806,7 +816,7 @@ static int wapi_scan_cmd(int sock, int argc, FAR char **argv) * Name: wapi_country_cmd * * Description: - * Set the country code + * Set/Get the country code * * Returned Value: * None @@ -815,6 +825,20 @@ static int wapi_scan_cmd(int sock, int argc, FAR char **argv) static int wapi_country_cmd(int sock, int argc, FAR char **argv) { + char country[4]; + int ret; + + if (argc == 1) + { + ret = wapi_get_country(sock, argv[0], country); + if (ret == 0) + { + printf("%s\n", country); + } + + return ret; + } + return wapi_set_country(sock, argv[0], argv[1]); } diff --git a/wireless/wapi/src/wireless.c b/wireless/wapi/src/wireless.c index 7aac5b133..4cfbf3ba0 100644 --- a/wireless/wapi/src/wireless.c +++ b/wireless/wapi/src/wireless.c @@ -1440,6 +1440,39 @@ int wapi_set_country(int sock, FAR const char *ifname, return ret; } +/**************************************************************************** + * Name: wapi_get_country + * + * Description: + * Get the country code + * + ****************************************************************************/ + +int wapi_get_country(int sock, FAR const char *ifname, + FAR char *country) +{ + struct iwreq wrq = + { + }; + + int ret; + + /* Prepare request. */ + + strlcpy(wrq.ifr_name, ifname, IFNAMSIZ); + wrq.u.data.pointer = (FAR void *)country; + wrq.u.data.length = 2; + ret = ioctl(sock, SIOCGIWCOUNTRY, (unsigned long)((uintptr_t)&wrq)); + if (ret < 0) + { + int errcode = errno; + WAPI_IOCTL_STRERROR(SIOCGIWSENS, errcode); + ret = -errcode; + } + + return ret; +} + /**************************************************************************** * Name: wapi_get_sensitivity *