Wapi simplify (#1)

apps/wireless/wapi:  Simplify usage of command line.  Remove duplicate extensions
This commit is contained in:
Xiang Xiao 2019-12-31 10:39:53 -06:00 committed by patacongo
parent 0536c5b523
commit 49c997501a
4 changed files with 80 additions and 201 deletions

View File

@ -116,8 +116,8 @@ enum wapi_route_target_e
enum wapi_essid_flag_e
{
WAPI_ESSID_ON,
WAPI_ESSID_OFF
WAPI_ESSID_OFF,
WAPI_ESSID_ON
};
/* Supported operation modes. */
@ -653,44 +653,6 @@ int wapi_scan_stat(int sock, FAR const char *ifname);
int wapi_scan_coll(int sock, FAR const char *ifname, FAR struct wapi_list_s *aps);
/************************************************************************************
* Name: wpa_driver_wext_set_ssid
*
* Description:
* Set SSID, SIOCSIWESSID
*
* Input Parameters:
* sockfd - Opened network socket
* ifname - Interface name
* ssid - SSID
* ssid_len - Length of SSID (0..32)
*
* Returned Value:
* 0 on success, -1 on failure
*
************************************************************************************/
int wpa_driver_wext_set_ssid(int sockfd, FAR const char *ifname,
FAR const uint8_t *ssid, size_t ssid_len);
/************************************************************************************
* Name: wpa_driver_wext_set_mode
*
* Description:
* Set wireless mode (infra/adhoc), SIOCSIWMODE
*
* Input Parameters:
* sockfd - Opened network socket
* ifname - Interface name
* mode - 0 = infra/BSS (associate with an AP), 1 = adhoc/IBSS
*
* Returned Value:
* 0 on success, -1 on failure
*
************************************************************************************/
int wpa_driver_wext_set_mode(int sockfd, FAR const char *ifname, int mode);
/************************************************************************************
* Name: wpa_driver_wext_set_key_ext
*

View File

@ -69,97 +69,6 @@
* Public Functions
************************************************************************************/
/************************************************************************************
* Name: wpa_driver_wext_set_ssid
*
* Description:
* Set SSID, SIOCSIWESSID
*
* Input Parameters:
* sockfd - Opened network socket
* ifname - Interface name
* ssid - SSID
* ssid_len - Length of SSID (0..32)
*
* Returned Value:
* 0 on success, -1 on failure
*
************************************************************************************/
int wpa_driver_wext_set_ssid(int sockfd, FAR const char *ifname,
FAR const uint8_t *ssid, size_t ssid_len)
{
struct iwreq iwr;
int ret = 0;
char buf[33];
DEBUGASSERT(ifname != NULL && ssid != NULL && ssid_len > 0);
if (ssid_len > WAPI_ESSID_MAX_SIZE)
{
return -1;
}
memset(&iwr, 0, sizeof(iwr));
strncpy(iwr.ifr_name, ifname, IFNAMSIZ);
/* flags: 1 = ESSID is active, 0 = not (promiscuous) */
iwr.u.essid.flags = (ssid_len != 0);
memset(buf, 0, sizeof(buf));
memcpy(buf, ssid, ssid_len);
iwr.u.essid.pointer = (caddr_t) buf;
iwr.u.essid.length = ssid_len;
if (ioctl(sockfd, SIOCSIWESSID, (unsigned long)&iwr) < 0)
{
nerr("ERROR: ioctl[SIOCSIWESSID]: %d", errno);
ret = -1;
}
return ret;
}
/************************************************************************************
* Name: wpa_driver_wext_set_mode
*
* Description:
* Set wireless mode (infra/adhoc), SIOCSIWMODE
*
* Input Parameters:
* sockfd - Opened network socket
* ifname - Interface name
* mode - 0 = infra/BSS (associate with an AP), 1 = adhoc/IBSS
*
* Returned Value:
* 0 on success, -1 on failure
*
************************************************************************************/
int wpa_driver_wext_set_mode(int sockfd, FAR const char *ifname, int mode)
{
struct iwreq iwr;
int ret = -1;
DEBUGASSERT(ifname != NULL);
memset(&iwr, 0, sizeof(iwr));
strncpy(iwr.ifr_name, ifname, IFNAMSIZ);
iwr.u.mode = mode;
if (ioctl(sockfd, SIOCSIWMODE, (unsigned long)&iwr) == 0)
{
ret = 0;
goto done;
}
nerr("ERROR: ioctl[SIOCSIWMODE]: %d", errno);
done:
return ret;
}
/************************************************************************************
* Name: wpa_driver_wext_set_key_ext
*
@ -272,7 +181,7 @@ int wpa_driver_wext_associate(FAR struct wpa_wconfig_s *wconfig)
strncpy(req.ifr_name, wconfig->ifname, IFNAMSIZ);
ret = wpa_driver_wext_set_mode(sockfd, wconfig->ifname, wconfig->sta_mode);
ret = wapi_set_mode(sockfd, wconfig->ifname, wconfig->sta_mode);
if (ret < 0)
{
nerr("ERROR: Fail set sta mode: %d\n", ret);
@ -306,8 +215,8 @@ int wpa_driver_wext_associate(FAR struct wpa_wconfig_s *wconfig)
goto close_socket;
}
ret = wpa_driver_wext_set_ssid(sockfd, wconfig->ifname, wconfig->ssid,
wconfig->ssidlen);
ret = wapi_set_essid(sockfd, wconfig->ifname,
(FAR const char *)wconfig->ssid, WAPI_ESSID_ON);
if (ret < 0)
{
nerr("ERROR: Fail set ssid: %d\n", ret);

View File

@ -38,6 +38,7 @@
* Included Files
****************************************************************************/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@ -76,8 +77,7 @@ typedef void (*cmd3_t)(int sock, FAR const char *arg1,
static int wapi_str2int(FAR const char *str);
static double wapi_str2double(FAR const char *str);
static unsigned int wapi_str2ndx(FAR const char *name, FAR const char **list,
unsigned int listlen);
static unsigned int wapi_str2ndx(FAR const char *name, FAR const char **list);
static void wapi_show_cmd(int sock, FAR const char *ifname);
static void wapi_ip_cmd(int sock, FAR const char *ifname,
@ -185,12 +185,18 @@ static double wapi_str2double(FAR const char *str)
*
****************************************************************************/
static unsigned int wapi_str2ndx(FAR const char *name, FAR const char **list,
unsigned int listlen)
static unsigned int wapi_str2ndx(FAR const char *name, FAR const char **list)
{
unsigned int ndx;
for (ndx = 0; ndx < listlen; ndx++)
/* Check the first character is enough, all prefix with WAPI_* */
if (isdigit(name[0]))
{
return atoi(name);
}
for (ndx = 0; list[ndx]; ndx++)
{
if (strcmp(name, list[ndx]) == 0)
{
@ -200,9 +206,9 @@ static unsigned int wapi_str2ndx(FAR const char *name, FAR const char **list,
WAPI_ERROR("ERROR: Invalid option string: %s\n", name);
WAPI_ERROR(" Valid options include:\n");
for (ndx = 0; ndx < listlen; ndx++)
for (ndx = 0; list[ndx]; ndx++)
{
WAPI_ERROR(" - %s\n", list[ndx]);
WAPI_ERROR(" - [%d] %s\n", ndx, list[ndx]);
}
exit(EXIT_FAILURE);
@ -456,8 +462,7 @@ static void wapi_freq_cmd(int sock, FAR const char *ifname,
/* Convert input strings to values */
frequency = wapi_str2double(freqstr);
freq_flag = (enum wapi_freq_flag_e)wapi_str2ndx(flagstr, g_wapi_freq_flags,
IW_FREQ_NFLAGS);
freq_flag = (enum wapi_freq_flag_e)wapi_str2ndx(flagstr, g_wapi_freq_flags);
/* Set the frequency */
@ -487,7 +492,7 @@ static void wapi_essid_cmd(int sock, FAR const char *ifname,
/* Convert input strings to values */
essid_flag = (enum wapi_essid_flag_e)wapi_str2ndx(flagstr, g_wapi_essid_flags, 2);
essid_flag = (enum wapi_essid_flag_e)wapi_str2ndx(flagstr, g_wapi_essid_flags);
/* Set the ESSID */
@ -517,7 +522,7 @@ static void wapi_mode_cmd(int sock, FAR const char *ifname,
/* Convert input strings to values */
mode = (enum wapi_mode_e)wapi_str2ndx(modestr, g_wapi_modes, IW_MODE_NFLAGS);
mode = (enum wapi_mode_e)wapi_str2ndx(modestr, g_wapi_modes);
/* Set operating mode */
@ -584,7 +589,7 @@ static void wapi_bitrate_cmd(int sock, FAR const char *ifname,
bitrate = wapi_str2int(ratestr);
bitrate_flag = (enum wapi_bitrate_flag_e)
wapi_str2ndx(flagstr, g_wapi_bitrate_flags, 2);
wapi_str2ndx(flagstr, g_wapi_bitrate_flags);
/* Set bitrate */
@ -617,7 +622,7 @@ static void wapi_txpower_cmd(int sock, FAR const char *ifname,
txpower = wapi_str2int(pwrstr);
txpower_flag = (enum wapi_txpower_flag_e)
wapi_str2ndx(flagstr, g_wapi_txpower_flags, IW_TXPOW_NFLAGS);
wapi_str2ndx(flagstr, g_wapi_txpower_flags);
/* Set txpower */
@ -650,7 +655,11 @@ static void wapi_scan_cmd(int sock, FAR const char *ifname)
/* Start scan */
ret = wapi_scan_init(sock, ifname);
WAPI_ERROR("ERROR: wapi_scan_init() failed: %d\n", ret);
if (ret < 0)
{
WAPI_ERROR("ERROR: wapi_scan_init() failed: %d\n", ret);
return;
}
/* Wait for completion */
@ -658,8 +667,11 @@ static void wapi_scan_cmd(int sock, FAR const char *ifname)
{
sleep(sleepdur);
ret = wapi_scan_stat(sock, ifname);
WAPI_ERROR("ERROR: wapi_scan_stat() failed: %d, sleeptries: %d\n",
ret, sleeptries);
if (ret < 0)
{
WAPI_ERROR("ERROR: wapi_scan_stat() failed: %d, sleeptries: %d\n",
ret, sleeptries);
}
}
while (--sleeptries > 0 && ret > 0);
@ -716,46 +728,46 @@ static void wapi_showusage(FAR const char *progname, int exitcode)
{
int i;
fprintf(stderr, "Usage: %s show <ifname>\n", progname);
fprintf(stderr, " %s scan <ifname>\n", progname);
fprintf(stderr, " %s ip <ifname> <IP address>\n", progname);
fprintf(stderr, " %s mask <ifname> <mask>\n", progname);
fprintf(stderr, " %s freq <ifname> <frequency> <flag>\n", progname);
fprintf(stderr, " %s essid <ifname> <essid> <flag>\n", progname);
fprintf(stderr, " %s mode <ifname> <ifname> <mode>\n", progname);
fprintf(stderr, " %s ap <ifname> <ifname> <MAC address>\n", progname);
fprintf(stderr, " %s bitrate <ifname> <bitrate> <flag>\n", progname);
fprintf(stderr, " %s txpower <ifname> <txpower> <flag>\n", progname);
fprintf(stderr, "Usage: %s show <ifname>\n", progname);
fprintf(stderr, " %s scan <ifname>\n", progname);
fprintf(stderr, " %s ip <ifname> <IP address>\n", progname);
fprintf(stderr, " %s mask <ifname> <mask>\n", progname);
fprintf(stderr, " %s freq <ifname> <frequency> <index/flag>\n", progname);
fprintf(stderr, " %s essid <ifname> <essid> <index/flag>\n", progname);
fprintf(stderr, " %s mode <ifname> <ifname> <index/mode>\n", progname);
fprintf(stderr, " %s ap <ifname> <ifname> <MAC address>\n", progname);
fprintf(stderr, " %s bitrate <ifname> <bitrate> <index/flag>\n", progname);
fprintf(stderr, " %s txpower <ifname> <txpower> <index/flag>\n", progname);
fprintf(stderr, " %s help\n", progname);
fprintf(stderr, "\nFrequency Flags:\n");
for (i = 0; i < IW_FREQ_NFLAGS; i++)
for (i = 0; g_wapi_freq_flags[i]; i++)
{
fprintf(stderr, " %s\n", g_wapi_freq_flags[i]);
fprintf(stderr, " [%d] %s\n", i, g_wapi_freq_flags[i]);
}
fprintf(stderr, "\nESSID Flags:\n");
for (i = 0; i < 2; i++)
for (i = 0; g_wapi_essid_flags[i]; i++)
{
fprintf(stderr, " %s\n", g_wapi_essid_flags[i]);
fprintf(stderr, " [%d] %s\n", i, g_wapi_essid_flags[i]);
}
fprintf(stderr, "\nOperating Modes:\n");
for (i = 0; i < IW_MODE_NFLAGS; i++)
for (i = 0; g_wapi_modes[i]; i++)
{
fprintf(stderr, " %s\n", g_wapi_modes[i]);
fprintf(stderr, " [%d] %s\n", i, g_wapi_modes[i]);
}
fprintf(stderr, "\nBitrate Flags:\n");
for (i = 0; i < 2; i++)
for (i = 0; g_wapi_bitrate_flags[i]; i++)
{
fprintf(stderr, " %s\n", g_wapi_bitrate_flags[i]);
fprintf(stderr, " [%d] %s\n", i, g_wapi_bitrate_flags[i]);
}
fprintf(stderr, "\nTX power Flags:\n");
for (i = 0; i < IW_TXPOW_NFLAGS; i++)
for (i = 0; g_wapi_txpower_flags[i]; i++)
{
fprintf(stderr, " %s\n", g_wapi_txpower_flags[i]);
fprintf(stderr, " [%d] %s\n", i, g_wapi_txpower_flags[i]);
}
exit(exitcode);

View File

@ -72,15 +72,17 @@ struct wapi_event_stream_s
FAR const char *g_wapi_freq_flags[] =
{
"WAPI_FREQ_AUTO",
"WAPI_FREQ_FIXED"
"WAPI_FREQ_FIXED",
NULL
};
/* ESSID */
FAR const char *g_wapi_essid_flags[] =
{
"WAPI_ESSID_OFF",
"WAPI_ESSID_ON",
"WAPI_ESSID_OFF"
NULL
};
/* Operating Mode */
@ -94,7 +96,8 @@ FAR const char *g_wapi_modes[] =
"WAPI_MODE_REPEAT",
"WAPI_MODE_SECOND",
"WAPI_MODE_MONITOR",
"WAPI_MODE_MESH"
"WAPI_MODE_MESH",
NULL
};
/* Bit Rate */
@ -102,7 +105,8 @@ FAR const char *g_wapi_modes[] =
FAR const char *g_wapi_bitrate_flags[] =
{
"WAPI_BITRATE_AUTO",
"WAPI_BITRATE_FIXED"
"WAPI_BITRATE_FIXED",
NULL
};
/* Transmit Power */
@ -111,7 +115,8 @@ FAR const char *g_wapi_txpower_flags[] =
{
"WAPI_TXPOWER_DBM",
"WAPI_TXPOWER_MWATT",
"WAPI_TXPOWER_RELATIVE"
"WAPI_TXPOWER_RELATIVE",
NULL
};
/****************************************************************************
@ -222,9 +227,6 @@ static void wapi_event_stream_init(FAR struct wapi_event_stream_s *stream,
static int wapi_event_stream_extract(FAR struct wapi_event_stream_s *stream,
FAR struct iw_event *iwe)
{
#warning Missing logic
// return iw_extract_event_stream((struct stream_descr *)stream, iwe, 0);
int ret;
struct iw_event *iwe_stream;
@ -385,7 +387,7 @@ static int wapi_scan_event(FAR struct iw_event *event, FAR struct wapi_list_s *l
int wapi_get_freq(int sock, FAR const char *ifname, FAR double *freq,
FAR enum wapi_freq_flag_e *flag)
{
struct iwreq wrq;
struct iwreq wrq = {};
int ret;
WAPI_VALIDATE_PTR(freq);
@ -436,7 +438,7 @@ int wapi_get_freq(int sock, FAR const char *ifname, FAR double *freq,
int wapi_set_freq(int sock, FAR const char *ifname, double freq,
enum wapi_freq_flag_e flag)
{
struct iwreq wrq;
struct iwreq wrq = {};
int ret;
/* Set freq. */
@ -482,7 +484,7 @@ int wapi_set_freq(int sock, FAR const char *ifname, double freq,
int wapi_freq2chan(int sock, FAR const char *ifname, double freq,
FAR int *chan)
{
struct iwreq wrq;
struct iwreq wrq = {};
char buf[sizeof(struct iw_range) * 2];
int ret;
@ -544,7 +546,7 @@ int wapi_freq2chan(int sock, FAR const char *ifname, double freq,
int wapi_chan2freq(int sock, FAR const char *ifname, int chan,
FAR double *freq)
{
struct iwreq wrq;
struct iwreq wrq = {};
char buf[sizeof(struct iw_range) * 2];
int ret;
@ -603,7 +605,7 @@ int wapi_chan2freq(int sock, FAR const char *ifname, int chan,
int wapi_get_essid(int sock, FAR const char *ifname, FAR char *essid,
FAR enum wapi_essid_flag_e *flag)
{
struct iwreq wrq;
struct iwreq wrq = {};
int ret;
WAPI_VALIDATE_PTR(essid);
@ -643,7 +645,7 @@ int wapi_set_essid(int sock, FAR const char *ifname, FAR const char *essid,
enum wapi_essid_flag_e flag)
{
char buf[WAPI_ESSID_MAX_SIZE + 1];
struct iwreq wrq;
struct iwreq wrq = {};
int ret;
/* Prepare request. */
@ -675,7 +677,7 @@ int wapi_set_essid(int sock, FAR const char *ifname, FAR const char *essid,
int wapi_get_mode(int sock, FAR const char *ifname, FAR enum wapi_mode_e *mode)
{
struct iwreq wrq;
struct iwreq wrq = {};
int ret;
WAPI_VALIDATE_PTR(mode);
@ -705,7 +707,7 @@ int wapi_get_mode(int sock, FAR const char *ifname, FAR enum wapi_mode_e *mode)
int wapi_set_mode(int sock, FAR const char *ifname, enum wapi_mode_e mode)
{
struct iwreq wrq;
struct iwreq wrq = {};
int ret;
wrq.u.mode = mode;
@ -762,7 +764,7 @@ int wapi_make_null_ether(FAR struct ether_addr *sa)
int wapi_get_ap(int sock, FAR const char *ifname, FAR struct ether_addr *ap)
{
struct iwreq wrq;
struct iwreq wrq = {};
int ret;
WAPI_VALIDATE_PTR(ap);
@ -793,7 +795,7 @@ int wapi_get_ap(int sock, FAR const char *ifname, FAR struct ether_addr *ap)
int wapi_set_ap(int sock, FAR const char *ifname,
FAR const struct ether_addr *ap)
{
struct iwreq wrq;
struct iwreq wrq = {};
int ret;
WAPI_VALIDATE_PTR(ap);
@ -824,7 +826,7 @@ int wapi_set_ap(int sock, FAR const char *ifname,
int wapi_get_bitrate(int sock, FAR const char *ifname,
FAR int *bitrate, FAR enum wapi_bitrate_flag_e *flag)
{
struct iwreq wrq;
struct iwreq wrq = {};
int ret;
WAPI_VALIDATE_PTR(bitrate);
@ -867,7 +869,7 @@ int wapi_get_bitrate(int sock, FAR const char *ifname,
int wapi_set_bitrate(int sock, FAR const char *ifname, int bitrate,
enum wapi_bitrate_flag_e flag)
{
struct iwreq wrq;
struct iwreq wrq = {};
int ret;
wrq.u.bitrate.value = bitrate;
@ -922,7 +924,7 @@ int wapi_mwatt2dbm(int mwatt)
int wapi_get_txpower(int sock, FAR const char *ifname, FAR int *power,
FAR enum wapi_txpower_flag_e *flag)
{
struct iwreq wrq;
struct iwreq wrq = {};
int ret;
WAPI_VALIDATE_PTR(power);
@ -983,7 +985,7 @@ int wapi_get_txpower(int sock, FAR const char *ifname, FAR int *power,
int wapi_set_txpower(int sock, FAR const char *ifname, int power,
enum wapi_txpower_flag_e flag)
{
struct iwreq wrq;
struct iwreq wrq = {};
int ret;
/* Construct the request. */
@ -1029,13 +1031,9 @@ int wapi_set_txpower(int sock, FAR const char *ifname, int power,
int wapi_scan_init(int sock, const char *ifname)
{
struct iwreq wrq;
struct iwreq wrq = {};
int ret;
wrq.u.data.pointer = NULL;
wrq.u.data.flags = 0;
wrq.u.data.length = 0;
strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
ret = ioctl(sock, SIOCSIWSCAN, (unsigned long)((uintptr_t)&wrq));
if (ret < 0)
@ -1061,13 +1059,11 @@ int wapi_scan_init(int sock, const char *ifname)
int wapi_scan_stat(int sock, FAR const char *ifname)
{
struct iwreq wrq;
struct iwreq wrq = {};
int ret;
char buf;
wrq.u.data.pointer = &buf;
wrq.u.data.flags = 0;
wrq.u.data.length = 0;
strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
if ((ret = ioctl(sock, SIOCGIWSCAN, (unsigned long)((uintptr_t)&wrq))) < 0)
@ -1112,7 +1108,7 @@ int wapi_scan_coll(int sock, FAR const char *ifname, FAR struct wapi_list_s *aps
{
FAR char *buf;
int buflen;
struct iwreq wrq;
struct iwreq wrq = {};
int ret;
WAPI_VALIDATE_PTR(aps);