You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ag...@apache.org on 2020/12/31 22:53:20 UTC

[incubator-nuttx-apps] 02/02: testing/sensortest: fix bug because getopt and add debug log

This is an automated email from the ASF dual-hosted git repository.

aguettouche pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git

commit 89bceba6b04704198cc4fbf9dbb1550d55976295
Author: dongjiuzhu <do...@xiaomi.com>
AuthorDate: Wed Dec 9 12:36:46 2020 +0800

    testing/sensortest: fix bug because getopt and add debug log
    
    Change-Id: Id9c3499dd56690c60b2f6579d128850ce7522a6e
    Signed-off-by: Jiuzhu Dong <do...@xiaomi.com>
---
 testing/sensortest/sensortest.c | 93 +++++++++++++++++++++++++----------------
 1 file changed, 56 insertions(+), 37 deletions(-)

diff --git a/testing/sensortest/sensortest.c b/testing/sensortest/sensortest.c
index d4e6ea7..c428432 100644
--- a/testing/sensortest/sensortest.c
+++ b/testing/sensortest/sensortest.c
@@ -152,15 +152,17 @@ static void print_gps(const char *buffer, const char *name)
 
 static void usage(void)
 {
-  printf("sensortest <command> [arguments...]\n");
-  printf(" Commands:\n");
-  printf("\t<sensor_node_name> ex, accel0(/dev/sensor/accel0)\n");
-
+  printf("sensortest [arguments...] <command>\n");
   printf("\t[-h      ]  sensotest commands help\n");
   printf("\t[-i <val>]  The output data period of sensor in us\n");
   printf("\t            default: 1000000\n");
   printf("\t[-b <val>]  The maximum report latency of sensor in us\n");
   printf("\t            default: 0\n");
+  printf("\t[-n <val>]  The specify number of output data\n");
+  printf("\t            default: 0\n");
+
+  printf(" Commands:\n");
+  printf("\t<sensor_node_name> ex, accel0(/dev/sensor/accel0)\n");
 }
 
 static void exit_handler(int signo)
@@ -179,7 +181,9 @@ static void exit_handler(int signo)
 int main(int argc, FAR char *argv[])
 {
   unsigned int interval = 1000000;
+  unsigned int received = 0;
   unsigned int latency = 0;
+  unsigned int count = 0;
   char devname[PATH_MAX];
   struct pollfd fds;
   FAR char *buffer;
@@ -201,49 +205,61 @@ int main(int argc, FAR char *argv[])
     }
 
   g_should_exit = false;
-  name = argv[1];
-  for (idx = 0; idx < ARRAYSIZE(g_sensor_info); idx++)
-    {
-      if (!strncmp(name, g_sensor_info[idx].name,
-          strlen(g_sensor_info[idx].name)))
-        {
-          len = g_sensor_info[idx].esize;
-          buffer = calloc(1, len);
-          break;
-        }
-    }
-
-  if (!len)
-    {
-      printf("The sensor node name:%s is invaild\n", name);
-      usage();
-      return -EINVAL;
-    }
-
-  if (!buffer)
-    {
-      return -ENOMEM;
-    }
-
-  while ((ret = getopt(argc, argv, "i:b:h")) != EOF)
+  while ((ret = getopt(argc, argv, "i:b:n:h")) != EOF)
     {
       switch (ret)
         {
           case 'i':
-            interval = strtol(optarg, NULL, 0);
+            interval = strtoul(optarg, NULL, 0);
             break;
 
           case 'b':
-            latency = strtol(optarg, NULL, 0);
+            latency = strtoul(optarg, NULL, 0);
+            break;
+
+          case 'n':
+            count = strtoul(optarg, NULL, 0);
             break;
 
           case 'h':
           default:
             usage();
-            goto opt_err;
-            break;
+            optind = 0;
+            return 0;
+        }
+    }
+
+  if (optind < argc)
+    {
+      name = argv[optind];
+      for (idx = 0; idx < ARRAYSIZE(g_sensor_info); idx++)
+        {
+          if (!strncmp(name, g_sensor_info[idx].name,
+              strlen(g_sensor_info[idx].name)))
+            {
+              len = g_sensor_info[idx].esize;
+              buffer = calloc(1, len);
+              break;
+            }
+        }
+
+      if (!len)
+        {
+          printf("The sensor node name:%s is invaild\n", name);
+          usage();
+          return -EINVAL;
+        }
+
+      if (!buffer)
+        {
+          return -ENOMEM;
         }
     }
+  else
+    {
+      usage();
+      return -EINVAL;
+    }
 
   snprintf(devname, PATH_MAX, DEVNAME_FMT, name);
   fd = open(devname, O_RDONLY | O_NONBLOCK);
@@ -252,7 +268,7 @@ int main(int argc, FAR char *argv[])
       ret = -errno;
       printf("Failed to open device:%s, ret:%s\n",
              devname, strerror(errno));
-      goto opt_err;
+      goto open_err;
     }
 
   ret = ioctl(fd, SNIOC_ACTIVATE, 1);
@@ -297,17 +313,21 @@ int main(int argc, FAR char *argv[])
   fds.fd = fd;
   fds.events = POLLIN;
 
-  while (!g_should_exit)
+  while ((!count || received < count) && !g_should_exit)
     {
       if (poll(&fds, 1, -1) > 0)
         {
           if (read(fd, buffer, len) >= len)
             {
+              received++;
               g_sensor_info[idx].print(buffer, name);
             }
         }
     }
 
+  printf("SensorTest: Received message: %s, number:%d/%d\n",
+         name, received, count);
+
   ret = ioctl(fd, SNIOC_ACTIVATE, 0);
   if (ret < 0)
     {
@@ -319,8 +339,7 @@ int main(int argc, FAR char *argv[])
 
 ctl_err:
   close(fd);
-opt_err:
+open_err:
   free(buffer);
-  optind = 0;
   return ret;
 }