summaryrefslogtreecommitdiff
path: root/mac/sar/src/sar_common.c
blob: 3cfec928f864f59e09ab8ee3d0ed7997eedbf00f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    mac/sar/src/sar_common.c
 * \brief   common function for the reassembly and the segmentation part
 * \ingroup mac_sar
 * 
 * All the necessary functions to for the SAR.
 * 
 */

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

/**
 * Initialize the sar context.
 * 
 * \param  ctx, the mac store context to be used by the SAR.
 * 
 * The SAR only uses the mac store if the mpdu received from the pbproc is not
 * associated with a data or a mme MFS. In that case the SAR creates the MFS 
 * using the rx params to create it.
 */
void sar_init (mac_store_t *ctx)
{
    // Initialize the bridge dma job pending list.
    bridge_dma_list_init (&sar_ctx.bridge_dma_jobs);

    // init the mac store ctx in the sar.
    sar_ctx.mfs_store = ctx;

    // init the expiration mechanism for the PBs and MFSs.
    heap_init (&sar_ctx.mfs_expiration_heap, mfs_expiration_less);
    cyg_mutex_init (&sar_ctx.expiration_heap_mutex);

    // Creates the mailbox to stock the message work 
    cyg_mbox_create (&sar_ctx.mailbox_handle, &sar_ctx.mailbox);

    // Init the bridgedma context by giving the functions to call back when a
    // job had been bridge.
    sar_ctx.bridgedma_ctx = phy_bridgedma_init (NULL, bridge_interruption,
            bridge_interruption);

    //init the ca
    //sar_ctx.ca_ctx = ca_init()

    //TODO TBR
    sar_ctx.phy_ctx = blk_alloc ();
    sar_ctx.phy_ctx->control.current_date = 0;
}

/**
 * Used by the upper layers to send or received messages.
 * 
 * \param sar_seg_done, used by the segmentation to inform the upper layer the 
 * buffer is not used anymore.
 */
void sar_init_segmentation_mme_cb (sar_segmentation_done_cb_t sar_seg_done)
{
    sar_ctx.sar_seg_msg_done = sar_seg_done;
}

/**
 * Used by the upper layers to send or received data.
 * 
 * \param sar_seg_done, used by the segmentation to inform the upper layer the 
 * buffer is not used anymore.
 */
void sar_init_segmentation_data_cb (sar_segmentation_done_cb_t sar_seg_done)
{
    sar_ctx.sar_seg_data_done = sar_seg_done;
}

/**
 * Initialise the callback pointer and return the contexte to the Upper layers
 * 
 * \param  sar rea done, inform the upper layers that the buffer is filled.
 * \param  data, a boolean to indicate if it is a data stream or not.
 * \return  the ctx to use to add a buffer.
 */
sar_reassembly_ctx_t *sar_init_reassembly_ul (
        sar_reassembly_done_cb_t sar_rea_done, bool data)
{
    if (data)
    {
        bridge_dma_list_init (&sar_ctx.data_ctx.jobs_pending_list);
        buffer_address_init (&sar_ctx.data_ctx.buffer_address_list);
        sar_ctx.data_ctx.sar_rea_done = sar_rea_done;
        sar_ctx.data_ctx.number_of_slots = SAR_NUMBER_OF_SLOTS_DATA;

        return &sar_ctx.data_ctx;
    }
    else
    {
        bridge_dma_list_init (&sar_ctx.mme_ctx.jobs_pending_list);
        buffer_address_init (&sar_ctx.mme_ctx.buffer_address_list);
        sar_ctx.mme_ctx.number_of_slots = SAR_NUMBER_OF_SLOTS_MSG;
        sar_ctx.mme_ctx.sar_rea_done = sar_rea_done;

        return &sar_ctx.mme_ctx;
    }
}

/**
 * Initialize the callback to send the measurement contained in the PBs.
 * 
 * \param  sar_measurement, the function to call to get the buffers to stock 
 * the measurements. 
 */
void sar_init_measurement_cb (sar_measurement_cb_t sar_measurement)
{
    sar_ctx.sar_measurement = sar_measurement;
}

/**
 * Initiliaze the callback to send noise measurements to the upper layer.
 * 
 * \param sar_noise, the function to call to send the measure.
 */
void sar_init_noise_cb (sar_noise_cb_t sar_noise)
{
    sar_ctx.sar_noise = sar_noise;
}

/**
 * Release all the data used in the sar.
 */
void sar_uninit (void)
{
    bridge_dma_uninit_list(&sar_ctx.bridge_dma_jobs);
    
    cyg_mbox_delete(sar_ctx.mailbox_handle);
    cyg_mutex_destroy (&sar_ctx.expiration_heap_mutex);

    //phy_bridgedma_uninit(sar_ctx.bridgedma_ctx);
    
    //TODO to be removed
    blk_release ((blk_t *) sar_ctx.phy_ctx);
}

/**
 * Uninitialize the sar_reassembly context.
 * 
 * \param  sar_reassembly_ctx given by the sar_init_reassembly
 */
void sar_uninit_reassembly (sar_reassembly_ctx_t *ctx)
{
    dbg_assert (ctx);
    
    bridge_dma_uninit_list(&ctx->jobs_pending_list);
}

/**
 * Add a buffer address to the sar to bridge pending jobs.
 * 
 * \param  sar_reassembly_ctx, the reassembly context to add a buffer
 * \param  buffer_addr the buffer address to add to the sar in order to bridge
 * the pending jobs.
 */
void sar_buffer_add (sar_reassembly_ctx_t *ctx, u8* buffer_addr)
{
    sar_work_message_t *work;

    work = blk_alloc ();

    work->ctx = ctx;
    work->msg.buffer_addr = buffer_addr;
    work->type = ADD_BUFFER;

    cyg_mbox_put (sar_ctx.mailbox_handle, (void*) work);
}

/**
 * Initialize the list of jobs for the bridge DMA.
 */
void bridge_dma_list_init (sar_bridge_dma_list_t *list)
{
    list->head = NULL;
    list->tail = NULL;
}

/**
 * Add a job to the bridge dma list
 *
 * \param  job_mfs head to add to the list
 */
void bridge_dma_add_job_to_process (sar_bridge_dma_list_t *bridge_list,
        sar_job_mfs_t *head, sar_job_mfs_t *last)
{
    dbg_assert (head);
    dbg_assert (last);

    if (bridge_list->tail == NULL)
    {
        bridge_list->head = head;
        bridge_list->tail = last;
        head->job.last = true;
    }
    else
    {
        bridge_list->tail->next = head;
        bridge_list->tail = last;
        last->job.last = true;
    }

    phy_bridgedma_start (sar_ctx.bridgedma_ctx, &head->job, &last->job);
}

/**
 * Remove the first job of the bridge dma list
 * Release all the PBs in it.
 * 
 * \return true if the function had freed a job, false otherwise
 */
bool bridge_dma_free_head (sar_bridge_dma_list_t *bridge_list)
{
    sar_job_mfs_t *job;

    /* Get the head of the list i.e. unleashed it from the bridge_dma_jobs
     * and put it in the job. 
     */
    if ((job = bridge_dma_get_head (bridge_list)) != NULL)
    {
        //tx transmission
        if (!job->job.direction)
        {
            mfs_tx_t *local_mfs;

            local_mfs = (mfs_tx_t *) job->mfs;
            pbproc_mfs_provide ((mfs_tx_t *) job->mfs, job->pb_quantity);

            if (local_mfs->common.is_mme)
            {
                (*sar_ctx.sar_seg_msg_done) (NULL, job->job.data_addr,
                        job->job.data_len);
            }
            else
            {
                (*sar_ctx.sar_seg_data_done) (NULL, job->job.data_addr,
                        job->job.data_len);
            }

            blk_release ((blk_t *) job->mfs);
        }
        else
        {
            if (((mfs_rx_t *) job->mfs)->common.is_mme)
            {
                (*sar_ctx.mme_ctx.sar_rea_done) (NULL, job->job.data_addr, 0);
            }
            else
            {
                (*sar_ctx.data_ctx.sar_rea_done) (NULL, job->job.data_addr, 0);
            }
        }
        blk_release ((blk_t *) job);
        return true;
    }

    return false;
}

/**
 * Return the head of the list and remove it from the list
 * 
 * \param  the list to get the head
 * \return  the head of the lsit
 */
sar_job_mfs_t * bridge_dma_get_head (sar_bridge_dma_list_t *list)
{
    dbg_assert (list);
    sar_job_mfs_t *job = NULL;

    dbg_assert (list);

    if (list->head == list->tail)
    {
        job = list->head;
        list->head = NULL;
        list->tail = NULL;
    }
    else if (list->head != NULL)
    {
        job = list->head;
        list->head = list->head->next;
    }

    return job;
}

/**
 * Add a job to the tx/rx pending job list
 *
 * \param  job_mfs head to add to the list
 */
void bridge_dma_add_pending_job (sar_reassembly_ctx_t *ctx,
        sar_job_mfs_t *head)
{
    dbg_assert (ctx);
    dbg_assert (head);

    if (ctx->jobs_pending_list.head == NULL)
    {
        ctx->jobs_pending_list.head = head;
        ctx->jobs_pending_list.tail = head;
    }
    else
    {
        ctx->jobs_pending_list.tail->next = head;
        ctx->jobs_pending_list.tail = head;
    }

    head->next = NULL;
}

/**
 * Verify the if a list is empty.
 * 
 * \return  boolean indicating is the state of the list 
 */
bool bridge_dma_list_is_empty (sar_bridge_dma_list_t *list)
{
    dbg_assert (list);

    if (list->head == NULL)
    {
        return true;
    }

    return false;
}

/**
 * Remove all the jobs in the list and release the mfs and PBs in it.
 * 
 * \param  the list to uninit.
 */
void bridge_dma_uninit_list (sar_bridge_dma_list_t *list)
{
    dbg_assert (list);

    sar_job_mfs_t *job_mfs;

    //release all pending jobs.
    while ( (job_mfs = bridge_dma_get_head (list))
            != NULL)
    {
        // release the PB in the job
        while ((job_mfs->job.first_pb_desc
                = (blk_t *) pb_free ((pb_t *) job_mfs->job.first_pb_desc)))
            ;

        blk_release ((blk_t *) job_mfs->mfs);
        blk_release ((blk_t *) job_mfs);
    }
    
    list->head = NULL;
    list->tail = NULL;
}

/**
 * Function called by the phy hal when the bridge return an IT
 * 
 * \param  user, user data
 * \param  status_word, l'etat du bridge
 */
bool bridge_interruption (void *user, u32 status_word)
{
    sar_work_message_t *work;

    work = blk_alloc ();
    work->type = BRIDGE;
    work->msg.bridge_direction = sar_ctx.bridge_dma_jobs.head->job.direction;

    cyg_mbox_put (sar_ctx.mailbox_handle, work);

    return true;
}

/** Add a MFS to the SAR.
 * 
 * \param  user, the user data
 * \param  mfs the mfs to add to the SAR.
 */
void sar_mfs_add (void *user, mfs_t *mfs)
{
    dbg_assert (mfs);

    // add a reference on the mfs
    blk_addref ((blk_t *) mfs);
    
    // lock the mutex to access to the heap.
    cyg_mutex_lock (&sar_ctx.expiration_heap_mutex);

    heap_node_init (&mfs->common.expiration_heap_node);

    heap_insert (&sar_ctx.mfs_expiration_heap,
            &mfs->common.expiration_heap_node);

    // unlock the mutex to access to the heap.
    cyg_mutex_unlock (&sar_ctx.expiration_heap_mutex);
}

/** Remove a MFS from the SAR
 * 
 * \param  user, the user data
 * \param  mfs the mfs to remove from the SAR. 
 */
void sar_mfs_remove (void *user, mfs_t *mfs)
{
    dbg_assert (mfs);

    // lock the mutex to access to the heap.
    cyg_mutex_lock (&sar_ctx.expiration_heap_mutex);

    heap_remove (&sar_ctx.mfs_expiration_heap,
            &mfs->common.expiration_heap_node);

    blk_release ((blk_t *) mfs);
    
    // unlock the mutex to access to the heap.
    cyg_mutex_unlock (&sar_ctx.expiration_heap_mutex);
}

/**
 * get the mfs from the heap.
 * 
 * \return the mfs on the root element of the heap.
 */
mfs_t* expiration_heap_get_mfs (void)
{
    mfs_t *mfs_type = NULL;

    // lock the mutex to access to the heap.
    cyg_mutex_lock (&sar_ctx.expiration_heap_mutex);

    if (!heap_empty (&sar_ctx.mfs_expiration_heap))
    {
        mfs_type = PARENT_OF (mfs_t, common, PARENT_OF (mfs_common_t,
                expiration_heap_node,
                heap_get_root (&sar_ctx.mfs_expiration_heap)));
    }

    cyg_mutex_unlock (&sar_ctx.expiration_heap_mutex);

    return mfs_type;
}

/**
 * ajust the expiration heap
 */
void expiration_heap_ajust (mfs_t *mfs)
{
    dbg_assert (mfs);

    cyg_mutex_lock (&sar_ctx.expiration_heap_mutex);

    heap_adjust (&sar_ctx.mfs_expiration_heap,
            &mfs->common.expiration_heap_node);

    cyg_mutex_unlock (&sar_ctx.expiration_heap_mutex);
}

/**
 * remove the root element from the expiration heap.
 */
void expiration_heap_remove_root (void)
{
    cyg_mutex_lock (&sar_ctx.expiration_heap_mutex);

    heap_remove_root (&sar_ctx.mfs_expiration_heap);

    cyg_mutex_unlock (&sar_ctx.expiration_heap_mutex);
}

/**
 * Check the validity of the PBs for the mfs
 * 
 */
void mfs_manage_expiration (void)
{
    mfs_t *mfs_type;
    pb_t *pb;

    mfs_type = expiration_heap_get_mfs ();

    if (mfs_type == NULL)
    {
        return;
    }
    //TODO uncoment
    //    else if (ca_mfs_uninit(sar_ctx.ca_ctx, mfs_type))
    //    {
    //        return;
    //    }

    if (mfs_type->common.expiration_delay != 0
            && (u32)(mfs_type->common.expiration_delay
                    + mfs_type->common.expiration_date)
                    < sar_ctx.phy_ctx->control.current_date)
    {
        //removes all the PBs in the MFS
        if (mfs_type->common.is_tx && ((mfs_tx_t *)mfs_type)->head)
        {
            pb_free_range (((mfs_tx_t *)mfs_type)->head);
        }
        else if (!mfs_type->common.is_tx && ((mfs_rx_t *)mfs_type)->head)
        {
            pb_free_range (((mfs_rx_t *)mfs_type)->head);
        }

        sar_mfs_remove (NULL, mfs_type);
    }
    else if (mfs_type->common.is_tx)
    {
        mfs_tx_t *mfs;

        mfs = (mfs_tx_t *) mfs_type;

        dbg_assert (mfs);

        if (mfs->head != NULL)
        {
            mfs->head = pb_manage_expiration (mfs->head,
                    SAR_MAX_SEGMENTATION_TIMER);
        }
    }
    else
    {
        mfs_rx_t *mfs;

        mfs = (mfs_rx_t *) mfs_type;

        if (mfs->ats)
        {
            pb = mfs->head;
            while (pb != NULL)
            {
                if (pb->header.mfbf && mf_get_ats (pb, pb->header.mfbo)
                        + SAR_MAX_REASSEMBLY_TIMER
                        <= phy_date (sar_ctx.phy_ctx))
                {
                    pb = pb_free (pb);
                }
                else if (pb->expiration_date + SAR_MAX_REASSEMBLY_TIMER
                        <= phy_date (sar_ctx.phy_ctx))
                {
                    pb = pb_free (pb);
                }
                else
                {
                    break;
                }
            }

            mfs->head = pb;
        }
        else if (mfs->head != NULL)
        {
            mfs->head= pb_manage_expiration (mfs->head,
                    SAR_MAX_REASSEMBLY_TIMER);
        }

        mfs_update_last_contiguous_and_tail (mfs, mfs->head);
    }

    expiration_heap_ajust (mfs_type);
}

/**
 * Called by the mfs_manage_expiration to expire the pbs in the MFS.
 * 
 * \param  head the first pb to check.
 * \return the new head of the chain.
 */
pb_t * pb_manage_expiration (pb_t *head, uint expiration_delay)
{
    while (head != NULL && head->expiration_date + expiration_delay
            <= phy_date (sar_ctx.phy_ctx))
    {
        head = pb_free (head);
    }

    return head;
}

/**
 * Free a pb of a chain and return the next PB of the PB given in parameter
 * 
 * \param  pb, The PB to free
 * \return  The next PB of the freed PB.
 */
pb_t *pb_free (pb_t *pb)
{
    pb_t *next;

    dbg_assert (pb);

    next = pb->next;
    blk_release_desc ((blk_t *) pb);

    return next;
}

/**
 * Free a range of pb
 * 
 * \param  pb, The PB to free
 * \return  The next PB of the freed PB.
 */
void pb_free_range (pb_t *pb)
{
    dbg_assert (pb);

    while ((pb = pb_free( pb)));
}

/**
 * Get the next date to expire a mfs.
 * 
 */
void mfs_expiration_next_date_get (void)
{
    mfs_t *mfs;

    mfs = expiration_heap_get_mfs ();

    if (mfs->common.expiration_delay)
    {
        sar_ctx.mfs_expiration_date = mfs->common.expiration_date
                + mfs->common.expiration_delay;
    }
    else
    {
        sar_ctx.mfs_expiration_date = 0;
    }
}

/**
 * Compare the two nodes of the tree, if the left side is lesser than the right
 * it returns true otherwise it returns false.
 * 
 * \param  left the left heap node
 * \param  right the right heap node
 * \return  true if left expires before right, wrong otherwise.
 */
bool mfs_expiration_less (heap_node_t *left, heap_node_t *right)
{
    mfs_t *mfs_left;
    mfs_t *mfs_right;

    pb_t *pb_left;
    pb_t *pb_right;

    bool res;

    dbg_assert (left);
    dbg_assert (right);

    mfs_left = PARENT_OF (mfs_t, common, PARENT_OF (mfs_common_t,
            expiration_heap_node, left));
    mfs_right = PARENT_OF (mfs_t, common, PARENT_OF (mfs_common_t,
            expiration_heap_node, right));

    dbg_assert (mfs_left);
    dbg_assert (mfs_right);

    if (mfs_left->common.is_tx)
    {
        pb_left = ((mfs_tx_t *) mfs_left)->head;
    }
    else
    {
        pb_left = ((mfs_rx_t *) mfs_left)->head;
    }

    if (mfs_right->common.is_tx)
    {
        pb_right = ((mfs_tx_t *) mfs_right)->head;
    }
    else
    {
        pb_right = ((mfs_rx_t *) mfs_right)->head;
    }

    res = false;

    if (pb_left == NULL && pb_right != NULL)
    {
        res = false;
    }
    else if (pb_left != NULL && pb_right == NULL)
    {
        res = true;
    }
    else if (pb_left != NULL && (pb_left->expiration_date
            > pb_right->expiration_date))
    {
        res = false;
    }
    else if (pb_right != NULL && (pb_left->expiration_date
            < pb_right->expiration_date))
    {
        res = true;
    }

    if (mfs_left->common.expiration_delay == 0
            && mfs_right->common.expiration_delay != 0)
    {
        res = false;
    }
    else if (mfs_left->common.expiration_delay != 0
            && mfs_right->common.expiration_delay == 0)
    {
        res = true;
    }
    else if (mfs_left->common.expiration_delay
            + mfs_left->common.expiration_date
            > mfs_right->common.expiration_delay
                    + mfs_right->common.expiration_date)
    {
        res = false;
    }
    else
    {
        res = true;
    }

    return res;
}

/**
 * Update last pb continuous on the mfs
 * 
 * Verify the continuity of the chain from the first PB of the MFS to the last
 * contiguous.
 * 
 * If the counter of ssn had been reseted it verify the coninuity search the ssn
 * 0 after the ssn 655535.
 * 
 * \param  mfs the MFS to update
 * \param  pb of the MFS from wich one it is needed to update
 */
void mfs_update_last_contiguous_and_tail (mfs_rx_t *mfs, pb_t *pb)
{
    dbg_assert (mfs);

    if (pb != NULL)
    {
        while (pb->next != NULL&& ((pb->header.ssn + 1) & 0x0000ffff)
                == (0x0000ffff & pb->next->header.ssn))
        {
            pb = pb->next;
        }

        mfs->last_contiguous = pb;

        while (pb->next != NULL)
        {
            pb = pb->next;
        }

        mfs->tail = pb;

        //update the mfs window
        if (mfs->head->header.ssn == mfs->ssn_min|| less_mod2p16 (
                mfs->head->header.ssn, mfs->ssn_min))
        {
            mfs->ssn_min = mfs->last_contiguous->header.ssn + 1;
        }
    }
    
    if (mfs->head == NULL)
    {
        mfs->last_contiguous = NULL;
        mfs->tail = NULL;
    }
}

/**
 * Get the type of the MF from the PB at the data adress plus the offset
 * 
 * \param  The first pb containing the MF.
 * \param  offset the offset to start reading
 * \return  MF type.
 */
u8 mf_get_type (pb_t * pb, u16 offset)
{
    u8 type;

    // verify if the PB is not null
    dbg_assert (pb);

    type = 0;
    /* verifying that the offset is in the length of a PB */
    if (offset == (BLK_SIZE - 1)&& pb->next != NULL)
    {
        // get the type of the Mac Frame
        type = *(pb->data + offset) >> 6;
    }
    else if (offset <= BLK_SIZE - 1)
    {
        // get the type of the Mac Frame
        type = *(pb->data + offset) >> 6;
    }

    return type;
}

/**
 * Test the existance of a MF at the address given by mf and return its length.
 * Will be used to determine if a complete MF is avaliable in the MFS.
 * Test if the MF start in the end of the PB by verifying the offset, if the MFH 
 * is in two PBs.
 * 
 * Depends of mf_get_type
 * 
 * \param  The first pb containing the MF.
 * \param  offset the offset to start reading
 * \return  length of the MF
 */
u16 mf_get_length (pb_t *pb, u16 offset)
{
    u8 type;
    u16 mf_length = 0;

    if ((type = mf_get_type (pb, offset)) == 0)
    {
        return 0;
    }

    /* verifying that the offset is in the length of a PB */
    if (offset == (BLK_SIZE - 1)&& pb->next != NULL)
    {
        // read the length of the mac frame
        mf_length = ((*(pb->data + pb->header.mfbo) & 0x3f)<< 8)
                + *(pb->next->data );
    }
    else if (offset <= BLK_SIZE - 1)
    {
        // read the length of the mac frame
        mf_length = ( ( *(pb->data + offset) & 0x3f) << 8)+ *(pb->data
                + offset + 1);
    }

    switch (type)
    {
        case 0x01:
            mf_length = mf_length + 6;
            break;
        case 0x02:
        case 0x03:
            mf_length = mf_length + 10;
            break;
        default:
            mf_length = 0;
    }

    if (mf_length > SAR_MF_MAX_SIZE || mf_length < SAR_MF_MIN_SIZE)
    {
        return 0;
    }

    return mf_length;
}

/**
 * Calculate the number of PB used for the MF
 * 
 * Depends of the mf_get_length function
 * 
 * \param PB the first PB where the MF starts
 * \param offset the offset of the PB where the MF starts
 * \return the quantity of PBs 
 */
u8 mf_get_quantity_of_pb (pb_t *pb, u16 offset)
{
    u8 number_of_pb;
    u16 mf_length;

    if ((mf_length = mf_get_length (pb, offset)) == 0)
    {
        return 0;
    }

    number_of_pb = ((mf_length + offset) >> SAR_DIV_BY_BLK_SIZE) + 1;

    return number_of_pb;
}

/**
 * gets the ATS of a MF in the address pointed by the PB plus the offset
 * 
 * Depends of mf_get_type
 * 
 * \param PB the first PB where the MF starts
 * \param offset the offset of the PB where the MF starts 
 * \return  the ATS of the MF.
 */
u32 mf_get_ats (pb_t *pb, u16 offset)
{
    u8 type = 0;
    u32 ats;

    if (offset > SAR_MAX_MF_POSITION_MANAGE_EXPIRATION)
    {
        return 0;
    }

    type = mf_get_type (pb, offset);

    if (type > 0x1&& type <= 0x3)
    {
        ats = (*(pb->data + offset + 2) << 24) + (*(pb->data + offset + 3)
                << 16)+ (*(pb->data + offset + 4) << 8)+ *(pb->data + offset
                + 5);

        return ats;
    }
    else
    {
        return 0;
    }
}

/** 
 * Initialize the buffer address list
 * 
 * \param  list the buffer address list to initiliaze
 */
void buffer_address_init (buffer_address_list_t *list)
{
    dbg_assert (list);

    list->curr_slot = 0;
    list->next_slot = 0;
}

/**
 * Add an address to the buffer address buffer list
 * 
 * \param  ctx the ctx containing the list.
 * \param  address, the address to add to the list
 */
void buffer_address_list_add (sar_reassembly_ctx_t *ctx, u8 *address)
{
    dbg_assert (ctx);

    ctx->buffer_address_list.slots[ctx->buffer_address_list.next_slot]
            = address;

    ctx->buffer_address_list.next_slot++;
    if (ctx->buffer_address_list.next_slot == ctx->number_of_slots)
    {
        ctx->buffer_address_list.next_slot = 0;
    }
}

/**
 * Peek the first element of the list without removing it.
 * 
 * \param  ctx the context.
 */
u8* buffer_address_list_peek (sar_reassembly_ctx_t *ctx)
{
    dbg_assert (ctx);

    return ctx->buffer_address_list.slots[ctx->buffer_address_list.curr_slot];
}

/**
 * Get the current address and go to the next one.
 * 
 * \param  ctx, the ctx containing the list.
 */
u8* buffer_address_list_get (sar_reassembly_ctx_t *ctx)
{
    u8 *value = NULL;

    dbg_assert (ctx);

    if (!(value
            = ctx->buffer_address_list.slots[ctx->buffer_address_list.curr_slot]))
    {
        return value;
    }

    ctx->buffer_address_list.slots[ctx->buffer_address_list.curr_slot] = 0x0;

    ctx->buffer_address_list.curr_slot++;
    if (ctx->buffer_address_list.curr_slot == ctx->number_of_slots)
    {
        ctx->buffer_address_list.curr_slot = 0;
    }

    return value;
}

/**
 * Get the first work to do by the SAR
 * 
 * \return the work message 
 */
sar_work_message_t* sar_work_get (void)
{
    mfs_t *mfs;
    u32 delay;

    mfs = expiration_heap_get_mfs ();

    dbg_assert (mfs);

    if ((delay = mfs->common.expiration_delay))
    {
        delay += mfs->common.expiration_date;
    }

    if (mfs->common.is_tx)
    {
        if (((mfs_tx_t *) mfs)->head&& delay > ((mfs_tx_t *) mfs)->head->expiration_date)
        {
            delay = ((mfs_tx_t *) mfs)->head->expiration_date;
        }
    }
    else
    {
        if (((mfs_rx_t *) mfs)->head&& delay > ((mfs_rx_t *) mfs)->head->expiration_date)
        {
            delay = ((mfs_rx_t *) mfs)->head->expiration_date;
        }
    }

    return (sar_work_message_t *) cyg_mbox_timed_get (sar_ctx.mailbox_handle,
            phy_date (sar_ctx.phy_ctx) + delay);
}

//TODO TBR
u32 phy_date (phy_t *phy_ctx)
{
    return phy_ctx->control.current_date;
}