1 /*
2  * Copyright (c) 2022 Rodrigo Peixoto <rodrigopex@gmail.com>
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #include "messages.h"
6 
7 #include <zephyr/logging/log.h>
8 #include <zephyr/zbus/zbus.h>
9 LOG_MODULE_DECLARE(zbus, CONFIG_ZBUS_LOG_LEVEL);
10 
11 ZBUS_CHAN_DECLARE(pkt_chan, data_ready_chan, ack);
12 
13 ZBUS_SUBSCRIBER_DEFINE(consumer_sub, 4);
14 
consumer_thread(void)15 static void consumer_thread(void)
16 {
17 	struct external_data_msg *dyn_alloc_msg;
18 	const struct zbus_channel *chan;
19 
20 	while (!zbus_sub_wait(&consumer_sub, &chan, K_FOREVER)) {
21 		LOG_INF("Channel %s\n", zbus_chan_name(chan));
22 
23 		__ASSERT_NO_MSG(chan == &data_ready_chan);
24 
25 		if (!zbus_chan_claim(&pkt_chan, K_MSEC(100))) {
26 			dyn_alloc_msg = zbus_chan_msg(&pkt_chan);
27 
28 			LOG_WRN("size=%02d", (int)dyn_alloc_msg->size);
29 			LOG_HEXDUMP_WRN(dyn_alloc_msg->reference, dyn_alloc_msg->size, "Content");
30 
31 			/* Cleanup the dynamic memory after using that */
32 			k_free(dyn_alloc_msg->reference);
33 			dyn_alloc_msg->reference = NULL;
34 			dyn_alloc_msg->size = 0;
35 
36 			zbus_chan_finish(&pkt_chan);
37 		}
38 		struct ack_msg a = {1};
39 
40 		zbus_chan_pub(&ack, &a, K_MSEC(250));
41 	}
42 }
43 
44 K_THREAD_DEFINE(consumer_thread_id, 1024, consumer_thread, NULL, NULL, NULL, 4, 0, 1000);
45