forked from ARMmbed/mbed-os
/
test_mac_mcps_sap.c
2978 lines (2437 loc) · 103 KB
/
test_mac_mcps_sap.c
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
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2016-2017, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "nsconfig.h"
#include "nsconfig.h"
#include "ns_types.h"
#include "eventOS_event.h"
#include "eventOS_scheduler.h"
#include "eventOS_callback_timer.h"
#include "ns_trace.h"
#include "nsdynmemLIB.h"
#include "randLIB.h"
#include "ccmLIB.h"
#include "mlme.h"
#include "mac_api.h"
#include "fhss_api.h"
#include "MAC/IEEE802_15_4/mac_defines.h"
#include "MAC/IEEE802_15_4/mac_header_helper_functions.h"
#include "MAC/IEEE802_15_4/mac_timer.h"
#include "MAC/IEEE802_15_4/mac_security_mib.h"
#include "MAC/IEEE802_15_4/mac_mlme.h"
#include "MAC/IEEE802_15_4/mac_filter.h"
#include "MAC/IEEE802_15_4/mac_pd_sap.h"
#include "MAC/IEEE802_15_4/mac_mcps_sap.h"
#include "MAC/IEEE802_15_4/mac_indirect_data.h"
#include "MAC/rf_driver_storage.h"
#include "NWK_INTERFACE/Include/protocol_stats.h"
#include "platform/arm_hal_interrupt.h"
#include "common_functions.h"
#include "mac_api.h"
#include "sw_mac.h"
#include "test_mac_mcps_sap.h"
#include "sw_mac_stub.h"
#include "ccm_security_stub.h"
#include "mac_security_mib_stub.h"
#include "mac_header_helper_functions_stub.h"
#include "nsdynmemLIB_stub.h"
#include "mac_indirect_data_stub.h"
#include "event_stub.h"
#include "mac_filter_stub.h"
#include "mac_mlme_stub.h"
#include "fhss_config_stub.h"
#include "fhss_mac_interface_stub.h"
#include <string.h>
static uint8_t tes_mac_mcps_mac64[8];
static mac_pre_build_frame_t * tes_mac_mcps_temporary_buffer_get(uint16_t length) {
mac_pre_build_frame_t *buf = malloc(sizeof(mac_pre_build_frame_t));
memset(buf,0, sizeof(mac_pre_build_frame_t));
if (length) {
buf->mac_payload = malloc(length);
buf->mac_allocated_payload_ptr = true;
buf->mac_payload_length = length;
}
return buf;
}
static void test_mac_buffer_init(mac_pre_parsed_frame_t *buffer, uint16_t header_length, uint16_t payload_length, uint8_t security_length)
{
buffer->mac_header_length = header_length;
buffer->mac_payload_length = payload_length;
buffer->security_aux_header_length = security_length;
buffer->macPayloadPtr = mac_header_message_start_pointer(buffer) + buffer->mac_header_length + buffer->security_aux_header_length;
}
static void test_mac_buffer_settings(mac_pre_build_frame_t *buf, mac_frame_e frame_type, uint8_t srcType, uint8_t dstType,bool ack_request, bool intra, bool security, mac_aux_security_header_t *security_params)
{
buf->fcf_dsn.frametype = frame_type;
buf->fcf_dsn.SrcAddrMode = srcType;
buf->fcf_dsn.DstAddrMode = dstType;
buf->fcf_dsn.intraPan = intra;
buf->fcf_dsn.securityEnabled = security;
buf->fcf_dsn.ackRequested = ack_request;
buf->mac_header_length_with_security = 3;
mac_header_helper_functions_stub.dst_presents = true;
mac_header_helper_functions_stub.ie_presents_ok = true;
switch (buf->fcf_dsn.DstAddrMode) {
case MAC_ADDR_MODE_64_BIT:
buf->mac_header_length_with_security += 10;
break;
case MAC_ADDR_MODE_16_BIT:
buf->mac_header_length_with_security += 4;
break;
default:
mac_header_helper_functions_stub.dst_presents = false;
break;
}
mac_header_helper_functions_stub.src_presents = true;
switch (buf->fcf_dsn.SrcAddrMode) {
case MAC_ADDR_MODE_64_BIT:
buf->mac_header_length_with_security += 8;
break;
case MAC_ADDR_MODE_16_BIT:
buf->mac_header_length_with_security += 2;
break;
default:
mac_header_helper_functions_stub.src_presents = false;
break;
}
if (!intra) {
buf->mac_header_length_with_security += 2;
mac_header_helper_functions_stub.src_presents = false;
}
if (security) {
buf->aux_header = *security_params;
if (buf->aux_header.securityLevel) {
switch (buf->aux_header.KeyIdMode) {
case 1:
case 5:
buf->security_mic_len = 4;
break;
case 2:
case 6:
buf->security_mic_len = 8;
break;
case 3:
case 7:
buf->security_mic_len = 16;
break;
default:
buf->security_mic_len = 0;
break;
}
buf->mac_header_length_with_security += 5; //Level and frame counter
switch (buf->aux_header.securityLevel) {
case MAC_KEY_ID_MODE_IMPLICIT:
break;
case MAC_KEY_ID_MODE_IDX:
buf->mac_header_length_with_security += 1;
break;
case MAC_KEY_ID_MODE_SRC4_IDX:
buf->mac_header_length_with_security += 5;
break;
case MAC_KEY_ID_MODE_SRC8_IDX:
buf->mac_header_length_with_security += 9;
break;
}
} else {
buf->aux_header.KeyIdMode = 0;
}
}
}
static void test_set_security_params(mac_aux_security_header_t *params, uint8_t security_level, uint8_t key_id_mode, uint8_t key_index)
{
params->KeyIdMode = key_id_mode;
params->KeyIndex = 1;
params->frameCounter = 0;
params->securityLevel = security_level;
}
static int8_t test_phy_rf_virtual_rx(const uint8_t *data_ptr, uint16_t data_len,int8_t driver_id)
{
}
static phy_link_tx_status_e virtual_tx_done_status;
static bool virtual_cb_ok = false;
static int8_t test_phy_rf_tx_done(int8_t driver_id, uint8_t tx_handle, phy_link_tx_status_e status, uint8_t cca_retry, uint8_t tx_retry)
{
if (status == virtual_tx_done_status) {
virtual_cb_ok = true;
} else {
virtual_cb_ok = false;
}
}
static uint16_t expected_virtual_rx_data_length = 0;
static bool virtual_rx_data_length_ok = false;
static int8_t test_phy_rf_rx(const uint8_t *data_ptr, uint16_t data_len, uint8_t link_quality, int8_t dbm, int8_t driver_id)
{
//Validate proper data length here
if (expected_virtual_rx_data_length == data_len) {
virtual_rx_data_length_ok = true;
} else {
virtual_rx_data_length_ok = false;
}
}
static arm_device_driver_list_s * rf_client_driver_allocate()
{
arm_device_driver_list_s *driver = malloc(sizeof(arm_device_driver_list_s));
phy_device_driver_s *dev_driver = malloc(sizeof(phy_device_driver_s));
memset(driver, 0, sizeof(arm_device_driver_list_s));
memset(dev_driver, 0, sizeof(phy_device_driver_s));
driver->phy_driver = dev_driver;
dev_driver->arm_net_virtual_rx_cb = &test_phy_rf_virtual_rx;
dev_driver->phy_rx_cb = &test_phy_rf_rx;
dev_driver->phy_tx_done_cb = &test_phy_rf_tx_done;
return driver;
}
static protocol_interface_rf_mac_setup_s * test_mac_rf_mac_class_allocate(void) {
protocol_interface_rf_mac_setup_s *rf_mac_setup = malloc(sizeof(protocol_interface_rf_mac_setup_s));
memset(rf_mac_setup,0, sizeof(protocol_interface_rf_mac_setup_s));
rf_mac_setup->dev_driver_tx_buffer.buf = malloc(128);
rf_mac_setup->dev_driver_tx_buffer.len = 127;
arm_device_driver_list_s *driver = malloc(sizeof(arm_device_driver_list_s));
phy_device_driver_s *dev_driver = malloc(sizeof(phy_device_driver_s));
memset(driver, 0, sizeof(arm_device_driver_list_s));
memset(dev_driver, 0, sizeof(phy_device_driver_s));
dev_driver->PHY_MAC = tes_mac_mcps_mac64;
dev_driver->phy_MTU = 127;
dev_driver->phy_header_length = 0;
driver->phy_driver = dev_driver;
rf_mac_setup->dev_driver = driver;
return rf_mac_setup;
}
static int fhss_tx_handle_callback(const fhss_api_t *api, bool is_broadcast_addr, uint8_t *destination_address, int frame_type, uint16_t frame_length, uint8_t phy_header_length, uint8_t phy_tail_length)
{
return 0;
}
static bool fhss_tx_condition = true;
static bool fhss_check_tx_conditions_callback(const fhss_api_t *api, bool is_broadcast_addr, uint8_t handle, int frame_type, uint16_t frame_length, uint8_t phy_header_length, uint8_t phy_tail_length)
{
if (frame_type == FHSS_SYNCH_REQUEST_FRAME) {
return false;
}
return fhss_tx_condition;
}
static uint8_t expect_data_confirmation_status = 0;
static bool data_confirm_valid = false;
void test_mcps_data_confirm( const mac_api_t* api, const mcps_data_conf_t *data )
{
if (data->status == expect_data_confirmation_status) {
data_confirm_valid = true;
} else {
data_confirm_valid = false;
}
}
void test_mcps_data_confirm_ext( const mac_api_t* api, const mcps_data_conf_t *data, const mcps_data_conf_payload_t *conf_data)
{
if (data->status == expect_data_confirmation_status) {
data_confirm_valid = true;
} else {
data_confirm_valid = false;
}
}
static uint16_t expected_data_length = 0;
static bool data_ind_valid = false;
static void test_mcps_data_indication( const mac_api_t* api, const mcps_data_ind_t *data )
{
if (data->msduLength == expected_data_length) {
data_ind_valid = true;
} else {
data_ind_valid = false;
}
}
uint16_t expected_ie_header_length = 0;
uint16_t expected_ie_payload_length = 0;
static void test_mcps_data_indication_ext( const mac_api_t* api, const mcps_data_ind_t *data , const mcps_data_ie_list_t *ie_list)
{
if (data->msduLength == expected_data_length && ie_list->headerIeListLength == expected_ie_header_length && ie_list->payloadIeListLength == expected_ie_payload_length) {
data_ind_valid = true;
} else {
data_ind_valid = false;
}
}
static mlme_primitive expected_mlme_id_cnf = 0;
static bool mlme_confirm_valid = false;
void test_mlme_confirm( const mac_api_t* api, mlme_primitive id, const void *data )
{
if (id == expected_mlme_id_cnf) {
mlme_confirm_valid = true;
} else {
mlme_confirm_valid = false;
}
}
static uint8_t expected_purge_status = 0;
static uint8_t expected_purge_handle = 0;
static uint8_t expected_purge_result = false;
void test_purge_confirm( const mac_api_t* api, const mcps_purge_conf_t *data )
{
if (expected_purge_handle != data->msduHandle) {
expected_purge_result = false;
} else if (expected_purge_status != data->status){
expected_purge_result = false;
} else {
expected_purge_result= true;
}
}
static mlme_primitive expected_mlme_ind_id_cnf = 0;
static uint8_t mlme_ind_status;
static bool mlme_ind_valid = false;
void test_mlme_indication( const mac_api_t* api, mlme_primitive id, const void *data )
{
mlme_ind_valid = false;
if (id == expected_mlme_ind_id_cnf) {
if (id == MLME_COMM_STATUS) {
mlme_comm_status_t *comm_status = (mlme_comm_status_t*) data;
if (comm_status->status == mlme_ind_status) {
mlme_ind_valid = true;
}
} else {
mlme_ind_valid = true;
}
}
}
static uint8_t expected_event_type = 0;
static bool valid_pd_sap_confirm_forward = false;
static void handler_func_ptr(arm_event_s *event)
{
if (event->event_type == expected_event_type) {
valid_pd_sap_confirm_forward = true;
} else {
valid_pd_sap_confirm_forward = false;
}
}
void test_mac_rf_mac_class_free(protocol_interface_rf_mac_setup_s *rf_ptr) {
free(rf_ptr->dev_driver_tx_buffer.buf);
free(rf_ptr->dev_driver->phy_driver);
free(rf_ptr->dev_driver);
if (rf_ptr->tun_extension_rf_driver) {
free(rf_ptr->tun_extension_rf_driver->phy_driver);
free(rf_ptr->tun_extension_rf_driver);
}
free(rf_ptr);
}
bool test_mac_generic_event_trig()
{
//Init event handler
event_stub.int8_value = 0;
int8_t mac_tasklet_id = mac_mcps_sap_tasklet_init();
mcps_data_req_t data_req;
ccm_globals_t ccm_ptr;
mac_api_t mac_api;
mlme_key_descriptor_t key_description;
mlme_device_descriptor_t device_description;
mac_aux_security_header_t secuirity_params;
mlme_key_device_descriptor_t key_device_description;
memset(&data_req, 0, sizeof(mcps_data_req_t));
uint8_t tx_buf[127];
uint8_t tx_temp[200];
protocol_interface_rf_mac_setup_s *rf_mac_setup = test_mac_rf_mac_class_allocate();
memset(tx_buf, 1, 127);
memset(&key_description, 0, sizeof(mlme_key_descriptor_t));
memset(&device_description, 0, sizeof(mlme_device_descriptor_t));
mac_api.data_conf_cb = &test_mcps_data_confirm;
mac_api.mlme_conf_cb = &test_mlme_confirm;
mac_api.data_ind_cb = &test_mcps_data_indication;
mac_api.mlme_ind_cb = &test_mlme_indication;
rf_mac_setup->macUpState = true;
sw_mac_stub.mac_api_ptr = &mac_api;
mac_generic_event_trig(MAC_MLME_EVENT_HANDLER,rf_mac_setup,false);
mac_generic_event_trig(MAC_MLME_SCAN_CONFIRM_HANDLER,rf_mac_setup,true);
test_mac_rf_mac_class_free(rf_mac_setup);
return true;
}
bool test_mcps_sap_trig_tx()
{
//Init event handler
event_stub.int8_value = 0;
int8_t mac_tasklet_id = mac_mcps_sap_tasklet_init();
protocol_interface_rf_mac_setup_s *rf_mac_setup = test_mac_rf_mac_class_allocate();
rf_mac_setup->fhss_api = ns_fhss_create(0, 0);
mac_pre_build_frame_t *buf = tes_mac_mcps_temporary_buffer_get(0);
rf_mac_setup->pd_data_request_bc_queue_to_go = buf;
// Test without MAC setup
mcps_sap_trig_tx(NULL);
if (rf_mac_setup->active_pd_data_request == buf) {
return false;
}
// Test when TX trig active
rf_mac_setup->active_mac_events |= (1 << MAC_SAP_TRIG_TX);
mcps_sap_trig_tx(rf_mac_setup);
if (rf_mac_setup->active_pd_data_request == buf) {
return false;
}
rf_mac_setup->active_mac_events &= ~(1 << MAC_SAP_TRIG_TX);
// FHSS is on broadcast channel and tx conditions are fine
fhss_mac_interface_stub.channel_bool_value = true;
fhss_tx_condition = true;
mcps_sap_trig_tx(rf_mac_setup);
// Active data request should be buf
if (rf_mac_setup->active_pd_data_request != buf) {
return false;
}
mac_mcps_buffer_queue_free(rf_mac_setup);
test_mac_rf_mac_class_free(rf_mac_setup);
return true;
}
bool test_mac_virtual_sap_data_cb() {
//Init event handler
event_stub.int8_value = 0;
int8_t mac_tasklet_id = mac_mcps_sap_tasklet_init();
mcps_data_req_t data_req;
ccm_globals_t ccm_ptr;
mac_api_t mac_api;
mlme_key_descriptor_t key_description;
mlme_device_descriptor_t device_description;
mac_aux_security_header_t secuirity_params;
mlme_key_device_descriptor_t key_device_description;
memset(&data_req, 0, sizeof(mcps_data_req_t));
uint8_t tx_buf[127];
uint8_t tx_temp[200];
uint8_t virtual_tx_temp[200];
protocol_interface_rf_mac_setup_s *rf_mac_setup = test_mac_rf_mac_class_allocate();
memset(tx_buf, 1, 127);
memset(&key_description, 0, sizeof(mlme_key_descriptor_t));
memset(&device_description, 0, sizeof(mlme_device_descriptor_t));
mac_api.data_conf_cb = &test_mcps_data_confirm;
mac_api.mlme_conf_cb = &test_mlme_confirm;
mac_api.data_ind_cb = &test_mcps_data_indication;
mac_api.mlme_ind_cb = &test_mlme_indication;
rf_mac_setup->macUpState = true;
sw_mac_stub.mac_api_ptr = &mac_api;
rf_mac_setup->tun_extension_rf_driver = rf_client_driver_allocate();
arm_phy_sap_msg_t phy_message;
memset(&phy_message, 0 , sizeof(arm_phy_sap_msg_t));
mac_virtual_sap_data_cb(NULL, NULL);
phy_message.id = MAC802_PD_SAP_DATA_TX_CONFIRM;
if (-1 != mac_virtual_sap_data_cb(rf_mac_setup, &phy_message) ) {
return false;
}
rf_mac_setup->macUpState = false;
//TEST MLME Extension
phy_message.id = MACTUN_MLME_NAP_EXTENSION;
uint8_t beacon_payload[18];
beacon_payload[0] = macBeaconPayload;
beacon_payload[1] = 0;
phy_message.message.mlme_request.mlme_ptr = beacon_payload;
phy_message.message.mlme_request.primitive = MLME_SET;
phy_message.message.mlme_request.ptr_length = 18;
mac_mlme_stub.int8_value = -1;
if (-1 != mac_virtual_sap_data_cb(rf_mac_setup, &phy_message) ) {
return false;
}
mac_mlme_stub.int8_value = 0;
if (0 != mac_virtual_sap_data_cb(rf_mac_setup, &phy_message) ) {
return false;
}
phy_message.message.mlme_request.primitive = MLME_SCAN;
phy_message.message.mlme_request.ptr_length = 40;
if (-1 != mac_virtual_sap_data_cb(rf_mac_setup, &phy_message) ) {
return false;
}
phy_message.message.mlme_request.ptr_length = 47;
if (0 != mac_virtual_sap_data_cb(rf_mac_setup, &phy_message) ) {
return false;
}
phy_message.message.mlme_request.primitive = MLME_START;
phy_message.message.mlme_request.ptr_length = 30;
if (-1 != mac_virtual_sap_data_cb(rf_mac_setup, &phy_message) ) {
return false;
}
phy_message.message.mlme_request.ptr_length = 34;
if (0 != mac_virtual_sap_data_cb(rf_mac_setup, &phy_message) ) {
return false;
}
// Test unsupported primitive
phy_message.message.mlme_request.primitive = 65535;
if (-1 != mac_virtual_sap_data_cb(rf_mac_setup, &phy_message) ) {
return false;
}
//Test Mac down state
phy_message.id = MACTUN_PD_SAP_NAP_IND;
if (-1 != mac_virtual_sap_data_cb(rf_mac_setup, &phy_message) ) {
return false;
}
//Test too long
rf_mac_setup->macUpState = true;
phy_message.message.generic_data_ind.data_ptr = virtual_tx_temp;
phy_message.message.generic_data_ind.data_len = 128;
if (mac_virtual_sap_data_cb(rf_mac_setup, &phy_message) == 0) {
return false;
}
//Test Buffer fail allocation
nsdynmemlib_stub.returnCounter = 0;
phy_message.message.generic_data_ind.data_len = 40;
if (-1 != mac_virtual_sap_data_cb(rf_mac_setup, &phy_message) ) {
return false;
}
//Test no ack request
nsdynmemlib_stub.returnCounter = 3;
mac_header_helper_functions_stub.fcf.ackRequested = false;
virtual_tx_done_status = PHY_LINK_TX_SUCCESS;
virtual_cb_ok = false;
//Waiting tx success from callback
if (0 != mac_virtual_sap_data_cb(rf_mac_setup, &phy_message) ) {
return false;
}
if (!rf_mac_setup->active_pd_data_request || !virtual_cb_ok) {
return false;
}
//Free current buffer
mac_mcps_buffer_queue_free(rf_mac_setup);
//Test with ack
nsdynmemlib_stub.returnCounter = 3;
mac_header_helper_functions_stub.fcf.ackRequested = true;
if (0 != mac_virtual_sap_data_cb(rf_mac_setup, &phy_message) ) {
return false;
}
if (!rf_mac_setup->active_pd_data_request) {
return false;
}
mac_mcps_buffer_queue_free(rf_mac_setup);
//Test copying MAC address from payload
uint8_t test_tx_buffer[21] = {0x61, 0xcc, 0x8, 0x55, 0x12, 0x3, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x2, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2};
uint8_t test_dst_addr_buffer[8] = {0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0x3};
phy_message.message.generic_data_ind.data_ptr = test_tx_buffer;
phy_message.message.generic_data_ind.data_len = sizeof(test_tx_buffer);
nsdynmemlib_stub.returnCounter = 3;
mac_header_helper_functions_stub.fcf.ackRequested = true;
mac_header_helper_functions_stub.fcf.DstAddrMode = MAC_ADDR_MODE_64_BIT;
memcpy(mac_header_helper_functions_stub.dst_address, test_dst_addr_buffer, 8);
if (0 != mac_virtual_sap_data_cb(rf_mac_setup, &phy_message) ) {
return false;
}
if (!rf_mac_setup->active_pd_data_request) {
return false;
}
// Destination address check
if (memcmp(rf_mac_setup->active_pd_data_request->DstAddr, test_dst_addr_buffer, 8)) {
return false;
}
mac_mcps_buffer_queue_free(rf_mac_setup);
test_mac_rf_mac_class_free(rf_mac_setup);
return true;
}
bool test_mac_mcps_data_confirmation()
{
//Init event handler
event_stub.int8_value = 0;
int8_t mac_tasklet_id = mac_mcps_sap_tasklet_init();
mcps_data_req_t data_req;
ccm_globals_t ccm_ptr;
mac_api_t mac_api;
mlme_key_descriptor_t key_description;
mlme_device_descriptor_t device_description;
mac_aux_security_header_t secuirity_params;
mlme_key_device_descriptor_t key_device_description;
memset(&data_req, 0, sizeof(mcps_data_req_t));
uint8_t tx_buf[127];
uint8_t tx_temp[200];
protocol_interface_rf_mac_setup_s *rf_mac_setup = test_mac_rf_mac_class_allocate();
memset(tx_buf, 1, 127);
memset(&key_description, 0, sizeof(mlme_key_descriptor_t));
memset(&device_description, 0, sizeof(mlme_device_descriptor_t));
mac_api.data_conf_cb = &test_mcps_data_confirm;
mac_api.mlme_conf_cb = &test_mlme_confirm;
mac_api.data_ind_cb = &test_mcps_data_indication;
mac_api.mlme_ind_cb = &test_mlme_indication;
rf_mac_setup->macUpState = true;
sw_mac_stub.mac_api_ptr = &mac_api;
data_req.msdu = tx_buf;
data_req.msduLength = 10;
rf_mac_setup->macUpState = true;
//Empty call
mcps_sap_pd_confirm(rf_mac_setup);
//TODO add better validation at callbacks
//CCA Fail
rf_mac_setup->mac_tx_result = MAC_CCA_FAIL;
nsdynmemlib_stub.returnCounter = 3;
mcps_sap_data_req_handler(rf_mac_setup, &data_req);
mcps_sap_pd_confirm(rf_mac_setup);
if (rf_mac_setup->active_pd_data_request) {
test_mac_rf_mac_class_free(rf_mac_setup);
return false;
}
nsdynmemlib_stub.returnCounter = 3;
mcps_sap_data_req_handler(rf_mac_setup, &data_req);
rf_mac_setup->mac_tx_result = MAC_TX_FAIL;
mcps_sap_pd_confirm(rf_mac_setup);
if (rf_mac_setup->active_pd_data_request) {
test_mac_rf_mac_class_free(rf_mac_setup);
return false;
}
nsdynmemlib_stub.returnCounter = 3;
mcps_sap_data_req_handler(rf_mac_setup, &data_req);
rf_mac_setup->mac_tx_result = MAC_TX_DONE;
mcps_sap_pd_confirm(rf_mac_setup);
if (rf_mac_setup->active_pd_data_request) {
test_mac_rf_mac_class_free(rf_mac_setup);
return false;
}
nsdynmemlib_stub.returnCounter = 3;
mcps_sap_data_req_handler(rf_mac_setup, &data_req);
rf_mac_setup->mac_tx_result = MAC_TX_DONE_PENDING;
mcps_sap_pd_confirm(rf_mac_setup);
if (rf_mac_setup->active_pd_data_request) {
test_mac_rf_mac_class_free(rf_mac_setup);
return false;
}
nsdynmemlib_stub.returnCounter = 3;
mcps_sap_data_req_handler(rf_mac_setup, &data_req);
rf_mac_setup->mac_tx_result = MAC_TX_TIMEOUT;
mcps_sap_pd_confirm(rf_mac_setup);
if (rf_mac_setup->active_pd_data_request) {
test_mac_rf_mac_class_free(rf_mac_setup);
return false;
}
//Change data packet to MAC command: DATA_REQ and test all CONFIRM events
nsdynmemlib_stub.returnCounter = 3;
data_req.DstAddrMode = MAC_ADDR_MODE_64_BIT;
mcps_sap_data_req_handler(rf_mac_setup, &data_req);
rf_mac_setup->mac_tx_result = MAC_TX_DONE;
//rf_mac_setup->active_pd_data_request->status = MLME_SUCCESS;
rf_mac_setup->active_pd_data_request->fcf_dsn.frametype = FC_CMD_FRAME;
rf_mac_setup->active_pd_data_request->mac_command_id = MAC_DATA_REQ;
rf_mac_setup->active_pd_data_request->upper_layer_request = false;
mcps_sap_pd_confirm(rf_mac_setup);
if (rf_mac_setup->active_pd_data_request) {
test_mac_rf_mac_class_free(rf_mac_setup);
return false;
}
//Test data wait state + driver request
nsdynmemlib_stub.returnCounter = 3;
rf_mac_setup->dev_driver->phy_driver->phy_header_length = 1;
mcps_sap_data_req_handler(rf_mac_setup, &data_req);
rf_mac_setup->mac_tx_result = MAC_TX_DONE_PENDING;
//rf_mac_setup->active_pd_data_request->status = MLME_SUCCESS;
rf_mac_setup->active_pd_data_request->fcf_dsn.frametype = FC_CMD_FRAME;
rf_mac_setup->active_pd_data_request->mac_command_id = MAC_DATA_REQ;
rf_mac_setup->active_pd_data_request->upper_layer_request = false;
mcps_sap_pd_confirm(rf_mac_setup);
if (!rf_mac_setup->active_pd_data_request || rf_mac_setup->mac_mlme_event != ARM_NWK_MAC_MLME_INDIRECT_DATA_POLL) {
test_mac_rf_mac_class_free(rf_mac_setup);
return false;
}
//Test Virtual mac extension confirmation + driver request
rf_mac_setup->tun_extension_rf_driver = rf_client_driver_allocate();
rf_mac_setup->active_pd_data_request->mac_command_id = 0xff;
rf_mac_setup->active_pd_data_request->fcf_dsn.frametype = FC_DATA_FRAME;
rf_mac_setup->active_pd_data_request->fcf_dsn.ackRequested = true;
rf_mac_setup->mac_tx_result = MAC_TX_FAIL;
mcps_sap_pd_confirm(rf_mac_setup);
if (rf_mac_setup->active_pd_data_request) {
test_mac_rf_mac_class_free(rf_mac_setup);
return false;
}
// Test FHSS channel retry
rf_mac_setup->fhss_api = ns_fhss_create(0, 0);
nsdynmemlib_stub.returnCounter = 3;
data_req.TxAckReq = true;
fhss_tx_condition = true;
mcps_sap_data_req_handler(rf_mac_setup, &data_req);
rf_mac_setup->mac_tx_result = MAC_TX_FAIL;
mcps_sap_pd_confirm(rf_mac_setup);
if (!rf_mac_setup->active_pd_data_request) {
test_mac_rf_mac_class_free(rf_mac_setup);
return false;
}
rf_mac_setup->mac_tx_result = MAC_TX_DONE;
mcps_sap_pd_confirm(rf_mac_setup);
if (rf_mac_setup->active_pd_data_request) {
test_mac_rf_mac_class_free(rf_mac_setup);
return false;
}
test_mac_rf_mac_class_free(rf_mac_setup);
return true;
}
bool test_mac_mcps_data_indication()
{
//Init event handler
event_stub.int8_value = 0;
int8_t mac_tasklet_id = mac_mcps_sap_tasklet_init();
ccm_globals_t ccm_ptr;
mac_api_t mac_api;
mlme_key_descriptor_t key_description;
mlme_device_descriptor_t device_description;
mac_aux_security_header_t secuirity_params;
mlme_key_device_descriptor_t key_device_description;
mcps_data_req_t data_req;
memset(&data_req, 0, sizeof(mcps_data_req_t));
uint8_t tx_buf[127];
uint8_t tx_temp[200];
protocol_interface_rf_mac_setup_s *rf_mac_setup = test_mac_rf_mac_class_allocate();
memset(tx_buf, 1, 127);
memset(&key_description, 0, sizeof(mlme_key_descriptor_t));
memset(&device_description, 0, sizeof(mlme_device_descriptor_t));
mac_api.data_conf_cb = &test_mcps_data_confirm;
mac_api.mlme_conf_cb = &test_mlme_confirm;
mac_api.data_ind_cb = &test_mcps_data_indication;
mac_api.mlme_ind_cb = &test_mlme_indication;
mac_api.data_ind_ext_cb = &test_mcps_data_indication_ext;
rf_mac_setup->mac_extension_enabled = false;
rf_mac_setup->macUpState = true;
rf_mac_setup->pan_id = 0x0101;
mac_header_helper_functions_stub.pan_src = 0x0101;
sw_mac_stub.mac_api_ptr = &mac_api;
nsdynmemlib_stub.returnCounter = 1;
mac_pre_parsed_frame_t *buffer = mcps_sap_pre_parsed_frame_buffer_get(tx_buf, 100);
buffer->fcf_dsn.frametype = FC_DATA_FRAME;
buffer->fcf_dsn.DstAddrMode = MAC_ADDR_MODE_16_BIT;
buffer->fcf_dsn.SrcAddrMode = MAC_ADDR_MODE_16_BIT;
buffer->fcf_dsn.intraPan = true;
buffer->fcf_dsn.securityEnabled = false;
test_mac_buffer_init(buffer, 9, 91, 0);
buffer->mac_class_ptr = NULL;
//First Buffer push
mcps_sap_pd_ind(buffer);
//Drop by mac filter
nsdynmemlib_stub.returnCounter = 1;
mac_filter_stub_def.value_int8 = 1;
buffer = mcps_sap_pre_parsed_frame_buffer_get(tx_buf, 100);
buffer->fcf_dsn.frametype = FC_DATA_FRAME;
buffer->fcf_dsn.DstAddrMode = MAC_ADDR_MODE_16_BIT;
buffer->fcf_dsn.SrcAddrMode = MAC_ADDR_MODE_16_BIT;
buffer->fcf_dsn.intraPan = true;
buffer->fcf_dsn.securityEnabled = false;
test_mac_buffer_init(buffer,9, 91, 0);
buffer->mac_class_ptr = rf_mac_setup;
mcps_sap_pd_ind(buffer);
//Drop by unknow SW MAC class
mac_filter_stub_def.value_int8 = 0;
sw_mac_stub.mac_api_ptr = NULL;
nsdynmemlib_stub.returnCounter = 1;
buffer = mcps_sap_pre_parsed_frame_buffer_get(tx_buf, 100);
buffer->fcf_dsn.frametype = FC_DATA_FRAME;
buffer->fcf_dsn.DstAddrMode = MAC_ADDR_MODE_16_BIT;
buffer->fcf_dsn.SrcAddrMode = MAC_ADDR_MODE_16_BIT;
buffer->fcf_dsn.intraPan = true;
buffer->fcf_dsn.securityEnabled = false;
test_mac_buffer_init(buffer,9, 91, 0);
buffer->mac_class_ptr = rf_mac_setup;
mcps_sap_pd_ind(buffer);
nsdynmemlib_stub.returnCounter = 1;
buffer = mcps_sap_pre_parsed_frame_buffer_get(tx_buf, 100);
buffer->fcf_dsn.frametype = FC_CMD_FRAME;
buffer->fcf_dsn.DstAddrMode = MAC_ADDR_MODE_16_BIT;
buffer->fcf_dsn.SrcAddrMode = MAC_ADDR_MODE_16_BIT;
buffer->fcf_dsn.intraPan = true;
buffer->fcf_dsn.securityEnabled = false;
test_mac_buffer_init(buffer,9, 91, 0);
buffer->mac_class_ptr = rf_mac_setup;
mcps_sap_pd_ind(buffer);
//
//Drop by data confirm ind allocate fail
sw_mac_stub.mac_api_ptr = &mac_api;
nsdynmemlib_stub.returnCounter = 1;
buffer = mcps_sap_pre_parsed_frame_buffer_get(tx_buf, 100);
buffer->fcf_dsn.frametype = FC_DATA_FRAME;
buffer->fcf_dsn.DstAddrMode = MAC_ADDR_MODE_16_BIT;
buffer->fcf_dsn.SrcAddrMode = MAC_ADDR_MODE_16_BIT;
buffer->fcf_dsn.intraPan = true;
buffer->fcf_dsn.securityEnabled = false;
test_mac_buffer_init(buffer,9, 91, 0);
buffer->mac_class_ptr = rf_mac_setup;
mcps_sap_pd_ind(buffer);
//Drp by rx on idle
//Drop when not waiting data
nsdynmemlib_stub.returnCounter = 2;
rf_mac_setup->macWaitingData = false;
buffer = mcps_sap_pre_parsed_frame_buffer_get(tx_buf, 100);
buffer->fcf_dsn.frametype = FC_DATA_FRAME;
buffer->fcf_dsn.DstAddrMode = MAC_ADDR_MODE_16_BIT;
buffer->fcf_dsn.SrcAddrMode = MAC_ADDR_MODE_16_BIT;
buffer->fcf_dsn.intraPan = true;
buffer->fcf_dsn.securityEnabled = false;
test_mac_buffer_init(buffer,9, 91, 0);
buffer->mac_class_ptr = rf_mac_setup;
mcps_sap_pd_ind(buffer);
//Drop by brodacst destination
nsdynmemlib_stub.returnCounter = 2;
rf_mac_setup->macWaitingData = true;
buffer = mcps_sap_pre_parsed_frame_buffer_get(tx_buf, 100);
buffer->fcf_dsn.frametype = FC_DATA_FRAME;
buffer->fcf_dsn.DstAddrMode = MAC_ADDR_MODE_16_BIT;
buffer->fcf_dsn.SrcAddrMode = MAC_ADDR_MODE_16_BIT;
buffer->fcf_dsn.intraPan = true;
buffer->fcf_dsn.ackRequested = false;
buffer->fcf_dsn.securityEnabled = false;
test_mac_buffer_init(buffer,9, 91, 0);
buffer->mac_class_ptr = rf_mac_setup;
//Set Bradcast address
mac_header_helper_functions_stub.dst_address[0] = 0xff;
mac_header_helper_functions_stub.dst_address[1] = 0xff;
mcps_sap_pd_ind(buffer);
//Drop Empty data packet
mac_mlme_stub.pollConfirm = 0xfc;
nsdynmemlib_stub.returnCounter = 2;
rf_mac_setup->macWaitingData = true;
buffer = mcps_sap_pre_parsed_frame_buffer_get(tx_buf, 9);
buffer->fcf_dsn.frametype = FC_DATA_FRAME;
buffer->fcf_dsn.DstAddrMode = MAC_ADDR_MODE_16_BIT;
buffer->fcf_dsn.SrcAddrMode = MAC_ADDR_MODE_16_BIT;
buffer->fcf_dsn.intraPan = true;
buffer->fcf_dsn.ackRequested = true;
buffer->fcf_dsn.securityEnabled = false;
test_mac_buffer_init(buffer,9, 0, 0);
buffer->mac_class_ptr = rf_mac_setup;
//Set Bradcast address
mac_header_helper_functions_stub.dst_address[0] = 0;
mac_header_helper_functions_stub.dst_address[1] = 0;
mcps_sap_pd_ind(buffer);
if (mac_mlme_stub.pollConfirm != MLME_NO_DATA) {
return false;
}
expected_data_length = 20;
data_ind_valid = false;
mac_mlme_stub.pollConfirm = 0xfc;
nsdynmemlib_stub.returnCounter = 2;
rf_mac_setup->macWaitingData = true;
buffer = mcps_sap_pre_parsed_frame_buffer_get(tx_buf, 29);
buffer->fcf_dsn.frametype = FC_DATA_FRAME;
buffer->fcf_dsn.DstAddrMode = MAC_ADDR_MODE_16_BIT;
buffer->fcf_dsn.SrcAddrMode = MAC_ADDR_MODE_16_BIT;
buffer->fcf_dsn.intraPan = true;
buffer->fcf_dsn.ackRequested = true;
buffer->fcf_dsn.securityEnabled = false;
rf_mac_setup->macDataPollReq = true;
rf_mac_setup->macRxDataAtPoll = false;
test_mac_buffer_init(buffer,9, 20, 6);
buffer->mac_class_ptr = rf_mac_setup;
//Set Bradcast address
mac_header_helper_functions_stub.dst_address[0] = 0;
mac_header_helper_functions_stub.dst_address[1] = 0;
mcps_sap_pd_ind(buffer);
if (rf_mac_setup->mac_mlme_event != ARM_NWK_MAC_MLME_INDIRECT_DATA_POLL_AFTER_DATA || !rf_mac_setup->macRxDataAtPoll) {
return false;
}
//Test security data packet
mac_header_helper_functions_stub.security_header.securityLevel = 5; //securityLevel
mac_header_helper_functions_stub.security_header.KeyIdMode = MAC_KEY_ID_MODE_IDX;
mac_header_helper_functions_stub.security_header.KeyIndex = 1;
mac_header_helper_functions_stub.security_header.frameCounter = 0;
mac_header_helper_functions_stub.uint8_ptr = tx_buf + 35; // Set Mic pointer