1 /*
2  * Copyright (c) 2023 Rodrigo Peixoto <rodrigopex@gmail.com>
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #include "messages.h"
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/util_macro.h>
9 #include <zephyr/sys/atomic.h>
10 #include <zephyr/zbus/zbus.h>
11 
12 #define CONSUMER_STACK_SIZE (CONFIG_IDLE_STACK_SIZE + CONFIG_BM_MESSAGE_SIZE)
13 
14 extern atomic_t count;
15 
16 static void s_cb(const struct zbus_channel *chan);
17 
18 ZBUS_CHAN_DECLARE(bm_channel);
19 
20 #define SFY(i, _) s##i
21 
22 #define CONSUMERS_NAME_LIST LISTIFY(CONFIG_BM_ONE_TO, SFY, (, /* separator */))
23 
24 #define CREATE_OBSERVER(s) ZBUS_LISTENER_DEFINE(s, s_cb)
25 
26 #define CREATE_OBSERVATIONS(s) ZBUS_CHAN_ADD_OBS(bm_channel, s, 3)
27 
28 /* clang-format off */
29 FOR_EACH(CREATE_OBSERVER, (;), CONSUMERS_NAME_LIST);
30 
31 FOR_EACH(CREATE_OBSERVATIONS, (;), CONSUMERS_NAME_LIST);
32 /* clang-format on */
33 
s_cb(const struct zbus_channel * chan)34 static void s_cb(const struct zbus_channel *chan)
35 {
36 
37 	if (IS_ENABLED(CONFIG_BM_FAIRPLAY)) {
38 		struct bm_msg msg_received;
39 
40 		memcpy(zbus_chan_msg(chan), &msg_received, chan->message_size);
41 
42 		atomic_add(&count, *((uint16_t *)msg_received.bytes));
43 	} else {
44 		const struct bm_msg *msg_received = zbus_chan_const_msg(chan);
45 
46 		atomic_add(&count, *((uint16_t *)msg_received->bytes));
47 	}
48 }
49