summaryrefslogtreecommitdiff
path: root/cesar/cp/cco/action/src/cco_action.c
blob: e8caaf9480c73fcfb3a1842a26e5ab73099254fe (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/cco/action/src/cco_action.c
 * \brief   CCo Action functions.
 * \ingroup cp_cco
 *
 */
#include <string.h>
#include "common/std.h"
#include "common/defs/homeplugAV.h"

#include "lib/set.h"
#include "lib/utils.h"
#include "lib/rnd.h"
#include "lib/slab.h"

#include "common/defs/ethernet.h"

#include "cl/cl_mactotei.h"

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

#include "cp/defs.h"
#include "cp/cp.h"
#include "cp/pwl/pwl.h"
#include "cp/fsm/fsm.h"
#include "cp/msg/msg.h"

#include "cp/cco/action/cco_action.h"
#include "cp/sta/mgr/sta_mgr.h"
#include "cp/sta/mgr/net.h"
#include "cp/sta/mgr/sta.h"
#include "cp/beacon/beacon.h"
#include "cp/sta/core/core.h"

#include "cp/beacon/beacon.h"

#include "cp/inc/context.h"
#include "cp/cco/action/inc/cco_action.h"

/**
 * Choose a SNID between the ones available.
 * \param  ctx  the CP context.
 */
static cp_snid_t
cp_cco_action_snid_choose (cp_t *ctx)
{
    u16 flags;
    u16 table [16];
    uint nb_elem;
    uint i;
    dbg_assert (ctx);

    // Get the present flags.
    flags = ~cp_sta_mgr_get_present_snids (ctx);

    // fill the table.
    for (i = 0, nb_elem = 0; i < 16; i++)
    {
        if ((flags >> i) & true)
        {
            table[nb_elem] = i;
            nb_elem ++;
        }
    }


    // Chose the new SNID.
    i = lib_rnd32 (&ctx->rnd) % nb_elem;

    return table[i];
}

/**
 * Function to sort the station in the CCo selection set.
 * \param  left  the left node.
 * \param  right  the right node.
 */
static inline bool
cp_cco_action_cco_selection__less (heap_node_t *left, heap_node_t *right)
{
    cp_sta_t *stal;
    cp_sta_t *star;

    dbg_assert (left);
    dbg_assert (right);

    stal = PARENT_OF (cp_sta_t, cco_selection_node, left);
    star = PARENT_OF (cp_sta_t, cco_selection_node, right);

    if (stal->cco_cap > star->cco_cap)
        return true;
    return false;
}


/**
 * Search for an available TEI in the AVLN list.
 * \param  ctx  the module context.
 * \return  return the TEI found.
 *
 */
cp_tei_t
cp_cco_action_tei_compute (cp_t *ctx)
{
    uint row;
    uint tei;
    uint i;
    dbg_assert (ctx);

    for (i = MAC_TEI_STA_MIN; MAC_TEI_IS_STA (i); i++)
    {
        row = i / CP_CCO_ACTION_TEI_FLAGS_ROW_SIZE_BITS;
        tei = ctx->cco_action.tei_flags[row] >>
            ((i % CP_CCO_ACTION_TEI_FLAGS_ROW_SIZE_BITS)- 1);
        if ((tei & true) == false)
        {
            tei = i;
            cp_cco_action_tei_in_use (ctx, tei);
            break;
        }
        else
            tei = MAC_TEI_UNASSOCIATED;
    }

    // No TEI are available.
    return tei;
}

/**
 * Release a TEI.
 * \param  ctx  the module context.
 * \param  tei  The TEI value to release.
 */
void
cp_cco_action_tei_release (cp_t *ctx, u8 tei)
{
    uint row;

    dbg_assert (ctx);
    dbg_assert (tei);

    row = tei / CP_CCO_ACTION_TEI_FLAGS_ROW_SIZE_BITS;

    ctx->cco_action.tei_flags[row] &= ~(1 << ((tei
         % CP_CCO_ACTION_TEI_FLAGS_ROW_SIZE_BITS)-1));
}

void
cp_cco_action_tei_in_use (cp_t *ctx, cp_tei_t tei)
{
    uint row;

    dbg_assert (ctx);
    dbg_assert (MAC_TEI_IS_STA (tei));

    row = tei / CP_CCO_ACTION_TEI_FLAGS_ROW_SIZE_BITS;

    ctx->cco_action.tei_flags[row] |= (1 << ((tei
         % CP_CCO_ACTION_TEI_FLAGS_ROW_SIZE_BITS) - 1));
}

void
cp_cco_action_tei_clear (cp_t *ctx)
{
    dbg_assert (ctx);

    memset (&ctx->cco_action.tei_flags, 0,
            sizeof (uint) * CP_CCO_ACTION_TEI_FLAGS_ROW);
}

/**
 * initialisation of CCo action module.
 * \param  ctx  the module context.
 *
 */
void
cp_cco_action_init (cp_t *ctx)
{
    dbg_assert (ctx);

    memset (&ctx->cco_action, 0, sizeof (cp_cco_action_t));

    /* Initialise the SET. */
    heap_init (&ctx->cco_action.selection_heap,
              cp_cco_action_cco_selection__less);
}

/**
 * Uninitialisation of CCo action module.
 * \param  ctx  the module context.
 *
 */
void
cp_cco_action_uninit (cp_t *ctx)
{
    dbg_assert (ctx);

    cp_cco_action_cco_selection__clear (ctx);
}

/**
 * Change the SNID.
 * \param  ctx  the module context.
 *
 *  It shall request the SNIDs in use from the station manager to choose
 * one randomly in the available SNIDs
 */
void
cp_cco_action_snid_change (cp_t *ctx)
{
    cp_snid_t snid;
    dbg_assert (ctx);

    snid = cp_cco_action_snid_choose (ctx);

    // Provide the SNID to the beacon module.
    cp_beacon_change_snid (ctx, snid);
}

/**
 * Change the SNID right now.
 * \param  ctx  the module context.
 *
 *  It shall request the SNIDs in use from the station manager to choose
 * one randomly in the available SNIDs
 */
void
cp_cco_action_snid_change_now (cp_t *ctx)
{
    cp_snid_t snid;
    dbg_assert (ctx);

    snid = cp_cco_action_snid_choose (ctx);

    cp_sta_own_data_set_snid (ctx, snid);
}

/**
 * perform garbage actions of CCo's responsibility.
 * \param  ctx  the module context.
 */
void
cp_cco_action_garbage (cp_t *ctx)
{
    cp_sta_t *sta;
    cp_sta_t *sta_next;
    cp_net_t *net;
    dbg_assert (ctx);

    if (cp_sta_own_data_get_tei (ctx))
    {
        net = cp_sta_mgr_get_our_avln (ctx);

        sta = cp_net_sta_get_first (ctx, net, CP_NET_STA_ASSOC);
        while (sta)
        {
            slab_addref (sta);
            sta_next = cp_net_sta_get_next (ctx, net, sta);
            if (less_mod2p32(sta->tei_lease_ms, cp_sta_core_get_date_ms (ctx)))
            {
                // Remove the node from the tei lease.
                cp_cco_action__cco__tei_expired (ctx, sta);
            }
            slab_release (sta);
            sta = sta_next;
        }

        cp_sta_mgr__assoc__timeout (ctx, cp_sta_core_get_date_ms (ctx));

        /* Compute the interval to request the discover beacon from the
         * stations of our AVLN. */
        cp_beacon_discover_compute_interval (ctx);
    }
}

/**
 * generate a new NEK value.
 * \param  ctx  the module context.
 *
 *   NEK shall be used no more than 1 hour, so must be changed after 1
 * hour  for that purpose, NEK expiration date is checked at every occurrence
 * of  the global garbage collector timer and if current NEK expiration
 * date reached,  a new NEK value shall be generated by calling this function
 */
void
cp_cco_action_gen_nek (cp_t *ctx)
{
    cp_key_t key;

    dbg_assert (ctx);
    cp_secu_aes_generate_key (lib_rnd32 (&ctx->rnd), &key);
    cp_beacon_change_nek (ctx, 0, key, true /* now. */);
}


/**
 * Send the SET tei map IND to all the station with the TEI of the new one.
 * \param  ctx  the CP context.
 * \param  tei  the tei of the station which has joined the AVLN.
 * \param  mac  the station mac address.
 */
static void
cp_cco_action_send_set_tei_map_to_new_sta (cp_t *ctx, cp_tei_t tei,
                                           mac_t mac)
{
    cp_mme_tx_t *mme_tx;
    cp_sta_t *sta_list;
    cp_mme_peer_t *peer;
    cp_net_t *net;

    dbg_assert (ctx);
    dbg_assert (tei);
    dbg_assert (mac);

    // Send unicast.
    // It will send all the TEI MAP to the station just associated.
    net = cp_sta_mgr_get_our_avln (ctx);
    peer = &CP_MME_PEER (mac, tei);

    // +1 For our own station which is not in the AVLN.
    mme_tx = cp_msg_cc_set_tei_map_ind_send_begin (ctx, peer,
                CP_MSG_CC_SET_TEI_MAP_IND_MODE_UPDATE,
                cp_net_get_num_stas (ctx, net) + 1);

    // Add our station first.
    cp_msg_cc_set_tei_map_ind_send_sta (ctx, mme_tx,
                            cp_sta_own_data_get_tei(ctx),
                            cp_sta_own_data_get_mac_address (ctx),
                            CP_MSG_CC_SET_TEI_MAP_IND_STATUS_AUTHENTICATED);

    sta_list = cp_net_sta_get_first (ctx, net, CP_NET_STA_ASSOC);
    while (sta_list)
    {
        enum cp_msg_cc_set_tei_map_ind_status_t station_status;

        if (cp_sta_get_authenticated (ctx, sta_list))
        {
            station_status =
                CP_MSG_CC_SET_TEI_MAP_IND_STATUS_AUTHENTICATED;
        }
        else if (cp_sta_get_tei (sta_list))
        {
            station_status = CP_MSG_CC_SET_TEI_MAP_IND_STATUS_ASSOCIATED;
        }
        else
        {
            station_status =
                CP_MSG_CC_SET_TEI_MAP_IND_STATUS_DISASSOCIATED;
        }
        cp_msg_cc_set_tei_map_ind_send_sta (ctx, mme_tx,
                                            cp_sta_get_tei (sta_list),
                                            cp_sta_get_mac_address (sta_list),
                                            station_status);

        sta_list = cp_net_sta_get_next (ctx, net, sta_list);

    }
    cp_msg_cc_set_tei_map_ind_send_end (ctx, mme_tx);
}

/**
 * Send the SET TEI MAP to all the station of the NET.
 * \param  ctx  the CP context.
 * \param  tei  the new station tei.
 * \param  mac  the station mac address.
 */
static void
cp_cco_action_send_set_tei_map_to_all_sta (cp_t *ctx, cp_tei_t tei,
                                           mac_t mac)
{
    cp_mme_peer_t *peer;
    cp_mme_tx_t *mme_tx;
    dbg_assert (ctx);
    dbg_assert (tei);
    dbg_assert (mac);

    peer = &CP_MME_PEER_ALL_STA;
    mme_tx = cp_msg_cc_set_tei_map_ind_send_begin (ctx, peer,
                CP_MSG_CC_SET_TEI_MAP_IND_MODE_ADD, 1);

    cp_msg_cc_set_tei_map_ind_send_sta (ctx, mme_tx,
                tei, mac,
                CP_MSG_CC_SET_TEI_MAP_IND_STATUS_ASSOCIATED);

    cp_msg_cc_set_tei_map_ind_send_end (ctx, mme_tx);
}

/**
 * Assoc type join.
 * \param  ctx  the CP context.
 * \param  msg  the assoc MME message.
 * \param  assoc  the assoc data read from the MME.
 */
static void
cp_cco_action__assoc__join (cp_t *ctx, cp_mme_rx_t *msg,
                            cp_msg_cc_assoc_req_t *assoc)
{
    cp_net_t *net;
    cp_sta_t *sta;
    cp_tei_t tei;
    cp_msg_cc_assoc_cnf_t cnf;

    dbg_assert (ctx);
    dbg_assert (msg);
    dbg_assert (assoc);

    /* Add the station to the AVLN. */
    net = cp_sta_mgr_get_our_avln (ctx);

    cnf.nid = cp_net_get_nid (ctx, net);
    cnf.snid = cp_net_get_snid (ctx, net);


    // If the station already exists.
    sta = cp_sta_mgr_sta_get_from_mac (ctx, msg->peer.mac);
    if (sta)
    {
        // The station already exists get the TEI.
        tei = cp_sta_get_tei (sta);
        slab_release (sta);
    }

    if ((sta && !tei) || !sta)
    {
        /* Try to get a TEI. */
        tei = cp_cco_action_tei_compute (ctx);
    }

    if (tei)
    {
        cnf.result = CP_MSG_CC_ASSOC_CNF_RESULT_SUCCESS;
        cnf.lease_time_min = CP_LEASE_ASSOC_MIN;
    }
    else
    {
        cnf.result =
            CP_MSG_CC_ASSOC_CNF_RESULT_FAILURE_TEMPORARY_RESSOURCE_EXHAUSTION;
    }

    cnf.sta_tei = tei;

    /* Send the answer. */
    cp_msg_cc_assoc_cnf_send (ctx, &msg->peer, &cnf);

    if (tei)
    {
        /* Send TEI MAP to all STA except the new one (need to do it before
         * adding the new STA to the STA manager). */
        cp_cco_action_send_set_tei_map_to_all_sta (ctx, tei, msg->peer.mac);
        sta = cp_sta_mgr_sta_add (ctx, net, tei, msg->peer.mac);
        /* Add the TEI lease corresponding to an unassociated STA. */
        cp_sta_mgr_commit_to_dataplane (ctx);
        sta->tei_lease_ms =
            MAC_SEC_TO_MS(CP_LEASE_ASSOC_MIN * 60);
        sta->last_seen_ms = cp_sta_core_get_date_ms (ctx) + sta->tei_lease_ms;

        /* Update the last request date in the sta. */
        cp_sta_set_assoc_confirmed (ctx, sta, false);
        sta->assoc_req_last_ms = cp_sta_core_get_date_ms (ctx);
        /* A new STA has joined an AVLN for the first time, generate an
         * event. */
        cp_fsm_event_t *event = cp_fsm_event_sta_new
            (ctx, CP_FSM_EVENT_TYPE_new_sta_associated, net,
             (cp_sta_t *) sta);
        cp_fsm_post (ctx, event);

        /* Release the reference on the station. */
        slab_release (sta);
    }
    if (!tei)
    {
        sta = cp_sta_mgr_sta_add (ctx, net, 0, msg->peer.mac);

        /* Last seen  current date + 7 min. */
        sta->last_seen_ms = cp_sta_core_get_date_ms (ctx) +
            MAC_SEC_TO_MS(420);
        /* Release the reference on the station. */
        slab_release (sta);
    }
}

/**
 * Assoc type renew.
 * \param  ctx  the CP context.
 * \param  msg  the assoc MME message.
 * \param  assoc  the assoc data read from the MME.
 */
static void
cp_cco_action__assoc__renew (cp_t *ctx, cp_mme_rx_t *msg,
                            cp_msg_cc_assoc_req_t *assoc)
{
    cp_net_t *net;
    cp_sta_t *sta;
    cp_msg_cc_assoc_cnf_t cnf;

    dbg_assert (ctx);
    dbg_assert (msg);
    dbg_assert (assoc);

    /* Add the station to the AVLN. */
    net = cp_sta_mgr_get_our_avln (ctx);

    cnf.nid = cp_net_get_nid (ctx, net);
    cnf.snid = cp_net_get_snid (ctx, net);

    /* Renew the TEI. */
    sta = cp_sta_mgr_sta_get_assoc (ctx, net, msg->peer.tei);

    if (sta)
    {
        /* Renew the TEI. */
        sta->tei_lease_ms =
            MAC_SEC_TO_MS(CP_LEASE_AUTH_MIN * 60);
        sta->last_seen_ms = cp_sta_core_get_date_ms (ctx) + sta->tei_lease_ms;

        cnf.result = CP_MSG_CC_ASSOC_CNF_RESULT_SUCCESS;
        cnf.sta_tei = cp_sta_get_tei (sta);
        cnf.lease_time_min = CP_LEASE_AUTH_MIN;

        slab_release (sta);
    }
    else
    {
        cnf.result = CP_MSG_CC_ASSOC_CNF_RESULT_FAILURE_OTHER_REASON;
        cnf.sta_tei = 0;
        cnf.lease_time_min = 0;
    }

    /* Send the answer. */
    cp_msg_cc_assoc_cnf_send (ctx, &msg->peer, &cnf);
}

/**
 * Static function part.
 * \param  ctx  the CP module context.
 * \param  assoc_req  the MME context.
 * \param  assoc  The mme in the processor format.
 */
static void
cp_cco_action_manage_sta_assoc_common (cp_t *ctx, cp_mme_rx_t *assoc_req,
                                       cp_msg_cc_assoc_req_t *assoc)
{
    dbg_assert (ctx);
    dbg_assert (assoc_req);
    dbg_assert (assoc);
    dbg_assert (ctx->mac_store);


    if (assoc->request_type == CP_MSG_CC_ASSOC_REQ_TYPE_NEW)
        cp_cco_action__assoc__join (ctx, assoc_req, assoc);
    else if (assoc->request_type == CP_MSG_CC_ASSOC_REQ_TYPE_RENEW)
        cp_cco_action__assoc__renew (ctx, assoc_req, assoc);
    else
        dbg_assert (assoc->request_type == CP_MSG_CC_ASSOC_REQ_TYPE_NB);
}

/**
 * manage association of a station.
 * \param  ctx  the module context.
 * \param  assoc_req  CM_ASSOC.REQ MME msg having being received
 */
void
cp_cco_action__cco__cc_assoc_req (cp_t *ctx, cp_mme_rx_t * assoc_req)
{
    cp_msg_cc_assoc_req_t assoc;

    dbg_assert (ctx);
    dbg_assert (assoc_req);

    /* Reading the data in the MME. */
    if (cp_msg_cc_assoc_req_receive(ctx, assoc_req, &assoc))
        cp_cco_action_manage_sta_assoc_common (ctx, assoc_req, &assoc);
}

/**
 * manage association of a station.
 * \param  ctx  the module context.
 * \param  assoc_req  CC_ASSOC.REQ MME msg having being received
 */
void
cp_cco_action__ucco__cc_assoc_req (cp_t *ctx, cp_mme_rx_t * assoc_req)
{
    cp_msg_cc_assoc_req_t assoc;

    dbg_assert (ctx);
    dbg_assert (assoc_req);
    dbg_assert (!cp_sta_own_data_get_tei (ctx));

    /* Reading the data in the MME. */
    cp_msg_cc_assoc_req_receive(ctx, assoc_req, &assoc);

    /* The MME is not for the station, it shall answer to the source station
     * with a Failure for others reason. */
    if ((assoc.request_type != CP_MSG_CC_ASSOC_REQ_TYPE_NEW)
        || assoc.nid != cp_sta_own_data_get_nid (ctx))
    {
    }
    else
    {
        /* Call the cco action start module.
         * It will provide a TEI to our station
         * Call the cco send central beacon in the beacon module.
         */
        cp_cco_action__unassoc__cco_start (ctx);

        /* Trigger an event to the FSM. */
        cp_fsm_trigger_new_event (ctx, bare, association_request);

        /* Continue the association procedure. */
        cp_cco_action_manage_sta_assoc_common (ctx, assoc_req, &assoc);
    }
}

/**
 * manage authentication of a station.
 * \param  ctx  the module context.
 * \param  get_key_req  CM_GET_KEY.REQ MME msg having being decrypted
 */
void
cp_cco_action__cco__cm_get_key_req_pid0 (cp_t *ctx, cp_mme_rx_t * get_key_req)
{
    cp_msg_cm_get_key_req_t req;
    cp_msg_cm_get_key_cnf_t cnf;
    cp_net_t *net;
    cp_sta_t *sta;

    dbg_assert (ctx);
    dbg_assert (get_key_req);

    // get the data in the payload of the mme.
    if (cp_msg_cm_get_key_req_receive (ctx, get_key_req, &req)
        && (cp_secu_protocol_check (NULL, &get_key_req->prun)
            == CP_SECU_PROTOCOL_RUN_CHECK_RESULT_NEW)
        && (req.key_type == CP_MSG_KEY_NEK)
        && MAC_TEI_IS_STA (get_key_req->peer.tei))
    {
        // Get the network.
        net = cp_sta_mgr_get_our_avln (ctx);
        // Get the station.
        sta = cp_sta_mgr_sta_get_assoc (ctx, net, get_key_req->peer.tei);

        if (sta)
        {
            uint nb_sta;
            cnf.result = CP_MSG_CM_GET_KEY_CNF_RESULT_KEY_GRANTED;
            cp_sta_set_assoc_confirmed (ctx, sta, true);

            if (req.key_type == CP_MSG_KEY_NEK)
            {
                uint i;

                cnf.eks = ctx->mac_config->nek[ctx->beacon.nek_index].eks;
                for (i = 0; i < COUNT (ctx->mac_config->nek[0].nek_enc); i++)
                    cnf.key.key[i] =
                        ctx->mac_config->nek[ctx->beacon.nek_index].nek_enc[i];
            }

            /* Send the TEI map. */
            nb_sta = cp_net_get_num_stas (ctx, net);
            if (nb_sta)
            {
                cp_cco_action_send_set_tei_map_to_new_sta (ctx,
                                                           cp_sta_get_tei(sta),
                                                           get_key_req->peer.mac);
            }

            slab_release (sta);
        }
        else
            cnf.result = CP_MSG_CM_GET_KEY_CNF_RESULT_REQUEST_REFUSED;
    }
    // Refuse the request.
    else
        cnf.result = CP_MSG_CM_GET_KEY_CNF_RESULT_REQUEST_REFUSED;

    cnf.key_type = req.key_type;
    cnf.nid = req.nid;
    cp_secu_protocol_next (&get_key_req->prun, true);
    cp_msg_cm_get_key_cnf_send (ctx, &get_key_req->peer, get_key_req->peks,
                                &get_key_req->prun, &cnf);
}

void
cp_cco_action__cco__sta_leave_send_tei_map (cp_t *ctx, cp_sta_t *sta)
{
    cp_mme_peer_t *peer;
    cp_net_t *net = NULL;
    uint status;
    cp_mme_tx_t *mme;

    dbg_assert (ctx);
    dbg_assert (sta);

    // Send the TEI MAP to the station.
    net = cp_sta_mgr_get_our_avln (ctx);

    if (!cp_net_is_empty (ctx, net))
    {
        peer = &CP_MME_PEER_ALL_STA;

        if (cp_sta_get_authenticated (ctx, sta))
        {
            status = CP_MSG_CC_SET_TEI_MAP_IND_STATUS_AUTHENTICATED;
        }
        else if (cp_sta_get_tei (sta))
        {
            status = CP_MSG_CC_SET_TEI_MAP_IND_STATUS_ASSOCIATED;
        }
        else
            status = CP_MSG_CC_SET_TEI_MAP_IND_STATUS_DISASSOCIATED;

        mme = cp_msg_cc_set_tei_map_ind_send_begin (ctx, peer,
                 CP_MSG_CC_SET_TEI_MAP_IND_MODE_DELETE,
                 1);
        cp_msg_cc_set_tei_map_ind_send_sta (ctx, mme, cp_sta_get_tei (sta),
                                            cp_sta_get_mac_address (sta),
                                            status);
        cp_msg_cc_set_tei_map_ind_send_end (ctx, mme);
    }
}

/**
 * manage explicit leave request of a station.
 * \param  ctx  the module context.
 * \param  sta  provided by the garbage function to send a CC_LEAVE_IND.
 *
 * Remove the station from the station manager.
 * \warn It does not release the reference of the station provided
 * in parameters.
 */
void
cp_cco_action__cco__tei_expired (cp_t *ctx, cp_sta_t *sta)
{
    cp_mme_peer_t peer;
    cp_net_t *net;

    dbg_assert (ctx);

    /* Get the network of the station. */
    net = cp_sta_mgr_get_our_avln (ctx);

    cp_sta_get_peer (sta, &peer);

    cp_msg_cc_leave_ind_send (ctx, &peer,
                              CP_MSG_CC_LEAVE_IND_REASON_TEI_LEASE_EXPIRED,
                              cp_net_get_nid(ctx, net));

    cp_cco_action__cco__sta_leave_send_tei_map (ctx, sta);
    cp_sta_mgr_release_station (ctx, cp_sta_get_tei (sta));

    /* Verify if the network is empty, if it is trigger an event to the FSM. */
    if (cp_net_is_empty (ctx, net))
        cp_fsm_trigger_new_event (ctx, bare, all_sta_leaved);
}

/**
 * Release a station.
 * \param  ctx  the CP context.
 * \param  mme  the MME received.
 */
void
cp_cco_action__cco__cc_leave (cp_t *ctx, cp_mme_rx_t *mme)
{
    cp_sta_t *sta;
    cp_net_t *net;
    bool res_read;
    enum cp_msg_cc_leave_req_reason_t reason_req;
    cp_nid_t nid;
    enum cp_msg_cc_leave_ind_reason_t reason_ind;


    dbg_assert (ctx);
    dbg_assert (mme);

    dbg_assert (mme->peer.tei);

    switch (mme->mmtype)
    {
    case CC_LEAVE_REQ:
        res_read = cp_msg_cc_leave_req_receive (ctx, mme, &reason_req);
        break;
    case CC_LEAVE_IND:
        res_read = cp_msg_cc_leave_ind_receive (ctx, mme, &reason_ind,
                                                &nid);
        break;
    default:
        dbg_assert_default ();
    }

    if (res_read)
    {
        net = cp_sta_mgr_get_our_avln (ctx);
        /* Get the station. */
        sta = cp_sta_mgr_sta_get_assoc (ctx, net, mme->peer.tei);
        dbg_check (sta);

        if (mme->mmtype == CC_LEAVE_REQ)
            cp_msg_cc_leave_cnf_send (ctx, &mme->peer);
        else
            cp_msg_cc_leave_rsp_send (ctx, &mme->peer);
        cp_beacon_discover_sta_removed (ctx, sta);
        cp_sta_mgr_release_station (ctx, cp_sta_get_tei (sta));

        cp_cco_action__cco__sta_leave_send_tei_map (ctx, sta);
        slab_release (sta);
    }
}

/**
 * Start to act as a CCo coming from the unassociated state..
 * \param  ctx  the module context.
 *
 * This function shall:
 *   - Choose a TEI for the station to authenticate it.
 *   - Choose a SNID for the AVLN.
 *   - Create and set our AVLN in the station manager.
 *   - Change the station CCo status.
 *   - Generate the NEK.
 *   - Program the NEK change timer.
 *   - Send a central beacon.
 */
void
cp_cco_action__unassoc__cco_start (cp_t *ctx)
{
    cp_tei_t tei;
    cp_net_t *net;
    cp_nid_t nid;
    cp_snid_t snid;
    dbg_assert (ctx);

    /* Verify if the station does not already have a TEI. */
    if (!cp_sta_own_data_get_tei(ctx))
    {
        tei = cp_cco_action_tei_compute (ctx);
        cp_sta_own_data_set_tei (ctx, tei);
        cp_sta_own_data_set_authenticated_status (ctx, true);
    }

    /* Set our AVLN. */
    snid = cp_sta_own_data_get_snid (ctx);
    nid = cp_sta_own_data_get_nid (ctx);
    net = cp_sta_mgr_add_avln (ctx, snid, nid);
    cp_sta_mgr_set_our_avln (ctx, net);


    cp_cco_action_gen_nek (ctx);
    cp_cco_action__assoc__cco_start (ctx);
}

/**
 * Start to act as a CCo coming from the unassociated state..
 * \param  ctx  the module context.
 *
 * This function shall:
 *   - Change the station CCo status.
 *   - Generate the NEK.
 *   - Program the NEK change timer (to change in few minutes).
 *   - Program the beacon timer to send a central beacon at the beginning of
 *   the next beacon period.
 */
void
cp_cco_action__assoc__cco_start (cp_t *ctx)
{
    cp_fsm_event_t *event;
    cp_sta_own_data_t *own;
    dbg_assert (ctx);

    /* Set the CCo status in the station own data. */
    own = cp_sta_mgr_get_sta_own_data (ctx);
    own->nid_track = cp_sta_own_data_get_nid (ctx);
    cp_sta_own_data_set_cco_status (ctx, true);

    // Program the timer to change the NEKs.
    event = cp_fsm_event_bare_new (ctx, CP_FSM_EVENT_TYPE_change_nek);
    cp_sta_core_gen_cyclic_event (ctx, &ctx->cco_action.nek_change,
                                  event,
                                  HPAV_NEK_CHANGE_MS);


    /* Call the beacon module to send the central beacon. */
    cp_beacon_discover_compute_interval (ctx);
    cp_beacon_cco_send_central_beacon (ctx);

    /* trigger the event for the assoc FSM */
    cp_fsm_trigger_new_event (ctx, bare, become_cco);
}

/**
 * Stop acting as a CCo and become a simple station.
 * \param  ctx  the module context.
 *
 * This function shall:
 *   - Change the station status.
 *   - Stop the NEK generation.
 */
void
cp_cco_action__cco__assoc_stop (cp_t *ctx)
{
    dbg_assert (ctx);

    /* Change the CCo status to false, the station leave the CCo status. */
    cp_sta_own_data_set_cco_status (ctx, false);

    /* Stop the timer for the NEK generation keys. */
    cp_sta_core_stop_timed_or_cyclic_event (ctx, &ctx->cco_action.nek_change);

    /* clear the TEI list. */
    cp_cco_action_tei_clear (ctx);
    cp_cco_action_cco_selection__clear (ctx);
    cp_beacon_reconfigure_timer (ctx);
}

/**
 * Stop acting as a CCo and become a unassociated station.
 * \param  ctx  the module context.
 *
 * This function shall:
 *   - Change the station status.
 *   - Reprogram the beacon timer to expire a few time after the beacon period
 *   start date.
 *   - Stop the NEK generation.
 *   - Delete the AVLN.
 *   - Change the station authenticated status to not authenticated.
 */
void
cp_cco_action__cco__unassoc_stop (cp_t *ctx)
{
    dbg_assert (ctx);

    cp_cco_action__cco__assoc_stop (ctx);

    /* Delete the AVLN. */
    cp_sta_mgr_set_our_avln (ctx, NULL);
    cp_sta_mgr_remove_avln (ctx, cp_sta_own_data_get_snid (ctx),
                            cp_sta_own_data_get_nid (ctx));

    /* Remove the station own data TEI. */
    cp_sta_own_data_set_tei (ctx, MAC_TEI_UNASSOCIATED);
    cp_sta_own_data_set_authenticated_status (ctx, false);

    /* Set our network to NULL. */
    cp_sta_mgr_set_our_avln (ctx, NULL);
}

void
cp_cco_action_drv_mac_stop (cp_t *ctx)
{
    dbg_assert (ctx);
    dbg_assert (cp_sta_own_data_get_cco_status (ctx));

    if (!cp_net_is_empty (ctx, cp_sta_mgr_get_our_avln (ctx))
        && (!cp_beacon_any_countdown_active (ctx)))
    {
        cp_sta_t *sta;
        cp_net_t *net;

        /* Clear the SET of cco selection. */
        cp_cco_action_cco_selection__clear (ctx);
        ctx->handover.reason = CP_HANDOVER_REASON_CCO_LEAVING;

        net = cp_sta_mgr_get_our_avln (ctx);
        for (sta = cp_net_sta_get_first (ctx, net, CP_NET_STA_ASSOC);
             sta;
             sta = cp_net_sta_get_next (ctx, net, sta))
        {
            if (cp_sta_get_authenticated (ctx, sta))
                cp_cco_action_cco_selection__mac_stop_sta_add (ctx, sta);
        }

        /* Trigger the event to start the handover CCo FSM. */
        cp_fsm_trigger_new_event (ctx, bare, CCO_HANDOVER_START);
        /* Trigger an event to change the Main FSM to go to the
         * CCO_LEAVING_HOIP state. */
        cp_fsm_trigger_new_event (ctx, bare, CCO_LEAVING_HANDOVER);
    }
    else if (cp_net_is_empty (ctx, cp_sta_mgr_get_our_avln (ctx))
             && (!cp_beacon_any_countdown_active (ctx)))
    {
        cp_fsm_event_t *event;
        /* Generate the FSM event. */
        event = cp_fsm_event_bare_new (ctx, CP_FSM_EVENT_TYPE_left);
        cp_fsm_post (ctx, event);
    }
}

void
cp_cco_action_drv_mac_stop_suspend (cp_t *ctx)
{
    cp_beacon_cco_send_central_beacon (ctx);
    cp_cco_action_drv_mac_stop (ctx);
}

void
cp_cco_action_drv_mac_stop_ended (cp_t *ctx)
{
    dbg_assert (ctx);
    cp_cco_action__cco__assoc_stop (ctx);

    cp_fsm_trigger_new_event (ctx, bare, to_leave);
}

void
cp_cco_action_cco_selection__mac_stop_sta_add (cp_t *ctx, cp_sta_t *sta)
{
    dbg_assert (ctx);
    dbg_assert (sta);

    slab_addref (sta);
    heap_node_init (&sta->cco_selection_node);
    heap_insert (&ctx->cco_action.selection_heap,
                 &sta->cco_selection_node);
}


void
cp_cco_action_cco_selection__sta_add (cp_t *ctx, cp_net_t *net, cp_sta_t *sta)
{
    cp_sta_own_data_t *own;
    dbg_assert (ctx);
    dbg_assert (net);
    dbg_assert (sta);

    if ((net == cp_sta_mgr_get_our_avln (ctx))
        && (MAC_TEI_IS_STA (cp_sta_get_tei (sta)))
        && (cp_sta_get_authenticated (ctx, sta)))
    {
        own = cp_sta_mgr_get_sta_own_data (ctx);
        if ((own->cco_prefered == false)
            && ((sta->cco_cap > CP_CCO_LEVEL)))
        {
            slab_addref (sta);
            heap_node_init (&sta->cco_selection_node);
            heap_insert (&ctx->cco_action.selection_heap,
                        &sta->cco_selection_node);
        }
    }
}

void
cp_cco_action_cco_selection__sta_remove (cp_t *ctx, cp_sta_t *sta)
{
    dbg_assert (ctx);
    dbg_assert (sta);

    dbg_assert (!heap_empty (&ctx->cco_action.selection_heap));

    heap_remove (&ctx->cco_action.selection_heap, &sta->cco_selection_node);
    heap_node_init (&sta->cco_selection_node);
    slab_release (sta);
}

void
cp_cco_action_cco_selection__clear (cp_t *ctx)
{
    cp_sta_t *sta;
    heap_node_t *node;

    dbg_assert (ctx);

    /* Verify the set. */
    while (!heap_empty (&ctx->cco_action.selection_heap))
    {
        node = heap_get_root (&ctx->cco_action.selection_heap);
        dbg_assert (node);

        sta = PARENT_OF (cp_sta_t, cco_selection_node, node);
        dbg_assert (sta);

        /* Remove the node from the heap. */
        heap_remove (&ctx->cco_action.selection_heap, &sta->cco_selection_node);
        heap_node_init (&sta->cco_selection_node);
        slab_release (sta);
    }
}

cp_sta_t *
cp_cco_action_cco_selection__selection (cp_t *ctx)
{
    cp_sta_own_data_t *own;
    cp_sta_t *sta = NULL;
    heap_node_t *node;
    dbg_assert (ctx);

    own = cp_sta_mgr_get_sta_own_data (ctx);

    if ((!own->cco_prefered)
        && (!heap_empty (&ctx->cco_action.selection_heap)))
    {
        node = heap_get_root (&ctx->cco_action.selection_heap);
        dbg_assert (node);

        sta = PARENT_OF (cp_sta_t, cco_selection_node, node);
        dbg_assert (sta);
        slab_addref (sta);
    }

    return sta;
}

void
cp_cco_action_sc__sc_cco__cc_assoc_req (cp_t *ctx, cp_mme_rx_t *mme)
{
    /* Check parameters. */
    dbg_assert (ctx);
    dbg_assert (mme);

    /* Check received MME.  */
    cp_msg_cc_assoc_req_t req;
    if (!cp_msg_cc_assoc_req_receive (ctx, mme, &req))
    {
        /* Error in the MME, do nothing. */
    }
    else
    {
        if (req.request_type == CP_MSG_CC_ASSOC_REQ_TYPE_NEW)
        {
            /* Request of association. */
            /* Peer is SC? */
            if (cp_mme_peer_cmp (&mme->peer, &ctx->sta_action.sc.peer))
            {
                /* Ok, reply  */
                cp_cco_action__assoc__join (ctx, mme, &req);
                cp_sta_t *sta = cp_sta_mgr_sta_get_from_mac (ctx, ctx->sta_action.sc.peer.mac);
                ctx->sta_action.sc.peer.tei = cp_sta_get_tei (sta);
                slab_release (sta);
            }
            else
            {
                /* Deny assoc request. */
                cp_msg_cc_assoc_cnf_t cnf;
                cnf.result = CP_MSG_CC_ASSOC_CNF_RESULT_FAILURE_OTHER_REASON;
                /* Get STA for AVLN. */
                cp_sta_t *sta = cp_sta_mgr_sta_get_from_mac (ctx, mme->peer.mac);
                cp_net_t *net = cp_sta_get_net (sta);
                /* Release STA. */
                slab_release (sta);
                /* Set NID/SNID. */
                cnf.nid = cp_net_get_nid (ctx, net);
                cnf.snid = cp_net_get_snid (ctx, net);
                /* No TEI for the STA. */
                cnf.sta_tei = 0;
                /* Lease default value. */
                cnf.lease_time_min = CP_LEASE_ASSOC_MIN;
                /* Send reply. */
                cp_msg_cc_assoc_cnf_send (ctx, &mme->peer, &cnf);
            }
        }
        else if (req.request_type == CP_MSG_CC_ASSOC_REQ_TYPE_RENEW)
            /* Renew is ok. */
            cp_cco_action__assoc__renew (ctx, mme, &req);
        else
            /* Should not occur. */
            dbg_assert (req.request_type == CP_MSG_CC_ASSOC_REQ_TYPE_NB);
    }
}

void
cp_cco_action_handover_ended (cp_t *ctx)
{
    /* trigger the event for the assoc FSM. */
    cp_fsm_trigger_new_event (ctx, bare, become_sta);
}