summaryrefslogtreecommitdiff
path: root/cesar/test_general/maximus/integration/sar-pbproc/src/station.c
blob: 9109d287e885604b37fa6955aff2b9d028fa7ad8 (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    test_general/integration/interface-dp/src/station.c
 * \brief   Station interface - DP.
 * \ingroup test_general
 *
 * Test the integration of the interface with the data plane.
 * The sniffer will be added to this test later.
 */
#include <cyg/kernel/kapi.h>
#include <cyg/hal/hal_arch.h>

#include "common/std.h"
#include "host/station/station.h"
#include <stdio.h>
#include <stdlib.h>

/** Library include. */
#include "lib/read_word.h"
#include "lib/trace.h"
#include "lib/circular_buffer.h"
#include "lib/list.h"
#include "lib/stats.h"

/** Data include. */
#include "mac/common/config.h"
#include "mac/common/ntb.h"
#include "mac/common/store.h"

/** Layers include. */
#include "mac/sar/sar.h"
#include "mac/pbproc/pbproc.h"
#include "mac/ca/ca.h"

#include "mac/sar/inc/trace.h"

struct tei_node_t
{
    list_node_t node;
    uint tei;
};
typedef struct tei_node_t tei_node_t;


/** station context. */
struct station_test_t
{
    /** The mac config. */
    mac_config_t mac_config;
    /** Short network identifier. */
    u8 snid;
    /** The mac store. */
    mac_store_t *mac_store;

    // Layers
    /** Pbproc context. */
    pbproc_t *pbproc;
    /** Sar context. */
    sar_t *sar;

    /* Schedule index. */
    uint sched_index;
    /* Pbproc activate. */
    bool pbproc_activate;

    cyg_handle_t sched_handle;
    cyg_thread sched_thread;

    /** Used to memorize the list of TEI inserted in the mac_Store to remove it
     * in the future.
     */
    list_t tei_list;
};
typedef struct station_test_t station_test_t;


u8 mme_buffer [2048] __attribute__((aligned (2048)));
u8 sched_stack [CYGNUM_HAL_STACK_SIZE_TYPICAL];

//----------------------------------------------------------------------------

static station_test_t station_test;

/****************** CALL BACKS DEFINITION ****************/

/**
 * Call back called by the SAR each time a packet is segmented.
 * In this test it will only display a message.
 * The buffer is a static buffer not a allocated one it does not need to be
 * freed.
 *
 * \param user, User data
 * \param  buffer the address of the buffer.
 */
void sar_segmentation_ul_done (void *user, u8* buffer, void *cl_data)
{
    printf ("******************************************\n");
    printf ("         sar Segmentation done\n   \n");
    printf ("******************************************\n");
}

/**
 * Implementation of the sar_reassembly_done_cb_t call back.
 * Only used in this test.
 * The buffer and the mfs shall be release.
 *
 * \param user, User data
 * \param  buffer the buffer address containing the data
 * \param  length  the data length
 * \param  mfs  the mfs used
 */
void sar_reassembly_ul_done (void *user, u8* buffer, uint length,
        mfs_rx_t *mfs, bool encrypted)
{
    dbg_assert (buffer);
    dbg_assert (length >= 60 && length <= 1518);
    dbg_assert (mfs);

    printf ("******************************************\n");
    printf ("          sar Reassembly done\n     \n");
    printf ("******************************************\n");

    free (buffer);

    blk_print_memory ();
}

/**
 * Implementation of the sar_reassembly_done_cb_t call back.
 * Only used in this test.
 * The buffer and the mfs shall be release.
 *
 * \param user, User data
 * \param  buffer the buffer address containing the data
 * \param  length  the data length
 * \param  mfs  the mfs used
 */
void sar_reassembly_mme_done (void *user, u8* buffer, uint length,
        mfs_rx_t *mfs, bool encrypted)
{
    dbg_assert (buffer);
    dbg_assert (length >= 60 && length <= 1518);
    dbg_assert (mfs);

    printf ("******************************************\n");
    printf ("          sar Reassembly MME done\n");
    printf ("******************************************\n");

    sar_mme_buffer_add (station_test.sar, buffer);

    blk_print_memory ();
}

// ---------------------------- Station functions --------------------------

/**
 * Add a link to the STAtion i.e. will create a MFS
 *
 * \param  ctx  the station context.
 * \param  type  TX or RX link, it is a short 1 for TX 0 for RX.
 * \param  bcast  short indicating if the link is a broadcast link
 * \param  mme   short indicating if the link will be use to transmit MMEs
 * \param  lid  short indicating if it is a link identitier.
 * \param  tei  short  the destination station.
 */
void station_test_link_add (station_test_t *ctx, uint type, uint bcast,
        uint mme, uint lid, uint tei)
{
    bool added;
    mfs_t *mfs;
    sta_t *sta;

    dbg_assert (ctx);
    dbg_assert (ctx->mac_store);
    dbg_assert (ctx->sar);

    if (MAC_TEI_IS_STA (tei))
    {
        mac_store_sta_add (ctx->mac_store, tei);
        sta = mac_store_sta_get (ctx->mac_store, tei);
        sta->authenticated = true;
        blk_release (sta);
    }

    mfs = mac_store_mfs_add (ctx->mac_store, type, bcast, mme, lid, tei,
            &added);
    dbg_assert (added);

    // add the link to the sar.
    sar_mfs_add (ctx->sar, mfs);

    /* Authenticated the station. */

    blk_release (mfs);
}

/**
 * Remove a link to the STAtion i.e. will create a MFS
 *
 * \param  ctx  the station context.
 * \param  type  TX or RX link, it is a short 1 for TX 0 for RX.
 * \param  bcast  short indicating if the link is a broadcast link
 * \param  mme   short indicating if the link will be use to transmit MMEs
 * \param  lid  short indicating if it is a link identitier.
 * \param  tei  short  the destination station.
 */
void station_test_link_remove (station_test_t *ctx, uint type, uint bcast,
        uint mme, uint lid, uint tei)
{
    /*
    mfs_t *mfs;
    pb_t *pb;
    */

    dbg_assert (ctx);
    dbg_assert (ctx->mac_store);
    dbg_assert (ctx->sar);
    dbg_assert (MAC_TEI_IS_STA (tei));

    sar_sta_remove (ctx->sar, tei);

    /*
    mfs = mac_store_mfs_get (ctx->mac_store, type, bcast, mme, lid, tei);

    if (mfs != NULL)
    {
        while (type && mfs->tx.head)
        {
            pb = mfs->tx.head;
            mfs->tx.head = mfs->tx.head->next;
            blk_release_desc ((blk_t *) pb);
        }

        while (!type && mfs->rx.head)
        {
            pb = mfs->rx.head;
            mfs->rx.head = mfs->rx.head->next;
            blk_release_desc ((blk_t *) pb);
        }

        // remove the MFS from the sar
        sar_mfs_remove (ctx->sar, mfs);

        blk_release (mfs);
    }
    */
}

/**
 * Create a schedule for the Channel Access (beacon period) in CSMA only mode.
 * Shall always be used to send or receive data.
 *
 * \param  int_sta  the pbproc context
 * \param  beacon_period_nb  the quantity of beacon periods for the test.
 */
void create_schedule_csma_only (station_test_t *int_sta, uint beacon_period_nb)
{
    pbproc_t *pbproc;

    dbg_assert (int_sta);

    pbproc = int_sta->pbproc;
    ca_beacon_period_t beacons_periods[beacon_period_nb];
    uint i;

    dbg_assert (pbproc);
    dbg_assert (beacon_period_nb < CA_BEACON_PERIOD_NB);

    /* Get and fill the schedule */
    ca_schedule_t *sched = ca_alloc_get_schedule (pbproc_get_ca (pbproc),
            int_sta->sched_index);

    sched->coexistence_mode = MAC_COEXISTENCE_SHARED_CSMA_HYBRID_MODE;
    sched->snid = int_sta->snid;
    sched->nek_switch = 0; //TODO
    sched->allocations_nb = 1;
    sched->allocations[0].end_offset_tck = 10000000;
    sched->allocations[0].glid = 0xff;

    /* Create a schedule for 14 beacon period */
    for (i = 0; i < beacon_period_nb; i++)
    {
        beacons_periods[i].start_date = 1000000 * i
                + my_station.current_tick_tck;
        beacons_periods[i].schedule_index = int_sta->sched_index;
    }

    /* Use the new schedule */
    ca_alloc_update_beacon_periods (pbproc_get_ca (pbproc), beacons_periods,
            beacon_period_nb);

    /* Activate the pbproc */
    if (!int_sta->pbproc_activate)
    {
        pbproc_activate (pbproc, true);
        int_sta->pbproc_activate = true;
    }

    int_sta->sched_index++;
}


/**
 * Thread entry finction
 *
 * \param  int_sta  the sta context.
 */
void pbproc_sched (cyg_addrword_t int_sta)
{
    station_test_t *station_test;

    station_test = (station_test_t *) int_sta;
    while (true)
    {
        create_schedule_csma_only (station_test, 14);
        station_test->sched_index ++;

        //create the second schedule.
        create_schedule_csma_only (station_test, 14);
        station_test->sched_index ++;

        //create the third schedule.
        create_schedule_csma_only (station_test, 14);
        station_test->sched_index ++;

        cyg_thread_delay (500);
    }
}


/**
 * Provides the Tei of the STA and the SNID
 *
 * \param  ctx  the station context
 * \param  tei  the tei of the station
 * \param  snid  the snid of the AVLN
 * \param  beacon_period_auto  generation automatic of the beacon period for
 * the CA scheduling
 * \param  cco  boolean informing if the sta is cco or not
 * \param  authenticated  boolean informing if the sta is authenticated or
 * not.
 */
void station_test_config (station_test_t *ctx, uint tei, uint snid,
        bool beacon_period_auto, mac_t mac_addr)
{
    dbg_assert (ctx);

    ctx->snid = snid;
    ctx->mac_config.tei = tei;

    if (mac_addr == 0)
    {
        ctx->mac_config.sta_mac_address = 0x123456789abcull;
    }
    else
        ctx->mac_config.sta_mac_address = mac_addr;

    if (beacon_period_auto)
    {
        // Create the Thread for the pbproc.
        cyg_thread_create (9, &pbproc_sched, (cyg_addrword_t) ctx, "Sched",
                sched_stack, CYGNUM_HAL_STACK_SIZE_TYPICAL,
                &ctx->sched_handle, &ctx->sched_thread);
        cyg_thread_resume (ctx->sched_handle);

        printf ("[STATION] PBproc Activated\n");
    }

    sar_mme_buffer_add (ctx->sar, mme_buffer);
    sar_activate (ctx->sar, true);
}

/**
 * Provides the TEI of all the station in the AVLN.
 *
 * \param  ctx  the station context.
 * \param  tei  the tei list of the stations
 * \param  qte  the quantity of TEIs in the list.
 */
void station_test_discover (station_test_t *ctx, uint *tei,
        uint qte)
{
    tei_node_t *node;
    uint i;

    for (i = 0; i < qte; i++)
    {
        if (tei[i] != 0)
        {
            node = blk_alloc ();
            list_init_node (&node->node);
            node->tei = tei[i];
            list_push (&ctx->tei_list, &node->node);

            mac_store_sta_add (ctx->mac_store, tei[i]);
        }
    }
}


// ---------------------------- Function calls -----------------------------

/** Uninitialise the function
 */
int fc_station_uninit (fcall_ctx_t *fcall, fcall_param_t **param,
    sci_msg_t **msg, void *data)
{
    uint i;
    sar_activate (station_test.sar, false);
    pbproc_activate (station_test.pbproc, false);

    for (i = MAC_TEI_STA_MIN; MAC_TEI_IS_STA (i); i++)
        sar_sta_remove (station_test.sar, i);

    sar_uninit (station_test.sar);
    pbproc_uninit (station_test.pbproc);
    mac_store_uninit (station_test.mac_store);

    lib_stats_uninit ();
    blk_print_memory ();

    return true;
}

/** Creates a link in the STA.
 *
 * - dtei  the tei of the station.
 * - fw  forward link
 * - rw  reverse link
 * - bcast  broadcast.
 *
 * \param  fcall the fcall context.
 * \param  param  the fcall param
 * \param  msg  the message
 * \param  data  anything
 */
int fc_add_mme_link (fcall_ctx_t *fcall, fcall_param_t **param,
    sci_msg_t **msg, void *data)
{
    uint dtei = 0;
    uint fw = 0;
    uint rw = 0;
    uint bcast = 0;

    mfs_tx_t *mfs_tx;
    mfs_rx_t *mfs_rx;
    bool added;

    fcall_param_bind_short (*param, *msg, "dtei", &dtei);
    fcall_param_bind_short (*param, *msg, "fw", &fw);
    fcall_param_bind_short (*param, *msg, "rw", &rw);
    fcall_param_bind_short (*param, *msg, "bcast", &bcast);

    fcall_param_reset (*param);

    if (fw)
    {
        mfs_tx = mac_store_mfs_add_tx (station_test.mac_store, bcast, true, MAC_LID_NONE, dtei, &added);

        if (mfs_tx == NULL)
            return false;

        sar_mfs_add (station_test.sar, (mfs_t *) mfs_tx);
        blk_release (mfs_tx);
    }

    if (rw)
    {
        mfs_rx = mac_store_mfs_add_rx (station_test.mac_store, bcast, true, MAC_LID_NONE, dtei, &added);

        if (mfs_rx == NULL)
            return false;

        sar_mfs_add (station_test.sar, (mfs_t *) mfs_rx);
        blk_release (mfs_rx);
    }

    return true;
}

/** Initialise the station parameters data as the mac address the tei.
 *
 * - stei.
 * - mac_addr.
 *
 * \param  fcall the fcall context.
 * \param  param  the fcall param
 * \param  msg  the message
 * \param  data  anything
 */
int fc_station_config (fcall_ctx_t *fcall, fcall_param_t **param,
    sci_msg_t **msg, void *data)
{
    uint stei = 0;
    mac_t mac_address = 0;

    fcall_param_bind_short (*param, *msg, "stei", &stei);
    fcall_param_bind (*param, *msg, "mac_addr", sizeof(mac_t), &mac_address);

    station_test.mac_config.sta_mac_address = mac_address;
    station_test.mac_config.tei = stei;
    station_test.mac_config.authenticated = true;
    sar_activate (station_test.sar, true);

    fcall_param_reset (*param);

    return true;
}


/** Prepare the station to work.
 * It will create default schedules for the Channel Access in CSMA-only mode
 * and activate the pbproc to start the communication with the others
 * stations.
 *
 * \param  fcall the fcall context.
 * \param  param  the fcall param
 * \param  msg  the message
 * \param  data  anything
 */
int fc_station_start (fcall_ctx_t *fcall, fcall_param_t **param,
    sci_msg_t **msg, void *data)
{
    ca_beacon_period_t beacons_periods[4];
    uint i;

    /* Get and fill the schedule */
    ca_schedule_t *sched = ca_alloc_get_schedule (pbproc_get_ca (station_test.pbproc),0);

    sched->coexistence_mode = MAC_COEXISTENCE_SHARED_CSMA_HYBRID_MODE;
    sched->snid = station_test.snid;
    sched->nek_switch = 0; //TODO
    sched->allocations_nb = 1;
    sched->allocations[0].end_offset_tck = BITS_ONES (24);
    sched->allocations[0].glid = 0xff;

    /* Create a schedule for 14 beacon period */
    for (i = 0; i < 4; i++)
    {
        beacons_periods[i].start_date = BITS_ONES(24) * i
                + my_station.current_tick_tck;
        beacons_periods[i].schedule_index = 0;
    }

    /* Use the new schedule */
    ca_alloc_update_beacon_periods (pbproc_get_ca (station_test.pbproc), beacons_periods,
            4);

    /* Only run this one !!! */
    pbproc_activate (station_test.pbproc, true);

    return true;
}

/**
 * Add a link to the Station in order to receive or transmit data.
 *
 * - type  TX or RX link, it is a short 1 for TX 0 for RX.
 * - bcast  short indicating if the link is a broadcast link
 * - mme   short indicating if the link will be use to transmit MMEs
 * - lid  short indicating if it is a link identitier.
 * - tei  short  the destination station.
 *
 * \param  fcall the fcall context.
 * \param  param  the fcall param
 * \param  msg  the message
 * \param  data  anything
 */
int fc_station_link_add (fcall_ctx_t *fcall, fcall_param_t **param,
        sci_msg_t **msg, void *data)
{
    uint type;
    uint bcast;
    uint mme;
    uint lid;
    uint tei;

    type = 0;
    bcast = 0;
    mme = 0;
    lid = 0;
    tei = 0;

    fcall_param_bind_short (*param, *msg, "type", &type);
    fcall_param_bind_short (*param, *msg, "bcast", &bcast);
    fcall_param_bind_short (*param, *msg, "mme", &mme);
    fcall_param_bind_short (*param, *msg, "lid", &lid);
    fcall_param_bind_short (*param, *msg, "tei", &tei);

    if (type)
        printf (
                "[STATION : %d] TX link added, bcast : %d, mme : %d, lid : %d, tei : %d\n",
                station_test.mac_config.tei, bcast, mme, lid, tei);
    else
        printf (
                "[STATION : %d] RX link added, bcast : %d, mme : %d, lid : %d, tei : %d\n",
                station_test.mac_config.tei, bcast, mme, lid, tei);

    station_test_link_add (&station_test, type, bcast, mme, lid, tei);

    fcall_param_reset (*param);

    return true;
}

/**
 * Remove a link to the Station in order to receive or transmit data.
 *
 * - type  TX or RX link, it is a short 1 for TX 0 for RX.
 * - bcast  short indicating if the link is a broadcast link
 * - mme   short indicating if the link will be use to transmit MMEs
 * - lid  short indicating if it is a link identitier.
 * - tei  short  the destination station.
 *
 * \param  fcall the fcall context.
 * \param  param  the fcall param
 * \param  msg  the message
 * \param  data  anything
 */
int fc_station_link_remove (fcall_ctx_t *fcall, fcall_param_t **param,
        sci_msg_t **msg, void *data)
{
    uint type;
    uint bcast;
    uint mme;
    uint lid;
    uint tei;

    type = 0;
    bcast = 0;
    mme = 0;
    lid = 0;
    tei = 0;

    fcall_param_bind_short (*param, *msg, "type", &type);
    fcall_param_bind_short (*param, *msg, "bcast", &bcast);
    fcall_param_bind_short (*param, *msg, "mme", &mme);
    fcall_param_bind_short (*param, *msg, "lid", &lid);
    fcall_param_bind_short (*param, *msg, "tei", &tei);

    if (type)
        printf (
                "[STATION : %d] TX link removed, bcast : %d, mme : %d, lid : %d, tei : %d\n",
                station_test.mac_config.tei, bcast, mme, lid, tei);
    else
        printf (
                "[STATION : %d] RX link removed, bcast : %d, mme : %d, lid : %d, tei : %d\n",
                station_test.mac_config.tei, bcast, mme, lid, tei);

    station_test_link_remove (&station_test, type, bcast, mme, lid, tei);

    fcall_param_reset (*param);

    return true;
}

/**
 * Initialize the configuration of the STA.
 * The parameters we must provide are the TEI of the STA, the SNID and the
 * boolean to indicate the auto generation of the beacon periods.
 *
 * \param  fcall the fcall context.
 * \param  param  the fcall param
 * \param  msg  the message
 * \param  data  anything
 */
int fc_station_init_config (fcall_ctx_t *fcall, fcall_param_t **param,
        sci_msg_t **msg, void *data)
{
    uint tei;
    uint snid;
    uint beacon_period_auto;
    mac_t mac;

    tei = 0;
    snid = 0;
    beacon_period_auto = 0;

    fcall_param_bind_short (*param, *msg, "tei", &tei);
    fcall_param_bind_short (*param, *msg, "snid", &snid);
    fcall_param_bind_short (*param, *msg, "auto_sched", &beacon_period_auto);

    station_test_config (&station_test, tei, snid, beacon_period_auto, mac);

    fcall_param_reset (*param);

    return true;
}

/**
 * Add a data buffer to the SAR.
 *
 * \param  fcall the fcall context.
 * \param  param  the fcall param
 * \param  msg  the message
 * \param  data  anything
 */
int fc_sar_data_buffer_add (fcall_ctx_t *fcall, fcall_param_t **param,
        sci_msg_t **msg, void *data)
{
    u8 *buffer;

    buffer = (u8*) malloc (2048 * sizeof(u8));
    sar_data_buffer_add (station_test.sar, buffer);

    fcall_param_reset (*param);

    return true;
}

/**
 * Add Msdu to send
 *
 * - lid  the link to use
 * - tei  the destination
 * - bcast  if the link is a bcast link
 * - mme  if it is a mme
 * - buffer  the buffer containing a frame
 * - length  the buffer length
 *
 * \param  fcall the fcall context.
 * \param  param  the fcall param
 * \param  msg  the message
 * \param  data  anything
 */
int fc_sar_msdu_add (fcall_ctx_t *fcall, fcall_param_t **param,
        sci_msg_t **msg, void *data)
{
    uint tei;
    uint lid;
    uint bcast;
    uint mme;
    uint length;
    u8 *buffer;

    mfs_tx_t *mfs;

    tei = 0;
    lid = 0;
    bcast = 0;
    mme = 0;
    length = 0;

    fcall_param_bind_short (*param, *msg, "tei", &tei);
    fcall_param_bind_short (*param, *msg, "lid", &lid);
    fcall_param_bind_short (*param, *msg, "bcast", &bcast);
    fcall_param_bind_short (*param, *msg, "mme", &mme);
    fcall_param_bind_short (*param, *msg, "length", &length);

    buffer = (u8 *) malloc (length * sizeof (length));
    fcall_param_bind (*param, *msg, "buffer", length * sizeof(u8), buffer);

    mfs = mac_store_mfs_get_tx (station_test.mac_store, bcast, mme, lid, tei);
    dbg_assert (mfs);

    sar_msdu_add (station_test.sar, buffer, length, mfs, NULL,
                  mac_ntb());

    blk_release (mfs);

    fcall_param_reset (*param);

    return true;
}

/**
 * Provide to the STA using the parameters of the function call the station
 * use for the test. Each STA shall known the TEI of the others present in the
 * AVLN.
 *
 * The function call parameters must be
 * - qte  the quantity of TEIs present in the paramters. One for each STA
 * without this one.
 * - tei  The list of TEIs.
 * - mac  the list of mac address.
 *
 * This will be use to add the STA's to the mac store, the STA will keep a
 * list of theses TEI to remove the STA during the uninit procedure.
 *
 *
 * \param  fcall the fcall context.
 * \param  param  the fcall param
 * \param  msg  the message
 * \param  data  anything
 */
int fc_station_discover (fcall_ctx_t *fcall, fcall_param_t **param,
        sci_msg_t **msg, void *data)
{
    uint tei[256];
    mac_t macs[256];
    uint qte;
    char val [2];
    char id[5];
    uint count;
    uint error;

    qte = 0;
    fcall_param_bind_short (*param, *msg, "qte", &qte);

    count = 0;
    error = 0;
    for (count = 0; count < qte; count ++)
    {
        tei[count] = 0;
        macs[count] = 0;
        sprintf (val, "%d", count);
        strcpy (id, "tei");
        strcat (id, val);
        fcall_param_bind_short (*param, *msg, id, &tei[count]);
    }

    station_test_discover (&station_test, tei, qte);

    fcall_param_reset (*param);

    return true;
}

/**
 * print the sar traces for the tests.
 * if the trace_config == y the traces are printed to the stdio.
 * otherwise nothing is printed.
 *
 * \param  fcall the fcall context.
 * \param  param  the fcall param
 * \param  msg  the message
 * \param  data  anything
 */
int fc_sar_print_trace (fcall_ctx_t *fcall, fcall_param_t **param,
        sci_msg_t **msg, void *data)
{
    fcall_param_reset (*param);

    sar_trace_print (station_test.sar);

    return true;
}


// ---------------------------- Stub functions ----------------------------
void
ce_measurements (void *data, pbproc_rx_params_t *rx_params,
                 uint total_pb_count, pb_t *chan_data,
                 uint chan_data_count, u8 false_pb_count, u32 ber_sum)
{
    if (chan_data)
        blk_release_desc_range_nb ((blk_t*) chan_data, chan_data_count);
}

/**
* CP receives a new MME.
* \param  user_data  the data registered by the actor in the init function.
* \param  mfs  the mfs
* \param  buffer  the buffer containing the MME.
* \param  length  the MME length
* \param  mme_recv  data use by the data plane.
* \param  encryption  inform if the packet comes from the PWL if it had been
* crypted or not.
*/
void
cp_mme_recv (void *user_data, mfs_rx_t *mfs, u8 *buffer, uint length,
    bool mme_recv, bool encryption)
{
    printf ("/*************************************************/\n");
    printf ("/ STATION : %d   Receive a MME          **********/\n",
            station_test.mac_config.tei);
    printf ("/ MME length : %d                       **********/\n", length);
    printf ("/ MME encrypted : %d                    **********/\n",
            encryption);
    printf ("/*************************************************/\n");
}

/**
* CP receives a beacon.
* \param  user_data the data registered by the actor in the init function.
* \param  beacon  the beacon freshly received.
*/
void
cp_beacon_add (void *user_data, pb_beacon_t *beacon)
{
    printf ("/*************************************************/\n");
    printf ("/ STATION : %d   Receive a beacon         ********/\n",
            station_test.mac_config.tei);
    printf ("/*************************************************/\n");


    // release the beacon.
    blk_release_desc ((blk_t *) beacon);
}

// -------------------------------------------------------------------------


/** Cesar init function as described in the wiki
 * http://pessac/cesar/trac/wiki/20071128Cesar#LEONcore
 */
void
cesar_init (void)
{
    // Initialise the trace system.
    trace_init ();

    // Initialise the mac_store.
    station_test.mac_store = mac_store_init ();

    // Initialise the mac config.
    mac_config_init (&station_test.mac_config);

    // Intialise the pbproc.
    station_test.pbproc = pbproc_init (&station_test.mac_config,
                                       station_test.mac_store);

    // Initialise the mac ntb.
    mac_ntb_init (pbproc_get_phy(station_test.pbproc), &station_test.mac_config);

    // Initialise the SAR.
    station_test.sar = sar_init (station_test.mac_store, station_test.pbproc,
                                 pbproc_get_ca (station_test.pbproc),
                                 station_test.mac_config.seed);

    // init the variables of the station.
    station_test.pbproc_activate = false;
    station_test.sched_index = 0;
}


/* ---------------------- Main function -------------------*/
int
cyg_user_start (void)
{
    lib_stats_init ();
    cesar_init ();

    station_log_set_level(&my_station, STATION_LOG_DEBUG);
    my_station.pipe_log_fd = 1;

    fcall_register (my_station.fcall, "fc_station_link_add",
            &fc_station_link_add, NULL);

    fcall_register (my_station.fcall, "fc_station_link_remove",
            &fc_station_link_remove, NULL);

    fcall_register (my_station.fcall, "fc_station_init_config",
            &fc_station_init_config, NULL);

    fcall_register (my_station.fcall, "fc_sar_data_buffer_add",
            &fc_sar_data_buffer_add, NULL);

    fcall_register (my_station.fcall, "fc_sar_mme_buffer_add",
            &fc_sar_data_buffer_add, NULL);

    fcall_register (my_station.fcall, "fc_sar_msdu_add", &fc_sar_msdu_add,
            NULL);

    fcall_register (my_station.fcall, "fc_station_discover",
            &fc_station_discover, NULL);

    fcall_register (my_station.fcall, "fc_add_mme_link",
        &fc_add_mme_link , NULL);

    fcall_register (my_station.fcall, "fc_station_config",
        &fc_station_config, NULL);

    fcall_register (my_station.fcall, "fc_station_start",
        &fc_station_start, NULL);

    fcall_register (my_station.fcall, "fc_sar_print_trace",
            &fc_sar_print_trace, NULL);

    fcall_register (my_station.fcall, "fc_station_uninit",
            &fc_station_uninit, NULL);

    sar_init_data_context (station_test.sar, station_test.sar);
    sar_init_mme_context (station_test.sar, station_test.sar);

    sar_init_segmentation_data_cb (station_test.sar, sar_segmentation_ul_done);
    sar_init_segmentation_mme_cb (station_test.sar, sar_segmentation_ul_done);
    sar_init_reassembly_data_cb (station_test.sar, sar_reassembly_ul_done);
    sar_init_reassembly_mme_cb (station_test.sar, sar_reassembly_mme_done);

    sar_init_measure_context (station_test.sar, &station_test.sar);
    sar_init_measurement_cb (station_test.sar, ce_measurements);

    return 0;
}