You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/09/01 01:23:55 UTC

[GitHub] vrahane closed pull request #1376: sensor_shell: Fix shell to not remove event from default queue

vrahane closed pull request #1376: sensor_shell: Fix shell to not remove event from default queue
URL: https://github.com/apache/mynewt-core/pull/1376
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/hw/sensor/include/sensor/sensor.h b/hw/sensor/include/sensor/sensor.h
index ce17424308..69fd8b46e3 100644
--- a/hw/sensor/include/sensor/sensor.h
+++ b/hw/sensor/include/sensor/sensor.h
@@ -1078,6 +1078,13 @@ sensor_mgr_put_read_evt(void *arg);
  */
 char*
 sensor_ftostr(float, char *, int);
+
+/**
+ * API to register sensor shell
+ */
+int
+sensor_shell_register(void);
+
 #endif
 
 #if MYNEWT_VAL(SENSOR_OIC)
diff --git a/hw/sensor/src/sensor_shell.c b/hw/sensor/src/sensor_shell.c
index 137a52ea68..8cce50f2b8 100644
--- a/hw/sensor/src/sensor_shell.c
+++ b/hw/sensor/src/sensor_shell.c
@@ -42,6 +42,7 @@
 #include "hal/hal_i2c.h"
 #include "parse/parse.h"
 
+static struct os_event g_sensor_shell_read_ev;
 static int sensor_cmd_exec(int, char **);
 static struct shell_cmd shell_sensor_cmd = {
     .sc_cmd = "sensor",
@@ -57,9 +58,15 @@ struct sensor_poll_data {
     int spd_poll_itvl;
     int spd_poll_duration;
     int spd_poll_delay;
+    int64_t spd_duration;
+    int64_t spd_start_ts;
+    struct sensor *spd_sensor;
+    sensor_type_t spd_sensor_type;
+    bool spd_read_in_progress;
 };
 
 static int g_sensor_shell_num_entries;
+static struct sensor_poll_data g_spd;
 
 static void
 sensor_display_help(void)
@@ -70,6 +77,8 @@ sensor_display_help(void)
     console_printf("  read <sensor_name> <type> [-n nsamples] [-i poll_itvl(ms)] [-d poll_duration(ms)]\n");
     console_printf("      read <no_of_samples> from sensor<sensor_name> of type:<type> at preset interval or \n");
     console_printf("      at <poll_interval> rate for <poll_duration>\n");
+    console_printf("  read_stop\n");
+    console_printf("      stops polling the sensor\n");
     console_printf("  type <sensor_name>\n");
     console_printf("      types supported by registered sensor\n");
     console_printf("  notify <sensor_name> [on/off] <type>\n");
@@ -419,28 +428,6 @@ sensor_shell_read_listener(struct sensor *sensor, void *arg, void *data,
     return (0);
 }
 
-void
-sensor_shell_timer_cb(void *arg)
-{
-    int timer_arg_val;
-
-    timer_arg_val = *(uint32_t *)arg;
-    os_cputime_timer_relative(&g_sensor_shell_timer, timer_arg_val);
-    os_sem_release(&g_sensor_shell_sem);
-}
-
-/* os cputime timer configuration and initialization */
-static void
-sensor_shell_config_timer(struct sensor_poll_data *spd)
-{
-    sensor_shell_timer_arg = spd->spd_poll_itvl * 1000;
-
-    os_cputime_timer_init(&g_sensor_shell_timer, sensor_shell_timer_cb,
-                          &sensor_shell_timer_arg);
-
-    os_cputime_timer_relative(&g_sensor_shell_timer, sensor_shell_timer_arg);
-}
-
 /* Check for number of samples */
 static int
 sensor_shell_chk_nsamples(struct sensor_poll_data *spd)
@@ -454,27 +441,6 @@ sensor_shell_chk_nsamples(struct sensor_poll_data *spd)
     return -1;
 }
 
-/* Check for escape sequence */
-static int
-sensor_shell_chk_escape_seq(void)
-{
-    char ch;
-    int newline;
-    int rc;
-
-    ch = 0;
-    /* Check for escape sequence */
-    rc = console_read(&ch, 1, &newline);
-    /* ^C or q or Q gets it out of the sampling loop */
-    if (rc || (ch == 3 || ch == 'q' || ch == 'Q')) {
-        os_cputime_timer_stop(&g_sensor_shell_timer);
-        console_printf("Sensor polling stopped rc:%u\n", rc);
-        return 0;
-    }
-
-    return -1;
-}
-
 /*
  * Incrementing duration based on interval if specified or
  * os_time if interval is not specified and checking duration
@@ -505,21 +471,79 @@ sensor_shell_polling_done(struct sensor_poll_data *spd, int64_t *duration,
     return -1;
 }
 
+static void
+sensor_shell_read_ev_cb(struct os_event *ev)
+{
+    struct sensor_poll_data *spd;
+    int rc;
+
+    if (!ev->ev_arg) {
+        goto done;
+    }
+
+    spd = ev->ev_arg;
+    rc = sensor_read(spd->spd_sensor, spd->spd_sensor_type,
+                     sensor_shell_read_listener, (void *)SENSOR_IGN_LISTENER,
+                     OS_TIMEOUT_NEVER);
+    if (rc) {
+        console_printf("Cannot read sensor\n");
+        g_spd.spd_read_in_progress = false;
+        goto done;
+    }
+
+    /* Check number of samples if provided */
+    if (!sensor_shell_chk_nsamples(spd)) {
+        g_spd.spd_read_in_progress = false;
+        goto done;
+    }
+
+    /* Check duration if provided */
+    if (!sensor_shell_polling_done(spd, &spd->spd_start_ts,
+                                   &spd->spd_duration)) {
+        g_spd.spd_read_in_progress = false;
+        goto done;
+    }
+
+    os_cputime_timer_relative(&g_sensor_shell_timer, sensor_shell_timer_arg);
+
+done:
+    return;
+}
+
+void
+sensor_shell_timer_cb(void *arg)
+{
+    g_sensor_shell_read_ev.ev_arg = arg;
+    g_sensor_shell_read_ev.ev_cb  = sensor_shell_read_ev_cb;
+    os_eventq_put(os_eventq_dflt_get(), &g_sensor_shell_read_ev);
+}
+
+/* os cputime timer configuration and initialization */
+static void
+sensor_shell_config_timer(struct sensor_poll_data *spd)
+{
+    sensor_shell_timer_arg = spd->spd_poll_itvl * 1000;
+
+    os_cputime_timer_init(&g_sensor_shell_timer, sensor_shell_timer_cb,
+                          spd);
+
+    os_cputime_timer_relative(&g_sensor_shell_timer, sensor_shell_timer_arg);
+}
+
 static int
 sensor_cmd_read(char *name, sensor_type_t type, struct sensor_poll_data *spd)
 {
-    struct sensor *sensor;
     int rc;
-    int64_t duration;
-    int64_t start_ts;
+
+    rc = SYS_EOK;
 
     /* Look up sensor by name */
-    sensor = sensor_mgr_find_next_bydevname(name, NULL);
-    if (!sensor) {
+    spd->spd_sensor = sensor_mgr_find_next_bydevname(name, NULL);
+    if (!spd->spd_sensor) {
         console_printf("Sensor %s not found!\n", name);
     }
 
-    if (!(type & sensor->s_types)) {
+    if (!(type & spd->spd_sensor->s_types)) {
         rc = SYS_EINVAL;
         /* Directly return without trying to unregister */
         console_printf("Read req for wrng type 0x%x from selected sensor: %s\n",
@@ -527,52 +551,12 @@ sensor_cmd_read(char *name, sensor_type_t type, struct sensor_poll_data *spd)
         return rc;
     }
 
-    /* Initialize the semaphore*/
-    os_sem_init(&g_sensor_shell_sem, 0);
-
+    spd->spd_sensor_type = type;
     if (spd->spd_poll_itvl) {
         sensor_shell_config_timer(spd);
     }
 
-    start_ts = duration = 0;
-
-    while (1) {
-        if (spd->spd_poll_itvl) {
-            /*
-             * Wait for semaphore from callback,
-             * this semaphore should only be considered when a
-             * a poll interval is specified
-             */
-            os_sem_pend(&g_sensor_shell_sem, OS_TIMEOUT_NEVER);
-        }
-
-        rc = sensor_read(sensor, type, sensor_shell_read_listener,
-                         (void *)SENSOR_IGN_LISTENER, OS_TIMEOUT_NEVER);
-        if (rc) {
-            console_printf("Cannot read sensor %s\n", name);
-            goto err;
-        }
-
-        /* Check number of samples if provided */
-        if (!sensor_shell_chk_nsamples(spd)) {
-            break;
-        }
-
-        /* Check duration if provided */
-        if (!sensor_shell_polling_done(spd, &start_ts, &duration)) {
-            break;
-        }
-
-        /* Check escape sequence */
-        if(!sensor_shell_chk_escape_seq()) {
-            break;
-        }
-    }
-
-err:
-
     g_sensor_shell_num_entries = 0;
-
     return rc;
 }
 
@@ -669,7 +653,6 @@ sensor_cmd_notify(char *name, bool on, char *type_string)
 static int
 sensor_cmd_exec(int argc, char **argv)
 {
-    struct sensor_poll_data spd;
     char *subcmd;
     int rc = 0;
     int i;
@@ -683,6 +666,12 @@ sensor_cmd_exec(int argc, char **argv)
     if (!strcmp(subcmd, "list")) {
         sensor_cmd_list_sensors();
     } else if (!strcmp(subcmd, "read")) {
+        if (g_spd.spd_read_in_progress == true) {
+            console_printf("Read already in progress\n");
+            rc = SYS_EINVAL;
+            goto done;
+        }
+
         if (argc < 6) {
             console_printf("Too few arguments: %d\n"
                            "Usage: sensor read <sensor_name> <type>"
@@ -693,24 +682,27 @@ sensor_cmd_exec(int argc, char **argv)
         }
 
         i = 4;
-        memset(&spd, 0, sizeof(struct sensor_poll_data));
+        memset(&g_spd, 0, sizeof(struct sensor_poll_data));
         if (argv[i] && !strcmp(argv[i], "-n")) {
-            spd.spd_nsamples = atoi(argv[++i]);
+            g_spd.spd_nsamples = atoi(argv[++i]);
             i++;
         }
         if (argv[i] && !strcmp(argv[i], "-i")) {
-            spd.spd_poll_itvl = atoi(argv[++i]);
+            g_spd.spd_poll_itvl = atoi(argv[++i]);
             i++;
         }
         if (argv[i] && !strcmp(argv[i], "-d")) {
-            spd.spd_poll_duration = atoi(argv[++i]);
+            g_spd.spd_poll_duration = atoi(argv[++i]);
             i++;
         }
 
-        rc = sensor_cmd_read(argv[2], (sensor_type_t) strtol(argv[3], NULL, 0), &spd);
+        rc = sensor_cmd_read(argv[2], (sensor_type_t) strtol(argv[3], NULL, 0), &g_spd);
         if (rc) {
             goto done;
         }
+
+        g_spd.spd_read_in_progress = true;
+
     } else if (!strcmp(argv[1], "type")) {
         rc = sensor_cmd_display_type(argv);
         if (rc) {
@@ -733,6 +725,10 @@ sensor_cmd_exec(int argc, char **argv)
            goto done;
         }
 
+    } else if (!strcmp(argv[1], "read_stop")) {
+        os_cputime_timer_stop(&g_sensor_shell_timer);
+        console_printf("Stop read\n");
+        g_spd.spd_read_in_progress = false;
     } else {
         console_printf("Unknown sensor command %s\n", subcmd);
         rc = SYS_EINVAL;


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services