summaryrefslogtreecommitdiff
path: root/cesar/cp2/sta/mgr/src/net.c
blob: 97b5e453a5d421b6e1153a0cdbcb8149024dae32 (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp2/sta/mgr/src/net.c
 * \brief   Network APIs.
 * \ingroup cp2_sta_mgr
 *
 */
#include "common/std.h"
#include "string.h"

#include "lib/blk.h"

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

#include "cp2/defs.h"
#include "cp2/fsm/fsm.h"
#include "cp2/sta/mgr/net.h"
#include "cp2/sta/mgr/sta.h"
#include "cp2/sta/mgr/sta_own_data.h"
#include "cp2/sta/mgr/sta_mgr.h"

#include "cp2/inc/context.h"
#include "cp2/sta/mgr/inc/net.h"
#include "cp2/sta/mgr/inc/sta.h"

/**
 * Station Associated lesser.
 *
 * \param  left  left node station.
 * \param  right  right node station.
 * \return  true if left is lesser than right
 */
bool
cp_net_station_assoc_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, left);
    sta_right = (cp_sta_private_t *) PARENT_OF (cp_sta_t,
                                               node, right);

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

/**
 * Station Unassociated lesser.
 *
 * \param  left  left node station.
 * \param  right  right node station.
 * \return  true if left is lesser than right
 */
bool
cp_net_station_unassoc_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, left);
    sta_right = (cp_sta_private_t *) PARENT_OF (cp_sta_t,
                                               node, right);

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


/**
 * Initialise the net.
 * \param  ctx  the CP context.
 * \param  net  the net to initialise.
 * \param  station_avln  Indicate if it corresponds to the station's avln.
 */
void
cp_net_init (cp_t *ctx, cp_net_t *net, bool station_avln)
{
    dbg_assert (net);

    memset (net, 0, sizeof (cp_net_t));

    net->station_avln = station_avln;
    net->present = station_avln;
    net->expiration_ms = MAC_SEC_TO_MS(CP_NET_EXPIRATION_DELAY_S);
    net->network_mode = CP_NET_NM_CSMA_ONLY;

    set_init (&net->associated_stas, cp_net_station_assoc_less);
    set_init (&net->unassociated_stas, cp_net_station_unassoc_less);

    slab_cache_init (&net->sta_slab_cache,
                     "Station cache",
                     sizeof (cp_sta_private_t),
                     (slab_object_destructor_t) cp_sta_uninit);
}

/**
 * Uninitialise the net.
 * \param  ctx  the module context.
 * \param  net  the net to uninitialise.
 */
void
cp_net_uninit (cp_t *ctx, cp_net_t *net)
{
    cp_sta_t *sta;

    dbg_assert (net);

    while (!set_empty (&net->unassociated_stas))
    {
        sta = PARENT_OF (cp_sta_t, node,
                         set_begin (&net->unassociated_stas));

        set_remove (&net->unassociated_stas, &sta->node);
        slab_release (sta);
    }

    while (!set_empty (&net->associated_stas))
    {
        sta = PARENT_OF (cp_sta_t, node,
                         set_begin (&net->associated_stas));

        set_remove (&net->associated_stas, &sta->node);
        slab_release (sta);
    }

    // Uninitialise the slabs cache.
    slab_cache_uninit (&net->sta_slab_cache);
    cp_net_init (ctx, net, net->station_avln);
}

/**
 * Garbage function for the unassociated station list and the associated
 * station list.
 *
 * \param  ctx  the module context.
 * \param  net  the network.
 * \param  date_ms  the current date in milliseconds.
 * \param  status  the station list to expire.
 */
void
cp_net_garbage_station_list (cp_t *ctx, cp_net_t *net, u32 date_ms,
                             enum cp_sta_state_t status)
{
    cp_sta_t *sta_next;
    cp_sta_t *sta;

    dbg_assert (ctx);
    dbg_assert (net);

    if (status >= CP_STA_STATE_ASSERT)
        return;

    sta = cp_net_get_first (ctx, net, status);

    while (sta)
    {
        sta_next = cp_net_get_next_sta (ctx, net, sta);

        if (less_mod2p32(sta->last_seen_ms
                         + MAC_SEC_TO_MS(CP_STA_EXPIRATION_DELAY_S),
                         date_ms))
        {
            cp_net_sta_remove (ctx, net, sta);
        }

        slab_release (sta);
        sta = sta_next;
    }

    if (cp_net_is_empty (ctx, net) && net->station_avln)
    {
        cp_fsm_event_t *event;
        event = cp_fsm_event_bare_new (ctx, CP_FSM_EVENT_TYPE_ALL_STA_LEAVED);
        cp_fsm_post (ctx, event);
    }
}

/**
 * Remove old station which has not been seen for a while.
 * \param  ctx  the module context.
 * \param  net  the network to garbage.
 * \param  date_ms  the current date in milliseconds.
 *
 * Remove old station which has not been seen for a while.
 */
void
cp_net_garbage_stations (cp_t *ctx, cp_net_t *net, u32 date_ms)
{
    dbg_assert (ctx);
    dbg_assert (net);

    cp_net_garbage_station_list (ctx, net, date_ms, CP_STA_STATE_EXISTS);
    cp_net_garbage_station_list (ctx, net, date_ms, CP_STA_STATE_ASSOCIATED);

    // Verify if the list not empty.
    if (set_empty (&net->associated_stas)
        && set_empty (&net->unassociated_stas))
    {
        //TODO Post the event in the FSM.
        net->present = false;
    }
}

/**
 * Set the CCo or the PCo in the AVLN.
 * \param  ctx  the module context.
 * \param  cp   the control plane context (set only if cco is true).
 * \param  tei  the station's TEI.
 * \param  cco  inform if it is the CCo (true) or the PCo (false).
 */
void
cp_net_set_xco (cp_t *ctx, cp_net_t *net, cp_tei_t tei, bool cco)
{
    cp_sta_t *sta = NULL;
    bool not_my_sta = true;

    dbg_assert (net);
    dbg_assert (tei);

    if (net->station_avln && cco)
    {
        dbg_assert (ctx);
        cp_sta_own_data_t *my_sta;

	my_sta = cp_sta_mgr_get_sta_own_data (ctx);
        if (cp_sta_own_data_get_tei (ctx) == tei)
        {
            my_sta = cp_sta_mgr_get_sta_own_data (ctx);
            my_sta->is_cco = true;
            not_my_sta = false;
        }
        else
        {
            my_sta->is_cco = false;
        }
    }

    if (not_my_sta)
        sta = cp_net_get_sta (ctx, net, tei);

    if (cco)
    {
        net->cco = sta;
        if (not_my_sta)
            ((cp_sta_private_t*)sta)->is_cco = true;
    }
    else
    {
        net->pco = sta;
        if (not_my_sta)
            ((cp_sta_private_t*)sta)->pco_glid = (0x80);
    }

    if (sta)
        slab_release (sta);
}

/**
 * Get the CCo of this AVLN.
 * \param  ctx  the module context.
 * \param  net  the network.
 * \return  A reference on the CCo station or NULL if this station is CCo.
 * The user is responsible to release the reference.
 */
cp_sta_t *
cp_net_get_cco (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (net);

    if (net->cco)
    {
        slab_addref (net->cco);
        return net->cco;
    }
    else
        return NULL;
}

/**
 * Get the PCo of the AVLN.
 * \param  ctx  the module context.
 * \param  net  the network.
 * \return  A reference on the PCo station.
 *
 * The user is responsible to release the reference.
 */
cp_sta_t *
cp_net_get_pco (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (net);

    if (net->pco)
    {
        slab_addref (net->pco);
        return net->pco;
    }
    else
        return NULL;
}

/**
 * Add a station to the AVLN.
 * \param  ctx  the module context.
 * \param  net  the network.
 * \param  tei  Station's TEI.
 * \param  mac_address  Station's mac address.
 * \return  A reference on the station just created.
 *
 * This function shall update the data of the AVLN if it has changed
 * i.e. cco, pco.
 */
cp_sta_t *
cp_net_sta_add (cp_t *ctx, cp_net_t *net, cp_tei_t tei, mac_t mac_address)
{
    cp_sta_private_t *sta = NULL;
    bool already_exists;

    dbg_assert (ctx);
    dbg_assert (net);
    dbg_assert (tei || mac_address);

    // Try to get the station.
    if (tei)
        sta = (cp_sta_private_t *) cp_net_get_sta (ctx, net, tei);
    else
        /* Try to get the sta from the mac address. */
        sta = (cp_sta_private_t *) cp_net_get_sta_unassoc (ctx, net,
                                                           mac_address);

    if (!sta)
    {
        sta = (cp_sta_private_t *) cp_sta_init ();
        already_exists = false;
    }
    else
    {
        slab_release (sta);
        already_exists = true;
    }

    if (tei)
        sta->station_state = CP_STA_STATE_ASSOCIATED;
    sta->tei = tei;

    if (mac_address != 0)
        sta->mac_address = mac_address;

    if (!already_exists)
    {
        net->num_visible_stas ++;
        net->num_stas ++;
        sta->visible = true;

        set_node_init (&((cp_sta_t *)sta)->node);

        // Add the station to the non unassociated list.
        if (tei == 0)
        {
            bool state;
            state = set_insert (&net->unassociated_stas,
                                &((cp_sta_t *)sta)->node);
            dbg_check (state);
        }
        // Add the station to the authenticated list.
        else
        {
            bool state;
            state = set_insert (&net->associated_stas,
                                &((cp_sta_t *)sta)->node);
            dbg_check (state);
        }
    }

    // Add a reference on the station.
    slab_addref (sta);
    return (cp_sta_t *) sta;
}

/**
 * Remove the station from the AVLN.
 * \param  ctx  the module context.
 * \param  net  the network context.
 * \param  tei  The Station's TEI.
 *
 * Remove the station from the AVLN.
 */
void
cp_net_sta_remove_assoc (cp_t *ctx, cp_net_t *net, cp_tei_t tei)
{
    cp_sta_private_t *sta;

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

    sta = (cp_sta_private_t *) cp_net_get_sta (ctx, net, tei);

    // Verify the node and delete the node from the set.
    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);

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

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

        // If the station is PCo to communicate with the CCo, modify the
        // pointer in the net.
        if ((sta->pco_glid >= 0x80) && (net->pco == (cp_sta_t *) sta))
            net->pco = NULL;

        set_remove (&net->associated_stas, &((cp_sta_t *)sta)->node);

        net->num_stas --;
        //release the station.
        slab_release (sta);
    }

    if (cp_net_is_empty (ctx, net))
    {
        cp_fsm_event_t *event;
        event = cp_fsm_event_bare_new (ctx, CP_FSM_EVENT_TYPE_ALL_STA_LEAVED);
        cp_fsm_post (ctx, event);

        if (cp_sta_mgr_net_list_is_empty (ctx))
        {
            cp_fsm_event_t *event;
            event = cp_fsm_event_bare_new (ctx,
                                           CP_FSM_EVENT_TYPE_NET_LIST_EMPTY);
            cp_fsm_post (ctx, event);
        }
    }
}

/**
 * Remove an unassociated station from the list.
 * \param  ctx  the module context.
 * \param  net  the network context.
 * \param  mac_addr  The station's mac address.
 *
 */
void
cp_net_sta_remove_unassoc (cp_t *ctx, cp_net_t *net, mac_t mac_addr)
{
    cp_sta_t *sta;

    dbg_assert (ctx);
    dbg_assert (net);
    dbg_assert (mac_addr);

    sta = cp_net_get_sta_unassoc (ctx, net, mac_addr);

    // Verify the node and delete the node from the set.
    if (sta)
    {
        // decrease the visible counter
        if (((cp_sta_private_t *)sta)->visible)
            net->num_visible_stas --;

        // 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);

        set_remove (&net->unassociated_stas, &sta->node);

        net->num_stas --;
        //release the station.
        slab_release (sta);
    }

    // If the net is empty post an event to the FSM.
    // Check the associated list.
    if (cp_net_is_empty (ctx, net))
    {
        cp_fsm_event_t *event;
        event = cp_fsm_event_bare_new (ctx, CP_FSM_EVENT_TYPE_ALL_STA_LEAVED);
        cp_fsm_post (ctx, event);
        net->present = false;
    }
}

/**
 * Remove the station from the net.
 * \param  ctx  the module context.
 * \param  net  the network context.
 * \param  sta  The station to remove.
 *
 *  The user shall release the reference
 */
void
cp_net_sta_remove (cp_t *ctx, cp_net_t *net, cp_sta_t * sta)
{
    dbg_assert (ctx);
    dbg_assert (net);
    dbg_assert (sta);

    if(cp_sta_get_state(sta) != CP_STA_STATE_EXISTS)
        cp_net_sta_remove_assoc (ctx, net, ((cp_sta_private_t *)sta)->tei);
    else
        cp_net_sta_remove_unassoc (ctx, net, ((cp_sta_private_t
                                               *)sta)->mac_address);
}

/**
 * Get the associated station identified by its Mac address.
 * \param  ctx  the module context.
 * \param  net  the network context.
 * \param  mac_address The station's mac_address.
 * \return  A reference on the station.
 *
 * returns NULL if the station does not exists. It only returns a new
 * reference on the station (one reference is still kept by the network),
 * when the user does not use it any more, it must release it.
 */
cp_sta_t *
cp_net_get_sta_from_mac (cp_t *ctx, cp_net_t *net, mac_t mac_addr)
{
    cp_sta_t *sta;
    cp_sta_t *sta_next;
    dbg_assert (ctx);
    dbg_assert (net);
    dbg_assert (mac_addr);

    sta = cp_net_get_first (ctx, net, CP_STA_STATE_ASSOCIATED);
    while (sta && cp_sta_get_mac_address (sta) != mac_addr)
    {
        sta_next = cp_net_get_next_sta (ctx, net, sta);
        slab_release (sta);
        sta = sta_next;
    }

    return sta;
}

/**
 * Get the unassociated station identified by its Mac address.
 * \param  ctx  the module context.
 * \param  net  the network context.
 * \param  mac_address The station's mac_address.
 * \return  A reference on the station.
 *
 * returns NULL if the station does not exists. It only returns a new
 * reference on the station (one reference is still kept by the network),
 * when the user does not use it any more, it must release it.
 */
cp_sta_t *
cp_net_get_sta_unassoc (cp_t *ctx, cp_net_t *net, mac_t mac_addr)
{
    cp_sta_t *sta;
    cp_sta_private_t sta_to_find;
    set_node_t *sta_node;

    dbg_assert (ctx);
    dbg_assert (net);
    dbg_assert (mac_addr);

    // if list is empty do nothing.
    if (set_empty (&net->unassociated_stas))
        return NULL;

    sta_to_find.mac_address = mac_addr;
    set_node_init (&sta_to_find.public_data.node);

    sta_node = set_find (&net->unassociated_stas,
                         &sta_to_find.public_data.node);

    if (!sta_node)
        return NULL;

    sta = PARENT_OF (cp_sta_t, node, sta_node);
    slab_addref (sta);
    return (cp_sta_t *) sta;
}


/**
 * Get the station identified by its TEI.
 * \param  ctx  the module context.
 * \param  net  the network context.
 * \param  tei  The station's tei.
 * \return  A reference on the station.
 *
 * returns NULL if the station does not exists. It only returns a new
 * reference on the station (one reference is still kept by the network),
 * when the user does not use it any more, it must release it.
 */
cp_sta_t *
cp_net_get_sta (cp_t *ctx, cp_net_t *net, cp_tei_t tei)
{
    cp_sta_t *sta;
    cp_sta_private_t sta_to_find;
    set_node_t *sta_node;

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

    // if list is empty do nothing.
    if (set_empty (&net->associated_stas))
        return NULL;

    sta_to_find.tei = tei;
    set_node_init (&sta_to_find.public_data.node);

    sta_node = set_find (&net->associated_stas,
                         &sta_to_find.public_data.node);

    if (!sta_node)
        return NULL;

    sta = PARENT_OF (cp_sta_t, node, sta_node);
    slab_addref (sta);
    return sta;
}

/**
 * Commit the changes of the AVLN.
 * \param  ctx  the module context.
 * \param  cp  the CP context.
 *
 * Use to commit the changes in the others databases. Only call this
 * function when all the stations have been added, or all stations have
 * been removed. This will check the modifications of the network station
 * list to update at least the mactotei table. It is useful to synchronize
 * those tables because the mactotei table does not have the possibility
 * to remove stations it only have the possibility to remove the station
 * by excluding it from the copy to the new one.
 */
void
cp_net_commit_to_dataplane (cp_t *ctx, cp_net_t *net)
{
    cp_sta_private_t *sta;
    cp_sta_private_t *sta_prev;
    cl_mactotei_blk_t *mactotei;

    dbg_assert (ctx);
    dbg_assert (net);

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

    // Get the first station from the set.
    if (set_empty (&net->associated_stas))
        return;

    // There is at least a station in the network list.
    mactotei = cl_mactotei_new ();

    for (sta = (cp_sta_private_t *)
            cp_net_get_first (ctx, net, CP_STA_STATE_ASSOCIATED);
            sta;
         sta_prev = sta,
         sta = (cp_sta_private_t *) cp_net_get_next_sta (ctx, net,
                                                (cp_sta_t *) sta_prev),
         blk_release(sta_prev))
    {
        // Copy all the bridge table from the previous list.
        cl_mactotei_copy_tei_and_tag (ctx->cl, mactotei, sta->tei,
                                      CP_MACTOTEI_TAGS_BRIDGE);
        // Add the current station.
        cl_mactotei_addr_add (mactotei, sta->mac_address, sta->tei,
                              CP_MACTOTEI_TAGS_LIST);

    }

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

/**
 * Returns the first station of an AVLN.
 * \param  ctx  the module context.
 * \param  net  the network context.
 * \param  status to search in the associated stations or unassociated STAs.
 * \return  the Station with the minimal TEI for the associated stations, the
 * one with the minimal mac address for the unassociated.
 *
 * Returns the first station of an AVLN.
 * The user has the responsibility to release the reference.
 */
cp_sta_t *
cp_net_get_first (cp_t *ctx, cp_net_t *net, enum cp_sta_state_t status)
{
    set_t *set;
    cp_sta_t *sta;

    dbg_assert (net);
    dbg_assert (ctx);

    if (status == CP_STA_STATE_EXISTS)
        set = &net->unassociated_stas;
    else
        set = &net->associated_stas;

    // Verify if the set contains a sta.
    if (set_empty (set))
        return NULL;

    sta = PARENT_OF (cp_sta_t, node, set_begin (set));
    slab_addref (sta);

    return sta;
}

/**
 * Get the next station from the AVLN stating by the previous one already
 * request.
 * \param  ctx  the module context.
 * \param  net  the network context.
 * \param  prev_sta  the previous station requested.
 * \return  the next station.
 *
 * This function shall be call after get_first.
 * The user have the responsibility to release the reference on both stations
 * i.e. prev_sta and the returned sta.
 */
cp_sta_t *
cp_net_get_next_sta (cp_t *ctx, cp_net_t *net, cp_sta_t *prev_sta)
{
    set_t *set;
    set_node_t *node;
    cp_sta_t *sta;

    dbg_assert (ctx);
    dbg_assert (prev_sta);

    // Verify if the station is associated or not
    if (cp_sta_get_state(prev_sta) == CP_STA_STATE_EXISTS)
        set = &net->unassociated_stas;
    else
        set = &net->associated_stas;

    // Get the next node.
    node = set_next (set, &prev_sta->node);

    if (node)
    {
        sta = PARENT_OF (cp_sta_t, node, node);
        slab_addref (sta);
        return sta;
    }
    else
    {
        return NULL;
    }
}

/**
 * Get the network SNID.
 * \param  ctx  the module context.
 * \param  net  the network context.
 * \return  The network SNID.
 */
cp_snid_t
cp_net_get_snid (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (net);

    return net->snid;
}

/**
 * Get the AVLN NID.
 * \param  ctx  the module context.
 * \param  net  the network context.
 * \return  The network NID.
 */
cp_nid_t
cp_net_get_nid (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (net);

    return net->nid;
}

/**
 * Set the AVLN slot id and slot usage.
 * \param  ctx  the module context.
 * \param  net  the network context.
 * \param  slot_id  The AVLN Slot id.
 * \param  slot_usage  the AVLN slot usage mask
 */
void
cp_net_set_slot_id_and_usage (cp_t *ctx, cp_net_t *net,
                              u8 slot_id, u8 slot_usage)
{
    dbg_assert (net);

    net->avln_slot_id = slot_id;
    net->avln_slot_usage = slot_usage;
}


/**
 * Get the slot id of the network.
 * \param  ctx  the module context.
 * \param  net  the network context.
 * \return  the slot id of the central beacon.
 */
u8
cp_net_get_slot_id (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (net);

    return net->avln_slot_id;
}

/**
 * Return the slot usage mask of the AVLN read in the central beacon of
 * the AVLN.
 * \param  ctx  the module context.
 * \param  net  the network context.
 * \return  The slot usage mask read in the central beacon.
 *
 */
u8
cp_net_get_slot_usage_mask (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (net);

    return net->avln_slot_usage;
}

/**
 * Get the number of station discovered in the AVLN.
 * \param  net  the network context.
 * \return  the number of discovered stations.
 */
u8
cp_net_get_num_discovered_stas (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (net);

    return net->num_visible_stas;
}

/**
 * Get the number of station of the AVLN.
 * \param  ctx  the module context.
 * \param  net  the network context.
 * \return  Return the number of station in the AVLN.
 *
 */
u8
cp_net_get_num_stas (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (net);

    return net->num_stas;
}

/**
 * Set the current network mode.
 * \param  ctx  the module context.
 * \param  net  the network context.
 * \param  mode  the mode.
 */
void
cp_net_set_nm (cp_t *ctx, cp_net_t *net, cp_net_network_mode_t mode)
{
    dbg_assert (net);
    dbg_assert (mode < CP_NET_NM_ASSERT);

    net->network_mode = mode;
}

/**
 * Get the current network mode.
 * \param  ctx  the module context.
 * \param  net  the network context.
 * \return  mode  the mode.
 */
cp_net_network_mode_t
cp_net_get_nm (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (net);

    return net->network_mode;
}

/**
 * Set the visible status of the station.
 * \param  ctx  the module context.
 * \param  net  the network context.
 * \param  sta  Set the visible status of the station.
 * \param  status  the status.
 *
 */
void
cp_net_sta_set_visible_status (cp_t *ctx, cp_net_t *net, cp_sta_t * sta,
                               cp_sta_visible_state_t status)
{
    dbg_assert (net);
    dbg_assert (sta);
    dbg_assert (status < CP_STA_VISIBLE_STATE_ASSERT);

    if (((cp_sta_private_t *) sta)->visible ^ status)
    {
        ((cp_sta_private_t *) sta)->visible = status;

        if (status)
            net->num_visible_stas ++;
        else
            net->num_visible_stas --;
    }
}

/**
 * Get the status of the network.
 * \param  ctx  the context module.
 * \param  net  the network context.
 * \return  true if empty, false otherwise.
 *
 * This function only check the associated station list in the network.
 */
bool
cp_net_is_empty (cp_t *ctx, cp_net_t *net)
{
    dbg_assert (ctx);
    dbg_assert (net);

    // Check the associated list.
    if (set_empty (&net->associated_stas))
    {
        return true;
    }
    return false;
}