rlc_am_retransmit.c 59.2 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_AM_MODULE 1
#define RLC_AM_RETRANSMIT_C 1
24 25 26 27 28 29 30
//-----------------------------------------------------------------------------
//#include "rtos_header.h"
//-----------------------------------------------------------------------------
#include "rlc_am.h"
#include "rlc.h"
#include "LAYER2/MAC/extern.h"
#include "UTIL/LOG/log.h"
31
#include "msc.h"
32
//-----------------------------------------------------------------------------
33
boolean_t rlc_am_nack_pdu (
34 35 36
  const protocol_ctxt_t* const  ctxt_pP,
  rlc_am_entity_t *const rlc_pP,
  const rlc_sn_t snP,
37 38 39
  const rlc_sn_t prev_nack_snP,
  sdu_size_t so_startP,
  sdu_size_t so_endP)
40
{
41 42 43 44 45 46 47 48 49 50 51
  // 5.2.1 Retransmission
  // ...
  // When an AMD PDU or a portion of an AMD PDU is considered for retransmission, the transmitting side of the AM
  // RLC entity shall:
  //     - if the AMD PDU is considered for retransmission for the first time:
  //         - set the RETX_COUNT associated with the AMD PDU to zero;
  //     - else, if it (the AMD PDU or the portion of the AMD PDU that is considered for retransmission) is not pending
  //            for retransmission already, or a portion of it is not pending for retransmission already:
  //         - increment the RETX_COUNT;
  //     - if RETX_COUNT = maxRetxThreshold:
  //         - indicate to upper layers that max retransmission has been reached.
52 53


54 55
  mem_block_t* mb_p         = rlc_pP->tx_data_pdu_buffer[snP % RLC_AM_WINDOW_SIZE].mem_block;
  rlc_am_tx_data_pdu_management_t *tx_data_pdu_buffer_p = &rlc_pP->tx_data_pdu_buffer[snP % RLC_AM_WINDOW_SIZE];
56 57
  //int          pdu_sdu_index;
  //int          sdu_index;
58 59 60
  boolean_t status = TRUE;
  boolean_t retx_count_increment = FALSE;
  sdu_size_t pdu_data_to_retx = 0;
gauthier's avatar
gauthier committed
61

62 63
  if (mb_p != NULL) {
    assert(so_startP <= so_endP);
gauthier's avatar
gauthier committed
64

65 66 67 68 69 70 71 72 73
    // Handle full PDU NACK first
    if ((so_startP == 0) && (so_endP == 0x7FFF)) {
    	if ((prev_nack_snP != snP) && (tx_data_pdu_buffer_p->flags.ack == 0) && (tx_data_pdu_buffer_p->flags.max_retransmit == 0)) {
    		pdu_data_to_retx = tx_data_pdu_buffer_p->payload_size;
            /* Increment VtReTxNext if this is the first NACK or if some segments have already been transmitted */
            if ((tx_data_pdu_buffer_p->flags.retransmit == 0) || (tx_data_pdu_buffer_p->nack_so_start))
            {
            	retx_count_increment = TRUE;
            }
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
            tx_data_pdu_buffer_p->nack_so_start = 0;
            tx_data_pdu_buffer_p->num_holes     = 0;
            tx_data_pdu_buffer_p->retx_hole_index = 0;
            tx_data_pdu_buffer_p->nack_so_stop  = tx_data_pdu_buffer_p->payload_size - 1;
        #if TRACE_RLC_AM_HOLE
            LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[HOLE] SN %04d GLOBAL NACK 0->%05d\n",
                  PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
                  snP,
                  so_stopP);
        #endif
            assert(tx_data_pdu_buffer_p->nack_so_start < tx_data_pdu_buffer_p->payload_size);
    	}
    	else {
    		status = FALSE;
    	}
      }
    else if (tx_data_pdu_buffer_p->flags.max_retransmit == 0) {
    	// Handle Segment offset
		if (so_endP == 0x7FFF) {
			so_endP = tx_data_pdu_buffer_p->payload_size - 1;
		}

		// Check consistency
		if ((so_startP < so_endP) && (so_endP < tx_data_pdu_buffer_p->payload_size)) {
	    	if (prev_nack_snP != snP) {
	    		/* New NACK_SN with SO */
                /* check whether a new segment is to be placed in Retransmission Buffer, then increment vrReTx */
                if ((tx_data_pdu_buffer_p->flags.retransmit == 0) || (so_startP < tx_data_pdu_buffer_p->nack_so_start)) {
                	retx_count_increment = TRUE;
                }

	            tx_data_pdu_buffer_p->num_holes     = 1;
	            tx_data_pdu_buffer_p->retx_hole_index = 0;
	            tx_data_pdu_buffer_p->hole_so_start[0] = so_startP;
	            tx_data_pdu_buffer_p->hole_so_stop[0] = so_endP;
	            tx_data_pdu_buffer_p->nack_so_start = so_startP;
	            tx_data_pdu_buffer_p->nack_so_stop = so_endP;
	            pdu_data_to_retx = so_endP - so_startP + 1;

	    	}
	    	else if ((tx_data_pdu_buffer_p->num_holes) && (tx_data_pdu_buffer_p->num_holes < RLC_AM_MAX_HOLES_REPORT_PER_PDU)) {
	    		/* New SOStart/SOEnd for the same NACK_SN than before */
	    		/* check discontinuity */
	    		if (so_startP > tx_data_pdu_buffer_p->hole_so_stop[tx_data_pdu_buffer_p->num_holes - 1]) {
		            tx_data_pdu_buffer_p->hole_so_start[tx_data_pdu_buffer_p->num_holes] = so_startP;
		            tx_data_pdu_buffer_p->hole_so_stop[tx_data_pdu_buffer_p->num_holes] = so_endP;
		            tx_data_pdu_buffer_p->nack_so_stop = so_endP;
		            tx_data_pdu_buffer_p->num_holes ++;
		            pdu_data_to_retx = so_endP - so_startP + 1;
	    		}
	    		else {
	    			status = FALSE;
	    		}
	    	}
	    	else {
	    		status = FALSE;
	    	}
		}
		else {
			status = FALSE;
		}
    }
    else {
    	status = FALSE;
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
    if (status) {
    	tx_data_pdu_buffer_p->flags.nack = 1;
    	if ((retx_count_increment) && (tx_data_pdu_buffer_p->retx_count == tx_data_pdu_buffer_p->retx_count_next)) {
    		tx_data_pdu_buffer_p->retx_count_next ++;
    	}
    	if (tx_data_pdu_buffer_p->flags.retransmit == 1) {
            if (prev_nack_snP != snP) {
                /* if first process of this NACK_SN and data already pending for retx */
            	rlc_pP->retrans_num_bytes_to_retransmit += (pdu_data_to_retx - tx_data_pdu_buffer_p->retx_payload_size);
            	tx_data_pdu_buffer_p->retx_payload_size = pdu_data_to_retx;
            }
            else if (tx_data_pdu_buffer_p->num_holes > 1) {
                /* Segment case : SOStart and SOEnd already received for same NACK_SN */
                /* filter case where a NACK_SN is received twice with SO first time and no SO second time */
            	rlc_pP->retrans_num_bytes_to_retransmit += pdu_data_to_retx;
            	tx_data_pdu_buffer_p->retx_payload_size += pdu_data_to_retx;
            }
    	}
        else {
        	tx_data_pdu_buffer_p->flags.retransmit = 1;
        	rlc_pP->retrans_num_bytes_to_retransmit += pdu_data_to_retx;
        	tx_data_pdu_buffer_p->retx_payload_size = pdu_data_to_retx;
        	rlc_pP->retrans_num_pdus ++;
        }
165 166
    }

167 168 169 170 171
    /* TODO: Move this part in UL SCH processing */
#if 0
    if (rlc_pP->tx_data_pdu_buffer[snP].retx_count >= rlc_pP->max_retx_threshold) {
      for (pdu_sdu_index = 0; pdu_sdu_index < rlc_pP->tx_data_pdu_buffer[snP].nb_sdus; pdu_sdu_index++) {
        sdu_index = rlc_pP->tx_data_pdu_buffer[snP].sdus_index[pdu_sdu_index];
172 173 174 175 176
        assert(pdu_sdu_index < RLC_AM_MAX_SDU_IN_PDU);
        assert(sdu_index < RLC_AM_SDU_CONTROL_BUFFER_SIZE);
        rlc_pP->input_sdus[sdu_index].nb_pdus_ack += 1;

        if (rlc_pP->input_sdus[sdu_index].nb_pdus_ack == rlc_pP->input_sdus[sdu_index].nb_pdus) {
177
#if TEST_RLC_AM
178 179 180 181 182 183
          rlc_am_v9_3_0_test_data_conf (rlc_pP->module_id, rlc_pP->rb_id, rlc_pP->input_sdus[sdu_index].mui, RLC_SDU_CONFIRM_NO);
#else
          rlc_data_conf(ctxt_pP, rlc_pP->rb_id, rlc_pP->input_sdus[sdu_index].mui, RLC_SDU_CONFIRM_NO, rlc_pP->is_data_plane);
#endif
          rlc_pP->stat_tx_pdcp_sdu_discarded   += 1;
          rlc_pP->stat_tx_pdcp_bytes_discarded += rlc_pP->input_sdus[sdu_index].sdu_size;
184 185 186 187 188 189
          MSC_LOG_EVENT((ctxt_pP->enb_flag == ENB_FLAG_YES) ? MSC_RLC_ENB:MSC_RLC_UE,\
                        "0 "PROTOCOL_RLC_AM_MSC_FMT" Dropped SDU mui %u cause max_retx %u reached",\
                        PROTOCOL_RLC_AM_MSC_ARGS(ctxt_pP,rlc_pP),
                        rlc_pP->input_sdus[sdu_index].mui,
                        rlc_pP->max_retx_threshold);

190
          rlc_am_free_in_sdu(ctxt_pP, rlc_pP, sdu_index);
191

192 193
        }
      }
194
    }
195
#endif
196
  } else {
197 198
    LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[NACK-PDU] ERROR NACK MISSING PDU SN %05d\n",
          PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
199
          snP);
200
    status = FALSE;
201
  }
202 203

  return status;
204 205
}
//-----------------------------------------------------------------------------
206
void rlc_am_ack_pdu (
207 208 209
  const protocol_ctxt_t* const  ctxt_pP,
  rlc_am_entity_t *const rlc_pP,
  const rlc_sn_t snP)
210
{
211 212
  mem_block_t* mb_p         = rlc_pP->tx_data_pdu_buffer[snP % RLC_AM_WINDOW_SIZE].mem_block;
  rlc_am_tx_data_pdu_management_t *tx_data_pdu_buffer = &rlc_pP->tx_data_pdu_buffer[snP % RLC_AM_WINDOW_SIZE];
213

214
  tx_data_pdu_buffer->flags.retransmit = 0;
gauthier's avatar
gauthier committed
215

216
  if ((tx_data_pdu_buffer->flags.ack == 0) && (mb_p != NULL)) {
gauthier's avatar
gauthier committed
217
    //if (mb_pP != NULL) {
218
    free_mem_block(mb_p, __func__);
219
    tx_data_pdu_buffer->mem_block = NULL;
220 221
    LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[ACK-PDU] ACK PDU SN %05d previous retx_count %d \n",
          PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
222
          snP,
223
		  tx_data_pdu_buffer->retx_count);
224

225 226 227 228
    if (tx_data_pdu_buffer->retx_payload_size) {
      rlc_pP->retrans_num_bytes_to_retransmit -= tx_data_pdu_buffer->retx_payload_size;
      tx_data_pdu_buffer->retx_payload_size = 0;
      tx_data_pdu_buffer->num_holes = 0;
229
      rlc_pP->retrans_num_pdus --;
230
    }
gauthier's avatar
gauthier committed
231

232
  } else {
233 234
    LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[ACK-PDU] WARNING ACK PDU SN %05d -> NO PDU TO ACK\n",
          PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
235 236 237
          snP);

    if (mb_p != NULL) {
238
      free_mem_block(mb_p, __func__);
239
      tx_data_pdu_buffer->mem_block = NULL;
240 241
    }
  }
242 243 244
  tx_data_pdu_buffer->flags.ack = 1;
  tx_data_pdu_buffer->flags.transmitted = 0;
  tx_data_pdu_buffer->flags.retransmit = 0;
245

246 247
}
//-----------------------------------------------------------------------------
248
mem_block_t* rlc_am_retransmit_get_copy (
249 250 251
  const protocol_ctxt_t* const  ctxt_pP,
  rlc_am_entity_t *const rlc_pP,
  const rlc_sn_t snP)
252
{
253
  mem_block_t* mb_original_p = rlc_pP->tx_data_pdu_buffer[snP % RLC_AM_WINDOW_SIZE].mem_block;
254

255 256
  AssertFatal (mb_original_p != NULL, "RLC AM PDU Copy Error: Empty block sn=%d vtA=%d vtS=%d LcId=%d !\n",
		  snP,rlc_pP->vt_a,rlc_pP->vt_s,rlc_pP->channel_id);
257

258
  rlc_am_tx_data_pdu_management_t *pdu_mngt = &rlc_pP->tx_data_pdu_buffer[snP % RLC_AM_WINDOW_SIZE];
259

260 261 262 263
  /* We need to allocate a new buffer and copy to it because header content may change for Polling bit */
  int size             = pdu_mngt->header_and_payload_size + sizeof(struct mac_tb_req);
  mem_block_t* mb_copy = get_free_mem_block(size, __func__);
  memcpy(mb_copy->data, mb_original_p->data, size);
264

265 266
  rlc_am_pdu_sn_10_t *pdu_p                         = (rlc_am_pdu_sn_10_t*) (&mb_copy->data[sizeof(struct mac_tb_req)]);
  ((struct mac_tb_req*)(mb_copy->data))->data_ptr = (uint8_t*)pdu_p;
267

268 269
  return mb_copy;
}
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
//-----------------------------------------------------------------------------
mem_block_t* rlc_am_retransmit_get_am_segment(
  const protocol_ctxt_t* const  ctxt_pP,
  rlc_am_entity_t *const rlc_pP,
  rlc_am_tx_data_pdu_management_t *const pdu_mngt,
  sdu_size_t * const payload_sizeP /* in-out*/)
{
	int16_t          sdus_segment_size[RLC_AM_MAX_SDU_IN_PDU];
	mem_block_t*   mb_original_p  = pdu_mngt->mem_block;
	mem_block_t*   mem_pdu_segment_p = NULL;
	uint8_t              *pdu_original_header_p        = NULL;
	uint8_t              *pdu_segment_header_p        = NULL;
	sdu_size_t     retx_so_start,retx_so_stop; //starting and ending SO for retransmission in this PDU
	rlc_sn_t sn = pdu_mngt->sn;
	uint16_t	header_so_part;
	boolean_t fi_start, fi_end;
	uint8_t sdu_index = 0;
	uint8_t sdu_segment_index = 0;
	uint8_t num_LIs_pdu_segment = pdu_mngt->nb_sdus - 1;
	uint8_t li_bit_offset = 4; /* toggle between 0 and 4 */
	uint8_t li_jump_offset = 1; /* toggle between 1 and 2 */


	AssertFatal (mb_original_p != NULL, "RLC AM PDU Segment Error: Empty block sn=%d vtA=%d vtS=%d LcId=%d !\n",
			sn,rlc_pP->vt_a,rlc_pP->vt_s,rlc_pP->channel_id);


	AssertFatal (pdu_mngt->payload == mb_original_p->data + sizeof(struct mac_tb_req) + pdu_mngt->header_and_payload_size - pdu_mngt->payload_size,
			"RLC AM PDU Segment Error: Inconsistent data pointers p1=%p p2=%p sn = %d total size = %d data size = %d LcId=%d !\n",
			pdu_mngt->payload,mb_original_p->data + sizeof(struct mac_tb_req),pdu_mngt->header_and_payload_size,pdu_mngt->payload_size,sn,rlc_pP->channel_id);

	/* Init ReTx Hole list if not configured, ie the whole PDU has to be retransmitted */
	if (pdu_mngt->num_holes == 0)
	{
		AssertFatal (pdu_mngt->retx_payload_size == pdu_mngt->payload_size,"RLC AM PDU ReTx Segment: Expecting full PDU size ReTxSize=%d DataSize=%d sn=%d vtA=%d vtS=%d LcId=%d !\n",
				pdu_mngt->retx_payload_size,pdu_mngt->payload_size,sn,rlc_pP->vt_a,rlc_pP->vt_s,rlc_pP->channel_id);
		pdu_mngt->retx_hole_index = 0;
		pdu_mngt->hole_so_start[0] = 0;
		pdu_mngt->hole_so_stop[0] = pdu_mngt->payload_size - 1;
		pdu_mngt->num_holes = 1;
	}

	/* Init SO Start and SO Stop */
	retx_so_start = pdu_mngt->hole_so_start[pdu_mngt->retx_hole_index];
	retx_so_stop = pdu_mngt->hole_so_stop[pdu_mngt->retx_hole_index];

	/* Init FI to the same value as original PDU */
	fi_start = (!(RLC_AM_PDU_GET_FI_START(*(pdu_mngt->first_byte))));
	fi_end = (!(RLC_AM_PDU_GET_FI_END(*(pdu_mngt->first_byte))));

	/* Handle no LI case first */
	if (num_LIs_pdu_segment == 0)
	{
		/* Bound retx_so_stop to available TBS */
		if (retx_so_stop - retx_so_start + 1 + RLC_AM_PDU_SEGMENT_HEADER_MIN_SIZE > rlc_pP->nb_bytes_requested_by_mac)
		{
			retx_so_stop = retx_so_start + rlc_pP->nb_bytes_requested_by_mac - RLC_AM_PDU_SEGMENT_HEADER_MIN_SIZE - 1;
		}

		*payload_sizeP = retx_so_stop - retx_so_start + 1;
		mem_pdu_segment_p = get_free_mem_block((*payload_sizeP + RLC_AM_PDU_SEGMENT_HEADER_MIN_SIZE + sizeof(struct mac_tb_req)), __func__);
		pdu_segment_header_p        = &mem_pdu_segment_p->data[sizeof(struct mac_tb_req)];

		/* clear all PDU segment */
		memset(pdu_segment_header_p, 0, *payload_sizeP + RLC_AM_PDU_SEGMENT_HEADER_MIN_SIZE);
		/* copy data part */
		memcpy(pdu_segment_header_p + RLC_AM_PDU_SEGMENT_HEADER_MIN_SIZE, pdu_mngt->payload + retx_so_start, *payload_sizeP);

		/* Set FI part to false if SO Start and SO End are different from PDU boundaries */
		if (retx_so_start)
		{
			fi_start = FALSE;
		}
		if (retx_so_stop < pdu_mngt->payload_size - 1)
		{
			fi_end = FALSE;
		}

		/* Header content is filled at the end */
	}
	else
	{
		/* Step 1 */
		/* Find the SDU index in the original PDU containing retx_so_start */
		sdu_size_t sdu_size = 0;
		sdu_size_t data_size = 0;
		sdu_size_t header_segment_length = RLC_AM_PDU_SEGMENT_HEADER_MIN_SIZE;
		pdu_original_header_p = pdu_mngt->first_byte + 2;
		li_bit_offset = 4; /* toggle between 0 and 4 */
		li_jump_offset = 1; /* toggle between 1 and 2 */


		/* Read first LI */
		sdu_size = RLC_AM_PDU_GET_LI(*pdu_original_header_p + *(pdu_original_header_p + 1),li_bit_offset);
		pdu_original_header_p += li_jump_offset;
		li_bit_offset ^= 0x4;
		li_jump_offset ^= 0x3;
		data_size += sdu_size;

		while ((data_size - 1 < retx_so_start) && (sdu_index < pdu_mngt->nb_sdus))
		{
			sdu_index ++;
			if (sdu_index < pdu_mngt->nb_sdus)
			{
				sdu_size = RLC_AM_PDU_GET_LI(*pdu_original_header_p + *(pdu_original_header_p + 1),li_bit_offset);
				pdu_original_header_p += li_jump_offset;
				li_bit_offset ^= 0x4;
				li_jump_offset ^= 0x3;
				data_size += sdu_size;
			}
		}

		/* Set FI Start if retx_so_start = cumulated data size */
		if (retx_so_start == data_size)
		{
			fi_start = TRUE;
			/* jump to next SDU */
			sdu_index ++;
			/* there must be at least one SDU more */
			AssertFatal (sdu_index < pdu_mngt->nb_sdus, "RLC AM PDU Segment Error: sdu_index=%d nb_sdus=%d sn=%d LcId=%d !\n",
					sdu_index,pdu_mngt->nb_sdus,sn,rlc_pP->channel_id);
			sdu_size = RLC_AM_PDU_GET_LI(*pdu_original_header_p + *(pdu_original_header_p + 1),li_bit_offset);
			pdu_original_header_p += li_jump_offset;
			li_bit_offset ^= 0x4;
			li_jump_offset ^= 0x3;
			data_size += sdu_size;
		}
		/* Set first SDU portion of the segment */
		sdus_segment_size[0] = data_size - retx_so_start;

		/* Now look for the end */
		while ((data_size - 1 < retx_so_stop) && (sdu_index < pdu_mngt->nb_sdus))
		{
			sdu_index ++;
			sdu_segment_index ++;
			if (sdu_index < pdu_mngt->nb_sdus)
			{
				sdu_size = RLC_AM_PDU_GET_LI(*pdu_original_header_p + *(pdu_original_header_p + 1),li_bit_offset);
				pdu_original_header_p += li_jump_offset;
				li_bit_offset ^= 0x4;
				li_jump_offset ^= 0x3;
				data_size += sdu_size;
				sdus_segment_size[sdu_segment_index] = sdu_size;
			}
		}

		/* Set FI End if retx_so_stop = cumulated data size */
		if (retx_so_stop == data_size - 1)
		{
			fi_end = TRUE;
		}
		/* Set last SDU portion of the segment even if it will be out of LI */
		if (sdu_segment_index)
		{
			sdus_segment_size[sdu_segment_index] = retx_so_stop - (data_size - sdu_size) + 1;
		}
		else
		{
			sdus_segment_size[0] = retx_so_stop - retx_so_start + 1;
		}

		/* Set number of LIs in the segment */
		num_LIs_pdu_segment = sdu_segment_index;

		/* Memory allocation taking into account available TBS */
		/* Compute Header Length and Data Length */
		/* Init Segment Payload data size */
		*payload_sizeP = sdus_segment_size[0];

		/* Bound to available TBS taking into account min PDU segment header*/
		if (*payload_sizeP + RLC_AM_PDU_SEGMENT_HEADER_MIN_SIZE > rlc_pP->nb_bytes_requested_by_mac)
		{
			*payload_sizeP = rlc_pP->nb_bytes_requested_by_mac - RLC_AM_PDU_SEGMENT_HEADER_MIN_SIZE;
		}

		sdu_segment_index = 1;
		while ((sdu_segment_index < num_LIs_pdu_segment + 1) && (rlc_pP->nb_bytes_requested_by_mac > *payload_sizeP + RLC_AM_PDU_SEGMENT_HEADER_SIZE(sdu_segment_index)))
		{
			/* Add next sdu_segment_index to data part */
			if (RLC_AM_PDU_SEGMENT_HEADER_SIZE(sdu_segment_index) + sdus_segment_size[sdu_segment_index] < rlc_pP->nb_bytes_requested_by_mac)
			{
				(*payload_sizeP) += sdus_segment_size[sdu_segment_index];
			}
			else
			{
				/* bound to available TBS size */
				(*payload_sizeP) += (rlc_pP->nb_bytes_requested_by_mac - RLC_AM_PDU_SEGMENT_HEADER_SIZE(sdu_segment_index));
				sdus_segment_size[sdu_segment_index] = rlc_pP->nb_bytes_requested_by_mac - RLC_AM_PDU_SEGMENT_HEADER_SIZE(sdu_segment_index);
			}
			header_segment_length = RLC_AM_PDU_SEGMENT_HEADER_SIZE(sdu_segment_index);
			sdu_segment_index ++;
		}

		num_LIs_pdu_segment = sdu_segment_index - 1;

		/* Check consistency between sdus_segment_size and payload_sizeP */
		data_size = 0;
		for (int i = 0; i < num_LIs_pdu_segment + 1; i++)
		{
			data_size += sdus_segment_size[i];
		}

		AssertFatal (data_size == *payload_sizeP, "RLC AM PDU Segment Data Error: SduSum=%d Data=%d sn=%d LcId=%d !\n",
				data_size,*payload_sizeP,sn,rlc_pP->channel_id);

		/* Allocation */
		AssertFatal (header_segment_length + *payload_sizeP <= pdu_mngt->header_and_payload_size + 2, "RLC AM PDU Segment Error: Hdr=%d Data=%d Original Hdr+Data =%d sn=%d LcId=%d !\n",
				header_segment_length,*payload_sizeP,pdu_mngt->header_and_payload_size,sn,rlc_pP->channel_id);
		mem_pdu_segment_p = get_free_mem_block((*payload_sizeP + header_segment_length + sizeof(struct mac_tb_req)), __func__);
		pdu_segment_header_p        = &mem_pdu_segment_p->data[sizeof(struct mac_tb_req)];

		/* clear all PDU segment */
		memset(pdu_segment_header_p, 0, *payload_sizeP + header_segment_length);
		/* copy data part */
		memcpy(pdu_segment_header_p + header_segment_length, pdu_mngt->payload + retx_so_start, *payload_sizeP);
	}

	/* Last step : update contexts and fill PDU Segment Header */
	if (mem_pdu_segment_p != NULL)
	{
		/* Update PDU Segment contexts */
		if (*payload_sizeP == pdu_mngt->hole_so_stop[pdu_mngt->retx_hole_index] - pdu_mngt->hole_so_start[pdu_mngt->retx_hole_index] + 1)
		{
			/* All data in the segment are transmitted : switch to next one */
			pdu_mngt->retx_hole_index ++;
			if (pdu_mngt->retx_hole_index < pdu_mngt->num_holes)
			{
				/* Set min SOStart to the value of next hole : assumption is holes are ordered by increasing SOStart */
				pdu_mngt->nack_so_start = pdu_mngt->hole_so_start[pdu_mngt->retx_hole_index];
			}
			else
			{
				/* no more scheduled Retx: reset values */
				/* Retx size is reset in the calling function */
				pdu_mngt->num_holes = 0;
				pdu_mngt->retx_hole_index = 0;
			}
		}
		else
		{
			/* not all segment data could be transmitted, just update SoStart */
			pdu_mngt->hole_so_start[pdu_mngt->retx_hole_index] += (*payload_sizeP);
			pdu_mngt->nack_so_start = pdu_mngt->hole_so_start[pdu_mngt->retx_hole_index];
		}

		/* Set Fixed part of AM PDU Segment Header */
		pdu_segment_header_p        = &mem_pdu_segment_p->data[sizeof(struct mac_tb_req)];

		/* Content is supposed to be init with 0 so with FIStart=FIEnd=TRUE */
		/* copy first two bytes from original: D/C + RF + FI + E+ SN*/
		*pdu_segment_header_p = *(pdu_mngt->first_byte);
		*(pdu_segment_header_p + 1) = *(pdu_mngt->first_byte + 1);

		/* Set Segmentation Flag */
		RLC_AM_PDU_SET_RF(*pdu_segment_header_p);
		/* clear polling */
		RLC_AM_PDU_CLEAR_POLL(*pdu_segment_header_p);
		/* Change FI */
		if (!fi_start)
		{
			// Set to not starting
			(*pdu_segment_header_p) |= (1 << (RLC_AM_PDU_FI_OFFSET + 1));

		}
		if (!fi_end)
		{
			// Set to not starting
			(*pdu_segment_header_p) |= (1 << (RLC_AM_PDU_FI_OFFSET));

		}

		/* Segment Offset */
		header_so_part = retx_so_start;

		/* Last Segment Flag (LSF) */
		if (retx_so_stop == pdu_mngt->payload_size - 1)
		{
			RLC_AM_PDU_SET_LSF(header_so_part);
		}

		/* Store SO bytes */
		* (pdu_segment_header_p + 2)  = (header_so_part >> 8) & 0xFF;
		* (pdu_segment_header_p + 3)  = header_so_part & 0xFF;

		/* Fill LI part */
		if (num_LIs_pdu_segment)
		{
			uint16_t index = 0;
			uint16_t temp = 0;
			/* Set Extension bit in first byte */
			RLC_AM_PDU_SET_E(*pdu_segment_header_p);

			/* loop on nb of LIs */
			pdu_segment_header_p += RLC_AM_PDU_SEGMENT_HEADER_MIN_SIZE;
			li_bit_offset = 4; /* toggle between 0 and 4 */
			li_jump_offset = 1; /* toggle between 1 and 2 */

			while (index < num_LIs_pdu_segment)
			{
				/* Set E bit for next LI if present */
				if (index < num_LIs_pdu_segment - 1)
					RLC_SET_BIT(temp,li_bit_offset + RLC_AM_LI_BITS);
				/* Set LI */
				RLC_AM_PDU_SET_LI(temp,sdus_segment_size[index],li_bit_offset);
				*pdu_segment_header_p = temp >> 8;
				*(pdu_segment_header_p + 1) = temp & 0xFF;
				pdu_segment_header_p += li_jump_offset;
				li_bit_offset ^= 0x4;
				li_jump_offset ^= 0x3;

				temp = ((*pdu_segment_header_p) << 8) + *(pdu_segment_header_p + 1);
				index ++;
			}
		}

	}

	return mem_pdu_segment_p;
589
}
590 591

#if 0
592
//-----------------------------------------------------------------------------
593
mem_block_t* rlc_am_retransmit_get_subsegment(
594 595 596 597
  const protocol_ctxt_t* const  ctxt_pP,
  rlc_am_entity_t *const rlc_pP,
  const rlc_sn_t snP,
  sdu_size_t * const sizeP /* in-out*/)
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
{

  // 5.2 ARQ procedures
  // ARQ procedures are only performed by an AM RLC entity.
  // 5.2.1 Retransmission
  // The transmitting side of an AM RLC entity can receive a negative acknowledgement (notification of reception failure
  // by its peer AM RLC entity) for an AMD PDU or a portion of an AMD PDU by the following:
  // - STATUS PDU from its peer AM RLC entity.
  //
  // When receiving a negative acknowledgement for an AMD PDU or a portion of an AMD PDU by a STATUS PDU from
  // its peer AM RLC entity, the transmitting side of the AM RLC entity shall:
  //     - if the SN of the corresponding AMD PDU falls within the range VT(A) <= SN < VT(S):
  //         - consider the AMD PDU or the portion of the AMD PDU for which a negative acknowledgement was
  //           received for retransmission.
  //
  // When an AMD PDU or a portion of an AMD PDU is considered for retransmission, the transmitting side of the AM
  // RLC entity shall:
  //     - if the AMD PDU is considered for retransmission for the first time:
  //         - set the RETX_COUNT associated with the AMD PDU to zero;
  //     - else, if it (the AMD PDU or the portion of the AMD PDU that is considered for retransmission) is not pending
  //       for retransmission already, or a portion of it is not pending for retransmission already:
  //         - increment the RETX_COUNT;
  //     - if RETX_COUNT = maxRetxThreshold:
  //         - indicate to upper layers that max retransmission has been reached.
  //
  // When retransmitting an AMD PDU, the transmitting side of an AM RLC entity shall:
  //     - if the AMD PDU can entirely fit within the total size of RLC PDU(s) indicated by lower layer at the particular
  //       transmission opportunity:
  //         - deliver the AMD PDU as it is except for the P field (the P field should be set according to sub clause 5.2.2) to
  //           lower layer;
  //     - otherwise:
  //         - segment the AMD PDU, form a new AMD PDU segment which will fit within the total size of RLC PDU(s)
  //           indicated by lower layer at the particular transmission opportunity and deliver the new AMD PDU segment
  //           to lower layer.
  //
  // When retransmitting a portion of an AMD PDU, the transmitting side of an AM RLC entity shall:
  //    - segment the portion of the AMD PDU as necessary, form a new AMD PDU segment which will fit within the
  //    total size of RLC PDU(s) indicated by lower layer at the particular transmission opportunity and deliver the new
  //    AMD PDU segment to lower layer.
  //
  // When forming a new AMD PDU segment, the transmitting side of an AM RLC entity shall:
  //    - only map the Data field of the original AMD PDU to the Data field of the new AMD PDU segment;
  //    - set the header of the new AMD PDU segment in accordance with the description in sub clause 6.;
  //    - set the P field according to sub clause 5.2.2.

643
  mem_block_t*           mb_original_p     = rlc_pP->tx_data_pdu_buffer[snP].mem_block;
644 645

  if (mb_original_p != NULL) {
646
    mem_block_t*           mb_sub_segment_p  = get_free_mem_block(*sizeP + sizeof(struct mac_tb_req), __func__);
647 648 649 650 651 652 653 654 655
    rlc_am_pdu_sn_10_t*    pdu_original_p    = (rlc_am_pdu_sn_10_t*) (&mb_original_p->data[sizeof(struct mac_tb_req)]);
    rlc_am_pdu_sn_10_t*    pdu_sub_segment_p = (rlc_am_pdu_sn_10_t*) (&mb_sub_segment_p->data[sizeof(struct mac_tb_req)]);
    rlc_am_pdu_info_t      pdu_info;
    int                    max_copy_payload_size;
    //LG avoid WARNING int                    test_max_copy_payload_size;
    int                    test_pdu_copy_size          = 0;

    ((struct mac_tb_req*)(mb_sub_segment_p->data))->data_ptr         = (uint8_t*)&(mb_sub_segment_p->data[sizeof(struct mac_tb_req)]);

656
    if (rlc_am_get_data_pdu_infos(ctxt_pP, rlc_pP, pdu_original_p, rlc_pP->tx_data_pdu_buffer[snP].header_and_payload_size, &pdu_info) >= 0) {
657
      int li_index = 0;
658 659
      int start_offset       = rlc_pP->tx_data_pdu_buffer[snP].nack_so_start;
      int stop_offset        = rlc_pP->tx_data_pdu_buffer[snP].nack_so_stop;
660

661 662
      LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] ORIGINAL PDU SN %04d:\n",
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
663 664 665 666 667 668
            snP);
      rlc_am_display_data_pdu_infos(ctxt_pP, rlc_pP, &pdu_info);

      // all 15 bits set to 1 (indicate that the missing portion of the AMD PDU includes all bytes
      // to the last byte of the AMD PDU)
      if (stop_offset == 0x7FFF) {
669 670
        rlc_pP->tx_data_pdu_buffer[snP].nack_so_stop = rlc_pP->tx_data_pdu_buffer[snP].payload_size - 1;
        stop_offset = rlc_pP->tx_data_pdu_buffer[snP].nack_so_stop;
671 672 673 674
        LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] UPDATED RETRANS PDU SN %04d nack_so_stop FROM 0x7FFF to %05d\n",
              PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
              snP,
              stop_offset);
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
      }

      // FIXED PART AMD PDU SEGMENT HEADER
      // 6.2.1.5 AMD PDU segment
      // AMD PDU segment consists of a Data field and an AMD PDU segment header.
      //
      // AMD PDU segment header consists of a fixed part (fields that are present for every AMD PDU segment) and an
      // extension part (fields that are present for an AMD PDU segment when necessary). The fixed part of the AMD PDU
      // segment header itself is byte aligned and consists of a D/C, a RF, a P, a FI, an E, a SN, a LSF and a SO. The extension
      // part of the AMD PDU segment header itself is byte aligned and consists of E(s) and LI(s).
      //
      // An AMD PDU segment header consists of an extension part only when more than one Data field elements are present
      // in the AMD PDU segment, in which case an E and a LI are present for every Data field element except the last.
      // Furthermore, when an AMD PDU segment header consists of an odd number of LI(s), four padding bits follow after the
      // last LI.

      pdu_sub_segment_p->b1 = (pdu_original_p->b1 & 0x83) | 0x40;
      pdu_sub_segment_p->b2 = pdu_original_p->b2;
      pdu_sub_segment_p->data[0] = ((uint8_t)(start_offset >> 8));
      pdu_sub_segment_p->data[1] = ((uint8_t)(start_offset & 0xFF));

      *sizeP = *sizeP - 4;


      int            continue_fill_pdu_with_pdu  = 1;
      int            test_pdu_remaining_size     = *sizeP;
      int            test_num_li                 = 0;
      int            fill_num_li                    ;
      int            test_li_sum                 = 0;
      int            not_test_fi                 = 0; // by default not 1st byte and not last byte af a SDU
      int            test_start_offset;
      unsigned int   test_li_length_in_bytes     = 1;
      int16_t          test_li_list[RLC_AM_MAX_SDU_IN_PDU];
      uint8_t*          fill_payload_p;
      //int            test_fi_last_byte_pdu_is_last_byte_sdu = 0;
      //int            test_fi_first_byte_pdu_is_first_byte_sdu = 0;

      rlc_am_e_li_t* e_li_sub_segment            = (rlc_am_e_li_t*)(&pdu_sub_segment_p->data[2]);
      //int            first_enter_in_start_offset_lt_li_sum = 1;
      int            not_fi_original                 = ((pdu_original_p->b1 & 0x18) >> 3) ^ 3;

      //-------------------------------------------------------
      // set MAX payload size that can be copied
      // first constraint : the size of the hole to retransmit
      rlc_am_get_next_hole(ctxt_pP, rlc_pP, snP, &start_offset, &stop_offset);
      max_copy_payload_size = stop_offset - start_offset + 1;
      assert(max_copy_payload_size > 0);
      assert(test_pdu_remaining_size > 0);
723 724
      LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] HOLE FOUND SO %d -> %d\n",
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
725 726
            start_offset,
            stop_offset);
727 728
      LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] ORIGINAL FI %d\n",
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
729 730 731 732
            (pdu_original_p->b1 & 0x18) >> 3);

      // second constraint the size of the pdu_p requested by MAC layer
      if (max_copy_payload_size > test_pdu_remaining_size) {
733 734
        LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] CUT max_copy_payload_size with test_pdu_remaining_size %d -> %d\n",
              PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
735 736 737 738
              max_copy_payload_size,
              test_pdu_remaining_size);
        max_copy_payload_size = test_pdu_remaining_size;
      }
739 740


741 742 743
      if (start_offset == 0) {
        not_test_fi = (not_fi_original & 0x02);
      }
744

745
      test_start_offset = start_offset;
gauthier's avatar
gauthier committed
746

747 748
      //.find the li corresponding to the nack_so_start (start_offset)
      if (pdu_info.num_li > 0) {
749 750
        LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] ORIGINAL NUM LI %d\n",
              PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
751
              pdu_info.num_li);
752

753
        while ((li_index < pdu_info.num_li) && (continue_fill_pdu_with_pdu)) {
754 755
          LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] FIND LI %d\n",
                PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
756 757 758
                pdu_info.li_list[li_index]);

          if (max_copy_payload_size > test_pdu_remaining_size) {
759 760
            LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] CUT max_copy_payload_size with test_pdu_remaining_size %d -> %d\n",
                  PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
761 762 763 764 765 766 767 768 769 770 771 772 773
                  max_copy_payload_size,
                  test_pdu_remaining_size);
            max_copy_payload_size = test_pdu_remaining_size;
          }

          assert(max_copy_payload_size >= 0);
          assert(test_pdu_remaining_size >= 0);

          test_li_sum               += pdu_info.li_list[li_index];

          //---------------------------------------------------------------
          if (test_start_offset < test_li_sum) {

774 775
            LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] test_start_offset < test_li_sum  %d < %d\n",
                  PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
776 777 778
                  test_start_offset,
                  test_li_sum);
            /*if (test_max_copy_payload_size > (test_li_sum - test_start_offset)) {
779
                #if TRACE_RLC_AM_RESEGMENT
780 781 782 783
                LOG_D(RLC, "[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u][RE-SEGMENT] CUT test_max_copy_payload_size with test_li_sum - test_start_offset %d -> %d\n",ctxt_pP->frame, rlc_pP->module_id, rlc_pP->rb_id,  test_max_copy_payload_size, test_li_sum - test_start_offset);
                #endif
                test_max_copy_payload_size = test_li_sum - test_start_offset;
            }*/
784

785
            if ((max_copy_payload_size + test_start_offset) < test_li_sum) {
786 787
              LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] (max_copy_payload_size %d + test_start_offset %d) < test_li_sum %d\n",
                    PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
788 789 790
                    max_copy_payload_size,
                    test_start_offset,
                    test_li_sum);
791 792
              LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] COPY SO %d -> %d  %d BYTES\n",
                    PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
793 794 795 796 797 798 799 800 801 802 803
                    test_start_offset ,
                    test_start_offset + max_copy_payload_size - 1,
                    max_copy_payload_size );
              assert(max_copy_payload_size > 0);
              continue_fill_pdu_with_pdu = 0;
              test_pdu_copy_size         = test_pdu_copy_size + max_copy_payload_size;
              test_start_offset          = test_start_offset + max_copy_payload_size;
              not_test_fi                = not_test_fi & 0x02;  // clear b0, last byte does not correspond to last byte of a SDU
              max_copy_payload_size      = 0;

            } else if ((max_copy_payload_size + test_start_offset) == test_li_sum) {
804 805
              LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] (max_copy_payload_size + test_start_offset) == test_li_sum %d == %d\n",
                    PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
806 807
                    (max_copy_payload_size + test_start_offset) ,
                    test_li_sum);
808 809
              LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] COPY SO %d -> %d  %d BYTES\n",
                    PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
810 811 812 813 814 815 816 817 818 819 820
                    test_start_offset ,
                    test_start_offset + max_copy_payload_size - 1,
                    max_copy_payload_size );
              assert(max_copy_payload_size > 0);
              continue_fill_pdu_with_pdu = 0;
              test_pdu_copy_size         = test_pdu_copy_size + max_copy_payload_size;
              test_start_offset          = test_start_offset + max_copy_payload_size;
              not_test_fi                = not_test_fi | 0x01;// set b0, last byte does correspond to last byte of a SDU
              max_copy_payload_size      = 0;

            } else if ((max_copy_payload_size + test_start_offset - (test_li_length_in_bytes ^ 3)) > test_li_sum) {
821 822 823
              LOG_T(RLC,
                    PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] (max_copy_payload_size + test_start_offset - (test_li_length_in_bytes ^ 3)) > test_li_sum %d > %d\n SET LI %d\n",
                    PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
824 825 826
                    (max_copy_payload_size + test_start_offset)  + (test_li_length_in_bytes ^ 3),
                    test_li_sum,
                    test_li_sum - test_start_offset);
827 828
              LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] COPY SO %d -> %d  %d BYTES\n",
                    PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
829 830 831 832 833 834 835 836 837 838 839 840
                    test_start_offset ,
                    test_li_sum - 1,
                    test_li_sum - test_start_offset );
              assert((test_li_sum - test_start_offset) > 0);
              test_li_list[test_num_li++] = test_li_sum - test_start_offset;
              test_pdu_copy_size          = test_pdu_copy_size + test_li_sum - test_start_offset;
              test_li_length_in_bytes      = test_li_length_in_bytes ^ 3;

              test_pdu_remaining_size     = test_pdu_remaining_size - test_li_sum + test_start_offset -
                                            test_li_length_in_bytes;

              max_copy_payload_size       = max_copy_payload_size - test_li_sum + test_start_offset - test_li_length_in_bytes;
841 842
              LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] NOW max_copy_payload_size %d BYTES test_start_offset %d\n",
                    PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
843 844 845 846 847 848
                    max_copy_payload_size,
                    test_li_sum);
              // normally the next while itereation will add bytes to PDU
              //not_test_fi = not_test_fi | 0x01;  // set b0, last byte does correspond to last byte of a SDU
              test_start_offset           = test_li_sum;

849
            } else {
850 851
              LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] (test_max_copy_payload_size + test_start_offset ) > test_li_sum %d > %d\n NO REMAINING SIZE FOR LI",
                    PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
852 853
                    (max_copy_payload_size + test_start_offset),
                    test_li_sum);
854 855
              LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] COPY SO %d -> %d  %d BYTES\n",
                    PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
856 857 858 859 860 861 862 863 864 865 866 867
                    test_start_offset ,
                    test_li_sum - 1,
                    test_li_sum - test_start_offset );
              assert((test_li_sum - test_start_offset) > 0);
              continue_fill_pdu_with_pdu = 0;
              test_pdu_copy_size         = test_pdu_copy_size + test_li_sum - test_start_offset;

              test_pdu_remaining_size    = test_pdu_remaining_size - test_li_sum + test_start_offset;

              max_copy_payload_size      = max_copy_payload_size - test_li_sum + test_start_offset;
              test_start_offset          = test_li_sum;
              not_test_fi                = not_test_fi | 0x01;  // set b0, last byte does correspond to last byte of a SDU
868
            }
869

870
            //---------------------------------------------------------------
871 872 873
            // start offset start at the begining of a SDU
            // and it cant be the first data field of the original PDU
          } else if (test_start_offset == test_li_sum) {
874 875
            LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] (test_start_offset == test_li_sum) %d == %d\n",
                  PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
876 877 878 879 880
                  test_start_offset ,
                  test_li_sum);

            if ((test_num_li == 0) && (test_pdu_copy_size == 0)) {
              not_test_fi = not_test_fi | 0x02;  // set b1, first byte does correspond to first byte of a SDU
881
            }
882
          }
883

884 885 886 887 888 889 890 891 892 893
          li_index = li_index + 1;
        }

        if ((continue_fill_pdu_with_pdu > 0) &&
            (li_index    == pdu_info.num_li) &&
            (pdu_info.hidden_size       > 0) &&
            (test_pdu_remaining_size    > 0) &&
            (max_copy_payload_size      > 0) ) {

          if (max_copy_payload_size > test_pdu_remaining_size) {
894 895
            LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] TRYING HIDDEN SIZE...CUT max_copy_payload_size with test_pdu_remaining_size %d -> %d\n",
                  PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
896 897 898 899 900 901 902 903 904
                  max_copy_payload_size,
                  test_pdu_remaining_size);
            max_copy_payload_size = test_pdu_remaining_size;
          }

          // remaining bytes to fill, redundant check, but ...
          if  ((max_copy_payload_size + test_start_offset) >= (pdu_info.hidden_size + test_li_sum)) {
            test_pdu_copy_size += (pdu_info.hidden_size  + test_li_sum - test_start_offset);
            LOG_T(RLC,
905 906
                  PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] COPYING WHOLE REMAINING SIZE %d (max_copy_payload_size %d, test_start_offset %d, pdu_info.hidden_size %d test_li_sum %d test_pdu_copy_size %d)\n",
                  PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
907 908 909 910 911 912 913 914 915
                  pdu_info.hidden_size  + test_li_sum - test_start_offset,
                  max_copy_payload_size,
                  test_start_offset,
                  pdu_info.hidden_size,
                  test_li_sum,
                  test_pdu_copy_size);
            test_start_offset   = pdu_info.hidden_size   + test_li_sum;
            not_test_fi = (not_test_fi & 0x2) | (not_fi_original & 0x1);  // set b0 idendical to the b0 of the non segmented PDU
          } else {
916 917
            LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] COPYING REMAINING SIZE %d (/%d)\n",
                  PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
918 919 920 921 922 923
                  max_copy_payload_size,
                  pdu_info.hidden_size);
            test_pdu_copy_size += max_copy_payload_size;
            test_start_offset = test_start_offset + max_copy_payload_size;
            not_test_fi = not_test_fi & 0x2;  // clear b0 because no SDU ending in this PDU
          }
924
        }
925
      } else { // num_li == 0
926 927
        LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] (num_li == 0)\n",
              PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP));
928
        test_pdu_copy_size = max_copy_payload_size;
929

930
        if ((stop_offset ==  (start_offset + max_copy_payload_size - 1)) && (stop_offset == rlc_pP->tx_data_pdu_buffer[snP].payload_size - 1)) {
931 932 933 934 935 936 937
          not_test_fi = (not_test_fi & 0x2) | (not_fi_original & 0x1);  // set b0 idendical to the b0 of the non segmented PDU
        } else {
          not_test_fi = not_test_fi & 0x2;  // clear b0 because no SDU ending in this PDU
        }
      }

      //---------------------------------------------------------------
938
      /*if (stop_offset == (rlc_pP->tx_data_pdu_buffer[snP].payload_size - 1)) {
939 940 941 942 943 944 945 946 947
              test_fi = (test_fi & 0x02) | (fi_original & 0x01);
      }*/
      //---------------------------------------------------------------
      // write FI field in header
      //---------------------------------------------------------------
      pdu_sub_segment_p->b1 = pdu_sub_segment_p->b1 | (((not_test_fi << 3) ^ 0x18) & 0x18);
      //---------------------------------------------------------------
      // fill the segment pdu_p with Lis and data
      //---------------------------------------------------------------
948
      LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] fill the segment pdu_p with Lis and data, test_num_li %d\n",
Cedric Roux's avatar
Cedric Roux committed
949
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP), test_num_li);
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

      if (test_num_li > 0) {
        pdu_sub_segment_p->b1 = pdu_sub_segment_p->b1 | 0x04; // set E bit
        test_li_length_in_bytes = 1;

        for (fill_num_li=0; fill_num_li < test_num_li; fill_num_li++) {
          test_li_length_in_bytes = test_li_length_in_bytes ^ 3;

          if (test_li_length_in_bytes  == 2) {
            if (fill_num_li == (test_num_li - 1)) {
              e_li_sub_segment->b1 = 0;
            } else {
              e_li_sub_segment->b1 =  0x80;
            }

            e_li_sub_segment->b1 = e_li_sub_segment->b1 | (test_li_list[fill_num_li] >> 4);
            e_li_sub_segment->b2 = test_li_list[fill_num_li] << 4;
            fill_payload_p         = (uint8_t*)(&e_li_sub_segment->b3);
            *sizeP               = *sizeP - 2;
          } else {
            if (fill_num_li != (test_num_li - 1)) {
              e_li_sub_segment->b2  = e_li_sub_segment->b2 | 0x08;
            }

            e_li_sub_segment->b2 = e_li_sub_segment->b2 | (test_li_list[fill_num_li] >> 8);
            e_li_sub_segment->b3 = test_li_list[fill_num_li] & 0xFF;
            e_li_sub_segment++;
            fill_payload_p         = (uint8_t*)e_li_sub_segment;
            *sizeP               = *sizeP - 1;
          }

Cedric Roux's avatar
Cedric Roux committed
981
          LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] ADD LI %d\n",
982
                PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
983 984 985
                test_li_list[fill_num_li]);
        }
      } else {
Cedric Roux's avatar
Cedric Roux committed
986
        LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] ADD NO LI\n",
987
              PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP));
988 989
        fill_payload_p = (uint8_t*)e_li_sub_segment;
      }
gauthier's avatar
gauthier committed
990

991 992 993 994
      //---------------------------------------------------------------
      // copy payload to retransmit
      //---------------------------------------------------------------
      memcpy(fill_payload_p,
995
             &rlc_pP->tx_data_pdu_buffer[snP].payload[start_offset],
996 997 998 999 1000
             test_pdu_copy_size);

      ((struct mac_tb_req*)(mb_sub_segment_p->data))->tb_size  = (tb_size_t)(((uint64_t)fill_payload_p)+ test_pdu_copy_size) - ((uint64_t)(&pdu_sub_segment_p->b1));

      // set LSF
1001
      if ((test_pdu_copy_size + start_offset) == rlc_pP->tx_data_pdu_buffer[snP].payload_size) {
1002 1003
        pdu_sub_segment_p->data[0] = pdu_sub_segment_p->data[0] | 0x80;

1004
        rlc_pP->tx_data_pdu_buffer[snP].flags.retransmit = 0;
1005

1006 1007
        LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] RE-SEND DATA PDU SN %04d SO %d %d BYTES PAYLOAD %d BYTES LSF!\n",
              PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
1008 1009 1010 1011 1012
              snP,
              start_offset,
              ((struct mac_tb_req*)(mb_sub_segment_p->data))->tb_size,
              test_pdu_copy_size);
      } else {
1013 1014
        LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] RE-SEND DATA PDU SN %04d SO %d %d BYTES PAYLOAD %d BYTES\n",
              PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
1015 1016 1017 1018 1019 1020
              snP,
              start_offset,
              ((struct mac_tb_req*)(mb_sub_segment_p->data))->tb_size,
              test_pdu_copy_size);
      }

1021 1022
      LOG_T(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] *sizeP %d = *sizeP %d - test_pdu_copy_size %d\n",
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
1023 1024 1025 1026 1027 1028 1029 1030
            *sizeP - test_pdu_copy_size, *sizeP,
            test_pdu_copy_size);

      *sizeP = *sizeP - test_pdu_copy_size;
      //---------------------------------------------------------------
      // update nack_so_start
      //---------------------------------------------------------------
      rlc_am_remove_hole(ctxt_pP, rlc_pP, snP, start_offset, test_pdu_copy_size+start_offset - 1);
1031
      //rlc_pP->tx_data_pdu_buffer[snP].nack_so_start = rlc_pP->tx_data_pdu_buffer[snP].nack_so_start + test_pdu_copy_size;
1032 1033
      LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] RE-SEND DATA PDU SN %04d NOW nack_so_start %d nack_so_stop %d\n",
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
1034
            snP,
1035 1036 1037 1038 1039
            rlc_pP->tx_data_pdu_buffer[snP].nack_so_start,
            rlc_pP->tx_data_pdu_buffer[snP].nack_so_stop);
      /*if (rlc_pP->tx_data_pdu_buffer[snP].nack_so_start == rlc_pP->tx_data_pdu_buffer[snP].nack_so_stop) {
          rlc_pP->tx_data_pdu_buffer[snP].nack_so_start = 0;
          rlc_pP->tx_data_pdu_buffer[snP].nack_so_stop  = 0x7FFF;
1040 1041
      }*/
    } else {
1042 1043 1044
      LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] COULD NOT GET INFO FOR DATA PDU SN %04d -> RETURN NULL\n",
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
            snP);
1045
      return NULL;
1046
    }
1047 1048
    return mb_sub_segment_p;
  } else {
1049 1050
    LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[RE-SEGMENT] RE-SEND DATA PDU SN %04d BUT NO PDU AVAILABLE -> RETURN NULL\n",
          PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
1051 1052 1053 1054
          snP);
    assert(3==4);
    return NULL;
  }
1055
}
1056
#endif
1057
//-----------------------------------------------------------------------------
1058 1059 1060 1061
void rlc_am_tx_buffer_display (
  const protocol_ctxt_t* const  ctxt_pP,
  rlc_am_entity_t* const rlc_pP,
  char* const message_pP)
1062
{
1063 1064
  rlc_sn_t       sn = rlc_pP->vt_a;
  int            i, loop = 0;
1065
  rlc_am_tx_data_pdu_management_t *tx_data_pdu_buffer_p;
1066 1067

  if (message_pP) {
1068 1069
    LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT" Retransmission buffer %s VT(A)=%04d VT(S)=%04d:",
          PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
1070 1071 1072 1073
          message_pP,
          rlc_pP->vt_a,
          rlc_pP->vt_s);
  } else {
1074 1075
    LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT" Retransmission buffer VT(A)=%04d VT(S)=%04d:",
          PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
1076 1077 1078 1079 1080
          rlc_pP->vt_a,
          rlc_pP->vt_s);
  }

  while (rlc_pP->vt_s != sn) {
1081 1082
	  tx_data_pdu_buffer_p = &rlc_pP->tx_data_pdu_buffer[sn % RLC_AM_WINDOW_SIZE];
    if (tx_data_pdu_buffer_p->mem_block) {
1083 1084 1085 1086
      if ((loop % 1) == 0) {
        LOG_D(RLC, "\nTX SN:\t");
      }

1087 1088 1089
      if (tx_data_pdu_buffer_p->flags.retransmit) {
        LOG_D(RLC, "%04d %d/%d Bytes (NACK RTX:%02d ",sn, tx_data_pdu_buffer_p->header_and_payload_size, tx_data_pdu_buffer_p->payload_size,
        		tx_data_pdu_buffer_p->retx_count);
1090
      } else {
1091 1092
        LOG_D(RLC, "%04d %d/%d Bytes (RTX:%02d ",sn, tx_data_pdu_buffer_p->header_and_payload_size, tx_data_pdu_buffer_p->payload_size,
        		tx_data_pdu_buffer_p->retx_count);
1093 1094
      }

1095 1096
      if (tx_data_pdu_buffer_p->num_holes == 0) {
        LOG_D(RLC, "SO:%04d->%04d)\t", tx_data_pdu_buffer_p->nack_so_start, tx_data_pdu_buffer_p->nack_so_stop);
1097
      } else {
1098
        for (i=0; i<tx_data_pdu_buffer_p->num_holes; i++) {
1099
          assert(i < RLC_AM_MAX_HOLES_REPORT_PER_PDU);
1100
          LOG_D(RLC, "SO:%04d->%04d)\t", tx_data_pdu_buffer_p->hole_so_start[i], tx_data_pdu_buffer_p->hole_so_stop[i]);
1101
        }
1102 1103 1104
      }

      loop++;
1105
    }
1106 1107 1108 1109 1110 1111

    sn = (sn + 1) & RLC_AM_SN_MASK;
  }

  LOG_D(RLC, "\n");
}
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229

//-----------------------------------------------------------------------------
mem_block_t * rlc_am_get_pdu_to_retransmit(
  const protocol_ctxt_t* const  ctxt_pP,
  rlc_am_entity_t* const rlc_pP)
{
	  rlc_sn_t             sn          = rlc_pP->vt_a;
	  rlc_sn_t             sn_end      = rlc_pP->vt_s;
	  mem_block_t*         pdu_p        = NULL;
	  rlc_am_tx_data_pdu_management_t* tx_data_pdu_management;

	  AssertFatal ((rlc_pP->retrans_num_pdus > 0) && (rlc_pP->vt_a !=  rlc_pP->vt_s), "RLC AM ReTx start process Error: NbPDUtoRetx=%d vtA=%d vtS=%d  LcId=%d !\n",
			  rlc_pP->retrans_num_pdus,rlc_pP->vt_a,rlc_pP->vt_s,rlc_pP->channel_id);

	  do
	  {
		  tx_data_pdu_management = &rlc_pP->tx_data_pdu_buffer[sn % RLC_AM_WINDOW_SIZE];
		  if ((tx_data_pdu_management->flags.retransmit) && (tx_data_pdu_management->flags.max_retransmit == 0))
		  {
			  AssertFatal (tx_data_pdu_management->sn == sn, "RLC AM ReTx PDU Error: SN Error pdu_sn=%d sn=%d vtA=%d vtS=%d LcId=%d !\n",
					  tx_data_pdu_management->sn,sn,rlc_pP->vt_a,rlc_pP->vt_s,rlc_pP->channel_id);
			  AssertFatal (tx_data_pdu_management->flags.transmitted == 1, "RLC AM ReTx PDU Error: State Error sn=%d vtA=%d vtS=%d LcId=%d !\n",
			  					  sn,rlc_pP->vt_a,rlc_pP->vt_s,rlc_pP->channel_id);
			  AssertFatal (tx_data_pdu_management->retx_payload_size > 0, "RLC AM ReTx PDU Error: No Data to Retx sn=%d vtA=%d vtS=%d LcId=%d !\n",
					  sn,rlc_pP->vt_a,rlc_pP->vt_s,rlc_pP->channel_id);

			  /* Either the whole RLC PDU is to be transmitted and there is enough MAC TBS or there is minimum TBS size for transmitting 1 AM PDU segment */
			  if ((tx_data_pdu_management->retx_payload_size == tx_data_pdu_management->payload_size) && (rlc_pP->nb_bytes_requested_by_mac >= tx_data_pdu_management->header_and_payload_size))
			  {
				  /* check maxretx is not hit */
				  if (tx_data_pdu_management->retx_count_next <= rlc_pP->max_retx_threshold)
				  {
					  pdu_p = rlc_am_retransmit_get_copy(ctxt_pP, rlc_pP, sn);

					  if (pdu_p != NULL)
					  {
						  rlc_pP->retrans_num_bytes_to_retransmit -= tx_data_pdu_management->retx_payload_size;
						  rlc_pP->retrans_num_pdus --;
						  tx_data_pdu_management->retx_payload_size = 0;
						  tx_data_pdu_management->flags.retransmit = 0;

				    	  // update stats
				          rlc_pP->stat_tx_data_pdu                   += 1;
				          rlc_pP->stat_tx_retransmit_pdu             += 1;
				          rlc_pP->stat_tx_retransmit_pdu_by_status   += 1;
				          rlc_pP->stat_tx_data_bytes                 += tx_data_pdu_management->payload_size;
				          rlc_pP->stat_tx_retransmit_bytes           += tx_data_pdu_management->payload_size;
				          rlc_pP->stat_tx_retransmit_bytes_by_status += tx_data_pdu_management->payload_size;

					  }
				  }
				  else
				  {
					  // TO DO : RLC Notification to RRC + ReEstablishment procedure
					  tx_data_pdu_management->flags.max_retransmit = 1;
				  }
			  }
			  else if (rlc_pP->nb_bytes_requested_by_mac >= 5)
			  {
				  /* Resegmentation case */
				  /* check maxretx is not hit */
				  if (tx_data_pdu_management->retx_count_next <= rlc_pP->max_retx_threshold)
				  {
					  sdu_size_t pdu_data_size = 0;

					  pdu_p = rlc_am_retransmit_get_am_segment(ctxt_pP, rlc_pP, tx_data_pdu_management,&pdu_data_size);

					  if (pdu_p != NULL)
					  {
						  AssertFatal ((tx_data_pdu_management->retx_payload_size >= pdu_data_size) && (rlc_pP->retrans_num_bytes_to_retransmit >= pdu_data_size), "RLC AM ReTx PDU Segment Error: DataSize=%d PDUReTxsize=%d TotalReTxsize=%d sn=%d LcId=%d !\n",
								  pdu_data_size,tx_data_pdu_management->retx_payload_size,rlc_pP->retrans_num_bytes_to_retransmit,sn,rlc_pP->channel_id);

						  tx_data_pdu_management->retx_payload_size -= pdu_data_size;
						  rlc_pP->retrans_num_bytes_to_retransmit -= pdu_data_size;
						  if (tx_data_pdu_management->retx_payload_size == 0)
						  {
							  rlc_pP->retrans_num_pdus --;
							  tx_data_pdu_management->retx_payload_size = 0;
							  tx_data_pdu_management->flags.retransmit = 0;
						  }

				    	  // update stats
				          rlc_pP->stat_tx_data_pdu                   += 1;
				          rlc_pP->stat_tx_retransmit_pdu             += 1;
				          rlc_pP->stat_tx_retransmit_pdu_by_status   += 1;
				          rlc_pP->stat_tx_data_bytes                 += pdu_data_size;
				          rlc_pP->stat_tx_retransmit_bytes           += pdu_data_size;
				          rlc_pP->stat_tx_retransmit_bytes_by_status += pdu_data_size;

					  }
				  }
				  else
				  {
					  // TO DO : RLC Notification to RRC + ReEstablishment procedure
					  tx_data_pdu_management->flags.max_retransmit = 1;
				  }
			  }

			  if (pdu_p != NULL)
			  {
				  /* check polling */
				  rlc_am_pdu_sn_10_t* pdu_header_p   = (rlc_am_pdu_sn_10_t*) (&pdu_p->data[sizeof(struct mac_tb_req)]);
				  rlc_am_pdu_polling(ctxt_pP, rlc_pP, pdu_header_p, tx_data_pdu_management->payload_size,false);

				  tx_data_pdu_management->retx_count = tx_data_pdu_management->retx_count_next;

				  break;
			  }

		  }

		  sn = RLC_AM_NEXT_SN(sn);
	  } while((sn != sn_end) && (rlc_pP->retrans_num_pdus > 0));

	  return pdu_p;
}

#if 0
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
//-----------------------------------------------------------------------------
void rlc_am_retransmit_any_pdu(
  const protocol_ctxt_t* const  ctxt_pP,
  rlc_am_entity_t* const rlc_pP)
{
  rlc_sn_t             sn           = (rlc_pP->vt_s - 1) & RLC_AM_SN_MASK;
  rlc_sn_t             sn_end       = (rlc_pP->vt_a - 1) & RLC_AM_SN_MASK;
  int                  found_pdu    = 0;
  rlc_sn_t             found_pdu_sn = 0; // avoid warning
  mem_block_t*         pdu_p        = NULL;
  rlc_am_pdu_sn_10_t*  pdu_sn_10_p  = NULL;

1242 1243
  LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[FORCE-TRAFFIC] rlc_am_retransmit_any_pdu()\n",
        PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP));
1244 1245

  while (sn != sn_end) {
1246
    if (rlc_pP->tx_data_pdu_buffer[sn].mem_block != NULL) {
1247 1248 1249 1250 1251
      if (!found_pdu) {
        found_pdu = 1;
        found_pdu_sn = sn;
      }

1252
      if (rlc_pP->tx_data_pdu_buffer[sn].header_and_payload_size <= rlc_pP->nb_bytes_requested_by_mac) {
1253 1254 1255
        LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[FORCE-TRAFFIC] RE-SEND DATA PDU SN %04d\n",
              PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
              sn);
1256
#if MESSAGE_CHART_GENERATOR_RLC_MAC
1257 1258 1259 1260
        MSC_LOG_EVENT((ctxt_pP->enb_flag == ENB_FLAG_YES) ? MSC_RLC_ENB:MSC_RLC_UE,\
                               "0 "PROTOCOL_RLC_AM_MSC_FMT" RTX any pdu found SN %u",\
                               PROTOCOL_RLC_AM_MSC_ARGS(ctxt_pP,rlc_pP), sn);
#endif
1261 1262 1263 1264
        rlc_am_nack_pdu (ctxt_pP, rlc_pP, sn, 0, 0x7FFF);
        // no need for update rlc_pP->nb_bytes_requested_by_mac
        pdu_p = rlc_am_retransmit_get_copy(ctxt_pP, rlc_pP, sn);
        pdu_sn_10_p = (rlc_am_pdu_sn_10_t*) (&pdu_p->data[sizeof(struct mac_tb_req)]);
1265
        rlc_am_pdu_polling(ctxt_pP, rlc_pP, pdu_sn_10_p, rlc_pP->tx_data_pdu_buffer[sn].header_and_payload_size,false);
fnabet's avatar
fnabet committed
1266 1267 1268 1269 1270
        //BugFix: polling is checked and done in function above !
        //pdu_sn_10_p->b1 = pdu_sn_10_p->b1 | 0x20;
        //BugFix : pdu_without_poll and byte_without_poll are reset only if a Poll is transmitted
        //rlc_pP->c_pdu_without_poll     = 0;
        //rlc_pP->c_byte_without_poll    = 0;
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
        //rlc_pP->poll_sn = (rlc_pP->vt_s -1) & RLC_AM_SN_MASK;
        rlc_pP->stat_tx_data_pdu                   += 1;
        rlc_pP->stat_tx_retransmit_pdu             += 1;
        rlc_pP->stat_tx_data_bytes                 += ((struct mac_tb_req*)(pdu_p->data))->tb_size;
        rlc_pP->stat_tx_retransmit_bytes           += ((struct mac_tb_req*)(pdu_p->data))->tb_size;
        list_add_tail_eurecom (pdu_p, &rlc_pP->pdus_to_mac_layer);
        return;
      }
    }

    sn = (sn - 1) & RLC_AM_SN_MASK;
  }

  // no pdu_p with correct size has been found
  // so re-segment a pdu_p if possible
  if (found_pdu) {
1287 1288
    LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[FORCE-TRAFFIC] SEND SEGMENT OF DATA PDU SN %04d\n",
          PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
1289 1290 1291
          found_pdu_sn);

    if (rlc_pP->nb_bytes_requested_by_mac > 4) {
1292
#if MESSAGE_CHART_GENERATOR_RLC_MAC
1293 1294 1295 1296
        MSC_LOG_EVENT((ctxt_pP->enb_flag == ENB_FLAG_YES) ? MSC_RLC_ENB:MSC_RLC_UE,\
                               "0 "PROTOCOL_RLC_AM_MSC_FMT" RTX any pdu found SN %u (subseg)",\
                               PROTOCOL_RLC_AM_MSC_ARGS(ctxt_pP,rlc_pP), found_pdu_sn);
#endif
1297 1298 1299
      rlc_am_nack_pdu (ctxt_pP, rlc_pP, found_pdu_sn, 0, 0x7FFF);
      pdu_p = rlc_am_retransmit_get_subsegment(ctxt_pP, rlc_pP, found_pdu_sn, &rlc_pP->nb_bytes_requested_by_mac);
      pdu_sn_10_p = (rlc_am_pdu_sn_10_t*) (&pdu_p->data[sizeof(struct mac_tb_req)]);
1300
      rlc_am_pdu_polling(ctxt_pP, rlc_pP, pdu_sn_10_p, rlc_pP->tx_data_pdu_buffer[found_pdu_sn].header_and_payload_size,false);
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
      pdu_sn_10_p->b1 = pdu_sn_10_p->b1 | 0x20;
      rlc_pP->c_pdu_without_poll     = 0;
      rlc_pP->c_byte_without_poll    = 0;
      //rlc_pP->poll_sn = (rlc_pP->vt_s -1) & RLC_AM_SN_MASK;
      rlc_pP->stat_tx_data_pdu                   += 1;
      rlc_pP->stat_tx_retransmit_pdu             += 1;
      rlc_pP->stat_tx_data_bytes                 += ((struct mac_tb_req*)(pdu_p->data))->tb_size;
      rlc_pP->stat_tx_retransmit_bytes           += ((struct mac_tb_req*)(pdu_p->data))->tb_size;
      list_add_tail_eurecom (pdu_p, &rlc_pP->pdus_to_mac_layer);
      return;
    } else {
1312 1313
      LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[FORCE-TRAFFIC] ... BUT NOT ENOUGH BYTES ALLOWED BY MAC %0d\n",
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
1314
            rlc_pP->nb_bytes_requested_by_mac);
1315
    }
1316
  }
1317
}
1318
#endif