You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/07/26 02:40:26 UTC

[incubator-nuttx] branch master updated (5f68aa56fd -> 01aa0c2d46)

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

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


    from 5f68aa56fd poll: defalut set POLLERR POLLHUP to events
     new dad5ab75ff driver/sensor: support userspace wirte data into sensor device
     new 8f39c5f11b driver/sensor: simplify buffer operation
     new 8d971101cd driver/sensor: change protype of set_interval, batch to follow ioctl
     new b3ea6522f4 driver/usensor: support register user sensor
     new 24040250f5 driver/sensor: support multi users to access device
     new e3e59a03b1 driver/sensor: update sensor data structure and state structure
     new 01aa0c2d46 driver/sensor: change nbuffer and sensor event structure type

The 7 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 drivers/sensors/Kconfig        |   9 +-
 drivers/sensors/Make.defs      |   4 +
 drivers/sensors/bmp280.c       |   6 +-
 drivers/sensors/ds18b20.c      |  20 +-
 drivers/sensors/fakesensor.c   |  36 +--
 drivers/sensors/hyt271.c       |  30 +-
 drivers/sensors/l3gd20.c       |  18 +-
 drivers/sensors/ms5611.c       |  14 +-
 drivers/sensors/sensor.c       | 657 +++++++++++++++++++++++++++--------------
 drivers/sensors/usensor.c      | 249 ++++++++++++++++
 drivers/sensors/wtgahrs2.c     |  30 +-
 include/nuttx/sensors/ioctl.h  |  47 +--
 include/nuttx/sensors/sensor.h | 134 ++++++---
 13 files changed, 886 insertions(+), 368 deletions(-)
 create mode 100644 drivers/sensors/usensor.c


[incubator-nuttx] 03/07: driver/sensor: change protype of set_interval, batch to follow ioctl

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 8d971101cd7531779a13205770382e61c871823f
Author: Jiuzhu Dong <do...@xiaomi.com>
AuthorDate: Fri Mar 11 11:03:02 2022 +0800

    driver/sensor: change protype of set_interval, batch to follow ioctl
    
    1. change unsigned int to unsigned long
    
    Signed-off-by: Jiuzhu Dong <do...@xiaomi.com>
---
 drivers/sensors/bmp280.c       |  4 ++--
 drivers/sensors/ds18b20.c      |  6 +++---
 drivers/sensors/fakesensor.c   | 16 ++++++++--------
 drivers/sensors/hyt271.c       |  6 +++---
 drivers/sensors/ms5611.c       |  6 +++---
 drivers/sensors/sensor.c       | 19 +++++++++----------
 drivers/sensors/wtgahrs2.c     | 10 +++++-----
 include/nuttx/sensors/sensor.h |  4 ++--
 8 files changed, 35 insertions(+), 36 deletions(-)

diff --git a/drivers/sensors/bmp280.c b/drivers/sensors/bmp280.c
index 703cd8e059..07bb96d19d 100644
--- a/drivers/sensors/bmp280.c
+++ b/drivers/sensors/bmp280.c
@@ -160,7 +160,7 @@ static int bmp280_putreg8(FAR struct bmp280_dev_s *priv, uint8_t regaddr,
 /* Sensor methods */
 
 static int bmp280_set_interval(FAR struct sensor_lowerhalf_s *lower,
-                               FAR unsigned int *period_us);
+                               FAR unsigned long *period_us);
 static int bmp280_activate(FAR struct sensor_lowerhalf_s *lower,
                            bool enable);
 static int bmp280_fetch(FAR struct sensor_lowerhalf_s *lower,
@@ -504,7 +504,7 @@ static uint32_t bmp280_compensate_press(FAR struct bmp280_dev_s *priv,
  ****************************************************************************/
 
 static int bmp280_set_interval(FAR struct sensor_lowerhalf_s *lower,
-                               FAR unsigned int *period_us)
+                               FAR unsigned long *period_us)
 {
   FAR struct bmp280_dev_s *priv = container_of(lower,
                                                FAR struct bmp280_dev_s,
diff --git a/drivers/sensors/ds18b20.c b/drivers/sensors/ds18b20.c
index cba9e686d6..ad77d4bf3b 100644
--- a/drivers/sensors/ds18b20.c
+++ b/drivers/sensors/ds18b20.c
@@ -146,7 +146,7 @@ struct ds18b20_dev_s
   struct onewire_config_s     config;         /* 1wire device configuration */
   struct ds18b20_config_s     reg;            /* Sensor resolution */
 #ifdef CONFIG_SENSORS_DS18B20_POLL
-  unsigned int                interval;       /* Polling interval */
+  unsigned long               interval;       /* Polling interval */
   sem_t                       run;            /* Locks sensor thread */
 #endif
 };
@@ -168,7 +168,7 @@ static int ds18b20_control(FAR struct sensor_lowerhalf_s *lower,
 
 #ifdef CONFIG_SENSORS_DS18B20_POLL
 static int ds18b20_set_interval(FAR struct sensor_lowerhalf_s *lower,
-                                FAR unsigned int *period_us);
+                                FAR unsigned long *period_us);
 #endif
 
 /****************************************************************************
@@ -778,7 +778,7 @@ static int ds18b20_active(FAR struct sensor_lowerhalf_s *lower,
 
 #ifdef CONFIG_SENSORS_DS18B20_POLL
 static int ds18b20_set_interval(FAR struct sensor_lowerhalf_s *lower,
-                               FAR unsigned int *period_us)
+                               FAR unsigned long *period_us)
 {
   FAR struct ds18b20_dev_s *priv = (FAR struct ds18b20_dev_s *)lower;
   priv->interval = *period_us;
diff --git a/drivers/sensors/fakesensor.c b/drivers/sensors/fakesensor.c
index 8631e4dc2a..ee27e45317 100644
--- a/drivers/sensors/fakesensor.c
+++ b/drivers/sensors/fakesensor.c
@@ -47,8 +47,8 @@ struct fakesensor_s
 {
   struct sensor_lowerhalf_s lower;
   struct file data;
-  unsigned int interval;
-  unsigned int batch;
+  unsigned long interval;
+  unsigned long batch;
   int raw_start;
   FAR const char *file_path;
   sem_t wakeup;
@@ -62,9 +62,9 @@ struct fakesensor_s
 static int fakesensor_activate(FAR struct sensor_lowerhalf_s *lower,
                                bool sw);
 static int fakesensor_set_interval(FAR struct sensor_lowerhalf_s *lower,
-                                   FAR unsigned int *period_us);
+                                   FAR unsigned long *period_us);
 static int fakesensor_batch(FAR struct sensor_lowerhalf_s *lower,
-                            FAR unsigned int *latency_us);
+                            FAR unsigned long *latency_us);
 static void fakesensor_push_event(FAR struct sensor_lowerhalf_s *lower);
 static int fakesensor_thread(int argc, char** argv);
 
@@ -120,7 +120,7 @@ static int fakesensor_read_csv_header(FAR struct fakesensor_s *sensor)
       fakesensor_read_csv_line(&sensor->data, buffer, sizeof(buffer), 0);
   if (sensor->interval == 0)
     {
-      sscanf(buffer, "interval:%d\n", &sensor->interval);
+      sscanf(buffer, "interval:%lu\n", &sensor->interval);
       sensor->interval *= 1000;
     }
 
@@ -234,7 +234,7 @@ static int fakesensor_activate(FAR struct sensor_lowerhalf_s *lower, bool sw)
 }
 
 static int fakesensor_set_interval(FAR struct sensor_lowerhalf_s *lower,
-                                   FAR unsigned int *period_us)
+                                   FAR unsigned long *period_us)
 {
   FAR struct fakesensor_s *sensor = container_of(lower,
                                                  struct fakesensor_s, lower);
@@ -243,11 +243,11 @@ static int fakesensor_set_interval(FAR struct sensor_lowerhalf_s *lower,
 }
 
 static int fakesensor_batch(FAR struct sensor_lowerhalf_s *lower,
-                            FAR unsigned int *latency_us)
+                            FAR unsigned long *latency_us)
 {
   FAR struct fakesensor_s *sensor = container_of(lower,
                                                  struct fakesensor_s, lower);
-  uint32_t max_latency = sensor->lower.buffer_number * sensor->interval;
+  unsigned long max_latency = sensor->lower.buffer_number * sensor->interval;
   if (*latency_us > max_latency)
     {
       *latency_us = max_latency;
diff --git a/drivers/sensors/hyt271.c b/drivers/sensors/hyt271.c
index a2a0744d19..744277e0e3 100644
--- a/drivers/sensors/hyt271.c
+++ b/drivers/sensors/hyt271.c
@@ -96,7 +96,7 @@ struct hyt271_dev_s
   sem_t                   lock_measure_cycle;       /* Locks measure cycle */
   uint32_t                freq;                     /* I2C Frequency */
 #ifdef CONFIG_SENSORS_HYT271_POLL
-  unsigned int            interval;                 /* Polling interval */
+  unsigned long           interval;                 /* Polling interval */
   sem_t                   run;                      /* Locks sensor thread */
   bool                    initial_read;             /* Already read */
 #endif
@@ -120,7 +120,7 @@ static int hyt271_control(FAR struct sensor_lowerhalf_s *lower,
 
 #ifdef CONFIG_SENSORS_HYT271_POLL
 static int hyt271_set_interval(FAR struct sensor_lowerhalf_s *lower,
-                               FAR unsigned int *period_us);
+                               FAR unsigned long *period_us);
 #endif
 
 /****************************************************************************
@@ -751,7 +751,7 @@ static int hyt271_active(FAR struct sensor_lowerhalf_s *lower,
 
 #ifdef CONFIG_SENSORS_HYT271_POLL
 static int hyt271_set_interval(FAR struct sensor_lowerhalf_s *lower,
-                               FAR unsigned int *period_us)
+                               FAR unsigned long *period_us)
 {
   FAR struct hyt271_sensor_s *priv = (FAR struct hyt271_sensor_s *)lower;
   priv->dev->interval = *period_us;
diff --git a/drivers/sensors/ms5611.c b/drivers/sensors/ms5611.c
index 60b321b24b..97a4b6bd36 100644
--- a/drivers/sensors/ms5611.c
+++ b/drivers/sensors/ms5611.c
@@ -94,7 +94,7 @@ struct ms5611_dev_s
 
   uint32_t                  freq;      /* Bus Frequency I2C/SPI */
   struct ms5611_calib_s     calib;     /* Calib. params from ROM */
-  unsigned int              interval;  /* Polling interval */
+  unsigned long             interval;  /* Polling interval */
   bool                      enabled;   /* Enable/Disable MS5611 */
   sem_t                     run;       /* Locks measure cycle */
   sem_t                     exclsem;   /* Manages exclusive to device */
@@ -121,7 +121,7 @@ static unsigned long ms5611_curtime(void);
 /* Sensor methods */
 
 static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
-                               FAR unsigned int *period_us);
+                               FAR unsigned long *period_us);
 static int ms5611_activate(FAR struct sensor_lowerhalf_s *lower,
                            bool enable);
 
@@ -540,7 +540,7 @@ static uint32_t ms5611_compensate_press(FAR struct ms5611_dev_s *priv,
  ****************************************************************************/
 
 static int ms5611_set_interval(FAR struct sensor_lowerhalf_s *lower,
-                               FAR unsigned int *period_us)
+                               FAR unsigned long *period_us)
 {
   FAR struct ms5611_dev_s *priv = container_of(lower,
                                                FAR struct ms5611_dev_s,
diff --git a/drivers/sensors/sensor.c b/drivers/sensors/sensor.c
index 9c8db17081..1dada70e8b 100644
--- a/drivers/sensors/sensor.c
+++ b/drivers/sensors/sensor.c
@@ -75,8 +75,8 @@ struct sensor_upperhalf_s
   sem_t              exclsem;            /* Manages exclusive access to file operations */
   sem_t              buffersem;          /* Wakeup user waiting for data in circular buffer */
   bool               enabled;            /* The status of sensor enable or disable */
-  unsigned int       interval;           /* The sample interval for sensor, in us */
-  unsigned int       latency;            /* The batch latency for sensor, in us */
+  unsigned long      interval;           /* The sample interval for sensor, in us */
+  unsigned long      latency;            /* The batch latency for sensor, in us */
 };
 
 /****************************************************************************
@@ -340,7 +340,6 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
   FAR struct inode *inode = filep->f_inode;
   FAR struct sensor_upperhalf_s *upper = inode->i_private;
   FAR struct sensor_lowerhalf_s *lower = upper->lower;
-  FAR unsigned int *val = (unsigned int *)(uintptr_t)arg;
   int ret;
 
   sninfo("cmd=%x arg=%08lx\n", cmd, arg);
@@ -382,15 +381,15 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
               break;
             }
 
-          if (upper->interval == *val)
+          if (upper->interval == arg)
             {
               break;
             }
 
-          ret = lower->ops->set_interval(lower, val);
+          ret = lower->ops->set_interval(lower, &arg);
           if (ret >= 0)
             {
-              upper->interval = *val;
+              upper->interval = arg;
             }
         }
         break;
@@ -409,15 +408,15 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
               break;
             }
 
-          if (upper->latency == *val)
+          if (upper->latency == arg)
             {
               break;
             }
 
-          ret = lower->ops->batch(lower, val);
+          ret = lower->ops->batch(lower, &arg);
           if (ret >= 0)
             {
-              upper->latency = *val;
+              upper->latency = arg;
             }
         }
         break;
@@ -460,7 +459,7 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
 
       case SNIOC_GET_NEVENTBUF:
         {
-          *val = lower->buffer_number;
+          *(FAR uint32_t *)(uintptr_t)arg = lower->buffer_number;
         }
         break;
 
diff --git a/drivers/sensors/wtgahrs2.c b/drivers/sensors/wtgahrs2.c
index 34ebd94106..004fb23eab 100644
--- a/drivers/sensors/wtgahrs2.c
+++ b/drivers/sensors/wtgahrs2.c
@@ -73,8 +73,8 @@
 struct wtgahrs2_sensor_s
 {
   struct sensor_lowerhalf_s lower;
-  unsigned int              interval;
-  unsigned int              last_update;
+  unsigned long             interval;
+  uint64_t                  last_update;
   bool                      enable;
 };
 
@@ -97,7 +97,7 @@ struct wtgahrs2_dev_s
 
 static int wtgahrs2_activate(FAR struct sensor_lowerhalf_s *lower, bool sw);
 static int wtgahrs2_set_interval(FAR struct sensor_lowerhalf_s *lower,
-                                 FAR unsigned int *interval);
+                                 FAR unsigned long *interval);
 
 /****************************************************************************
  * Private Data
@@ -105,7 +105,7 @@ static int wtgahrs2_set_interval(FAR struct sensor_lowerhalf_s *lower,
 
 /* in microseconds */
 
-static const unsigned int g_wtgahrs2_interval[] =
+static const unsigned long g_wtgahrs2_interval[] =
 {
   10000000,  /* 0.1 hz */
   2000000,   /* 0.5 hz */
@@ -161,7 +161,7 @@ static int wtgahrs2_activate(FAR struct sensor_lowerhalf_s *lower, bool sw)
 }
 
 static int wtgahrs2_set_interval(FAR struct sensor_lowerhalf_s *lower,
-                                 FAR unsigned int *interval)
+                                 FAR unsigned long *interval)
 {
   FAR struct wtgahrs2_sensor_s *dev = (FAR struct wtgahrs2_sensor_s *)lower;
   int idx = 0;
diff --git a/include/nuttx/sensors/sensor.h b/include/nuttx/sensors/sensor.h
index c6ea2aa049..0285144904 100644
--- a/include/nuttx/sensors/sensor.h
+++ b/include/nuttx/sensors/sensor.h
@@ -628,7 +628,7 @@ struct sensor_ops_s
    **************************************************************************/
 
   CODE int (*set_interval)(FAR struct sensor_lowerhalf_s *lower,
-                           FAR unsigned int *period_us);
+                           FAR unsigned long *period_us);
 
   /**************************************************************************
    * Name: batch
@@ -672,7 +672,7 @@ struct sensor_ops_s
    **************************************************************************/
 
   CODE int (*batch)(FAR struct sensor_lowerhalf_s *lower,
-                    FAR unsigned int *latency_us);
+                    FAR unsigned long *latency_us);
 
   /**************************************************************************
    * Name: fetch


[incubator-nuttx] 05/07: driver/sensor: support multi users to access device

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 24040250f5d6645b4b1c0165d8a52323c755c7f6
Author: Jiuzhu Dong <do...@xiaomi.com>
AuthorDate: Tue Mar 22 15:46:02 2022 +0800

    driver/sensor: support multi users to access device
    
    1.Allow multi users to access the same sensor device simultaneously.
    2.Get real state of sensor device by cmd SNIOC_GET_STATE for users.
    3.Get update state since last read by poll without timeout for users.
    4.Sensor device will be activated when first user open and will close when
      last user closed.
    5.When multi users to access device, driver always set the minimum
      sampling interval and latency to the sensor device and allow
      downsampled for users above the minimum sampling interval.
    6.The circbuffer will overwrite old data when buffer is full, so if users
      don't read data soon, data will be lost, and the oldest data in circbuffer
      are returned to the users.
    7.Always read the last data in the circbuffer as initial value for new
      users when the sensor device has not yet generated new data.
    8.when user uses poll, if subscription interval is satisfied, the POLLIN
      events is returned for each users.
    9.When new user generate or the state of sensor device changed, the POLLPRI
      will notify to all users.
    10.Support multi advertisers to subscribe their own data as loop test.
    
    Signed-off-by: Jiuzhu Dong <do...@xiaomi.com>
---
 drivers/sensors/Kconfig        |   7 -
 drivers/sensors/sensor.c       | 499 +++++++++++++++++++++++++++++------------
 include/nuttx/sensors/ioctl.h  |   9 +-
 include/nuttx/sensors/sensor.h |  11 +
 4 files changed, 372 insertions(+), 154 deletions(-)

diff --git a/drivers/sensors/Kconfig b/drivers/sensors/Kconfig
index d4d50e8d87..1f2fc138ac 100644
--- a/drivers/sensors/Kconfig
+++ b/drivers/sensors/Kconfig
@@ -12,13 +12,6 @@ menuconfig SENSORS
 
 if SENSORS
 
-config SENSORS_NPOLLWAITERS
-	int "Number of poll threads"
-	default 2
-	---help---
-		Maximum number of threads than can be waiting for POLL events.
-		Default: 2
-
 config USENSOR
 	bool "Usensor Device Support"
 	default n
diff --git a/drivers/sensors/sensor.c b/drivers/sensors/sensor.c
index 1dada70e8b..5576ec8fb1 100644
--- a/drivers/sensors/sensor.c
+++ b/drivers/sensors/sensor.c
@@ -34,6 +34,7 @@
 
 #include <poll.h>
 #include <fcntl.h>
+#include <nuttx/list.h>
 #include <nuttx/kmalloc.h>
 #include <nuttx/mm/circbuf.h>
 #include <nuttx/sensors/sensor.h>
@@ -55,28 +56,48 @@
 
 /* This structure describes sensor info */
 
-struct sensor_info
+struct sensor_info_s
 {
   uint8_t   esize;
   FAR char *name;
 };
 
+/* This structure describes user info of sensor, the user may be
+ * advertiser or subscriber
+ */
+
+struct sensor_user_s
+{
+  /* The common info */
+
+  struct list_node node;       /* Node of users list */
+  struct pollfd   *fds;        /* The poll structure of thread waiting events */
+  bool             changed;    /* This is used to indicate event happens and need to
+                                * asynchronous notify other users
+                                */
+  sem_t            buffersem;  /* Wakeup user waiting for data in circular buffer */
+
+  /* The subscriber info
+   * Support multi advertisers to subscribe their own data when they
+   * appear in dual role
+   */
+
+  size_t           generation; /* Last generation subscriber has seen */
+  unsigned long    interval;   /* The interval for subscriber */
+  unsigned long    latency;    /* The bactch latency for subscriber */
+};
+
 /* This structure describes the state of the upper half driver */
 
 struct sensor_upperhalf_s
 {
-  /* poll structures of threads waiting for driver events. */
-
-  FAR struct pollfd             *fds[CONFIG_SENSORS_NPOLLWAITERS];
-  FAR struct sensor_lowerhalf_s *lower;  /* the handle of lower half driver */
+  FAR struct sensor_lowerhalf_s *lower;  /* The handle of lower half driver */
+  struct sensor_state_s          state;  /* The state of sensor device */
   struct circbuf_s   buffer;             /* The circular buffer of sensor device */
   uint8_t            esize;              /* The element size of circular buffer */
-  uint8_t            crefs;              /* Number of times the device has been opened */
   sem_t              exclsem;            /* Manages exclusive access to file operations */
-  sem_t              buffersem;          /* Wakeup user waiting for data in circular buffer */
-  bool               enabled;            /* The status of sensor enable or disable */
-  unsigned long      interval;           /* The sample interval for sensor, in us */
-  unsigned long      latency;            /* The batch latency for sensor, in us */
+  size_t             generation;         /* The current generation count */
+  struct list_node   userlist;           /* List of users */
 };
 
 /****************************************************************************
@@ -102,7 +123,7 @@ static ssize_t sensor_push_event(FAR void *priv, FAR const void *data,
  * Private Data
  ****************************************************************************/
 
-static const struct sensor_info g_sensor_info[] =
+static const struct sensor_info_s g_sensor_info[] =
 {
   {0,                                           NULL},
   {sizeof(struct sensor_event_accel),           "accel"},
@@ -157,32 +178,174 @@ static const struct file_operations g_sensor_fops =
  * Private Functions
  ****************************************************************************/
 
-static void sensor_pollnotify(FAR struct sensor_upperhalf_s *upper,
-                              pollevent_t eventset)
+static bool sensor_in_range(size_t left, size_t value, size_t right)
 {
-  FAR struct pollfd *fd;
-  int semcount;
-  int i;
+  if (left < right)
+    {
+      return left <= value && value < right;
+    }
+  else
+    {
+      /* Maybe the data overflowed and a wraparound occurred */
+
+      return left <= value || value < right;
+    }
+}
+
+static bool sensor_is_updated(size_t generation, size_t ugeneration)
+{
+  return generation > ugeneration;
+}
+
+static int sensor_update_interval(FAR struct sensor_upperhalf_s *upper,
+                                  FAR struct sensor_user_s *user,
+                                  unsigned long interval)
+{
+  FAR struct sensor_lowerhalf_s *lower = upper->lower;
+  FAR struct sensor_user_s *tmp;
+  unsigned long min_interval = interval;
+  int ret = 0;
+
+  if (interval == user->interval)
+    {
+      return 0;
+    }
+
+  if (interval <= upper->state.min_interval)
+    {
+      goto update;
+    }
 
-  for (i = 0; i < CONFIG_SENSORS_NPOLLWAITERS; i++)
+  list_for_every_entry(&upper->userlist, tmp, struct sensor_user_s, node)
     {
-      fd = upper->fds[i];
-      if (fd)
+      if (tmp == user)
         {
-          fd->revents |= (fd->events & eventset);
+          continue;
+        }
 
-          if (fd->revents != 0)
-            {
-              sninfo("Report events: %08" PRIx32 "\n", fd->revents);
+      if (min_interval > tmp->interval)
+        {
+          min_interval = tmp->interval;
+        }
+    }
 
-              nxsem_get_value(fd->sem, &semcount);
-              if (semcount < 1)
-                {
-                  nxsem_post(fd->sem);
-                }
-            }
+update:
+  if (min_interval == upper->state.min_interval)
+    {
+      user->interval = interval;
+      return ret;
+    }
+
+  if (min_interval != ULONG_MAX && lower->ops->set_interval)
+    {
+      ret = lower->ops->set_interval(lower, &min_interval);
+      if (ret < 0)
+        {
+          return ret;
         }
     }
+
+  upper->state.min_interval = min_interval;
+  user->interval = interval;
+  sensor_pollnotify(upper, POLLPRI);
+  return ret;
+}
+
+static int sensor_update_latency(FAR struct sensor_upperhalf_s *upper,
+                                 FAR struct sensor_user_s *user,
+                                 unsigned long latency)
+{
+  FAR struct sensor_lowerhalf_s *lower = upper->lower;
+  FAR struct sensor_user_s *tmp;
+  unsigned long min_latency = latency;
+  int ret = 0;
+
+  if (upper->state.min_interval == 0)
+    {
+      return -EINVAL;
+    }
+
+  if (latency == user->latency)
+    {
+      return 0;
+    }
+
+  if (latency <= upper->state.min_latency)
+    {
+      goto update;
+    }
+
+  list_for_every_entry(&upper->userlist, tmp, struct sensor_user_s, node)
+    {
+      if (tmp == user)
+        {
+          continue;
+        }
+
+      if (min_latency > tmp->latency)
+        {
+          min_latency = tmp->latency;
+        }
+    }
+
+update:
+  if (min_latency == upper->state.min_latency)
+    {
+      user->latency = latency;
+      return ret;
+    }
+
+  if (min_latency != ULONG_MAX && lower->ops->batch)
+    {
+      ret = lower->ops->batch(lower, &min_latency);
+      if (ret < 0)
+        {
+          return ret;
+        }
+    }
+
+  upper->state.min_latency = min_latency;
+  user->latency = latency;
+  sensor_pollnotify(upper, POLLPRI);
+  return ret;
+}
+
+static void sensor_pollnotify_one(FAR struct sensor_user_s *user,
+                                  pollevent_t eventset)
+{
+  int semcount;
+
+  if (eventset == POLLPRI)
+    {
+      user->changed = true;
+    }
+
+  if (!user->fds)
+    {
+      return;
+    }
+
+  user->fds->revents |= (user->fds->events & eventset);
+  if (user->fds->revents != 0)
+    {
+      sninfo("Report events: %08" PRIx32 "\n", user->fds->revents);
+      nxsem_get_value(user->fds->sem, &semcount);
+      if (semcount < 1)
+        {
+          nxsem_post(user->fds->sem);
+        }
+    }
+}
+
+static void sensor_pollnotify(FAR struct sensor_upperhalf_s *upper,
+                              pollevent_t eventset)
+{
+  FAR struct sensor_user_s *user;
+
+  list_for_every_entry(&upper->userlist, user, struct sensor_user_s, node)
+    {
+      sensor_pollnotify_one(user, eventset);
+    }
 }
 
 static int sensor_open(FAR struct file *filep)
@@ -190,7 +353,7 @@ static int sensor_open(FAR struct file *filep)
   FAR struct inode *inode = filep->f_inode;
   FAR struct sensor_upperhalf_s *upper = inode->i_private;
   FAR struct sensor_lowerhalf_s *lower = upper->lower;
-  uint8_t tmp;
+  FAR struct sensor_user_s *user;
   int ret;
 
   ret = nxsem_wait(&upper->exclsem);
@@ -199,17 +362,49 @@ static int sensor_open(FAR struct file *filep)
       return ret;
     }
 
-  tmp = upper->crefs + 1;
-  if (tmp == 0)
+  user = kmm_zalloc(sizeof(struct sensor_user_s));
+  if (user == NULL)
     {
-      /* More than 255 opens; uint8_t overflows to zero */
+      ret = -ENOMEM;
+      goto errout_with_sem;
+    }
 
-      ret = -EMFILE;
-      goto err;
+  if (filep->f_oflags & O_RDOK)
+    {
+      if (upper->state.nsubscribers == 0 && lower->ops->activate)
+        {
+          ret = lower->ops->activate(lower, true);
+          if (ret < 0)
+            {
+              goto errout_with_user;
+            }
+        }
+
+      upper->state.nsubscribers++;
+    }
+
+  if (filep->f_oflags & O_WROK)
+    {
+      upper->state.nadvertisers++;
     }
 
-  upper->crefs = tmp;
-err:
+  user->interval   = ULONG_MAX;
+  user->latency    = ULONG_MAX;
+  user->generation = upper->generation;
+  nxsem_init(&user->buffersem, 0, 0);
+  nxsem_set_protocol(&user->buffersem, SEM_PRIO_NONE);
+  list_add_tail(&upper->userlist, &user->node);
+
+  /* The new user generation, notify to other users */
+
+  sensor_pollnotify(upper, POLLPRI);
+
+  filep->f_priv = user;
+  goto errout_with_sem;
+
+errout_with_user:
+  kmm_free(user);
+errout_with_sem:
   nxsem_post(&upper->exclsem);
   return ret;
 }
@@ -219,6 +414,7 @@ static int sensor_close(FAR struct file *filep)
   FAR struct inode *inode = filep->f_inode;
   FAR struct sensor_upperhalf_s *upper = inode->i_private;
   FAR struct sensor_lowerhalf_s *lower = upper->lower;
+  FAR struct sensor_user_s *user = filep->f_priv;
   int ret;
 
   ret = nxsem_wait(&upper->exclsem);
@@ -227,18 +423,31 @@ static int sensor_close(FAR struct file *filep)
       return ret;
     }
 
-  if (--upper->crefs <= 0 && upper->enabled)
+  if (filep->f_oflags & O_RDOK)
     {
-      ret = lower->ops->activate ?
-            lower->ops->activate(lower, false) : -ENOTSUP;
-      if (ret >= 0)
+      upper->state.nsubscribers--;
+      if (upper->state.nsubscribers == 0 && lower->ops->activate)
         {
-          upper->enabled = false;
-          circbuf_uninit(&upper->buffer);
+          lower->ops->activate(lower, false);
         }
     }
 
+  if (filep->f_oflags & O_WROK)
+    {
+      upper->state.nadvertisers--;
+    }
+
+  list_delete(&user->node);
+  sensor_update_latency(upper, user, ULONG_MAX);
+  sensor_update_interval(upper, user, ULONG_MAX);
+  nxsem_destroy(&user->buffersem);
+
+  /* The user is closed, notify to other users */
+
+  sensor_pollnotify(upper, POLLPRI);
   nxsem_post(&upper->exclsem);
+
+  kmm_free(user);
   return ret;
 }
 
@@ -248,7 +457,9 @@ static ssize_t sensor_read(FAR struct file *filep, FAR char *buffer,
   FAR struct inode *inode = filep->f_inode;
   FAR struct sensor_upperhalf_s *upper = inode->i_private;
   FAR struct sensor_lowerhalf_s *lower = upper->lower;
+  FAR struct sensor_user_s *user = filep->f_priv;
   ssize_t ret;
+  size_t nums;
 
   if (!buffer || !len)
     {
@@ -266,7 +477,7 @@ static ssize_t sensor_read(FAR struct file *filep, FAR char *buffer,
       if (!(filep->f_oflags & O_NONBLOCK))
         {
           nxsem_post(&upper->exclsem);
-          ret = nxsem_wait_uninterruptible(&upper->buffersem);
+          ret = nxsem_wait_uninterruptible(&user->buffersem);
           if (ret < 0)
             {
               return ret;
@@ -278,7 +489,7 @@ static ssize_t sensor_read(FAR struct file *filep, FAR char *buffer,
               return ret;
             }
         }
-      else if (!upper->enabled)
+      else if (!upper->state.nsubscribers)
         {
           ret = -EAGAIN;
           goto out;
@@ -304,7 +515,7 @@ static ssize_t sensor_read(FAR struct file *filep, FAR char *buffer,
           else
             {
               nxsem_post(&upper->exclsem);
-              ret = nxsem_wait_uninterruptible(&upper->buffersem);
+              ret = nxsem_wait_uninterruptible(&user->buffersem);
               if (ret < 0)
                 {
                   return ret;
@@ -318,7 +529,37 @@ static ssize_t sensor_read(FAR struct file *filep, FAR char *buffer,
             }
         }
 
-      ret = circbuf_read(&upper->buffer, buffer, len);
+      /* Always read the last data in the circbuffer as initial value
+       * for new users when the sensor device has not yet generated
+       * new data.
+       */
+
+      if (user->generation == upper->generation)
+        {
+          user->generation--;
+        }
+
+      /* If user's generation isn't within circbuffer range, the
+       * oldest data in circbuffer are returned to the users.
+       */
+
+      else if (!sensor_in_range(upper->generation - lower->buffer_number,
+                                user->generation, upper->generation))
+
+        {
+          user->generation = upper->generation - lower->buffer_number;
+        }
+
+      nums = upper->generation - user->generation;
+      if (len < nums * upper->esize)
+        {
+          nums = len / upper->esize;
+        }
+
+      len = nums * upper->esize;
+      ret = circbuf_peekat(&upper->buffer, user->generation * upper->esize,
+                           buffer, len);
+      user->generation += nums;
     }
 
 out:
@@ -340,6 +581,7 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
   FAR struct inode *inode = filep->f_inode;
   FAR struct sensor_upperhalf_s *upper = inode->i_private;
   FAR struct sensor_lowerhalf_s *lower = upper->lower;
+  FAR struct sensor_user_s *user = filep->f_priv;
   int ret;
 
   sninfo("cmd=%x arg=%08lx\n", cmd, arg);
@@ -352,72 +594,23 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
 
   switch (cmd)
     {
-      case SNIOC_ACTIVATE:
+      case SNIOC_GET_STATE:
         {
-          if (upper->enabled == !!arg)
-            {
-              break;
-            }
-
-          ret = lower->ops->activate ?
-                lower->ops->activate(lower, !!arg) : -ENOTSUP;
-          if (ret >= 0)
-            {
-              upper->enabled = !!arg;
-              if (!upper->enabled)
-                {
-                  upper->interval = 0;
-                  upper->latency = 0;
-                }
-            }
+          memcpy((FAR void *)(uintptr_t)arg,
+                 &upper->state, sizeof(upper->state));
+          user->changed = false;
         }
         break;
 
       case SNIOC_SET_INTERVAL:
         {
-          if (lower->ops->set_interval == NULL)
-            {
-              ret = -ENOTSUP;
-              break;
-            }
-
-          if (upper->interval == arg)
-            {
-              break;
-            }
-
-          ret = lower->ops->set_interval(lower, &arg);
-          if (ret >= 0)
-            {
-              upper->interval = arg;
-            }
+          ret = sensor_update_interval(upper, user, arg);
         }
         break;
 
       case SNIOC_BATCH:
         {
-          if (lower->ops->batch == NULL)
-            {
-              ret = -ENOTSUP;
-              break;
-            }
-
-          if (upper->interval == 0)
-            {
-              ret = -EINVAL;
-              break;
-            }
-
-          if (upper->latency == arg)
-            {
-              break;
-            }
-
-          ret = lower->ops->batch(lower, &arg);
-          if (ret >= 0)
-            {
-              upper->latency = arg;
-            }
+          ret = sensor_update_latency(upper, user, arg);
         }
         break;
 
@@ -467,7 +660,14 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
         {
           if (!circbuf_is_init(&upper->buffer))
             {
-              lower->buffer_number = arg;
+              if (arg >= lower->buffer_number)
+                {
+                  lower->buffer_number = arg;
+                }
+              else
+                {
+                  ret = -ERANGE;
+                }
             }
           else
             {
@@ -497,15 +697,15 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
 }
 
 static int sensor_poll(FAR struct file *filep,
-                       struct pollfd *fds, bool setup)
+                       FAR struct pollfd *fds, bool setup)
 {
   FAR struct inode *inode = filep->f_inode;
   FAR struct sensor_upperhalf_s *upper = inode->i_private;
   FAR struct sensor_lowerhalf_s *lower = upper->lower;
+  FAR struct sensor_user_s *user = filep->f_priv;
   pollevent_t eventset = 0;
   int semcount;
   int ret;
-  int i;
 
   ret = nxsem_wait(&upper->exclsem);
   if (ret < 0)
@@ -515,24 +715,16 @@ static int sensor_poll(FAR struct file *filep,
 
   if (setup)
     {
-      for (i = 0; i < CONFIG_SENSORS_NPOLLWAITERS; i++)
-        {
-          if (NULL == upper->fds[i])
-            {
-              upper->fds[i] = fds;
-              fds->priv = &upper->fds[i];
-              break;
-            }
-        }
-
       /* Don't have enough space to store fds */
 
-      if (i == CONFIG_SENSORS_NPOLLWAITERS)
+      if (user->fds)
         {
           ret = -ENOSPC;
           goto errout;
         }
 
+      user->fds = fds;
+      fds->priv = filep;
       if (lower->ops->fetch)
         {
           /* Always return POLLIN for fetch data directly(non-block) */
@@ -543,34 +735,32 @@ static int sensor_poll(FAR struct file *filep,
             }
           else
             {
-              nxsem_get_value(&upper->buffersem, &semcount);
+              nxsem_get_value(&user->buffersem, &semcount);
               if (semcount > 0)
                 {
                   eventset |= (fds->events & POLLIN);
                 }
             }
         }
-      else if (!circbuf_is_empty(&upper->buffer))
+      else if (sensor_is_updated(upper->generation, user->generation))
         {
           eventset |= (fds->events & POLLIN);
         }
 
+      if (user->changed)
+        {
+          eventset |= (fds->events & POLLPRI);
+        }
+
       if (eventset)
         {
-          sensor_pollnotify(upper, eventset);
+          sensor_pollnotify_one(user, eventset);
         }
     }
-  else if (fds->priv != NULL)
+  else
     {
-      for (i = 0; i < CONFIG_SENSORS_NPOLLWAITERS; i++)
-        {
-          if (fds == upper->fds[i])
-            {
-              upper->fds[i] = NULL;
-              fds->priv = NULL;
-              break;
-            }
-        }
+      user->fds = NULL;
+      fds->priv = NULL;
     }
 
 errout:
@@ -583,10 +773,13 @@ static ssize_t sensor_push_event(FAR void *priv, FAR const void *data,
 {
   FAR struct sensor_upperhalf_s *upper = priv;
   FAR struct sensor_lowerhalf_s *lower = upper->lower;
+  FAR struct sensor_user_s *user;
+  size_t envcount;
   int semcount;
   int ret;
 
-  if (!bytes)
+  envcount = bytes / upper->esize;
+  if (!envcount || bytes != envcount * upper->esize)
     {
       return -EINVAL;
     }
@@ -611,11 +804,19 @@ static ssize_t sensor_push_event(FAR void *priv, FAR const void *data,
     }
 
   circbuf_overwrite(&upper->buffer, data, bytes);
-  sensor_pollnotify(upper, POLLIN);
-  nxsem_get_value(&upper->buffersem, &semcount);
-  if (semcount < 1)
+  upper->generation += envcount;
+  list_for_every_entry(&upper->userlist, user, struct sensor_user_s, node)
     {
-      nxsem_post(&upper->buffersem);
+      if (sensor_is_updated(upper->generation, user->generation))
+        {
+          nxsem_get_value(&user->buffersem, &semcount);
+          if (semcount < 1)
+            {
+              nxsem_post(&user->buffersem);
+            }
+
+          sensor_pollnotify_one(user, POLLIN);
+        }
     }
 
   nxsem_post(&upper->exclsem);
@@ -625,6 +826,7 @@ static ssize_t sensor_push_event(FAR void *priv, FAR const void *data,
 static void sensor_notify_event(FAR void *priv)
 {
   FAR struct sensor_upperhalf_s *upper = priv;
+  FAR struct sensor_user_s *user;
   int semcount;
 
   if (nxsem_wait(&upper->exclsem) < 0)
@@ -632,11 +834,15 @@ static void sensor_notify_event(FAR void *priv)
       return;
     }
 
-  sensor_pollnotify(upper, POLLIN);
-  nxsem_get_value(&upper->buffersem, &semcount);
-  if (semcount < 1)
+  list_for_every_entry(&upper->userlist, user, struct sensor_user_s, node)
     {
-      nxsem_post(&upper->buffersem);
+      nxsem_get_value(&user->buffersem, &semcount);
+      if (semcount < 1)
+        {
+          nxsem_post(&user->buffersem);
+        }
+
+      sensor_pollnotify_one(user, POLLIN);
     }
 
   nxsem_post(&upper->exclsem);
@@ -737,10 +943,15 @@ int sensor_custom_register(FAR struct sensor_lowerhalf_s *lower,
   upper->lower = lower;
   upper->esize = esize;
 
-  nxsem_init(&upper->exclsem, 0, 1);
-  nxsem_init(&upper->buffersem, 0, 0);
+  list_initialize(&upper->userlist);
+  upper->state.min_interval = ULONG_MAX;
+  upper->state.min_latency = ULONG_MAX;
+  if (lower->ops->activate)
+    {
+      upper->state.nadvertisers = 1;
+    }
 
-  nxsem_set_protocol(&upper->buffersem, SEM_PRIO_NONE);
+  nxsem_init(&upper->exclsem, 0, 1);
 
   /* Bind the lower half data structure member */
 
@@ -772,7 +983,6 @@ int sensor_custom_register(FAR struct sensor_lowerhalf_s *lower,
 
 drv_err:
   nxsem_destroy(&upper->exclsem);
-  nxsem_destroy(&upper->buffersem);
 
   kmm_free(upper);
 
@@ -832,7 +1042,10 @@ void sensor_custom_unregister(FAR struct sensor_lowerhalf_s *lower,
   unregister_driver(path);
 
   nxsem_destroy(&upper->exclsem);
-  nxsem_destroy(&upper->buffersem);
+  if (circbuf_is_init(&upper->buffer))
+    {
+      circbuf_uninit(&upper->buffer);
+    }
 
   kmm_free(upper);
 }
diff --git a/include/nuttx/sensors/ioctl.h b/include/nuttx/sensors/ioctl.h
index 210708f38d..d2cfff813e 100644
--- a/include/nuttx/sensors/ioctl.h
+++ b/include/nuttx/sensors/ioctl.h
@@ -234,12 +234,13 @@
 #define SNIOC_READROMCODE          _SNIOC(0x0067)  /* Arg: uint64_t* pointer */
 #define SNIOC_SETALARM             _SNIOC(0x0068)  /* Arg: struct ds18b20_alarm_s* */
 
-/* Command:      SNIOC_ACTIVATE
- * Description:  Enable or disable sensor
- * Argument:     true or false.
+/* Command:      SNIOC_GET_STATE
+ * Description:  Get state for all subscribers, include min_interval,
+ *               min_latency and the number of subscribers.
+ * Argument:     This is the state pointer
  */
 
-#define SNIOC_ACTIVATE             _SNIOC(0x0080)
+#define SNIOC_GET_STATE            _SNIOC(0x0080)
 
 /* Command:      SNIOC_SET_INTERVAL
  * Description:  Set interval between samples
diff --git a/include/nuttx/sensors/sensor.h b/include/nuttx/sensors/sensor.h
index 6c694be01f..9336ad5028 100644
--- a/include/nuttx/sensors/sensor.h
+++ b/include/nuttx/sensors/sensor.h
@@ -874,6 +874,17 @@ struct sensor_lowerhalf_s
   FAR void *priv;
 };
 
+/* This structure describes the state for the sensor device */
+
+struct sensor_state_s
+{
+  unsigned long min_interval;  /* The minimum subscription interval for sensor, in us */
+  unsigned long min_latency;   /* The minimum batch latency for sensor, in us */
+  unsigned long nsubscribers;  /* The number of subcribers, if subsrciber exists,
+                                * the sensor deivce is enabled */
+  unsigned long nadvertisers;  /* The number of advertisers */
+};
+
 /* This structure describes the register info for the user sensor */
 
 #ifdef CONFIG_USENSOR


[incubator-nuttx] 02/07: driver/sensor: simplify buffer operation

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 8f39c5f11bfc80efd11ac1df44481601a61e7e11
Author: Jiuzhu Dong <do...@xiaomi.com>
AuthorDate: Thu Jul 21 02:32:29 2022 +0000

    driver/sensor: simplify buffer operation
    
    1.Simplify buffer opeations to avoid frequent resize for batch and no-batch mode.
    2.When sensor event is first generated, the buffer is initialized.
    3.Remove and merge batch_number to buffer_number when device support batch mode.
    
    Signed-off-by: Jiuzhu Dong <do...@xiaomi.com>
---
 drivers/sensors/sensor.c       | 60 ++++++++++++++----------------------------
 include/nuttx/sensors/sensor.h | 11 +++-----
 2 files changed, 24 insertions(+), 47 deletions(-)

diff --git a/drivers/sensors/sensor.c b/drivers/sensors/sensor.c
index fa9c241e16..9c8db17081 100644
--- a/drivers/sensors/sensor.c
+++ b/drivers/sensors/sensor.c
@@ -207,17 +207,6 @@ static int sensor_open(FAR struct file *filep)
       ret = -EMFILE;
       goto err;
     }
-  else if (tmp == 1)
-    {
-      /* Initialize sensor buffer */
-
-      ret = circbuf_init(&upper->buffer, NULL, lower->buffer_number *
-                         upper->esize);
-      if (ret < 0)
-        {
-          goto err;
-        }
-    }
 
   upper->crefs = tmp;
 err:
@@ -330,19 +319,6 @@ static ssize_t sensor_read(FAR struct file *filep, FAR char *buffer,
         }
 
       ret = circbuf_read(&upper->buffer, buffer, len);
-
-      /* Release some buffer space when current mode isn't batch mode
-       * and last mode is batch mode, and the number of bytes available
-       * in buffer is less than the number of bytes origin.
-       */
-
-      uint32_t buffer_size = lower->buffer_number * upper->esize;
-      if (upper->latency == 0 &&
-          circbuf_size(&upper->buffer) > buffer_size &&
-          circbuf_used(&upper->buffer) <= buffer_size)
-        {
-          ret = circbuf_resize(&upper->buffer, buffer_size);
-        }
     }
 
 out:
@@ -393,8 +369,6 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
                 {
                   upper->interval = 0;
                   upper->latency = 0;
-                  ret = circbuf_resize(&upper->buffer, lower->buffer_number *
-                                                       upper->esize);
                 }
             }
         }
@@ -444,17 +418,6 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
           if (ret >= 0)
             {
               upper->latency = *val;
-              if (*val != 0)
-                {
-                  /* Adjust length of buffer in batch mode */
-
-                  uint32_t buffer_size = (ROUNDUP(*val, upper->interval) /
-                                         upper->interval +
-                                         lower->buffer_number) *
-                                         upper->esize;
-
-                  ret = circbuf_resize(&upper->buffer, buffer_size);
-                }
             }
         }
         break;
@@ -497,16 +460,19 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
 
       case SNIOC_GET_NEVENTBUF:
         {
-          *val = lower->buffer_number + lower->batch_number;
+          *val = lower->buffer_number;
         }
         break;
 
       case SNIOC_SET_BUFFER_NUMBER:
         {
-          if (arg != 0)
+          if (!circbuf_is_init(&upper->buffer))
             {
               lower->buffer_number = arg;
-              ret = circbuf_resize(&upper->buffer, arg * upper->esize);
+            }
+          else
+            {
+              ret = -EBUSY;
             }
         }
         break;
@@ -617,6 +583,7 @@ static ssize_t sensor_push_event(FAR void *priv, FAR const void *data,
                                  size_t bytes)
 {
   FAR struct sensor_upperhalf_s *upper = priv;
+  FAR struct sensor_lowerhalf_s *lower = upper->lower;
   int semcount;
   int ret;
 
@@ -631,6 +598,19 @@ static ssize_t sensor_push_event(FAR void *priv, FAR const void *data,
       return ret;
     }
 
+  if (!circbuf_is_init(&upper->buffer))
+    {
+      /* Initialize sensor buffer when data is first generated */
+
+      ret = circbuf_init(&upper->buffer, NULL, lower->buffer_number *
+                         upper->esize);
+      if (ret < 0)
+        {
+          nxsem_post(&upper->exclsem);
+          return ret;
+        }
+    }
+
   circbuf_overwrite(&upper->buffer, data, bytes);
   sensor_pollnotify(upper, POLLIN);
   nxsem_get_value(&upper->buffersem, &semcount);
diff --git a/include/nuttx/sensors/sensor.h b/include/nuttx/sensors/sensor.h
index cc4bafbcbf..c6ea2aa049 100644
--- a/include/nuttx/sensors/sensor.h
+++ b/include/nuttx/sensors/sensor.h
@@ -811,17 +811,14 @@ struct sensor_lowerhalf_s
    * is too small, the event will be overwrite before application read them.
    * So, it's recommended to set according to sensor odr. If odr is low, you
    * can set to one. If odr is high, you can set to two or three.
+   *
+   * If device support batch mode, the number of events that hardware fifo
+   * hold maximum number of samples, must be aligned with size of
+   * struct sensor_event_xxx.
    */
 
   uint32_t buffer_number;
 
-  /* The number of events that hardware fifo hold maximum number of samples,
-   * must be aligned with size of struct sensor_event_xxx.
-   * If sensor don't hardware fifo, you don't need to care about fifo_size.
-   */
-
-  uint32_t batch_number;
-
   /* The uncalibrated use to describe whether the sensor event is
    * uncalibrated. True is uncalibrated data, false is calibrated data,
    * default false.


[incubator-nuttx] 04/07: driver/usensor: support register user sensor

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit b3ea6522f4083813c7e010b535e8a1732e59854f
Author: Jiuzhu Dong <do...@xiaomi.com>
AuthorDate: Thu Mar 3 21:40:58 2022 +0800

    driver/usensor: support register user sensor
    
    1.cmd:SNIOC_REGISTER with struct sensor_reginfo_s to register user sensor
    2.cmd:SNIOC_UNREGISTER to unregister user sensor
    
    Signed-off-by: Jiuzhu Dong <do...@xiaomi.com>
---
 drivers/sensors/Kconfig        |   6 +
 drivers/sensors/Make.defs      |   4 +
 drivers/sensors/usensor.c      | 249 +++++++++++++++++++++++++++++++++++++++++
 include/nuttx/sensors/ioctl.h  |  20 ++++
 include/nuttx/sensors/sensor.h |  25 +++++
 5 files changed, 304 insertions(+)

diff --git a/drivers/sensors/Kconfig b/drivers/sensors/Kconfig
index f6e9259f90..d4d50e8d87 100644
--- a/drivers/sensors/Kconfig
+++ b/drivers/sensors/Kconfig
@@ -19,6 +19,12 @@ config SENSORS_NPOLLWAITERS
 		Maximum number of threads than can be waiting for POLL events.
 		Default: 2
 
+config USENSOR
+	bool "Usensor Device Support"
+	default n
+	---help---
+		Allow application to register user sensor by /dev/usensor.
+
 config SENSORS_WTGAHRS2
 	bool "Wtgahrs2 Sensor Support"
 	default n
diff --git a/drivers/sensors/Make.defs b/drivers/sensors/Make.defs
index 70277a90c3..f372b78ec2 100644
--- a/drivers/sensors/Make.defs
+++ b/drivers/sensors/Make.defs
@@ -24,6 +24,10 @@ ifeq ($(CONFIG_SENSORS),y)
 
 CSRCS += sensor.c
 
+ifeq ($(CONFIG_USENSOR),y)
+CSRCS += usensor.c
+endif
+
 ifeq ($(CONFIG_SENSORS_WTGAHRS2),y)
   CSRCS += wtgahrs2.c
 endif
diff --git a/drivers/sensors/usensor.c b/drivers/sensors/usensor.c
new file mode 100644
index 0000000000..a32a97e3f4
--- /dev/null
+++ b/drivers/sensors/usensor.c
@@ -0,0 +1,249 @@
+/****************************************************************************
+ * drivers/sensors/usensor.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/list.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/sensors/sensor.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define USENSOR_PATH       "/dev/usensor"
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int usensor_open(FAR struct file *filep);
+static int usensor_close(FAR struct file *filep);
+static ssize_t usensor_read(FAR struct file *filep, FAR char *buffer,
+                            size_t buflen);
+static ssize_t usensor_write(FAR struct file *filep, FAR const char *buffer,
+                             size_t buflen);
+static int usensor_ioctl(FAR struct file *filep, int cmd,
+                         unsigned long arg);
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* This structure describes context of usensor */
+
+struct usensor_context_s
+{
+  sem_t            exclsem;   /* Manages exclusive access to file operations */
+  struct list_node list;      /* List of node registered */
+};
+
+/* This structure describes lowerhalf driver for an usensor registered */
+
+struct usensor_lowerhalf_s
+{
+  struct list_node          node;    /* node in all usensor list */
+  struct sensor_lowerhalf_s driver;  /* The lowerhalf driver of user sensor */
+  char                      path[1]; /* The path of usensor character device */
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct file_operations g_usensor_fops =
+{
+  usensor_open,  /* open  */
+  usensor_close, /* close */
+  usensor_read,  /* read  */
+  usensor_write, /* write */
+  NULL,          /* seek  */
+  usensor_ioctl, /* ioctl */
+  NULL,          /* poll  */
+};
+
+static const struct sensor_ops_s g_usensor_ops =
+{
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int usensor_register(FAR struct usensor_context_s *usensor,
+                            FAR const struct sensor_reginfo_s *info)
+{
+  FAR struct usensor_lowerhalf_s *lower;
+  int ret;
+
+  lower = kmm_zalloc(sizeof(*lower) + strlen(info->path));
+  if (!lower)
+    {
+      return -ENOMEM;
+    }
+
+  lower->driver.buffer_number = info->nqueue;
+  lower->driver.ops = &g_usensor_ops;
+  strcpy(lower->path, info->path);
+  ret = sensor_custom_register(&lower->driver, lower->path, info->esize);
+  if (ret < 0)
+    {
+      goto errout_with_lower;
+    }
+
+  ret = nxsem_wait(&usensor->exclsem);
+  if (ret < 0)
+    {
+      goto errout_with_register;
+    }
+
+  list_add_tail(&usensor->list, &lower->node);
+  nxsem_post(&usensor->exclsem);
+
+  return ret;
+
+errout_with_register:
+  sensor_custom_unregister(&lower->driver, info->path);
+errout_with_lower:
+  kmm_free(lower);
+  return ret;
+}
+
+static int usensor_unregister(FAR struct usensor_context_s *usensor,
+                              FAR const char *path)
+{
+  FAR struct usensor_lowerhalf_s *lower;
+  int ret;
+
+  ret = nxsem_wait(&usensor->exclsem);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  list_for_every_entry(&usensor->list, lower, struct usensor_lowerhalf_s,
+                       node)
+    {
+      if (strcmp(path, lower->path) == 0)
+        {
+          list_delete(&lower->node);
+          nxsem_post(&usensor->exclsem);
+          sensor_custom_unregister(&lower->driver, path);
+          kmm_free(lower);
+          return 0;
+        }
+    }
+
+  nxsem_post(&usensor->exclsem);
+  return -ENOENT;
+}
+
+static int usensor_open(FAR struct file *filep)
+{
+  return 0;
+}
+
+static int usensor_close(FAR struct file *filep)
+{
+  return 0;
+}
+
+static ssize_t usensor_write(FAR struct file *filep, FAR const char *buffer,
+                             size_t buflen)
+{
+  return buflen;
+}
+
+static ssize_t usensor_read(FAR struct file *filep, FAR char *buffer,
+                            size_t buflen)
+{
+  return buflen;
+}
+
+static int usensor_ioctl(FAR struct file *filep, int cmd,
+                         unsigned long arg)
+{
+  FAR struct inode *inode = filep->f_inode;
+  FAR struct usensor_context_s *usensor = inode->i_private;
+  int ret = -ENOTTY;
+
+  if (cmd == SNIOC_REGISTER)
+    {
+      FAR const struct sensor_reginfo_s *info;
+
+      info = (FAR const struct sensor_reginfo_s *)(uintptr_t)arg;
+      ret = usensor_register(usensor, info);
+    }
+  else if (cmd == SNIOC_UNREGISTER)
+    {
+      FAR const char *path;
+
+      path = (FAR const char *)(uintptr_t)arg;
+      ret = usensor_unregister(usensor, path);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: usensor_initialize
+ *
+ * Description:
+ *   This function registers usensor character node "/dev/usensor", so that
+ *   application can register user sensor by this node. The node will
+ *   manager all user sensors in this character dirver.
+ ****************************************************************************/
+
+int usensor_initialize(void)
+{
+  FAR struct usensor_context_s *usensor;
+  int ret;
+
+  usensor = kmm_zalloc(sizeof(*usensor));
+  if (!usensor)
+    {
+      return -ENOMEM;
+    }
+
+  nxsem_init(&usensor->exclsem, 0, 1);
+  list_initialize(&usensor->list);
+
+  ret = register_driver(USENSOR_PATH, &g_usensor_fops, 0666, usensor);
+  if (ret < 0)
+    {
+      goto errout_with_sem;
+    }
+
+  return ret;
+
+errout_with_sem:
+  nxsem_destroy(&usensor->exclsem);
+  kmm_free(usensor);
+  return ret;
+}
diff --git a/include/nuttx/sensors/ioctl.h b/include/nuttx/sensors/ioctl.h
index ff9d76679e..210708f38d 100644
--- a/include/nuttx/sensors/ioctl.h
+++ b/include/nuttx/sensors/ioctl.h
@@ -315,4 +315,24 @@
 
 #define SNIOC_CALIBRATE            _SNIOC(0x0088)
 
+#ifdef CONFIG_USENSOR
+/* Command:      SNIOC_REGISTER
+ * Description:  Register user sensor.
+ * Argument:     A pointer of structure sensor_reginfo_s for user sensor.
+ * Note:         If register is failed, return errno, otherwise,
+ *               return OK.
+ */
+
+#define SNIOC_REGISTER             _SNIOC(0x0089)
+
+/* Command:      SNIOC_UNREGISTER
+ * Description:  Unregister user sensor.
+ * Argument:     The path name of user sensor character node.
+ * Note:         If register is failed, return errno, otherwise,
+ *               return OK.
+ */
+
+#define SNIOC_UNREGISTER           _SNIOC(0x0090)
+#endif
+
 #endif /* __INCLUDE_NUTTX_SENSORS_IOCTL_H */
diff --git a/include/nuttx/sensors/sensor.h b/include/nuttx/sensors/sensor.h
index 0285144904..6c694be01f 100644
--- a/include/nuttx/sensors/sensor.h
+++ b/include/nuttx/sensors/sensor.h
@@ -874,6 +874,17 @@ struct sensor_lowerhalf_s
   FAR void *priv;
 };
 
+/* This structure describes the register info for the user sensor */
+
+#ifdef CONFIG_USENSOR
+struct sensor_reginfo_s
+{
+  FAR const char *path; /* The path of user sensor */
+  uint16_t esize;       /* The element size of user sensor */
+  uint32_t nqueue;      /* The number of queue buffered elements */
+};
+#endif
+
 /****************************************************************************
  * Public Function Prototypes
  ****************************************************************************/
@@ -978,6 +989,20 @@ void sensor_unregister(FAR struct sensor_lowerhalf_s *dev, int devno);
 
 void sensor_custom_unregister(FAR struct sensor_lowerhalf_s *dev,
                               FAR const char *path);
+
+/****************************************************************************
+ * Name: usensor_initialize
+ *
+ * Description:
+ *   This function registers usensor character node "/dev/usensor", so that
+ *   application can register user sensor by this node. The node will
+ *   manager all user sensor in this character dirver.
+ ****************************************************************************/
+
+#ifdef CONFIG_USENSOR
+int usensor_initialize(void);
+#endif
+
 #undef EXTERN
 #if defined(__cplusplus)
 }


[incubator-nuttx] 01/07: driver/sensor: support userspace wirte data into sensor device

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit dad5ab75ff4d483020bee77c6dd952e33ef07802
Author: Jiuzhu Dong <do...@xiaomi.com>
AuthorDate: Fri Mar 11 17:05:47 2022 +0800

    driver/sensor: support userspace wirte data into sensor device
    
    Signed-off-by: Jiuzhu Dong <do...@xiaomi.com>
---
 drivers/sensors/sensor.c       | 31 ++++++++++++++++++++++++++-----
 include/nuttx/sensors/sensor.h | 12 ++++++++----
 2 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/drivers/sensors/sensor.c b/drivers/sensors/sensor.c
index 670302c6f9..fa9c241e16 100644
--- a/drivers/sensors/sensor.c
+++ b/drivers/sensors/sensor.c
@@ -89,10 +89,14 @@ static int     sensor_open(FAR struct file *filep);
 static int     sensor_close(FAR struct file *filep);
 static ssize_t sensor_read(FAR struct file *filep, FAR char *buffer,
                            size_t buflen);
+static ssize_t sensor_write(FAR struct file *filep, FAR const char *buffer,
+                            size_t buflen);
 static int     sensor_ioctl(FAR struct file *filep, int cmd,
                             unsigned long arg);
 static int     sensor_poll(FAR struct file *filep, FAR struct pollfd *fds,
                            bool setup);
+static ssize_t sensor_push_event(FAR void *priv, FAR const void *data,
+                                 size_t bytes);
 
 /****************************************************************************
  * Private Data
@@ -140,7 +144,7 @@ static const struct file_operations g_sensor_fops =
   sensor_open,    /* open  */
   sensor_close,   /* close */
   sensor_read,    /* read  */
-  NULL,           /* write */
+  sensor_write,   /* write */
   NULL,           /* seek  */
   sensor_ioctl,   /* ioctl */
   sensor_poll     /* poll  */
@@ -346,6 +350,15 @@ out:
   return ret;
 }
 
+static ssize_t sensor_write(FAR struct file *filep, FAR const char *buffer,
+                            size_t buflen)
+{
+  FAR struct inode *inode = filep->f_inode;
+  FAR struct sensor_upperhalf_s *upper = inode->i_private;
+
+  return sensor_push_event(upper, buffer, buflen);
+}
+
 static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
 {
   FAR struct inode *inode = filep->f_inode;
@@ -600,15 +613,22 @@ errout:
   return ret;
 }
 
-static void sensor_push_event(FAR void *priv, FAR const void *data,
-                              size_t bytes)
+static ssize_t sensor_push_event(FAR void *priv, FAR const void *data,
+                                 size_t bytes)
 {
   FAR struct sensor_upperhalf_s *upper = priv;
   int semcount;
+  int ret;
 
-  if (!bytes || nxsem_wait(&upper->exclsem) < 0)
+  if (!bytes)
     {
-      return;
+      return -EINVAL;
+    }
+
+  ret = nxsem_wait(&upper->exclsem);
+  if (ret < 0)
+    {
+      return ret;
     }
 
   circbuf_overwrite(&upper->buffer, data, bytes);
@@ -620,6 +640,7 @@ static void sensor_push_event(FAR void *priv, FAR const void *data,
     }
 
   nxsem_post(&upper->exclsem);
+  return bytes;
 }
 
 static void sensor_notify_event(FAR void *priv)
diff --git a/include/nuttx/sensors/sensor.h b/include/nuttx/sensors/sensor.h
index a0298d4bd4..cc4bafbcbf 100644
--- a/include/nuttx/sensors/sensor.h
+++ b/include/nuttx/sensors/sensor.h
@@ -843,13 +843,17 @@ struct sensor_lowerhalf_s
        *   It is provided by upper half driver to lower half driver.
        *
        * Input Parameters:
-       *   priv   - Upper half driver handle
+       *   priv   - Upper half driver handle.
        *   data   - The buffer of event, it can be all type of sensor events.
-       *   bytes  - The number of bytes of sensor event
+       *   bytes  - The number of bytes of sensor event.
+       *
+       * Returned Value:
+       *   The bytes of push is returned when success;
+       *   A negated errno value is returned on any failure.
        **********************************************************************/
 
-      CODE void (*push_event)(FAR void *priv, FAR const void *data,
-                              size_t bytes);
+      CODE ssize_t (*push_event)(FAR void *priv, FAR const void *data,
+                                 size_t bytes);
 
       /**********************************************************************
        * Name: notify_event


[incubator-nuttx] 06/07: driver/sensor: update sensor data structure and state structure

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit e3e59a03b148fbb1dab84e74761b915c19961b99
Author: Jiuzhu Dong <do...@xiaomi.com>
AuthorDate: Wed Mar 23 17:09:46 2022 +0800

    driver/sensor: update sensor data structure and state structure
    
    Signed-off-by: Jiuzhu Dong <do...@xiaomi.com>
---
 drivers/sensors/sensor.c       | 136 +++++++++++++++++++++--------------------
 drivers/sensors/usensor.c      |   2 +-
 include/nuttx/sensors/ioctl.h  |  18 ++----
 include/nuttx/sensors/sensor.h |  85 +++++++++++++-------------
 4 files changed, 118 insertions(+), 123 deletions(-)

diff --git a/drivers/sensors/sensor.c b/drivers/sensors/sensor.c
index 5576ec8fb1..c1587c7f3f 100644
--- a/drivers/sensors/sensor.c
+++ b/drivers/sensors/sensor.c
@@ -58,7 +58,7 @@
 
 struct sensor_info_s
 {
-  uint8_t   esize;
+  unsigned long esize;
   FAR char *name;
 };
 
@@ -82,7 +82,7 @@ struct sensor_user_s
    * appear in dual role
    */
 
-  size_t           generation; /* Last generation subscriber has seen */
+  unsigned long    generation; /* Last generation subscriber has seen */
   unsigned long    interval;   /* The interval for subscriber */
   unsigned long    latency;    /* The bactch latency for subscriber */
 };
@@ -94,9 +94,7 @@ struct sensor_upperhalf_s
   FAR struct sensor_lowerhalf_s *lower;  /* The handle of lower half driver */
   struct sensor_state_s          state;  /* The state of sensor device */
   struct circbuf_s   buffer;             /* The circular buffer of sensor device */
-  uint8_t            esize;              /* The element size of circular buffer */
   sem_t              exclsem;            /* Manages exclusive access to file operations */
-  size_t             generation;         /* The current generation count */
   struct list_node   userlist;           /* List of users */
 };
 
@@ -125,39 +123,39 @@ static ssize_t sensor_push_event(FAR void *priv, FAR const void *data,
 
 static const struct sensor_info_s g_sensor_info[] =
 {
-  {0,                                           NULL},
-  {sizeof(struct sensor_event_accel),           "accel"},
-  {sizeof(struct sensor_event_mag),             "mag"},
-  {sizeof(struct sensor_event_gyro),            "gyro"},
-  {sizeof(struct sensor_event_light),           "light"},
-  {sizeof(struct sensor_event_baro),            "baro"},
-  {sizeof(struct sensor_event_prox),            "prox"},
-  {sizeof(struct sensor_event_humi),            "humi"},
-  {sizeof(struct sensor_event_temp),            "temp"},
-  {sizeof(struct sensor_event_rgb),             "rgb"},
-  {sizeof(struct sensor_event_hall),            "hall"},
-  {sizeof(struct sensor_event_ir),              "ir"},
-  {sizeof(struct sensor_event_gps),             "gps"},
-  {sizeof(struct sensor_event_uv),              "uv"},
-  {sizeof(struct sensor_event_noise),           "noise"},
-  {sizeof(struct sensor_event_pm25),            "pm25"},
-  {sizeof(struct sensor_event_pm1p0),           "pm1p0"},
-  {sizeof(struct sensor_event_pm10),            "pm10"},
-  {sizeof(struct sensor_event_co2),             "co2"},
-  {sizeof(struct sensor_event_hcho),            "hcho"},
-  {sizeof(struct sensor_event_tvoc),            "tvoc"},
-  {sizeof(struct sensor_event_ph),              "ph"},
-  {sizeof(struct sensor_event_dust),            "dust"},
-  {sizeof(struct sensor_event_hrate),           "hrate"},
-  {sizeof(struct sensor_event_hbeat),           "hbeat"},
-  {sizeof(struct sensor_event_ecg),             "ecg"},
-  {sizeof(struct sensor_event_ppgd),            "ppgd"},
-  {sizeof(struct sensor_event_ppgq),            "ppgq"},
-  {sizeof(struct sensor_event_impd),            "impd"},
-  {sizeof(struct sensor_event_ots),             "ots"},
-  {sizeof(struct sensor_event_gps_satellite),   "gps_satellite"},
-  {sizeof(struct sensor_event_wake_gesture),    "wake_gesture"},
-  {sizeof(struct sensor_event_cap),             "cap"},
+  {0,                                     NULL},
+  {sizeof(struct sensor_accel),           "accel"},
+  {sizeof(struct sensor_mag),             "mag"},
+  {sizeof(struct sensor_gyro),            "gyro"},
+  {sizeof(struct sensor_light),           "light"},
+  {sizeof(struct sensor_baro),            "baro"},
+  {sizeof(struct sensor_prox),            "prox"},
+  {sizeof(struct sensor_humi),            "humi"},
+  {sizeof(struct sensor_temp),            "temp"},
+  {sizeof(struct sensor_rgb),             "rgb"},
+  {sizeof(struct sensor_hall),            "hall"},
+  {sizeof(struct sensor_ir),              "ir"},
+  {sizeof(struct sensor_gps),             "gps"},
+  {sizeof(struct sensor_uv),              "uv"},
+  {sizeof(struct sensor_noise),           "noise"},
+  {sizeof(struct sensor_pm25),            "pm25"},
+  {sizeof(struct sensor_pm1p0),           "pm1p0"},
+  {sizeof(struct sensor_pm10),            "pm10"},
+  {sizeof(struct sensor_co2),             "co2"},
+  {sizeof(struct sensor_hcho),            "hcho"},
+  {sizeof(struct sensor_tvoc),            "tvoc"},
+  {sizeof(struct sensor_ph),              "ph"},
+  {sizeof(struct sensor_dust),            "dust"},
+  {sizeof(struct sensor_hrate),           "hrate"},
+  {sizeof(struct sensor_hbeat),           "hbeat"},
+  {sizeof(struct sensor_ecg),             "ecg"},
+  {sizeof(struct sensor_ppgd),            "ppgd"},
+  {sizeof(struct sensor_ppgq),            "ppgq"},
+  {sizeof(struct sensor_impd),            "impd"},
+  {sizeof(struct sensor_ots),             "ots"},
+  {sizeof(struct sensor_gps_satellite),   "gps_satellite"},
+  {sizeof(struct sensor_wake_gesture),    "wake_gesture"},
+  {sizeof(struct sensor_cap),             "cap"},
 };
 
 static const struct file_operations g_sensor_fops =
@@ -178,7 +176,8 @@ static const struct file_operations g_sensor_fops =
  * Private Functions
  ****************************************************************************/
 
-static bool sensor_in_range(size_t left, size_t value, size_t right)
+static bool sensor_in_range(unsigned long left, unsigned long value,
+                            unsigned long right)
 {
   if (left < right)
     {
@@ -192,7 +191,8 @@ static bool sensor_in_range(size_t left, size_t value, size_t right)
     }
 }
 
-static bool sensor_is_updated(size_t generation, size_t ugeneration)
+static bool sensor_is_updated(unsigned long generation,
+                              unsigned long ugeneration)
 {
   return generation > ugeneration;
 }
@@ -390,7 +390,7 @@ static int sensor_open(FAR struct file *filep)
 
   user->interval   = ULONG_MAX;
   user->latency    = ULONG_MAX;
-  user->generation = upper->generation;
+  user->generation = upper->state.generation;
   nxsem_init(&user->buffersem, 0, 0);
   nxsem_set_protocol(&user->buffersem, SEM_PRIO_NONE);
   list_add_tail(&upper->userlist, &user->node);
@@ -458,8 +458,8 @@ static ssize_t sensor_read(FAR struct file *filep, FAR char *buffer,
   FAR struct sensor_upperhalf_s *upper = inode->i_private;
   FAR struct sensor_lowerhalf_s *lower = upper->lower;
   FAR struct sensor_user_s *user = filep->f_priv;
+  unsigned long nums;
   ssize_t ret;
-  size_t nums;
 
   if (!buffer || !len)
     {
@@ -534,7 +534,7 @@ static ssize_t sensor_read(FAR struct file *filep, FAR char *buffer,
        * new data.
        */
 
-      if (user->generation == upper->generation)
+      if (user->generation == upper->state.generation)
         {
           user->generation--;
         }
@@ -543,21 +543,22 @@ static ssize_t sensor_read(FAR struct file *filep, FAR char *buffer,
        * oldest data in circbuffer are returned to the users.
        */
 
-      else if (!sensor_in_range(upper->generation - lower->buffer_number,
-                                user->generation, upper->generation))
+      else if (!sensor_in_range(upper->state.generation - lower->nbuffer,
+                                user->generation, upper->state.generation))
 
         {
-          user->generation = upper->generation - lower->buffer_number;
+          user->generation = upper->state.generation - lower->nbuffer;
         }
 
-      nums = upper->generation - user->generation;
-      if (len < nums * upper->esize)
+      nums = upper->state.generation - user->generation;
+      if (len < nums * upper->state.esize)
         {
-          nums = len / upper->esize;
+          nums = len / upper->state.esize;
         }
 
-      len = nums * upper->esize;
-      ret = circbuf_peekat(&upper->buffer, user->generation * upper->esize,
+      len = nums * upper->state.esize;
+      ret = circbuf_peekat(&upper->buffer,
+                           user->generation * upper->state.esize,
                            buffer, len);
       user->generation += nums;
     }
@@ -650,9 +651,9 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
         }
         break;
 
-      case SNIOC_GET_NEVENTBUF:
+      case SNIOC_SET_USERPRIV:
         {
-          *(FAR uint32_t *)(uintptr_t)arg = lower->buffer_number;
+          upper->state.priv = (FAR void *)(uintptr_t)arg;
         }
         break;
 
@@ -660,9 +661,9 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
         {
           if (!circbuf_is_init(&upper->buffer))
             {
-              if (arg >= lower->buffer_number)
+              if (arg >= lower->nbuffer)
                 {
-                  lower->buffer_number = arg;
+                  lower->nbuffer = arg;
                 }
               else
                 {
@@ -742,7 +743,7 @@ static int sensor_poll(FAR struct file *filep,
                 }
             }
         }
-      else if (sensor_is_updated(upper->generation, user->generation))
+      else if (sensor_is_updated(upper->state.generation, user->generation))
         {
           eventset |= (fds->events & POLLIN);
         }
@@ -774,12 +775,12 @@ static ssize_t sensor_push_event(FAR void *priv, FAR const void *data,
   FAR struct sensor_upperhalf_s *upper = priv;
   FAR struct sensor_lowerhalf_s *lower = upper->lower;
   FAR struct sensor_user_s *user;
-  size_t envcount;
+  unsigned long envcount;
   int semcount;
   int ret;
 
-  envcount = bytes / upper->esize;
-  if (!envcount || bytes != envcount * upper->esize)
+  envcount = bytes / upper->state.esize;
+  if (!envcount || bytes != envcount * upper->state.esize)
     {
       return -EINVAL;
     }
@@ -794,8 +795,8 @@ static ssize_t sensor_push_event(FAR void *priv, FAR const void *data,
     {
       /* Initialize sensor buffer when data is first generated */
 
-      ret = circbuf_init(&upper->buffer, NULL, lower->buffer_number *
-                         upper->esize);
+      ret = circbuf_init(&upper->buffer, NULL, lower->nbuffer *
+                         upper->state.esize);
       if (ret < 0)
         {
           nxsem_post(&upper->exclsem);
@@ -804,10 +805,10 @@ static ssize_t sensor_push_event(FAR void *priv, FAR const void *data,
     }
 
   circbuf_overwrite(&upper->buffer, data, bytes);
-  upper->generation += envcount;
+  upper->state.generation += envcount;
   list_for_every_entry(&upper->userlist, user, struct sensor_user_s, node)
     {
-      if (sensor_is_updated(upper->generation, user->generation))
+      if (sensor_is_updated(upper->state.generation, user->generation))
         {
           nxsem_get_value(&user->buffersem, &semcount);
           if (semcount < 1)
@@ -916,7 +917,7 @@ int sensor_register(FAR struct sensor_lowerhalf_s *lower, int devno)
  ****************************************************************************/
 
 int sensor_custom_register(FAR struct sensor_lowerhalf_s *lower,
-                           FAR const char *path, uint8_t esize)
+                           FAR const char *path, unsigned long esize)
 {
   FAR struct sensor_upperhalf_s *upper;
   int ret = -EINVAL;
@@ -941,9 +942,9 @@ int sensor_custom_register(FAR struct sensor_lowerhalf_s *lower,
   /* Initialize the upper-half data structure */
 
   upper->lower = lower;
-  upper->esize = esize;
 
   list_initialize(&upper->userlist);
+  upper->state.esize = esize;
   upper->state.min_interval = ULONG_MAX;
   upper->state.min_latency = ULONG_MAX;
   if (lower->ops->activate)
@@ -959,9 +960,9 @@ int sensor_custom_register(FAR struct sensor_lowerhalf_s *lower,
 
   if (!lower->ops->fetch)
     {
-      if (!lower->buffer_number)
+      if (!lower->nbuffer)
         {
-          lower->buffer_number = 1;
+          lower->nbuffer = 1;
         }
 
       lower->push_event = sensor_push_event;
@@ -969,9 +970,10 @@ int sensor_custom_register(FAR struct sensor_lowerhalf_s *lower,
   else
     {
       lower->notify_event = sensor_notify_event;
-      lower->buffer_number = 0;
+      lower->nbuffer = 0;
     }
 
+  upper->state.nbuffer = lower->nbuffer;
   sninfo("Registering %s\n", path);
   ret = register_driver(path, &g_sensor_fops, 0666, upper);
   if (ret)
diff --git a/drivers/sensors/usensor.c b/drivers/sensors/usensor.c
index a32a97e3f4..ef203b51f4 100644
--- a/drivers/sensors/usensor.c
+++ b/drivers/sensors/usensor.c
@@ -104,7 +104,7 @@ static int usensor_register(FAR struct usensor_context_s *usensor,
       return -ENOMEM;
     }
 
-  lower->driver.buffer_number = info->nqueue;
+  lower->driver.nbuffer = info->nbuffer;
   lower->driver.ops = &g_usensor_ops;
   strcpy(lower->path, info->path);
   ret = sensor_custom_register(&lower->driver, lower->path, info->esize);
diff --git a/include/nuttx/sensors/ioctl.h b/include/nuttx/sensors/ioctl.h
index d2cfff813e..7cee985204 100644
--- a/include/nuttx/sensors/ioctl.h
+++ b/include/nuttx/sensors/ioctl.h
@@ -256,22 +256,12 @@
 
 #define SNIOC_BATCH                _SNIOC(0x0082)
 
-/* Command:      SNIOC_GET_NEVENTBUF
- * Description:  the number of sensor events that sensor buffer of upper half
- *               holds.
- * Argument:     This is the number of events pointer, is output parameter.
- * Note:         Tell the application layer number of sensor events in sensor
- *               buffer.
- *               This buffer is used to solve the problem that the
- *               application layer can't read the sensor event in time.
- *               Recommend the number of sensor events in application layer's
- *               buffer is same as result by call this function.
- *               This is number of sensor events rather than the length of
- *               buffer.
- *               See sensor.h(struct sensor_lower_half_s buffer_bytes).
+/* Command:      SNIOC_SET_USERPRIV
+ * Description:  Set the private data of userspace user.
+ * Argument:     This is the pointer of private data.
  */
 
-#define SNIOC_GET_NEVENTBUF        _SNIOC(0x0083)
+#define SNIOC_SET_USERPRIV         _SNIOC(0x0083)
 
 /* Command:      SNIOC_SET_BUFFER_NUMBER
  * Description:  Set the number of events intermediate circualr buffer can
diff --git a/include/nuttx/sensors/sensor.h b/include/nuttx/sensors/sensor.h
index 9336ad5028..9be32a3268 100644
--- a/include/nuttx/sensors/sensor.h
+++ b/include/nuttx/sensors/sensor.h
@@ -313,7 +313,7 @@ static inline uint64_t sensor_get_timestamp(void)
  * reported.
  */
 
-struct sensor_event_accel   /* Type: Accerometer */
+struct sensor_accel         /* Type: Accerometer */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float x;                  /* Axis X in m/s^2 */
@@ -322,7 +322,7 @@ struct sensor_event_accel   /* Type: Accerometer */
   float temperature;        /* Temperature in degrees celsius */
 };
 
-struct sensor_event_gyro    /* Type: Gyroscope */
+struct sensor_gyro          /* Type: Gyroscope */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float x;                  /* Axis X in rad/s */
@@ -331,7 +331,7 @@ struct sensor_event_gyro    /* Type: Gyroscope */
   float temperature;        /* Temperature in degrees celsius */
 };
 
-struct sensor_event_mag     /* Type: Magnetic Field */
+struct sensor_mag           /* Type: Magnetic Field */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float x;                  /* Axis X in Gauss or micro Tesla (uT) */
@@ -340,38 +340,38 @@ struct sensor_event_mag     /* Type: Magnetic Field */
   float temperature;        /* Temperature in degrees celsius */
 };
 
-struct sensor_event_baro    /* Type: Barometer */
+struct sensor_baro          /* Type: Barometer */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float pressure;           /* pressure measurement in millibar or hpa */
   float temperature;        /* Temperature in degrees celsius */
 };
 
-struct sensor_event_prox    /* Type: proximity */
+struct sensor_prox          /* Type: proximity */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float proximity;          /* distance to the nearest object in centimeters */
 };
 
-struct sensor_event_light   /* Type: Light */
+struct sensor_light         /* Type: Light */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float light;              /* in SI lux units */
 };
 
-struct sensor_event_humi    /* Type: Relative Humidity */
+struct sensor_humi          /* Type: Relative Humidity */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float humidity;           /* in percent  */
 };
 
-struct sensor_event_temp    /* Type: Ambient Temperature */
+struct sensor_temp          /* Type: Ambient Temperature */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float temperature;        /* Temperature in degrees celsius */
 };
 
-struct sensor_event_rgb     /* Type: RGB */
+struct sensor_rgb           /* Type: RGB */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float r;                  /* Units is percent */
@@ -379,19 +379,19 @@ struct sensor_event_rgb     /* Type: RGB */
   float b;                  /* Units is percent */
 };
 
-struct sensor_event_hall    /* Type: HALL */
+struct sensor_hall          /* Type: HALL */
 {
   uint64_t timestamp;       /* Units is microseconds */
   bool hall;                /* Boolean type */
 };
 
-struct sensor_event_ir      /* Type: Infrared Ray */
+struct sensor_ir            /* Type: Infrared Ray */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float ir;                 /* in SI units lux */
 };
 
-struct sensor_event_gps     /* Type: Gps */
+struct sensor_gps           /* Type: Gps */
 {
   uint64_t timestamp;       /* Time since system start, Units is microseconds */
 
@@ -424,85 +424,85 @@ struct sensor_event_gps     /* Type: Gps */
   uint32_t satellites_used; /* Number of satellites used */
 };
 
-struct sensor_event_uv      /* Type: Ultraviolet Light */
+struct sensor_uv            /* Type: Ultraviolet Light */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float uvi;                /* the value range is 0 - 15 */
 };
 
-struct sensor_event_noise   /* Type: Noise Loudness */
+struct sensor_noise         /* Type: Noise Loudness */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float db;                 /* in SI units db */
 };
 
-struct sensor_event_pm25    /* Type: PM25 */
+struct sensor_pm25          /* Type: PM25 */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float pm25;               /* in SI units ug/m^3 */
 };
 
-struct sensor_event_pm10    /* Type: PM10 */
+struct sensor_pm10          /* Type: PM10 */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float pm10;               /* in SI units ug/m^3 */
 };
 
-struct sensor_event_pm1p0   /* Type: PM1P0 */
+struct sensor_pm1p0         /* Type: PM1P0 */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float pm1p0;              /* in SI units ug/m^3 */
 };
 
-struct sensor_event_co2     /* Type: CO2 */
+struct sensor_co2           /* Type: CO2 */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float co2;                /* in SI units ppm */
 };
 
-struct sensor_event_hcho    /* Type: HCHO */
+struct sensor_hcho          /* Type: HCHO */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float hcho;               /* in SI units ppm */
 };
 
-struct sensor_event_tvoc    /* Type: TVOC */
+struct sensor_tvoc          /* Type: TVOC */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float tvoc;               /* in SI units ppm */
 };
 
-struct sensor_event_ph      /* Type: PH */
+struct sensor_ph            /* Type: PH */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float ph;                 /* PH = 7.0 neutral, PH < 7.0 acidic, PH > 7.0 alkaline */
 };
 
-struct sensor_event_dust    /* Type: DUST */
+struct sensor_dust          /* Type: DUST */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float dust;               /* is SI units ug/m^3 */
 };
 
-struct sensor_event_hrate   /* Type: Heart Rate */
+struct sensor_hrate         /* Type: Heart Rate */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float bpm;                /* is SI units BPM */
 };
 
-struct sensor_event_hbeat   /* Type: Heart Beat */
+struct sensor_hbeat         /* Type: Heart Beat */
 {
   uint64_t timestamp;       /* Units is microseconds */
   float beat;               /* Units is times/minutes */
 };
 
-struct sensor_event_ecg     /* Type: ECG */
+struct sensor_ecg           /* Type: ECG */
 {
   uint64_t timestamp;       /* Unit is microseconds */
   float ecg;                /* Unit is μV */
 };
 
-struct sensor_event_ppgd    /* Type: PPGD */
+struct sensor_ppgd          /* Type: PPGD */
 {
   uint64_t timestamp;       /* Unit is microseconds */
   uint32_t ppg[2];          /* PPG from 2 channels. Units are ADC counts. */
@@ -510,7 +510,7 @@ struct sensor_event_ppgd    /* Type: PPGD */
   uint16_t gain[2];         /* ADC gains of channels. Units are V/V or V/A. */
 };
 
-struct sensor_event_ppgq    /* Type: PPDQ */
+struct sensor_ppgq          /* Type: PPDQ */
 {
   uint64_t timestamp;       /* Unit is microseconds */
   uint32_t ppg[4];          /* PPG from 4 channels. Units are ADC counts. */
@@ -518,21 +518,21 @@ struct sensor_event_ppgq    /* Type: PPDQ */
   uint16_t gain[4];         /* ADC gains of channels. Units are V/V or V/A. */
 };
 
-struct sensor_event_impd    /* Type: Impedance */
+struct sensor_impd          /* Type: Impedance */
 {
   uint64_t timestamp;       /* Unit is microseconds */
   float real;               /* Real part, unit is Ohm(Ω) */
   float imag;               /* Imaginary part, unit is Ohm(Ω) */
 };
 
-struct sensor_event_ots     /* Type: OTS */
+struct sensor_ots           /* Type: OTS */
 {
   uint64_t timestamp;       /* Unit is microseconds */
   int32_t x;                /* Axis X in counts */
   int32_t y;                /* Axis Y in counts */
 };
 
-struct sensor_event_gps_satellite
+struct sensor_gps_satellite
 {
   uint64_t timestamp;       /* Time since system start, Units is microseconds */
   uint32_t count;           /* Total number of messages of satellites visible */
@@ -561,7 +561,7 @@ struct sensor_event_gps_satellite
   info[4];
 };
 
-struct sensor_event_wake_gesture     /* Type: Wake gesture */
+struct sensor_wake_gesture  /* Type: Wake gesture */
 {
   uint64_t timestamp;                /* Units is microseconds */
 
@@ -572,7 +572,7 @@ struct sensor_event_wake_gesture     /* Type: Wake gesture */
   uint32_t event;
 };
 
-struct sensor_event_cap     /* Type: Capacitance */
+struct sensor_cap           /* Type: Capacitance */
 {
   uint64_t timestamp;       /* Unit is microseconds */
   int32_t status;           /* Detection status */
@@ -814,10 +814,10 @@ struct sensor_lowerhalf_s
    *
    * If device support batch mode, the number of events that hardware fifo
    * hold maximum number of samples, must be aligned with size of
-   * struct sensor_event_xxx.
+   * struct sensor_xxx.
    */
 
-  uint32_t buffer_number;
+  unsigned long nbuffer;
 
   /* The uncalibrated use to describe whether the sensor event is
    * uncalibrated. True is uncalibrated data, false is calibrated data,
@@ -878,11 +878,14 @@ struct sensor_lowerhalf_s
 
 struct sensor_state_s
 {
-  unsigned long min_interval;  /* The minimum subscription interval for sensor, in us */
+  unsigned long esize;         /* The element size of circular buffer */
+  unsigned long nbuffer;       /* The number of events that the circular buffer can hold */
   unsigned long min_latency;   /* The minimum batch latency for sensor, in us */
-  unsigned long nsubscribers;  /* The number of subcribers, if subsrciber exists,
-                                * the sensor deivce is enabled */
+  unsigned long min_interval;  /* The minimum subscription interval for sensor, in us */
+  unsigned long nsubscribers;  /* The number of subcribers */
   unsigned long nadvertisers;  /* The number of advertisers */
+  unsigned long generation;    /* The recent generation of circular buffer */
+  FAR void     *priv;          /* The pointer to private data of userspace user */
 };
 
 /* This structure describes the register info for the user sensor */
@@ -890,9 +893,9 @@ struct sensor_state_s
 #ifdef CONFIG_USENSOR
 struct sensor_reginfo_s
 {
-  FAR const char *path; /* The path of user sensor */
-  uint16_t esize;       /* The element size of user sensor */
-  uint32_t nqueue;      /* The number of queue buffered elements */
+  FAR const char *path;        /* The path of user sensor */
+  unsigned long   esize;       /* The element size of user sensor */
+  unsigned long   nbuffer;     /* The number of queue buffered elements */
 };
 #endif
 
@@ -966,7 +969,7 @@ int sensor_register(FAR struct sensor_lowerhalf_s *dev, int devno);
  ****************************************************************************/
 
 int sensor_custom_register(FAR struct sensor_lowerhalf_s *dev,
-                           FAR const char *path, uint8_t esize);
+                           FAR const char *path, unsigned long esize);
 
 /****************************************************************************
  * Name: sensor_unregister


[incubator-nuttx] 07/07: driver/sensor: change nbuffer and sensor event structure type

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 01aa0c2d462e75cd37b4666c29f0f8d62fa63773
Author: Jiuzhu Dong <do...@xiaomi.com>
AuthorDate: Fri Mar 25 18:10:24 2022 +0800

    driver/sensor: change nbuffer and sensor event structure type
    
    Signed-off-by: Jiuzhu Dong <do...@xiaomi.com>
---
 drivers/sensors/bmp280.c     |  2 +-
 drivers/sensors/ds18b20.c    | 14 +++++++-------
 drivers/sensors/fakesensor.c | 22 +++++++++++-----------
 drivers/sensors/hyt271.c     | 24 ++++++++++++------------
 drivers/sensors/l3gd20.c     | 18 +++++++++---------
 drivers/sensors/ms5611.c     |  8 ++++----
 drivers/sensors/wtgahrs2.c   | 20 ++++++++++----------
 7 files changed, 54 insertions(+), 54 deletions(-)

diff --git a/drivers/sensors/bmp280.c b/drivers/sensors/bmp280.c
index 07bb96d19d..723c6cc349 100644
--- a/drivers/sensors/bmp280.c
+++ b/drivers/sensors/bmp280.c
@@ -602,7 +602,7 @@ static int bmp280_fetch(FAR struct sensor_lowerhalf_s *lower,
   int32_t temp;
   int ret;
   struct timespec ts;
-  struct sensor_event_baro baro_data;
+  struct sensor_baro baro_data;
 
   if (buflen != sizeof(baro_data))
     {
diff --git a/drivers/sensors/ds18b20.c b/drivers/sensors/ds18b20.c
index ad77d4bf3b..67014bdf8f 100644
--- a/drivers/sensors/ds18b20.c
+++ b/drivers/sensors/ds18b20.c
@@ -571,12 +571,12 @@ static void ds18b20_notify(FAR struct ds18b20_dev_s *dev,
                            FAR struct ds18b20_sensor_data_s *data)
 {
   FAR struct ds18b20_sensor_s *sensor = &dev->sensor;
-  struct sensor_event_temp temp;
+  struct sensor_temp temp;
 
   temp.temperature = ds18b20_temp(data->spad);
   temp.timestamp   = data->timestamp;
   sensor->lower.push_event(sensor->lower.priv, &temp,
-                           sizeof(struct sensor_event_temp));
+                           sizeof(struct sensor_temp));
 }
 #endif
 
@@ -640,18 +640,18 @@ static int ds18b20_fetch(FAR struct sensor_lowerhalf_s *lower,
 
   /* Check if the user is reading the right size */
 
-  if (buflen != sizeof(struct sensor_event_temp))
+  if (buflen != sizeof(struct sensor_temp))
     {
       snerr("ERROR: You need to read %d bytes from this sensor!\n",
-            sizeof(struct sensor_event_temp));
+            sizeof(struct sensor_temp));
       return -EINVAL;
     }
 
   ret = ds18b20_measure_read(priv, &data);
   if (!ret)
     {
-      FAR struct sensor_event_temp *temp =
-        (FAR struct sensor_event_temp *)buffer;
+      FAR struct sensor_temp *temp =
+        (FAR struct sensor_temp *)buffer;
       temp->temperature = ds18b20_temp(data.spad);
       temp->timestamp   = data.timestamp;
     }
@@ -952,7 +952,7 @@ int ds18b20_register(int devno, FAR struct onewire_master_s *onewire,
   tmp->lower.ops = &g_ds18b20_ops;
   tmp->lower.type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
   tmp->lower.uncalibrated = false;
-  tmp->lower.buffer_number = 1;
+  tmp->lower.nbuffer = 1;
   ret = sensor_register(&tmp->lower, devno);
   if (ret < 0)
     {
diff --git a/drivers/sensors/fakesensor.c b/drivers/sensors/fakesensor.c
index ee27e45317..823f15b1a2 100644
--- a/drivers/sensors/fakesensor.c
+++ b/drivers/sensors/fakesensor.c
@@ -133,7 +133,7 @@ static int fakesensor_read_csv_header(FAR struct fakesensor_s *sensor)
 
 static inline void fakesensor_read_accel(FAR struct fakesensor_s *sensor)
 {
-  struct sensor_event_accel accel;
+  struct sensor_accel accel;
   char raw[50];
   fakesensor_read_csv_line(
           &sensor->data, raw, sizeof(raw), sensor->raw_start);
@@ -141,12 +141,12 @@ static inline void fakesensor_read_accel(FAR struct fakesensor_s *sensor)
   accel.temperature = NAN;
   accel.timestamp = sensor_get_timestamp();
   sensor->lower.push_event(sensor->lower.priv, &accel,
-                    sizeof(struct sensor_event_accel));
+                    sizeof(struct sensor_accel));
 }
 
 static inline void fakesensor_read_mag(FAR struct fakesensor_s *sensor)
 {
-  struct sensor_event_mag mag;
+  struct sensor_mag mag;
   char raw[50];
   fakesensor_read_csv_line(
           &sensor->data, raw, sizeof(raw), sensor->raw_start);
@@ -154,12 +154,12 @@ static inline void fakesensor_read_mag(FAR struct fakesensor_s *sensor)
   mag.temperature = NAN;
   mag.timestamp = sensor_get_timestamp();
   sensor->lower.push_event(sensor->lower.priv, &mag,
-                           sizeof(struct sensor_event_mag));
+                           sizeof(struct sensor_mag));
 }
 
 static inline void fakesensor_read_gyro(FAR struct fakesensor_s *sensor)
 {
-  struct sensor_event_gyro gyro;
+  struct sensor_gyro gyro;
   char raw[50];
   fakesensor_read_csv_line(
           &sensor->data, raw, sizeof(raw), sensor->raw_start);
@@ -167,12 +167,12 @@ static inline void fakesensor_read_gyro(FAR struct fakesensor_s *sensor)
   gyro.temperature = NAN;
   gyro.timestamp = sensor_get_timestamp();
   sensor->lower.push_event(sensor->lower.priv, &gyro,
-                    sizeof(struct sensor_event_gyro));
+                    sizeof(struct sensor_gyro));
 }
 
 static inline void fakesensor_read_gps(FAR struct fakesensor_s *sensor)
 {
-  struct sensor_event_gps gps;
+  struct sensor_gps gps;
   float time;
   char latitude;
   char longitude;
@@ -181,7 +181,7 @@ static inline void fakesensor_read_gps(FAR struct fakesensor_s *sensor)
   float hoop;
   float altitude;
   char raw[150];
-  memset(&gps, 0, sizeof(struct sensor_event_gps));
+  memset(&gps, 0, sizeof(struct sensor_gps));
   read:
   fakesensor_read_csv_line(
           &sensor->data, raw, sizeof(raw), sensor->raw_start);
@@ -210,7 +210,7 @@ static inline void fakesensor_read_gps(FAR struct fakesensor_s *sensor)
   gps.altitude = altitude;
 
   sensor->lower.push_event(sensor->lower.priv, &gps,
-                           sizeof(struct sensor_event_gps));
+                           sizeof(struct sensor_gps));
 }
 
 static int fakesensor_activate(FAR struct sensor_lowerhalf_s *lower, bool sw)
@@ -247,7 +247,7 @@ static int fakesensor_batch(FAR struct sensor_lowerhalf_s *lower,
 {
   FAR struct fakesensor_s *sensor = container_of(lower,
                                                  struct fakesensor_s, lower);
-  unsigned long max_latency = sensor->lower.buffer_number * sensor->interval;
+  unsigned long max_latency = sensor->lower.nbuffer * sensor->interval;
   if (*latency_us > max_latency)
     {
       *latency_us = max_latency;
@@ -392,7 +392,7 @@ int fakesensor_init(int type, FAR const char *file_name,
 
   sensor->lower.type = type;
   sensor->lower.ops = &g_fakesensor_ops;
-  sensor->lower.buffer_number = batch_number;
+  sensor->lower.nbuffer = batch_number;
   sensor->file_path = file_name;
 
   nxsem_init(&sensor->wakeup, 0, 0);
diff --git a/drivers/sensors/hyt271.c b/drivers/sensors/hyt271.c
index 744277e0e3..06945942ca 100644
--- a/drivers/sensors/hyt271.c
+++ b/drivers/sensors/hyt271.c
@@ -152,7 +152,7 @@ static const struct sensor_ops_s g_hyt271_ops =
  ****************************************************************************/
 
 static void hyt271_humi_from_rawdata(FAR struct hyt271_sensor_data_s *data,
-                                     FAR struct sensor_event_humi *humi)
+                                     FAR struct sensor_humi *humi)
 {
   humi->timestamp   = data->timestamp;
   humi->humidity    = HYT271_HUMIDATA(data->data);
@@ -169,7 +169,7 @@ static void hyt271_humi_from_rawdata(FAR struct hyt271_sensor_data_s *data,
  ****************************************************************************/
 
 static void hyt271_temp_from_rawdata(FAR struct hyt271_sensor_data_s *data,
-                                     FAR struct sensor_event_temp *temp)
+                                     FAR struct sensor_temp *temp)
 {
   temp->timestamp   = data->timestamp;
   temp->temperature = HYT271_TEMPDATA(data->data);
@@ -635,14 +635,14 @@ static int hyt271_fetch(FAR struct sensor_lowerhalf_s *lower,
     {
       case SENSOR_TYPE_AMBIENT_TEMPERATURE:
         {
-            struct sensor_event_temp temp;
+            struct sensor_temp temp;
             hyt271_temp_from_rawdata(&data, &temp);
             memcpy(buffer, &temp, sizeof(temp));
         }
         break;
       case SENSOR_TYPE_RELATIVE_HUMIDITY:
         {
-            struct sensor_event_humi humi;
+            struct sensor_humi humi;
             hyt271_humi_from_rawdata(&data, &humi);
             memcpy(buffer, &humi, sizeof(humi));
         }
@@ -812,19 +812,19 @@ static int hyt271_thread(int argc, char** argv)
           if (priv->initial_read == false || (hsensor->enabled == true &&
               !HYT271_HUMIRAWEQUAL(orawdata, data.data)))
             {
-              struct sensor_event_humi humi;
+              struct sensor_humi humi;
               hyt271_humi_from_rawdata(&data, &humi);
               hsensor->lower.push_event(hsensor->lower.priv, &humi,
-                                        sizeof(struct sensor_event_humi));
+                                        sizeof(struct sensor_humi));
             }
 
           if (priv->initial_read == false || (tsensor->enabled == true &&
               !HYT271_TEMPRAWEQUAL(orawdata, data.data)))
             {
-              struct sensor_event_temp temp;
+              struct sensor_temp temp;
               hyt271_temp_from_rawdata(&data, &temp);
               tsensor->lower.push_event(tsensor->lower.priv, &temp,
-                                        sizeof(struct sensor_event_temp));
+                                        sizeof(struct sensor_temp));
             }
 
           if (priv->initial_read == false)
@@ -917,11 +917,11 @@ int hyt271_register(int devno, FAR struct i2c_master_s *i2c, uint8_t addr,
 #ifdef CONFIG_SENSORS_HYT271_POLL
   tmp->enabled = false;
 #endif
-  tmp->buffer_size = sizeof(struct sensor_event_humi);
+  tmp->buffer_size = sizeof(struct sensor_humi);
   tmp->lower.ops = &g_hyt271_ops;
   tmp->lower.type = SENSOR_TYPE_RELATIVE_HUMIDITY;
   tmp->lower.uncalibrated = false;
-  tmp->lower.buffer_number = 1;
+  tmp->lower.nbuffer = 1;
   ret = sensor_register(&tmp->lower, devno);
   if (ret < 0)
     {
@@ -935,11 +935,11 @@ int hyt271_register(int devno, FAR struct i2c_master_s *i2c, uint8_t addr,
 #ifdef CONFIG_SENSORS_HYT271_POLL
   tmp->enabled = false;
 #endif
-  tmp->buffer_size = sizeof(struct sensor_event_temp);
+  tmp->buffer_size = sizeof(struct sensor_temp);
   tmp->lower.ops = &g_hyt271_ops;
   tmp->lower.type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
   tmp->lower.uncalibrated = false;
-  tmp->lower.buffer_number = 1;
+  tmp->lower.nbuffer = 1;
   ret = sensor_register(&tmp->lower, devno);
   if (ret < 0)
     {
diff --git a/drivers/sensors/l3gd20.c b/drivers/sensors/l3gd20.c
index 99a8fa8a88..dad4cd5181 100644
--- a/drivers/sensors/l3gd20.c
+++ b/drivers/sensors/l3gd20.c
@@ -82,7 +82,7 @@ static void l3gd20_write_register(FAR struct l3gd20_dev_s *dev,
                                   uint8_t const reg_data);
 static void l3gd20_reset(FAR struct l3gd20_dev_s *dev);
 static void l3gd20_read_measurement_data(FAR struct l3gd20_dev_s *dev,
-                                         FAR struct sensor_event_gyro *data);
+                                         FAR struct sensor_gyro *data);
 static void l3gd20_read_gyroscope_data(FAR struct l3gd20_dev_s *dev,
                                        uint16_t *x_gyr, uint16_t *y_gyr,
                                        uint16_t *z_gyr);
@@ -217,7 +217,7 @@ static void l3gd20_reset(FAR struct l3gd20_dev_s *dev)
  ****************************************************************************/
 
 static void l3gd20_read_measurement_data(FAR struct l3gd20_dev_s *dev,
-                                         FAR struct sensor_event_gyro *data)
+                                         FAR struct sensor_gyro *data)
 {
   uint16_t x_gyr = 0;
   uint16_t y_gyr = 0;
@@ -381,7 +381,7 @@ static int l3gd20_interrupt_handler(int irq, FAR void *context,
 
 static void l3gd20_worker(FAR void *arg)
 {
-  struct sensor_event_gyro temp;
+  struct sensor_gyro temp;
 
   FAR struct l3gd20_dev_s *priv = (FAR struct l3gd20_dev_s *)(arg);
   DEBUGASSERT(priv != NULL);
@@ -393,7 +393,7 @@ static void l3gd20_worker(FAR void *arg)
   /* push data to upper half driver */
 
   priv->lower.push_event(priv->lower.priv, &temp,
-                         sizeof(struct sensor_event_gyro));
+                         sizeof(struct sensor_gyro));
 }
 
 #else
@@ -409,16 +409,16 @@ static int l3gd20_fetch(FAR struct sensor_lowerhalf_s *lower,
                                                FAR struct l3gd20_dev_s,
                                                lower);
 
-  if (buflen != sizeof(struct sensor_event_gyro))
+  if (buflen != sizeof(struct sensor_gyro))
       return 0;
 
   DEBUGASSERT(priv != NULL);
 
   /* Read out the latest sensor data */
 
-  l3gd20_read_measurement_data(priv, (FAR struct sensor_event_gyro *)buffer);
+  l3gd20_read_measurement_data(priv, (FAR struct sensor_gyro *)buffer);
 
-  return sizeof(struct sensor_event_gyro);
+  return sizeof(struct sensor_gyro);
 }
 #endif
 
@@ -432,7 +432,7 @@ static int l3gd20_activate(FAR struct sensor_lowerhalf_s *lower,
   FAR struct l3gd20_dev_s *priv = container_of(lower,
                                                FAR struct l3gd20_dev_s,
                                                lower);
-  struct sensor_event_gyro temp;
+  struct sensor_gyro temp;
 
 #ifdef CONFIG_DEBUG_SENSORS_INFO
   uint8_t reg_content;
@@ -559,7 +559,7 @@ int l3gd20_register(int devno, FAR struct spi_dev_s *spi,
   priv->timestamp        = 0;
 
   priv->lower.type = SENSOR_TYPE_GYROSCOPE;
-  priv->lower.buffer_number = CONFIG_SENSORS_L3GD20_BUFFER_SIZE;
+  priv->lower.nbuffer = CONFIG_SENSORS_L3GD20_BUFFER_SIZE;
   priv->lower.ops = &g_l2gd20_ops;
   priv->lower.uncalibrated = true;
 
diff --git a/drivers/sensors/ms5611.c b/drivers/sensors/ms5611.c
index 97a4b6bd36..095e08e9b8 100644
--- a/drivers/sensors/ms5611.c
+++ b/drivers/sensors/ms5611.c
@@ -250,7 +250,7 @@ static int ms5611_read24(FAR struct ms5611_dev_s *priv, uint8_t *regval)
 }
 
 static inline void baro_measure_read(FAR struct ms5611_dev_s *priv,
-                                     FAR struct sensor_event_baro *baro)
+                                     FAR struct sensor_baro *baro)
 {
   uint32_t press;
   uint32_t temp;
@@ -370,7 +370,7 @@ static int ms5611_thread(int argc, char **argv)
   FAR struct ms5611_dev_s *priv = (FAR struct ms5611_dev_s *)
         ((uintptr_t)strtoul(argv[1], NULL, 0));
 
-  struct sensor_event_baro baro_data;
+  struct sensor_baro baro_data;
 
   while (true)
     {
@@ -390,7 +390,7 @@ static int ms5611_thread(int argc, char **argv)
        baro_measure_read(priv, &baro_data);
 
        priv->sensor_lower.push_event(priv->sensor_lower.priv, &baro_data,
-                                     sizeof(struct sensor_event_baro));
+                                     sizeof(struct sensor_baro));
 
       /* Sleeping thread before fetching the next sensor data */
 
@@ -596,7 +596,7 @@ static int ms5611_fetch(FAR struct sensor_lowerhalf_s *lower,
   FAR struct ms5611_dev_s *priv = container_of(lower,
                                                FAR struct ms5611_dev_s,
                                                sensor_lower);
-  struct sensor_event_baro baro_data;
+  struct sensor_baro baro_data;
 
   if (buflen != sizeof(baro_data))
     {
diff --git a/drivers/sensors/wtgahrs2.c b/drivers/sensors/wtgahrs2.c
index 004fb23eab..c1c54a2f83 100644
--- a/drivers/sensors/wtgahrs2.c
+++ b/drivers/sensors/wtgahrs2.c
@@ -83,7 +83,7 @@ struct wtgahrs2_dev_s
   struct wtgahrs2_sensor_s  dev[WTGAHRS2_MAX_IDX];
   struct file               file;
 
-  struct sensor_event_gps   gps;
+  struct sensor_gps   gps;
   unsigned char             gps_mask;
 };
 
@@ -186,7 +186,7 @@ static void wtgahrs2_accel_data(FAR struct wtgahrs2_dev_s *rtdata,
   FAR struct wtgahrs2_sensor_s *dev = &rtdata->dev[WTGAHRS2_ACCEL_IDX];
   FAR struct sensor_lowerhalf_s *lower = &dev->lower;
   uint64_t now = sensor_get_timestamp();
-  struct sensor_event_accel accel;
+  struct sensor_accel accel;
 
   if (!dev->enable || now - dev->last_update < dev->interval)
     {
@@ -212,7 +212,7 @@ static void wtgahrs2_gyro_data(FAR struct wtgahrs2_dev_s *rtdata,
   FAR struct wtgahrs2_sensor_s *dev = &rtdata->dev[WTGAHRS2_GYRO_IDX];
   FAR struct sensor_lowerhalf_s *lower = &dev->lower;
   uint64_t now = sensor_get_timestamp();
-  struct sensor_event_gyro gyro;
+  struct sensor_gyro gyro;
 
   if (!dev->enable || now - dev->last_update < dev->interval)
     {
@@ -238,7 +238,7 @@ static void wtgahrs2_mag_data(FAR struct wtgahrs2_dev_s *rtdata,
   FAR struct wtgahrs2_sensor_s *dev = &rtdata->dev[WTGAHRS2_MAG_IDX];
   FAR struct sensor_lowerhalf_s *lower = &dev->lower;
   uint64_t now = sensor_get_timestamp();
-  struct sensor_event_mag mag;
+  struct sensor_mag mag;
 
   if (!dev->enable || now - dev->last_update < dev->interval)
     {
@@ -264,7 +264,7 @@ static void wtgahrs2_baro_data(FAR struct wtgahrs2_dev_s *rtdata,
   FAR struct wtgahrs2_sensor_s *dev = &rtdata->dev[WTGAHRS2_BARO_IDX];
   FAR struct sensor_lowerhalf_s *lower = &dev->lower;
   uint64_t now = sensor_get_timestamp();
-  struct sensor_event_baro baro;
+  struct sensor_baro baro;
 
   if (!dev->enable || now - dev->last_update < dev->interval)
     {
@@ -471,7 +471,7 @@ int wtgahrs2_initialize(FAR const char *path, int devno)
   tmp = &rtdata->dev[WTGAHRS2_ACCEL_IDX];
   tmp->lower.ops = &g_wtgahrs2_ops;
   tmp->lower.type = SENSOR_TYPE_ACCELEROMETER;
-  tmp->lower.buffer_number = 1;
+  tmp->lower.nbuffer = 1;
   ret = sensor_register(&tmp->lower, devno);
   if (ret < 0)
     {
@@ -483,7 +483,7 @@ int wtgahrs2_initialize(FAR const char *path, int devno)
   tmp = &rtdata->dev[WTGAHRS2_GYRO_IDX];
   tmp->lower.ops = &g_wtgahrs2_ops;
   tmp->lower.type = SENSOR_TYPE_GYROSCOPE;
-  tmp->lower.buffer_number = 1;
+  tmp->lower.nbuffer = 1;
   ret = sensor_register(&tmp->lower, devno);
   if (ret < 0)
     {
@@ -495,7 +495,7 @@ int wtgahrs2_initialize(FAR const char *path, int devno)
   tmp = &rtdata->dev[WTGAHRS2_MAG_IDX];
   tmp->lower.ops = &g_wtgahrs2_ops;
   tmp->lower.type = SENSOR_TYPE_MAGNETIC_FIELD;
-  tmp->lower.buffer_number = 1;
+  tmp->lower.nbuffer = 1;
   ret = sensor_register(&tmp->lower, devno);
   if (ret < 0)
     {
@@ -507,7 +507,7 @@ int wtgahrs2_initialize(FAR const char *path, int devno)
   tmp = &rtdata->dev[WTGAHRS2_BARO_IDX];
   tmp->lower.ops = &g_wtgahrs2_ops;
   tmp->lower.type = SENSOR_TYPE_BAROMETER;
-  tmp->lower.buffer_number = 1;
+  tmp->lower.nbuffer = 1;
   ret = sensor_register(&tmp->lower, devno);
   if (ret < 0)
     {
@@ -519,7 +519,7 @@ int wtgahrs2_initialize(FAR const char *path, int devno)
   tmp = &rtdata->dev[WTGAHRS2_GPS_IDX];
   tmp->lower.ops = &g_wtgahrs2_ops;
   tmp->lower.type = SENSOR_TYPE_GPS;
-  tmp->lower.buffer_number = 1;
+  tmp->lower.nbuffer = 1;
   ret = sensor_register(&tmp->lower, devno);
   if (ret < 0)
     {