Zephyr API Documentation 4.4.0-rc2
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
zbus_proxy_agent.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_ZBUS_PROXY_AGENT_H_
8#define ZEPHYR_INCLUDE_ZBUS_PROXY_AGENT_H_
9
10#include <zephyr/kernel.h>
12#include <zephyr/zbus/zbus.h>
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
25
26/* Forward declarations */
28struct zbus_proxy_agent;
29
37
43 const struct zbus_channel *chan;
46};
47
58 uint8_t message[CONFIG_ZBUS_PROXY_AGENT_MAX_MESSAGE_SIZE];
62 char channel_name[CONFIG_ZBUS_PROXY_AGENT_MAX_CHANNEL_NAME_SIZE];
63};
64
70 const struct zbus_channel *chan;
72 uint8_t message[CONFIG_ZBUS_PROXY_AGENT_MAX_MESSAGE_SIZE];
73};
74
96
108typedef int (*zbus_proxy_agent_recv_cb_t)(const struct zbus_proxy_agent *agent,
109 const struct zbus_proxy_msg *msg);
110
116 int (*backend_init)(const struct zbus_proxy_agent *agent);
118 int (*backend_send)(const struct zbus_proxy_agent *agent, struct zbus_proxy_msg *msg);
120 int (*backend_set_recv_cb)(const struct zbus_proxy_agent *agent,
122};
123
134 const struct zbus_proxy_agent *agent);
135
148
160#define ZBUS_PROXY_AGENT_DEFINE(_name, _type, _backend_dt_node) \
161 _ZBUS_PROXY_AGENT_BACKEND_CONFIG_DEFINE(_name, _type, _backend_dt_node) \
162 K_THREAD_STACK_DEFINE(_name##_thread_stack, \
163 CONFIG_ZBUS_PROXY_AGENT_WORK_QUEUE_STACK_SIZE); \
164 K_MSGQ_DEFINE(_name##_rx_msgq, sizeof(struct zbus_proxy_agent_rx_msg), \
165 CONFIG_ZBUS_PROXY_AGENT_RX_QUEUE_DEPTH, 4); \
166 static struct k_thread _name##_thread; \
167 static k_tid_t _name##_thread_id; \
168 const struct zbus_proxy_agent _name = { \
169 .name = #_name, \
170 .backend_config = _ZBUS_PROXY_AGENT_GET_BACKEND_CONFIG(_name, _type), \
171 .backend_api = _ZBUS_PROXY_AGENT_GET_BACKEND_API(_type), \
172 .thread = &_name##_thread, \
173 .msgq = &_name##_rx_msgq, \
174 .stack = _name##_thread_stack, \
175 .thread_id = &_name##_thread_id, \
176 }; \
177 static void _name##_zbus_listener_cb(const struct zbus_channel *chan) \
178 { \
179 zbus_proxy_agent_listener_cb(chan, &_name); \
180 } \
181 ZBUS_LISTENER_DEFINE(_name##_listener, _name##_zbus_listener_cb); \
182 _ZBUS_PROXY_AGENT_GENERATE_SHADOW_VALIDATOR(_name); \
183 static int _name##_init(void) \
184 { \
185 return zbus_init_proxy_agent(&_name); \
186 } \
187 SYS_INIT(_name##_init, APPLICATION, CONFIG_ZBUS_PROXY_AGENT_INIT_PRIORITY)
188
190
197#define _ZBUS_PROXY_AGENT_BACKEND_CONFIG_DEFINE(_name, _type, _backend_dt_node) \
198 _ZBUS_PROXY_AGENT_BACKEND_CONFIG_DEFINE_##_type(_name, _backend_dt_node)
199
206#define _ZBUS_PROXY_AGENT_GET_BACKEND_CONFIG(_name, _type) \
207 _ZBUS_PROXY_AGENT_GET_BACKEND_CONFIG_##_type(_name)
208
214#define _ZBUS_PROXY_AGENT_GET_BACKEND_API(_type) _ZBUS_PROXY_AGENT_GET_BACKEND_API_##_type
215
222void zbus_proxy_agent_log_shadow_pub_denied(const struct zbus_proxy_agent *agent);
223
230#define _ZBUS_PROXY_AGENT_VALIDATOR(_proxy_name) _proxy_name##_shadow_validator
231
240#define _ZBUS_PROXY_AGENT_GENERATE_SHADOW_VALIDATOR(_proxy_name) \
241 bool _proxy_name##_shadow_validator(const void *msg, size_t msg_size) \
242 { \
243 (void)msg; \
244 (void)msg_size; \
245 /* Only allow publishing from the proxy agent's dedicated thread */ \
246 if (k_current_get() != *_proxy_name.thread_id) { \
247 zbus_proxy_agent_log_shadow_pub_denied(&_proxy_name); \
248 return false; \
249 } \
250 return true; \
251 }
252
254
267#define ZBUS_PROXY_ADD_CHAN(_agent, _chan) \
268 ZBUS_CHAN_ADD_OBS(_chan, _agent##_listener, zz)
269
271
278#define _ZBUS_SHADOW_CHAN_REGISTER(_name, _proxy_name) \
279 extern const struct zbus_proxy_agent _proxy_name; \
280 static const STRUCT_SECTION_ITERABLE(zbus_shadow_channel, \
281 _CONCAT(_zbus_shadow_chan_, _name)) = { \
282 .chan = &_name, \
283 .proxy_agent = &_proxy_name, \
284 }
285
287
301#define ZBUS_SHADOW_CHAN_DEFINE(_name, _type, _proxy_name, _user_data, _observers, _init_val) \
302 ZBUS_CHAN_DEFINE(_name, _type, _ZBUS_PROXY_AGENT_VALIDATOR(_proxy_name), _user_data, \
303 _observers, _init_val); \
304 _ZBUS_SHADOW_CHAN_REGISTER(_name, _proxy_name)
305
320#define ZBUS_SHADOW_CHAN_DEFINE_WITH_ID(_name, _id, _type, _proxy_name, _user_data, _observers, \
321 _init_val) \
322 ZBUS_CHAN_DEFINE_WITH_ID(_name, _id, _type, _ZBUS_PROXY_AGENT_VALIDATOR(_proxy_name), \
323 _user_data, _observers, _init_val); \
324 _ZBUS_SHADOW_CHAN_REGISTER(_name, _proxy_name)
325
329
330#ifdef __cplusplus
331}
332#endif
333
334#endif /* ZEPHYR_INCLUDE_ZBUS_PROXY_AGENT_H_ */
struct z_thread_stack_element k_thread_stack_t
Typedef of struct z_thread_stack_element.
Definition arch_interface.h:46
int(* zbus_proxy_agent_recv_cb_t)(const struct zbus_proxy_agent *agent, const struct zbus_proxy_msg *msg)
Type definition for the proxy agent receive callback function.
Definition zbus_proxy_agent.h:108
zbus_proxy_agent_backend_type
Enum for supported proxy agent backend types.
Definition zbus_proxy_agent.h:33
int zbus_init_proxy_agent(const struct zbus_proxy_agent *agent)
Internal proxy agent receive callback.
void zbus_proxy_agent_listener_cb(const struct zbus_channel *chan, const struct zbus_proxy_agent *agent)
Internal proxy agent listener callback.
@ ZBUS_PROXY_AGENT_BACKEND_IPC
IPC backend for inter-process communication.
Definition zbus_proxy_agent.h:35
struct k_thread * k_tid_t
Definition thread.h:383
Public kernel APIs.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
Message Queue Structure.
Definition kernel.h:5168
Thread Structure.
Definition thread.h:259
Type used to represent a channel.
Definition zbus.h:84
Backend API structure for proxy agent backends.
Definition zbus_proxy_agent.h:114
int(* backend_send)(const struct zbus_proxy_agent *agent, struct zbus_proxy_msg *msg)
Send function for the backend.
Definition zbus_proxy_agent.h:118
int(* backend_set_recv_cb)(const struct zbus_proxy_agent *agent, zbus_proxy_agent_recv_cb_t recv_cb)
Function to set the receive callback for incoming messages.
Definition zbus_proxy_agent.h:120
int(* backend_init)(const struct zbus_proxy_agent *agent)
Initializer function for the backend.
Definition zbus_proxy_agent.h:116
Internal thread payload for validated received messages.
Definition zbus_proxy_agent.h:68
const struct zbus_channel * chan
Resolved shadow channel that will receive the message.
Definition zbus_proxy_agent.h:70
uint8_t message[CONFIG_ZBUS_PROXY_AGENT_MAX_MESSAGE_SIZE]
Binary message data.
Definition zbus_proxy_agent.h:72
Structure for proxy agent configuration.
Definition zbus_proxy_agent.h:80
k_thread_stack_t * stack
Stack for the dedicated proxy agent thread.
Definition zbus_proxy_agent.h:92
const struct zbus_proxy_agent_backend_api * backend_api
Backend API determined at compile-time based on backend type.
Definition zbus_proxy_agent.h:86
const void * backend_config
Backend specific configuration.
Definition zbus_proxy_agent.h:84
const char * name
The name of the proxy agent.
Definition zbus_proxy_agent.h:82
struct k_thread * thread
Dedicated thread used to process received proxy messages.
Definition zbus_proxy_agent.h:88
struct k_msgq * msgq
msgq for received proxy messages
Definition zbus_proxy_agent.h:90
k_tid_t *const thread_id
Pointer to the thread ID of the dedicated proxy agent thread.
Definition zbus_proxy_agent.h:94
Structure for proxy agent message.
Definition zbus_proxy_agent.h:54
uint32_t message_size
Size of the message data.
Definition zbus_proxy_agent.h:56
uint32_t channel_name_len
Length of the channel name including the NUL terminator.
Definition zbus_proxy_agent.h:60
char channel_name[CONFIG_ZBUS_PROXY_AGENT_MAX_CHANNEL_NAME_SIZE]
Channel name associated with the message.
Definition zbus_proxy_agent.h:62
uint8_t message[CONFIG_ZBUS_PROXY_AGENT_MAX_MESSAGE_SIZE]
Binary message data.
Definition zbus_proxy_agent.h:58
Metadata for shadow channels to track their ownership by proxy agents.
Definition zbus_proxy_agent.h:41
const struct zbus_proxy_agent * proxy_agent
Pointer to the proxy agent that owns this shadow channel.
Definition zbus_proxy_agent.h:45
const struct zbus_channel * chan
Pointer to the shadow channel.
Definition zbus_proxy_agent.h:43