From 453cde945fff338627d9274647ed5467a41fc776 Mon Sep 17 00:00:00 2001 From: yinshengkai Date: Thu, 21 Dec 2023 22:19:19 +0800 Subject: [PATCH] note: add note stream drivers Signed-off-by: yinshengkai --- drivers/note/Kconfig | 20 ++++- drivers/note/Make.defs | 4 + drivers/note/note_driver.c | 6 +- drivers/note/note_initialize.c | 10 +++ drivers/note/notestream_driver.c | 115 +++++++++++++++++++++++++ include/nuttx/note/notestream_driver.h | 77 +++++++++++++++++ 6 files changed, 230 insertions(+), 2 deletions(-) create mode 100644 drivers/note/notestream_driver.c create mode 100644 include/nuttx/note/notestream_driver.h diff --git a/drivers/note/Kconfig b/drivers/note/Kconfig index 225b4f8446..8119904df1 100644 --- a/drivers/note/Kconfig +++ b/drivers/note/Kconfig @@ -92,10 +92,28 @@ config DRIVERS_NOTE_STRIP_FORMAT ---help--- Strip sched_note_printf format string. +config DRIVERS_NOTELOWEROUT + bool "Note lower output" + default n + ---help--- + Note uses lower output. + +config DRIVERS_NOTEFILE + bool "Note file driver" + ---help--- + The Note driver output to file. + +config DRIVERS_NOTEFILE_PATH + string "Note file path" + depends on DRIVERS_NOTEFILE + default "/dev/ttyS1" + ---help--- + The Note driver output to file path. + config DRIVERS_NOTELOG bool "Note syslog driver" ---help--- - The note driver output to syslog. + The Note driver output to syslog. config DRIVERS_NOTESNAP bool "Last scheduling information" diff --git a/drivers/note/Make.defs b/drivers/note/Make.defs index 02a14ee1c9..3878ff4ef7 100644 --- a/drivers/note/Make.defs +++ b/drivers/note/Make.defs @@ -25,6 +25,10 @@ ifeq ($(CONFIG_DRIVERS_NOTE),y) CSRCS += note_initialize.c endif +ifneq ($(CONFIG_DRIVERS_NOTEFILE)$(CONFIG_DRIVERS_NOTELOWEROUT),) + CSRCS += notestream_driver.c +endif + ifeq ($(CONFIG_DRIVERS_NOTERAM),y) CSRCS += noteram_driver.c endif diff --git a/drivers/note/note_driver.c b/drivers/note/note_driver.c index f9bed9bee1..cb0fdd3d8c 100644 --- a/drivers/note/note_driver.c +++ b/drivers/note/note_driver.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -191,7 +192,10 @@ FAR static struct note_driver_s * (FAR struct note_driver_s *)&g_noteram_driver, #endif #ifdef CONFIG_DRIVERS_NOTELOG - &g_notelog_driver, + (FAR struct note_driver_s *)&g_notelog_driver, +#endif +#ifdef CONFIG_DRIVERS_NOTELOWEROUT + (FAR struct note_driver_s *)&g_notestream_lowerout, #endif #ifdef CONFIG_DRIVERS_NOTERPMSG (FAR struct note_driver_s *)&g_noterpmsg_driver, diff --git a/drivers/note/note_initialize.c b/drivers/note/note_initialize.c index 475dafa444..1b933e45d7 100644 --- a/drivers/note/note_initialize.c +++ b/drivers/note/note_initialize.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -105,6 +106,15 @@ int note_initialize(void) } #endif +#ifdef CONFIG_DRIVERS_NOTEFILE + ret = notefile_register(CONFIG_DRIVERS_NOTEFILE_PATH); + if (ret < 0) + { + serr("notefile_register failed %d\n", ret); + return ret; + } +#endif + #ifdef CONFIG_NOTE_RTT ret = notertt_register(); if (ret < 0) diff --git a/drivers/note/notestream_driver.c b/drivers/note/notestream_driver.c new file mode 100644 index 0000000000..2b94528177 --- /dev/null +++ b/drivers/note/notestream_driver.c @@ -0,0 +1,115 @@ +/**************************************************************************** + * drivers/note/notestream_driver.c + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include +#include + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +#ifdef CONFIG_DRIVERS_NOTEFILE +struct notestream_file_s +{ + struct notestream_driver_s driver; + struct lib_fileoutstream_s filestream; + struct file file; +}; +#endif + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static void notestream_add(FAR struct note_driver_s *drv, + FAR const void *note, size_t len); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct note_driver_ops_s g_notestream_ops = +{ + notestream_add +}; + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef CONFIG_DRIVERS_NOTELOWEROUT +struct notestream_driver_s g_notestream_lowerout = +{ + { + &g_notestream_ops + }, + &g_lowoutstream +}; +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void notestream_add(FAR struct note_driver_s *drv, + FAR const void *note, size_t len) +{ + FAR struct notestream_driver_s *drivers = + (FAR struct notestream_driver_s *)drv; + lib_stream_puts(drivers->stream, note, len); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +#ifdef CONFIG_DRIVERS_NOTEFILE +int notefile_register(FAR const char *filename) +{ + FAR struct notestream_file_s *notefile; + int ret; + + notefile = kmm_zalloc(sizeof(struct notestream_file_s)); + if (notefile == NULL) + { + return -ENOMEM; + } + + notefile->driver.stream = ¬efile->filestream.common; + ret = file_open(¬efile->file, filename, O_WRONLY); + if (ret < 0) + { + kmm_free(notefile); + return ret; + } + + notefile->driver.driver.ops = &g_notestream_ops; + lib_fileoutstream(¬efile->filestream, ¬efile->file); + return note_driver_register(¬efile->driver.driver); +} +#endif + diff --git a/include/nuttx/note/notestream_driver.h b/include/nuttx/note/notestream_driver.h new file mode 100644 index 0000000000..c6772a6cc2 --- /dev/null +++ b/include/nuttx/note/notestream_driver.h @@ -0,0 +1,77 @@ +/**************************************************************************** + * include/nuttx/note/notestream_driver.h + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_NOTE_NOTESTREAM_DRIVER_H +#define __INCLUDE_NUTTX_NOTE_NOTESTREAM_DRIVER_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +struct notestream_driver_s +{ + struct note_driver_s driver; + struct lib_outstream_s *stream; +}; + +#if defined(__cplusplus) +extern "C" +{ +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef CONFIG_DRIVERS_NOTELOWEROUT +extern struct notestream_driver_s g_notestream_lowerout; +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: note_driver_unregister + ****************************************************************************/ +#ifdef CONFIG_DRIVERS_NOTEFILE +int notefile_register(FAR const char *filename); +#endif + +#if defined(__cplusplus) +} +#endif + +#endif /* __INCLUDE_NUTTX_NOTE_NOTESTREAM_DRIVER_H */