|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +/* |
| 8 | + * AT#XLOG command — enable/disable the Zephyr UART log backend at runtime. |
| 9 | + * |
| 10 | + * The UART (zephyr,console) is shared with the modem-trace backend |
| 11 | + * (sm_trace_backend_uart.c). AT#XLOG and AT#XTRACE are mutually exclusive: |
| 12 | + * each refuses to activate while the other is in use. |
| 13 | + * |
| 14 | + */ |
| 15 | + |
| 16 | +#include <zephyr/devicetree.h> |
| 17 | + |
| 18 | +/* Only compiled if zephyr,console is present and active in the devicetree. */ |
| 19 | +#if DT_HAS_CHOSEN(zephyr_console) && DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_console), okay) |
| 20 | + |
| 21 | +#include <zephyr/drivers/uart.h> |
| 22 | +#include <zephyr/kernel.h> |
| 23 | +#include <zephyr/logging/log.h> |
| 24 | +#include <zephyr/logging/log_ctrl.h> |
| 25 | +#include <zephyr/pm/device.h> |
| 26 | + |
| 27 | +#include "sm_at_host.h" |
| 28 | + |
| 29 | +LOG_MODULE_REGISTER(sm_log_uart, CONFIG_SM_LOG_LEVEL); |
| 30 | + |
| 31 | +/* Zephyr console UART is used both for application logs and modem traces. */ |
| 32 | +#define UART_DEVICE_NODE DT_CHOSEN(zephyr_console) |
| 33 | +static const struct device *const uart_dev = DEVICE_DT_GET(UART_DEVICE_NODE); |
| 34 | + |
| 35 | +static bool log_active; |
| 36 | + |
| 37 | +static int uart_suspend(void) |
| 38 | +{ |
| 39 | + int ret = pm_device_action_run(uart_dev, PM_DEVICE_ACTION_SUSPEND); |
| 40 | + |
| 41 | + if (ret && ret != -EALREADY) { |
| 42 | + LOG_ERR("Failed to %s UART device: %d", "suspend", ret); |
| 43 | + return ret; |
| 44 | + } |
| 45 | + |
| 46 | + return 0; |
| 47 | +} |
| 48 | + |
| 49 | +static int uart_resume(void) |
| 50 | +{ |
| 51 | + int ret = pm_device_action_run(uart_dev, PM_DEVICE_ACTION_RESUME); |
| 52 | + |
| 53 | + if (ret && ret != -EALREADY) { |
| 54 | + LOG_ERR("Failed to %s UART device: %d", "resume", ret); |
| 55 | + return ret; |
| 56 | + } |
| 57 | + return 0; |
| 58 | +} |
| 59 | + |
| 60 | +static bool uart_is_active(void) |
| 61 | +{ |
| 62 | + enum pm_device_state state = PM_DEVICE_STATE_OFF; |
| 63 | + int err = pm_device_state_get(uart_dev, &state); |
| 64 | + |
| 65 | + if (err) { |
| 66 | + LOG_ERR("Failed to get UART device state (%d).", err); |
| 67 | + return false; |
| 68 | + } |
| 69 | + return state == PM_DEVICE_STATE_ACTIVE; |
| 70 | +} |
| 71 | + |
| 72 | +SM_AT_CMD_CUSTOM(xlog, "AT#XLOG", handle_at_log); |
| 73 | +STATIC int handle_at_log(enum at_parser_cmd_type cmd_type, struct at_parser *parser, uint32_t) |
| 74 | +{ |
| 75 | + const struct log_backend *log_be = log_backend_get_by_name("log_backend_uart"); |
| 76 | + |
| 77 | + if (!log_be) { |
| 78 | + return -ENODEV; |
| 79 | + } |
| 80 | + |
| 81 | + if (cmd_type == AT_PARSER_CMD_TYPE_SET) { |
| 82 | + int mode; |
| 83 | + int ret = at_parser_num_get(parser, 1, &mode); |
| 84 | + |
| 85 | + if (ret || (mode < 0) || (mode > 1)) { |
| 86 | + return -EINVAL; |
| 87 | + } |
| 88 | + |
| 89 | + if (mode == (int)log_active) { |
| 90 | + return 0; |
| 91 | + } |
| 92 | + |
| 93 | + if (mode == 1 && uart_is_active()) { |
| 94 | + return -EBUSY; |
| 95 | + } |
| 96 | + |
| 97 | + if (mode == 1) { |
| 98 | + ret = uart_resume(); |
| 99 | + if (ret) { |
| 100 | + return ret; |
| 101 | + } |
| 102 | + if (!log_be->cb->initialized) { |
| 103 | + log_backend_init(log_be); |
| 104 | + } |
| 105 | + log_backend_enable(log_be, log_be->cb->ctx, CONFIG_LOG_DEFAULT_LEVEL); |
| 106 | + log_active = true; |
| 107 | + } else { |
| 108 | + log_backend_disable(log_be); |
| 109 | + ret = uart_suspend(); |
| 110 | + if (ret) { |
| 111 | + return ret; |
| 112 | + } |
| 113 | + log_active = false; |
| 114 | + } |
| 115 | + return 0; |
| 116 | + } else if (cmd_type == AT_PARSER_CMD_TYPE_READ) { |
| 117 | + rsp_send("\r\n#XLOG: %d\r\n", (int)log_active); |
| 118 | + return 0; |
| 119 | + } else if (cmd_type == AT_PARSER_CMD_TYPE_TEST) { |
| 120 | + rsp_send("\r\n#XLOG: (0,1)\r\n"); |
| 121 | + return 0; |
| 122 | + } |
| 123 | + |
| 124 | + return -EINVAL; |
| 125 | +} |
| 126 | + |
| 127 | +static int sm_log_uart_init(void) |
| 128 | +{ |
| 129 | + if (!IS_ENABLED(CONFIG_LOG_BACKEND_UART) || |
| 130 | + !IS_ENABLED(CONFIG_LOG_BACKEND_UART_AUTOSTART)) { |
| 131 | + /* Start with UART log backend disabled. */ |
| 132 | + if (uart_suspend()) { |
| 133 | + LOG_ERR("Failed to suspend UART log backend"); |
| 134 | + sm_init_failed = true; |
| 135 | + return -EFAULT; |
| 136 | + } |
| 137 | + } else { |
| 138 | + log_active = true; |
| 139 | + } |
| 140 | + |
| 141 | + return 0; |
| 142 | +} |
| 143 | + |
| 144 | +SYS_INIT(sm_log_uart_init, POST_KERNEL, CONFIG_APPLICATION_INIT_PRIORITY); |
| 145 | + |
| 146 | +#endif /* DT_HAS_CHOSEN(zephyr_console) */ |
0 commit comments