lib: os: add a header for printk hook functions

Add a zephyr/printk.h header for the __printk_hook functions, these are
currently manually declared by all console drivers for no good reason.

Move the documentation into the header and also unify the way that
console drivers call the function.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
This commit is contained in:
Fabio Baltieri 2024-07-23 12:01:03 +01:00 committed by Anas Nashif
commit ef14c9b867
15 changed files with 59 additions and 80 deletions

View file

@ -16,7 +16,7 @@
#include <zephyr/device.h>
#include <zephyr/init.h>
#include <zephyr/sys/printk.h>
#include <zephyr/sys/printk-hooks.h>
extern int efi_console_putchar(int c);
@ -43,10 +43,6 @@ static int console_out(int c)
extern void __stdout_hook_install(int (*hook)(int));
#endif
#if defined(CONFIG_PRINTK)
extern void __printk_hook_install(int (*fn)(int));
#endif
/**
* @brief Install printk/stdout hook for EFI console output
*/

View file

@ -8,6 +8,7 @@
#include <zephyr/device.h>
#include <zephyr/init.h>
#include <zephyr/drivers/ipm.h>
#include <zephyr/sys/printk-hooks.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(ipm_console, CONFIG_IPM_LOG_LEVEL);
@ -43,25 +44,17 @@ static int console_out(int c)
#if defined(CONFIG_STDOUT_CONSOLE)
extern void __stdout_hook_install(int (*hook)(int));
#else
#define __stdout_hook_install(x) \
do { /* nothing */ \
} while ((0))
#endif
#if defined(CONFIG_PRINTK)
extern void __printk_hook_install(int (*fn)(int));
#else
#define __printk_hook_install(x) \
do { /* nothing */ \
} while ((0))
#endif
/* Install printk/stdout hooks */
static void ipm_console_hook_install(void)
{
#if defined(CONFIG_STDOUT_CONSOLE)
__stdout_hook_install(console_out);
#endif
#if defined(CONFIG_PRINTK)
__printk_hook_install(console_out);
#endif
}
static int ipm_console_init(void)

View file

@ -10,6 +10,7 @@
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/sys/printk-hooks.h>
#include <zephyr/drivers/ipm.h>
#include <zephyr/drivers/console/ipm_console.h>
@ -30,7 +31,6 @@ static int consoleOut(int character)
return character;
}
extern void __printk_hook_install(int (*fn)(int));
extern void __stdout_hook_install(int (*fn)(int));
int ipm_console_sender_init(const struct device *d)

View file

@ -5,6 +5,7 @@
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/sys/printk-hooks.h>
#include <zephyr/device.h>
#include <zephyr/init.h>
@ -27,18 +28,6 @@ static int console_out(int c)
#if defined(CONFIG_STDOUT_CONSOLE)
extern void __stdout_hook_install(int (*hook)(int));
#else
#define __stdout_hook_install(x) \
do {/* nothing */ \
} while ((0))
#endif
#if defined(CONFIG_PRINTK)
extern void __printk_hook_install(int (*fn)(int));
#else
#define __printk_hook_install(x) \
do {/* nothing */ \
} while ((0))
#endif
/**
@ -47,8 +36,12 @@ extern void __printk_hook_install(int (*fn)(int));
*/
static int jailhouse_console_init(void)
{
#if defined(CONFIG_STDOUT_CONSOLE)
__stdout_hook_install(console_out);
#endif
#if defined(CONFIG_PRINTK)
__printk_hook_install(console_out);
#endif
return 0;
}

View file

@ -7,6 +7,7 @@
#include <zephyr/init.h>
#include <zephyr/arch/posix/posix_trace.h>
#include <zephyr/sys/printk-hooks.h>
#define _STDOUT_BUF_SIZE 256
static char stdout_buff[_STDOUT_BUF_SIZE];
@ -51,7 +52,6 @@ void posix_flush_stdout(void)
static int posix_arch_console_init(void)
{
#ifdef CONFIG_PRINTK
extern void __printk_hook_install(int (*fn)(int));
__printk_hook_install(print_char);
#endif
#ifdef CONFIG_STDOUT_CONSOLE

View file

@ -10,6 +10,7 @@
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/sys/printk-hooks.h>
#include <zephyr/device.h>
#include <zephyr/init.h>
#include <zephyr/linker/devicetree_regions.h>
@ -27,7 +28,6 @@
#define RAM_CONSOLE_BUF_ATTR
#endif
extern void __printk_hook_install(int (*fn)(int));
extern void __stdout_hook_install(int (*fn)(int));
char ram_console_buf[CONFIG_RAM_CONSOLE_BUFFER_SIZE] RAM_CONSOLE_BUF_ATTR;

View file

@ -11,11 +11,11 @@
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/sys/printk-hooks.h>
#include <zephyr/device.h>
#include <zephyr/init.h>
#include <SEGGER_RTT.h>
extern void __printk_hook_install(int (*fn)(int));
extern void __stdout_hook_install(int (*fn)(int));
static bool host_present;

View file

@ -31,6 +31,7 @@
#include <zephyr/linker/sections.h>
#include <zephyr/sys/atomic.h>
#include <zephyr/sys/printk.h>
#include <zephyr/sys/printk-hooks.h>
#include <zephyr/pm/device_runtime.h>
#ifdef CONFIG_UART_CONSOLE_MCUMGR
#include <zephyr/mgmt/mcumgr/transport/serial.h>
@ -114,10 +115,6 @@ static int console_out(int c)
extern void __stdout_hook_install(int (*hook)(int c));
#endif
#if defined(CONFIG_PRINTK)
extern void __printk_hook_install(int (*fn)(int c));
#endif
#if defined(CONFIG_CONSOLE_HANDLER)
static struct k_fifo *avail_queue;
static struct k_fifo *lines_queue;

View file

@ -7,6 +7,7 @@
#include <zephyr/init.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/winstream.h>
#include <zephyr/sys/printk-hooks.h>
#include <zephyr/devicetree.h>
#include <zephyr/cache.h>
@ -48,24 +49,16 @@ int arch_printk_char_out(int c)
#if defined(CONFIG_STDOUT_CONSOLE)
extern void __stdout_hook_install(int (*hook)(int));
#else
#define __stdout_hook_install(x) \
do {/* nothing */ \
} while ((0))
#endif
#if defined(CONFIG_PRINTK)
extern void __printk_hook_install(int (*fn)(int));
#else
#define __printk_hook_install(x) \
do {/* nothing */ \
} while ((0))
#endif
static void winstream_console_hook_install(void)
{
#if defined(CONFIG_STDOUT_CONSOLE)
__stdout_hook_install(arch_printk_char_out);
#endif
#if defined(CONFIG_PRINTK)
__printk_hook_install(arch_printk_char_out);
#endif
}

View file

@ -6,6 +6,7 @@
#include <xtensa/simcall.h>
#include <zephyr/device.h>
#include <zephyr/init.h>
#include <zephyr/sys/printk-hooks.h>
#if defined(CONFIG_PRINTK) || defined(CONFIG_STDOUT_CONSOLE)
/**
@ -35,18 +36,6 @@ int arch_printk_char_out(int c)
#if defined(CONFIG_STDOUT_CONSOLE)
extern void __stdout_hook_install(int (*hook)(int));
#else
#define __stdout_hook_install(x) \
do {/* nothing */ \
} while ((0))
#endif
#if defined(CONFIG_PRINTK)
extern void __printk_hook_install(int (*fn)(int));
#else
#define __printk_hook_install(x) \
do {/* nothing */ \
} while ((0))
#endif
/**
@ -54,8 +43,12 @@ extern void __printk_hook_install(int (*fn)(int));
*/
static void xt_sim_console_hook_install(void)
{
#if defined(CONFIG_STDOUT_CONSOLE)
__stdout_hook_install(arch_printk_char_out);
#endif
#if defined(CONFIG_PRINTK)
__printk_hook_install(arch_printk_char_out);
#endif
}
/**

View file

@ -17,6 +17,7 @@
#include <zephyr/init.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/device_mmio.h>
#include <zephyr/sys/printk-hooks.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(uart_hvc_xen, CONFIG_UART_LOG_LEVEL);
@ -267,7 +268,6 @@ DEVICE_DT_DEFINE(DT_NODELABEL(xen_hvc), xen_console_init, NULL, &xen_hvc_data,
&xen_hvc_api);
#ifdef CONFIG_XEN_EARLY_CONSOLEIO
extern void __printk_hook_install(int (*fn)(int));
extern void __stdout_hook_install(int (*fn)(int));
int xen_consoleio_putc(int c)

View file

@ -0,0 +1,29 @@
/*
* Copyright 2024 Google LLC
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_SYS_PRINTK_HOOKS_H_
#define ZEPHYR_INCLUDE_SYS_PRINTK_HOOKS_H_
/**
* @brief Install the character output routine for printk
*
* To be called by the platform's console driver at init time. Installs a
* routine that outputs one ASCII character at a time.
* @param fn putc routine to install
*/
void __printk_hook_install(int (*fn)(int c));
/**
* @brief Get the current character output routine for printk
*
* To be called by any console driver that would like to save
* current hook - if any - for later re-installation.
*
* @return a function pointer or NULL if no hook is set
*/
void *__printk_get_hook(void);
#endif /* ZEPHYR_INCLUDE_SYS_PRINTK_HOOKS_H_ */

View file

@ -14,6 +14,7 @@
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/sys/printk-hooks.h>
#include <stdarg.h>
#include <zephyr/toolchain.h>
#include <zephyr/linker/sections.h>
@ -54,26 +55,11 @@ __attribute__((weak)) int arch_printk_char_out(int c)
static int (*_char_out)(int c) = arch_printk_char_out;
/**
* @brief Install the character output routine for printk
*
* To be called by the platform's console driver at init time. Installs a
* routine that outputs one ASCII character at a time.
* @param fn putc routine to install
*/
void __printk_hook_install(int (*fn)(int c))
{
_char_out = fn;
}
/**
* @brief Get the current character output routine for printk
*
* To be called by any console driver that would like to save
* current hook - if any - for later re-installation.
*
* @return a function pointer or NULL if no hook is set
*/
void *__printk_get_hook(void)
{
return _char_out;

View file

@ -16,6 +16,7 @@
#include <zephyr/init.h>
#include <zephyr/drivers/uart_pipe.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/printk-hooks.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/logging/log_backend.h>
@ -260,7 +261,6 @@ static int monitor_console_out(int c)
return c;
}
extern void __printk_hook_install(int (*fn)(int));
extern void __stdout_hook_install(int (*fn)(int));
#endif /* !CONFIG_UART_CONSOLE && !CONFIG_RTT_CONSOLE && !CONFIG_LOG_PRINTK */

View file

@ -5,14 +5,13 @@
*/
#include <zephyr/ztest.h>
#include <zephyr/sys/printk-hooks.h>
#define BUF_SZ 1024
static int pos;
char pk_console[BUF_SZ];
void __printk_hook_install(int (*fn)(int));
void *__printk_get_hook(void);
int (*_old_char_out)(int);
#if defined(CONFIG_PICOLIBC)