Zephyr API Documentation 4.3.99
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
98
110typedef int (*zbus_proxy_agent_recv_cb_t)(const struct zbus_proxy_agent *agent,
111 const struct zbus_proxy_msg *msg);
112
118 int (*backend_init)(const struct zbus_proxy_agent *agent);
120 int (*backend_send)(const struct zbus_proxy_agent *agent, struct zbus_proxy_msg *msg);
122 int (*backend_set_recv_cb)(const struct zbus_proxy_agent *agent,
124};
125
135
143#define ZBUS_PROXY_AGENT_BACKEND_DEFINE(_name, _type, _api) \
144 STRUCT_SECTION_ITERABLE(zbus_proxy_agent_backend_desc, _name) = { \
145 .type = (_type), \
146 .api = (_api), \
147 }
148
159 const struct zbus_proxy_agent *agent);
160
173
185#define ZBUS_PROXY_AGENT_DEFINE(_name, _type, _backend_dt_node) \
186 _ZBUS_PROXY_AGENT_BACKEND_CONFIG_DEFINE(_name, _type, _backend_dt_node) \
187 K_THREAD_STACK_DEFINE(_name##_thread_stack, \
188 CONFIG_ZBUS_PROXY_AGENT_WORK_QUEUE_STACK_SIZE); \
189 K_MSGQ_DEFINE(_name##_rx_msgq, sizeof(struct zbus_proxy_agent_rx_msg), \
190 CONFIG_ZBUS_PROXY_AGENT_RX_QUEUE_DEPTH, 4); \
191 static struct k_thread _name##_thread; \
192 static k_tid_t _name##_thread_id; \
193 const struct zbus_proxy_agent _name = { \
194 .name = #_name, \
195 .type = _type, \
196 .backend_config = _ZBUS_PROXY_AGENT_GET_BACKEND_CONFIG(_name, _type), \
197 .backend_api = _ZBUS_PROXY_AGENT_GET_BACKEND_API(_type), \
198 .thread = &_name##_thread, \
199 .msgq = &_name##_rx_msgq, \
200 .stack = _name##_thread_stack, \
201 .thread_id = &_name##_thread_id, \
202 }; \
203 static void _name##_zbus_listener_cb(const struct zbus_channel *chan) \
204 { \
205 zbus_proxy_agent_listener_cb(chan, &_name); \
206 } \
207 ZBUS_LISTENER_DEFINE(_name##_listener, _name##_zbus_listener_cb); \
208 _ZBUS_PROXY_AGENT_GENERATE_SHADOW_VALIDATOR(_name); \
209 static int _name##_init(void) \
210 { \
211 return zbus_init_proxy_agent(&_name); \
212 } \
213 SYS_INIT(_name##_init, APPLICATION, CONFIG_ZBUS_PROXY_AGENT_INIT_PRIORITY)
214
216
223#define _ZBUS_PROXY_AGENT_BACKEND_CONFIG_DEFINE(_name, _type, _backend_dt_node) \
224 _ZBUS_PROXY_AGENT_BACKEND_CONFIG_DEFINE_##_type(_name, _backend_dt_node)
225
232#define _ZBUS_PROXY_AGENT_GET_BACKEND_CONFIG(_name, _type) \
233 _ZBUS_PROXY_AGENT_GET_BACKEND_CONFIG_##_type(_name)
234
240#define _ZBUS_PROXY_AGENT_GET_BACKEND_API(_type) _ZBUS_PROXY_AGENT_GET_BACKEND_API_##_type
241
248void zbus_proxy_agent_log_shadow_pub_denied(const struct zbus_proxy_agent *agent);
249
256#define _ZBUS_PROXY_AGENT_VALIDATOR(_proxy_name) _proxy_name##_shadow_validator
257
266#define _ZBUS_PROXY_AGENT_GENERATE_SHADOW_VALIDATOR(_proxy_name) \
267 bool _proxy_name##_shadow_validator(const void *msg, size_t msg_size) \
268 { \
269 (void)msg; \
270 (void)msg_size; \
271 /* Only allow publishing from the proxy agent's dedicated thread */ \
272 if (k_current_get() != *_proxy_name.thread_id) { \
273 zbus_proxy_agent_log_shadow_pub_denied(&_proxy_name); \
274 return false; \
275 } \
276 return true; \
277 }
278
280
293#define ZBUS_PROXY_ADD_CHAN(_agent, _chan) \
294 ZBUS_CHAN_ADD_OBS(_chan, _agent##_listener, zz)
295
297
304#define _ZBUS_SHADOW_CHAN_REGISTER(_name, _proxy_name) \
305 extern const struct zbus_proxy_agent _proxy_name; \
306 static const STRUCT_SECTION_ITERABLE(zbus_shadow_channel, \
307 _CONCAT(_zbus_shadow_chan_, _name)) = { \
308 .chan = &_name, \
309 .proxy_agent = &_proxy_name, \
310 }
311
313
327#define ZBUS_SHADOW_CHAN_DEFINE(_name, _type, _proxy_name, _user_data, _observers, _init_val) \
328 ZBUS_CHAN_DEFINE(_name, _type, _ZBUS_PROXY_AGENT_VALIDATOR(_proxy_name), _user_data, \
329 _observers, _init_val); \
330 _ZBUS_SHADOW_CHAN_REGISTER(_name, _proxy_name)
331
346#define ZBUS_SHADOW_CHAN_DEFINE_WITH_ID(_name, _id, _type, _proxy_name, _user_data, _observers, \
347 _init_val) \
348 ZBUS_CHAN_DEFINE_WITH_ID(_name, _id, _type, _ZBUS_PROXY_AGENT_VALIDATOR(_proxy_name), \
349 _user_data, _observers, _init_val); \
350 _ZBUS_SHADOW_CHAN_REGISTER(_name, _proxy_name)
351
355
356#ifdef __cplusplus
357}
358#endif
359
360#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:110
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:116
int(* backend_send)(const struct zbus_proxy_agent *agent, struct zbus_proxy_msg *msg)
Send function for the backend.
Definition zbus_proxy_agent.h:120
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:122
int(* backend_init)(const struct zbus_proxy_agent *agent)
Initializer function for the backend.
Definition zbus_proxy_agent.h:118
Structure for proxy agent backend descriptor.
Definition zbus_proxy_agent.h:129
const struct zbus_proxy_agent_backend_api * api
API for the backend.
Definition zbus_proxy_agent.h:133
enum zbus_proxy_agent_backend_type type
Type of the backend.
Definition zbus_proxy_agent.h:131
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:94
enum zbus_proxy_agent_backend_type type
The type of the proxy agent backend.
Definition zbus_proxy_agent.h:84
const struct zbus_proxy_agent_backend_api * backend_api
Backend API determined at compile-time based on backend type.
Definition zbus_proxy_agent.h:88
const void * backend_config
Backend specific configuration.
Definition zbus_proxy_agent.h:86
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:90
struct k_msgq * msgq
msgq for received proxy messages
Definition zbus_proxy_agent.h:92
k_tid_t *const thread_id
Pointer to the thread ID of the dedicated proxy agent thread.
Definition zbus_proxy_agent.h:96
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