From 93426ca7cc6ea5111bcf591189352d144ab5ade1 Mon Sep 17 00:00:00 2001 From: ligd Date: Tue, 7 Dec 2021 22:40:00 +0800 Subject: [PATCH] syslog_rpmsg: update syslog_rpmsg to support non-overwrite Signed-off-by: ligd --- drivers/syslog/Kconfig | 8 ++++ drivers/syslog/syslog_rpmsg.c | 81 ++++++++++++++++++++++++----------- 2 files changed, 65 insertions(+), 24 deletions(-) diff --git a/drivers/syslog/Kconfig b/drivers/syslog/Kconfig index e8f68e8c5f..e004c14b8b 100644 --- a/drivers/syslog/Kconfig +++ b/drivers/syslog/Kconfig @@ -250,6 +250,14 @@ config SYSLOG_RPMSG_WORK_DELAY default 100 depends on SYSLOG_RPMSG +config SYSLOG_RPMSG_OVERWRITE + bool "SYSLOG RPMSG overwrite" + default n + depends on SYSLOG_RPMSG + ---help--- + Allow syslog rpmsg overwrite, maybe caused syslog lost. + Set 'n' if you don't want lost logs, but may harmful to preformance. + config SYSLOG_RPMSG_SERVER bool "Enable RPMSG server for SYSLOG" default n diff --git a/drivers/syslog/syslog_rpmsg.c b/drivers/syslog/syslog_rpmsg.c index 54ce5708dc..d339729153 100644 --- a/drivers/syslog/syslog_rpmsg.c +++ b/drivers/syslog/syslog_rpmsg.c @@ -63,6 +63,8 @@ struct syslog_rpmsg_s bool suspend; bool transfer; /* The transfer flag */ ssize_t trans_len; /* The data length when transfer */ + + sem_t sem; }; /**************************************************************************** @@ -147,31 +149,52 @@ static void syslog_rpmsg_work(FAR void *priv_) static void syslog_rpmsg_putchar(FAR struct syslog_rpmsg_s *priv, int ch, bool last) { + size_t next; + + while (1) + { + next = priv->head + 1; + if (next >= priv->size) + { + next = 0; + } + + if (next == priv->tail) + { +#ifndef SYSLOG_RPMSG_OVERWRITE + if (!up_interrupt_context() && !sched_idletask()) + { + nxsem_wait(&priv->sem); + } + else +#endif + { + /* Overwrite */ + + priv->buffer[priv->tail] = 0; + priv->tail += 1; + + if (priv->tail >= priv->size) + { + priv->tail = 0; + } + + if (priv->transfer) + { + priv->trans_len--; + } + + break; + } + } + else + { + break; + } + } + priv->buffer[priv->head] = ch & 0xff; - - priv->head += 1; - if (priv->head >= (priv->size)) - { - priv->head = 0; - } - - /* Allow overwrite */ - - if (priv->head == (priv->tail)) - { - priv->buffer[priv->tail] = 0; - - priv->tail += 1; - if (priv->tail >= priv->size) - { - priv->tail = 0; - } - - if (priv->transfer) - { - priv->trans_len--; - } - } + priv->head = next; if (last && !priv->suspend && !priv->transfer && is_rpmsg_ept_ready(&priv->ept)) @@ -246,6 +269,7 @@ static int syslog_rpmsg_ept_cb(FAR struct rpmsg_endpoint *ept, { irqstate_t flags; ssize_t len_end; + int sval; flags = enter_critical_section(); @@ -268,6 +292,12 @@ static int syslog_rpmsg_ept_cb(FAR struct rpmsg_endpoint *ept, { priv->tail -= priv->size; } + + nxsem_get_value(&priv->sem, &sval); + while (sval++ < 0) + { + nxsem_post(&priv->sem); + } } priv->transfer = false; @@ -338,6 +368,9 @@ void syslog_rpmsg_init_early(FAR void *buffer, size_t size) char cur; size_t i; + nxsem_init(&priv->sem, 0, 0); + nxsem_set_protocol(&priv->sem, SEM_PRIO_NONE); + priv->buffer = buffer; priv->size = size;