1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_LOGGING_LOG_MULTIDOMAIN_HELPER_H_
8 #define ZEPHYR_INCLUDE_LOGGING_LOG_MULTIDOMAIN_HELPER_H_
9 
10 /**
11  * @brief Logger multidomain backend helpers
12  *
13  * This module aims to provide baseline for links and backends and simplify
14  * the implementation. It is not core part of logging in similar way as
15  * log_output module is just a helper for log message formatting. Links and
16  * backends can be implemented without this helper.
17  *
18  * @defgroup log_backend_multidomain Logger multidomain backend helpers
19  * @ingroup log_backend
20  * @{
21  */
22 
23 /**
24  * @name Multidomain message IDs
25  * @anchor LOG_MULTIDOMAIN_HELPER_MESSAGE_IDS
26  * @{
27  */
28 
29 /** @brief Logging message ID. */
30 #define Z_LOG_MULTIDOMAIN_ID_MSG 0
31 
32 /** @brief Domain count request ID. */
33 #define Z_LOG_MULTIDOMAIN_ID_GET_DOMAIN_CNT 1
34 
35 /** @brief Source count request ID. */
36 #define Z_LOG_MULTIDOMAIN_ID_GET_SOURCE_CNT 2
37 
38 /** @brief Domain name request ID. */
39 #define Z_LOG_MULTIDOMAIN_ID_GET_DOMAIN_NAME 3
40 
41 /** @brief Source name request ID. */
42 #define Z_LOG_MULTIDOMAIN_ID_GET_SOURCE_NAME 4
43 
44 /** @brief Compile time and run-time levels request ID. */
45 #define Z_LOG_MULTIDOMAIN_ID_GET_LEVELS 5
46 
47 /** @brief Setting run-time level ID. */
48 #define Z_LOG_MULTIDOMAIN_ID_SET_RUNTIME_LEVEL 6
49 
50 /** @brief Get number of dropped message ID. */
51 #define Z_LOG_MULTIDOMAIN_ID_DROPPED 7
52 
53 /** @brief Link-backend readiness indication ID/ */
54 #define Z_LOG_MULTIDOMAIN_ID_READY 8
55 
56 /**@} */
57 
58 /**
59  * @name Multidomain status flags
60  * @anchor LOG_MULTIDOMAIN_STATUS
61  * @{
62  */
63 
64 /** @brief OK. */
65 #define Z_LOG_MULTIDOMAIN_STATUS_OK 0
66 /** @brief Error. */
67 #define Z_LOG_MULTIDOMAIN_STATUS_ERR 1
68 
69 /**@} */
70 
71 /** @brief Content of the logging message. */
72 struct log_multidomain_log_msg {
73 	uint8_t data[0];
74 } __packed;
75 
76 /** @brief Content of the domain count message. */
77 struct log_multidomain_domain_cnt {
78 	uint16_t count;
79 } __packed;
80 
81 /** @brief Content of the source count message. */
82 struct log_multidomain_source_cnt {
83 	uint8_t domain_id;
84 	uint16_t count;
85 } __packed;
86 
87 /** @brief Content of the domain name message. */
88 struct log_multidomain_domain_name {
89 	uint8_t domain_id;
90 	char name[0];
91 } __packed;
92 
93 /** @brief Content of the source name message. */
94 struct log_multidomain_source_name {
95 	uint8_t domain_id;
96 	uint16_t source_id;
97 	char name[0];
98 } __packed;
99 
100 /** @brief Content of the message for getting logging levels. */
101 struct log_multidomain_levels {
102 	uint8_t domain_id;
103 	uint16_t source_id;
104 	uint8_t level;
105 	uint8_t runtime_level;
106 } __packed;
107 
108 /** @brief Content of the message for setting logging level. */
109 struct log_multidomain_set_runtime_level {
110 	uint8_t domain_id;
111 	uint16_t source_id;
112 	uint8_t runtime_level;
113 } __packed;
114 
115 /** @brief Content of the message for getting amount of dropped messages. */
116 struct log_multidomain_dropped {
117 	uint32_t dropped;
118 } __packed;
119 
120 /** @brief Union with all message types. */
121 union log_multidomain_msg_data {
122 	struct log_multidomain_log_msg log_msg;
123 	struct log_multidomain_domain_cnt domain_cnt;
124 	struct log_multidomain_source_cnt source_cnt;
125 	struct log_multidomain_domain_name domain_name;
126 	struct log_multidomain_source_name source_name;
127 	struct log_multidomain_levels levels;
128 	struct log_multidomain_set_runtime_level set_rt_level;
129 	struct log_multidomain_dropped dropped;
130 };
131 
132 /** @brief Message. */
133 struct log_multidomain_msg {
134 	uint8_t id;
135 	uint8_t status;
136 	union log_multidomain_msg_data data;
137 } __packed;
138 
139 /** @brief Forward declaration. */
140 struct log_multidomain_link;
141 
142 /** @brief Structure with link transport API. */
143 struct log_multidomain_link_transport_api {
144 	int (*init)(struct log_multidomain_link *link);
145 	int (*send)(struct log_multidomain_link *link, void *data, size_t len);
146 };
147 
148 /** @brief Union for holding data returned by associated remote backend. */
149 union log_multidomain_link_dst {
150 	uint16_t count;
151 
152 	struct {
153 		char *dst;
154 		size_t *len;
155 	} name;
156 
157 	struct {
158 		uint8_t level;
159 		uint8_t runtime_level;
160 	} levels;
161 
162 	struct {
163 		uint8_t level;
164 	} set_runtime_level;
165 };
166 
167 /** @brief Remote link API. */
168 extern struct log_link_api log_multidomain_link_api;
169 
170 /** @brief Remote link structure. */
171 struct log_multidomain_link {
172 	const struct log_multidomain_link_transport_api *transport_api;
173 	struct k_sem rdy_sem;
174 	const struct log_link *link;
175 	union log_multidomain_link_dst dst;
176 	int status;
177 	bool ready;
178 };
179 
180 /** @brief Forward declaration. */
181 struct log_multidomain_backend;
182 
183 /** @brief Backend transport API. */
184 struct log_multidomain_backend_transport_api {
185 	int (*init)(struct log_multidomain_backend *backend);
186 	int (*send)(struct log_multidomain_backend *backend, void *data, size_t len);
187 };
188 
189 /** @brief Remote backend API. */
190 extern const struct log_backend_api log_multidomain_backend_api;
191 
192 /** @brief Remote backend structure. */
193 struct log_multidomain_backend {
194 	const struct log_multidomain_backend_transport_api *transport_api;
195 	const struct log_backend *log_backend;
196 	struct k_sem rdy_sem;
197 	bool panic;
198 	int status;
199 	bool ready;
200 };
201 
202 /** @brief Function to be called when data is received from remote.
203  *
204  * @param link Link instance.
205  * @param data Data.
206  * @param len Data length.
207  */
208 void log_multidomain_link_on_recv_cb(struct log_multidomain_link *link,
209 				     const void *data, size_t len);
210 
211 /** @brief Function called on error reported by transport layer.
212  *
213  * @param link Link instance.
214  * @param err Error code.
215  */
216 void log_multidomain_link_on_error(struct log_multidomain_link *link, int err);
217 
218 /** @brief Function called when connection with remote is established.
219  *
220  * @param link Link instance.
221  * @param err Error code.
222  */
223 void log_multidomain_link_on_started(struct log_multidomain_link *link, int err);
224 
225 /** @brief Function to be called when data is received from remote.
226  *
227  * @param backend Backend instance.
228  * @param data Data.
229  * @param len Data length.
230  */
231 void log_multidomain_backend_on_recv_cb(struct log_multidomain_backend *backend,
232 					const void *data, size_t len);
233 
234 /** @brief Function called on error reported by transport layer.
235  *
236  * @param backend Backend instance.
237  * @param err Error code.
238  */
239 void log_multidomain_backend_on_error(struct log_multidomain_backend *backend, int err);
240 
241 /** @brief Function called when connection with remote is established.
242  *
243  * @param backend Backend instance.
244  * @param err Error code.
245  */
246 void log_multidomain_backend_on_started(struct log_multidomain_backend *backend, int err);
247 
248 /** @} */
249 
250 #endif /* ZEPHYR_INCLUDE_LOGGING_LOG_MULTIDOMAIN_HELPER_H_ */
251