1 /*
2  * Copyright (c) 2018 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/shell/shell_log_backend.h>
8 #include <zephyr/shell/shell.h>
9 #include "shell_ops.h"
10 #include <zephyr/logging/log_ctrl.h>
11 
12 static bool process_msg_from_buffer(const struct shell *sh);
13 
z_shell_log_backend_output_func(uint8_t * data,size_t length,void * ctx)14 int z_shell_log_backend_output_func(uint8_t *data, size_t length, void *ctx)
15 {
16 	z_shell_print_stream(ctx, data, length);
17 	return length;
18 }
19 
20 /* Set fifo clean state (in case of deferred mode). */
fifo_reset(const struct shell_log_backend * backend)21 static void fifo_reset(const struct shell_log_backend *backend)
22 {
23 	mpsc_pbuf_init(backend->mpsc_buffer, backend->mpsc_buffer_config);
24 }
25 
z_shell_log_backend_enable(const struct shell_log_backend * backend,void * ctx,uint32_t init_log_level)26 void z_shell_log_backend_enable(const struct shell_log_backend *backend,
27 				void *ctx, uint32_t init_log_level)
28 {
29 	int err = 0;
30 
31 	if (IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE)) {
32 		const struct shell *sh;
33 
34 		sh = (const struct shell *)ctx;
35 
36 		z_flag_sync_mode_set(sh, true);
37 		/* Reenable transport in blocking mode */
38 		err = sh->iface->api->enable(sh->iface, true);
39 	}
40 
41 	if (err == 0) {
42 		fifo_reset(backend);
43 		log_backend_enable(backend->backend, ctx, init_log_level);
44 		log_output_ctx_set(backend->log_output, ctx);
45 		backend->control_block->dropped_cnt = 0;
46 		backend->control_block->state = SHELL_LOG_BACKEND_ENABLED;
47 	}
48 }
49 
z_shell_log_backend_disable(const struct shell_log_backend * backend)50 void z_shell_log_backend_disable(const struct shell_log_backend *backend)
51 {
52 	log_backend_disable(backend->backend);
53 	backend->control_block->state = SHELL_LOG_BACKEND_DISABLED;
54 }
55 
z_shell_log_backend_process(const struct shell_log_backend * backend)56 bool z_shell_log_backend_process(const struct shell_log_backend *backend)
57 {
58 	const struct shell *sh =
59 			(const struct shell *)backend->backend->cb->ctx;
60 	uint32_t dropped;
61 	bool colors = IS_ENABLED(CONFIG_SHELL_VT100_COLORS) &&
62 			z_flag_use_colors_get(sh);
63 
64 	dropped = atomic_set(&backend->control_block->dropped_cnt, 0);
65 	if (dropped) {
66 		struct shell_vt100_colors col;
67 
68 		if (colors) {
69 			z_shell_vt100_colors_store(sh, &col);
70 			z_shell_vt100_color_set(sh, SHELL_VT100_COLOR_RED);
71 		}
72 
73 		log_output_dropped_process(backend->log_output, dropped);
74 
75 		if (colors) {
76 			z_shell_vt100_colors_restore(sh, &col);
77 		}
78 	}
79 
80 	return process_msg_from_buffer(sh);
81 }
82 
panic(const struct log_backend * const backend)83 static void panic(const struct log_backend *const backend)
84 {
85 	const struct shell *sh = (const struct shell *)backend->cb->ctx;
86 	int err;
87 
88 	if (IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE)) {
89 		return;
90 	}
91 
92 	err = sh->iface->api->enable(sh->iface, true);
93 
94 	if (err == 0) {
95 		sh->log_backend->control_block->state =
96 						SHELL_LOG_BACKEND_PANIC;
97 		z_flag_sync_mode_set(sh, true);
98 
99 		/* Move to the start of next line. */
100 		z_shell_multiline_data_calc(&sh->ctx->vt100_ctx.cons,
101 					    sh->ctx->cmd_buff_pos,
102 					    sh->ctx->cmd_buff_len);
103 		z_shell_op_cursor_vert_move(sh, -1);
104 		z_shell_op_cursor_horiz_move(sh,
105 					   -sh->ctx->vt100_ctx.cons.cur_x);
106 
107 		while (process_msg_from_buffer(sh)) {
108 			/* empty */
109 		}
110 	} else {
111 		z_shell_log_backend_disable(sh->log_backend);
112 	}
113 }
114 
dropped(const struct log_backend * const backend,uint32_t cnt)115 static void dropped(const struct log_backend *const backend, uint32_t cnt)
116 {
117 	const struct shell *sh = (const struct shell *)backend->cb->ctx;
118 	const struct shell_log_backend *log_backend = sh->log_backend;
119 
120 	if (IS_ENABLED(CONFIG_SHELL_STATS)) {
121 		atomic_add(&sh->stats->log_lost_cnt, cnt);
122 	}
123 	atomic_add(&log_backend->control_block->dropped_cnt, cnt);
124 }
125 
copy_to_pbuffer(struct mpsc_pbuf_buffer * mpsc_buffer,union log_msg_generic * msg,uint32_t timeout)126 static bool copy_to_pbuffer(struct mpsc_pbuf_buffer *mpsc_buffer,
127 			    union log_msg_generic *msg, uint32_t timeout)
128 {
129 	size_t wlen;
130 	union mpsc_pbuf_generic *dst;
131 
132 	wlen = log_msg_generic_get_wlen((union mpsc_pbuf_generic *)msg);
133 	dst = mpsc_pbuf_alloc(mpsc_buffer, wlen, K_MSEC(timeout));
134 	if (!dst) {
135 		/* No space to store the log */
136 		return false;
137 	}
138 
139 	/* First word contains internal mpsc packet flags and when copying
140 	 * those flags must be omitted.
141 	 */
142 	uint8_t *dst_data = (uint8_t *)dst + sizeof(struct mpsc_pbuf_hdr);
143 	uint8_t *src_data = (uint8_t *)msg + sizeof(struct mpsc_pbuf_hdr);
144 	size_t hdr_wlen = DIV_ROUND_UP(sizeof(struct mpsc_pbuf_hdr),
145 					   sizeof(uint32_t));
146 	if (wlen <= hdr_wlen) {
147 		return false;
148 	}
149 
150 	dst->hdr.data = msg->buf.hdr.data;
151 	memcpy(dst_data, src_data, (wlen - hdr_wlen) * sizeof(uint32_t));
152 
153 	mpsc_pbuf_commit(mpsc_buffer, dst);
154 
155 	return true;
156 }
157 
process_log_msg(const struct shell * sh,const struct log_output * log_output,union log_msg_generic * msg,bool locked,bool colors)158 static void process_log_msg(const struct shell *sh,
159 			     const struct log_output *log_output,
160 			     union log_msg_generic *msg,
161 			     bool locked, bool colors)
162 {
163 	unsigned int key = 0;
164 	uint32_t flags = LOG_OUTPUT_FLAG_LEVEL | LOG_OUTPUT_FLAG_TIMESTAMP | LOG_OUTPUT_FLAG_THREAD;
165 
166 	if (IS_ENABLED(CONFIG_SHELL_LOG_FORMAT_TIMESTAMP)) {
167 		flags |= LOG_OUTPUT_FLAG_FORMAT_TIMESTAMP;
168 	}
169 
170 	if (colors) {
171 		flags |= LOG_OUTPUT_FLAG_COLORS;
172 	}
173 
174 	if (locked) {
175 		/*
176 		 * If running in the thread context, lock the shell mutex to synchronize with
177 		 * messages printed on the shell thread. In the ISR context, using a mutex is
178 		 * forbidden so use the IRQ lock to at least synchronize log messages printed
179 		 * in different contexts.
180 		 */
181 		if (k_is_in_isr()) {
182 			key = irq_lock();
183 		} else {
184 			k_mutex_lock(&sh->ctx->wr_mtx, K_FOREVER);
185 		}
186 		if (!z_flag_cmd_ctx_get(sh)) {
187 			z_shell_cmd_line_erase(sh);
188 		}
189 	}
190 
191 	log_output_msg_process(log_output, &msg->log, flags);
192 
193 	if (locked) {
194 		if (!z_flag_cmd_ctx_get(sh)) {
195 			z_shell_print_prompt_and_cmd(sh);
196 		}
197 		if (k_is_in_isr()) {
198 			irq_unlock(key);
199 		} else {
200 			k_mutex_unlock(&sh->ctx->wr_mtx);
201 		}
202 	}
203 }
204 
process_msg_from_buffer(const struct shell * sh)205 static bool process_msg_from_buffer(const struct shell *sh)
206 {
207 	const struct shell_log_backend *log_backend = sh->log_backend;
208 	struct mpsc_pbuf_buffer *mpsc_buffer = log_backend->mpsc_buffer;
209 	const struct log_output *log_output = log_backend->log_output;
210 	union log_msg_generic *msg;
211 	bool colors = IS_ENABLED(CONFIG_SHELL_VT100_COLORS) &&
212 			z_flag_use_colors_get(sh);
213 
214 	msg = (union log_msg_generic *)mpsc_pbuf_claim(mpsc_buffer);
215 	if (!msg) {
216 		return false;
217 	}
218 
219 	process_log_msg(sh, log_output, msg, false, colors);
220 
221 	mpsc_pbuf_free(mpsc_buffer, &msg->buf);
222 
223 	return true;
224 }
225 
process(const struct log_backend * const backend,union log_msg_generic * msg)226 static void process(const struct log_backend *const backend,
227 		    union log_msg_generic *msg)
228 {
229 	const struct shell *sh = (const struct shell *)backend->cb->ctx;
230 	const struct shell_log_backend *log_backend = sh->log_backend;
231 	struct mpsc_pbuf_buffer *mpsc_buffer = log_backend->mpsc_buffer;
232 	const struct log_output *log_output = log_backend->log_output;
233 	bool colors = IS_ENABLED(CONFIG_SHELL_VT100_COLORS) &&
234 			z_flag_use_colors_get(sh);
235 	struct k_poll_signal *signal;
236 
237 	switch (sh->log_backend->control_block->state) {
238 	case SHELL_LOG_BACKEND_ENABLED:
239 		if (IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE)) {
240 			process_log_msg(sh, log_output, msg, true, colors);
241 		} else {
242 			if (copy_to_pbuffer(mpsc_buffer, msg, log_backend->timeout)) {
243 				if (IS_ENABLED(CONFIG_MULTITHREADING)) {
244 					signal = &sh->ctx->signals[SHELL_SIGNAL_LOG_MSG];
245 					k_poll_signal_raise(signal, 0);
246 				}
247 			} else {
248 				dropped(backend, 1);
249 			}
250 		}
251 
252 		break;
253 	case SHELL_LOG_BACKEND_PANIC:
254 		z_shell_cmd_line_erase(sh);
255 		process_log_msg(sh, log_output, msg, true, colors);
256 
257 		break;
258 
259 	case SHELL_LOG_BACKEND_DISABLED:
260 		__fallthrough;
261 	default:
262 		break;
263 	}
264 }
265 
266 const struct log_backend_api log_backend_shell_api = {
267 	.process = process,
268 	.dropped = dropped,
269 	.panic = panic,
270 };
271