1 /*
2  * Copyright (c) 2020 Nordic Semiconductor
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <zephyr.h>
7 #include <syscall_handler.h>
8 #include <logging/log_msg2.h>
9 #include <logging/log_core.h>
10 #include <logging/log_ctrl.h>
11 
z_log_msg2_finalize(struct log_msg2 * msg,const void * source,const struct log_msg2_desc desc,const void * data)12 void z_log_msg2_finalize(struct log_msg2 *msg, const void *source,
13 			 const struct log_msg2_desc desc, const void *data)
14 {
15 	if (!msg) {
16 		z_log_dropped(false);
17 
18 		return;
19 	}
20 
21 	if (data) {
22 		uint8_t *d = msg->data + desc.package_len;
23 
24 		memcpy(d, data, desc.data_len);
25 	}
26 
27 	msg->hdr.desc = desc;
28 	msg->hdr.source = source;
29 	z_log_msg2_commit(msg);
30 }
31 
z_impl_z_log_msg2_static_create(const void * source,const struct log_msg2_desc desc,uint8_t * package,const void * data)32 void z_impl_z_log_msg2_static_create(const void *source,
33 			      const struct log_msg2_desc desc,
34 			      uint8_t *package, const void *data)
35 {
36 	uint32_t msg_wlen = log_msg2_get_total_wlen(desc);
37 	struct log_msg2 *msg = z_log_msg2_alloc(msg_wlen);
38 
39 	if (msg) {
40 		memcpy(msg->data, package, desc.package_len);
41 	}
42 
43 	z_log_msg2_finalize(msg, source, desc, data);
44 }
45 
46 #ifdef CONFIG_USERSPACE
z_vrfy_z_log_msg2_static_create(const void * source,const struct log_msg2_desc desc,uint8_t * package,const void * data)47 static inline void z_vrfy_z_log_msg2_static_create(const void *source,
48 			      const struct log_msg2_desc desc,
49 			      uint8_t *package, const void *data)
50 {
51 	return z_impl_z_log_msg2_static_create(source, desc, package, data);
52 }
53 #include <syscalls/z_log_msg2_static_create_mrsh.c>
54 #endif
55 
z_impl_z_log_msg2_runtime_vcreate(uint8_t domain_id,const void * source,uint8_t level,const void * data,size_t dlen,const char * fmt,va_list ap)56 void z_impl_z_log_msg2_runtime_vcreate(uint8_t domain_id, const void *source,
57 				uint8_t level, const void *data, size_t dlen,
58 				const char *fmt, va_list ap)
59 {
60 	int plen;
61 
62 	if (fmt) {
63 		va_list ap2;
64 
65 		va_copy(ap2, ap);
66 		plen = cbvprintf_package(NULL, Z_LOG_MSG2_ALIGN_OFFSET, 0,
67 					 fmt, ap2);
68 		__ASSERT_NO_MSG(plen >= 0);
69 		va_end(ap2);
70 	} else {
71 		plen = 0;
72 	}
73 
74 	size_t msg_wlen = Z_LOG_MSG2_ALIGNED_WLEN(plen, dlen);
75 	struct log_msg2 *msg;
76 	struct log_msg2_desc desc =
77 		Z_LOG_MSG_DESC_INITIALIZER(domain_id, level, plen, dlen);
78 
79 	if (IS_ENABLED(CONFIG_LOG2_MODE_IMMEDIATE)) {
80 		msg = alloca(msg_wlen * sizeof(int));
81 	} else {
82 		msg = z_log_msg2_alloc(msg_wlen);
83 	}
84 
85 	if (msg && fmt) {
86 		plen = cbvprintf_package(msg->data, (size_t)plen, 0, fmt, ap);
87 		__ASSERT_NO_MSG(plen >= 0);
88 	}
89 
90 	z_log_msg2_finalize(msg, source, desc, data);
91 }
92 
93 #ifdef CONFIG_USERSPACE
z_vrfy_z_log_msg2_runtime_vcreate(uint8_t domain_id,const void * source,uint8_t level,const void * data,size_t dlen,const char * fmt,va_list ap)94 static inline void z_vrfy_z_log_msg2_runtime_vcreate(uint8_t domain_id,
95 				const void *source,
96 				uint8_t level, const void *data, size_t dlen,
97 				const char *fmt, va_list ap)
98 {
99 	return z_impl_z_log_msg2_runtime_vcreate(domain_id, source, level, data,
100 						dlen, fmt, ap);
101 }
102 #include <syscalls/z_log_msg2_runtime_vcreate_mrsh.c>
103 #endif
104