1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <logging/log_backend.h>
8 #include <logging/log_backend_std.h>
9 #include <logging/log_output.h>
10 #include <openthread/platform/logging.h>
11 #include <utils/uart.h>
12 #include <platform-zephyr.h>
13 
14 #ifndef CONFIG_LOG_BACKEND_SPINEL_BUFFER_SIZE
15 #define CONFIG_LOG_BACKEND_SPINEL_BUFFER_SIZE 0
16 #endif
17 
18 static uint8_t char_buf[CONFIG_LOG_BACKEND_SPINEL_BUFFER_SIZE];
19 static bool panic_mode;
20 static uint16_t last_log_level;
21 
22 static int write(uint8_t *data, size_t length, void *ctx);
23 
24 LOG_OUTPUT_DEFINE(log_output_spinel, write, char_buf, sizeof(char_buf));
25 
is_panic_mode(void)26 static inline bool is_panic_mode(void)
27 {
28 	return panic_mode;
29 }
30 
put(const struct log_backend * const backend,struct log_msg * msg)31 static void put(const struct log_backend *const backend,
32 		struct log_msg *msg)
33 {
34 	/* prevent adding CRLF, which may crash spinel decoding */
35 	uint32_t flag = LOG_OUTPUT_FLAG_CRLF_NONE;
36 
37 	last_log_level = msg->hdr.ids.level;
38 
39 	log_backend_std_put(&log_output_spinel, flag, msg);
40 }
41 
process(const struct log_backend * const backend,union log_msg2_generic * msg)42 static void process(const struct log_backend *const backend,
43 		union log_msg2_generic *msg)
44 {
45 	/* prevent adding CRLF, which may crash spinel decoding */
46 	uint32_t flags = LOG_OUTPUT_FLAG_CRLF_NONE | log_backend_std_get_flags();
47 
48 	log_output_msg2_process(&log_output_spinel, &msg->log, flags);
49 }
50 
sync_string(const struct log_backend * const backend,struct log_msg_ids src_level,uint32_t timestamp,const char * fmt,va_list ap)51 static void sync_string(const struct log_backend *const backend,
52 			 struct log_msg_ids src_level, uint32_t timestamp,
53 			 const char *fmt, va_list ap)
54 {
55 	/* prevent adding CRLF, which may crash spinel decoding */
56 	uint32_t flag = LOG_OUTPUT_FLAG_CRLF_NONE;
57 
58 	log_backend_std_sync_string(&log_output_spinel, flag, src_level,
59 				    timestamp, fmt, ap);
60 }
61 
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)62 static void sync_hexdump(const struct log_backend *const backend,
63 			 struct log_msg_ids src_level, uint32_t timestamp,
64 			 const char *metadata, const uint8_t *data,
65 			 uint32_t length)
66 {
67 	/* prevent adding CRLF, which may crash spinel decoding */
68 	uint32_t flag = LOG_OUTPUT_FLAG_CRLF_NONE;
69 
70 	log_backend_std_sync_hexdump(&log_output_spinel, flag, src_level,
71 				     timestamp, metadata, data, length);
72 }
73 
log_backend_spinel_init(struct log_backend const * const backend)74 static void log_backend_spinel_init(struct log_backend const *const backend)
75 {
76 	memset(char_buf, '\0', sizeof(char_buf));
77 }
78 
panic(struct log_backend const * const backend)79 static void panic(struct log_backend const *const backend)
80 {
81 	ARG_UNUSED(backend);
82 	panic_mode = true;
83 }
84 
dropped(const struct log_backend * const backend,uint32_t cnt)85 static void dropped(const struct log_backend *const backend, uint32_t cnt)
86 {
87 	ARG_UNUSED(backend);
88 
89 	log_backend_std_dropped(&log_output_spinel, cnt);
90 }
91 
write(uint8_t * data,size_t length,void * ctx)92 static int write(uint8_t *data, size_t length, void *ctx)
93 {
94 	otLogLevel log_level;
95 
96 	if (is_panic_mode()) {
97 		/* In panic mode otPlatLog implemented for Spinel protocol
98 		 * cannot be used, because it cannot be called from interrupt.
99 		 * In such situation raw data bytes without encoding are send.
100 		 */
101 		platformUartPanic();
102 		otPlatUartSend(data, length);
103 	} else {
104 		switch (last_log_level) {
105 		case LOG_LEVEL_ERR:
106 			log_level = OT_LOG_LEVEL_CRIT;
107 			break;
108 		case LOG_LEVEL_WRN:
109 			log_level = OT_LOG_LEVEL_WARN;
110 			break;
111 		case LOG_LEVEL_INF:
112 			log_level = OT_LOG_LEVEL_INFO;
113 			break;
114 		case LOG_LEVEL_DBG:
115 			log_level = OT_LOG_LEVEL_DEBG;
116 			break;
117 		default:
118 			log_level = OT_LOG_LEVEL_NONE;
119 			break;
120 		}
121 		otPlatLog(log_level, OT_LOG_REGION_PLATFORM, "%s", data);
122 	}
123 
124 	/* make sure that buffer will be clean in next attempt */
125 	memset(char_buf, '\0', length);
126 	return length;
127 }
128 
129 const struct log_backend_api log_backend_spinel_api = {
130 	.process = IS_ENABLED(CONFIG_LOG2) ? process : NULL,
131 	.put = IS_ENABLED(CONFIG_LOG_MODE_DEFERRED) ? put : NULL,
132 	.put_sync_string = IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE) ?
133 			sync_string : NULL,
134 	.put_sync_hexdump = IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE) ?
135 			sync_hexdump : NULL,
136 	.panic = panic,
137 	.init = log_backend_spinel_init,
138 	.dropped = IS_ENABLED(CONFIG_LOG_IMMEDIATE) ? NULL : dropped,
139 };
140 
141 LOG_BACKEND_DEFINE(log_backend_spinel, log_backend_spinel_api, true);
142