2016-06-19 17:48:42 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/syslog/syslog_channel.c
|
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2016-06-19 17:48:42 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-06-19 17:48:42 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2016-06-19 17:48:42 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-18 20:54:55 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2016-06-20 15:35:38 +02:00
|
|
|
#include <sys/types.h>
|
2016-06-18 20:54:55 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2021-07-15 00:40:40 +02:00
|
|
|
#include <semaphore.h>
|
2016-06-18 20:54:55 +02:00
|
|
|
|
|
|
|
#include <nuttx/syslog/syslog.h>
|
2021-03-29 14:36:58 +02:00
|
|
|
#include <nuttx/compiler.h>
|
2016-06-18 20:54:55 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_RAMLOG_SYSLOG
|
|
|
|
# include <nuttx/syslog/ramlog.h>
|
2021-05-18 16:19:37 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_SYSLOG_RPMSG
|
2020-02-22 21:47:40 +01:00
|
|
|
# include <nuttx/syslog/syslog_rpmsg.h>
|
2021-05-18 16:19:37 +02:00
|
|
|
#endif
|
|
|
|
|
2022-01-31 09:37:29 +01:00
|
|
|
#ifdef CONFIG_SYSLOG_RTT
|
2022-11-27 19:18:45 +01:00
|
|
|
# include <nuttx/segger/rtt.h>
|
2022-01-31 09:37:29 +01:00
|
|
|
#endif
|
|
|
|
|
2021-05-18 16:19:37 +02:00
|
|
|
#ifdef CONFIG_ARCH_LOWPUTC
|
2016-06-18 20:54:55 +02:00
|
|
|
# include <nuttx/arch.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "syslog.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-21 23:08:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-05-18 16:19:37 +02:00
|
|
|
#if defined(CONFIG_SYSLOG_DEFAULT)
|
2021-03-29 14:36:58 +02:00
|
|
|
static int syslog_default_putc(FAR struct syslog_channel_s *channel,
|
|
|
|
int ch);
|
2021-06-10 16:05:58 +02:00
|
|
|
static ssize_t syslog_default_write(FAR struct syslog_channel_s *channel,
|
|
|
|
FAR const char *buffer, size_t buflen);
|
2016-06-18 20:54:55 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2021-06-10 16:05:58 +02:00
|
|
|
* Private Data
|
2016-06-18 20:54:55 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_RAMLOG_SYSLOG)
|
2021-05-18 16:19:37 +02:00
|
|
|
static const struct syslog_channel_ops_s g_ramlog_channel_ops =
|
2016-06-18 20:54:55 +02:00
|
|
|
{
|
|
|
|
ramlog_putc,
|
2021-06-17 11:00:47 +02:00
|
|
|
ramlog_putc,
|
|
|
|
NULL,
|
|
|
|
ramlog_write
|
2016-06-18 20:54:55 +02:00
|
|
|
};
|
2021-05-18 16:19:37 +02:00
|
|
|
|
|
|
|
static struct syslog_channel_s g_ramlog_channel =
|
|
|
|
{
|
|
|
|
&g_ramlog_channel_ops
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_SYSLOG_RPMSG)
|
|
|
|
static const struct syslog_channel_ops_s g_rpmsg_channel_ops =
|
2020-02-22 21:47:40 +01:00
|
|
|
{
|
|
|
|
syslog_rpmsg_putc,
|
|
|
|
syslog_rpmsg_putc,
|
2020-04-27 06:15:11 +02:00
|
|
|
syslog_rpmsg_flush,
|
|
|
|
syslog_rpmsg_write
|
2020-02-22 21:47:40 +01:00
|
|
|
};
|
2021-05-18 16:19:37 +02:00
|
|
|
|
|
|
|
static struct syslog_channel_s g_rpmsg_channel =
|
|
|
|
{
|
|
|
|
&g_rpmsg_channel_ops
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2022-01-31 09:37:29 +01:00
|
|
|
#if defined(CONFIG_SYSLOG_RTT)
|
|
|
|
static const struct syslog_channel_ops_s g_rtt_channel_ops =
|
|
|
|
{
|
|
|
|
syslog_rtt_putc,
|
|
|
|
syslog_rtt_putc,
|
|
|
|
NULL,
|
|
|
|
syslog_rtt_write
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct syslog_channel_s g_rtt_channel =
|
|
|
|
{
|
|
|
|
&g_rtt_channel_ops
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2021-05-18 16:19:37 +02:00
|
|
|
#if defined(CONFIG_SYSLOG_DEFAULT)
|
2021-03-29 14:36:58 +02:00
|
|
|
static const struct syslog_channel_ops_s g_default_channel_ops =
|
2016-06-18 20:54:55 +02:00
|
|
|
{
|
|
|
|
syslog_default_putc,
|
2021-06-10 16:05:58 +02:00
|
|
|
syslog_default_putc,
|
|
|
|
NULL,
|
|
|
|
syslog_default_write
|
2016-06-18 20:54:55 +02:00
|
|
|
};
|
|
|
|
|
2021-03-29 14:36:58 +02:00
|
|
|
static struct syslog_channel_s g_default_channel =
|
|
|
|
{
|
|
|
|
&g_default_channel_ops
|
|
|
|
};
|
2021-05-18 16:19:37 +02:00
|
|
|
#endif
|
2021-03-29 14:36:58 +02:00
|
|
|
|
2022-05-17 09:46:49 +02:00
|
|
|
/* This is a simply sanity check to avoid we have more elements than the
|
|
|
|
* `g_syslog_channel` array can hold
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SYSLOG_DEFAULT
|
|
|
|
# define SYSLOG_DEFAULT_AVAILABLE 1
|
|
|
|
#else
|
|
|
|
# define SYSLOG_DEFAULT_AVAILABLE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_RAMLOG_SYSLOG
|
|
|
|
# define RAMLOG_SYSLOG_AVAILABLE 1
|
|
|
|
#else
|
|
|
|
# define RAMLOG_SYSLOG_AVAILABLE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_SYSLOG_RPMSG
|
|
|
|
# define SYSLOG_RPMSG_AVAILABLE 1
|
|
|
|
#else
|
|
|
|
# define SYSLOG_RPMSG_AVAILABLE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_SYSLOG_RTT
|
|
|
|
# define SYSLOG_RTT_AVAILABLE 1
|
|
|
|
#else
|
|
|
|
# define SYSLOG_RTT_AVAILABLE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define SYSLOG_NCHANNELS (SYSLOG_DEFAULT_AVAILABLE + \
|
|
|
|
RAMLOG_SYSLOG_AVAILABLE + \
|
|
|
|
SYSLOG_RPMSG_AVAILABLE + \
|
|
|
|
SYSLOG_RTT_AVAILABLE)
|
|
|
|
|
|
|
|
#if SYSLOG_NCHANNELS > CONFIG_SYSLOG_MAX_CHANNELS
|
|
|
|
# error "Maximum channel number exceeds."
|
|
|
|
#endif
|
|
|
|
|
2016-06-18 20:54:55 +02:00
|
|
|
/* This is the current syslog channel in use */
|
|
|
|
|
2021-03-29 14:36:58 +02:00
|
|
|
FAR struct syslog_channel_s
|
2021-03-13 16:37:19 +01:00
|
|
|
*g_syslog_channel[CONFIG_SYSLOG_MAX_CHANNELS] =
|
|
|
|
{
|
2021-05-18 16:19:37 +02:00
|
|
|
#if defined(CONFIG_SYSLOG_DEFAULT)
|
|
|
|
&g_default_channel,
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_RAMLOG_SYSLOG)
|
|
|
|
&g_ramlog_channel,
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_SYSLOG_RPMSG)
|
2022-01-31 09:37:29 +01:00
|
|
|
&g_rpmsg_channel,
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_SYSLOG_RTT)
|
|
|
|
&g_rtt_channel
|
2021-05-18 16:19:37 +02:00
|
|
|
#endif
|
2021-03-13 16:37:19 +01:00
|
|
|
};
|
2016-06-18 20:54:55 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-20 15:35:38 +02:00
|
|
|
/****************************************************************************
|
2021-06-10 16:05:58 +02:00
|
|
|
* Name: syslog_default_putc
|
2016-06-20 15:35:38 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2021-03-29 14:36:58 +02:00
|
|
|
* If the arch supports a low-level putc function, output will be
|
|
|
|
* redirected there. Else acts as a dummy, no-nothing channel.
|
2016-06-20 15:35:38 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-05-18 16:19:37 +02:00
|
|
|
#if defined(CONFIG_SYSLOG_DEFAULT)
|
2021-03-29 14:36:58 +02:00
|
|
|
static int syslog_default_putc(FAR struct syslog_channel_s *channel, int ch)
|
2016-06-18 20:54:55 +02:00
|
|
|
{
|
2021-03-29 14:36:58 +02:00
|
|
|
UNUSED(channel);
|
|
|
|
|
|
|
|
#if defined(CONFIG_ARCH_LOWPUTC)
|
|
|
|
return up_putc(ch);
|
2022-03-19 08:52:22 +01:00
|
|
|
#else
|
2016-06-18 20:54:55 +02:00
|
|
|
return ch;
|
2022-03-19 08:52:22 +01:00
|
|
|
#endif
|
2016-06-18 20:54:55 +02:00
|
|
|
}
|
2021-06-10 16:05:58 +02:00
|
|
|
|
|
|
|
static ssize_t syslog_default_write(FAR struct syslog_channel_s *channel,
|
|
|
|
FAR const char *buffer, size_t buflen)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_ARCH_LOWPUTC)
|
2022-07-21 13:25:22 +02:00
|
|
|
up_nputs(buffer, buflen);
|
2021-06-10 16:05:58 +02:00
|
|
|
#endif
|
|
|
|
|
2022-07-11 17:11:40 +02:00
|
|
|
UNUSED(channel);
|
2022-03-14 06:38:02 +01:00
|
|
|
return buflen;
|
2021-06-10 16:05:58 +02:00
|
|
|
}
|
2016-06-18 20:54:55 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: syslog_channel
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Configure the SYSLOGging function to use the provided channel to
|
|
|
|
* generate SYSLOG output.
|
|
|
|
*
|
2016-06-20 15:35:38 +02:00
|
|
|
* Input Parameters:
|
2016-06-18 20:54:55 +02:00
|
|
|
* channel - Provides the interface to the channel to be used.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2019-09-17 18:46:23 +02:00
|
|
|
* Zero (OK) is returned on success. A negated errno value is returned
|
2016-06-18 20:54:55 +02:00
|
|
|
* on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-29 14:36:58 +02:00
|
|
|
int syslog_channel(FAR struct syslog_channel_s *channel)
|
2016-06-18 20:54:55 +02:00
|
|
|
{
|
2021-03-13 16:37:19 +01:00
|
|
|
#if (CONFIG_SYSLOG_MAX_CHANNELS != 1)
|
|
|
|
int i;
|
|
|
|
#endif
|
|
|
|
|
2016-06-18 20:54:55 +02:00
|
|
|
DEBUGASSERT(channel != NULL);
|
|
|
|
|
|
|
|
if (channel != NULL)
|
|
|
|
{
|
2021-03-29 14:36:58 +02:00
|
|
|
DEBUGASSERT(channel->sc_ops->sc_putc != NULL &&
|
|
|
|
channel->sc_ops->sc_force != NULL);
|
2016-06-18 20:54:55 +02:00
|
|
|
|
2021-03-13 16:37:19 +01:00
|
|
|
#if (CONFIG_SYSLOG_MAX_CHANNELS == 1)
|
|
|
|
g_syslog_channel[0] = channel;
|
2016-06-18 20:54:55 +02:00
|
|
|
return OK;
|
2021-03-13 16:37:19 +01:00
|
|
|
#else
|
|
|
|
for (i = 0; i < CONFIG_SYSLOG_MAX_CHANNELS; i++)
|
|
|
|
{
|
|
|
|
if (g_syslog_channel[i] == NULL)
|
|
|
|
{
|
|
|
|
g_syslog_channel[i] = channel;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else if (g_syslog_channel[i] == channel)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: syslog_channel_remove
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Removes an already configured SYSLOG channel from the list of used
|
|
|
|
* channels.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* channel - Provides the interface to the channel to be removed.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success. A negated errno value is returned
|
|
|
|
* on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-29 14:36:58 +02:00
|
|
|
int syslog_channel_remove(FAR struct syslog_channel_s *channel)
|
2021-03-13 16:37:19 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
DEBUGASSERT(channel != NULL);
|
|
|
|
|
|
|
|
if (channel != NULL)
|
|
|
|
{
|
|
|
|
for (i = 0; i < CONFIG_SYSLOG_MAX_CHANNELS; i++)
|
|
|
|
{
|
|
|
|
if (g_syslog_channel[i] == channel)
|
|
|
|
{
|
|
|
|
/* Get the rest of the channels one position back
|
|
|
|
* to ensure that there are no holes in the list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (i < (CONFIG_SYSLOG_MAX_CHANNELS - 1) &&
|
|
|
|
g_syslog_channel[i + 1] != NULL)
|
|
|
|
{
|
|
|
|
g_syslog_channel[i] = g_syslog_channel[i + 1];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_syslog_channel[i] = NULL;
|
|
|
|
|
2021-06-17 16:45:01 +02:00
|
|
|
/* The channel is now removed from the list and its driver
|
|
|
|
* can be safely uninitialized.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (channel->sc_ops->sc_close)
|
|
|
|
{
|
|
|
|
channel->sc_ops->sc_close(channel);
|
|
|
|
}
|
|
|
|
|
2021-03-13 16:37:19 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
2016-06-18 20:54:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|