-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathchecks.c
More file actions
281 lines (227 loc) · 6.31 KB
/
checks.c
File metadata and controls
281 lines (227 loc) · 6.31 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* Copyright (c) 2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <unity.h>
#include <zephyr/zbus/zbus.h>
#include <zephyr/logging/log.h>
#include "app_common.h"
#include "location.h"
#include "power.h"
#include "network.h"
#include "checks.h"
ZBUS_MSG_SUBSCRIBER_DEFINE(location_subscriber);
ZBUS_MSG_SUBSCRIBER_DEFINE(network_subscriber);
ZBUS_MSG_SUBSCRIBER_DEFINE(power_subscriber);
ZBUS_CHAN_ADD_OBS(LOCATION_CHAN, location_subscriber, 0);
ZBUS_CHAN_ADD_OBS(NETWORK_CHAN, network_subscriber, 0);
ZBUS_CHAN_ADD_OBS(POWER_CHAN, power_subscriber, 0);
LOG_MODULE_REGISTER(main_module_checks, 4);
void check_location_event(enum location_msg_type expected_location_type)
{
int err;
const struct zbus_channel *chan;
enum location_msg_type location_msg_type;
/* Allow the test thread to sleep so that the DUT's thread is allowed to run. */
k_sleep(K_MSEC(100));
err = zbus_sub_wait_msg(&location_subscriber, &chan, &location_msg_type, K_MSEC(10000));
if (err == -ENOMSG) {
LOG_ERR("No location event received");
TEST_FAIL();
} else if (err) {
LOG_ERR("zbus_sub_wait, error: %d", err);
TEST_FAIL();
return;
}
if (chan != &LOCATION_CHAN) {
LOG_ERR("Received message from wrong channel, expected %s, got %s",
zbus_chan_name(&LOCATION_CHAN), zbus_chan_name(chan));
TEST_FAIL();
}
TEST_ASSERT_EQUAL(expected_location_type, location_msg_type);
}
void check_network_event(enum network_msg_type expected_network_type)
{
int err;
const struct zbus_channel *chan;
struct network_msg network_msg;
/* Allow the test thread to sleep so that the DUT's thread is allowed to run. */
k_sleep(K_MSEC(100));
err = zbus_sub_wait_msg(&network_subscriber, &chan, &network_msg, K_MSEC(10000));
if (err == -ENOMSG) {
LOG_ERR("No network event received");
TEST_FAIL();
} else if (err) {
LOG_ERR("zbus_sub_wait, error: %d", err);
TEST_FAIL();
return;
}
if (chan != &NETWORK_CHAN) {
LOG_ERR("Received message from wrong channel, expected %s, got %s",
zbus_chan_name(&NETWORK_CHAN), zbus_chan_name(chan));
TEST_FAIL();
}
TEST_ASSERT_EQUAL(expected_network_type, network_msg.type);
}
void check_power_event(enum power_msg_type expected_power_type)
{
int err;
const struct zbus_channel *chan;
struct power_msg power_msg;
/* Allow the test thread to sleep so that the DUT's thread is allowed to run. */
k_sleep(K_MSEC(100));
err = zbus_sub_wait_msg(&power_subscriber, &chan, &power_msg, K_MSEC(10000));
if (err == -ENOMSG) {
LOG_ERR("No power event received");
TEST_FAIL();
} else if (err) {
LOG_ERR("zbus_sub_wait, error: %d", err);
TEST_FAIL();
return;
}
if (chan != &POWER_CHAN) {
LOG_ERR("Received message from wrong channel, expected %s, got %s",
zbus_chan_name(&POWER_CHAN), zbus_chan_name(chan));
TEST_FAIL();
}
TEST_ASSERT_EQUAL(expected_power_type, power_msg.type);
}
static void check_no_location_events(void)
{
int err;
const struct zbus_channel *chan;
enum location_msg_type location_msg_type;
err = zbus_sub_wait_msg(&location_subscriber, &chan, &location_msg_type, K_MSEC(10000));
if (err == -ENOMSG) {
return;
} else if (err) {
LOG_ERR("zbus_sub_wait, error: %d", err);
TEST_FAIL();
return;
}
LOG_ERR("Received unexpected location event: %d", location_msg_type);
TEST_FAIL();
}
static void check_no_network_events(void)
{
int err;
const struct zbus_channel *chan;
struct network_msg network_msg;
err = zbus_sub_wait_msg(&network_subscriber, &chan, &network_msg, K_MSEC(10000));
if (err == -ENOMSG) {
return;
} else if (err) {
LOG_ERR("zbus_sub_wait, error: %d", err);
TEST_FAIL();
return;
}
LOG_ERR("Received unexpected network event: %d", network_msg.type);
TEST_FAIL();
}
static void check_no_power_events(void)
{
int err;
const struct zbus_channel *chan;
struct power_msg power_msg;
err = zbus_sub_wait_msg(&power_subscriber, &chan, &power_msg, K_MSEC(10000));
if (err == -ENOMSG) {
return;
} else if (err) {
LOG_ERR("zbus_sub_wait, error: %d", err);
TEST_FAIL();
return;
}
LOG_ERR("Received unexpected power event: %d", power_msg.type);
TEST_FAIL();
}
void check_no_events(uint32_t timeout_sec)
{
k_sleep(K_SECONDS(timeout_sec));
check_no_location_events();
check_no_network_events();
check_no_power_events();
}
void purge_location_events(void)
{
while (true) {
int err;
const struct zbus_channel *chan;
enum location_msg_type location_msg_type;
err = zbus_sub_wait_msg(&location_subscriber, &chan, &location_msg_type,
K_MSEC(100));
if (err == -ENOMSG) {
break;
} else if (err) {
LOG_ERR("zbus_sub_wait, error: %d", err);
TEST_FAIL();
return;
}
}
}
void purge_network_events(void)
{
while (true) {
int err;
const struct zbus_channel *chan;
struct network_msg network_msg;
err = zbus_sub_wait_msg(&network_subscriber, &chan, &network_msg, K_MSEC(100));
if (err == -ENOMSG) {
break;
} else if (err) {
LOG_ERR("zbus_sub_wait, error: %d", err);
TEST_FAIL();
return;
}
}
}
void purge_power_events(void)
{
while (true) {
int err;
const struct zbus_channel *chan;
struct power_msg power_msg;
err = zbus_sub_wait_msg(&power_subscriber, &chan, &power_msg, K_MSEC(100));
if (err == -ENOMSG) {
break;
} else if (err) {
LOG_ERR("zbus_sub_wait, error: %d", err);
TEST_FAIL();
return;
}
}
}
void purge_all_events(void)
{
purge_location_events();
purge_network_events();
purge_power_events();
}
int wait_for_location_event(enum location_msg_type expected_type, uint32_t timeout_sec)
{
int err;
const struct zbus_channel *chan;
enum location_msg_type location_msg_type;
uint32_t start_time = k_uptime_seconds();
uint32_t elapsed_time;
err = zbus_sub_wait_msg(&location_subscriber, &chan, &location_msg_type,
K_SECONDS(timeout_sec));
if (err == -ENOMSG) {
return -ENOMSG;
} else if (err) {
LOG_ERR("zbus_sub_wait, error: %d", err);
return err;
}
if (chan != &LOCATION_CHAN) {
LOG_ERR("Received message from wrong channel, expected %s, got %s",
zbus_chan_name(&LOCATION_CHAN), zbus_chan_name(chan));
return -EINVAL;
}
if (location_msg_type != expected_type) {
LOG_ERR("Received unexpected location event: %d", location_msg_type);
return -EINVAL;
}
elapsed_time = k_uptime_seconds() - start_time;
LOG_DBG("Received expected location event: %d, wait: %d", location_msg_type, elapsed_time);
return elapsed_time;
}