2012-02-11 01:32:53 +01:00
|
|
|
/****************************************************************************
|
2012-08-02 02:42:46 +02:00
|
|
|
* drivers/syslog/ramlog.c
|
2012-02-11 01:32:53 +01:00
|
|
|
*
|
2017-09-29 23:24:40 +02:00
|
|
|
* Copyright (C) 2012, 2016-2017 Gregory Nutt. All rights reserved.
|
2012-02-11 01:32:53 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
2014-07-01 23:09:07 +02:00
|
|
|
#include <stdio.h>
|
2012-02-11 01:32:53 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2016-11-03 18:00:47 +01:00
|
|
|
#include <nuttx/arch.h>
|
2012-02-11 01:32:53 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2016-11-03 18:00:47 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2014-04-10 17:29:30 +02:00
|
|
|
#include <nuttx/syslog/ramlog.h>
|
2012-02-11 01:32:53 +01:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
#include <nuttx/irq.h>
|
2012-02-11 01:32:53 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_RAMLOG
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct ramlog_dev_s
|
|
|
|
{
|
2012-02-11 16:58:11 +01:00
|
|
|
#ifndef CONFIG_RAMLOG_NONBLOCKING
|
2012-02-11 01:32:53 +01:00
|
|
|
volatile uint8_t rl_nwaiters; /* Number of threads waiting for data */
|
2012-02-11 16:58:11 +01:00
|
|
|
#endif
|
2012-02-11 01:32:53 +01:00
|
|
|
volatile uint16_t rl_head; /* The head index (where data is added) */
|
2012-02-11 04:50:52 +01:00
|
|
|
volatile uint16_t rl_tail; /* The tail index (where data is removed) */
|
2012-02-11 01:32:53 +01:00
|
|
|
sem_t rl_exclsem; /* Enforces mutually exclusive access */
|
2012-02-11 16:58:11 +01:00
|
|
|
#ifndef CONFIG_RAMLOG_NONBLOCKING
|
2012-02-11 01:32:53 +01:00
|
|
|
sem_t rl_waitsem; /* Used to wait for data */
|
2012-02-11 16:58:11 +01:00
|
|
|
#endif
|
2012-02-11 01:32:53 +01:00
|
|
|
size_t rl_bufsize; /* Size of the RAM buffer */
|
|
|
|
FAR char *rl_buffer; /* Circular RAM buffer */
|
|
|
|
|
|
|
|
/* The following is a list if poll structures of threads waiting for
|
|
|
|
* driver events. The 'struct pollfd' reference for each open is also
|
|
|
|
* retained in the f_priv field of the 'struct file'.
|
|
|
|
*/
|
|
|
|
|
2015-11-05 16:38:42 +01:00
|
|
|
FAR struct pollfd *rl_fds[CONFIG_RAMLOG_NPOLLWAITERS];
|
2012-02-11 01:32:53 +01:00
|
|
|
};
|
|
|
|
|
2016-06-19 21:03:20 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-02-11 15:20:40 +01:00
|
|
|
/* Helper functions */
|
|
|
|
|
2019-11-14 14:40:35 +01:00
|
|
|
#ifndef CONFIG_RAMLOG_NONBLOCKING
|
|
|
|
static int ramlog_readnotify(FAR struct ramlog_dev_s *priv);
|
|
|
|
#endif
|
|
|
|
static void ramlog_pollnotify(FAR struct ramlog_dev_s *priv,
|
|
|
|
pollevent_t eventset);
|
2012-02-11 15:20:40 +01:00
|
|
|
static ssize_t ramlog_addchar(FAR struct ramlog_dev_s *priv, char ch);
|
|
|
|
|
|
|
|
/* Character driver methods */
|
2012-02-11 01:32:53 +01:00
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
static ssize_t ramlog_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t ramlog_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
2012-02-11 01:32:53 +01:00
|
|
|
static int ramlog_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_ramlogfops =
|
|
|
|
{
|
2016-05-26 18:01:15 +02:00
|
|
|
NULL, /* open */
|
|
|
|
NULL, /* close */
|
|
|
|
ramlog_read, /* read */
|
|
|
|
ramlog_write, /* write */
|
|
|
|
NULL, /* seek */
|
2019-05-22 02:57:54 +02:00
|
|
|
NULL, /* ioctl */
|
|
|
|
ramlog_poll /* poll */
|
2016-05-26 18:01:15 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
, NULL /* unlink */
|
2012-02-11 01:32:53 +01:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2012-02-11 15:20:40 +01:00
|
|
|
/* This is the pre-allocated buffer used for the console RAM log and/or
|
|
|
|
* for the syslogging function.
|
|
|
|
*/
|
2012-02-11 04:50:52 +01:00
|
|
|
|
2020-02-17 19:10:15 +01:00
|
|
|
#ifdef CONFIG_RAMLOG_SYSLOG
|
2014-07-01 20:00:40 +02:00
|
|
|
static char g_sysbuffer[CONFIG_RAMLOG_BUFSIZE];
|
2012-02-12 21:27:49 +01:00
|
|
|
|
|
|
|
/* This is the device structure for the console or syslogging function. It
|
2016-06-19 21:03:20 +02:00
|
|
|
* must be statically initialized because the RAMLOG ramlog_putc function
|
2012-08-02 02:42:46 +02:00
|
|
|
* could be called before the driver initialization logic executes.
|
2012-02-12 21:27:49 +01:00
|
|
|
*/
|
|
|
|
|
2014-04-13 22:32:20 +02:00
|
|
|
static struct ramlog_dev_s g_sysdev =
|
2012-02-12 21:27:49 +01:00
|
|
|
{
|
|
|
|
#ifndef CONFIG_RAMLOG_NONBLOCKING
|
|
|
|
0, /* rl_nwaiters */
|
|
|
|
#endif
|
|
|
|
0, /* rl_head */
|
|
|
|
0, /* rl_tail */
|
|
|
|
SEM_INITIALIZER(1), /* rl_exclsem */
|
|
|
|
#ifndef CONFIG_RAMLOG_NONBLOCKING
|
|
|
|
SEM_INITIALIZER(0), /* rl_waitsem */
|
|
|
|
#endif
|
2014-07-01 20:00:40 +02:00
|
|
|
CONFIG_RAMLOG_BUFSIZE, /* rl_bufsize */
|
2012-02-12 21:27:49 +01:00
|
|
|
g_sysbuffer /* rl_buffer */
|
|
|
|
};
|
2012-02-11 04:50:52 +01:00
|
|
|
#endif
|
|
|
|
|
2012-02-11 01:32:53 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-11-14 14:40:35 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ramlog_readnotify
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_RAMLOG_NONBLOCKING
|
|
|
|
static int ramlog_readnotify(FAR struct ramlog_dev_s *priv)
|
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Notify all waiting readers that they can read from the FIFO */
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
for (i = 0; i < priv->rl_nwaiters; i++)
|
|
|
|
{
|
|
|
|
nxsem_post(&priv->rl_waitsem);
|
|
|
|
}
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
|
|
|
|
/* Return number of notified readers. */
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-02-11 01:32:53 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ramlog_pollnotify
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-02-11 15:20:40 +01:00
|
|
|
static void ramlog_pollnotify(FAR struct ramlog_dev_s *priv,
|
2015-11-05 16:38:42 +01:00
|
|
|
pollevent_t eventset)
|
2012-02-11 01:32:53 +01:00
|
|
|
{
|
|
|
|
FAR struct pollfd *fds;
|
|
|
|
irqstate_t flags;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* This function may be called from an interrupt handler */
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_RAMLOG_NPOLLWAITERS; i++)
|
|
|
|
{
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2012-02-11 01:32:53 +01:00
|
|
|
fds = priv->rl_fds[i];
|
|
|
|
if (fds)
|
|
|
|
{
|
|
|
|
fds->revents |= (fds->events & eventset);
|
|
|
|
if (fds->revents != 0)
|
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(fds->sem);
|
2012-02-11 01:32:53 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2012-02-11 01:32:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-11 15:20:40 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ramlog_addchar
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-01-27 19:12:19 +01:00
|
|
|
static ssize_t ramlog_addchar(FAR struct ramlog_dev_s *priv, char ch)
|
2012-02-11 15:20:40 +01:00
|
|
|
{
|
|
|
|
irqstate_t flags;
|
2016-05-26 18:01:15 +02:00
|
|
|
size_t nexthead;
|
2012-02-11 15:20:40 +01:00
|
|
|
|
|
|
|
/* Disable interrupts (in case we are NOT called from interrupt handler) */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2012-02-11 15:20:40 +01:00
|
|
|
|
|
|
|
/* Calculate the write index AFTER the next byte is written */
|
|
|
|
|
|
|
|
nexthead = priv->rl_head + 1;
|
|
|
|
if (nexthead >= priv->rl_bufsize)
|
|
|
|
{
|
|
|
|
nexthead = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Would the next write overflow the circular buffer? */
|
|
|
|
|
|
|
|
if (nexthead == priv->rl_tail)
|
|
|
|
{
|
2012-02-12 21:27:49 +01:00
|
|
|
/* Yes... Return an indication that nothing was saved in the buffer. */
|
2012-02-11 15:20:40 +01:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2012-02-11 15:20:40 +01:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No... copy the byte and re-enable interrupts */
|
|
|
|
|
|
|
|
priv->rl_buffer[priv->rl_head] = ch;
|
|
|
|
priv->rl_head = nexthead;
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2012-02-11 15:20:40 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2012-02-11 01:32:53 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ramlog_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t ramlog_read(FAR struct file *filep, FAR char *buffer, size_t len)
|
|
|
|
{
|
2015-11-05 16:38:42 +01:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct ramlog_dev_s *priv;
|
2012-02-11 01:32:53 +01:00
|
|
|
ssize_t nread;
|
|
|
|
char ch;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Some sanity checking */
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
2015-11-05 16:38:42 +01:00
|
|
|
priv = (FAR struct ramlog_dev_s *)inode->i_private;
|
2012-02-11 01:32:53 +01:00
|
|
|
|
|
|
|
/* If the circular buffer is empty, then wait for something to be written
|
|
|
|
* to it. This function may NOT be called from an interrupt handler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(!up_interrupt_context());
|
|
|
|
|
|
|
|
/* Get exclusive access to the rl_tail index */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->rl_exclsem);
|
2012-02-11 01:32:53 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop until something is read */
|
|
|
|
|
2016-05-26 18:01:15 +02:00
|
|
|
for (nread = 0; (size_t)nread < len; )
|
2012-02-11 01:32:53 +01:00
|
|
|
{
|
|
|
|
/* Get the next byte from the buffer */
|
|
|
|
|
|
|
|
if (priv->rl_head == priv->rl_tail)
|
|
|
|
{
|
2012-02-11 16:58:11 +01:00
|
|
|
/* The circular buffer is empty. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_RAMLOG_NONBLOCKING
|
|
|
|
/* Return what we have (with zero mean the end-of-file) */
|
|
|
|
|
|
|
|
break;
|
|
|
|
#else
|
|
|
|
/* Did we read anything? */
|
2012-02-11 01:32:53 +01:00
|
|
|
|
|
|
|
if (nread > 0)
|
|
|
|
{
|
2019-11-14 14:40:35 +01:00
|
|
|
/* Yes.. break out to return what we have. */
|
2012-02-11 01:32:53 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-11-14 14:40:35 +01:00
|
|
|
/* If the driver was opened with O_NONBLOCK option, then don't wait. */
|
2012-02-11 01:32:53 +01:00
|
|
|
|
|
|
|
if (filep->f_oflags & O_NONBLOCK)
|
|
|
|
{
|
|
|
|
nread = -EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, wait for something to be written to the circular
|
|
|
|
* buffer. Increment the number of waiters so that the ramlog_write()
|
2019-11-14 14:40:35 +01:00
|
|
|
* will note that it needs to post the semaphore to wake us up.
|
2012-02-11 01:32:53 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
priv->rl_nwaiters++;
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->rl_exclsem);
|
2012-02-11 01:32:53 +01:00
|
|
|
|
|
|
|
/* We may now be pre-empted! But that should be okay because we
|
|
|
|
* have already incremented nwaiters. Pre-emptions is disabled
|
|
|
|
* but will be re-enabled while we are waiting.
|
|
|
|
*/
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->rl_waitsem);
|
2012-02-11 01:32:53 +01:00
|
|
|
|
|
|
|
/* Interrupts will be disabled when we return. So the decrementing
|
|
|
|
* rl_nwaiters here is safe.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->rl_nwaiters--;
|
|
|
|
sched_unlock();
|
|
|
|
|
|
|
|
/* Did we successfully get the rl_waitsem? */
|
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Yes... then retake the mutual exclusion semaphore */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->rl_exclsem);
|
2012-02-11 01:32:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Was the semaphore wait successful? Did we successful re-take the
|
|
|
|
* mutual exclusion semaphore?
|
|
|
|
*/
|
|
|
|
|
2014-04-13 22:32:20 +02:00
|
|
|
if (ret < 0)
|
2012-02-11 01:32:53 +01:00
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
/* No.. One of the two nxsem_wait's failed. */
|
2012-02-11 01:32:53 +01:00
|
|
|
|
2019-11-14 14:40:35 +01:00
|
|
|
/* Return the error. We did handle the case where we read
|
|
|
|
* anything already before waiting.
|
|
|
|
*/
|
2012-02-11 01:32:53 +01:00
|
|
|
|
2019-11-14 14:40:35 +01:00
|
|
|
nread = ret;
|
2012-02-11 01:32:53 +01:00
|
|
|
|
|
|
|
/* Break out to return what we have. Note, we can't exactly
|
|
|
|
* "break" out because whichever error occurred, we do not hold
|
|
|
|
* the exclusion semaphore.
|
|
|
|
*/
|
|
|
|
|
|
|
|
goto errout_without_sem;
|
|
|
|
}
|
2012-02-11 16:58:11 +01:00
|
|
|
#endif /* CONFIG_RAMLOG_NONBLOCKING */
|
2012-02-11 01:32:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The circular buffer is not empty, get the next byte from the
|
|
|
|
* tail index.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ch = priv->rl_buffer[priv->rl_tail];
|
|
|
|
|
2019-11-14 14:40:35 +01:00
|
|
|
/* Increment the tail index. */
|
2012-02-11 01:32:53 +01:00
|
|
|
|
|
|
|
if (++priv->rl_tail >= priv->rl_bufsize)
|
|
|
|
{
|
|
|
|
priv->rl_tail = 0;
|
|
|
|
}
|
|
|
|
|
2019-11-14 14:40:35 +01:00
|
|
|
/* Add the character to the user buffer. */
|
2012-02-11 01:32:53 +01:00
|
|
|
|
|
|
|
buffer[nread] = ch;
|
|
|
|
nread++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Relinquish the mutual exclusion semaphore */
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->rl_exclsem);
|
2012-02-11 01:32:53 +01:00
|
|
|
|
|
|
|
/* Notify all poll/select waiters that they can write to the FIFO */
|
|
|
|
|
2012-02-11 16:58:11 +01:00
|
|
|
#ifndef CONFIG_RAMLOG_NONBLOCKING
|
2012-02-11 01:32:53 +01:00
|
|
|
errout_without_sem:
|
2012-02-11 16:58:11 +01:00
|
|
|
#endif
|
|
|
|
|
2012-02-11 01:32:53 +01:00
|
|
|
if (nread > 0)
|
|
|
|
{
|
|
|
|
ramlog_pollnotify(priv, POLLOUT);
|
|
|
|
}
|
2012-02-13 00:54:26 +01:00
|
|
|
|
|
|
|
/* Return the number of characters actually read */
|
|
|
|
|
2012-02-11 01:32:53 +01:00
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ramlog_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t ramlog_write(FAR struct file *filep, FAR const char *buffer, size_t len)
|
|
|
|
{
|
2015-11-05 16:38:42 +01:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct ramlog_dev_s *priv;
|
2019-11-14 14:40:35 +01:00
|
|
|
int readers_waken;
|
2012-02-11 01:32:53 +01:00
|
|
|
ssize_t nwritten;
|
2012-02-11 16:58:11 +01:00
|
|
|
char ch;
|
|
|
|
int ret;
|
2012-02-11 01:32:53 +01:00
|
|
|
|
|
|
|
/* Some sanity checking */
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
2015-11-05 16:38:42 +01:00
|
|
|
priv = (FAR struct ramlog_dev_s *)inode->i_private;
|
2012-02-11 01:32:53 +01:00
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
/* Loop until all of the bytes have been written. This function may be
|
|
|
|
* called from an interrupt handler! Semaphores cannot be used!
|
|
|
|
*
|
|
|
|
* The write logic only needs to modify the rl_head index. Therefore,
|
|
|
|
* there is a difference in the way that rl_head and rl_tail are protected:
|
2019-11-14 14:40:35 +01:00
|
|
|
* rl_tail is protected with a semaphore; rl_head is protected by disabling
|
2015-10-04 23:04:00 +02:00
|
|
|
* interrupts.
|
|
|
|
*/
|
2012-02-11 01:32:53 +01:00
|
|
|
|
2016-05-26 18:01:15 +02:00
|
|
|
for (nwritten = 0; (size_t)nwritten < len; nwritten++)
|
2012-02-11 01:32:53 +01:00
|
|
|
{
|
2012-02-11 16:58:11 +01:00
|
|
|
/* Get the next character to output */
|
|
|
|
|
|
|
|
ch = buffer[nwritten];
|
|
|
|
|
2012-02-12 03:53:01 +01:00
|
|
|
/* Ignore carriage returns */
|
|
|
|
|
|
|
|
#ifdef CONFIG_RAMLOG_CRLF
|
|
|
|
if (ch == '\r')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-02-11 16:58:11 +01:00
|
|
|
/* Pre-pend a carriage before a linefeed */
|
|
|
|
|
|
|
|
if (ch == '\n')
|
|
|
|
{
|
|
|
|
ret = ramlog_addchar(priv, '\r');
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-09-16 15:48:48 +02:00
|
|
|
/* The buffer is full and nothing was saved. The remaining
|
|
|
|
* data to be written is dropped on the floor.
|
2012-02-11 16:58:11 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-02-12 03:53:01 +01:00
|
|
|
#endif
|
2012-02-11 16:58:11 +01:00
|
|
|
|
|
|
|
/* Then output the character */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
ret = ramlog_addchar(priv, ch);
|
2012-02-11 15:20:40 +01:00
|
|
|
if (ret < 0)
|
2012-02-11 01:32:53 +01:00
|
|
|
{
|
2019-09-16 15:48:48 +02:00
|
|
|
/* The buffer is full and nothing was saved. The remaining
|
|
|
|
* data to be written is dropped on the floor.
|
2012-02-11 01:32:53 +01:00
|
|
|
*/
|
|
|
|
|
2012-02-11 15:20:40 +01:00
|
|
|
break;
|
2012-02-11 01:32:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Was anything written? */
|
|
|
|
|
|
|
|
if (nwritten > 0)
|
|
|
|
{
|
2019-11-14 14:40:35 +01:00
|
|
|
readers_waken = 0;
|
|
|
|
|
2012-02-20 21:02:53 +01:00
|
|
|
#ifndef CONFIG_RAMLOG_NONBLOCKING
|
2019-11-14 14:40:35 +01:00
|
|
|
/* Are there threads waiting for read data? */
|
|
|
|
|
|
|
|
readers_waken = ramlog_readnotify(priv);
|
2012-02-20 21:02:53 +01:00
|
|
|
#endif
|
2012-02-11 16:58:11 +01:00
|
|
|
|
2019-11-14 14:40:35 +01:00
|
|
|
/* If there are multiple readers, some of them might block despite
|
|
|
|
* POLLIN because first reader might read all data. Favor readers
|
|
|
|
* and notify poll waiters only if no reader was awaken, even if the
|
|
|
|
* latter may starve.
|
|
|
|
*
|
|
|
|
* This also implies we do not have to make these two notify
|
|
|
|
* operations a critical section.
|
|
|
|
*/
|
2012-02-11 01:32:53 +01:00
|
|
|
|
2019-11-14 14:40:35 +01:00
|
|
|
if (readers_waken == 0)
|
2012-02-11 01:32:53 +01:00
|
|
|
{
|
2019-11-14 14:40:35 +01:00
|
|
|
/* Notify all poll/select waiters that they can read from the FIFO */
|
2012-02-11 01:32:53 +01:00
|
|
|
|
2019-11-14 14:40:35 +01:00
|
|
|
ramlog_pollnotify(priv, POLLIN);
|
2012-02-11 01:32:53 +01:00
|
|
|
}
|
|
|
|
}
|
2012-02-11 16:58:11 +01:00
|
|
|
|
2012-02-13 00:54:26 +01:00
|
|
|
/* We always have to return the number of bytes requested and NOT the
|
|
|
|
* number of bytes that were actually written. Otherwise, callers
|
2019-09-16 15:48:48 +02:00
|
|
|
* probably retry, causing same error condition again.
|
2012-02-13 00:54:26 +01:00
|
|
|
*/
|
2012-02-11 01:32:53 +01:00
|
|
|
|
2012-02-13 00:54:26 +01:00
|
|
|
return len;
|
2012-02-11 01:32:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ramlog_poll
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ramlog_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct ramlog_dev_s *priv;
|
|
|
|
pollevent_t eventset;
|
2019-11-14 14:40:35 +01:00
|
|
|
irqstate_t flags;
|
|
|
|
size_t next_head;
|
2012-02-11 01:32:53 +01:00
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Some sanity checking */
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
2015-11-05 16:38:42 +01:00
|
|
|
priv = (FAR struct ramlog_dev_s *)inode->i_private;
|
2012-02-11 01:32:53 +01:00
|
|
|
|
|
|
|
/* Get exclusive access to the poll structures */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&priv->rl_exclsem);
|
2012-02-11 01:32:53 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2012-02-11 01:32:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Are we setting up the poll? Or tearing it down? */
|
|
|
|
|
|
|
|
if (setup)
|
|
|
|
{
|
|
|
|
/* This is a request to set up the poll. Find an available
|
2019-09-16 15:48:48 +02:00
|
|
|
* slot for the poll structure reference.
|
2012-02-11 01:32:53 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_RAMLOG_NPOLLWAITERS; i++)
|
|
|
|
{
|
|
|
|
/* Find an available slot */
|
|
|
|
|
|
|
|
if (!priv->rl_fds[i])
|
|
|
|
{
|
|
|
|
/* Bind the poll structure and this slot */
|
|
|
|
|
|
|
|
priv->rl_fds[i] = fds;
|
|
|
|
fds->priv = &priv->rl_fds[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= CONFIG_RAMLOG_NPOLLWAITERS)
|
|
|
|
{
|
|
|
|
fds->priv = NULL;
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2019-11-14 14:40:35 +01:00
|
|
|
/* Should immediately notify on any of the requested events? */
|
2012-02-11 01:32:53 +01:00
|
|
|
|
|
|
|
eventset = 0;
|
|
|
|
|
2019-11-14 14:40:35 +01:00
|
|
|
flags = enter_critical_section();
|
|
|
|
next_head = priv->rl_head + 1;
|
|
|
|
if (next_head >= priv->rl_bufsize)
|
2012-02-11 01:32:53 +01:00
|
|
|
{
|
2019-11-14 14:40:35 +01:00
|
|
|
next_head = 0;
|
2012-02-11 01:32:53 +01:00
|
|
|
}
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2019-11-14 14:40:35 +01:00
|
|
|
/* First, check if the receive buffer is not full. */
|
|
|
|
|
|
|
|
if (next_head != priv->rl_tail)
|
2012-02-11 01:32:53 +01:00
|
|
|
{
|
|
|
|
eventset |= POLLOUT;
|
|
|
|
}
|
|
|
|
|
2019-11-14 14:40:35 +01:00
|
|
|
/* Check if the receive buffer is not empty. */
|
2012-02-11 01:32:53 +01:00
|
|
|
|
|
|
|
if (priv->rl_head != priv->rl_tail)
|
|
|
|
{
|
|
|
|
eventset |= POLLIN;
|
|
|
|
}
|
2019-11-14 14:40:35 +01:00
|
|
|
leave_critical_section(flags);
|
2012-02-11 01:32:53 +01:00
|
|
|
|
|
|
|
if (eventset)
|
|
|
|
{
|
|
|
|
ramlog_pollnotify(priv, eventset);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (fds->priv)
|
|
|
|
{
|
|
|
|
/* This is a request to tear down the poll. */
|
|
|
|
|
|
|
|
struct pollfd **slot = (struct pollfd **)fds->priv;
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2012-02-11 01:32:53 +01:00
|
|
|
if (!slot)
|
|
|
|
{
|
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Remove all memory of the poll setup */
|
|
|
|
|
|
|
|
*slot = NULL;
|
|
|
|
fds->priv = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->rl_exclsem);
|
2012-02-11 01:32:53 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ramlog_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create the RAM logging device and register it at the specified path.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ramlog_register(FAR const char *devpath, FAR char *buffer, size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct ramlog_dev_s *priv;
|
|
|
|
int ret = -ENOMEM;
|
|
|
|
|
|
|
|
/* Sanity checking */
|
|
|
|
|
|
|
|
DEBUGASSERT(devpath && buffer && buflen > 1);
|
|
|
|
|
|
|
|
/* Allocate a RAM logging device structure */
|
|
|
|
|
2014-09-01 01:34:44 +02:00
|
|
|
priv = (struct ramlog_dev_s *)kmm_zalloc(sizeof(struct ramlog_dev_s));
|
2015-11-05 16:38:42 +01:00
|
|
|
if (priv != NULL)
|
2012-02-11 01:32:53 +01:00
|
|
|
{
|
|
|
|
/* Initialize the non-zero values in the RAM logging device structure */
|
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&priv->rl_exclsem, 0, 1);
|
2012-02-11 16:58:11 +01:00
|
|
|
#ifndef CONFIG_RAMLOG_NONBLOCKING
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&priv->rl_waitsem, 0, 0);
|
2016-11-03 18:00:47 +01:00
|
|
|
|
|
|
|
/* The rl_waitsem semaphore is used for signaling and, hence, should
|
|
|
|
* not have priority inheritance enabled.
|
|
|
|
*/
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_setprotocol(&priv->rl_waitsem, SEM_PRIO_NONE);
|
2012-02-11 16:58:11 +01:00
|
|
|
#endif
|
2016-11-03 18:00:47 +01:00
|
|
|
|
2012-02-11 01:32:53 +01:00
|
|
|
priv->rl_bufsize = buflen;
|
|
|
|
priv->rl_buffer = buffer;
|
|
|
|
|
|
|
|
/* Register the character driver */
|
|
|
|
|
|
|
|
ret = register_driver(devpath, &g_ramlogfops, 0666, priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(priv);
|
2012-02-11 01:32:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2012-02-11 15:20:40 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2020-02-18 09:40:59 +01:00
|
|
|
* Name: ramlog_syslog_register
|
2012-02-11 15:20:40 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2014-04-10 17:29:30 +02:00
|
|
|
* Use a pre-allocated RAM logging device and register it at the path
|
|
|
|
* specified by CONFIG_RAMLOG_SYSLOG
|
2012-02-11 15:20:40 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-02-13 00:54:26 +01:00
|
|
|
#ifdef CONFIG_RAMLOG_SYSLOG
|
2020-02-18 09:40:59 +01:00
|
|
|
void ramlog_syslog_register(void)
|
2012-02-11 15:20:40 +01:00
|
|
|
{
|
2012-02-13 00:54:26 +01:00
|
|
|
/* Register the syslog character driver */
|
|
|
|
|
2020-02-18 09:40:59 +01:00
|
|
|
register_driver(CONFIG_SYSLOG_DEVPATH, &g_ramlogfops, 0666, &g_sysdev);
|
2012-02-11 04:50:52 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-02-11 01:32:53 +01:00
|
|
|
/****************************************************************************
|
2016-06-19 21:03:20 +02:00
|
|
|
* Name: ramlog_putc
|
2012-02-11 01:32:53 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2016-06-20 16:57:08 +02:00
|
|
|
* This is the low-level system logging interface.
|
2012-02-11 01:32:53 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-02-17 19:10:15 +01:00
|
|
|
#ifdef CONFIG_RAMLOG_SYSLOG
|
2016-06-19 21:03:20 +02:00
|
|
|
int ramlog_putc(int ch)
|
2012-02-11 15:20:40 +01:00
|
|
|
{
|
|
|
|
FAR struct ramlog_dev_s *priv = &g_sysdev;
|
2019-11-14 14:40:35 +01:00
|
|
|
int readers_waken = 0;
|
2012-02-11 16:58:11 +01:00
|
|
|
int ret;
|
|
|
|
|
2014-07-01 23:09:07 +02:00
|
|
|
#ifdef CONFIG_RAMLOG_CRLF
|
|
|
|
/* Ignore carriage returns. But return success. */
|
2012-02-12 03:53:01 +01:00
|
|
|
|
|
|
|
if (ch == '\r')
|
|
|
|
{
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
2012-02-11 16:58:11 +01:00
|
|
|
/* Pre-pend a newline with a carriage return */
|
|
|
|
|
|
|
|
if (ch == '\n')
|
|
|
|
{
|
|
|
|
ret = ramlog_addchar(priv, '\r');
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2012-02-12 21:27:49 +01:00
|
|
|
/* The buffer is full and nothing was saved. */
|
2012-02-11 16:58:11 +01:00
|
|
|
|
2017-09-29 23:24:40 +02:00
|
|
|
return ret;
|
2012-02-11 16:58:11 +01:00
|
|
|
}
|
|
|
|
}
|
2012-02-12 03:53:01 +01:00
|
|
|
#endif
|
2012-02-11 16:58:11 +01:00
|
|
|
|
2014-07-01 23:09:07 +02:00
|
|
|
/* Add the character to the RAMLOG */
|
|
|
|
|
|
|
|
ret = ramlog_addchar(priv, ch);
|
2017-09-29 23:24:40 +02:00
|
|
|
if (ret < 0)
|
2014-07-01 23:09:07 +02:00
|
|
|
{
|
2019-09-16 15:48:48 +02:00
|
|
|
/* The buffer is full and 'ch' was not saved. */
|
2014-07-01 23:09:07 +02:00
|
|
|
|
2017-09-29 23:24:40 +02:00
|
|
|
return ret;
|
2014-07-01 23:09:07 +02:00
|
|
|
}
|
|
|
|
|
2019-11-14 14:40:35 +01:00
|
|
|
#ifndef CONFIG_RAMLOG_NONBLOCKING
|
|
|
|
/* Are there threads waiting for read data? */
|
|
|
|
|
|
|
|
readers_waken = ramlog_readnotify(priv);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* If there are multiple readers, some of them might block despite
|
|
|
|
* POLLIN because first reader might read all data. Favor readers
|
|
|
|
* and notify poll waiters only if no reader was awaken, even if the
|
|
|
|
* latter may starve.
|
|
|
|
*
|
|
|
|
* This also implies we do not have to make these two notify
|
|
|
|
* operations a critical section.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (readers_waken == 0)
|
|
|
|
{
|
|
|
|
/* Notify all poll/select waiters that they can read from the FIFO */
|
|
|
|
|
|
|
|
ramlog_pollnotify(priv, POLLIN);
|
|
|
|
}
|
|
|
|
|
2017-09-29 23:24:40 +02:00
|
|
|
/* Return the character added on success */
|
2014-07-01 23:09:07 +02:00
|
|
|
|
2017-09-29 23:24:40 +02:00
|
|
|
return ch;
|
2012-02-11 15:20:40 +01:00
|
|
|
}
|
2012-02-11 01:32:53 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* CONFIG_RAMLOG */
|