1 /*
2 * Copyright (c) 2020 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <errno.h>
8 #include <stdio.h>
9
10 #include <zephyr/fff.h>
11 #include <zephyr/net/ieee802154_radio.h>
12 #include <zephyr/net/net_pkt.h>
13 #include <zephyr/kernel.h>
14 #include <zephyr/ztest.h>
15
16 #include <openthread/message.h>
17 #include <openthread/platform/radio.h>
18 #include <platform-zephyr.h>
19
20 DEFINE_FFF_GLOBALS;
21
22 /**
23 * @brief Tests for the radio.c - OpenThread radio api
24 * @defgroup openthread_tests radio
25 * @ingroup all_tests
26 * @{
27 */
28
29 #define ACK_PKT_LENGTH 3
30 #define FRAME_TYPE_MASK 0x07
31 #define FRAME_TYPE_ACK 0x02
32
33 K_SEM_DEFINE(ot_sem, 0, 1);
34
35 /**
36 * Fake pointer as it should not be accessed by the code.
37 * Should not be null to be sure it was properly passed.
38 */
39 otInstance *ot = (otInstance *)0xAAAA;
40 otMessage *ip_msg = (otMessage *)0xBBBB;
41
42 /* forward declarations */
43 FAKE_VALUE_FUNC(int, scan_mock, const struct device *, uint16_t, energy_scan_done_cb_t);
44 FAKE_VALUE_FUNC(int, cca_mock, const struct device *);
45 FAKE_VALUE_FUNC(int, set_channel_mock, const struct device *, uint16_t);
46 FAKE_VALUE_FUNC(int, filter_mock, const struct device *, bool, enum ieee802154_filter_type,
47 const struct ieee802154_filter *);
48 FAKE_VALUE_FUNC(int, set_txpower_mock, const struct device *, int16_t);
49 FAKE_VALUE_FUNC(int64_t, get_time_mock, const struct device *);
50 FAKE_VALUE_FUNC(int, tx_mock, const struct device *, enum ieee802154_tx_mode, struct net_pkt *,
51 struct net_buf *);
52 FAKE_VALUE_FUNC(int, start_mock, const struct device *);
53 FAKE_VALUE_FUNC(int, stop_mock, const struct device *);
54 FAKE_VALUE_FUNC(int, configure_mock, const struct device *, enum ieee802154_config_type,
55 const struct ieee802154_config *);
56 FAKE_VALUE_FUNC(enum ieee802154_hw_caps, get_capabilities_caps_mock, const struct device *);
57
58 static enum ieee802154_hw_caps get_capabilities(const struct device *dev);
59
60 /* mocks */
61 static struct ieee802154_radio_api rapi = {.get_capabilities = get_capabilities,
62 .cca = cca_mock,
63 .set_channel = set_channel_mock,
64 .filter = filter_mock,
65 .set_txpower = set_txpower_mock,
66 .get_time = get_time_mock,
67 .tx = tx_mock,
68 .start = start_mock,
69 .stop = stop_mock,
70 .configure = configure_mock,
71 .ed_scan = scan_mock};
72
73 #define DT_DRV_COMPAT vnd_ieee802154
74 DEVICE_DT_INST_DEFINE(0, NULL, NULL, NULL, NULL, POST_KERNEL, 0, &rapi);
75
76 static const struct device *const radio = DEVICE_DT_INST_GET(0);
77
78 static int16_t rssi_scan_mock_max_ed;
rssi_scan_mock(const struct device * dev,uint16_t duration,energy_scan_done_cb_t done_cb)79 static int rssi_scan_mock(const struct device *dev, uint16_t duration,
80 energy_scan_done_cb_t done_cb)
81 {
82 zassert_equal(dev, radio, "Device handle incorrect.");
83 zassert_equal(duration, 1, "otPlatRadioGetRssi shall pass minimal allowed value.");
84
85 /* use return value as callback param */
86 done_cb(radio, rssi_scan_mock_max_ed);
87
88 return 0;
89 }
90
91 FAKE_VOID_FUNC(otPlatRadioEnergyScanDone, otInstance *, int8_t);
92
otSysEventSignalPending(void)93 void otSysEventSignalPending(void) { k_sem_give(&ot_sem); }
94
otTaskletsSignalPending(otInstance * aInstance)95 void otTaskletsSignalPending(otInstance *aInstance)
96 {
97 zassert_equal(aInstance, ot, "Incorrect instance.");
98 k_sem_give(&ot_sem);
99 }
100
make_sure_sem_set(k_timeout_t timeout)101 static void make_sure_sem_set(k_timeout_t timeout)
102 {
103 zassert_equal(k_sem_take(&ot_sem, timeout), 0, "Sem not released.");
104 }
105
106 static otRadioFrame otPlatRadioReceiveDone_expected_aframe;
107 static otError otPlatRadioReceiveDone_expected_error;
otPlatRadioReceiveDone(otInstance * aInstance,otRadioFrame * aFrame,otError aError)108 void otPlatRadioReceiveDone(otInstance *aInstance, otRadioFrame *aFrame, otError aError)
109 {
110 zassert_equal(aInstance, ot, "Incorrect instance.");
111 zassert_equal(otPlatRadioReceiveDone_expected_aframe.mChannel, aFrame->mChannel);
112 zassert_equal(otPlatRadioReceiveDone_expected_aframe.mLength, aFrame->mLength);
113 zassert_mem_equal(otPlatRadioReceiveDone_expected_aframe.mPsdu, aFrame->mPsdu,
114 aFrame->mLength, NULL);
115 zassert_equal(otPlatRadioReceiveDone_expected_error, aError);
116 }
117
118 FAKE_VOID_FUNC(otPlatRadioTxDone, otInstance *, otRadioFrame *, otRadioFrame *, otError);
119
get_capabilities(const struct device * dev)120 static enum ieee802154_hw_caps get_capabilities(const struct device *dev)
121 {
122 enum ieee802154_hw_caps caps;
123
124 zassert_equal(dev, radio, "Device handle incorrect.");
125
126 caps = IEEE802154_HW_FCS | IEEE802154_HW_TX_RX_ACK | IEEE802154_HW_FILTER |
127 IEEE802154_HW_ENERGY_SCAN | IEEE802154_HW_SLEEP_TO_TX;
128 if (IS_ENABLED(CONFIG_NET_PKT_TXTIME)) {
129 caps |= IEEE802154_HW_TXTIME;
130 }
131 return caps;
132 }
133
134 FAKE_VALUE_FUNC(otError, otIp6Send, otInstance *, otMessage *);
135
otIp6NewMessage(otInstance * aInstance,const otMessageSettings * aSettings)136 otMessage *otIp6NewMessage(otInstance *aInstance, const otMessageSettings *aSettings)
137 {
138 zassert_equal(aInstance, ot, "Incorrect instance.");
139 return ip_msg;
140 }
141
142 FAKE_VALUE_FUNC(otError, otMessageAppend, otMessage *, const void *, uint16_t);
143
144 FAKE_VOID_FUNC(otMessageFree, otMessage *);
145
otPlatRadioTxStarted(otInstance * aInstance,otRadioFrame * aFrame)146 void otPlatRadioTxStarted(otInstance *aInstance, otRadioFrame *aFrame)
147 {
148 zassert_equal(aInstance, ot, "Incorrect instance.");
149 }
150
151 /**
152 * @brief Test for immediate energy scan
153 * Tests for case when radio energy scan returns success at the first call.
154 *
155 */
ZTEST(openthread_radio,test_energy_scan_immediate_test)156 ZTEST(openthread_radio, test_energy_scan_immediate_test)
157 {
158 const uint8_t chan = 10;
159 const uint8_t dur = 100;
160 const int16_t energy = -94;
161
162 set_channel_mock_fake.return_val = 0;
163
164 scan_mock_fake.return_val = 0;
165 zassert_equal(otPlatRadioEnergyScan(ot, chan, dur), OT_ERROR_NONE,
166 "Energy scan returned error.");
167 zassert_equal(1, scan_mock_fake.call_count);
168 zassert_equal(dur, scan_mock_fake.arg1_val);
169 zassert_not_null(scan_mock_fake.arg2_val, "Scan callback not specified.");
170 zassert_equal(1, set_channel_mock_fake.call_count);
171 zassert_equal(chan, set_channel_mock_fake.arg1_val);
172
173 scan_mock_fake.arg2_val(radio, energy);
174 make_sure_sem_set(K_NO_WAIT);
175
176 platformRadioProcess(ot);
177 zassert_equal(1, otPlatRadioEnergyScanDone_fake.call_count);
178 zassert_equal_ptr(ot, otPlatRadioEnergyScanDone_fake.arg0_val, NULL);
179 zassert_equal(energy, otPlatRadioEnergyScanDone_fake.arg1_val);
180 }
181
182 /**
183 * @brief Test for delayed energy scan
184 * Tests for case when radio returns not being able to start energy scan and
185 * the scan should be scheduled for later.
186 *
187 */
ZTEST(openthread_radio,test_energy_scan_delayed_test)188 ZTEST(openthread_radio, test_energy_scan_delayed_test)
189 {
190 const uint8_t chan = 10;
191 const uint8_t dur = 100;
192 const int16_t energy = -94;
193
194 /* request scan */
195 set_channel_mock_fake.return_val = 0;
196 scan_mock_fake.return_val = -EBUSY;
197
198 zassert_equal(otPlatRadioEnergyScan(ot, chan, dur), OT_ERROR_NONE,
199 "Energy scan returned error.");
200 zassert_equal(1, scan_mock_fake.call_count);
201 zassert_equal(dur, scan_mock_fake.arg1_val);
202 zassert_not_null(scan_mock_fake.arg2_val, "Scan callback not specified.");
203 zassert_equal(1, set_channel_mock_fake.call_count);
204 zassert_equal(chan, set_channel_mock_fake.arg1_val);
205 make_sure_sem_set(K_NO_WAIT);
206
207 /* process reported event */
208 RESET_FAKE(scan_mock);
209 RESET_FAKE(set_channel_mock);
210 FFF_RESET_HISTORY();
211
212 scan_mock_fake.return_val = 0;
213 set_channel_mock_fake.return_val = 0;
214
215 platformRadioProcess(ot);
216 zassert_equal(1, scan_mock_fake.call_count);
217 zassert_equal(dur, scan_mock_fake.arg1_val);
218 zassert_not_null(scan_mock_fake.arg2_val, "Scan callback not specified.");
219 zassert_equal(1, set_channel_mock_fake.call_count);
220 zassert_equal(chan, set_channel_mock_fake.arg1_val);
221
222 /* invoke scan done */
223 scan_mock_fake.arg2_val(radio, energy);
224 make_sure_sem_set(K_NO_WAIT);
225
226 platformRadioProcess(ot);
227 zassert_equal(1, otPlatRadioEnergyScanDone_fake.call_count);
228 zassert_equal_ptr(ot, otPlatRadioEnergyScanDone_fake.arg0_val, NULL);
229 zassert_equal(energy, otPlatRadioEnergyScanDone_fake.arg1_val);
230 }
231
create_ack_frame(void)232 static void create_ack_frame(void)
233 {
234 struct net_pkt *packet;
235 struct net_buf *buf;
236 const uint8_t lqi = 230;
237 const int8_t rssi = -80;
238
239 packet = net_pkt_alloc(K_NO_WAIT);
240 buf = net_pkt_get_reserve_tx_data(ACK_PKT_LENGTH, K_NO_WAIT);
241 net_pkt_append_buffer(packet, buf);
242
243 buf->len = ACK_PKT_LENGTH;
244 buf->data[0] = FRAME_TYPE_ACK;
245
246 net_pkt_set_ieee802154_rssi_dbm(packet, rssi);
247 net_pkt_set_ieee802154_lqi(packet, lqi);
248 zassert_equal(ieee802154_handle_ack(NULL, packet), NET_OK, "Handling ack failed.");
249 net_pkt_unref(packet);
250 }
251
252 /**
253 * @brief Test for tx data handling
254 * Tests if OT frame is correctly passed to the radio driver.
255 * Additionally verifies ACK frame passing back to the OT.
256 *
257 */
ZTEST(openthread_radio,test_tx_test)258 ZTEST(openthread_radio, test_tx_test)
259 {
260 const uint8_t chan = 20;
261 uint8_t chan2 = chan - 1;
262 const int8_t power = -3;
263 net_time_t expected_target_time = 0;
264
265 otRadioFrame *frm = otPlatRadioGetTransmitBuffer(ot);
266
267 zassert_not_null(frm, "Transmit buffer is null.");
268
269 zassert_equal(otPlatRadioSetTransmitPower(ot, power), OT_ERROR_NONE,
270 "Failed to set TX power.");
271
272 set_channel_mock_fake.return_val = 0;
273 zassert_equal(otPlatRadioReceive(ot, chan), OT_ERROR_NONE, "Failed to receive.");
274 zassert_equal(1, set_channel_mock_fake.call_count);
275 zassert_equal(chan, set_channel_mock_fake.arg1_val);
276 zassert_equal(1, set_txpower_mock_fake.call_count);
277 zassert_equal(power, set_txpower_mock_fake.arg1_val);
278 zassert_equal(1, start_mock_fake.call_count);
279 zassert_equal_ptr(radio, start_mock_fake.arg0_val, NULL);
280 RESET_FAKE(set_channel_mock);
281 RESET_FAKE(set_txpower_mock);
282 RESET_FAKE(start_mock);
283 FFF_RESET_HISTORY();
284
285 if (IS_ENABLED(CONFIG_NET_PKT_TXTIME)) {
286 /* cover dealing with wrapped scheduling time:
287 * current time: (UINT32_MAX + 1) us
288 * target time wrapped: (3 + 5) us, unwrapped: (UINT32_MAX + 3 + 5) us
289 */
290 get_time_mock_fake.return_val = (int64_t)UINT32_MAX * NSEC_PER_USEC + 1000;
291 frm->mInfo.mTxInfo.mTxDelayBaseTime = 3U;
292 frm->mInfo.mTxInfo.mTxDelay = 5U;
293 expected_target_time =
294 get_time_mock_fake.return_val +
295 (frm->mInfo.mTxInfo.mTxDelayBaseTime + frm->mInfo.mTxInfo.mTxDelay) *
296 NSEC_PER_USEC;
297 }
298
299 /* ACKed frame */
300 frm->mChannel = chan2;
301 frm->mInfo.mTxInfo.mCsmaCaEnabled = true;
302 frm->mPsdu[0] = IEEE802154_AR_FLAG_SET;
303 set_channel_mock_fake.return_val = 0;
304 zassert_equal(otPlatRadioTransmit(ot, frm), OT_ERROR_NONE, "Transmit failed.");
305 k_yield();
306
307 create_ack_frame();
308 make_sure_sem_set(Z_TIMEOUT_MS(100));
309
310 platformRadioProcess(ot);
311 zassert_equal(1, set_channel_mock_fake.call_count);
312 zassert_equal(chan2, set_channel_mock_fake.arg1_val);
313 if (IS_ENABLED(CONFIG_NET_PKT_TXTIME)) {
314 zassert_equal(0, cca_mock_fake.call_count);
315 } else {
316 zassert_equal(1, cca_mock_fake.call_count);
317 zassert_equal_ptr(radio, cca_mock_fake.arg0_val, NULL);
318 }
319 zassert_equal(1, set_txpower_mock_fake.call_count);
320 zassert_equal(power, set_txpower_mock_fake.arg1_val);
321 zassert_equal(1, tx_mock_fake.call_count);
322 zassert_equal_ptr(frm->mPsdu, tx_mock_fake.arg3_val->data, NULL);
323 zassert_equal(expected_target_time, net_pkt_timestamp_ns(tx_mock_fake.arg2_val));
324 zassert_equal(IS_ENABLED(CONFIG_NET_PKT_TXTIME) ? IEEE802154_TX_MODE_TXTIME_CCA
325 : IEEE802154_TX_MODE_DIRECT,
326 tx_mock_fake.arg1_val);
327 zassert_equal(1, otPlatRadioTxDone_fake.call_count);
328 zassert_equal_ptr(ot, otPlatRadioTxDone_fake.arg0_val, NULL);
329 zassert_equal(OT_ERROR_NONE, otPlatRadioTxDone_fake.arg3_val);
330 RESET_FAKE(set_channel_mock);
331 RESET_FAKE(set_txpower_mock);
332 RESET_FAKE(tx_mock);
333 RESET_FAKE(otPlatRadioTxDone);
334 FFF_RESET_HISTORY();
335
336 /* Non-ACKed frame */
337 frm->mChannel = --chan2;
338 frm->mInfo.mTxInfo.mCsmaCaEnabled = false;
339 frm->mPsdu[0] = 0;
340
341 set_channel_mock_fake.return_val = 0;
342 zassert_equal(otPlatRadioTransmit(ot, frm), OT_ERROR_NONE, "Transmit failed.");
343 make_sure_sem_set(Z_TIMEOUT_MS(100));
344 platformRadioProcess(ot);
345 zassert_equal(1, set_channel_mock_fake.call_count);
346 zassert_equal(chan2, set_channel_mock_fake.arg1_val);
347 zassert_equal(1, set_txpower_mock_fake.call_count);
348 zassert_equal(power, set_txpower_mock_fake.arg1_val);
349 zassert_equal(1, tx_mock_fake.call_count);
350 zassert_equal_ptr(frm->mPsdu, tx_mock_fake.arg3_val->data, NULL);
351 zassert_equal(1, otPlatRadioTxDone_fake.call_count);
352 zassert_equal_ptr(ot, otPlatRadioTxDone_fake.arg0_val, NULL);
353 zassert_equal(OT_ERROR_NONE, otPlatRadioTxDone_fake.arg3_val);
354 }
355
356 /**
357 * @brief Test for tx power setting
358 * Tests if tx power requested by the OT is correctly passed to the radio.
359 *
360 */
ZTEST(openthread_radio,test_tx_power_test)361 ZTEST(openthread_radio, test_tx_power_test)
362 {
363 int8_t out_power = 0;
364
365 zassert_equal(otPlatRadioSetTransmitPower(ot, -3), OT_ERROR_NONE,
366 "Failed to set TX power.");
367 zassert_equal(otPlatRadioGetTransmitPower(ot, &out_power), OT_ERROR_NONE,
368 "Failed to obtain TX power.");
369 zassert_equal(out_power, -3, "Got different power than set.");
370 zassert_equal(otPlatRadioSetTransmitPower(ot, -6), OT_ERROR_NONE,
371 "Failed to set TX power.");
372 zassert_equal(otPlatRadioGetTransmitPower(ot, &out_power), OT_ERROR_NONE,
373 "Failed to obtain TX power.");
374 zassert_equal(out_power, -6, "Second call to otPlatRadioSetTransmitPower failed.");
375 }
376
377 /**
378 * @brief Test for getting radio sensitivity
379 * There is no api to get radio sensitivity from the radio so the value is
380 * hardcoded in radio.c. Test only verifies if the value returned makes any
381 * sense.
382 *
383 */
ZTEST(openthread_radio,test_sensitivity_test)384 ZTEST(openthread_radio, test_sensitivity_test)
385 {
386 /*
387 * Nothing to test actually as this is constant 100.
388 * When radio interface will be extended to get sensitivity this test
389 * can be extended with the radio api call. For now just verify if the
390 * value is reasonable.
391 */
392 zassert_true(-80 > otPlatRadioGetReceiveSensitivity(ot), "Radio sensitivity not in range.");
393 }
394
395 static enum ieee802154_config_type custom_configure_match_mock_expected_type;
396 static struct ieee802154_config custom_configure_match_mock_expected_config;
custom_configure_match_mock(const struct device * dev,enum ieee802154_config_type type,const struct ieee802154_config * config)397 static int custom_configure_match_mock(const struct device *dev, enum ieee802154_config_type type,
398 const struct ieee802154_config *config)
399 {
400 zassert_equal_ptr(dev, radio, "Device handle incorrect.");
401 zassert_equal(custom_configure_match_mock_expected_type, type);
402 switch (type) {
403 case IEEE802154_CONFIG_AUTO_ACK_FPB:
404 zassert_equal(custom_configure_match_mock_expected_config.auto_ack_fpb.mode,
405 config->auto_ack_fpb.mode, NULL);
406 zassert_equal(custom_configure_match_mock_expected_config.auto_ack_fpb.enabled,
407 config->auto_ack_fpb.enabled, NULL);
408 break;
409 case IEEE802154_CONFIG_ACK_FPB:
410 zassert_equal(custom_configure_match_mock_expected_config.ack_fpb.extended,
411 config->ack_fpb.extended, NULL);
412 zassert_equal(custom_configure_match_mock_expected_config.ack_fpb.enabled,
413 config->ack_fpb.enabled, NULL);
414 if (custom_configure_match_mock_expected_config.ack_fpb.addr == NULL) {
415 zassert_is_null(config->ack_fpb.addr, NULL);
416 } else {
417 zassert_mem_equal(custom_configure_match_mock_expected_config.ack_fpb.addr,
418 config->ack_fpb.addr,
419 (config->ack_fpb.extended) ? sizeof(otExtAddress) : 2,
420 NULL);
421 }
422 break;
423 default:
424 zassert_unreachable("Unexpected config type %d.", type);
425 break;
426 }
427
428 return 0;
429 }
set_expected_match_values(enum ieee802154_config_type type,uint8_t * addr,bool extended,bool enabled)430 static void set_expected_match_values(enum ieee802154_config_type type, uint8_t *addr,
431 bool extended, bool enabled)
432 {
433 custom_configure_match_mock_expected_type = type;
434 switch (type) {
435 case IEEE802154_CONFIG_AUTO_ACK_FPB:
436 custom_configure_match_mock_expected_config.auto_ack_fpb.enabled = enabled;
437 custom_configure_match_mock_expected_config.auto_ack_fpb.mode =
438 IEEE802154_FPB_ADDR_MATCH_THREAD;
439 break;
440 case IEEE802154_CONFIG_ACK_FPB:
441 custom_configure_match_mock_expected_config.ack_fpb.extended = extended;
442 custom_configure_match_mock_expected_config.ack_fpb.enabled = enabled;
443 custom_configure_match_mock_expected_config.ack_fpb.addr = addr;
444 break;
445 default:
446 break;
447 }
448 }
449
450 /**
451 * @brief Test different types of OT source match.
452 * Tests if Enable, Disable, Add and Clear Source Match calls are passed to the
453 * radio driver correctly.
454 *
455 */
ZTEST(openthread_radio,test_source_match_test)456 ZTEST(openthread_radio, test_source_match_test)
457 {
458 otExtAddress ext_addr;
459 configure_mock_fake.custom_fake = custom_configure_match_mock;
460
461 /* Enable/Disable */
462 set_expected_match_values(IEEE802154_CONFIG_AUTO_ACK_FPB, NULL, false, true);
463 otPlatRadioEnableSrcMatch(ot, true);
464
465 set_expected_match_values(IEEE802154_CONFIG_AUTO_ACK_FPB, NULL, false, false);
466 otPlatRadioEnableSrcMatch(ot, false);
467
468 set_expected_match_values(IEEE802154_CONFIG_AUTO_ACK_FPB, NULL, false, true);
469 otPlatRadioEnableSrcMatch(ot, true);
470
471 /* Add */
472 sys_put_le16(12345, ext_addr.m8);
473 set_expected_match_values(IEEE802154_CONFIG_ACK_FPB, ext_addr.m8, false, true);
474 zassert_equal(otPlatRadioAddSrcMatchShortEntry(ot, 12345), OT_ERROR_NONE,
475 "Failed to add short src entry.");
476
477 for (int i = 0; i < sizeof(ext_addr.m8); i++) {
478 ext_addr.m8[i] = i;
479 }
480 set_expected_match_values(IEEE802154_CONFIG_ACK_FPB, ext_addr.m8, true, true);
481 zassert_equal(otPlatRadioAddSrcMatchExtEntry(ot, &ext_addr), OT_ERROR_NONE,
482 "Failed to add ext src entry.");
483
484 /* Clear */
485 sys_put_le16(12345, ext_addr.m8);
486 set_expected_match_values(IEEE802154_CONFIG_ACK_FPB, ext_addr.m8, false, false);
487 zassert_equal(otPlatRadioClearSrcMatchShortEntry(ot, 12345), OT_ERROR_NONE,
488 "Failed to clear short src entry.");
489
490 set_expected_match_values(IEEE802154_CONFIG_ACK_FPB, ext_addr.m8, true, false);
491 zassert_equal(otPlatRadioClearSrcMatchExtEntry(ot, &ext_addr), OT_ERROR_NONE,
492 "Failed to clear ext src entry.");
493
494 set_expected_match_values(IEEE802154_CONFIG_ACK_FPB, NULL, false, false);
495 otPlatRadioClearSrcMatchShortEntries(ot);
496
497 set_expected_match_values(IEEE802154_CONFIG_ACK_FPB, NULL, true, false);
498 otPlatRadioClearSrcMatchExtEntries(ot);
499 }
500
501 static bool custom_configure_promiscuous_mock_promiscuous;
custom_configure_promiscuous_mock(const struct device * dev,enum ieee802154_config_type type,const struct ieee802154_config * config)502 static int custom_configure_promiscuous_mock(const struct device *dev,
503 enum ieee802154_config_type type,
504 const struct ieee802154_config *config)
505 {
506 zassert_equal(dev, radio, "Device handle incorrect.");
507 zassert_equal(type, IEEE802154_CONFIG_PROMISCUOUS, "Config type incorrect.");
508 custom_configure_promiscuous_mock_promiscuous = config->promiscuous;
509
510 return 0;
511 }
512 /**
513 * @brief Test for enabling or disabling promiscuous mode
514 * Tests if OT can successfully enable or disable promiscuous mode.
515 *
516 */
ZTEST(openthread_radio,test_promiscuous_mode_set_test)517 ZTEST(openthread_radio, test_promiscuous_mode_set_test)
518 {
519 zassert_false(otPlatRadioGetPromiscuous(ot),
520 "By default promiscuous mode shall be disabled.");
521
522 configure_mock_fake.custom_fake = custom_configure_promiscuous_mock;
523 otPlatRadioSetPromiscuous(ot, true);
524 zassert_true(otPlatRadioGetPromiscuous(ot), "Mode not enabled.");
525 zassert_equal(1, configure_mock_fake.call_count);
526 zassert_true(custom_configure_promiscuous_mock_promiscuous);
527
528 RESET_FAKE(configure_mock);
529
530 configure_mock_fake.custom_fake = custom_configure_promiscuous_mock;
531 otPlatRadioSetPromiscuous(ot, false);
532 zassert_false(otPlatRadioGetPromiscuous(ot), "Mode still enabled.");
533 zassert_equal(1, configure_mock_fake.call_count);
534 zassert_false(custom_configure_promiscuous_mock_promiscuous);
535 }
536
537 /**
538 * @brief Test of proper radio to OT capabilities mapping
539 * Tests if different radio capabilities map for their corresponding OpenThread
540 * capability
541 */
ZTEST(openthread_radio,test_get_caps_test)542 ZTEST(openthread_radio, test_get_caps_test)
543 {
544 rapi.get_capabilities = get_capabilities_caps_mock;
545
546 /* no caps */
547 get_capabilities_caps_mock_fake.return_val = 0;
548 zassert_equal(otPlatRadioGetCaps(ot), OT_RADIO_CAPS_NONE,
549 "Incorrect capabilities returned.");
550
551 /* not used by OT */
552 get_capabilities_caps_mock_fake.return_val = IEEE802154_HW_FCS;
553 zassert_equal(otPlatRadioGetCaps(ot), OT_RADIO_CAPS_NONE,
554 "Incorrect capabilities returned.");
555
556 /* not implemented or not fully supported */
557 get_capabilities_caps_mock_fake.return_val = IEEE802154_HW_PROMISC;
558 zassert_equal(otPlatRadioGetCaps(ot), OT_RADIO_CAPS_NONE,
559 "Incorrect capabilities returned.");
560
561 /* proper mapping */
562 get_capabilities_caps_mock_fake.return_val = IEEE802154_HW_CSMA;
563 zassert_equal(otPlatRadioGetCaps(ot), OT_RADIO_CAPS_CSMA_BACKOFF,
564 "Incorrect capabilities returned.");
565
566 get_capabilities_caps_mock_fake.return_val = IEEE802154_HW_ENERGY_SCAN;
567 zassert_equal(otPlatRadioGetCaps(ot), OT_RADIO_CAPS_ENERGY_SCAN,
568 "Incorrect capabilities returned.");
569
570 get_capabilities_caps_mock_fake.return_val = IEEE802154_HW_TX_RX_ACK;
571 zassert_equal(otPlatRadioGetCaps(ot), OT_RADIO_CAPS_ACK_TIMEOUT,
572 "Incorrect capabilities returned.");
573
574 get_capabilities_caps_mock_fake.return_val = IEEE802154_HW_TXTIME;
575 zassert_equal(otPlatRadioGetCaps(ot),
576 IS_ENABLED(CONFIG_NET_PKT_TXTIME) ? OT_RADIO_CAPS_TRANSMIT_TIMING
577 : OT_RADIO_CAPS_NONE,
578 "Incorrect capabilities returned.");
579
580 get_capabilities_caps_mock_fake.return_val = IEEE802154_HW_SLEEP_TO_TX;
581 zassert_equal(otPlatRadioGetCaps(ot), OT_RADIO_CAPS_SLEEP_TO_TX,
582 "Incorrect capabilities returned.");
583
584 /* all at once */
585 get_capabilities_caps_mock_fake.return_val =
586 IEEE802154_HW_FCS | IEEE802154_HW_PROMISC | IEEE802154_HW_FILTER |
587 IEEE802154_HW_CSMA | IEEE802154_HW_TX_RX_ACK | IEEE802154_HW_ENERGY_SCAN |
588 IEEE802154_HW_TXTIME | IEEE802154_HW_SLEEP_TO_TX;
589 zassert_equal(
590 otPlatRadioGetCaps(ot),
591 OT_RADIO_CAPS_CSMA_BACKOFF | OT_RADIO_CAPS_ENERGY_SCAN | OT_RADIO_CAPS_ACK_TIMEOUT |
592 OT_RADIO_CAPS_SLEEP_TO_TX |
593 (IS_ENABLED(CONFIG_NET_PKT_TXTIME) ? OT_RADIO_CAPS_TRANSMIT_TIMING : 0),
594 "Incorrect capabilities returned.");
595
596 rapi.get_capabilities = get_capabilities;
597 }
598
599 /**
600 * @brief Test for getting the rssi value from the radio
601 * Tests if correct value is returned from the otPlatRadioGetRssi function.
602 *
603 */
ZTEST(openthread_radio,test_get_rssi_test)604 ZTEST(openthread_radio, test_get_rssi_test)
605 {
606 const int8_t rssi = -103;
607
608 rapi.ed_scan = rssi_scan_mock;
609
610 rssi_scan_mock_max_ed = rssi;
611 zassert_equal(otPlatRadioGetRssi(ot), rssi, "Invalid RSSI value received.");
612
613 rapi.ed_scan = scan_mock;
614 }
615
616 /**
617 * @brief Test switching between radio states
618 * Tests if radio is correctly switched between states.
619 *
620 */
ZTEST(openthread_radio,test_radio_state_test)621 ZTEST(openthread_radio, test_radio_state_test)
622 {
623 const uint8_t channel = 12;
624 const uint8_t power = 10;
625
626 zassert_equal(otPlatRadioSetTransmitPower(ot, power), OT_ERROR_NONE,
627 "Failed to set TX power.");
628 zassert_equal(otPlatRadioDisable(ot), OT_ERROR_NONE, "Failed to disable radio.");
629
630 zassert_false(otPlatRadioIsEnabled(ot), "Radio reports as enabled.");
631
632 zassert_equal(otPlatRadioSleep(ot), OT_ERROR_INVALID_STATE,
633 "Changed to sleep regardless being disabled.");
634
635 zassert_equal(otPlatRadioEnable(ot), OT_ERROR_NONE, "Enabling radio failed.");
636
637 zassert_true(otPlatRadioIsEnabled(ot), "Radio reports disabled.");
638
639 zassert_equal(otPlatRadioSleep(ot), OT_ERROR_NONE, "Failed to switch to sleep mode.");
640
641 zassert_true(otPlatRadioIsEnabled(ot), "Radio reports as disabled.");
642
643 set_channel_mock_fake.return_val = 0;
644 zassert_equal(otPlatRadioReceive(ot, channel), OT_ERROR_NONE, "Failed to receive.");
645 zassert_equal(platformRadioChannelGet(ot), channel, "Channel number not remembered.");
646
647 zassert_true(otPlatRadioIsEnabled(ot), "Radio reports as disabled.");
648 zassert_equal(1, set_channel_mock_fake.call_count);
649 zassert_equal(channel, set_channel_mock_fake.arg1_val);
650 zassert_equal(1, set_txpower_mock_fake.call_count);
651 zassert_equal(power, set_txpower_mock_fake.arg1_val);
652 zassert_equal(1, start_mock_fake.call_count);
653 zassert_equal_ptr(radio, start_mock_fake.arg0_val, NULL);
654 zassert_equal(1, stop_mock_fake.call_count);
655 zassert_equal_ptr(radio, stop_mock_fake.arg0_val, NULL);
656 }
657
658 static uint16_t custom_filter_mock_pan_id;
659 static uint16_t custom_filter_mock_short_addr;
660 static uint8_t *custom_filter_mock_ieee_addr;
custom_filter_mock(const struct device * dev,bool set,enum ieee802154_filter_type type,const struct ieee802154_filter * filter)661 static int custom_filter_mock(const struct device *dev, bool set, enum ieee802154_filter_type type,
662 const struct ieee802154_filter *filter)
663 {
664 switch (type) {
665 case IEEE802154_FILTER_TYPE_IEEE_ADDR:
666 custom_filter_mock_ieee_addr = filter->ieee_addr;
667 break;
668 case IEEE802154_FILTER_TYPE_SHORT_ADDR:
669 custom_filter_mock_short_addr = filter->short_addr;
670 break;
671 case IEEE802154_FILTER_TYPE_PAN_ID:
672 custom_filter_mock_pan_id = filter->pan_id;
673 break;
674 default:
675 zassert_false(true, "Type not supported in mock: %d.", type);
676 break;
677 }
678 return 0;
679 }
680
681 /**
682 * @brief Test address filtering
683 * Tests if short, extended address and PanID are correctly passed to the radio
684 * driver.
685 *
686 */
ZTEST(openthread_radio,test_address_test)687 ZTEST(openthread_radio, test_address_test)
688 {
689 const uint16_t pan_id = 0xDEAD;
690 const uint16_t short_add = 0xCAFE;
691 otExtAddress ieee_addr;
692
693 for (int i = 0; i < sizeof(ieee_addr.m8); i++) {
694 ieee_addr.m8[i] = 'a' + i;
695 }
696
697 filter_mock_fake.custom_fake = custom_filter_mock;
698 otPlatRadioSetPanId(ot, pan_id);
699 zassert_equal(1, filter_mock_fake.call_count);
700 zassert_true(filter_mock_fake.arg1_val);
701 zassert_equal(IEEE802154_FILTER_TYPE_PAN_ID, filter_mock_fake.arg2_val);
702 zassert_equal(pan_id, custom_filter_mock_pan_id);
703 RESET_FAKE(filter_mock);
704 FFF_RESET_HISTORY();
705
706 filter_mock_fake.custom_fake = custom_filter_mock;
707 otPlatRadioSetShortAddress(ot, short_add);
708 zassert_equal(1, filter_mock_fake.call_count);
709 zassert_true(filter_mock_fake.arg1_val);
710 zassert_equal(IEEE802154_FILTER_TYPE_SHORT_ADDR, filter_mock_fake.arg2_val);
711 zassert_equal(short_add, custom_filter_mock_short_addr);
712 RESET_FAKE(filter_mock);
713 FFF_RESET_HISTORY();
714
715 filter_mock_fake.custom_fake = custom_filter_mock;
716 otPlatRadioSetExtendedAddress(ot, &ieee_addr);
717 zassert_equal(1, filter_mock_fake.call_count);
718 zassert_true(filter_mock_fake.arg1_val);
719 zassert_equal(IEEE802154_FILTER_TYPE_IEEE_ADDR, filter_mock_fake.arg2_val);
720 zassert_mem_equal(ieee_addr.m8, custom_filter_mock_ieee_addr, OT_EXT_ADDRESS_SIZE, NULL);
721 }
722
alloc_pkt(struct net_pkt ** out_packet,uint8_t buf_ct,uint8_t offset)723 uint8_t alloc_pkt(struct net_pkt **out_packet, uint8_t buf_ct, uint8_t offset)
724 {
725 struct net_pkt *packet;
726 struct net_buf *buf;
727 uint8_t len = 0;
728 uint8_t buf_num;
729
730 packet = net_pkt_alloc(K_NO_WAIT);
731 for (buf_num = 0; buf_num < buf_ct; buf_num++) {
732 buf = net_pkt_get_reserve_tx_data(IEEE802154_MAX_PHY_PACKET_SIZE,
733 K_NO_WAIT);
734 net_pkt_append_buffer(packet, buf);
735
736 for (int i = 0; i < buf->size; i++) {
737 buf->data[i] = (offset + i + buf_num) & 0xFF;
738 }
739
740 len = buf->size - 3;
741 buf->len = len;
742 }
743
744 *out_packet = packet;
745 return len;
746 }
747
748 /**
749 * @brief Test received messages handling.
750 * Tests if received frames are properly passed to the OpenThread
751 *
752 */
ZTEST(openthread_radio,test_receive_test)753 ZTEST(openthread_radio, test_receive_test)
754 {
755 struct net_pkt *packet;
756 struct net_buf *buf;
757 const uint8_t channel = 21;
758 const int8_t power = -5;
759 const uint8_t lqi = 240;
760 const int8_t rssi = -90;
761 uint8_t len;
762
763 len = alloc_pkt(&packet, 1, 'a');
764 buf = packet->buffer;
765
766 net_pkt_set_ieee802154_lqi(packet, lqi);
767 net_pkt_set_ieee802154_rssi_dbm(packet, rssi);
768
769 zassert_equal(otPlatRadioSetTransmitPower(ot, power), OT_ERROR_NONE,
770 "Failed to set TX power.");
771
772 set_channel_mock_fake.return_val = 0;
773 zassert_equal(otPlatRadioReceive(ot, channel), OT_ERROR_NONE, "Failed to receive.");
774 zassert_equal(1, set_channel_mock_fake.call_count);
775 zassert_equal(channel, set_channel_mock_fake.arg1_val);
776 zassert_equal(1, set_txpower_mock_fake.call_count);
777 zassert_equal(power, set_txpower_mock_fake.arg1_val);
778 zassert_equal(1, start_mock_fake.call_count);
779 zassert_equal_ptr(radio, start_mock_fake.arg0_val, NULL);
780
781 /*
782 * Not setting any expect values as nothing shall be called from
783 * notify_new_rx_frame calling thread. OT functions can be called only
784 * after semaphore for main thread is released.
785 */
786 notify_new_rx_frame(packet);
787
788 make_sure_sem_set(Z_TIMEOUT_MS(100));
789 otPlatRadioReceiveDone_expected_error = OT_ERROR_NONE;
790 otPlatRadioReceiveDone_expected_aframe.mChannel = channel;
791 otPlatRadioReceiveDone_expected_aframe.mLength = len;
792 otPlatRadioReceiveDone_expected_aframe.mPsdu = buf->data;
793 platformRadioProcess(ot);
794 }
795
796 /**
797 * @brief Test received messages handling.
798 * Tests if received frames are properly passed to the OpenThread
799 *
800 */
ZTEST(openthread_radio,test_net_pkt_transmit)801 ZTEST(openthread_radio, test_net_pkt_transmit)
802 {
803 void *expected_data_ptrs[2];
804 struct net_pkt *packet;
805 struct net_buf *buf;
806 const uint8_t channel = 21;
807 const int8_t power = -5;
808 uint8_t len;
809
810 /* success */
811 len = alloc_pkt(&packet, 2, 'a');
812 buf = packet->buffer;
813 zassert_equal(otPlatRadioSetTransmitPower(ot, power), OT_ERROR_NONE,
814 "Failed to set TX power.");
815
816 set_channel_mock_fake.return_val = 0;
817 zassert_equal(otPlatRadioReceive(ot, channel), OT_ERROR_NONE, "Failed to receive.");
818 zassert_equal(1, set_channel_mock_fake.call_count);
819 zassert_equal(channel, set_channel_mock_fake.arg1_val);
820 zassert_equal(1, set_txpower_mock_fake.call_count);
821 zassert_equal(power, set_txpower_mock_fake.arg1_val);
822 zassert_equal(1, start_mock_fake.call_count);
823 zassert_equal_ptr(radio, start_mock_fake.arg0_val, NULL);
824
825 notify_new_tx_frame(packet);
826
827 make_sure_sem_set(Z_TIMEOUT_MS(100));
828
829 otMessageAppend_fake.return_val = OT_ERROR_NONE;
830 otIp6Send_fake.return_val = OT_ERROR_NONE;
831
832 /* Do not expect free in case of success */
833
834 expected_data_ptrs[0] = buf->data;
835 expected_data_ptrs[1] = buf->frags->data;
836 platformRadioProcess(ot);
837 zassert_equal(2, otMessageAppend_fake.call_count);
838 zassert_equal_ptr(ip_msg, otMessageAppend_fake.arg0_history[0], NULL);
839 zassert_equal_ptr(ip_msg, otMessageAppend_fake.arg0_history[1], NULL);
840 zassert_equal_ptr(expected_data_ptrs[0], otMessageAppend_fake.arg1_history[0], NULL);
841 zassert_equal_ptr(expected_data_ptrs[1], otMessageAppend_fake.arg1_history[1], NULL);
842 zassert_equal(len, otMessageAppend_fake.arg2_history[0]);
843 zassert_equal(len, otMessageAppend_fake.arg2_history[1]);
844 zassert_equal(1, otIp6Send_fake.call_count);
845 zassert_equal_ptr(ot, otIp6Send_fake.arg0_val, NULL);
846 zassert_equal_ptr(ip_msg, otIp6Send_fake.arg1_val, NULL);
847
848 RESET_FAKE(otMessageAppend);
849 RESET_FAKE(otIp6Send);
850 FFF_RESET_HISTORY();
851
852 /* fail on append */
853 len = alloc_pkt(&packet, 2, 'b');
854 buf = packet->buffer;
855
856 notify_new_tx_frame(packet);
857
858 make_sure_sem_set(Z_TIMEOUT_MS(100));
859
860 otMessageAppend_fake.return_val = OT_ERROR_NO_BUFS;
861 expected_data_ptrs[0] = buf->data;
862
863 platformRadioProcess(ot);
864 zassert_equal(1, otMessageAppend_fake.call_count);
865 zassert_equal_ptr(ip_msg, otMessageAppend_fake.arg0_val, NULL);
866 zassert_equal_ptr(expected_data_ptrs[0], otMessageAppend_fake.arg1_val, NULL);
867 zassert_equal(len, otMessageAppend_fake.arg2_val);
868 zassert_equal_ptr(ip_msg, otMessageFree_fake.arg0_val, NULL);
869
870 RESET_FAKE(otMessageAppend);
871 FFF_RESET_HISTORY();
872
873 /* fail on send */
874 len = alloc_pkt(&packet, 1, 'c');
875 buf = packet->buffer;
876
877 notify_new_tx_frame(packet);
878
879 make_sure_sem_set(Z_TIMEOUT_MS(100));
880
881 otMessageAppend_fake.return_val = OT_ERROR_NONE;
882 otIp6Send_fake.return_val = OT_ERROR_BUSY;
883 expected_data_ptrs[0] = buf->data;
884
885 /* Do not expect free in case of failure in send */
886
887 platformRadioProcess(ot);
888 zassert_equal(1, otMessageAppend_fake.call_count);
889 zassert_equal_ptr(ip_msg, otMessageAppend_fake.arg0_val, NULL);
890 zassert_equal_ptr(expected_data_ptrs[0], otMessageAppend_fake.arg1_val, NULL);
891 zassert_equal(len, otMessageAppend_fake.arg2_val);
892 zassert_equal(1, otIp6Send_fake.call_count);
893 zassert_equal_ptr(ot, otIp6Send_fake.arg0_val, NULL);
894 zassert_equal_ptr(ip_msg, otIp6Send_fake.arg1_val, NULL);
895 }
896
897 #ifdef CONFIG_OPENTHREAD_CSL_RECEIVER
898 static int64_t custom_configure_csl_rx_time_mock_csl_rx_time;
custom_configure_csl_rx_time(const struct device * dev,enum ieee802154_config_type type,const struct ieee802154_config * config)899 static int custom_configure_csl_rx_time(const struct device *dev,
900 enum ieee802154_config_type type,
901 const struct ieee802154_config *config)
902 {
903 zassert_equal(dev, radio, "Device handle incorrect.");
904 zassert_equal(type, IEEE802154_CONFIG_CSL_RX_TIME, "Config type incorrect.");
905 custom_configure_csl_rx_time_mock_csl_rx_time = config->csl_rx_time;
906
907 return 0;
908 }
909
ZTEST(openthread_radio,test_csl_receiver_sample_time)910 ZTEST(openthread_radio, test_csl_receiver_sample_time)
911 {
912 uint32_t sample_time = 50U;
913
914 configure_mock_fake.custom_fake = custom_configure_csl_rx_time;
915 otPlatRadioUpdateCslSampleTime(NULL, sample_time);
916 zassert_equal(1, configure_mock_fake.call_count);
917 zassert_equal(sample_time * NSEC_PER_USEC, custom_configure_csl_rx_time_mock_csl_rx_time);
918 }
919
920
921 static struct ieee802154_config custom_configure_rx_slot_mock_config;
custom_configure_csl_rx_slot(const struct device * dev,enum ieee802154_config_type type,const struct ieee802154_config * config)922 static int custom_configure_csl_rx_slot(const struct device *dev,
923 enum ieee802154_config_type type,
924 const struct ieee802154_config *config)
925 {
926 zassert_equal(dev, radio, "Device handle incorrect.");
927 zassert_equal(type, IEEE802154_CONFIG_RX_SLOT, "Config type incorrect.");
928 custom_configure_rx_slot_mock_config.rx_slot.channel = config->rx_slot.channel;
929 custom_configure_rx_slot_mock_config.rx_slot.start = config->rx_slot.start;
930 custom_configure_rx_slot_mock_config.rx_slot.duration = config->rx_slot.duration;
931
932 return 0;
933 }
934
ZTEST(openthread_radio,test_csl_receiver_receive_at)935 ZTEST(openthread_radio, test_csl_receiver_receive_at)
936 {
937 uint8_t channel = 11U;
938 uint32_t start = 1000U;
939 uint32_t duration = 100U;
940 int res;
941
942 configure_mock_fake.custom_fake = custom_configure_csl_rx_slot;
943 res = otPlatRadioReceiveAt(NULL, channel, start, duration);
944 zassert_ok(res);
945 zassert_equal(1, configure_mock_fake.call_count);
946 zassert_equal(channel, custom_configure_rx_slot_mock_config.rx_slot.channel);
947 zassert_equal(start * NSEC_PER_USEC, custom_configure_rx_slot_mock_config.rx_slot.start);
948 zassert_equal(duration * NSEC_PER_USEC,
949 custom_configure_rx_slot_mock_config.rx_slot.duration);
950 }
951 #endif
952
openthread_radio_setup(void)953 static void *openthread_radio_setup(void)
954 {
955 platformRadioInit();
956 return NULL;
957 }
958
openthread_radio_before(void * f)959 static void openthread_radio_before(void *f)
960 {
961 ARG_UNUSED(f);
962 RESET_FAKE(scan_mock);
963 RESET_FAKE(cca_mock);
964 RESET_FAKE(set_channel_mock);
965 RESET_FAKE(filter_mock);
966 RESET_FAKE(set_txpower_mock);
967 RESET_FAKE(tx_mock);
968 RESET_FAKE(start_mock);
969 RESET_FAKE(stop_mock);
970 RESET_FAKE(configure_mock);
971 RESET_FAKE(get_capabilities_caps_mock);
972 RESET_FAKE(otPlatRadioEnergyScanDone);
973 RESET_FAKE(otPlatRadioTxDone);
974 RESET_FAKE(otMessageFree);
975 FFF_RESET_HISTORY();
976 }
977
978 ZTEST_SUITE(openthread_radio, NULL, openthread_radio_setup, openthread_radio_before, NULL, NULL);
979