From 09ab135d2ffd867fbb7a1473a9cabca1f5c16580 Mon Sep 17 00:00:00 2001 From: likun17 Date: Thu, 11 Jan 2024 18:29:32 +0800 Subject: [PATCH] sensor: Fixed the problem of user information lag in cross-core communication "stublist". When its remote core publishes a message, all subscribed cores will receive the message, but the local core "stublist" does not update the user's "generation" and "bufferpos" parameters. Signed-off-by: likun17 --- drivers/sensors/sensor.c | 50 ++++++++++++++++++++++++++-------- drivers/sensors/sensor_rpmsg.c | 18 ++++++++++++ 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/drivers/sensors/sensor.c b/drivers/sensors/sensor.c index 999064fab6a11..5b812a88f3944 100644 --- a/drivers/sensors/sensor.c +++ b/drivers/sensors/sensor.c @@ -456,9 +456,17 @@ static ssize_t sensor_do_samples(FAR struct sensor_upperhalf_s *upper, if (user->state.interval == UINT32_MAX) { - ret = circbuf_peekat(&upper->buffer, - user->bufferpos * upper->state.esize, - buffer, len); + if (buffer != NULL) + { + ret = circbuf_peekat(&upper->buffer, + user->bufferpos * upper->state.esize, + buffer, len); + } + else + { + ret = len; + } + user->bufferpos += nums; circbuf_peekat(&upper->timing, (user->bufferpos - 1) * TIMING_BUF_ESIZE, @@ -506,9 +514,17 @@ static ssize_t sensor_do_samples(FAR struct sensor_upperhalf_s *upper, ((user->state.generation + user->state.interval) << 1); if (delta >= 0) { - ret += circbuf_peekat(&upper->buffer, - (pos - 1) * upper->state.esize, - buffer + ret, upper->state.esize); + if (buffer != NULL) + { + ret += circbuf_peekat(&upper->buffer, + (pos - 1) * upper->state.esize, + buffer + ret, upper->state.esize); + } + else + { + ret += upper->state.esize; + } + user->bufferpos = pos; user->state.generation += user->state.interval; if (ret >= len) @@ -701,7 +717,7 @@ static ssize_t sensor_read(FAR struct file *filep, FAR char *buffer, FAR struct sensor_user_s *user = filep->f_priv; ssize_t ret; - if (!buffer || !len) + if (!len) { return -EINVAL; } @@ -709,6 +725,11 @@ static ssize_t sensor_read(FAR struct file *filep, FAR char *buffer, nxrmutex_lock(&upper->lock); if (lower->ops->fetch) { + if (buffer == NULL) + { + return -EINVAL; + } + if (!(filep->f_oflags & O_NONBLOCK)) { nxrmutex_unlock(&upper->lock); @@ -738,11 +759,18 @@ static ssize_t sensor_read(FAR struct file *filep, FAR char *buffer, } else if (lower->persist) { - /* Persistent device can get latest old data if not updated. */ + if (buffer == NULL) + { + ret = upper->state.esize; + } + else + { + /* Persistent device can get latest old data if not updated. */ - ret = circbuf_peekat(&upper->buffer, - (user->bufferpos - 1) * upper->state.esize, - buffer, upper->state.esize); + ret = circbuf_peekat(&upper->buffer, + (user->bufferpos - 1) * upper->state.esize, + buffer, upper->state.esize); + } } else { diff --git a/drivers/sensors/sensor_rpmsg.c b/drivers/sensors/sensor_rpmsg.c index 232c2be122a96..6cf1476f15eb6 100644 --- a/drivers/sensors/sensor_rpmsg.c +++ b/drivers/sensors/sensor_rpmsg.c @@ -1132,6 +1132,8 @@ static int sensor_rpmsg_publish_handler(FAR struct rpmsg_endpoint *ept, { FAR struct sensor_rpmsg_data_s *msg = data; FAR struct sensor_rpmsg_cell_s *cell; + FAR struct sensor_rpmsg_stub_s *stub; + FAR struct sensor_rpmsg_stub_s *stmp; FAR struct sensor_rpmsg_dev_s *dev; size_t written = sizeof(*msg); @@ -1154,6 +1156,22 @@ static int sensor_rpmsg_publish_handler(FAR struct rpmsg_endpoint *ept, } dev->push_event(dev->upper, cell->data, cell->len); + + /* When the remote core publishes a message, the subscribed cores will + * receive the message. When the subscribed core publishes a new + * message, it will take away the message published by the remote core, + * so all stublist information needs to be updated. + */ + + sensor_rpmsg_lock(dev); + list_for_every_entry_safe(&dev->stublist, stub, stmp, + struct sensor_rpmsg_stub_s, node) + { + file_read(&stub->file, NULL, cell->len); + } + + sensor_rpmsg_unlock(dev); + written += sizeof(*cell) + cell->len + 0x7; written &= ~0x7; }