Boot Linux faster!

Check our new training course

Boot Linux faster!

Check our new training course
and Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

  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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/*
 * Copyright (c) 2020 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <ztest.h>

#include <zephyr.h>
#include <errno.h>
#include <stdio.h>

#include <net/ieee802154_radio.h>
#include <net/net_pkt.h>

#include <openthread/platform/radio.h>
#include <platform-zephyr.h>

/**
 * @brief Tests for the radio.c - OpenThread radio api
 * @defgroup openthread_tests radio
 * @ingroup all_tests
 * @{
 */

#define ACK_PKT_LENGTH 3
#define FRAME_TYPE_MASK 0x07
#define FRAME_TYPE_ACK 0x02

K_SEM_DEFINE(ot_sem, 0, 1);

energy_scan_done_cb_t scan_done_cb;

/**
 * Fake pointer as it should not be accessed by the code.
 * Should not be null to be sure it was properly passed.
 */
otInstance *ot = (otInstance *)0xAAAA;

/* forward declarations */
static int scan_mock(struct device *dev, u16_t duration,
		     energy_scan_done_cb_t done_cb);
static enum ieee802154_hw_caps get_capabilities(struct device *dev);
static int cca_mock(struct device *dev);
static int set_channel_mock(struct device *dev, u16_t channel);
static int filter_mock(struct device *dev, bool set,
		       enum ieee802154_filter_type type,
		       const struct ieee802154_filter *filter);
static int set_txpower_mock(struct device *dev, s16_t dbm);
static int tx_mock(struct device *dev, enum ieee802154_tx_mode mode,
		   struct net_pkt *pkt, struct net_buf *frag);
static int start_mock(struct device *dev);
static int stop_mock(struct device *dev);
static int configure_mock(struct device *dev, enum ieee802154_config_type type,
			  const struct ieee802154_config *config);

/* mocks */
static struct ieee802154_radio_api rapi = {
	.get_capabilities = get_capabilities,
	.cca = cca_mock,
	.set_channel = set_channel_mock,
	.filter = filter_mock,
	.set_txpower = set_txpower_mock,
	.tx = tx_mock,
	.start = start_mock,
	.stop = stop_mock,
	.configure = configure_mock,
#ifdef CONFIG_NET_L2_IEEE802154_SUB_GHZ
	.get_subg_channel_count = NULL,
#endif /* CONFIG_NET_L2_IEEE802154_SUB_GHZ */
	.ed_scan = scan_mock
};

static struct device radio = { .driver_api = &rapi };

static int scan_mock(struct device *dev, u16_t duration,
		     energy_scan_done_cb_t done_cb)
{
	zassert_equal(dev, &radio, "Device handle incorrect.");
	ztest_check_expected_value(duration);
	scan_done_cb = done_cb;
	return ztest_get_return_value();
}

static int rssi_scan_mock(struct device *dev, u16_t duration,
			  energy_scan_done_cb_t done_cb)
{
	zassert_equal(dev, &radio, "Device handle incorrect.");
	zassert_equal(duration, 1,
		      "otPlatRadioGetRssi shall pass minimal allowed value.");

	/* use return value as callback param */
	done_cb(&radio, ztest_get_return_value());

	return 0;
}

static int set_channel_mock(struct device *dev, u16_t channel)
{
	zassert_equal(dev, &radio, "Device handle incorrect.");
	ztest_check_expected_value(channel);
	return ztest_get_return_value();
}

void otPlatRadioEnergyScanDone(otInstance *aInstance, int8_t aEnergyScanMaxRssi)
{
	zassert_equal(aInstance, ot, "Incorrect instance.");
	ztest_check_expected_value(aEnergyScanMaxRssi);
}

void otSysEventSignalPending(void)
{
	k_sem_give(&ot_sem);
}

void otTaskletsSignalPending(otInstance *aInstance)
{
	zassert_equal(aInstance, ot, "Incorrect instance.");
	k_sem_give(&ot_sem);
}

static void make_sure_sem_set(k_timeout_t timeout)
{
	zassert_equal(k_sem_take(&ot_sem, timeout), 0, "Sem not released.");
}

void otPlatRadioReceiveDone(otInstance *aInstance, otRadioFrame *aFrame,
			    otError aError)
{
	zassert_equal(aInstance, ot, "Incorrect instance.");
	ztest_check_expected_value(aFrame->mChannel);
	ztest_check_expected_value(aFrame->mLength);
	ztest_check_expected_data(aFrame->mPsdu, aFrame->mLength);
	ztest_check_expected_value(aError);
}

void otPlatRadioTxDone(otInstance *aInstance, otRadioFrame *aFrame,
		       otRadioFrame *aAckFrame, otError aError)
{
	zassert_equal(aInstance, ot, "Incorrect instance.");
	ztest_check_expected_value(aError);
}

static enum ieee802154_hw_caps get_capabilities(struct device *dev)
{
	zassert_equal(dev, &radio, "Device handle incorrect.");

	return IEEE802154_HW_FCS | IEEE802154_HW_2_4_GHZ |
	       IEEE802154_HW_TX_RX_ACK | IEEE802154_HW_FILTER |
	       IEEE802154_HW_ENERGY_SCAN;
}

static enum ieee802154_hw_caps get_capabilities_caps_mock(struct device *dev)
{
	zassert_equal(dev, &radio, "Device handle incorrect.");

	return ztest_get_return_value();
}

static int configure_mock(struct device *dev, enum ieee802154_config_type type,
			  const struct ieee802154_config *config)
{
	zassert_equal(dev, &radio, "Device handle incorrect.");

	zassert_equal(type, IEEE802154_CONFIG_EVENT_HANDLER,
		      "Only event handler configuration was expected.");

	return 0;
}

static int configure_match_mock(struct device *dev,
				enum ieee802154_config_type type,
				const struct ieee802154_config *config)
{
	zassert_equal(dev, &radio, "Device handle incorrect.");
	ztest_check_expected_value(type);

	switch (type) {
	case IEEE802154_CONFIG_AUTO_ACK_FPB:
		ztest_check_expected_value(config->auto_ack_fpb.mode);
		ztest_check_expected_value(config->auto_ack_fpb.enabled);
		break;
	case IEEE802154_CONFIG_ACK_FPB:
		ztest_check_expected_value(config->ack_fpb.extended);
		ztest_check_expected_value(config->ack_fpb.enabled);
		ztest_check_expected_data(
			config->ack_fpb.addr,
			(config->ack_fpb.extended) ? sizeof(otExtAddress) : 2);
		break;
	default:
		zassert_unreachable("Unexpected config type %d.", type);
		break;
	}

	return 0;
}

static int configure_promiscuous_mock(struct device *dev,
				      enum ieee802154_config_type type,
				      const struct ieee802154_config *config)
{
	zassert_equal(dev, &radio, "Device handle incorrect.");
	zassert_equal(type, IEEE802154_CONFIG_PROMISCUOUS,
		      "Config type incorrect.");
	ztest_check_expected_value(config->promiscuous);

	return 0;
}

static int cca_mock(struct device *dev)
{
	/* not using assert to verify function called */
	ztest_check_expected_value(dev);
	return 0;
}

static int filter_mock(struct device *dev, bool set,
		       enum ieee802154_filter_type type,
		       const struct ieee802154_filter *filter)
{
	zassert_equal(dev, &radio, "Device handle incorrect.");
	ztest_check_expected_value(set);
	ztest_check_expected_value(type);
	switch (type) {
	case IEEE802154_FILTER_TYPE_IEEE_ADDR:
		ztest_check_expected_data(filter->ieee_addr,
					  OT_EXT_ADDRESS_SIZE);
		break;
	case IEEE802154_FILTER_TYPE_SHORT_ADDR:
		ztest_check_expected_value(filter->short_addr);
		break;
	case IEEE802154_FILTER_TYPE_PAN_ID:
		ztest_check_expected_value(filter->pan_id);
		break;
	default:
		zassert_false(true, "Type not supported in mock: %d.", type);
		break;
	}
	return 0;
}

static int set_txpower_mock(struct device *dev, s16_t dbm)
{
	zassert_equal(dev, &radio, "Device handle incorrect.");
	ztest_check_expected_value(dbm);

	return 0;
}

static int tx_mock(struct device *dev, enum ieee802154_tx_mode mode,
		   struct net_pkt *pkt, struct net_buf *frag)
{
	zassert_equal(dev, &radio, "Device handle incorrect.");
	ztest_check_expected_value(frag->data);

	return 0;
}

static int start_mock(struct device *dev)
{
	ztest_check_expected_value(dev);
	return 0;
}

static int stop_mock(struct device *dev)
{
	ztest_check_expected_value(dev);
	return 0;
}

struct device *device_get_binding_stub(const char *name)
{
	return &radio;
}

void otPlatRadioTxStarted(otInstance *aInstance, otRadioFrame *aFrame)
{
	zassert_equal(aInstance, ot, "Incorrect instance.");
}

/**
 * @brief Test for immediate energy scan
 * Tests for case when radio energy scan returns success at the first call.
 *
 */
static void test_energy_scan_immediate_test(void)
{
	const u8_t chan = 10;
	const u8_t dur = 100;
	const s16_t energy = -94;

	scan_done_cb = NULL;

	ztest_returns_value(set_channel_mock, 0);
	ztest_expect_value(set_channel_mock, channel, chan);

	ztest_returns_value(scan_mock, 0);
	ztest_expect_value(scan_mock, duration, dur);

	zassert_equal(otPlatRadioEnergyScan(ot, chan, dur), OT_ERROR_NONE,
		      "Energy scan returned error.");
	zassert_not_null(scan_done_cb, "Scan callback not specified.");

	scan_done_cb(&radio, energy);
	make_sure_sem_set(K_NO_WAIT);

	ztest_expect_value(otPlatRadioEnergyScanDone, aEnergyScanMaxRssi,
			   energy);
	platformRadioProcess(ot);
}

/**
 * @brief Test for delayed energy scan
 * Tests for case when radio returns not being able to start energy scan and
 * the scan should be sheduled for later.
 *
 */
static void test_energy_scan_delayed_test(void)
{
	const u8_t chan = 10;
	const u8_t dur = 100;
	const s16_t energy = -94;

	scan_done_cb = NULL;

	/* request scan */
	ztest_returns_value(set_channel_mock, 0);
	ztest_expect_value(set_channel_mock, channel, chan);

	ztest_returns_value(scan_mock, -EBUSY);
	ztest_expect_value(scan_mock, duration, dur);

	zassert_equal(otPlatRadioEnergyScan(ot, chan, dur), OT_ERROR_NONE,
		      "Energy scan returned error.");
	zassert_not_null(scan_done_cb, "Scan callback not specified.");
	make_sure_sem_set(K_NO_WAIT);

	/* process reported event */
	ztest_returns_value(scan_mock, 0);
	ztest_expect_value(scan_mock, duration, dur);

	ztest_returns_value(set_channel_mock, 0);
	ztest_expect_value(set_channel_mock, channel, chan);
	platformRadioProcess(ot);
	zassert_not_null(scan_done_cb, "Scan callback not specified.");

	/* invoke scan done */
	scan_done_cb(&radio, energy);
	make_sure_sem_set(K_NO_WAIT);

	ztest_expect_value(otPlatRadioEnergyScanDone, aEnergyScanMaxRssi,
			   energy);
	platformRadioProcess(ot);
}

static void create_ack_frame(void)
{
	struct net_pkt *packet;
	struct net_buf *buf;
	const u8_t lqi = 230;
	const s8_t rssi = -80;

	packet = net_pkt_alloc(K_NO_WAIT);
	buf = net_pkt_get_reserve_tx_data(K_NO_WAIT);
	net_pkt_append_buffer(packet, buf);

	buf->len = ACK_PKT_LENGTH;
	buf->data[0] = FRAME_TYPE_ACK;

	net_pkt_set_ieee802154_rssi(packet, rssi);
	net_pkt_set_ieee802154_lqi(packet, lqi);
	zassert_equal(ieee802154_radio_handle_ack(NULL, packet), NET_OK,
		      "Handling ack failed.");
	net_pkt_unref(packet);
}

/**
 * @brief Test for tx data handling
 * Tests if OT frame is correctly passed to the radio driver.
 * Additionally verifies ACK frame passing back to the OT.
 *
 */
static void test_tx_test(void)
{
	const u8_t chan = 20;
	u8_t chan2 = chan - 1;
	const s8_t power = -3;

	otRadioFrame *frm = otPlatRadioGetTransmitBuffer(ot);

	zassert_not_null(frm, "Transmit buffer is null.");

	otPlatRadioSetTransmitPower(ot, power);

	ztest_returns_value(set_channel_mock, 0);
	ztest_expect_value(set_channel_mock, channel, chan);
	ztest_expect_value(set_txpower_mock, dbm, power);
	ztest_expect_value(start_mock, dev, &radio);
	otPlatRadioReceive(ot, chan);

	/* ACKed frame */
	frm->mChannel = chan2;
	frm->mInfo.mTxInfo.mCsmaCaEnabled = true;
	frm->mPsdu[0] = IEEE802154_AR_FLAG_SET;
	ztest_returns_value(set_channel_mock, 0);
	ztest_expect_value(set_channel_mock, channel, chan2);
	ztest_expect_value(cca_mock, dev, &radio);
	ztest_expect_value(tx_mock, frag->data, frm->mPsdu);
	ztest_expect_value(set_txpower_mock, dbm, power);
	zassert_equal(otPlatRadioTransmit(ot, frm), OT_ERROR_NONE,
		      "Transmit failed.");

	create_ack_frame();
	make_sure_sem_set(Z_TIMEOUT_MS(100));

	ztest_expect_value(otPlatRadioTxDone, aError, OT_ERROR_NONE);
	platformRadioProcess(ot);

	/* Non-ACKed frame */
	frm->mChannel = --chan2;
	frm->mInfo.mTxInfo.mCsmaCaEnabled = false;
	frm->mPsdu[0] = 0;

	ztest_returns_value(set_channel_mock, 0);
	ztest_expect_value(set_channel_mock, channel, chan2);
	ztest_expect_value(tx_mock, frag->data, frm->mPsdu);
	ztest_expect_value(set_txpower_mock, dbm, power);
	zassert_equal(otPlatRadioTransmit(ot, frm), OT_ERROR_NONE,
		      "Transmit failed.");
	make_sure_sem_set(Z_TIMEOUT_MS(100));
	ztest_expect_value(otPlatRadioTxDone, aError, OT_ERROR_NONE);
	platformRadioProcess(ot);

	/* ACKed frame, no ACK */
	frm->mChannel = --chan2;
	frm->mInfo.mTxInfo.mCsmaCaEnabled = false;
	frm->mPsdu[0] = IEEE802154_AR_FLAG_SET;

	ztest_returns_value(set_channel_mock, 0);
	ztest_expect_value(set_channel_mock, channel, chan2);
	ztest_expect_value(tx_mock, frag->data, frm->mPsdu);
	ztest_expect_value(set_txpower_mock, dbm, power);
	zassert_equal(otPlatRadioTransmit(ot, frm), OT_ERROR_NONE,
		      "Transmit failed.");
	make_sure_sem_set(Z_TIMEOUT_MS(100));

	ztest_expect_value(otPlatRadioTxDone, aError, OT_ERROR_NO_ACK);
	platformRadioProcess(ot);
}

/**
 * @brief Test for tx power setting
 * Tests if tx power requested by the OT is correctly passed to the radio.
 *
 */
static void test_tx_power_test(void)
{
	s8_t out_power = 0;

	otPlatRadioSetTransmitPower(ot, -3);
	otPlatRadioGetTransmitPower(ot, &out_power);
	zassert_equal(out_power, -3, "Got different power than set.");
	otPlatRadioSetTransmitPower(ot, -6);
	otPlatRadioGetTransmitPower(ot, &out_power);
	zassert_equal(out_power, -6,
		      "Second call to otPlatRadioSetTransmitPower failed.");
}

/**
 * @brief Test for getting radio sensitivity
 * There is no api to get radio sensitivity from the radio so the value is
 * hardcoded in radio.c. Test only verifies if the value returned makes any
 * sense.
 *
 */
static void test_sensitivity_test(void)
{
	/*
	 * Nothing to test actually as this is constant 100.
	 * When radio interface will be extended to get sensitivity this test
	 * can be extended with the radio api call. For now just verify if the
	 * value is reasonable.
	 */
	zassert_true(-80 > otPlatRadioGetReceiveSensitivity(ot),
		     "Radio sensitivity not in range.");
}

static void set_expected_match_values(enum ieee802154_config_type type,
				      u8_t *addr, bool extended, bool enabled)
{
	ztest_expect_value(configure_match_mock, type, type);
	switch (type) {
	case IEEE802154_CONFIG_AUTO_ACK_FPB:
		ztest_expect_value(configure_match_mock,
				   config->auto_ack_fpb.enabled, enabled);
		ztest_expect_value(configure_match_mock,
				   config->auto_ack_fpb.mode,
				   IEEE802154_FPB_ADDR_MATCH_THREAD);
		break;
	case IEEE802154_CONFIG_ACK_FPB:
		ztest_expect_value(configure_match_mock,
				   config->ack_fpb.extended, extended);
		ztest_expect_value(configure_match_mock,
				   config->ack_fpb.enabled, enabled);
		ztest_expect_data(configure_match_mock, config->ack_fpb.addr,
				  addr);
		break;
	default:
		break;
	}
}

/**
 * @brief Test different types of OT source match.
 * Tests if Enable, Disable, Add and Clear Source Match calls are passed to the
 * radio driver correctly.
 *
 */
static void test_source_match_test(void)
{
	otExtAddress ext_addr;

	rapi.configure = configure_match_mock;
	/* Enable/Disable */
	set_expected_match_values(IEEE802154_CONFIG_AUTO_ACK_FPB, NULL, false,
				  true);
	otPlatRadioEnableSrcMatch(ot, true);
	set_expected_match_values(IEEE802154_CONFIG_AUTO_ACK_FPB, NULL, false,
				  false);
	otPlatRadioEnableSrcMatch(ot, false);

	set_expected_match_values(IEEE802154_CONFIG_AUTO_ACK_FPB, NULL, false,
				  true);
	otPlatRadioEnableSrcMatch(ot, true);

	/* Add */
	sys_put_le16(12345, ext_addr.m8);
	set_expected_match_values(IEEE802154_CONFIG_ACK_FPB, ext_addr.m8, false,
				  true);
	otPlatRadioAddSrcMatchShortEntry(ot, 12345);

	for (int i = 0; i < sizeof(ext_addr.m8); i++) {
		ext_addr.m8[i] = i;
	}
	set_expected_match_values(IEEE802154_CONFIG_ACK_FPB, ext_addr.m8, true,
				  true);
	otPlatRadioAddSrcMatchExtEntry(ot, &ext_addr);

	/* Clear */
	sys_put_le16(12345, ext_addr.m8);
	set_expected_match_values(IEEE802154_CONFIG_ACK_FPB, ext_addr.m8, false,
				  false);
	otPlatRadioClearSrcMatchShortEntry(ot, 12345);

	set_expected_match_values(IEEE802154_CONFIG_ACK_FPB, ext_addr.m8, true,
				  false);
	otPlatRadioClearSrcMatchExtEntry(ot, &ext_addr);

	set_expected_match_values(IEEE802154_CONFIG_ACK_FPB, NULL, false,
				  false);
	otPlatRadioClearSrcMatchShortEntries(ot);

	set_expected_match_values(IEEE802154_CONFIG_ACK_FPB, NULL, true, false);
	otPlatRadioClearSrcMatchExtEntries(ot);

	rapi.configure = configure_mock;
}

/**
 * @brief Test for enabling or disabling promiscuous mode
 * Tests if OT can successfully enable or disable promiscuous mode.
 *
 */
static void test_promiscuous_mode_set_test(void)
{
	rapi.configure = configure_promiscuous_mock;

	zassert_false(otPlatRadioGetPromiscuous(ot),
		      "By default promiscuous mode shall be disabled.");

	ztest_expect_value(configure_promiscuous_mock, config->promiscuous,
			   true);
	otPlatRadioSetPromiscuous(ot, true);
	zassert_true(otPlatRadioGetPromiscuous(ot), "Mode not enabled.");

	ztest_expect_value(configure_promiscuous_mock, config->promiscuous,
			   false);
	otPlatRadioSetPromiscuous(ot, false);
	zassert_false(otPlatRadioGetPromiscuous(ot), "Mode still enabled.");

	rapi.configure = configure_mock;
}

/**
 * @brief Test of proper radio to OT capabilities mapping
 * Tests if different radio capabilities map for their corresponding OpenThread
 * capability
 *
 */
static void test_get_caps_test(void)
{
	rapi.get_capabilities = get_capabilities_caps_mock;

	/* no caps */
	ztest_returns_value(get_capabilities_caps_mock, 0);
	zassert_equal(otPlatRadioGetCaps(ot), OT_RADIO_CAPS_NONE,
		      "Incorrect capabilities returned.");

	/* not used by OT */
	ztest_returns_value(get_capabilities_caps_mock, IEEE802154_HW_FCS);
	zassert_equal(otPlatRadioGetCaps(ot), OT_RADIO_CAPS_NONE,
		      "Incorrect capabilities returned.");
	ztest_returns_value(get_capabilities_caps_mock, IEEE802154_HW_2_4_GHZ);
	zassert_equal(otPlatRadioGetCaps(ot), OT_RADIO_CAPS_NONE,
		      "Incorrect capabilities returned.");
	ztest_returns_value(get_capabilities_caps_mock, IEEE802154_HW_SUB_GHZ);
	zassert_equal(otPlatRadioGetCaps(ot), OT_RADIO_CAPS_NONE,
		      "Incorrect capabilities returned.");

	/* not implemented or not fully supported */
	ztest_returns_value(get_capabilities_caps_mock, IEEE802154_HW_TXTIME);
	zassert_equal(otPlatRadioGetCaps(ot), OT_RADIO_CAPS_NONE,
		      "Incorrect capabilities returned.");

	ztest_returns_value(get_capabilities_caps_mock, IEEE802154_HW_PROMISC);
	zassert_equal(otPlatRadioGetCaps(ot), OT_RADIO_CAPS_NONE,
		      "Incorrect capabilities returned.");

	/* proper mapping */
	ztest_returns_value(get_capabilities_caps_mock, IEEE802154_HW_CSMA);
	zassert_equal(otPlatRadioGetCaps(ot),
		      OT_RADIO_CAPS_TRANSMIT_RETRIES |
			      OT_RADIO_CAPS_CSMA_BACKOFF,
		      "Incorrect capabilities returned.");

	ztest_returns_value(get_capabilities_caps_mock,
			    IEEE802154_HW_ENERGY_SCAN);
	zassert_equal(otPlatRadioGetCaps(ot), OT_RADIO_CAPS_ENERGY_SCAN,
		      "Incorrect capabilities returned.");

	ztest_returns_value(get_capabilities_caps_mock,
			    IEEE802154_HW_TX_RX_ACK);
	zassert_equal(otPlatRadioGetCaps(ot), OT_RADIO_CAPS_ACK_TIMEOUT,
		      "Incorrect capabilities returned.");

	/* all at once */
	ztest_returns_value(
		get_capabilities_caps_mock,
		IEEE802154_HW_FCS | IEEE802154_HW_PROMISC |
			IEEE802154_HW_FILTER | IEEE802154_HW_CSMA |
			IEEE802154_HW_2_4_GHZ | IEEE802154_HW_TX_RX_ACK |
			IEEE802154_HW_SUB_GHZ | IEEE802154_HW_ENERGY_SCAN |
			IEEE802154_HW_TXTIME);
	zassert_equal(
		otPlatRadioGetCaps(ot),
		OT_RADIO_CAPS_TRANSMIT_RETRIES | OT_RADIO_CAPS_CSMA_BACKOFF |
			OT_RADIO_CAPS_ENERGY_SCAN | OT_RADIO_CAPS_ACK_TIMEOUT,
		"Incorrect capabilities returned.");

	rapi.get_capabilities = get_capabilities;
}

/**
 * @brief Test for getting the rssi value from the radio
 * Tests if correct value is returned from the otPlatRadioGetRssi function.
 *
 */
static void test_get_rssi_test(void)
{
	const s8_t rssi = -103;

	rapi.ed_scan = rssi_scan_mock;

	ztest_returns_value(rssi_scan_mock, rssi);
	zassert_equal(otPlatRadioGetRssi(ot), rssi,
		      "Invalid RSSI value reveiced.");

	rapi.ed_scan = scan_mock;
}

/**
 * @brief Test switching between radio states
 * Tests if radio is correctly switched between states.
 *
 */
static void test_radio_state_test(void)
{
	const u8_t channel = 12;
	const u8_t power = 10;

	otPlatRadioSetTransmitPower(ot, power);
	zassert_equal(otPlatRadioDisable(ot), OT_ERROR_NONE,
		      "Failed to disable radio.");

	zassert_false(otPlatRadioIsEnabled(ot), "Radio reports as enabled.");

	zassert_equal(otPlatRadioSleep(ot), OT_ERROR_INVALID_STATE,
		      "Changed to sleep regardles being disabled.");

	zassert_equal(otPlatRadioEnable(ot), OT_ERROR_NONE,
		      "Enabling radio failed.");

	zassert_true(otPlatRadioIsEnabled(ot), "Radio reports disabled.");

	ztest_expect_value(stop_mock, dev, &radio);
	zassert_equal(otPlatRadioSleep(ot), OT_ERROR_NONE,
		      "Failed to switch to sleep mode.");

	zassert_true(otPlatRadioIsEnabled(ot), "Radio reports as disabled.");

	ztest_returns_value(set_channel_mock, 0);
	ztest_expect_value(set_channel_mock, channel, channel);
	ztest_expect_value(set_txpower_mock, dbm, power);
	ztest_expect_value(start_mock, dev, &radio);
	otPlatRadioReceive(ot, channel);
	zassert_equal(platformRadioChannelGet(ot), channel,
		      "Channel number not remembered.");

	zassert_true(otPlatRadioIsEnabled(ot), "Radio reports as disabled.");
}

/**
 * @brief Test address filtering
 * Tests if short, extended address and PanID are correctly passed to the radio
 * driver.
 *
 */
static void test_address_test(void)
{
	const u16_t pan_id = 0xDEAD;
	const u16_t short_add = 0xCAFE;
	otExtAddress ieee_addr;

	for (int i = 0; i < sizeof(ieee_addr.m8); i++) {
		ieee_addr.m8[i] = 'a' + i;
	}

	ztest_expect_value(filter_mock, set, true);
	ztest_expect_value(filter_mock, type, IEEE802154_FILTER_TYPE_PAN_ID);
	ztest_expect_value(filter_mock, filter->pan_id, pan_id);
	otPlatRadioSetPanId(ot, pan_id);

	ztest_expect_value(filter_mock, set, true);
	ztest_expect_value(filter_mock, type,
			   IEEE802154_FILTER_TYPE_SHORT_ADDR);
	ztest_expect_value(filter_mock, filter->short_addr, short_add);
	otPlatRadioSetShortAddress(ot, short_add);

	ztest_expect_value(filter_mock, set, true);
	ztest_expect_value(filter_mock, type, IEEE802154_FILTER_TYPE_IEEE_ADDR);
	ztest_expect_data(filter_mock, filter->ieee_addr, ieee_addr.m8);
	otPlatRadioSetExtendedAddress(ot, &ieee_addr);
}

/**
 * @brief Test received messages handling.
 * Tests if received frames are properly passed to the OpenThread
 *
 */
static void test_receive_test(void)
{
	struct net_pkt *packet;
	struct net_buf *buf;
	const u8_t channel = 21;
	const s8_t power = -5;
	const u8_t lqi = 240;
	const s8_t rssi = -90;
	u8_t len;

	packet = net_pkt_alloc(K_NO_WAIT);
	buf = net_pkt_get_reserve_tx_data(K_NO_WAIT);
	net_pkt_append_buffer(packet, buf);

	for (int i = 0; i < buf->size; i++) {
		buf->data[i] = ('a' + i) & 0xFF;
	}
	len = buf->size - 3;
	buf->len = len;

	net_pkt_set_ieee802154_lqi(packet, lqi);
	net_pkt_set_ieee802154_rssi(packet, rssi);

	otPlatRadioSetTransmitPower(ot, power);

	ztest_returns_value(set_channel_mock, 0);
	ztest_expect_value(set_channel_mock, channel, channel);
	ztest_expect_value(set_txpower_mock, dbm, power);
	ztest_expect_value(start_mock, dev, &radio);
	otPlatRadioReceive(ot, channel);

	/*
	 * Not setting any expect values as nothing shall be called from
	 * notify_new_rx_frame calling thread. OT functions can be called only
	 * after semaphore for main thread is released.
	 */
	notify_new_rx_frame(packet);

	make_sure_sem_set(Z_TIMEOUT_MS(100));
	ztest_expect_value(otPlatRadioReceiveDone, aError, OT_ERROR_NONE);
	ztest_expect_value(otPlatRadioReceiveDone, aFrame->mChannel, channel);
	ztest_expect_value(otPlatRadioReceiveDone, aFrame->mLength, len);
	ztest_expect_data(otPlatRadioReceiveDone, aFrame->mPsdu, buf->data);
	platformRadioProcess(ot);
}

void test_main(void)
{
	platformRadioInit();

	ztest_test_suite(openthread_radio,
		ztest_unit_test(test_energy_scan_immediate_test),
		ztest_unit_test(test_energy_scan_delayed_test),
		ztest_unit_test(test_tx_test),
		ztest_unit_test(test_tx_power_test),
		ztest_unit_test(test_sensitivity_test),
		ztest_unit_test(test_source_match_test),
		ztest_unit_test(test_promiscuous_mode_set_test),
		ztest_unit_test(test_get_caps_test),
		ztest_unit_test(test_get_rssi_test),
		ztest_unit_test(test_radio_state_test),
		ztest_unit_test(test_address_test),
		ztest_unit_test(test_receive_test));

	ztest_run_test_suite(openthread_radio);
}