rlc_am_status_report.c 51.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_STATUS_REPORT_C 1
24
//-----------------------------------------------------------------------------
gauthier's avatar
gauthier committed
25
#include <string.h>
26 27 28
//-----------------------------------------------------------------------------
#include "platform_types.h"
//-----------------------------------------------------------------------------
29
#if ENABLE_ITTI
30 31
# include "intertask_interface.h"
#endif
gauthier's avatar
gauthier committed
32
#include "assertions.h"
33 34 35 36
#include "list.h"
#include "rlc_am.h"
#include "LAYER2/MAC/extern.h"
#include "UTIL/LOG/log.h"
37

38
#   if ENABLE_ITTI
39
//-----------------------------------------------------------------------------
40 41
void
rlc_am_itti_display_status_ind_infos(
42 43 44
  const protocol_ctxt_t* const            ctxt_pP,
  const rlc_am_entity_t *const            rlc_pP,
  const rlc_am_control_pdu_info_t *const  pdu_info_pP)
45
{
46 47 48
  char                 message_string[1000];
  size_t               message_string_size = 0;
  MessageDef          *msg_p;
49

50
  int num_nack;
51

52 53 54
  if (!pdu_info_pP->d_c) {
    message_string_size += sprintf(&message_string[message_string_size], "Bearer      : %u\n", rlc_pP->rb_id);
    message_string_size += sprintf(&message_string[message_string_size], "CONTROL PDU ACK SN %04d", pdu_info_pP->ack_sn);
55

56 57 58 59 60 61 62 63 64
    for (num_nack = 0; num_nack < pdu_info_pP->num_nack; num_nack++) {
      if (pdu_info_pP->nack_list[num_nack].e2) {
        message_string_size += sprintf(&message_string[message_string_size], "\n\tNACK SN %04d SO START %05d SO END %05d",  pdu_info_pP->nack_list[num_nack].nack_sn,
                                       pdu_info_pP->nack_list[num_nack].so_start,
                                       pdu_info_pP->nack_list[num_nack].so_end);
      } else {
        message_string_size += sprintf(&message_string[message_string_size], "\n\tNACK SN %04d",  pdu_info_pP->nack_list[num_nack].nack_sn);
      }
    }
65

66
    message_string_size += sprintf(&message_string[message_string_size], "\n");
67

68 69 70 71
    msg_p = itti_alloc_new_message_sized (ctxt_pP->enb_flag > 0 ? TASK_RLC_ENB:TASK_RLC_UE , RLC_AM_STATUS_PDU_IND, message_string_size + sizeof (IttiMsgText));
    msg_p->ittiMsg.rlc_am_status_pdu_ind.size = message_string_size;
    memcpy(&msg_p->ittiMsg.rlc_am_status_pdu_ind.text, message_string, message_string_size);

72
    itti_send_msg_to_task(TASK_UNKNOWN, ctxt_pP->instance, msg_p);
73
  }
74 75 76 77
}

#   endif

78
//-----------------------------------------------------------------------------
79
uint16_t rlc_am_read_bit_field(
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
  uint8_t** data_ppP,
  unsigned int* bit_pos_pP,
  const signed int bits_to_readP)
{
  uint16_t        value     = 0;
  unsigned int bits_read = 0;
  signed int bits_to_read = bits_to_readP;

  do {
    // bits read > bits to read
    if ((8 - *bit_pos_pP) > bits_to_read) {
      bits_read = 8 - *bit_pos_pP;
      value = (value << bits_to_read) | ((((uint16_t)(**data_ppP)) & (uint16_t)(0x00FF >> *bit_pos_pP)) >> (bits_read -
                                         bits_to_read));
      *bit_pos_pP = *bit_pos_pP + bits_to_read;
      return value;
      // bits read == bits to read
    } else if ((8 - *bit_pos_pP) == bits_to_read) {
      value = (value << bits_to_read) | (((uint16_t)(**data_ppP)) & (uint16_t)(0x00FF >> *bit_pos_pP));
      *bit_pos_pP = 0;
      *data_ppP = *data_ppP + 1;
      return value;
      // bits read < bits to read
    } else {
      bits_read = 8 - *bit_pos_pP;
      value = (value << bits_read) | ((((uint16_t)(**data_ppP)) & (uint16_t)(0x00FF >> *bit_pos_pP)));
      *bit_pos_pP = 0;
      *data_ppP = *data_ppP + 1;
      bits_to_read = bits_to_read - bits_read;
    }
  } while (bits_to_read > 0);

  return value;
113 114
}
//-----------------------------------------------------------------------------
115 116
void
rlc_am_write8_bit_field(
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
  uint8_t** data_ppP,
  unsigned int* bit_pos_pP,
  const signed int bits_to_writeP,
  const uint8_t valueP)
{
  unsigned int available_bits;
  signed int bits_to_write= bits_to_writeP;

  do {
    available_bits = 8 - *bit_pos_pP;

    // available_bits > bits to write
    if (available_bits > bits_to_write) {
      **data_ppP = **data_ppP | (((valueP & (((uint8_t)0xFF) >> (available_bits - bits_to_write)))) << (available_bits -
                                 bits_to_write));
      *bit_pos_pP = *bit_pos_pP + bits_to_write;
      return;
      // bits read == bits to read
    } else if (available_bits == bits_to_write) {
      **data_ppP = **data_ppP | (valueP & (((uint8_t)0xFF) >> (8 - bits_to_write)));
      *bit_pos_pP = 0;
      *data_ppP = *data_ppP + 1;
      return;
      // available_bits < bits to write
    } else {
      **data_ppP = **data_ppP  | (valueP >> (bits_to_write - available_bits));
      *bit_pos_pP = 0;
      *data_ppP = *data_ppP + 1;
      bits_to_write = bits_to_write - available_bits;
    }
  } while (bits_to_write > 0);
148 149
}
//-----------------------------------------------------------------------------
150 151
void
rlc_am_write16_bit_field(
152 153 154 155 156 157
  uint8_t** data_ppP,
  unsigned int* bit_pos_pP,
  signed int bits_to_writeP,
  const uint16_t valueP)
{
  assert(bits_to_writeP <= 16);
158

159 160 161 162 163 164
  if (bits_to_writeP > 8) {
    rlc_am_write8_bit_field(data_ppP,bit_pos_pP,  bits_to_writeP - 8, (uint8_t)(valueP >> 8));
    rlc_am_write8_bit_field(data_ppP,bit_pos_pP,  8, (uint8_t)(valueP & 0x00FF));
  } else {
    rlc_am_write8_bit_field(data_ppP,bit_pos_pP,  bits_to_writeP, (uint8_t)(valueP & 0x00FF));
  }
165 166
}
//-----------------------------------------------------------------------------
167 168
signed int
rlc_am_get_control_pdu_infos(
169 170 171
  rlc_am_pdu_sn_10_t* const        header_pP,
  sdu_size_t * const               total_size_pP,
  rlc_am_control_pdu_info_t* const pdu_info_pP)
172
{
173 174 175 176 177 178 179 180
  memset(pdu_info_pP, 0, sizeof (rlc_am_control_pdu_info_t));

  pdu_info_pP->d_c = header_pP->b1 >> 7;


  if (!pdu_info_pP->d_c) {
    pdu_info_pP->cpt    = (header_pP->b1 >> 4) & 0x07;

181 182 183
    if (pdu_info_pP->cpt != 0x00) {
      return -3;
    }
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

    pdu_info_pP->ack_sn = ((header_pP->b2 >> 2) & 0x3F) | (((uint16_t)(header_pP->b1 & 0x0F)) << 6);
    pdu_info_pP->e1     = (header_pP->b2 >> 1) & 0x01;
    //*total_size_pP -= 1;

    if (pdu_info_pP->e1) {
      unsigned int nack_to_read  = 1;
      unsigned int bit_pos       = 7; // range from 0 (MSB/left) to 7 (LSB/right)
      uint8_t*        byte_pos_p      = &header_pP->b2;

      while (nack_to_read)  {
        pdu_info_pP->nack_list[pdu_info_pP->num_nack].nack_sn = rlc_am_read_bit_field(&byte_pos_p, &bit_pos, 10);
        pdu_info_pP->nack_list[pdu_info_pP->num_nack].e1      = rlc_am_read_bit_field(&byte_pos_p, &bit_pos, 1);
        pdu_info_pP->nack_list[pdu_info_pP->num_nack].e2      = rlc_am_read_bit_field(&byte_pos_p, &bit_pos, 1);

        // READ SOstart, SOend field
        if (pdu_info_pP->nack_list[pdu_info_pP->num_nack].e2) {
          pdu_info_pP->nack_list[pdu_info_pP->num_nack].so_start = rlc_am_read_bit_field(&byte_pos_p, &bit_pos, 15);
          pdu_info_pP->nack_list[pdu_info_pP->num_nack].so_end   = rlc_am_read_bit_field(&byte_pos_p, &bit_pos, 15);
gauthier's avatar
gauthier committed
203
        } else {
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
          pdu_info_pP->nack_list[pdu_info_pP->num_nack].so_start = 0;
          // 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)
          pdu_info_pP->nack_list[pdu_info_pP->num_nack].so_end   = 0x7FFF;
        }

        pdu_info_pP->num_nack = pdu_info_pP->num_nack + 1;

        if (!pdu_info_pP->nack_list[pdu_info_pP->num_nack - 1].e1) {
          nack_to_read = 0;
          *total_size_pP = *total_size_pP - (sdu_size_t)((uint64_t)byte_pos_p + (uint64_t)((bit_pos + 7)/8) - (uint64_t)header_pP);
          return 0;
        }

        if (pdu_info_pP->num_nack == RLC_AM_MAX_NACK_IN_STATUS_PDU) {
          *total_size_pP = *total_size_pP - (sdu_size_t)((uint64_t)byte_pos_p + (uint64_t)((bit_pos + 7)/8) - (uint64_t)header_pP);
          return -2;
221
        }
222 223 224
      }

      *total_size_pP = *total_size_pP - (sdu_size_t)((uint64_t)byte_pos_p + (uint64_t)((bit_pos + 7)/8) - (uint64_t)header_pP);
225
    } else {
226
      *total_size_pP = *total_size_pP - 2;
227
    }
228 229 230 231 232

    return 0;
  } else {
    return -1;
  }
233 234
}
//-----------------------------------------------------------------------------
235 236
void
rlc_am_display_control_pdu_infos(
237 238
  const rlc_am_control_pdu_info_t* const pdu_info_pP
)
239
{
240
  int num_nack;
241

242 243
  if (!pdu_info_pP->d_c) {
    LOG_T(RLC, "CONTROL PDU ACK SN %04d", pdu_info_pP->ack_sn);
244

245 246 247 248 249 250 251 252
    for (num_nack = 0; num_nack < pdu_info_pP->num_nack; num_nack++) {
      if (pdu_info_pP->nack_list[num_nack].e2) {
        LOG_T(RLC, "\n\tNACK SN %04d SO START %05d SO END %05d",  pdu_info_pP->nack_list[num_nack].nack_sn,
              pdu_info_pP->nack_list[num_nack].so_start,
              pdu_info_pP->nack_list[num_nack].so_end);
      } else {
        LOG_T(RLC, "\n\tNACK SN %04d",  pdu_info_pP->nack_list[num_nack].nack_sn);
      }
253
    }
254 255 256 257 258

    LOG_T(RLC, "\n");
  } else {
    LOG_E(RLC, "CAN'T DISPLAY CONTROL INFO: PDU IS DATA PDU\n");
  }
259 260
}
//-----------------------------------------------------------------------------
261 262
void
rlc_am_receive_process_control_pdu(
263 264 265 266 267
  const protocol_ctxt_t* const  ctxt_pP,
  rlc_am_entity_t *const rlc_pP,
  mem_block_t* const tb_pP,
  uint8_t** first_byte_ppP,
  sdu_size_t * const tb_size_in_bytes_pP)
268
{
269 270
  rlc_am_pdu_sn_10_t *rlc_am_pdu_sn_10_p = (rlc_am_pdu_sn_10_t*)*first_byte_ppP;
  sdu_size_t          initial_pdu_size   = *tb_size_in_bytes_pP;
271
  rlc_sn_t        ack_sn    = RLC_AM_NEXT_SN(rlc_pP->vt_a);
272 273 274 275 276 277 278
  rlc_sn_t        sn_cursor = rlc_pP->vt_a;
  rlc_sn_t		vt_a_new  = rlc_pP->vt_a;
  rlc_sn_t		sn_data_cnf;
  rlc_sn_t        nack_sn,prev_nack_sn;
  sdu_size_t		data_cnf_so_stop = 0x7FFF;
  unsigned int nack_index;
  boolean_t status = TRUE;
gauthier's avatar
gauthier committed
279

280
  if (rlc_am_get_control_pdu_infos(rlc_am_pdu_sn_10_p, tb_size_in_bytes_pP, &rlc_pP->control_pdu_info) >= 0) {
gauthier's avatar
gauthier committed
281

282
    rlc_am_tx_buffer_display(ctxt_pP, rlc_pP, " TX BUFFER BEFORE PROCESS OF STATUS PDU");
283 284
    LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT" RX CONTROL PDU VT(A) %04d VT(S) %04d POLL_SN %04d ACK_SN %04d\n",
          PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
gauthier's avatar
gauthier committed
285 286 287
          rlc_pP->vt_a,
          rlc_pP->vt_s,
          rlc_pP->poll_sn,
288 289
          rlc_pP->control_pdu_info.ack_sn);
    rlc_am_display_control_pdu_infos(&rlc_pP->control_pdu_info);
290

291
    ack_sn    = rlc_pP->control_pdu_info.ack_sn;
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
    // 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.

    // 5.2.2.2    Reception of a STATUS report
    // Upon reception of a STATUS report from the receiving RLC AM entity the
    // transmitting side of an AM RLC entity shall:
    // - if the STATUS report comprises a positive or negative
    //     acknowledgement for the RLC data PDU with sequence number equal to
    //     POLL_SN:
    //     - if t-PollRetransmit is running:
    //         - stop and reset t-PollRetransmit.
    assert(ack_sn < RLC_AM_SN_MODULO);
313
    assert(rlc_pP->control_pdu_info.num_nack < RLC_AM_MAX_NACK_IN_STATUS_PDU);
314

315
    /* Note : ackSn can be equal to current vtA only in case the status pdu contains a list of nack_sn with same value = vtA with SOStart/SOEnd */
316 317 318
    /* and meaning the report is not complete due to not enough ressources to fill all SOStart/SOEnd of this NACK_SN */
    if (RLC_AM_DIFF_SN(rlc_pP->vt_s,rlc_pP->vt_a) >= RLC_AM_DIFF_SN(ack_sn,rlc_pP->vt_a))
    {
319
      if (rlc_pP->control_pdu_info.num_nack == 0) {
320 321
        while (sn_cursor != ack_sn) {
          rlc_am_ack_pdu(ctxt_pP, rlc_pP, sn_cursor);
322
          sn_cursor = RLC_AM_NEXT_SN(sn_cursor);
323
        }
324 325 326

        vt_a_new = ack_sn;
        sn_data_cnf = RLC_AM_PREV_SN(vt_a_new);
327
      } else {
328
        nack_index = 0;
329
        nack_sn   = rlc_pP->control_pdu_info.nack_list[nack_index].nack_sn;
330
        prev_nack_sn = 0x3FFF;
331

332 333 334 335 336 337 338 339
        while (sn_cursor != nack_sn) {
            rlc_am_ack_pdu(ctxt_pP, rlc_pP, sn_cursor);
            sn_cursor = RLC_AM_NEXT_SN(sn_cursor);
        }

        vt_a_new = nack_sn;

        // catch DataCfn
340
        rlc_am_tx_data_pdu_management_t *tx_data_pdu_buffer_p = &rlc_pP->tx_data_pdu_buffer[nack_sn % RLC_AM_WINDOW_SIZE];
341 342 343 344 345 346 347 348 349 350 351 352 353
        if (tx_data_pdu_buffer_p->retx_payload_size == tx_data_pdu_buffer_p->payload_size) {
        	sn_data_cnf   = RLC_AM_PREV_SN(nack_sn);
        }
        else if (tx_data_pdu_buffer_p->nack_so_start != 0) {
        	sn_data_cnf       = nack_sn;
        	data_cnf_so_stop    = tx_data_pdu_buffer_p->nack_so_start - 1;
        }
        else {
        	sn_data_cnf       = RLC_AM_PREV_SN(nack_sn);
        }


        while ((sn_cursor != ack_sn) && (status)) {
354 355 356 357 358
          if (sn_cursor != nack_sn) {
            rlc_am_ack_pdu(ctxt_pP,
                           rlc_pP,
                           sn_cursor);
          } else {
359
        	status = rlc_am_nack_pdu (ctxt_pP,
360
                             rlc_pP,
361 362
							 nack_sn,
							 prev_nack_sn,
363 364
                             rlc_pP->control_pdu_info.nack_list[nack_index].so_start,
                             rlc_pP->control_pdu_info.nack_list[nack_index].so_end);
365 366

            nack_index = nack_index + 1;
367
            prev_nack_sn = nack_sn;
368

369 370
            if (nack_index < rlc_pP->control_pdu_info.num_nack) {
               nack_sn = rlc_pP->control_pdu_info.nack_list[nack_index].nack_sn;
371
            }
372 373 374 375 376 377 378
            else if (nack_sn != ack_sn) {
            	/* general case*/
            	nack_sn = ack_sn;
            }
            else {
            	/*specific case when the sender did not have enough TBS to fill all SOStart SOEnd for this NACK_SN */
            	break;
379
            }
380
          }
Bilel's avatar
Bilel committed
381 382 383 384
          if (prev_nack_sn != nack_sn) {
        	  /* do not increment sn_cursor in case of several informations for the same nack_sn */
              sn_cursor = (sn_cursor + 1)  & RLC_AM_SN_MASK;
          }
385
        }
386
      }
387 388


389
    } else {
Gabriel's avatar
Gabriel committed
390 391
      LOG_N(RLC, PROTOCOL_RLC_AM_CTXT_FMT" WARNING CONTROL PDU ACK SN %d OUT OF WINDOW vtA=%d vtS=%d\n",
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),ack_sn,rlc_pP->vt_a,rlc_pP->vt_s);
392
      status = FALSE;
393 394
    }
  } else {
395 396
    LOG_W(RLC, PROTOCOL_RLC_AM_CTXT_FMT" ERROR IN DECODING CONTROL PDU\n",
          PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP));
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
    status = FALSE;
  }

  if (status) {
      /* Check for Stopping TpollReTx */
      if ((rlc_pP->poll_sn != RLC_SN_UNDEFINED) &&
          (RLC_AM_DIFF_SN(ack_sn,rlc_pP->vt_a) > RLC_AM_DIFF_SN(rlc_pP->poll_sn,rlc_pP->vt_a))) {
        rlc_am_stop_and_reset_timer_poll_retransmit(ctxt_pP, rlc_pP);
        rlc_pP->poll_sn = RLC_SN_UNDEFINED;
      }

      //TODO : this part does not cover all cases of Data Cnf and move it at the end of Status PDU processing
      sn_cursor = rlc_pP->vt_a;

      /* Handle all acked PDU up to and excluding sn_data_cnf */
      while (sn_cursor != sn_data_cnf) {
    	  rlc_am_pdu_sdu_data_cnf(ctxt_pP,rlc_pP,sn_cursor);
    	  sn_cursor = RLC_AM_NEXT_SN(sn_cursor);
      }

      // Handle last SN. TO DO : case of PDU partially ACKED with SDU to be data conf
      if (data_cnf_so_stop == 0x7FFF) {
    	  rlc_am_pdu_sdu_data_cnf(ctxt_pP,rlc_pP,sn_data_cnf);
      }

Gabriel's avatar
Gabriel committed
422 423 424
      LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT" RECEIVE STATUS PDU ACK_SN=%d NewvtA=%d OldvtA=%d SnDataCnf=%d DataCnfSOStop=%d vtS=%d\n",
                  PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),ack_sn,vt_a_new,rlc_pP->vt_a,sn_data_cnf,data_cnf_so_stop,rlc_pP->vt_s);

425 426 427
      /* Update vtA and vtMS */
      rlc_pP->vt_a = vt_a_new;
      rlc_pP->vt_ms = (rlc_pP->vt_a + RLC_AM_WINDOW_SIZE) & RLC_AM_SN_MASK;
428
  }
429

430
  *first_byte_ppP = (uint8_t*)((uint64_t)*first_byte_ppP + initial_pdu_size - *tb_size_in_bytes_pP);
winckel's avatar
winckel committed
431

432
  free_mem_block(tb_pP, __func__);
433
  rlc_am_tx_buffer_display(ctxt_pP, rlc_pP, NULL);
434 435
}
//-----------------------------------------------------------------------------
436 437
int
rlc_am_write_status_pdu(
438 439 440 441
  const protocol_ctxt_t* const     ctxt_pP,
  rlc_am_entity_t *const           rlc_pP,
  rlc_am_pdu_sn_10_t* const        rlc_am_pdu_sn_10_pP,
  rlc_am_control_pdu_info_t* const pdu_info_pP)
442 443
{
  unsigned int bit_pos       = 4; // range from 0 (MSB/left) to 7 (LSB/right)
444
  uint8_t*        byte_pos_p    = &rlc_am_pdu_sn_10_pP->b1;
gauthier's avatar
gauthier committed
445 446
  unsigned int index         = 0;
  unsigned int num_bytes     = 0;
447

gauthier's avatar
gauthier committed
448
  rlc_am_write16_bit_field(&byte_pos_p, &bit_pos, 10, pdu_info_pP->ack_sn);
449

gauthier's avatar
gauthier committed
450
  if (pdu_info_pP->num_nack > 0) {
451
    rlc_am_write8_bit_field(&byte_pos_p, &bit_pos, 1, 1);
452
  } else {
453
    rlc_am_write8_bit_field(&byte_pos_p, &bit_pos, 1, 0);
454
  }
455

gauthier's avatar
gauthier committed
456
  for (index = 0; index < pdu_info_pP->num_nack ; index++) {
457 458 459 460 461 462 463 464 465
    rlc_am_write16_bit_field(&byte_pos_p, &bit_pos, 10, pdu_info_pP->nack_list[index].nack_sn);
    rlc_am_write8_bit_field(&byte_pos_p, &bit_pos, 1,  pdu_info_pP->nack_list[index].e1);
    rlc_am_write8_bit_field(&byte_pos_p, &bit_pos, 1,  pdu_info_pP->nack_list[index].e2);

    // if SO_START SO_END fields
    if (pdu_info_pP->nack_list[index].e2 > 0) {
      rlc_am_write16_bit_field(&byte_pos_p, &bit_pos, 15, pdu_info_pP->nack_list[index].so_start);
      rlc_am_write16_bit_field(&byte_pos_p, &bit_pos, 15, pdu_info_pP->nack_list[index].so_end);
    }
466
  }
467

468 469
  ptrdiff_t diff = byte_pos_p - &rlc_am_pdu_sn_10_pP->b1; // this is the difference in terms of typeof(byte_pos_p), which is uint8_t
  num_bytes = diff;
470

471
  if (bit_pos > 0) {
472
    num_bytes += 1;
473
  }
474

475
#if TRACE_RLC_AM_STATUS_CREATION
476 477 478
  LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT" WROTE STATUS PDU %d BYTES\n",
        PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
        num_bytes);
479 480 481 482
#endif
  return num_bytes;
}
//-----------------------------------------------------------------------------
483 484
void
rlc_am_send_status_pdu(
485 486 487
  const protocol_ctxt_t* const  ctxt_pP,
  rlc_am_entity_t *const rlc_pP
)
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
{
  // When STATUS reporting has been triggered, the receiving side of an AM RLC entity shall:
  // - if t-StatusProhibit is not running:
  //     - at the first transmission opportunity indicated by lower layer, construct a STATUS PDU and deliver it to lower layer;
  // - else:
  //     - at the first transmission opportunity indicated by lower layer after t-StatusProhibit expires, construct a single
  //       STATUS PDU even if status reporting was triggered several times while t-StatusProhibit was running and
  //       deliver it to lower layer;
  //
  // When a STATUS PDU has been delivered to lower layer, the receiving side of an AM RLC entity shall:
  //     - start t-StatusProhibit.
  //
  // When constructing a STATUS PDU, the AM RLC entity shall:
  //     - for the AMD PDUs with SN such that VR(R) <= SN < VR(MR) that has not been completely received yet, in
  //       increasing SN of PDUs and increasing byte segment order within PDUs, starting with SN = VR(R) up to
  //       the point where the resulting STATUS PDU still fits to the total size of RLC PDU(s) indicated by lower layer:
  //         - for an AMD PDU for which no byte segments have been received yet::
  //             - include in the STATUS PDU a NACK_SN which is set to the SN of the AMD PDU;
  //         - for a continuous sequence of byte segments of a partly received AMD PDU that have not been received yet:
  //             - include in the STATUS PDU a set of NACK_SN, SOstart and SOend
  //     - set the ACK_SN to the SN of the next not received RLC Data PDU which is not indicated as missing in the
  //       resulting STATUS PDU.

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
  signed int                    nb_bits_to_transmit   = rlc_pP->nb_bytes_requested_by_mac << 3;
  // minimum header size in bits to be transmitted: D/C + CPT + ACK_SN + E1
  signed int                    nb_bits_transmitted	  = RLC_AM_PDU_D_C_BITS + RLC_AM_STATUS_PDU_CPT_LENGTH + RLC_AM_SN_BITS + RLC_AM_PDU_E_BITS;
  rlc_am_control_pdu_info_t     control_pdu_info;
  rlc_am_pdu_info_t            *pdu_info_cursor_p     = NULL;
  rlc_sn_t                      sn_cursor             = 0;
  rlc_sn_t                      sn_nack               = rlc_pP->vr_r;
  mem_block_t                  *cursor_p              = rlc_pP->receiver_buffer.head;
  int                           all_segments_received = 0;
  int                           waited_so             = 0;
  mem_block_t                  *tb_p                  = NULL;
  sdu_size_t                    pdu_size              = 0;
  boolean_t						status_report_completed	= false;
  boolean_t						segment_loop_end	  = false;

  memset(&control_pdu_info, 0, sizeof(rlc_am_control_pdu_info_t));

#if TRACE_RLC_AM_STATUS_CREATION
  LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] nb_bits_to_transmit %d (15 already allocated for header)\n",
        PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
        nb_bits_to_transmit);
  rlc_am_rx_list_display(rlc_pP, " DISPLAY BEFORE CONSTRUCTION OF STATUS REPORT");
#endif

  /* Handle no NACK first */
  if (rlc_pP->vr_r == rlc_pP->vr_ms) {

	  control_pdu_info.ack_sn = rlc_pP->vr_ms;
	  status_report_completed = true;
fnabet's avatar
fnabet committed
540 541 542 543 544 545
#if TRACE_RLC_AM_STATUS_CREATION
          LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d ALL ACK WITH ACK_SN %04d\n",
                PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
                __LINE__,
				rlc_pP->vr_ms);
#endif
546
  }
fnabet's avatar
fnabet committed
547
  else if ((cursor_p != NULL) && ((nb_bits_transmitted + RLC_AM_SN_BITS + (RLC_AM_PDU_E_BITS << 1)) <= nb_bits_to_transmit)) {
548 549 550 551

    pdu_info_cursor_p       = &((rlc_am_rx_pdu_management_t*)(cursor_p->data))->pdu_info;
    sn_cursor             = pdu_info_cursor_p->sn;

fnabet's avatar
fnabet committed
552 553
    /* Set E1 bit for the presence of first NACK_SN/E1/E2 */
    control_pdu_info.e1 = 1;
554 555 556 557 558 559 560 561 562 563 564

    // 12 bits = size of NACK_SN field + E1, E2 bits
    // 42 bits = size of NACK_SN field + SO_START, SO_END fields, E1, E2 bits
    while ((!status_report_completed) && (RLC_AM_DIFF_SN(sn_nack,rlc_pP->vr_r) < RLC_AM_DIFF_SN(rlc_pP->vr_ms,rlc_pP->vr_r))
    		&& (cursor_p != NULL) && (nb_bits_transmitted <= nb_bits_to_transmit)) {

      pdu_info_cursor_p       = &((rlc_am_rx_pdu_management_t*)(cursor_p->data))->pdu_info;
      sn_cursor             = pdu_info_cursor_p->sn;
      all_segments_received = ((rlc_am_rx_pdu_management_t*)(cursor_p->data))->all_segments_received;

      /* First fill NACK_SN with each missing PDU between current sn_nack and sn_cursor */
Bilel's avatar
Bilel committed
565
      while ((sn_nack != sn_cursor) && (RLC_AM_DIFF_SN(sn_nack,rlc_pP->vr_r) < RLC_AM_DIFF_SN(rlc_pP->vr_ms,rlc_pP->vr_r))) {
566 567 568 569 570 571
    	  if (nb_bits_transmitted + RLC_AM_SN_BITS + (RLC_AM_PDU_E_BITS << 1) <= nb_bits_to_transmit) {
    		  /* Fill NACK_SN infos */
              control_pdu_info.nack_list[control_pdu_info.num_nack].nack_sn   = sn_nack;
              control_pdu_info.nack_list[control_pdu_info.num_nack].so_start  = 0;
              control_pdu_info.nack_list[control_pdu_info.num_nack].so_end    = RLC_AM_STATUS_PDU_SO_END_ALL_BYTES;
              control_pdu_info.nack_list[control_pdu_info.num_nack].e2        = 0;
fnabet's avatar
fnabet committed
572 573
              /* Set E1 for next NACK_SN. The last one will be cleared */
              control_pdu_info.nack_list[control_pdu_info.num_nack].e1  = 1;
574
              control_pdu_info.num_nack += 1;
fnabet's avatar
fnabet committed
575
              nb_bits_transmitted += (RLC_AM_SN_BITS + (RLC_AM_PDU_E_BITS << 1));
576 577 578 579 580 581 582 583 584 585 586 587 588
#if TRACE_RLC_AM_STATUS_CREATION
          LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d PREPARE SENDING NACK %04d\n",
                PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
                __LINE__,
				sn_nack);
#endif
    		  sn_nack = RLC_AM_NEXT_SN(sn_nack);
    	  }
    	  else {
    		  /* Not enough UL TBS*/
    		  /* latest value of sn_nack shall be used as ACK_SN */
    		  control_pdu_info.ack_sn = sn_nack;
    		  status_report_completed = true;
fnabet's avatar
fnabet committed
589 590 591 592 593 594
#if TRACE_RLC_AM_STATUS_CREATION
          LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d NOT ENOUGH TBS STOP WITH ACK_SN %04d\n",
                PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
                __LINE__,
				sn_nack);
#endif
595 596 597 598
    		  break;
    	  }
      }

Bilel's avatar
Bilel committed
599 600 601 602
      if (sn_nack == rlc_pP->vr_ms) {
    	  break;
      }

603
      /* Now process all Segments of sn_cursor if PDU not fully received */
fnabet's avatar
fnabet committed
604
      if ((!status_report_completed) && (all_segments_received == 0) && (sn_cursor != rlc_pP->vr_ms)) {
605 606 607 608 609
    	  AssertFatal (sn_nack == sn_cursor, "RLC AM Tx Status PDU Data sn_nack=%d and sn_cursor=%d should be equal LcId=%d\n",sn_nack,sn_cursor, rlc_pP->channel_id);

    	  /* First ensure there is enough TBS for at least 1 SOStart/SOEnd, else break */
    	  if ((nb_bits_transmitted + RLC_AM_SN_BITS + (RLC_AM_PDU_E_BITS << 1) + (RLC_AM_STATUS_PDU_SO_LENGTH << 1)) <= nb_bits_to_transmit) {
    		  /* Init loop flags */
fnabet's avatar
fnabet committed
610 611
              /* Check lsf */
    		  segment_loop_end = (pdu_info_cursor_p->lsf == 1);
612 613 614 615 616 617 618 619

    		  /* Init first SO Start according to first segment */
    		  if (pdu_info_cursor_p->so) {
    			  /* Fill the first SO */
                  control_pdu_info.nack_list[control_pdu_info.num_nack].nack_sn   = sn_cursor;
                  control_pdu_info.nack_list[control_pdu_info.num_nack].so_start  = 0;
                  control_pdu_info.nack_list[control_pdu_info.num_nack].so_end    = pdu_info_cursor_p->so - 1;
                  control_pdu_info.nack_list[control_pdu_info.num_nack].e2        = 1;
fnabet's avatar
fnabet committed
620 621
                  /* Set E1 for next NACK_SN. The last one will be cleared */
                  control_pdu_info.nack_list[control_pdu_info.num_nack].e1  = 1;
622 623 624 625 626 627
                  control_pdu_info.num_nack += 1;
                  nb_bits_transmitted += (RLC_AM_SN_BITS + (RLC_AM_PDU_E_BITS << 1) + (RLC_AM_STATUS_PDU_SO_LENGTH << 1));
#if TRACE_RLC_AM_STATUS_CREATION
            LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d PREPARE SENDING NACK %04d SO START %05d SO END %05d\n",
                  PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
                  __LINE__,
fnabet's avatar
fnabet committed
628 629 630
				  sn_cursor,
                  0,
				  pdu_info_cursor_p->so - 1);
631 632 633 634 635 636 637
#endif
                  waited_so = pdu_info_cursor_p->so + pdu_info_cursor_p->payload_size;
    		  }
    		  else {
        		  waited_so = pdu_info_cursor_p->payload_size;
    		  }

638 639 640 641 642 643 644
              /* Go to next segment */
              cursor_p = cursor_p->next;
              if (cursor_p != NULL)
              {
                  pdu_info_cursor_p     = &((rlc_am_rx_pdu_management_t*)(cursor_p->data))->pdu_info;
              }

645 646
    		  /* Find the first discontinuity and then fill SOStart/SOEnd */
    		  while (!segment_loop_end) {
fnabet's avatar
fnabet committed
647 648 649 650 651 652 653 654 655 656 657
    			  if ((cursor_p != NULL) && (pdu_info_cursor_p->sn == sn_cursor)) {

					  /* PDU segment is for the same SN*/
					  /* Check lsf */
					  segment_loop_end = (pdu_info_cursor_p->lsf == 1);

					  if (waited_so < pdu_info_cursor_p->so) {
						  /* SO is greater than previous received portion : gap identified to fill */
						  if ((nb_bits_transmitted + RLC_AM_SN_BITS + (RLC_AM_PDU_E_BITS << 1) + (RLC_AM_STATUS_PDU_SO_LENGTH << 1)) <= nb_bits_to_transmit) {
							  control_pdu_info.nack_list[control_pdu_info.num_nack].nack_sn   = sn_cursor;
							  control_pdu_info.nack_list[control_pdu_info.num_nack].so_start  = waited_so;
658
							  control_pdu_info.nack_list[control_pdu_info.num_nack].so_end    = pdu_info_cursor_p->so - 1;
fnabet's avatar
fnabet committed
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
							  control_pdu_info.nack_list[control_pdu_info.num_nack].e2        = 1;
							  /* Set E1 for next NACK_SN. The last one will be cleared */
							  control_pdu_info.nack_list[control_pdu_info.num_nack].e1  = 1;
							  control_pdu_info.num_nack += 1;
							  nb_bits_transmitted += (RLC_AM_SN_BITS + (RLC_AM_PDU_E_BITS << 1) + (RLC_AM_STATUS_PDU_SO_LENGTH << 1));
#if TRACE_RLC_AM_STATUS_CREATION
		LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d PREPARE SENDING NACK %04d SO START %05d SO END %05d\n",
			  PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
			  __LINE__,
			  sn_cursor,
			  waited_so,
			  pdu_info_cursor_p->so);
#endif
						  }
						  else {
							  /* Not enough resources to set a SOStart/SEnd, then set ACK_SN to current NACK_SN and stop Status PDU build */
							  control_pdu_info.ack_sn = sn_cursor;
							  status_report_completed = true;
							  segment_loop_end = true;
							  break;
						  }
					  }
					  else {
						  /* contiguous segment: only update waited_so */
						  /* Assuming so and payload_size updated according to duplication removal done at reception ... */
						  waited_so += pdu_info_cursor_p->payload_size;
					  }

					  /* Go to next received PDU or PDU Segment */
					  cursor_p = cursor_p->next;
	                  if (cursor_p != NULL)
	                  {
		                  pdu_info_cursor_p     = &((rlc_am_rx_pdu_management_t*)(cursor_p->data))->pdu_info;
	                  }

    			  } //end if (cursor_p != NULL) && (pdu_info_cursor_p->sn == sn_cursor)
    			  else {
    				  /* Previous PDU segment was the last one and did not have lsf indication : fill the latest gap */
697 698 699 700 701
    		    	  if ((nb_bits_transmitted + RLC_AM_SN_BITS + (RLC_AM_PDU_E_BITS << 1) + (RLC_AM_STATUS_PDU_SO_LENGTH << 1)) <= nb_bits_to_transmit) {
    	                      control_pdu_info.nack_list[control_pdu_info.num_nack].nack_sn   = sn_cursor;
    	                      control_pdu_info.nack_list[control_pdu_info.num_nack].so_start  = waited_so;
    	                      control_pdu_info.nack_list[control_pdu_info.num_nack].so_end    = RLC_AM_STATUS_PDU_SO_END_ALL_BYTES;
    	                      control_pdu_info.nack_list[control_pdu_info.num_nack].e2        = 1;
fnabet's avatar
fnabet committed
702 703
    	                      /* Set E1 for next NACK_SN. The last one will be cleared */
    	                      control_pdu_info.nack_list[control_pdu_info.num_nack].e1  = 1;
704 705
    	                      control_pdu_info.num_nack += 1;
    	                      nb_bits_transmitted += (RLC_AM_SN_BITS + (RLC_AM_PDU_E_BITS << 1) + (RLC_AM_STATUS_PDU_SO_LENGTH << 1));
fnabet's avatar
fnabet committed
706 707 708 709 710 711 712 713
#if TRACE_RLC_AM_STATUS_CREATION
            LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d PREPARE SENDING LAST NACK %04d SO START %05d SO END %05d\n",
                  PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
                  __LINE__,
				  sn_cursor,
				  waited_so,
				  RLC_AM_STATUS_PDU_SO_END_ALL_BYTES);
#endif
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
     		    	  }
    		    	  else {
    		    		  /* Not enough resources to set a SOStart/SEnd, then set ACK_SN to current NACK_SN and stop Status PDU build */
    		    		  control_pdu_info.ack_sn = sn_cursor;
    		    		  status_report_completed = true;
    		    	  }

    		    	  segment_loop_end = true;
    			  }
    		  } //end  while (!segment_loop_end)
    	  } // end if enough resource for transmitting at least one SOStart/SOEnd
    	  else {
    		  /* Not enough UL TBS to set at least one SOStart/SOEnd */
    		  /* latest value of sn_nack shall be used as ACK_SN */
    		  control_pdu_info.ack_sn = sn_nack;
    		  status_report_completed = true;
    	  }
fnabet's avatar
fnabet committed
731 732
      } // end while on all PDU segments of sn_cursor
      else {
fnabet's avatar
fnabet committed
733 734 735 736
    	  /* Go to next received PDU or PDU segment with different SN */
    	  do {
    		  	  cursor_p = cursor_p->next;
    	  	  } while ((cursor_p != NULL) && (((rlc_am_rx_pdu_management_t*)(cursor_p->data))->pdu_info.sn == sn_cursor));
737 738
      }

Bilel's avatar
Bilel committed
739 740
      /* Increment sn_nack except if sn_cursor = vrMS and if current SN was not fully received */
      if (RLC_AM_DIFF_SN(sn_cursor,rlc_pP->vr_r) < RLC_AM_DIFF_SN(rlc_pP->vr_ms,rlc_pP->vr_r)) {
fnabet's avatar
fnabet committed
741
    	  sn_nack = RLC_AM_NEXT_SN(sn_cursor);
742
      }
Bilel's avatar
Bilel committed
743 744 745
      else {
    	  sn_nack = rlc_pP->vr_ms;
      }
746 747


fnabet's avatar
fnabet committed
748 749 750
    } // End main while NACK_SN

    /* Clear E1 of last nack_sn entry */
Gabriel's avatar
Gabriel committed
751 752 753 754 755 756
	AssertFatal ((control_pdu_info.num_nack) || (all_segments_received == 0), "RLC AM Tx Status PDU Data Error no NACK_SN vrR=%d vrMS=%d lastSN_NACK=%d Completed=%d NbBytesAvailable=%d LcId=%d\n",
	        rlc_pP->vr_r,rlc_pP->vr_ms,sn_nack,status_report_completed,(nb_bits_to_transmit >> 3),rlc_pP->channel_id);
	if (control_pdu_info.num_nack) {
	    control_pdu_info.nack_list[control_pdu_info.num_nack - 1].e1  = 0;
	}

757 758 759 760 761 762 763 764

    /* Set ACK_SN unless it was set before */
    if (!status_report_completed){

    	control_pdu_info.ack_sn = sn_nack;
    }

  } else {
fnabet's avatar
fnabet committed
765
	/* reception buffer empty or not enough TBS for filling at least 1 NACK_SN + E1 + E2 */
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
    control_pdu_info.ack_sn = rlc_pP->vr_r;
#if TRACE_RLC_AM_STATUS_CREATION
    LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d PREPARE SENDING ACK %04d  = VR(R)\n",
          PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
          __LINE__,
          control_pdu_info.ack_sn);
#endif
  }


  //msg ("[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u] nb_bits_to_transmit %d\n",
  //     rlc_pP->module_id, rlc_pP->rb_id, ctxt_pP->frame,nb_bits_to_transmit);

#if TRACE_RLC_AM_STATUS_CREATION
  LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d PREPARE SENDING ACK %04d NUM NACK %d\n",
        PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
        __LINE__,
        control_pdu_info.ack_sn,
        control_pdu_info.num_nack);
#endif


  /* encode the control pdu */
  pdu_size = (nb_bits_transmitted + 7) >> 3;
  AssertFatal (pdu_size <= rlc_pP->nb_bytes_requested_by_mac, "RLC AM Tx Status PDU Data size=%d bigger than remaining TBS=%d nb_bits_transmitted=%d LcId=%d\n",
		  pdu_size,rlc_pP->nb_bytes_requested_by_mac,nb_bits_transmitted, rlc_pP->channel_id);


#if TRACE_RLC_AM_STATUS_CREATION
  LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d forecast pdu_size %d\n",
        PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
        __LINE__,
        pdu_size);
#endif
  tb_p = get_free_mem_block(sizeof(struct mac_tb_req) + pdu_size, __func__);
  memset(tb_p->data, 0, sizeof(struct mac_tb_req) + pdu_size);
  //estimation only ((struct mac_tb_req*)(tb_p->data))->tb_size  = pdu_size;
  ((struct mac_tb_req*)(tb_p->data))->data_ptr         = (uint8_t*)&(tb_p->data[sizeof(struct mac_tb_req)]);

  // warning reuse of pdu_size
  // TODO : rlc_am_write_status_pdu should be rewritten as not very tested ...
  pdu_size = rlc_am_write_status_pdu(ctxt_pP, rlc_pP,(rlc_am_pdu_sn_10_t*)(((struct mac_tb_req*)(tb_p->data))->data_ptr), &control_pdu_info);
  ((struct mac_tb_req*)(tb_p->data))->tb_size  = pdu_size;
  //assert((((struct mac_tb_req*)(tb_p->data))->tb_size) < 3000);

#if TRACE_RLC_AM_STATUS_CREATION
  LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] SEND STATUS PDU SIZE %d, rlc_pP->nb_bytes_requested_by_mac %d, nb_bits_to_transmit>>3 %d\n",
        PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
        pdu_size,
        rlc_pP->nb_bytes_requested_by_mac,
        nb_bits_to_transmit >> 3);
#endif

  AssertFatal (pdu_size == ((nb_bits_transmitted + 7) >> 3), "RLC AM Tx Status PDU Data encoding size=%d different than expected=%d LcId=%d\n",
  		  pdu_size,((nb_bits_transmitted + 7) >> 3), rlc_pP->channel_id);

  // remaining bytes to transmit for RLC (retrans pdus and new data pdus)
  rlc_pP->nb_bytes_requested_by_mac = rlc_pP->nb_bytes_requested_by_mac - pdu_size;
  // put pdu in trans
  list_add_head(tb_p, &rlc_pP->control_pdu_list);
  rlc_pP->stat_tx_control_pdu   += 1;
  rlc_pP->stat_tx_control_bytes += pdu_size;

}


#if 0
//-----------------------------------------------------------------------------
void
rlc_am_send_status_pdu_backup(
  const protocol_ctxt_t* const  ctxt_pP,
  rlc_am_entity_t *const rlc_pP
)
{
  // When STATUS reporting has been triggered, the receiving side of an AM RLC entity shall:
  // - if t-StatusProhibit is not running:
  //     - at the first transmission opportunity indicated by lower layer, construct a STATUS PDU and deliver it to lower layer;
  // - else:
  //     - at the first transmission opportunity indicated by lower layer after t-StatusProhibit expires, construct a single
  //       STATUS PDU even if status reporting was triggered several times while t-StatusProhibit was running and
  //       deliver it to lower layer;
  //
  // When a STATUS PDU has been delivered to lower layer, the receiving side of an AM RLC entity shall:
  //     - start t-StatusProhibit.
  //
  // When constructing a STATUS PDU, the AM RLC entity shall:
  //     - for the AMD PDUs with SN such that VR(R) <= SN < VR(MR) that has not been completely received yet, in
  //       increasing SN of PDUs and increasing byte segment order within PDUs, starting with SN = VR(R) up to
  //       the point where the resulting STATUS PDU still fits to the total size of RLC PDU(s) indicated by lower layer:
  //         - for an AMD PDU for which no byte segments have been received yet::
  //             - include in the STATUS PDU a NACK_SN which is set to the SN of the AMD PDU;
  //         - for a continuous sequence of byte segments of a partly received AMD PDU that have not been received yet:
  //             - include in the STATUS PDU a set of NACK_SN, SOstart and SOend
  //     - set the ACK_SN to the SN of the next not received RLC Data PDU which is not indicated as missing in the
  //       resulting STATUS PDU.

gauthier's avatar
gauthier committed
862 863 864 865 866 867 868 869 870 871
  signed int                    nb_bits_to_transmit   = rlc_pP->nb_bytes_requested_by_mac << 3;
  rlc_am_control_pdu_info_t     control_pdu_info;
  rlc_am_pdu_info_t            *pdu_info_cursor_p     = NULL;
  rlc_sn_t                      previous_sn_cursor    = (rlc_pP->vr_r - 1) & RLC_AM_SN_MASK;
  rlc_sn_t                      sn_cursor             = 0;
  mem_block_t                  *cursor_p              = rlc_pP->receiver_buffer.head;
  int                           all_segments_received = 0;
  int                           waited_so             = 0;
  mem_block_t                  *tb_p                  = NULL;
  sdu_size_t                    pdu_size              = 0;
872 873 874 875

  memset(&control_pdu_info, 0, sizeof(rlc_am_control_pdu_info_t));
  // header size
  nb_bits_to_transmit = nb_bits_to_transmit - 15;
876
#if TRACE_RLC_AM_STATUS_CREATION
877 878
  LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] nb_bits_to_transmit %d (15 already allocated for header)\n",
        PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
879
        nb_bits_to_transmit);
gauthier's avatar
gauthier committed
880
  rlc_am_rx_list_display(rlc_pP, " DISPLAY BEFORE CONSTRUCTION OF STATUS REPORT");
881 882
#endif

883

gauthier's avatar
gauthier committed
884
  if (cursor_p != NULL) {
885 886 887
    pdu_info_cursor_p       = &((rlc_am_rx_pdu_management_t*)(cursor_p->data))->pdu_info;
    sn_cursor             = pdu_info_cursor_p->sn;

fnabet's avatar
fnabet committed
888
    while (!(RLC_AM_SN_IN_WINDOW(sn_cursor, rlc_pP->vr_r))) {
889 890 891
      cursor_p                = cursor_p->next;
      previous_sn_cursor    = sn_cursor;

gauthier's avatar
gauthier committed
892 893
      pdu_info_cursor_p       = &((rlc_am_rx_pdu_management_t*)(cursor_p->data))->pdu_info;
      sn_cursor             = pdu_info_cursor_p->sn;
894
#if TRACE_RLC_AM_STATUS_CREATION
895 896
      LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d FIND VR(R) <= SN sn_cursor %04d -> %04d\n",
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
897 898 899 900 901 902 903 904 905 906 907 908
            __LINE__,
            previous_sn_cursor,
            sn_cursor);
#endif
    }

    // 12 bits = size of NACK_SN field + E1, E2 bits
    // 42 bits = size of NACK_SN field + SO_START, SO_END fields, E1, E2 bits
    while ((cursor_p != NULL) && (nb_bits_to_transmit >= 12)) {
      pdu_info_cursor_p       = &((rlc_am_rx_pdu_management_t*)(cursor_p->data))->pdu_info;
      all_segments_received = ((rlc_am_rx_pdu_management_t*)(cursor_p->data))->all_segments_received;
      sn_cursor             = pdu_info_cursor_p->sn;
909
#if TRACE_RLC_AM_STATUS_CREATION
910 911
      LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d LOOPING sn_cursor %04d previous sn_cursor %04d \n",
            PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
            __LINE__,
            sn_cursor,
            previous_sn_cursor);
#endif

      // -------------------------------------------------------------------------------
      // case resegmentation : several PDUs related to the same SN queued in list
      // -------------------------------------------------------------------------------
      if (sn_cursor == previous_sn_cursor) {
        do {
          cursor_p = cursor_p->next;

          if (cursor_p != NULL) {
            pdu_info_cursor_p       = &((rlc_am_rx_pdu_management_t*)(cursor_p->data))->pdu_info;
            all_segments_received = ((rlc_am_rx_pdu_management_t*)(cursor_p->data))->all_segments_received;
            sn_cursor             = pdu_info_cursor_p->sn;
928
#if TRACE_RLC_AM_STATUS_CREATION
929 930
            LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d NOW sn_cursor %04d \n",
                  PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
931 932 933 934 935 936
                  __LINE__,
                  sn_cursor);
#endif
          } else {
            if (all_segments_received) {
              control_pdu_info.ack_sn = (sn_cursor + 1) & RLC_AM_SN_MASK;
937
#if TRACE_RLC_AM_STATUS_CREATION
938 939
              LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d PREPARE SENDING ACK SN %04d \n",
                    PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
940 941 942 943 944
                    __LINE__,
                    control_pdu_info.ack_sn);
#endif
            } else {
              control_pdu_info.ack_sn = (previous_sn_cursor + 1) & RLC_AM_SN_MASK;
945
#if TRACE_RLC_AM_STATUS_CREATION
946 947
              LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d PREPARE SENDING ACK SN %04d (CASE PREVIOUS SN)\n",
                    PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
948 949 950 951
                    __LINE__,
                    control_pdu_info.ack_sn);
#endif
            }
952

953 954 955 956
            goto end_push_nack;
          }
        } while ((cursor_p != NULL) && (sn_cursor == previous_sn_cursor));
      }
957

958 959 960 961 962 963 964 965 966 967 968 969 970
      // -------------------------------------------------------------------------------
      // simple case, PDU(s) is/are missing
      // -------------------------------------------------------------------------------
      while (((previous_sn_cursor + 1) & RLC_AM_SN_MASK) != sn_cursor) {
        if (nb_bits_to_transmit > 12) {
          previous_sn_cursor = (previous_sn_cursor + 1) & RLC_AM_SN_MASK;
          control_pdu_info.nack_list[control_pdu_info.num_nack].nack_sn   = previous_sn_cursor;
          control_pdu_info.nack_list[control_pdu_info.num_nack].so_start  = 0;
          control_pdu_info.nack_list[control_pdu_info.num_nack].so_end    = 0x7ff;
          control_pdu_info.nack_list[control_pdu_info.num_nack].e1        = 1;
          control_pdu_info.nack_list[control_pdu_info.num_nack].e2        = 0;
          control_pdu_info.num_nack += 1;
          nb_bits_to_transmit = nb_bits_to_transmit - 12;
971
#if TRACE_RLC_AM_STATUS_CREATION
972 973
          LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d PREPARE SENDING NACK %04d\n",
                PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
gauthier's avatar
gauthier committed
974
                __LINE__,
975
                previous_sn_cursor);
976
#endif
977 978
        } else {
          control_pdu_info.ack_sn = (previous_sn_cursor + 1) & RLC_AM_SN_MASK;
979
#if TRACE_RLC_AM_STATUS_CREATION
980 981
          LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d NO MORE BITS FOR SENDING NACK %04d -> ABORT AND SET FINAL ACK %04d\n",
                PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
gauthier's avatar
gauthier committed
982
                __LINE__,
983 984
                previous_sn_cursor,
                control_pdu_info.ack_sn);
985
#endif
986 987 988
          goto end_push_nack;
        }
      }
989

990 991 992 993 994
      // -------------------------------------------------------------------------------
      // not so simple case, a resegmented PDU(s) is missing
      // -------------------------------------------------------------------------------
      if (all_segments_received == 0) {
        waited_so = 0;
995
#if TRACE_RLC_AM_STATUS_CREATION
996 997
        LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] if (all_segments_received == 0) \n",
              PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP));
998
#endif
999 1000 1001 1002 1003 1004 1005

        do {
          if (pdu_info_cursor_p->so > waited_so) {
            if (nb_bits_to_transmit >= 42) {
              control_pdu_info.nack_list[control_pdu_info.num_nack].nack_sn   = sn_cursor;
              control_pdu_info.nack_list[control_pdu_info.num_nack].so_start  = waited_so;
              control_pdu_info.nack_list[control_pdu_info.num_nack].so_end    = pdu_info_cursor_p->so - 1;
1006
              control_pdu_info.nack_list[control_pdu_info.num_nack].e1        = 1;
1007
              control_pdu_info.nack_list[control_pdu_info.num_nack].e2        = 1;
1008
              control_pdu_info.num_nack += 1;
1009
              nb_bits_to_transmit = nb_bits_to_transmit - 42;
1010
#if TRACE_RLC_AM_STATUS_CREATION
1011 1012
              LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d PREPARE SENDING NACK %04d SO START %05d SO END %05d (CASE SO %d > WAITED SO %d)\n",
                    PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
1013 1014 1015 1016 1017 1018
                    __LINE__,
                    sn_cursor,
                    waited_so,
                    pdu_info_cursor_p->so - 1,
                    pdu_info_cursor_p->so,
                    waited_so);
1019
#endif
1020 1021 1022 1023

              if (pdu_info_cursor_p->lsf == 1) { // last segment flag
                //waited_so = 0x7FF;
                waited_so = 0x7FFF;
1024
#if TRACE_RLC_AM_STATUS_CREATION
1025 1026
                LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d SN %04d SET WAITED SO 0x7FFF)\n",
                      PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
1027
                      __LINE__, sn_cursor);
1028
#endif
1029 1030 1031
                //break;
              } else {
                waited_so = pdu_info_cursor_p->so + pdu_info_cursor_p->payload_size;
1032
#if TRACE_RLC_AM_STATUS_CREATION
1033 1034
                LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d SN %04d SET WAITED SO %d @1\n",
                      PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
1035 1036 1037
                      __LINE__,
                      sn_cursor,
                      waited_so);
1038
#endif
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
              }
            } else {
              control_pdu_info.ack_sn = sn_cursor;
              goto end_push_nack;
            }
          } else { //else { // pdu_info_cursor_p->so <= waited_so
            waited_so = pdu_info_cursor_p->so + pdu_info_cursor_p->payload_size;

            if (pdu_info_cursor_p->lsf == 1) { // last segment flag
              //waited_so = 0x7FF;
              waited_so = 0x7FFF;
            }
1051

1052
#if TRACE_RLC_AM_STATUS_CREATION
1053 1054
            LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d SN %04d SET WAITED SO %d @2\n",
                  PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
1055 1056
                  __LINE__,
                  sn_cursor, waited_so);
1057
#endif
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
          }

          cursor_p = cursor_p->next;

          if (cursor_p != NULL) {
            pdu_info_cursor_p       = &((rlc_am_rx_pdu_management_t*)(cursor_p->data))->pdu_info;
            all_segments_received = ((rlc_am_rx_pdu_management_t*)(cursor_p->data))->all_segments_received;
            previous_sn_cursor    = sn_cursor;
            sn_cursor             = pdu_info_cursor_p->sn;
          } else {
            // LG control_pdu_info.ack_sn = (previous_sn_cursor + 1) & RLC_AM_SN_MASK;
            control_pdu_info.ack_sn = previous_sn_cursor;
            goto end_push_nack;
          }
        } while ((cursor_p != NULL) && (sn_cursor == previous_sn_cursor));

        // may be last segment of PDU not received
        //if ((sn_cursor != previous_sn_cursor) && (waited_so != 0x7FF)) {
        if ((sn_cursor != previous_sn_cursor) && (waited_so != 0x7FFF)) {
          if (nb_bits_to_transmit >= 42) {
            control_pdu_info.nack_list[control_pdu_info.num_nack].nack_sn   = previous_sn_cursor;
            control_pdu_info.nack_list[control_pdu_info.num_nack].so_start  = waited_so;
            //control_pdu_info.nack_list[control_pdu_info.num_nack].so_end    = 0x7FF;
            control_pdu_info.nack_list[control_pdu_info.num_nack].so_end    = 0x7FFF;
            control_pdu_info.nack_list[control_pdu_info.num_nack].e1        = 1;
            control_pdu_info.nack_list[control_pdu_info.num_nack].e2        = 1;
            control_pdu_info.num_nack += 1;
            nb_bits_to_transmit = nb_bits_to_transmit - 42;
1086
#if TRACE_RLC_AM_STATUS_CREATION
1087 1088
            LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d PREPARE SENDING NACK %04d SO START %05d SO END %05d\n",
                  PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
1089 1090 1091 1092
                  __LINE__,
                  previous_sn_cursor,
                  waited_so,
                  0x7FFF);
1093 1094
#endif
          } else {
1095 1096
            control_pdu_info.ack_sn = previous_sn_cursor;
            goto end_push_nack;
1097
          }
1098 1099 1100 1101 1102 1103 1104
        }

        waited_so = 0;
      } else {
        waited_so = 0;
        cursor_p = cursor_p->next;
        previous_sn_cursor = sn_cursor;
1105
      }
1106 1107 1108
    }

    control_pdu_info.ack_sn = (previous_sn_cursor + 1) & RLC_AM_SN_MASK;
1109
  } else {
1110
    control_pdu_info.ack_sn = rlc_pP->vr_r;
1111
#if TRACE_RLC_AM_STATUS_CREATION
1112 1113
    LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d PREPARE SENDING ACK %04d  = VR(R)\n",
          PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
1114 1115
          __LINE__,
          control_pdu_info.ack_sn);
1116 1117 1118 1119
#endif
  }

end_push_nack:
1120

1121
  if (control_pdu_info.num_nack > 0) {
1122
    control_pdu_info.nack_list[control_pdu_info.num_nack - 1].e1  = 0;
1123
  }
1124

gauthier's avatar
gauthier committed
1125
  //msg ("[FRAME %5u][%s][RLC_AM][MOD %u/%u][RB %u] nb_bits_to_transmit %d\n",
1126
  //     rlc_pP->module_id, rlc_pP->rb_id, ctxt_pP->frame,nb_bits_to_transmit);
1127

1128
#if TRACE_RLC_AM_STATUS_CREATION
1129 1130
  LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d PREPARE SENDING ACK %04d NUM NACK %d\n",
        PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
gauthier's avatar
gauthier committed
1131 1132 1133
        __LINE__,
        control_pdu_info.ack_sn,
        control_pdu_info.num_nack);
1134 1135
#endif
  // encode the control pdu
gauthier's avatar
gauthier committed
1136
  pdu_size = rlc_pP->nb_bytes_requested_by_mac - ((nb_bits_to_transmit - 7 )>> 3);
1137

1138
#if TRACE_RLC_AM_STATUS_CREATION
1139 1140
  LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] LINE %d forecast pdu_size %d\n",
        PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
1141 1142
        __LINE__,
        pdu_size);
1143
#endif
1144
  tb_p = get_free_mem_block(sizeof(struct mac_tb_req) + pdu_size, __func__);
gauthier's avatar
gauthier committed
1145 1146
  memset(tb_p->data, 0, sizeof(struct mac_tb_req) + pdu_size);
  //estimation only ((struct mac_tb_req*)(tb_p->data))->tb_size  = pdu_size;
1147
  ((struct mac_tb_req*)(tb_p->data))->data_ptr         = (uint8_t*)&(tb_p->data[sizeof(struct mac_tb_req)]);
1148 1149

  // warning reuse of pdu_size
1150
  pdu_size = rlc_am_write_status_pdu(ctxt_pP, rlc_pP,(rlc_am_pdu_sn_10_t*)(((struct mac_tb_req*)(tb_p->data))->data_ptr), &control_pdu_info);
gauthier's avatar
gauthier committed
1151 1152
  ((struct mac_tb_req*)(tb_p->data))->tb_size  = pdu_size;
  //assert((((struct mac_tb_req*)(tb_p->data))->tb_size) < 3000);
1153

1154
#if TRACE_RLC_AM_STATUS_CREATION
1155 1156
  LOG_D(RLC, PROTOCOL_RLC_AM_CTXT_FMT"[SEND-STATUS] SEND STATUS PDU SIZE %d, rlc_pP->nb_bytes_requested_by_mac %d, nb_bits_to_transmit>>3 %d\n",
        PROTOCOL_RLC_AM_CTXT_ARGS(ctxt_pP,rlc_pP),
gauthier's avatar
gauthier committed
1157 1158 1159
        pdu_size,
        rlc_pP->nb_bytes_requested_by_mac,
        nb_bits_to_transmit >> 3);
1160
#endif
gauthier's avatar
gauthier committed
1161
  assert(pdu_size == (rlc_pP->nb_bytes_requested_by_mac - (nb_bits_to_transmit >> 3)));
1162 1163

  // remaining bytes to transmit for RLC (retrans pdus and new data pdus)
gauthier's avatar
gauthier committed
1164
  rlc_pP->nb_bytes_requested_by_mac = rlc_pP->nb_bytes_requested_by_mac - pdu_size;
1165
  // put pdu in trans
gauthier's avatar
gauthier committed
1166 1167 1168
  list_add_head(tb_p, &rlc_pP->control_pdu_list);
  rlc_pP->stat_tx_control_pdu   += 1;
  rlc_pP->stat_tx_control_bytes += pdu_size;
1169 1170

}
1171 1172
#endif