Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | /* * Copyright (c) 2022 Rodrigo Peixoto <rodrigopex@gmail.com> * SPDX-License-Identifier: Apache-2.0 */ #include <zephyr/kernel.h> #include <zephyr/logging/log.h> #include <zephyr/zbus/zbus.h> #include <zephyr/ztest.h> LOG_MODULE_DECLARE(zbus, CONFIG_ZBUS_LOG_LEVEL); struct sensor_data_msg { int a; int b; }; ZBUS_CHAN_DEFINE(chan1, /* Name */ struct sensor_data_msg, /* Message type */ NULL, /* Validator */ NULL, /* User data */ ZBUS_OBSERVERS_EMPTY, /* observers */ ZBUS_MSG_INIT(0) /* Initial value major 0, minor 1, build 1023 */ ); ZBUS_CHAN_DEFINE(chan2, /* Name */ struct sensor_data_msg, /* Message type */ NULL, /* Validator */ NULL, /* User data */ ZBUS_OBSERVERS(lis2), /* observers */ ZBUS_MSG_INIT(0) /* Initial value major 0, minor 1, build 1023 */ ); ZBUS_CHAN_DEFINE(chan3, /* Name */ struct sensor_data_msg, /* Message type */ NULL, /* Validator */ NULL, /* User data */ ZBUS_OBSERVERS_EMPTY, /* observers */ ZBUS_MSG_INIT(0) /* Initial value major 0, minor 1, build 1023 */ ); ZBUS_SUBSCRIBER_DEFINE(sub1, 1); ZBUS_SUBSCRIBER_DEFINE(sub2, 1); static int count_callback1; static void callback1(const struct zbus_channel *chan) { ++count_callback1; } ZBUS_LISTENER_DEFINE(lis1, callback1); static int count_callback2; static void callback2(const struct zbus_channel *chan) { ++count_callback2; } ZBUS_LISTENER_DEFINE(lis2, callback2); ZBUS_LISTENER_DEFINE(lis3, callback2); ZBUS_LISTENER_DEFINE(lis4, callback2); ZBUS_LISTENER_DEFINE(lis5, callback2); ZBUS_LISTENER_DEFINE(lis6, callback2); ZBUS_LISTENER_DEFINE(lis7, callback2); ZTEST(basic, test_specification_based__zbus_obs_add_rm_obs) { count_callback1 = 0; struct sensor_data_msg sd = {.a = 10, .b = 100}; /* Tyring to add same static observer as one dynamic */ zassert_equal(-EEXIST, zbus_chan_add_obs(&chan2, &lis2, K_MSEC(200)), NULL); zassert_equal(0, zbus_chan_pub(&chan1, &sd, K_MSEC(500)), NULL); zassert_equal(count_callback1, 0, "The counter could not be more than zero, no obs"); zassert_equal(0, zbus_chan_add_obs(&chan1, &lis1, K_MSEC(200)), NULL); zassert_equal(-EALREADY, zbus_chan_add_obs(&chan1, &lis1, K_MSEC(200)), "It cannot be added twice"); zassert_equal(0, zbus_chan_pub(&chan1, &sd, K_MSEC(500)), NULL); zassert_equal(count_callback1, 1, "The counter could not be more than zero, no obs, %d", count_callback1); zassert_equal(0, zbus_chan_rm_obs(&chan1, &lis1, K_MSEC(200)), "It must remove the obs"); zassert_equal(-ENODATA, zbus_chan_rm_obs(&chan1, &lis1, K_MSEC(200)), "It cannot be removed twice"); zassert_equal(0, zbus_chan_pub(&chan1, &sd, K_MSEC(500)), NULL); zassert_equal(count_callback1, 1, "The counter could not be more than zero, no obs, %d", count_callback1); count_callback2 = 0; zassert_equal(0, zbus_chan_pub(&chan2, &sd, K_MSEC(500)), NULL); zassert_equal(count_callback2, 1, "The counter could not be more than zero, no obs"); zassert_equal(0, zbus_chan_add_obs(&chan2, &lis3, K_MSEC(200)), NULL); zassert_equal(-EALREADY, zbus_chan_add_obs(&chan2, &lis3, K_MSEC(200)), "It cannot be added twice"); zassert_equal(0, zbus_chan_pub(&chan2, &sd, K_MSEC(500)), NULL); zassert_equal(count_callback2, 3, "The counter could not be more than zero, no obs, %d", count_callback2); count_callback2 = 0; zassert_equal(0, zbus_chan_add_obs(&chan2, &sub1, K_MSEC(200)), NULL); zassert_equal(0, zbus_chan_add_obs(&chan2, &sub2, K_MSEC(200)), NULL); zassert_equal(0, zbus_chan_add_obs(&chan2, &lis4, K_MSEC(200)), "It must add the obs"); zassert_equal(0, zbus_chan_add_obs(&chan2, &lis5, K_MSEC(200)), "It must add the obs"); zassert_equal(0, zbus_chan_add_obs(&chan2, &lis6, K_MSEC(200)), "It must add the obs"); /* Make the heap full */ void *mem; do { mem = k_malloc(1); } while (mem != NULL); /* With the heap full it will not be possible to add another obs */ zassert_equal(-ENOMEM, zbus_chan_add_obs(&chan2, &lis7, K_MSEC(200)), NULL); zassert_equal(0, zbus_chan_pub(&chan2, &sd, K_MSEC(500)), NULL); zassert_equal(count_callback2, 5, NULL); /* To cause an error to sub1 and sub2. They have the queue full in this point */ /* ENOMSG must be the result */ zassert_equal(-ENOMSG, zbus_chan_pub(&chan2, &sd, K_MSEC(500)), NULL); zassert_equal(count_callback2, 10, NULL); zassert_equal(0, zbus_chan_rm_obs(&chan2, &sub1, K_MSEC(200)), NULL); zassert_equal(0, zbus_chan_rm_obs(&chan2, &sub2, K_MSEC(200)), NULL); } struct aux2_wq_data { struct k_work work; }; static struct aux2_wq_data wq_handler; static void wq_dh_cb(struct k_work *item) { zassert_equal(-EAGAIN, zbus_chan_add_obs(&chan2, &sub1, K_MSEC(200)), NULL); zassert_equal(-EAGAIN, zbus_chan_rm_obs(&chan2, &sub2, K_MSEC(200)), NULL); } ZTEST(basic, test_specification_based__zbus_obs_add_rm_obs_busy) { zassert_equal(0, zbus_chan_claim(&chan2, K_NO_WAIT), NULL); k_work_init(&wq_handler.work, wq_dh_cb); k_work_submit(&wq_handler.work); k_msleep(1000); zassert_equal(0, zbus_chan_finish(&chan2), NULL); } ZBUS_CHAN_DEFINE(chan4, /* Name */ struct sensor_data_msg, /* Message type */ NULL, /* Validator */ NULL, /* User data */ ZBUS_OBSERVERS(prio_lis6, prio_lis5), /* observers */ ZBUS_MSG_INIT(0) /* Initial value major 0, minor 1, build 1023 */ ); static int execution_sequence_idx; static uint8_t execution_sequence[6] = {0}; #define CALLBACK_DEF(_lis, _idx) \ static void _CONCAT(prio_cb, _idx)(const struct zbus_channel *chan) \ { \ execution_sequence[execution_sequence_idx] = _idx; \ ++execution_sequence_idx; \ } \ ZBUS_LISTENER_DEFINE(_lis, _CONCAT(prio_cb, _idx)) CALLBACK_DEF(prio_lis1, 1); CALLBACK_DEF(prio_lis2, 2); CALLBACK_DEF(prio_lis3, 3); CALLBACK_DEF(prio_lis4, 4); CALLBACK_DEF(prio_lis5, 5); CALLBACK_DEF(prio_lis6, 6); ZBUS_CHAN_ADD_OBS(chan4, prio_lis3, 3); ZBUS_CHAN_ADD_OBS(chan4, prio_lis4, 2); /* Checking the ZBUS_CHAN_ADD_OBS. The execution sequence must be: 6, 5, 4, 3, 2, 1. */ ZTEST(basic, test_specification_based__zbus_obs_priority) { struct sensor_data_msg sd = {.a = 70, .b = 116}; execution_sequence_idx = 0; zassert_equal(0, zbus_chan_add_obs(&chan4, &prio_lis2, K_MSEC(200)), NULL); zassert_equal(0, zbus_chan_add_obs(&chan4, &prio_lis1, K_MSEC(200)), NULL); zassert_equal(0, zbus_chan_pub(&chan4, &sd, K_MSEC(500)), NULL); zassert_equal(execution_sequence[0], 6, NULL); zassert_equal(execution_sequence[1], 5, NULL); zassert_equal(execution_sequence[2], 4, NULL); zassert_equal(execution_sequence[3], 3, NULL); zassert_equal(execution_sequence[4], 2, NULL); zassert_equal(execution_sequence[5], 1, NULL); } ZTEST_SUITE(basic, NULL, NULL, NULL, NULL, NULL); |