ra_procedures.c 22.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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
 */
21

22
/*! \file ra_procedures.c
23
 * \brief Routines for UE MAC-layer Random-access procedures (36.321) V8.6 2009-03
24
 * \author R. Knopp and Navid Nikaein
25 26 27
 * \date 2011
 * \version 0.1
 * \company Eurecom
28
 * \email: knopp@eurecom.fr navid.nikaein@eurecom.fr
29 30 31
 * \note
 * \warning
 */
32 33 34

#include "extern.h"
#include "defs.h"
35 36
#include "proto.h"
#include "UTIL/LOG/vcd_signal_dumper.h"
37 38 39 40
#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
41
#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
42
#include "UTIL/LOG/log.h"
winckel's avatar
winckel committed
43
#include "UTIL/OPT/opt.h"
44 45 46 47 48 49
#include "OCG.h"
#include "OCG_extern.h"
#ifdef PHY_EMUL
#include "SIMULATION/simulation_defs.h"
#endif

50
#include "SIMULATION/TOOLS/defs.h" // for taus
51

52 53
int8_t get_DELTA_PREAMBLE(module_id_t module_idP,int CC_id)
{
knopp's avatar
knopp committed
54 55 56 57

  if (CC_id>0) {
    LOG_E(MAC,"Transmission on secondary CCs is not supported yet\n");
    mac_xface->macphy_exit("MAC FATAL  CC_id>0");
58
    return 0; // not reached
knopp's avatar
knopp committed
59
  }
60

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

64
  if (UE_mac_inst[module_idP].tdd_Config) { // TDD
65
    if (prachConfigIndex < 20) {
66
      preambleformat = 0;
67
    } else if (prachConfigIndex < 30) {
68
      preambleformat = 1;
69
    } else if (prachConfigIndex < 40) {
70
      preambleformat = 2;
71
    } else if (prachConfigIndex < 48) {
72
      preambleformat = 3;
73
    } else {
74
      preambleformat = 4;
75
    }
76 77
  } else { // FDD
    preambleformat = prachConfigIndex>>2;
78
  }
79

80 81 82 83
  switch (preambleformat) {
  case 0:
  case 1:
    return(0);
84

85 86 87
  case 2:
  case 3:
    return(-3);
88

89 90
  case 4:
    return(8);
91

92 93
  default:
    LOG_E(MAC,"[UE %d] ue_procedures.c: FATAL, Illegal preambleformat %d, prachConfigIndex %d\n",
94 95
          module_idP,
          preambleformat,prachConfigIndex);
gauthier's avatar
gauthier committed
96
    mac_xface->macphy_exit("MAC get_DELTA_PREAMBLE Illegal preamble format");
97 98 99 100 101
    return(0);
  }

}

102

knopp's avatar
knopp committed
103
/// This routine implements Section 5.1.2 (UE Random Access Resource Selection) from 36.321
104
void get_prach_resources(module_id_t module_idP,
105 106 107 108 109 110
                         int CC_id,
                         uint8_t eNB_index,
                         uint8_t t_id,
                         uint8_t first_Msg3,
                         RACH_ConfigDedicated_t *rach_ConfigDedicated)
{
111

112
  uint8_t Msg3_size = UE_mac_inst[module_idP].RA_Msg3_size;
113
  PRACH_RESOURCES_t *prach_resources = &UE_mac_inst[module_idP].RA_prach_resources;
114
  RACH_ConfigCommon_t *rach_ConfigCommon = NULL;
115 116
  uint8_t noGroupB = 0;
  uint8_t f_id = 0,num_prach=0;
117
  int numberOfRA_Preambles;
118 119 120 121
  int messageSizeGroupA;
  int sizeOfRA_PreamblesGroupA;
  int messagePowerOffsetGroupB;
  int PLThreshold;
122

knopp's avatar
knopp committed
123 124 125 126 127
  if (CC_id>0) {
    LOG_E(MAC,"Transmission on secondary CCs is not supported yet\n");
    mac_xface->macphy_exit("MAC FATAL  CC_id>0");
    return;
  }
128

129
  if (UE_mac_inst[module_idP].radioResourceConfigCommon) {
130
    rach_ConfigCommon = &UE_mac_inst[module_idP].radioResourceConfigCommon->rach_ConfigCommon;
131
  } else {
132 133 134
    LOG_E(MAC,"[UE %d] FATAL  radioResourceConfigCommon is NULL !!!\n",module_idP);
    mac_xface->macphy_exit("MAC FATAL  radioResourceConfigCommon is NULL");
    return; // not reached
135 136
  }

137 138
  numberOfRA_Preambles = (1+rach_ConfigCommon->preambleInfo.numberOfRA_Preambles)<<2;  

139
  if (rach_ConfigDedicated) {   // This is for network controlled Mobility, later
140 141 142 143 144
    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;
    }
145 146
  }

Cedric Roux's avatar
Cedric Roux committed
147 148 149
  /* TODO: gcc warns if this variable is not always set, let's put -1 for no more warning */
  messageSizeGroupA = -1;

150
  if (!rach_ConfigCommon->preambleInfo.preamblesGroupAConfig) {
151 152
    noGroupB = 1;
  } else {
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    sizeOfRA_PreamblesGroupA = (rach_ConfigCommon->preambleInfo.preamblesGroupAConfig->sizeOfRA_PreamblesGroupA+1)<<2;
    switch (rach_ConfigCommon->preambleInfo.preamblesGroupAConfig->messageSizeGroupA) {
    case 0:
      messageSizeGroupA = 56;
      break;
    case 1:
      messageSizeGroupA = 144;
      break;
    case 2:
      messageSizeGroupA = 208;
      break;
    case 3:
      messageSizeGroupA = 256;
      break;
    }

Cedric Roux's avatar
Cedric Roux committed
169 170
    /* TODO: what value to use as default? */
    messagePowerOffsetGroupB = -9999;
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    switch (rach_ConfigCommon->preambleInfo.preamblesGroupAConfig->messagePowerOffsetGroupB) {
    case 0:
      messagePowerOffsetGroupB = -9999;
      break;
    case 1:
      messagePowerOffsetGroupB = 0;
      break;
    case 2:
      messagePowerOffsetGroupB = 5;
      break;
    case 3:
      messagePowerOffsetGroupB = 8;
      break;
    case 4:
      messagePowerOffsetGroupB = 10;
      break;
    case 5:
      messagePowerOffsetGroupB = 12;
      break;
    case 6:
      messagePowerOffsetGroupB = 15;
      break;
    case 7:
      messagePowerOffsetGroupB = 18;
      break;
    }

    PLThreshold = 0 - get_DELTA_PREAMBLE(module_idP,CC_id) - get_Po_NOMINAL_PUSCH(module_idP,CC_id) - messagePowerOffsetGroupB;
    // Note Pcmax is set to 0 here, we have to fix this

    if (sizeOfRA_PreamblesGroupA == numberOfRA_Preambles) {
202
      noGroupB = 1;
203
    }
204 205 206
  }

  if (first_Msg3 == 1) {
207 208
    if (noGroupB == 1) {
      // use Group A procedure
209
      UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  = (taus())%numberOfRA_Preambles;
210 211
      UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex = 0;
      UE_mac_inst[module_idP].RA_usedGroupA = 1;
212 213
    } else if ((Msg3_size <messageSizeGroupA) ||
               (mac_xface->get_PL(module_idP,0,eNB_index) > PLThreshold)) {
214
      // use Group A procedure
215
      UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  = (taus())%sizeOfRA_PreamblesGroupA;
216 217 218
      UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex = 0;
      UE_mac_inst[module_idP].RA_usedGroupA = 1;
    } else { // use Group B
219 220
      UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  = sizeOfRA_PreamblesGroupA +
        (taus())%(numberOfRA_Preambles - sizeOfRA_PreamblesGroupA);
221 222 223 224 225 226 227
      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,CC_id);
  } else { // Msg3 is being retransmitted
    if (UE_mac_inst[module_idP].RA_usedGroupA == 1) {
228
      if (rach_ConfigCommon->preambleInfo.preamblesGroupAConfig) {
229
        UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  = (taus())%rach_ConfigCommon->preambleInfo.preamblesGroupAConfig->sizeOfRA_PreamblesGroupA;
230
      } else {
231
        UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  = (taus())&0x3f;
232
      }
233 234 235

      UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex = 0;
    } else {
236
      // FIXME rach_ConfigCommon->preambleInfo.preamblesGroupAConfig may be zero
237 238 239
      UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex  =
        rach_ConfigCommon->preambleInfo.preamblesGroupAConfig->sizeOfRA_PreamblesGroupA +
        (taus())%(rach_ConfigCommon->preambleInfo.numberOfRA_Preambles -
240
                  rach_ConfigCommon->preambleInfo.preamblesGroupAConfig->sizeOfRA_PreamblesGroupA);
241 242
      UE_mac_inst[module_idP].RA_prach_resources.ra_RACH_MaskIndex = 0;
    }
243
  }
244

245
  // choose random PRACH resource in TDD
246
  if (UE_mac_inst[module_idP].tdd_Config) {
247
    num_prach = mac_xface->get_num_prach_tdd(mac_xface->frame_parms);
248

249
    if ((num_prach>0) && (num_prach<6)) {
250
      UE_mac_inst[module_idP].RA_prach_resources.ra_TDD_map_index = (taus()%num_prach);
251
    }
252

253
    f_id = mac_xface->get_fid_prach_tdd(mac_xface->frame_parms,
254
                                        UE_mac_inst[module_idP].RA_prach_resources.ra_TDD_map_index);
255 256 257
  }

  // choose RA-RNTI
258
  UE_mac_inst[module_idP].RA_prach_resources.ra_RNTI = 1 + t_id + 10*f_id;
259 260
}

261 262
void Msg1_tx(module_id_t module_idP,uint8_t CC_id,frame_t frameP, uint8_t eNB_id)
{
knopp's avatar
knopp committed
263 264 265 266 267 268

  if (CC_id>0) {
    LOG_E(MAC,"Transmission on secondary CCs is not supported yet\n");
    mac_xface->macphy_exit("MAC FATAL  CC_id>0");
    return;
  }
269 270

  // start contention resolution timer
271
  UE_mac_inst[module_idP].RA_attempt_number++;
272

navid's avatar
navid committed
273
  if (opt_enabled) {
274
    trace_pdu(0, NULL, 0, module_idP, 0 , UE_mac_inst[module_idP].RA_prach_resources.ra_PreambleIndex,
275
        UE_mac_inst[module_idP].txFrame, UE_mac_inst[module_idP].txSubframe, 0, UE_mac_inst[module_idP].RA_attempt_number);
276
    LOG_D(OPT,"[UE %d][RAPROC] TX MSG1 Frame %d trace pdu for rnti %x  with size %d\n",
277
          module_idP, frameP, 1, UE_mac_inst[module_idP].RA_Msg3_size);
278
  }
279

280 281 282
}


283 284
void Msg3_tx(module_id_t module_idP,uint8_t CC_id,frame_t frameP, uint8_t eNB_id)
{
knopp's avatar
knopp committed
285 286 287 288 289 290

  if (CC_id>0) {
    LOG_E(MAC,"Transmission on secondary CCs is not supported yet\n");
    mac_xface->macphy_exit("MAC FATAL  CC_id>0");
    return;
  }
291 292

  // start contention resolution timer
293
  LOG_D(MAC,"[UE %d][RAPROC] Frame %d : Msg3_tx: Setting contention resolution timer\n",module_idP,frameP);
294 295
  UE_mac_inst[module_idP].RA_contention_resolution_cnt = 0;
  UE_mac_inst[module_idP].RA_contention_resolution_timer_active = 1;
296

navid's avatar
navid committed
297
  if (opt_enabled) { // msg3
298
    trace_pdu(0, &UE_mac_inst[module_idP].CCCH_pdu.payload[0], UE_mac_inst[module_idP].RA_Msg3_size,
299
              module_idP, 3, UE_mac_inst[module_idP].crnti, UE_mac_inst[module_idP].txFrame, UE_mac_inst[module_idP].txSubframe, 0, 0);
300
    LOG_D(OPT,"[UE %d][RAPROC] MSG3 Frame %d trace pdu Preamble %d   with size %d\n",
301
          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);
302
  }
303

304 305 306
}


307 308
PRACH_RESOURCES_t *ue_get_rach(module_id_t module_idP,int CC_id,frame_t frameP, uint8_t eNB_indexP,sub_frame_t subframeP)
{
309 310


311
  uint8_t                        Size=0;
knopp's avatar
knopp committed
312
  UE_MODE_t                 UE_mode = mac_xface->get_ue_mode(module_idP,0,eNB_indexP);
313 314
  uint8_t                        lcid = CCCH;
  uint16_t                       Size16;
315
  struct RACH_ConfigCommon *rach_ConfigCommon = (struct RACH_ConfigCommon *)NULL;
316
  int32_t                       frame_diff=0;
317
  mac_rlc_status_resp_t     rlc_status;
318 319 320
  uint8_t                        dcch_header_len=0;
  uint16_t                       sdu_lengths[8];
  uint8_t                        ulsch_buff[MAX_ULSCH_PAYLOAD_BYTES];
321

knopp's avatar
knopp committed
322 323 324
  if (CC_id>0) {
    LOG_E(MAC,"Transmission on secondary CCs is not supported yet\n");
    mac_xface->macphy_exit("MAC FATAL  CC_id>0");
325
    return 0; // not reached
knopp's avatar
knopp committed
326 327
  }

328
  if (UE_mode == PRACH) {
329
    if (UE_mac_inst[module_idP].radioResourceConfigCommon) {
330
      rach_ConfigCommon = &UE_mac_inst[module_idP].radioResourceConfigCommon->rach_ConfigCommon;
331
    } else {
332 333 334 335 336 337
      return(NULL);
    }

    if (Is_rrc_registered == 1) {

      if (UE_mac_inst[module_idP].RA_active == 0) {
knopp's avatar
knopp committed
338
        LOG_D(MAC,"RA not active\n");
339 340
        // check if RRC is ready to initiate the RA procedure
        Size = mac_rrc_data_req(module_idP,
341
                                CC_id,
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
                                frameP,
                                CCCH,1,
                                &UE_mac_inst[module_idP].CCCH_pdu.payload[sizeof(SCH_SUBHEADER_SHORT)+1],0,
                                eNB_indexP,
                                0);
        Size16 = (uint16_t)Size;

        //  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;

366
            if (UE_mac_inst[module_idP].RA_window_cnt == 9) {
367
              UE_mac_inst[module_idP].RA_window_cnt = 10;  // Note: 9 subframe window doesn't exist, after 8 is 10!
368
            }
369 370 371
          } else {
            LOG_D(MAC,"[UE %d] FATAL Frame %d: rach_ConfigCommon is NULL !!!\n",module_idP,frameP);
            mac_xface->macphy_exit("MAC rach_ConfigCommon is NULL");
372
          }
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392

          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,CC_id,eNB_indexP,subframeP,1,NULL);

          generate_ulsch_header((uint8_t*)&UE_mac_inst[module_idP].CCCH_pdu.payload[0],  // mac header
                                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);
393

fnabet's avatar
fnabet committed
394
        } else if (UE_mac_inst[module_idP].scheduling_info.BSR_bytes[UE_mac_inst[module_idP].scheduling_info.LCGID[DCCH]] > 0) {
395 396
          // 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
397
          rlc_status = mac_rlc_status_ind(module_idP,UE_mac_inst[module_idP].crnti, eNB_indexP,frameP,subframeP,ENB_FLAG_NO,MBMS_FLAG_NO,
398 399 400 401
                                          DCCH,
                                          6);

          if (UE_mac_inst[module_idP].crnti_before_ho)
402 403
            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",
404 405 406 407 408
                  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);

409
          sdu_lengths[0] = mac_rlc_data_req(module_idP, UE_mac_inst[module_idP].crnti,
410
                                            eNB_indexP, frameP,ENB_FLAG_NO, MBMS_FLAG_NO,
411
                                            DCCH,
412
											6,	//not used
413 414 415
                                            (char *)&ulsch_buff[0]);

          LOG_D(MAC,"[UE %d] TX Got %d bytes for DCCH\n",module_idP,sdu_lengths[0]);
416 417 418 419 420 421
          update_bsr(module_idP, frameP, subframeP,eNB_indexP);
	      UE_mac_inst[module_idP].scheduling_info.BSR[UE_mac_inst[module_idP].scheduling_info.LCGID[DCCH]] =
	    		  locate_BsrIndexByBufferSize(BSR_TABLE, BSR_TABLE_SIZE, UE_mac_inst[module_idP].scheduling_info.BSR_bytes[UE_mac_inst[module_idP].scheduling_info.LCGID[DCCH]]);

	      //TO DO: fill BSR infos in UL TBS

422 423 424 425 426 427 428 429 430 431 432
          //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;

433
            if (UE_mac_inst[module_idP].RA_window_cnt == 9) {
434
              UE_mac_inst[module_idP].RA_window_cnt = 10;  // Note: 9 subframe window doesn't exist, after 8 is 10!
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
          } else {
            LOG_D(MAC,"[UE %d] FATAL Frame %d: rach_ConfigCommon is NULL !!!\n",module_idP,frameP);
            mac_xface->macphy_exit("MAC rach_ConfigCommon is NULL");
          }

          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,CC_id,eNB_indexP,subframeP,1,NULL);
          generate_ulsch_header((uint8_t*)ulsch_buff,  // mac header
                                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) {
          frame_diff = (sframe_t)frameP - UE_mac_inst[module_idP].RA_backoff_frame;

470
          if (frame_diff < 0) {
471
            frame_diff = -frame_diff;
472
          }
473 474 475 476 477 478 479 480 481 482 483

          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;

484
          if (frame_diff < 0) {
485
            frame_diff = -frame_diff;
486
          }
487 488 489 490 491 492 493 494 495 496 497 498

          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++;
499 500
          UE_mac_inst[module_idP].RA_prach_resources.ra_PREAMBLE_RECEIVED_TARGET_POWER +=
            (rach_ConfigCommon->powerRampingParameters.powerRampingStep<<1);  // 2dB increments in ASN.1 definition
Cedric Roux's avatar
Cedric Roux committed
501
	  int preambleTransMax = -1;
502
	  switch (rach_ConfigCommon->ra_SupervisionInfo.preambleTransMax) {
Cedric Roux's avatar
Cedric Roux committed
503
	  case PreambleTransMax_n3:
504 505
	    preambleTransMax = 3;
	    break;
Cedric Roux's avatar
Cedric Roux committed
506
	  case PreambleTransMax_n4:
507 508
	    preambleTransMax = 4;
	    break;
Cedric Roux's avatar
Cedric Roux committed
509
	  case PreambleTransMax_n5:
510 511
	    preambleTransMax = 5;
	    break;
Cedric Roux's avatar
Cedric Roux committed
512
	  case PreambleTransMax_n6:
513 514
	    preambleTransMax = 6;
	    break;
Cedric Roux's avatar
Cedric Roux committed
515
	  case PreambleTransMax_n7:
516 517
	    preambleTransMax = 7;
	    break;
Cedric Roux's avatar
Cedric Roux committed
518
	  case PreambleTransMax_n8:
519 520
	    preambleTransMax = 8;
	    break;
Cedric Roux's avatar
Cedric Roux committed
521
	  case PreambleTransMax_n10:
522 523
	    preambleTransMax = 10;
	    break;
Cedric Roux's avatar
Cedric Roux committed
524
	  case PreambleTransMax_n20:
525 526
	    preambleTransMax = 20;
	    break;
Cedric Roux's avatar
Cedric Roux committed
527
	  case PreambleTransMax_n50:
528 529
	    preambleTransMax = 50;
	    break;
Cedric Roux's avatar
Cedric Roux committed
530
	  case PreambleTransMax_n100:
531 532
	    preambleTransMax = 100;
	    break;
Cedric Roux's avatar
Cedric Roux committed
533
	  case PreambleTransMax_n200:
534 535 536 537 538 539
	    preambleTransMax = 200;
	    break;
	  } 

          if (UE_mac_inst[module_idP].RA_PREAMBLE_TRANSMISSION_COUNTER == preambleTransMax) {
            LOG_D(MAC,"[UE %d] Frame %d: Maximum number of RACH attempts (%d)\n",module_idP,frameP,preambleTransMax);
540 541 542
            // 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,CC_id);
543
          }
544 545 546 547 548 549 550 551

          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,CC_id,eNB_indexP,subframeP,0,NULL);
          return(&UE_mac_inst[module_idP].RA_prach_resources);
        }
552
      }
553 554 555 556
    }
  } else if (UE_mode == PUSCH) {
    LOG_D(MAC,"[UE %d] FATAL: Should not have checked for RACH in PUSCH yet ...",module_idP);
    mac_xface->macphy_exit("MAC FATAL: Should not have checked for RACH in PUSCH yet");
557
  }
558

559 560
  return(NULL);
}