summaryrefslogtreecommitdiff
path: root/hal/phy/maximus/src/maximus_phy_access.c
blob: 59096adfffc4fe5dfe5fbb02223ce749c421e61c (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    hal/phy/maximus/src/maximus_phy_access.c
 * \brief   HAL Phy access functions for Maximus.
 * \ingroup hal_phy_maximus
 */

#include "common/std.h"
#include "ecos/packages/hal/maximus/arch/current/include/hal_host_intr.h"
#include "hal/phy/access.h"
#include "host/station.h"
#include "hal/phy/maximus/inc/maximus_phy_ctx.h"
#include "hal/phy/maximus/inc/maximus_phy_access.h"
#include "hal/phy/maximus/inc/maximus_phy_ctrl.h"
#include "hal/phy/maximus/inc/maximus_interrupts.h"
#include "mac/common/timings.h" // for 'MAC_SLOT_TCK'
#include <string.h> // for 'memset'
#include <errno.h>

/**
 * Maximus access backoff start callback called when the corresponding netclock message is received.
 * \param  data  pointer to phy_t structure.
 * set errno to:
 * - EINVAL if data is null
 * - EPROTO if medium state is not idle
 * if 'netclock_schedule()' fails, it sets errno
 */
void maximus_phy_access_backoff_cb (void *data)
{
    dbg_assert_ptr(data);
    if (NULL == data)
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        phy_t *ctx = (phy_t*)data;

        dbg_assert_ptr(ctx);
        if (NULL == ctx)
        {
            errno = EINVAL;
            station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                        "%s: errno = %d", __FUNCTION__, errno);
        }
        else
        {
            dbg_assert(MAXIMUS_PHY_MEDIUM_IDLE == ctx->access.medium_state);
            if (MAXIMUS_PHY_MEDIUM_IDLE != ctx->access.medium_state)
            {
                errno = EPROTO;
                station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                            "%s: errno = %d because cannot start the PRP if medium state is not IDLE", __FUNCTION__, errno);
            }
            else
            {
                MAXIMUS_PHY_TRACE (M_PRP0, my_station.current_tick_tck);

                // update medium state
                ctx->access.medium_state = MAXIMUS_PHY_MEDIUM_PRS0;

                // reset cap medium
                ctx->access.cap_medium = 0;

                // reset PRS0 = 0
                ctx->access.prs0 = false;

                // reset slot counter
                ctx->access.slot_count = 0;

                // reset TX blocked
                ctx->control.tx_blocked_on_false_alarm = false;

                /* Send a netclock message to Maximus simulator scheduled in a slot time,
                 * to increment slot counter. */

                if (-1 == netclock_schedule(my_station.netclock,
                                            ctx->access.backoff_slot_count_netclock_cb,
                                            NETWORK_CLOCK_TYPE_STATION,
                                            my_station.current_tick_tck + MAC_SLOT_TCK,
                                            &maximus_phy_access_slot_count_cb,
                                            (void*)ctx,
                                            &ctx->access.backoff_slot_count_netclock_id))
                {
                    station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                                "%s: errno = %d", __FUNCTION__, errno);
                    dbg_assert_print(false, "errno = %d when scheduling a netclock message", errno);
                }

               /* Send a netclock message to Maximus simulator scheduled in PRS detection time,
                * to emit or read PRS0. */

                if (-1 == netclock_schedule(my_station.netclock,
                                            ctx->access.backoff_start_netclock_cb,
                                            NETWORK_CLOCK_TYPE_STATION,
                                            my_station.current_tick_tck + MAXIMUS_PHY_PRS_DETECTION_DELAY_TCK,
                                            &maximus_phy_access_prs0_cb,
                                            (void*)ctx,
                                            &ctx->access.backoff_start_netclock_id))
                {
                    station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                                "%s: errno = %d", __FUNCTION__, errno);
                    dbg_assert_print(false, "errno = %d when scheduling a netclock message", errno);
                }
            }
        }
    }
}


/**
 * Maximus access slot count callback called when the corresponding netclock message is received.
 * \param  data  pointer to phy_t structure.
 * set errno to:
 * - EINVAL if data is null
 * - EPROTO if medium state is not PRS0, PRS1 or CW
 * if 'netclock_schedule()' fails, it sets errno
 */
void
maximus_phy_access_slot_count_cb (void *data)
{
    dbg_assert_ptr(data);
    if (NULL == data)
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        phy_t *ctx = (phy_t*)data;

        dbg_assert((MAXIMUS_PHY_MEDIUM_PRS0 == ctx->access.medium_state)
                   || (MAXIMUS_PHY_MEDIUM_PRS1 == ctx->access.medium_state)
                   || (MAXIMUS_PHY_MEDIUM_CW == ctx->access.medium_state)
                   || (MAXIMUS_PHY_MEDIUM_WAIT_CONF == ctx->access.medium_state));
        if ((MAXIMUS_PHY_MEDIUM_PRS0 != ctx->access.medium_state)
            && (MAXIMUS_PHY_MEDIUM_PRS1 != ctx->access.medium_state)
            && (MAXIMUS_PHY_MEDIUM_CW != ctx->access.medium_state)
            && (MAXIMUS_PHY_MEDIUM_WAIT_CONF != ctx->access.medium_state))
        {
            errno = EPROTO;
            station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                        "%s: errno = %d because medium state is not PRS0, PRS1, CW or WAIT_CONF", __FUNCTION__, errno);
        }
        else
        {
            // increment slot count
            ctx->access.slot_count++;

            MAXIMUS_PHY_TRACE (M_SLOT, my_station.current_tick_tck,
                               ctx->access.slot_count);

            /* During PRP, send a netclock message to Maximus simulator scheduled in a slot time,
             * to increment slot counter. */

            if (-1 == netclock_schedule(my_station.netclock,
                                        ctx->access.backoff_slot_count_netclock_cb,
                                        NETWORK_CLOCK_TYPE_STATION,
                                        my_station.current_tick_tck + MAC_SLOT_TCK,
                                        &maximus_phy_access_slot_count_cb,
                                        (void*)ctx,
                                        &ctx->access.backoff_slot_count_netclock_id))
            {
                station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                            "%s: errno = %d", __FUNCTION__, errno);
                dbg_assert_print(false, "errno = %d when scheduling a netclock message", errno);
            }
        }
    }
}


/**
 * Maximus access PRS0 callback called when the corresponding netclock message is received,
 * i.e. when the PRS0 must be sent.
 * \param  data  pointer to phy_t structure.
 * set errno to:
 * - EINVAL if data is null
 * if 'sci_send()' fails, it sets errno
 */
void
maximus_phy_access_prs0_cb (void *data)
{
    dbg_assert_ptr(data);
    if (NULL == data)
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        phy_t *ctx = (phy_t*)data;

        /* According to the CAP value, PHY emits or reads PRS0 and PRS1:
         * - if CAP = 3, PHY emits PRS0 (= 1) and PRS1 (= 1);
         * - if CAP = 2, PHY emits PRS0 (= 1) and reads PRS1;
         * - if CAP = 1, PHY reads PRS0:
         *   - if PRS0 = 0, PHY emits PRS1 (= 1);
         *   - if PRS0 = 1, PHY reads PRS1;
         * - if CAP = 0, PHY reads PRS0 and PRS1. */

        if (2 <= ctx->access.cap_sent)
        {
            /* PHY emits PRS0. */

            // for phy sci message
            sci_msg_t msg;

            // init for phy sci message
            memset(ctx->buffer, '\0', SCI_MSG_MAX_SIZE);

            if (0 != sci_msg_init(&msg, ctx->buffer, SCI_MSG_MAX_SIZE))
            {
                station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                            "%s: errno = %d", __FUNCTION__, errno);
                dbg_assert_print(false, "errno = %d when initializing SCI message", errno);
            }
            else
            {
                // for phy message
                uint32_t iv[3];
                uint32_t nek[4];
                uint32_t pb_measurement[PHY_PB_MAX_NB];
                uint32_t pb_header[PHY_PB_MAX_NB];
                uint8_t prs0 = 1;

                // init for phy message
                memset(iv, '\0', 3*sizeof(uint32_t));
                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));

                /* Send phy message containing PRS0. */

                // fill sci data
                if ((int)sizeof(uint8_t) != sci_msg_push(&msg, sizeof(uint8_t)))
                {
                    station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                                "%s: errno = %d", __FUNCTION__, errno);
                    dbg_assert_print(false, "errno = %d when pushing SCI message", errno);
                }
                else
                {
                    memcpy(msg.data_begin, &prs0, sizeof(u8));

                    // fill phy and sci header
                    if ( (0 != maximus_phy_fill_hdr(ctx,
                                                    &msg,
                                                    PHY_TYPE_PRS,
                                                    PHY_MPDU_FORMAT_NONE,
                                                    0, // pb_nb
                                                    1, // msg_nb
                                                    (uint8_t)ctx->control.next_tx_param.fc_mode,
                                                    (uint8_t)ctx->control.next_tx_param.short_ppdu,
                                                    (uint8_t)ctx->control.next_tx_param.mod,
                                                    (uint8_t)ctx->control.next_tx_param.fecrate,
                                                    (uint8_t)ctx->control.next_tx_param.gil,
                                                    (uint8_t)ctx->control.next_tx_param.tonemap_index,
                                                    0, // tx_id
                                                    PHY_FLAG_CRC_OK,
                                                    0, // symbol_nb
                                                    iv,
                                                    nek,
                                                    pb_measurement,
                                                    pb_header))
                         || (0 != sci_fill_hdr(my_station.sci, &msg, SCI_MSG_TYPE_PHY, 0 /* flags */)) )
                    {
                        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                                    "%s: errno = %d", __FUNCTION__, errno);
                        dbg_assert_print(false, "errno = %d when filling header", errno);
                    }
                    else
                    {
                        // send message
                        if (msg.length != sci_send(my_station.sci, &msg))
                        {
                            station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                                        "%s: errno = %d", __FUNCTION__, errno);
                            dbg_assert_print(false, "errno = %d when sending the PRS0", errno);
                        }
                    }
                }
            }

            // update cap medium
            ctx->access.cap_medium = 2;
        }

        /* Send a netclock message to Maximus simulator scheduled in (a slot time - PRS detection time),
         * to compute PRS0 result. */

        if (-1 == netclock_schedule(my_station.netclock,
                                    ctx->access.backoff_start_netclock_cb,
                                    NETWORK_CLOCK_TYPE_STATION,
                                    my_station.current_tick_tck + MAC_SLOT_TCK - MAXIMUS_PHY_PRS_DETECTION_DELAY_TCK,
                                    &maximus_phy_access_prs0_result_cb,
                                    (void*)ctx,
                                    &ctx->access.backoff_start_netclock_id))
        {
            station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                        "%s: errno = %d", __FUNCTION__, errno);
            dbg_assert_print(false, "errno = %d when scheduling a netclock message", errno);
        }
    }
}


/**
 * Maximus access PRS0 result callback called when the corresponding netclock message is received,
 * i.e. at the end of the PRS0.
 * \param  data  pointer to phy_t structure.
 * set errno to:
 * - EINVAL if data is null
 * if 'netclock_schedule()' fails, it sets errno
 */
void
maximus_phy_access_prs0_result_cb (void *data)
{
    dbg_assert_ptr(data);
    if (NULL == data)
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        phy_t *ctx = (phy_t*)data;

        MAXIMUS_PHY_TRACE (M_PRP1, my_station.current_tick_tck);

        // update medium state
        ctx->access.medium_state = MAXIMUS_PHY_MEDIUM_PRS1;

        // if PHY read PRS0 = 1, we already know that PRP is lost
        if (ctx->access.prs0)
        {
            // set prp result
            ctx->access.prp_result = false;
        }

        /* Send a netclock message to Maximus simulator scheduled in PRS detection time,
         * to emit or read PRS1. */

        if (-1 == netclock_schedule(my_station.netclock,
                                    ctx->access.backoff_start_netclock_cb,
                                    NETWORK_CLOCK_TYPE_STATION,
                                    my_station.current_tick_tck + MAXIMUS_PHY_PRS_DETECTION_DELAY_TCK,
                                    &maximus_phy_access_prs1_cb,
                                    (void*)ctx,
                                    &ctx->access.backoff_start_netclock_id))
        {
            station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                        "%s: errno = %d", __FUNCTION__, errno);
            dbg_assert_print(false, "errno = %d when scheduling a netclock message", errno);
        }
    }
}


/**
 * Maximus access PRS1 callback called when the corresponding netclock message is received,
 * i.e. when the PRS1 must be sent.
 * \param  data  pointer to phy_t structure.
 * set errno to:
 * - EINVAL if data is null
 * if 'sci_send()' or 'netclock_schedule()' fails, it sets errno
 */
void
maximus_phy_access_prs1_cb (void *data)
{
    dbg_assert_ptr(data);
    if (NULL == data)
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        phy_t *ctx = (phy_t*)data;

        /* According to the CAP value, PHY emits or reads PRS0 and PRS1:
         * - if CAP = 3, PHY emits PRS0 (= 1) and PRS1 (= 1);
         * - if CAP = 2, PHY emits PRS0 (= 1) and reads PRS1;
         * - if CAP = 1, PHY reads PRS0:
         *   - if PRS0 = 0, PHY emits PRS1 (= 1);
         *   - if PRS0 = 1, PHY reads PRS1;
         * - if CAP = 0, PHY reads PRS0 and PRS1. */

        if ((3 == ctx->access.cap_sent)
            || ((1 == ctx->access.cap_sent)
                && !ctx->access.prs0))
        {
            /* PHY emits PRS1. */

            // for phy sci message
            sci_msg_t msg;

            // init for phy sci message
            memset(ctx->buffer, '\0', SCI_MSG_MAX_SIZE);

            if (0 != sci_msg_init(&msg, ctx->buffer, SCI_MSG_MAX_SIZE))
            {
                station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                            "%s: errno = %d", __FUNCTION__, errno);
                dbg_assert_print(false, "errno = %d when initializing SCI message", errno);
            }
            else
            {
                // for phy message
                uint32_t iv[3];
                uint32_t nek[4];
                uint32_t pb_measurement[PHY_PB_MAX_NB];
                uint32_t pb_header[PHY_PB_MAX_NB];
                u8 prs1 = 1;

                // init for phy message
                memset(iv, '\0', 3*sizeof(uint32_t));
                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));

                /* Send phy message containing PRS1. */

                // fill sci data
                if ((int)sizeof(uint8_t) != sci_msg_push(&msg, sizeof(uint8_t)))
                {
                    station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                                "%s: errno = %d", __FUNCTION__, errno);
                    dbg_assert_print(false, "errno = %d when pushing SCI message", errno);
                }
                else
                {
                    memcpy(msg.data_begin, &prs1, sizeof(u8));

                    // fill phy and sci header
                    if ( (0 != maximus_phy_fill_hdr(ctx,
                                                    &msg,
                                                    PHY_TYPE_PRS,
                                                    PHY_MPDU_FORMAT_NONE,
                                                    0, // pb_nb
                                                    1, // msg_nb
                                                    (uint8_t)ctx->control.next_tx_param.fc_mode,
                                                    (uint8_t)ctx->control.next_tx_param.short_ppdu,
                                                    (uint8_t)ctx->control.next_tx_param.mod,
                                                    (uint8_t)ctx->control.next_tx_param.fecrate,
                                                    (uint8_t)ctx->control.next_tx_param.gil,
                                                    (uint8_t)ctx->control.next_tx_param.tonemap_index,
                                                    0, // tx_id
                                                    PHY_FLAG_CRC_OK,
                                                    0, // symbol_nb
                                                    iv,
                                                    nek,
                                                    pb_measurement,
                                                    pb_header))
                         || (0 != sci_fill_hdr(my_station.sci, &msg, SCI_MSG_TYPE_PHY, 0 /* flags */)) )
                    {
                        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                                    "%s: errno = %d", __FUNCTION__, errno);
                        dbg_assert_print(false, "errno = %d when filling header", errno);
                    }
                    else
                    {
                        // send message
                        if (msg.length != sci_send(my_station.sci, &msg))
                        {
                            station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                                        "%s: errno = %d", __FUNCTION__, errno);
                            dbg_assert_print(false, "errno = %d when sending the PRS1", errno);
                        }
                    }
                }
            }

            // update cap medium
            ctx->access.cap_medium |= 0x01;            
        }

        /* Send a netclock message to Maximus simulator scheduled in (a slot time - PRS detection time - 1),
         * to compute PRP result one tick before the beginning of the third slot (= first slot of CW). */

        if (-1 == netclock_schedule(my_station.netclock,
                                    ctx->access.backoff_start_netclock_cb,
                                    NETWORK_CLOCK_TYPE_STATION,
                                    my_station.current_tick_tck + MAC_SLOT_TCK - MAXIMUS_PHY_PRS_DETECTION_DELAY_TCK - 1,
                                    &maximus_phy_access_prp_result_cb,
                                    (void*)ctx,
                                    &ctx->access.backoff_start_netclock_id))
        {
            station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                        "%s: errno = %d", __FUNCTION__, errno);
            dbg_assert_print(false, "errno = %d when scheduling a netclock message", errno);
        }
    }
}


/**
 * Maximus access PRP result callback called when the corresponding netclock message is received,
 * i.e. at the end of the PRS1.
 * \param  data  pointer to phy_t structure.
 * set errno to:
 * - EINVAL if data is null
 */
void
maximus_phy_access_prp_result_cb (void *data)
{
    dbg_assert_ptr(data);
    if (NULL == data)
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        phy_t *ctx = (phy_t*)data;

        MAXIMUS_PHY_TRACE (M_PRP_END, my_station.current_tick_tck,
                           ctx->access.cap_sent, ctx->access.cap_medium,
                           ctx->access.cap_mpdu);

        // update medium state
        ctx->access.medium_state = MAXIMUS_PHY_MEDIUM_CW;

        // activate preamble detection
        ctx->control.pre_detection = true;

        /* If cap_sent = cap_medium (cap_sent can be different of cap_mpdu if the CAP was updated during the PRS0),
         * OR cap_mpdu ≥ cap_medium, PRP is won. */

        // compute prp result                
        if ((ctx->access.cap_sent == ctx->access.cap_medium)
            || ((uint)ctx->access.cap_mpdu >= (uint)ctx->access.cap_medium))
        {
            ctx->access.prp_result = true;
        }
        else
        {
            ctx->access.prp_result = false;

            /* If PRP is lost (i.e. the detected CAP is greater than the programmed one), PHY stops Tx. */

            if (ctx->control.stop_tx_on_prp_lost)
            {
                maximus_phy_next_tx_frame_cancel(ctx);
                maximus_phy_current_tx_frame_cancel(ctx);
            }
        }

        ctx->access.backoff_start_netclock_id = 0;
    }
}


/**
 * Maximus access timer program callback called when the corresponding netclock message is received.
 * set errno to:
 * - EINVAL if data or ctx->control.access_cb are null
 */
void maximus_phy_access_timer_cb (void *data)
{
    /* If no PRE has been received, PHY raises an IT_ACCESS when access timer expires,
     * i.e. this callback has to call the 'phy_access_cb'. */

    dbg_assert_ptr(data);
    if (NULL == data)
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        phy_t *ctx = (phy_t*)data;

        dbg_assert_ptr(ctx);
        dbg_assert_ptr(ctx->control.access_cb);
        if ((NULL == ctx)
            || (NULL == ctx->control.access_cb))
        {
            errno = EINVAL;
            station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                        "%s: errno = %d", __FUNCTION__, errno);
        }
        else
        {
            if (MAXIMUS_PHY_MEDIUM_BUSY_RX != ctx->access.medium_state)
            {                
               ctx->control.current_cb = ctx->control.access_cb;
               maximus_pending_isrs |= (1 << PHY_HAL_INTERRUPT_PHY);
            }

            ctx->access.timer_program_netclock_id = 0;
        }
    }
}


/**
 * Cancel the backoff procedure.
 * \param  ctx  phy context.
 * set errno to:
 * - EINVAL if ctx is null
 */
void maximus_phy_access_backoff_cancel (phy_t *ctx)
{
    dbg_assert_ptr(ctx);
    if(NULL == ctx)
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        if (0 != ctx->access.backoff_start_netclock_id)
        {
            /* Send a netclock message to Maximus simulator
             * to unschedule the event sent in 'phy_access_backoff_start'
             * according to ctx->access.backoff_start_netclok_id. */

            if (-1 == netclock_unschedule(my_station.netclock, ctx->access.backoff_start_netclock_id))
            {
                if (ctx->warning_assert)
                {
                    station_log(&my_station, STATION_LOG_WARNING, STATION_LOGTYPE_PHY,
                                "%s: errno = %d when unscheduling a netclock message", __FUNCTION__, errno);
                    dbg_assert_print(false, "errno = %d when unscheduling a netclock message", errno);
                }
            }
            ctx->access.backoff_start_netclock_id = 0;
        }
    }
}


/**
 * Reset the slot count.
 * \param  ctx  phy context.
 * set errno to:
 * - EINVAL if ctx is null
 */
void maximus_phy_access_slot_count_reset (phy_t *ctx)
{
    dbg_assert_ptr(ctx);
    if(NULL == ctx)
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        /* Send a netclock message to Maximus simulator
         * to unschedule the event sent in 'maximus_phy_access_backoff_cb' or 'maximus_phy_access_slot_count_cb',
         * according to ctx->access.backoff_slot_count_netclok_id. */

        if (0 != ctx->access.backoff_slot_count_netclock_id)
        {
            if (-1 == netclock_unschedule(my_station.netclock, ctx->access.backoff_slot_count_netclock_id))
            {
                if (ctx->warning_assert)
                {
                    station_log(&my_station, STATION_LOG_WARNING, STATION_LOGTYPE_PHY,
                                "%s: errno = %d when unscheduling a netclock message", __FUNCTION__, errno);
                    dbg_assert_print(false, "errno = %d when unscheduling a netclock message", errno);
                }
            }
            ctx->access.backoff_slot_count_netclock_id = 0;
        }
    }
}


/**
 * Start the backoff procedure.
 * \param  ctx  phy context
 * \param  date  PRP start date
 * \param  cap  channel access priority
 * set errno to:
 * - EINVAL if ctx is null
 * if 'netclock_schedule()' fails, it sets errno
 *
 * Reset the backoff procedure hardware to start a new backoff procedure at
 * the given date with the given priority.
 */
void
phy_access_backoff_start (phy_t *ctx, u32 date, uint cap)
{
    /* To prepare next Tx, CA starts the backoff procedure,
     * defining a date to start PRP and giving the CAP. */

    dbg_assert_ptr(ctx);
    if (NULL == ctx)
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        MAXIMUS_PHY_TRACE (ACCESS_BACKOFF_START, date, cap);

        // cancel the backoff procedure and reset the slot count
        maximus_phy_access_backoff_cancel(ctx);
        maximus_phy_access_slot_count_reset(ctx);

        // update cap mpdu
        ctx->access.cap_mpdu = cap;

        // update cap sent
        ctx->access.cap_sent = cap;

        if (MAXIMUS_PHY_MEDIUM_CW == ctx->access.medium_state)
        {
            // reset medium state: consider that CW is finished
            ctx->access.medium_state = MAXIMUS_PHY_MEDIUM_IDLE;
        }

        // reset prp result to 'true'
        ctx->access.prp_result = true;

        /* Send a netclock message to Maximus simulator scheduled at the given date. */

        if (-1 == netclock_schedule(my_station.netclock,
                                    ctx->access.backoff_start_netclock_cb,
                                    NETWORK_CLOCK_TYPE_STATION,
                                    maximus_phy_schedule_tick(date),
                                    &maximus_phy_access_backoff_cb,
                                    (void*)ctx,
                                    &ctx->access.backoff_start_netclock_id))
        {
            station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                        "%s: errno = %d", __FUNCTION__, errno);
            dbg_assert_print(false, "errno = %d when scheduling a netclock message", errno);
        }
    }
}


/**
 * Change the cap to a new one.
 * \param  ctx  phy context
 * \param  cap  the new channel access priority
 * set errno to:
 * - EINVAL if ctx is null
 *
 * Update the hardware priority.
 */
void
phy_access_backoff_update (phy_t *ctx, uint cap)
{
    /* CA can change the cap_sent to a new one:
     * - if this occurs before the PRP, the new CAP can be used without any other precaution;
     * - if this occurs during the PRS0, the new CAP can change the PRS1 only (if the new CAP is higher than the old one, the PRS1 will be 1, if it is lower, the PRS1 will be 0);
     * - if this occurs after the PRS0, the CAP can not be changed. */

    dbg_assert_ptr(ctx);
    if (NULL == ctx)
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        MAXIMUS_PHY_TRACE (ACCESS_BACKOFF_UPDATE, cap);

        // update cap mpdu without condition
        ctx->access.cap_mpdu = (u8)cap;

        if ((MAXIMUS_PHY_MEDIUM_PRS0 != ctx->access.medium_state)
            && (MAXIMUS_PHY_MEDIUM_PRS1 != ctx->access.medium_state)
            && (MAXIMUS_PHY_MEDIUM_CW != ctx->access.medium_state))
        {
            /* Set the given cap value into PHY context (cap_sent). */

            ctx->access.cap_sent = (u8)cap;
        }
        else if (MAXIMUS_PHY_MEDIUM_PRS0 == ctx->access.medium_state)
        {
            if ((u8)cap > ctx->access.cap_sent)
            {
                ctx->access.cap_sent |= 0x01; // set PRS1 to 1
            }
            else if ((u8)cap < ctx->access.cap_sent)
            {
                ctx->access.cap_sent &= 0x02; // set PRS1 to 0
            }
        }
    }
}


/**
 * Return the number of slot counted.
 * \param  ctx  phy context
 * \return  slot_count
 * return 0 if it fails with errno =
 * - EINVAL if ctx is null
 */
uint
phy_access_backoff_slot_count (phy_t *ctx)
{
    /* CA can get the number of slots counted.
     * PHY counts slots after the PRP until the next Tx or until a PRE is received.
     * This counter is incremented at the beginning of each new slot.
     * Return the slot_count value of PHY context. */

    uint slot_count = 0;

    dbg_assert_ptr(ctx);
    if (NULL == ctx)
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                    "%s: errno = %d", __FUNCTION__, errno); 
    }
    else
    {
        slot_count = ctx->access.slot_count;
    }

    return slot_count;
}


/**
 * Return true if the PRP is won.
 * \param  ctx  phy context
 * \return  true if CAP_sent == CAP_medium && CAP_mpdu >= CAP_medium
 * return 'false' if it fails with errno =
 * - EINVAL if ctx is null
 */
bool
phy_access_backoff_prp_won (phy_t *ctx)
{
    /* CA can get the PRP result (won/lost).
     * If cap_sent = cap_medium (cap_sent can be different of cap_mpdu if the CAP was updated during the PRS0)
     * OR cap_mpdu ≥ cap_medium, PRP is won.
     * Return the prp_result value of PHY context. */

    bool prp_result = false;

    dbg_assert_ptr(ctx);
    if (NULL == ctx)
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        prp_result = ctx->access.prp_result;
    }

    return prp_result;
}


/**
 * Program the access timer to the given date.
 * \param  ctx  phy context
 * \param  date  timer expiration date
 * set errno to:
 * - EINVAL if ctx is null
 * if 'netclock_schedule()' fails, it sets errno
 *
 * ACCESS interrupt is disabled on preamble reception but not on PRP loss.
 */
void
phy_access_timer_program (phy_t *ctx, u32 date)
{
    /* CA programs the access timer in order to precise the date at which CA decides to prepare its Tx. */

    dbg_assert_ptr(ctx);
    if (NULL == ctx)
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        MAXIMUS_PHY_TRACE (ACCESS_TIMER_PROGRAM, date);

        phy_access_timer_cancel(ctx);

        /* Send a netclock message to Maximus simulator scheduled at the given date. */

        if (-1 == netclock_schedule(my_station.netclock,
                                    ctx->access.timer_program_netclock_cb,
                                    NETWORK_CLOCK_TYPE_STATION,
                                    maximus_phy_schedule_tick(date),
                                    &maximus_phy_access_timer_cb,
                                    (void*)ctx,
                                    &ctx->access.timer_program_netclock_id))
        {
            station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                        "%s: errno = %d", __FUNCTION__, errno);
            dbg_assert_print(false, "errno = %d when scheduling a netclock message", errno);
        }
    }
}


/**
 * Cancel the access timer.
 * \param  ctx  phy context
 * set errno to:
 * - EINVAL if ctx is null
 * if 'netclock_unschedule()' fails, it sets errno
 */
void
phy_access_timer_cancel (phy_t *ctx)
{
    /* When modem is stopped, CA cancels the access timer. */

    dbg_assert_ptr(ctx);
    if(NULL == ctx)
    {
        errno = EINVAL;
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_PHY,
                    "%s: errno = %d", __FUNCTION__, errno);
    }
    else
    {
        MAXIMUS_PHY_TRACE (ACCESS_TIMER_CANCEL);

        /* Send a netclock message to Maximus simulator
         * to unschedule the event sent in the previous function ('phy_access_timer_program')
         * according to ctx->access.timer_program_netclok_id. */

        if (0 != ctx->access.timer_program_netclock_id)
        {
            if (-1 == netclock_unschedule(my_station.netclock, ctx->access.timer_program_netclock_id))
            {
                if (ctx->warning_assert)
                {
                    station_log(&my_station, STATION_LOG_WARNING, STATION_LOGTYPE_PHY,
                                "%s: errno = %d when unscheduling a netclock message", __FUNCTION__, errno);
                    dbg_assert_print(false, "errno = %d when unscheduling a netclock message", errno);
                }
            }
            ctx->access.timer_program_netclock_id = 0;
        }
    }
}