summaryrefslogtreecommitdiff
path: root/cesar/cp/beacon/src/beacon.c
blob: 4930fbd98739afa906e2f2f5c7167e7a9b6ff55e (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
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/beacon/src/beacon.c
 * \brief   Beacon Module.
 * \ingroup cp_beacon
 *
 * The beacon module is responsible for :
 *  - When the station acts as CCo:
 *      - Create the central beacon.
 *      - Get the discover info from the others stations.
 */
#include "common/std.h"
#include "string.h"
#include "stdio.h"

#include "hal/arch/arch.h"

#include "cp/defs.h"
#include "cp/cp.h"
#include "cp/fsm/fsm.h"
#include "cp/beacon/beacon.h"
#include "cp/cco/bw/bw_lib_alloc.h"
#include "cp/beacon/ntb/ntb.h"

#include "mac/common/timings.h"
#include "mac/common/ntb.h"

#include "cp/beacon/inc/beacon.h"
#include "cp/beacon/inc/unpack.h"
#include "cp/beacon/inc/ca_sched.h"
#include "cp/beacon/inc/bentry_size.h"
#include "cp/beacon/inc/beacon_discover.h"
#include "cp/beacon/inc/trace.h"
#include "cp/inc/context.h"

/**
 * Compare the discover info bentry with the previous one.
 * \param  disc1  discover bentry one.
 * \param  disc2  discover bentry two.
 * \return  true if different.
 */
static inline bool
cp_beacon_discover_info_struct_updated (
    cp_beacon_bentry_discover_info_t *disc1,
    cp_beacon_bentry_discover_info_t *disc2)
{
    dbg_assert (disc1);
    dbg_assert (disc2);

    if ((disc1->cco_cap == disc2->cco_cap)
        && (disc1->proxy_net_cap == disc2->proxy_net_cap)
        && (disc1->backup_cco_cap == disc2->backup_cco_cap)
        && (disc1->cco_status == disc2->cco_status)
        && (disc1->pco_status == disc2->pco_status)
        && (disc1->backup_cco_status == disc2->backup_cco_status)
        && (disc1->num_dis_sta == disc2->num_dis_sta)
        && (disc1->num_dis_net == disc2->num_dis_net)
        && (disc1->authentication == disc2->authentication)
        && (disc1->status_user_ap_cco == disc2->status_user_ap_cco))
    {
        return false;
    }

    return true;
}

/** Timer expires.
 * \param  ctx  the CP context.
 */
static void
cp_beacon_timer_expires (cp_t *ctx)
{
    dbg_assert (ctx);

    // Post an event in the FSM beacon timer expires.
    cp_fsm_post_new_event (ctx, bare, BEACON_TIMER_EXPIRES);

    CP_BEACON_TRACE (TIMER, mac_ntb());
}

/**
 * The beacon received from the AVLN.
 * \param  ctx  the module context.
 * \param  beacon  The beacon received.
 *
 * Add the beacon to the list of received beacon, and raise the flag
 * beacon received in STA_CORE
 */
void
cp_beacon_receive (cp_t *ctx, cp_beacon_desc_t * beacon)
{
    dbg_assert (ctx);
    dbg_assert (beacon);

    beacon->next = NULL;
    if (ctx->beacon.list)
    {
        ctx->beacon.list_tail->next = beacon;
        ctx->beacon.list_tail = beacon;
    }
    else
    {
        ctx->beacon.list = beacon;
        ctx->beacon.list_tail = beacon;
    }

    // Raise the flag.
    cp_sta_core_signal_recv_beacon_event (ctx);

    CP_BEACON_TRACE (BEACON_RECV, mac_ntb());
}

/**
 * Fill the unpack structure with the regions from the Regions manager.
 * \param  ctx  the module context.
 * \param  beacon   the unpack structure.
 */
static void
cp_beacon__cco__regions (cp_t *ctx, cp_beacon_unpack_t *beacon)
{
    cp_beacon_unpack_region_t *local;
    cp_cco_region_inl_alloc_t *region;
    dbg_assert (ctx);
    dbg_assert (beacon);

    if ((beacon->bmis.regions.nb_regions = cp_cco_region_get_nb_region (ctx)))
    {
        beacon->bmis.regions.present = true;
        for (region = cp_cco_region_get_region (ctx);
             region;
             region = cp_cco_region_get_next_alloc (ctx, region))
        {
            local = slab_alloc (&beacon->bmis.regions.cache);
            local->et_atu = region->et;
            local->type = region->state;
            cp_beacon_unpack_region_add (beacon, local);
        }
    }
}

/**
 * Fill the ca schedules from the unpack structure to provided it to the CA.
 * \param  unpack  the unpack structure.
 * \param  schedules  the CA schedules.
 */
static void
cp_beacon_ca_schedules_from_unpack (cp_beacon_unpack_t *unpack,
                                    cp_beacon_ca_schedules_t *schedules)
{
    cp_beacon_unpack_schedule_t *sched;

    dbg_assert (unpack);
    dbg_assert (schedules);

    if (unpack->bmis.sched_npersistent.present)
    {
        for (sched = cp_beacon_unpack_schedule_get_first (
                &unpack->bmis.sched_npersistent);
            sched;
            sched = cp_beacon_unpack_schedule_get_next (
                &unpack->bmis.sched_npersistent,
                sched))
            cp_beacon_ca_schedule_copy_and_add (schedules, sched);
    }

    if (unpack->bmis.sched_persistent.present)
    {
        for (sched = cp_beacon_unpack_schedule_get_first (
                &unpack->bmis.sched_persistent);
            sched;
            sched = cp_beacon_unpack_schedule_get_next (
                &unpack->bmis.sched_persistent,
                sched))
            cp_beacon_ca_schedule_copy_and_add (schedules, sched);
    }
}

/**
 * Fill the schedules of the unpack beacon structure using the bwandwidth
 * manager and fill the schedules set for the CA.
 * \param  ctx  the module context.
 * \param  beacon   the unpacked beacon structure.
 * \param  schedules  the set with the mixed schedules for the CA.
 */
static void
cp_beacon__cco__schedules (cp_t *ctx, cp_beacon_unpack_t *beacon,
                           cp_beacon_ca_schedules_t *schedules)
{
    cp_cco_bw_alloc_t *alloc;
    cp_beacon_unpack_schedule_t *sched;

    dbg_assert (ctx);
    dbg_assert (beacon);
    dbg_assert (schedules);

    beacon->bmis.sched_npersistent.nb_sai = 0;
    beacon->bmis.sched_npersistent.nb_nsai = 0;
    for (alloc = cp_cco_bw_get_first_alloc (ctx,
                               CP_CCO_BW_PERSISTENCE_NOT_PERSISTENT);
         alloc;
         alloc = cp_cco_bw_get_next_alloc (ctx, alloc))
    {
        sched = slab_alloc (&beacon->bmis.sched_npersistent.cache);
        sched->stpf = alloc->stpf;
        sched->glid = alloc->glid;
        sched->st_atu = alloc->st_atu;
        sched->et_atu = alloc->et_atu;
        cp_beacon_unpack_schedule_add (&beacon->bmis.sched_npersistent,
                                       sched);

        if (sched->stpf)
            beacon->bmis.sched_npersistent.nb_sai ++;
        else
            beacon->bmis.sched_npersistent.nb_nsai ++;

        cp_beacon_ca_schedule_copy_and_add (schedules, sched);
    }
    beacon->bmis.sched_npersistent.present =
        !set_empty (&beacon->bmis.sched_npersistent.set);

    beacon->bmis.sched_persistent.nb_sai = 0;
    beacon->bmis.sched_persistent.nb_nsai = 0;
    for (alloc = cp_cco_bw_get_first_alloc (ctx,
                               CP_CCO_BW_PERSISTENCE_PERSISTENT);
         alloc;
         alloc = cp_cco_bw_get_next_alloc (ctx, alloc))
    {
        sched = slab_alloc (&beacon->bmis.sched_persistent.cache);
        sched->stpf = alloc->stpf;
        sched->glid = alloc->glid;
        sched->st_atu = alloc->st_atu;
        sched->et_atu = alloc->et_atu;
        cp_beacon_unpack_schedule_add (&beacon->bmis.sched_persistent,
                                       sched);

        if (sched->stpf)
            beacon->bmis.sched_persistent.nb_sai ++;
        else
            beacon->bmis.sched_persistent.nb_nsai ++;

        /* Insert the allocation in the schedules set. */
        cp_beacon_ca_schedule_copy_and_add (schedules, sched);
    }

    beacon->bmis.sched_persistent.present =
        !set_empty (&beacon->bmis.sched_persistent.set);
}

/**
 * Fill the unpack structure to send a beacon.
 * \param  ctx  the module context.
 * \param  unpack  the unpack structure.
 * \param  schedules  the schedule set for the CA.
 * \param  beacon_type  the beacon type.
 */
void
cp_beacon__cco__unpack_fill (cp_t *ctx, cp_beacon_unpack_t *unpack,
                             cp_beacon_ca_schedules_t *schedules,
                             uint beacon_type)
{
    cp_nid_t nid;
    cp_sta_own_data_t *sta;
    cp_net_t *net = NULL;

    dbg_assert (ctx);
    dbg_assert (unpack);
    dbg_assert (schedules);

    /* Initialise the unpack structure. */
    cp_beacon_unpack_init (unpack);

    nid = cp_sta_own_data_get_nid (ctx);
    sta = cp_sta_mgr_get_sta_own_data (ctx);
    if (cp_sta_own_data_get_tei (ctx))
        net = cp_sta_mgr_get_our_avln (ctx);

    unpack->variant_fields.nid_msb = nid >> 22;
    unpack->variant_fields.nid_lsb = nid & BITS_ONES (22);
    unpack->variant_fields.hm = sta->hybrid_mode;
    unpack->variant_fields.stei = cp_sta_own_data_get_tei (ctx);
    unpack->variant_fields.bt = beacon_type;
    unpack->variant_fields.ncnr = CP_BEACON_NON_COORDINATED_NETWORK;
    unpack->variant_fields.npsm = CP_BEACON_NPSM_NOT_ACTIVE;
    unpack->variant_fields.num_slots = 1;
    unpack->variant_fields.slot_usage = cp_sta_mgr_get_slot_usage (ctx);
    unpack->variant_fields.slot_id = net == NULL ? 0 : cp_net_get_slot_id (ctx, net);
    unpack->variant_fields.aclss = 0;
    unpack->variant_fields.hoip = ctx->beacon.hoip.hoip_flag;
    unpack->variant_fields.rtsbf = false;
    unpack->variant_fields.nm = CP_BEACON_NM_CSMA_ONLY;
    unpack->variant_fields.cco_cap = CP_CCO_LEVEL;

    /* Fill the bentries. */
    cp_beacon__cco__regions (ctx, unpack);
    cp_beacon__cco__schedules (ctx, unpack, schedules);
    unpack->bmis.mac_address.present = true;
    unpack->bmis.mac_address.mac_addr = cp_sta_own_data_get_mac_address (ctx);
    unpack->bmis.bpsto.present = true;

    if (beacon_type == CP_BEACON_DISCOVER_BEACON)
    {
        unpack->bmis.discover_info.present = true;
        unpack->bmis.discover_info.cco_cap = CP_CCO_LEVEL;
        unpack->bmis.discover_info.proxy_net_cap = CP_PCO_CAP;
        unpack->bmis.discover_info.backup_cco_cap = CP_BACKUP_CCO_CAP;
        unpack->bmis.discover_info.cco_status =
            cp_sta_own_data_get_cco_status(ctx);
        unpack->bmis.discover_info.pco_status =
            cp_sta_own_data_get_pco_status(ctx);
        unpack->bmis.discover_info.backup_cco_status = false;
        unpack->bmis.discover_info.num_dis_sta =
            cp_sta_mgr_get_num_discovered_stas (ctx);
        unpack->bmis.discover_info.num_dis_net =
            cp_sta_mgr_get_num_discovered_stas (ctx);
        unpack->bmis.discover_info.authentication =
            cp_sta_own_data_get_authenticated_status(ctx);
        unpack->bmis.discover_info.status_user_ap_cco = false;

        /* Verify if the current data are the same as the last one sent. */
        unpack->bmis.discover_info.updated =
            cp_beacon_discover_info_struct_updated (
                &unpack->bmis.discover_info,
                &ctx->beacon.discover_info_last);
        ctx->beacon.discover_info_last = unpack->bmis.discover_info;
    }

    /* If the unpack is a central one, check if a discover beacon is can be
     * requested to a station. */
    if ((beacon_type == CP_BEACON_CENTRAL_BEACON)
        && (cp_beacon_discover_need_to_request (ctx,
                                                &unpack->bmis.discover.tei)))
    {
        unpack->bmis.discover.present = true;
    }

    /* If the snid countdown is not 0. */
    if (ctx->beacon.snids.snidcd)
    {
        unpack->bmis.snids.present = true;
        unpack->bmis.snids.snid_cd = ctx->beacon.snids.snidcd;
        unpack->bmis.snids.snid = ctx->beacon.snids.snid;
    }

    /* If the hybrid mode countdown is not 0. */
    if (ctx->beacon.hm.hmcd)
    {
        unpack->bmis.hm.present = true;
        unpack->bmis.hm.hmcd = ctx->beacon.hm.hmcd;
        unpack->bmis.hm.hm = ctx->beacon.hm.hm;
    }

    /* If the handover countdown is not 0. */
    if (ctx->beacon.hoip.hoipcd)
    {
        unpack->bmis.hoip.present = true;
        unpack->bmis.hoip.hoipcd = ctx->beacon.hoip.hoipcd;
        unpack->bmis.hoip.cco = ctx->beacon.hoip.cco;
    }
}

/** Compute the CA schedules using the allocation defined in the bw module.
 * \param  ctx  the CP context.
 * \param  hybrid_mode  the hybrid mode of the medium
 * \param  schedules  the schedules computed from the BW or read in the
 * beacon.
 */
void
cp_beacon_compute_ca_schedules (cp_t *ctx,
                                cp_beacon_ca_schedules_t *schedules)
{
    ca_beacon_period_t beacon_period[CP_SCHED_BEACON_PERIODS_MAX];
    ca_schedule_t *ca_sched;
    cp_beacon_ca_schedule_t *alloc;
    uint i;
    const u32 *bp_start_date;
    cp_sta_own_data_t *own;

    dbg_assert (ctx);
    dbg_assert (schedules);

    own = cp_sta_mgr_get_sta_own_data (ctx);

    /* ADD the beacon periods to the CA. */
    bp_start_date = cp_pwl_get_beacon_period_start_time (ctx);
    dbg_assert (bp_start_date);
    for (i = 0; i < CP_SCHED_BEACON_PERIODS_MAX; i++)
    {
        ca_sched = ca_alloc_get_schedule (ctx->ca,
                                          ctx->beacon.ca_schedule_index);
        ca_sched->allocations_nb = 0;
        ca_sched->nek_switch = ctx->beacon.nek_index;

        if (ctx->beacon.snids.snidcd)
        {
            if (((int)(ctx->beacon.snids.snidcd - i)) > 0)
                /* Until the countdown is not complete, the SNID is still the
                 * one in the station own data. */
                ca_sched->snid = cp_sta_own_data_get_snid (ctx);
            else
                /* The snid is the one in the snid context. */
                ca_sched->snid = ctx->beacon.snids.snid;
        }
        else
            ca_sched->snid = cp_sta_own_data_get_snid (ctx);

        if (ctx->beacon.hm.hmcd)
        {
            if (((int)(ctx->beacon.hm.hmcd - i)) > 0)
                /* Until the countdown is not complete, the Hybrid mode is still the
                 * one in the station own data. */
                ca_sched->coexistence_mode = own->hybrid_mode;
            else
                /* The snid is the one in the snid context. */
                ca_sched->coexistence_mode = ctx->beacon.hm.hm;
        }
        else
            ca_sched->coexistence_mode = own->hybrid_mode;

        // Fill the Schedule.
        for (alloc = cp_beacon_ca_schedule_get_first (schedules);
             alloc;
             alloc = cp_beacon_ca_schedule_get_next (schedules, alloc))
        {
            // If the allocation has a start time present flag a hole must be
            // added before this time.
            if (alloc->stpf
                && (MAC_ATU_TO_TCK(alloc->st_atu)
                    != ca_sched->allocations[
                    ca_sched->allocations_nb - 1].end_offset_tck))
            {
                ca_sched->allocations[
                    ca_sched->allocations_nb].end_offset_tck =
                    MAC_ATU_TO_TCK(alloc->st_atu);
                ca_sched->allocations[ca_sched->allocations_nb].glid =
                    MAC_LID_SPC_HOLE;
                ca_sched->allocations_nb ++;
            }

            ca_sched->allocations
                [ca_sched->allocations_nb].end_offset_tck =
                MAC_ATU_TO_TCK (alloc->et_atu);
            ca_sched->allocations[ca_sched->allocations_nb].glid =
                alloc->glid | 0x80;

            ca_sched->allocations_nb ++;
        }

        // Fill the end of the beacon period with an empty allocation.
        if (less_mod2p32(ca_sched->allocations
                         [ca_sched->allocations_nb - 1]
                         .end_offset_tck, cp_pwl_get_beacon_period_ntb(ctx)))
        {
            ca_sched->allocations[ca_sched->allocations_nb].glid =
                MAC_LID_SHARED_CSMA;
            ca_sched->allocations
                [ca_sched->allocations_nb].end_offset_tck =
                cp_pwl_get_beacon_period_ntb (ctx);
            ca_sched->allocations_nb++;
        }


        beacon_period[i].start_date = bp_start_date[i];
        beacon_period[i].schedule_index = ctx->beacon.ca_schedule_index;

        if ((++ctx->beacon.ca_schedule_index) >= CA_SCHEDULE_NB)
            ctx->beacon.ca_schedule_index = 0;
    }

    CP_BEACON_TRACE (SCHEDULES, mac_ntb(), beacon_period[0].start_date,
                     beacon_period[1].start_date,
                     beacon_period[2].start_date);

    ca_alloc_update_beacon_periods (ctx->ca, beacon_period,
                                    CP_SCHED_BEACON_PERIODS_MAX);
}

/**
 * Create the schedules for the Channel access from the schedules in the
 * unpacked beacon.
 * \param  ctx  the Module context.
 * \param  unpack  the unpack structure.
 * \param  bts  the beacon time stamp.
 * \param  btos  the array of beacon time offset.
 */
static void
cp_beacon_sta_compute_schedules (cp_t *ctx,
                                 cp_beacon_unpack_t  *unpack,
                                 uint bts, s16 *btos)
{
    cp_beacon_ca_schedules_t schedules;
    cp_beacon_unpack_schedule_t *sched;
    cp_beacon_ca_schedule_t *ca_sched;
    dbg_assert (ctx);
    dbg_assert (unpack);
    dbg_assert (btos);

    cp_pwl__sta__compute_beacon_period_start_time (ctx, bts, btos,
                                                   unpack->bmis.bpsto.bpsto);

    /* Convert the schedules from the unpack structure to the schedules ca. */
    cp_beacon_ca_schedule_init (&schedules);

    if (unpack->bmis.sched_npersistent.present)
    {
        for (sched = cp_beacon_unpack_schedule_get_first (
                &unpack->bmis.sched_npersistent);
            sched;
            sched = cp_beacon_unpack_schedule_get_next (
                &unpack->bmis.sched_npersistent, sched))
        {
            ca_sched = slab_alloc (&schedules.cache);
            *ca_sched = *sched;
            cp_beacon_ca_schedule_add (&schedules, ca_sched);
        }
    }

    if (unpack->bmis.sched_persistent.present)
    {
        for (sched = cp_beacon_unpack_schedule_get_first (
                &unpack->bmis.sched_persistent);
            sched;
            sched = cp_beacon_unpack_schedule_get_next (
                &unpack->bmis.sched_persistent, sched))
        {
            ca_sched = slab_alloc (&schedules.cache);
            *ca_sched = *sched;
            cp_beacon_ca_schedule_add (&schedules, ca_sched);
        }
    }

    cp_beacon_compute_ca_schedules (ctx,
                                    &schedules);
    cp_beacon_ca_schedule_uninit (&schedules);
}

/**
 * Send the beacon over the PWL.
 * TODO Implement the proxy beacon part
 *
 * \param  ctx  the cp beacon context
 * \param  beacon the beacon to send
 * \param  beacon_type  the beacon type.
 * \param  mac_address  the station own mac address.
 */
static void
cp_beacon_send_beacon (cp_t *ctx, cp_beacon_desc_t *beacon,
                       uint beacon_type, mac_t mac_address)
{
    mfs_tx_t *mfs_beacon;
    bool added;
    cp_beacon_common_t *common;

    dbg_assert (ctx);
    dbg_assert (ctx->ca);
    dbg_assert (beacon);

    /* Create the MFS
     * see http://pessac/cesar/trac/wiki/SoftMacBeacons#BeaconMFS
     * for more details.
     */
    dbg_assert (ctx->mac_store);
    switch (beacon_type)
    {
        case CP_BEACON_CENTRAL_BEACON:
            mfs_beacon = mac_store_mfs_add_tx (ctx->mac_store, true, false,
                                           MAC_LID_SPC_CENTRAL,
                                           MAC_TEI_BCAST, &added);
            ctx->beacon.common[0].mfs = mfs_beacon;
            mfs_beacon->cap = 0x3;
            common = &ctx->beacon.common[0];
            break;
        case CP_BEACON_DISCOVER_BEACON:
            mfs_beacon = mac_store_mfs_add_tx (ctx->mac_store, true, false,
                                           MAC_LID_DISCOVER,
                                           MAC_TEI_BCAST, &added);
            ctx->beacon.common[1].mfs = mfs_beacon;
            mfs_beacon->cap = 0x2;
            common = &ctx->beacon.common[1];
            break;
        default:
            dbg_assert (false);
    }

    dbg_assert (mfs_beacon);

    if (added)
    {
        blk_addref (mfs_beacon);
        mfs_beacon->beacon = true;
        mfs_beacon->common.ats = false;

        // Add the MFS to the CA
        ca_mfs_add (ctx->ca, mfs_beacon);
    }

    ca_mfs_hold (ctx->ca, mfs_beacon);
    dbg_assert (ctx->interface);
    interface_beacon_prepare (ctx->interface, (pb_beacon_t *)
                              beacon, mac_address, mfs_beacon,
                              &common->bpsto);

    blk_release (mfs_beacon);
}

/**
 * Update the station in the station manager with the data contained in the
 * beacon.
 * \param  ctx  the module context.
 * \param  unpack  the unpack structure.
 * \param  net  the network corresponding to the beacon.
 */
static cp_sta_t *
cp_beacon_update_sta_peer (cp_t *ctx,
                           cp_beacon_unpack_t *unpack,
                           cp_net_t *net)
{
    cp_sta_t *sta;
    uint bt;

    dbg_assert (ctx);
    dbg_assert (unpack);
    dbg_assert (net);

    bt = unpack->variant_fields.bt;
    ctx->beacon.common[bt].nb_beacon_recv++;
    ctx->beacon.common[bt].last_beacon_ntb = mac_ntb();

    sta = cp_sta_mgr_sta_add (ctx, net, unpack->variant_fields.stei,
                             unpack->bmis.mac_address.mac_addr);

    /* Is the station UCCo. */
    if ((unpack->variant_fields.bt == CP_BEACON_DISCOVER_BEACON)
        && (unpack->variant_fields.stei == MAC_TEI_UNASSOCIATED))
    {
        cp_net_set_ucco (ctx, net, sta);
    }
    else if (bt == CP_BEACON_CENTRAL_BEACON)
    {
        cp_net_set_cco (ctx, net, unpack->variant_fields.stei);
        cp_sta_set_authenticated (ctx, sta, true);
    }
    else if (bt == CP_BEACON_PROXY_BEACON)
    {
        cp_net_set_pco (ctx, net, unpack->variant_fields.stei);
        cp_sta_set_authenticated (ctx, sta, true);
    }

    if (unpack->bmis.discover_info.present)
    {
        sta->is_backup_cco =
            unpack->bmis.discover_info.backup_cco_status;
        sta->cco_cap = unpack->variant_fields.cco_cap;
        sta->pco_cap = unpack->bmis.discover_info.proxy_net_cap;
        sta->backup_cco_cap =
            unpack->bmis.discover_info.backup_cco_cap;
        sta->numDisSta = unpack->bmis.discover_info.num_dis_sta;
        sta->numDisNet = unpack->bmis.discover_info.num_dis_net;

        if (unpack->bmis.discover_info.cco_status)
            cp_net_set_cco (ctx, net, unpack->variant_fields.stei);
        if (unpack->bmis.discover_info.pco_status)
            cp_net_set_pco (ctx, net, unpack->variant_fields.stei);

        cp_sta_set_authenticated (ctx, sta,
                                  unpack->bmis.discover_info.authentication);
    }

    return sta;
}

/** Generate and send a discover beacon from the central or proxy beacon.
 * \param  ctx  the CP context.
 * \param  unpack  the unpack structure.
 */
static void
cp_beacon_sta_send_discover_beacon (cp_t *ctx, cp_beacon_unpack_t *unpack)
{
    cp_beacon_desc_t *beacon;
    dbg_assert (ctx);
    dbg_assert (unpack);

    unpack->variant_fields.stei = cp_sta_own_data_get_tei (ctx);
    unpack->variant_fields.bt = CP_BEACON_DISCOVER_BEACON;

    /* Mac address. */
    unpack->bmis.mac_address.present = true;
    unpack->bmis.mac_address.mac_addr = cp_sta_own_data_get_mac_address (ctx);

    /* discover info bentry. */
    unpack->bmis.discover_info.cco_cap = CP_CCO_LEVEL;
    unpack->bmis.discover_info.proxy_net_cap = CP_PCO_CAP;
    unpack->bmis.discover_info.backup_cco_cap = CP_BACKUP_CCO_CAP;
    unpack->bmis.discover_info.cco_status =
        cp_sta_own_data_get_cco_status (ctx);
    unpack->bmis.discover_info.pco_status =
        cp_sta_own_data_get_pco_status(ctx);
    unpack->bmis.discover_info.backup_cco_status = false;
    unpack->bmis.discover_info.num_dis_sta =
        cp_sta_mgr_get_num_discovered_stas (ctx);
    unpack->bmis.discover_info.num_dis_net =
        cp_sta_mgr_get_num_discovered_net (ctx);
    unpack->bmis.discover_info.authentication =
        cp_sta_own_data_get_authenticated_status(ctx);
    unpack->bmis.discover_info.status_user_ap_cco = false;

    /* Verify if the current data are the same as the last one sent. */
    unpack->bmis.discover_info.updated =
        cp_beacon_discover_info_struct_updated (&unpack->bmis.discover_info,
                                            &ctx->beacon.discover_info_last);
    ctx->beacon.discover_info_last = unpack->bmis.discover_info;

    /* Generate the beacon and send. */
    beacon = cp_beacon_unpack__pack (unpack);

    if (unpack->bmis.bpsto.present)
        ctx->beacon.common[unpack->variant_fields.bt].bpsto.bpsto =
            unpack->bmis.bpsto.bpsto_addr;
    cp_beacon_send_beacon (ctx, beacon, CP_BEACON_DISCOVER_BEACON,
                           cp_sta_own_data_get_mac_address (ctx));

    CP_BEACON_TRACE (BEACON_SEND, mac_ntb(), CP_BEACON_DISCOVER_BEACON);
}

/**
 * Countdown decrement and process the necessary data.
 * \param  ctx  the CP context.
 */
static void
cp_beacon_countdowns (cp_t *ctx)
{
    dbg_assert (ctx);

    if (cp_pwl_is_new_beacon_period (ctx))
    {
        /* Snid... */
        if (ctx->beacon.snids.snidcd)
        {
            if (ctx->beacon.snids.snidcd - 1 == 0)
            {
                ctx->beacon.snids.snidcd = 0;
                cp_sta_own_data_set_snid (ctx, ctx->beacon.snids.snid);
            }
            else
                ctx->beacon.snids.snidcd --;
        }

        /* Hybrid mode... */
        if (ctx->beacon.hm.hmcd)
        {
            if (ctx->beacon.hm.hmcd - 1 == 0)
            {
                cp_sta_own_data_t *own = cp_sta_mgr_get_sta_own_data (ctx);

                ctx->beacon.hm.hmcd = 0;
                own->hybrid_mode = ctx->beacon.hm.hm;
            }
            else
                ctx->beacon.hm.hmcd --;
        }

        /* Handover in progress... */
        if (ctx->beacon.hoip.hoipcd)
        {
            if (ctx->beacon.hoip.hoipcd - 1 == 0)
            {
                cp_sta_t *sta;
                cp_net_t *net;

                ctx->beacon.hoip.hoipcd = 0;

                net = cp_sta_mgr_get_our_avln (ctx);
                if (cp_sta_own_data_get_tei (ctx) == ctx->beacon.hoip.cco)
                {
                    cp_fsm_post_new_event (ctx, bare, HOIP_EXPIRED);
                }
                else
                {
                    sta = cp_sta_mgr_sta_get_assoc (ctx, net,
                                                    ctx->beacon.hoip.cco);
                    cp_net_set_cco (ctx, net, ctx->beacon.hoip.cco);
                    cp_fsm_post_new_event (ctx, sta, HOIP_EXPIRED, net, sta);
                }
            }
            else
                ctx->beacon.hoip.hoipcd --;
        }
    }
}

/**
 * Initialise the beacon module.
 * \param  ctx  the module context.
 */
void
cp_beacon_init (cp_t *ctx)
{
    uint i;
    dbg_assert (ctx);

    // Initialise the beacon module.
    memset (&ctx->beacon, 0, sizeof (cp_beacon_t));

    // Initialise the instance of the leon timer.
    hal_timer_instance_init (ctx->hal_timer, &ctx->beacon.leon_timer,
                             ctx,
                            (hal_timer_instance_cb_t) cp_beacon_timer_expires);

    // Initialise the central beacon common data.
    for (i = 0; i < CP_BEACON_TYPE_NB; i++)
        cp_beacon_common_init (&ctx->beacon.common[i]);

    ctx->beacon.ca_sched_first_shoot = true;

    // Initialise the callback in the interface module.
    interface_callback_beacon_init (ctx->interface,
                            (interface_beacon_add_cb_t)cp_beacon_receive,
                            ctx);

    cp_beacon_trace_init (ctx);
}

/**
 * Uninitialise the beacon module.
 * \param  ctx  the module context.
 */
void
cp_beacon_uninit (cp_t *ctx)
{
    uint i;
    cp_beacon_desc_t *beacon;

    // Cancel the Hal timer.
    hal_timer_instance_cancel (ctx->hal_timer, &ctx->beacon.leon_timer);
    hal_timer_instance_uninit (ctx->hal_timer, &ctx->beacon.leon_timer);

    // Release all the beacon in the context.
    while ((beacon = ctx->beacon.list))
    {
        ctx->beacon.list = ctx->beacon.list->next;
        // Release the current one.
        blk_release_desc ( (blk_t *) beacon);
    }
    ctx->beacon.list_tail = NULL;

    // Uninitialise the central beacon common data.
    for (i = 0; i < CP_BEACON_TYPE_NB; i++)
        cp_beacon_common_uninit (&ctx->beacon.common[i], ctx);

    cp_beacon_discover_uninit (&ctx->beacon.discover);

    cp_beacon_trace_uninit (ctx);
}

void
cp_beacon_cco_send_beacon (cp_t *ctx, uint beacon_type)
{
    cp_beacon_common_t *common;
    cp_beacon_unpack_t beacon;
    cp_beacon_desc_t *beacon_real;
    cp_beacon_ca_schedules_t schedules;

    dbg_assert (ctx);
    dbg_assert (beacon_type < CP_BEACON_TYPE_NB);

    // Beacon generated. Provide it to the CA.
    common = &ctx->beacon.common[beacon_type];
    cp_beacon_countdowns (ctx);

    cp_beacon_ca_schedule_init (&schedules);
    cp_beacon__cco__unpack_fill (ctx, &beacon, &schedules, beacon_type);

    if (beacon_type == CP_BEACON_CENTRAL_BEACON
        || beacon_type == CP_BEACON_DISCOVER_BEACON)
    {
        // Compute the PWL beacon period.
        cp_pwl__tracker__compute_beacon_period_start_time (ctx);

        // Copy the BTOs.
        memcpy (common->bpsto.bto, cp_pwl_get_bto (ctx),
                sizeof (common->bpsto.bto));

        cp_beacon_compute_ca_schedules (ctx, &schedules);

        // Program the timer to awake and send another central beacon.
        hal_timer_instance_cancel (ctx->hal_timer, &ctx->beacon.leon_timer);
        hal_timer_instance_program (ctx->hal_timer, &ctx->beacon.leon_timer,
                                    cp_pwl_get_next_timer_date (ctx, true));
        CP_BEACON_TRACE (TIMER_PRGM, mac_ntb(),
                         cp_pwl_get_next_timer_date (ctx, true));
    }

    common->nb_beacon_sent ++;
    beacon_real = cp_beacon_unpack__pack (&beacon);

    if (beacon.bmis.bpsto.present)
        ctx->beacon.common[beacon.variant_fields.bt].bpsto.bpsto =
            beacon.bmis.bpsto.bpsto_addr;

    cp_beacon_send_beacon (ctx, beacon_real, beacon_type,
                           cp_sta_own_data_get_mac_address (ctx));

    /* If the CCo has been choose to send the discover beacon. */
    if (beacon.bmis.discover.present
        && (beacon.bmis.discover.tei == cp_sta_own_data_get_tei (ctx)))
    {
        beacon.variant_fields.bt = CP_BEACON_DISCOVER_BEACON;
        beacon.bmis.discover_info.cco_cap = CP_CCO_LEVEL;
        beacon.bmis.discover_info.proxy_net_cap = CP_PCO_CAP;
        beacon.bmis.discover_info.backup_cco_cap = CP_BACKUP_CCO_CAP;
        beacon.bmis.discover_info.cco_status =
            cp_sta_own_data_get_cco_status(ctx);
        beacon.bmis.discover_info.pco_status =
            cp_sta_own_data_get_pco_status(ctx);
        beacon.bmis.discover_info.backup_cco_status = false;
        beacon.bmis.discover_info.num_dis_sta =
            cp_sta_mgr_get_num_discovered_stas (ctx);
        beacon.bmis.discover_info.num_dis_net =
            cp_sta_mgr_get_num_discovered_stas (ctx);
        beacon.bmis.discover_info.authentication =
            cp_sta_own_data_get_authenticated_status(ctx);
        beacon.bmis.discover_info.status_user_ap_cco = false;

        beacon_real = cp_beacon_unpack__pack (&beacon);
        if (beacon.bmis.bpsto.present)
            ctx->beacon.common[beacon.variant_fields.bt].bpsto.bpsto =
                beacon.bmis.bpsto.bpsto_addr;

        // Copy the BTOs.
        memcpy (&ctx->beacon.common[beacon.variant_fields.bt].bpsto.bto,
                cp_pwl_get_bto (ctx),
                sizeof (common->bpsto.bto));

        cp_beacon_send_beacon (ctx, beacon_real, CP_BEACON_DISCOVER_BEACON,
                               cp_sta_own_data_get_mac_address (ctx));
    }

    cp_beacon_ca_schedule_uninit (&schedules);
    cp_beacon_unpack_uninit (&beacon);
    CP_BEACON_TRACE (BEACON_SEND, mac_ntb(), beacon_type);
}

/**
 * Generate and send a discover beacon.
 * \param  ctx  the module context.
 *
 */
void
cp_beacon_cco_send_central_beacon (cp_t *ctx)
{
    dbg_assert (ctx);

    cp_beacon_cco_send_beacon (ctx,
                               CP_BEACON_CENTRAL_BEACON);
}

/**
 * Generate and send a discover beacon.
 * \param  ctx  the module context.
 *
 */
void
cp_beacon_cco_send_discover_beacon (cp_t *ctx)
{
    dbg_assert (ctx);

    cp_beacon_cco_send_beacon (ctx,
                               CP_BEACON_DISCOVER_BEACON);
}

/**
 * Create default schedules and provide it to the Channel Access.
 * \param  ctx  the module context.
 *
 */
void
cp_beacon_create_default_schedules (cp_t *ctx)
{
    uint i;
    dbg_assert (ctx);
    ca_beacon_period_t beacon_period[CP_SCHED_BEACON_PERIODS_MAX];
    ca_schedule_t *ca_sched;
    const u32 *bp_start_date;

    ca_sched = ca_alloc_get_schedule (ctx->ca,
               ctx->beacon.ca_schedule_index);
    ca_sched->allocations_nb = 1;
    ca_sched->coexistence_mode = 0x2 /* fully hybrid mode. */;
    ca_sched->nek_switch = 0; //TODO

    ca_sched->allocations[0].end_offset_tck = cp_pwl_get_beacon_period_ntb(ctx);
    ca_sched->allocations[0].glid = MAC_LID_SHARED_CSMA;

    /* Only use to synchronise the BP.
     * useful when the station becomes CCo or UCCo.
     */
    cp_pwl__tracker__compute_beacon_period_start_time (ctx);

    bp_start_date = cp_pwl_get_beacon_period_start_time (ctx);
    // Create the beacon periods.
    for (i = 0; i < CP_SCHED_BEACON_PERIODS_MAX; i++)
    {
        beacon_period[i].start_date = bp_start_date[i];
        beacon_period[i].schedule_index = ctx->beacon.ca_schedule_index;
    }

    // Special case if it is not the first shoot.
    if (!ctx->beacon.ca_sched_first_shoot && !beacon_period[0].schedule_index)
    {
        beacon_period[0].schedule_index = CA_SCHEDULE_NB - 1;
    }

    CP_BEACON_TRACE (SCHEDULES, mac_ntb(), beacon_period[0].start_date,
                     beacon_period[1].start_date,
                     beacon_period[2].start_date);

    // Provide the beacon period to the CA.
    ca_alloc_update_beacon_periods (ctx->ca, beacon_period,
                                    CP_SCHED_BEACON_PERIODS_MAX);

    // Program the timer.
    hal_timer_instance_cancel (ctx->hal_timer, &ctx->beacon.leon_timer);
    hal_timer_instance_program (ctx->hal_timer, &ctx->beacon.leon_timer,
                                cp_pwl_get_next_timer_date(ctx, true));
    CP_BEACON_TRACE (TIMER_PRGM, mac_ntb(),
                     cp_pwl_get_next_timer_date(ctx, true));

    if (ctx->beacon.ca_sched_first_shoot)
        ctx->beacon.ca_sched_first_shoot = false;

    // Increment the schedule index and verify it does not exceed the max
    // value of schedule index.
    if ( (++ctx->beacon.ca_schedule_index) >= CA_SCHEDULE_NB)
        ctx->beacon.ca_schedule_index = 0;

    CP_BEACON_TRACE (SCHEDULES, mac_ntb(), beacon_period[0].start_date,
                     beacon_period[1].start_date,
                     beacon_period[2].start_date);
}

/**
 * Process the first beacon in the received list.
 * \param  ctx  the control plane context.
 */
void
cp_beacon_get_and_process_beacon (cp_t *ctx)
{
    cp_beacon_desc_t *beacon;
    cp_beacon_unpack_t unpack;
    cp_net_t *net;
    cp_sta_t *sta;
    u64 nid;
    cp_sta_own_data_t *own;

    dbg_assert (ctx);

    /* check the countdowns. */
    cp_beacon_countdowns (ctx);

    dbg_assert (ctx->beacon.list);
    beacon = ctx->beacon.list;
    ctx->beacon.list = ctx->beacon.list->next;

    /* Check the CRC. */
    if (((pb_t *) beacon)->phy_pb.pb_rx.pb_measurement.crc_error)
    {
        blk_release_desc ((blk_t *) beacon);
        return;
    }

    cp_beacon_unpack_init (&unpack);
    cp_beacon_unpack__unpack (&unpack, beacon);

    nid = (((u64)unpack.variant_fields.nid_msb) << 22);
    nid |= unpack.variant_fields.nid_lsb;

    // Add the AVLN to the net list.
    net = cp_sta_mgr_add_avln (ctx, beacon->payload->fc.snid, nid);
    sta = cp_beacon_update_sta_peer (ctx, &unpack, net);
    // Store the access mode of the station.
    cp_net_set_access (ctx, net, beacon->payload->fc.access);

    if ((!ctx->beacon.hoip.hoipcd)
        && (cp_sta_own_data_get_cco_status (ctx)))
        cp_fsm_post_new_event (ctx, sta, HANDOVER_DISCOVERED_STA, net, sta);

    /* Our station is associated and the beacon is from our CCo */
    own = cp_sta_mgr_get_sta_own_data (ctx);
    if (cp_sta_own_data_get_tei (ctx)
        && (unpack.variant_fields.bt == CP_BEACON_CENTRAL_BEACON)
        && (nid == own->nid_track)
        && (beacon->payload->fc.snid == cp_sta_own_data_get_snid (ctx)))
    {
        /* Check the countdowns. */
        if (unpack.bmis.snids.present)
        {
            ctx->beacon.snids.snidcd = unpack.bmis.snids.snid_cd;
            ctx->beacon.snids.snid = unpack.bmis.snids.snid;
        }

        if (unpack.bmis.hm.present)
        {
            ctx->beacon.hm.hmcd = unpack.bmis.hm.hmcd;
            ctx->beacon.hm.hm = unpack.bmis.hm.hm;
        }

        if (unpack.bmis.hoip.present)
        {
            ctx->beacon.hoip.hoipcd = unpack.bmis.hoip.hoipcd;
            ctx->beacon.hoip.cco = unpack.bmis.hoip.cco;
        }

        /* If the CCo request the station to send a discover beacon. */
        if (cp_sta_own_data_get_tei (ctx) == unpack.bmis.discover.tei)
            cp_beacon_sta_send_discover_beacon (ctx, &unpack);

        cp_beacon_ntb_clk_sync(ctx, beacon->payload->fc.bts,
                               beacon->payload->fc.preamble_sysdate);


        cp_beacon_sta_compute_schedules (ctx, &unpack,
                                         beacon->payload->fc.bts,
                                         (s16*) beacon->payload->fc.bto);

        // Program the timer.
        hal_timer_instance_cancel (ctx->hal_timer, &ctx->beacon.leon_timer);
        hal_timer_instance_program (ctx->hal_timer, &ctx->beacon.leon_timer,
                                    cp_pwl_get_next_timer_date (ctx, false));
        CP_BEACON_TRACE (TIMER_PRGM, mac_ntb(),
                         cp_pwl_get_next_timer_date(ctx, false));
    }
    /* Our station is not associated. */
    else if (!cp_sta_own_data_get_tei (ctx)
             && ((unpack.variant_fields.bt == CP_BEACON_DISCOVER_BEACON)
                 || (unpack.variant_fields.bt == CP_BEACON_CENTRAL_BEACON))
             && (nid == own->nid_track)
             && (beacon->payload->fc.snid == cp_sta_own_data_get_snid (ctx)))
    {
        cp_beacon_sta_compute_schedules (ctx, &unpack,
                                         beacon->payload->fc.bts,
                                         (s16*) beacon->payload->fc.bto);

        // Program the timer.
        hal_timer_instance_cancel (ctx->hal_timer, &ctx->beacon.leon_timer);
        hal_timer_instance_program (ctx->hal_timer, &ctx->beacon.leon_timer,
                                    cp_pwl_get_next_timer_date (ctx, false));
        CP_BEACON_TRACE (TIMER_PRGM, mac_ntb(),
                         cp_pwl_get_next_timer_date(ctx, false));
    }

    // Raise the event in the FSM for station action.
    cp_fsm_post_new_event (ctx, beacon, BEACON, beacon, net, sta);

    // Release the beacon.
    blk_release_desc ((blk_t *) beacon);

    /* Release the station provided the function which processed the beacon.
     * cp_beacon_process_central_proxy or cp_beacon_process_discover. */
    slab_release (sta);

    /* Uninitialise the unpack structure. */
    cp_beacon_unpack_uninit (&unpack);
}

/**
 * Read the regions of the central beacon from another AVLN.
 *
 * \param  ctx  the Module context.
 * \param  beacon  the beacon to process.
 * \param  beacon_vf  the variant fields previously read.
 */
void
cp_beacon_process_central_read_regions (cp_t *ctx, cp_beacon_desc_t *beacon,
                                        cp_beacon_variant_fields_t *beacon_vf)
{
    dbg_assert (ctx);
    dbg_assert (beacon);
    dbg_assert (beacon_vf);
}



/**
 * Process a tracked network using the central beacon or the proxy beacon.
 * \param  ctx  the CP module context.
 * \param  beacon  the beacon received and tracked.
 */
void
cp_beacon_process_tracked_avln (cp_t *ctx, cp_beacon_desc_t *beacon,
                                cp_net_t *net)
{
    cp_beacon_unpack_t unpack;
    cp_beacon_ca_schedules_t schedules;
    cp_sta_own_data_t *own;
    dbg_assert (ctx);
    dbg_assert (beacon);
    dbg_assert (net);

    cp_beacon_unpack_init (&unpack);
    cp_beacon_unpack__unpack (&unpack, beacon);
    cp_beacon_ca_schedule_init (&schedules);

    cp_beacon_ca_schedules_from_unpack (&unpack, &schedules);
    cp_pwl__sta__compute_beacon_period_start_time (ctx,
                                                   beacon->payload->fc.bts,
                                                   (s16 *) &beacon->payload->fc.bto,
                                                   unpack.bmis.bpsto.bpsto);

    own = cp_sta_mgr_get_sta_own_data (ctx);
    own->hybrid_mode = unpack.variant_fields.hm;
    cp_beacon_compute_ca_schedules (ctx, &schedules);
    cp_beacon_ca_schedule_uninit (&schedules);
    cp_beacon_unpack_uninit (&unpack);

    // Program the timer.
    hal_timer_instance_cancel (ctx->hal_timer, &ctx->beacon.leon_timer);
    hal_timer_instance_program (ctx->hal_timer, &ctx->beacon.leon_timer,
                                cp_pwl_get_next_timer_date (ctx, false));
    CP_BEACON_TRACE (TIMER_PRGM, mac_ntb(),
                     cp_pwl_get_next_timer_date(ctx, false));

    // TODO : Call the ntb_clock_sync function.
}

/**
 * Function to call once the beacon has not been received.
 * \param ctx  the module context.
 *
 * The beacon timer has expired and this function had been called by the FSM.
 */
void
cp_beacon_beacon_not_received (cp_t *ctx)
{
    set_t schedules;
    cp_cco_bw_alloc_t *alloc;
    cp_beacon_ca_schedule_t *sched;
    cp_beacon_ca_schedules_t ca_sched;

    dbg_assert (ctx);

    cp_beacon_countdowns (ctx);
    cp_fsm_post_new_event (ctx, bare, BEACON_NOT_RECEIVED);

    // Track the frequency.
    cp_pwl__tracker__compute_beacon_period_start_time (ctx);

    /* Add schedules to the CA. */
    cp_cco_bw_finalise_sched(ctx);
    cp_cco_bw_schedules_get_beacon_period_full (ctx, &schedules);

    cp_beacon_ca_schedule_init (&ca_sched);
    for (alloc = cp_cco_bw_schedules_get_first (&schedules);
         alloc;
         alloc = cp_cco_bw_schedules_get_next (&schedules, alloc))
    {
        sched = slab_alloc (&ca_sched.cache);
        sched->st_atu = alloc->st_atu;
        sched->et_atu = alloc->et_atu;
        sched->glid = alloc->glid;
        sched->stpf = alloc->stpf;
        cp_beacon_ca_schedule_add (&ca_sched, sched);
    }
    cp_cco_bw_schedules_uninit (&schedules);

    cp_beacon_compute_ca_schedules (ctx, &ca_sched);
    cp_beacon_ca_schedule_uninit (&ca_sched);

    // Program the timer.
    hal_timer_instance_program (ctx->hal_timer, &ctx->beacon.leon_timer,
                                mac_ntb () +
                                  cp_pwl_get_beacon_period_ntb (ctx));

    CP_BEACON_TRACE (TIMER_PRGM, mac_ntb(),
                     mac_ntb() + cp_pwl_get_beacon_period_ntb (ctx));
    CP_BEACON_TRACE (BEACON_NOT_RECV, mac_ntb());
}

/**
 * Set the nek in the beacon module.
 * \param  ctx  the CP context.
 * \param  eks  the eks tu use.
 * \param  nek  the nek key.
 * \param  now  indicate if the nek shall be use right now.
 */
void
cp_beacon_change_nek (cp_t *ctx, uint eks, cp_key_t nek, bool now)
{
    uint i,index;
    dbg_assert (ctx);
    dbg_assert (ctx->mac_config);

    if (now)
        index = ctx->beacon.nek_index;
    else
        index = !ctx->beacon.nek_index;

    ctx->mac_config->nek[index].eks = MAC_EKS_CLEAR;
    arch_reorder_barrier ();
    for (i = 0; i < COUNT(nek.key); i++)
        ctx->mac_config->nek[index].nek[i] = nek.key[i];
    arch_reorder_barrier ();
    ctx->mac_config->nek[index].eks = eks;
}

/**
 * Deactivate the Beacon module.
 * \param  ctx  the CP context.
 */
void
cp_beacon_deactivate (cp_t *ctx)
{
    uint i;
    dbg_assert (ctx);

    /* Stop the hal timer. */
    hal_timer_instance_cancel (ctx->hal_timer, &ctx->beacon.leon_timer);

    /* Release pending beacons. */
    if (ctx->beacon.list)
    {
        blk_release_desc_range ((blk_t *) ctx->beacon.list,
                                (blk_t *) ctx->beacon.list_tail);
        ctx->beacon.list = NULL;
        ctx->beacon.list_tail = NULL;
    }

    /* Release the MFS from the common data. */
    for (i = 0; i < 3; i++)
    {
        if (ctx->beacon.common[i].mfs)
        {
            blk_release (ctx->beacon.common[i].mfs);
            ctx->beacon.common[i].mfs = NULL;
        }
        cp_beacon_common_init (&ctx->beacon.common[i]);
    }
}

void
cp_beacon_change_snid (cp_t *ctx, cp_snid_t snid)
{
    dbg_assert (ctx);
    dbg_assert (cp_sta_own_data_get_cco_status (ctx));

    if (!ctx->beacon.snids.snidcd)
    {
        ctx->beacon.snids.snid = snid;
        ctx->beacon.snids.snidcd = CP_BEACON_COUNTDOWN_SNID;
    }
}

void
cp_beacon_change_hm (cp_t *ctx, uint hybrid_mode)
{
    dbg_assert (ctx);
    dbg_assert (cp_sta_own_data_get_cco_status (ctx));

    if (!ctx->beacon.hm.hmcd)
    {
        ctx->beacon.hm.hm = hybrid_mode;
        ctx->beacon.hm.hmcd = CP_BEACON_COUNTDOWN_HM;
    }
}

void
cp_beacon_handover (cp_t *ctx, cp_tei_t cco)
{
    dbg_assert (ctx);

    if (!ctx->beacon.hoip.hoipcd)
    {
        ctx->beacon.hoip.cco = cco;
        ctx->beacon.hoip.hoipcd = CP_BEACON_COUNTDOWN_HOIP;
    }
}

void
cp_beacon_handover_hoipflag (cp_t *ctx, enum cp_beacon_hoip_e flag)
{
    dbg_assert (ctx);
    dbg_assert (flag < CP_BEACON_HOIP_MAX);

    ctx->beacon.hoip.hoip_flag = flag;
}

void
cp_beacon_reconfigure_timer (cp_t *ctx)
{
    bool cco;
    dbg_assert (ctx);

    // Program the timer.
    cco = cp_sta_own_data_get_cco_status (ctx);
    hal_timer_instance_cancel (ctx->hal_timer, &ctx->beacon.leon_timer);
    hal_timer_instance_program (ctx->hal_timer, &ctx->beacon.leon_timer,
                                cp_pwl_get_next_timer_date (ctx, cco));
    CP_BEACON_TRACE (TIMER_PRGM, mac_ntb(),
                     cp_pwl_get_next_timer_date (ctx, cco));
}

/**
 * Check if another countdown is currently active.
 * \param  ctx  the module context.
 * \return  true if another countdown is active, false otherwise.
 */
bool
cp_beacon_any_countdown_active (cp_t *ctx)
{
    dbg_assert (ctx);

    return ((ctx->beacon.snids.snidcd)
            || (ctx->beacon.hm.hmcd)
            || (ctx->beacon.hoip.hoipcd));
}