summaryrefslogtreecommitdiff
path: root/cesar/bsu/src/bsu.c
blob: 40df3f51657385fbdc27215560621c555b40309e (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
/* Cesar project {{{
 *
 * Copyright (C) 2010 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    bsu/src/bsu.c
 * \brief   BSU core functions.
 * \ingroup bsu
 */
#include "common/std.h"
#include "lib/bitstream.h"
#include "bsu/bsu.h"
#include "bsu/ntb/ntb.h"
#include "bsu/aclf/aclf.h"
#include "mac/ca/ca.h"
#include "mac/common/timings.h"
#include "bsu/inc/context.h"
#include "bsu/inc/interface.h"
#include <string.h>
#include "hal/arch/arch.h"

/* Define the process invalid value. */
#define BSU_MERGE_PROCESS_INVALID 0xff

/* Define the number of beacon periods to provide to the CA. */
#define BSU_BEACON_PERIOD_NB 4

/* Define the delay to program the timer. */
#define BSU_WAKEUP_DELAY_MS 15

/** Static declaration. */
static bsu_t bsu_global;

/**
 * Remove the MFS beacon.
 * \param  ctx  the module context.
 * \param  index  the MFS index.
 */
static inline void
bsu_mfs_remove (bsu_t *ctx, uint index)
{
    ca_mfs_remove (ctx->ca, ctx->mfs_beacons[index]);
    pbproc_mfs_remove_all (ctx->mfs_beacons[index]);
    mac_store_mfs_remove (ctx->mac_store,
                          PARENT_OF (mfs_t, tx, ctx->mfs_beacons[index]));
    blk_release (ctx->mfs_beacons[index]);
    ctx->mfs_beacons[index] = NULL;
}

/**
 * Reprogram bsu timer.
 * \param  ctx  the module context.
 * \param  date  the date to wake up.
 */
static inline void
bsu_reprogram_timer (bsu_t *ctx, u32 date)
{
    /* Reprogram the timer. */
    hal_timer_instance_program (
        ctx->hal_timer, &ctx->timer,
        date - MAC_MS_TO_TCK (BSU_WAKEUP_DELAY_MS));
}

/**
 * Get the associated AVLN.
 * \param  ctx  the module context.
 * \param  nid  the NID of the AVLN.
 * \param  snid  the SNID of the AVLN.
 * \return  the AVLN object.
 *
 * The AVLNs are static object do not release it.
 */
inline bsu_avln_t*
bsu_avln_get (bsu_t *ctx, u64 nid, u8 snid)
{
    uint i;
    for (i = 0; i < ctx->avlns_nb; i++)
        if (ctx->avlns[i].bs.nid == nid && ctx->avlns[i].bs.snid == snid)
            return &ctx->avlns[i];
    return NULL;
}

/**
 * Add an AVLN.
 * \param  ctx  the module context.
 * \param  nid  the NID of the AVLN.
 * \param  snid  the SNID of the AVLN.
 * \return  the AVLN object.
 *
 * The AVLNs are static object do not release it.
 */
inline bsu_avln_t*
bsu_avln_add (bsu_t *ctx, u64 nid, u8 snid)
{
    bsu_avln_t* avln = bsu_avln_get (ctx, nid, snid);
    if (!avln && ctx->avlns_nb < BSU_FOREIGN_AVLNS_NB)
    {
        avln = &ctx->avlns[ctx->avlns_nb];
        avln->bs.nid = nid;
        avln->bs.snid = snid;
        ctx->avlns_nb++;
    }
    return avln;
}

/**
 * Remove an AVLN.
 * \param  ctx  the module context.
 * \param  nid  the NID of the AVLN.
 * \param  snid  the SNID of the AVLN.
 *
 * The AVLNs are static object do not release it.
 */
inline void
bsu_avln_remove (bsu_t *ctx, u64 nid, u8 snid)
{
    bsu_avln_t *to_remove = NULL;
    uint i, pos;
    for (i = 0; i < ctx->avlns_nb; i++)
    {
        if (ctx->avlns[i].bs.nid == nid && ctx->avlns[i].bs.snid == snid)
        {
            to_remove = &ctx->avlns[i];
            pos = i;
            break;
        }
    }
    if (to_remove)
    {
        for (i = pos; i < ctx->avlns_nb - 1; i++)
            ctx->avlns[i] = ctx->avlns[i+1];
        ctx->avlns_nb--;
    }
}

/**
 * Initialise the process position index object.
 * \param  ctx  the module context.
 * \param  sched  the schedules.
 * \param  proc  the process object.
 * \param  beacon_period_index  the beacon period for the one the schedule is
 * being computed.
 */
static inline void
bsu_schedules_merge_process_init (bsu_t *ctx, bsu_beacon_schedules_t *sched,
                                  bsu_avln_schedules_process_t *proc,
                                  uint beacon_period_index)
{
    uint i;
    proc->ps_alloc_index = BSU_MERGE_PROCESS_INVALID;
    proc->ps = NULL;
    proc->nps_alloc_index = BSU_MERGE_PROCESS_INVALID;
    /* Persistent schedules position index. */
    for (i = 0; i < sched->ps.nb; i++)
    {
        if (sched->ps.ps[i].pscd <= beacon_period_index
            && (u32) (sched->ps.ps[i].pscd + sched->ps.ps[i].cscd)
                >= beacon_period_index)
        {
            proc->ps = &sched->ps.ps[i];
            proc->ps_alloc_index = 0;
            break;
        }
    }
    /* Non persistent schedule index. */
    if (sched->nps.ns)
        proc->nps_alloc_index = 0;
    dbg_assert (proc->nps_alloc_index != BSU_MERGE_PROCESS_INVALID
                || proc->ps_alloc_index != BSU_MERGE_PROCESS_INVALID);
}

/**
 * Take the first allocation available from the persistent schedules.
 * \param  proc_schedule  the process schedule structure to take the first
 * allocation.
 * \return  the first SAI of the persistent schedule.
 */
static inline bsu_beacon_sai_t*
bsu_schedules_process__ps_first_allocation (
    bsu_t *ctx, bsu_avln_schedules_process_t *proc)
{
    bsu_beacon_sai_t *sai = &proc->ps->sais[proc->ps_alloc_index++];
    if (proc->ps_alloc_index == proc->ps->ns)
    {
        proc->ps_alloc_index = BSU_MERGE_PROCESS_INVALID;
        proc->ps = NULL;
    }
    return sai;
}

/**
 * Take the first allocation available from the non persistent schedules.
 * \param  proc_schedule  the process schedule structure to take the first
 * allocation.
 * \return  the first SAI of the non persistent schedule.
 */
static inline bsu_beacon_sai_t*
bsu_schedules_process__nps_first_allocation (
    bsu_t *ctx, bsu_avln_schedules_process_t *proc,
    bsu_beacon_schedules_t *schedules)
{
    bsu_beacon_sai_t *sai = &schedules->nps.sais[proc->nps_alloc_index++];
    if (proc->nps_alloc_index == schedules->nps.ns)
        proc->nps_alloc_index = BSU_MERGE_PROCESS_INVALID;
    return sai;
}

/**
 * Get the lesser schedule.
 * \param  ctx  the module context.
 * \param  asched  the schedules read in the beacon.
 * \param  proc  the process object.
 * \return  the schedule to insert in the CA allocation.
 */
inline bsu_beacon_sai_t*
bsu_schedules_merge_get_lesser (bsu_t *ctx, bsu_beacon_schedules_t *asched,
                                bsu_avln_schedules_process_t *proc)
{
    bsu_beacon_sai_t *sai = NULL;
    /* Persistent schedules lonely. */
    if (proc->ps
        && proc->nps_alloc_index == BSU_MERGE_PROCESS_INVALID
        && proc->ps_alloc_index != BSU_MERGE_PROCESS_INVALID)
        sai = bsu_schedules_process__ps_first_allocation (ctx, proc);
    /* Non persistent schedule alone. */
    else if (!proc->ps
             && proc->nps_alloc_index != BSU_MERGE_PROCESS_INVALID)
        sai = bsu_schedules_process__nps_first_allocation (ctx, proc,
                                                           asched);
    /* Non persistent and persistent schedules are available. */
    else if (proc->nps_alloc_index != BSU_MERGE_PROCESS_INVALID
             && proc->ps_alloc_index != BSU_MERGE_PROCESS_INVALID)
    {
        /* Persistent schedules is before the non persistent one. */
        if (proc->ps->sais[proc->ps_alloc_index].end_time_atu <
            asched->nps.sais[proc->nps_alloc_index].end_time_atu)
            sai = bsu_schedules_process__ps_first_allocation (ctx, proc);
        /* Non persistent schedules is before the persistent one. */
        else
            sai = bsu_schedules_process__nps_first_allocation (ctx, proc,
                                                               asched);
    }
    return sai;
}

/**
 * Add an allocation into CA schedules.
 * \param  ctx  the module context.
 * \param  schedules  the CA schedules.
 * \param  pos  the position in the schedules allocations array.
 * \param  end_time_atu  the end of the allocation.
 * \param  glid  the GLID of the allocation
 */
static inline void
bsu_schedules_merge__allocation_add (bsu_t *ctx, ca_schedule_t *schedules,
                                     uint pos, u32 end_time_atu, u8 glid)
{
    schedules->allocations[pos].end_offset_tck =
        MAC_ATU_TO_TCK (end_time_atu);
    schedules->allocations[pos].glid = glid;
}

/**
 * Merge schedules read from the central beacon into the ca schedules.
 * \param  ctx  the module context.
 * \param  asched  the schedules read in the beacon.
 * \param  schedules  the CA schedules objects to fill.
 * \param  bp_index  the beacon period for the one the schedule is being
 * computed.
 */
inline void
bsu_schedules_merge (bsu_t *ctx, bsu_beacon_schedules_t *asched,
                     ca_schedule_t *schedule, uint bp_index)
{
    uint alloc = 0;
    bsu_beacon_sai_t *sai;
    bsu_avln_schedules_process_t process;
    bsu_schedules_merge_process_init (ctx, asched, &process, bp_index);
    while ((sai = bsu_schedules_merge_get_lesser (ctx, asched, &process)))
    {
        /* Add a hole between the previous allocation end this one. */
        if ((alloc
             && sai->stpf
             && MAC_ATU_TO_TCK(sai->start_time_atu) !=
             schedule->allocations[alloc-1].end_offset_tck)
            || (alloc == 0 && sai->start_time_atu != 0))
        {
            bsu_schedules_merge__allocation_add (ctx, schedule, alloc,
                                                 sai->start_time_atu,
                                                 MAC_LID_SPC_HOLE);
            alloc++;
        }
        /* Add this allocation. */
        bsu_schedules_merge__allocation_add (ctx, schedule, alloc,
                                             sai->end_time_atu,
                                             sai->glid | 0x80);
        alloc++;
    }
    /* store the number of allocations. */
    schedule->allocations_nb = alloc;
    /* Store the end of data for this schedule-> */
    schedule->coexistence_mode = asched->hm[bp_index];
    schedule->nek_switch = asched->nek_switch[bp_index];
    schedule->snid = asched->snid[bp_index];
}

/**
 * End to fill the schedules and provide it to the CA.
 * \param  ctx  the module context.
 * \param  avln  the station avln data containing the schedules read from the
 * beacon.
 * \param  schedules  the CA schedules objects to fill.
 * \param  nb  number of schedules.
 *
 * Merge the schedules read from the beacon.
 * Provide the schedules to the CA.
 */
inline void
bsu_ca_schedules (bsu_t *ctx, bsu_avln_t *avln)
{
    dbg_assert (avln);
    ca_beacon_period_t beacon_period[BSU_BEACON_SCHEDULES_MAX];
    ca_schedule_t *schedule;
    u32 bpsd[BSU_BEACON_SCHEDULES_MAX];
    uint i;
    bsu_aclf_beacon_period_start_date (ctx->aclf, bpsd, COUNT (bpsd));
    /* Get the CA schedules from the next one and for BPSD_NB. */
    for (i = 0; i < COUNT (bpsd); i++)
    {
        schedule = ca_alloc_get_schedule (ctx->ca, ctx->ca_index);
        bsu_schedules_merge (ctx, &avln->bs.schedules, schedule, i);
        beacon_period[i].start_date = bpsd[i];
        beacon_period[i].schedule_index = ctx->ca_index;
        ctx->ca_index = (ctx->ca_index + 1) % CA_SCHEDULE_NB;
    }
    /* Provide the schedules to the CA. */
    ca_alloc_update_beacon_periods (ctx->ca, beacon_period, COUNT (bpsd));
    BSU_TRACE (SCHEDULES, bpsd[0], bpsd[1], bpsd[2]);
}

/**
 * Decrease schedules countdown.
 * \param  ctx  the module context.
 * \param  avln  the AVLN object.
 */
inline void
bsu_avln_schedules_decrease_countdown (bsu_t *ctx, bsu_avln_t *avln)
{
    uint i;
    for (i = 0; i < avln->bs.schedules.ps.nb; i++)
    {
        /* If one of the persistent schedules has the CSCD == 0, it should be
         * removed from the schedules. */
        if (avln->bs.schedules.ps.ps[i].pscd == 0
            && avln->bs.schedules.ps.ps[i].cscd == 0)
        {
            uint j;
            for (j = i; j < avln->bs.schedules.ps.nb - 1; j++)
                avln->bs.schedules.ps.ps[j] = avln->bs.schedules.ps.ps[j+1];
            avln->bs.schedules.ps.nb--;
        }
        if (avln->bs.schedules.ps.nb)
        {
            if (avln->bs.schedules.ps.ps[i].pscd)
                avln->bs.schedules.ps.ps[i].pscd--;
            else if (avln->bs.schedules.ps.ps[i].cscd
                     && (ctx->beacon.vf.nm != BSU_BEACON_NM_CSMA_ONLY
                         || (ctx->beacon.vf.nm == BSU_BEACON_NM_CSMA_ONLY
                             && avln->bs.schedules.ps.ps[i].cscd !=
                             HPAV_SCHEDULE_PERMAMENT_VALUE)))
                avln->bs.schedules.ps.ps[i].cscd--;
        }
    }
}

/**
 * Create the beacon to be send over the PLC.
 * \param  ctx  the module context.
 * \param  type  the beacon type.
 */
static void
bsu_beacon_send_prepare (bsu_t *ctx, bsu_beacon_type_t type)
{
    pb_beacon_t *beacon;
    pbproc_tx_beacon_params_t params;
    bsu_aclf_bto (ctx->aclf, (s16*) params.bto, COUNT (params.bto));
    beacon = bsu_beacon_write (&ctx->beacon, type,
                               ctx->mac_config, &params);
    /* Send the beacon. */
    bsu_beacon_send (ctx, type, beacon, &params);
}

/**
 * BSU timer expires.
 * \param  ud  the module context.
 *
 * Reprogram schedules using previous data, when acting as CCo sends the
 * central beacon.
 */
void
bsu_timer_event_process (void *ud)
{
    bsu_t *ctx = (bsu_t *) ud;
    if (ctx->activate)
    {
        dbg_assert (ctx);
        u32 bpsd[BSU_BEACON_PERIOD_NB];
        bsu_aclf_beacon_period_start_date (ctx->aclf, bpsd, COUNT (bpsd));
        dbg_assert (less_mod2p32 (phy_date (), bpsd[1]));
        if (ctx->is_sta == BSU_UPDATE_STA_TYPE_STA)
            bsu_avln_schedules_decrease_countdown (ctx, ctx->sta_avln);
        else
        {
            /* Are update data late ?. */
            if (less_mod2p32 (ctx->beacon.beacon_period_start_date, bpsd[0]))
                /* Create and send the beacon. */
                bsu_beacon_countdown (&ctx->beacon);
            if (ctx->is_sta == BSU_UPDATE_STA_TYPE_CCO)
                bsu_beacon_send_prepare (ctx, BSU_BEACON_TYPE_CENTRAL);
            if (ctx->is_sta == BSU_UPDATE_STA_TYPE_UCCO
                || (ctx->beacon.bmis.discover.present
                    && ctx->beacon.bmis.discover.tei == ctx->mac_config->tei))
            {
                bsu_beacon_send_prepare (ctx, BSU_BEACON_TYPE_DISCOVER);
                /* Not UCCo discover beacon had been sent set the flag to
                 * false to avoid to resend a discover beacon on next wake up. */
                if (ctx->is_sta != BSU_UPDATE_STA_TYPE_UCCO)
                    ctx->beacon.bmis.discover.present = false;
            }
        }
        bsu_ca_schedules (ctx, ctx->sta_avln);
        bsu_reprogram_timer (ctx, bpsd[2]);
    }
}

/**
 * Provide last data from beacon structure data into AVLN schedules.
 * \param  ctx  the module context.
 */
static inline void
bsu_update_sta_avln_schedules (bsu_t *ctx)
{
    uint i;
    /* Coexistence mode */
    if (ctx->beacon.bmis.change_hm.present)
    {
        ctx->sta_avln->bs.schedules.hm[0] = ctx->beacon.vf.hm;
        for (i = ctx->beacon.bmis.change_hm.hmccd;
             i < COUNT (ctx->sta_avln->bs.schedules.hm);
             i++)
            ctx->sta_avln->bs.schedules.hm[i] =
                ctx->beacon.bmis.change_hm.newhm;
    }
    /* NEK switch. */
    if (ctx->beacon.bmis.eks.present
        && ctx->beacon.bmis.eks.kbc == BSU_BEACON_EKS_KBC_NEK)
    {
        for (i = ctx->beacon.bmis.eks.kccd;
             i < COUNT (ctx->sta_avln->bs.schedules.nek_switch);
             i++)
            ctx->sta_avln->bs.schedules.nek_switch[i] =
                ctx->beacon.bmis.eks.new_eks;
    }
    /* SNID change. */
    if (ctx->beacon.bmis.change_snid.present)
    {
        for (i = ctx->beacon.bmis.change_snid.snidccd;
             i < COUNT (ctx->sta_avln->bs.schedules.snid);
             i++)
            ctx->sta_avln->bs.schedules.snid[i] =
                ctx->beacon.bmis.change_snid.new_snid;
        if (ctx->beacon.bmis.change_snid.snidccd == 0)
            ctx->sta_avln->bs.snid =
                ctx->beacon.bmis.change_snid.new_snid;
    }
    /* Copy schedules. */
    ctx->sta_avln->bs.schedules.ps = ctx->beacon.bmis.ps;
    ctx->sta_avln->bs.schedules.nps = ctx->beacon.bmis.nps;
    ctx->sta_avln->bs.schedules.bpsto = ctx->beacon.bmis.bpsto;
}

bsu_t *
bsu_init (bsu_aclf_t *aclf, mac_config_t *mac_config, phy_t *phy,
          mac_store_t *mac_store, ca_t *ca, sar_t *sar, hal_timer_t *timer)
{
    bsu_t *ctx = &bsu_global;
    dbg_assert (aclf);
    dbg_assert (mac_config);
    dbg_assert (phy);
    dbg_assert (mac_store);
    dbg_assert (ca);
    dbg_assert (sar);
    dbg_assert (timer);
    /* Initialise the context. */
    ctx->aclf = aclf;
    ctx->mac_config = mac_config;
    ctx->phy = phy;
    ctx->mac_store = mac_store;
    ctx->ca = ca;
    ctx->sar = sar;
    ctx->hal_timer = timer;
    /* Initialise the NTB. */
    uint i;
    for (i = 0; i < HPAV_AVLNS_NB_MAX; i++)
        bsu_ntb_init (&ctx->avlns[i].sync);
    ctx->sta_avln = &ctx->poweron;
    /* Initialise the SAR callback. */
    sar_init_beacon_cb (sar, ctx, (sar_beacon_cb_t)  bsu_beacon_recv);
    /* Initialise timer events. */
    hal_timer_instance_init (timer, &ctx->timer, ctx,
                             bsu_timer_event_process);
    /* Bacon part. */
    memset (&ctx->beacon, 0, sizeof (bsu_beacon_t));
    ctx->activate = false;
    ctx->is_sta = BSU_UPDATE_STA_TYPE_STA;
    for (i = 0; i < COUNT (ctx->mfs_beacons); i++)
        ctx->mfs_beacons[i] = NULL;
    for (i = 0; i < COUNT (ctx->beacon_nb_recv); i++)
        ctx->beacon_nb_recv[i] = 0;
    for (i = 0; i < COUNT (ctx->beacon_nb_sent); i++)
        ctx->beacon_nb_sent[i] = 0;
    ctx->ca_index = 0;
    /* Trace. */
    bsu_trace_init (ctx);
    return ctx;
}

void
bsu_init_beacon_cb (bsu_beacon_processed_t cb, void *cb_ud)
{
    bsu_global.ul_cb = cb;
    bsu_global.ul_data = cb_ud;
}

void
bsu_uninit (bsu_t *ctx)
{
    dbg_assert (ctx);
    hal_timer_instance_cancel (ctx->hal_timer, &ctx->timer);
    bsu_ntb_uninit (&ctx->sta_avln->sync);
    uint i;
    for (i = 0; i < BSU_FOREIGN_AVLNS_NB; i++)
        bsu_ntb_uninit (&ctx->avlns[i].sync);
    hal_timer_instance_cancel (ctx->hal_timer, &ctx->timer);
    for (i = 0; i < COUNT (ctx->mfs_beacons); i++)
    {
        if (ctx->mfs_beacons[i])
            bsu_mfs_remove (ctx, i);
    }
    bsu_trace_uninit (ctx);
}

bool
bsu_beacon_process (bsu_t *ctx, pb_beacon_t *beacon,
                    pbproc_rx_beacon_params_t *params)
{
    dbg_assert (ctx);
    dbg_assert (beacon);
    dbg_assert (params);
    /* Check the CRC. */
    bool crc_ok =
        !((pb_t *) beacon)->phy_pb.pb_rx.pb_measurement.crc_error;
    if (crc_ok)
    {
        bsu_beacon_bmi_discover_t discover;
        bsu_beacon_track_info_t tinfo;
        bsu_beacon_read_track_info (beacon, &tinfo);
        ctx->beacon_nb_recv[tinfo.bt]++;
        /* It the beacon from our AVLN ? */
        if (tinfo.bt == BSU_BEACON_TYPE_CENTRAL
            && ctx->is_sta == BSU_UPDATE_STA_TYPE_STA
            && ctx->nid_track == tinfo.nid
            && ctx->snid_track == params->snid
            && ctx->tei_track == tinfo.tei)
        {
            discover.present = false;
            dbg_assert (ctx->is_sta == BSU_UPDATE_STA_TYPE_STA);
            dbg_assert (tinfo.bt == BSU_BEACON_TYPE_CENTRAL);
            u32 bpsto = 0;
            ctx->sta_avln->bs.nid = tinfo.nid;
            ctx->sta_avln->bs.snid = params->snid;
            ctx->sta_avln->bs.schedules.bpsto.bpsto = 0;
            ctx->sta_avln->bs.schedules.bpsto.present = false;
            bsu_beacon_read_schedules (
                beacon, &ctx->sta_avln->bs.schedules, &discover);
            if (ctx->sta_avln->bs.schedules.bpsto.present)
                bpsto = ctx->sta_avln->bs.schedules.bpsto.bpsto;
            /* NTB synchronisation. */
            bsu_ntb_clk_sync (&ctx->sta_avln->sync, ctx->phy,
                              params->bts, params->preamble_sysdate,
                              params->preamble_date);
            /* Configure the clock frequency. */
            bsu_ntb_clock_configure (
                &ctx->sta_avln->sync, ctx->mac_config, ctx->phy);
            bsu_aclf_compute_beacon_period_start_date (
                ctx->aclf, params->bts, (s16*) params->bto,
                ctx->sta_avln->bs.schedules.bpsto.bpsto,
                ctx->sta_avln->sync.ntb_offset_tck);
            /* Create the CA schedules. */
            bsu_ca_schedules (ctx, ctx->sta_avln);
            bsu_reprogram_timer (
                ctx, bsu_aclf_beacon_period_start_date_next (ctx->aclf));
            BSU_TRACE (
                BEACON_PROCESS, phy_date (), params->snid, tinfo.tei,
                tinfo.bt, ctx->sta_avln->sync.ntb_offset_tck);
            /* A discover beacon had been requested ? */
            if (discover.present && discover.tei == ctx->mac_config->tei)
                bsu_beacon_send_prepare (ctx, BSU_BEACON_TYPE_DISCOVER);
        }
        /* Track only Central beacon when associated. */
        else if ((ctx->mac_config->tei == MAC_TEI_UNASSOCIATED
                  && tinfo.bt == BSU_BEACON_TYPE_DISCOVER)
                 || tinfo.bt == BSU_BEACON_TYPE_CENTRAL)
        {
            bsu_avln_t *avln;
            avln = bsu_avln_add (ctx, tinfo.nid, params->snid);
            /* Other AVLN. */
            if (avln && avln != ctx->sta_avln)
            {
                bsu_beacon_read_schedules (beacon, &avln->bs.schedules,
                                           &discover);
                avln->bs.schedules.snid[0] = params->snid;
                /* NTB synchronisation. */
                bsu_ntb_clk_sync (&avln->sync, ctx->phy,
                                  params->bts, params->preamble_sysdate,
                                  params->preamble_date);
                BSU_TRACE (BEACON_PROCESS, phy_date (), params->snid,
                           tinfo.tei, tinfo.bt,
                           avln ? avln->sync.ntb_offset_tck : 0);
            }
        }
    }
    else
    {
        blk_release_desc ((blk_t*) beacon);
        BSU_TRACE (BEACON_CRC_ERROR, phy_date ());
    }
    return crc_ok;
}

/**
 * Check if new persistent schedules is scheduled from the CP and copy it into
 * the BSU context.
 * \param  ctx  the module context.
 * \param  beacon  the beacon data to be used in the future.
 */
inline void
bsu_update__persistent_schedules (bsu_t *ctx, bsu_beacon_t *beacon)
{
    uint index;
    dbg_assert ((beacon->bmis.ps.nb == BSU_BEACON_BMI_PERSISTENT_SCHEDULE_MAX
                 && beacon->bmis.ps.ps[0].pscd + beacon->bmis.ps.ps[0].cscd
                 < beacon->bmis.ps.ps[1].pscd)
                || beacon->bmis.ps.nb
                != BSU_BEACON_BMI_PERSISTENT_SCHEDULE_MAX);
    /* If the persistent schedules number are at the maximum
     * ignore the data from the CP. */
    for (index = ctx->beacon.bmis.ps.nb;
         ctx->beacon.bmis.ps.nb < beacon->bmis.ps.nb;
         index ++)
    {
        ctx->beacon.bmis.ps.nb++;
        ctx->beacon.bmis.ps.ps[index] = beacon->bmis.ps.ps[index];
        uint beacon_delta =
            (bsu_aclf_beacon_period_start_date_next (ctx->aclf)
             - beacon->beacon_period_start_date)
            / bsu_aclf_beacon_period (ctx->aclf);
        if (ctx->beacon.bmis.ps.ps[index].pscd <= beacon_delta)
        {
            beacon_delta -= ctx->beacon.bmis.ps.ps[index].pscd;
            ctx->beacon.bmis.ps.ps[index].pscd = 0;
            if (ctx->beacon.vf.nm != BSU_BEACON_NM_CSMA_ONLY
                || (ctx->beacon.vf.nm == BSU_BEACON_NM_CSMA_ONLY
                    && ctx->beacon.bmis.ps.ps[index].cscd
                    != HPAV_SCHEDULE_PERMAMENT_VALUE))
                ctx->beacon.bmis.ps.ps[index].cscd -= beacon_delta;
        }
        else
            ctx->beacon.bmis.ps.ps[index].pscd -= beacon_delta;
    }
}

void
bsu_update (bsu_beacon_t *beacon, bsu_update_sta_type_t is_sta)
{
    bsu_t *ctx = &bsu_global;
    dbg_assert (ctx);
    dbg_assert (beacon);
    arch_dsr_lock ();
    ctx->is_sta = is_sta;
    bsu_aclf_aclsc (ctx->aclf, is_sta != BSU_UPDATE_STA_TYPE_STA);
    dbg_assert ((beacon->bmis.ps.nb
                 && beacon->bmis.ps.ps[0].ns > 0)
                || beacon->bmis.nps.ns);
    /* If persistent schedules are present and there is more than one. The
     * second can only be present if the first one has its PSCD equal to 0.
     * See specification section 5.1.2 Beacon Period Structure Figure 5-3. */
    dbg_assert (beacon->bmis.ps.nb < BSU_BEACON_BMI_PERSISTENT_SCHEDULE_MAX
                || (beacon->bmis.ps.nb
                    == BSU_BEACON_BMI_PERSISTENT_SCHEDULE_MAX
                    && beacon->bmis.ps.ps[0].pscd == 0
                    && beacon->bmis.ps.ps[0].pscd
                    < beacon->bmis.ps.ps[1].pscd));
    dbg_assert (beacon->vf.hm < MAC_COEXISTENCE_NB);
    /* If the data provided are for the next beacon period. */
    if (lesseq_mod2p32 (bsu_aclf_beacon_period_start_date_next (ctx->aclf),
                        beacon->beacon_period_start_date)
        || !ctx->activate)
        ctx->beacon = *beacon;
    /* Data in the beacon had already expired. */
    else
    {
        /* Copy some payload variant fields. */
        ctx->beacon.vf.ncnr = beacon->vf.ncnr;
        ctx->beacon.vf.npsm = beacon->vf.npsm;
        ctx->beacon.vf.rtsbf = beacon->vf.rtsbf;
        ctx->beacon.vf.ccocap = beacon->vf.ccocap;
        ctx->beacon.vf.hoip = beacon->vf.hoip;
        /* Copy data in non countdown beacon entry. */
        ctx->beacon.bmis.mac_address_present =
            beacon->bmis.mac_address_present;
        ctx->beacon.bmis.discover = beacon->bmis.discover;
        ctx->beacon.bmis.discover_info = beacon->bmis.discover_info;
        ctx->beacon.bmis.bpsto = beacon->bmis.bpsto;
        ctx->beacon.bmis.nps = beacon->bmis.nps;
        ctx->beacon.bmis.region = beacon->bmis.region;
        /* Process the beacon entry with countdowns. */
        bsu_update__persistent_schedules (ctx, beacon);
        if (beacon->bmis.eks.present && !ctx->beacon.bmis.eks.present)
            ctx->beacon.bmis.eks = beacon->bmis.eks;
        if (beacon->bmis.handover.present
            && !ctx->beacon.bmis.handover.present)
            ctx->beacon.bmis.handover = beacon->bmis.handover;
        if (beacon->bmis.relocation.present
            && !ctx->beacon.bmis.relocation.present)
            ctx->beacon.bmis.relocation = beacon->bmis.relocation;
        if (beacon->bmis.aclsc.present
            && !ctx->beacon.bmis.aclsc.present)
            ctx->beacon.bmis.aclsc = beacon->bmis.aclsc;
        if (beacon->bmis.cns.present
            && !ctx->beacon.bmis.cns.present)
            ctx->beacon.bmis.cns = beacon->bmis.cns;
        if (beacon->bmis.change_hm.present
            && !ctx->beacon.bmis.change_hm.present)
            ctx->beacon.bmis.change_hm = beacon->bmis.change_hm;
        if (beacon->bmis.change_snid.present
            && !ctx->beacon.bmis.change_snid.present)
            ctx->beacon.bmis.change_snid = beacon->bmis.change_snid;
        if (beacon->bmis.mac_address_present.present
            && !ctx->beacon.bmis.mac_address_present.present)
            ctx->beacon.bmis.mac_address_present =
                beacon->bmis.mac_address_present;
        if (beacon->bmis.discover_info.present)
            ctx->beacon.bmis.discover_info =
                beacon->bmis.discover_info;
    }
    bsu_update_sta_avln_schedules (ctx);
    arch_dsr_unlock ();
}

void
bsu_track_avln (u64 nid, u16 snid, u8 tei, mac_coexistence_mode_t hm)
{
    uint i;
    bsu_t *ctx = &bsu_global;
    arch_dsr_lock ();
    /* Station acts as STA now. */
    ctx->is_sta = BSU_UPDATE_STA_TYPE_STA;
    bsu_aclf_aclsc (ctx->aclf, false);
    ctx->nid_track = nid;
    ctx->snid_track = snid;
    ctx->tei_track = tei;
    ctx->sta_avln = bsu_avln_add (ctx, nid, snid);
    for (i = 0; i < COUNT (ctx->sta_avln->bs.schedules.hm); i++)
        ctx->sta_avln->bs.schedules.hm [i] = hm;
    for (i = 0; i < COUNT (ctx->sta_avln->bs.schedules.snid); i++)
        ctx->sta_avln->bs.schedules.snid[i] = snid;
    /* If there is still beacon to be sent drop it. */
    for (i = 0; i < COUNT (ctx->mfs_beacons); i++)
    {
        if (ctx->mfs_beacons[i])
            bsu_mfs_remove (ctx, i);
    }
    bsu_ca_schedules (ctx, ctx->sta_avln);
    arch_dsr_unlock ();
}

void
bsu_power_on (u8 snid)
{
    bsu_t *ctx = &bsu_global;
    ctx->sta_avln = &ctx->poweron;
    ctx->sta_avln->bs.nid = ctx->beacon.vf.nid;
    ctx->sta_avln->bs.snid = snid;
    ctx->sta_avln->bs.tei = MAC_TEI_UNASSOCIATED;
    uint i;
    for (i = 0; i < COUNT (ctx->sta_avln->bs.schedules.hm); i++)
    {
        ctx->sta_avln->bs.schedules.hm [i] = ctx->beacon.vf.hm;
        ctx->sta_avln->bs.schedules.snid [i] = snid;
        ctx->sta_avln->bs.schedules.nek_switch [i] = 0;
    }
    ctx->sta_avln->bs.schedules.ps = ctx->beacon.bmis.ps;
    ctx->sta_avln->bs.schedules.nps = ctx->beacon.bmis.nps;
}

void
bsu_activate (bool status)
{
    bsu_t *ctx = &bsu_global;
    dbg_assert (status != ctx->activate);
    ctx->activate = status;
    if (status)
    {
        u32 bpsd[BSU_BEACON_PERIOD_NB];
        bsu_aclf_aclsc (ctx->aclf, true);
        bsu_aclf_activate (ctx->aclf, true);
        /* Program the first schedules in the CA. */
        bsu_aclf_beacon_period_start_date (ctx->aclf, bpsd, COUNT (bpsd));
        bsu_ca_schedules (ctx, ctx->sta_avln);
        bsu_reprogram_timer (
            ctx, bsu_aclf_beacon_period_start_date_next (ctx->aclf));
    }
    /* Stops BSU. */
    else
    {
        uint i;
        hal_timer_instance_cancel (ctx->hal_timer, &ctx->timer);
        for (i = 0; i < HPAV_AVLNS_NB_MAX; i++)
            bsu_ntb_init (&ctx->avlns[i].sync);
        ctx->sta_avln = NULL;
        ctx->avlns_nb = 0;
        bsu_aclf_aclsc (ctx->aclf, false);
        bsu_aclf_activate (ctx->aclf, false);
    }
}

uint
bsu_nek_index_current (void)
{
    bsu_t *ctx = &bsu_global;
    dbg_assert (ctx->sta_avln);
    return ctx->sta_avln->bs.schedules.nek_switch[0];
}

uint
bsu_nek_index_next (void)
{
    bsu_t *ctx = &bsu_global;
    dbg_assert (ctx->sta_avln);
    uint index_max =
        ctx->sta_avln->bs.schedules.nek_switch[BSU_BEACON_SCHEDULES_MAX-1];
    return (index_max + 1) % MAC_NEK_INDEX_NB;
}

void
bsu_update_discover_info (bsu_beacon_bmi_discover_info_t *discover)
{
    bsu_t *ctx = &bsu_global;
    dbg_assert (discover);
    arch_dsr_lock ();
    ctx->beacon.bmis.discover_info = *discover;
    arch_dsr_unlock ();
}