Skip to content

Commit 4d80338

Browse files
rodrigopexcarlescufi
authored andcommitted
samples: zbus: fix samples to work with iterable sections observers
Replace the `CONFIG_ZBUS_RUNTIME_OBSERVERS_POOL_SIZE` with the `CONFIG_ZBUS_RUNTIME_OBSERVERS` and add the heap. Add user_data to hello world sample iterator functions. Signed-off-by: Rodrigo Peixoto <[email protected]>
1 parent 6880b85 commit 4d80338

File tree

5 files changed

+48
-48
lines changed

5 files changed

+48
-48
lines changed

samples/subsys/zbus/hello_world/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ CONFIG_ZBUS=y
66
CONFIG_ZBUS_LOG_LEVEL_INF=y
77
CONFIG_ZBUS_CHANNEL_NAME=y
88
CONFIG_ZBUS_OBSERVER_NAME=y
9+
CONFIG_ZBUS_RUNTIME_OBSERVERS=y

samples/subsys/zbus/hello_world/sample.yaml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,7 @@ tests:
2626
- "I: From listener -> Acc x=2, y=2, z=2"
2727
- "I: Pub a valid value to a channel with validator successfully."
2828
- "I: Pub an invalid value to a channel with validator successfully."
29-
arch_exclude:
30-
- xtensa
3129
platform_exclude: qemu_leon3
3230
tags: zbus
3331
integration_platforms:
3432
- qemu_x86
35-
sample.zbus.hello_world_no_iterable_sections:
36-
harness: console
37-
harness_config:
38-
type: multi_line
39-
ordered: false
40-
regex:
41-
- "I: Sensor sample started raw reading, version 0.1-2!"
42-
- "I: From subscriber -> Acc x=1, y=1, z=1"
43-
- "I: From listener -> Acc x=1, y=1, z=1"
44-
- "I: From subscriber -> Acc x=2, y=2, z=2"
45-
- "I: From listener -> Acc x=2, y=2, z=2"
46-
- "I: Pub a valid value to a channel with validator successfully."
47-
- "I: Pub an invalid value to a channel with validator successfully."
48-
arch_allow:
49-
- xtensa
50-
tags: zbus

samples/subsys/zbus/hello_world/src/main.c

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ struct acc_msg {
2222
int z;
2323
};
2424

25-
ZBUS_CHAN_DEFINE(version_chan, /* Name */
25+
ZBUS_CHAN_DEFINE(version_chan, /* Name */
2626
struct version_msg, /* Message type */
2727

28-
NULL, /* Validator */
29-
NULL, /* User data */
28+
NULL, /* Validator */
29+
NULL, /* User data */
3030
ZBUS_OBSERVERS_EMPTY, /* observers */
3131
ZBUS_MSG_INIT(.major = 0, .minor = 1,
3232
.build = 2) /* Initial value major 0, minor 1, build 2 */
3333
);
3434

35-
ZBUS_CHAN_DEFINE(acc_data_chan, /* Name */
35+
ZBUS_CHAN_DEFINE(acc_data_chan, /* Name */
3636
struct acc_msg, /* Message type */
3737

38-
NULL, /* Validator */
39-
NULL, /* User data */
38+
NULL, /* Validator */
39+
NULL, /* User data */
4040
ZBUS_OBSERVERS(foo_lis, bar_sub), /* observers */
4141
ZBUS_MSG_INIT(.x = 0, .y = 0, .z = 0) /* Initial value */
4242
);
@@ -55,12 +55,12 @@ static bool simple_chan_validator(const void *msg, size_t msg_size)
5555
}
5656

5757
ZBUS_CHAN_DEFINE(simple_chan, /* Name */
58-
int, /* Message type */
58+
int, /* Message type */
5959

6060
simple_chan_validator, /* Validator */
61-
NULL, /* User data */
62-
ZBUS_OBSERVERS_EMPTY, /* observers */
63-
0 /* Initial value is 0 */
61+
NULL, /* User data */
62+
ZBUS_OBSERVERS_EMPTY, /* observers */
63+
0 /* Initial value is 0 */
6464
);
6565

6666
static void listener_callback_example(const struct zbus_channel *chan)
@@ -89,34 +89,51 @@ static void subscriber_task(void)
8989
}
9090
}
9191

92-
K_THREAD_DEFINE(subscriber_task_id, CONFIG_MAIN_STACK_SIZE,
93-
subscriber_task, NULL, NULL, NULL, 3, 0, 0);
92+
K_THREAD_DEFINE(subscriber_task_id, CONFIG_MAIN_STACK_SIZE, subscriber_task, NULL, NULL, NULL, 3, 0,
93+
0);
9494

95-
#if defined(CONFIG_ZBUS_STRUCTS_ITERABLE_ACCESS)
96-
static int count;
97-
98-
static bool print_channel_data_iterator(const struct zbus_channel *chan)
95+
static bool print_channel_data_iterator(const struct zbus_channel *chan, void *user_data)
9996
{
100-
LOG_INF("%d - Channel %s:", count, zbus_chan_name(chan));
97+
int *count = user_data;
98+
99+
LOG_INF("%d - Channel %s:", *count, zbus_chan_name(chan));
101100
LOG_INF(" Message size: %d", zbus_chan_msg_size(chan));
102-
++count;
103101
LOG_INF(" Observers:");
104-
for (const struct zbus_observer *const *obs = chan->observers; *obs != NULL; ++obs) {
105-
LOG_INF(" - %s", (*obs)->name);
102+
103+
++(*count);
104+
105+
struct zbus_channel_observation *observation;
106+
107+
for (int16_t i = chan->data->observers_start_idx, limit = chan->data->observers_end_idx;
108+
i < limit; ++i) {
109+
STRUCT_SECTION_GET(zbus_channel_observation, i, &observation);
110+
111+
__ASSERT(observation != NULL, "observation must be not NULL");
112+
113+
LOG_INF(" - %s", observation->obs->name);
114+
}
115+
116+
struct zbus_observer_node *obs_nd, *tmp;
117+
118+
SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&chan->data->observers, obs_nd, tmp, node) {
119+
LOG_INF(" - %s", obs_nd->obs->name);
106120
}
107121

108122
return true;
109123
}
110124

111-
static bool print_observer_data_iterator(const struct zbus_observer *obs)
125+
static bool print_observer_data_iterator(const struct zbus_observer *obs, void *user_data)
112126
{
113-
LOG_INF("%d - %s %s", count, obs->queue ? "Subscriber" : "Listener", zbus_obs_name(obs));
127+
int *count = user_data;
128+
129+
LOG_INF("%d - %s %s", *count,
130+
obs->type == ZBUS_OBSERVER_LISTENER_TYPE ? "Listener" : "Subscriber",
131+
zbus_obs_name(obs));
114132

115-
++count;
133+
++(*count);
116134

117135
return true;
118136
}
119-
#endif /* CONFIG_ZBUS_STRUCTS_ITERABLE_ACCESS */
120137

121138
int main(void)
122139
{
@@ -127,17 +144,15 @@ int main(void)
127144
LOG_INF("Sensor sample started raw reading, version %u.%u-%u!", v->major, v->minor,
128145
v->build);
129146

130-
#if defined(CONFIG_ZBUS_STRUCTS_ITERABLE_ACCESS)
131-
count = 0;
147+
int count = 0;
132148

133149
LOG_INF("Channel list:");
134-
zbus_iterate_over_channels(print_channel_data_iterator);
150+
zbus_iterate_over_channels_with_user_data(print_channel_data_iterator, &count);
135151

136152
count = 0;
137153

138154
LOG_INF("Observers list:");
139-
zbus_iterate_over_observers(print_observer_data_iterator);
140-
#endif
155+
zbus_iterate_over_observers_with_user_data(print_observer_data_iterator, &count);
141156
zbus_chan_pub(&acc_data_chan, &acc1, K_SECONDS(1));
142157

143158
k_msleep(1000);

samples/subsys/zbus/runtime_obs_registration/prj.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ CONFIG_LOG_MODE_MINIMAL=y
44
CONFIG_BOOT_BANNER=n
55
CONFIG_ZBUS=y
66
CONFIG_ZBUS_LOG_LEVEL_INF=y
7-
CONFIG_ZBUS_RUNTIME_OBSERVERS_POOL_SIZE=2
7+
CONFIG_ZBUS_RUNTIME_OBSERVERS=y
8+
CONFIG_HEAP_MEM_POOL_SIZE=256
89
CONFIG_MP_MAX_NUM_CPUS=1

samples/subsys/zbus/runtime_obs_registration/sample.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ tests:
55
min_ram: 16
66
integration_platforms:
77
- qemu_x86
8+
arch_exclude: nios2
89
harness: console
910
harness_config:
1011
type: multi_line

0 commit comments

Comments
 (0)