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
|
|
|
*
|
2020-05-17 16:47:40 +02: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
|
2012-02-11 01:32:53 +01:00
|
|
|
*
|
2020-05-17 16:47:40 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-02-11 01:32:53 +01:00
|
|
|
*
|
2020-05-17 16:47:40 +02: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.
|
2012-02-11 01:32:53 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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>
|
2021-05-20 14:55:11 +02:00
|
|
|
#include <ctype.h>
|
2012-02-11 01:32:53 +01:00
|
|
|
|
2016-11-03 18:00:47 +01:00
|
|
|
#include <nuttx/arch.h>
|
2012-02-11 01:32:53 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.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>
|
2021-04-19 15:53:25 +02:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
2014-04-10 17:29:30 +02:00
|
|
|
#include <nuttx/syslog/ramlog.h>
|
2021-03-29 14:36:58 +02:00
|
|
|
#include <nuttx/compiler.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
|
2021-04-19 15:53:25 +02:00
|
|
|
volatile uint8_t rl_nwaiters; /* Number of threads waiting for data */
|
2012-02-11 16:58:11 +01:00
|
|
|
#endif
|
2021-04-19 15:53:25 +02:00
|
|
|
volatile size_t rl_head; /* The head index (where data is added) */
|
|
|
|
volatile size_t rl_tail; /* The tail index (where data is removed) */
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t rl_lock; /* Enforces mutually exclusive access */
|
2012-02-11 16:58:11 +01:00
|
|
|
#ifndef CONFIG_RAMLOG_NONBLOCKING
|
2021-04-19 15:53:25 +02:00
|
|
|
sem_t rl_waitsem; /* Used to wait for data */
|
2012-02-11 16:58:11 +01:00
|
|
|
#endif
|
2021-04-19 15:53:25 +02:00
|
|
|
size_t rl_bufsize; /* Size of the RAM buffer */
|
|
|
|
FAR char *rl_buffer; /* Circular RAM buffer */
|
2012-02-11 01:32:53 +01:00
|
|
|
|
|
|
|
/* 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);
|
2021-06-16 10:43:50 +02:00
|
|
|
static int ramlog_addchar(FAR struct ramlog_dev_s *priv, char ch);
|
2012-02-11 15:20:40 +01:00
|
|
|
|
|
|
|
/* Character driver methods */
|
2012-02-11 01:32:53 +01:00
|
|
|
|
2021-06-17 11:00:47 +02:00
|
|
|
static ssize_t ramlog_file_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t ramlog_file_write(FAR struct file *filep,
|
|
|
|
FAR const char *buffer, size_t buflen);
|
|
|
|
static int ramlog_file_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg);
|
|
|
|
static int ramlog_file_poll(FAR struct file *filep,
|
|
|
|
FAR struct pollfd *fds, bool setup);
|
2012-02-11 01:32:53 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_ramlogfops =
|
|
|
|
{
|
2021-06-17 11:00:47 +02:00
|
|
|
NULL, /* open */
|
|
|
|
NULL, /* close */
|
|
|
|
ramlog_file_read, /* read */
|
|
|
|
ramlog_file_write, /* write */
|
|
|
|
NULL, /* seek */
|
|
|
|
ramlog_file_ioctl, /* ioctl */
|
|
|
|
ramlog_file_poll /* poll */
|
2016-05-26 18:01:15 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2021-06-17 11:00:47 +02:00
|
|
|
, 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
|
2022-08-22 09:51:31 +02:00
|
|
|
# ifdef RAMLOG_BUFFER_SECTION
|
2021-05-20 14:06:38 +02:00
|
|
|
static char g_sysbuffer[CONFIG_RAMLOG_BUFSIZE]
|
2022-08-22 09:51:31 +02:00
|
|
|
locate_data(RAMLOG_BUFFER_SECTION);
|
|
|
|
# else
|
|
|
|
static char g_sysbuffer[CONFIG_RAMLOG_BUFSIZE];
|
|
|
|
# endif
|
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
|
|
|
{
|
2022-08-22 09:51:31 +02:00
|
|
|
# ifndef CONFIG_RAMLOG_NONBLOCKING
|
2012-02-12 21:27:49 +01:00
|
|
|
0, /* rl_nwaiters */
|
2022-08-22 09:51:31 +02:00
|
|
|
# endif
|
2021-05-20 14:55:11 +02:00
|
|
|
CONFIG_RAMLOG_BUFSIZE, /* rl_head */
|
|
|
|
CONFIG_RAMLOG_BUFSIZE, /* rl_tail */
|
2022-09-06 08:18:45 +02:00
|
|
|
NXMUTEX_INITIALIZER, /* rl_lock */
|
2022-08-22 09:51:31 +02:00
|
|
|
# ifndef CONFIG_RAMLOG_NONBLOCKING
|
2022-09-06 08:18:45 +02:00
|
|
|
NXSEM_INITIALIZER(0,
|
|
|
|
PRIOINHERIT_FLAGS_DISABLE), /* rl_waitsem */
|
2022-08-22 09:51:31 +02:00
|
|
|
# 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
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-28 14:39:59 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ramlog_bufferused
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static size_t ramlog_bufferused(FAR struct ramlog_dev_s *priv)
|
|
|
|
{
|
|
|
|
return (priv->rl_bufsize + priv->rl_head - priv->rl_tail) %
|
|
|
|
priv->rl_bufsize;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
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();
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(&priv->rl_fds[i], 1, eventset);
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2012-02-11 01:32:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 12:10:29 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ramlog_initbuf
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize g_sysdev based on the current system ramlog buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_RAMLOG_SYSLOG
|
|
|
|
static void ramlog_initbuf(void)
|
|
|
|
{
|
|
|
|
FAR struct ramlog_dev_s *priv = &g_sysdev;
|
|
|
|
bool is_empty = true;
|
|
|
|
char prev;
|
|
|
|
char cur;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (priv->rl_head != CONFIG_RAMLOG_BUFSIZE ||
|
|
|
|
priv->rl_tail != CONFIG_RAMLOG_BUFSIZE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
prev = priv->rl_buffer[priv->rl_bufsize - 1];
|
|
|
|
|
|
|
|
for (i = 0; i < priv->rl_bufsize; i++)
|
|
|
|
{
|
|
|
|
cur = priv->rl_buffer[i];
|
|
|
|
|
|
|
|
if (!isascii(cur))
|
|
|
|
{
|
|
|
|
memset(priv->rl_buffer, 0, priv->rl_bufsize);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (prev && !cur)
|
|
|
|
{
|
|
|
|
priv->rl_head = i;
|
|
|
|
is_empty = false;
|
|
|
|
}
|
|
|
|
else if (!prev && cur)
|
|
|
|
{
|
|
|
|
priv->rl_tail = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
prev = cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i != priv->rl_bufsize || is_empty)
|
|
|
|
{
|
|
|
|
priv->rl_head = priv->rl_tail = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-02-11 15:20:40 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ramlog_addchar
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-16 10:43:50 +02:00
|
|
|
static int 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
|
|
|
|
2021-07-07 12:10:29 +02:00
|
|
|
#ifdef CONFIG_RAMLOG_SYSLOG
|
|
|
|
if (priv == &g_sysdev)
|
|
|
|
{
|
|
|
|
ramlog_initbuf();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
2021-06-16 10:43:50 +02:00
|
|
|
#ifdef CONFIG_RAMLOG_CRLF
|
|
|
|
/* Ignore carriage returns */
|
|
|
|
|
|
|
|
if (ch == '\r')
|
|
|
|
{
|
|
|
|
leave_critical_section(flags);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pre-pend a carriage before a linefeed */
|
|
|
|
|
|
|
|
if (ch == '\n')
|
|
|
|
{
|
|
|
|
ch = '\r';
|
|
|
|
}
|
|
|
|
|
|
|
|
again:
|
|
|
|
#endif
|
|
|
|
|
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)
|
|
|
|
{
|
2020-07-28 09:29:43 +02:00
|
|
|
#ifdef CONFIG_RAMLOG_OVERWRITE
|
|
|
|
/* Yes... Overwrite with the latest log in the circular buffer */
|
|
|
|
|
2021-05-20 14:55:11 +02:00
|
|
|
priv->rl_buffer[priv->rl_tail] = '\0';
|
2020-07-28 09:29:43 +02:00
|
|
|
priv->rl_tail += 1;
|
|
|
|
if (priv->rl_tail >= priv->rl_bufsize)
|
|
|
|
{
|
|
|
|
priv->rl_tail = 0;
|
|
|
|
}
|
|
|
|
#else
|
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;
|
2020-07-28 09:29:43 +02:00
|
|
|
#endif
|
2012-02-11 15:20:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No... copy the byte and re-enable interrupts */
|
|
|
|
|
|
|
|
priv->rl_buffer[priv->rl_head] = ch;
|
|
|
|
priv->rl_head = nexthead;
|
2021-06-16 10:43:50 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_RAMLOG_CRLF
|
|
|
|
if (ch == '\r')
|
|
|
|
{
|
|
|
|
ch = '\n';
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2012-02-11 15:20:40 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-06-17 11:00:47 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ramlog_addbuf
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t ramlog_addbuf(FAR struct ramlog_dev_s *priv,
|
|
|
|
FAR const char *buffer, size_t len)
|
|
|
|
{
|
|
|
|
int readers_waken;
|
|
|
|
ssize_t nwritten;
|
|
|
|
char ch;
|
|
|
|
int ret;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->rl_lock);
|
2021-06-17 11:00:47 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (nwritten = 0; (size_t)nwritten < len; nwritten++)
|
|
|
|
{
|
|
|
|
/* Get the next character to output */
|
|
|
|
|
|
|
|
ch = buffer[nwritten];
|
|
|
|
|
|
|
|
/* Then output the character */
|
|
|
|
|
|
|
|
ret = ramlog_addchar(priv, ch);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* The buffer is full and nothing was saved. The remaining
|
|
|
|
* data to be written is dropped on the floor.
|
|
|
|
*/
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Was anything written? */
|
|
|
|
|
|
|
|
if (nwritten > 0)
|
|
|
|
{
|
|
|
|
readers_waken = 0;
|
|
|
|
|
|
|
|
#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
|
2021-07-04 17:31:21 +02:00
|
|
|
* and notify poll waiters only if no reader was awakened, even if
|
|
|
|
* the latter may starve.
|
2021-06-17 11:00:47 +02:00
|
|
|
*
|
|
|
|
* This also implies we do not have to make these two notify
|
|
|
|
* operations a critical section.
|
|
|
|
*/
|
|
|
|
|
2022-02-28 14:39:59 +01:00
|
|
|
if (readers_waken == 0 &&
|
|
|
|
ramlog_bufferused(priv) >= CONFIG_RAMLOG_POLLTHRESHOLD)
|
2021-06-17 11:00:47 +02:00
|
|
|
{
|
|
|
|
/* Notify all poll/select waiters that they can read from the
|
|
|
|
* FIFO.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ramlog_pollnotify(priv, POLLIN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We always have to return the number of bytes requested and NOT the
|
|
|
|
* number of bytes that were actually written. Otherwise, callers
|
|
|
|
* probably retry, causing same error condition again.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->rl_lock);
|
2021-06-17 11:00:47 +02:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2012-02-11 01:32:53 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ramlog_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-17 11:00:47 +02:00
|
|
|
static ssize_t ramlog_file_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len)
|
2012-02-11 01:32:53 +01:00
|
|
|
{
|
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 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->rl_lock);
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-17 16:47:40 +02: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
|
2020-05-17 16:47:40 +02:00
|
|
|
* buffer. Increment the number of waiters so that the
|
2021-06-17 11:00:47 +02:00
|
|
|
* ramlog_file_write() 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++;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->rl_lock);
|
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)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Yes... then retake the mutual exclusion mutex */
|
2012-02-11 01:32:53 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->rl_lock);
|
2012-02-11 01:32:53 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Was the mutex wait successful? Did we successful re-take the
|
|
|
|
* mutual exclusion mutex?
|
2012-02-11 01:32:53 +01:00
|
|
|
*/
|
|
|
|
|
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
|
2022-09-06 08:18:45 +02:00
|
|
|
* the exclusion mutex.
|
2012-02-11 01:32:53 +01:00
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_without_lock;
|
2012-02-11 01:32:53 +01:00
|
|
|
}
|
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];
|
2021-05-20 14:55:11 +02:00
|
|
|
priv->rl_buffer[priv->rl_tail] = '\0';
|
2012-02-11 01:32:53 +01:00
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Relinquish the mutual exclusion mutex */
|
2012-02-11 01:32:53 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->rl_lock);
|
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
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_without_lock:
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2021-06-17 11:00:47 +02:00
|
|
|
* Name: ramlog_file_write
|
2012-02-11 01:32:53 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-17 11:00:47 +02:00
|
|
|
static ssize_t ramlog_file_write(FAR struct file *filep,
|
|
|
|
FAR const char *buffer, size_t len)
|
2012-02-11 01:32:53 +01:00
|
|
|
{
|
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
|
|
|
|
|
|
|
/* 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
|
|
|
|
2021-06-17 11:00:47 +02:00
|
|
|
return ramlog_addbuf(priv, buffer, len);
|
2012-02-11 01:32:53 +01:00
|
|
|
}
|
|
|
|
|
2021-04-19 15:53:25 +02:00
|
|
|
/****************************************************************************
|
2021-06-17 11:00:47 +02:00
|
|
|
* Name: ramlog_file_ioctl
|
2021-04-19 15:53:25 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-17 11:00:47 +02:00
|
|
|
static int ramlog_file_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg)
|
2021-04-19 15:53:25 +02:00
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct ramlog_dev_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
priv = (FAR struct ramlog_dev_s *)inode->i_private;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->rl_lock);
|
2021-04-19 15:53:25 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case FIONREAD:
|
2022-02-28 14:39:59 +01:00
|
|
|
*(FAR int *)((uintptr_t)arg) = ramlog_bufferused(priv);
|
2021-04-19 15:53:25 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->rl_lock);
|
2021-04-19 15:53:25 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-02-11 01:32:53 +01:00
|
|
|
/****************************************************************************
|
2021-06-17 11:00:47 +02:00
|
|
|
* Name: ramlog_file_poll
|
2012-02-11 01:32:53 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-17 11:00:47 +02:00
|
|
|
static int ramlog_file_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup)
|
2012-02-11 01:32:53 +01:00
|
|
|
{
|
|
|
|
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 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->rl_lock);
|
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)
|
2020-05-17 16:47:40 +02:00
|
|
|
{
|
|
|
|
eventset |= POLLOUT;
|
|
|
|
}
|
2012-02-11 01:32:53 +01:00
|
|
|
|
2019-11-14 14:40:35 +01:00
|
|
|
/* Check if the receive buffer is not empty. */
|
2012-02-11 01:32:53 +01:00
|
|
|
|
2022-02-28 14:39:59 +01:00
|
|
|
if (ramlog_bufferused(priv) >= CONFIG_RAMLOG_POLLTHRESHOLD)
|
2020-05-17 16:47:40 +02:00
|
|
|
{
|
|
|
|
eventset |= POLLIN;
|
|
|
|
}
|
|
|
|
|
2019-11-14 14:40:35 +01:00
|
|
|
leave_critical_section(flags);
|
2012-02-11 01:32:53 +01:00
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
ramlog_pollnotify(priv, eventset);
|
2012-02-11 01:32:53 +01:00
|
|
|
}
|
|
|
|
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 */
|
|
|
|
|
2022-02-01 17:55:50 +01:00
|
|
|
*slot = NULL;
|
|
|
|
fds->priv = NULL;
|
2012-02-11 01:32:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->rl_lock);
|
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 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&priv->rl_lock);
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-05-17 15:56:21 +02:00
|
|
|
nxsem_set_protocol(&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)
|
|
|
|
{
|
2022-10-16 18:50:59 +02:00
|
|
|
nxmutex_destroy(&priv->rl_lock);
|
|
|
|
#ifndef CONFIG_RAMLOG_NONBLOCKING
|
|
|
|
nxsem_destroy(&priv->rl_waitsem);
|
|
|
|
#endif
|
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
|
2021-03-29 14:36:58 +02:00
|
|
|
int ramlog_putc(FAR struct syslog_channel_s *channel, 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;
|
|
|
|
|
2021-03-29 14:36:58 +02:00
|
|
|
UNUSED(channel);
|
|
|
|
|
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
|
2021-07-04 17:31:21 +02:00
|
|
|
* and notify poll waiters only if no reader was awakened, even if
|
|
|
|
* the latter may starve.
|
2019-11-14 14:40:35 +01:00
|
|
|
*
|
|
|
|
* This also implies we do not have to make these two notify
|
|
|
|
* operations a critical section.
|
|
|
|
*/
|
|
|
|
|
2022-02-28 14:39:59 +01:00
|
|
|
if (readers_waken == 0 &&
|
|
|
|
ramlog_bufferused(priv) >= CONFIG_RAMLOG_POLLTHRESHOLD)
|
2019-11-14 14:40:35 +01:00
|
|
|
{
|
|
|
|
/* 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
|
|
|
}
|
2021-06-17 11:00:47 +02:00
|
|
|
|
|
|
|
ssize_t ramlog_write(FAR struct syslog_channel_s *channel,
|
|
|
|
FAR const char *buffer, size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct ramlog_dev_s *priv = &g_sysdev;
|
|
|
|
|
|
|
|
return ramlog_addbuf(priv, buffer, buflen);
|
|
|
|
}
|
2012-02-11 01:32:53 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* CONFIG_RAMLOG */
|