summaryrefslogtreecommitdiff
path: root/cp/beacon/src/bentry.c
blob: f18aa52402d41c99cfb920e362ee1de9f8fb6c9f (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/beacon/src/bentry.c
 * \brief   Bentry processing
 * \ingroup cp_beacon
 *
 */
#include "common/std.h"
#include "stdio.h"

#include "mac/common/timings.h"

#include "lib/read_word.h"
#include "lib/blk.h"
#include "lib/bitstream.h"

#include "cp/beacon/inc/bentry.h"
#include "cp/beacon/inc/beacons_ctx.h"
#include "cp/beacon/forward.h"
#include "cp/beacon/inc/beacons_work.h"

#include "cp/station/station.h"
#include "cp/secu/secu.h"

/**
 * Fill the bentry header
 *
 * \param  bentry_header_addr  the bentry address.
 * \param  type  the type of bentry.
 * \param  length  the bentry length in bytes.
 *
 * \return the address to store the rest of the bentry.
 */
u8 *
cp_bentry_header_fill (u8 *bentry_header_addr, uint type, uint length)
{
    bitstream_t bitstream;

    dbg_assert (bentry_header_addr);

    bitstream_init (&bitstream, bentry_header_addr, CP_BENTRY_HEADER, BITSTREAM_WRITE);
    bitstream_access (&bitstream, &type, CP_BENTRY_BIT_SIZE_BEHDR);
    bitstream_access (&bitstream, &length, CP_BENTRY_BIT_SIZE_BELEN); 
    bitstream_finalise (&bitstream);

    return bentry_header_addr + CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_HEADER);  
}

/**
 * Create the non persistent schedule bentry for the beacon period.
 *
 * \param  ctx  the cp_beacon_context. 
 * \param  bentry_addr  the address to store the data in the beacon payload.
 * \param  sched  the ca schedule to fill the next schedule.
 * \return  the next address to store the next bentry.
 */
u8 *
cp_bentry_non_persistent_schedule(cp_beacon_t *ctx, u8 *bentry_addr,
                              cp_beacon_desc_alloc_t *sched)
{
    // TODO Fill correctly this function. For this moment it will not be use.
    bitstream_t bitstream;
    cp_bentry_non_persistent_schedule_struct_t bentry;
    cp_bentry_sai_t sai;
    u8 *data_addr;
    uint total_size; 

    dbg_assert(ctx);
    dbg_assert(bentry_addr);

    bentry.behdr = CP_BENTRY_NON_PERSISTENT_SCHEDULE;
    bentry.belen = 0x6;
    bentry.ns = 1;

    // First allocation of persistent schedule.
    // Configuration on CSMA only mode, the glid = 0xff.
    sai.stpf = true;
    sai.glid = 0xFF;
    sai.st = 0x0;
    sai.et = ctx->cbeacon.bp_tau[0];

    data_addr = cp_bentry_header_fill (bentry_addr, bentry.behdr, bentry.belen);

    // store the bentry in the bentry.
    bitstream_init (&bitstream, data_addr, CP_BENTRY_BIT_SIZE_NON_PERSISTENT_SCHEDULE,
                    BITSTREAM_WRITE);
    bitstream_access (&bitstream, &bentry.ns, CP_BENTRY_BIT_SIZE_NS);
    bitstream_finalise (&bitstream);
    total_size = CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_NON_PERSISTENT_SCHEDULE);

    // store the bentry in the bentry.
    bitstream_init (&bitstream, data_addr + total_size, CP_BENTRY_BIT_SIZE_SAI_STPF,
                    BITSTREAM_WRITE);
    bitstream_access (&bitstream, &sai.stpf, CP_BENTRY_BIT_SIZE_STPF);
    bitstream_access (&bitstream, &sai.glid, CP_BENTRY_BIT_SIZE_GLID);
    bitstream_access (&bitstream, &sai.st, CP_BENTRY_BIT_SIZE_ST);
    bitstream_access (&bitstream, &sai.et, CP_BENTRY_BIT_SIZE_ET);
    bitstream_finalise (&bitstream);
    total_size += CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_SAI_STPF);

    return data_addr + CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_SAI_STPF);
}

/**
 * Create the schedule bentry for the beacon period.
 *
 * \param  ctx  the cp_beacon_context. 
 * \param  bentry_addr  the address to store the data in the beacon payload.
 * \param  sched  the ca schedule to fill the next schedule.
 * \return  the next address to store the next bentry.
 */
u8 *
cp_bentry_persistent_schedule(cp_beacon_t *ctx, u8 *bentry_addr,
                              cp_beacon_desc_alloc_t *sched)
{
    bitstream_t bitstream;
    cp_bentry_persistent_schedule_struct_t bentry;
    cp_bentry_sai_t sai;
    u8 *data_addr;
    uint total_size;
    uint data;

    // laranjeiro 
    // date : 2008/01/07 
    // TODO fill this correctly
    // Actually only a schedule is generated for the CSMA mode only.

    dbg_assert(ctx);
    dbg_assert(bentry_addr);

    bentry.behdr = CP_BENTRY_PERSISTENT_SCHEDULE;

    bentry.pscd = ctx->bentries_data.pscd;
    bentry.cscd = ctx->bentries_data.cscd;
    bentry.ns = 1;

    // First allocation of persistent schedule.
    // Configuration on CSMA only mode, the glid = 0xff.
    sai.stpf = true;
    sai.glid = 0xFF;
    sai.st = 0x0;
    sai.et = ctx->cbeacon.bp_tau[0];

    data_addr = bentry_addr + CP_BENTRY_BYTE_SIZEOF (CP_BENTRY_HEADER);
        cp_bentry_header_fill (bentry_addr, bentry.behdr,
                                       bentry.belen);

    // store the bentry in the bentry.
    bitstream_init (&bitstream, data_addr, CP_BENTRY_BIT_SIZE_PERSISTENT_SCHEDULE,
                    BITSTREAM_WRITE);
    bitstream_access (&bitstream, &bentry.pscd, CP_BENTRY_BIT_SIZE_PSCD);
    bitstream_access (&bitstream, &bentry.cscd, CP_BENTRY_BIT_SIZE_CSCD);
    // Reserved bits.
    data = 0x0;
    bitstream_access (&bitstream, &data, 2);
    bitstream_access (&bitstream, &bentry.ns, CP_BENTRY_BIT_SIZE_NS);
    bitstream_finalise (&bitstream);
    total_size = CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_PERSISTENT_SCHEDULE);

    // store the bentry in the bentry.
    bitstream_init (&bitstream, data_addr + total_size, CP_BENTRY_BIT_SIZE_SAI_STPF,
                    BITSTREAM_WRITE);
    bitstream_access (&bitstream, &sai.stpf, CP_BENTRY_BIT_SIZE_STPF);
    bitstream_access (&bitstream, &sai.glid, CP_BENTRY_BIT_SIZE_GLID);
    bitstream_access (&bitstream, &sai.st, CP_BENTRY_BIT_SIZE_ST);
    bitstream_access (&bitstream, &sai.et, CP_BENTRY_BIT_SIZE_ET);
    bitstream_finalise (&bitstream);
    total_size += CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_SAI_STPF);


    sched->ca_sched->allocations_nb = 1;
    sched->ca_sched->allocations[0].end_offset_tck = MAC_ATU_TO_TCK (sai.et);
    sched->ca_sched->allocations[0].glid = 0xff;

    bentry.belen = total_size;
    data_addr = cp_bentry_header_fill (bentry_addr, bentry.behdr, bentry.belen);

    return data_addr + total_size;
}


/**
 * Create the schedules when the STA is UCCo to be correspond to the current
 * schedule of the CCo.
 * The schedule provided are non persistent, it is on vailable if the STA is
 * not associated with the CCo and the CCo is a UCCo.
 *
 * \param  ctx  the cp beacon module context
 * \param  bentry_addr  the bentry addr
 * \return  the address of the next bentry. 
 */
u8 *
cp_bentry_default_schedule (cp_beacon_t *ctx, u8 *bentry_addr)
{
    bitstream_t bitstream;
    cp_bentry_non_persistent_schedule_struct_t bentry;
    cp_bentry_sai_t sai;
    u8 *data_addr;
    uint total_size; 

    dbg_assert(ctx);
    dbg_assert(bentry_addr);

    bentry.behdr = CP_BENTRY_NON_PERSISTENT_SCHEDULE;
    bentry.belen = 0x5;
    bentry.ns = 1;

    // First allocation of persistent schedule.
    // Configuration on CSMA only mode, the glid = 0xff.
    sai.stpf = true;
    sai.glid = 0xFF;
    sai.st = 0x0;
    sai.et = CP_BENRTY_MAX_SAI_DATA_VALUE;

    data_addr = cp_bentry_header_fill (bentry_addr, bentry.behdr, bentry.belen);

    // store the bentry in the bentry.
    bitstream_init (&bitstream, data_addr, CP_BENTRY_BIT_SIZE_NON_PERSISTENT_SCHEDULE,
                    BITSTREAM_WRITE);
    bitstream_access (&bitstream, &bentry.ns, CP_BENTRY_BIT_SIZE_NS);
    bitstream_finalise (&bitstream);
    total_size = CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_NON_PERSISTENT_SCHEDULE);

    // store the bentry in the bentry.
    bitstream_init (&bitstream, data_addr + total_size, CP_BENTRY_BIT_SIZE_SAI_STPF,
                    BITSTREAM_WRITE);
    bitstream_access (&bitstream, &sai.stpf, CP_BENTRY_BIT_SIZE_STPF);
    bitstream_access (&bitstream, &sai.glid, CP_BENTRY_BIT_SIZE_GLID);
    bitstream_access (&bitstream, &sai.st, CP_BENTRY_BIT_SIZE_ST);
    bitstream_access (&bitstream, &sai.et, CP_BENTRY_BIT_SIZE_ET);
    bitstream_finalise (&bitstream);
    total_size += CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_SAI_STPF);

    return data_addr + total_size;
}


/**
 * Create the regions for the beacons.
 *
 * \param  ctx  the cp_beacon context
 * \param  bentry_addr  the bentry address.
 */
u8 *
cp_bentry_regions (cp_beacon_t *ctx, u8 *bentry_addr)
{
    bitstream_t bitstr;
    cp_bentry_regions_struct_t regions[64];
    uint nr;
    u8 *data_addr;
    uint i;

    dbg_assert (ctx);
    dbg_assert (bentry_addr);

    nr = 1;
    regions[0].rt = CP_BENTRY_REGIONS_LOCAL_CSMA; 
    regions[0].ret = ctx->cbeacon.bp_tau[0];

    data_addr = cp_bentry_header_fill (bentry_addr, CP_BENTRY_REGIONS, 
                                       CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_REGION(nr)));

    bitstream_init (&bitstr, data_addr, CP_BENTRY_BIT_SIZE_NUM_REG, BITSTREAM_WRITE);
    bitstream_access (&bitstr, &nr, CP_BENTRY_BIT_SIZE_NUM_REG);
    bitstream_finalise (&bitstr);
    data_addr += CP_BENTRY_BYTE_SIZEOF ((CP_BENTRY_BIT_SIZE_NUM_REG + 2));

    // Store the bentry
    for ( i = 0; i < nr; i++)
    {
        bitstream_init (&bitstr, data_addr, CP_BENTRY_BIT_SIZE_ONE_REGION ,BITSTREAM_WRITE);
        bitstream_access (&bitstr, &regions[i].rt, CP_BENTRY_BIT_SIZE_REG_TYPE);
        bitstream_access (&bitstr, &regions[i].ret, CP_BENTRY_BIT_SIZE_REG_ET);
        bitstream_finalise (&bitstr);

        data_addr += CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_ONE_REGION);
    }

    return data_addr;
}

/**
 * Store the mac address of the station in the beacon bentry
 *
 * \param  ctx  the cp_beacon context
 * \param  bentry_addr  the bentry addr.
 * \return  the next address to store the next bentry.
 */
u8 *
cp_bentry_mac_address(cp_beacon_t *ctx, u8 *bentry_addr)
{
    u8 *data_addr;

    dbg_assert (ctx);
    dbg_assert (ctx->sta);
    dbg_assert (bentry_addr);

    // Store the bentry
    data_addr = cp_bentry_header_fill (bentry_addr, CP_BENTRY_MAC_ADDRESS, 0x6);
    cp_station_get_mac_address (ctx->sta, data_addr); 

    return bentry_addr + CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_MAC_ADDRESS);
}

/**
 * Store the tei of the next sta which shall send a discover beacon.
 *
 * \param  ctx  the cp_beacon context.
 * \param  bentry_addr  the bentry addr
 * \param  tei  the sta tei to request a discover beacon.
 * \return  the next address to store the next bentry.
 */
u8 *
cp_bentry_discover(cp_beacon_t *ctx, u8 *bentry_addr, u8 tei)
{
    uint behdr;
    uint belen;
    u8 *data_addr;

    dbg_assert (ctx);
    dbg_assert (bentry_addr);

    behdr = CP_BENTRY_DISCOVER;
    belen = 1;

    data_addr = cp_bentry_header_fill (bentry_addr, CP_BENTRY_DISCOVER, 1);
    *data_addr = tei;

    return bentry_addr + CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_DISCOVER);
}

/**
 * Store the discover list in the bentry.
 *
 * \param  ctx  the cp_beacon context.
 * \param  bentry_addr  the bentry addr
 * \return  the next address to store the next bentry.* \param  ctx
 */
u8 *
cp_bentry_discover_info(cp_beacon_t *ctx, u8 *bentry_addr)
{
    uint data;
    bitstream_t bitstr;
    cp_bentry_discover_info_struct_t dis;
    u8 *dis_data;

    dbg_assert (ctx);
    dbg_assert (ctx->sta);
    dbg_assert (bentry_addr);

    dis_data = cp_bentry_header_fill (bentry_addr, CP_BENTRY_DISCOVER_INFO,
                           CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_DISCOVER_INFO));

    // Store the address of the discovery bentry data in the dis_data
    // variable.
    dis.cco_cap = cp_cco_get_cco_cap(ctx->cco);
    dis.proxy_net_cap = cp_cco_get_proxy_cap(ctx->cco);
    dis.backup_cco_cap = cp_cco_get_backup_cap(ctx->cco);
    dis.cco_status = cp_cco_get_cco_status (ctx->cco);
    dis.pco_status = cp_cco_get_pco_status(ctx->cco);
    dis.backup_cco_status = cp_cco_get_backup_cco_status(ctx->cco);
    dis.num_dis_sta = cp_station_get_num_dis_sta(ctx->sta);
    dis.num_dis_net = cp_station_get_num_dis_net(ctx->sta);
    dis.authentication = cp_station_is_authenticated(ctx->sta);
    dis.status_user_ap_cco = cp_cco_get_user_appointed_cco_status(ctx->cco);

    // Store the discovery bentry in the bentry.
    bitstream_init(&bitstr, dis_data, CP_BENTRY_BIT_SIZE_DISCOVER_INFO,
                   BITSTREAM_WRITE);

    // Store the update boolean to 0
    data = 0x0;
    bitstream_access (&bitstr, &data, 1);
    bitstream_access (&bitstr, &dis.cco_cap,
                         CP_BENTRY_BIT_SIZE_DIS_CCO_CAP);
    bitstream_access (&bitstr, &dis.proxy_net_cap, CP_BENTRY_BIT_SIZE_DIS_PROXY_NET_CAP);
    bitstream_access (&bitstr, &dis.backup_cco_cap, CP_BENTRY_BIT_SIZE_DIS_BACK_CCO_CAP);
    bitstream_access (&bitstr, &dis.cco_status, CP_BENTRY_BIT_SIZE_DIS_CCO_STATUS);
    bitstream_access (&bitstr, &dis.pco_status, CP_BENTRY_BIT_SIZE_DIS_PCO_STATUS);
    bitstream_access (&bitstr, &dis.backup_cco_status, 
                         CP_BENTRY_BIT_SIZE_DIS_BACK_CCO_STATUS );
    bitstream_access (&bitstr, &dis.num_dis_sta, CP_BENTRY_BIT_SIZE_DIS_NUM_DIS_STA);
    bitstream_access (&bitstr, &dis.num_dis_net, CP_BENTRY_BIT_SIZE_DIS_NUM_DIS_NET);
    bitstream_access (&bitstr, &dis.authentication, CP_BENTRY_BIT_SIZE_DIS_AUTH);
    bitstream_access (&bitstr, &dis.status_user_ap_cco, CP_BENTRY_BIT_SIZE_DIS_USER_APP);
    bitstream_finalise (&bitstr);

    // Copy the the discover bentry to the data variable.
    data = read_u32_from_word (dis_data);
    dis.updated = CP_BENTRY_DISCOVER_UPDATE_MASK(data) 
        || CP_BENTRY_DISCOVER_UPDATE_MASK(ctx->bentries_data.discover_info);

    if (dis.updated)
    {
        *dis_data |= 0x1;
        ctx->bentries_data.discover_info = read_u32_from_word (dis_data);
    }
    
    return dis_data + CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_DISCOVER_INFO);
}

/**
 * BPSTO always present in CSMA only mode and for discover beacons.
 * This bentry data is stamped by the CA.
 * The address of the first byte of the BPSTO is stored in bpsto camp of the
 * structure common.
 *
 * \param  ctx  the cp_beacon context.
 * \param  common  common data of all beacons.
 * \param  bentry_addr  the bentry addr
 * \return  the next address to store the next bentry.
 */
u8 *
cp_bentry_bpsto (cp_beacon_t *ctx, cp_beacon_common_t *common, u8 *bentry_addr)
{
    dbg_assert (ctx);
    dbg_assert (common);
    dbg_assert (bentry_addr);

    common->bto_bpsto.bpsto = cp_bentry_header_fill (bentry_addr,
                           CP_BENTRY_BEACON_PERIOD_START_OFFSET,
                           CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_BPSTO));

    return bentry_addr + CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_HEADER) 
        + CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_BPSTO);
}

/**
 * Encryption key change.
 *
 * \param  ctx  the cp_beacon context.
 * \param  bentry_addr  the bentry addr
 * \return  the next address to store the next bentry. 
 */
u8 *
cp_bentry_encryption_key_change (cp_beacon_t *ctx, u8 *bentry_addr)
{
    cp_bentry_key_change_struct_t data;
    bitstream_t bitstream;
    u8 *data_addr;
    uint rsvd;

    dbg_assert (ctx);
    dbg_assert (ctx->secu);
    dbg_assert (bentry_addr);

    data_addr = cp_bentry_header_fill (bentry_addr, CP_BENTRY_ENCRYPTION_KEY_CHANGE,
                                     CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_HEADER));

    data.kccd = ctx->bentries_data.kccd;
    data.kbc = cp_secu_get_kbc (ctx->secu);
    data.new_eks = cp_secu_get_eks (ctx->secu);

    bitstream_init (&bitstream, data_addr, CP_BENTRY_BIT_SIZE_EKC,
                            BITSTREAM_WRITE); 
    bitstream_access (&bitstream, &data.kccd, CP_BENTRY_BIT_SIZE_EKC_KCCD); 
    bitstream_access (&bitstream, &data.kbc, CP_BENTRY_BIT_SIZE_EKC_KBC); 
    // reserved bits
    rsvd = 0;
    bitstream_access (&bitstream, &rsvd, 1); 
    bitstream_access (&bitstream, &data.new_eks, CP_BENTRY_BIT_SIZE_EKC_NEWEKS); 
    bitstream_finalise (&bitstream);

    return data_addr + CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_EKC); 
}

/**
 * Handover. Add a the bentry when the CCo will change.
 *
 * \param  ctx  the cp_beacon context.
 * \param  bentry_addr  the bentry addr
 * \param  new_cco_tei  the new cco tei.
 * \return  the next address to store the next bentry. 
 */
u8 *
cp_bentry_handover(cp_beacon_t *ctx, u8 *bentry_addr, u8 new_cco_tei)
{
    bitstream_t bitstream;
    cp_bentry_handover_struct_t data;
    uint rsvd = 0;
    u8 *data_addr;

    dbg_assert (ctx);
    dbg_assert (ctx->sta);
    dbg_assert (bentry_addr);

    data_addr = cp_bentry_header_fill (bentry_addr, CP_BENTRY_CCO_HANDOVER, 
                           CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_HANDOVER));

    data.hcd = ctx->bentries_data.hcd;
    data.nctei = new_cco_tei;

    bitstream_init (&bitstream, data_addr, CP_BENTRY_BIT_SIZE_HANDOVER,
                            BITSTREAM_WRITE); 
    bitstream_access(&bitstream, &data.hcd, CP_BENTRY_BIT_SIZE_HANDOVER_HCD); 
    // reserved bits
    rsvd = 0;
    bitstream_access(&bitstream, &rsvd, 2); 
    bitstream_access(&bitstream, &data.nctei, CP_BENTRY_BIT_SIZE_HANDOVER_NCTEI); 
    bitstream_finalise (&bitstream);

    return bentry_addr + CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_HANDOVER) 
        + CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_HEADER);
}

/**
 * Chnage hybrid mode
 *
 * \param  ctx  the cp_beacon context.
 * \param  bentry_addr the bentry addr
 * \return  the next address to store the next bentry.
 */
u8 *
cp_bentry_change_hm (cp_beacon_t *ctx, u8 *bentry_addr)
{
    bitstream_t bitstream;
    cp_bentry_hm_struct_t data;
    u8 *data_addr;

    dbg_assert (ctx);
    dbg_assert (bentry_addr);

    data_addr = cp_bentry_header_fill (bentry_addr, CP_BENTRY_CHANGE_HM, 
                           CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_HM));

    data.hmccd = ctx->bentries_data.hmccd;
    data.new_hm = ctx->hm;

    bitstream_init (&bitstream, data_addr, CP_BENTRY_BIT_SIZE_HM,
                            BITSTREAM_WRITE); 
    bitstream_access (&bitstream, &data.hmccd, CP_BENTRY_BIT_SIZE_HM_HMCCD); 
    bitstream_access (&bitstream, &data.new_hm, CP_BENTRY_BIT_SIZE_HM_NEWHM); 
    bitstream_finalise (&bitstream);

    return bentry_addr + CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_HM)
        + CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_HEADER);
}

/**
 * Process the persistent schedule bentry
 * 
 * \param  ctx  the cp_beacon context.
 * \param  bentry  the bentry to process.
 * \param  sched  the current schedule to program
 */
u8 *
cp_bentry_persistent_schedule_process (cp_beacon_t *ctx, u8 *bentry,
                                       cp_beacon_desc_alloc_t *sched)
{
    bitstream_t bitstream;
    cp_bentry_persistent_schedule_struct_t bb;
    cp_bentry_sai_t sai;
    uint ns;
    uint res;

    dbg_assert (ctx);
    dbg_assert (ctx->ca);
    dbg_assert (bentry);
    dbg_assert (sched);

    /* TODO Fill this function correctly...
     * Actually only a schedule is present for the CSMA-only mode.
     */
   
    // Get the pscd, cscd and the ns from the bentry.
    bitstream_init (&bitstream, bentry, CP_BENTRY_BIT_SIZE_PERSISTENT_SCHEDULE,
               BITSTREAM_READ);
    bitstream_access (&bitstream, &bb.pscd, CP_BENTRY_BIT_SIZE_PSCD);
    bitstream_access (&bitstream, &bb.cscd, CP_BENTRY_BIT_SIZE_CSCD);
    bitstream_access (&bitstream, &res, 2);
    bitstream_access (&bitstream, &bb.ns, CP_BENTRY_BIT_SIZE_NS);
    bitstream_finalise (&bitstream);

    bentry += CP_BENTRY_BYTE_SIZEOF (CP_BENTRY_BIT_SIZE_PERSISTENT_SCHEDULE); 

    ctx->bentries_data.pscd = bb.pscd;
    ctx->bentries_data.cscd = bb.cscd;

    // Initialize the schedule persistente session.
    for (ns = bb.ns; ns; ns--)
    {
        if (read_u8_from_word (bentry) & 0x1)
        {
            // SAI with STPF == 1
            bitstream_init (&bitstream, bentry, CP_BENTRY_BIT_SIZE_SAI_STPF,
                            BITSTREAM_READ);
            bitstream_access (&bitstream, &sai.stpf, CP_BENTRY_BIT_SIZE_STPF);
            bitstream_access (&bitstream, &sai.glid, CP_BENTRY_BIT_SIZE_GLID);
            bitstream_access (&bitstream, &sai.st, CP_BENTRY_BIT_SIZE_ST);
            bitstream_access (&bitstream, &sai.et, CP_BENTRY_BIT_SIZE_ET);
            bitstream_finalise (&bitstream);

            // Store the glid in the allocation, the glid in the sptf is 7 bit
            // long, they admit that the msb bit is allow true.
            sched->ca_sched->allocations[sched->next_alloc].glid =
                0x80 | sai.glid;
            sched->ca_sched->allocations[sched->next_alloc].end_offset_tck =
                MAC_ATU_TO_TCK (sai.et);

            bentry += CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_SAI_STPF);
        }
        else
        {
            bitstream_init (&bitstream, bentry, CP_BENTRY_BIT_SIZE_SAI,
                            BITSTREAM_READ);
            bitstream_access (&bitstream, &sai.stpf, CP_BENTRY_BIT_SIZE_STPF);
            bitstream_access (&bitstream, &sai.glid, CP_BENTRY_BIT_SIZE_GLID);
            bitstream_access (&bitstream, &sai.et, CP_BENTRY_BIT_SIZE_ET);
            bitstream_finalise (&bitstream);

            // Store the glid in the allocation, the glid in the sptf is 7 bit
            // long, they admit that the msb bit is allow true.
            sched->ca_sched->allocations[sched->next_alloc].glid =
                0x80 | sai.glid;
            sched->ca_sched->allocations[sched->next_alloc].end_offset_tck =
                MAC_ATU_TO_TCK (sai.et);

            bentry += CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_SAI);
        }

        sched->next_alloc++;
    }

    return bentry;
}

/**
 * Process the non persistent schedule bentry
 *
 * \param  ctx  the cp_beacon context
 * \param  bentry to process
 * \param  sched current schedule program
 */
u8 *
cp_bentry_non_persistent_schedule_process (cp_beacon_t *ctx, u8 *bentry,
                                           cp_beacon_desc_alloc_t *sched)
{
    bitstream_t bitstream;
    cp_bentry_persistent_schedule_struct_t bb;
    cp_bentry_sai_t sai;
    uint ns;
    uint bentry_end = 0;

    dbg_assert (ctx);
    dbg_assert (ctx->ca);
    dbg_assert (bentry);
    dbg_assert (sched);

    /* TODO Fill this function correctly...
     * Actually only a schedule is present for the CSMA-only mode.
     */
   
    // Get the pscd, cscd and the ns from the bentry.
    bitstream_init (&bitstream, bentry, CP_BENTRY_BIT_SIZE_NON_PERSISTENT_SCHEDULE,
               BITSTREAM_READ);
    bitstream_access (&bitstream, &bb.ns, CP_BENTRY_BIT_SIZE_NS);
    bitstream_finalise (&bitstream);

    bentry += CP_BENTRY_BYTE_SIZEOF (CP_BENTRY_BIT_SIZE_NON_PERSISTENT_SCHEDULE); 

    // Initialize the schedule persistente session.
    for (ns = bb.ns; ns; ns--)
    {
        if (read_u8_from_word (bentry) & 0x1)
        {
            // SAI with STPF == 1
            bitstream_init (&bitstream, bentry, CP_BENTRY_BIT_SIZE_SAI_STPF,
                            BITSTREAM_READ);
            bitstream_access (&bitstream, &sai.stpf, CP_BENTRY_BIT_SIZE_STPF);
            bitstream_access (&bitstream, &sai.glid, CP_BENTRY_BIT_SIZE_GLID);
            bitstream_access (&bitstream, &sai.st, CP_BENTRY_BIT_SIZE_ST);
            bitstream_access (&bitstream, &sai.et, CP_BENTRY_BIT_SIZE_ET);
            bitstream_finalise (&bitstream);

            // Store the glid in the allocation, the glid in the sptf is 7 bit
            // long, they admit that the msb bit is allow true.
            sched->ca_sched->allocations[sched->next_alloc].glid =
                0x80 | sai.glid;
            sched->ca_sched->allocations[sched->next_alloc].end_offset_tck =
                MAC_ATU_TO_TCK (sai.et);

            bentry_end += CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_SAI_STPF);
        }
        else
        {
            bitstream_init (&bitstream, bentry, CP_BENTRY_BIT_SIZE_SAI,
                            BITSTREAM_READ);
            bitstream_access (&bitstream, &sai.stpf, CP_BENTRY_BIT_SIZE_STPF);
            bitstream_access (&bitstream, &sai.glid, CP_BENTRY_BIT_SIZE_GLID);
            bitstream_access (&bitstream, &sai.et, CP_BENTRY_BIT_SIZE_ET);
            bitstream_finalise (&bitstream);

            // Store the glid in the allocation, the glid in the sptf is 7 bit
            // long, they admit that the msb bit is allow true.
            sched->ca_sched->allocations[sched->next_alloc].glid =
                0x80 | sai.glid;
            sched->ca_sched->allocations[sched->next_alloc].end_offset_tck =
                MAC_ATU_TO_TCK (sai.et);

            bentry_end += CP_BENTRY_BYTE_SIZEOF(CP_BENTRY_BIT_SIZE_SAI);
        }

        sched->next_alloc++;
    }

    return bentry + bentry_end;
}

/**
 * Process a bentry regions.
 *
 * \param  ctx  the cp_beacon context
 * \param  bentry to process, the bentry payload, it does not contain the
 * bentry header and the bentry length.
 */
u8 *
cp_bentry_regions_process (cp_beacon_t *ctx, u8 *bentry)
{
    // TODO

    return bentry + 3;
}

/**
 * Process a mac address bentry.
 *
 * \param  ctx  the cp_beacon context
 * \param  bentry to process only the payload, the bentry header and the
 * length are not provided.
 * \param  mac  the mac address to copy the bentry address in it.
 */
u8 *
cp_bentry_mac_address_process (cp_beacon_t *ctx, u8 *bentry, mac_t *mac)
{
    bitstream_t bitstream;
    dbg_assert (ctx);
    dbg_assert (ctx->sta);
    dbg_assert (mac);

    bitstream_init (&bitstream, bentry, 6, BITSTREAM_READ);
    bitstream_access (&bitstream, mac, 48);
    bitstream_finalise (&bitstream);

    return bentry + 6;
}

/**
 * Process a discover bentry.
 * TODO  The discover beacon must contain the MAC address of the sta which
 * emits the it.
 *
 * \param  ctx  the cp_beacon context
 * \param  bentry to process
 * \param  central_beacon  the central beacon received.
 */
u8 *
cp_bentry_discover_process (cp_beacon_t *ctx, u8 *bentry,
                            cp_beacon_desc_t *central_beacon)
{
    dbg_assert (ctx); 
    dbg_assert (ctx->sta);
    dbg_assert (bentry);
    dbg_assert (central_beacon);

    if (cp_station_get_tei(ctx->sta) == read_u8_from_word(bentry))
    {
        cp_beacon_desc_t *beacon;
        mfs_tx_t *mfs_beacon;
        bool added;
        bitstream_t bitstream;
        uint tei;
        uint bt;
        uint word;
        u8 *bentry;
        uint nbe;

        u8 *cbentry;
        uint cbentry_hdr;
        uint cnbe;
        uint cbentry_len;
        uint bentry_header_len;

        //Generate the discover beacon and provide it to the CA.
        beacon = (cp_beacon_desc_t *) blk_alloc_desc();
        
        // Store the discover beacon in the discover context.
        if (ctx->dbeacon.last_beacon)
        {
            blk_release_desc ((blk_t *) ctx->dbeacon.last_beacon);
        }
        blk_addref_desc ((blk_t *) beacon);
        ctx->dbeacon.last_beacon = beacon;

        // Copy the nid of the central beacon.
        beacon->nid_msb = central_beacon->nid_msb;

        // copy the rest of the beacon paylaod
        memcpy (beacon->payload->beacon_mpdu_payload,
            central_beacon->payload->beacon_mpdu_payload, 8);

        tei = cp_station_get_tei (ctx->sta);
        bt = CP_BEACON_DISCOVER_BEACON;
        // store the data.
        beacon->payload->beacon_mpdu_payload[3] = tei;
        word = read_u32_from_word (beacon->payload->beacon_mpdu_payload + 4);
        bitstream_init (&bitstream, beacon->payload->beacon_mpdu_payload + 4,
                        sizeof (uint), BITSTREAM_WRITE);
        bitstream_access (&bitstream, &bt, 3);
        bitstream_access (&bitstream, &word, sizeof(uint) - 3);
        bitstream_finalise (&bitstream);

        // Insert the mandatory bentries.
        bentry = beacon->payload->bmis;
        bentry = cp_bentry_mac_address (ctx, bentry);
        bentry = cp_bentry_discover_info (ctx, bentry);
        
        // Search for the region, non persistent and perisistent bentry in the
        // central beacon.
        nbe = 2;
        bentry_header_len = CP_BENTRY_BYTE_SIZEOF (CP_BENTRY_HEADER);
        cbentry = central_beacon->payload->bmis;
        for (cnbe = central_beacon->payload->nbe; cnbe; cnbe --)
        {
            cbentry_hdr = read_u8_from_word(cbentry);
            cbentry_len = read_u8_from_word (cbentry + 1);

            if ((cbentry_hdr == CP_BENTRY_REGIONS)
                || (cbentry_hdr == CP_BENTRY_NON_PERSISTENT_SCHEDULE)
                || (cbentry_hdr == CP_BENTRY_PERSISTENT_SCHEDULE))
            {
                    memcpy (bentry, cbentry, cbentry_len); 
                    nbe ++;
                    bentry += bentry_header_len + cbentry_len;
            }

            cbentry += bentry_header_len + cbentry_len;
        }

        beacon->payload->nbe = nbe;

        /* Create the MFS
         * see http://pessac/cesar/trac/wiki/SoftMacBeacons#BeaconMFS
         * for more details.
         */
        dbg_assert (ctx->mac_store);
        mfs_beacon = mac_store_mfs_add_tx (ctx->mac_store, true, false, 
                                           MAC_LID_DISCOVER, 0xff, &added);

        if (added)
        {
            ctx->dbeacon.mfs = mfs_beacon;
            mfs_beacon->common.ats = false;
            mfs_beacon->cap = 0x2;
            mfs_beacon->beacon = true;

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

        // Estimate the bts and btos.
        cp_beacon_estimate_bts_bto_bp (ctx, &ctx->dbeacon);

        dbg_assert (ctx->pbproc);
        pbproc_mfs_beacon_prepare (ctx->pbproc, mfs_beacon, (pb_beacon_t *) beacon,
                                   &ctx->dbeacon.bto_bpsto);
    }

    return bentry + 3;
}

/**
 * Process a discover info bentry.
 *
 * \param  ctx  the cp_beacon context
 * \param  bentry to process
 */
u8 *
cp_bentry_discover_info_process (cp_beacon_t *ctx, u8 *bentry)
{
    dbg_assert (ctx);
    dbg_assert (bentry);

    return bentry + 4;
}

/**
 * Process a beacon period start offset bentry.
 *
 * \param  ctx  the cp_beacon context
 * \param  bentry to process, the bentry payload, it does not contain the
 * bentry header and the bentry length.
 * \param  bpsto  the beacon period start time offset to be filled.
 */
u8 *
cp_bentry_bpsto_process (cp_beacon_t *ctx, u8 *bentry, uint *bpsto)
{
    dbg_assert (ctx);
    dbg_assert (ctx->sta);

    *bpsto = read_u24_from_word (bentry);

    return bentry + 3;
}

/**
 * Process the encryption key change bentry.
 *
 * \param  ctx  the cp_beacon context
 * \param  bentry to process
 */
u8 *
cp_bentry_encryption_key_change_process (cp_beacon_t *ctx, u8 *bentry)
{
    dbg_assert (false);
}

/**
 * Process the Handover in progress bentry.
 *
 * \param  ctx  the cp_beacon context
 * \param  bentry to process
 */
u8 *
cp_bentry_handover_process (cp_beacon_t *ctx, u8 *bentry)
{
    dbg_assert (false);
}

/**
 * Process the beacon relocation bentry.
 *
 * \param  ctx  the cp_beacon context
 * \param  bentry to process
 */
u8 *
cp_bentry_beacon_relocation_process (cp_beacon_t *ctx, u8 *bentry)
{
    dbg_assert(false);
}

/**
 * Process the AC Line synchronization bentry.
 *
 * \param  ctx  the cp_beacon context
 * \param  bentry to process
 */
u8 *
cp_bentry_ac_line_sync_countdown_process (cp_beacon_t *ctx, u8 *bentry)
{
    dbg_assert (false);
}
            
/**
 * Process the bentry change num slots.
 *
 * \param  ctx  the cp_beacon context
 * \param  bentry to process
 */
u8 *
cp_bentry_change_num_slots_process (cp_beacon_t *ctx, u8 *bentry)
{
    dbg_assert (false);
}
               
/**
 * Process the change Hybrid Mode bentry.
 *
 * \param  ctx  the cp_beacon context
 * \param  bentry to process
 */
u8 *
cp_bentry_change_hm_process (cp_beacon_t *ctx, u8 *bentry)
{
    dbg_assert (false);
}
   
/**
 * Process the change snid bentry.
 *
 *\param  ctx  the cp_beacon context
 * \param  bentry to process
 */
u8 *
cp_bentry_change_snid_process (cp_beacon_t *ctx, u8 *bentry)
{
    dbg_assert (false);
}
               
/**
 * Proces the vendor specific bentry.
 *
 * \param  ctx  the cp_beacon context
 * \param  bentry to process
 */
u8 *
cp_bentry_vendor_specific_process (cp_beacon_t *ctx, u8 *bentry)
{
    dbg_assert (false);
}