summaryrefslogtreecommitdiff
path: root/cesar/cp/sta/mgr/src/sta_mgr.c
blob: 480396a7a5d4d45652bdc134567c5d698ebfd23d (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/sta/mgr/src/sta_mgr.c
 * \brief   Station manager source.
 * \ingroup cp_sta_mgr
 *
 */
#include "common/std.h"

#include "cp/defs.h"
#include "cp/cp.h"
#include "cp/fsm/fsm.h"
#include "cp/sta/mgr/sta_mgr.h"
#include "cp/sta/mgr/sta_own_data.h"
#include "cp/sta/core/core.h"
#include "cp/cco/action/cco_action.h"
#include "cl/cl_mactotei.h"
#include "mac/sar/sar.h"
#include "bsu/bsu.h"

#include "cp/inc/context.h"
#include "cp/sta/mgr/inc/sta.h"
#include "cp/sta/mgr/inc/net.h"
#include "cp/sta/mgr/inc/sta_mgr.h"
#include "cp/sta/mgr/inc/sta_own_data.h"

/**
 * Elects a station in our AVLN to be the acknowledge station for all the
 * station to which the packet is send.
 * \param  ctx  the module context.
 *
 * This function choose the station using the data of the tone maps in the
 * station contained in the mac store. It shall insert in the mac config the
 * station's TEI which will acknowledge the packets.
 * The station manager is responsible to elects another station if the current
 * one is removed or if another one shall become the acknowledge station.
 */
void
cp_sta_mgr_elects_sta_partial_ack (cp_t *ctx)
{
    dbg_assert (ctx);

    /* FIXME:To be correctly specified, the BLE seems to not be a good
     * criteria for the partial ack, when we only have ROBO tonemap the BLE
     * is the same for all the stations. */
//    /* Do nothing if your AVLN is not set. */
//    if (ctx->sta_mgr.our_avln != NULL)
//    {
//        cp_sta_t *sta;
//        sta_t *sta_store;
//
//        dbg_assert (ctx->mac_store);
//        dbg_assert (ctx->mac_config);
//
//        /* The BLE is a 8 bits long field. */
//        uint ble = 0x100;
//        cp_tei_t sta_tei = MAC_TEI_UNASSOCIATED;
//
//        /* Parse the station manager for all the station present in our AVLN.
//         * The one which as the lesser BLE will be choose to be the partial
//         * ack's station. */
//        for (sta = cp_net_sta_get_first (ctx, ctx->sta_mgr.our_avln,
//                                         CP_NET_STA_ASSOC);
//             sta;
//             sta = cp_net_sta_get_next (ctx, ctx->sta_mgr.our_avln, sta))
//        {
//            /* Get the station from the mac store. */
//            sta_store = mac_store_sta_get (ctx->mac_store, cp_sta_get_tei
//                                           (sta));
//            dbg_assert (sta_store);
//            if (sta_store->tx_tonemaps
//                && (sta_store->tx_tonemaps->default_tmi < TONEMAP_INDEX_NB))
//            {
//                uint tmi = sta_store->tx_tonemaps->default_tmi;
//
//                if (ble > sta_store->tx_tonemaps->tm[tmi]->ble)
//                {
//                    ble = sta_store->tx_tonemaps->tm[tmi]->ble;
//                    sta_tei = cp_sta_get_tei (sta);
//                }
//            }
//
//            blk_release (sta_store);
//        }
//
//        /* Store the TEI of the station. */
//        if (sta_tei != MAC_TEI_UNASSOCIATED)
//            ctx->mac_config->partial_ack_tei_default = sta_tei;
//    }
}

/**
 * Compare two station with the mac address.
 * \param  left  the left station.
 * \param  right  the right station.
 */
static bool
cp_sta_mgr_sta_mac_address_less (set_node_t *left, set_node_t *right)
{
    cp_sta_private_t *sta_left;
    cp_sta_private_t *sta_right;

    dbg_assert (left);
    dbg_assert (right);

    sta_left = (cp_sta_private_t *) PARENT_OF (cp_sta_t,
                                               node_sta_mgr,
                                               left);
    sta_right = (cp_sta_private_t *) PARENT_OF (cp_sta_t,
                                               node_sta_mgr,
                                               right);

    if (sta_left->mac_address < sta_right->mac_address)
        return true;
    else
        return false;
}

/**
 * Get the first station from the release station list.
 * \param  ctx  the CP context.
 * \return  the first station of the list.
 *
 * \warn It does not add a reference on the list, so a slab_release on the
 * station will free the station data, without removing it from the dataplane.
 */
static cp_sta_t *
cp_sta_mgr_release_sta_get_first (cp_t *ctx)
{
    cp_sta_t *sta = NULL;

    dbg_assert (ctx);

    if (!list_empty (&ctx->sta_mgr.release_sta_list))
    {
        sta = PARENT_OF (cp_sta_t, release_node,
                         list_begin (&ctx->sta_mgr.release_sta_list));
        dbg_check (sta);
    }

    return sta;
}

/**
 * Add a station to the station manager.
 * \param  ctx  the CP context.
 * \param  net  the network within the station exists.
 * \param  tei  the station's TEI.
 * \param  mac_address  the station's mac address.
 * \return  the newly created station.
 */
static cp_sta_t*
cp_sta_mgr_sta_add_realy (cp_t *ctx, cp_net_t *net, cp_tei_t tei,
                          mac_t mac_address)
{
    cp_sta_private_t *sta;
    bool state;

    sta = (cp_sta_private_t *) cp_sta_init (&ctx->sta_mgr.sta_slab_cache);

    sta->tei = tei;
    sta->mac_address = mac_address;
    sta->net = net;

    if (MAC_TEI_IS_STA (tei))
        sta->net->num_associated_stas ++;
    if (net == ctx->sta_mgr.our_avln &&  sta->net->num_associated_stas == 1)
        cp_fsm_post_new_event (ctx, bare, sta_status_changed);

    net->num_visible_stas ++;
    net->num_stas ++;
    sta->visible = true;
    sta->association_confirmed = true;

    /* Add the station to the station manager set only if the mac address is
     * not a broadcast one. */
    if (mac_address != MAC_BROADCAST)
    {
        state = set_insert (&ctx->sta_mgr.stas,
                            &sta->public_data.node_sta_mgr);
        dbg_check (state);
    }


    // Add the station to the non unassociated list.
    if (tei == MAC_TEI_UNASSOCIATED)
    {
        state = set_insert (&net->unassociated_stas,
                            &sta->public_data.node_net);
        dbg_check (state);
    }
    // Add the station to the authenticated list.
    else
    {
        state = set_insert (&net->associated_stas,
                            &sta->public_data.node_net);
        dbg_check (state);

        if (net == ctx->sta_mgr.our_avln)
        {
            dbg_assert (ctx->mac_store);
            dbg_assert (ctx->cl);

            mac_store_sta_add (ctx->mac_store, tei);
            cp_sta_set_assoc_confirmed (ctx, (cp_sta_t *) sta, true);
            cp_sta_mgr_commit_to_dataplane (ctx);
        }
    }

    // Add a reference on the station.
    slab_addref (sta);

    return &sta->public_data;
}

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

    memset (&ctx->sta_mgr, 0, sizeof (cp_sta_mgr_t));

    // Initialise the station own data.
    cp_sta_own_data_init (ctx);

    // Initialise the list of releasing stations.
    list_init (&ctx->sta_mgr.release_sta_list);

    // Initialise the slab cache.
    slab_cache_init (&ctx->sta_mgr.sta_slab_cache,
                     "Station cache",
                     sizeof (cp_sta_private_t),
                     (slab_object_destructor_t) cp_sta_uninit);

    // Initialise the set of station sorted by mac addresses.
    set_init (&ctx->sta_mgr.stas, cp_sta_mgr_sta_mac_address_less);
}

void
cp_sta_mgr_uninit (cp_t *ctx)
{
    cp_net_t *net;
    dbg_assert (ctx);
    // Uninitialise the station contained in the release list.
    if (ctx->sta_mgr.our_avln)
    {
        cp_sta_t *sta;
        while ((sta = cp_sta_mgr_release_sta_get_first (ctx)))
        {
            sar_sta_remove (ctx->sar, cp_sta_get_tei (sta));
            /* Remove the station from the list. */
            list_remove (&ctx->sta_mgr.release_sta_list,
                         &sta->release_node);
            slab_release (sta);
        }

        while ((sta = cp_net_sta_get_first (ctx, ctx->sta_mgr.our_avln,
                                            CP_NET_STA_ASSOC)))
        {
            cp_sta_mgr_sta_remove (ctx, sta);
            slab_release (sta);
        }
    }
    /* Remove all the AVLN present in the station manager. */
    for (net = cp_sta_mgr_get_first_avln (ctx);
         net;
         net = cp_sta_mgr_get_next_avln (ctx, net))
        cp_sta_mgr_remove_avln (
            ctx, cp_net_get_snid (ctx, net), cp_net_get_nid (ctx, net));
    // Uninitialise the station own data.
    cp_sta_own_data_uninit (ctx);
}

cp_net_t *
cp_sta_mgr_add_avln (cp_t *ctx, cp_snid_t snid, cp_nid_t nid)
{
    dbg_assert (ctx);
    dbg_assert (HPAV_NID_IS_VALID (nid));
    /* Search in the network list. */
    cp_net_t *net = cp_sta_mgr_get_avln (ctx, snid, nid);
    if (!net)
    {
        uint i;
        for (i = 0; i < HPAV_AVLNS_NB_MAX && !net; i++)
        {
            if (ctx->sta_mgr.networks[i].present == false)
            {
                net = &ctx->sta_mgr.networks[i];
                cp_net_init (ctx, net);
                net->present = true;
                net->snid = snid;
                net->nid = nid;
            }
        }
    }
    return net;
}

void
cp_sta_mgr_remove_avln (cp_t *ctx, cp_snid_t snid, cp_nid_t nid)
{
    dbg_assert (ctx);
    dbg_assert (HPAV_NID_IS_VALID (nid));
    cp_net_t *net = cp_sta_mgr_get_avln (ctx, snid, nid);
    if (net)
    {
        /* Is it our net ? */
        if (ctx->sta_mgr.our_avln == net)
            cp_sta_mgr_set_our_avln (ctx, NULL);
        /* Get the CCo to inform the BSU. */
        mac_t cco_mac = MAC_ZERO;
        cp_sta_t *cco = cp_net_get_cco (ctx, net);
        if (cco)
        {
            cco_mac = cp_sta_get_mac_address (cco);
            /* Mac address can be broadcast. */
            if (cco_mac == MAC_BROADCAST)
                cco_mac = MAC_ZERO;
            slab_release (cco);
        }
        /* Inform BSU. */
        bsu_avln_remove (nid, snid, cco_mac);
        cp_net_uninit (ctx, net);
    }
}

cp_net_t *
cp_sta_mgr_get_avln (cp_t *ctx, cp_snid_t snid, cp_nid_t nid)
{
    dbg_assert (ctx);
    dbg_assert (HPAV_NID_IS_VALID (nid));
    cp_net_t *net = NULL;
    for (net = cp_sta_mgr_get_first_avln (ctx);
         net;
         net = cp_sta_mgr_get_next_avln (ctx, net))
    {
        if (net->nid == nid && net->snid == snid)
         return net;
    }
    return NULL;
}

cp_net_t *
cp_sta_mgr_get_first_avln (cp_t *ctx)
{
    dbg_assert (ctx);
    uint i;
    cp_net_t *net = NULL;
    for (i = 0; i < HPAV_AVLNS_NB_MAX && !net; i++)
    {
        if (ctx->sta_mgr.networks[i].present)
            net = &ctx->sta_mgr.networks[i];
    }
    return net;
}

cp_net_t *
cp_sta_mgr_get_next_avln (cp_t *ctx, cp_net_t *prev)
{
    dbg_assert (ctx);
    dbg_assert (prev);
    dbg_assert (prev <= &ctx->sta_mgr.networks[HPAV_AVLNS_NB_MAX-1]);
    cp_net_t *net = NULL;
    /* Two possibilities, prev is already the last AVLN. */
    if (prev == &ctx->sta_mgr.networks[HPAV_AVLNS_NB_MAX-1])
        return NULL;
    else
        for (net = prev + 1;
             !net->present
             && net != &ctx->sta_mgr.networks[HPAV_AVLNS_NB_MAX-1];
             net++)
            ;
    /* If net is present return it. */
    if (net->present)
        return net;
    else
        return NULL;
}

u16
cp_sta_mgr_get_present_snids (cp_t *ctx)
{
    dbg_assert (ctx);
    uint snidflags = 0;
    cp_net_t *net;
    for (net = cp_sta_mgr_get_first_avln (ctx);
         net;
         net = cp_sta_mgr_get_next_avln (ctx, net))
        snidflags |= (u16) (1 << (net->snid));
    return snidflags;
}

void
cp_sta_mgr_garbage (cp_t *ctx)
{
    cp_sta_t *sta;
    cp_net_t *net;
    dbg_assert (ctx);
    u32 now = cp_sta_core_get_date_ms (ctx);
    for (net = cp_sta_mgr_get_first_avln (ctx);
         net;
         net = cp_sta_mgr_get_next_avln (ctx, net))
    {
        if (net != ctx->sta_mgr.our_avln)
            cp_net_garbage_stations (ctx, net, now);
        if (net->blacklisted
            && less_mod2p32 ( net->blacklisted_reset_date, phy_date ()))
            net->blacklisted = false;
    }
    /* Release the release stations if possible. */
    while ((sta = cp_sta_mgr_release_sta_get_first (ctx))
           && (less_mod2p32 (sta->tei_lease_date_ms, now)))
    {
        /* remove the station from the list. */
        list_remove (&ctx->sta_mgr.release_sta_list,
                     &sta->release_node);

        sar_sta_remove (ctx->sar, cp_sta_get_tei (sta));

        if (cp_sta_own_data_get_cco_status (ctx))
        {
            /* Request the CCo to release the TEI. */
            cp_cco_action_tei_release (ctx, cp_sta_get_tei (sta));
        }
        slab_release (sta);
    }
    cp_sta_mgr_elects_sta_partial_ack (ctx);
    if (cp_sta_mgr_net_list_is_empty (ctx))
        cp_fsm_trigger_new_event (ctx, bare, net_list_empty);
}

cp_sta_own_data_t *
cp_sta_mgr_get_sta_own_data (cp_t *ctx)
{
    dbg_assert (ctx);

    return (cp_sta_own_data_t *) &ctx->sta_mgr.sta_own_data;
}

u8
cp_sta_mgr_get_slot_usage (cp_t *ctx)
{
    dbg_assert (ctx);
    uint slot_usage = 0;
    cp_net_t *net;
    for (net = cp_sta_mgr_get_first_avln (ctx);
         net;
         net = cp_sta_mgr_get_next_avln (ctx, net))
        slot_usage |= 1 << (net->avln_slot_id);
    return slot_usage;
}

u8
cp_sta_mgr_get_num_discovered_stas (cp_t *ctx)
{
    dbg_assert (ctx);
    uint num = 0;
    cp_net_t *net;
    for (net = cp_sta_mgr_get_first_avln (ctx);
         net;
         net = cp_sta_mgr_get_next_avln (ctx, net))
        num += cp_net_get_num_discovered_stas (ctx, net);
    return num;
}

u8
cp_sta_mgr_get_num_discovered_net (cp_t *ctx)
{
    dbg_assert (ctx);
    uint num = 0;
    cp_net_t *net;
    for (net = cp_sta_mgr_get_first_avln (ctx);
         net;
         net = cp_sta_mgr_get_next_avln (ctx, net))
    {
        if (ctx->sta_mgr.our_avln != net)
            num ++;
    }
    return num;
}

bool
cp_sta_mgr_net_list_is_empty (cp_t *ctx)
{
    dbg_assert (ctx);
    cp_net_t *net;
    for (net = cp_sta_mgr_get_first_avln (ctx);
         net;
         net = cp_sta_mgr_get_next_avln (ctx, net))
    {
        if (net != ctx->sta_mgr.our_avln)
            return false;
    }
    /* If we are here, all the AVLNs are empty, test our one. */
    if (ctx->sta_mgr.our_avln)
        return (set_empty (&ctx->sta_mgr.our_avln->associated_stas)
                && set_empty (&ctx->sta_mgr.our_avln->unassociated_stas));
    else
        return true;
}

cp_net_t *
cp_sta_mgr_get_our_avln (cp_t *ctx)
{
    dbg_assert (ctx);
    dbg_assert (ctx->sta_mgr.our_avln);
    dbg_assert (ctx->sta_mgr.our_avln->present);
    return ctx->sta_mgr.our_avln;
}

void
cp_sta_mgr_set_our_avln (cp_t *ctx, cp_net_t *net)
{
    cp_sta_t *sta;
    dbg_assert (ctx);
    dbg_assert (ctx->cl);
    dbg_assert (ctx->sar);

    /* If our AVLN was already setted, remove the station from the store. */
    if (ctx->sta_mgr.our_avln)
    {
        cp_sta_t *sta;
        /* Release the CL's mac to tei table. */
        cl_mactotei_release_table (ctx->cl);

        /* Release the station from the mac store. */
        sta = cp_net_sta_get_first (ctx, ctx->sta_mgr.our_avln, CP_NET_STA_ASSOC);
        while (sta)
        {
            sar_sta_remove (ctx->sar, cp_sta_get_tei (sta));
            sta = cp_net_sta_get_next (ctx, net, sta);
        }

        /* Release the station in the release state. */
        if (cp_sta_own_data_get_cco_status (ctx))
        {
            while ((sta = cp_sta_mgr_release_sta_get_first (ctx)))
            {
                sar_sta_remove (ctx->sar, cp_sta_get_tei (sta));
                /* Remove the station from the list. */
                list_remove (&ctx->sta_mgr.release_sta_list,
                             &sta->release_node);
                slab_release (sta);
            }
        }

        sar_cleanup (ctx->sar);
    }

    ctx->sta_mgr.our_avln = net;

    if (net)
    {
        // Store the nid and the snid to the station own data.
        cp_sta_own_data_set_nid (ctx, cp_net_get_nid (ctx, net));
        cp_sta_own_data_set_snid (ctx, cp_net_get_snid (ctx, net));

        /* Commit the new list. */
        cp_sta_mgr_commit_to_dataplane (ctx);

        /* Add the station to the store. */
        for (sta = cp_net_sta_get_first (ctx, net, CP_NET_STA_ASSOC);
             sta;
             sta = cp_net_sta_get_next (ctx, net, sta))
        {
            mac_store_sta_add (ctx->mac_store, cp_sta_get_tei (sta));
        }
    }
}

void
cp_sta_mgr_release_station (cp_t *ctx, cp_tei_t tei)
{
    cp_sta_t *sta;
    cp_net_t *net;

    dbg_assert (ctx);
    dbg_assert (tei);
    dbg_assert (cp_sta_own_data_get_cco_status (ctx));

    net = cp_sta_mgr_get_our_avln (ctx);
    sta = cp_sta_mgr_sta_get_assoc (ctx, net, tei);
    dbg_assert (sta);

    /* Remove the station from the set of the net. */
    set_remove (&net->associated_stas, &sta->node_net);
    /* Remove the station from the sta mgr set. */
    set_remove (&ctx->sta_mgr.stas, &sta->node_sta_mgr);

    list_init_node (&sta->release_node);
    list_push (&ctx->sta_mgr.release_sta_list, &sta->release_node);

    /* Set the station tei_lease_ms to 5 min as indicated in the HPAV
     * specification. */
    sta->tei_lease_date_ms = CP_STA_MGR_STATION_RELEASE_MS +
        cp_sta_core_get_date_ms (ctx);

    /* Remove the reference provided. */
    slab_release (sta);
    /* Check if our net is empty, if it is post an FSM event all_sta_leaved */
    if (cp_sta_own_data_get_tei (ctx)
        && net == cp_sta_mgr_get_our_avln (ctx)
        && cp_net_is_empty (ctx, net))
            cp_fsm_trigger_new_event (ctx, bare, all_sta_leaved);
}

cp_sta_t *
cp_sta_mgr_sta_add (cp_t *ctx, cp_net_t *net, cp_tei_t tei, mac_t mac_address)
{
    cp_sta_private_t *sta = NULL;

    dbg_assert (ctx);
    dbg_assert (net);
    dbg_assert ((tei && mac_address == MAC_BROADCAST)
                || (mac_address & ~MAC_BROADCAST) == 0);

    /* Try to get the station by the TEI. */
    if (tei)
        sta = (cp_sta_private_t *) cp_sta_mgr_sta_get_assoc (ctx, net, tei);

    /* Try to get the station by the mac address. */
    if (!sta && MAC_IS_VALID(mac_address))
        sta = (cp_sta_private_t *) cp_sta_mgr_sta_get_from_mac (ctx,
                                                                mac_address);

    if (!sta)
    {
        sta = (cp_sta_private_t *) cp_sta_mgr_sta_add_realy (ctx, net,
                                                             tei, mac_address);
    }
    else
    {
        /* Last case. The network change. */
        if (sta->net != net)
        {
            cp_sta_mgr_sta_remove (ctx, (cp_sta_t *) sta);
            slab_release (sta);

            sta = (cp_sta_private_t *) cp_sta_mgr_sta_add_realy (ctx, net,
                                                                 tei,
                                                                 mac_address);
        }
        /* First case only the TEI change from the previous state. */
        else if ((sta->tei == MAC_TEI_UNASSOCIATED)
            && (tei != MAC_TEI_UNASSOCIATED)
            && (sta->mac_address == mac_address))
        {
            /* Remove the station. */
            cp_sta_mgr_sta_remove (ctx, (cp_sta_t *) sta);
            slab_release (sta);

            /* Add the station with the new TEI. */
            sta = (cp_sta_private_t *) cp_sta_mgr_sta_add_realy (ctx, net,
                                                                 tei,
                                                                 mac_address);
            if (net == ctx->sta_mgr.our_avln)
                mac_store_sta_add (ctx->mac_store, tei);
        }
        /* Second case,
         * - station's TEI != TEI and != MAC_TEI_UNASSOCIATED.
         * - station's Mac address was broadcast. */
        else if ((sta->tei == tei)
                 && (tei != MAC_TEI_UNASSOCIATED)
                 && (sta->mac_address != mac_address)
                 && (sta->mac_address == MAC_BROADCAST))
        {
            /* Add the station mgr node to the set. */
            sta->mac_address = mac_address;
            set_insert (&ctx->sta_mgr.stas,
                        &sta->public_data.node_sta_mgr);
        }
        /* Third case, only the station's TEI change. */
        else if ((sta->tei != tei)
                 && (tei != MAC_TEI_UNASSOCIATED)
                 && (sta->mac_address == mac_address)
                 && (mac_address != MAC_BROADCAST))
        {
            /* Only change the station's TEI. */
            sta->tei = tei;
        }
        /* Fourth case, only the station's TEI change from a associated tei to
         * the unassociated TEI.. */
        else if ((sta->tei != tei)
                 && (tei == MAC_TEI_UNASSOCIATED)
                 && (sta->mac_address == mac_address)
                 && (mac_address != MAC_BROADCAST))
        {
            cp_sta_mgr_sta_remove (ctx, (cp_sta_t *) sta);
            slab_release (sta);

            sta = (cp_sta_private_t *) cp_sta_mgr_sta_add_realy (ctx, net,
                                                                 tei,
                                                                 mac_address);
        }
        /* Fifth case, Same TEI only the mac address change from A to B
         * without being null or broadcast. */
        else if ((sta->mac_address != mac_address)
                 && (mac_address != MAC_BROADCAST)
                 && (mac_address != 0))
        {
            cp_sta_mgr_sta_remove (ctx, (cp_sta_t *) sta);
            slab_release (sta);

            sta = (cp_sta_private_t *) cp_sta_mgr_sta_add_realy (ctx, net,
                                                                 tei,
                                                                 mac_address);
        }

    }

    /* Update the last seen value. */
    sta->public_data.last_seen_ms = cp_sta_core_get_date_ms (ctx);
    return (cp_sta_t *) sta;
}

void
cp_sta_mgr_sta_remove (cp_t *ctx, cp_sta_t * station)
{
    set_t *set;
    cp_sta_private_t *sta = (cp_sta_private_t *) station;
    cp_net_t *net;

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

    net = sta->net;

    // If the station is CCo, modify the net CCo pointer to NULL.
    if (cp_sta_get_cco_status(station))
        net->cco = NULL;

    // If the station is PCo to communicate with the CCo, modify the
    // pointer in the net.
    if (net->pco == station)
        net->pco = NULL;

    if ((net == ctx->sta_mgr.our_avln)
        && (MAC_TEI_IS_STA (sta->tei)))
        sar_sta_remove (ctx->sar, sta->tei);

    set = (cp_sta_get_tei (&sta->public_data)) ?
        &sta->net->associated_stas : &sta->net->unassociated_stas;
    /* Remove the station from its net. */
    set_remove (set, &sta->public_data.node_net);

    /* Remove the station from the station manager. If the mac address of the
     * station is broadcast, the station is not in the set. */
    if (sta->mac_address != MAC_BROADCAST)
        set_remove (&ctx->sta_mgr.stas, &sta->public_data.node_sta_mgr);

    /* decrease the visible counter. */
    if (sta->visible)
        sta->net->num_visible_stas --;

    /* decrease the number of stations. */
    sta->net->num_stas --;

    if (MAC_TEI_IS_STA (cp_sta_get_tei (&sta->public_data)))
        sta->net->num_associated_stas--;
    if (net == ctx->sta_mgr.our_avln &&  sta->net->num_associated_stas == 0)
        cp_fsm_post_new_event (ctx, bare, sta_status_changed);

    cp_sta_mgr_elects_sta_partial_ack (ctx);

    /* Release the station. */
    slab_release (sta);
}

void
cp_sta_mgr_sta_remove_from_mac (cp_t *ctx, mac_t mac_addr)
{
    cp_sta_t *sta;
    dbg_assert (ctx);
    dbg_assert (mac_addr);

    sta = cp_sta_mgr_sta_get_from_mac (ctx, mac_addr);
    /* Remove the reference provided by the station manager. */
    slab_release (sta);

    cp_sta_mgr_sta_remove (ctx, sta);
}

void
cp_sta_mgr_sta_remove_assoc (cp_t *ctx, cp_net_t *net, cp_tei_t tei)
{
    cp_sta_t *sta;

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

    sta = cp_sta_mgr_sta_get_assoc (ctx, net, tei);

    if (sta)
    {
        // The get STA returns another reference on the station which we
        // want to release this reference is not useful, this release
        // allows to remove the reference just added by the get STA.
        slab_release (sta);

        /* Release the station. */
        cp_sta_mgr_sta_remove (ctx, sta);
    }
}


cp_sta_t *
cp_sta_mgr_sta_get_assoc (cp_t *ctx, cp_net_t *net, cp_tei_t tei)
{
    cp_sta_t *sta = NULL;
    cp_sta_private_t sta_cmp;
    set_node_t *node;

    dbg_assert (ctx);
    dbg_assert (net);
    dbg_assert (tei);

    if (! set_empty (&net->associated_stas))
    {
        memset (&sta_cmp, 0, sizeof (cp_sta_private_t));
        sta_cmp.tei = tei;

        node = set_find (&net->associated_stas,
                         &sta_cmp.public_data.node_net);

        if (node)
        {
            sta = PARENT_OF (cp_sta_t, node_net, node);
            slab_addref (sta);
        }
    }

    return sta;
}

cp_sta_t *
cp_sta_mgr_sta_get_from_mac (cp_t *ctx, mac_t mac_addr)
{
    cp_sta_private_t sta;
    set_node_t *node;
    cp_sta_t *sta_found = NULL;

    dbg_assert (ctx);
    dbg_assert (mac_addr);

    if (!set_empty (&ctx->sta_mgr.stas))
    {
        sta.mac_address = mac_addr;
        set_node_init (&sta.public_data.node_sta_mgr);
        node = set_find (&ctx->sta_mgr.stas, &sta.public_data.node_sta_mgr);

        if (node)
        {
            sta_found = PARENT_OF (cp_sta_t, node_sta_mgr, node);
            slab_addref (sta_found);
        }
    }

    return sta_found;
}

void
cp_sta_mgr_commit_to_dataplane (cp_t *ctx)
{
    cp_sta_t *sta;
    cl_mactotei_blk_t *mactotei;

    dbg_assert (ctx);
    dbg_assert (ctx->cl);

    // If the net does not correspond to the AVLN, do not commit.
    if (!ctx->sta_mgr.our_avln)
        return;

    // Get the first station from the set.
    if (set_empty (&ctx->sta_mgr.our_avln->associated_stas))
    {
        cl_mactotei_release_table (ctx->cl);
    }
    else
    {
        // There is at least a station in the network list.
        mactotei = cl_mactotei_new ();

        for (sta = cp_net_sta_get_first (ctx, ctx->sta_mgr.our_avln,
                                     CP_NET_STA_ASSOC);
             sta; sta = cp_net_sta_get_next (ctx, ctx->sta_mgr.our_avln, sta))
        {
            cp_sta_private_t *sta_private = (cp_sta_private_t *) sta;
            // Copy all the bridge table from the previous list.
            cl_mactotei_copy_tei_and_tag (ctx->cl, mactotei, sta_private->tei,
                                          sta_private->tei);
            // Add the current station.
            cl_mactotei_addr_add (mactotei, sta_private->mac_address,
                                  sta_private->tei, MAC_TEI_UNASSOCIATED);

        }

        // Request the CL to use the table.
        cl_mactotei_use_table (ctx->cl, mactotei);
    }
}

void
cp_sta_mgr__assoc__timeout (cp_t *ctx, uint time_ms)
{
    cp_sta_t *sta;
    cp_net_t *net;
    dbg_assert (ctx);
    dbg_assert (cp_sta_own_data_get_cco_status (ctx));

    net = ctx->sta_mgr.our_avln;

    if (net)
    {
        /* Parse only the associated stations. */
        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_assoc_confirmed (ctx, sta)
                && lesseq_mod2p32 (sta->assoc_req_last_ms + CP_ASSOC_FULL_TIMEOUT_MS, time_ms))
                cp_sta_set_assoc_confirmed (ctx, sta, true);
        }
     }
}