summaryrefslogtreecommitdiff
path: root/cesar/cl/src/cl.c
blob: 186413c81e9887b496482ba2c7f5b9d98d7b5195 (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cl/src/cl.c
 * \brief   private function for the Convergence Layer
 * \ingroup cl
 *
 */

#include "common/std.h"
#include "common/defs/ethernet.h"
#include "lib/bitstream.h"

#include "config/cl.h"
#include "cl/cl.h"
#include "cl/data_rate.h"
#include "mac/sar/sar.h"
#include "mac/pbproc/pbproc.h"
#include "mac/common/timings.h"

#include "cl/inc/context.h"
#include "cl/inc/cl.h"
#include "cl/inc/trace.h"
#include "cl/bridge_table.h"    // bridge_table_*

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

#include <string.h>
#include "config/cl/eoc.h"

#include "mac/common/store.h"

#include "hal/arch/arch.h"
#include "hal/gpio/gpio.h"

static struct cl_t cl_global;

/**
 * Search for the lid in the classifier
 *
 * \param  ctx  CL context
 * \param  tei  the tei found previously by the mactotei table.
 * \param  tag  tag provided by the upper layer.
 * \param  bcast  true if the link is a broadcast one, false otherwise.
 * \param  acs  put true if the link should be process by the ACS
 * \param  drop put true if the data should be drop.
 */
static inline uint
cl_classifer_get_lid_ (cl_t *ctx, uint tei, uint tag,
                      bool *bcast, bool *acs,
                      bool *drop)
{
    static uint lid_table[] = { 1, 0, 0, 1, 2, 2, 3, 3 };
    uint lid;

    dbg_assert (tag < COUNT (lid_table));

    if (tei == MAC_TEI_BCAST)
        *bcast = true;
    else
        *bcast = false;
    *acs = false;
    *drop = false;

    lid = lid_table[tag];

    if (CONFIG_CL_EOC_CLASSIFY)
    {
        if ((tei == MAC_TEI_UNASSOCIATED || tei == MAC_TEI_BCAST))
            lid = 0;
        else if (tag == CL_EOC_MME_DATA_TAG)
            lid = MAC_LLID_MME;
        else
            lid = MAC_LLID_MIN;
    }

    CL_TRACE (CLASSIFIER, phy_date (), tei, *bcast, *acs, *drop, lid);
    /** TODO fill the classifier */
    return lid;
}

uint
cl_classifer_get_lid (cl_t *ctx, uint tei, uint tag,
                              bool *bcast, bool *acs,
                              bool *drop)
{
    dbg_assert (ctx);
    dbg_assert (bcast);
    dbg_assert (acs);
    dbg_assert (drop);
    return cl_classifer_get_lid_ (ctx, tei, tag,bcast, acs, drop);
}
/**
 * Send a data once the TEI has been find.
 *
 * \param  ctx  the cl context.
 * \param  buffer  the buffer containing the data to send.
 * \param  length  the buffer data length.
 * \param  tei  the TEI found from the cl_mactotei list.
 * \param  tag  the tag provided by the upper layer.
 * \param  cl_data  opaque type.
 * \param  arrival_time_ntb  the arrival time in NTB.
 * \param  dmac  the final destination mac address.
 */
static inline void
cl_data_send_with_tei (cl_t *ctx, u8 *buffer, uint length, uint tei,
                       uint tag, cl_send_t *cl_data, u32 arrival_time_ntb,
                       mac_t dmac)
{
    uint lid;
    bool bcast;
    bool acs;
    bool drop;
    mfs_tx_t *mfs;

    dbg_claim (ctx);
    dbg_claim (buffer);
    dbg_claim ((length >= ETH_PACKET_MIN_SIZE_ALLOWED)
                && (length <= ETH_PACKET_MAX_SIZE));
    dbg_claim (tei);

    /* Get some data from the classifier. */
    lid = cl_classifer_get_lid_ (ctx, tei, tag, &bcast, &acs, &drop);

    if (MAC_LID_IS_PLID (lid))
    {
        bool added;

        /* Create the MFS if it does not exits. */
        mfs = mac_store_mfs_add_tx (ctx->mac_store, bcast, false, lid, tei,
                                    &added);

        if (added)
            sar_mfs_add (ctx->sar, (mfs_t *)mfs);
#if CONFIG_CL_EOC_ROUTE
        if (mfs && mfs->seg_nb >= MFS_TX_MAX_SEG_NB)
            drop = true;
        if (mfs)
            blk_addref (mfs);
#endif
    }
    else
    {
        /* try to get the mfs from the store. */
#if !CONFIG_CL_EOC_ROUTE
        mfs = mac_store_mfs_get_tx (ctx->mac_store, bcast, false, lid, tei);
#else
        mfs = mac_store_mfs_get_sta_tx_data (ctx->mac_store, lid, tei);
        if (mfs)
            blk_addref (mfs);
        if (mfs && mfs->seg_nb >= MFS_TX_MAX_SEG_NB)
            drop = true;
#endif
    }

    if (!drop && !acs && mfs)
    {
        CL_TRACE (DATA_SEND, phy_date (), buffer, length, mfs->common.tei,
                  mfs->common.lid,  mfs->common.bcast);
        ctx->data_send_link.mfs = mfs;
        ctx->data_send_link.last_update_date_ntb = arrival_time_ntb;
        ctx->data_send_link.dmac = dmac;
        sar_msdu_add (ctx->sar, buffer, length, mfs, cl_data,
                      arrival_time_ntb);

        /* update data rate informations associated to the TX
         * from the local sta to the associated sta */

        if (MAC_TEI_IS_STA (tei) && CONFIG_CL_DATA_RATE)
        {
            sta_t * sta = mac_store_sta_get (ctx->mac_store, tei);
            if (sta)
            {
                data_rate_update_info (&(sta->tx_data_rate), length);
                blk_release (sta);
            }
        }
    }
    else
    {
        CL_TRACE (DATA_SEND_DROP, phy_date (), ctx->mac_config->authenticated,
                  buffer, length);
        dbg_assert (ctx->data_tx.cb);
        (*ctx->data_tx.cb) (ctx->data_tx.user, buffer);
        /* release the mfs */
        if (mfs)
            blk_release (mfs);
    }

}

/** Initialise the cl_send object.
 * \param  cl  the cl context.
 * \param  buffer  the buffer
 * \param  mme  if the buffer contains a MME.
 * \return  a cl data object.
 *
 * This object is used to hold reference to the buffer. Once this object is
 * destroyed, it will release the associated buffer.  This is useful for
 * multiple unicast where the same buffer is sent several time to the SAR
 * layer.
 */
static cl_send_t *
cl_send_init (cl_t *ctx, u8 *buffer, bool mme)
{
    cl_send_t *cl_data;
    dbg_assert (ctx);
    dbg_assert (buffer);

    cl_data = slab_alloc (&ctx->slab_buffer_handler);

    cl_data->cl = ctx;
    cl_data->buffer = buffer;
    cl_data->mme = mme;
    return cl_data;
}

/**
 * Gives back the buffer to the CP or the HLE.
 * \param  object  destructed object
 *
 * This is the destructor of cl_send_t objects.
 */
static void
cl_send_release (void *object)
{
    cl_send_t *cl_data;
    cl_t *ctx;
    dbg_assert (object);

    cl_data = object;
    ctx = cl_data->cl;

    if (cl_data->mme)
    {
        cl_mme_buffer_add (cl_data->cl, cl_data->buffer);
        CL_TRACE (MME_SEND_DONE, phy_date (), cl_data->buffer);
    }
    else
    {
        dbg_assert (ctx->data_tx.cb);
        (*ctx->data_tx.cb) (ctx->data_tx.user, cl_data->buffer);
        CL_TRACE (DATA_SEND_DONE, phy_date (), cl_data->buffer);
    }
}

/**
 * Init the Convergence Layer and return a pointer on the CL context.
 *
 * \param  mac_store  the mac store.
 * \param  sar  the sar context.
 * \param  mac_config  the mac config.
 * \return  the convergence layer context.
 */
cl_t *
cl_init (mac_store_t *mac_store, sar_t *sar, mac_config_t *mac_config)
{
    cl_t *ctx;

    dbg_assert (mac_store);
    dbg_assert (sar);
    dbg_assert (mac_config);

    ctx = &cl_global;

    cl_global.mac_store = mac_store;

    /* Initialize the SAR */
    cl_global.sar = sar;
    cl_global.mme_buffer = ARCH_CPU_TO_UNCACHEABLE(cl_global.static_mme_buffer);

    // MME part
    sar_init_mme_context (cl_global.sar, &cl_global);
    sar_init_segmentation_mme_cb (cl_global.sar,
            (sar_segmentation_done_cb_t) cl_mme_sar_send_done);
    sar_init_reassembly_mme_cb (cl_global.sar,(sar_reassembly_done_cb_t) cl_mme_recv);

    sar_mme_buffer_add (cl_global.sar, cl_global.mme_buffer);

    // Data TX part
    sar_init_data_context (cl_global.sar, &cl_global);
    sar_init_segmentation_data_cb (cl_global.sar,
                                   (sar_segmentation_done_cb_t) cl_data_send_done);

    // DATA RX part
    sar_init_reassembly_data_cb (cl_global.sar, (sar_reassembly_done_cb_t)cl_data_recv);

    /* init the mme */
    cl_mme_init (&cl_global.mme);

    /* init the cl_mactotei table */
    cl_global.mactotei = NULL;

    /* init the cl mme buffer to null */
    cl_global.mme_ul_send.cb = NULL;
    cl_global.mme_ul_send.user = NULL;

    /* add the mac_config */
    cl_global.mac_config = mac_config;

    /* Initialize the trace system. */
    cl_trace_init (ctx);

    /* Initialise the slab cache. */
    slab_cache_init (&ctx->slab_buffer_handler,
                     "CL cache", sizeof (cl_send_t), cl_send_release);

    /* Initialize the local bridge table module. */
    bridge_table_init (ctx);

#if CONFIG_CL_EOC_ROUTE
    cl_eoc_mactotei_init (ctx);
#endif
    /* Initialise the data link.  */
    ctx->data_send_link.mfs = NULL;

    CL_TRACE (INIT, phy_date ());

    /* Debug leds. */
    GPIO_SETUP (LED_CL_RX, GPIO_DIRECTION_OUT);
    GPIO_SET (LED_CL_RX, 0);
    GPIO_SETUP (LED_CL_TX, GPIO_DIRECTION_OUT);
    GPIO_SET (LED_CL_TX, 0);


    return &cl_global;
}

/**
 * Init the MME module of the CL
 *
 * \param  ctx  the mme context.
 */
void cl_mme_init (cl_mme_t *ctx)
{
    dbg_assert (ctx);

    ctx->ul_mme_recv_done = NULL;
    ctx->ul_mme_recv_done_user_data = NULL;
}

/**
 * Uninit the Convergence layer context.
 *
 * \param  ctx  the convergence layer context
 */
void cl_uninit (cl_t *ctx)
{
    dbg_assert (ctx);

    CL_TRACE (UNINIT, phy_date ());

    if (ctx->mactotei)
    {
        cl_mactotei_release_table (ctx);
    }

    /* Uninitialise the slab cache. */
    slab_cache_uninit (&ctx->slab_buffer_handler);

    /* De-initialize the local bridge table module. */
    bridge_table_deinit (ctx);
    cl_data_send_link_clear (ctx);
    cl_trace_uninit(ctx);
}

/**
 * Initialize the CL to call the Upper layer once the CP ends processing the
 * MME.
 * Used each time the CP needs to send an MME to the upper layer.
 *
 * \param  ctx  the CL context
 * \param  cb  the upper layer callback to use to send an MME.
 * \param  user  the user data to provide with the callback
 */
void cl_mme_ul_init_send_done (cl_t *ctx, cl_mme_ul_recv_done_cb_t cb,
        void *user)
{
    dbg_assert (ctx);
    dbg_assert (cb);

    ctx->mme.ul_mme_recv_done = cb;
    ctx->mme.ul_mme_recv_done_user_data = user;
}

/**
 * Init the function call when an MME is received from the SAR or the HLE.
 * The CP registers its callback to allow the CL to call it each time a MME
 * is received from the PLC or the HLE.
 *
 * \param  ctx  the CL ctx
 * \param  mme_recv_cb
 * \param  user_data  the user data
 */
void cl_mme_recv_init (cl_t *ctx, cl_mme_recv_cb_t mme_recv_cb, void
*user_data)
{
    dbg_assert (ctx);
    dbg_assert (mme_recv_cb);

    ctx->mme.mme_recv_cb = mme_recv_cb;
    ctx->mme.mme_recv_user_data = user_data;
}

/**
 * Initialize the CL to send MMEs to the Upper layer considered as data.
 * Used each time the CP needs to send an MME to the upper layer.
 *
 * \param  ctx  the CL context
 * \param  cb  the upper layer callback to use to send an MME.
 * \param  user  the user data to provide with the callback
 */
void cl_mme_init_ul_as_data (cl_t *ctx, cl_mme_ul_send_done_cb_t cb,
        void *user)
{
    dbg_assert (ctx);
    dbg_assert (cb);

    ctx->mme_ul_send.cb = cb;
    ctx->mme_ul_send.user = user;
}

/**
 * Initialize the CP call back to get a buffer.
 *
 * \param  cl  the cl context
 * \param  cb  the call back function
 * \param  user_data  the user data.
 */
void cl_mme_init_buffer_add_cb (cl_t *cl, cl_mme_buffer_add_cb_t cb,
        void *user_data)
{
    dbg_assert (cl);
    dbg_assert (cb);

    cl->mme.mme_buffer_add_cb = cb;
    cl->mme.mme_buffer_add_user_data = user_data;
}

/**
 * Sends a packet to the SAR.
 * \param  ctx  the CL context.
 * \param  buffer the buffer length.
 * \param  length  the buffer length.
 * \param  tei  the destination TEI.
 * \param  mme  true if the message comes from the CP, false otherwise.
 * \param  arrival_time_ntb  the arrival time in NTB.
 */
static void
cl_send_unicast (cl_t *ctx, u8 *buffer, uint length, uint tei,
                 bool mme, u32 arrival_time_ntb)
{
    cl_send_t *cl_data;
    mfs_tx_t *mfs;
    bool added;
    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert ((length >= ETH_PACKET_MIN_SIZE_ALLOWED)
                && (length <= ETH_PACKET_MAX_SIZE));
    dbg_assert (MAC_TEI_FOREIGN != tei);

    if (mme &&  ((MAC_TEI_IS_STA (tei)) || (MAC_TEI_BCAST == tei)))
    {
        /* Allocate the buffer handler. */
        cl_data = cl_send_init (ctx, buffer, mme);

        mfs = mac_store_mfs_add_tx (ctx->mac_store,
                                    tei == MAC_TEI_BCAST ? true : false,
                                    true /* mme */,
                                    MAC_LID_NONE,
                                    tei,
                                    &added);

        if (added)
            sar_mfs_add (ctx->sar, (mfs_t *) mfs);

        CL_TRACE (MME_SEND_UNICAST, phy_date (), length, buffer, tei);
        sar_msdu_add (ctx->sar, buffer, length,
                      mfs, cl_data, arrival_time_ntb);

        blk_release (mfs);
    }
    else
        /* FIXME to be implemented for the DATA too. */
        dbg_assert_default ();
}

/**
 * Sends a packet to the SAR.
 * \param  ctx  the CL context.
 * \param  buffer the buffer length.
 * \param  length  the buffer length.
 * \param  tei  the destination TEI.
 * \param  mme  true if the message comes from the CP, false otherwise.
 * \param  tag  the tag provided by the upper layer.
 * \param  arrival_time_ntb  the arrival time NTB.
 */
static void
cl_send_multiunicast (cl_t *ctx, u8 *buffer, uint length, bool mme, uint tag,
                      u32 arrival_time_ntb)
{
    cl_send_t *cl_data;
    uint i;
    sta_t *sta;
    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert ((length >= ETH_PACKET_MIN_SIZE_ALLOWED)
                && (length <= ETH_PACKET_MAX_SIZE));

    if (mme)
    {
        /* Allocate the buffer handler. */
        cl_data = cl_send_init (ctx, buffer, mme);

        for ( i = MAC_TEI_STA_MIN; i < MAC_TEI_STA_NB; i++)
        {
            sta = mac_store_sta_get (ctx->mac_store, i);

            if (sta && sta->multi_unicast_receiver)
            {
                mfs_tx_t *mfs;
                bool added;

                mfs = mac_store_mfs_add_tx (ctx->mac_store,
                                            false /* MME */,
                                            true /* MME */,
                                            MAC_LID_NONE,
                                            i,
                                            &added);
                if (added)
                    sar_mfs_add (ctx->sar, (mfs_t *) mfs);

                CL_TRACE (MME_SEND_MULTIUNICAST, phy_date (), length, buffer,
                          i);
                slab_addref (cl_data);
                sar_msdu_add (ctx->sar, buffer, length,
                              mfs, cl_data, arrival_time_ntb);

                blk_release (mfs);
                blk_release (sta);
            }
        }
        slab_release (cl_data);
    }
    else
        /* FIXME Implement for DATA too. */
        dbg_assert_default ();
}

/**
 * Prepare the CL to send a Frame.
 * \param  ctx  the cl context.
 * \param  buffer  the Ethernet frame to send.
 * \param  length  the Ethernet frame length.
 * \param  tag  the tag for the classifier.
 * \param  arrrival_time_ntb  arrival time in the High layers.
 * \param  dmac  the destination mac address.
 * \param  smac  the source mac address.
 */
static void
cl_data_send_prepare (cl_t *ctx, u8 *buffer, uint length, uint tag,
                      u32 arrival_time_ntb, mac_t dmac, mac_t smac)
{
    /* The source MAC address should not be not be a multicast, a zero address
     * nor a broadcast one (note that checking multicast address also checks
     * for broadcast). Linux, in bridging mode, handle this problem and reject
     * any of those packets (see net/bridge/br_input.c:127 (in
     * br_handle_frame). */
    dbg_assert (smac != MAC_ZERO);
    dbg_assert (!mac_is_multicast (smac));
    /* Add the source MAC address to the local bridge table if not our MAC
     * address. */
    dbg_assert (ctx->mac_config);
    if (ctx->mac_config->sta_mac_address != smac && !CONFIG_CL_EOC_ROUTE)
        bridge_table_add (ctx, smac);

    /* Data are forbidden if not authenticated. */
    if (!ctx->mac_config->authenticated)
    {
        (*ctx->data_tx.cb) (ctx->data_tx.user, buffer);
        CL_TRACE (DATA_SEND_DROP, phy_date (), ctx->mac_config->authenticated,
                  buffer, length);
        return;
    }

    /* Get the TEI from the mactotei table. */
    uint tei;

    if (!CONFIG_CL_EOC_ROUTE)
        tei = cl_mactotei_table_find_tei_from_mac (ctx, dmac);
    else
        tei = MAC_TEI_UNASSOCIATED;

#if CONFIG_CL_EOC_ROUTE
    /* Find destination TEI */
    if (MAC_TEI_IS_EOC_STA(ctx->mac_config->tei))
        tei = MAC_TEI_CCO_DEF;
    else if (tei == MAC_TEI_UNASSOCIATED)
        tei = cl_eoc_mactotei_find_tei (ctx, dmac);
    /* For STA, if tei is unassociated, or MAC limit is reached
     * drop the packet */
    if (MAC_TEI_IS_EOC_STA(ctx->mac_config->tei))
    {
        bool drop;
        drop = !cl_eoc_mactotei_entry_insert (ctx, smac,
                                              ctx->mac_config->tei);
        if ((tei == MAC_TEI_UNASSOCIATED) || drop)
        {
            dbg_assert (ctx->data_tx.cb);
            (*ctx->data_tx.cb) (ctx->data_tx.user, buffer);
            return;
        }
    }
#endif

    dbg_assert (tei != ctx->mac_config->tei);

    /* If the TEI is not found the packet is send as broadcast. */
    cl_data_send_with_tei (ctx, buffer, length,
                           tei == MAC_TEI_UNASSOCIATED ? MAC_TEI_BCAST: tei,
                           tag, NULL, arrival_time_ntb, dmac);
}

void
cl_data_send (cl_t *ctx, u8 *buffer, uint length, uint tag,
              u32 arrival_time_ntb)
{
    mac_t smac, dmac;
    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert ((length >= ETH_PACKET_MIN_SIZE_ALLOWED)
                && (length <= ETH_PACKET_MAX_SIZE));
    bitstream_direct_read_macs (buffer, &dmac, &smac);
    if (ctx->data_send_link.mfs
        && ctx->data_send_link.mfs->fsm_state != MFS_FSM_CMD_RELEASE
        && dmac == ctx->data_send_link.dmac
        && ctx->mac_config->authenticated
        && less_mod2p32 (arrival_time_ntb,
                         ctx->data_send_link.last_update_date_ntb
                         + MAC_MS_TO_TCK (CL_DATA_SEND_EXCEED_TIME_MS)))
    {
        CL_TRACE (DATA_SEND, phy_date (), buffer, length,
                  ctx->data_send_link.mfs->common.tei,
                  ctx->data_send_link.mfs->common.lid,
                  ctx->data_send_link.mfs->common.bcast);
#if CONFIG_CL_EOC_ROUTE
        if (ctx->data_send_link.mfs->seg_nb >= MFS_TX_MAX_SEG_NB)
            (*ctx->data_tx.cb) (ctx->data_tx.user, buffer);
        else
#endif
            sar_msdu_add (ctx->sar, buffer, length,
                          ctx->data_send_link.mfs, NULL,
                          arrival_time_ntb);
    }
    else
    {
        /* Release the MFS's reference. */
        if (ctx->data_send_link.mfs)
        {
            blk_release (ctx->data_send_link.mfs);
            ctx->data_send_link.mfs = NULL;
        }
        cl_data_send_prepare (ctx, buffer, length, tag,
                              arrival_time_ntb, dmac, smac);
    }
}

/**
 * Send an MME from the CP to the PLC via the SAR MME way.
 *
 * \param  ctx  the CL context
 * \param  buffer  the buffer containing the MME
 * \param  length  the length of the MME
 * \param  mfs  the MFS use for the SAR if the MME should be send over the PLC
 */
void
cl_mme_send (cl_t *ctx, u8 *buffer, uint length, uint tei)
{
    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert ((length >= ETH_PACKET_MIN_SIZE_ALLOWED)
                && length <= ETH_PACKET_MAX_SIZE);
    dbg_assert (ctx->sar);


    /* Unicast or broadcast neighbour.
     * Unicast is allowed even if the station is not associated.
     * The broadcast is only allowed if the station is associated. */
    if (((MAC_TEI_IS_STA (tei)
          || (tei == MAC_TEI_BCAST)))
        && (MAC_TEI_IS_STA(ctx->mac_config->tei)))
    {
        cl_send_unicast (ctx, buffer, length,
                         tei,
                         true /* MME */,
                         mac_ntb());
    }
    /* Handle the unassociated unicast with temporary MFS. */
    else if (MAC_TEI_IS_STA (tei)
             && (ctx->mac_config->tei == MAC_TEI_UNASSOCIATED))
    {
        mfs_tx_t *mfs;
        cl_send_t *cl_data;

        mfs = &mac_store_mfs_unassociated_add (
            ctx->mac_store, true /* tx */, false /* unicast */,
            true /* mme */, MAC_LID_NONE, tei)->tx;

        dbg_assert (ctx->sar);
        sar_mfs_add (ctx->sar, (mfs_t *) mfs);

        cl_data = cl_send_init (ctx, buffer, true /* MME. */);

        CL_TRACE (MME_SEND_UNASSOC_UNICAST, phy_date (), length, buffer,
                  mfs->common.tei);
        sar_msdu_add (ctx->sar, buffer, length,
                      mfs, cl_data, mac_ntb());
        blk_release (mfs);
    }
    /* It the destination TEI is broadcast and our station is not
     * associated, disallow to send a broadcast packet as a multi unicast.*/
    else if (tei == MAC_TEI_MULTI_UNICAST)
    {
        dbg_assert (ctx->mac_config->tei);
        cl_send_multiunicast (ctx, buffer, length, true /* MME */, 1,
                              mac_ntb());
    }
    /* the destination is not associated or associated and our station
     * can or not be associated */
    else if ((tei == MAC_TEI_UNASSOCIATED)
             || (tei == MAC_TEI_BCAST))
    {
        mfs_tx_t *mfs;
        cl_send_t *cl_data;

        mfs = &mac_store_mfs_unassociated_add (
            ctx->mac_store, true /* tx */, true /* bcast */,
            true /* mme */, MAC_LID_NONE, MAC_TEI_BCAST)->tx;

        /* Quick hack to send the CC_ASSOC.CNF before the
         * CC_SET_TEI_MAP.IND. */
        mfs->cap = 3;

        dbg_assert (ctx->sar);
        sar_mfs_add (ctx->sar, (mfs_t *) mfs);

        cl_data = cl_send_init (ctx, buffer, true /* MME. */);

        CL_TRACE (MME_SEND_TO_UNASSOC, phy_date (), length, buffer,
                  mfs->common.tei);
        sar_msdu_add (ctx->sar, buffer, length,
                      mfs, cl_data, mac_ntb());

        blk_release (mfs);
    }
    else
    {
        CL_TRACE (MME_SEND_TO_DRIVER, phy_date (), length, buffer);
        dbg_assert (ctx->mme_ul_send.cb);
        arch_dsr_lock ();
        (*ctx->mme_ul_send.cb) (ctx->mme_ul_send.user, buffer, length);
        arch_dsr_unlock ();
    }
}

/**
 * Callback called by the sar when a MME as been sent over the PWL.
 *
 * \param  ctx  the cl context.
 * \param  buffer  the buffer used.
 * \param  cl_data  the data provided to the SAR on the send.
 */
void cl_mme_sar_send_done (cl_t *ctx, u8 *buffer, void *cl_data)
{
    cl_send_t *data;
    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert (cl_data);

    data = cl_data;

    slab_release (data);

    /* Debug info. */
    GPIO_TOGGLE (LED_CL_TX);
}

void cl_mme_recv (cl_t *ctx, u8 *buffer, uint length, mfs_rx_t *mfs, bool
                  encryption)
{
    dbg_assert (ctx);

    dbg_assert (ctx->mme.mme_recv_cb);
    dbg_assert (buffer);
    dbg_assert ((length >= ETH_PACKET_MIN_SIZE_ALLOWED)
                && (length <= ETH_PACKET_MAX_SIZE));

    (*ctx->mme.mme_recv_cb) (ctx->mme.mme_recv_user_data,
                             mfs ? mfs->common.tei : MAC_TEI_FOREIGN,
                             buffer, length, mfs ? true : false, encryption);

    CL_TRACE (MME_RECV, phy_date (), length, buffer, mfs ? true : false);

    /* Debug info. */
    GPIO_TOGGLE (LED_CL_RX);
}

void cl_mme_recv_done (cl_t *ctx, u8* buffer, bool mme_recv)
{
    dbg_assert (ctx);
    dbg_assert (buffer);

    CL_TRACE (MME_RECV_DONE, phy_date (), buffer, mme_recv);

    if (mme_recv)
    {
        dbg_assert (ctx->sar);
        sar_mme_buffer_add (ctx->sar, buffer);
    }
    else
    {
        dbg_assert (ctx->mme.ul_mme_recv_done);
        (*ctx->mme.ul_mme_recv_done) (ctx->mme.ul_mme_recv_done_user_data,
                buffer);
    }
}




/**
 * Initialize the callback to inform the upper layer when a data had been sent
 * over the PLC.
 *
 * \param  cl  the CL context
 * \param  cb  the callback to call once the data had been sent
 * \param  user  the user data to provide with the callback call
 */
void cl_data_send_done_init (cl_t *cl, cl_data_send_done_cb_t cb, void *user)
{
    dbg_assert (cl);
    dbg_assert (cb);

    cl->data_tx.cb = cb;
    cl->data_tx.user = user;
}


/**
 * The SAR inform the CL that the data previously provided had been sent over
 * the PLC.
 *
 * \param  ctx  the CL context.
 * \param  buffer  the buffer containing the MME
 * \param  cl_data  the data provided to the SAR on the msdu add.
 */
void cl_data_send_done (cl_t *ctx, u8 *buffer, void *cl_data)
{
    dbg_assert (ctx);
    dbg_assert (buffer);

    if (!cl_data)
    {
        /* Give back to upper layer. */
        dbg_assert (ctx->data_tx.cb);
        (*ctx->data_tx.cb) (ctx->data_tx.user, buffer);
        CL_TRACE (DATA_SEND_DONE, phy_date (), buffer);
    }
    else
        slab_release (cl_data);

    /* Debug info. */
    GPIO_TOGGLE (LED_CL_TX);
}

/**
 * Initialize the callback to receive the data from the PLC.
 *
 * \param  cl  the CL context
 * \param  cb  the function callback to call
 * \param  user  the user data to provide on the callback.
 */
void cl_data_recv_init (cl_t *cl, cl_data_recv_cb_t cb, void *user)
{
    dbg_assert (cl);
    dbg_assert (cb);

    cl->data_rx.cb = cb;
    cl->data_rx.user = user;
}

/**
 * Called by the SAR each time it has a data to provide to the CL.
 *
 * \param  ctx  the CL context
 * \param  buffer  the buffer containing the data to send to the Upper layer.
 * \param  length  the data length in the buffer
 * \param  mfs  the mfs used to receive the data.
 */
void cl_data_recv (cl_t *ctx, u8 *buffer, uint length, mfs_rx_t *mfs)
{
    uint tei;

    dbg_assert (ctx);
    dbg_assert (buffer);
    dbg_assert ((length >= ETH_PACKET_MIN_SIZE_ALLOWED)
                && (length <= ETH_PACKET_MAX_SIZE));
    dbg_assert (mfs);

    dbg_assert (ctx->data_rx.cb);
    /* Get source MAC address. */
    mac_t src = bitstream_direct_read_mac (buffer + 6);

    if (CONFIG_TRACE)
    {
        mac_t dest = bitstream_direct_read_large (buffer, 0, 48);
        mac_t src = bitstream_direct_read_large (buffer, 48, 48);
        CL_TRACE (DATA_RECV, phy_date (), buffer, TRACE_U64(dest),
                  TRACE_U64(src), length);
    }

    (*ctx->data_rx.cb) (ctx->data_rx.user, buffer, length);

    /* update data rate informations associated to the RX
     * from the associated sta to the local sta */
    tei = mfs->common.tei;

#if CONFIG_CL_EOC_ROUTE
    bool ok = true;
    if (MAC_TEI_IS_EOC_CCO(ctx->mac_config->tei))
        ok = cl_eoc_mactotei_entry_insert (ctx, src, tei);
    dbg_assert (ok);
#endif

    if (MAC_TEI_IS_STA (tei) && CONFIG_CL_DATA_RATE)
    {
        sta_t * sta = mac_store_sta_get (ctx->mac_store, tei);
        if (sta)
        {
            data_rate_update_info(&(sta->rx_data_rate), length);
            blk_release (sta);
        }
    }
    /* Debug info. */
    GPIO_TOGGLE (LED_CL_RX);
}

/**
 * Provides a buffer to the CP.
 *
 * \param  ctx  the CL context
 * \param  buffer  the buffer to reassembly some data
 */
void cl_mme_buffer_add (cl_t *ctx, u8 *buffer)
{
    dbg_assert (ctx);
    dbg_assert (buffer);

    CL_TRACE (MME_BUFFER_ADD, phy_date (), buffer);
    dbg_assert (ctx->mme.mme_buffer_add_cb);
    (*ctx->mme.mme_buffer_add_cb) (ctx->mme.mme_buffer_add_user_data, buffer);
}

/**
 * Provides a buffer to the SAR to reassembly data
 *
 * \param ctx  the CL context
 * \param buffer  the buffer to reassembly some data
 */
void cl_data_buffer_add (cl_t *ctx, u8 *buffer)
{
    dbg_assert (ctx);
    dbg_assert (buffer);

    CL_TRACE (DATA_BUFFER_ADD, phy_date (), buffer);

    sar_buffer_add (ctx->sar, buffer, true);
}

void
cl_data_send_link_clear (cl_t *ctx)
{
    arch_dsr_lock ();
    if (ctx->data_send_link.mfs)
        blk_release (ctx->data_send_link.mfs);
    ctx->data_send_link.mfs = NULL;
    ctx->data_send_link.dmac = MAC_ZERO;
    arch_dsr_unlock ();
}