1 /*
2  * Copyright (c) 2018 omSquare s.r.o.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <logging/log_backend.h>
8 #include <logging/log_core.h>
9 #include <logging/log_msg.h>
10 #include <logging/log_output.h>
11 #include <logging/log_backend_std.h>
12 #include <SEGGER_RTT.h>
13 
14 #ifndef CONFIG_LOG_BACKEND_RTT_BUFFER_SIZE
15 #define CONFIG_LOG_BACKEND_RTT_BUFFER_SIZE 0
16 #endif
17 
18 #ifndef CONFIG_LOG_BACKEND_RTT_MESSAGE_SIZE
19 #define CONFIG_LOG_BACKEND_RTT_MESSAGE_SIZE 0
20 #endif
21 
22 #ifndef CONFIG_LOG_BACKEND_RTT_OUTPUT_BUFFER_SIZE
23 #define CONFIG_LOG_BACKEND_RTT_OUTPUT_BUFFER_SIZE 0
24 #endif
25 
26 #ifndef CONFIG_LOG_BACKEND_RTT_RETRY_DELAY_MS
27 /* Long enough to detect host presence */
28 #define CONFIG_LOG_BACKEND_RTT_RETRY_DELAY_MS 10
29 #endif
30 
31 #ifndef CONFIG_LOG_BACKEND_RTT_RETRY_CNT
32 /* Big enough to detect host presence */
33 #define CONFIG_LOG_BACKEND_RTT_RETRY_CNT 10
34 #endif
35 
36 #define DROP_MAX 99
37 
38 #define DROP_MSG "messages dropped:    \r\n"
39 
40 #define DROP_MSG_LEN (sizeof(DROP_MSG) - 1)
41 
42 #define MESSAGE_SIZE CONFIG_LOG_BACKEND_RTT_MESSAGE_SIZE
43 
44 #define CHAR_BUF_SIZE \
45 	((IS_ENABLED(CONFIG_LOG_BACKEND_RTT_MODE_BLOCK) && \
46 	 !IS_ENABLED(CONFIG_LOG_IMMEDIATE)) ? \
47 		CONFIG_LOG_BACKEND_RTT_OUTPUT_BUFFER_SIZE : 1)
48 
49 #define RTT_LOCK() \
50 	COND_CODE_0(CONFIG_LOG_BACKEND_RTT_BUFFER, (SEGGER_RTT_LOCK()), ())
51 
52 #define RTT_UNLOCK() \
53 	COND_CODE_0(CONFIG_LOG_BACKEND_RTT_BUFFER, (SEGGER_RTT_UNLOCK()), ())
54 
55 #define RTT_BUFFER_SIZE \
56 	COND_CODE_0(CONFIG_LOG_BACKEND_RTT_BUFFER, \
57 		(0), (CONFIG_LOG_BACKEND_RTT_BUFFER_SIZE))
58 
59 
60 static const char *drop_msg = DROP_MSG;
61 static uint8_t rtt_buf[RTT_BUFFER_SIZE];
62 static uint8_t line_buf[MESSAGE_SIZE + DROP_MSG_LEN];
63 static uint8_t *line_pos;
64 static uint8_t char_buf[CHAR_BUF_SIZE];
65 static int drop_cnt;
66 static int drop_warn;
67 static bool panic_mode;
68 static bool host_present;
69 
70 static int data_out_block_mode(uint8_t *data, size_t length, void *ctx);
71 static int data_out_drop_mode(uint8_t *data, size_t length, void *ctx);
72 
73 static int char_out_drop_mode(uint8_t data);
74 static int line_out_drop_mode(void);
75 
is_sync_mode(void)76 static inline bool is_sync_mode(void)
77 {
78 	return IS_ENABLED(CONFIG_LOG_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 
234 LOG_OUTPUT_DEFINE(log_output_rtt,
235 		  IS_ENABLED(CONFIG_LOG_BACKEND_RTT_MODE_BLOCK) ?
236 			  data_out_block_mode : data_out_drop_mode,
237 		  char_buf, sizeof(char_buf));
238 
put(const struct log_backend * const backend,struct log_msg * msg)239 static void put(const struct log_backend *const backend,
240 		struct log_msg *msg)
241 {
242 	uint32_t flag = IS_ENABLED(CONFIG_LOG_BACKEND_RTT_SYST_ENABLE) ?
243 		LOG_OUTPUT_FLAG_FORMAT_SYST : 0;
244 
245 	log_backend_std_put(&log_output_rtt, flag, msg);
246 }
247 
log_backend_rtt_cfg(void)248 static void log_backend_rtt_cfg(void)
249 {
250 	SEGGER_RTT_ConfigUpBuffer(CONFIG_LOG_BACKEND_RTT_BUFFER, "Logger",
251 				  rtt_buf, sizeof(rtt_buf),
252 				  SEGGER_RTT_MODE_NO_BLOCK_SKIP);
253 }
254 
log_backend_rtt_init(struct log_backend const * const backend)255 static void log_backend_rtt_init(struct log_backend const *const backend)
256 {
257 	if (CONFIG_LOG_BACKEND_RTT_BUFFER > 0) {
258 		log_backend_rtt_cfg();
259 	}
260 
261 	host_present = true;
262 	line_pos = line_buf;
263 }
264 
panic(struct log_backend const * const backend)265 static void panic(struct log_backend const *const backend)
266 {
267 	panic_mode = true;
268 	log_backend_std_panic(&log_output_rtt);
269 }
270 
dropped(const struct log_backend * const backend,uint32_t cnt)271 static void dropped(const struct log_backend *const backend, uint32_t cnt)
272 {
273 	ARG_UNUSED(backend);
274 
275 	log_backend_std_dropped(&log_output_rtt, cnt);
276 }
277 
sync_string(const struct log_backend * const backend,struct log_msg_ids src_level,uint32_t timestamp,const char * fmt,va_list ap)278 static void sync_string(const struct log_backend *const backend,
279 		     struct log_msg_ids src_level, uint32_t timestamp,
280 		     const char *fmt, va_list ap)
281 {
282 	uint32_t flag = IS_ENABLED(CONFIG_LOG_BACKEND_RTT_SYST_ENABLE) ?
283 		LOG_OUTPUT_FLAG_FORMAT_SYST : 0;
284 
285 	log_backend_std_sync_string(&log_output_rtt, flag, src_level,
286 				    timestamp, fmt, ap);
287 }
288 
sync_hexdump(const struct log_backend * const backend,struct log_msg_ids src_level,uint32_t timestamp,const char * metadata,const uint8_t * data,uint32_t length)289 static void sync_hexdump(const struct log_backend *const backend,
290 			 struct log_msg_ids src_level, uint32_t timestamp,
291 			 const char *metadata, const uint8_t *data, uint32_t length)
292 {
293 	uint32_t flag = IS_ENABLED(CONFIG_LOG_BACKEND_RTT_SYST_ENABLE) ?
294 		LOG_OUTPUT_FLAG_FORMAT_SYST : 0;
295 
296 	log_backend_std_sync_hexdump(&log_output_rtt, flag, src_level,
297 				     timestamp, metadata, data, length);
298 }
299 
process(const struct log_backend * const backend,union log_msg2_generic * msg)300 static void process(const struct log_backend *const backend,
301 		union log_msg2_generic *msg)
302 {
303 	uint32_t flags = log_backend_std_get_flags();
304 
305 	log_output_msg2_process(&log_output_rtt, &msg->log, flags);
306 }
307 
308 const struct log_backend_api log_backend_rtt_api = {
309 	.process = IS_ENABLED(CONFIG_LOG2) ? process : NULL,
310 	.put = IS_ENABLED(CONFIG_LOG_MODE_DEFERRED) ? put : NULL,
311 	.put_sync_string = IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE) ?
312 			sync_string : NULL,
313 	.put_sync_hexdump = IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE) ?
314 			sync_hexdump : NULL,
315 	.panic = panic,
316 	.init = log_backend_rtt_init,
317 	.dropped = IS_ENABLED(CONFIG_LOG_IMMEDIATE) ? NULL : dropped,
318 };
319 
320 LOG_BACKEND_DEFINE(log_backend_rtt, log_backend_rtt_api, true);
321