1 /*
2  * Copyright (c) 2021, Carlo Caione <ccaione@baylibre.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Simple backend that adds an offset (defined into the DT) to whatever it is
7  * passed in input as IPC message.
8  */
9 
10 #include <zephyr/ipc/ipc_service_backend.h>
11 
12 #include <zephyr/logging/log.h>
13 #include <zephyr/sys/util_macro.h>
14 #include <zephyr/kernel.h>
15 #include <zephyr/device.h>
16 
17 #define DT_DRV_COMPAT		ipc_service_backend
18 
19 struct backend_data_t {
20 	const struct ipc_ept_cfg *cfg;
21 };
22 
23 struct backend_config_t {
24 	unsigned int offset;
25 };
26 
send(const struct device * instance,void * token,const void * p_data,size_t len)27 static int send(const struct device *instance, void *token,
28 		const void *p_data, size_t len)
29 {
30 	const struct backend_config_t *config;
31 	struct backend_data_t *data;
32 	uint8_t *msg;
33 
34 	config = instance->config;
35 	data = instance->data;
36 	msg = (uint8_t *) p_data;
37 
38 	*msg += config->offset;
39 
40 	data->cfg->cb.received(msg, sizeof(*msg), data->cfg->priv);
41 
42 	return 0;
43 }
44 
register_ept(const struct device * instance,void ** token,const struct ipc_ept_cfg * cfg)45 static int register_ept(const struct device *instance,
46 			void **token,
47 			const struct ipc_ept_cfg *cfg)
48 {
49 	struct backend_data_t *data = instance->data;
50 
51 	data->cfg = cfg;
52 
53 	return 0;
54 }
55 
deregister_ept(const struct device * instance,void * token)56 static int deregister_ept(const struct device *instance, void *token)
57 {
58 	struct backend_data_t *data = instance->data;
59 
60 	data->cfg = NULL;
61 
62 	return 0;
63 }
64 
65 const static struct ipc_service_backend backend_ops = {
66 	.send = send,
67 	.register_endpoint = register_ept,
68 	.deregister_endpoint = deregister_ept,
69 };
70 
71 #define DEFINE_BACKEND_DEVICE(i)					\
72 	static struct backend_config_t backend_config_##i = {		\
73 		.offset = DT_INST_PROP(i, offset),			\
74 	};								\
75 									\
76 	static struct backend_data_t backend_data_##i;			\
77 									\
78 	DEVICE_DT_INST_DEFINE(i,					\
79 			 NULL,						\
80 			 NULL,						\
81 			 &backend_data_##i,				\
82 			 &backend_config_##i,				\
83 			 POST_KERNEL,					\
84 			 CONFIG_IPC_SERVICE_REG_BACKEND_PRIORITY,	\
85 			 &backend_ops);
86 
87 DT_INST_FOREACH_STATUS_OKAY(DEFINE_BACKEND_DEVICE)
88