2020-10-19 15:24:13 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/sensors/sensor.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 <sys/types.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2020-10-19 15:24:13 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <poll.h>
|
|
|
|
#include <fcntl.h>
|
2022-03-22 08:46:02 +01:00
|
|
|
#include <nuttx/list.h>
|
2020-10-19 15:24:13 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2020-11-12 11:40:37 +01:00
|
|
|
#include <nuttx/mm/circbuf.h>
|
2020-10-19 15:24:13 +02:00
|
|
|
#include <nuttx/sensors/sensor.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Device naming ************************************************************/
|
|
|
|
|
|
|
|
#define ROUNDUP(x, esize) ((x + (esize - 1)) / (esize)) * (esize)
|
2022-03-29 05:39:44 +02:00
|
|
|
#define DEVNAME_FMT "/dev/sensor/sensor_%s%s%d"
|
2020-10-19 15:24:13 +02:00
|
|
|
#define DEVNAME_UNCAL "_uncal"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This structure describes sensor info */
|
|
|
|
|
2022-03-22 08:46:02 +01:00
|
|
|
struct sensor_info_s
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
2022-03-23 10:09:46 +01:00
|
|
|
unsigned long esize;
|
2020-11-02 11:02:04 +01:00
|
|
|
FAR char *name;
|
2020-10-19 15:24:13 +02:00
|
|
|
};
|
|
|
|
|
2022-03-22 08:46:02 +01:00
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
|
2022-03-23 10:09:46 +01:00
|
|
|
unsigned long generation; /* Last generation subscriber has seen */
|
2022-03-22 08:46:02 +01:00
|
|
|
unsigned long interval; /* The interval for subscriber */
|
|
|
|
unsigned long latency; /* The bactch latency for subscriber */
|
2022-05-10 16:15:54 +02:00
|
|
|
bool readlast; /* The flag of readlast */
|
2022-03-22 08:46:02 +01:00
|
|
|
};
|
|
|
|
|
2020-10-19 15:24:13 +02:00
|
|
|
/* This structure describes the state of the upper half driver */
|
|
|
|
|
|
|
|
struct sensor_upperhalf_s
|
|
|
|
{
|
2022-03-22 08:46:02 +01:00
|
|
|
FAR struct sensor_lowerhalf_s *lower; /* The handle of lower half driver */
|
|
|
|
struct sensor_state_s state; /* The state of sensor device */
|
2020-11-12 11:40:37 +01:00
|
|
|
struct circbuf_s buffer; /* The circular buffer of sensor device */
|
2020-10-19 15:24:13 +02:00
|
|
|
sem_t exclsem; /* Manages exclusive access to file operations */
|
2022-04-29 17:08:59 +02:00
|
|
|
pid_t semholder; /* The current holder of the semaphore */
|
|
|
|
int16_t semcount; /* Number of counts held */
|
2022-03-22 08:46:02 +01:00
|
|
|
struct list_node userlist; /* List of users */
|
2020-10-19 15:24:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void sensor_pollnotify(FAR struct sensor_upperhalf_s *upper,
|
|
|
|
pollevent_t eventset);
|
|
|
|
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);
|
2022-03-11 10:05:47 +01:00
|
|
|
static ssize_t sensor_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
2020-10-19 15:24:13 +02:00
|
|
|
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);
|
2022-03-11 10:05:47 +01:00
|
|
|
static ssize_t sensor_push_event(FAR void *priv, FAR const void *data,
|
|
|
|
size_t bytes);
|
2020-10-19 15:24:13 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-03-22 08:46:02 +01:00
|
|
|
static const struct sensor_info_s g_sensor_info[] =
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
2022-03-23 10:09:46 +01:00
|
|
|
{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"},
|
2020-10-19 15:24:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct file_operations g_sensor_fops =
|
|
|
|
{
|
|
|
|
sensor_open, /* open */
|
|
|
|
sensor_close, /* close */
|
|
|
|
sensor_read, /* read */
|
2022-03-11 10:05:47 +01:00
|
|
|
sensor_write, /* write */
|
2020-10-19 15:24:13 +02:00
|
|
|
NULL, /* seek */
|
|
|
|
sensor_ioctl, /* ioctl */
|
|
|
|
sensor_poll /* poll */
|
2022-01-10 15:51:17 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
, NULL /* unlink */
|
|
|
|
#endif
|
2020-10-19 15:24:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-04-29 17:08:59 +02:00
|
|
|
static void sensor_semtake(FAR struct sensor_upperhalf_s *upper)
|
|
|
|
{
|
|
|
|
pid_t pid = gettid();
|
|
|
|
|
|
|
|
if (pid == upper->semholder)
|
|
|
|
{
|
|
|
|
upper->semcount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nxsem_wait_uninterruptible(&upper->exclsem);
|
|
|
|
upper->semholder = pid;
|
|
|
|
upper->semcount = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sensor_semgive(FAR struct sensor_upperhalf_s *upper)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(upper->semholder == gettid());
|
|
|
|
|
|
|
|
if (upper->semcount > 1)
|
|
|
|
{
|
|
|
|
upper->semcount--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
upper->semholder = INVALID_PROCESS_ID;
|
|
|
|
upper->semcount = 0;
|
|
|
|
nxsem_post(&upper->exclsem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 10:09:46 +01:00
|
|
|
static bool sensor_in_range(unsigned long left, unsigned long value,
|
|
|
|
unsigned long right)
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
2022-03-22 08:46:02 +01:00
|
|
|
if (left < right)
|
|
|
|
{
|
|
|
|
return left <= value && value < right;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Maybe the data overflowed and a wraparound occurred */
|
|
|
|
|
|
|
|
return left <= value || value < right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 10:09:46 +01:00
|
|
|
static bool sensor_is_updated(unsigned long generation,
|
|
|
|
unsigned long ugeneration)
|
2022-03-22 08:46:02 +01:00
|
|
|
{
|
|
|
|
return generation > ugeneration;
|
|
|
|
}
|
|
|
|
|
2022-04-13 08:00:08 +02:00
|
|
|
static int sensor_update_interval(FAR struct file *filep,
|
|
|
|
FAR struct sensor_upperhalf_s *upper,
|
2022-03-22 08:46:02 +01:00
|
|
|
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;
|
|
|
|
}
|
2020-10-19 15:24:13 +02:00
|
|
|
|
2022-03-22 08:46:02 +01:00
|
|
|
list_for_every_entry(&upper->userlist, tmp, struct sensor_user_s, node)
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
2022-03-22 08:46:02 +01:00
|
|
|
if (tmp == user)
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
2022-03-22 08:46:02 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-10-19 15:24:13 +02:00
|
|
|
|
2022-03-22 08:46:02 +01:00
|
|
|
if (min_interval > tmp->interval)
|
|
|
|
{
|
|
|
|
min_interval = tmp->interval;
|
|
|
|
}
|
|
|
|
}
|
2020-11-04 14:31:30 +01:00
|
|
|
|
2022-03-22 08:46:02 +01:00
|
|
|
update:
|
|
|
|
if (min_interval == upper->state.min_interval)
|
|
|
|
{
|
|
|
|
user->interval = interval;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (min_interval != ULONG_MAX && lower->ops->set_interval)
|
|
|
|
{
|
2022-04-13 08:00:08 +02:00
|
|
|
ret = lower->ops->set_interval(lower, filep, &min_interval);
|
2022-03-22 08:46:02 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
2020-10-19 15:24:13 +02:00
|
|
|
}
|
|
|
|
}
|
2022-03-22 08:46:02 +01:00
|
|
|
|
|
|
|
upper->state.min_interval = min_interval;
|
|
|
|
user->interval = interval;
|
|
|
|
sensor_pollnotify(upper, POLLPRI);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-04-13 08:00:08 +02:00
|
|
|
static int sensor_update_latency(FAR struct file *filep,
|
|
|
|
FAR struct sensor_upperhalf_s *upper,
|
2022-03-22 08:46:02 +01:00
|
|
|
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)
|
|
|
|
{
|
2022-04-13 08:00:08 +02:00
|
|
|
ret = lower->ops->batch(lower, filep, &min_latency);
|
2022-03-22 08:46:02 +01:00
|
|
|
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);
|
|
|
|
}
|
2020-10-19 15:24:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int sensor_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct sensor_upperhalf_s *upper = inode->i_private;
|
2020-11-12 11:40:37 +01:00
|
|
|
FAR struct sensor_lowerhalf_s *lower = upper->lower;
|
2022-03-22 08:46:02 +01:00
|
|
|
FAR struct sensor_user_s *user;
|
2022-04-29 17:08:59 +02:00
|
|
|
int ret = 0;
|
2020-10-19 15:24:13 +02:00
|
|
|
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semtake(upper);
|
2022-03-22 08:46:02 +01:00
|
|
|
user = kmm_zalloc(sizeof(struct sensor_user_s));
|
|
|
|
if (user == NULL)
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
2022-03-22 08:46:02 +01:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_sem;
|
|
|
|
}
|
2020-11-04 14:31:30 +01:00
|
|
|
|
2022-04-13 08:00:08 +02:00
|
|
|
if (lower->ops->open)
|
|
|
|
{
|
|
|
|
ret = lower->ops->open(lower, filep);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 08:46:02 +01:00
|
|
|
if (filep->f_oflags & O_RDOK)
|
|
|
|
{
|
|
|
|
if (upper->state.nsubscribers == 0 && lower->ops->activate)
|
|
|
|
{
|
2022-04-13 08:00:08 +02:00
|
|
|
ret = lower->ops->activate(lower, filep, true);
|
2022-03-22 08:46:02 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-04-13 08:00:08 +02:00
|
|
|
goto errout_with_open;
|
2022-03-22 08:46:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
upper->state.nsubscribers++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filep->f_oflags & O_WROK)
|
|
|
|
{
|
|
|
|
upper->state.nadvertisers++;
|
2022-05-21 11:50:29 +02:00
|
|
|
if (filep->f_oflags & SENSOR_PERSIST)
|
|
|
|
{
|
|
|
|
lower->persist = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (upper->state.generation && lower->persist)
|
|
|
|
{
|
|
|
|
user->generation = upper->state.generation - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
user->generation = upper->state.generation;
|
2020-10-19 15:24:13 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 08:46:02 +01:00
|
|
|
user->interval = ULONG_MAX;
|
|
|
|
user->latency = ULONG_MAX;
|
2022-05-10 16:15:54 +02:00
|
|
|
user->readlast = true;
|
2022-03-22 08:46:02 +01:00
|
|
|
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;
|
|
|
|
|
2022-04-13 08:00:08 +02:00
|
|
|
errout_with_open:
|
|
|
|
if (lower->ops->close)
|
|
|
|
{
|
|
|
|
lower->ops->close(lower, filep);
|
|
|
|
}
|
|
|
|
|
2022-03-22 08:46:02 +01:00
|
|
|
errout_with_user:
|
|
|
|
kmm_free(user);
|
|
|
|
errout_with_sem:
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semgive(upper);
|
2020-10-19 15:24:13 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2022-03-22 08:46:02 +01:00
|
|
|
FAR struct sensor_user_s *user = filep->f_priv;
|
2022-04-29 17:08:59 +02:00
|
|
|
int ret = 0;
|
2020-10-19 15:24:13 +02:00
|
|
|
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semtake(upper);
|
2022-04-13 08:00:08 +02:00
|
|
|
if (lower->ops->close)
|
|
|
|
{
|
|
|
|
ret = lower->ops->close(lower, filep);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semgive(upper);
|
2022-04-13 08:00:08 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 08:46:02 +01:00
|
|
|
if (filep->f_oflags & O_RDOK)
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
2022-03-22 08:46:02 +01:00
|
|
|
upper->state.nsubscribers--;
|
|
|
|
if (upper->state.nsubscribers == 0 && lower->ops->activate)
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
2022-04-13 08:00:08 +02:00
|
|
|
lower->ops->activate(lower, filep, false);
|
2020-10-19 15:24:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 08:46:02 +01:00
|
|
|
if (filep->f_oflags & O_WROK)
|
|
|
|
{
|
|
|
|
upper->state.nadvertisers--;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_delete(&user->node);
|
2022-04-13 08:00:08 +02:00
|
|
|
sensor_update_latency(filep, upper, user, ULONG_MAX);
|
|
|
|
sensor_update_interval(filep, upper, user, ULONG_MAX);
|
2022-03-22 08:46:02 +01:00
|
|
|
nxsem_destroy(&user->buffersem);
|
|
|
|
|
|
|
|
/* The user is closed, notify to other users */
|
|
|
|
|
|
|
|
sensor_pollnotify(upper, POLLPRI);
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semgive(upper);
|
2022-03-22 08:46:02 +01:00
|
|
|
|
|
|
|
kmm_free(user);
|
2020-10-19 15:24:13 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t sensor_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct sensor_upperhalf_s *upper = inode->i_private;
|
|
|
|
FAR struct sensor_lowerhalf_s *lower = upper->lower;
|
2022-03-22 08:46:02 +01:00
|
|
|
FAR struct sensor_user_s *user = filep->f_priv;
|
2022-03-23 10:09:46 +01:00
|
|
|
unsigned long nums;
|
2020-10-19 15:24:13 +02:00
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
if (!buffer || !len)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semtake(upper);
|
2020-11-04 04:23:39 +01:00
|
|
|
if (lower->ops->fetch)
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
2020-11-04 04:23:39 +01:00
|
|
|
if (!(filep->f_oflags & O_NONBLOCK))
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semgive(upper);
|
2022-03-22 08:46:02 +01:00
|
|
|
ret = nxsem_wait_uninterruptible(&user->buffersem);
|
2020-11-04 04:23:39 +01:00
|
|
|
if (ret < 0)
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semtake(upper);
|
2020-10-19 15:24:13 +02:00
|
|
|
}
|
2022-03-22 08:46:02 +01:00
|
|
|
else if (!upper->state.nsubscribers)
|
2021-02-07 10:13:31 +01:00
|
|
|
{
|
|
|
|
ret = -EAGAIN;
|
|
|
|
goto out;
|
|
|
|
}
|
2020-11-04 04:23:39 +01:00
|
|
|
|
2022-04-13 08:00:08 +02:00
|
|
|
ret = lower->ops->fetch(lower, filep, buffer, len);
|
2020-10-19 15:24:13 +02:00
|
|
|
}
|
2020-11-04 04:23:39 +01:00
|
|
|
else
|
|
|
|
{
|
2022-04-26 08:01:11 +02:00
|
|
|
/* If readlast is true, you can always read the last data
|
|
|
|
* in the circbuffer as initial value for new users when the
|
|
|
|
* sensor device has not yet generated new data, otherwise,
|
|
|
|
* it will return 0 when there isn't new data.
|
2020-11-04 04:23:39 +01:00
|
|
|
*/
|
2020-10-19 15:24:13 +02:00
|
|
|
|
2022-05-10 16:15:54 +02:00
|
|
|
if (user->readlast)
|
2020-11-04 04:23:39 +01:00
|
|
|
{
|
2022-04-26 08:01:11 +02:00
|
|
|
if (circbuf_is_empty(&upper->buffer))
|
2020-11-04 04:23:39 +01:00
|
|
|
{
|
2022-04-26 08:01:11 +02:00
|
|
|
ret = -ENODATA;
|
2021-02-07 10:13:31 +01:00
|
|
|
goto out;
|
2020-11-04 04:23:39 +01:00
|
|
|
}
|
2022-04-26 08:01:11 +02:00
|
|
|
|
2022-05-21 11:50:29 +02:00
|
|
|
/* If the device data is persistent, and when the device has no
|
|
|
|
* new data, the user can copy the old data, otherwise return
|
|
|
|
* -ENODATA.
|
|
|
|
*/
|
|
|
|
|
2022-04-26 08:01:11 +02:00
|
|
|
if (user->generation == upper->state.generation)
|
2020-11-04 04:23:39 +01:00
|
|
|
{
|
2022-05-21 11:50:29 +02:00
|
|
|
if (lower->persist)
|
|
|
|
{
|
|
|
|
user->generation--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ENODATA;
|
|
|
|
goto out;
|
|
|
|
}
|
2022-04-26 08:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We must make sure that when the semaphore is equal to 1,
|
|
|
|
* there must be events available in the buffer, so we use a
|
|
|
|
* while statement to synchronize this case that other read
|
|
|
|
* operations consume events that have just entered the buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (circbuf_is_empty(&upper->buffer) ||
|
|
|
|
user->generation == upper->state.generation)
|
|
|
|
{
|
|
|
|
if (filep->f_oflags & O_NONBLOCK)
|
2020-11-04 04:23:39 +01:00
|
|
|
{
|
2022-04-26 08:01:11 +02:00
|
|
|
ret = -EAGAIN;
|
|
|
|
goto out;
|
2020-11-04 04:23:39 +01:00
|
|
|
}
|
2022-04-26 08:01:11 +02:00
|
|
|
else
|
2020-11-04 04:23:39 +01:00
|
|
|
{
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semgive(upper);
|
2022-04-26 08:01:11 +02:00
|
|
|
ret = nxsem_wait_uninterruptible(&user->buffersem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semtake(upper);
|
2020-11-04 04:23:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-19 15:24:13 +02:00
|
|
|
|
2022-03-22 08:46:02 +01:00
|
|
|
/* If user's generation isn't within circbuffer range, the
|
|
|
|
* oldest data in circbuffer are returned to the users.
|
|
|
|
*/
|
|
|
|
|
2022-04-26 08:01:11 +02:00
|
|
|
if (!sensor_in_range(upper->state.generation - lower->nbuffer,
|
|
|
|
user->generation, upper->state.generation))
|
2022-03-22 08:46:02 +01:00
|
|
|
|
|
|
|
{
|
2022-03-23 10:09:46 +01:00
|
|
|
user->generation = upper->state.generation - lower->nbuffer;
|
2022-03-22 08:46:02 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 10:09:46 +01:00
|
|
|
nums = upper->state.generation - user->generation;
|
|
|
|
if (len < nums * upper->state.esize)
|
2022-03-22 08:46:02 +01:00
|
|
|
{
|
2022-03-23 10:09:46 +01:00
|
|
|
nums = len / upper->state.esize;
|
2022-03-22 08:46:02 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 10:09:46 +01:00
|
|
|
len = nums * upper->state.esize;
|
|
|
|
ret = circbuf_peekat(&upper->buffer,
|
|
|
|
user->generation * upper->state.esize,
|
2022-03-22 08:46:02 +01:00
|
|
|
buffer, len);
|
|
|
|
user->generation += nums;
|
2020-10-19 15:24:13 +02:00
|
|
|
}
|
|
|
|
|
2021-02-07 10:13:31 +01:00
|
|
|
out:
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semgive(upper);
|
2020-10-19 15:24:13 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-11 10:05:47 +01:00
|
|
|
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;
|
2022-04-08 16:46:29 +02:00
|
|
|
FAR struct sensor_lowerhalf_s *lower = upper->lower;
|
2022-03-11 10:05:47 +01:00
|
|
|
|
2022-04-08 16:46:29 +02:00
|
|
|
return lower->push_event(lower->priv, buffer, buflen);
|
2022-03-11 10:05:47 +01:00
|
|
|
}
|
|
|
|
|
2020-10-19 15:24:13 +02:00
|
|
|
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;
|
2022-03-22 08:46:02 +01:00
|
|
|
FAR struct sensor_user_s *user = filep->f_priv;
|
2022-04-29 17:08:59 +02:00
|
|
|
int ret = 0;
|
2020-10-19 15:24:13 +02:00
|
|
|
|
2020-11-24 23:12:02 +01:00
|
|
|
sninfo("cmd=%x arg=%08lx\n", cmd, arg);
|
2020-10-19 15:24:13 +02:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2022-03-22 08:46:02 +01:00
|
|
|
case SNIOC_GET_STATE:
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semtake(upper);
|
2022-03-22 08:46:02 +01:00
|
|
|
memcpy((FAR void *)(uintptr_t)arg,
|
|
|
|
&upper->state, sizeof(upper->state));
|
|
|
|
user->changed = false;
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semgive(upper);
|
2020-10-19 15:24:13 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SNIOC_SET_INTERVAL:
|
|
|
|
{
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semtake(upper);
|
2022-04-13 08:00:08 +02:00
|
|
|
ret = sensor_update_interval(filep, upper, user, arg);
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semgive(upper);
|
2020-10-19 15:24:13 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SNIOC_BATCH:
|
|
|
|
{
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semtake(upper);
|
2022-04-13 08:00:08 +02:00
|
|
|
ret = sensor_update_latency(filep, upper, user, arg);
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semgive(upper);
|
2020-10-19 15:24:13 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-09-27 14:16:15 +02:00
|
|
|
case SNIOC_SELFTEST:
|
|
|
|
{
|
|
|
|
if (lower->ops->selftest == NULL)
|
|
|
|
{
|
|
|
|
ret = -ENOTSUP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-04-13 08:00:08 +02:00
|
|
|
ret = lower->ops->selftest(lower, filep, arg);
|
2021-09-27 14:16:15 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-11-09 13:37:27 +01:00
|
|
|
case SNIOC_SET_CALIBVALUE:
|
|
|
|
{
|
|
|
|
if (lower->ops->set_calibvalue == NULL)
|
|
|
|
{
|
|
|
|
ret = -ENOTSUP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-04-13 08:00:08 +02:00
|
|
|
ret = lower->ops->set_calibvalue(lower, filep, arg);
|
2021-11-09 13:37:27 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-07-21 04:47:12 +02:00
|
|
|
case SNIOC_CALIBRATE:
|
|
|
|
{
|
|
|
|
if (lower->ops->calibrate == NULL)
|
|
|
|
{
|
|
|
|
ret = -ENOTSUP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-04-13 08:00:08 +02:00
|
|
|
ret = lower->ops->calibrate(lower, filep, arg);
|
2022-07-21 04:47:12 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-03-23 10:09:46 +01:00
|
|
|
case SNIOC_SET_USERPRIV:
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semtake(upper);
|
2022-03-23 10:09:46 +01:00
|
|
|
upper->state.priv = (FAR void *)(uintptr_t)arg;
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semgive(upper);
|
2020-11-04 14:31:30 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-02-08 09:26:25 +01:00
|
|
|
case SNIOC_SET_BUFFER_NUMBER:
|
2020-11-04 14:31:30 +01:00
|
|
|
{
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semtake(upper);
|
2022-07-21 04:32:29 +02:00
|
|
|
if (!circbuf_is_init(&upper->buffer))
|
2020-11-04 14:31:30 +01:00
|
|
|
{
|
2022-03-23 10:09:46 +01:00
|
|
|
if (arg >= lower->nbuffer)
|
2022-03-22 08:46:02 +01:00
|
|
|
{
|
2022-03-23 10:09:46 +01:00
|
|
|
lower->nbuffer = arg;
|
2022-03-22 08:46:02 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ERANGE;
|
|
|
|
}
|
2022-07-21 04:32:29 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -EBUSY;
|
2020-11-04 14:31:30 +01:00
|
|
|
}
|
2022-04-29 17:08:59 +02:00
|
|
|
|
|
|
|
sensor_semgive(upper);
|
2020-10-19 15:24:13 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-04-26 08:01:11 +02:00
|
|
|
case SNIOC_READLAST:
|
|
|
|
{
|
2022-05-10 16:15:54 +02:00
|
|
|
sensor_semtake(upper);
|
|
|
|
user->readlast = !!arg;
|
|
|
|
sensor_semgive(upper);
|
2022-04-26 08:01:11 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-10-19 15:24:13 +02:00
|
|
|
default:
|
2020-11-04 14:31:30 +01:00
|
|
|
|
|
|
|
/* Lowerhalf driver process other cmd. */
|
|
|
|
|
|
|
|
if (lower->ops->control)
|
|
|
|
{
|
2022-04-13 08:00:08 +02:00
|
|
|
ret = lower->ops->control(lower, filep, cmd, arg);
|
2020-11-04 14:31:30 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ENOTTY;
|
|
|
|
}
|
|
|
|
|
2020-10-19 15:24:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sensor_poll(FAR struct file *filep,
|
2022-03-22 08:46:02 +01:00
|
|
|
FAR struct pollfd *fds, bool setup)
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct sensor_upperhalf_s *upper = inode->i_private;
|
2020-11-04 04:23:39 +01:00
|
|
|
FAR struct sensor_lowerhalf_s *lower = upper->lower;
|
2022-03-22 08:46:02 +01:00
|
|
|
FAR struct sensor_user_s *user = filep->f_priv;
|
2020-10-19 15:24:13 +02:00
|
|
|
pollevent_t eventset = 0;
|
2020-11-04 04:23:39 +01:00
|
|
|
int semcount;
|
2022-04-29 17:08:59 +02:00
|
|
|
int ret = 0;
|
2020-10-19 15:24:13 +02:00
|
|
|
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semtake(upper);
|
2020-10-19 15:24:13 +02:00
|
|
|
if (setup)
|
|
|
|
{
|
2020-11-04 14:31:30 +01:00
|
|
|
/* Don't have enough space to store fds */
|
|
|
|
|
2022-03-22 08:46:02 +01:00
|
|
|
if (user->fds)
|
2020-11-04 14:31:30 +01:00
|
|
|
{
|
|
|
|
ret = -ENOSPC;
|
|
|
|
goto errout;
|
|
|
|
}
|
2020-10-19 15:24:13 +02:00
|
|
|
|
2022-03-22 08:46:02 +01:00
|
|
|
user->fds = fds;
|
|
|
|
fds->priv = filep;
|
2020-11-04 04:23:39 +01:00
|
|
|
if (lower->ops->fetch)
|
|
|
|
{
|
|
|
|
/* Always return POLLIN for fetch data directly(non-block) */
|
|
|
|
|
|
|
|
if (filep->f_oflags & O_NONBLOCK)
|
|
|
|
{
|
|
|
|
eventset |= (fds->events & POLLIN);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-22 08:46:02 +01:00
|
|
|
nxsem_get_value(&user->buffersem, &semcount);
|
2020-11-04 04:23:39 +01:00
|
|
|
if (semcount > 0)
|
|
|
|
{
|
|
|
|
eventset |= (fds->events & POLLIN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-23 10:09:46 +01:00
|
|
|
else if (sensor_is_updated(upper->state.generation, user->generation))
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
|
|
|
eventset |= (fds->events & POLLIN);
|
|
|
|
}
|
|
|
|
|
2022-03-22 08:46:02 +01:00
|
|
|
if (user->changed)
|
|
|
|
{
|
|
|
|
eventset |= (fds->events & POLLPRI);
|
|
|
|
}
|
|
|
|
|
2020-10-19 15:24:13 +02:00
|
|
|
if (eventset)
|
|
|
|
{
|
2022-03-22 08:46:02 +01:00
|
|
|
sensor_pollnotify_one(user, eventset);
|
2020-10-19 15:24:13 +02:00
|
|
|
}
|
|
|
|
}
|
2022-03-22 08:46:02 +01:00
|
|
|
else
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
2022-03-22 08:46:02 +01:00
|
|
|
user->fds = NULL;
|
|
|
|
fds->priv = NULL;
|
2020-10-19 15:24:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semgive(upper);
|
2020-10-19 15:24:13 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-11 10:05:47 +01:00
|
|
|
static ssize_t sensor_push_event(FAR void *priv, FAR const void *data,
|
|
|
|
size_t bytes)
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
|
|
|
FAR struct sensor_upperhalf_s *upper = priv;
|
2022-07-21 04:32:29 +02:00
|
|
|
FAR struct sensor_lowerhalf_s *lower = upper->lower;
|
2022-03-22 08:46:02 +01:00
|
|
|
FAR struct sensor_user_s *user;
|
2022-03-23 10:09:46 +01:00
|
|
|
unsigned long envcount;
|
2020-10-19 15:24:13 +02:00
|
|
|
int semcount;
|
2022-03-11 10:05:47 +01:00
|
|
|
int ret;
|
2020-10-19 15:24:13 +02:00
|
|
|
|
2022-03-23 10:09:46 +01:00
|
|
|
envcount = bytes / upper->state.esize;
|
|
|
|
if (!envcount || bytes != envcount * upper->state.esize)
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
2022-03-11 10:05:47 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semtake(upper);
|
2022-07-21 04:32:29 +02:00
|
|
|
if (!circbuf_is_init(&upper->buffer))
|
|
|
|
{
|
|
|
|
/* Initialize sensor buffer when data is first generated */
|
|
|
|
|
2022-03-23 10:09:46 +01:00
|
|
|
ret = circbuf_init(&upper->buffer, NULL, lower->nbuffer *
|
|
|
|
upper->state.esize);
|
2022-07-21 04:32:29 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semgive(upper);
|
2022-07-21 04:32:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 11:40:37 +01:00
|
|
|
circbuf_overwrite(&upper->buffer, data, bytes);
|
2022-03-23 10:09:46 +01:00
|
|
|
upper->state.generation += envcount;
|
2022-03-22 08:46:02 +01:00
|
|
|
list_for_every_entry(&upper->userlist, user, struct sensor_user_s, node)
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
2022-03-23 10:09:46 +01:00
|
|
|
if (sensor_is_updated(upper->state.generation, user->generation))
|
2022-03-22 08:46:02 +01:00
|
|
|
{
|
|
|
|
nxsem_get_value(&user->buffersem, &semcount);
|
|
|
|
if (semcount < 1)
|
|
|
|
{
|
|
|
|
nxsem_post(&user->buffersem);
|
|
|
|
}
|
|
|
|
|
|
|
|
sensor_pollnotify_one(user, POLLIN);
|
|
|
|
}
|
2020-10-19 15:24:13 +02:00
|
|
|
}
|
|
|
|
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semgive(upper);
|
2022-03-11 10:05:47 +01:00
|
|
|
return bytes;
|
2020-10-19 15:24:13 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 04:23:39 +01:00
|
|
|
static void sensor_notify_event(FAR void *priv)
|
|
|
|
{
|
|
|
|
FAR struct sensor_upperhalf_s *upper = priv;
|
2022-03-22 08:46:02 +01:00
|
|
|
FAR struct sensor_user_s *user;
|
2020-11-04 04:23:39 +01:00
|
|
|
int semcount;
|
|
|
|
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semtake(upper);
|
2022-03-22 08:46:02 +01:00
|
|
|
list_for_every_entry(&upper->userlist, user, struct sensor_user_s, node)
|
2020-11-04 04:23:39 +01:00
|
|
|
{
|
2022-03-22 08:46:02 +01:00
|
|
|
nxsem_get_value(&user->buffersem, &semcount);
|
|
|
|
if (semcount < 1)
|
|
|
|
{
|
|
|
|
nxsem_post(&user->buffersem);
|
|
|
|
}
|
|
|
|
|
|
|
|
sensor_pollnotify_one(user, POLLIN);
|
2020-11-04 04:23:39 +01:00
|
|
|
}
|
|
|
|
|
2022-04-29 17:08:59 +02:00
|
|
|
sensor_semgive(upper);
|
2020-11-04 04:23:39 +01:00
|
|
|
}
|
|
|
|
|
2020-10-19 15:24:13 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sensor_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function binds an instance of a "lower half" Sensor driver with the
|
|
|
|
* "upper half" Sensor device and registers that device so that can be used
|
|
|
|
* by application code.
|
|
|
|
*
|
|
|
|
* We will register the chararter device by node name format based on the
|
|
|
|
* type of sensor. Multiple types of the same type are distinguished by
|
|
|
|
* numbers. eg: accel0, accel1
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2020-11-02 11:02:04 +01:00
|
|
|
* dev - A pointer to an instance of lower half sensor driver. This
|
|
|
|
* instance is bound to the sensor driver and must persists as long
|
|
|
|
* as the driver persists.
|
|
|
|
* devno - The user specifies which device of this type, from 0. If the
|
|
|
|
* devno alerady exists, -EEXIST will be returned.
|
2020-10-19 15:24:13 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK if the driver was successfully register; A negated errno value is
|
|
|
|
* returned on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-11-02 11:02:04 +01:00
|
|
|
int sensor_register(FAR struct sensor_lowerhalf_s *lower, int devno)
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
2022-03-29 05:39:44 +02:00
|
|
|
char path[PATH_MAX];
|
2020-11-22 09:04:37 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(lower != NULL);
|
|
|
|
|
2022-03-29 05:39:44 +02:00
|
|
|
snprintf(path, PATH_MAX, DEVNAME_FMT,
|
2020-11-22 09:04:37 +01:00
|
|
|
g_sensor_info[lower->type].name,
|
|
|
|
lower->uncalibrated ? DEVNAME_UNCAL : "",
|
|
|
|
devno);
|
|
|
|
return sensor_custom_register(lower, path,
|
|
|
|
g_sensor_info[lower->type].esize);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sensor_custom_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function binds an instance of a "lower half" Sensor driver with the
|
|
|
|
* "upper half" Sensor device and registers that device so that can be used
|
|
|
|
* by application code.
|
|
|
|
*
|
|
|
|
* You can register the character device type by specific path and esize.
|
|
|
|
* This API corresponds to the sensor_custom_unregister.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - A pointer to an instance of lower half sensor driver. This
|
|
|
|
* instance is bound to the sensor driver and must persists as long
|
|
|
|
* as the driver persists.
|
|
|
|
* path - The user specifies path of device. ex: /dev/sensor/xxx.
|
|
|
|
* esize - The element size of intermediate circular buffer.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK if the driver was successfully register; A negated errno value is
|
|
|
|
* returned on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int sensor_custom_register(FAR struct sensor_lowerhalf_s *lower,
|
2022-03-23 10:09:46 +01:00
|
|
|
FAR const char *path, unsigned long esize)
|
2020-11-22 09:04:37 +01:00
|
|
|
{
|
|
|
|
FAR struct sensor_upperhalf_s *upper;
|
2020-10-19 15:24:13 +02:00
|
|
|
int ret = -EINVAL;
|
|
|
|
|
|
|
|
DEBUGASSERT(lower != NULL);
|
|
|
|
|
2020-11-22 09:04:37 +01:00
|
|
|
if (lower->type >= SENSOR_TYPE_COUNT || !esize)
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
2020-11-22 09:04:37 +01:00
|
|
|
snerr("ERROR: type is invalid\n");
|
2020-10-19 15:24:13 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate the upper-half data structure */
|
|
|
|
|
|
|
|
upper = kmm_zalloc(sizeof(struct sensor_upperhalf_s));
|
|
|
|
if (!upper)
|
|
|
|
{
|
|
|
|
snerr("ERROR: Allocation failed\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the upper-half data structure */
|
|
|
|
|
2022-03-22 08:46:02 +01:00
|
|
|
list_initialize(&upper->userlist);
|
2022-03-23 10:09:46 +01:00
|
|
|
upper->state.esize = esize;
|
2022-03-22 08:46:02 +01:00
|
|
|
upper->state.min_interval = ULONG_MAX;
|
|
|
|
upper->state.min_latency = ULONG_MAX;
|
|
|
|
if (lower->ops->activate)
|
|
|
|
{
|
|
|
|
upper->state.nadvertisers = 1;
|
|
|
|
}
|
2020-10-19 15:24:13 +02:00
|
|
|
|
2022-04-29 17:08:59 +02:00
|
|
|
upper->semholder = INVALID_PROCESS_ID;
|
2022-03-22 08:46:02 +01:00
|
|
|
nxsem_init(&upper->exclsem, 0, 1);
|
2020-10-19 15:24:13 +02:00
|
|
|
|
|
|
|
/* Bind the lower half data structure member */
|
|
|
|
|
|
|
|
lower->priv = upper;
|
|
|
|
|
2020-11-04 04:23:39 +01:00
|
|
|
if (!lower->ops->fetch)
|
|
|
|
{
|
2022-03-23 10:09:46 +01:00
|
|
|
if (!lower->nbuffer)
|
2020-11-22 09:04:37 +01:00
|
|
|
{
|
2022-03-23 10:09:46 +01:00
|
|
|
lower->nbuffer = 1;
|
2020-11-04 04:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lower->push_event = sensor_push_event;
|
|
|
|
}
|
|
|
|
else
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
2020-11-04 04:23:39 +01:00
|
|
|
lower->notify_event = sensor_notify_event;
|
2022-03-23 10:09:46 +01:00
|
|
|
lower->nbuffer = 0;
|
2020-10-19 15:24:13 +02:00
|
|
|
}
|
|
|
|
|
2022-04-08 16:46:29 +02:00
|
|
|
#ifdef CONFIG_SENSORS_RPMSG
|
|
|
|
lower = sensor_rpmsg_register(lower, path);
|
|
|
|
if (lower == NULL)
|
|
|
|
{
|
|
|
|
ret = -EIO;
|
|
|
|
goto drv_err;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-06-14 05:44:24 +02:00
|
|
|
upper->state.nbuffer = lower->nbuffer;
|
2022-04-08 16:46:29 +02:00
|
|
|
upper->lower = lower;
|
2022-06-14 05:44:24 +02:00
|
|
|
sninfo("Registering %s\n", path);
|
|
|
|
ret = register_driver(path, &g_sensor_fops, 0666, upper);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
goto drv_err;
|
|
|
|
}
|
|
|
|
|
2020-10-19 15:24:13 +02:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
drv_err:
|
|
|
|
nxsem_destroy(&upper->exclsem);
|
|
|
|
|
|
|
|
kmm_free(upper);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sensor_unregister
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function unregister character node and release all resource about
|
|
|
|
* upper half driver.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2020-11-02 11:02:04 +01:00
|
|
|
* dev - A pointer to an instance of lower half sensor driver. This
|
|
|
|
* instance is bound to the sensor driver and must persists as long
|
|
|
|
* as the driver persists.
|
|
|
|
* devno - The user specifies which device of this type, from 0.
|
2020-10-19 15:24:13 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-11-02 11:02:04 +01:00
|
|
|
void sensor_unregister(FAR struct sensor_lowerhalf_s *lower, int devno)
|
2020-10-19 15:24:13 +02:00
|
|
|
{
|
2022-03-29 05:39:44 +02:00
|
|
|
char path[PATH_MAX];
|
2020-10-19 15:24:13 +02:00
|
|
|
|
2022-03-29 05:39:44 +02:00
|
|
|
snprintf(path, PATH_MAX, DEVNAME_FMT,
|
2020-11-22 09:04:37 +01:00
|
|
|
g_sensor_info[lower->type].name,
|
|
|
|
lower->uncalibrated ? DEVNAME_UNCAL : "",
|
|
|
|
devno);
|
|
|
|
sensor_custom_unregister(lower, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sensor_custom_unregister
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function unregister character node and release all resource about
|
|
|
|
* upper half driver. This API corresponds to the sensor_custom_register.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - A pointer to an instance of lower half sensor driver. This
|
|
|
|
* instance is bound to the sensor driver and must persists as long
|
|
|
|
* as the driver persists.
|
|
|
|
* path - The user specifies path of device, ex: /dev/sensor/xxx
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void sensor_custom_unregister(FAR struct sensor_lowerhalf_s *lower,
|
|
|
|
FAR const char *path)
|
|
|
|
{
|
|
|
|
FAR struct sensor_upperhalf_s *upper;
|
|
|
|
|
2020-10-19 15:24:13 +02:00
|
|
|
DEBUGASSERT(lower != NULL);
|
|
|
|
DEBUGASSERT(lower->priv != NULL);
|
|
|
|
|
|
|
|
upper = lower->priv;
|
|
|
|
|
|
|
|
sninfo("UnRegistering %s\n", path);
|
|
|
|
unregister_driver(path);
|
|
|
|
|
2022-04-08 16:46:29 +02:00
|
|
|
#ifdef CONFIG_SENSORS_RPMSG
|
|
|
|
sensor_rpmsg_unregister(lower);
|
|
|
|
#endif
|
|
|
|
|
2020-10-19 15:24:13 +02:00
|
|
|
nxsem_destroy(&upper->exclsem);
|
2022-03-22 08:46:02 +01:00
|
|
|
if (circbuf_is_init(&upper->buffer))
|
|
|
|
{
|
|
|
|
circbuf_uninit(&upper->buffer);
|
|
|
|
}
|
2020-10-19 15:24:13 +02:00
|
|
|
|
|
|
|
kmm_free(upper);
|
|
|
|
}
|