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);
12 
13 ZBUS_SUBSCRIBER_DEFINE(producer_sub, 4);
14 
producer_thread(void)15 static void producer_thread(void)
16 {
17 	const struct zbus_channel *chan;
18 	void *msg;
19 
20 	for (uint8_t i = 0; (i < 16); ++i) {
21 		msg = k_malloc(i + 1);
22 
23 		__ASSERT_NO_MSG(msg != NULL);
24 
25 		struct external_data_msg dyn_alloc_msg = {.reference = msg, .size = i + 1};
26 
27 		for (int j = 0; j < dyn_alloc_msg.size; ++j) {
28 			((uint8_t *)dyn_alloc_msg.reference)[j] = i;
29 		}
30 
31 		zbus_chan_pub(&pkt_chan, &dyn_alloc_msg, K_MSEC(250));
32 
33 		/* Wait for an consumer's ACK */
34 		if (zbus_sub_wait(&producer_sub, &chan, K_FOREVER)) {
35 			LOG_ERR("Ack not received!");
36 		}
37 	}
38 }
39 
40 K_THREAD_DEFINE(producer_thread_id, 1024, producer_thread, NULL, NULL, NULL, 5, 0, 1000);
41