You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by st...@apache.org on 2016/12/13 05:42:21 UTC

[1/6] incubator-mynewt-core git commit: get sensor shell commands compiling and working with sample accel data

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/sensors_branch [created] 269791e47


get sensor shell commands compiling and working with sample accel data


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/269791e4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/269791e4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/269791e4

Branch: refs/heads/sensors_branch
Commit: 269791e47292171e3a772b4470a5daa7a6fe8f38
Parents: 4eb4020
Author: Sterling Hughes <st...@apache.org>
Authored: Mon Dec 12 21:41:42 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Mon Dec 12 21:42:04 2016 -0800

----------------------------------------------------------------------
 apps/slinky/src/main.c                     | 29 ++++++++-
 hw/drivers/sensors/sim/src/generic_accel.c |  3 +-
 hw/sensor/include/sensor/accel.h           |  1 +
 hw/sensor/include/sensor/sensor.h          |  3 +
 hw/sensor/src/sensor.c                     | 25 +++++++-
 hw/sensor/src/sensor_shell.c               | 84 +++++++++++++++++++++++++
 hw/sensor/syscfg.yml                       |  2 +-
 7 files changed, 143 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/269791e4/apps/slinky/src/main.c
----------------------------------------------------------------------
diff --git a/apps/slinky/src/main.c b/apps/slinky/src/main.c
index 298696b..9a2b3ac 100755
--- a/apps/slinky/src/main.c
+++ b/apps/slinky/src/main.c
@@ -270,6 +270,33 @@ init_tasks(void)
     os_eventq_dflt_set(&slinky_evq);
 }
 
+static int
+slinky_sim_accel_init(struct os_dev *dev, void *arg)
+{
+    struct sim_accel_cfg cfg;
+    int rc;
+
+    rc = sim_accel_init(dev, arg);
+    if (rc != 0) {
+        goto err;
+    }
+
+    cfg.sac_nr_samples = 10;
+    cfg.sac_nr_axises = 1;
+    /* read once per sec.  API should take this value in ms. */
+    cfg.sac_sample_itvl = OS_TICKS_PER_SEC;
+
+    rc = sim_accel_config((struct sim_accel *) dev, &cfg);
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+
 /**
  * main
  *
@@ -323,7 +350,7 @@ main(int argc, char **argv)
     sensor_pkg_init();
 
     os_dev_create((struct os_dev *) &sim_accel_sensor, "simaccel0",
-            OS_DEV_INIT_KERNEL, OS_DEV_INIT_PRIMARY, sim_accel_init, NULL);
+            OS_DEV_INIT_KERNEL, OS_DEV_INIT_PRIMARY, slinky_sim_accel_init, NULL);
 
     os_start();
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/269791e4/hw/drivers/sensors/sim/src/generic_accel.c
----------------------------------------------------------------------
diff --git a/hw/drivers/sensors/sim/src/generic_accel.c b/hw/drivers/sensors/sim/src/generic_accel.c
index 164c1e2..cbed0d0 100644
--- a/hw/drivers/sensors/sim/src/generic_accel.c
+++ b/hw/drivers/sensors/sim/src/generic_accel.c
@@ -112,7 +112,8 @@ sim_accel_sensor_read(struct sensor *sensor, sensor_type_t type,
     int i;
     int rc;
 
-    if (type != SENSOR_TYPE_ACCELEROMETER) {
+    /* If the read isn't looking for accel data, then don't do anything. */
+    if (!(type & SENSOR_TYPE_ACCELEROMETER)) {
         rc = SYS_EINVAL;
         goto err;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/269791e4/hw/sensor/include/sensor/accel.h
----------------------------------------------------------------------
diff --git a/hw/sensor/include/sensor/accel.h b/hw/sensor/include/sensor/accel.h
index e56871b..431e05c 100644
--- a/hw/sensor/include/sensor/accel.h
+++ b/hw/sensor/include/sensor/accel.h
@@ -29,6 +29,7 @@ extern "C" {
 #endif
 
 /* Data representing a singular read from an accelerometer.
+ * All values are in MS^2
  */
 struct sensor_accel_data {
     int32_t sad_x;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/269791e4/hw/sensor/include/sensor/sensor.h
----------------------------------------------------------------------
diff --git a/hw/sensor/include/sensor/sensor.h b/hw/sensor/include/sensor/sensor.h
index 64ab640..2ec36f6 100644
--- a/hw/sensor/include/sensor/sensor.h
+++ b/hw/sensor/include/sensor/sensor.h
@@ -247,6 +247,8 @@ int sensor_init(struct sensor *, struct os_dev *dev);
 int sensor_lock(struct sensor *);
 void sensor_unlock(struct sensor *);
 int sensor_register_listener(struct sensor *, struct sensor_listener *);
+int sensor_unregister_listener(struct sensor *, struct sensor_listener *);
+
 int sensor_read(struct sensor *, sensor_type_t, sensor_data_func_t, void *,
         uint32_t);
 
@@ -317,6 +319,7 @@ sensor_get_interface(struct sensor *sensor, sensor_type_t type)
 int sensor_mgr_lock(void);
 void sensor_mgr_unlock(void);
 int sensor_mgr_register(struct sensor *);
+struct os_eventq *sensor_mgr_evq_get(void);
 
 
 typedef int (*sensor_mgr_compare_func_t)(struct sensor *, void *);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/269791e4/hw/sensor/src/sensor.c
----------------------------------------------------------------------
diff --git a/hw/sensor/src/sensor.c b/hw/sensor/src/sensor.c
index 319de12..ab4c413 100644
--- a/hw/sensor/src/sensor.c
+++ b/hw/sensor/src/sensor.c
@@ -214,7 +214,7 @@ done:
     os_callout_reset(&sensor_mgr.mgr_wakeup_callout, task_next_wakeup);
 }
 
-static struct os_eventq *
+struct os_eventq *
 sensor_mgr_evq_get(void)
 {
     os_eventq_ensure(&sensor_mgr.mgr_eventq, NULL);
@@ -474,6 +474,29 @@ err:
     return (rc);
 }
 
+int
+sensor_unregister_listener(struct sensor *sensor,
+        struct sensor_listener *listener)
+{
+    int rc;
+
+    rc = sensor_lock(sensor);
+    if (rc != 0) {
+        goto err;
+    }
+
+    /* Remove this entry from the list */
+    SLIST_REMOVE(&sensor->s_listener_list, listener, sensor_listener,
+            sl_next);
+
+    sensor_unlock(sensor);
+
+    return (0);
+err:
+    return (rc);
+}
+
+
 struct sensor_read_ctx {
     sensor_data_func_t user_func;
     void *user_arg;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/269791e4/hw/sensor/src/sensor_shell.c
----------------------------------------------------------------------
diff --git a/hw/sensor/src/sensor_shell.c b/hw/sensor/src/sensor_shell.c
index 6c8b7ac..dbde99f 100644
--- a/hw/sensor/src/sensor_shell.c
+++ b/hw/sensor/src/sensor_shell.c
@@ -30,6 +30,7 @@
 #include "os/os.h"
 
 #include "sensor/sensor.h"
+#include "sensor/accel.h"
 #include "console/console.h"
 #include "shell/shell.h"
 
@@ -74,6 +75,80 @@ sensor_cmd_list_sensors(void)
     sensor_mgr_unlock();
 }
 
+struct sensor_shell_read_ctx {
+    sensor_type_t type;
+    int num_entries;
+};
+
+static int
+sensor_shell_read_listener(struct sensor *sensor, void *arg, void *data)
+{
+    struct sensor_shell_read_ctx *ctx;
+    struct sensor_accel_data *sad;
+
+    ctx = (struct sensor_shell_read_ctx *) arg;
+
+    ++ctx->num_entries;
+
+    if (ctx->type == SENSOR_TYPE_ACCELEROMETER) {
+        sad = (struct sensor_accel_data *) data;
+        if (sad->sad_x != SENSOR_ACCEL_DATA_UNUSED) {
+            console_printf("x = %d, ", sad->sad_x);
+        }
+        if (sad->sad_y != SENSOR_ACCEL_DATA_UNUSED) {
+            console_printf("y = %d, ", sad->sad_y);
+        }
+        if (sad->sad_z != SENSOR_ACCEL_DATA_UNUSED) {
+            console_printf("z = %d", sad->sad_z);
+        }
+        console_printf("\n");
+    }
+
+    return (0);
+}
+
+static void
+sensor_cmd_read(char *name, sensor_type_t type, int nsamples)
+{
+    struct sensor *sensor;
+    struct sensor_listener listener;
+    struct sensor_shell_read_ctx ctx;
+    int rc;
+
+    /* Look up sensor by name */
+    sensor = sensor_mgr_find_next_bydevname(name, NULL);
+    if (!sensor) {
+        console_printf("Sensor %s not found!\n", name);
+    }
+
+    /* Register a listener and then trigger/read a bunch of samples.
+     */
+    memset(&ctx, 0, sizeof(ctx));
+
+    ctx.type = type;
+
+    listener.sl_sensor_type = type;
+    listener.sl_func = sensor_shell_read_listener;
+    listener.sl_arg = &ctx;
+
+    rc = sensor_register_listener(sensor, &listener);
+    if (rc != 0) {
+        goto err;
+    }
+
+    while (1) {
+        rc = sensor_read(sensor, type, NULL, NULL, OS_TIMEOUT_NEVER);
+        if (ctx.num_entries >= nsamples) {
+            break;
+        }
+    }
+
+    sensor_unregister_listener(sensor, &listener);
+
+err:
+    return;
+}
+
 
 static int
 sensor_cmd_exec(int argc, char **argv)
@@ -90,6 +165,15 @@ sensor_cmd_exec(int argc, char **argv)
     subcmd = argv[1];
     if (!strcmp(subcmd, "list")) {
         sensor_cmd_list_sensors();
+    } else if (!strcmp(subcmd, "read")) {
+        if (argc != 5) {
+            console_printf("Three arguments required for read: device name, "
+                    "sensor type and number of samples, only %d provided.\n",
+                    argc - 2);
+            rc = SYS_EINVAL;
+            goto err;
+        }
+        sensor_cmd_read(argv[2], (sensor_type_t) atoi(argv[3]), atoi(argv[4]));
     } else {
         console_printf("Unknown sensor command %s\n", subcmd);
         rc = SYS_EINVAL;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/269791e4/hw/sensor/syscfg.yml
----------------------------------------------------------------------
diff --git a/hw/sensor/syscfg.yml b/hw/sensor/syscfg.yml
index 0826cc9..879fbb2 100644
--- a/hw/sensor/syscfg.yml
+++ b/hw/sensor/syscfg.yml
@@ -21,7 +21,7 @@
 syscfg.defs:
     SENSOR_MGR_WAKEUP_RATE:
         description: 'The default wakeup rate of the sensor manager'
-        value: 1000
+        value: 1
 
     SENSOR_CLI:
         description: 'Whether or not to enable the sensor shell support'


[4/6] incubator-mynewt-core git commit: update sensor implementation, closer to final cut, still not compiling

Posted by st...@apache.org.
update sensor implementation, closer to final cut, still not compiling


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/1fa93e52
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/1fa93e52
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/1fa93e52

Branch: refs/heads/sensors_branch
Commit: 1fa93e5274c9b692537884facfbd4fc3578ebe83
Parents: f449f3d
Author: Sterling Hughes <st...@apache.org>
Authored: Mon Dec 5 19:14:32 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Mon Dec 12 21:42:04 2016 -0800

----------------------------------------------------------------------
 hw/sensor/include/sensor/sensor.h | 155 +++++++------
 hw/sensor/src/sensor.c            | 398 +++++++++++++++++++++++++--------
 2 files changed, 394 insertions(+), 159 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1fa93e52/hw/sensor/include/sensor/sensor.h
----------------------------------------------------------------------
diff --git a/hw/sensor/include/sensor/sensor.h b/hw/sensor/include/sensor/sensor.h
index d2e0af1..24661fa 100644
--- a/hw/sensor/include/sensor/sensor.h
+++ b/hw/sensor/include/sensor/sensor.h
@@ -26,13 +26,35 @@
 extern "C" {
 #endif
 
-struct sensor_dev;
+
+/**
+ * @{ Sensor Manager API
+ */
+#define SENSOR_MGR_WAKEUP_TICKS (MYNEWT_VAL(SENSOR_MGR_WAKEUP_RATE) * \
+        (OS_TICKS_PER_SEC / 1000))
+
+int sensor_mgr_register(struct sensor *);
+
+typedef int (*sensor_mgr_compare_func_t*)(struct sensor *, void *);
+struct sensor *sensor_mgr_find_next(sensor_mgr_compare_func_t, void *,
+        struct sensor *);
+struct sensor *sensor_mgr_find_next_bytype(sensor_type_t, struct sensor *);
+struct sensor *sensor_mgr_find_next_bydevname(char *, struct sensor *);
+
+/**
+ * }@
+ */
+
+
+/**
+ * @{ Sensor API
+ */
 
 typedef enum {
     /* No sensor type, used for queries */
     SENSOR_TYPE_NONE                 = 0,
-    /* Accellerometer functionality supported */
-    SENSOR_TYPE_ACCELLEROMETER       = (1 << 0),
+    /* Accelerometer functionality supported */
+    SENSOR_TYPE_ACCELEROMETER       = (1 << 0),
     /* Ambient temperature supported */
     SENSOR_TYPE_AMBIENT_TEMPERATURE  = (1 << 1),
     /* Gravity supported */
@@ -41,8 +63,8 @@ typedef enum {
     SENSOR_TYPE_GYROSCOPE            = (1 << 3),
     /* Light supported */
     SENSOR_TYPE_LIGHT                = (1 << 4),
-    /* Linear accelleration supported */
-    SENSOR_TYPE_LINEAR_ACCELLERATION = (1 << 5),
+    /* Linear acceleration supported */
+    SENSOR_TYPE_LINEAR_ACCELERATION = (1 << 5),
     /* Magnetic field supported */
     SENSOR_TYPE_MAGNETIC_FIELD       = (1 << 6),
     /* Orientation sensor supported */
@@ -79,22 +101,31 @@ typedef enum {
 #define SENSOR_LISTENER_TYPE_NOTIFY (0)
 #define SENSOR_LISTENER_TYPE_POLL   (1)
 
-struct sensor_reading {
-    /* The timestamp, in CPU time ticks that the sensor value
-     * was read.
-     */
-    uint32_t sv_ts;
-    /* The value of the sensor, based on the type being read.
-     */
-    void *sv_value;
-};
-
+/**
+ * Opaque 32-bit value, must understand underlying sensor type
+ * format in order to interpret.
+ */
 #define SENSOR_VALUE_TYPE_OPAQUE (0)
+/**
+ * 32-bit signed integer
+ */
 #define SENSOR_VALUE_TYPE_INT32  (1)
+/**
+ * 32-bit floating point
+ */
 #define SENSOR_VALUE_TYPE_FLOAT  (2)
 
+/**
+ * Configuration structure, describing a specific sensor type off of
+ * an existing sensor.
+ */
 struct sensor_cfg {
+    /* The value type for this sensor (e.g. SENSOR_VALUE_TYPE_INT32).
+     * Used to describe the result format for the value corresponding
+     * to a specific sensor type.
+     */
     uint8_t sc_valtype;
+    /* Reserved for future usage */
     uint8_t _reserved[3];
 };
 
@@ -102,33 +133,32 @@ struct sensor_cfg {
  * Callback for handling sensor data, specified in a sensor listener.
  *
  * @param The sensor for which data is being returned
- * @param The sensor listener that's configured to handle this data.
- * @param A single sensor reading
+ * @param The argument provided to sensor_read() function.
+ * @param A single sensor reading for that sensor listener
  *
  * @return 0 on succes, non-zero error code on failure.
  */
-typedef int (*sensor_data_func_t)(struct sensor *,
-        struct sensor_listener *,
-        struct sensor_reading *v);
+typedef int (*sensor_data_func_t)(struct sensor *, void *, void *);
 
 /**
  *
  */
 struct sensor_listener {
-    /* The type of sensor listener, either notification based "tell me
-     * when there is a value," or polled - collect this value for me
-     * every 'n' seconds and notify me.
+    /* The type of sensor data to listen for, this is interpreted as a
+     * mask, and this listener is called for all sensor types on this
+     * sensor that match the mask.
      */
-    uint8_t sl_type;
-    /* Padding */
-    uint8_t _pad1;
-    /* Poll rate in miliseconds. */
-    uint16_t sl_poll_rate_ms;
-    /* The type of sensor data to listen for */
     sensor_type_t sl_sensor_type;
+
     /* Sensor data handler function, called when has data */
     sensor_data_func_t sl_func;
 
+    /* Argument for the sensor listener */
+    void *sl_arg;
+
+    /* Next item in the sensor listener list.  The head of this list is
+     * contained within the sensor object.
+     */
     SLIST_ENTRY(sensor_listener) sl_next;
 };
 
@@ -146,15 +176,22 @@ struct sensor;
 typedef void *(*sensor_get_interface_func_t)(struct sensor *, sensor_type_t);
 
 /**
- * Read a value from a sensor, given a specific sensor type (e.g. SENSOR_TYPE_PROXIMITY).
+ * Read a single value from a sensor, given a specific sensor type
+ * (e.g. SENSOR_TYPE_PROXIMITY).
  *
  * @param The sensor to read from
- * @param The type of sensor value to read
- * @param A pointer to the sensor value to place the returned result into.
+ * @param The type(s) of sensor values to read.  Mask containing that type, provide
+ *        all, to get all values.
+ * @param The function to call with each value read.  If NULL, it calls all
+ *        sensor listeners associated with this function.
+ * @param The argument to pass to the read callback.
+ * @param Timeout.  If block until result, specify OS_TIMEOUT_NEVER, 0 returns
+ *        immediately (no wait.)
  *
  * @return 0 on success, non-zero error code on failure.
  */
-typedef int (*sensor_read_func_t)(struct sensor *, sensor_type_t, void *);
+typedef int (*sensor_read_func_t)(struct sensor *, sensor_data_func_t, void *,
+        uint32_t);
 
 /**
  * Get the configuration of the sensor for the sensor type.  This includes
@@ -174,9 +211,6 @@ struct sensor_driver_funcs {
     sensor_get_config_func_t sd_get_config;
 };
 
-/* XXX: Make me a syscfg */
-#define SENSOR_DEFAULT_POLL_RATE (10 * (OS_TICKS_PER_SEC/1000))
-
 struct sensor {
     /* The OS device this sensor inherits from, this is typically a sensor specific
      * driver.
@@ -187,6 +221,16 @@ struct sensor {
      * that variable.
      */
     sensor_type_t s_types;
+    /**
+     * Poll rate in MS for this sensor.
+     */
+    uint32_t s_poll_rate;
+
+    /**
+     * The next time at which we want to poll data from this sensor
+     */
+    os_time_t s_next_run;
+
     /* Sensor driver specific functions, created by the device registering the sensor.
      */
     const struct sensor_driver_funcs s_funcs;
@@ -197,12 +241,12 @@ struct sensor {
     SLIST_ENTRY(sensor) s_next;
 };
 
-int sensor_init(struct sensor *);
-int sensor_register(struct sensor *);
+
+int sensor_init(struct sensor *, struct os_dev *dev);
 int sensor_lock(struct sensor *);
 void sensor_unlock(struct sensor *);
 int sensor_register_listener(struct sensor *, struct sensor_listener *);
-struct sensor *sensor_find_next(sensor_type_t, struct sensor *);
+int sensor_read(struct sensor *, sensor_type_t, sensor_data_func_t, uint32_t);
 
 
 /**
@@ -222,35 +266,7 @@ sensor_get_config(struct sensor *sensor, sensor_type_t type, struct sensor_confi
 }
 
 /**
- * Read the data for sensor type "type," from the sensor, "sensor" and
- * return the result into the "value" parameter.
- *
- * @param The senssor to read data from
- * @param The type of sensor data to read from the sensor
- * @param The sensor value to place data into.
- *
- * @return 0 on success, non-zero on failure.
- */
-static inline int
-sensor_read(struct sensor *sensor, sensor_type_t type, struct sensor_value *value)
-{
-    int rc;
-
-    rc = sensor_lock(sensor);
-    if (rc != 0) {
-        goto done;
-    }
-
-    rc = sensor->s_funcs.sd_read(sensor, type, value);
-
-    sensor_unlock(sensor);
-
-done:
-    return (rc);
-}
-
-/**
- * Get a more specific interface for this sensor, like accellerometer, or gyro.
+ * Get a more specific interface for this sensor, like accelerometer, or gyro.
  *
  * @param The sensor to get the interface from
  * @param The type of interface to get from this sensor
@@ -276,6 +292,9 @@ done:
     return (rc);
 }
 
+/**
+ * @}
+ */
 
 #ifdef __cplusplus
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/1fa93e52/hw/sensor/src/sensor.c
----------------------------------------------------------------------
diff --git a/hw/sensor/src/sensor.c b/hw/sensor/src/sensor.c
index 3c219b2..251b171 100644
--- a/hw/sensor/src/sensor.c
+++ b/hw/sensor/src/sensor.c
@@ -24,8 +24,215 @@
 
 struct os_callout sensors_callout;
 
-TAILQ_HEAD(, struct sensor) g_sensor_list =
-    TAILQ_HEAD_INITIALIZER(&g_sensor_list);
+struct {
+    struct os_mutex mgr_lock;
+
+    struct os_callout mgr_poll_callout;
+
+    TAILQ_HEAD(, struct sensor) mgr_sensor_list;
+} sensor_mgr;
+
+static int
+sensor_mgr_lock(void)
+{
+    int rc;
+
+    rc = os_mutex_pend(&sensor_mgr.mgr_lock);
+    if (rc == 0 || rc == OS_NOT_STARTED) {
+        return (0);
+    }
+    return (rc);
+}
+
+static void
+sensor_mgr_unlock(void)
+{
+    (void) os_mutex_release(&sensor_mgr.mgr_lock);
+}
+
+static void
+sensor_mgr_remove(struct sensor *sensor)
+{
+    TAILQ_REMOVE(&sensor_mgr.mgr_sensor_list, sensor, s_next);
+}
+
+static void
+sensor_mgr_insert(struct sensor *sensor)
+{
+    struct sensor *cursor;
+
+    cursor = NULL;
+    TAILQ_FOREACH(cursor, &sensor_mgr.mgr_sensor_list, s_next) {
+        if (cursor->s_next_wakeup == OS_TIMEOUT_NEVER) {
+            break;
+        }
+
+        if (OS_TIME_TICK_LT(sensor->s_next_wakeup, cursor->s_next_wakeup)) {
+            break;
+        }
+    }
+
+    if (cursor) {
+        TAILQ_INSERT_BEFORE(cursor, sensor, s_next);
+    } else {
+        TAILQ_INSERT_TAIL(&sensor_mgr.mgr_sensor_list, s_next);
+    }
+}
+
+static os_time_t
+sensor_mgr_poll_one(struct sensor *sensor, os_time_t now)
+{
+    int rc;
+
+    rc = sensor_lock(sensor);
+    if (rc != 0) {
+        goto err;
+    }
+
+    /* Sensor read results.  Every time a sensor is read, all of its
+     * listeners are called by default.  Specify NULL as a callback,
+     * because we just want to run all the listeners.
+     */
+    sensor_read(sensor, SENSOR_TYPE_ALL, NULL, NULL, OS_TIMEOUT_NEVER);
+
+    /* Remove the sensor from the sensor list for insertion sort. */
+    sensor_mgr_remove(sensor);
+
+    /* Set next wakeup, and insertion sort the sensor back into the
+     * list.
+     */
+    sensor->s_next_run = now + os_time_ms_to_ticks(sensor->s_poll_rate);
+
+    /* Re-insert the sensor manager, with the new wakeup time. */
+    sensor_mgr_insert(sensor);
+
+    /* Unlock the sensor to allow other access */
+    sensor_unlock(sensor);
+
+    return (sensor->s_next_run);
+err:
+    /* Couldn't lock it.  Re-run task and spin until we get result. */
+    return (0);
+}
+
+/**
+ * Event that wakes up the sensor manager, this goes through the sensor
+ * list and polls any active sensors.
+ */
+static void
+sensor_mgr_wakeup_event(struct os_event *ev)
+{
+    struct sensor *cursor;
+    os_time_t now;
+    os_time_t task_next_wakeup;
+    int rc;
+
+    rc = sensor_mgr_lock();
+    if (rc != 0) {
+        goto err;
+    }
+
+    now = os_time_get();
+    task_next_wakeup = now + SENSOR_MGR_WAKEUP_TICKS;
+
+    TAILQ_FOREACH(cursor, &sensor_mgr.mgr_sensor_list, s_next) {
+        /* Sensors that are not periodic are inserted at the end of the sensor
+         * list.
+         */
+        if (cursor->s_next_run == OS_TIMEOUT_NEVER) {
+            break;
+        }
+
+        /* List is sorted by what runs first.  If we reached the first element that
+         * doesn't run, break out.
+         */
+        if (OS_TIME_TICK_LT(now, cursor->s_next_run)) {
+            break;
+        }
+
+        /* Sensor poll one completes the poll, updates the sensor's "next run,"
+         * and re-inserts it into the list.  It returns the next wakeup time
+         * for this sensor.
+         */
+        next_wakeup = sensor_poll_one(cursor, now);
+
+        /* If the next wakeup time for this sensor is before the task's next
+         * scheduled wakeup, move that forward, so we can collect data from that
+         * sensor
+         */
+        if (task_next_wakeup > next_wakeup) {
+            task_next_wakeup = next_wakeup;
+        }
+    }
+
+    os_callout_reset(&sensor_mgr.mgr_wakeup_callout, task_next_wakeup);
+}
+
+
+
+
+static void
+sensor_mgr_init(void)
+{
+    memset(&sensor_mgr, 0, sizeof(sensor_mgr));
+    TAILQ_INIT(&sensor_mgr.mgr_sensor_list);
+    /**
+     * Initialize sensor polling callout and set it to fire on boot.
+     */
+    os_callout_init(&sensor_mgr.mgr_wakeup_callout, sensor_mgr_evq_get(),
+            sensor_mgr_wakeup_event, NULL);
+    os_callout_reset(&sensor_mgr.mgr_wakeup_callout, 0);
+
+    os_mutex_init(&sensor_mgr.mgr_lock);
+}
+
+
+struct sensor *
+sensor_mgr_find_next(sensor_mgr_compare_func_t compare_func, void *arg,
+        struct sensor *prev_cursor)
+{
+    struct sensor *cursor;
+    int rc;
+
+    cursor = NULL;
+
+    /* Couldn't acquire lock of sensor list, exit */
+    rc = sensor_mgr_lock();
+    if (rc != 0) {
+        goto done;
+    }
+
+    cursor = prev_cursor;
+    if (cursor == NULL) {
+        cursor = TAILQ_FIRST(&g_sensor_list);
+    }
+
+    while (cursor != NULL) {
+        if (compare_func(cursor, arg)) {
+            break;
+        }
+    }
+
+    sensor_mgr_unlock();
+
+done:
+    return (cursor);
+}
+
+
+static int
+sensor_mgr_match_bytype(struct sensor *sensor, void *arg)
+{
+    sensor_type_t *type;
+
+    type = (sensor_type_t *) arg;
+
+    if ((*type & sensor->s_type) != 0) {
+        return (1);
+    } else {
+        return (0);
+    }
+}
 
 /**
  * Find the "next" sensor available for a given sensor type.
@@ -40,25 +247,62 @@ TAILQ_HEAD(, struct sensor) g_sensor_list =
  *         none found.
  */
 struct sensor *
-sensor_find_next(sensor_type_t type, struct sensor *prev_cursor)
+sensor_mgr_find_next_bytype(sensor_type_t type, struct sensor *prev_cursor)
 {
-    struct sensor *cursor;
+    return (sensor_mgr_find_next(sensor_mgr_match_bytype, type, prev_cursor));
+}
 
-    cursor = prev_cursor;
-    if (cursor == NULL) {
-        cursor = TAILQ_FIRST(&g_sensor_list);
-    }
+static int
+sensor_mgr_match_bydevname(struct sensor *sensor, void *arg)
+{
+    char *devname;
 
-    while (cursor != NULL) {
-        if ((cursor->s_type & type) != 0) {
-            return (cursor);
-        }
-        cursor = TAILQ_NEXT(cursor);
+    devname = (char *) arg;
+
+    if (!strcmp(sensor->s_dev->od_name, devname)) {
+        return (1);
     }
 
-    return (NULL);
+    return (0);
 }
 
+
+/**
+ *
+ */
+struct sensor *
+sensor_mgr_find_next_bydevname(char *devname, struct sensor *prev_cursor)
+{
+    return (sensor_mgr_find_next(sensor_mgr_match_bydevname, devname,
+            prev_cursor));
+}
+
+
+/**
+ * Register the sensor with the global sensor list.   This makes the sensor searchable
+ * by other packages, who may want to look it up by type.
+ *
+ * @param The sensor to register
+ *
+ * @return 0 on success, non-zero error code on failure.
+ */
+int
+sensor_mgr_register(struct sensor *sensor)
+{
+    return (0);
+}
+
+
+void
+sensor_pkg_init(void)
+{
+    /* Ensure this is only called by sysinit */
+    SYSINIT_ASSERT_ACTIVE();
+
+    sensor_mgr_init();
+}
+
+
 /**
  * Lock access to the sensor specified by sensor.  Blocks until lock acquired.
  *
@@ -95,12 +339,13 @@ sensor_unlock(struct sensor *sensor)
 /**
  * Initialize a sensor
  *
- * @param sensor The sensor to initialize
+ * @param The sensor to initialize
+ * @param The device to associate with this sensor.
  *
  * @return 0 on success, non-zero error code on failure.
  */
 int
-sensor_init(struct sensor *sensor)
+sensor_init(struct sensor *sensor, struct os_dev *dev)
 {
     memset(sensor, 0, sizeof(*sensor));
 
@@ -108,26 +353,14 @@ sensor_init(struct sensor *sensor)
     if (rc != 0) {
         goto err;
     }
+    sensor->s_dev = dev;
+
     return (0);
 err:
     return (rc);
 }
 
 
-/**
- * Register the sensor with the global sensor list.   This makes the sensor searchable
- * by other packages, who may want to look it up by type.
- *
- * @param The sensor to register
- *
- * @return 0 on success, non-zero error code on failure.
- */
-int
-sensor_register(struct sensor *sensor)
-{
-    return (0);
-}
-
 
 /**
  * Register a sensor listener.  This allows a calling application to receive
@@ -161,80 +394,63 @@ err:
     return (rc);
 }
 
-static os_time_t
-sensor_poll_run(struct sensor *sensor, os_time_t now)
+struct sensor_read_ctx {
+    sensor_data_func_t user_func;
+    void *user_arg;
+};
+
+static int
+sensor_read_data_func(struct sensor *sensor, void *arg, void *data)
 {
     struct sensor_listener *listener;
-    struct sensor_value value;
-    os_time_t next_run;
-    int rc;
-
-    next_run = OS_TIMEOUT_NEVER;
+    struct sensor_read_ctx *ctx;
 
+    /* Notify all listeners first */
     SLIST_FOREACH(listener, &sensor->s_listener_list, sl_next) {
-        if (listener->sl_type == SENSOR_LISTENER_TYPE_NOTIFY) {
-            continue;
-        }
-        if (listener->sl_next_run >= now) {
-            rc |= sensor_read(sensor, type, &value);
-            rc |= listener->sl_func(sensor, listener, value);
-
-            listener->sl_next_run = os_time_get() +
-                os_time_ms_to_ticks(sl->sl_poll_rate_ms);
-            if (listener->sl_next_run < next_run) {
-                next_run = listener->sl_next_run;
-            }
-        }
+        listener->sl_func(sensor, listener->sl_arg, data);
     }
-    sensor->s_next_wakeup = next_run;
 
-    return (next_run);
+    /* Call data function */
+    ctx = (struct sensor_read_ctx *) arg;
+    if (ctx->user_func != NULL) {
+        return (ctx->user_func(sensor, ctx->user_arg, data));
+    } else {
+        return (0);
+    }
 }
 
-
-static void
-sensor_poll_event(struct os_event *ev)
+/**
+ * Read the data for sensor type "type," from the sensor, "sensor" and
+ * return the result into the "value" parameter.
+ *
+ * @param The senssor to read data from
+ * @param The type of sensor data to read from the sensor
+ * @param The callback to call for data returned from that sensor
+ * @param The argument to pass to this callback.
+ * @param Timeout before aborting sensor read
+ *
+ * @return 0 on success, non-zero on failure.
+ */
+int
+sensor_read(struct sensor *sensor, sensor_type_t type,
+        sensor_data_func_t data_func, void *arg, uint32_t timeout)
 {
-    struct sensor *cursor;
-    os_time_t time;
-    os_time_t next_wakeup;
-    os_time_t sensor_task_next_wakeup;
-
-    time = os_time_get();
+    struct sensor_read_ctx src;
+    int rc;
 
-    sensor_next_wakeup = time + SENSOR_DEFAULT_POLL_RATE;
+    rc = sensor_lock(sensor);
+    if (rc != 0) {
+        goto done;
+    }
 
-    TAILQ_FOREACH(cursor, &g_sensor_list, s_next) {
-        if (cursor->s_next_wakeup == OS_TIMEOUT_NEVER) {
-            break;
-        }
+    src.user_func = data_func;
+    src.user_arg = arg;
 
-        /* Sort lowest to highest, so when we encounter a entry in the list
-         * that isn't ready, we can exit.
-         */
-        if (cursor->s_poll_expire < time) {
-            break;
-        }
+    rc = sensor->s_funcs.sd_read(sensor, type, data_func, arg, timeout);
 
-        next_wakeup = sensor_poll_run(cursor, now);
-        if (next_wakeup < sensor_task_next_wakeup) {
-            sensor_task_next_wakeup = next_wakeup;
-        }
-    }
+    sensor_unlock(sensor);
 
-    os_callout_reset(&sensor_callout, sensor_task_next_wakeup);
+done:
+    return (rc);
 }
 
-void
-sensor_pkg_init(void)
-{
-    /* Ensure this is only called by sysinit */
-    SYSINIT_ASSERT_ACTIVE();
-
-    /**
-     * Initialize callout and set it to fire on boot.
-     */
-    os_callout_init(&sensors_callout, sensor_evq_get(), sensors_poll_event,
-            NULL);
-    os_callout_reset(&sensors_callout, 0);
-}


[6/6] incubator-mynewt-core git commit: get sensor apis compiling.

Posted by st...@apache.org.
get sensor apis compiling.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/397f4a2d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/397f4a2d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/397f4a2d

Branch: refs/heads/sensors_branch
Commit: 397f4a2d7eb52f9933f6c4f236898d24e3c9fca3
Parents: 1fa93e5
Author: Sterling Hughes <st...@apache.org>
Authored: Mon Dec 5 21:06:31 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Mon Dec 12 21:42:04 2016 -0800

----------------------------------------------------------------------
 apps/slinky/pkg.yml               |   1 +
 hw/sensor/include/sensor/sensor.h |  84 ++++++++++++-----------
 hw/sensor/src/sensor.c            | 121 ++++++++++++++++++++++-----------
 hw/sensor/syscfg.yml              |  24 +++++++
 4 files changed, 151 insertions(+), 79 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/397f4a2d/apps/slinky/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/slinky/pkg.yml b/apps/slinky/pkg.yml
index f0785b9..cd63f0a 100644
--- a/apps/slinky/pkg.yml
+++ b/apps/slinky/pkg.yml
@@ -26,6 +26,7 @@ pkg.keywords:
 
 pkg.deps:
     - sys/console/full
+    - hw/sensor 
     - test/flash_test
     - mgmt/imgmgr
     - mgmt/newtmgr

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/397f4a2d/hw/sensor/include/sensor/sensor.h
----------------------------------------------------------------------
diff --git a/hw/sensor/include/sensor/sensor.h b/hw/sensor/include/sensor/sensor.h
index 24661fa..a825b51 100644
--- a/hw/sensor/include/sensor/sensor.h
+++ b/hw/sensor/include/sensor/sensor.h
@@ -20,31 +20,16 @@
 #ifndef __SENSOR_H__
 #define __SENSOR_H__
 
-#include <os/os_dev.h>
+#include "os/os.h"
+#include "os/os_dev.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
 
-/**
- * @{ Sensor Manager API
- */
-#define SENSOR_MGR_WAKEUP_TICKS (MYNEWT_VAL(SENSOR_MGR_WAKEUP_RATE) * \
-        (OS_TICKS_PER_SEC / 1000))
-
-int sensor_mgr_register(struct sensor *);
-
-typedef int (*sensor_mgr_compare_func_t*)(struct sensor *, void *);
-struct sensor *sensor_mgr_find_next(sensor_mgr_compare_func_t, void *,
-        struct sensor *);
-struct sensor *sensor_mgr_find_next_bytype(sensor_type_t, struct sensor *);
-struct sensor *sensor_mgr_find_next_bydevname(char *, struct sensor *);
-
-/**
- * }@
- */
-
+/* Forward declare sensor structure defined below. */
+struct sensor;
 
 /**
  * @{ Sensor API
@@ -98,9 +83,6 @@ typedef enum {
 } sensor_type_t;
 
 
-#define SENSOR_LISTENER_TYPE_NOTIFY (0)
-#define SENSOR_LISTENER_TYPE_POLL   (1)
-
 /**
  * Opaque 32-bit value, must understand underlying sensor type
  * format in order to interpret.
@@ -190,8 +172,8 @@ typedef void *(*sensor_get_interface_func_t)(struct sensor *, sensor_type_t);
  *
  * @return 0 on success, non-zero error code on failure.
  */
-typedef int (*sensor_read_func_t)(struct sensor *, sensor_data_func_t, void *,
-        uint32_t);
+typedef int (*sensor_read_func_t)(struct sensor *, sensor_type_t,
+        sensor_data_func_t, void *, uint32_t);
 
 /**
  * Get the configuration of the sensor for the sensor type.  This includes
@@ -216,6 +198,11 @@ struct sensor {
      * driver.
      */
     struct os_dev *s_dev;
+
+    /* The lock for this sensor object */
+    struct os_mutex s_lock;
+
+
     /* A bit mask describing the types of sensor objects available from this sensor.
      * If the bit corresponding to the sensor_type_t is set, then this sensor supports
      * that variable.
@@ -236,17 +223,17 @@ struct sensor {
     const struct sensor_driver_funcs s_funcs;
     /* A list of listeners that are registered to receive data off of this sensor
      */
-    SLIST_HEAD(, struct sensor_listener) s_listener_list;
+    SLIST_HEAD(, sensor_listener) s_listener_list;
     /* The next sensor in the global sensor list. */
-    SLIST_ENTRY(sensor) s_next;
+    TAILQ_ENTRY(sensor) s_next;
 };
 
-
 int sensor_init(struct sensor *, struct os_dev *dev);
 int sensor_lock(struct sensor *);
 void sensor_unlock(struct sensor *);
 int sensor_register_listener(struct sensor *, struct sensor_listener *);
-int sensor_read(struct sensor *, sensor_type_t, sensor_data_func_t, uint32_t);
+int sensor_read(struct sensor *, sensor_type_t, sensor_data_func_t, void *,
+        uint32_t);
 
 
 /**
@@ -260,7 +247,8 @@ int sensor_read(struct sensor *, sensor_type_t, sensor_data_func_t, uint32_t);
  * @return 0 on success, non-zero error code on failure.
  */
 static inline int
-sensor_get_config(struct sensor *sensor, sensor_type_t type, struct sensor_config *cfg)
+sensor_get_config(struct sensor *sensor, sensor_type_t type,
+        struct sensor_cfg *cfg)
 {
     return (sensor->s_funcs.sd_get_config(sensor, type, cfg));
 }
@@ -271,26 +259,40 @@ sensor_get_config(struct sensor *sensor, sensor_type_t type, struct sensor_confi
  * @param The sensor to get the interface from
  * @param The type of interface to get from this sensor
  *
- * @return A pointer to the more specific sensor interface on success, or NULL if
- *         not found.
+ * @return A pointer to the more specific sensor interface on success, or NULL
+ * if not found.
  */
 static inline void *
 sensor_get_interface(struct sensor *sensor, sensor_type_t type)
 {
-    int rc;
+    return (sensor->s_funcs.sd_get_interface(sensor, type));
+}
 
-    rc = sensor_lock(sensor);
-    if (rc != 0) {
-        goto done;
-    }
+/**
+ * @{ Sensor Manager API
+ */
 
-    rc = sensor->s_funcs.sd_get_interface(sensor, type);
 
-    sensor_unlock(sensor);
+/* Default number of ticks to wakeup the sensor task.  If sensor
+ * polling has been configured more frequently than this, it will
+ * be triggered instead.
+ */
+#define SENSOR_MGR_WAKEUP_TICKS (MYNEWT_VAL(SENSOR_MGR_WAKEUP_RATE) * \
+        (OS_TICKS_PER_SEC / 1000))
+
+int sensor_mgr_register(struct sensor *);
+
+typedef int (*sensor_mgr_compare_func_t)(struct sensor *, void *);
+struct sensor *sensor_mgr_find_next(sensor_mgr_compare_func_t, void *,
+        struct sensor *);
+struct sensor *sensor_mgr_find_next_bytype(sensor_type_t, struct sensor *);
+struct sensor *sensor_mgr_find_next_bydevname(char *, struct sensor *);
+
+/**
+ * }@
+ */
+
 
-done:
-    return (rc);
-}
 
 /**
  * @}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/397f4a2d/hw/sensor/src/sensor.c
----------------------------------------------------------------------
diff --git a/hw/sensor/src/sensor.c b/hw/sensor/src/sensor.c
index 251b171..95fb9ab 100644
--- a/hw/sensor/src/sensor.c
+++ b/hw/sensor/src/sensor.c
@@ -17,19 +17,21 @@
  * under the License.
  */
 
+#include <string.h>
 #include <errno.h>
 #include <assert.h>
 
-#include "sensor/sensor.h"
+#include "os/os.h"
+#include "sysinit/sysinit.h"
 
-struct os_callout sensors_callout;
+#include "sensor/sensor.h"
 
 struct {
     struct os_mutex mgr_lock;
 
-    struct os_callout mgr_poll_callout;
+    struct os_callout mgr_wakeup_callout;
 
-    TAILQ_HEAD(, struct sensor) mgr_sensor_list;
+    TAILQ_HEAD(, sensor) mgr_sensor_list;
 } sensor_mgr;
 
 static int
@@ -37,7 +39,7 @@ sensor_mgr_lock(void)
 {
     int rc;
 
-    rc = os_mutex_pend(&sensor_mgr.mgr_lock);
+    rc = os_mutex_pend(&sensor_mgr.mgr_lock, OS_TIMEOUT_NEVER);
     if (rc == 0 || rc == OS_NOT_STARTED) {
         return (0);
     }
@@ -63,11 +65,11 @@ sensor_mgr_insert(struct sensor *sensor)
 
     cursor = NULL;
     TAILQ_FOREACH(cursor, &sensor_mgr.mgr_sensor_list, s_next) {
-        if (cursor->s_next_wakeup == OS_TIMEOUT_NEVER) {
+        if (cursor->s_next_run == OS_TIMEOUT_NEVER) {
             break;
         }
 
-        if (OS_TIME_TICK_LT(sensor->s_next_wakeup, cursor->s_next_wakeup)) {
+        if (OS_TIME_TICK_LT(sensor->s_next_run, cursor->s_next_run)) {
             break;
         }
     }
@@ -75,13 +77,49 @@ sensor_mgr_insert(struct sensor *sensor)
     if (cursor) {
         TAILQ_INSERT_BEFORE(cursor, sensor, s_next);
     } else {
-        TAILQ_INSERT_TAIL(&sensor_mgr.mgr_sensor_list, s_next);
+        TAILQ_INSERT_TAIL(&sensor_mgr.mgr_sensor_list, sensor, s_next);
+    }
+}
+
+/**
+ * Register the sensor with the global sensor list.   This makes the sensor
+ * searchable by other packages, who may want to look it up by type.
+ *
+ * @param The sensor to register
+ *
+ * @return 0 on success, non-zero error code on failure.
+ */
+int
+sensor_mgr_register(struct sensor *sensor)
+{
+    int rc;
+
+    rc = sensor_mgr_lock();
+    if (rc != 0) {
+        goto err;
     }
+
+    rc = sensor_lock(sensor);
+    if (rc != 0) {
+        goto err;
+    }
+
+    sensor_mgr_insert(sensor);
+
+    sensor_unlock(sensor);
+
+    sensor_mgr_unlock();
+
+    return (0);
+err:
+    return (rc);
 }
 
+
 static os_time_t
 sensor_mgr_poll_one(struct sensor *sensor, os_time_t now)
 {
+    uint32_t sensor_ticks;
     int rc;
 
     rc = sensor_lock(sensor);
@@ -101,7 +139,8 @@ sensor_mgr_poll_one(struct sensor *sensor, os_time_t now)
     /* Set next wakeup, and insertion sort the sensor back into the
      * list.
      */
-    sensor->s_next_run = now + os_time_ms_to_ticks(sensor->s_poll_rate);
+    os_time_ms_to_ticks(sensor->s_poll_rate, &sensor_ticks);
+    sensor->s_next_run = now + sensor_ticks;
 
     /* Re-insert the sensor manager, with the new wakeup time. */
     sensor_mgr_insert(sensor);
@@ -125,17 +164,20 @@ sensor_mgr_wakeup_event(struct os_event *ev)
     struct sensor *cursor;
     os_time_t now;
     os_time_t task_next_wakeup;
+    os_time_t next_wakeup;
     int rc;
 
+    now = os_time_get();
+    task_next_wakeup = now + SENSOR_MGR_WAKEUP_TICKS;
+
     rc = sensor_mgr_lock();
     if (rc != 0) {
-        goto err;
+        /* Schedule again in 1 tick, see if we can reacquire the lock */
+        task_next_wakeup = now + 1;
+        goto done;
     }
 
-    now = os_time_get();
-    task_next_wakeup = now + SENSOR_MGR_WAKEUP_TICKS;
-
-    TAILQ_FOREACH(cursor, &sensor_mgr.mgr_sensor_list, s_next) {
+     TAILQ_FOREACH(cursor, &sensor_mgr.mgr_sensor_list, s_next) {
         /* Sensors that are not periodic are inserted at the end of the sensor
          * list.
          */
@@ -154,7 +196,7 @@ sensor_mgr_wakeup_event(struct os_event *ev)
          * and re-inserts it into the list.  It returns the next wakeup time
          * for this sensor.
          */
-        next_wakeup = sensor_poll_one(cursor, now);
+        next_wakeup = sensor_mgr_poll_one(cursor, now);
 
         /* If the next wakeup time for this sensor is before the task's next
          * scheduled wakeup, move that forward, so we can collect data from that
@@ -165,10 +207,16 @@ sensor_mgr_wakeup_event(struct os_event *ev)
         }
     }
 
+done:
     os_callout_reset(&sensor_mgr.mgr_wakeup_callout, task_next_wakeup);
 }
 
-
+static struct os_eventq *
+sensor_mgr_evq_get(void)
+{
+    /* XXX: FIll me in */
+    return (NULL);
+}
 
 
 static void
@@ -176,6 +224,7 @@ sensor_mgr_init(void)
 {
     memset(&sensor_mgr, 0, sizeof(sensor_mgr));
     TAILQ_INIT(&sensor_mgr.mgr_sensor_list);
+
     /**
      * Initialize sensor polling callout and set it to fire on boot.
      */
@@ -204,7 +253,7 @@ sensor_mgr_find_next(sensor_mgr_compare_func_t compare_func, void *arg,
 
     cursor = prev_cursor;
     if (cursor == NULL) {
-        cursor = TAILQ_FIRST(&g_sensor_list);
+        cursor = TAILQ_FIRST(&sensor_mgr.mgr_sensor_list);
     }
 
     while (cursor != NULL) {
@@ -227,7 +276,7 @@ sensor_mgr_match_bytype(struct sensor *sensor, void *arg)
 
     type = (sensor_type_t *) arg;
 
-    if ((*type & sensor->s_type) != 0) {
+    if ((*type & sensor->s_types) != 0) {
         return (1);
     } else {
         return (0);
@@ -249,7 +298,8 @@ sensor_mgr_match_bytype(struct sensor *sensor, void *arg)
 struct sensor *
 sensor_mgr_find_next_bytype(sensor_type_t type, struct sensor *prev_cursor)
 {
-    return (sensor_mgr_find_next(sensor_mgr_match_bytype, type, prev_cursor));
+    return (sensor_mgr_find_next(sensor_mgr_match_bytype, (void *) &type,
+                prev_cursor));
 }
 
 static int
@@ -268,7 +318,13 @@ sensor_mgr_match_bydevname(struct sensor *sensor, void *arg)
 
 
 /**
+ * Search teh sensor list, and find the next sensor that correspondes
+ * to a given device name.
  *
+ * @param The device name to search for
+ * @param The previous sensor found with this device name
+ *
+ * @return 0 on success, non-zero error code on failure
  */
 struct sensor *
 sensor_mgr_find_next_bydevname(char *devname, struct sensor *prev_cursor)
@@ -277,22 +333,10 @@ sensor_mgr_find_next_bydevname(char *devname, struct sensor *prev_cursor)
             prev_cursor));
 }
 
-
 /**
- * Register the sensor with the global sensor list.   This makes the sensor searchable
- * by other packages, who may want to look it up by type.
- *
- * @param The sensor to register
- *
- * @return 0 on success, non-zero error code on failure.
+ * Initialize the sensor package, called through SYSINIT.  Note, this function
+ * will assert if called directly, and _NOT_ through the sysinit package.
  */
-int
-sensor_mgr_register(struct sensor *sensor)
-{
-    return (0);
-}
-
-
 void
 sensor_pkg_init(void)
 {
@@ -316,11 +360,9 @@ sensor_lock(struct sensor *sensor)
     int rc;
 
     rc = os_mutex_pend(&sensor->s_lock, OS_TIMEOUT_NEVER);
-    if (rc != 0) {
-        goto err;
+    if (rc == 0 || rc == OS_NOT_STARTED) {
+        return (0);
     }
-    return (0);
-err:
     return (rc);
 }
 
@@ -347,6 +389,8 @@ sensor_unlock(struct sensor *sensor)
 int
 sensor_init(struct sensor *sensor, struct os_dev *dev)
 {
+    int rc;
+
     memset(sensor, 0, sizeof(*sensor));
 
     rc = os_mutex_init(&sensor->s_lock);
@@ -446,7 +490,8 @@ sensor_read(struct sensor *sensor, sensor_type_t type,
     src.user_func = data_func;
     src.user_arg = arg;
 
-    rc = sensor->s_funcs.sd_read(sensor, type, data_func, arg, timeout);
+    rc = sensor->s_funcs.sd_read(sensor, type, sensor_read_data_func, &src,
+            timeout);
 
     sensor_unlock(sensor);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/397f4a2d/hw/sensor/syscfg.yml
----------------------------------------------------------------------
diff --git a/hw/sensor/syscfg.yml b/hw/sensor/syscfg.yml
new file mode 100644
index 0000000..b3dc9bb
--- /dev/null
+++ b/hw/sensor/syscfg.yml
@@ -0,0 +1,24 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+# Package: hw/sensor
+
+syscfg.defs:
+    SENSOR_MGR_WAKEUP_RATE:
+        description: 'The default wakeup rate of the sensor manager'
+        value: 1000


[2/6] incubator-mynewt-core git commit: add sensor API, interim commit. Things are working in simulated mode to register and list sensors.

Posted by st...@apache.org.
add sensor API, interim commit.  Things are working in simulated mode to register and list sensors.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/c44ff1f4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/c44ff1f4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/c44ff1f4

Branch: refs/heads/sensors_branch
Commit: c44ff1f4e218a1c41d771a975a7c6ff9b54ec613
Parents: 397f4a2
Author: Sterling Hughes <st...@apache.org>
Authored: Sat Dec 10 16:47:11 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Mon Dec 12 21:42:04 2016 -0800

----------------------------------------------------------------------
 apps/slinky/pkg.yml                            |   1 +
 apps/slinky/src/main.c                         |   9 +
 hw/drivers/sensors/sim/include/sim/sim_accel.h |  50 ++++++
 hw/drivers/sensors/sim/pkg.yml                 |  25 +++
 hw/drivers/sensors/sim/src/generic_accel.c     | 177 ++++++++++++++++++++
 hw/sensor/include/sensor/accel.h               |  48 ++++++
 hw/sensor/include/sensor/sensor.h              |  95 +++++++----
 hw/sensor/pkg.yml                              |   4 -
 hw/sensor/src/sensor.c                         |  52 +++++-
 hw/sensor/src/sensor_priv.h                    |  29 ++++
 hw/sensor/src/sensor_shell.c                   | 114 +++++++++++++
 hw/sensor/syscfg.yml                           |   4 +
 12 files changed, 567 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c44ff1f4/apps/slinky/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/slinky/pkg.yml b/apps/slinky/pkg.yml
index cd63f0a..ab87296 100644
--- a/apps/slinky/pkg.yml
+++ b/apps/slinky/pkg.yml
@@ -27,6 +27,7 @@ pkg.keywords:
 pkg.deps:
     - sys/console/full
     - hw/sensor 
+    - hw/drivers/sensors/sim 
     - test/flash_test
     - mgmt/imgmgr
     - mgmt/newtmgr

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c44ff1f4/apps/slinky/src/main.c
----------------------------------------------------------------------
diff --git a/apps/slinky/src/main.c b/apps/slinky/src/main.c
index 342a901..298696b 100755
--- a/apps/slinky/src/main.c
+++ b/apps/slinky/src/main.c
@@ -29,6 +29,8 @@
 #include <log/log.h>
 #include <stats/stats.h>
 #include <config/config.h>
+#include <sensor/sensor.h>
+#include <sim/sim_accel.h>
 #include "flash_map/flash_map.h"
 #include <hal/hal_system.h>
 #if MYNEWT_VAL(SPLIT_LOADER)
@@ -109,6 +111,8 @@ static struct cbmem cbmem;
 
 static struct os_eventq slinky_evq;
 
+struct sim_accel sim_accel_sensor;
+
 static char *
 test_conf_get(int argc, char **argv, char *buf, int max_len)
 {
@@ -316,6 +320,11 @@ main(int argc, char **argv)
 
     init_tasks();
 
+    sensor_pkg_init();
+
+    os_dev_create((struct os_dev *) &sim_accel_sensor, "simaccel0",
+            OS_DEV_INIT_KERNEL, OS_DEV_INIT_PRIMARY, sim_accel_init, NULL);
+
     os_start();
 
     /* os start should never return. If it does, this should be an error */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c44ff1f4/hw/drivers/sensors/sim/include/sim/sim_accel.h
----------------------------------------------------------------------
diff --git a/hw/drivers/sensors/sim/include/sim/sim_accel.h b/hw/drivers/sensors/sim/include/sim/sim_accel.h
new file mode 100644
index 0000000..6f0f6a1
--- /dev/null
+++ b/hw/drivers/sensors/sim/include/sim/sim_accel.h
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef __SIM_ACCEL_H__
+#define __SIM_ACCEL_H__
+
+#include "os/os.h"
+#include "os/os_dev.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct sim_accel_cfg {
+    uint8_t sac_nr_samples;
+    uint8_t sac_nr_axises;
+    uint16_t sac_sample_itvl;
+};
+
+struct sim_accel {
+    struct os_dev sa_dev;
+    struct sensor sa_sensor;
+    struct sim_accel_cfg sa_cfg;
+    os_time_t sa_last_read_time;
+};
+
+int sim_accel_init(struct os_dev *, void *);
+int sim_accel_config(struct sim_accel *, struct sim_accel_cfg *);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __SENSOR_SIM_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c44ff1f4/hw/drivers/sensors/sim/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/drivers/sensors/sim/pkg.yml b/hw/drivers/sensors/sim/pkg.yml
new file mode 100644
index 0000000..f0c3a6e
--- /dev/null
+++ b/hw/drivers/sensors/sim/pkg.yml
@@ -0,0 +1,25 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+pkg.name: hw/drivers/sensors/sim
+pkg.description: Sensor simulated interfaces
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c44ff1f4/hw/drivers/sensors/sim/src/generic_accel.c
----------------------------------------------------------------------
diff --git a/hw/drivers/sensors/sim/src/generic_accel.c b/hw/drivers/sensors/sim/src/generic_accel.c
new file mode 100644
index 0000000..164c1e2
--- /dev/null
+++ b/hw/drivers/sensors/sim/src/generic_accel.c
@@ -0,0 +1,177 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * resarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <string.h>
+#include <errno.h>
+#include <assert.h>
+
+#include "defs/error.h"
+
+#include "os/os.h"
+#include "sysinit/sysinit.h"
+
+#include "sensor/sensor.h"
+#include "sensor/accel.h"
+
+#include "sim/sim_accel.h"
+
+/* Exports for the sensor interface.
+ */
+static void *sim_accel_sensor_get_interface(struct sensor *, sensor_type_t);
+static int sim_accel_sensor_read(struct sensor *, sensor_type_t,
+        sensor_data_func_t, void *, uint32_t);
+static int sim_accel_sensor_get_config(struct sensor *, sensor_type_t,
+        struct sensor_cfg *);
+
+static const struct sensor_driver g_sim_accel_sensor_driver = {
+    sim_accel_sensor_get_interface,
+    sim_accel_sensor_read,
+    sim_accel_sensor_get_config
+};
+
+/**
+ * Expects to be called back through os_dev_create().
+ *
+ * @param The device object associated with this accellerometer
+ * @param Argument passed to OS device init, unused
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int
+sim_accel_init(struct os_dev *dev, void *arg)
+{
+    struct sim_accel *sa;
+    struct sensor *sensor;
+    int rc;
+
+    sa = (struct sim_accel *) dev;
+
+    sensor = &sa->sa_sensor;
+
+    rc = sensor_init(sensor, dev);
+    if (rc != 0) {
+        goto err;
+    }
+
+    rc = sensor_set_driver(sensor, SENSOR_TYPE_ACCELEROMETER,
+            (struct sensor_driver *) &g_sim_accel_sensor_driver);
+    if (rc != 0) {
+        goto err;
+    }
+
+    rc = sensor_mgr_register(sensor);
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+int
+sim_accel_config(struct sim_accel *sa, struct sim_accel_cfg *cfg)
+{
+    /* Overwrite the configuration associated with this generic accelleromter. */
+    memcpy(&sa->sa_cfg, cfg, sizeof(*cfg));
+
+    return (0);
+}
+
+static void *
+sim_accel_sensor_get_interface(struct sensor *sensor, sensor_type_t type)
+{
+    return (NULL);
+}
+
+
+static int
+sim_accel_sensor_read(struct sensor *sensor, sensor_type_t type,
+        sensor_data_func_t data_func, void *data_arg, uint32_t timeout)
+{
+    struct sim_accel *sa;
+    struct sensor_accel_data sad;
+    os_time_t now;
+    uint32_t num_samples;
+    int i;
+    int rc;
+
+    if (type != SENSOR_TYPE_ACCELEROMETER) {
+        rc = SYS_EINVAL;
+        goto err;
+    }
+
+    sa = (struct sim_accel *) SENSOR_GET_DEVICE(sensor);
+
+    /* When a sensor is "read", we get the last 'n' samples from the device
+     * and pass them to the sensor data function.  Based on the sample
+     * interval provided to sim_accel_config() and the last time this function
+     * was called, 'n' samples are generated.
+     */
+    now = os_time_get();
+
+    num_samples = (now - sa->sa_last_read_time) / sa->sa_cfg.sac_sample_itvl;
+    num_samples = min(num_samples, sa->sa_cfg.sac_nr_samples);
+
+    /* By default only readings are provided for 1-axis (x), however,
+     * if number of axises is configured, up to 3-axises of data can be
+     * returned.
+     */
+    sad.sad_x = 0;
+    sad.sad_y = SENSOR_ACCEL_DATA_UNUSED;
+    sad.sad_z = SENSOR_ACCEL_DATA_UNUSED;
+
+    if (sa->sa_cfg.sac_nr_axises > 1) {
+        sad.sad_y = 0;
+    }
+    if (sa->sa_cfg.sac_nr_axises > 2) {
+        sad.sad_z = 0;
+    }
+
+    /* Call data function for each of the generated readings. */
+    for (i = 0; i < num_samples; i++) {
+        rc = data_func(sensor, data_arg, &sad);
+        if (rc != 0) {
+            goto err;
+        }
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+static int
+sim_accel_sensor_get_config(struct sensor *sensor, sensor_type_t type,
+        struct sensor_cfg *cfg)
+{
+    int rc;
+
+    if (type != SENSOR_TYPE_ACCELEROMETER) {
+        rc = SYS_EINVAL;
+        goto err;
+    }
+
+    cfg->sc_valtype = SENSOR_VALUE_TYPE_MS2_TRIPLET;
+
+    return (0);
+err:
+    return (rc);
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c44ff1f4/hw/sensor/include/sensor/accel.h
----------------------------------------------------------------------
diff --git a/hw/sensor/include/sensor/accel.h b/hw/sensor/include/sensor/accel.h
new file mode 100644
index 0000000..e56871b
--- /dev/null
+++ b/hw/sensor/include/sensor/accel.h
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef __SENSOR_ACCEL_H__
+#define __SENSOR_ACCEL_H__
+
+#include "os/os.h"
+#include "os/os_dev.h"
+#include "sensor/sensor.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Data representing a singular read from an accelerometer.
+ */
+struct sensor_accel_data {
+    int32_t sad_x;
+    int32_t sad_y;
+    int32_t sad_z;
+} __attribute__((packed));
+
+/**
+ * Accelerometer data is unused for this field.
+ */
+#define SENSOR_ACCEL_DATA_UNUSED (0xFFFFFFFF)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __SENSOR_ACCEL_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c44ff1f4/hw/sensor/include/sensor/sensor.h
----------------------------------------------------------------------
diff --git a/hw/sensor/include/sensor/sensor.h b/hw/sensor/include/sensor/sensor.h
index a825b51..64ab640 100644
--- a/hw/sensor/include/sensor/sensor.h
+++ b/hw/sensor/include/sensor/sensor.h
@@ -27,6 +27,10 @@
 extern "C" {
 #endif
 
+/* Package init function.  Remove when we have post-kernel init stages.
+ */
+void sensor_pkg_init(void);
+
 
 /* Forward declare sensor structure defined below. */
 struct sensor;
@@ -36,36 +40,32 @@ struct sensor;
  */
 
 typedef enum {
-    /* No sensor type, used for queries */
+ /* No sensor type, used for queries */
     SENSOR_TYPE_NONE                 = 0,
     /* Accelerometer functionality supported */
-    SENSOR_TYPE_ACCELEROMETER       = (1 << 0),
-    /* Ambient temperature supported */
-    SENSOR_TYPE_AMBIENT_TEMPERATURE  = (1 << 1),
-    /* Gravity supported */
-    SENSOR_TYPE_GRAVITY              = (1 << 2),
+    SENSOR_TYPE_ACCELEROMETER        = (1 << 0),
+    /* Magnetic field supported */
+    SENSOR_TYPE_MAGNETIC_FIELD       = (1 << 1),
     /* Gyroscope supported */
-    SENSOR_TYPE_GYROSCOPE            = (1 << 3),
+    SENSOR_TYPE_GYROSCOPE            = (1 << 2),
     /* Light supported */
-    SENSOR_TYPE_LIGHT                = (1 << 4),
-    /* Linear acceleration supported */
-    SENSOR_TYPE_LINEAR_ACCELERATION = (1 << 5),
-    /* Magnetic field supported */
-    SENSOR_TYPE_MAGNETIC_FIELD       = (1 << 6),
-    /* Orientation sensor supported */
-    SENSOR_TYPE_ORIENTATION          = (1 << 7),
+    SENSOR_TYPE_LIGHT                = (1 << 3),
+    /* Temperature supported */
+    SENSOR_TYPE_TEMPERATURE          = (1 << 4),
+    /* Ambient temperature supported */
+    SENSOR_TYPE_AMBIENT_TEMPERATURE  = (1 << 5),
     /* Pressure sensor supported */
-    SENSOR_TYPE_PRESSURE             = (1 << 8),
+    SENSOR_TYPE_PRESSURE             = (1 << 6),
     /* Proximity sensor supported */
-    SENSOR_TYPE_PROXIMITY            = (1 << 9),
+    SENSOR_TYPE_PROXIMITY            = (1 << 7),
     /* Relative humidity supported */
-    SENSOR_TYPE_RELATIVE_HUMIDITY    = (1 << 10),
-    /* Rotation Vector supported */
-    SENSOR_TYPE_ROTATION_VECTOR      = (1 << 11),
-    /* Temperature supported */
-    SENSOR_TYPE_TEMPERATURE          = (1 << 12),
+    SENSOR_TYPE_RELATIVE_HUMIDITY    = (1 << 8),
+    /* Rotation vector (quaternion) supported */
+    SENSOR_TYPE_ROTATION_VECTOR      = (1 << 9),
     /* Altitude Supported */
-    SENSOR_TYPE_ALTITUDE             = (1 << 13),
+    SENSOR_TYPE_ALTITUDE             = (1 << 10),
+    /* Weight Supported */
+    SENSOR_TYPE_WEIGHT               = (1 << 11),
     /* User defined sensor type 1 */
     SENSOR_TYPE_USER_DEFINED_1       = (1 << 26),
     /* User defined sensor type 2 */
@@ -96,6 +96,18 @@ typedef enum {
  * 32-bit floating point
  */
 #define SENSOR_VALUE_TYPE_FLOAT  (2)
+/**
+ * Meters per second squared.
+ *
+ * 32-bit signed integer, with 0xFFFFFFFF reserved for unused.
+ */
+#define SENSOR_VALUE_TYPE_MS2    (3)
+/**
+ * Triplet of meters per second squared.
+ */
+#define SENSOR_VALUE_TYPE_MS2_TRIPLET (4)
+
+
 
 /**
  * Configuration structure, describing a specific sensor type off of
@@ -118,7 +130,7 @@ struct sensor_cfg {
  * @param The argument provided to sensor_read() function.
  * @param A single sensor reading for that sensor listener
  *
- * @return 0 on succes, non-zero error code on failure.
+ * @return 0 on success, non-zero error code on failure.
  */
 typedef int (*sensor_data_func_t)(struct sensor *, void *, void *);
 
@@ -144,8 +156,6 @@ struct sensor_listener {
     SLIST_ENTRY(sensor_listener) sl_next;
 };
 
-struct sensor;
-
 /**
  * Get a more specific interface on this sensor object (e.g. Gyro, Magnometer),
  * which has additional functions for managing the sensor.
@@ -187,12 +197,17 @@ typedef int (*sensor_read_func_t)(struct sensor *, sensor_type_t,
 typedef int (*sensor_get_config_func_t)(struct sensor *, sensor_type_t,
         struct sensor_cfg *);
 
-struct sensor_driver_funcs {
+struct sensor_driver {
     sensor_get_interface_func_t sd_get_interface;
     sensor_read_func_t sd_read;
     sensor_get_config_func_t sd_get_config;
 };
 
+/*
+ * Return the OS device structure corresponding to this sensor
+ */
+#define SENSOR_GET_DEVICE(__s) ((__s)->s_dev)
+
 struct sensor {
     /* The OS device this sensor inherits from, this is typically a sensor specific
      * driver.
@@ -220,7 +235,7 @@ struct sensor {
 
     /* Sensor driver specific functions, created by the device registering the sensor.
      */
-    const struct sensor_driver_funcs s_funcs;
+    struct sensor_driver *s_funcs;
     /* A list of listeners that are registered to receive data off of this sensor
      */
     SLIST_HEAD(, sensor_listener) s_listener_list;
@@ -235,6 +250,25 @@ int sensor_register_listener(struct sensor *, struct sensor_listener *);
 int sensor_read(struct sensor *, sensor_type_t, sensor_data_func_t, void *,
         uint32_t);
 
+/**
+ * Set the driver functions for this sensor, along with the type of sensor
+ * data available for the given sensor.
+ *
+ * @param The sensor to set the driver information for
+ * @param The types of sensor data available for this sensor
+ * @param The driver functions for this sensor
+ *
+ * @return 0 on success, non-zero error code on failure
+ */
+static inline int
+sensor_set_driver(struct sensor *sensor, sensor_type_t type,
+        struct sensor_driver *driver)
+{
+    sensor->s_funcs = driver;
+    sensor->s_types = type;
+
+    return (0);
+}
 
 /**
  * Read the configuration for the sensor type "type," and return the
@@ -250,7 +284,7 @@ static inline int
 sensor_get_config(struct sensor *sensor, sensor_type_t type,
         struct sensor_cfg *cfg)
 {
-    return (sensor->s_funcs.sd_get_config(sensor, type, cfg));
+    return (sensor->s_funcs->sd_get_config(sensor, type, cfg));
 }
 
 /**
@@ -265,7 +299,7 @@ sensor_get_config(struct sensor *sensor, sensor_type_t type,
 static inline void *
 sensor_get_interface(struct sensor *sensor, sensor_type_t type)
 {
-    return (sensor->s_funcs.sd_get_interface(sensor, type));
+    return (sensor->s_funcs->sd_get_interface(sensor, type));
 }
 
 /**
@@ -280,8 +314,11 @@ sensor_get_interface(struct sensor *sensor, sensor_type_t type)
 #define SENSOR_MGR_WAKEUP_TICKS (MYNEWT_VAL(SENSOR_MGR_WAKEUP_RATE) * \
         (OS_TICKS_PER_SEC / 1000))
 
+int sensor_mgr_lock(void);
+void sensor_mgr_unlock(void);
 int sensor_mgr_register(struct sensor *);
 
+
 typedef int (*sensor_mgr_compare_func_t)(struct sensor *, void *);
 struct sensor *sensor_mgr_find_next(sensor_mgr_compare_func_t, void *,
         struct sensor *);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c44ff1f4/hw/sensor/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/sensor/pkg.yml b/hw/sensor/pkg.yml
index 2274252..decf1c9 100644
--- a/hw/sensor/pkg.yml
+++ b/hw/sensor/pkg.yml
@@ -22,8 +22,4 @@ pkg.description: Sensor Interface
 pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
 pkg.homepage: "http://mynewt.apache.org/"
 pkg.keywords:
-pkg.req_apis:
-    - SENSOR_HW_IMPL
 
-pkg.init_function: sensor_pkg_init
-pkg.init_stage: 0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c44ff1f4/hw/sensor/src/sensor.c
----------------------------------------------------------------------
diff --git a/hw/sensor/src/sensor.c b/hw/sensor/src/sensor.c
index 95fb9ab..319de12 100644
--- a/hw/sensor/src/sensor.c
+++ b/hw/sensor/src/sensor.c
@@ -26,15 +26,18 @@
 
 #include "sensor/sensor.h"
 
+#include "sensor_priv.h"
+
 struct {
     struct os_mutex mgr_lock;
 
     struct os_callout mgr_wakeup_callout;
+    struct os_eventq *mgr_eventq;
 
     TAILQ_HEAD(, sensor) mgr_sensor_list;
 } sensor_mgr;
 
-static int
+int
 sensor_mgr_lock(void)
 {
     int rc;
@@ -46,7 +49,7 @@ sensor_mgr_lock(void)
     return (rc);
 }
 
-static void
+void
 sensor_mgr_unlock(void)
 {
     (void) os_mutex_release(&sensor_mgr.mgr_lock);
@@ -214,10 +217,10 @@ done:
 static struct os_eventq *
 sensor_mgr_evq_get(void)
 {
-    /* XXX: FIll me in */
-    return (NULL);
-}
+    os_eventq_ensure(&sensor_mgr.mgr_eventq, NULL);
 
+    return (sensor_mgr.mgr_eventq);
+}
 
 static void
 sensor_mgr_init(void)
@@ -235,7 +238,26 @@ sensor_mgr_init(void)
     os_mutex_init(&sensor_mgr.mgr_lock);
 }
 
-
+/**
+ * The sensor manager contains a list of sensors, this function returns
+ * the next sensor in that list, for which compare_func() returns successful
+ * (one).  If prev_cursor is provided, the function starts at that point
+ * in the sensor list.
+ *
+ * @warn This function MUST be locked by sensor_mgr_lock/unlock() if the goal is
+ * to iterate through sensors (as opposed to just finding one.)  As the
+ * "prev_cursor" may be resorted in the sensor list, in between calls.
+ *
+ * @param The comparison function to use against sensors in the list.
+ * @param The argument to provide to that comparison function
+ * @param The previous sensor in the sensor manager list, in case of
+ *        iteration.  If desire is to find first matching sensor, provide a
+ *        NULL value.
+ *
+ * @return A pointer to the first sensor found from prev_cursor, or
+ *         NULL, if none found.
+ *
+ */
 struct sensor *
 sensor_mgr_find_next(sensor_mgr_compare_func_t compare_func, void *arg,
         struct sensor *prev_cursor)
@@ -254,12 +276,15 @@ sensor_mgr_find_next(sensor_mgr_compare_func_t compare_func, void *arg,
     cursor = prev_cursor;
     if (cursor == NULL) {
         cursor = TAILQ_FIRST(&sensor_mgr.mgr_sensor_list);
+    } else {
+        cursor = TAILQ_NEXT(prev_cursor, s_next);
     }
 
     while (cursor != NULL) {
         if (compare_func(cursor, arg)) {
             break;
         }
+        cursor = TAILQ_NEXT(cursor, s_next);
     }
 
     sensor_mgr_unlock();
@@ -269,6 +294,7 @@ done:
 }
 
 
+
 static int
 sensor_mgr_match_bytype(struct sensor *sensor, void *arg)
 {
@@ -276,6 +302,10 @@ sensor_mgr_match_bytype(struct sensor *sensor, void *arg)
 
     type = (sensor_type_t *) arg;
 
+    /* s_types is a bitmask that contains the supported sensor types for this
+     * sensor, and type is the bitmask we're searching for.  Compare the two,
+     * and if there is a match, return true (1).
+     */
     if ((*type & sensor->s_types) != 0) {
         return (1);
     } else {
@@ -340,10 +370,17 @@ sensor_mgr_find_next_bydevname(char *devname, struct sensor *prev_cursor)
 void
 sensor_pkg_init(void)
 {
+    /* Call directly until sysinit has the right hooks for us */
+#if 0
     /* Ensure this is only called by sysinit */
     SYSINIT_ASSERT_ACTIVE();
+#endif
 
     sensor_mgr_init();
+
+#if MYNEWT_VAL(SENSOR_CLI)
+    sensor_shell_register();
+#endif
 }
 
 
@@ -405,7 +442,6 @@ err:
 }
 
 
-
 /**
  * Register a sensor listener.  This allows a calling application to receive
  * callbacks for data from a given sensor object.
@@ -490,7 +526,7 @@ sensor_read(struct sensor *sensor, sensor_type_t type,
     src.user_func = data_func;
     src.user_arg = arg;
 
-    rc = sensor->s_funcs.sd_read(sensor, type, sensor_read_data_func, &src,
+    rc = sensor->s_funcs->sd_read(sensor, type, sensor_read_data_func, &src,
             timeout);
 
     sensor_unlock(sensor);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c44ff1f4/hw/sensor/src/sensor_priv.h
----------------------------------------------------------------------
diff --git a/hw/sensor/src/sensor_priv.h b/hw/sensor/src/sensor_priv.h
new file mode 100644
index 0000000..a15aa22
--- /dev/null
+++ b/hw/sensor/src/sensor_priv.h
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef __SENSOR_PRIV_H__
+#define __SENSOR_PRIV_H__
+
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(SENSOR_CLI)
+int sensor_shell_register(void);
+#endif
+
+#endif /* __SENSOR_PRIV_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c44ff1f4/hw/sensor/src/sensor_shell.c
----------------------------------------------------------------------
diff --git a/hw/sensor/src/sensor_shell.c b/hw/sensor/src/sensor_shell.c
new file mode 100644
index 0000000..6c8b7ac
--- /dev/null
+++ b/hw/sensor/src/sensor_shell.c
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(SENSOR_CLI)
+
+#include <string.h>
+#include <errno.h>
+#include <assert.h>
+
+#include "defs/error.h"
+
+#include "os/os.h"
+
+#include "sensor/sensor.h"
+#include "console/console.h"
+#include "shell/shell.h"
+
+static int sensor_cmd_exec(int, char **);
+static const struct shell_cmd shell_sensor_cmd = {
+    .sc_cmd = "sensor",
+    .sc_cmd_func = sensor_cmd_exec
+};
+
+static void
+sensor_display_help(void)
+{
+    console_printf("Possible commands for sensor are:\n");
+    console_printf("  list\n");
+}
+
+static void
+sensor_cmd_display_sensor(struct sensor *sensor)
+{
+    console_printf("sensor dev = %s, type = %lld\n", sensor->s_dev->od_name,
+            sensor->s_types);
+}
+
+static void
+sensor_cmd_list_sensors(void)
+{
+    struct sensor *sensor;
+
+    sensor = NULL;
+
+    sensor_mgr_lock();
+
+    while (1) {
+        sensor = sensor_mgr_find_next_bytype(SENSOR_TYPE_ALL, sensor);
+        if (sensor == NULL) {
+            break;
+        }
+
+        sensor_cmd_display_sensor(sensor);
+    }
+
+    sensor_mgr_unlock();
+}
+
+
+static int
+sensor_cmd_exec(int argc, char **argv)
+{
+    char *subcmd;
+    int rc;
+
+    if (argc <= 1) {
+        sensor_display_help();
+        rc = 0;
+        goto done;
+    }
+
+    subcmd = argv[1];
+    if (!strcmp(subcmd, "list")) {
+        sensor_cmd_list_sensors();
+    } else {
+        console_printf("Unknown sensor command %s\n", subcmd);
+        rc = SYS_EINVAL;
+        goto err;
+    }
+
+done:
+    return (0);
+err:
+    return (rc);
+}
+
+
+int
+sensor_shell_register(void)
+{
+    shell_cmd_register((struct shell_cmd *) &shell_sensor_cmd);
+
+    return (0);
+}
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c44ff1f4/hw/sensor/syscfg.yml
----------------------------------------------------------------------
diff --git a/hw/sensor/syscfg.yml b/hw/sensor/syscfg.yml
index b3dc9bb..0826cc9 100644
--- a/hw/sensor/syscfg.yml
+++ b/hw/sensor/syscfg.yml
@@ -22,3 +22,7 @@ syscfg.defs:
     SENSOR_MGR_WAKEUP_RATE:
         description: 'The default wakeup rate of the sensor manager'
         value: 1000
+
+    SENSOR_CLI:
+        description: 'Whether or not to enable the sensor shell support'
+        value: 1


[3/6] incubator-mynewt-core git commit: first commit of sensor work, not even compiled yet.

Posted by st...@apache.org.
first commit of sensor work, not even compiled yet.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/f449f3d7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/f449f3d7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/f449f3d7

Branch: refs/heads/sensors_branch
Commit: f449f3d7af9c9710291bc22202408fff46e5b2d9
Parents: 6cf018b
Author: Sterling Hughes <st...@apache.org>
Authored: Mon Dec 5 14:04:46 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Mon Dec 12 21:42:04 2016 -0800

----------------------------------------------------------------------
 hw/sensor/include/sensor/sensor.h | 284 +++++++++++++++++++++++++++++++++
 hw/sensor/pkg.yml                 |  29 ++++
 hw/sensor/src/sensor.c            | 240 ++++++++++++++++++++++++++++
 3 files changed, 553 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f449f3d7/hw/sensor/include/sensor/sensor.h
----------------------------------------------------------------------
diff --git a/hw/sensor/include/sensor/sensor.h b/hw/sensor/include/sensor/sensor.h
new file mode 100644
index 0000000..d2e0af1
--- /dev/null
+++ b/hw/sensor/include/sensor/sensor.h
@@ -0,0 +1,284 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef __SENSOR_H__
+#define __SENSOR_H__
+
+#include <os/os_dev.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct sensor_dev;
+
+typedef enum {
+    /* No sensor type, used for queries */
+    SENSOR_TYPE_NONE                 = 0,
+    /* Accellerometer functionality supported */
+    SENSOR_TYPE_ACCELLEROMETER       = (1 << 0),
+    /* Ambient temperature supported */
+    SENSOR_TYPE_AMBIENT_TEMPERATURE  = (1 << 1),
+    /* Gravity supported */
+    SENSOR_TYPE_GRAVITY              = (1 << 2),
+    /* Gyroscope supported */
+    SENSOR_TYPE_GYROSCOPE            = (1 << 3),
+    /* Light supported */
+    SENSOR_TYPE_LIGHT                = (1 << 4),
+    /* Linear accelleration supported */
+    SENSOR_TYPE_LINEAR_ACCELLERATION = (1 << 5),
+    /* Magnetic field supported */
+    SENSOR_TYPE_MAGNETIC_FIELD       = (1 << 6),
+    /* Orientation sensor supported */
+    SENSOR_TYPE_ORIENTATION          = (1 << 7),
+    /* Pressure sensor supported */
+    SENSOR_TYPE_PRESSURE             = (1 << 8),
+    /* Proximity sensor supported */
+    SENSOR_TYPE_PROXIMITY            = (1 << 9),
+    /* Relative humidity supported */
+    SENSOR_TYPE_RELATIVE_HUMIDITY    = (1 << 10),
+    /* Rotation Vector supported */
+    SENSOR_TYPE_ROTATION_VECTOR      = (1 << 11),
+    /* Temperature supported */
+    SENSOR_TYPE_TEMPERATURE          = (1 << 12),
+    /* Altitude Supported */
+    SENSOR_TYPE_ALTITUDE             = (1 << 13),
+    /* User defined sensor type 1 */
+    SENSOR_TYPE_USER_DEFINED_1       = (1 << 26),
+    /* User defined sensor type 2 */
+    SENSOR_TYPE_USER_DEFINED_2       = (1 << 27),
+    /* User defined sensor type 3 */
+    SENSOR_TYPE_USER_DEFINED_3       = (1 << 28),
+    /* User defined sensor type 4 */
+    SENSOR_TYPE_USER_DEFINED_4       = (1 << 29),
+    /* User defined sensor type 5 */
+    SENSOR_TYPE_USER_DEFINED_5       = (1 << 30),
+    /* User defined sensor type 6 */
+    SENSOR_TYPE_USER_DEFINED_6       = (1 << 31),
+    /* A selector, describes all sensors */
+    SENSOR_TYPE_ALL                  = 0xFFFFFFFF
+} sensor_type_t;
+
+
+#define SENSOR_LISTENER_TYPE_NOTIFY (0)
+#define SENSOR_LISTENER_TYPE_POLL   (1)
+
+struct sensor_reading {
+    /* The timestamp, in CPU time ticks that the sensor value
+     * was read.
+     */
+    uint32_t sv_ts;
+    /* The value of the sensor, based on the type being read.
+     */
+    void *sv_value;
+};
+
+#define SENSOR_VALUE_TYPE_OPAQUE (0)
+#define SENSOR_VALUE_TYPE_INT32  (1)
+#define SENSOR_VALUE_TYPE_FLOAT  (2)
+
+struct sensor_cfg {
+    uint8_t sc_valtype;
+    uint8_t _reserved[3];
+};
+
+/**
+ * Callback for handling sensor data, specified in a sensor listener.
+ *
+ * @param The sensor for which data is being returned
+ * @param The sensor listener that's configured to handle this data.
+ * @param A single sensor reading
+ *
+ * @return 0 on succes, non-zero error code on failure.
+ */
+typedef int (*sensor_data_func_t)(struct sensor *,
+        struct sensor_listener *,
+        struct sensor_reading *v);
+
+/**
+ *
+ */
+struct sensor_listener {
+    /* The type of sensor listener, either notification based "tell me
+     * when there is a value," or polled - collect this value for me
+     * every 'n' seconds and notify me.
+     */
+    uint8_t sl_type;
+    /* Padding */
+    uint8_t _pad1;
+    /* Poll rate in miliseconds. */
+    uint16_t sl_poll_rate_ms;
+    /* The type of sensor data to listen for */
+    sensor_type_t sl_sensor_type;
+    /* Sensor data handler function, called when has data */
+    sensor_data_func_t sl_func;
+
+    SLIST_ENTRY(sensor_listener) sl_next;
+};
+
+struct sensor;
+
+/**
+ * Get a more specific interface on this sensor object (e.g. Gyro, Magnometer),
+ * which has additional functions for managing the sensor.
+ *
+ * @param The sensor to get the interface on
+ * @param The type of sensor interface to get.
+ *
+ * @return void * A pointer to the specific interface requested, or NULL on failure.
+ */
+typedef void *(*sensor_get_interface_func_t)(struct sensor *, sensor_type_t);
+
+/**
+ * Read a value from a sensor, given a specific sensor type (e.g. SENSOR_TYPE_PROXIMITY).
+ *
+ * @param The sensor to read from
+ * @param The type of sensor value to read
+ * @param A pointer to the sensor value to place the returned result into.
+ *
+ * @return 0 on success, non-zero error code on failure.
+ */
+typedef int (*sensor_read_func_t)(struct sensor *, sensor_type_t, void *);
+
+/**
+ * Get the configuration of the sensor for the sensor type.  This includes
+ * the value type of the sensor.
+ *
+ * @param The type of sensor value to get configuration for
+ * @param A pointer to the sensor value to place the returned result into.
+ *
+ * @return 0 on success, non-zero error code on failure.
+ */
+typedef int (*sensor_get_config_func_t)(struct sensor *, sensor_type_t,
+        struct sensor_cfg *);
+
+struct sensor_driver_funcs {
+    sensor_get_interface_func_t sd_get_interface;
+    sensor_read_func_t sd_read;
+    sensor_get_config_func_t sd_get_config;
+};
+
+/* XXX: Make me a syscfg */
+#define SENSOR_DEFAULT_POLL_RATE (10 * (OS_TICKS_PER_SEC/1000))
+
+struct sensor {
+    /* The OS device this sensor inherits from, this is typically a sensor specific
+     * driver.
+     */
+    struct os_dev *s_dev;
+    /* A bit mask describing the types of sensor objects available from this sensor.
+     * If the bit corresponding to the sensor_type_t is set, then this sensor supports
+     * that variable.
+     */
+    sensor_type_t s_types;
+    /* Sensor driver specific functions, created by the device registering the sensor.
+     */
+    const struct sensor_driver_funcs s_funcs;
+    /* A list of listeners that are registered to receive data off of this sensor
+     */
+    SLIST_HEAD(, struct sensor_listener) s_listener_list;
+    /* The next sensor in the global sensor list. */
+    SLIST_ENTRY(sensor) s_next;
+};
+
+int sensor_init(struct sensor *);
+int sensor_register(struct sensor *);
+int sensor_lock(struct sensor *);
+void sensor_unlock(struct sensor *);
+int sensor_register_listener(struct sensor *, struct sensor_listener *);
+struct sensor *sensor_find_next(sensor_type_t, struct sensor *);
+
+
+/**
+ * Read the configuration for the sensor type "type," and return the
+ * configuration into "cfg."
+ *
+ * @param sensor The sensor to read configuration for
+ * @param type The type of sensor configuration to read
+ * @param cfg The configuration structure to point to.
+ *
+ * @return 0 on success, non-zero error code on failure.
+ */
+static inline int
+sensor_get_config(struct sensor *sensor, sensor_type_t type, struct sensor_config *cfg)
+{
+    return (sensor->s_funcs.sd_get_config(sensor, type, cfg));
+}
+
+/**
+ * Read the data for sensor type "type," from the sensor, "sensor" and
+ * return the result into the "value" parameter.
+ *
+ * @param The senssor to read data from
+ * @param The type of sensor data to read from the sensor
+ * @param The sensor value to place data into.
+ *
+ * @return 0 on success, non-zero on failure.
+ */
+static inline int
+sensor_read(struct sensor *sensor, sensor_type_t type, struct sensor_value *value)
+{
+    int rc;
+
+    rc = sensor_lock(sensor);
+    if (rc != 0) {
+        goto done;
+    }
+
+    rc = sensor->s_funcs.sd_read(sensor, type, value);
+
+    sensor_unlock(sensor);
+
+done:
+    return (rc);
+}
+
+/**
+ * Get a more specific interface for this sensor, like accellerometer, or gyro.
+ *
+ * @param The sensor to get the interface from
+ * @param The type of interface to get from this sensor
+ *
+ * @return A pointer to the more specific sensor interface on success, or NULL if
+ *         not found.
+ */
+static inline void *
+sensor_get_interface(struct sensor *sensor, sensor_type_t type)
+{
+    int rc;
+
+    rc = sensor_lock(sensor);
+    if (rc != 0) {
+        goto done;
+    }
+
+    rc = sensor->s_funcs.sd_get_interface(sensor, type);
+
+    sensor_unlock(sensor);
+
+done:
+    return (rc);
+}
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __SENSOR_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f449f3d7/hw/sensor/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/sensor/pkg.yml b/hw/sensor/pkg.yml
new file mode 100644
index 0000000..2274252
--- /dev/null
+++ b/hw/sensor/pkg.yml
@@ -0,0 +1,29 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+pkg.name: hw/sensor
+pkg.description: Sensor Interface
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+pkg.req_apis:
+    - SENSOR_HW_IMPL
+
+pkg.init_function: sensor_pkg_init
+pkg.init_stage: 0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f449f3d7/hw/sensor/src/sensor.c
----------------------------------------------------------------------
diff --git a/hw/sensor/src/sensor.c b/hw/sensor/src/sensor.c
new file mode 100644
index 0000000..3c219b2
--- /dev/null
+++ b/hw/sensor/src/sensor.c
@@ -0,0 +1,240 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <errno.h>
+#include <assert.h>
+
+#include "sensor/sensor.h"
+
+struct os_callout sensors_callout;
+
+TAILQ_HEAD(, struct sensor) g_sensor_list =
+    TAILQ_HEAD_INITIALIZER(&g_sensor_list);
+
+/**
+ * Find the "next" sensor available for a given sensor type.
+ *
+ * If the sensor parameter, is present find the next entry from that
+ * parameter.  Otherwise, find the first matching sensor.
+ *
+ * @param The type of sensor to search for
+ * @param The cursor to search from, or NULL to start from the beginning.
+ *
+ * @return A pointer to the sensor object matching that sensor type, or NULL if
+ *         none found.
+ */
+struct sensor *
+sensor_find_next(sensor_type_t type, struct sensor *prev_cursor)
+{
+    struct sensor *cursor;
+
+    cursor = prev_cursor;
+    if (cursor == NULL) {
+        cursor = TAILQ_FIRST(&g_sensor_list);
+    }
+
+    while (cursor != NULL) {
+        if ((cursor->s_type & type) != 0) {
+            return (cursor);
+        }
+        cursor = TAILQ_NEXT(cursor);
+    }
+
+    return (NULL);
+}
+
+/**
+ * Lock access to the sensor specified by sensor.  Blocks until lock acquired.
+ *
+ * @param The sensor to lock
+ *
+ * @return 0 on success, non-zero on failure.
+ */
+int
+sensor_lock(struct sensor *sensor)
+{
+    int rc;
+
+    rc = os_mutex_pend(&sensor->s_lock, OS_TIMEOUT_NEVER);
+    if (rc != 0) {
+        goto err;
+    }
+    return (0);
+err:
+    return (rc);
+}
+
+/**
+ * Unlock access to the sensor specified by sensor.  Blocks until lock acquired.
+ *
+ * @param The sensor to unlock access to.
+ */
+void
+sensor_unlock(struct sensor *sensor)
+{
+    os_mutex_release(&sensor->s_lock);
+}
+
+
+/**
+ * Initialize a sensor
+ *
+ * @param sensor The sensor to initialize
+ *
+ * @return 0 on success, non-zero error code on failure.
+ */
+int
+sensor_init(struct sensor *sensor)
+{
+    memset(sensor, 0, sizeof(*sensor));
+
+    rc = os_mutex_init(&sensor->s_lock);
+    if (rc != 0) {
+        goto err;
+    }
+    return (0);
+err:
+    return (rc);
+}
+
+
+/**
+ * Register the sensor with the global sensor list.   This makes the sensor searchable
+ * by other packages, who may want to look it up by type.
+ *
+ * @param The sensor to register
+ *
+ * @return 0 on success, non-zero error code on failure.
+ */
+int
+sensor_register(struct sensor *sensor)
+{
+    return (0);
+}
+
+
+/**
+ * Register a sensor listener.  This allows a calling application to receive
+ * callbacks for data from a given sensor object.
+ *
+ * For more information on the type of callbacks available, see the documentation
+ * for the sensor listener structure.
+ *
+ * @param The sensor to register a listener on
+ * @param The listener to register onto the sensor
+ *
+ * @return 0 on success, non-zero error code on failure.
+ */
+int
+sensor_register_listener(struct sensor *sensor,
+        struct sensor_listener *listener)
+{
+    int rc;
+
+    rc = sensor_lock(sensor);
+    if (rc != 0) {
+        goto err;
+    }
+
+    SLIST_INSERT_HEAD(&sensor->s_listener_list, listener, sl_next);
+
+    sensor_unlock(sensor);
+
+    return (0);
+err:
+    return (rc);
+}
+
+static os_time_t
+sensor_poll_run(struct sensor *sensor, os_time_t now)
+{
+    struct sensor_listener *listener;
+    struct sensor_value value;
+    os_time_t next_run;
+    int rc;
+
+    next_run = OS_TIMEOUT_NEVER;
+
+    SLIST_FOREACH(listener, &sensor->s_listener_list, sl_next) {
+        if (listener->sl_type == SENSOR_LISTENER_TYPE_NOTIFY) {
+            continue;
+        }
+        if (listener->sl_next_run >= now) {
+            rc |= sensor_read(sensor, type, &value);
+            rc |= listener->sl_func(sensor, listener, value);
+
+            listener->sl_next_run = os_time_get() +
+                os_time_ms_to_ticks(sl->sl_poll_rate_ms);
+            if (listener->sl_next_run < next_run) {
+                next_run = listener->sl_next_run;
+            }
+        }
+    }
+    sensor->s_next_wakeup = next_run;
+
+    return (next_run);
+}
+
+
+static void
+sensor_poll_event(struct os_event *ev)
+{
+    struct sensor *cursor;
+    os_time_t time;
+    os_time_t next_wakeup;
+    os_time_t sensor_task_next_wakeup;
+
+    time = os_time_get();
+
+    sensor_next_wakeup = time + SENSOR_DEFAULT_POLL_RATE;
+
+    TAILQ_FOREACH(cursor, &g_sensor_list, s_next) {
+        if (cursor->s_next_wakeup == OS_TIMEOUT_NEVER) {
+            break;
+        }
+
+        /* Sort lowest to highest, so when we encounter a entry in the list
+         * that isn't ready, we can exit.
+         */
+        if (cursor->s_poll_expire < time) {
+            break;
+        }
+
+        next_wakeup = sensor_poll_run(cursor, now);
+        if (next_wakeup < sensor_task_next_wakeup) {
+            sensor_task_next_wakeup = next_wakeup;
+        }
+    }
+
+    os_callout_reset(&sensor_callout, sensor_task_next_wakeup);
+}
+
+void
+sensor_pkg_init(void)
+{
+    /* Ensure this is only called by sysinit */
+    SYSINIT_ASSERT_ACTIVE();
+
+    /**
+     * Initialize callout and set it to fire on boot.
+     */
+    os_callout_init(&sensors_callout, sensor_evq_get(), sensors_poll_event,
+            NULL);
+    os_callout_reset(&sensors_callout, 0);
+}


[5/6] incubator-mynewt-core git commit: fixup compiler warnings which caught invalid enum checks.

Posted by st...@apache.org.
fixup compiler warnings which caught invalid enum checks.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/4eb40205
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/4eb40205
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/4eb40205

Branch: refs/heads/sensors_branch
Commit: 4eb402055f514f049c44afe8382604b5c722febc
Parents: c44ff1f
Author: Sterling Hughes <st...@apache.org>
Authored: Sat Dec 10 16:47:37 2016 -0800
Committer: Sterling Hughes <st...@apache.org>
Committed: Mon Dec 12 21:42:04 2016 -0800

----------------------------------------------------------------------
 boot/split/src/split.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4eb40205/boot/split/src/split.c
----------------------------------------------------------------------
diff --git a/boot/split/src/split.c b/boot/split/src/split.c
index b29a627..15bb289 100644
--- a/boot/split/src/split.c
+++ b/boot/split/src/split.c
@@ -89,7 +89,7 @@ split_app_active_set(int active)
 int
 split_mode_set(split_mode_t split_mode)
 {
-    if (split_mode < 0 || split_mode >= SPLIT_MODE_CNT) {
+    if (split_mode >= (split_mode_t) SPLIT_MODE_CNT) {
         return SYS_EINVAL;
     }