|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/kernel.h> |
| 8 | +#include <zephyr/logging/log.h> |
| 9 | +#include <string.h> |
| 10 | +#include <zephyr/zbus/zbus.h> |
| 11 | +#include <zephyr/zbus/multidomain/zbus_multidomain.h> |
| 12 | + |
| 13 | +#include "common.h" |
| 14 | + |
| 15 | +LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG); |
| 16 | + |
| 17 | +/* NOTE: Shared channels request_channel and response_channel are defined in common.h */ |
| 18 | + |
| 19 | +/* Set up proxy agent for UART1 and add response channel to be forwarded */ |
| 20 | +ZBUS_PROXY_AGENT_DEFINE(uart1_proxy, ZBUS_MULTIDOMAIN_TYPE_UART, uart1); |
| 21 | +ZBUS_PROXY_ADD_CHANNEL(uart1_proxy, request_channel); |
| 22 | + |
| 23 | +/* Log response data coming from the response channel */ |
| 24 | +void uart_forwarder_listener_cb(const struct zbus_channel *chan) |
| 25 | +{ |
| 26 | + const struct response_data *data = zbus_chan_const_msg(chan); |
| 27 | + |
| 28 | + LOG_INF("Received message on channel %s", chan->name); |
| 29 | + LOG_INF("Response ID: %d, Value: %d", data->response_id, data->value); |
| 30 | +} |
| 31 | + |
| 32 | +ZBUS_LISTENER_DEFINE(uart_forwarder_listener, uart_forwarder_listener_cb); |
| 33 | +ZBUS_CHAN_ADD_OBS(response_channel, uart_forwarder_listener, 0); |
| 34 | + |
| 35 | +bool print_channel_info(const struct zbus_channel *chan) |
| 36 | +{ |
| 37 | + if (!chan) { |
| 38 | + LOG_ERR("Channel is NULL"); |
| 39 | + return false; |
| 40 | + } |
| 41 | + LOG_INF("Channel %s is a %s channel", chan->name, |
| 42 | + ZBUS_CHANNEL_IS_SHADOW(chan) ? "shadow" : "master"); |
| 43 | + |
| 44 | + return true; |
| 45 | +} |
| 46 | + |
| 47 | +int main(void) |
| 48 | +{ |
| 49 | + LOG_INF("ZBUS Multidomain UART Forwarder Sample Application"); |
| 50 | + zbus_iterate_over_channels(print_channel_info); |
| 51 | + |
| 52 | + struct request_data data = {.request_id = 1, .min_value = -1, .max_value = 1}; |
| 53 | + |
| 54 | + while (1) { |
| 55 | + int ret = zbus_chan_pub(&request_channel, &data, K_MSEC(100)); |
| 56 | + |
| 57 | + if (ret < 0) { |
| 58 | + LOG_ERR("Failed to publish on channel %s: %d", request_channel.name, ret); |
| 59 | + } else { |
| 60 | + LOG_INF("Published on channel %s. Request ID=%d, Min=%d, Max=%d", |
| 61 | + request_channel.name, data.request_id, data.min_value, |
| 62 | + data.max_value); |
| 63 | + } |
| 64 | + |
| 65 | + data.request_id++; |
| 66 | + data.min_value -= 1; |
| 67 | + data.max_value += 1; |
| 68 | + k_sleep(K_SECONDS(5)); |
| 69 | + } |
| 70 | + return 0; |
| 71 | +} |
0 commit comments