1 /*
2  * Copyright (c) 2018 omSquare s.r.o.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/logging/log_backend.h>
8 #include <zephyr/logging/log_core.h>
9 #include <zephyr/logging/log_output.h>
10 #include <zephyr/logging/log_backend_std.h>
11 #include <SEGGER_RTT.h>
12 
13 #ifndef CONFIG_LOG_BACKEND_RTT_BUFFER_SIZE
14 #define CONFIG_LOG_BACKEND_RTT_BUFFER_SIZE 0
15 #endif
16 
17 #ifndef CONFIG_LOG_BACKEND_RTT_MESSAGE_SIZE
18 #define CONFIG_LOG_BACKEND_RTT_MESSAGE_SIZE 0
19 #endif
20 
21 #ifndef CONFIG_LOG_BACKEND_RTT_OUTPUT_BUFFER_SIZE
22 #define CONFIG_LOG_BACKEND_RTT_OUTPUT_BUFFER_SIZE 0
23 #endif
24 
25 #ifndef CONFIG_LOG_BACKEND_RTT_RETRY_DELAY_MS
26 /* Long enough to detect host presence */
27 #define CONFIG_LOG_BACKEND_RTT_RETRY_DELAY_MS 10
28 #endif
29 
30 #ifndef CONFIG_LOG_BACKEND_RTT_RETRY_CNT
31 /* Big enough to detect host presence */
32 #define CONFIG_LOG_BACKEND_RTT_RETRY_CNT 10
33 #endif
34 
35 #define DROP_MAX 99
36 
37 #define DROP_MSG "messages dropped:    \r\n"
38 
39 #define DROP_MSG_LEN (sizeof(DROP_MSG) - 1)
40 
41 #define MESSAGE_SIZE CONFIG_LOG_BACKEND_RTT_MESSAGE_SIZE
42 
43 #define CHAR_BUF_SIZE \
44 	((IS_ENABLED(CONFIG_LOG_BACKEND_RTT_MODE_BLOCK) && \
45 	 !IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE)) ? \
46 		CONFIG_LOG_BACKEND_RTT_OUTPUT_BUFFER_SIZE : 1)
47 
48 #define RTT_LOCK() \
49 	COND_CODE_0(CONFIG_LOG_BACKEND_RTT_BUFFER, (SEGGER_RTT_LOCK()), ())
50 
51 #define RTT_UNLOCK() \
52 	COND_CODE_0(CONFIG_LOG_BACKEND_RTT_BUFFER, (SEGGER_RTT_UNLOCK()), ())
53 
54 #define RTT_BUFFER_SIZE \
55 	COND_CODE_0(CONFIG_LOG_BACKEND_RTT_BUFFER, \
56 		(0), (CONFIG_LOG_BACKEND_RTT_BUFFER_SIZE))
57 
58 
59 static const char *drop_msg = DROP_MSG;
60 static uint8_t rtt_buf[RTT_BUFFER_SIZE];
61 static uint8_t line_buf[MESSAGE_SIZE + DROP_MSG_LEN];
62 static uint8_t *line_pos;
63 static uint8_t char_buf[CHAR_BUF_SIZE];
64 static int drop_cnt;
65 static int drop_warn;
66 static bool panic_mode;
67 static bool host_present;
68 
69 static int data_out_block_mode(uint8_t *data, size_t length, void *ctx);
70 static int data_out_drop_mode(uint8_t *data, size_t length, void *ctx);
71 
72 static int char_out_drop_mode(uint8_t data);
73 static int line_out_drop_mode(void);
74 static uint32_t log_format_current = CONFIG_LOG_BACKEND_RTT_OUTPUT_DEFAULT;
75 
is_sync_mode(void)76 static inline bool is_sync_mode(void)
77 {
78 	return IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE) || panic_mode;
79 }
80 
is_panic_mode(void)81 static inline bool is_panic_mode(void)
82 {
83 	return panic_mode;
84 }
85 
data_out_drop_mode(uint8_t * data,size_t length,void * ctx)86 static int data_out_drop_mode(uint8_t *data, size_t length, void *ctx)
87 {
88 	(void) ctx;
89 	uint8_t *pos;
90 
91 	if (is_sync_mode()) {
92 		return data_out_block_mode(data, length, ctx);
93 	}
94 
95 	for (pos = data; pos < data + length; pos++) {
96 		if (char_out_drop_mode(*pos)) {
97 			break;
98 		}
99 	}
100 
101 	return (int) (pos - data);
102 }
103 
char_out_drop_mode(uint8_t data)104 static int char_out_drop_mode(uint8_t data)
105 {
106 	if (data == '\n') {
107 		if (line_out_drop_mode()) {
108 			return 1;
109 		}
110 		line_pos = line_buf;
111 		return 0;
112 	}
113 
114 	if (line_pos < line_buf + MESSAGE_SIZE - 1) {
115 		*line_pos++ = data;
116 	}
117 
118 	/* not enough space in line buffer, we have to wait for EOL */
119 	return 0;
120 }
121 
line_out_drop_mode(void)122 static int line_out_drop_mode(void)
123 {
124 	/* line cannot be empty */
125 	__ASSERT_NO_MSG(line_pos > line_buf);
126 
127 	/* Handle the case if line contains only '\n' */
128 	if (line_pos - line_buf == 1) {
129 		line_pos++;
130 	}
131 
132 	*(line_pos - 1) = '\r';
133 	*line_pos++ = '\n';
134 
135 	if (drop_cnt > 0 && !drop_warn) {
136 		int cnt = MIN(drop_cnt, DROP_MAX);
137 
138 		__ASSERT_NO_MSG(line_pos - line_buf <= MESSAGE_SIZE);
139 
140 		memmove(line_buf + DROP_MSG_LEN, line_buf, line_pos - line_buf);
141 		(void)memcpy(line_buf, drop_msg, DROP_MSG_LEN);
142 		line_pos += DROP_MSG_LEN;
143 		drop_warn = 1;
144 
145 
146 		if (cnt < 10) {
147 			line_buf[DROP_MSG_LEN - 2] = ' ';
148 			line_buf[DROP_MSG_LEN - 3] = (uint8_t) ('0' + cnt);
149 			line_buf[DROP_MSG_LEN - 4] = ' ';
150 		} else {
151 			line_buf[DROP_MSG_LEN - 2] = (uint8_t) ('0' + cnt % 10);
152 			line_buf[DROP_MSG_LEN - 3] = (uint8_t) ('0' + cnt / 10);
153 			line_buf[DROP_MSG_LEN - 4] = '>';
154 		}
155 	}
156 
157 	int ret;
158 
159 	RTT_LOCK();
160 	ret = SEGGER_RTT_WriteSkipNoLock(CONFIG_LOG_BACKEND_RTT_BUFFER,
161 					 line_buf, line_pos - line_buf);
162 	RTT_UNLOCK();
163 
164 	if (ret == 0) {
165 		drop_cnt++;
166 	} else {
167 		drop_cnt = 0;
168 		drop_warn = 0;
169 	}
170 
171 	return 0;
172 }
173 
on_failed_write(int retry_cnt)174 static void on_failed_write(int retry_cnt)
175 {
176 	if (retry_cnt == 0) {
177 		host_present = false;
178 	} else if (is_sync_mode()) {
179 		k_busy_wait(USEC_PER_MSEC *
180 				CONFIG_LOG_BACKEND_RTT_RETRY_DELAY_MS);
181 	} else {
182 		k_msleep(CONFIG_LOG_BACKEND_RTT_RETRY_DELAY_MS);
183 	}
184 }
185 
on_write(int retry_cnt)186 static void on_write(int retry_cnt)
187 {
188 	host_present = true;
189 	if (is_panic_mode()) {
190 		/* In panic mode block on each write until host reads it. This
191 		 * way it is ensured that if system resets all messages are read
192 		 * by the host. While pending on data being read by the host we
193 		 * must also detect situation where host is disconnected.
194 		 */
195 		while (SEGGER_RTT_HasDataUp(CONFIG_LOG_BACKEND_RTT_BUFFER) &&
196 			host_present) {
197 			on_failed_write(retry_cnt--);
198 		}
199 	}
200 
201 }
202 
data_out_block_mode(uint8_t * data,size_t length,void * ctx)203 static int data_out_block_mode(uint8_t *data, size_t length, void *ctx)
204 {
205 	int ret = 0;
206 	/* This function is also called in drop mode for synchronous operation
207 	 * in that case retry is undesired */
208 	int retry_cnt = IS_ENABLED(CONFIG_LOG_BACKEND_RTT_MODE_BLOCK) ?
209 			 CONFIG_LOG_BACKEND_RTT_RETRY_CNT : 1;
210 
211 	do {
212 		if (!is_sync_mode()) {
213 			RTT_LOCK();
214 			ret = SEGGER_RTT_WriteSkipNoLock(CONFIG_LOG_BACKEND_RTT_BUFFER,
215 							 data, length);
216 			RTT_UNLOCK();
217 		} else {
218 			ret = SEGGER_RTT_WriteSkipNoLock(CONFIG_LOG_BACKEND_RTT_BUFFER,
219 							 data, length);
220 		}
221 
222 		if (ret) {
223 			on_write(retry_cnt);
224 		} else if (host_present) {
225 			retry_cnt--;
226 			on_failed_write(retry_cnt);
227 		} else {
228 		}
229 	} while ((ret == 0) && host_present);
230 
231 	return ((ret == 0) && host_present) ? 0 : length;
232 }
233 
data_out_overwrite_mode(uint8_t * data,size_t length,void * ctx)234 static int data_out_overwrite_mode(uint8_t *data, size_t length, void *ctx)
235 {
236 	if (!is_sync_mode()) {
237 		RTT_LOCK();
238 		SEGGER_RTT_WriteWithOverwriteNoLock(CONFIG_LOG_BACKEND_RTT_BUFFER,
239 						    data, length);
240 
241 		RTT_UNLOCK();
242 	} else {
243 		SEGGER_RTT_WriteWithOverwriteNoLock(CONFIG_LOG_BACKEND_RTT_BUFFER,
244 						    data, length);
245 	}
246 
247 	return length;
248 }
249 
250 LOG_OUTPUT_DEFINE(log_output_rtt,
251 		  IS_ENABLED(CONFIG_LOG_BACKEND_RTT_MODE_BLOCK) ?
252 		  data_out_block_mode :
253 		  IS_ENABLED(CONFIG_LOG_BACKEND_RTT_MODE_OVERWRITE) ?
254 		  data_out_overwrite_mode : data_out_drop_mode,
255 		  char_buf, sizeof(char_buf));
256 
log_backend_rtt_cfg(void)257 static void log_backend_rtt_cfg(void)
258 {
259 	SEGGER_RTT_ConfigUpBuffer(CONFIG_LOG_BACKEND_RTT_BUFFER, "Logger",
260 				  rtt_buf, sizeof(rtt_buf),
261 				  SEGGER_RTT_MODE_NO_BLOCK_SKIP);
262 }
263 
log_backend_rtt_init(struct log_backend const * const backend)264 static void log_backend_rtt_init(struct log_backend const *const backend)
265 {
266 	if (CONFIG_LOG_BACKEND_RTT_BUFFER > 0) {
267 		log_backend_rtt_cfg();
268 	}
269 
270 	host_present = true;
271 	line_pos = line_buf;
272 }
273 
panic(struct log_backend const * const backend)274 static void panic(struct log_backend const *const backend)
275 {
276 	panic_mode = true;
277 	log_backend_std_panic(&log_output_rtt);
278 }
279 
dropped(const struct log_backend * const backend,uint32_t cnt)280 static void dropped(const struct log_backend *const backend, uint32_t cnt)
281 {
282 	ARG_UNUSED(backend);
283 
284 	log_backend_std_dropped(&log_output_rtt, cnt);
285 }
286 
process(const struct log_backend * const backend,union log_msg_generic * msg)287 static void process(const struct log_backend *const backend,
288 		union log_msg_generic *msg)
289 {
290 	uint32_t flags = log_backend_std_get_flags();
291 
292 	log_format_func_t log_output_func = log_format_func_t_get(log_format_current);
293 
294 	log_output_func(&log_output_rtt, &msg->log, flags);
295 }
296 
format_set(const struct log_backend * const backend,uint32_t log_type)297 static int format_set(const struct log_backend *const backend, uint32_t log_type)
298 {
299 	log_format_current = log_type;
300 	return 0;
301 }
302 
303 const struct log_backend_api log_backend_rtt_api = {
304 	.process = process,
305 	.panic = panic,
306 	.init = log_backend_rtt_init,
307 	.dropped = IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE) ? NULL : dropped,
308 	.format_set = format_set,
309 };
310 
311 LOG_BACKEND_DEFINE(log_backend_rtt, log_backend_rtt_api, true);
312