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:27 UTC

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

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