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_SUBSCRIBER_DEFINE(peripheral_sub, 8);
12 
13 ZBUS_CHAN_DECLARE(sensor_data_chan);
14 
15 static bool sensor_data_from_bridge;
16 ZBUS_CHAN_DEFINE(sensor_data_chan,	 /* Name */
17 		 struct sensor_data_msg, /* Message type */
18 
19 		 NULL,			     /* Validator */
20 		 &sensor_data_from_bridge,   /* User data */
21 		 ZBUS_OBSERVERS(bridge_sub), /* observers */
22 		 ZBUS_MSG_INIT(0)	     /* Initial value {0} */
23 );
24 
25 static bool start_measurement_from_bridge;
26 ZBUS_CHAN_DEFINE(start_measurement_chan, /* Name */
27 		 struct action_msg,	 /* Message type */
28 
29 		 NULL,					     /* Validator */
30 		 &start_measurement_from_bridge,	     /* User data */
31 		 ZBUS_OBSERVERS(bridge_sub, peripheral_sub), /* observers */
32 		 ZBUS_MSG_INIT(false)			     /* Initial value */
33 );
34 
peripheral_thread(void)35 static void peripheral_thread(void)
36 {
37 	const struct zbus_channel *chan;
38 	struct sensor_data_msg sd = {0, 0};
39 
40 	while (!zbus_sub_wait(&peripheral_sub, &chan, K_FOREVER)) {
41 		LOG_DBG("Peripheral sending sensor data");
42 
43 		sd.a += 1;
44 		sd.b += 10;
45 
46 		zbus_chan_pub(&sensor_data_chan, &sd, K_MSEC(250));
47 	}
48 }
49 
50 K_THREAD_DEFINE(peripheral_thread_id, 1024, peripheral_thread, NULL, NULL, NULL, 3, 0, 0);
51