rlc_um_segment.c 31.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.0  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

22
23
#define RLC_UM_MODULE 1
#define RLC_UM_SEGMENT_C 1
24
25
26
27
//-----------------------------------------------------------------------------
#include "platform_types.h"
#include "platform_constants.h"
//-----------------------------------------------------------------------------
28
#if USER_MODE
29
30
#include <assert.h>
#endif
31
#include "assertions.h"
knopp's avatar
mutexes    
knopp committed
32
#include "msc.h"
33
34
35
36
37
38
39
#include "list.h"
#include "rlc_um.h"
#include "rlc_primitives.h"
#include "UTIL/LOG/log.h"

//-----------------------------------------------------------------------------
void
knopp's avatar
mutexes    
knopp committed
40
rlc_um_segment_10 (const protocol_ctxt_t* const ctxt_pP, rlc_um_entity_t *rlc_pP)
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
  list_t              pdus;
  signed int          pdu_remaining_size;
  signed int          test_pdu_remaining_size;

  int                 nb_bytes_to_transmit = rlc_pP->nb_bytes_requested_by_mac;
  rlc_um_pdu_sn_10_t *pdu_p;
  struct mac_tb_req  *pdu_tb_req_p;
  mem_block_t        *pdu_mem_p;
  char               *data;
  char               *data_sdu_p;
  rlc_um_e_li_t      *e_li_p;
  struct rlc_um_tx_sdu_management *sdu_mngt_p;
  unsigned int       li_length_in_bytes;
  unsigned int       test_li_length_in_bytes;
  unsigned int       test_remaining_size_to_substract;
  unsigned int       test_remaining_num_li_to_substract;
  unsigned int       continue_fill_pdu_with_sdu;
  unsigned int       num_fill_sdu;
  unsigned int       test_num_li;
  unsigned int       fill_num_li;
  mem_block_t        *sdu_in_buffer = NULL;
  unsigned int       data_pdu_size;

  unsigned int       fi_first_byte_pdu_is_first_byte_sdu;
  unsigned int       fi_last_byte_pdu_is_last_byte_sdu;
  unsigned int       fi;
  unsigned int       max_li_overhead;

  if (nb_bytes_to_transmit < 3) {
71
#if TRACE_RLC_UM_SEGMENT
72
73
    LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" NO SEGMENTATION nb_bytes to transmit = %d\n",
          PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
74
          nb_bytes_to_transmit);
gauthier's avatar
gauthier committed
75
#endif
76
77
78
79
80
81
82
    return;
  }

  list_init (&pdus, NULL);    // param string identifying the list is NULL
  pdu_mem_p = NULL;

  // not fine locking
knopp's avatar
mutexes    
knopp committed
83
  RLC_UM_MUTEX_LOCK(&rlc_pP->lock_input_sdus, ctxt_pP, rlc_pP);
84
85
86

  while ((list_get_head(&rlc_pP->input_sdus)) && (nb_bytes_to_transmit > 0)) {

87
#if TRACE_RLC_UM_SEGMENT
88
89
    LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" nb_bytes_to_transmit %d BO %d\n",
          PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
90
91
92
93
94
95
96
97
98
99
100
101
102
103
          nb_bytes_to_transmit,
          rlc_pP->buffer_occupancy);
#endif

    // pdu_p management
    if (!pdu_mem_p) {
      if (rlc_pP->input_sdus.nb_elements <= 1) {
        max_li_overhead = 0;
      } else {
        max_li_overhead = (((rlc_pP->input_sdus.nb_elements - 1) * 3) / 2) + ((rlc_pP->input_sdus.nb_elements - 1) % 2);
      }

      if  (nb_bytes_to_transmit >= (rlc_pP->buffer_occupancy + rlc_pP->tx_header_min_length_in_bytes + max_li_overhead)) {
        data_pdu_size = rlc_pP->buffer_occupancy + rlc_pP->tx_header_min_length_in_bytes + max_li_overhead;
104
#if TRACE_RLC_UM_SEGMENT
105
106
        LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" alloc PDU size %d bytes to contain not all bytes requested by MAC but all BO of RLC@1\n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
107
108
109
110
              data_pdu_size);
#endif
      } else {
        data_pdu_size = nb_bytes_to_transmit;
111
#if TRACE_RLC_UM_SEGMENT
112
113
        LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" alloc PDU size %d bytes to contain all bytes requested by MAC@1\n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
114
115
116
117
118
              data_pdu_size);
#endif
      }

      if (!(pdu_mem_p = get_free_mem_block (data_pdu_size + sizeof(struct mac_tb_req)))) {
119
#if TRACE_RLC_UM_SEGMENT
120
121
        LOG_E(RLC, PROTOCOL_RLC_UM_CTXT_FMT" ERROR COULD NOT GET NEW PDU, EXIT\n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP));
122
#endif
knopp's avatar
mutexes    
knopp committed
123
        RLC_UM_MUTEX_UNLOCK(&rlc_pP->lock_input_sdus);
124
        return;
125
126
      }

127
#if TRACE_RLC_UM_SEGMENT
128
129
      LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" get new PDU %d bytes\n",
            PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
130
131
132
133
134
135
136
137
            data_pdu_size);
#endif
      pdu_remaining_size = data_pdu_size - 2;
      pdu_p        = (rlc_um_pdu_sn_10_t*) (&pdu_mem_p->data[sizeof(struct mac_tb_req)]);
      pdu_tb_req_p = (struct mac_tb_req*) (pdu_mem_p->data);

      memset (pdu_mem_p->data, 0, sizeof (rlc_um_pdu_sn_10_t)+sizeof(struct mac_tb_req));
      li_length_in_bytes = 1;
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

    //----------------------------------------
    // compute how many SDUS can fill the PDU
    //----------------------------------------
    continue_fill_pdu_with_sdu = 1;
    num_fill_sdu               = 0;
    test_num_li                = 0;
    sdu_in_buffer              = list_get_head(&rlc_pP->input_sdus);
    test_pdu_remaining_size    = pdu_remaining_size;
    test_li_length_in_bytes    = 1;
    test_remaining_size_to_substract   = 0;
    test_remaining_num_li_to_substract = 0;


    while ((sdu_in_buffer) && (continue_fill_pdu_with_sdu > 0)) {
      sdu_mngt_p = ((struct rlc_um_tx_sdu_management *) (sdu_in_buffer->data));

      if (sdu_mngt_p->sdu_remaining_size > test_pdu_remaining_size) {
        // no LI
        continue_fill_pdu_with_sdu = 0;
        num_fill_sdu += 1;
        test_pdu_remaining_size = 0;
        test_remaining_size_to_substract = 0;
        test_remaining_num_li_to_substract = 0;
      } else if (sdu_mngt_p->sdu_remaining_size == test_pdu_remaining_size) {
        // fi will indicate end of PDU is end of SDU, no need for LI
        continue_fill_pdu_with_sdu = 0;
        num_fill_sdu += 1;
        test_pdu_remaining_size = 0;
        test_remaining_size_to_substract = 0;
        test_remaining_num_li_to_substract = 0;
      } else if ((sdu_mngt_p->sdu_remaining_size + (test_li_length_in_bytes ^ 3)) == test_pdu_remaining_size ) {
        // no LI
        continue_fill_pdu_with_sdu = 0;
        num_fill_sdu += 1;
        test_pdu_remaining_size = 0;
        test_remaining_size_to_substract = 0;
        test_remaining_num_li_to_substract = 0;
        pdu_remaining_size = pdu_remaining_size - (test_li_length_in_bytes ^ 3);
      } else if ((sdu_mngt_p->sdu_remaining_size + (test_li_length_in_bytes ^ 3)) < test_pdu_remaining_size ) {
        test_num_li += 1;
        num_fill_sdu += 1;
        test_pdu_remaining_size = test_pdu_remaining_size - (sdu_mngt_p->sdu_remaining_size + (test_li_length_in_bytes ^ 3));
        test_remaining_size_to_substract = test_li_length_in_bytes ^ 3;
        test_remaining_num_li_to_substract = 1;
        test_li_length_in_bytes = test_li_length_in_bytes ^ 3;
      } else {
186
#if TRACE_RLC_UM_SEGMENT
187
188
        LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" sdu_mngt_p->sdu_remaining_size=%d test_pdu_remaining_size=%d test_li_length_in_bytes=%d\n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
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
              sdu_mngt_p->sdu_remaining_size,
              test_pdu_remaining_size,
              test_li_length_in_bytes ^ 3);
#endif
        // reduce the size of the PDU
        continue_fill_pdu_with_sdu = 0;
        num_fill_sdu += 1;
        test_pdu_remaining_size = 0;
        test_remaining_size_to_substract = 0;
        test_remaining_num_li_to_substract = 0;
        pdu_remaining_size = pdu_remaining_size - 1;
      }

      sdu_in_buffer = sdu_in_buffer->next;
    }

    if (test_remaining_num_li_to_substract > 0) {
      // there is a LI that is not necessary
      test_num_li = test_num_li - 1;
      pdu_remaining_size = pdu_remaining_size - test_remaining_size_to_substract;
    }

    //----------------------------------------
    // Do the real filling of the pdu_p
    //----------------------------------------
214
#if TRACE_RLC_UM_SEGMENT
215
216
    LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" data shift %d Bytes num_li %d\n",
          PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
217
218
          ((test_num_li*3) +1) >> 1,
          test_num_li);
gauthier's avatar
gauthier committed
219
220
#endif

221
222
223
224
225
226
227
    data = ((char*)(&pdu_p->data[((test_num_li*3) +1) >> 1]));
    e_li_p = (rlc_um_e_li_t*)(pdu_p->data);
    continue_fill_pdu_with_sdu          = 1;
    li_length_in_bytes                  = 1;
    fill_num_li                         = 0;
    fi_first_byte_pdu_is_first_byte_sdu = 0;
    fi_last_byte_pdu_is_last_byte_sdu   = 0;
228

229
230
231
232
233
234
235
236
237
238
239
240
    sdu_in_buffer = list_get_head(&rlc_pP->input_sdus);

    if (
      ((struct rlc_um_tx_sdu_management *) (sdu_in_buffer->data))->sdu_remaining_size ==
      ((struct rlc_um_tx_sdu_management *) (sdu_in_buffer->data))->sdu_size) {
      fi_first_byte_pdu_is_first_byte_sdu = 1;
    }

    while ((sdu_in_buffer) && (continue_fill_pdu_with_sdu > 0)) {
      sdu_mngt_p = ((struct rlc_um_tx_sdu_management *) (sdu_in_buffer->data));

      if (sdu_mngt_p->sdu_segmented_size == 0) {
241
#if TRACE_RLC_UM_SEGMENT
242
243
        LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" GET NEW SDU %p AVAILABLE SIZE %d Bytes\n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
244
245
246
247
              sdu_mngt_p,
              sdu_mngt_p->sdu_remaining_size);
#endif
      } else {
248
#if TRACE_RLC_UM_SEGMENT
249
250
        LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" GET AGAIN SDU %p REMAINING AVAILABLE SIZE %d Bytes / %d Bytes \n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
251
252
253
254
255
256
257
258
259
              sdu_mngt_p,
              sdu_mngt_p->sdu_remaining_size,
              sdu_mngt_p->sdu_size);
#endif
      }

      data_sdu_p = (char *) &(sdu_in_buffer->data[sizeof (struct rlc_um_tx_sdu_management) + sdu_mngt_p->sdu_segmented_size]);

      if (sdu_mngt_p->sdu_remaining_size > pdu_remaining_size) {
260
#if TRACE_RLC_UM_SEGMENT
261
262
        LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" Filling all remaining PDU with %d bytes\n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
263
              pdu_remaining_size);
264
265
        LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" pdu_mem_p %p pdu_mem_p->data %p pdu_p %p pdu_p->data %p data %p data_sdu_p %p pdu_remaining_size %d\n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
              pdu_mem_p,
              pdu_mem_p->data,
              pdu_p,
              pdu_p->data,
              data,
              data_sdu_p,
              pdu_remaining_size);
#endif

        memcpy(data, data_sdu_p, pdu_remaining_size);
        sdu_mngt_p->sdu_remaining_size = sdu_mngt_p->sdu_remaining_size - pdu_remaining_size;
        sdu_mngt_p->sdu_segmented_size = sdu_mngt_p->sdu_segmented_size + pdu_remaining_size;
        fi_last_byte_pdu_is_last_byte_sdu = 0;
        // no LI
        rlc_pP->buffer_occupancy -= pdu_remaining_size;
        continue_fill_pdu_with_sdu = 0;
        pdu_remaining_size = 0;
      } else if (sdu_mngt_p->sdu_remaining_size == pdu_remaining_size) {
284
#if TRACE_RLC_UM_SEGMENT
285
286
        LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" Exactly Filling remaining PDU with %d remaining bytes of SDU\n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
              pdu_remaining_size);
#endif
        memcpy(data, data_sdu_p, pdu_remaining_size);

        // free SDU
        rlc_pP->buffer_occupancy -= sdu_mngt_p->sdu_remaining_size;
        sdu_in_buffer = list_remove_head(&rlc_pP->input_sdus);
        free_mem_block (sdu_in_buffer);
        sdu_in_buffer = list_get_head(&rlc_pP->input_sdus);
        sdu_mngt_p    = NULL;


        fi_last_byte_pdu_is_last_byte_sdu = 1;
        // fi will indicate end of PDU is end of SDU, no need for LI
        continue_fill_pdu_with_sdu = 0;
        pdu_remaining_size = 0;
      } else if ((sdu_mngt_p->sdu_remaining_size + (li_length_in_bytes ^ 3)) < pdu_remaining_size ) {
304
#if TRACE_RLC_UM_SEGMENT
305
306
        LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" Filling  PDU with %d all remaining bytes of SDU\n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
              sdu_mngt_p->sdu_remaining_size);
#endif
        memcpy(data, data_sdu_p, sdu_mngt_p->sdu_remaining_size);
        data = &data[sdu_mngt_p->sdu_remaining_size];
        li_length_in_bytes = li_length_in_bytes ^ 3;
        fill_num_li += 1;

        if (li_length_in_bytes  == 2) {
          if (fill_num_li == test_num_li) {
            //e_li_p->e1  = 0;
            e_li_p->b1 = 0;
          } else {
            //e_li_p->e1  = 1;
            e_li_p->b1 =  0x80;
          }

          //e_li_p->li1 = sdu_mngt_p->sdu_remaining_size;
          e_li_p->b1 = e_li_p->b1 | (sdu_mngt_p->sdu_remaining_size >> 4);
          e_li_p->b2 = sdu_mngt_p->sdu_remaining_size << 4;
326
#if TRACE_RLC_UM_SEGMENT
327
328
          LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" set e_li_p->b1=%02X set e_li_p->b2=%02X fill_num_li=%d test_num_li=%d\n",
                PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
329
330
331
332
333
334
335
336
337
338
                e_li_p->b1,
                e_li_p->b2,
                fill_num_li,
                test_num_li);
#endif
        } else {
          if (fill_num_li != test_num_li) {
            //e_li_p->e2  = 1;
            e_li_p->b2  = e_li_p->b2 | 0x08;
          }
339

340
341
342
          //e_li_p->li2 = sdu_mngt_p->sdu_remaining_size;
          e_li_p->b2 = e_li_p->b2 | (sdu_mngt_p->sdu_remaining_size >> 8);
          e_li_p->b3 = sdu_mngt_p->sdu_remaining_size & 0xFF;
343
#if TRACE_RLC_UM_SEGMENT
344
345
          LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" set e_li_p->b2=%02X set e_li_p->b3=%02X fill_num_li=%d test_num_li=%d\n",
                PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
346
347
348
                e_li_p->b2,
                e_li_p->b3,
                fill_num_li,
gauthier's avatar
gauthier committed
349
350
                test_num_li);
#endif
351
352
          e_li_p++;
        }
353

354
        pdu_remaining_size = pdu_remaining_size - (sdu_mngt_p->sdu_remaining_size + li_length_in_bytes);
355

356
357
358
359
        // free SDU
        rlc_pP->buffer_occupancy -= sdu_mngt_p->sdu_remaining_size;
        sdu_in_buffer = list_remove_head(&rlc_pP->input_sdus);
        free_mem_block (sdu_in_buffer);
360
        sdu_in_buffer = list_get_head(&rlc_pP->input_sdus);
361
362
363
        sdu_mngt_p    = NULL;

      } else {
364
#if TRACE_RLC_UM_SEGMENT
365
366
        LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" Filling  PDU with %d all remaining bytes of SDU and reduce TB size by %d bytes\n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
367
368
              sdu_mngt_p->sdu_remaining_size,
              pdu_remaining_size - sdu_mngt_p->sdu_remaining_size);
gauthier's avatar
gauthier committed
369
#endif
370
371
#if USER_MODE
#if !EXMIMO
372
373
374
375
376
377
378
379
380
381
382
383
384
385
        assert(1!=1);
#endif
#endif
        memcpy(data, data_sdu_p, sdu_mngt_p->sdu_remaining_size);
        // reduce the size of the PDU
        continue_fill_pdu_with_sdu = 0;
        fi_last_byte_pdu_is_last_byte_sdu = 1;
        pdu_remaining_size = pdu_remaining_size - sdu_mngt_p->sdu_remaining_size;
        // free SDU
        rlc_pP->buffer_occupancy -= sdu_mngt_p->sdu_remaining_size;
        sdu_in_buffer = list_remove_head(&rlc_pP->input_sdus);
        free_mem_block (sdu_in_buffer);
        sdu_in_buffer = list_get_head(&rlc_pP->input_sdus);
        sdu_mngt_p    = NULL;
386

387
388
      }
    }
389

390
391
392
393
394
395
396
397
398
399
    // set framing info
    if (fi_first_byte_pdu_is_first_byte_sdu) {
      fi = 0;
    } else {
      fi = 2;
    }

    if (!fi_last_byte_pdu_is_last_byte_sdu) {
      fi = fi + 1;
    }
400

401
402
403
404
405
406
    pdu_p->b1 =  (fi << 3); //pdu_p->b1 |

    // set fist e bit
    if (fill_num_li > 0) {
      pdu_p->b1 = pdu_p->b1 | 0x04;
    }
407

408
409
410
411
412
413
414
    pdu_p->b1 = pdu_p->b1 | ((rlc_pP->vt_us >> 8) & 0x03);
    pdu_p->b2 = rlc_pP->vt_us & 0xFF;
    rlc_pP->vt_us = rlc_pP->vt_us+1;

    pdu_tb_req_p->data_ptr        = (unsigned char*)pdu_p;
    pdu_tb_req_p->tb_size = data_pdu_size - pdu_remaining_size;
    list_add_tail_eurecom (pdu_mem_p, &rlc_pP->pdus_to_mac_layer);
415
#if TRACE_RLC_PAYLOAD
416
    rlc_util_print_hex_octets(RLC, pdu_mem_p->data, data_pdu_size);
gauthier's avatar
   
gauthier committed
417
#endif
418
419
420
    AssertFatal( pdu_tb_req_p->tb_size > 0 , "SEGMENT10: FINAL RLC UM PDU LENGTH %d", pdu_tb_req_p->tb_size);
    pdu_p = NULL;
    pdu_mem_p = NULL;
421

422
423
424
425
    //nb_bytes_to_transmit = nb_bytes_to_transmit - data_pdu_size;
    nb_bytes_to_transmit = 0; // 1 PDU only
  }

knopp's avatar
mutexes    
knopp committed
426
  RLC_UM_MUTEX_UNLOCK(&rlc_pP->lock_input_sdus);
427
428
429
}
//-----------------------------------------------------------------------------
void
knopp's avatar
mutexes    
knopp committed
430
rlc_um_segment_5 (const protocol_ctxt_t* const ctxt_pP, rlc_um_entity_t *rlc_pP)
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
  list_t              pdus;
  signed int          pdu_remaining_size      = 0;
  signed int          test_pdu_remaining_size = 0;

  int                 nb_bytes_to_transmit = rlc_pP->nb_bytes_requested_by_mac;
  rlc_um_pdu_sn_5_t  *pdu_p                = NULL;
  struct mac_tb_req  *pdu_tb_req_p         = NULL;
  mem_block_t        *pdu_mem_p            = NULL;
  char               *data                 = NULL;
  char               *data_sdu_p           = NULL;
  rlc_um_e_li_t      *e_li_p               = NULL;
  struct rlc_um_tx_sdu_management *sdu_mngt_p           = NULL;
  unsigned int       li_length_in_bytes                 = 0;
  unsigned int       test_li_length_in_bytes            = 0;
  unsigned int       test_remaining_size_to_substract   = 0;
  unsigned int       test_remaining_num_li_to_substract = 0;
  unsigned int       continue_fill_pdu_with_sdu         = 0;
  unsigned int       num_fill_sdu                       = 0;
  unsigned int       test_num_li                        = 0;
  unsigned int       fill_num_li                        = 0;
  mem_block_t        *sdu_in_buffer                     = NULL;
  unsigned int       data_pdu_size                      = 0;

  unsigned int       fi_first_byte_pdu_is_first_byte_sdu = 0;
  unsigned int       fi_last_byte_pdu_is_last_byte_sdu   = 0;
  unsigned int       fi                                  = 0;
  unsigned int       max_li_overhead                     = 0;

  if (nb_bytes_to_transmit < 2) {
461
#if TRACE_RLC_UM_SEGMENT
462
463
    LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" NO SEGMENTATION5 nb_bytes to transmit = %d\n",
          PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
464
          nb_bytes_to_transmit);
gauthier's avatar
gauthier committed
465
#endif
466
467
468
469
470
471
    return;
  }

  list_init (&pdus, NULL);    // param string identifying the list is NULL
  pdu_mem_p = NULL;

knopp's avatar
mutexes    
knopp committed
472
  RLC_UM_MUTEX_LOCK(&rlc_pP->lock_input_sdus, ctxt_pP, rlc_pP);
473
474

  while ((list_get_head(&rlc_pP->input_sdus)) && (nb_bytes_to_transmit > 0)) {
475
#if TRACE_RLC_UM_SEGMENT
476
477
    LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT"nb_bytes_to_transmit %d BO %d\n",
          PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
478
479
480
481
482
483
484
485
486
487
488
489
490
491
          nb_bytes_to_transmit,
          rlc_pP->buffer_occupancy);
#endif

    // pdu_p management
    if (!pdu_mem_p) {
      if (rlc_pP->input_sdus.nb_elements <= 1) {
        max_li_overhead = 0;
      } else {
        max_li_overhead = (((rlc_pP->input_sdus.nb_elements - 1) * 3) / 2) + ((rlc_pP->input_sdus.nb_elements - 1) % 2);
      }

      if  (nb_bytes_to_transmit >= (rlc_pP->buffer_occupancy + rlc_pP->tx_header_min_length_in_bytes + max_li_overhead)) {
        data_pdu_size = rlc_pP->buffer_occupancy + rlc_pP->tx_header_min_length_in_bytes + max_li_overhead;
492
#if TRACE_RLC_UM_SEGMENT
493
494
        LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" alloc PDU size %d bytes to contain not all bytes requested by MAC but all BO of RLC@1\n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
495
496
497
498
              data_pdu_size);
#endif
      } else {
        data_pdu_size = nb_bytes_to_transmit;
499
#if TRACE_RLC_UM_SEGMENT
500
501
        LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" alloc PDU size %d bytes to contain all bytes requested by MAC@1\n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
502
503
504
505
506
              data_pdu_size);
#endif
      }

      if (!(pdu_mem_p = get_free_mem_block (data_pdu_size + sizeof(struct mac_tb_req)))) {
507
#if TRACE_RLC_UM_SEGMENT
508
509
        LOG_E(RLC, PROTOCOL_RLC_UM_CTXT_FMT" ERROR COULD NOT GET NEW PDU, EXIT\n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP));
510
#endif
knopp's avatar
mutexes    
knopp committed
511
        RLC_UM_MUTEX_UNLOCK(&rlc_pP->lock_input_sdus);
512
        return;
513
514
      }

515
#if TRACE_RLC_UM_SEGMENT
516
517
      LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" get new PDU %d bytes\n",
            PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
518
            data_pdu_size);
gauthier's avatar
gauthier committed
519
#endif
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
      pdu_remaining_size = data_pdu_size - 1;
      pdu_p        = (rlc_um_pdu_sn_5_t*) (&pdu_mem_p->data[sizeof(struct mac_tb_req)]);
      pdu_tb_req_p = (struct mac_tb_req*) (pdu_mem_p->data);

      memset (pdu_mem_p->data, 0, sizeof (rlc_um_pdu_sn_5_t)+sizeof(struct mac_tb_req));
      li_length_in_bytes = 1;
    }

    //----------------------------------------
    // compute how many SDUS can fill the PDU
    //----------------------------------------
    continue_fill_pdu_with_sdu = 1;
    num_fill_sdu               = 0;
    test_num_li                = 0;
    sdu_in_buffer              = list_get_head(&rlc_pP->input_sdus);
    test_pdu_remaining_size    = pdu_remaining_size;
    test_li_length_in_bytes    = 1;
    test_remaining_size_to_substract   = 0;
    test_remaining_num_li_to_substract = 0;


    while ((sdu_in_buffer) && (continue_fill_pdu_with_sdu > 0)) {
      sdu_mngt_p = ((struct rlc_um_tx_sdu_management *) (sdu_in_buffer->data));

      if (sdu_mngt_p->sdu_remaining_size > test_pdu_remaining_size) {
        // no LI
        continue_fill_pdu_with_sdu = 0;
        num_fill_sdu += 1;
        test_pdu_remaining_size = 0;
        test_remaining_size_to_substract = 0;
        test_remaining_num_li_to_substract = 0;
      } else if (sdu_mngt_p->sdu_remaining_size == test_pdu_remaining_size) {
        // fi will indicate end of PDU is end of SDU, no need for LI
        continue_fill_pdu_with_sdu = 0;
        num_fill_sdu += 1;
        test_pdu_remaining_size = 0;
        test_remaining_size_to_substract = 0;
        test_remaining_num_li_to_substract = 0;
      } else if ((sdu_mngt_p->sdu_remaining_size + (test_li_length_in_bytes ^ 3)) == test_pdu_remaining_size ) {
        // no LI
        continue_fill_pdu_with_sdu = 0;
        num_fill_sdu += 1;
        test_pdu_remaining_size = 0;
        test_remaining_size_to_substract = 0;
        test_remaining_num_li_to_substract = 0;
        pdu_remaining_size = pdu_remaining_size - (test_li_length_in_bytes ^ 3);
        data_pdu_size -=  (test_li_length_in_bytes ^ 3);//modifier pour duy
      } else if ((sdu_mngt_p->sdu_remaining_size + (test_li_length_in_bytes ^ 3)) < test_pdu_remaining_size ) {
        test_num_li += 1;
        num_fill_sdu += 1;
        test_pdu_remaining_size = test_pdu_remaining_size - (sdu_mngt_p->sdu_remaining_size + (test_li_length_in_bytes ^ 3));
        test_remaining_size_to_substract = test_li_length_in_bytes ^ 3;
        test_remaining_num_li_to_substract = 1;
        test_li_length_in_bytes = test_li_length_in_bytes ^ 3;
      } else {
575
#if TRACE_RLC_UM_SEGMENT
576
577
        LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" sdu_mngt_p->sdu_remaining_size=%d test_pdu_remaining_size=%d test_li_length_in_bytes=%d\n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
578
579
580
581
582
583
584
585
586
587
588
589
590
              sdu_mngt_p->sdu_remaining_size,
              test_pdu_remaining_size,
              test_li_length_in_bytes ^ 3);
#endif
        // reduce the size of the PDU
        continue_fill_pdu_with_sdu = 0;
        num_fill_sdu += 1;
        test_pdu_remaining_size = 0;
        test_remaining_size_to_substract = 0;
        test_remaining_num_li_to_substract = 0;
        pdu_remaining_size = pdu_remaining_size - 1;
        data_pdu_size -= 1;//modifier pour duy
      }
gauthier's avatar
gauthier committed
591

592
593
594
595
596
597
598
599
600
601
602
603
      sdu_in_buffer = sdu_in_buffer->next;
    }

    if (test_remaining_num_li_to_substract > 0) {
      // there is a LI that is not necessary
      test_num_li = test_num_li - 1;
      pdu_remaining_size = pdu_remaining_size - test_remaining_size_to_substract;
    }

    //----------------------------------------
    // Do the real filling of the pdu_p
    //----------------------------------------
604
#if TRACE_RLC_UM_SEGMENT
605
606
    LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" data shift %d Bytes num_li %d\n",
          PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
          ((test_num_li*3) +1) >> 1,
          test_num_li);
#endif

    data = ((char*)(&pdu_p->data[((test_num_li*3) +1) >> 1]));
    e_li_p = (rlc_um_e_li_t*)(pdu_p->data);
    continue_fill_pdu_with_sdu          = 1;
    li_length_in_bytes                  = 1;
    fill_num_li                         = 0;
    fi_first_byte_pdu_is_first_byte_sdu = 0;
    fi_last_byte_pdu_is_last_byte_sdu   = 0;

    sdu_in_buffer = list_get_head(&rlc_pP->input_sdus);

    if (
      ((struct rlc_um_tx_sdu_management *) (sdu_in_buffer->data))->sdu_remaining_size ==
      ((struct rlc_um_tx_sdu_management *) (sdu_in_buffer->data))->sdu_size) {
      fi_first_byte_pdu_is_first_byte_sdu = 1;
    }

    while ((sdu_in_buffer) && (continue_fill_pdu_with_sdu > 0)) {
      sdu_mngt_p = ((struct rlc_um_tx_sdu_management *) (sdu_in_buffer->data));

      if (sdu_mngt_p->sdu_segmented_size == 0) {
631
#if TRACE_RLC_UM_SEGMENT
632
633
        LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" GET NEW SDU %p AVAILABLE SIZE %d Bytes\n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
634
635
636
637
              sdu_mngt_p,
              sdu_mngt_p->sdu_remaining_size);
#endif
      } else {
638
#if TRACE_RLC_UM_SEGMENT
639
640
        LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" GET AGAIN SDU %p REMAINING AVAILABLE SIZE %d Bytes / %d Bytes \n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
641
642
643
644
645
646
647
648
649
              sdu_mngt_p,
              sdu_mngt_p->sdu_remaining_size,
              sdu_mngt_p->sdu_size);
#endif
      }

      data_sdu_p = (char*) &(sdu_in_buffer->data[sizeof (struct rlc_um_tx_sdu_management) + sdu_mngt_p->sdu_segmented_size]);

      if (sdu_mngt_p->sdu_remaining_size > pdu_remaining_size) {
650
#if TRACE_RLC_UM_SEGMENT
651
652
        LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" Filling all remaining PDU with %d bytes\n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
653
              pdu_remaining_size);
654
655
        LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" pdu_mem_p %p pdu_p %p pdu_p->data %p data %p data_sdu_p %p pdu_remaining_size %d\n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
              pdu_mem_p,
              pdu_p,
              pdu_p->data,
              data,
              data_sdu_p,
              pdu_remaining_size);
#endif

        memcpy(data, data_sdu_p, pdu_remaining_size);
        sdu_mngt_p->sdu_remaining_size = sdu_mngt_p->sdu_remaining_size - pdu_remaining_size;
        sdu_mngt_p->sdu_segmented_size = sdu_mngt_p->sdu_segmented_size + pdu_remaining_size;
        fi_last_byte_pdu_is_last_byte_sdu = 0;
        // no LI
        rlc_pP->buffer_occupancy -= pdu_remaining_size;
        continue_fill_pdu_with_sdu = 0;
        pdu_remaining_size = 0;
      } else if (sdu_mngt_p->sdu_remaining_size == pdu_remaining_size) {
673
#if TRACE_RLC_UM_SEGMENT
674
675
        LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" Exactly Filling remaining PDU with %d remaining bytes of SDU\n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
              pdu_remaining_size);
#endif
        memcpy(data, data_sdu_p, pdu_remaining_size);
        // free SDU
        rlc_pP->buffer_occupancy -= sdu_mngt_p->sdu_remaining_size;
        sdu_in_buffer = list_remove_head(&rlc_pP->input_sdus);
        free_mem_block (sdu_in_buffer);
        sdu_in_buffer = list_get_head(&rlc_pP->input_sdus);
        sdu_mngt_p    = NULL;

        fi_last_byte_pdu_is_last_byte_sdu = 1;
        // fi will indicate end of PDU is end of SDU, no need for LI
        continue_fill_pdu_with_sdu = 0;
        pdu_remaining_size = 0;
      } else if ((sdu_mngt_p->sdu_remaining_size + (li_length_in_bytes ^ 3)) < pdu_remaining_size ) {
691
#if TRACE_RLC_UM_SEGMENT
692
693
        LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" Filling  PDU with %d all remaining bytes of SDU\n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
              sdu_mngt_p->sdu_remaining_size);
#endif
        memcpy(data, data_sdu_p, sdu_mngt_p->sdu_remaining_size);
        data = &data[sdu_mngt_p->sdu_remaining_size];
        li_length_in_bytes = li_length_in_bytes ^ 3;
        fill_num_li += 1;

        if (li_length_in_bytes  == 2) {
          if (fill_num_li == test_num_li) {
            //e_li_p->e1  = 0;
            e_li_p->b1 = 0;
          } else {
            //e_li_p->e1  = 1;
            e_li_p->b1 =  0x80;
          }

          //e_li_p->li1 = sdu_mngt_p->sdu_remaining_size;
          e_li_p->b1 = e_li_p->b1 | (sdu_mngt_p->sdu_remaining_size >> 4);
          e_li_p->b2 = sdu_mngt_p->sdu_remaining_size << 4;
713
#if TRACE_RLC_UM_SEGMENT
714
715
          LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" set e_li_p->b1=%02X set e_li_p->b2=%02X fill_num_li=%d test_num_li=%d\n",
                PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
716
717
718
719
720
721
722
723
724
725
                e_li_p->b1,
                e_li_p->b2,
                fill_num_li,
                test_num_li);
#endif
        } else {
          if (fill_num_li != test_num_li) {
            //e_li_p->e2  = 1;
            e_li_p->b2  = e_li_p->b2 | 0x08;
          }
726

727
728
729
          //e_li_p->li2 = sdu_mngt_p->sdu_remaining_size;
          e_li_p->b2 = e_li_p->b2 | (sdu_mngt_p->sdu_remaining_size >> 8);
          e_li_p->b3 = sdu_mngt_p->sdu_remaining_size & 0xFF;
730
#if TRACE_RLC_UM_SEGMENT
731
732
          LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" set e_li_p->b2=%02X set e_li_p->b3=%02X fill_num_li=%d test_num_li=%d\n",
                PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
733
734
735
                e_li_p->b2,
                e_li_p->b3,
                fill_num_li,
gauthier's avatar
gauthier committed
736
737
                test_num_li);
#endif
738
739
          e_li_p++;
        }
740

741
        pdu_remaining_size = pdu_remaining_size - (sdu_mngt_p->sdu_remaining_size + li_length_in_bytes);
742

743
744
745
746
        // free SDU
        rlc_pP->buffer_occupancy -= sdu_mngt_p->sdu_remaining_size;
        sdu_in_buffer = list_remove_head(&rlc_pP->input_sdus);
        free_mem_block (sdu_in_buffer);
747
        sdu_in_buffer = list_get_head(&rlc_pP->input_sdus);
748
749
750
        sdu_mngt_p    = NULL;

      } else {
751
#if TRACE_RLC_UM_SEGMENT
752
753
        LOG_D(RLC, PROTOCOL_RLC_UM_CTXT_FMT" Filling  PDU with %d all remaining bytes of SDU and reduce TB size by %d bytes\n",
              PROTOCOL_RLC_UM_CTXT_ARGS(ctxt_pP,rlc_pP),
754
755
              sdu_mngt_p->sdu_remaining_size,
              pdu_remaining_size - sdu_mngt_p->sdu_remaining_size);
gauthier's avatar
gauthier committed
756
#endif
757
#if USER_MODE
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
        assert(1!=1);
#endif
        memcpy(data, data_sdu_p, sdu_mngt_p->sdu_remaining_size);
        // reduce the size of the PDU
        continue_fill_pdu_with_sdu = 0;
        fi_last_byte_pdu_is_last_byte_sdu = 1;
        pdu_remaining_size = pdu_remaining_size - sdu_mngt_p->sdu_remaining_size;
        // free SDU
        rlc_pP->buffer_occupancy -= sdu_mngt_p->sdu_remaining_size;
        sdu_in_buffer = list_remove_head(&rlc_pP->input_sdus);
        free_mem_block (sdu_in_buffer);
        sdu_in_buffer = list_get_head(&rlc_pP->input_sdus);
        sdu_mngt_p    = NULL;
      }
    }
773

774
775
776
777
778
779
    // set framing info
    if (fi_first_byte_pdu_is_first_byte_sdu) {
      fi = 0;
    } else {
      fi = 2;
    }
780

781
782
783
784
785
786
787
788
789
790
    if (!fi_last_byte_pdu_is_last_byte_sdu) {
      fi = fi + 1;
    }

    pdu_p->b1 =  (fi << 6); //pdu_p->b1 |

    // set fist e bit
    if (fill_num_li > 0) {
      pdu_p->b1 = pdu_p->b1 | 0x20;
    }
791

792
793
794
795
796
797
    pdu_p->b1 = pdu_p->b1 | (rlc_pP->vt_us & 0x1F);
    rlc_pP->vt_us = rlc_pP->vt_us+1;

    pdu_tb_req_p->data_ptr        = (unsigned char*)pdu_p;
    pdu_tb_req_p->tb_size         = data_pdu_size - pdu_remaining_size;
    list_add_tail_eurecom (pdu_mem_p, &rlc_pP->pdus_to_mac_layer);
798
#if TRACE_RLC_PAYLOAD
799
    rlc_util_print_hex_octets(RLC, (unsigned char*)pdu_mem_p->data, data_pdu_size);
gauthier's avatar
   
gauthier committed
800
#endif
801
802
803
804
    AssertFatal( pdu_tb_req_p->tb_size > 0 , "SEGMENT5: FINAL RLC UM PDU LENGTH %d", pdu_tb_req_p->tb_size);

    pdu_p = NULL;
    pdu_mem_p = NULL;
805

806
807
808
    //nb_bytes_to_transmit = nb_bytes_to_transmit - data_pdu_size;
    nb_bytes_to_transmit = 0; // 1 PDU only
  }
809

knopp's avatar
mutexes    
knopp committed
810
  RLC_UM_MUTEX_UNLOCK(&rlc_pP->lock_input_sdus);
811
812
}