wireless/wapi: independent scan and scan_results

add scan <ssid> support

Signed-off-by: chao.an <anchao@xiaomi.com>
This commit is contained in:
chao.an 2020-04-03 13:07:58 +08:00 committed by Abdelatif Guettouche
parent 7c8c30537f
commit 6bafe90db4
3 changed files with 224 additions and 212 deletions

View File

@ -641,7 +641,7 @@ int wapi_make_socket(void);
* *
****************************************************************************/ ****************************************************************************/
int wapi_scan_init(int sock, FAR const char *ifname); int wapi_scan_init(int sock, FAR const char *ifname, FAR const char *essid);
/**************************************************************************** /****************************************************************************
* Name: wapi_scan_stat * Name: wapi_scan_stat
@ -670,6 +670,19 @@ int wapi_scan_stat(int sock, FAR const char *ifname);
int wapi_scan_coll(int sock, FAR const char *ifname, int wapi_scan_coll(int sock, FAR const char *ifname,
FAR struct wapi_list_s *aps); FAR struct wapi_list_s *aps);
/****************************************************************************
* Name: wapi_scan_coll_free
*
* Description:
* Free the scan results.
*
* Input Parameters:
* aps - Release the collected struct wapi_scan_info_s.
*
****************************************************************************/
void wapi_scan_coll_free(FAR struct wapi_list_s *aps);
#ifdef CONFIG_WIRELESS_WAPI_INITCONF #ifdef CONFIG_WIRELESS_WAPI_INITCONF
/**************************************************************************** /****************************************************************************
* Name: wapi_load_config * Name: wapi_load_config

View File

@ -54,78 +54,70 @@
* Private Types * Private Types
****************************************************************************/ ****************************************************************************/
/* Generic form of a command handler */
typedef CODE int (*wapi_cmd_t)(int sock, int argc, FAR char **argv);
/* Describes one command */ /* Describes one command */
struct wapi_command_s struct wapi_command_s
{ {
FAR const char *name; FAR const char *name;
uint8_t noptions; uint8_t minargs;
CODE void *handler; uint8_t maxargs;
wapi_cmd_t handler;
}; };
/* Generic form of a command handler */
typedef void (*cmd1_t)(int sock, FAR const char *arg1);
typedef void (*cmd2_t)(int sock, FAR const char *arg1,
FAR const char *arg2);
typedef void (*cmd3_t)(int sock, FAR const char *arg1,
FAR const char *arg2, FAR const char *arg3);
/**************************************************************************** /****************************************************************************
* Private Function Prototypes * Private Function Prototypes
****************************************************************************/ ****************************************************************************/
static int wapi_str2int(FAR const char *str); static int wapi_str2int(FAR const char *str);
static double wapi_str2double(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); 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,
FAR const char *addrstr);
static void wapi_mask_cmd(int sock, FAR const char *ifname,
FAR const char *maskstr);
static void wapi_freq_cmd(int sock, FAR const char *ifname,
FAR const char *freqstr, FAR const char *flagstr);
static void wapi_essid_cmd(int sock, FAR const char *ifname,
FAR const char *essid, FAR const char *flagstr);
static void wapi_psk_cmd(int sock, FAR const char *ifname,
FAR const char *passphrase, FAR const char *flagstr);
static void wapi_disconnect_cmd(int sock, FAR const char *ifname);
static void wapi_mode_cmd(int sock, FAR const char *ifname,
FAR const char *modestr);
static void wapi_ap_cmd(int sock, FAR const char *ifname,
FAR const char *macstr);
static void wapi_bitrate_cmd(int sock, FAR const char *ifname,
FAR const char *ratestr,
FAR const char *flagstr);
static void wapi_txpower_cmd(int sock, FAR const char *ifname,
FAR const char *pwrstr,
FAR const char *flagstr);
static void wapi_scan_cmd(int sock, FAR const char *ifname);
static void wapi_showusage(FAR const char *progname, int exitcode); static void wapi_showusage(FAR const char *progname, int exitcode);
static int wapi_show_cmd (int sock, int argc, FAR char **argv);
static int wapi_ip_cmd (int sock, int argc, FAR char **argv);
static int wapi_mask_cmd (int sock, int argc, FAR char **argv);
static int wapi_freq_cmd (int sock, int argc, FAR char **argv);
static int wapi_essid_cmd (int sock, int argc, FAR char **argv);
static int wapi_psk_cmd (int sock, int argc, FAR char **argv);
static int wapi_disconnect_cmd (int sock, int argc, FAR char **argv);
static int wapi_mode_cmd (int sock, int argc, FAR char **argv);
static int wapi_ap_cmd (int sock, int argc, FAR char **argv);
static int wapi_bitrate_cmd (int sock, int argc, FAR char **argv);
static int wapi_txpower_cmd (int sock, int argc, FAR char **argv);
static int wapi_scan_results_cmd (int sock, int argc, FAR char **argv);
static int wapi_scan_cmd (int sock, int argc, FAR char **argv);
/**************************************************************************** /****************************************************************************
* Private Data * Private Data
****************************************************************************/ ****************************************************************************/
static const struct wapi_command_s g_wapi_commands[] = static const struct wapi_command_s g_wapi_commands[] =
{ {
{"help", 0, (CODE void *)NULL}, {"help", 0, 0, NULL},
{"show", 1, (CODE void *)wapi_show_cmd}, {"show", 1, 1, wapi_show_cmd},
{"scan", 1, (CODE void *)wapi_scan_cmd}, {"scan", 1, 2, wapi_scan_cmd},
{"ip", 2, (CODE void *)wapi_ip_cmd}, {"scan_results", 1, 1, wapi_scan_results_cmd},
{"mask", 2, (CODE void *)wapi_mask_cmd}, {"ip", 2, 2, wapi_ip_cmd},
{"freq", 3, (CODE void *)wapi_freq_cmd}, {"mask", 2, 2, wapi_mask_cmd},
{"essid", 3, (CODE void *)wapi_essid_cmd}, {"freq", 3, 3, wapi_freq_cmd},
{"psk", 3, (CODE void *)wapi_psk_cmd}, {"essid", 3, 3, wapi_essid_cmd},
{"disconnect", 1, (CODE void *)wapi_disconnect_cmd}, {"psk", 3, 3, wapi_psk_cmd},
{"mode", 2, (CODE void *)wapi_mode_cmd}, {"disconnect", 1, 1, wapi_disconnect_cmd},
{"ap", 2, (CODE void *)wapi_ap_cmd}, {"mode", 2, 2, wapi_mode_cmd},
{"bitrate", 3, (CODE void *)wapi_bitrate_cmd}, {"ap", 2, 2, wapi_ap_cmd},
{"txpower", 3, (CODE void *)wapi_txpower_cmd}, {"bitrate", 3, 3, wapi_bitrate_cmd},
{"txpower", 3, 3, wapi_txpower_cmd},
}; };
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define NCOMMANDS (sizeof(g_wapi_commands) / sizeof(struct wapi_command_s)) #define NCOMMANDS (sizeof(g_wapi_commands) / sizeof(struct wapi_command_s))
/**************************************************************************** /****************************************************************************
@ -235,8 +227,9 @@ 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 int wapi_show_cmd(int sock, int argc, FAR char **argv)
{ {
FAR const char *ifname = argv[0];
struct in_addr addr; struct in_addr addr;
double freq; double freq;
@ -266,6 +259,7 @@ static void wapi_show_cmd(int sock, FAR const char *ifname)
if (ret < 0) if (ret < 0)
{ {
WAPI_ERROR("ERROR: wapi_get_ip() failed: %d\n", ret); WAPI_ERROR("ERROR: wapi_get_ip() failed: %d\n", ret);
return ret;
} }
else else
{ {
@ -279,6 +273,7 @@ static void wapi_show_cmd(int sock, FAR const char *ifname)
if (ret < 0) if (ret < 0)
{ {
WAPI_ERROR("ERROR: wapi_get_netmask() failed: %d\n", ret); WAPI_ERROR("ERROR: wapi_get_netmask() failed: %d\n", ret);
return ret;
} }
else else
{ {
@ -291,6 +286,7 @@ static void wapi_show_cmd(int sock, FAR const char *ifname)
if (ret < 0) if (ret < 0)
{ {
WAPI_ERROR("ERROR: wapi_get_freq() failed: %d\n", ret); WAPI_ERROR("ERROR: wapi_get_freq() failed: %d\n", ret);
return ret;
} }
else else
{ {
@ -304,6 +300,7 @@ static void wapi_show_cmd(int sock, FAR const char *ifname)
if (ret < 0) if (ret < 0)
{ {
WAPI_ERROR("ERROR: wapi_freq2chan() failed: %d\n", ret); WAPI_ERROR("ERROR: wapi_freq2chan() failed: %d\n", ret);
return ret;
} }
else else
{ {
@ -314,6 +311,7 @@ static void wapi_show_cmd(int sock, FAR const char *ifname)
if (ret < 0) if (ret < 0)
{ {
WAPI_ERROR("ERROR: wapi_chan2freq() failed: %d\n", ret); WAPI_ERROR("ERROR: wapi_chan2freq() failed: %d\n", ret);
return ret;
} }
else else
{ {
@ -327,6 +325,7 @@ static void wapi_show_cmd(int sock, FAR const char *ifname)
if (ret < 0) if (ret < 0)
{ {
WAPI_ERROR("ERROR: wapi_get_essid() failed: %d\n", ret); WAPI_ERROR("ERROR: wapi_get_essid() failed: %d\n", ret);
return ret;
} }
else else
{ {
@ -340,6 +339,7 @@ static void wapi_show_cmd(int sock, FAR const char *ifname)
if (ret < 0) if (ret < 0)
{ {
WAPI_ERROR("ERROR: wapi_get_mode() failed: %d\n", ret); WAPI_ERROR("ERROR: wapi_get_mode() failed: %d\n", ret);
return ret;
} }
else else
{ {
@ -352,6 +352,7 @@ static void wapi_show_cmd(int sock, FAR const char *ifname)
if (ret < 0) if (ret < 0)
{ {
WAPI_ERROR("ERROR: wapi_get_ap() failed: %d\n", ret); WAPI_ERROR("ERROR: wapi_get_ap() failed: %d\n", ret);
return ret;
} }
else else
{ {
@ -367,6 +368,7 @@ static void wapi_show_cmd(int sock, FAR const char *ifname)
if (ret < 0) if (ret < 0)
{ {
WAPI_ERROR("ERROR: wapi_get_bitrate() failed: %d\n", ret); WAPI_ERROR("ERROR: wapi_get_bitrate() failed: %d\n", ret);
return ret;
} }
else else
{ {
@ -380,12 +382,15 @@ static void wapi_show_cmd(int sock, FAR const char *ifname)
if (ret < 0) if (ret < 0)
{ {
WAPI_ERROR("ERROR: wapi_get_txpower() failed: %d\n", ret); WAPI_ERROR("ERROR: wapi_get_txpower() failed: %d\n", ret);
return ret;
} }
else else
{ {
printf(" TxPower: %d\n", txpower); printf(" TxPower: %d\n", txpower);
printf(" Flag: %s\n", g_wapi_txpower_flags[txpower_flag]); printf(" Flag: %s\n", g_wapi_txpower_flags[txpower_flag]);
} }
return ret;
} }
/**************************************************************************** /****************************************************************************
@ -399,23 +404,17 @@ static void wapi_show_cmd(int sock, FAR const char *ifname)
* *
****************************************************************************/ ****************************************************************************/
static void wapi_ip_cmd(int sock, FAR const char *ifname, static int wapi_ip_cmd(int sock, int argc, FAR char **argv)
FAR const char *addrstr)
{ {
struct in_addr addr; struct in_addr addr;
int ret;
/* Format the request */ /* Format the request */
addr.s_addr = inet_addr(addrstr); addr.s_addr = inet_addr(argv[1]);
/* Set the IP address */ /* Set the IP address */
ret = wapi_set_ip(sock, ifname, &addr); return wapi_set_ip(sock, argv[0], &addr);
if (ret < 0)
{
WAPI_ERROR("ERROR: wapi_set_ip() failed: %d\n", ret);
}
} }
/**************************************************************************** /****************************************************************************
@ -429,23 +428,17 @@ static void wapi_ip_cmd(int sock, FAR const char *ifname,
* *
****************************************************************************/ ****************************************************************************/
static void wapi_mask_cmd(int sock, FAR const char *ifname, static int wapi_mask_cmd(int sock, int argc, FAR char **argv)
FAR const char *maskstr)
{ {
struct in_addr addr; struct in_addr addr;
int ret;
/* Format the request */ /* Format the request */
addr.s_addr = inet_addr(maskstr); addr.s_addr = inet_addr(argv[1]);
/* Set the network mask */ /* Set the network mask */
ret = wapi_set_netmask(sock, ifname, &addr); return wapi_set_netmask(sock, argv[0], &addr);
if (ret < 0)
{
WAPI_ERROR("ERROR: wapi_set_netmask() failed: %d\n", ret);
}
} }
/**************************************************************************** /****************************************************************************
@ -459,25 +452,20 @@ static void wapi_mask_cmd(int sock, FAR const char *ifname,
* *
****************************************************************************/ ****************************************************************************/
static void wapi_freq_cmd(int sock, FAR const char *ifname, static int wapi_freq_cmd(int sock, int argc, FAR char **argv)
FAR const char *freqstr, FAR const char *flagstr)
{ {
double frequency; double frequency;
enum wapi_freq_flag_e freq_flag; enum wapi_freq_flag_e freq_flag;
int ret;
/* Convert input strings to values */ /* Convert input strings to values */
frequency = wapi_str2double(freqstr); frequency = wapi_str2double(argv[1]);
freq_flag = (enum wapi_freq_flag_e)wapi_str2ndx(flagstr, g_wapi_freq_flags); freq_flag = (enum wapi_freq_flag_e)wapi_str2ndx(argv[2],
g_wapi_freq_flags);
/* Set the frequency */ /* Set the frequency */
ret = wapi_set_freq(sock, ifname, frequency, freq_flag); return wapi_set_freq(sock, argv[0], frequency, freq_flag);
if (ret < 0)
{
WAPI_ERROR("ERROR: \nwapi_set_freq() failed: %d\n", ret);
}
} }
/**************************************************************************** /****************************************************************************
@ -491,24 +479,18 @@ static void wapi_freq_cmd(int sock, FAR const char *ifname,
* *
****************************************************************************/ ****************************************************************************/
static void wapi_essid_cmd(int sock, FAR const char *ifname, static int wapi_essid_cmd(int sock, int argc, FAR char **argv)
FAR const char *essid, FAR const char *flagstr)
{ {
enum wapi_essid_flag_e essid_flag; enum wapi_essid_flag_e essid_flag;
int ret;
/* Convert input strings to values */ /* Convert input strings to values */
essid_flag = (enum wapi_essid_flag_e) essid_flag = (enum wapi_essid_flag_e)
wapi_str2ndx(flagstr, g_wapi_essid_flags); wapi_str2ndx(argv[2], g_wapi_essid_flags);
/* Set the ESSID */ /* Set the ESSID */
ret = wapi_set_essid(sock, ifname, essid, essid_flag); return wapi_set_essid(sock, argv[0], argv[1], essid_flag);
if (ret < 0)
{
WAPI_ERROR("ERROR: wapi_set_essid() failed: %d\n", ret);
}
} }
/**************************************************************************** /****************************************************************************
@ -522,24 +504,18 @@ static void wapi_essid_cmd(int sock, FAR const char *ifname,
* *
****************************************************************************/ ****************************************************************************/
static void wapi_psk_cmd(int sock, FAR const char *ifname, static int wapi_psk_cmd(int sock, int argc, FAR char **argv)
FAR const char *passphrase, FAR const char *flagstr)
{ {
enum wpa_alg_e alg_flag; enum wpa_alg_e alg_flag;
int ret;
/* Convert input strings to values */ /* Convert input strings to values */
alg_flag = (enum wpa_alg_e)wapi_str2ndx(flagstr, g_wapi_alg_flags); alg_flag = (enum wpa_alg_e)wapi_str2ndx(argv[2], g_wapi_alg_flags);
/* Set the Passphrase */ /* Set the Passphrase */
ret = wpa_driver_wext_set_key_ext(sock, ifname, alg_flag, return wpa_driver_wext_set_key_ext(sock, argv[0], alg_flag,
passphrase, strlen(passphrase)); argv[1], strlen(argv[1]));
if (ret < 0)
{
WAPI_ERROR("ERROR: wpa_driver_wext_set_key_ext() failed: %d\n", ret);
}
} }
/**************************************************************************** /****************************************************************************
@ -553,9 +529,11 @@ static void wapi_psk_cmd(int sock, FAR const char *ifname,
* *
****************************************************************************/ ****************************************************************************/
static void wapi_disconnect_cmd(int sock, FAR const char *ifname) static int wapi_disconnect_cmd(int sock, int argc, FAR char **argv)
{ {
wpa_driver_wext_disconnect(sock, ifname); wpa_driver_wext_disconnect(sock, argv[0]);
return 0;
} }
/**************************************************************************** /****************************************************************************
@ -569,23 +547,17 @@ static void wapi_disconnect_cmd(int sock, FAR const char *ifname)
* *
****************************************************************************/ ****************************************************************************/
static void wapi_mode_cmd(int sock, FAR const char *ifname, static int wapi_mode_cmd(int sock, int argc, FAR char **argv)
FAR const char *modestr)
{ {
enum wapi_mode_e mode; enum wapi_mode_e mode;
int ret;
/* Convert input strings to values */ /* Convert input strings to values */
mode = (enum wapi_mode_e)wapi_str2ndx(modestr, g_wapi_modes); mode = (enum wapi_mode_e)wapi_str2ndx(argv[1], g_wapi_modes);
/* Set operating mode */ /* Set operating mode */
ret = wapi_set_mode(sock, ifname, mode); return wapi_set_mode(sock, argv[0], mode);
if (ret < 0)
{
WAPI_ERROR("ERROR: \nwapi_set_mode() failed: %d\n", ret);
}
} }
/**************************************************************************** /****************************************************************************
@ -599,26 +571,20 @@ static void wapi_mode_cmd(int sock, FAR const char *ifname,
* *
****************************************************************************/ ****************************************************************************/
static void wapi_ap_cmd(int sock, FAR const char *ifname, static int wapi_ap_cmd(int sock, int argc, FAR char **argv)
FAR const char *macstr)
{ {
struct ether_addr ap; struct ether_addr ap;
int ret;
/* Convert input strings to values */ /* Convert input strings to values */
sscanf(macstr, "%02x:%02x:%02x:%02x:%02x:%02x", sscanf(argv[1], "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx",
&ap.ether_addr_octet[0], &ap.ether_addr_octet[1], &ap.ether_addr_octet[0], &ap.ether_addr_octet[1],
&ap.ether_addr_octet[2], &ap.ether_addr_octet[3], &ap.ether_addr_octet[2], &ap.ether_addr_octet[3],
&ap.ether_addr_octet[4], &ap.ether_addr_octet[5]); &ap.ether_addr_octet[4], &ap.ether_addr_octet[5]);
/* Set ap */ /* Set ap */
ret = wapi_set_ap(sock, ifname, &ap); return wapi_set_ap(sock, argv[0], &ap);
if (ret < 0)
{
WAPI_ERROR("ERROR: \nwapi_set_ap() failed: %d\n", ret);
}
} }
/**************************************************************************** /****************************************************************************
@ -632,27 +598,20 @@ static void wapi_ap_cmd(int sock, FAR const char *ifname,
* *
****************************************************************************/ ****************************************************************************/
static void wapi_bitrate_cmd(int sock, FAR const char *ifname, static int wapi_bitrate_cmd(int sock, int argc, FAR char **argv)
FAR const char *ratestr, FAR const char *flagstr)
{ {
enum wapi_bitrate_flag_e bitrate_flag; enum wapi_bitrate_flag_e bitrate_flag;
int bitrate; int bitrate;
int ret;
/* Convert input strings to values */ /* Convert input strings to values */
bitrate = wapi_str2int(ratestr); bitrate = wapi_str2int(argv[1]);
bitrate_flag = (enum wapi_bitrate_flag_e) bitrate_flag = (enum wapi_bitrate_flag_e)
wapi_str2ndx(flagstr, g_wapi_bitrate_flags); wapi_str2ndx(argv[2], g_wapi_bitrate_flags);
/* Set bitrate */ /* Set bitrate */
ret = wapi_set_bitrate(sock, ifname, bitrate, bitrate_flag); return wapi_set_bitrate(sock, argv[0], bitrate, bitrate_flag);
if (ret < 0)
{
WAPI_ERROR("ERROR: \nwapi_set_bitrate() failed: %d\n", ret);
}
} }
/**************************************************************************** /****************************************************************************
@ -666,82 +625,71 @@ static void wapi_bitrate_cmd(int sock, FAR const char *ifname,
* *
****************************************************************************/ ****************************************************************************/
static void wapi_txpower_cmd(int sock, FAR const char *ifname, static int wapi_txpower_cmd(int sock, int argc, FAR char **argv)
FAR const char *pwrstr, FAR const char *flagstr)
{ {
enum wapi_txpower_flag_e txpower_flag; enum wapi_txpower_flag_e txpower_flag;
int txpower; int txpower;
int ret;
/* Convert input strings to values */ /* Convert input strings to values */
txpower = wapi_str2int(pwrstr); txpower = wapi_str2int(argv[1]);
txpower_flag = (enum wapi_txpower_flag_e) txpower_flag = (enum wapi_txpower_flag_e)
wapi_str2ndx(flagstr, g_wapi_txpower_flags); wapi_str2ndx(argv[2], g_wapi_txpower_flags);
/* Set txpower */ /* Set txpower */
ret = wapi_set_txpower(sock, ifname, txpower, txpower_flag); return wapi_set_txpower(sock, argv[0], txpower, txpower_flag);
if (ret < 0)
{
WAPI_ERROR("ERROR: \nwapi_set_txpower() failed: %d\n", ret);
}
} }
/**************************************************************************** /****************************************************************************
* Name: wapi_scan_cmd * Name: wapi_scan_results_cmd
* *
* Description: * Description:
* Scans available APs in the range using given ifname interface. * Print the scan results.
* *
* Returned Value: * Returned Value:
* None * None
* *
****************************************************************************/ ****************************************************************************/
static void wapi_scan_cmd(int sock, FAR const char *ifname) static int wapi_scan_results_cmd(int sock, int argc, FAR char **argv)
{ {
int sleepdur = 1; int sleepdur = 200 * 1000;
int sleeptries = 5; int sleeptries = 25;
struct wapi_list_s list; struct wapi_list_s list;
FAR struct wapi_scan_info_s *info; FAR struct wapi_scan_info_s *info;
int ret; int ret;
/* Start scan */
ret = wapi_scan_init(sock, ifname);
if (ret < 0)
{
WAPI_ERROR("ERROR: wapi_scan_init() failed: %d\n", ret);
return;
}
/* Wait for completion */ /* Wait for completion */
do do
{ {
sleep(sleepdur); ret = wapi_scan_stat(sock, argv[0]);
ret = wapi_scan_stat(sock, ifname);
if (ret < 0) if (ret < 0)
{ {
WAPI_ERROR("ERROR: wapi_scan_stat() failed: %d, sleeptries: %d\n", WAPI_ERROR("ERROR: wapi_scan_stat() failed: %d, sleeptries: %d\n",
ret, sleeptries); ret, sleeptries);
} }
else if (ret == 1)
{
usleep(sleepdur);
}
} }
while (--sleeptries > 0 && ret > 0); while (--sleeptries > 0 && ret > 0);
if (ret < 0) if (ret < 0)
{ {
return; return ret;
} }
/* Collect results */ /* Collect results */
bzero(&list, sizeof(struct wapi_list_s)); bzero(&list, sizeof(struct wapi_list_s));
ret = wapi_scan_coll(sock, ifname, &list); ret = wapi_scan_coll(sock, argv[0], &list);
if (ret < 0) if (ret < 0)
{ {
WAPI_ERROR("ERROR: wapi_scan_coll() failed: %d\n", ret); WAPI_ERROR("ERROR: wapi_scan_coll() failed: %d\n", ret);
return ret;
} }
/* Print found aps */ /* Print found aps */
@ -758,15 +706,37 @@ static void wapi_scan_cmd(int sock, FAR const char *ifname)
/* Free ap list */ /* Free ap list */
info = list.head.scan; wapi_scan_coll_free(&list);
while (info) return 0;
{ }
FAR struct wapi_scan_info_s *temp;
temp = info->next; /****************************************************************************
free(info); * Name: wapi_scan_cmd
info = temp; *
* Description:
* Scans available APs in the range using given ifname interface.
*
* Returned Value:
* None
*
****************************************************************************/
static int wapi_scan_cmd(int sock, int argc, FAR char **argv)
{
FAR const char *essid;
int ret;
essid = argc > 1 ? argv[1] : NULL;
/* Start scan */
ret = wapi_scan_init(sock, argv[0], essid);
if (ret < 0)
{
return ret;
} }
return wapi_scan_results_cmd(sock, 1, argv);
} }
/**************************************************************************** /****************************************************************************
@ -784,61 +754,63 @@ static void wapi_showusage(FAR const char *progname, int exitcode)
{ {
int i; int i;
fprintf(stderr, "Usage: %s show <ifname>\n", progname); fprintf(stderr, "Usage:\n");
fprintf(stderr, " %s scan <ifname>\n", progname); fprintf(stderr, "\t%s show <ifname>\n", progname);
fprintf(stderr, " %s ip <ifname> <IP address>\n", progname); fprintf(stderr, "\t%s scan <ifname>\n", progname);
fprintf(stderr, " %s mask <ifname> <mask>\n", progname); fprintf(stderr, "\t%s scan_results <ifname>\n", progname);
fprintf(stderr, " %s freq <ifname> <frequency> <index/flag>\n", fprintf(stderr, "\t%s ip <ifname> <IP address>\n", progname);
progname); fprintf(stderr, "\t%s mask <ifname> <mask>\n", progname);
fprintf(stderr, " %s essid <ifname> <essid> <index/flag>\n", fprintf(stderr, "\t%s freq <ifname> <frequency> <index/flag>\n",
progname); progname);
fprintf(stderr, " %s psk <ifname> <passphrase> <index/flag>\n", fprintf(stderr, "\t%s essid <ifname> <essid> <index/flag>\n",
progname); progname);
fprintf(stderr, " %s disconnect <ifname>\n", progname); fprintf(stderr, "\t%s psk <ifname> <passphrase> <index/flag>\n",
fprintf(stderr, " %s mode <ifname> <ifname> <index/mode>\n", progname);
progname); fprintf(stderr, "\t%s disconnect <ifname>\n", progname);
fprintf(stderr, " %s ap <ifname> <ifname> <MAC address>\n", fprintf(stderr, "\t%s mode <ifname> <ifname> <index/mode>\n",
progname); progname);
fprintf(stderr, " %s bitrate <ifname> <bitrate> <index/flag>\n", fprintf(stderr, "\t%s ap <ifname> <ifname> <MAC address>\n",
progname); progname);
fprintf(stderr, " %s txpower <ifname> <txpower> <index/flag>\n", fprintf(stderr, "\t%s bitrate <ifname> <bitrate> <index/flag>\n",
progname); progname);
fprintf(stderr, " %s help\n", progname); fprintf(stderr, "\t%s txpower <ifname> <txpower> <index/flag>\n",
progname);
fprintf(stderr, "\t%s help\n", progname);
fprintf(stderr, "\nFrequency Flags:\n"); fprintf(stderr, "\nFrequency Flags:\n");
for (i = 0; g_wapi_freq_flags[i]; i++) for (i = 0; g_wapi_freq_flags[i]; i++)
{ {
fprintf(stderr, " [%d] %s\n", i, g_wapi_freq_flags[i]); fprintf(stderr, "\t[%d] %s\n", i, g_wapi_freq_flags[i]);
} }
fprintf(stderr, "\nESSID Flags:\n"); fprintf(stderr, "\nESSID Flags:\n");
for (i = 0; g_wapi_essid_flags[i]; i++) for (i = 0; g_wapi_essid_flags[i]; i++)
{ {
fprintf(stderr, " [%d] %s\n", i, g_wapi_essid_flags[i]); fprintf(stderr, "\t[%d] %s\n", i, g_wapi_essid_flags[i]);
} }
fprintf(stderr, "\nPassphrase algorithm Flags:\n"); fprintf(stderr, "\nPassphrase algorithm Flags:\n");
for (i = 0; g_wapi_alg_flags[i]; i++) for (i = 0; g_wapi_alg_flags[i]; i++)
{ {
fprintf(stderr, " [%d] %s\n", i, g_wapi_alg_flags[i]); fprintf(stderr, "\t[%d] %s\n", i, g_wapi_alg_flags[i]);
} }
fprintf(stderr, "\nOperating Modes:\n"); fprintf(stderr, "\nOperating Modes:\n");
for (i = 0; g_wapi_modes[i]; i++) for (i = 0; g_wapi_modes[i]; i++)
{ {
fprintf(stderr, " [%d] %s\n", i, g_wapi_modes[i]); fprintf(stderr, "\t[%d] %s\n", i, g_wapi_modes[i]);
} }
fprintf(stderr, "\nBitrate Flags:\n"); fprintf(stderr, "\nBitrate Flags:\n");
for (i = 0; g_wapi_bitrate_flags[i]; i++) for (i = 0; g_wapi_bitrate_flags[i]; i++)
{ {
fprintf(stderr, " [%d] %s\n", i, g_wapi_bitrate_flags[i]); fprintf(stderr, "\t[%d] %s\n", i, g_wapi_bitrate_flags[i]);
} }
fprintf(stderr, "\nTX power Flags:\n"); fprintf(stderr, "\nTX power Flags:\n");
for (i = 0; g_wapi_txpower_flags[i]; i++) for (i = 0; g_wapi_txpower_flags[i]; i++)
{ {
fprintf(stderr, " [%d] %s\n", i, g_wapi_txpower_flags[i]); fprintf(stderr, "\t[%d] %s\n", i, g_wapi_txpower_flags[i]);
} }
exit(exitcode); exit(exitcode);
@ -884,11 +856,11 @@ int main(int argc, FAR char *argv[])
wapi_showusage(argv[0], EXIT_FAILURE); wapi_showusage(argv[0], EXIT_FAILURE);
} }
if (wapicmd->noptions + 2 < argc) if (wapicmd->maxargs + 2 < argc)
{ {
WAPI_ERROR("ERROR: Garbage at end of command ignored\n"); WAPI_ERROR("ERROR: Garbage at end of command ignored\n");
} }
else if (wapicmd->noptions + 2 > argc) else if (wapicmd->minargs + 2 > argc)
{ {
WAPI_ERROR("ERROR: Missing required command options: %s\n", WAPI_ERROR("ERROR: Missing required command options: %s\n",
cmdname); cmdname);
@ -913,26 +885,9 @@ int main(int argc, FAR char *argv[])
return EXIT_FAILURE; return EXIT_FAILURE;
} }
/* Dispatch the command handling */ if (wapicmd->handler(sock, argc - 2, argc == 2 ? NULL : &argv[2]) < 0)
switch (wapicmd->noptions)
{ {
default: WAPI_ERROR("ERROR: Process command (%s) failed.\n", cmdname);
case 0:
WAPI_ERROR("ERROR: Internal craziness\n");
wapi_showusage(argv[0], EXIT_FAILURE);
case 1:
((cmd1_t)wapicmd->handler)(sock, argv[2]);
break;
case 2:
((cmd2_t)wapicmd->handler)(sock, argv[2], argv[3]);
break;
case 3:
((cmd3_t)wapicmd->handler)(sock, argv[2], argv[3], argv[4]);
break;
} }
/* Close communication socket */ /* Close communication socket */

View File

@ -1143,14 +1143,28 @@ int wapi_set_txpower(int sock, FAR const char *ifname, int power,
* *
****************************************************************************/ ****************************************************************************/
int wapi_scan_init(int sock, const char *ifname) int wapi_scan_init(int sock, const char *ifname, const char *essid)
{ {
struct iw_scan_req req;
struct iwreq wrq = struct iwreq wrq =
{ {
}; };
size_t essid_len;
int ret; int ret;
if (essid && (essid_len = strlen(essid)) > 0)
{
memset(&req, 0, sizeof(req));
req.essid_len = essid_len;
req.bssid.sa_family = ARPHRD_ETHER;
memset(req.bssid.sa_data, 0xff, IFHWADDRLEN);
memcpy(req.essid, essid, essid_len);
wrq.u.data.pointer = (caddr_t)&req;
wrq.u.data.length = sizeof(req);
wrq.u.data.flags = IW_SCAN_THIS_ESSID;
}
strncpy(wrq.ifr_name, ifname, IFNAMSIZ); strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
ret = ioctl(sock, SIOCSIWSCAN, (unsigned long)((uintptr_t)&wrq)); ret = ioctl(sock, SIOCSIWSCAN, (unsigned long)((uintptr_t)&wrq));
if (ret < 0) if (ret < 0)
@ -1319,3 +1333,33 @@ alloc:
free(buf); free(buf);
return ret; return ret;
} }
/****************************************************************************
* Name: wapi_scan_coll_free
*
* Description:
* Free the scan results.
*
* Returned Value:
* None
*
****************************************************************************/
void wapi_scan_coll_free(FAR struct wapi_list_s *list)
{
FAR struct wapi_scan_info_s *temp;
FAR struct wapi_scan_info_s *info;
if (list == NULL)
{
return;
}
info = list->head.scan;
while (info)
{
temp = info->next;
free(info);
info = temp;
}
}