38
38
* Included Files
39
39
****************************************************************************/
40
40
41
+ #include <ctype.h>
41
42
#include <stdio.h>
42
43
#include <stdlib.h>
43
44
#include <unistd.h>
@@ -76,8 +77,7 @@ typedef void (*cmd3_t)(int sock, FAR const char *arg1,
76
77
77
78
static int wapi_str2int (FAR const char * str );
78
79
static double wapi_str2double (FAR const char * str );
79
- static unsigned int wapi_str2ndx (FAR const char * name , FAR const char * * list ,
80
- unsigned int listlen );
80
+ static unsigned int wapi_str2ndx (FAR const char * name , FAR const char * * list );
81
81
82
82
static void wapi_show_cmd (int sock , FAR const char * ifname );
83
83
static void wapi_ip_cmd (int sock , FAR const char * ifname ,
@@ -185,12 +185,18 @@ static double wapi_str2double(FAR const char *str)
185
185
*
186
186
****************************************************************************/
187
187
188
- static unsigned int wapi_str2ndx (FAR const char * name , FAR const char * * list ,
189
- unsigned int listlen )
188
+ static unsigned int wapi_str2ndx (FAR const char * name , FAR const char * * list )
190
189
{
191
190
unsigned int ndx ;
192
191
193
- for (ndx = 0 ; ndx < listlen ; ndx ++ )
192
+ /* Check the first character is enough, all prefix with WAPI_* */
193
+
194
+ if (isdigit (name [0 ]))
195
+ {
196
+ return atoi (name );
197
+ }
198
+
199
+ for (ndx = 0 ; list [ndx ]; ndx ++ )
194
200
{
195
201
if (strcmp (name , list [ndx ]) == 0 )
196
202
{
@@ -200,9 +206,9 @@ static unsigned int wapi_str2ndx(FAR const char *name, FAR const char **list,
200
206
201
207
WAPI_ERROR ("ERROR: Invalid option string: %s\n" , name );
202
208
WAPI_ERROR (" Valid options include:\n" );
203
- for (ndx = 0 ; ndx < listlen ; ndx ++ )
209
+ for (ndx = 0 ; list [ ndx ] ; ndx ++ )
204
210
{
205
- WAPI_ERROR (" - % s\n" , list [ndx ]);
211
+ WAPI_ERROR (" - [%d] % s\n" , ndx , list [ndx ]);
206
212
}
207
213
208
214
exit (EXIT_FAILURE );
@@ -456,8 +462,7 @@ static void wapi_freq_cmd(int sock, FAR const char *ifname,
456
462
/* Convert input strings to values */
457
463
458
464
frequency = wapi_str2double (freqstr );
459
- freq_flag = (enum wapi_freq_flag_e )wapi_str2ndx (flagstr , g_wapi_freq_flags ,
460
- IW_FREQ_NFLAGS );
465
+ freq_flag = (enum wapi_freq_flag_e )wapi_str2ndx (flagstr , g_wapi_freq_flags );
461
466
462
467
/* Set the frequency */
463
468
@@ -487,7 +492,7 @@ static void wapi_essid_cmd(int sock, FAR const char *ifname,
487
492
488
493
/* Convert input strings to values */
489
494
490
- essid_flag = (enum wapi_essid_flag_e )wapi_str2ndx (flagstr , g_wapi_essid_flags , 2 );
495
+ essid_flag = (enum wapi_essid_flag_e )wapi_str2ndx (flagstr , g_wapi_essid_flags );
491
496
492
497
/* Set the ESSID */
493
498
@@ -517,7 +522,7 @@ static void wapi_mode_cmd(int sock, FAR const char *ifname,
517
522
518
523
/* Convert input strings to values */
519
524
520
- mode = (enum wapi_mode_e )wapi_str2ndx (modestr , g_wapi_modes , IW_MODE_NFLAGS );
525
+ mode = (enum wapi_mode_e )wapi_str2ndx (modestr , g_wapi_modes );
521
526
522
527
/* Set operating mode */
523
528
@@ -584,7 +589,7 @@ static void wapi_bitrate_cmd(int sock, FAR const char *ifname,
584
589
585
590
bitrate = wapi_str2int (ratestr );
586
591
bitrate_flag = (enum wapi_bitrate_flag_e )
587
- wapi_str2ndx (flagstr , g_wapi_bitrate_flags , 2 );
592
+ wapi_str2ndx (flagstr , g_wapi_bitrate_flags );
588
593
589
594
/* Set bitrate */
590
595
@@ -617,7 +622,7 @@ static void wapi_txpower_cmd(int sock, FAR const char *ifname,
617
622
618
623
txpower = wapi_str2int (pwrstr );
619
624
txpower_flag = (enum wapi_txpower_flag_e )
620
- wapi_str2ndx (flagstr , g_wapi_txpower_flags , IW_TXPOW_NFLAGS );
625
+ wapi_str2ndx (flagstr , g_wapi_txpower_flags );
621
626
622
627
/* Set txpower */
623
628
@@ -650,16 +655,23 @@ static void wapi_scan_cmd(int sock, FAR const char *ifname)
650
655
/* Start scan */
651
656
652
657
ret = wapi_scan_init (sock , ifname );
653
- WAPI_ERROR ("ERROR: wapi_scan_init() failed: %d\n" , ret );
658
+ if (ret < 0 )
659
+ {
660
+ WAPI_ERROR ("ERROR: wapi_scan_init() failed: %d\n" , ret );
661
+ return ;
662
+ }
654
663
655
664
/* Wait for completion */
656
665
657
666
do
658
667
{
659
668
sleep (sleepdur );
660
669
ret = wapi_scan_stat (sock , ifname );
661
- WAPI_ERROR ("ERROR: wapi_scan_stat() failed: %d, sleeptries: %d\n" ,
662
- ret , sleeptries );
670
+ if (ret < 0 )
671
+ {
672
+ WAPI_ERROR ("ERROR: wapi_scan_stat() failed: %d, sleeptries: %d\n" ,
673
+ ret , sleeptries );
674
+ }
663
675
}
664
676
while (-- sleeptries > 0 && ret > 0 );
665
677
@@ -716,46 +728,46 @@ static void wapi_showusage(FAR const char *progname, int exitcode)
716
728
{
717
729
int i ;
718
730
719
- fprintf (stderr , "Usage: %s show <ifname>\n" , progname );
720
- fprintf (stderr , " %s scan <ifname>\n" , progname );
721
- fprintf (stderr , " %s ip <ifname> <IP address>\n" , progname );
722
- fprintf (stderr , " %s mask <ifname> <mask>\n" , progname );
723
- fprintf (stderr , " %s freq <ifname> <frequency> < flag>\n" , progname );
724
- fprintf (stderr , " %s essid <ifname> <essid> < flag>\n" , progname );
725
- fprintf (stderr , " %s mode <ifname> <ifname> < mode>\n" , progname );
726
- fprintf (stderr , " %s ap <ifname> <ifname> <MAC address>\n" , progname );
727
- fprintf (stderr , " %s bitrate <ifname> <bitrate> < flag>\n" , progname );
728
- fprintf (stderr , " %s txpower <ifname> <txpower> < flag>\n" , progname );
731
+ fprintf (stderr , "Usage: %s show <ifname>\n" , progname );
732
+ fprintf (stderr , " %s scan <ifname>\n" , progname );
733
+ fprintf (stderr , " %s ip <ifname> <IP address>\n" , progname );
734
+ fprintf (stderr , " %s mask <ifname> <mask>\n" , progname );
735
+ fprintf (stderr , " %s freq <ifname> <frequency> <index/ flag>\n" , progname );
736
+ fprintf (stderr , " %s essid <ifname> <essid> <index/ flag>\n" , progname );
737
+ fprintf (stderr , " %s mode <ifname> <ifname> <index/ mode>\n" , progname );
738
+ fprintf (stderr , " %s ap <ifname> <ifname> <MAC address>\n" , progname );
739
+ fprintf (stderr , " %s bitrate <ifname> <bitrate> <index/ flag>\n" , progname );
740
+ fprintf (stderr , " %s txpower <ifname> <txpower> <index/ flag>\n" , progname );
729
741
fprintf (stderr , " %s help\n" , progname );
730
742
731
743
fprintf (stderr , "\nFrequency Flags:\n" );
732
- for (i = 0 ; i < IW_FREQ_NFLAGS ; i ++ )
744
+ for (i = 0 ; g_wapi_freq_flags [ i ] ; i ++ )
733
745
{
734
- fprintf (stderr , " % s\n" , g_wapi_freq_flags [i ]);
746
+ fprintf (stderr , " [%d] % s\n" , i , g_wapi_freq_flags [i ]);
735
747
}
736
748
737
749
fprintf (stderr , "\nESSID Flags:\n" );
738
- for (i = 0 ; i < 2 ; i ++ )
750
+ for (i = 0 ; g_wapi_essid_flags [ i ] ; i ++ )
739
751
{
740
- fprintf (stderr , " % s\n" , g_wapi_essid_flags [i ]);
752
+ fprintf (stderr , " [%d] % s\n" , i , g_wapi_essid_flags [i ]);
741
753
}
742
754
743
755
fprintf (stderr , "\nOperating Modes:\n" );
744
- for (i = 0 ; i < IW_MODE_NFLAGS ; i ++ )
756
+ for (i = 0 ; g_wapi_modes [ i ] ; i ++ )
745
757
{
746
- fprintf (stderr , " % s\n" , g_wapi_modes [i ]);
758
+ fprintf (stderr , " [%d] % s\n" , i , g_wapi_modes [i ]);
747
759
}
748
760
749
761
fprintf (stderr , "\nBitrate Flags:\n" );
750
- for (i = 0 ; i < 2 ; i ++ )
762
+ for (i = 0 ; g_wapi_bitrate_flags [ i ] ; i ++ )
751
763
{
752
- fprintf (stderr , " % s\n" , g_wapi_bitrate_flags [i ]);
764
+ fprintf (stderr , " [%d] % s\n" , i , g_wapi_bitrate_flags [i ]);
753
765
}
754
766
755
767
fprintf (stderr , "\nTX power Flags:\n" );
756
- for (i = 0 ; i < IW_TXPOW_NFLAGS ; i ++ )
768
+ for (i = 0 ; g_wapi_txpower_flags [ i ] ; i ++ )
757
769
{
758
- fprintf (stderr , " % s\n" , g_wapi_txpower_flags [i ]);
770
+ fprintf (stderr , " [%d] % s\n" , i , g_wapi_txpower_flags [i ]);
759
771
}
760
772
761
773
exit (exitcode );
0 commit comments