2020-09-08 12:44:29 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/note/noteram_driver.c
|
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* 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
|
2020-09-08 12:44:29 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2020-09-08 12:44:29 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* 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.
|
2020-09-08 12:44:29 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sched.h>
|
2020-09-09 02:06:38 +02:00
|
|
|
#include <fcntl.h>
|
2020-09-08 12:44:29 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2020-10-28 02:45:30 +01:00
|
|
|
#include <string.h>
|
2020-09-08 12:44:29 +02:00
|
|
|
|
|
|
|
#include <nuttx/spinlock.h>
|
2020-10-28 02:45:30 +01:00
|
|
|
#include <nuttx/sched.h>
|
2020-09-08 12:44:29 +02:00
|
|
|
#include <nuttx/sched_note.h>
|
2023-04-27 13:42:27 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2022-12-16 11:31:28 +01:00
|
|
|
#include <nuttx/note/note_driver.h>
|
2020-09-08 12:44:29 +02:00
|
|
|
#include <nuttx/note/noteram_driver.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
struct noteram_driver_s
|
2020-09-08 12:44:29 +02:00
|
|
|
{
|
2023-04-27 13:42:27 +02:00
|
|
|
struct note_driver_s driver;
|
2023-04-20 12:27:15 +02:00
|
|
|
FAR uint8_t *ni_buffer;
|
|
|
|
size_t ni_bufsize;
|
2022-03-26 21:51:22 +01:00
|
|
|
unsigned int ni_overwrite;
|
2020-09-08 12:44:29 +02:00
|
|
|
volatile unsigned int ni_head;
|
|
|
|
volatile unsigned int ni_tail;
|
2020-09-09 02:06:38 +02:00
|
|
|
volatile unsigned int ni_read;
|
2023-04-27 13:42:27 +02:00
|
|
|
spinlock_t lock;
|
2020-09-08 12:44:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-09-09 02:06:38 +02:00
|
|
|
static int noteram_open(FAR struct file *filep);
|
2020-09-08 12:44:29 +02:00
|
|
|
static ssize_t noteram_read(FAR struct file *filep,
|
|
|
|
FAR char *buffer, size_t buflen);
|
2020-09-09 02:06:38 +02:00
|
|
|
static int noteram_ioctl(struct file *filep, int cmd, unsigned long arg);
|
2022-12-16 11:31:28 +01:00
|
|
|
static void noteram_add(FAR struct note_driver_s *drv,
|
|
|
|
FAR const void *note, size_t len);
|
2020-09-08 12:44:29 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_noteram_fops =
|
|
|
|
{
|
2020-09-09 02:06:38 +02:00
|
|
|
noteram_open, /* open */
|
2020-09-08 12:44:29 +02:00
|
|
|
NULL, /* close */
|
|
|
|
noteram_read, /* read */
|
|
|
|
NULL, /* write */
|
|
|
|
NULL, /* seek */
|
2020-09-09 02:06:38 +02:00
|
|
|
noteram_ioctl, /* ioctl */
|
2020-09-08 12:44:29 +02:00
|
|
|
};
|
|
|
|
|
2023-04-20 12:27:15 +02:00
|
|
|
static uint8_t g_ramnote_buffer[CONFIG_DRIVERS_NOTERAM_BUFSIZE];
|
|
|
|
|
2022-12-16 11:31:28 +01:00
|
|
|
static const struct note_driver_ops_s g_noteram_ops =
|
|
|
|
{
|
|
|
|
noteram_add
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
struct noteram_driver_s g_noteram_driver =
|
2022-12-16 11:31:28 +01:00
|
|
|
{
|
2023-04-27 13:42:27 +02:00
|
|
|
{&g_noteram_ops},
|
|
|
|
g_ramnote_buffer,
|
|
|
|
CONFIG_DRIVERS_NOTERAM_BUFSIZE,
|
|
|
|
#ifdef CONFIG_DRIVERS_NOTERAM_DEFAULT_NOOVERWRITE
|
|
|
|
NOTERAM_MODE_OVERWRITE_DISABLE
|
|
|
|
#else
|
|
|
|
NOTERAM_MODE_OVERWRITE_ENABLE
|
|
|
|
#endif
|
2022-12-16 11:31:28 +01:00
|
|
|
};
|
|
|
|
|
2020-09-08 12:44:29 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-09-09 02:06:38 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: noteram_buffer_clear
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Clear all contents of the circular buffer.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
static void noteram_buffer_clear(FAR struct noteram_driver_s *drv)
|
2020-09-09 02:06:38 +02:00
|
|
|
{
|
2023-04-27 13:42:27 +02:00
|
|
|
drv->ni_tail = drv->ni_head;
|
|
|
|
drv->ni_read = drv->ni_head;
|
2020-09-09 02:06:38 +02:00
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
if (drv->ni_overwrite == NOTERAM_MODE_OVERWRITE_OVERFLOW)
|
2020-09-09 02:06:38 +02:00
|
|
|
{
|
2023-04-27 13:42:27 +02:00
|
|
|
drv->ni_overwrite = NOTERAM_MODE_OVERWRITE_DISABLE;
|
2020-09-09 02:06:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 12:44:29 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: noteram_next
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the circular buffer index at offset from the specified index
|
|
|
|
* value, handling wraparound
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* ndx - Old circular buffer index
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* New circular buffer index
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
static inline unsigned int noteram_next(FAR struct noteram_driver_s *drv,
|
|
|
|
unsigned int ndx,
|
2020-09-08 12:44:29 +02:00
|
|
|
unsigned int offset)
|
|
|
|
{
|
|
|
|
ndx += offset;
|
2023-04-27 13:42:27 +02:00
|
|
|
if (ndx >= drv->ni_bufsize)
|
2020-09-08 12:44:29 +02:00
|
|
|
{
|
2023-04-27 13:42:27 +02:00
|
|
|
ndx -= drv->ni_bufsize;
|
2020-09-08 12:44:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ndx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: noteram_length
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Length of data currently in circular buffer.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Length of data currently in circular buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
static unsigned int noteram_length(FAR struct noteram_driver_s *drv)
|
2020-09-08 12:44:29 +02:00
|
|
|
{
|
2023-04-27 13:42:27 +02:00
|
|
|
unsigned int head = drv->ni_head;
|
|
|
|
unsigned int tail = drv->ni_tail;
|
2020-09-08 12:44:29 +02:00
|
|
|
|
|
|
|
if (tail > head)
|
|
|
|
{
|
2023-04-27 13:42:27 +02:00
|
|
|
head += drv->ni_bufsize;
|
2020-09-08 12:44:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return head - tail;
|
|
|
|
}
|
2020-09-09 02:06:38 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: noteram_unread_length
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Length of unread data currently in circular buffer.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Length of unread data currently in circular buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
static unsigned int noteram_unread_length(FAR struct noteram_driver_s *drv)
|
2020-09-09 02:06:38 +02:00
|
|
|
{
|
2023-04-27 13:42:27 +02:00
|
|
|
unsigned int head = drv->ni_head;
|
|
|
|
unsigned int read = drv->ni_read;
|
2020-09-09 02:06:38 +02:00
|
|
|
|
|
|
|
if (read > head)
|
|
|
|
{
|
2023-04-27 13:42:27 +02:00
|
|
|
head += drv->ni_bufsize;
|
2020-09-09 02:06:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return head - read;
|
|
|
|
}
|
2020-09-08 12:44:29 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: noteram_remove
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Remove the variable length note from the tail of the circular buffer
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* We are within a critical section.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
static void noteram_remove(FAR struct noteram_driver_s *drv)
|
2020-09-08 12:44:29 +02:00
|
|
|
{
|
|
|
|
unsigned int tail;
|
|
|
|
unsigned int length;
|
|
|
|
|
|
|
|
/* Get the tail index of the circular buffer */
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
tail = drv->ni_tail;
|
|
|
|
DEBUGASSERT(tail < drv->ni_bufsize);
|
2020-09-08 12:44:29 +02:00
|
|
|
|
|
|
|
/* Get the length of the note at the tail index */
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
length = drv->ni_buffer[tail];
|
|
|
|
DEBUGASSERT(length <= noteram_length(drv));
|
2020-09-08 12:44:29 +02:00
|
|
|
|
|
|
|
/* Increment the tail index to remove the entire note from the circular
|
|
|
|
* buffer.
|
|
|
|
*/
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
if (drv->ni_read == drv->ni_tail)
|
2020-09-09 02:06:38 +02:00
|
|
|
{
|
|
|
|
/* The read index also needs increment. */
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
drv->ni_read = noteram_next(drv, tail, length);
|
2020-09-09 02:06:38 +02:00
|
|
|
}
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
drv->ni_tail = noteram_next(drv, tail, length);
|
2020-09-08 12:44:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: noteram_get
|
|
|
|
*
|
|
|
|
* Description:
|
2020-09-09 02:06:38 +02:00
|
|
|
* Get the next note from the read index of the circular buffer.
|
2020-09-08 12:44:29 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* buffer - Location to return the next note
|
|
|
|
* buflen - The length of the user provided buffer.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, the positive, non-zero length of the return note is
|
|
|
|
* provided. Zero is returned only if the circular buffer is empty. A
|
|
|
|
* negated errno value is returned in the event of any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
static ssize_t noteram_get(FAR struct noteram_driver_s *drv,
|
|
|
|
FAR uint8_t *buffer, size_t buflen)
|
2020-09-08 12:44:29 +02:00
|
|
|
{
|
|
|
|
FAR struct note_common_s *note;
|
|
|
|
unsigned int remaining;
|
2020-09-09 02:06:38 +02:00
|
|
|
unsigned int read;
|
2020-09-08 12:44:29 +02:00
|
|
|
ssize_t notelen;
|
|
|
|
size_t circlen;
|
|
|
|
|
|
|
|
DEBUGASSERT(buffer != NULL);
|
|
|
|
|
|
|
|
/* Verify that the circular buffer is not empty */
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
circlen = noteram_unread_length(drv);
|
2020-09-08 12:44:29 +02:00
|
|
|
if (circlen <= 0)
|
|
|
|
{
|
2022-12-28 11:00:06 +01:00
|
|
|
return 0;
|
2020-09-08 12:44:29 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 02:06:38 +02:00
|
|
|
/* Get the read index of the circular buffer */
|
2020-09-08 12:44:29 +02:00
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
read = drv->ni_read;
|
|
|
|
DEBUGASSERT(read < drv->ni_bufsize);
|
2020-09-08 12:44:29 +02:00
|
|
|
|
2020-09-09 02:06:38 +02:00
|
|
|
/* Get the length of the note at the read index */
|
2020-09-08 12:44:29 +02:00
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
note = (FAR struct note_common_s *)&drv->ni_buffer[read];
|
2020-09-08 12:44:29 +02:00
|
|
|
notelen = note->nc_length;
|
|
|
|
DEBUGASSERT(notelen <= circlen);
|
|
|
|
|
|
|
|
/* Is the user buffer large enough to hold the note? */
|
|
|
|
|
|
|
|
if (buflen < notelen)
|
|
|
|
{
|
2020-09-09 02:06:38 +02:00
|
|
|
/* Skip the large note so that we do not get constipated. */
|
2020-09-08 12:44:29 +02:00
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
drv->ni_read = noteram_next(drv, read, notelen);
|
2020-09-08 12:44:29 +02:00
|
|
|
|
|
|
|
/* and return an error */
|
|
|
|
|
2022-12-28 11:00:06 +01:00
|
|
|
return -EFBIG;
|
2020-09-08 12:44:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop until the note has been transferred to the user buffer */
|
|
|
|
|
|
|
|
remaining = (unsigned int)notelen;
|
|
|
|
while (remaining > 0)
|
|
|
|
{
|
2020-09-09 02:06:38 +02:00
|
|
|
/* Copy the next byte at the read index */
|
2020-09-08 12:44:29 +02:00
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
*buffer++ = drv->ni_buffer[read];
|
2020-09-08 12:44:29 +02:00
|
|
|
|
|
|
|
/* Adjust indices and counts */
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
read = noteram_next(drv, read, 1);
|
2020-09-08 12:44:29 +02:00
|
|
|
remaining--;
|
|
|
|
}
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
drv->ni_read = read;
|
2020-09-08 12:44:29 +02:00
|
|
|
|
|
|
|
return notelen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: noteram_size
|
|
|
|
*
|
|
|
|
* Description:
|
2020-09-09 02:06:38 +02:00
|
|
|
* Return the size of the next note at the read index of the circular
|
|
|
|
* buffer.
|
2020-09-08 12:44:29 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned if the circular buffer is empty. Otherwise, the size
|
|
|
|
* of the next note is returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
static ssize_t noteram_size(FAR struct noteram_driver_s *drv)
|
2020-09-08 12:44:29 +02:00
|
|
|
{
|
|
|
|
FAR struct note_common_s *note;
|
2020-09-09 02:06:38 +02:00
|
|
|
unsigned int read;
|
2020-09-08 12:44:29 +02:00
|
|
|
ssize_t notelen;
|
|
|
|
size_t circlen;
|
|
|
|
|
|
|
|
/* Verify that the circular buffer is not empty */
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
circlen = noteram_unread_length(drv);
|
2020-09-08 12:44:29 +02:00
|
|
|
if (circlen <= 0)
|
|
|
|
{
|
2022-12-28 11:00:06 +01:00
|
|
|
return 0;
|
2020-09-08 12:44:29 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 02:06:38 +02:00
|
|
|
/* Get the read index of the circular buffer */
|
2020-09-08 12:44:29 +02:00
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
read = drv->ni_read;
|
|
|
|
DEBUGASSERT(read < drv->ni_bufsize);
|
2020-09-08 12:44:29 +02:00
|
|
|
|
2020-09-09 02:06:38 +02:00
|
|
|
/* Get the length of the note at the read index */
|
2020-09-08 12:44:29 +02:00
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
note = (FAR struct note_common_s *)&drv->ni_buffer[read];
|
2020-09-08 12:44:29 +02:00
|
|
|
notelen = note->nc_length;
|
|
|
|
DEBUGASSERT(notelen <= circlen);
|
|
|
|
|
|
|
|
return notelen;
|
|
|
|
}
|
|
|
|
|
2020-09-09 02:06:38 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: noteram_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int noteram_open(FAR struct file *filep)
|
|
|
|
{
|
2023-04-27 13:42:27 +02:00
|
|
|
FAR struct noteram_driver_s *drv =
|
|
|
|
(FAR struct noteram_driver_s *)filep->f_inode->i_private;
|
|
|
|
|
2020-09-09 02:06:38 +02:00
|
|
|
/* Reset the read index of the circular buffer */
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
filep->f_priv = drv;
|
|
|
|
drv->ni_read = drv->ni_tail;
|
2020-09-09 02:06:38 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2020-09-08 12:44:29 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: noteram_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t noteram_read(FAR struct file *filep,
|
|
|
|
FAR char *buffer, size_t buflen)
|
|
|
|
{
|
|
|
|
ssize_t notelen;
|
|
|
|
ssize_t retlen ;
|
2022-12-28 11:00:06 +01:00
|
|
|
irqstate_t flags;
|
2023-04-27 13:42:27 +02:00
|
|
|
FAR struct noteram_driver_s *drv =
|
|
|
|
(FAR struct noteram_driver_s *)filep->f_priv;
|
2020-09-08 12:44:29 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(filep != 0 && buffer != NULL && buflen > 0);
|
|
|
|
|
|
|
|
/* Then loop, adding as many notes as possible to the user buffer. */
|
|
|
|
|
|
|
|
retlen = 0;
|
2023-04-27 13:42:27 +02:00
|
|
|
flags = spin_lock_irqsave_wo_note(&drv->lock);
|
2020-09-08 12:44:29 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Get the next note (removing it from the buffer) */
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
notelen = noteram_get(drv, (FAR uint8_t *)buffer, buflen);
|
2020-09-08 12:44:29 +02:00
|
|
|
if (notelen < 0)
|
|
|
|
{
|
|
|
|
/* We were unable to read the next note, probably because it will
|
|
|
|
* not fit into the user buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (retlen == 0)
|
|
|
|
{
|
|
|
|
/* If nothing was read then report the error. Otherwise,
|
|
|
|
* just silently drop the note.
|
|
|
|
*/
|
|
|
|
|
|
|
|
retlen = notelen;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update pointers from the note that was transferred */
|
|
|
|
|
|
|
|
retlen += notelen;
|
|
|
|
buffer += notelen;
|
|
|
|
buflen -= notelen;
|
|
|
|
|
|
|
|
/* Will the next note fit? There is a race here and even if the next
|
|
|
|
* note will fit, it may fail still when noteram_get() is called.
|
|
|
|
*
|
|
|
|
* It won't fit (or an error occurred). Return what we have without
|
|
|
|
* trying to get the next note (which would cause it to be deleted).
|
|
|
|
*/
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
notelen = noteram_size(drv);
|
2020-09-08 12:44:29 +02:00
|
|
|
}
|
|
|
|
while (notelen > 0 && notelen <= buflen);
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
spin_unlock_irqrestore_wo_note(&drv->lock, flags);
|
2020-09-08 12:44:29 +02:00
|
|
|
return retlen;
|
|
|
|
}
|
|
|
|
|
2020-09-09 02:06:38 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: noteram_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-12-26 06:18:33 +01:00
|
|
|
static int noteram_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
2020-09-09 02:06:38 +02:00
|
|
|
{
|
|
|
|
int ret = -ENOSYS;
|
2023-04-27 13:42:27 +02:00
|
|
|
FAR struct noteram_driver_s *drv =
|
|
|
|
(FAR struct noteram_driver_s *)filep->f_priv;
|
|
|
|
irqstate_t flags = spin_lock_irqsave_wo_note(&drv->lock);
|
2020-09-09 02:06:38 +02:00
|
|
|
|
|
|
|
/* Handle the ioctl commands */
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* NOTERAM_CLEAR
|
|
|
|
* - Clear all contents of the circular buffer
|
|
|
|
* Argument: Ignored
|
|
|
|
*/
|
|
|
|
|
|
|
|
case NOTERAM_CLEAR:
|
2023-04-27 13:42:27 +02:00
|
|
|
noteram_buffer_clear(drv);
|
2020-09-09 02:06:38 +02:00
|
|
|
ret = OK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* NOTERAM_GETMODE
|
|
|
|
* - Get overwrite mode
|
|
|
|
* Argument: A writable pointer to unsigned int
|
|
|
|
*/
|
|
|
|
|
|
|
|
case NOTERAM_GETMODE:
|
|
|
|
if (arg == 0)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-27 13:42:27 +02:00
|
|
|
*(unsigned int *)arg = drv->ni_overwrite;
|
2020-09-09 02:06:38 +02:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* NOTERAM_SETMODE
|
|
|
|
* - Set overwrite mode
|
|
|
|
* Argument: A read-only pointer to unsigned int
|
|
|
|
*/
|
|
|
|
|
|
|
|
case NOTERAM_SETMODE:
|
|
|
|
if (arg == 0)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-27 13:42:27 +02:00
|
|
|
drv->ni_overwrite = *(unsigned int *)arg;
|
2020-09-09 02:06:38 +02:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-12-26 06:18:33 +01:00
|
|
|
#ifdef NOTERAM_GETTASKNAME
|
2020-10-28 02:45:30 +01:00
|
|
|
/* NOTERAM_GETTASKNAME
|
|
|
|
* - Get task name string
|
|
|
|
* Argument: A writable pointer to struct note_get_taskname_s
|
|
|
|
* Result: If -ESRCH, the corresponding task name doesn't
|
|
|
|
* exist.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case NOTERAM_GETTASKNAME:
|
|
|
|
{
|
2022-12-26 06:18:33 +01:00
|
|
|
FAR struct noteram_get_taskname_s *param;
|
2020-10-28 02:45:30 +01:00
|
|
|
|
2022-12-26 06:18:33 +01:00
|
|
|
if (arg == 0)
|
2020-10-28 02:45:30 +01:00
|
|
|
{
|
2022-12-26 06:18:33 +01:00
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
2020-10-28 02:45:30 +01:00
|
|
|
}
|
2022-12-26 06:18:33 +01:00
|
|
|
|
|
|
|
param = (FAR struct noteram_get_taskname_s *)arg;
|
|
|
|
ret = note_get_taskname(param->pid, param->taskname);
|
2020-10-28 02:45:30 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2020-09-09 02:06:38 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
spin_unlock_irqrestore_wo_note(&drv->lock, flags);
|
2020-09-09 02:06:38 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-09-08 12:44:29 +02:00
|
|
|
/****************************************************************************
|
2022-12-16 11:31:28 +01:00
|
|
|
* Name: noteram_add
|
2020-09-08 12:44:29 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Add the variable length note to the transport layer
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* note - The note buffer
|
|
|
|
* notelen - The buffer length
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* We are within a critical section.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
static void noteram_add(FAR struct note_driver_s *driver,
|
2022-12-16 11:31:28 +01:00
|
|
|
FAR const void *note, size_t notelen)
|
2020-09-08 12:44:29 +02:00
|
|
|
{
|
|
|
|
FAR const char *buf = note;
|
2023-04-27 13:42:27 +02:00
|
|
|
FAR struct noteram_driver_s *drv = (FAR struct noteram_driver_s *)driver;
|
2020-09-08 12:44:29 +02:00
|
|
|
unsigned int head;
|
2023-03-02 09:26:19 +01:00
|
|
|
unsigned int remain;
|
|
|
|
unsigned int space;
|
2020-09-09 02:06:38 +02:00
|
|
|
irqstate_t flags;
|
2020-09-08 12:44:29 +02:00
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
flags = spin_lock_irqsave_wo_note(&drv->lock);
|
2020-09-08 12:44:29 +02:00
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
if (drv->ni_overwrite == NOTERAM_MODE_OVERWRITE_OVERFLOW)
|
2020-09-09 02:06:38 +02:00
|
|
|
{
|
2023-04-27 13:42:27 +02:00
|
|
|
spin_unlock_irqrestore_wo_note(&drv->lock, flags);
|
2020-09-09 02:06:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
DEBUGASSERT(note != NULL && notelen < drv->ni_bufsize);
|
|
|
|
remain = drv->ni_bufsize - noteram_length(drv);
|
2020-09-08 12:44:29 +02:00
|
|
|
|
2023-03-02 09:26:19 +01:00
|
|
|
if (remain < notelen)
|
2020-09-08 12:44:29 +02:00
|
|
|
{
|
2023-04-27 13:42:27 +02:00
|
|
|
if (drv->ni_overwrite == NOTERAM_MODE_OVERWRITE_DISABLE)
|
2020-09-08 12:44:29 +02:00
|
|
|
{
|
2023-03-02 09:26:19 +01:00
|
|
|
/* Stop recording if not in overwrite mode */
|
2020-09-09 02:06:38 +02:00
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
drv->ni_overwrite = NOTERAM_MODE_OVERWRITE_OVERFLOW;
|
|
|
|
spin_unlock_irqrestore_wo_note(&drv->lock, flags);
|
2023-03-02 09:26:19 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-09-09 02:06:38 +02:00
|
|
|
|
2023-03-02 09:26:19 +01:00
|
|
|
/* Remove the note at the tail index , make sure there is enough space
|
|
|
|
*/
|
2020-09-08 12:44:29 +02:00
|
|
|
|
2023-03-02 09:26:19 +01:00
|
|
|
do
|
|
|
|
{
|
2023-04-27 13:42:27 +02:00
|
|
|
noteram_remove(drv);
|
|
|
|
remain = drv->ni_bufsize - noteram_length(drv);
|
2020-09-08 12:44:29 +02:00
|
|
|
}
|
2023-03-02 09:26:19 +01:00
|
|
|
while (remain < notelen);
|
2020-09-08 12:44:29 +02:00
|
|
|
}
|
|
|
|
|
2023-04-27 13:42:27 +02:00
|
|
|
head = drv->ni_head;
|
|
|
|
space = drv->ni_bufsize - head;
|
2023-03-02 09:26:19 +01:00
|
|
|
space = space < notelen ? space : notelen;
|
2023-04-27 13:42:27 +02:00
|
|
|
memcpy(drv->ni_buffer + head, note, space);
|
|
|
|
memcpy(drv->ni_buffer, buf + space, notelen - space);
|
|
|
|
drv->ni_head = noteram_next(drv, head, notelen);
|
|
|
|
spin_unlock_irqrestore_wo_note(&drv->lock, flags);
|
2020-09-08 12:44:29 +02:00
|
|
|
}
|
|
|
|
|
2022-12-16 11:31:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-09-08 12:44:29 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: noteram_register
|
|
|
|
*
|
|
|
|
* Description:
|
2022-12-14 05:53:09 +01:00
|
|
|
* Register a serial driver at /dev/note/ram that can be used by an
|
2020-09-08 12:44:29 +02:00
|
|
|
* application to read data from the circular note buffer.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero on succress. A negated errno value is returned on a failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int noteram_register(void)
|
|
|
|
{
|
2023-04-27 13:42:27 +02:00
|
|
|
return register_driver("/dev/note/ram", &g_noteram_fops, 0666,
|
|
|
|
&g_noteram_driver);
|
2020-09-08 12:44:29 +02:00
|
|
|
}
|
2023-04-27 13:42:27 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: noteram_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register a serial driver at /dev/note/ram that can be used by an
|
|
|
|
* application to read data from the circular note buffer.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* devpath: The path of the Noteram device
|
|
|
|
* bufsize: The size of the circular buffer
|
|
|
|
* overwrite: The overwrite mode
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero on succress. A negated errno value is returned on a failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct note_driver_s *
|
|
|
|
noteram_initialize(FAR const char *devpath, size_t bufsize, bool overwrite)
|
|
|
|
{
|
|
|
|
FAR struct noteram_driver_s *drv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
drv = (FAR struct noteram_driver_s *)kmm_malloc(sizeof(*drv) + bufsize);
|
|
|
|
if (drv == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
drv->driver.ops = &g_noteram_ops;
|
|
|
|
drv->ni_bufsize = bufsize;
|
|
|
|
drv->ni_buffer = (FAR uint8_t *)(drv + 1);
|
|
|
|
drv->ni_overwrite = overwrite;
|
|
|
|
drv->ni_head = 0;
|
|
|
|
drv->ni_tail = 0;
|
|
|
|
drv->ni_read = 0;
|
|
|
|
|
|
|
|
ret = note_driver_register(&drv->driver);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
kmm_free(drv);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (devpath == NULL)
|
|
|
|
{
|
|
|
|
return &drv->driver;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = register_driver(devpath, &g_noteram_fops, 0666, drv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
kmm_free(drv);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &drv->driver;
|
|
|
|
}
|