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>
|
2022-06-27 10:45:44 +02:00
|
|
|
#include <sys/boardctl.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
|
|
|
|
|
2023-11-23 06:02:47 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define RAMLOG_MAGIC_NUMBER 0x12345678
|
|
|
|
#define RAMLOG_HEADER_LEN 12
|
|
|
|
|
2012-02-11 01:32:53 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-11-23 06:02:47 +01:00
|
|
|
struct ramlog_header_s
|
|
|
|
{
|
|
|
|
uint32_t rl_magic; /* The rl_magic number for ramlog buffer init */
|
|
|
|
volatile uint32_t rl_head; /* The head index (where data is added) */
|
|
|
|
volatile uint32_t rl_tail; /* The tail index (where data is removed) */
|
|
|
|
char rl_buffer[]; /* Circular RAM buffer */
|
|
|
|
};
|
|
|
|
|
2012-02-11 01:32:53 +01:00
|
|
|
struct ramlog_dev_s
|
|
|
|
{
|
2023-11-23 06:02:47 +01:00
|
|
|
/* The following is the header of the RAM buffer,
|
|
|
|
* Store the RAM BUFFER init rl_magic number,
|
|
|
|
* and read/write pointers
|
|
|
|
*/
|
|
|
|
|
|
|
|
FAR struct ramlog_header_s *rl_header;
|
|
|
|
|
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
|
2023-11-23 06:02:47 +01:00
|
|
|
uint32_t rl_bufsize; /* Size of the 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
|
2023-11-18 11:14:25 +01:00
|
|
|
static void ramlog_readnotify(FAR struct ramlog_dev_s *priv);
|
2019-11-14 14:40:35 +01:00
|
|
|
#endif
|
|
|
|
static void ramlog_pollnotify(FAR struct ramlog_dev_s *priv,
|
|
|
|
pollevent_t eventset);
|
2023-11-18 11:14:25 +01:00
|
|
|
static void 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 */
|
2023-01-02 14:02:51 +01:00
|
|
|
NULL, /* mmap */
|
2023-01-02 18:06:12 +01:00
|
|
|
NULL, /* truncate */
|
2021-06-17 11:00:47 +02:00
|
|
|
ramlog_file_poll /* poll */
|
2012-02-11 01:32:53 +01:00
|
|
|
};
|
|
|
|
|
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
|
|
|
{
|
2023-11-23 06:02:47 +01:00
|
|
|
(FAR struct ramlog_header_s *)g_sysbuffer, /* rl_buffer */
|
|
|
|
NXMUTEX_INITIALIZER, /* rl_lock */
|
2022-08-22 09:51:31 +02:00
|
|
|
# ifndef CONFIG_RAMLOG_NONBLOCKING
|
2023-11-23 06:02:47 +01:00
|
|
|
SEM_INITIALIZER(0), /* rl_waitsem */
|
2022-08-22 09:51:31 +02:00
|
|
|
# endif
|
2023-11-23 06:02:47 +01:00
|
|
|
CONFIG_RAMLOG_BUFSIZE - RAMLOG_HEADER_LEN /* rl_bufsize */
|
2012-02-12 21:27:49 +01:00
|
|
|
};
|
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)
|
|
|
|
{
|
2023-11-23 06:02:47 +01:00
|
|
|
FAR struct ramlog_header_s *header = priv->rl_header;
|
|
|
|
|
|
|
|
return (priv->rl_bufsize + header->rl_head - header->rl_tail) %
|
|
|
|
priv->rl_bufsize;
|
2022-02-28 14:39:59 +01:00
|
|
|
}
|
|
|
|
|
2019-11-14 14:40:35 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ramlog_readnotify
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_RAMLOG_NONBLOCKING
|
2023-11-18 11:14:25 +01:00
|
|
|
static void ramlog_readnotify(FAR struct ramlog_dev_s *priv)
|
2019-11-14 14:40:35 +01:00
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
/* Notify all waiting readers that they can read from the FIFO */
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
2023-09-26 15:12:10 +02:00
|
|
|
|
2023-11-18 11:14:25 +01:00
|
|
|
for (; ; )
|
2019-11-14 14:40:35 +01:00
|
|
|
{
|
2023-09-26 15:12:10 +02:00
|
|
|
int semcount = 0;
|
|
|
|
|
2023-10-25 05:23:17 +02:00
|
|
|
nxsem_get_value(&priv->rl_waitsem, &semcount);
|
2023-09-26 15:12:10 +02:00
|
|
|
if (semcount >= 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-11-14 14:40:35 +01:00
|
|
|
nxsem_post(&priv->rl_waitsem);
|
|
|
|
}
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
}
|
|
|
|
#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;
|
|
|
|
|
|
|
|
/* This function may be called from an interrupt handler */
|
|
|
|
|
2023-11-19 12:19:53 +01:00
|
|
|
flags = enter_critical_section();
|
|
|
|
poll_notify(priv->rl_fds, CONFIG_RAMLOG_NPOLLWAITERS, eventset);
|
|
|
|
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;
|
2023-11-23 06:02:47 +01:00
|
|
|
FAR struct ramlog_header_s *header = priv->rl_header;
|
2021-07-07 12:10:29 +02:00
|
|
|
|
2023-11-23 06:02:47 +01:00
|
|
|
if (header->rl_magic != RAMLOG_MAGIC_NUMBER)
|
2021-07-07 12:10:29 +02:00
|
|
|
{
|
2023-11-23 06:02:47 +01:00
|
|
|
memset(header, 0, CONFIG_RAMLOG_BUFSIZE);
|
|
|
|
header->rl_magic = RAMLOG_MAGIC_NUMBER;
|
2021-07-07 12:10:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-02-11 15:20:40 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ramlog_addchar
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-11-18 11:14:25 +01:00
|
|
|
static void ramlog_addchar(FAR struct ramlog_dev_s *priv, char ch)
|
2012-02-11 15:20:40 +01:00
|
|
|
{
|
2023-11-23 06:02:47 +01:00
|
|
|
FAR struct ramlog_header_s *header;
|
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);
|
2023-11-18 11:14:25 +01:00
|
|
|
return;
|
2021-06-16 10:43:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
2023-11-23 06:02:47 +01:00
|
|
|
header = priv->rl_header;
|
|
|
|
nexthead = header->rl_head + 1;
|
2012-02-11 15:20:40 +01:00
|
|
|
if (nexthead >= priv->rl_bufsize)
|
|
|
|
{
|
|
|
|
nexthead = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Would the next write overflow the circular buffer? */
|
|
|
|
|
2023-11-23 06:02:47 +01:00
|
|
|
if (nexthead == header->rl_tail)
|
2012-02-11 15:20:40 +01:00
|
|
|
{
|
2020-07-28 09:29:43 +02:00
|
|
|
/* Yes... Overwrite with the latest log in the circular buffer */
|
|
|
|
|
2023-11-23 06:02:47 +01:00
|
|
|
header->rl_buffer[header->rl_tail] = '\0';
|
|
|
|
header->rl_tail += 1;
|
|
|
|
if (header->rl_tail >= priv->rl_bufsize)
|
2020-07-28 09:29:43 +02:00
|
|
|
{
|
2023-11-23 06:02:47 +01:00
|
|
|
header->rl_tail = 0;
|
2020-07-28 09:29:43 +02:00
|
|
|
}
|
2012-02-11 15:20:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No... copy the byte and re-enable interrupts */
|
|
|
|
|
2023-11-23 06:02:47 +01:00
|
|
|
header->rl_buffer[header->rl_head] = ch;
|
|
|
|
header->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
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2023-11-18 11:14:25 +01:00
|
|
|
size_t nwritten;
|
2021-06-17 11:00:47 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-11-18 11:14:25 +01:00
|
|
|
for (nwritten = 0; nwritten < len; nwritten++)
|
2021-06-17 11:00:47 +02:00
|
|
|
{
|
|
|
|
/* Get the next character to output */
|
|
|
|
|
|
|
|
ch = buffer[nwritten];
|
|
|
|
|
|
|
|
/* Then output the character */
|
|
|
|
|
2023-11-18 11:14:25 +01:00
|
|
|
ramlog_addchar(priv, ch);
|
2021-06-17 11:00:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Was anything written? */
|
|
|
|
|
|
|
|
if (nwritten > 0)
|
|
|
|
{
|
|
|
|
#ifndef CONFIG_RAMLOG_NONBLOCKING
|
|
|
|
/* Are there threads waiting for read data? */
|
|
|
|
|
2023-11-18 11:14:25 +01:00
|
|
|
ramlog_readnotify(priv);
|
2021-06-17 11:00:47 +02:00
|
|
|
#endif
|
|
|
|
|
2023-11-18 11:14:25 +01:00
|
|
|
if (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;
|
2023-11-23 06:02:47 +01:00
|
|
|
FAR struct ramlog_header_s *header;
|
2015-11-05 16:38:42 +01:00
|
|
|
FAR struct ramlog_dev_s *priv;
|
2012-02-11 01:32:53 +01:00
|
|
|
ssize_t nread;
|
|
|
|
char ch;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Some sanity checking */
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
priv = 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 */
|
|
|
|
|
2023-11-23 06:02:47 +01:00
|
|
|
header = priv->rl_header;
|
|
|
|
|
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 */
|
|
|
|
|
2023-11-23 06:02:47 +01:00
|
|
|
if (header->rl_head == header->rl_tail)
|
2012-02-11 01:32:53 +01:00
|
|
|
{
|
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
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
|
|
/* 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
|
|
|
|
2023-11-18 11:14:25 +01:00
|
|
|
return ret;
|
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.
|
|
|
|
*/
|
|
|
|
|
2023-11-23 06:02:47 +01:00
|
|
|
ch = header->rl_buffer[header->rl_tail];
|
|
|
|
header->rl_buffer[header->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
|
|
|
|
2023-11-23 06:02:47 +01:00
|
|
|
if (++header->rl_tail >= priv->rl_bufsize)
|
2012-02-11 01:32:53 +01:00
|
|
|
{
|
2023-11-23 06:02:47 +01:00
|
|
|
header->rl_tail = 0;
|
2012-02-11 01:32:53 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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 */
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
priv = 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;
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
priv = inode->i_private;
|
2021-04-19 15:53:25 +02:00
|
|
|
|
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;
|
2023-11-18 11:14:25 +01:00
|
|
|
pollevent_t eventset = POLLOUT;
|
2019-11-14 14:40:35 +01:00
|
|
|
irqstate_t flags;
|
2012-02-11 01:32:53 +01:00
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Some sanity checking */
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
priv = 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)
|
|
|
|
{
|
2023-11-19 12:19:53 +01:00
|
|
|
fds->priv = NULL;
|
|
|
|
ret = -EBUSY;
|
2012-02-11 01:32:53 +01:00
|
|
|
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
|
|
|
|
2019-11-14 14:40:35 +01:00
|
|
|
flags = enter_critical_section();
|
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
|
|
|
|
2023-11-19 12:19:53 +01:00
|
|
|
poll_notify(&fds, 1, 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 */
|
|
|
|
|
2023-11-23 06:02:47 +01:00
|
|
|
DEBUGASSERT(devpath && buffer && buflen > (RAMLOG_HEADER_LEN + 1));
|
2012-02-11 01:32:53 +01:00
|
|
|
|
|
|
|
/* Allocate a RAM logging device structure */
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
priv = 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);
|
2012-02-11 16:58:11 +01:00
|
|
|
#endif
|
2016-11-03 18:00:47 +01:00
|
|
|
|
2023-11-23 06:02:47 +01:00
|
|
|
priv->rl_bufsize = buflen - RAMLOG_HEADER_LEN;
|
|
|
|
priv->rl_header = (FAR struct ramlog_header_s *)buffer;
|
2012-02-11 01:32:53 +01:00
|
|
|
|
|
|
|
/* 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;
|
2012-02-11 16:58:11 +01:00
|
|
|
|
2021-03-29 14:36:58 +02:00
|
|
|
UNUSED(channel);
|
|
|
|
|
2014-07-01 23:09:07 +02:00
|
|
|
/* Add the character to the RAMLOG */
|
|
|
|
|
2023-11-18 11:14:25 +01:00
|
|
|
ramlog_addchar(priv, ch);
|
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? */
|
|
|
|
|
2023-11-18 11:14:25 +01:00
|
|
|
ramlog_readnotify(priv);
|
2019-11-14 14:40:35 +01:00
|
|
|
#endif
|
|
|
|
|
2023-11-18 11:14:25 +01:00
|
|
|
if (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 */
|