1 /*
2  * Copyright (c) 2024 Embeint Inc
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #include <zephyr/zbus/zbus.h>
7 #include <zephyr/ztest.h>
8 #include <zephyr/ztest_assert.h>
9 
10 struct msg {
11 	int x;
12 };
13 
14 enum channel_ids {
15 	CHAN_A = 100,
16 	CHAN_B = 123,
17 	CHAN_C = 0x12343243,
18 	CHAN_D = 123,
19 	CHAN_E = 1,
20 	CHAN_F = 357489,
21 };
22 
23 ZBUS_CHAN_DEFINE_WITH_ID(chan_a, CHAN_A, struct msg, NULL, NULL, ZBUS_OBSERVERS_EMPTY,
24 			 ZBUS_MSG_INIT(0));
25 ZBUS_CHAN_DEFINE_WITH_ID(chan_b, CHAN_B, struct msg, NULL, NULL, ZBUS_OBSERVERS_EMPTY,
26 			 ZBUS_MSG_INIT(0));
27 ZBUS_CHAN_DEFINE_WITH_ID(chan_c, CHAN_C, struct msg, NULL, NULL, ZBUS_OBSERVERS_EMPTY,
28 			 ZBUS_MSG_INIT(0));
29 ZBUS_CHAN_DEFINE_WITH_ID(chan_d, CHAN_D, struct msg, NULL, NULL, ZBUS_OBSERVERS_EMPTY,
30 			 ZBUS_MSG_INIT(0));
31 ZBUS_CHAN_DEFINE_WITH_ID(chan_e, CHAN_E, struct msg, NULL, NULL, ZBUS_OBSERVERS_EMPTY,
32 			 ZBUS_MSG_INIT(0));
33 ZBUS_CHAN_DEFINE_WITH_ID(chan_f, CHAN_F, struct msg, NULL, NULL, ZBUS_OBSERVERS_EMPTY,
34 			 ZBUS_MSG_INIT(0));
35 ZBUS_CHAN_DEFINE(chan_g, struct msg, NULL, NULL, ZBUS_OBSERVERS_EMPTY, ZBUS_MSG_INIT(0));
36 
ZTEST(channel_id,test_channel_retrieval)37 ZTEST(channel_id, test_channel_retrieval)
38 {
39 	/* Invalid/unknown channel IDs */
40 	zassert_is_null(zbus_chan_from_id(0x1000000));
41 	zassert_is_null(zbus_chan_from_id(ZBUS_CHAN_ID_INVALID));
42 
43 	/* Standard retrieval */
44 	zassert_equal(&chan_a, zbus_chan_from_id(CHAN_A));
45 	zassert_equal(&chan_c, zbus_chan_from_id(CHAN_C));
46 	zassert_equal(&chan_e, zbus_chan_from_id(CHAN_E));
47 	zassert_equal(&chan_f, zbus_chan_from_id(CHAN_F));
48 
49 	/* Duplicate channel IDs */
50 	zassert_true((&chan_b == zbus_chan_from_id(CHAN_B)) ||
51 		     (&chan_d == zbus_chan_from_id(CHAN_B)));
52 }
53 
54 ZTEST_SUITE(channel_id, NULL, NULL, NULL, NULL, NULL);
55