From d9a02dd742705c1fe1fc5283d64b44819ccbeaaf Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 11 Apr 2017 16:34:50 -0600 Subject: [PATCH 1/3] apps/wireless/wapi: Consolidate some duplicate logic. --- wireless/wapi/src/wapi.c | 131 ++++++++++++--------------------------- 1 file changed, 38 insertions(+), 93 deletions(-) diff --git a/wireless/wapi/src/wapi.c b/wireless/wapi/src/wapi.c index 09f6538ef..60f00a3b9 100644 --- a/wireless/wapi/src/wapi.c +++ b/wireless/wapi/src/wapi.c @@ -114,7 +114,7 @@ static const struct wapi_command_s g_wapi_commands[] = {"mode", 2, (CODE void *)wapi_mode_cmd}, {"ap", 2, (CODE void *)wapi_ap_cmd}, {"bitrate", 3, (CODE void *)wapi_bitrate_cmd}, - {"txpower", 2, (CODE void *)wapi_txpower_cmd}, + {"txpower", 3, (CODE void *)wapi_txpower_cmd}, }; #define NCOMMANDS (sizeof(g_wapi_commands) / sizeof(struct wapi_command_s)) @@ -175,6 +175,31 @@ static double wapi_str2double(FAR const char *str) return value; } +/**************************************************************************** + * Name: wapi_str2ndx + * + * Description: + * Return the index of a string in a list of strings + * + ****************************************************************************/ + +static unsigned int wapi_str2ndx(FAR const char *name, FAR const char **list, + unsigned int listlen) +{ + unsigned int ndx; + + for (ndx = 0; ndx < listlen; ndx++) + { + if (strcmp(name, list[ndx]) == 0) + { + return ndx; + } + } + + WAPI_ERROR("ERROR: Invalid string: %s\n", name); + exit(EXIT_FAILURE); +} + /**************************************************************************** * Name: wapi_show_cmd * @@ -418,29 +443,13 @@ static void wapi_freq_cmd(int sock, FAR const char *ifname, { double frequency; wapi_freq_flag_t freq_flag; - bool found = false; int ret; - int i; /* Convert input strings to values */ frequency = wapi_str2double(freqstr); - - for (i = 0; i < IW_FREQ_NFLAGS; i++) - { - if (strcmp(flagstr, g_wapi_freq_flags[i]) == 0) - { - freq_flag = (wapi_freq_flag_t)i; - found = true; - break; - } - } - - if (!found) - { - WAPI_ERROR("ERROR: Invalid frequency flag: %s\n", flagstr); - exit(EXIT_FAILURE); - } + freq_flag = (wapi_freq_flag_t)wapi_str2ndx(flagstr, g_wapi_freq_flags, + IW_FREQ_NFLAGS); /* Set the frequency */ @@ -466,28 +475,12 @@ static void wapi_essid_cmd(int sock, FAR const char *ifname, FAR const char *essid, FAR const char *flagstr) { wapi_essid_flag_t essid_flag; - bool found = false; int ret; - int i; /* Convert input strings to values */ - for (i = 0; i < 2; i++) - { - if (strcmp(flagstr, g_wapi_essid_flags[i]) == 0) - { - essid_flag = (wapi_essid_flag_t)i; - found = true; - break; - } - } - - if (!found) - { - WAPI_ERROR("ERROR: Invalid ESSID flag: %s\n", flagstr); - exit(EXIT_FAILURE); - } - + essid_flag = (wapi_essid_flag_t)wapi_str2ndx(flagstr, g_wapi_essid_flags, 2); + /* Set the ESSID */ ret = wapi_set_essid(sock, ifname, essid, essid_flag); @@ -512,27 +505,11 @@ static void wapi_mode_cmd(int sock, FAR const char *ifname, FAR const char *modestr) { wapi_mode_t mode; - bool found = false; int ret; - int i; /* Convert input strings to values */ - for (i = 0; i < IW_MODE_NFLAGS; i++) - { - if (strcmp(modestr, g_wapi_modes[i]) == 0) - { - mode = (wapi_mode_t)i; - found = true; - break; - } - } - - if (!found) - { - WAPI_ERROR("ERROR: Invalid operating mode: %s\n", modestr); - exit(EXIT_FAILURE); - } + mode = (wapi_mode_t)wapi_str2ndx(modestr, g_wapi_modes, IW_MODE_NFLAGS); /* Set operating mode */ @@ -592,30 +569,14 @@ static void wapi_bitrate_cmd(int sock, FAR const char *ifname, { wapi_bitrate_flag_t bitrate_flag; - bool found = false; int bitrate; int ret; - int i; /* Convert input strings to values */ - bitrate = wapi_str2int(ratestr); - - for (i = 0; i < 2; i++) - { - if (strcmp(flagstr, g_wapi_bitrate_flags[i]) == 0) - { - bitrate_flag = (wapi_bitrate_flag_t)i; - found = true; - break; - } - } - - if (!found) - { - WAPI_ERROR("ERROR: Invalid bitrate flag: %s\n", flagstr); - exit(EXIT_FAILURE); - } + bitrate = wapi_str2int(ratestr); + bitrate_flag = (wapi_bitrate_flag_t) + wapi_str2ndx(flagstr, g_wapi_bitrate_flags, 2); /* Set bitrate */ @@ -641,30 +602,14 @@ static void wapi_txpower_cmd(int sock, FAR const char *ifname, FAR const char *pwrstr, FAR const char *flagstr) { wapi_txpower_flag_t txpower_flag; - bool found = false; int txpower; int ret; - int i; /* Convert input strings to values */ - txpower = wapi_str2int(pwrstr); - - for (i = 0; i < IW_TXPOW_NFLAGS; i++) - { - if (strcmp(flagstr, g_wapi_txpower_flags[i]) == 0) - { - txpower_flag = (wapi_txpower_flag_t)i; - found = true; - break; - } - } - - if (!found) - { - WAPI_ERROR("ERROR: Invalid TX power flag: %s\n", flagstr); - exit(EXIT_FAILURE); - } + txpower = wapi_str2int(pwrstr); + txpower_flag = (wapi_txpower_flag_t) + wapi_str2ndx(flagstr, g_wapi_txpower_flags, IW_TXPOW_NFLAGS); /* Set txpower */ @@ -728,7 +673,7 @@ static void wapi_scan_cmd(int sock, FAR const char *ifname) for (info = list.head.scan; info; info = info->next) { - printf(">> %02x:%02x:%02x:%02x:%02x:%02x %s\n", + printf(" %02x:%02x:%02x:%02x:%02x:%02x %s\n", info->ap.ether_addr_octet[0], info->ap.ether_addr_octet[1], info->ap.ether_addr_octet[2], info->ap.ether_addr_octet[3], info->ap.ether_addr_octet[4], info->ap.ether_addr_octet[5], From 65d59f84582a002d862b879bd7f7286cc2bfcdf0 Mon Sep 17 00:00:00 2001 From: Juha Niskanen Date: Wed, 12 Apr 2017 06:47:07 -0600 Subject: [PATCH 2/3] examples/ostest: clarify when pthread clean-up test mutex need to remain usable --- examples/ostest/pthread_cleanup.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/examples/ostest/pthread_cleanup.c b/examples/ostest/pthread_cleanup.c index 54650823f..61c11f0f8 100644 --- a/examples/ostest/pthread_cleanup.c +++ b/examples/ostest/pthread_cleanup.c @@ -60,16 +60,20 @@ static void cleanup(FAR void * data) FAR struct sync_s *sync = (FAR struct sync_s *) data; int status; - /* Note: pthread_cond_wait() will release the mutex while it waits on - * condition value. So a EPERM error is not a failure. + /* Note: The behavior of canceling pthread_cond_wait() with asynchronous + * cancellation is not defined. On NuttX we get EPERM here, but application + * code must not rely on this. */ status = pthread_mutex_unlock(&sync->lock); +#ifndef CONFIG_CANCELLATION_POINTS if (status == EPERM) { printf("pthread_cleanup: thread did not have mutex locked: %d\n", status); + return; } - else if (status != 0) +#endif + if (status != 0) { printf("pthread_cleanup: ERROR pthread_mutex_unlock in cleanup handler. " "Status: %d\n", status); @@ -138,6 +142,7 @@ static void test_cleanup(void) printf("pthread_cleanup: ERROR pthread_join returned wrong result: %p\n", result); } +#ifdef CONFIG_CANCELLATION_POINTS /* Do some operations on lock in order to check if it is in usable state. */ status = pthread_mutex_trylock(&sync.lock); @@ -151,6 +156,7 @@ static void test_cleanup(void) { printf("pthread_cleanup: ERROR pthread_mutex_unlock, status=%d\n", status); } +#endif } /**************************************************************************** From 58da85523dcc3aa005400283fcd18db379a7d5cc Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Wed, 12 Apr 2017 07:09:37 -0600 Subject: [PATCH 3/3] wireless/wapi: Improve some output. --- wireless/wapi/src/util.h | 83 +++++++++++++++++++++++++++++++--------- wireless/wapi/src/wapi.c | 10 ++++- 2 files changed, 73 insertions(+), 20 deletions(-) diff --git a/wireless/wapi/src/util.h b/wireless/wapi/src/util.h index 50de0b6eb..e7c304f7f 100644 --- a/wireless/wapi/src/util.h +++ b/wireless/wapi/src/util.h @@ -47,29 +47,74 @@ * Pre-processor Definitions ****************************************************************************/ -#define WAPI_IOCTL_STRERROR(cmd) \ - fprintf( \ - stderr, "%s:%d:%s():ioctl(%s): %s\n", \ - __FILE__, __LINE__, __func__, \ - wapi_ioctl_command_name(cmd), strerror(errno)) +#ifdef DEBUG_WIRELESS_ERROR +# ifdef CONFIG_LIBC_STRERROR +# define WAPI_IOCTL_STRERROR(cmd) \ + fprintf( \ + stderr, "%s:%d:%s():ioctl(%s): %s\n", \ + __FILE__, __LINE__, __func__, \ + wapi_ioctl_command_name(cmd), strerror(errno)) -#define WAPI_STRERROR(fmt, ...) \ - fprintf( \ - stderr, "%s:%d:%s():" fmt ": %s\n", \ - __FILE__, __LINE__, __func__, \ - ## __VA_ARGS__, strerror(errno)) +# define WAPI_STRERROR(fmt, ...) \ + fprintf( \ + stderr, "%s:%d:%s():" fmt ": %s\n", \ + __FILE__, __LINE__, __func__, \ + ## __VA_ARGS__, strerror(errno)) +# else +# define WAPI_IOCTL_STRERROR(cmd) \ + fprintf( \ + stderr, "%s:%d:%s():ioctl(%s): %d\n", \ + __FILE__, __LINE__, __func__, \ + wapi_ioctl_command_name(cmd), errno) -#define WAPI_ERROR(fmt, ...) \ - fprintf( \ - stderr, "%s:%d:%s(): " fmt , \ - __FILE__, __LINE__, __func__, ## __VA_ARGS__) +# define WAPI_STRERROR(fmt, ...) \ + fprintf( \ + stderr, "%s:%d:%s():" fmt ": %d\n", \ + __FILE__, __LINE__, __func__, \ + ## __VA_ARGS__, errno) +# endif + +# define WAPI_ERROR(fmt, ...) \ + fprintf( \ + stderr, "%s:%d:%s(): " fmt , \ + __FILE__, __LINE__, __func__, ## __VA_ARGS__) + +#else +# ifdef CONFIG_LIBC_STRERROR +# define WAPI_IOCTL_STRERROR(cmd) \ + fprintf( \ + stderr, "ioctl(%s): %s\n", \ + wapi_ioctl_command_name(cmd), strerror(errno)) + +# define WAPI_STRERROR(fmt, ...) \ + fprintf( \ + stderr, fmt ": %s\n", \ + ## __VA_ARGS__, strerror(errno)) +# else +# define WAPI_IOCTL_STRERROR(cmd) \ + fprintf( \ + stderr, "ioctl(%s): %d\n", \ + wapi_ioctl_command_name(cmd), errno) + +# define WAPI_STRERROR(fmt, ...) \ + fprintf( \ + stderr, fmt ": %d\n", \ + ## __VA_ARGS__, errno) +# endif + +# define WAPI_ERROR(fmt, ...) \ + fprintf( \ + stderr, fmt , \ + ## __VA_ARGS__) + +#endif #define WAPI_VALIDATE_PTR(ptr) \ - if (!ptr) \ - { \ - WAPI_ERROR("Null pointer: %s.\n", #ptr); \ - return -1; \ - } + if (ptr == NULL) \ + { \ + WAPI_ERROR("Null pointer: %p\n", ptr); \ + return -EINVAL; \ + } /**************************************************************************** * Public Function Prototypes diff --git a/wireless/wapi/src/wapi.c b/wireless/wapi/src/wapi.c index 60f00a3b9..a5f53c2d2 100644 --- a/wireless/wapi/src/wapi.c +++ b/wireless/wapi/src/wapi.c @@ -76,6 +76,8 @@ 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 void wapi_show_cmd(int sock, FAR const char *ifname); static void wapi_ip_cmd(int sock, FAR const char *ifname, @@ -196,7 +198,13 @@ static unsigned int wapi_str2ndx(FAR const char *name, FAR const char **list, } } - WAPI_ERROR("ERROR: Invalid string: %s\n", name); + WAPI_ERROR("ERROR: Invalid option string: %s\n", name); + WAPI_ERROR(" Valid options include:\n"); + for (ndx = 0; ndx < listlen; ndx++) + { + WAPI_ERROR(" - %s\n", list[ndx]); + } + exit(EXIT_FAILURE); }