Skip to content

Commit aa74f05

Browse files
zbus: add multidomain types and proxy agent framework
Add the type definitions and API framework for ZBus multidomain proxy agents. This provides the foundation for backend-agnostic communication between domains. - Define zbus_multidomain_type enum for different backends - Add zbus_proxy_agent_msg structure for inter-domain messages - Create zbus_proxy_agent_api for backend abstraction - Add zbus_proxy_agent_config for agent configuration - Implement core proxy agent functions (init, send, thread) - Add retransmission logic - Add macro system for backend-specific configuration generation The proxy agent framework allows different communication backends (UART, IPC, etc.) to be plugged in using a common API interface. Signed-off-by: Trond F. Christiansen <[email protected]>
1 parent 68e8b57 commit aa74f05

File tree

7 files changed

+944
-0
lines changed

7 files changed

+944
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_INCLUDE_ZBUS_MULTIDOMAIN_H_
8+
#define ZEPHYR_INCLUDE_ZBUS_MULTIDOMAIN_H_
9+
10+
#include <zephyr/kernel.h>
11+
#include <zephyr/sys/crc.h>
12+
#include <string.h>
13+
#include <zephyr/net_buf.h>
14+
#include <zephyr/sys/slist.h>
15+
#include <zephyr/zbus/zbus.h>
16+
#include <zephyr/zbus/multidomain/zbus_multidomain_types.h>
17+
18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
21+
22+
/**
23+
* @brief Zbus Multi-domain API
24+
* @defgroup zbus_multidomain_apis Zbus Multi-domain APIs
25+
* @ingroup zbus_apis
26+
* @since 3.3.0
27+
* @version 1.0.0
28+
* @ingroup os_services
29+
* @{
30+
*/
31+
32+
/**
33+
* @brief Structure for tracking sent messages awaiting acknowledgment.
34+
*
35+
* This structure is used internally by the proxy agent to keep track of messages
36+
* that have been sent but not yet acknowledged. It contains a copy of the message,
37+
* the number of transmit attempts, and a delayed work item for timeout handling.
38+
*/
39+
struct zbus_proxy_agent_tracked_msg {
40+
/** Copy of the sent message */
41+
struct zbus_proxy_agent_msg msg;
42+
43+
/** Pointer to the proxy agent configuration */
44+
struct zbus_proxy_agent_config *config;
45+
46+
/** Number of transmit attempts made for this message */
47+
uint8_t transmit_attempts;
48+
49+
/** Work item for handling acknowledgment timeout */
50+
struct k_work_delayable work;
51+
};
52+
53+
/**
54+
* @brief Configuration structure for the proxy agent.
55+
*
56+
* This structure holds the configuration for a proxy agent, including its name,
57+
* type, backend specific API, and backend specific configuration.
58+
*/
59+
struct zbus_proxy_agent_config {
60+
/* The name of the proxy agent */
61+
const char *name;
62+
63+
/* The type of the proxy agent */
64+
enum zbus_multidomain_type type;
65+
66+
/* Pointer to the backend specific API */
67+
const struct zbus_proxy_agent_api *api;
68+
69+
/* Pointer to the backend specific configuration */
70+
void *backend_config;
71+
72+
/* Pool for tracking sent messages awaiting acknowledgment */
73+
struct net_buf_pool *sent_msg_pool;
74+
75+
/* List of sent messages awaiting acknowledgment */
76+
sys_slist_t sent_msg_list;
77+
};
78+
79+
/**
80+
* @brief Set up a proxy agent using the provided configuration.
81+
*
82+
* Starts the proxy agent thread and initializes the necessary resources.
83+
*
84+
* @note This macro sets up net_buf_pool for tracking sent messages, defines
85+
* a zbus subscriber, and creates a thread for the proxy agent.
86+
*
87+
* @note the ZBUS_MULTIDOMAIN_SENT_MSG_POOL_SIZE configuration option
88+
* must be set to a value greater than or equal to the maximum number of
89+
* unacknowledged messages that can be in flight at any given time.
90+
*
91+
* @note The configuration options ZBUS_MULTIDOMAIN_PROXY_STACK_SIZE and
92+
* ZBUS_MULTIDOMAIN_PROXY_PRIORITY define the stack size and priority of the
93+
* proxy agent thread, respectively.
94+
*
95+
* @param _name The name of the proxy agent.
96+
* @param _type The type of the proxy agent (enum zbus_multidomain_type)
97+
* @param _nodelabel The device node label for the proxy agent.
98+
*/
99+
#define ZBUS_PROXY_AGENT_DEFINE(_name, _type, _nodelabel) \
100+
NET_BUF_POOL_DEFINE(_name##_sent_msg_pool, CONFIG_ZBUS_MULTIDOMAIN_SENT_MSG_POOL_SIZE, \
101+
sizeof(struct zbus_proxy_agent_tracked_msg), sizeof(uint32_t), NULL); \
102+
_ZBUS_GENERATE_BACKEND_CONFIG(_name, _type, _nodelabel); \
103+
struct zbus_proxy_agent_config _name##_config = { \
104+
.name = #_name, \
105+
.type = _type, \
106+
.api = _ZBUS_GET_API(_type), \
107+
.backend_config = _ZBUS_GET_CONFIG(_name, _type), \
108+
.sent_msg_pool = &_name##_sent_msg_pool, \
109+
}; \
110+
ZBUS_MSG_SUBSCRIBER_DEFINE(_name##_subscriber); \
111+
K_THREAD_DEFINE(_name##_thread_id, CONFIG_ZBUS_MULTIDOMAIN_PROXY_STACK_SIZE, \
112+
zbus_proxy_agent_thread, &_name##_config, &_name##_subscriber, NULL, \
113+
CONFIG_ZBUS_MULTIDOMAIN_PROXY_PRIORITY, 0, 0);
114+
115+
/**
116+
* @brief Add a channel to the proxy agent.
117+
*
118+
* @param _name The name of the proxy agent.
119+
* @param _chan The channel to be added.
120+
*/
121+
#define ZBUS_PROXY_ADD_CHANNEL(_name, _chan) ZBUS_CHAN_ADD_OBS(_chan, _name##_subscriber, 0);
122+
123+
/**
124+
* @brief Thread function for the proxy agent.
125+
*
126+
* This function runs in a separate thread and continuously listens for messages
127+
* on the zbus observer. It processes incoming messages and forwards them
128+
* to the appropriate backend for sending.
129+
*
130+
* @param config Pointer to the configuration structure for the proxy agent.
131+
* @param subscriber Pointer to the zbus observer that the proxy agent listens to.
132+
* @return negative error code on failure.
133+
*/
134+
int zbus_proxy_agent_thread(struct zbus_proxy_agent_config *config,
135+
const struct zbus_observer *subscriber);
136+
137+
/** @cond INTERNAL_HIDDEN */
138+
139+
/**
140+
* @brief Macros to generate backend specific configurations for the proxy agent.
141+
*
142+
* This macro generates the backend specific configurations based on the type of
143+
* the proxy agent.
144+
*
145+
* @param _name The name of the proxy agent.
146+
* @param _type The type of the proxy agent (enum zbus_multidomain_type).
147+
* @param _nodelabel The device node label for the proxy agent.
148+
*
149+
* @note This macro finds the matching backend configuration macro from the
150+
* backend specific header files. Requires the backend specific header files to
151+
* define the macros in the format `_ZBUS_GENERATE_BACKEND_CONFIG_<type>(_name, _nodelabel)`.
152+
*/
153+
#define _ZBUS_GENERATE_BACKEND_CONFIG(_name, _type, _nodelabel) \
154+
_ZBUS_GENERATE_BACKEND_CONFIG_##_type(_name, _nodelabel)
155+
156+
/**
157+
* @brief Generic macros to get the API and configuration for the specified type of proxy agent.
158+
*
159+
* These macros are used to retrieve the API and configuration for the specified type of
160+
* proxy agent. The type is specified as an argument to the macro.
161+
*
162+
* @param _type The type of the proxy agent (enum zbus_multidomain_type).
163+
* @param _name The name of the proxy agent.
164+
*
165+
* @note These macros are used to retrieve the API and configuration for the specified type of
166+
* proxy agent. Requires the backend specific header files to define the macros in the format
167+
* `_ZBUS_GET_API_<type>()` and `_ZBUS_GET_CONFIG_<name, type>()`.
168+
*/
169+
#define _ZBUS_GET_API(_type) _ZBUS_GET_API_##_type()
170+
#define _ZBUS_GET_CONFIG(_name, _type) _ZBUS_GET_CONFIG_##_type(_name)
171+
172+
/** @endcond */
173+
174+
/**
175+
* @}
176+
*/
177+
178+
#ifdef __cplusplus
179+
}
180+
#endif
181+
182+
#endif /* ZEPHYR_INCLUDE_ZBUS_MULTIDOMAIN_H_ */

0 commit comments

Comments
 (0)