summaryrefslogtreecommitdiff
path: root/hal/phy/maximus/test/src/test_phy_ctrl.c
blob: 08f4b2f713b13fdcf159238b848a5e10d7e65edf (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    hal/phy/maximus/test/src/test_phy_ctrl.c
 * \brief   HAL Phy control test functions for Maximus.
 * \ingroup hal_phy_maximus
 */
 
#include "common/std.h"
#include "lib/test.h"
#include "host/station.h"
#include "host/sci.h"
#include "maximus/common/types/phy_types.h"
#include "host/fwd.h" // for 'phy_msg_hdr_t'
#include "hal/phy/maximus/inc/maximus_phy_ctx.h"
#include "hal/phy/maximus/inc/maximus_phy_ctrl.h"
#include "hal/phy/maximus/inc/maximus_interrupts.h"
#include "hal/phy/maximus/inc/maximus_dur.h"
#include "hal/phy/maximus/test/inc/test_phy_maximus.h"
#include <stdio.h> // for 'printf'
#include <string.h> // for 'memset'
#include <netinet/in.h> // for 'ntohl' and 'ntohs' functions
#include <unistd.h> // for 'read'
#include <fcntl.h> // for 'read'
#include <errno.h>

#define PHY_PB_NB 3 // for 'maximus_phy_recv_test_case' (for PHY_TYPE_MPDU_PAYLOAD)

extern uint32_t maximus_pending_isrs; // used in 'station.c'
extern station_ctx_t my_station;
phy_t *ctx;

void maximus_phy_fill_hdr_test_case(test_t t)
{
    printf("fill hdr\n");
    test_case_begin(t, "fill hdr");
    
    sci_msg_t msg;
    unsigned char buffer[SCI_MSG_MAX_SIZE];
    uint8_t type = PHY_TYPE_MPDU_PAYLOAD;
    uint8_t mpdu_format = PHY_MPDU_FORMAT_SOF;
    uint8_t pb_nb = 3;
    uint8_t tonemap_index = 0;
    uint8_t flags = 0x06;
    uint32_t nek[4];
    uint32_t pb_measurement[PHY_PB_MAX_NB];
    uint32_t pb_header[PHY_PB_MAX_NB];

    memset(nek, '\0', 4*sizeof(uint32_t));
    memset(pb_measurement, '\0', PHY_PB_MAX_NB*sizeof(uint32_t));
    memset(pb_header, '\0', PHY_PB_MAX_NB*sizeof(uint32_t));

    test_begin(t, "sci init")
    {
        memset(buffer, '\0', SCI_MSG_MAX_SIZE);
        test_fail_unless (0 == sci_msg_init(&msg, buffer, SCI_MSG_MAX_SIZE));
        msg.length = 0;
    } test_end;   

    test_begin(t, "fill hdr")
    {
        test_fail_unless ((0 == maximus_phy_fill_hdr(ctx,
                                                     &msg,
                                                     type,
                                                     mpdu_format,
                                                     pb_nb,
                                                     tonemap_index,
                                                     flags,
                                                     nek,
                                                     pb_measurement,
                                                     pb_header))
                          && (PHY_VERSION == msg.hdr.phy->version)
                          && (type == msg.hdr.phy->type)
                          && (mpdu_format == msg.hdr.phy->mpdu_format)
                          && (pb_nb == msg.hdr.phy->pb_nb)
                          && (tonemap_index == msg.hdr.phy->tonemap_index)
                          && (flags == msg.hdr.phy->flags)
                          && (0 == memcmp (nek, msg.hdr.phy->nek, 4*sizeof(uint32_t)))
                          && (0 == memcmp (pb_measurement, msg.hdr.phy->pb_measurement, pb_nb*sizeof(uint32_t)))
                          && (0 == memcmp (pb_header, msg.hdr.phy->pb_header, pb_nb*sizeof(uint32_t)))
                          && (EINVAL != errno));
    } test_end;

    test_begin(t, "fill hdr with incorrect flags")
    {
        flags = 0x08;
        
        dbg_fatal_try_begin
        {
          test_fail_unless ((-1 == maximus_phy_fill_hdr(ctx,
                                                        &msg,
                                                        type,
                                                        mpdu_format,
                                                        pb_nb,
                                                        tonemap_index,
                                                        flags,
                                                        nek,
                                                        pb_measurement,
                                                        pb_header))
                            && (EINVAL == errno));
        }
        dbg_fatal_try_catch (const char *fatal_message)
        {
          printf("fill hdr with incorrect flags\n%s\n", fatal_message);
        }
        dbg_fatal_try_end;

        // reset errno
        errno = 0;
    } test_end;

    test_begin(t, "fill hdr with incorrect number of PBs")
    {
        flags = 0x00;
        pb_nb = 200;
        
        dbg_fatal_try_begin
        {
          test_fail_unless ((-1 == maximus_phy_fill_hdr(ctx,
                                                        &msg,
                                                        type,
                                                        mpdu_format,
                                                        pb_nb,
                                                        tonemap_index,
                                                        flags,
                                                        nek,
                                                        pb_measurement,
                                                        pb_header))
                            && (EINVAL == errno));
        }
        dbg_fatal_try_catch (const char *fatal_message)
        {
          printf("fill hdr with incorrect number of PBs\n%s\n", fatal_message);
        }
        dbg_fatal_try_end;
        
        // reset errno
        errno = 0;
    } test_end;
}

void maximus_phy_recv_test_case(test_t t)
{
    // for PHY_TYPE_NOISE
    static u16 frequency_noise[PHY_CARRIER_NB];
    static u8 time_noise[PHY_SYMBOL_MAX_NB];
    const unsigned short int max_transfer_size = 255*2; // max value on 8 bits * 2 because size is in words => max transfer size in bytes
    const unsigned short int frequency_noise_nb = 7;
    static phy_chandata_t frequency_noise_chandata[7];
    static phy_chandata_t time_noise_chandata;
    static u8 frequency_noise_data[7][510];
    static u8 time_noise_data[510];
    // for PHY_TYPE_SPECTRUM
    static u64 spectrum[PHY_CARRIER_NB];
    const unsigned short int spectrum_nb = 25;
    static phy_chandata_t spectrum_chandata[25];
    static u8 spectrum_data[SCI_MSG_MAX_SIZE];
    
    sci_msg_t msg;
    unsigned char buffer[SCI_MSG_MAX_SIZE];
    
    printf("recv\n");        
    test_case_begin(t, "recv");

    test_begin(t, "sci init")
    {
        memset(buffer, '\0', SCI_MSG_MAX_SIZE);
        test_fail_unless (0 == sci_msg_init(&msg, buffer, SCI_MSG_MAX_SIZE));
        msg.length = 0;
    } test_end;        

    test_begin(t, "NULL msg")
    {
        dbg_fatal_try_begin
        {
            test_fail_unless ((0 > (maximus_phy_recv(NULL, ctx)))
                              && (EINVAL == errno));
        }
        dbg_fatal_try_catch (const char *fatal_message)
        {
          printf("NULL msg\n%s\n", fatal_message);
        }
        dbg_fatal_try_end;

        // reset errno
        errno = 0;
    } test_end;
    
    test_begin(t, "NULL phy")
    {
        dbg_fatal_try_begin
        {
            test_fail_unless ((0 > (maximus_phy_recv(&msg, NULL)))
                              && (EINVAL == errno));
        }
        dbg_fatal_try_catch (const char *fatal_message)
        {
          printf("NULL phy\n%s\n", fatal_message);
        }
        dbg_fatal_try_end;
        
        // reset errno
        errno = 0;
    } test_end;

    test_begin(t, "recv")
    {
        int pb_counter;
        
        // phy header
        phy_msg_hdr_t phy_hdr;
        u32 nek[4];
        pb_measurement_t pb_measurement[PHY_PB_MAX_NB];
        u32 pb_header[PHY_PB_MAX_NB];
        
        // sci header
        sci_msg_hdr_t sci_hdr;
        
        // sci data
        u32 fc_10 = 1234567890; // for PHY_TYPE_FC_HYBRID_MODE
        u32 fc_av[4]; // for PHY_TYPE_FC_HYBRID_MODE and PHY_TYPE_FC_AV_ONLY_MODE
        u32 prs = 1; // for PHY_TYPE_PRS
        // for PHY_TYPE_MPDU_PAYLOAD
        u8 pb_data[PHY_PB_NB][PHY_PB_MAX_SIZE];
        phy_pb_t phy_pb[PHY_PB_NB];
        // for PHY_TYPE_TONEMAP
        u8 tonemap1_data[PHY_PB_MAX_SIZE];
        u8 tonemap2_data[PHY_CARRIER_NB/2-PHY_PB_MAX_SIZE];
        
        // initialize sci data
        char c = '1';
        memset(nek, 'a', 4*sizeof(u32));
        memset(pb_measurement, '0', PHY_PB_MAX_NB*sizeof(pb_measurement_t));
        memset(pb_header, '0', PHY_PB_MAX_NB*sizeof(u32));
        memset(fc_av, 'b', 4*sizeof(u32));
        memset(frequency_noise, 'c', PHY_CARRIER_NB*sizeof(u16));
        memset(frequency_noise_data, '\0', frequency_noise_nb*max_transfer_size);
        memset(time_noise, 'd', PHY_SYMBOL_MAX_NB*sizeof(u8));
        memset(time_noise_data, '\0', max_transfer_size);
        memset(spectrum, 'e', PHY_CARRIER_NB*sizeof(u64));
        memset(spectrum_data, '\0', SCI_MSG_MAX_SIZE);
        for (pb_counter = 0; pb_counter < PHY_PB_NB; pb_counter++)
        {
            memset(&pb_measurement[pb_counter], c, sizeof(pb_measurement_t));
            c++;
            memset(&pb_header[pb_counter], c, sizeof(u32));
            c++;
            memset(&pb_data[pb_counter][0], c, PHY_PB_MAX_SIZE*sizeof(u8));
            c++;
        }
        memset(tonemap1_data, '9', PHY_PB_MAX_SIZE);
        memset(tonemap2_data, '8', PHY_CARRIER_NB/2-PHY_PB_MAX_SIZE);
                
        // fill the phy header
        phy_hdr.version = PHY_VERSION;
        phy_hdr.type = PHY_TYPE_NONE;
        phy_hdr.mpdu_format = PHY_MPDU_FORMAT_NONE;
        phy_hdr.pb_nb = PHY_PB_NB;
        phy_hdr.tonemap_index = 0;
        phy_hdr.flags = 0x06;
        phy_hdr.reserved = 0;
        memcpy(phy_hdr.nek, nek, 4*sizeof(uint32_t));
        memcpy(phy_hdr.pb_measurement, pb_measurement, PHY_PB_MAX_NB*sizeof(uint32_t));
        memcpy(phy_hdr.pb_header, pb_header, PHY_PB_MAX_NB*sizeof(uint32_t));
        
        for (phy_hdr.type = PHY_TYPE_FC_HYBRID_MODE; phy_hdr.type < PHY_TYPE_NB; phy_hdr.type++)
        {
            /* Fill the sci data. */
            // AV Frame Control (16 octets)
            if ((PHY_TYPE_FC_HYBRID_MODE == phy_hdr.type)
                || (PHY_TYPE_FC_AV_ONLY_MODE == phy_hdr.type))
            {
                    ctx->control.pre_detection = true;
                    test_fail_unless ((16 == sci_msg_push(&msg, 16))
                                      && (EINVAL != errno)
                                      && (ENOSPC != errno));
                    memcpy(msg.data_begin, fc_av, 16);
            }
            // HP1.0.1 Frame Control (4 octets)
            if (PHY_TYPE_FC_HYBRID_MODE == phy_hdr.type)
            {
                test_fail_unless ((4 == sci_msg_push(&msg, 4))
                                  && (EINVAL != errno)
                                  && (ENOSPC != errno));
                memcpy(msg.data_begin, &fc_10, 4);
            }
            // PRS (1 octet, but must be aligned on 4 octets)
            if (PHY_TYPE_PRS == phy_hdr.type)
            {
                ctx->access.medium_state = MAXIMUS_PHY_MEDIUM_PRS0;
                test_fail_unless ((4 == sci_msg_push(&msg, 4))
                                  && (EINVAL != errno)
                                  && (ENOSPC != errno));
                memcpy(msg.data_begin, &prs, 4);
            }
            // PBs
            if (PHY_TYPE_MPDU_PAYLOAD == phy_hdr.type)
            {
                // set mpdu format
                phy_hdr.mpdu_format = PHY_MPDU_FORMAT_BEACON;
                
                for (pb_counter = 0; pb_counter < phy_hdr.pb_nb; pb_counter++)
                {
                    test_fail_unless ((PHY_PB_MAX_SIZE == sci_msg_push(&msg, PHY_PB_MAX_SIZE))
                                      && (EINVAL != errno)
                                      && (ENOSPC != errno));
                    memcpy(msg.data_begin, &pb_data[pb_counter][0], PHY_PB_MAX_SIZE);        
                    phy_pb[pb_counter].pb_rx.blk.data = &pb_data[pb_counter][0];
                    phy_pb[pb_counter].pb_rx.blk.next = &phy_pb[pb_counter+1].pb_rx.blk;
                    if (phy_hdr.pb_nb-1 == pb_counter)
                    {
                        phy_pb[pb_counter].pb_rx.blk.next = NULL;
                    }
                }
                // set pb dma
                ctx->pb_dma.first_pb = phy_pb;
                ctx->pb_dma.current_pb = &ctx->pb_dma.first_pb->pb_rx;
                ctx->pb_dma.nb_total = PHY_PB_NB;
                ctx->pb_dma.nb_ready = PHY_PB_NB;
                ctx->pb_dma.nb_pb_it = PHY_PB_NB-1;
            }
            // tonemap
            if (PHY_TYPE_TONEMAP == phy_hdr.type)
            {
                // reset mpdu format
                phy_hdr.mpdu_format = PHY_MPDU_FORMAT_NONE;
                
                // set number of PBs
                phy_hdr.pb_nb = 2; // tonemap uses 2 blocks
                
                test_fail_unless ((PHY_PB_MAX_SIZE == sci_msg_push(&msg, PHY_PB_MAX_SIZE))
                                  && (EINVAL != errno)
                                  && (ENOSPC != errno));
                memcpy(msg.data_begin, tonemap1_data, PHY_PB_MAX_SIZE);
                test_fail_unless ((PHY_CARRIER_NB/2-PHY_PB_MAX_SIZE == sci_msg_push(&msg, PHY_CARRIER_NB/2-PHY_PB_MAX_SIZE))
                                  && (EINVAL != errno)
                                  && (ENOSPC != errno));
                memcpy(msg.data_begin, tonemap2_data, PHY_CARRIER_NB/2-PHY_PB_MAX_SIZE);
            }
            // noise
            if (PHY_TYPE_NOISE == phy_hdr.type)
            {
                // for frequency noise
                unsigned int i=0;
                phy_chandata_t *current_chandata = frequency_noise_chandata;
                
                // reset SCI message
                memset(buffer, '\0', SCI_MSG_MAX_SIZE);
                test_fail_unless (0 == sci_msg_init(&msg, buffer, SCI_MSG_MAX_SIZE));
                msg.length = 0;
                
                // frequency noise
                for (i=0; i<frequency_noise_nb; i++)
                { 
                    current_chandata->blk.data = &frequency_noise_data[i][0];
                    current_chandata->blk.next = &(frequency_noise_chandata[i+1].blk);
                    current_chandata = PARENT_OF(phy_chandata_t, blk, current_chandata->blk.next);
                }
                current_chandata->blk.next = NULL; // for last chandata
                ctx->pb_dma.first_chandata[MAXIMUS_PHY_CHANDATA_TYPE_FREQUENCY_NOISE] = frequency_noise_chandata;
                
                // time noise
                time_noise_chandata.blk.data = time_noise_data;
                time_noise_chandata.blk.next = NULL;
                ctx->pb_dma.first_chandata[MAXIMUS_PHY_CHANDATA_TYPE_TIME_NOISE] = &time_noise_chandata;
                
                // fill sci data
                test_fail_unless ((PHY_CARRIER_NB*sizeof(u16) == sci_msg_push(&msg, PHY_CARRIER_NB*sizeof(u16)))
                                  && (EINVAL != errno)
                                  && (ENOSPC != errno));        
                memcpy(msg.data_begin, frequency_noise, PHY_CARRIER_NB*sizeof(u16));
                test_fail_unless ((PHY_SYMBOL_MAX_NB*sizeof(u8) == sci_msg_push(&msg, PHY_SYMBOL_MAX_NB*sizeof(u8)))
                                  && (EINVAL != errno)
                                  && (ENOSPC != errno));        
                memcpy(msg.data_begin, time_noise, PHY_SYMBOL_MAX_NB*sizeof(u8));
                
            }
            // spectrum
            if (PHY_TYPE_SPECTRUM == phy_hdr.type)
            {
                unsigned int i=0;
                phy_chandata_t *current_chandata = spectrum_chandata;
                
                // reset SCI message
                memset(buffer, '\0', SCI_MSG_MAX_SIZE);
                test_fail_unless (0 == sci_msg_init(&msg, buffer, SCI_MSG_MAX_SIZE));
                msg.length = 0;
                                
                for (i=0; i<spectrum_nb; i++)
                {
                    current_chandata->blk.data = &spectrum_data[i*max_transfer_size];
                    current_chandata->blk.next = &(spectrum_chandata[i+1].blk);
                    current_chandata = PARENT_OF(phy_chandata_t, blk, current_chandata->blk.next);
                }
                current_chandata->blk.next = NULL;
                ctx->pb_dma.first_chandata[MAXIMUS_PHY_CHANDATA_TYPE_SPECTRUM] = spectrum_chandata;
                
                // fill sci data
                test_fail_unless ((PHY_CARRIER_NB*sizeof(u64) == sci_msg_push(&msg, PHY_CARRIER_NB*sizeof(u64)))
                                  && (EINVAL != errno)
                                  && (ENOSPC != errno));        
                memcpy(msg.data_begin, spectrum, PHY_CARRIER_NB*sizeof(u64));
            }
            // phy header
            test_fail_unless ((sizeof(phy_msg_hdr_t) == sci_msg_push(&msg, sizeof(phy_msg_hdr_t)))
                              && (EINVAL != errno)
                              && (ENOSPC != errno));
            memcpy(msg.data_begin, &phy_hdr, sizeof(phy_msg_hdr_t));
    
            // fill the sci header
            memcpy((unsigned char *)&sci_hdr.magic_id, SCI_MSG_MAGIC, 4);
            sci_hdr.version = SCI_MSG_VERSION;
            sci_hdr.type = SCI_MSG_TYPE_PHY;
            sci_hdr.flags = 0;
            sci_hdr.station_id = my_station.id;
            sci_hdr.length = msg.length - sizeof(sci_msg_hdr_t);
            sci_hdr.msg_id = my_station.sci->current_msg_id | SCI_MSG_ID_STATION;
            sci_hdr.netclock_high = my_station.current_tick_tck >> 32;
            sci_hdr.netclock_low = my_station.current_tick_tck & 0xffffffff;
            msg.sci_hdr = &sci_hdr;
    
            test_fail_unless ((0 <= (maximus_phy_recv(&msg, ctx)))
                              && (EINVAL != errno)
                              && (EPROTO != errno));
                              
            test_fail_unless ((phy_hdr.version == msg.hdr.phy->version)
                              && (phy_hdr.type == msg.hdr.phy->type)
                              && (phy_hdr.mpdu_format == msg.hdr.phy->mpdu_format)
                              && (phy_hdr.pb_nb == msg.hdr.phy->pb_nb)
                              && (phy_hdr.tonemap_index == msg.hdr.phy->tonemap_index)
                              && (phy_hdr.flags == msg.hdr.phy->flags)
                              && (phy_hdr.reserved == msg.hdr.phy->reserved)
                              && (0 == memcmp(phy_hdr.nek, msg.hdr.phy->nek, 4*sizeof(uint32_t)))
                              && (0 == memcmp(phy_hdr.pb_measurement, msg.hdr.phy->pb_measurement, msg.hdr.phy->pb_nb*sizeof(uint32_t)))
                              && (0 == memcmp(phy_hdr.pb_header, msg.hdr.phy->pb_header, msg.hdr.phy->pb_nb*sizeof(uint32_t))));
            
            /* Check results. */
            // test fc_av
            if ((PHY_TYPE_FC_HYBRID_MODE == phy_hdr.type)
                || (PHY_TYPE_FC_AV_ONLY_MODE == phy_hdr.type))
            {
                test_fail_unless (0 == memcmp(fc_av, ctx->control.fc_av, 4*sizeof(u32)));
            }
            // test fc_10
            if (PHY_TYPE_FC_HYBRID_MODE == phy_hdr.type)
            {
                test_fail_unless (fc_10 == ctx->control.fc_10);
            }
            // test PRS
            if (PHY_TYPE_PRS == phy_hdr.type)
            {
                test_fail_unless (2 == ctx->access.cap_medium);
            }
            // test PBs
            if (PHY_TYPE_MPDU_PAYLOAD == phy_hdr.type)
            {
                phy_pb_rx_t *current_pb = &ctx->pb_dma.first_pb->pb_rx;
                
                for (pb_counter = 0; pb_counter < phy_hdr.pb_nb; pb_counter++)
                {
                    test_fail_unless (pb_header[pb_counter] == current_pb->header);
                    test_fail_unless (0 == memcmp(&pb_measurement[pb_counter], &current_pb->pb_measurement, sizeof(pb_measurement_t)));
                    test_fail_unless (0 == memcmp(&pb_data[pb_counter][0], current_pb->blk.data, PHY_PB_MAX_SIZE*sizeof(u8)));
                    current_pb = PARENT_OF(phy_pb_rx_t, blk, current_pb->blk.next);
                }
            }
            // test tonemap
            if (PHY_TYPE_TONEMAP == phy_hdr.type)
            {
                // tonemap uses 2 blocks
                test_fail_unless (0 == memcmp(tonemap1_data, ctx->tm_dma.tonemap[phy_hdr.tonemap_index]->data, PHY_PB_MAX_SIZE));
                test_fail_unless (0 == memcmp(tonemap2_data, ctx->tm_dma.tonemap[phy_hdr.tonemap_index]->next->data, PHY_CARRIER_NB/2-PHY_PB_MAX_SIZE));
            }
            // test noise
            if (PHY_TYPE_NOISE == phy_hdr.type)
            {
                // for frequency noise
                int counter;
                unsigned short int last_chandata_size = 12;
                phy_chandata_t *current_chandata;
                
                // test time noise
                //printf("\ttime noise length = %d\n", strlen((char*)ctx->pb_dma.first_chandata[MAXIMUS_PHY_CHANDATA_TYPE_TIME_NOISE]->blk.data));
                //printf("\ttime noise = %s\n", ctx->pb_dma.first_chandata[MAXIMUS_PHY_CHANDATA_TYPE_TIME_NOISE]->blk.data);
                test_fail_unless (0 == memcmp(time_noise, ctx->pb_dma.first_chandata[MAXIMUS_PHY_CHANDATA_TYPE_TIME_NOISE]->blk.data, PHY_SYMBOL_MAX_NB*sizeof(u8)));
                
                // test frequency noise
                // set current chandata
                current_chandata = ctx->pb_dma.first_chandata[MAXIMUS_PHY_CHANDATA_TYPE_FREQUENCY_NOISE];
                for (counter=0; counter<frequency_noise_nb; counter++)
                {
                    //printf("\tfrequency noise %d length = %d\n", counter, strlen((char*)current_chandata->blk.data));
                    //printf("\tfrequency noise = %s\n", current_chandata->blk.data);
                    if (frequency_noise_nb-1 != counter)
                    {
                        test_fail_unless (0 == memcmp(&frequency_noise[counter*max_transfer_size/2], current_chandata->blk.data, max_transfer_size));
                        current_chandata = PARENT_OF(phy_chandata_t, blk, current_chandata->blk.next);
                    }
                    else
                    {
                        test_fail_unless (0 == memcmp(&frequency_noise[counter*max_transfer_size/2], current_chandata->blk.data, last_chandata_size));
                    }
                }
            }
            // test spectrum
            if (PHY_TYPE_SPECTRUM == phy_hdr.type)
            {
                int counter;
                unsigned short int last_chandata_size = 48;
                // set current chandata
                phy_chandata_t *current_chandata = ctx->pb_dma.first_chandata[MAXIMUS_PHY_CHANDATA_TYPE_SPECTRUM];
                for (counter=0; counter<spectrum_nb; counter++)
                {
                    //printf("\tspectrum %d length = %d\n", counter, strlen((char*)current_chandata->blk.data));
                    //printf("\tspectrum = %s\n", current_chandata->blk.data);
                    if (spectrum_nb-1 != counter)
                    {
                        test_fail_unless (0 == memcmp(&spectrum[counter*max_transfer_size/8], ctx->pb_dma.first_chandata[MAXIMUS_PHY_CHANDATA_TYPE_SPECTRUM]->blk.data, max_transfer_size));
                        current_chandata = PARENT_OF(phy_chandata_t, blk, current_chandata->blk.next);
                    }
                    else
                    {
                        test_fail_unless (0 == memcmp(&spectrum[counter*max_transfer_size/8], ctx->pb_dma.first_chandata[MAXIMUS_PHY_CHANDATA_TYPE_SPECTRUM]->blk.data, last_chandata_size));                      
                    }
                }
            }
        }
    } test_end;
}

void phy_init_test_case(test_t t)
{
    int user_data = 1234567890;

    printf("init\n");
    test_case_begin(t, "init");
    
    test_begin(t, "init")
    {   
        ctx = phy_init ((void*)&user_data,
                        &phy_rx_fc_cb,
                        &phy_access_cb,
                        &phy_access_conf_cb,
                        &phy_pbdma_cb,
                        &phy_deferred_cb);
        test_fail_unless ((EINVAL != errno)
                          && (NULL != ctx) 
                          && (1234567890 == *((int*)ctx->control.user_data))
                          && (&phy_rx_fc_cb == ctx->control.rx_fc_cb)
                          && (&phy_access_cb == ctx->control.access_cb)
                          && (&phy_access_conf_cb == ctx->control.access_conf_cb)
                          && (&phy_pbdma_cb == ctx->control.pbdma_cb)
                          && (&phy_deferred_cb == ctx->control.deferred_cb));
    } test_end;
    
    test_begin(t, "rx fc cb")
    {
        test_fail_unless ((NULL != ctx->control.rx_fc_cb)
                          && ((*ctx->control.rx_fc_cb)(ctx->control.user_data, (u32)my_station.current_tick_tck, ctx->control.fc_av)));
    } test_end;
    
    test_begin(t, "deferred cb")
    {
        test_fail_unless (NULL != ctx->control.deferred_cb);
        (*ctx->control.deferred_cb)(ctx->control.user_data);
    } test_end;
    
    return;
}

void phy_reset_test_case(test_t t)
{
    printf("reset\n");
    test_case_begin(t, "reset");
    
    test_begin(t, "reset")
    {
        phy_reset (ctx);
        test_fail_unless ((EINVAL != errno)
                          && (NULL != ctx) 
                          && (NULL != (int*)ctx->control.user_data)
                          && (NULL != ctx->control.rx_fc_cb)
                          && (NULL != ctx->control.access_cb)
                          && (NULL != ctx->control.access_conf_cb)
                          && (NULL != ctx->control.pbdma_cb)
                          && (NULL != ctx->control.deferred_cb)
                          && (NULL != ctx->access.access_backoff_netclock_cb)
                          && (NULL != ctx->access.access_timer_netclock_cb)
                          && (NULL != ctx->control.tx_frame_netclock_cb)); 
    } test_end;
    
    return;
}

void phy_uninit_test_case(test_t t)
{
    printf("uninit\n");
    test_case_begin(t, "uninit");
  
    test_begin(t, "uninit")
    {
        char data[sizeof(phy_t)];
        
        memset(data, '\0', sizeof(phy_t));
        phy_uninit (ctx);
        test_fail_unless ((EINVAL != errno)
                          && (0 == memcmp(ctx, data, sizeof(phy_t))));
    } test_end;
    
    return;
}

void phy_date_test_case(test_t t)
{
    printf("date\n");
    test_case_begin(t, "date");

    test_begin(t, "date")
    {
        if (NULL != ctx)
        {
            my_station.current_tick_tck = 5;
        }
        test_fail_unless ((NULL != ctx)
                          && ((u32)my_station.current_tick_tck == phy_date(ctx))
                          && (EINVAL != errno));
    } test_end;
    
    return;
}

void phy_tx_fc10_test_case(test_t t)
{
    printf("tx fc10\n");
    test_case_begin(t, "tx fc10");

    test_begin(t, "tx fc10")
    {
        u32 fc_10 = 10;
        phy_tx_fc10 (ctx, fc_10);
        test_fail_unless ((EINVAL != errno)
                          && (NULL != ctx)
                          && (fc_10 == ctx->control.fc_10));
    } test_end;
    
    return;
}

void phy_tx_param_test_case(test_t t)
{
    printf("tx param\n");
    test_case_begin(t, "tx param");
    
    test_begin(t, "tx param")
    {
        phy_fc_mode_t fc_mode = PHY_FC_MODE_HYBRID_1;
        bool short_ppdu = false;
        phy_mod_t mod = PHY_MOD_TM;
        phy_fecrate_t fecrate = PHY_FEC_RATE_1_2;
        phy_pb_size_t pb_size = PHY_PB_SIZE_136;
        phy_gil_t gil = PHY_GIL_417;
        uint tonemap_index = 3;
        
        phy_tx_param (ctx, fc_mode, short_ppdu,
                      mod, fecrate, pb_size,
                      gil, tonemap_index);
        test_fail_unless((fc_mode == ctx->control.fc_mode)
                         && (short_ppdu == ctx->control.short_ppdu)
                         && (mod == ctx->tm_dma.mod)
                         && (fecrate == ctx->tm_dma.fecrate)
                         && (pb_size == ctx->pb_dma.pb_size)
                         && (gil == ctx->tm_dma.gil)
                         && (tonemap_index == ctx->tm_dma.tonemap_index));
        
    } test_end;
            
    return;
}

void phy_tx_frame_test_case(test_t t)
{
    u32 date = 10;
    bool want_conf = true;
    bool stop_tx_on_prp_lost = true;
    u32 fc_av[4];
    tick_t msg_tick_tck;
    
    phy_pb_t pb1;
    phy_pb_t pb2;
    phy_pb_t pb3;
    u8 pb1_data[512];
    u8 pb2_data[512];
    u8 pb3_data[512];
    
    // for tonemask
    u8 tonemask[PHY_CARRIER_NB/8];
        
    // for tonemap
    blk_t tonemap1;
    blk_t tonemap2;
    uint carrier_nb = PHY_CARRIER_NB;
    u8 tonemap1_data[PHY_PB_MAX_SIZE];
    u8 tonemap2_data[PHY_CARRIER_NB/2-PHY_PB_MAX_SIZE];
    uint tonemap_index = 3;
    
    printf("tx frame\n");
    test_case_begin(t, "tx frame");
    
    // set tonemask
    memset(tonemask, '1', (PHY_CARRIER_NB/8)*sizeof(u8));
    phy_set_tonemask (ctx, tonemask, carrier_nb);

    // set tonemap
    memset(tonemap1_data, '2', PHY_PB_MAX_SIZE*sizeof(u8));
    memset(tonemap2_data, '3', (PHY_CARRIER_NB/2-PHY_PB_MAX_SIZE)*sizeof(u8));
    tonemap2.next = NULL;
    tonemap2.data = tonemap2_data;
    tonemap1.next = &tonemap2;
    tonemap1.data = tonemap1_data;
    ctx->tm_dma.mod = PHY_MOD_TM;
    phy_set_tonemap (ctx, tonemap_index, &tonemap1);

    // set fc av
    memset(&fc_av, '\0', 4*sizeof(u32));
    fc_av[0] = 0x00000001; // DT_AV = SOF
    
    test_begin(t, "tx frame")
    {
        phy_tx_frame (ctx, date, want_conf, stop_tx_on_prp_lost, fc_av);
        test_fail_unless ((stop_tx_on_prp_lost == ctx->control.stop_tx_on_prp_lost)
                          && (0 == memcmp(fc_av, ctx->control.fc_av, 4*sizeof(u32))));
    } test_end;
    
    // check that the correct netclock message has been sent to Maximus
    test_begin(t, "first netclock message")
    {
        sci_msg_hdr_t sci_hdr;
        netclock_msg_hdr_t netclock_hdr;
        int fd_in = -1;
        
        netclock_callback_t reference, *found_callback;
        set_node_t *found_node;

        // open pipe
        fd_in =  open(my_station.pipe_out_name, O_RDONLY);

        // read sci header
        test_fail_unless ((-1 != fd_in)
                          && (sizeof(sci_msg_hdr_t) == read(fd_in, &sci_hdr, sizeof(sci_msg_hdr_t))));

        // read netclock header
        test_fail_unless (sizeof(netclock_msg_hdr_t) == read(fd_in, &netclock_hdr, sizeof(netclock_msg_hdr_t)));
       
        // check netclock tick value
        test_fail_unless (date == ntohl(netclock_hdr.tick_low));
    
        // get new network tick
        msg_tick_tck = (((unsigned long long)(ntohl(netclock_hdr.tick_high))) << 32) 
                        | (unsigned long long)(ntohl(netclock_hdr.tick_low));

        // update current tick value
        my_station.current_tick_tck = msg_tick_tck;
        
        // close pipe
        close(fd_in);
        
        /* Remove netclock callback. */
        
        // init callback to find
        reference.id = ntohs(netclock_hdr.id);
        set_node_init(&reference.node);
    
        // find it
        test_fail_unless((found_node = set_find(&my_station.netclock->callback_set, &reference.node)) != NULL);
    
        // remove it from callback set
        found_callback = (netclock_callback_t *)PARENT_OF(netclock_callback_t, node, found_node);
        set_remove(&my_station.netclock->callback_set, found_node);
    } test_end;
    
    // test the first callback
    test_begin(t, "maximus tx fc cb")
    {
        maximus_phy_tx_fc_t tx_fc;
        
        tx_fc.isr = maximus_pending_isrs;
        tx_fc.ctx = ctx;
        tx_fc.want_conf = want_conf;

        maximus_phy_tx_fc_cb ((void*)&tx_fc);
        test_fail_unless ((EINVAL != errno)
                          && (maximus_pending_isrs & (1 << PHY_HAL_INTERRUPT_PHY)));

    } test_end;
   
    test_begin(t, "access conf cb")
    {
        test_fail_unless ((NULL != ctx->control.access_conf_cb)
                          && ((*ctx->control.access_conf_cb)(ctx->control.user_data)));
    } test_end;
   
    // check that the correct phy message has been sent to Maximus
    test_begin(t, "first phy message")
    {
        unsigned char data[SCI_MSG_MAX_SIZE];
        sci_msg_hdr_t *sci_hdr;
        phy_msg_hdr_t *phy_hdr;
        int fd_in = -1;

        // open pipe
        fd_in =  open(my_station.pipe_out_name, O_RDONLY);

        // read sci and phy headers
        memset(data, '\0', SCI_MSG_MAX_SIZE);
        test_fail_unless ((-1 != fd_in)
                          && (sizeof(sci_msg_hdr_t) == read(fd_in, data, sizeof(sci_msg_hdr_t)))
                          && (sizeof(phy_msg_hdr_t) == read(fd_in, data+sizeof(sci_msg_hdr_t), sizeof(phy_msg_hdr_t))));

        // set sci and phy headers pointers
        sci_hdr = (sci_msg_hdr_t *)(data);
        phy_hdr = (phy_msg_hdr_t *)(data+sizeof(sci_msg_hdr_t));

        // check phy header
        test_fail_unless (PHY_TYPE_FC_HYBRID_MODE == phy_hdr->type);
        
        // read the remaining part of message
        test_fail_unless (5*sizeof(u32) == read(fd_in, data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t), 5*sizeof(u32)));

        // check phy data
        test_fail_unless (0 == memcmp(&ctx->control.fc_10, data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t), sizeof(u32)));
        test_fail_unless (0 == memcmp(&ctx->control.fc_av, data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t)+sizeof(u32), 4*sizeof(u32)));
                 
        // close pipe
        close(fd_in);
    } test_end;
        
    // check that the correct netclock message has been sent to Maximus
    test_begin(t, "second netclock message")
    {
        sci_msg_hdr_t sci_hdr;
        netclock_msg_hdr_t netclock_hdr;
        int fd_in = -1;
        uint bits_per_symbol;
        uint symbol_nb = 1;
        uint duration_in_ticks;
        
        netclock_callback_t reference, *found_callback;
        set_node_t *found_node;

        // calculate frame control transimission time
        bits_per_symbol = maximus_dur_bits_per_symbol(ctx->tm_dma.mod,
                                                      ctx->tm_dma.tonemask,
                                                      ctx->tm_dma.tonemap[tonemap_index],
                                                      ctx->tm_dma.carrier_nb);
        if ((PHY_FC_MODE_HYBRID_2 == ctx->control.fc_mode)
            || (PHY_FC_MODE_AV_2 == ctx->control.fc_mode))
        {
            symbol_nb = 2;
        }
        duration_in_ticks = maximus_dur_data_tck(ctx->tm_dma.gil, symbol_nb);

        // open pipe
        fd_in =  open(my_station.pipe_out_name, O_RDONLY);

        // read sci header
        test_fail_unless ((-1 != fd_in)
                          && (sizeof(sci_msg_hdr_t) == read(fd_in, &sci_hdr, sizeof(sci_msg_hdr_t))));

        // read netclock header
        test_fail_unless(sizeof(netclock_msg_hdr_t) == read(fd_in, &netclock_hdr, sizeof(netclock_msg_hdr_t)));
       
        // check netclock tick value
        test_fail_unless (date+duration_in_ticks == ntohl(netclock_hdr.tick_low));
        
        // get new network tick
        msg_tick_tck = (((unsigned long long)(ntohl(netclock_hdr.tick_high))) << 32) 
                        | (unsigned long long)(ntohl(netclock_hdr.tick_low));

        // update current tick value
        my_station.current_tick_tck = msg_tick_tck;
        
        // close pipe
        close(fd_in);
        
        /* Remove netclock callback. */
        
        // init callback to find
        reference.id = ntohs(netclock_hdr.id);
        set_node_init(&reference.node);
    
        // find it
        test_fail_unless((found_node = set_find(&my_station.netclock->callback_set, &reference.node)) != NULL);
    
        // remove it from callback set
        found_callback = (netclock_callback_t *)PARENT_OF(netclock_callback_t, node, found_node);
        set_remove(&my_station.netclock->callback_set, found_node);
    } test_end;
    
    // test the second callback
    test_begin(t, "maximus tx frame cb")
    {
        maximus_phy_tx_frame_t tx_frame;

        memset(pb1_data, 'a', 512);
        memset(pb2_data, 'b', 512);
        memset(pb3_data, 'c', 512);
        pb3.pb_tx.blk.next = NULL;
        pb3.pb_tx.blk.data = pb3_data;
        pb3.pb_tx.header = 3;
        pb2.pb_tx.blk.next = &pb3.pb_tx.blk;
        pb2.pb_tx.blk.data = pb2_data;
        pb2.pb_tx.header = 2;
        pb1.pb_tx.blk.next = &pb2.pb_tx.blk;
        pb1.pb_tx.blk.data = pb1_data;
        pb1.pb_tx.header = 1;
                
        ctx->pb_dma.pb_size = PHY_PB_SIZE_520;
        ctx->pb_dma.nb_total = 3;
        ctx->pb_dma.nb_ready = 3;
        ctx->pb_dma.first_pb = &pb1;

        tx_frame.isr = maximus_pending_isrs;
        tx_frame.ctx = ctx;
        
        maximus_phy_tx_frame_cb ((void*)&tx_frame);
        test_fail_unless ((EINVAL != errno)
                          && (maximus_pending_isrs & (1 << PHY_HAL_INTERRUPT_PHY)));
    } test_end;
   
    // check that the correct phy message has been sent to Maximus
    test_begin(t, "second phy message")
    {
        unsigned char data[SCI_MSG_MAX_SIZE];
        sci_msg_hdr_t *sci_hdr;
        phy_msg_hdr_t *phy_hdr;
        int fd_in = -1;
        unsigned int pb_counter;
        phy_pb_tx_t *current_pb = &ctx->pb_dma.first_pb->pb_tx;

        // open pipe
        fd_in =  open(my_station.pipe_out_name, O_RDONLY);

        // read sci and phy headers
        memset(data, '\0', SCI_MSG_MAX_SIZE);
        test_fail_unless ((-1 != fd_in)
                          && (sizeof(sci_msg_hdr_t) == read(fd_in, data, sizeof(sci_msg_hdr_t)))
                          && (sizeof(phy_msg_hdr_t) == read(fd_in, data+sizeof(sci_msg_hdr_t), sizeof(phy_msg_hdr_t))));

        // set sci and phy headers pointers
        sci_hdr = (sci_msg_hdr_t *)(data);
        phy_hdr = (phy_msg_hdr_t *)(data+sizeof(sci_msg_hdr_t));

        // check phy header
        test_fail_unless (PHY_TYPE_MPDU_PAYLOAD == phy_hdr->type);
        test_fail_unless (PHY_MPDU_FORMAT_SOF == phy_hdr->mpdu_format);
        test_fail_unless (ctx->pb_dma.nb_ready == phy_hdr->pb_nb);
    
        // read the remaining part of message
        test_fail_unless (phy_hdr->pb_nb*PHY_PB_MAX_SIZE == read(fd_in, data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t), phy_hdr->pb_nb*PHY_PB_MAX_SIZE));

        // check pb header and phy data
        for (pb_counter = 0; pb_counter < phy_hdr->pb_nb; pb_counter++)
        {
            test_fail_unless (phy_hdr->pb_header[pb_counter] == current_pb->header);
            test_fail_unless (0 == memcmp(data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t)+(phy_hdr->pb_nb-1-pb_counter)*PHY_PB_MAX_SIZE, current_pb->blk.data, PHY_PB_MAX_SIZE));
            current_pb = PARENT_OF(phy_pb_tx_t, blk, current_pb->blk.next);
        }
                        
        // get new network tick
        msg_tick_tck = (((unsigned long long)(ntohl(sci_hdr->netclock_high))) << 32) 
                        | (unsigned long long)(ntohl(sci_hdr->netclock_low));

        // update current tick value
        my_station.current_tick_tck = msg_tick_tck;
        
        // close pipe
        close(fd_in);
    } test_end;
    
    test_begin(t, "short ppdu")
    {
        // for phy message
        unsigned char data[SCI_MSG_MAX_SIZE];
        sci_msg_hdr_t *sci_hdr;
        phy_msg_hdr_t *phy_hdr;
        int fd_in = -1;
        
        // test the callback
        maximus_phy_tx_fc_t tx_fc;
        tx_fc.isr = maximus_pending_isrs;
        tx_fc.ctx = ctx;
        tx_fc.want_conf = false;
        ctx->control.short_ppdu = true;
        maximus_phy_tx_fc_cb ((void*)&tx_fc);
        test_fail_unless ((EINVAL != errno)
                          && (maximus_pending_isrs & (1 << PHY_HAL_INTERRUPT_PHY)));
    
        // check that the correct phy message has been sent to Maximus
    
        // open pipe
        fd_in =  open(my_station.pipe_out_name, O_RDONLY);

        // read sci and phy headers
        memset(data, '\0', SCI_MSG_MAX_SIZE);
        test_fail_unless ((-1 != fd_in)
                          && (sizeof(sci_msg_hdr_t) == read(fd_in, data, sizeof(sci_msg_hdr_t)))
                          && (sizeof(phy_msg_hdr_t) == read(fd_in, data+sizeof(sci_msg_hdr_t), sizeof(phy_msg_hdr_t))));

        // set sci and phy headers pointers
        sci_hdr = (sci_msg_hdr_t *)(data);
        phy_hdr = (phy_msg_hdr_t *)(data+sizeof(sci_msg_hdr_t));

        // check phy header
        test_fail_unless (PHY_TYPE_FC_HYBRID_MODE == phy_hdr->type);
        
        // read the remaining part of message
        test_fail_unless (5*sizeof(u32) == read(fd_in, data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t), 5*sizeof(u32)));

        // check phy data
        test_fail_unless (0 == memcmp(&ctx->control.fc_10, data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t), sizeof(u32)));
        test_fail_unless (0 == memcmp(&ctx->control.fc_av, data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t)+sizeof(u32), 4*sizeof(u32)));
                 
        // close pipe
        close(fd_in);
    } test_end;
    
    return;
}

void maximus_phy_send_test_case(test_t t)
{
    // for tonemap
    blk_t tonemap1;
    blk_t tonemap2;
    u8 tonemap1_data[PHY_PB_MAX_SIZE];
    u8 tonemap2_data[PHY_CARRIER_NB/2-PHY_PB_MAX_SIZE];
    uint tonemap_index = 3;
    
    printf("maximus phy send\n");
    test_case_begin(t, "maximus phy send");
    
    // set tonemap
    memset(tonemap1_data, '1', PHY_PB_MAX_SIZE*sizeof(u8));
    memset(tonemap2_data, '2', (PHY_CARRIER_NB/2-PHY_PB_MAX_SIZE)*sizeof(u8));
    tonemap2.next = NULL;
    tonemap2.data = tonemap2_data;
    tonemap1.next = &tonemap2;
    tonemap1.data = tonemap1_data;
    ctx->tm_dma.mod = PHY_MOD_TM;
    phy_set_tonemap (ctx, tonemap_index, &tonemap1);
    
    // set tx param
    ctx->access.medium_state = MAXIMUS_PHY_MEDIUM_BUSY;
    ctx->control.rx_tx_mode = false;
    
    // test the tonemap callback
    test_begin(t, "maximus phy send tonemap cb")
    {
        maximus_phy_send_tonemap_cb ((void*)ctx);
        test_fail_unless (EINVAL != errno);
    } test_end;

    // check that the correct phy message has been sent to Maximus
    test_begin(t, "tonemap phy message")
    {
        unsigned char data[SCI_MSG_MAX_SIZE];
        sci_msg_hdr_t *sci_hdr;
        phy_msg_hdr_t *phy_hdr;
        int fd_in = -1;

        // open pipe
        fd_in =  open(my_station.pipe_out_name, O_RDONLY);

        // read sci and phy headers
        memset(data, '\0', SCI_MSG_MAX_SIZE);
        test_fail_unless ((-1 != fd_in)
                          && (sizeof(sci_msg_hdr_t) == read(fd_in, data, sizeof(sci_msg_hdr_t)))
                          && (sizeof(phy_msg_hdr_t) == read(fd_in, data+sizeof(sci_msg_hdr_t), sizeof(phy_msg_hdr_t))));

        // set sci and phy headers pointers
        sci_hdr = (sci_msg_hdr_t *)(data);
        phy_hdr = (phy_msg_hdr_t *)(data+sizeof(sci_msg_hdr_t));

        // check phy header
        test_fail_unless (PHY_TYPE_TONEMAP == phy_hdr->type);
        
        // read the remaining part of message
        test_fail_unless (PHY_CARRIER_NB/2 == read(fd_in, data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t), PHY_CARRIER_NB/2));

        // check phy data
        test_fail_unless (0 == memcmp(tonemap2_data, data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t), PHY_CARRIER_NB/2-PHY_PB_MAX_SIZE));
        test_fail_unless (0 == memcmp(tonemap1_data, data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t)+PHY_CARRIER_NB/2-PHY_PB_MAX_SIZE, PHY_PB_MAX_SIZE));
                 
        // close pipe
        close(fd_in);
    } test_end;
    
    // set medium state
    ctx->access.medium_state = MAXIMUS_PHY_MEDIUM_BUSY;
    
    // test the tonemap callback
    test_begin(t, "maximus phy send noise cb")
    {
        maximus_phy_send_noise_cb ((void*)ctx);
        test_fail_unless (EINVAL != errno);
    } test_end;

    // check that the correct phy message has been sent to Maximus
    test_begin(t, "noise phy message")
    {
        unsigned char data[SCI_MSG_MAX_SIZE];
        sci_msg_hdr_t *sci_hdr;
        phy_msg_hdr_t *phy_hdr;
        int fd_in = -1;

        // open pipe
        fd_in =  open(my_station.pipe_out_name, O_RDONLY);

        // read sci and phy headers
        memset(data, '\0', SCI_MSG_MAX_SIZE);
        test_fail_unless ((-1 != fd_in)
                          && (sizeof(sci_msg_hdr_t) == read(fd_in, data, sizeof(sci_msg_hdr_t)))
                          && (sizeof(phy_msg_hdr_t) == read(fd_in, data+sizeof(sci_msg_hdr_t), sizeof(phy_msg_hdr_t))));

        // set sci and phy headers pointers
        sci_hdr = (sci_msg_hdr_t *)(data);
        phy_hdr = (phy_msg_hdr_t *)(data+sizeof(sci_msg_hdr_t));

        // check phy header
        test_fail_unless (PHY_TYPE_NOISE == phy_hdr->type);
        
        // read time noise
        test_fail_unless (PHY_SYMBOL_MAX_NB*sizeof(uint8_t) == read(fd_in, data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t), PHY_SYMBOL_MAX_NB*sizeof(uint8_t)));
                
        // read frequency noise
        test_fail_unless (PHY_CARRIER_NB*sizeof(uint16_t) == read(fd_in, data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t)+PHY_SYMBOL_MAX_NB*sizeof(uint8_t), PHY_CARRIER_NB*sizeof(uint16_t)));
        
        // check phy data
        test_fail_unless (0 == memcmp(data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t), ctx->pb_dma.first_chandata[MAXIMUS_PHY_CHANDATA_TYPE_TIME_NOISE]->blk.data, PHY_SYMBOL_MAX_NB*sizeof(uint8_t)));
         
        // close pipe
        close(fd_in);
    } test_end;
    
    // set medium state
    ctx->access.medium_state = MAXIMUS_PHY_MEDIUM_BUSY;
    
    // test the tonemap callback
    test_begin(t, "maximus phy send spectrum cb")
    {
        maximus_phy_send_spectrum_cb ((void*)ctx);
        test_fail_unless (EINVAL != errno);
    } test_end;

    // check that the correct phy message has been sent to Maximus
    test_begin(t, "spectrum phy message")
    {
        unsigned char data[SCI_MSG_MAX_SIZE];
        sci_msg_hdr_t *sci_hdr;
        phy_msg_hdr_t *phy_hdr;
        int fd_in = -1;

        // open pipe
        fd_in =  open(my_station.pipe_out_name, O_RDONLY);

        // read sci and phy headers
        memset(data, '\0', SCI_MSG_MAX_SIZE);
        test_fail_unless ((-1 != fd_in)
                          && (sizeof(sci_msg_hdr_t) == read(fd_in, data, sizeof(sci_msg_hdr_t)))
                          && (sizeof(phy_msg_hdr_t) == read(fd_in, data+sizeof(sci_msg_hdr_t), sizeof(phy_msg_hdr_t))));

        // set sci and phy headers pointers
        sci_hdr = (sci_msg_hdr_t *)(data);
        phy_hdr = (phy_msg_hdr_t *)(data+sizeof(sci_msg_hdr_t));

        // check phy header
        test_fail_unless (PHY_TYPE_SPECTRUM == phy_hdr->type);
        
        // read the remaining part of message
        test_fail_unless (PHY_CARRIER_NB*sizeof(uint64_t) == read(fd_in, data+sizeof(sci_msg_hdr_t)+sizeof(phy_msg_hdr_t), PHY_CARRIER_NB*sizeof(uint64_t)));
        
        // close pipe
        close(fd_in);
    } test_end;
}

void phy_set_clock_test_case(test_t t)
{
    printf("set clock\n");
    test_case_begin(t, "set clock");

    test_begin(t, "set clock")
    {
        uint phy_clock = 1234567890;
                
        phy_set_clock (phy_clock);
        test_fail_unless (EINVAL != errno);
    } test_end;

    return;
}

void phy_rx_param_test_case(test_t t)
{
    printf("rx param\n");
    test_case_begin(t, "rx param");
    
    test_begin(t, "rx param")
    {
        phy_fc_mode_t fc_mode = PHY_FC_MODE_HYBRID_2;
        
        phy_rx_param (ctx, fc_mode);
        test_fail_unless (fc_mode == ctx->control.fc_mode);        
    } test_end;
            
    return;
}

void phy_rx_activate_test_case(test_t t)
{
    u32 date = 1234567891;
    bool pre_detection = true;
    bool now = false;
    
    printf("rx activate\n");
    test_case_begin(t, "rx activate");
    
    phy_rx_activate (ctx, now, date, pre_detection);

    // check that the correct netclock message has been sent to Maximus
    test_begin(t, "netclock message")
    {   
        unsigned char data[256];
        netclock_msg_hdr_t *netclock_hdr;
        int fd_in = -1;

        // open pipe
        fd_in =  open(my_station.pipe_out_name, O_RDONLY);

        // read sci header and netclock header
        memset(data, '\0', 256);
        test_fail_unless ((-1 != fd_in)
                          && (sizeof(sci_msg_hdr_t) == read(fd_in, data, sizeof(sci_msg_hdr_t)))
                          && (sizeof(netclock_msg_hdr_t) == read(fd_in, data+sizeof(sci_msg_hdr_t), sizeof(netclock_msg_hdr_t))));

        // set netclock header pointer
        netclock_hdr = (netclock_msg_hdr_t *)(data+sizeof(sci_msg_hdr_t));
       
        // check netclock tick value
        test_fail_unless (date == ntohl(netclock_hdr->tick_low));

        // close pipe
        close(fd_in);
    } test_end;
    
    // test the callback
    test_begin(t, "maximus rx activate cb")
    {
        maximus_phy_rx_activate_t rx_activate;
        rx_activate.isr = maximus_pending_isrs;
        rx_activate.ctx = ctx;
        rx_activate.pre_detection = pre_detection;
        
        maximus_phy_rx_activate_cb ((void*)&rx_activate);
        test_fail_unless ((EINVAL != errno)
                          && (maximus_pending_isrs & (1 << PHY_HAL_INTERRUPT_PHY))
                          && (pre_detection == ctx->control.pre_detection));
    } test_end;
    
    now = true;
    pre_detection = false;
    phy_rx_activate (ctx, now, date, pre_detection);
    test_begin(t, "activate rx now")
    {
        test_fail_unless ((EINVAL != errno)
                          && (pre_detection == ctx->control.pre_detection));
    } test_end;
    
    return;
}

void phy_rx_prepare_test_case(test_t t)
{
    printf("rx prepare\n");
    test_case_begin(t, "rx prepare");

    test_begin(t, "rx prepare")
    {
        bool short_ppdu = false;
        phy_mod_t mod = PHY_MOD_NONE;
        phy_fecrate_t fecrate = PHY_FEC_RATE_NONE;
        phy_pb_size_t pb_size = PHY_PB_SIZE_136;
        phy_gil_t gil = PHY_GIL_417;
        uint tonemap_index = 1234567890;
        uint symbol_nb = 0;
        
        phy_rx_prepare (ctx,
                        short_ppdu,
                        mod,
                        fecrate,
                        pb_size,
                        gil,
                        tonemap_index,
                        symbol_nb);
        test_fail_unless ((EINVAL != errno)
                          && (NULL != ctx)
                          && (tonemap_index == ctx->tm_dma.tonemap_index)
                          && (pb_size == ctx->pb_dma.pb_size)
                          && (0 == ctx->pb_dma.index_current_pb)
                          && (ctx->pb_dma.current_pb == &ctx->pb_dma.first_pb->pb_rx)
                          && (!ctx->pb_dma.pb_crc_error));
    } test_end;
    
    return;
}

void phy_ctrl_test_suite(test_t t)
{
    station_init (&my_station);

    test_suite_begin(t, "phy control");
    phy_init_test_case(t);
    maximus_phy_fill_hdr_test_case(t);
    maximus_phy_recv_test_case(t);
    phy_date_test_case(t);
    phy_tx_fc10_test_case(t);
    phy_tx_param_test_case(t);
    phy_tx_frame_test_case(t);
    maximus_phy_send_test_case(t);
    phy_set_clock_test_case(t);
    phy_rx_param_test_case(t);
    phy_rx_activate_test_case(t);
    phy_rx_prepare_test_case(t);
    phy_reset_test_case(t);
    phy_uninit_test_case(t);
    
    station_down (&my_station);
}