ra_procedures.c 50.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*******************************************************************************

  Eurecom OpenAirInterface
  Copyright(c) 1999 - 2011 Eurecom

  This program is free software; you can redistribute it and/or modify it
  under the terms and conditions of the GNU General Public License,
  version 2, as published by the Free Software Foundation.

  This program is distributed in the hope it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.

  The full GNU General Public License is included in this distribution in
  the file called "COPYING".

  Contact Information
  Openair Admin: openair_admin@eurecom.fr
  Openair Tech : openair_tech@eurecom.fr
  Forums       : http://forums.eurecom.fsr/openairinterface
  Address      : Eurecom, 2229, route des crêtes, 06560 Valbonne Sophia Antipolis, France

28
 *******************************************************************************/
29 30

/*! \file openair2/LAYER2/MAC/ra_procedures.c
31
 * \brief Routines for UE MAC-layer Random-access procedures (36.321) V8.6 2009-03
32
 * \author R. Knopp and navid nikaein
33 34 35
 * \date 2011
 * \version 0.1
 * \company Eurecom
36
 * \email: knopp@eurecom.fr and navid nikaein
37 38 39
 * \note
 * \warning
 */
40 41 42

#include "extern.h"
#include "defs.h"
43 44
#include "proto.h"
#include "UTIL/LOG/vcd_signal_dumper.h"
45 46 47 48
#include "PHY_INTERFACE/defs.h"
#include "PHY_INTERFACE/extern.h"
#include "COMMON/mac_rrc_primitives.h"
#include "RRC/LITE/extern.h"
winckel's avatar
winckel committed
49
#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
50
#include "UTIL/LOG/log.h"
winckel's avatar
winckel committed
51
#include "UTIL/OPT/opt.h"
52 53 54 55 56 57 58 59
#include "OCG.h"
#include "OCG_extern.h"
#ifdef PHY_EMUL
#include "SIMULATION/simulation_defs.h"
#endif

extern inline unsigned int taus(void);

60
int8_t get_DELTA_PREAMBLE(module_id_t module_idP) {
61

62 63
  uint8_t prachConfigIndex = UE_mac_inst[module_idP].radioResourceConfigCommon->prach_Config.prach_ConfigInfo.prach_ConfigIndex;
  uint8_t preambleformat;
64

65 66 67 68 69 70 71 72 73 74 75
  if (UE_mac_inst[module_idP].tdd_Config) { // TDD
      if (prachConfigIndex < 20)
        preambleformat = 0;
      else if (prachConfigIndex < 30)
        preambleformat = 1;
      else if (prachConfigIndex < 40)
        preambleformat = 2;
      else if (prachConfigIndex < 48)
        preambleformat = 3;
      else
        preambleformat = 4;
76 77
  }
  else { // FDD
78
      preambleformat = prachConfigIndex>>2;
79 80 81 82 83 84 85 86 87 88 89 90
  }
  switch (preambleformat) {
  case 0:
  case 1:
    return(0);
  case 2:
  case 3:
    return(-3);
  case 4:
    return(8);
  default:
    LOG_E(MAC,"[UE %d] ue_procedures.c: FATAL, Illegal preambleformat %d, prachConfigIndex %d\n",
91 92
        module_idP,
        preambleformat,prachConfigIndex);
gauthier's avatar
gauthier committed
93
    mac_xface->macphy_exit("MAC get_DELTA_PREAMBLE Illegal preamble format");
94 95 96 97 98 99
    return(0);
  }

}

/// This routine implements Section 5.1.2 (Random Access Resource Selection) from 36.321
100
void get_prach_resources(module_id_t module_idP,
101 102 103
    uint8_t eNB_index,
    uint8_t t_id,
    uint8_t first_Msg3,
104 105
    RACH_ConfigDedicated_t *rach_ConfigDedicated) {

106
  uint8_t Msg3_size = UE_mac_inst[module_idP].RA_Msg3_size;
107
  PRACH_RESOURCES_t *prach_resources = &UE_mac_inst[module_idP].RA_prach_resources;
108
  RACH_ConfigCommon_t *rach_ConfigCommon = NULL;
109 110
  uint8_t noGroupB = 0;
  uint8_t f_id = 0,num_prach=0;
111

112 113
  if (UE_mac_inst[module_idP].radioResourceConfigCommon)
    rach_ConfigCommon = &UE_mac_inst[module_idP].radioResourceConfigCommon->rach_ConfigCommon;
114
  else {
115
      LOG_E(MAC,"[UE %d] FATAL  radioResourceConfigCommon is NULL !!!\n",module_idP);
gauthier's avatar
gauthier committed
116
      mac_xface->macphy_exit("MAC FATAL  radioResourceConfigCommon is NULL");
117 118 119
  }

  if (rach_ConfigDedicated) {   // This is for network controlled Mobility, later
120 121 122 123 124
      if (rach_ConfigDedicated->ra_PRACH_MaskIndex != 0) {
          prach_resources->ra_PreambleIndex = rach_ConfigDedicated->ra_PreambleIndex;
          prach_resources->ra_RACH_MaskIndex = rach_ConfigDedicated->ra_PRACH_MaskIndex;
          return;
      }
125 126 127
  }

  if (!rach_ConfigCommon->preambleInfo.preamblesGroupAConfig) {
128
      noGroupB = 1;
129 130 131

  }
  else {
132 133 134
      if (rach_ConfigCommon->preambleInfo.preamblesGroupAConfig->sizeOfRA_PreamblesGroupA ==
          rach_ConfigCommon->preambleInfo.numberOfRA_Preambles)
        noGroupB = 1;
135 136 137
  }

  if (first_Msg3 == 1) {
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
      if (noGroupB == 1) {
          // use Group A procedure
          UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  = (taus())&0x3f;
          UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex = 0;
          UE_mac_inst[module_idP].RA_usedGroupA = 1;
      }
      else if ((Msg3_size < rach_ConfigCommon->preambleInfo.preamblesGroupAConfig->messageSizeGroupA) ||
          (mac_xface->get_PL(module_idP,eNB_index) > UE_mac_inst[module_idP].RA_maxPL)) {
          // use Group A procedure
          UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  = (taus())%rach_ConfigCommon->preambleInfo.preamblesGroupAConfig->sizeOfRA_PreamblesGroupA;
          UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex = 0;
          UE_mac_inst[module_idP].RA_usedGroupA = 1;
      }
      else {  // use Group B
          UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  =
              rach_ConfigCommon->preambleInfo.preamblesGroupAConfig->sizeOfRA_PreamblesGroupA +
              (taus())%(rach_ConfigCommon->preambleInfo.numberOfRA_Preambles -
                  rach_ConfigCommon->preambleInfo.preamblesGroupAConfig->sizeOfRA_PreamblesGroupA);
          UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex = 0;
          UE_mac_inst[module_idP].RA_usedGroupA = 0;
      }
      UE_mac_inst[module_idP].RA_prach_resources.ra_PREAMBLE_RECEIVED_TARGET_POWER = get_Po_NOMINAL_PUSCH(module_idP);
160 161
  }
  else {  // Msg3 is being retransmitted
162 163 164 165 166 167 168 169 170 171 172 173 174 175
      if (UE_mac_inst[module_idP].RA_usedGroupA == 1) {
          if (rach_ConfigCommon->preambleInfo.preamblesGroupAConfig)
            UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  = (taus())%rach_ConfigCommon->preambleInfo.preamblesGroupAConfig->sizeOfRA_PreamblesGroupA;
          else
            UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  = (taus())&0x3f;
          UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex = 0;
      }
      else {
          UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  =
              rach_ConfigCommon->preambleInfo.preamblesGroupAConfig->sizeOfRA_PreamblesGroupA +
              (taus())%(rach_ConfigCommon->preambleInfo.numberOfRA_Preambles -
                  rach_ConfigCommon->preambleInfo.preamblesGroupAConfig->sizeOfRA_PreamblesGroupA);
          UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex = 0;
      }
176 177
  }
  // choose random PRACH resource in TDD
178 179 180 181 182 183
  if (UE_mac_inst[module_idP].tdd_Config) {
      num_prach = mac_xface->get_num_prach_tdd(mac_xface->lte_frame_parms);
      if ((num_prach>0) && (num_prach<6))
        UE_mac_inst[module_idP].RA_prach_resources.ra_TDD_map_index = (taus()%num_prach);
      f_id = mac_xface->get_fid_prach_tdd(mac_xface->lte_frame_parms,
          UE_mac_inst[module_idP].RA_prach_resources.ra_TDD_map_index);
184 185 186
  }

  // choose RA-RNTI
187
  UE_mac_inst[module_idP].RA_prach_resources.ra_RNTI = 1 + t_id + 10*f_id;
188 189
}

190
void Msg1_tx(module_id_t module_idP,frame_t frameP, uint8_t eNB_id) {
191 192

  // start contention resolution timer
193
  UE_mac_inst[module_idP].RA_attempt_number++;
194 195
#if defined(USER_MODE) && defined(OAI_EMU)
  if (oai_emulation.info.opt_enabled) {
196 197 198 199
      trace_pdu(0, NULL, 0, module_idP, 3, UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex,
          UE_mac_inst[module_idP].subframe, 0, UE_mac_inst[module_idP].RA_attempt_number);
      LOG_D(OPT,"[UE %d][RAPROC] TX MSG1 Frame %d trace pdu for rnti %x  with size %d\n",
          module_idP, frameP, 1, UE_mac_inst[module_idP].RA_Msg3_size);
200 201 202 203 204
  }
#endif
}


205
void Msg3_tx(module_id_t module_idP,frame_t frameP, uint8_t eNB_id) {
206 207

  // start contention resolution timer
208 209 210
  LOG_I(MAC,"[UE %d][RAPROC] Frame %d : Msg3_tx: Setting contention resolution timer\n",module_idP,frameP);
  UE_mac_inst[module_idP].RA_contention_resolution_cnt = 0;
  UE_mac_inst[module_idP].RA_contention_resolution_timer_active = 1;
211 212 213

#if defined(USER_MODE) && defined(OAI_EMU)
  if (oai_emulation.info.opt_enabled) { // msg3
214 215 216 217
      trace_pdu(0, &UE_mac_inst[module_idP].CCCH_pdu.payload[0], UE_mac_inst[module_idP].RA_Msg3_size,
          module_idP, 3, UE_mac_inst[module_idP].crnti, UE_mac_inst[module_idP].subframe, 0, 0);
      LOG_D(OPT,"[UE %d][RAPROC] MSG3 Frame %d trace pdu Preamble %d   with size %d\n",
          module_idP, frameP, UE_mac_inst[module_idP].crnti /*UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex*/, UE_mac_inst[module_idP].RA_Msg3_size);
218 219 220 221 222
  }
#endif
}


223
PRACH_RESOURCES_t *ue_get_rach(module_id_t module_idP,frame_t frameP, uint8_t eNB_indexP,sub_frame_t subframeP){
224 225


226
  uint8_t                        Size=0;
227
  UE_MODE_t                 UE_mode = mac_xface->get_ue_mode(module_idP,eNB_indexP);
228 229
  uint8_t                        lcid = CCCH;
  uint16_t                       Size16;
230
  struct RACH_ConfigCommon *rach_ConfigCommon = (struct RACH_ConfigCommon *)NULL;
231
  int32_t                       frame_diff=0;
232
  mac_rlc_status_resp_t     rlc_status;
233 234 235
  uint8_t                        dcch_header_len=0;
  uint16_t                       sdu_lengths[8];
  uint8_t                        ulsch_buff[MAX_ULSCH_PAYLOAD_BYTES];
236 237

  if (UE_mode == PRACH) {
238 239 240 241
      if (UE_mac_inst[module_idP].radioResourceConfigCommon)
        rach_ConfigCommon = &UE_mac_inst[module_idP].radioResourceConfigCommon->rach_ConfigCommon;
      else {
          return(NULL);
242
      }
243 244 245 246 247 248 249 250 251 252
      if (Is_rrc_registered == 1) {

          if (UE_mac_inst[module_idP].RA_active == 0) {
              // check if RRC is ready to initiate the RA procedure
              Size = mac_rrc_data_req(module_idP,
                  frameP,
                  CCCH,1,
                  &UE_mac_inst[module_idP].CCCH_pdu.payload[sizeof(SCH_SUBHEADER_SHORT)+1],0,
                  eNB_indexP,
                  0);
253
              Size16 = (uint16_t)Size;
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

              //	LOG_D(MAC,"[UE %d] Frame %d: Requested RRCConnectionRequest, got %d bytes\n",module_idP,frameP,Size);
              LOG_D(RRC, "[MSC_MSG][FRAME %05d][RRC_UE][MOD %02d][][--- MAC_DATA_REQ (RRCConnectionRequest eNB %d) --->][MAC_UE][MOD %02d][]\n",
                  frameP, module_idP, eNB_indexP, module_idP);
              LOG_D(MAC,"[UE %d] Frame %d: Requested RRCConnectionRequest, got %d bytes\n",module_idP,frameP,Size);

              if (Size>0) {

                  UE_mac_inst[module_idP].RA_active                        = 1;
                  UE_mac_inst[module_idP].RA_PREAMBLE_TRANSMISSION_COUNTER = 1;
                  UE_mac_inst[module_idP].RA_Msg3_size                     = Size+sizeof(SCH_SUBHEADER_SHORT)+sizeof(SCH_SUBHEADER_SHORT);
                  UE_mac_inst[module_idP].RA_prachMaskIndex                = 0;
                  UE_mac_inst[module_idP].RA_prach_resources.Msg3          = UE_mac_inst[module_idP].CCCH_pdu.payload;
                  UE_mac_inst[module_idP].RA_backoff_cnt                   = 0;  // add the backoff condition here if we have it from a previous RA reponse which failed (i.e. backoff indicator)
                  if (rach_ConfigCommon) {
                      UE_mac_inst[module_idP].RA_window_cnt                    = 2+ rach_ConfigCommon->ra_SupervisionInfo.ra_ResponseWindowSize;
                      if (UE_mac_inst[module_idP].RA_window_cnt == 9)
                        UE_mac_inst[module_idP].RA_window_cnt = 10;  // Note: 9 subframe window doesn't exist, after 8 is 10!
                  }
                  else {
                      LOG_D(MAC,"[UE %d] FATAL Frame %d: rach_ConfigCommon is NULL !!!\n",module_idP,frameP);
gauthier's avatar
gauthier committed
275
                      mac_xface->macphy_exit("MAC rach_ConfigCommon is NULL");
276 277 278 279 280 281 282 283
                  }
                  UE_mac_inst[module_idP].RA_tx_frame         = frameP;
                  UE_mac_inst[module_idP].RA_tx_subframe      = subframeP;
                  UE_mac_inst[module_idP].RA_backoff_frame    = frameP;
                  UE_mac_inst[module_idP].RA_backoff_subframe = subframeP;
                  // Fill in preamble and PRACH resource
                  get_prach_resources(module_idP,eNB_indexP,subframeP,1,NULL);

284
                  generate_ulsch_header((uint8_t*)&UE_mac_inst[module_idP].CCCH_pdu.payload[0],  // mac header
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
                      1,      // num sdus
                      0,            // short pading
                      &Size16,  // sdu length
                      &lcid,    // sdu lcid
                      NULL,  // power headroom
                      NULL,  // crnti
                      NULL,  // truncated bsr
                      NULL, // short bsr
                      NULL, // long_bsr
                      1); //post_padding

                  return(&UE_mac_inst[module_idP].RA_prach_resources);
              }
              else if (UE_mac_inst[module_idP].scheduling_info.BSR_bytes[DCCH] > 0) {
                  // This is for triggering a transmission on DCCH using PRACH (during handover, or sending SR for example)
                  dcch_header_len = 2 + 2;  /// SHORT Subheader + C-RNTI control element
gauthier's avatar
gauthier committed
301
                  rlc_status = mac_rlc_status_ind(0, module_idP,frameP,ENB_FLAG_NO,MBMS_FLAG_NO,
302 303 304 305 306 307 308 309 310
                      DCCH,
                      6);
                  if (UE_mac_inst[module_idP].crnti_before_ho)
                    LOG_D(MAC,"[UE %d] Frame %d : UL-DCCH -> ULSCH, HO RRCConnectionReconfigurationComplete (%x, %x), RRC message has %d bytes to send throug PRACH (mac header len %d)\n",
                        module_idP,frameP, UE_mac_inst[module_idP].crnti,UE_mac_inst[module_idP].crnti_before_ho, rlc_status.bytes_in_buffer,dcch_header_len);
                  else
                    LOG_D(MAC,"[UE %d] Frame %d : UL-DCCH -> ULSCH, RRC message has %d bytes to send through PRACH(mac header len %d)\n",
                        module_idP,frameP, rlc_status.bytes_in_buffer,dcch_header_len);

gauthier's avatar
gauthier committed
311
                  sdu_lengths[0] = mac_rlc_data_req(eNB_indexP, module_idP,frameP,ENB_FLAG_NO, MBMS_FLAG_NO,
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
                      DCCH,
                      (char *)&ulsch_buff[0]);

                  LOG_D(MAC,"[UE %d] TX Got %d bytes for DCCH\n",module_idP,sdu_lengths[0]);
                  update_bsr(module_idP, frameP, DCCH,UE_mac_inst[module_idP].scheduling_info.LCGID[DCCH]);
                  //header_len +=2;
                  UE_mac_inst[module_idP].RA_active                        = 1;
                  UE_mac_inst[module_idP].RA_PREAMBLE_TRANSMISSION_COUNTER = 1;
                  UE_mac_inst[module_idP].RA_Msg3_size                     = Size+dcch_header_len;
                  UE_mac_inst[module_idP].RA_prachMaskIndex                = 0;
                  UE_mac_inst[module_idP].RA_prach_resources.Msg3          = ulsch_buff;
                  UE_mac_inst[module_idP].RA_backoff_cnt                   = 0;  // add the backoff condition here if we have it from a previous RA reponse which failed (i.e. backoff indicator)
                  if (rach_ConfigCommon) {
                      UE_mac_inst[module_idP].RA_window_cnt                    = 2+ rach_ConfigCommon->ra_SupervisionInfo.ra_ResponseWindowSize;
                      if (UE_mac_inst[module_idP].RA_window_cnt == 9)
                        UE_mac_inst[module_idP].RA_window_cnt = 10;  // Note: 9 subframe window doesn't exist, after 8 is 10!
                  }
                  else {
                      LOG_D(MAC,"[UE %d] FATAL Frame %d: rach_ConfigCommon is NULL !!!\n",module_idP,frameP);
gauthier's avatar
gauthier committed
331
                      mac_xface->macphy_exit("MAC rach_ConfigCommon is NULL");
332 333 334 335 336 337 338
                  }
                  UE_mac_inst[module_idP].RA_tx_frame         = frameP;
                  UE_mac_inst[module_idP].RA_tx_subframe      = subframeP;
                  UE_mac_inst[module_idP].RA_backoff_frame    = frameP;
                  UE_mac_inst[module_idP].RA_backoff_subframe = subframeP;
                  // Fill in preamble and PRACH resource
                  get_prach_resources(module_idP,eNB_indexP,subframeP,1,NULL);
339
                  generate_ulsch_header((uint8_t*)ulsch_buff,  // mac header
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
                      1,      // num sdus
                      0,            // short pading
                      &Size16,  // sdu length
                      &lcid,    // sdu lcid
                      NULL,  // power headroom
                      &UE_mac_inst[module_idP].crnti,  // crnti
                      NULL,  // truncated bsr
                      NULL, // short bsr
                      NULL, // long_bsr
                      0); //post_padding

                  return(&UE_mac_inst[module_idP].RA_prach_resources);
              }
          }
          else {  // RACH is active
              LOG_D(MAC,"[MAC][UE %d][RAPROC] frameP %d, subframe %d: RA Active, window cnt %d (RA_tx_frame %d, RA_tx_subframe %d)\n",module_idP,
                  frameP,subframeP,UE_mac_inst[module_idP].RA_window_cnt,
                  UE_mac_inst[module_idP].RA_tx_frame,UE_mac_inst[module_idP].RA_tx_subframe);
              // compute backoff parameters
              if (UE_mac_inst[module_idP].RA_backoff_cnt>0) {
gauthier's avatar
gauthier committed
360
                  frame_diff = (sframe_t)frameP - UE_mac_inst[module_idP].RA_backoff_frame;
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
                  if (frame_diff < 0)
                    frame_diff = -frame_diff;
                  UE_mac_inst[module_idP].RA_backoff_cnt -= ((10*frame_diff) + (subframeP-UE_mac_inst[module_idP].RA_backoff_subframe));

                  UE_mac_inst[module_idP].RA_backoff_frame    = frameP;
                  UE_mac_inst[module_idP].RA_backoff_subframe = subframeP;
              }
              // compute RA window parameters
              if (UE_mac_inst[module_idP].RA_window_cnt>0) {
                  frame_diff = (frame_t)frameP - UE_mac_inst[module_idP].RA_tx_frame;
                  if (frame_diff < 0)
                    frame_diff = -frame_diff;
                  UE_mac_inst[module_idP].RA_window_cnt -= ((10*frame_diff) + (subframeP-UE_mac_inst[module_idP].RA_tx_subframe));
                  LOG_D(MAC,"[MAC][UE %d][RAPROC] frameP %d, subframe %d: RA Active, adjusted window cnt %d\n",module_idP,
                      frameP,subframeP,UE_mac_inst[module_idP].RA_window_cnt);
              }
              if ((UE_mac_inst[module_idP].RA_window_cnt<=0) &&
                  (UE_mac_inst[module_idP].RA_backoff_cnt<=0)) {

                  UE_mac_inst[module_idP].RA_tx_frame    = frameP;
                  UE_mac_inst[module_idP].RA_tx_subframe = subframeP;
                  UE_mac_inst[module_idP].RA_PREAMBLE_TRANSMISSION_COUNTER++;
                  UE_mac_inst[module_idP].RA_prach_resources.ra_PREAMBLE_RECEIVED_TARGET_POWER += (rach_ConfigCommon->powerRampingParameters.powerRampingStep<<1);  // 2dB increments in ASN.1 definition
                  if (UE_mac_inst[module_idP].RA_PREAMBLE_TRANSMISSION_COUNTER == rach_ConfigCommon->ra_SupervisionInfo.preambleTransMax) {
                      LOG_D(MAC,"[UE %d] Frame %d: Maximum number of RACH attempts (%d)\n",module_idP,frameP,rach_ConfigCommon->ra_SupervisionInfo.preambleTransMax);
                      // send message to RRC
                      UE_mac_inst[module_idP].RA_PREAMBLE_TRANSMISSION_COUNTER=1;
                      UE_mac_inst[module_idP].RA_prach_resources.ra_PREAMBLE_RECEIVED_TARGET_POWER = get_Po_NOMINAL_PUSCH(module_idP);
                  }
                  UE_mac_inst[module_idP].RA_window_cnt                    = 2+ rach_ConfigCommon->ra_SupervisionInfo.ra_ResponseWindowSize;
                  UE_mac_inst[module_idP].RA_backoff_cnt                   = 0;

                  // Fill in preamble and PRACH resource
                  get_prach_resources(module_idP,eNB_indexP,subframeP,0,NULL);
                  return(&UE_mac_inst[module_idP].RA_prach_resources);
              }
          }
398 399 400
      }
  }
  else if (UE_mode == PUSCH) {
401
      LOG_D(MAC,"[UE %d] FATAL: Should not have checked for RACH in PUSCH yet ...",module_idP);
gauthier's avatar
gauthier committed
402
      mac_xface->macphy_exit("MAC FATAL: Should not have checked for RACH in PUSCH yet");
403 404 405
  }
  return(NULL);
}
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050

void cancel_ra_proc(module_id_t module_idP, frame_t frameP, rnti_t rnti) {
  unsigned char i;
  LOG_I(MAC,"[eNB %d][RAPROC] Frame %d Cancelling RA procedure for UE rnti %x\n",module_idP,frameP,rnti);

  for (i=0;i<NB_RA_PROC_MAX;i++) {
      if (rnti == eNB_mac_inst[module_idP].RA_template[i].rnti) {
          eNB_mac_inst[module_idP].RA_template[i].RA_active=FALSE;
          eNB_mac_inst[module_idP].RA_template[i].generate_rar=0;
          eNB_mac_inst[module_idP].RA_template[i].generate_Msg4=0;
          eNB_mac_inst[module_idP].RA_template[i].wait_ack_Msg4=0;
          eNB_mac_inst[module_idP].RA_template[i].timing_offset=0;
          eNB_mac_inst[module_idP].RA_template[i].RRC_timer=20;
          eNB_mac_inst[module_idP].RA_template[i].rnti = 0;
      }
  }
}

void terminate_ra_proc(module_id_t module_idP,frame_t frameP,rnti_t rnti,unsigned char *msg3, uint16_t msg3_len) {

  unsigned char rx_ces[MAX_NUM_CE],num_ce,num_sdu,i,*payload_ptr;
  unsigned char rx_lcids[NB_RB_MAX];
  uint16_t rx_lengths[NB_RB_MAX];
  int8_t UE_id;

  LOG_I(MAC,"[eNB %d][RAPROC] Frame %d, Received msg3 %x.%x.%x.%x.%x.%x, Terminating RA procedure for UE rnti %x\n",
      module_idP,frameP,
      msg3[3],msg3[4],msg3[5],msg3[6],msg3[7], msg3[8], rnti);

  for (i=0;i<NB_RA_PROC_MAX;i++) {
      LOG_D(MAC,"[RAPROC] Checking proc %d : rnti (%x, %x), active %d\n",i,
          eNB_mac_inst[module_idP].RA_template[i].rnti, rnti,
          eNB_mac_inst[module_idP].RA_template[i].RA_active);
      if ((eNB_mac_inst[module_idP].RA_template[i].rnti==rnti) &&
          (eNB_mac_inst[module_idP].RA_template[i].RA_active==TRUE)) {

          payload_ptr = parse_ulsch_header(msg3,&num_ce,&num_sdu,rx_ces,rx_lcids,rx_lengths,msg3_len);
          LOG_D(MAC,"[eNB %d][RAPROC] Frame %d Received CCCH: length %d, offset %d\n",
              module_idP,frameP,rx_lengths[0],payload_ptr-msg3);
          if (/*(num_ce == 0) &&*/ (num_sdu==1) && (rx_lcids[0] == CCCH)) { // This is an RRCConnectionRequest/Restablishment
              memcpy(&eNB_mac_inst[module_idP].RA_template[i].cont_res_id[0],payload_ptr,6);
              LOG_D(MAC,"[eNB %d][RAPROC] Frame %d Received CCCH: length %d, offset %d\n",
                  module_idP,frameP,rx_lengths[0],payload_ptr-msg3);
              UE_id=add_new_ue(module_idP,eNB_mac_inst[module_idP].RA_template[i].rnti);
              if (UE_id==-1) {
                  mac_xface->macphy_exit("[MAC][eNB] Max user count reached\n");
              }
              else {
                  LOG_I(MAC,"[eNB %d][RAPROC] Frame %d Added user with rnti %x => UE %d\n",
                      module_idP,frameP,eNB_mac_inst[module_idP].RA_template[i].rnti,UE_id);
              }

              if (Is_rrc_registered == 1)
                mac_rrc_data_ind(module_idP,frameP,CCCH,(uint8_t *)payload_ptr,rx_lengths[0],1,module_idP,0);
              // add_user.  This is needed to have the rnti for configuring UE (PHY). The UE is removed if RRC
              // doesn't provide a CCCH SDU

          }
          else if (num_ce >0) {  // handle msg3 which is not RRCConnectionRequest
              //	process_ra_message(msg3,num_ce,rx_lcids,rx_ces);
          }

          eNB_mac_inst[module_idP].RA_template[i].generate_Msg4 = 1;
          eNB_mac_inst[module_idP].RA_template[i].wait_ack_Msg4 = 0;

          return;
      } // if process is active

  } // loop on RA processes
}

void rx_sdu(module_id_t enb_mod_idP,frame_t frameP,rnti_t rntiP,uint8_t *sdu, uint16_t sdu_len) {

  unsigned char  rx_ces[MAX_NUM_CE],num_ce,num_sdu,i,*payload_ptr;
  unsigned char  rx_lcids[NB_RB_MAX];
  unsigned short rx_lengths[NB_RB_MAX];
  module_id_t    ue_mod_id = find_UE_id(enb_mod_idP,rntiP);
  int ii,j;
  start_meas(&eNB_mac_inst[enb_mod_idP].rx_ulsch_sdu);
  
  if ((ue_mod_id >  NUMBER_OF_UE_MAX) || (ue_mod_id == -1) || (ue_mod_id == 255) )
  
  for(ii=0; ii<NB_RB_MAX; ii++) rx_lengths[ii] = 0;

  vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_RX_SDU,1);

  LOG_D(MAC,"[eNB %d] Received ULSCH sdu from PHY (rnti %x, UE_id %d), parsing header\n",enb_mod_idP,rntiP,ue_mod_id);
  payload_ptr = parse_ulsch_header(sdu,&num_ce,&num_sdu,rx_ces,rx_lcids,rx_lengths,sdu_len);

  // control element
  for (i=0;i<num_ce;i++) {

    switch (rx_ces[i]) { // implement and process BSR + CRNTI +
    case POWER_HEADROOM:
      if (ue_mod_id != UE_INDEX_INVALID ){
	eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].phr_info =  (payload_ptr[0] & 0x3f);// - PHR_MAPPING_OFFSET;
	LOG_D(MAC, "[eNB] MAC CE_LCID %d : Received PHR PH = %d (db)\n", rx_ces[i], eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].phr_info);
      }
      payload_ptr+=sizeof(POWER_HEADROOM_CMD);
      break;
      case CRNTI:
        LOG_D(MAC, "[eNB] MAC CE_LCID %d : Received CRNTI %d \n", rx_ces[i], payload_ptr[0]);
        payload_ptr+=1;
        break;
    case TRUNCATED_BSR:
    case SHORT_BSR: {
      if (ue_mod_id  != UE_INDEX_INVALID ){
	uint8_t lcgid;
	lcgid = (payload_ptr[0] >> 6);
	LOG_D(MAC, "[eNB] MAC CE_LCID %d : Received short BSR LCGID = %u bsr = %d\n",
	      rx_ces[i], lcgid, payload_ptr[0] & 0x3f);
	eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].bsr_info[lcgid] = (payload_ptr[0] & 0x3f);
      }
      payload_ptr += 1;//sizeof(SHORT_BSR); // fixme
    } break;
    case LONG_BSR:
      if (ue_mod_id  != UE_INDEX_INVALID ){
	eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].bsr_info[LCGID0] = ((payload_ptr[0] & 0xFC) >> 2);
	eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].bsr_info[LCGID1] =
	  ((payload_ptr[0] & 0x03) << 4) | ((payload_ptr[1] & 0xF0) >> 4);
	eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].bsr_info[LCGID2] =
	  ((payload_ptr[1] & 0x0F) << 2) | ((payload_ptr[2] & 0xC0) >> 6);
	eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].bsr_info[LCGID3] = (payload_ptr[2] & 0x3F);
	LOG_D(MAC, "[eNB] MAC CE_LCID %d: Received long BSR LCGID0 = %u LCGID1 = "
	      "%u LCGID2 = %u LCGID3 = %u\n",
	      rx_ces[i],
	      eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].bsr_info[LCGID0],
	      eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].bsr_info[LCGID1],
	      eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].bsr_info[LCGID2],
	      eNB_mac_inst[enb_mod_idP].UE_template[ue_mod_id].bsr_info[LCGID3]);
      }
      payload_ptr += 3;////sizeof(LONG_BSR);
      break;
    default:
      LOG_E(MAC, "[eNB] Received unknown MAC header (0x%02x)\n", rx_ces[i]);
      break;
    }
  }
  
  for (i=0;i<num_sdu;i++) {
    LOG_D(MAC,"SDU Number %d MAC Subheader SDU_LCID %d, length %d\n",i,rx_lcids[i],rx_lengths[i]);
    
    switch (rx_lcids[i]) {
    case CCCH : 
      LOG_I(MAC,"[eNB %d][RAPROC] Frame %d, Received CCCH:  %x.%x.%x.%x.%x.%x, Terminating RA procedure for UE rnti %x\n",
	    enb_mod_idP,frameP,
	    payload_ptr[0],payload_ptr[1],payload_ptr[2],payload_ptr[3],payload_ptr[4], payload_ptr[5], rntiP);

      for (ii=0;ii<NB_RA_PROC_MAX;ii++) {
	LOG_D(MAC,"[RAPROC] Checking proc %d : rnti (%x, %x), active %d\n",ii,
	      eNB_mac_inst[enb_mod_idP].RA_template[ii].rnti, rntiP,
	      eNB_mac_inst[enb_mod_idP].RA_template[ii].RA_active);
	
	if ((eNB_mac_inst[enb_mod_idP].RA_template[ii].rnti==rntiP) &&
	    (eNB_mac_inst[enb_mod_idP].RA_template[ii].RA_active==TRUE)) {
	  
          //payload_ptr = parse_ulsch_header(msg3,&num_ce,&num_sdu,rx_ces,rx_lcids,rx_lengths,msg3_len);
	  
	  if (ue_mod_id == UE_INDEX_INVALID) {
	    memcpy(&eNB_mac_inst[enb_mod_idP].RA_template[ii].cont_res_id[0],payload_ptr,6);
	    LOG_I(MAC,"[eNB %d][RAPROC] Frame %d CCCH: Received RRCConnectionRequest: length %d, offset %d\n",
                  enb_mod_idP,frameP,rx_lengths[ii],payload_ptr-sdu);
	    if ((ue_mod_id=add_new_ue(enb_mod_idP,eNB_mac_inst[enb_mod_idP].RA_template[ii].rnti)) == -1 )
	      mac_xface->macphy_exit("[MAC][eNB] Max user count reached\n");
	    else 
	      LOG_I(MAC,"[eNB %d][RAPROC] Frame %d Added user with rnti %x => UE %d\n",
		    enb_mod_idP,frameP,eNB_mac_inst[enb_mod_idP].RA_template[ii].rnti,ue_mod_id);
	  } else {
	     LOG_I(MAC,"[eNB %d][RAPROC] Frame %d CCCH: Received RRCConnectionReestablishment from UE %d: length %d, offset %d\n",
		   enb_mod_idP,frameP,ue_mod_id,rx_lengths[ii],payload_ptr-sdu);
	  }
	  
	  if (Is_rrc_registered == 1)
	    mac_rrc_data_ind(enb_mod_idP,frameP,CCCH,(uint8_t *)payload_ptr,rx_lengths[ii],1,enb_mod_idP,0);
	  
	  
          if (num_ce >0) {  // handle msg3 which is not RRCConnectionRequest
	    //	process_ra_message(msg3,num_ce,rx_lcids,rx_ces);
	  }
	  
	  eNB_mac_inst[enb_mod_idP].RA_template[ii].generate_Msg4 = 1;
	  eNB_mac_inst[enb_mod_idP].RA_template[ii].wait_ack_Msg4 = 0;
	  
	  
	} // if process is active
	
      } // loop on RA processes
      
      break;
    case  DCCH : 
    case DCCH1 :
      //      if(eNB_mac_inst[module_idP].Dcch_lchan[UE_id].Active==1){
      
#if defined(ENABLE_MAC_PAYLOAD_DEBUG)
      LOG_T(MAC,"offset: %d\n",(unsigned char)((unsigned char*)payload_ptr-sdu));
      for (j=0;j<32;j++)
	LOG_T(MAC,"%x ",payload_ptr[j]);
      LOG_T(MAC,"\n");
#endif
      
      //  This check is just to make sure we didn't get a bogus SDU length, to be removed ...
      if (rx_lengths[i]<CCCH_PAYLOAD_SIZE_MAX) {
	LOG_D(MAC,"[eNB %d] Frame %d : ULSCH -> UL-DCCH, received %d bytes form UE %d on LCID %d(%d) \n",
	      enb_mod_idP,frameP, rx_lengths[i], ue_mod_id, rx_lcids[i], rx_lcids[i]);
	
	mac_rlc_data_ind(enb_mod_idP,ue_mod_id, frameP,ENB_FLAG_YES,MBMS_FLAG_NO,
			 rx_lcids[i],
			 (char *)payload_ptr,
			 rx_lengths[i],
			 1,
			 NULL);//(unsigned int*)crc_status);
	eNB_mac_inst[enb_mod_idP].eNB_UE_stats[ue_mod_id].num_pdu_rx[rx_lcids[i]]+=1;
	eNB_mac_inst[enb_mod_idP].eNB_UE_stats[ue_mod_id].num_bytes_rx[rx_lcids[i]]+=rx_lengths[i];
	
      }
      //      }
      break;
    case DTCH: // default DRB 
      //      if(eNB_mac_inst[module_idP].Dcch_lchan[UE_id].Active==1){
	
#if defined(ENABLE_MAC_PAYLOAD_DEBUG)
      LOG_T(MAC,"offset: %d\n",(unsigned char)((unsigned char*)payload_ptr-sdu));
      for (j=0;j<32;j++)
	LOG_T(MAC,"%x ",payload_ptr[j]);
      LOG_T(MAC,"\n");
#endif
      
      LOG_D(MAC,"[eNB %d] Frame %d : ULSCH -> UL-DTCH, received %d bytes from UE %d for lcid %d (%d)\n",
	    enb_mod_idP,frameP, rx_lengths[i], ue_mod_id,rx_lcids[i],rx_lcids[i]);
      
      if ((rx_lengths[i] <SCH_PAYLOAD_SIZE_MAX) &&  (rx_lengths[i] > 0) ) {   // MAX SIZE OF transport block
	mac_rlc_data_ind(enb_mod_idP,ue_mod_id, frameP,ENB_FLAG_YES,MBMS_FLAG_NO,
			 DTCH,
			 (char *)payload_ptr,
			   rx_lengths[i],
			 1,
			 NULL);//(unsigned int*)crc_status);
	eNB_mac_inst[enb_mod_idP].eNB_UE_stats[ue_mod_id].num_pdu_rx[rx_lcids[i]]+=1;
	eNB_mac_inst[enb_mod_idP].eNB_UE_stats[ue_mod_id].num_bytes_rx[rx_lcids[i]]+=rx_lengths[i];
	  
      }
	//      }
      break;
    default :  //if (rx_lcids[i] >= DTCH) {
      eNB_mac_inst[enb_mod_idP].eNB_UE_stats[ue_mod_id].num_errors_rx+=1;
      LOG_E(MAC,"[eNB %d] received unsupported or unknown LCID %d from UE %d ", rx_lcids[i], ue_mod_id);
      break;
    }
    payload_ptr+=rx_lengths[i];
    
  }

  eNB_mac_inst[enb_mod_idP].eNB_UE_stats[ue_mod_id].total_pdu_bytes_rx+=sdu_len;
  eNB_mac_inst[enb_mod_idP].eNB_UE_stats[ue_mod_id].total_num_pdus_rx+=1;
  
  vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_RX_SDU,0);
  stop_meas(&eNB_mac_inst[enb_mod_idP].rx_ulsch_sdu);
}

// First stage of Random-Access Scheduling
void schedule_RA(module_id_t module_idP,frame_t frameP, sub_frame_t subframeP,unsigned char Msg3_subframe,unsigned char *nprb,unsigned int *nCCE) {

  start_meas(&eNB_mac_inst[module_idP].schedule_ra);
  RA_TEMPLATE *RA_template = (RA_TEMPLATE *)&eNB_mac_inst[module_idP].RA_template[0];
  unsigned char i;//,harq_pid,round;
  uint16_t rrc_sdu_length;
  unsigned char lcid,offset;
  module_id_t UE_id= UE_INDEX_INVALID;
  unsigned short TBsize = -1;
  unsigned short msg4_padding,msg4_post_padding,msg4_header;

  for (i=0;i<NB_RA_PROC_MAX;i++) {

      if (RA_template[i].RA_active == TRUE) {

          LOG_I(MAC,"[eNB %d][RAPROC] RA %d is active (generate RAR %d, generate_Msg4 %d, wait_ack_Msg4 %d, rnti %x)\n",
              module_idP,i,RA_template[i].generate_rar,RA_template[i].generate_Msg4,RA_template[i].wait_ack_Msg4, RA_template[i].rnti);

          if (RA_template[i].generate_rar == 1) {
              *nprb= (*nprb) + 3;
              *nCCE = (*nCCE) + 4;
              RA_template[i].Msg3_subframe=Msg3_subframe;
          }
          else if (RA_template[i].generate_Msg4 == 1) {

              // check for Msg4 Message
              UE_id = find_UE_id(module_idP,RA_template[i].rnti);
              if (Is_rrc_registered == 1) {

                  // Get RRCConnectionSetup for Piggyback
                  rrc_sdu_length = mac_rrc_data_req(module_idP,
                      frameP,
                      CCCH,1,
                      &eNB_mac_inst[module_idP].CCCH_pdu.payload[0],
                      1,
                      module_idP,
                      0); // not used in this case
                  if (rrc_sdu_length == -1)
                    mac_xface->macphy_exit("[MAC][eNB Scheduler] CCCH not allocated\n");
                  else {
                      //msg("[MAC][eNB %d] Frame %d, subframeP %d: got %d bytes from RRC\n",module_idP,frameP, subframeP,rrc_sdu_length);
                  }
              }

              LOG_I(MAC,"[eNB %d][RAPROC] Frame %d, subframeP %d: UE_id %d, Is_rrc_registered %d, rrc_sdu_length %d\n",
                  module_idP,frameP, subframeP,UE_id, Is_rrc_registered,rrc_sdu_length);

              if (rrc_sdu_length>0) {
                  LOG_I(MAC,"[eNB %d][RAPROC] Frame %d, subframeP %d: Generating Msg4 with RRC Piggyback (RA proc %d, RNTI %x)\n",
                      module_idP,frameP, subframeP,i,RA_template[i].rnti);

                  //msg("[MAC][eNB %d][RAPROC] Frame %d, subframeP %d: Received %d bytes for Msg4: \n",module_idP,frameP,subframeP,rrc_sdu_length);
                  //	  for (j=0;j<rrc_sdu_length;j++)
                  //	    msg("%x ",(unsigned char)eNB_mac_inst[module_idP].CCCH_pdu.payload[j]);
                  //	  msg("\n");
                  //	  msg("[MAC][eNB] Frame %d, subframeP %d: Generated DLSCH (Msg4) DCI, format 1A, for UE %d\n",frameP, subframeP,UE_id);
                  // Schedule Reflection of Connection request



                  // Compute MCS for 3 PRB
                  msg4_header = 1+6+1;  // CR header, CR CE, SDU header

                  if (mac_xface->lte_frame_parms->frame_type == TDD) {

                      switch (mac_xface->lte_frame_parms->N_RB_DL) {
                      case 6:
                        ((DCI1A_1_5MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->ndi=1;

                        if ((rrc_sdu_length+msg4_header) <= 22) {
                            ((DCI1A_1_5MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=4;
                            TBsize = 22;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 28) {
                            ((DCI1A_1_5MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=5;
                            TBsize = 28;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 32) {
                            ((DCI1A_1_5MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=6;
                            TBsize = 32;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 41) {
                            ((DCI1A_1_5MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=7;
                            TBsize = 41;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 49) {
                            ((DCI1A_1_5MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=8;
                            TBsize = 49;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 57) {
                            ((DCI1A_1_5MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=9;
                            TBsize = 57;
                        }
                        break;
                      case 25:

                        ((DCI1A_5MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->ndi=1;

                        if ((rrc_sdu_length+msg4_header) <= 22) {
                            ((DCI1A_5MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=4;
                            TBsize = 22;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 28) {
                            ((DCI1A_5MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=5;
                            TBsize = 28;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 32) {
                            ((DCI1A_5MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=6;
                            TBsize = 32;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 41) {
                            ((DCI1A_5MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=7;
                            TBsize = 41;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 49) {
                            ((DCI1A_5MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=8;
                            TBsize = 49;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 57) {
                            ((DCI1A_5MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=9;
                            TBsize = 57;
                        }
                        break;
                      case 50:

                        ((DCI1A_10MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->ndi=1;

                        if ((rrc_sdu_length+msg4_header) <= 22) {
                            ((DCI1A_10MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=4;
                            TBsize = 22;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 28) {
                            ((DCI1A_10MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=5;
                            TBsize = 28;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 32) {
                            ((DCI1A_10MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=6;
                            TBsize = 32;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 41) {
                            ((DCI1A_10MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=7;
                            TBsize = 41;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 49) {
                            ((DCI1A_10MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=8;
                            TBsize = 49;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 57) {
                            ((DCI1A_10MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=9;
                            TBsize = 57;
                        }
                        break;
                      case 100:

                        ((DCI1A_20MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->ndi=1;

                        if ((rrc_sdu_length+msg4_header) <= 22) {
                            ((DCI1A_20MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=4;
                            TBsize = 22;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 28) {
                            ((DCI1A_20MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=5;
                            TBsize = 28;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 32) {
                            ((DCI1A_20MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=6;
                            TBsize = 32;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 41) {
                            ((DCI1A_20MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=7;
                            TBsize = 41;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 49) {
                            ((DCI1A_20MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=8;
                            TBsize = 49;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 57) {
                            ((DCI1A_20MHz_TDD_1_6_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=9;
                            TBsize = 57;
                        }
                        break;
                      }
                  }
                  else { // FDD DCI
                      switch (mac_xface->lte_frame_parms->N_RB_DL) {
                      case 6:
                        ((DCI1A_1_5MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->ndi=1;

                        if ((rrc_sdu_length+msg4_header) <= 22) {
                            ((DCI1A_1_5MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=4;
                            TBsize = 22;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 28) {
                            ((DCI1A_1_5MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=5;
                            TBsize = 28;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 32) {
                            ((DCI1A_1_5MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=6;
                            TBsize = 32;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 41) {
                            ((DCI1A_1_5MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=7;
                            TBsize = 41;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 49) {
                            ((DCI1A_1_5MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=8;
                            TBsize = 49;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 57) {
                            ((DCI1A_1_5MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=9;
                            TBsize = 57;
                        }
                        break;
                      case 25:
                        ((DCI1A_5MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->ndi=1;

                        if ((rrc_sdu_length+msg4_header) <= 22) {
                            ((DCI1A_5MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=4;
                            TBsize = 22;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 28) {
                            ((DCI1A_5MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=5;
                            TBsize = 28;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 32) {
                            ((DCI1A_5MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=6;
                            TBsize = 32;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 41) {
                            ((DCI1A_5MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=7;
                            TBsize = 41;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 49) {
                            ((DCI1A_5MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=8;
                            TBsize = 49;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 57) {
                            ((DCI1A_5MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=9;
                            TBsize = 57;
                        }
                        break;
                      case 50:
                        ((DCI1A_10MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->ndi=1;

                        if ((rrc_sdu_length+msg4_header) <= 22) {
                            ((DCI1A_10MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=4;
                            TBsize = 22;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 28) {
                            ((DCI1A_10MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=5;
                            TBsize = 28;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 32) {
                            ((DCI1A_10MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=6;
                            TBsize = 32;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 41) {
                            ((DCI1A_10MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=7;
                            TBsize = 41;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 49) {
                            ((DCI1A_10MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=8;
                            TBsize = 49;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 57) {
                            ((DCI1A_5MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=9;
                            TBsize = 57;
                        }
                        break;
                      case 100:
                        ((DCI1A_20MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->ndi=1;

                        if ((rrc_sdu_length+msg4_header) <= 22) {
                            ((DCI1A_20MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=4;
                            TBsize = 22;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 28) {
                            ((DCI1A_20MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=5;
                            TBsize = 28;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 32) {
                            ((DCI1A_20MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=6;
                            TBsize = 32;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 41) {
                            ((DCI1A_20MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=7;
                            TBsize = 41;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 49) {
                            ((DCI1A_20MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=8;
                            TBsize = 49;
                        }
                        else if ((rrc_sdu_length+msg4_header) <= 57) {
                            ((DCI1A_20MHz_FDD_t*)&RA_template[i].RA_alloc_pdu2[0])->mcs=9;
                            TBsize = 57;
                        }
                        break;
                      }
                  }
                  RA_template[i].generate_Msg4=0;
                  RA_template[i].generate_Msg4_dci=1;
                  RA_template[i].wait_ack_Msg4=1;
                  RA_template[i].RA_active = FALSE;
                  lcid=0;

                  if ((TBsize - rrc_sdu_length - msg4_header) <= 2) {
                      msg4_padding = TBsize - rrc_sdu_length - msg4_header;
                      msg4_post_padding = 0;
                  }
                  else {
                      msg4_padding = 0;
                      msg4_post_padding = TBsize - rrc_sdu_length - msg4_header -1;
                  }
                  LOG_I(MAC,"[eNB %d][RAPROC] Frame %d subframeP %d Msg4 : TBS %d, sdu_len %d, msg4_header %d, msg4_padding %d, msg4_post_padding %d\n",
                      module_idP,frameP,subframeP,TBsize,rrc_sdu_length,msg4_header,msg4_padding,msg4_post_padding);
                  offset = generate_dlsch_header((unsigned char*)eNB_mac_inst[module_idP].DLSCH_pdu[(unsigned char)UE_id][0].payload[0],
                      1,                           //num_sdus
                      &rrc_sdu_length,             //
                      &lcid,                       // sdu_lcid
                      255,                         // no drx
                      0,                           // no timing advance
                      RA_template[i].cont_res_id,  // contention res id
                      msg4_padding,                // no padding
                      msg4_post_padding);

                  memcpy((void*)&eNB_mac_inst[module_idP].DLSCH_pdu[(unsigned char)UE_id][0].payload[0][(unsigned char)offset],
                      &eNB_mac_inst[module_idP].CCCH_pdu.payload[0],
                      rrc_sdu_length);

#if defined(USER_MODE) && defined(OAI_EMU)
                  if (oai_emulation.info.opt_enabled){
                      trace_pdu(1, (uint8_t *)eNB_mac_inst[module_idP].DLSCH_pdu[(unsigned char)UE_id][0].payload[0],
                          rrc_sdu_length, UE_id, 3, find_UE_RNTI(module_idP, UE_id),
                          eNB_mac_inst[module_idP].subframe,0,0);
                      LOG_D(OPT,"[eNB %d][DLSCH] Frame %d trace pdu for rnti %x with size %d\n",
                          module_idP, frameP, find_UE_RNTI(module_idP,UE_id), rrc_sdu_length);
                  }
#endif
                  *nprb= (*nprb) + 3;
                  *nCCE = (*nCCE) + 4;
              }
              //try here
          }
          /*
	else if (eNB_mac_inst[module_idP].RA_template[i].wait_ack_Msg4==1) {
	// check HARQ status and retransmit if necessary
	LOG_I(MAC,"[eNB %d][RAPROC] Frame %d, subframeP %d: Checking if Msg4 was acknowledged :\n",module_idP,frameP,subframeP);
	// Get candidate harq_pid from PHY
	mac_xface->get_ue_active_harq_pid(module_idP,eNB_mac_inst[module_idP].RA_template[i].rnti,subframeP,&harq_pid,&round,0);
	if (round>0) {
           *nprb= (*nprb) + 3;
           *nCCE = (*nCCE) + 4;
	}
	}
           */
      }
  }
  stop_meas(&eNB_mac_inst[module_idP].schedule_ra);
}

void initiate_ra_proc(module_id_t module_idP, frame_t frameP, uint16_t preamble_index,int16_t timing_offset,uint8_t sect_id,sub_frame_t subframeP,uint8_t f_id) {

  uint8_t i;

  LOG_I(MAC,"[eNB %d][RAPROC] Frame %d Initiating RA procedure for preamble index %d\n",module_idP,frameP,preamble_index);

  for (i=0;i<NB_RA_PROC_MAX;i++) {
      if (eNB_mac_inst[module_idP].RA_template[i].RA_active==FALSE) {
          eNB_mac_inst[module_idP].RA_template[i].RA_active=TRUE;
          eNB_mac_inst[module_idP].RA_template[i].generate_rar=1;
          eNB_mac_inst[module_idP].RA_template[i].generate_Msg4=0;
          eNB_mac_inst[module_idP].RA_template[i].wait_ack_Msg4=0;
          eNB_mac_inst[module_idP].RA_template[i].timing_offset=timing_offset;
          // Put in random rnti (to be replaced with proper procedure!!)
          eNB_mac_inst[module_idP].RA_template[i].rnti = taus();
          eNB_mac_inst[module_idP].RA_template[i].RA_rnti = 1+subframeP+(10*f_id);
          eNB_mac_inst[module_idP].RA_template[i].preamble_index = preamble_index;
          LOG_D(MAC,"[eNB %d][RAPROC] Frame %d Activating RAR generation for process %d, rnti %x, RA_active %d\n",
              module_idP,frameP,i,eNB_mac_inst[module_idP].RA_template[i].rnti,
              eNB_mac_inst[module_idP].RA_template[i].RA_active);

          return;
      }
  }
}