rrc_UE.c 109 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 28 29 30 31 32 33 34 35 36 37 38 39
/*******************************************************************************

  Eurecom OpenAirInterface 2
  Copyright(c) 1999 - 2010 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

*******************************************************************************/

/*! \file rrc_UE.c
* \brief rrc procedures for UE
* \author Raymond Knopp and Navid Nikaein
* \date 2011
* \version 1.0
* \company Eurecom
* \email: raymond.knopp@eurecom.fr and  navid.nikaein@eurecom.fr
*/


#include "defs.h"
40
#include "PHY/TOOLS/dB_routines.h"
41 42 43 44 45 46
#include "extern.h"
#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
#include "LAYER2/RLC/rlc.h"
#include "COMMON/mac_rrc_primitives.h"
#include "UTIL/LOG/log.h"
#include "UTIL/LOG/vcd_signal_dumper.h"
47 48 49
#ifndef CELLULAR
#include "RRC/LITE/MESSAGES/asn1_msg.h"
#endif
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#include "RRCConnectionRequest.h"
#include "RRCConnectionReconfiguration.h"
#include "UL-CCCH-Message.h"
#include "DL-CCCH-Message.h"
#include "UL-DCCH-Message.h"
#include "DL-DCCH-Message.h"
#include "BCCH-DL-SCH-Message.h"
#ifdef Rel10
#include "MCCH-Message.h"
#endif
#include "MeasConfig.h"
#include "MeasGapConfig.h"
#include "MeasObjectEUTRA.h"
#include "TDD-Config.h"
#include "UECapabilityEnquiry.h"
#include "UE-CapabilityRequest.h"
#ifdef PHY_ABSTRACTION
#include "OCG.h"
#include "OCG_extern.h"
#endif
#ifdef USER_MODE
#include "RRC/NAS/nas_config.h"
#include "RRC/NAS/rb_config.h"
#endif
74 75 76 77 78

#if defined(ENABLE_SECURITY)
# include "UTIL/OSA/osa_defs.h"
#endif

79 80
#include "pdcp.h"

81 82 83 84
#if defined(ENABLE_ITTI)
# include "intertask_interface.h"
#endif

85 86 87 88 89 90 91 92 93 94 95 96
#ifdef PHY_EMUL
extern EMULATION_VARS *Emul_vars;
#endif
extern eNB_MAC_INST *eNB_mac_inst;
extern UE_MAC_INST *UE_mac_inst;
#ifdef BIGPHYSAREA
extern void *bigphys_malloc(int);
#endif

//#define XER_PRINT

extern inline unsigned int taus(void);
97
extern s8 dB_fixed2(u32 x,u32 y);
98 99 100 101 102 103 104 105 106 107 108 109 110 111

void init_SI_UE(u8 Mod_id,u8 eNB_index) {

  int i;


  UE_rrc_inst[Mod_id].sizeof_SIB1[eNB_index] = 0;
  UE_rrc_inst[Mod_id].sizeof_SI[eNB_index] = 0;

  UE_rrc_inst[Mod_id].SIB1[eNB_index] = (u8 *)malloc16(32);
  UE_rrc_inst[Mod_id].sib1[eNB_index] = (SystemInformationBlockType1_t *)malloc16(sizeof(SystemInformationBlockType1_t));
  UE_rrc_inst[Mod_id].SI[eNB_index] = (u8 *)malloc16(64);

  for (i=0;i<NB_CNX_UE;i++) {
112
    UE_rrc_inst[Mod_id].si[eNB_index][i] = (SystemInformation_t *)malloc16(sizeof(SystemInformation_t));
113 114 115 116 117 118 119 120 121 122
  }

  UE_rrc_inst[Mod_id].Info[eNB_index].SIB1Status = 0;
  UE_rrc_inst[Mod_id].Info[eNB_index].SIStatus = 0;


}

#ifdef Rel10
void init_MCCH_UE(u8 Mod_id, u8 eNB_index) {
123
  int i;
124 125 126
  UE_rrc_inst[Mod_id].sizeof_MCCH_MESSAGE[eNB_index] = 0;
  UE_rrc_inst[Mod_id].MCCH_MESSAGE[eNB_index] = (u8 *)malloc16(32);
  UE_rrc_inst[Mod_id].mcch_message[eNB_index] = (MBSFNAreaConfiguration_r9_t *)malloc16(sizeof(MBSFNAreaConfiguration_r9_t));
127 128
  for (i=0; i<8;i++) // MAX MBSFN Area
    UE_rrc_inst[Mod_id].Info[eNB_index].MCCHStatus[i] = 0;
129 130 131 132 133 134 135 136

}
#endif

static
void openair_rrc_lite_ue_init_security(u8 Mod_id)
{
#if defined(ENABLE_SECURITY)
137 138
//    uint8_t *kRRCenc;
//    uint8_t *kRRCint;
139 140 141 142 143 144 145 146 147
    char ascii_buffer[65];
    uint8_t i;

    memset(UE_rrc_inst[Mod_id].kenb, Mod_id, 32);

    for (i = 0; i < 32; i++) {
        sprintf(&ascii_buffer[2 * i], "%02X", UE_rrc_inst[Mod_id].kenb[i]);
    }

148
    LOG_T(RRC, "[OSA][UE %02d] kenb    = %s\n", Mod_id, ascii_buffer);
149
#endif
150
}
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

/*------------------------------------------------------------------------------*/
char openair_rrc_lite_ue_init(u8 Mod_id, unsigned char eNB_index){
  /*-----------------------------------------------------------------------------*/
  LOG_D(RRC,"[UE %d] INIT State = RRC_IDLE (eNB %d)\n",Mod_id,eNB_index);
  LOG_D(RRC,"[MSC_NEW][FRAME 00000][RRC_UE][MOD %02d][]\n", Mod_id+NB_eNB_INST);
  LOG_D(RRC, "[MSC_NEW][FRAME 00000][IP][MOD %02d][]\n", Mod_id+NB_eNB_INST);

  UE_rrc_inst[Mod_id].Info[eNB_index].State=RRC_IDLE;
  UE_rrc_inst[Mod_id].Info[eNB_index].T300_active = 0;
  UE_rrc_inst[Mod_id].Info[eNB_index].T304_active = 0;
  UE_rrc_inst[Mod_id].Info[eNB_index].T310_active = 0;
  UE_rrc_inst[Mod_id].Info[eNB_index].UE_index=0xffff;
  UE_rrc_inst[Mod_id].Srb0[eNB_index].Active=0;
  UE_rrc_inst[Mod_id].Srb1[eNB_index].Active=0;
  UE_rrc_inst[Mod_id].Srb2[eNB_index].Active=0;
167
  UE_rrc_inst[Mod_id].HandoverInfoUe.measFlag=1;
168

169 170 171 172 173 174 175 176 177
  UE_rrc_inst[Mod_id].ciphering_algorithm = SecurityAlgorithmConfig__cipheringAlgorithm_eea0;
#ifdef Rel10
  UE_rrc_inst[Mod_id].integrity_algorithm = SecurityAlgorithmConfig__integrityProtAlgorithm_eia0_v920;
#else
  UE_rrc_inst[Mod_id].integrity_algorithm = SecurityAlgorithmConfig__integrityProtAlgorithm_reserved;
#endif

  openair_rrc_lite_ue_init_security(Mod_id);

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
  init_SI_UE(Mod_id,eNB_index);
  LOG_D(RRC,"[UE %d] INIT: phy_sync_2_ch_ind\n", Mod_id);

#ifndef NO_RRM
  send_msg(&S_rrc,msg_rrc_phy_synch_to_CH_ind(Mod_id,eNB_index,UE_rrc_inst[Mod_id].Mac_id));
#endif

#ifdef NO_RRM //init ch SRB0, SRB1 & BDTCH
  openair_rrc_on(Mod_id,0);
#endif
#ifdef CBA 
  int j;
  for(j=0; j<NUM_MAX_CBA_GROUP; j++)
    UE_rrc_inst[Mod_id].cba_rnti[j] = 0x0000;
  UE_rrc_inst[Mod_id].num_active_cba_groups = 0;
#endif

  return 0;
}

/*------------------------------------------------------------------------------*/
void rrc_ue_generate_RRCConnectionRequest(u8 Mod_id, u32 frame, u8 eNB_index){
  /*------------------------------------------------------------------------------*/

  u8 i=0,rv[6];
203
  
204 205 206 207 208 209
  if(UE_rrc_inst[Mod_id].Srb0[eNB_index].Tx_buffer.payload_size ==0){

    // Get RRCConnectionRequest, fill random for now
    // Generate random byte stream for contention resolution
      for (i=0;i<6;i++) {
#ifdef SMBV   
210 211
        // if SMBV is configured the contention resolution needs to be fix for the connection procedure to succeed
        rv[i]=i;
212
#else
213
        rv[i]=taus()&0xff;
214 215 216 217 218 219 220
#endif
      LOG_T(RRC,"%x.",rv[i]);
    }
    LOG_T(RRC,"\n");
    UE_rrc_inst[Mod_id].Srb0[eNB_index].Tx_buffer.payload_size = do_RRCConnectionRequest((u8 *)UE_rrc_inst[Mod_id].Srb0[eNB_index].Tx_buffer.Payload,rv);

    LOG_I(RRC,"[UE %d] : Frame %d, Logical Channel UL-CCCH (SRB0), Generating RRCConnectionRequest (bytes %d, eNB %d)\n",
221
      Mod_id, frame, UE_rrc_inst[Mod_id].Srb0[eNB_index].Tx_buffer.payload_size, eNB_index);
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

    for (i=0;i<UE_rrc_inst[Mod_id].Srb0[eNB_index].Tx_buffer.payload_size;i++) {
      LOG_T(RRC,"%x.",UE_rrc_inst[Mod_id].Srb0[eNB_index].Tx_buffer.Payload[i]);
    }
    LOG_T(RRC,"\n");
    /*
      UE_rrc_inst[Mod_id].Srb0[Idx].Tx_buffer.Payload[i] = taus()&0xff;

    UE_rrc_inst[Mod_id].Srb0[Idx].Tx_buffer.payload_size =i;
    */

  }
}


mui_t rrc_mui=0;

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
/* NAS Attach request with IMSI */
static const char nas_attach_req_imsi[] =
{
    0x07, 0x41,
    /* EPS Mobile identity = IMSI */
    0x71, 0x08, 0x29, 0x80, 0x43, 0x21, 0x43, 0x65, 0x87,
    0xF9,
    /* End of EPS Mobile Identity */
    0x02, 0xE0, 0xE0, 0x00, 0x20, 0x02, 0x03,
    0xD0, 0x11, 0x27, 0x1A, 0x80, 0x80, 0x21, 0x10, 0x01, 0x00, 0x00,
    0x10, 0x81, 0x06, 0x00, 0x00, 0x00, 0x00, 0x83, 0x06, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x0A, 0x00, 0x52, 0x12, 0xF2,
    0x01, 0x27, 0x11,
};

/* NAS Attach request with GUTI */
static const char nas_attach_req_guti[] =
{
    0x07, 0x41,
258
    /* EPS Mobile identity = GUTI */
259 260 261 262 263 264 265 266 267 268
    0x71, 0x0B, 0xF6, 0x12, 0xF2, 0x01, 0x80, 0x00, 0x01, 0xE0, 0x00,
    0xDA, 0x1F,
    /* End of EPS Mobile Identity */
    0x02, 0xE0, 0xE0, 0x00, 0x20, 0x02, 0x03,
    0xD0, 0x11, 0x27, 0x1A, 0x80, 0x80, 0x21, 0x10, 0x01, 0x00, 0x00,
    0x10, 0x81, 0x06, 0x00, 0x00, 0x00, 0x00, 0x83, 0x06, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x0A, 0x00, 0x52, 0x12, 0xF2,
    0x01, 0x27, 0x11,
};

269
/*------------------------------------------------------------------------------*/
270
void rrc_ue_generate_RRCConnectionSetupComplete(u8 Mod_id, u32 frame, u8 eNB_index, uint8_t Transaction_id){
271 272
  /*------------------------------------------------------------------------------*/

273
  u8 buffer[100];
274 275
  u8 size;

276
  size = do_RRCConnectionSetupComplete(buffer, Transaction_id, sizeof(nas_attach_req_guti), nas_attach_req_guti);
277 278

  LOG_I(RRC,"[UE %d][RAPROC] Frame %d : Logical Channel UL-DCCH (SRB1), Generating RRCConnectionSetupComplete (bytes%d, eNB %d)\n",
279
    Mod_id,frame, size, eNB_index);
280 281 282 283 284

   LOG_D(RLC, "[MSC_MSG][FRAME %05d][RRC_UE][MOD %02d][][--- PDCP_DATA_REQ/%d Bytes (RRCConnectionSetupComplete to eNB %d MUI %d) --->][PDCP][MOD %02d][RB %02d]\n",
                                     frame, Mod_id+NB_eNB_INST, size, eNB_index, rrc_mui, Mod_id+NB_eNB_INST, DCCH);

   //  rrc_rlc_data_req(Mod_id+NB_eNB_INST,frame, 0 ,DCCH,rrc_mui++,0,size,(char*)buffer);
285
  pdcp_rrc_data_req (Mod_id + NB_eNB_INST, frame, 0, DCCH, rrc_mui++, 0, size, buffer, 1);
286 287 288

}

289
void rrc_ue_generate_RRCConnectionReconfigurationComplete(u8 Mod_id, u32 frame, u8 eNB_index, uint8_t Transaction_id) {
290 291 292

  u8 buffer[32], size;

293
  size = do_RRCConnectionReconfigurationComplete(buffer, Transaction_id);
294 295

  LOG_I(RRC,"[UE %d] Frame %d : Logical Channel UL-DCCH (SRB1), Generating RRCConnectionReconfigurationComplete (bytes %d, eNB_index %d)\n",
296
    Mod_id,frame, size, eNB_index);
297
  LOG_D(RLC, "[MSC_MSG][FRAME %05d][RRC_UE][MOD %02d][][--- PDCP_DATA_REQ/%d Bytes (RRCConnectionReconfigurationComplete to eNB %d MUI %d) --->][PDCP][MOD %02d][RB %02d]\n",
298
    frame, Mod_id+NB_eNB_INST, size, eNB_index, rrc_mui, Mod_id+NB_eNB_INST, DCCH);
299 300

  //rrc_rlc_data_req(Mod_id+NB_eNB_INST,frame, 0 ,DCCH,rrc_mui++,0,size,(char*)buffer);
301
  pdcp_rrc_data_req (Mod_id + NB_eNB_INST, frame, 0, DCCH, rrc_mui++, 0, size, buffer, 1);
302 303 304 305 306 307 308 309 310
}

/*------------------------------------------------------------------------------*/
int rrc_ue_decode_ccch(u8 Mod_id, u32 frame, SRB_INFO *Srb_info, u8 eNB_index){
  /*------------------------------------------------------------------------------*/

  //DL_CCCH_Message_t dlccchmsg;
  DL_CCCH_Message_t *dl_ccch_msg=NULL;//&dlccchmsg;
  asn_dec_rval_t dec_rval;
311
  int rval=0;
312 313 314 315 316 317

  vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_DECODE_CCCH, VCD_FUNCTION_IN);

  //memset(dl_ccch_msg,0,sizeof(DL_CCCH_Message_t));

  //  LOG_D(RRC,"[UE %d] Decoding DL-CCCH message (%d bytes), State %d\n",Mod_id,Srb_info->Rx_buffer.payload_size,
318
  //  UE_rrc_inst[Mod_id].Info[eNB_index].State);
319 320

  dec_rval = uper_decode(NULL,
321 322 323 324
      &asn_DEF_DL_CCCH_Message,
      (void**)&dl_ccch_msg,
      (uint8_t*)Srb_info->Rx_buffer.Payload,
      100,0,0);
325 326 327 328 329

#ifdef XER_PRINT
  xer_fprint(stdout,&asn_DEF_DL_CCCH_Message,(void*)dl_ccch_msg);
#endif

330
#if defined(ENABLE_ITTI)
331
# if defined(DISABLE_ITTI_XER_PRINT)
332 333 334 335
  {
    MessageDef *message_p;

    message_p = itti_alloc_new_message (TASK_RRC_UE, RRC_DL_CCCH_MESSAGE);
336
    memcpy (&message_p->ittiMsg, (void *) dl_ccch_msg, sizeof(RrcDlCcchMessage));
337

338
    itti_send_msg_to_task (TASK_UNKNOWN, Mod_id + NB_eNB_INST, message_p);
339
  }
340 341
# else
  {
342 343
    char        message_string[10000];
    size_t      message_string_size;
344

345
    if ((message_string_size = xer_sprint(message_string, sizeof(message_string), &asn_DEF_DL_CCCH_Message, (void *)dl_ccch_msg)) > 0)
346 347 348
    {
      MessageDef *message_p;

349
      message_p = itti_alloc_new_message_sized (TASK_RRC_UE, GENERIC_LOG, message_string_size);
350 351
      memcpy(&message_p->ittiMsg.generic_log, message_string, message_string_size);

352
      itti_send_msg_to_task(TASK_UNKNOWN, Mod_id + NB_eNB_INST, message_p);
353 354 355
    }
  }
# endif
356 357
#endif

358 359 360 361 362 363 364 365 366 367 368 369
  if ((dec_rval.code != RC_OK) && (dec_rval.consumed==0)) {
    LOG_E(RRC,"[UE %d] Frame %d : Failed to decode DL-CCCH-Message (%d bytes)\n",Mod_id,dec_rval.consumed);
    vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_DECODE_CCCH, VCD_FUNCTION_OUT);
    return -1;
  }

  if (dl_ccch_msg->message.present == DL_CCCH_MessageType_PR_c1) {

    if (UE_rrc_inst[Mod_id].Info[eNB_index].State == RRC_SI_RECEIVED) {

      switch (dl_ccch_msg->message.choice.c1.present) {

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
        case DL_CCCH_MessageType__c1_PR_NOTHING:
          LOG_I(RRC, "[UE%d] Frame %d : Received PR_NOTHING on DL-CCCH-Message\n", Mod_id, frame);
          rval = 0;
          break;

        case DL_CCCH_MessageType__c1_PR_rrcConnectionReestablishment:
          LOG_D(RRC,
              "[MSC_MSG][FRAME %05d][MAC_UE][MOD %02d][][--- MAC_DATA_IND (rrcConnectionReestablishment ENB %d) --->][RRC_UE][MOD %02d][]\n", frame, Mod_id+NB_eNB_INST, eNB_index, Mod_id+NB_eNB_INST);

          LOG_I(RRC,
              "[UE%d] Frame %d : Logical Channel DL-CCCH (SRB0), Received RRCConnectionReestablishment\n", Mod_id, frame);
          rval = 0;
          break;

        case DL_CCCH_MessageType__c1_PR_rrcConnectionReestablishmentReject:
          LOG_D(RRC,
              "[MSC_MSG][FRAME %05d][MAC_UE][MOD %02d][][--- MAC_DATA_IND (RRCConnectionReestablishmentReject ENB %d) --->][RRC_UE][MOD %02d][]\n", frame, Mod_id+NB_eNB_INST, eNB_index, Mod_id+NB_eNB_INST);
          LOG_I(RRC,
              "[UE%d] Frame %d : Logical Channel DL-CCCH (SRB0), Received RRCConnectionReestablishmentReject\n", Mod_id, frame);
          rval = 0;
          break;

        case DL_CCCH_MessageType__c1_PR_rrcConnectionReject:
          LOG_D(RRC,
              "[MSC_MSG][FRAME %05d][MAC_UE][MOD %02d][][--- MAC_DATA_IND (rrcConnectionReject ENB %d) --->][RRC_UE][MOD %02d][]\n", frame, Mod_id+NB_eNB_INST, eNB_index, Mod_id+NB_eNB_INST);

          LOG_I(RRC,
              "[UE%d] Frame %d : Logical Channel DL-CCCH (SRB0), Received RRCConnectionReject \n", Mod_id, frame);
          rval = 0;
          break;

        case DL_CCCH_MessageType__c1_PR_rrcConnectionSetup:
          LOG_D(RRC,
              "[MSC_MSG][FRAME %05d][MAC_UE][MOD %02d][][--- MAC_DATA_IND (rrcConnectionSetup ENB %d) --->][RRC_UE][MOD %02d][]\n", frame, Mod_id+NB_eNB_INST, eNB_index, Mod_id+NB_eNB_INST);

          LOG_I(RRC,
              "[UE%d][RAPROC] Frame %d : Logical Channel DL-CCCH (SRB0), Received RRCConnectionSetup \n", Mod_id, frame);
          // Get configuration

          // Release T300 timer
          UE_rrc_inst[Mod_id].Info[eNB_index].T300_active = 0;
          rrc_ue_process_radioResourceConfigDedicated(Mod_id, frame, eNB_index,
              &dl_ccch_msg->message.choice.c1.choice.rrcConnectionSetup.criticalExtensions.choice.c1.choice.rrcConnectionSetup_r8.radioResourceConfigDedicated);

          rrc_ue_generate_RRCConnectionSetupComplete(Mod_id, frame, eNB_index, dl_ccch_msg->message.choice.c1.choice.rrcConnectionSetup.rrc_TransactionIdentifier);

          rval = 0;
          break;

        default:
          LOG_E(RRC, "[UE%d] Frame %d : Unknown message\n", Mod_id, frame);
          rval = -1;
          break;
423 424 425 426 427 428 429 430
      }
    }
  }

  vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_DECODE_CCCH, VCD_FUNCTION_OUT);
  return rval;
}

431 432
s32 rrc_ue_establish_srb1(u8 Mod_id,u32 frame,u8 eNB_index, struct SRB_ToAddMod *SRB_config) {
  // add descriptor from RRC PDU
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455

  u8 lchan_id = DCCH;

  UE_rrc_inst[Mod_id].Srb1[eNB_index].Active = 1;
  UE_rrc_inst[Mod_id].Srb1[eNB_index].Status = RADIO_CONFIG_OK;//RADIO CFG
  UE_rrc_inst[Mod_id].Srb1[eNB_index].Srb_info.Srb_id = 1;

    // copy default configuration for now
  //  memcpy(&UE_rrc_inst[Mod_id].Srb1[eNB_index].Srb_info.Lchan_desc[0],&DCCH_LCHAN_DESC,LCHAN_DESC_SIZE);
  //  memcpy(&UE_rrc_inst[Mod_id].Srb1[eNB_index].Srb_info.Lchan_desc[1],&DCCH_LCHAN_DESC,LCHAN_DESC_SIZE);


  LOG_I(RRC,"[UE %d], CONFIG_SRB1 %d corresponding to eNB_index %d\n", Mod_id,lchan_id,eNB_index);

  //rrc_pdcp_config_req (Mod_id+NB_eNB_INST, frame, 0, ACTION_ADD, lchan_id,UNDEF_SECURITY_MODE);
  //  rrc_rlc_config_req(Mod_id+NB_eNB_INST,frame,0,ACTION_ADD,lchan_id,SIGNALLING_RADIO_BEARER,Rlc_info_am_config);

  //  UE_rrc_inst[Mod_id].Srb1[eNB_index].Srb_info.Tx_buffer.payload_size=DEFAULT_MEAS_IND_SIZE+1;


  return(0);
}

456 457
s32 rrc_ue_establish_srb2(u8 Mod_id,u32 frame,u8 eNB_index, struct SRB_ToAddMod *SRB_config) {
  // add descriptor from RRC PDU
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

  u8 lchan_id = DCCH1;

  UE_rrc_inst[Mod_id].Srb2[eNB_index].Active = 1;
  UE_rrc_inst[Mod_id].Srb2[eNB_index].Status = RADIO_CONFIG_OK;//RADIO CFG
  UE_rrc_inst[Mod_id].Srb2[eNB_index].Srb_info.Srb_id = 2;

    // copy default configuration for now
  //  memcpy(&UE_rrc_inst[Mod_id].Srb2[eNB_index].Srb_info.Lchan_desc[0],&DCCH_LCHAN_DESC,LCHAN_DESC_SIZE);
  //  memcpy(&UE_rrc_inst[Mod_id].Srb2[eNB_index].Srb_info.Lchan_desc[1],&DCCH_LCHAN_DESC,LCHAN_DESC_SIZE);


  LOG_I(RRC,"[UE %d], CONFIG_SRB2 %d corresponding to eNB_index %d\n",Mod_id,lchan_id,eNB_index);

  //rrc_pdcp_config_req (Mod_id+NB_eNB_INST, frame, 0, ACTION_ADD, lchan_id, UNDEF_SECURITY_MODE);
  //  rrc_rlc_config_req(Mod_id+NB_eNB_INST,frame,0,ACTION_ADD,lchan_id,SIGNALLING_RADIO_BEARER,Rlc_info_am_config);

  //  UE_rrc_inst[Mod_id].Srb1[eNB_index].Srb_info.Tx_buffer.payload_size=DEFAULT_MEAS_IND_SIZE+1;


  return(0);
}

s32 rrc_ue_establish_drb(u8 Mod_id,u32 frame,u8 eNB_index,
482
                                        struct DRB_ToAddMod *DRB_config) { // add descriptor from RRC PDU
483
#ifdef NAS_NETLINK
484
  int oip_ifup=0,ip_addr_offset3=0,ip_addr_offset4=0;
485
#endif
486 487

  LOG_I(RRC,"[UE] Frame %d: processing RRCConnectionReconfiguration: reconfiguring DRB %ld/LCID %d\n",
488
    frame,DRB_config->drb_Identity,(int)*DRB_config->logicalChannelIdentity);
489 490
  /*
  rrc_pdcp_config_req (Mod_id+NB_eNB_INST, frame, 0, ACTION_ADD,
491
                             (eNB_index * NB_RB_MAX) + *DRB_config->logicalChannelIdentity, UNDEF_SECURITY_MODE);
492 493
 
 rrc_rlc_config_req(Mod_id+NB_eNB_INST,frame,0,ACTION_ADD,
494 495
                    (eNB_index * NB_RB_MAX) + *DRB_config->logicalChannelIdentity,
                    RADIO_ACCESS_BEARER,Rlc_info_um);
496 497 498 499 500 501 502 503 504 505 506
  */
#ifdef NAS_NETLINK
#    ifdef OAI_EMU
    ip_addr_offset3 = oai_emulation.info.nb_enb_local;
    ip_addr_offset4 = NB_eNB_INST;
#    else
    ip_addr_offset3 = 0;
    ip_addr_offset4 = 8;
#    endif
#    if !defined(OAI_NW_DRIVER_TYPE_ETHERNET) && !defined(EXMIMO)
    LOG_I(OIP,"[UE %d] trying to bring up the OAI interface oai%d, IP 10.0.%d.%d\n", Mod_id, ip_addr_offset3+Mod_id,
507
          ip_addr_offset3+Mod_id+1,ip_addr_offset4+Mod_id+1);
508
    oip_ifup=nas_config(ip_addr_offset3+Mod_id,   // interface_id
509 510
                        ip_addr_offset3+Mod_id+1, // third_octet
                        ip_addr_offset4+Mod_id+1); // fourth_octet
511 512 513 514 515
    if (oip_ifup == 0 ){ // interface is up --> send a config the DRB
#        ifdef OAI_EMU
      oai_emulation.info.oai_ifup[Mod_id]=1;
#        endif
      LOG_I(OIP,"[UE %d] Config the oai%d to send/receive pkt on DRB %d to/from the protocol stack\n",
516 517 518 519 520 521 522 523 524 525 526 527
            Mod_id,
            ip_addr_offset3+Mod_id,
            (eNB_index * NB_RB_MAX) + *DRB_config->logicalChannelIdentity);

      rb_conf_ipv4(0,//add
                   Mod_id,//cx align with the UE index
                   ip_addr_offset3+Mod_id,//inst num_enb+ue_index
                   (eNB_index * NB_RB_MAX) + *DRB_config->logicalChannelIdentity,//rb
                   0,//dscp
                   ipv4_address(ip_addr_offset3+Mod_id+1,ip_addr_offset4+Mod_id+1),//saddr
                   ipv4_address(ip_addr_offset3+Mod_id+1,eNB_index+1));//daddr
      LOG_D(RRC,"[UE %d] State = Attached (eNB %d)\n",Mod_id,eNB_index);
528 529 530 531 532 533 534 535 536 537 538 539
    }
#    else
#        ifdef OAI_EMU
      oai_emulation.info.oai_ifup[Mod_id]=1;
#        endif
#    endif
#endif

  return(0);
}


540
void  rrc_ue_process_measConfig(u8 Mod_id,u32 frame, u8 eNB_index,MeasConfig_t *measConfig){
541 542 543 544 545 546 547 548 549

  // This is the procedure described in 36.331 Section 5.5.2.1
  int i;
  long ind;
  MeasObjectToAddMod_t *measObj;

  if (measConfig->measObjectToRemoveList != NULL) {
    for (i=0;i<measConfig->measObjectToRemoveList->list.count;i++) {
      ind   = *measConfig->measObjectToRemoveList->list.array[i];
550
      free(UE_rrc_inst[Mod_id].MeasObj[eNB_index][ind-1]);
551 552 553 554 555 556 557 558 559
    }
  }
  if (measConfig->measObjectToAddModList != NULL) {
    LOG_D(RRC,"Measurement Object List is present\n");
    for (i=0;i<measConfig->measObjectToAddModList->list.count;i++) {
      measObj = measConfig->measObjectToAddModList->list.array[i];
      ind   = measConfig->measObjectToAddModList->list.array[i]->measObjectId;

      if (UE_rrc_inst[Mod_id].MeasObj[eNB_index][ind-1]) {
560 561 562 563
        LOG_D(RRC,"Modifying measurement object %d\n",ind);
        memcpy((char*)UE_rrc_inst[Mod_id].MeasObj[eNB_index][ind-1],
            (char*)measObj,
            sizeof(MeasObjectToAddMod_t));
564 565
      }
      else {
566 567 568 569 570 571 572 573 574
        LOG_I(RRC,"Adding measurement object %d\n",ind);
        if (measObj->measObject.present == MeasObjectToAddMod__measObject_PR_measObjectEUTRA) {
          LOG_I(RRC,"EUTRA Measurement : carrierFreq %d, allowedMeasBandwidth %d,presenceAntennaPort1 %d, neighCellConfig %d\n",
              measObj->measObject.choice.measObjectEUTRA.carrierFreq,
              measObj->measObject.choice.measObjectEUTRA.allowedMeasBandwidth,
              measObj->measObject.choice.measObjectEUTRA.presenceAntennaPort1,
              measObj->measObject.choice.measObjectEUTRA.neighCellConfig.buf[0]);
          UE_rrc_inst[Mod_id].MeasObj[eNB_index][ind-1]=measObj;
        }
575 576 577
      }
    }
    rrc_mac_config_req(Mod_id,0,0,eNB_index,
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
        (RadioResourceConfigCommonSIB_t *)NULL,
        (struct PhysicalConfigDedicated *)NULL,
        UE_rrc_inst[Mod_id].MeasObj[eNB_index],
        (MAC_MainConfig_t *)NULL,
        0,
        (struct LogicalChannelConfig *)NULL,
        (MeasGapConfig_t *)NULL,
        (TDD_Config_t *)NULL,
        (MobilityControlInfo_t *)NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL
#ifdef Rel10
        ,
        0,
        (MBSFN_AreaInfoList_r9_t *)NULL,
        (PMCH_InfoList_r9_t *)NULL
598 599
#endif
#ifdef CBA
600 601 602
        ,
        0,
        0
603
#endif
604
    );
605 606 607 608
  }
  if (measConfig->reportConfigToRemoveList != NULL) {
    for (i=0;i<measConfig->reportConfigToRemoveList->list.count;i++) {
      ind   = *measConfig->reportConfigToRemoveList->list.array[i];
609
      free(UE_rrc_inst[Mod_id].ReportConfig[eNB_index][ind-1]);
610 611 612 613 614 615
    }
  }
  if (measConfig->reportConfigToAddModList != NULL) {
    LOG_I(RRC,"Report Configuration List is present\n");
    for (i=0;i<measConfig->reportConfigToAddModList->list.count;i++) {
      ind   = measConfig->reportConfigToAddModList->list.array[i]->reportConfigId;
616
      if (UE_rrc_inst[Mod_id].ReportConfig[eNB_index][ind-1]) {
617 618 619 620
        LOG_I(RRC,"Modifying Report Configuration %d\n",ind-1);
        memcpy((char*)UE_rrc_inst[Mod_id].ReportConfig[eNB_index][ind-1],
            (char*)measConfig->reportConfigToAddModList->list.array[i],
            sizeof(ReportConfigToAddMod_t));
621 622
      }
      else {
623 624
        LOG_D(RRC,"Adding Report Configuration %d %p \n",ind-1,measConfig->reportConfigToAddModList->list.array[i]);
        UE_rrc_inst[Mod_id].ReportConfig[eNB_index][ind-1] = measConfig->reportConfigToAddModList->list.array[i];
625 626 627 628 629 630 631
      }
    }
  }

  if (measConfig->quantityConfig != NULL) {
    if (UE_rrc_inst[Mod_id].QuantityConfig[eNB_index]) {
      LOG_D(RRC,"Modifying Quantity Configuration \n");
632
      memcpy((char*)UE_rrc_inst[Mod_id].QuantityConfig[eNB_index],
633 634
          (char*)measConfig->quantityConfig,
          sizeof(QuantityConfig_t));
635 636 637 638 639 640 641 642 643 644
    }
    else {
      LOG_D(RRC,"Adding Quantity configuration\n");
      UE_rrc_inst[Mod_id].QuantityConfig[eNB_index] = measConfig->quantityConfig;
    }
  }

  if (measConfig->measIdToRemoveList != NULL) {
    for (i=0;i<measConfig->measIdToRemoveList->list.count;i++) {
      ind   = *measConfig->measIdToRemoveList->list.array[i];
645
      free(UE_rrc_inst[Mod_id].MeasId[eNB_index][ind-1]);
646 647 648 649 650 651
    }
  }

  if (measConfig->measIdToAddModList != NULL) {
    for (i=0;i<measConfig->measIdToAddModList->list.count;i++) {
      ind   = measConfig->measIdToAddModList->list.array[i]->measId;
652
      if (UE_rrc_inst[Mod_id].MeasId[eNB_index][ind-1]) {
653 654 655 656
        LOG_D(RRC,"Modifying Measurement ID %d\n",ind-1);
        memcpy((char*)UE_rrc_inst[Mod_id].MeasId[eNB_index][ind-1],
            (char*)measConfig->measIdToAddModList->list.array[i],
            sizeof(MeasIdToAddMod_t));
657 658
      }
      else {
659 660
        LOG_D(RRC,"Adding Measurement ID %d %p\n",ind-1,measConfig->measIdToAddModList->list.array[i]);
        UE_rrc_inst[Mod_id].MeasId[eNB_index][ind-1] = measConfig->measIdToAddModList->list.array[i];
661 662 663 664 665 666
      }
    }
  }

  if (measConfig->measGapConfig !=NULL) {
    if (UE_rrc_inst[Mod_id].measGapConfig[eNB_index]) {
667
      memcpy((char*)UE_rrc_inst[Mod_id].measGapConfig[eNB_index],
668 669
           (char*)measConfig->measGapConfig,
                sizeof(MeasGapConfig_t));
670 671 672 673 674 675
    }
    else {
      UE_rrc_inst[Mod_id].measGapConfig[eNB_index] = measConfig->measGapConfig;
    }
  }

676 677 678 679
  if (measConfig->quantityConfig != NULL) {
    if (UE_rrc_inst[Mod_id].QuantityConfig[eNB_index]) {
      LOG_I(RRC,"Modifying Quantity Configuration \n");
      memcpy((char*)UE_rrc_inst[Mod_id].QuantityConfig[eNB_index],
680 681
          (char*)measConfig->quantityConfig,
          sizeof(QuantityConfig_t));
682 683 684 685 686 687 688 689 690 691
    }
    else {
      LOG_I(RRC,"Adding Quantity configuration\n");
      UE_rrc_inst[Mod_id].QuantityConfig[eNB_index] = measConfig->quantityConfig;
    }
    
    UE_rrc_inst[Mod_id].filter_coeff_rsrp = 1./pow(2,(*UE_rrc_inst[Mod_id].QuantityConfig[eNB_index]->quantityConfigEUTRA->filterCoefficientRSRP)/4);
    UE_rrc_inst[Mod_id].filter_coeff_rsrq = 1./pow(2,(*UE_rrc_inst[Mod_id].QuantityConfig[eNB_index]->quantityConfigEUTRA->filterCoefficientRSRQ)/4);

    LOG_I(RRC,"[UE %d] set rsrp-coeff for eNB %d: %d rsrq-coeff: %d rsrp_factor: %f rsrq_factor: %f \n",
692 693 694 695 696
        Mod_id, eNB_index, // UE_rrc_inst[Mod_id].Info[eNB_index].UE_index,
        *UE_rrc_inst[Mod_id].QuantityConfig[eNB_index]->quantityConfigEUTRA->filterCoefficientRSRP,
        *UE_rrc_inst[Mod_id].QuantityConfig[eNB_index]->quantityConfigEUTRA->filterCoefficientRSRQ,
        UE_rrc_inst[Mod_id].filter_coeff_rsrp, UE_rrc_inst[Mod_id].filter_coeff_rsrp,
        UE_rrc_inst[Mod_id].filter_coeff_rsrp, UE_rrc_inst[Mod_id].filter_coeff_rsrq);
697 698
  }

699 700 701
  if (measConfig->s_Measure != NULL) {
    UE_rrc_inst[Mod_id].s_measure = *measConfig->s_Measure;
  }
702 703 704 705 706 707 708
 
  if (measConfig->speedStatePars != NULL) {
    if (UE_rrc_inst[Mod_id].speedStatePars)
      memcpy((char*)UE_rrc_inst[Mod_id].speedStatePars,(char*)measConfig->speedStatePars,sizeof(struct MeasConfig__speedStatePars));
    else
      UE_rrc_inst[Mod_id].speedStatePars = measConfig->speedStatePars;
    LOG_I(RRC,"[UE %d] Configuring mobility optimization params for UE %d \n",
709
        Mod_id,UE_rrc_inst[Mod_id].Info[0].UE_index);
710
  }
711 712
}

713
void    
714
rrc_ue_process_radioResourceConfigDedicated(u8 Mod_id,u32 frame, u8 eNB_index,
715
                                            RadioResourceConfigDedicated_t *radioResourceConfigDedicated) {
716 717 718 719 720 721 722 723 724 725 726 727 728

  long SRB_id,DRB_id;
  int i,cnt;
  LogicalChannelConfig_t *SRB1_logicalChannelConfig,*SRB2_logicalChannelConfig;
#ifdef CBA  
  uint8_t cba_found = 0;
  uint16_t cba_RNTI;
#endif 

  // Save physicalConfigDedicated if present
  if (radioResourceConfigDedicated->physicalConfigDedicated) {
    if (UE_rrc_inst[Mod_id].physicalConfigDedicated[eNB_index]) {
      memcpy((char*)UE_rrc_inst[Mod_id].physicalConfigDedicated[eNB_index],(char*)radioResourceConfigDedicated->physicalConfigDedicated,
729
           sizeof(struct PhysicalConfigDedicated));
730 731 732 733 734 735 736 737 738 739

    }
    else {
      UE_rrc_inst[Mod_id].physicalConfigDedicated[eNB_index] = radioResourceConfigDedicated->physicalConfigDedicated;
    }
  }
  // Apply macMainConfig if present
  if (radioResourceConfigDedicated->mac_MainConfig) {
    if (radioResourceConfigDedicated->mac_MainConfig->present == RadioResourceConfigDedicated__mac_MainConfig_PR_explicitValue) {
      if (UE_rrc_inst[Mod_id].mac_MainConfig[eNB_index]) {
740 741
        memcpy((char*)UE_rrc_inst[Mod_id].mac_MainConfig[eNB_index],(char*)&radioResourceConfigDedicated->mac_MainConfig->choice.explicitValue,
             sizeof(MAC_MainConfig_t));
742 743
      }
      else
744
        UE_rrc_inst[Mod_id].mac_MainConfig[eNB_index] = &radioResourceConfigDedicated->mac_MainConfig->choice.explicitValue;
745 746 747 748 749 750 751
    }
  }

  // Apply spsConfig if present
  if (radioResourceConfigDedicated->sps_Config) {
    if (UE_rrc_inst[Mod_id].sps_Config[eNB_index]) {
      memcpy(UE_rrc_inst[Mod_id].sps_Config[eNB_index],radioResourceConfigDedicated->sps_Config,
752
           sizeof(struct SPS_Config));
753 754 755 756 757 758 759 760
    }
    else {
      UE_rrc_inst[Mod_id].sps_Config[eNB_index] = radioResourceConfigDedicated->sps_Config;
    }
  }
#ifdef CBA
  if (radioResourceConfigDedicated->cba_RNTI_vlola) {
    cba_RNTI = (uint16_t) (((radioResourceConfigDedicated->cba_RNTI_vlola->buf[1]&0xff) << 8) | 
761
                  (radioResourceConfigDedicated->cba_RNTI_vlola->buf[0]&0xff));
762 763
    for (i=0 ; i< NUM_MAX_CBA_GROUP; i++) {
      if (UE_rrc_inst[Mod_id].cba_rnti[i] == cba_RNTI ) {
764 765
        cba_found=1;
        break;
766
      } else if (UE_rrc_inst[Mod_id].cba_rnti[i] == 0 )
767
      break;
768 769 770 771 772
    }
    if (cba_found==0) {
      UE_rrc_inst[Mod_id].num_active_cba_groups++;
      UE_rrc_inst[Mod_id].cba_rnti[i]=cba_RNTI;
      LOG_D(RRC, "[UE %d] Frame %d: radioResourceConfigDedicated reveived CBA_RNTI = %x for group %d from eNB %d \n", 
773
          Mod_id,frame, UE_rrc_inst[Mod_id].cba_rnti[i], i, eNB_index);
774 775 776 777 778 779
    }
  }
#endif 
  // Establish SRBs if present
  // loop through SRBToAddModList
  if (radioResourceConfigDedicated->srb_ToAddModList) {
780 781 782 783 784 785 786 787 788
    uint8_t *kRRCenc = NULL;
    uint8_t *kRRCint = NULL;

#if defined(ENABLE_SECURITY)
    derive_key_rrc_enc(UE_rrc_inst[Mod_id].ciphering_algorithm,
                       UE_rrc_inst[Mod_id].kenb, &kRRCenc);
    derive_key_rrc_int(UE_rrc_inst[Mod_id].integrity_algorithm,
                       UE_rrc_inst[Mod_id].kenb, &kRRCint);
#endif
789 790 791

// Refresh SRBs
    rrc_pdcp_config_asn1_req(NB_eNB_INST+Mod_id,frame,0,eNB_index,
792 793 794 795 796 797 798 799
                             radioResourceConfigDedicated->srb_ToAddModList,
                             (DRB_ToAddModList_t*)NULL,
                             (DRB_ToReleaseList_t*)NULL,
                             UE_rrc_inst[Mod_id].ciphering_algorithm |
                             (UE_rrc_inst[Mod_id].integrity_algorithm << 4),
                             kRRCenc,
                             kRRCint,
                             NULL
800
#ifdef Rel10
801
                           ,(PMCH_InfoList_r9_t *)NULL
802
#endif
803
                            );
804 805

    // Refresh SRBs
806
    rrc_rlc_config_asn1_req(NB_eNB_INST+Mod_id,frame,0,eNB_index,
807 808 809
                            radioResourceConfigDedicated->srb_ToAddModList,
                            (DRB_ToAddModList_t*)NULL,
                            (DRB_ToReleaseList_t*)NULL
810
#ifdef Rel10
811
                            ,(PMCH_InfoList_r9_t *)NULL
812
#endif
813
    );
814 815 816 817 818 819

    for (cnt=0;cnt<radioResourceConfigDedicated->srb_ToAddModList->list.count;cnt++) {

      SRB_id = radioResourceConfigDedicated->srb_ToAddModList->list.array[cnt]->srb_Identity;
      LOG_D(RRC,"[UE %d]: Frame %d SRB config cnt %d (SRB%ld)\n",Mod_id,frame,cnt,SRB_id);
      if (SRB_id == 1) {
820 821 822 823 824 825
        if (UE_rrc_inst[Mod_id].SRB1_config[eNB_index]) {
          memcpy(UE_rrc_inst[Mod_id].SRB1_config[eNB_index],radioResourceConfigDedicated->srb_ToAddModList->list.array[cnt],
                 sizeof(struct SRB_ToAddMod));
        }
        else {
          UE_rrc_inst[Mod_id].SRB1_config[eNB_index] = radioResourceConfigDedicated->srb_ToAddModList->list.array[cnt];
826

827 828 829 830 831 832 833 834 835 836 837 838
          rrc_ue_establish_srb1(Mod_id,frame,eNB_index,radioResourceConfigDedicated->srb_ToAddModList->list.array[cnt]);
          if (UE_rrc_inst[Mod_id].SRB1_config[eNB_index]->logicalChannelConfig) {
            if (UE_rrc_inst[Mod_id].SRB1_config[eNB_index]->logicalChannelConfig->present == SRB_ToAddMod__logicalChannelConfig_PR_explicitValue) {
              SRB1_logicalChannelConfig = &UE_rrc_inst[Mod_id].SRB1_config[eNB_index]->logicalChannelConfig->choice.explicitValue;
            }
            else {
              SRB1_logicalChannelConfig = &SRB1_logicalChannelConfig_defaultValue;
            }
          }
          else {
            SRB1_logicalChannelConfig = &SRB1_logicalChannelConfig_defaultValue;
          }
839 840 841

      LOG_D(RRC, "[MSC_MSG][FRAME %05d][RRC_UE][MOD %02d][][--- MAC_CONFIG_REQ  (SRB1 eNB %d) --->][MAC_UE][MOD %02d][]\n",
            frame, Mod_id, eNB_index, Mod_id);
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
      rrc_mac_config_req(Mod_id,0,0,eNB_index,
                         (RadioResourceConfigCommonSIB_t *)NULL,
                         UE_rrc_inst[Mod_id].physicalConfigDedicated[eNB_index],
                         (MeasObjectToAddMod_t **)NULL,
                         UE_rrc_inst[Mod_id].mac_MainConfig[eNB_index],
                         1,
                         SRB1_logicalChannelConfig,
                         (MeasGapConfig_t *)NULL,
                         NULL,
                         NULL,
                         NULL,
                         NULL,
                         NULL,
                         NULL,
                         NULL,
                         NULL
858
#ifdef Rel10	       
859 860 861 862
                         ,
                         0,
                         (MBSFN_AreaInfoList_r9_t *)NULL,
                         (PMCH_InfoList_r9_t *)NULL
863 864
#endif
#ifdef CBA
865 866 867
                         ,
                         0,
                         0
868
#endif
869 870
                         );
        }
871 872
      }
      else {
873 874 875 876 877
        if (UE_rrc_inst[Mod_id].SRB2_config[eNB_index]) {
          memcpy(UE_rrc_inst[Mod_id].SRB2_config[eNB_index],radioResourceConfigDedicated->srb_ToAddModList->list.array[cnt],
                 sizeof(struct SRB_ToAddMod));
        }
        else {
878

879
          UE_rrc_inst[Mod_id].SRB2_config[eNB_index] = radioResourceConfigDedicated->srb_ToAddModList->list.array[cnt];
880

881 882 883 884 885 886 887 888 889 890 891 892 893 894
          rrc_ue_establish_srb2(Mod_id,frame,eNB_index,radioResourceConfigDedicated->srb_ToAddModList->list.array[cnt]);
          if (UE_rrc_inst[Mod_id].SRB2_config[eNB_index]->logicalChannelConfig) {
            if (UE_rrc_inst[Mod_id].SRB2_config[eNB_index]->logicalChannelConfig->present == SRB_ToAddMod__logicalChannelConfig_PR_explicitValue){
              LOG_I(RRC,"Applying Explicit SRB2 logicalChannelConfig\n");
              SRB2_logicalChannelConfig = &UE_rrc_inst[Mod_id].SRB2_config[eNB_index]->logicalChannelConfig->choice.explicitValue;
            }
            else {
              LOG_I(RRC,"Applying default SRB2 logicalChannelConfig\n");
              SRB2_logicalChannelConfig = &SRB2_logicalChannelConfig_defaultValue;
            }
          }
          else {
            SRB2_logicalChannelConfig = &SRB2_logicalChannelConfig_defaultValue;
          }
895 896 897 898

      LOG_D(RRC, "[MSC_MSG][FRAME %05d][RRC_UE][MOD %02d][][--- MAC_CONFIG_REQ  (SRB2 eNB %d) --->][MAC_UE][MOD %02d][]\n",
            frame, Mod_id, eNB_index, Mod_id);
      rrc_mac_config_req(Mod_id,0,0,eNB_index,
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
                         (RadioResourceConfigCommonSIB_t *)NULL,
                         UE_rrc_inst[Mod_id].physicalConfigDedicated[eNB_index],
                         (MeasObjectToAddMod_t **)NULL,
                         UE_rrc_inst[Mod_id].mac_MainConfig[eNB_index],
                         2,
                         SRB2_logicalChannelConfig,
                         UE_rrc_inst[Mod_id].measGapConfig[eNB_index],
                         (TDD_Config_t *)NULL,
                         (MobilityControlInfo_t *)NULL,
                         NULL,
                         NULL,
                         NULL,
                         NULL,
                         NULL,
                         NULL
#ifdef Rel10
                         ,
                         0,
                         (MBSFN_AreaInfoList_r9_t *)NULL,
                         (PMCH_InfoList_r9_t *)NULL
919 920
#endif
#ifdef CBA
921 922 923
                         ,
                         0,
                         0
924
#endif
925 926
                        );
        }
927 928 929 930 931 932
      }
    }
  }

  // Establish DRBs if present
  if (radioResourceConfigDedicated->drb_ToAddModList) {
933
    uint8_t *kUPenc = NULL;
934 935 936 937 938

#if defined(ENABLE_SECURITY)
    derive_key_up_enc(UE_rrc_inst[Mod_id].integrity_algorithm,
                      UE_rrc_inst[Mod_id].kenb, &kUPenc);
#endif
939 940 941

    // Refresh DRBs
    rrc_pdcp_config_asn1_req(NB_eNB_INST+Mod_id,frame,0,eNB_index,
942 943 944 945 946 947 948 949
                             (SRB_ToAddModList_t*)NULL,
                             radioResourceConfigDedicated->drb_ToAddModList,
                             (DRB_ToReleaseList_t*)NULL,
                             UE_rrc_inst[Mod_id].ciphering_algorithm |
                             (UE_rrc_inst[Mod_id].integrity_algorithm << 4),
                             NULL,
                             NULL,
                             kUPenc
950
#ifdef Rel10
951
                             ,(PMCH_InfoList_r9_t *)NULL
952
#endif
953 954
                            );

955
  // Refresh DRBs
956
    rrc_rlc_config_asn1_req(NB_eNB_INST+Mod_id,frame,0,eNB_index,
957 958 959
                            (SRB_ToAddModList_t*)NULL,
                            radioResourceConfigDedicated->drb_ToAddModList,
                            (DRB_ToReleaseList_t*)NULL
960
#ifdef Rel10
961
                            ,(PMCH_InfoList_r9_t *)NULL
962
#endif
963
                            );
964 965 966
    for (i=0;i<radioResourceConfigDedicated->drb_ToAddModList->list.count;i++) {
      DRB_id   = radioResourceConfigDedicated->drb_ToAddModList->list.array[i]->drb_Identity-1;
      if (UE_rrc_inst[Mod_id].DRB_config[eNB_index][DRB_id]) {
967 968
        memcpy(UE_rrc_inst[Mod_id].DRB_config[eNB_index][DRB_id],radioResourceConfigDedicated->drb_ToAddModList->list.array[i],
               sizeof(struct DRB_ToAddMod));
969 970
      }
      else {
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
        UE_rrc_inst[Mod_id].DRB_config[eNB_index][DRB_id] = radioResourceConfigDedicated->drb_ToAddModList->list.array[i];

        rrc_ue_establish_drb(Mod_id,frame,eNB_index,radioResourceConfigDedicated->drb_ToAddModList->list.array[i]);
        // MAC/PHY Configuration
        LOG_D(RRC, "[MSC_MSG][FRAME %05d][RRC_UE][MOD %02d][][--- MAC_CONFIG_REQ (DRB %d eNB %d) --->][MAC_UE][MOD %02d][]\n",
              frame, Mod_id, radioResourceConfigDedicated->drb_ToAddModList->list.array[i]->drb_Identity, eNB_index, Mod_id);
        rrc_mac_config_req(Mod_id,0,0,eNB_index,
                           (RadioResourceConfigCommonSIB_t *)NULL,
                           UE_rrc_inst[Mod_id].physicalConfigDedicated[eNB_index],
                           (MeasObjectToAddMod_t **)NULL,
                           UE_rrc_inst[Mod_id].mac_MainConfig[eNB_index],
                           *UE_rrc_inst[Mod_id].DRB_config[eNB_index][DRB_id]->logicalChannelIdentity,
                           UE_rrc_inst[Mod_id].DRB_config[eNB_index][DRB_id]->logicalChannelConfig,
                           UE_rrc_inst[Mod_id].measGapConfig[eNB_index],
                           (TDD_Config_t*)NULL,
                           (MobilityControlInfo_t *)NULL,
                           NULL,
                           NULL,
                           NULL,
                           NULL,
                           NULL,
                           NULL
#ifdef Rel10
                           ,
                           0,
                           (MBSFN_AreaInfoList_r9_t *)NULL,
                           (PMCH_InfoList_r9_t *)NULL
998 999
#endif
#ifdef CBA
1000 1001 1002
                           ,
                           UE_rrc_inst[Mod_id].num_active_cba_groups, //
                           UE_rrc_inst[Mod_id].cba_rnti[0]
1003
#endif
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 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060

      }
    }
  }

  UE_rrc_inst[Mod_id].Info[eNB_index].State = RRC_CONNECTED;
  LOG_I(RRC,"[UE %d] State = RRC_CONNECTED (eNB %d)\n",Mod_id,eNB_index);


}

void rrc_ue_process_securityModeCommand(uint8_t Mod_id,uint32_t frame,SecurityModeCommand_t *securityModeCommand,uint8_t eNB_index) {

  asn_enc_rval_t enc_rval;

  UL_DCCH_Message_t ul_dcch_msg;
  // SecurityModeCommand_t SecurityModeCommand;
  uint8_t buffer[200];
  int i, securityMode;
  
  LOG_I(RRC,"[UE %d] Frame %d: Receiving from SRB1 (DL-DCCH), Processing securityModeCommand (eNB %d)\n",
	Mod_id,frame,eNB_index);

  switch (securityModeCommand->criticalExtensions.choice.c1.choice.securityModeCommand_r8.securityConfigSMC.securityAlgorithmConfig.cipheringAlgorithm){
  case SecurityAlgorithmConfig__cipheringAlgorithm_eea0:
    LOG_I(RRC,"[UE %d] Security algorithm is set to eea0\n",Mod_id);
    securityMode= SecurityAlgorithmConfig__cipheringAlgorithm_eea0;
    break;
  case SecurityAlgorithmConfig__cipheringAlgorithm_eea1:
    LOG_I(RRC,"[UE %d] Security algorithm is set to eea1\n",Mod_id);
    securityMode= SecurityAlgorithmConfig__cipheringAlgorithm_eea1;
    break;
  case SecurityAlgorithmConfig__cipheringAlgorithm_eea2:
    LOG_I(RRC,"[UE %d] Security algorithm is set to eea2\n",Mod_id);
    securityMode = SecurityAlgorithmConfig__cipheringAlgorithm_eea2;
    break;
  default:
    LOG_I(RRC,"[UE %d] Security algorithm is set to none\n",Mod_id);
    securityMode = SecurityAlgorithmConfig__cipheringAlgorithm_spare1;
    break;
  }
  switch (securityModeCommand->criticalExtensions.choice.c1.choice.securityModeCommand_r8.securityConfigSMC.securityAlgorithmConfig.integrityProtAlgorithm){
  case SecurityAlgorithmConfig__integrityProtAlgorithm_eia1:
    LOG_I(RRC,"[UE %d] Integrity protection algorithm is set to eia1\n",Mod_id);
    securityMode |= 1 << 5;
    break;
  case SecurityAlgorithmConfig__integrityProtAlgorithm_eia2:
    LOG_I(RRC,"[UE %d] Integrity protection algorithm is set to eia2\n",Mod_id);
    securityMode |= 1 << 6;
    break;
  default:
    LOG_I(RRC,"[UE %d] Integrity protection algorithm is set to none\n",Mod_id);
    securityMode |= 0x70 ;
    break;
  }
  LOG_D(RRC,"[UE %d] security mode is %x \n",Mod_id, securityMode);
1061 1062 1063 1064 1065

  /* Store the parameters received */
  UE_rrc_inst[Mod_id].ciphering_algorithm = securityModeCommand->criticalExtensions.choice.c1.choice.securityModeCommand_r8.securityConfigSMC.securityAlgorithmConfig.cipheringAlgorithm;
  UE_rrc_inst[Mod_id].integrity_algorithm = securityModeCommand->criticalExtensions.choice.c1.choice.securityModeCommand_r8.securityConfigSMC.securityAlgorithmConfig.integrityProtAlgorithm;

1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
  memset((void *)&ul_dcch_msg,0,sizeof(UL_DCCH_Message_t));
  //memset((void *)&SecurityModeCommand,0,sizeof(SecurityModeCommand_t));

  ul_dcch_msg.message.present           = UL_DCCH_MessageType_PR_c1;
  if (securityMode >= NO_SECURITY_MODE)
    ul_dcch_msg.message.choice.c1.present = UL_DCCH_MessageType__c1_PR_securityModeComplete;
  else 
    ul_dcch_msg.message.choice.c1.present = UL_DCCH_MessageType__c1_PR_securityModeFailure;
  
  if (securityModeCommand->criticalExtensions.present == SecurityModeCommand__criticalExtensions_PR_c1) {
    if (securityModeCommand->criticalExtensions.choice.c1.present == SecurityModeCommand__criticalExtensions__c1_PR_securityModeCommand_r8) {
    
      ul_dcch_msg.message.choice.c1.choice.securityModeComplete.rrc_TransactionIdentifier = securityModeCommand->rrc_TransactionIdentifier;
      ul_dcch_msg.message.choice.c1.choice.securityModeComplete.criticalExtensions.present = SecurityModeCommand__criticalExtensions_PR_c1;
      ul_dcch_msg.message.choice.c1.choice.securityModeComplete.criticalExtensions.choice.securityModeComplete_r8.nonCriticalExtension =NULL; 

      LOG_I(RRC,"[UE %d] Frame %d: Receiving from SRB1 (DL-DCCH), encoding securityModeComplete (eNB %d)\n",
1083
          Mod_id,frame,eNB_index);
1084 1085

      enc_rval = uper_encode_to_buffer(&asn_DEF_UL_DCCH_Message,
1086 1087 1088
          (void*)&ul_dcch_msg,
          buffer,
          100);
1089 1090 1091 1092 1093 1094

#ifdef XER_PRINT
      xer_fprint(stdout, &asn_DEF_UL_DCCH_Message, (void*)&ul_dcch_msg);
#endif	  

#ifdef USER_MODE
1095
      LOG_D(RRC, "securityModeComplete Encoded %d bits (%d bytes)\n", enc_rval.encoded, (enc_rval.encoded+7)/8);
1096
#endif
1097 1098 1099 1100
      for (i = 0; i < (enc_rval.encoded + 7) / 8; i++)
        LOG_T(RRC, "%02x.", buffer[i]);
      LOG_T(RRC, "\n");
      pdcp_rrc_data_req (Mod_id + NB_eNB_INST, frame, 0, DCCH, rrc_mui++, 0, (enc_rval.encoded + 7) / 8, buffer, 1);
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
    }
  }
  
}
void rrc_ue_process_ueCapabilityEnquiry(uint8_t Mod_id,uint32_t frame,UECapabilityEnquiry_t *UECapabilityEnquiry,uint8_t eNB_index) {

  asn_enc_rval_t enc_rval;

  UL_DCCH_Message_t ul_dcch_msg;


  UE_CapabilityRAT_Container_t ue_CapabilityRAT_Container;

  uint8_t buffer[200];
  int i;

  LOG_I(RRC,"[UE %d] Frame %d: Receiving from SRB1 (DL-DCCH), Processing UECapabilityEnquiry (eNB %d)\n",
1118
    Mod_id,frame,eNB_index);
1119 1120 1121 1122 1123 1124 1125


  memset((void *)&ul_dcch_msg,0,sizeof(UL_DCCH_Message_t));
  memset((void *)&ue_CapabilityRAT_Container,0,sizeof(UE_CapabilityRAT_Container_t));

  ul_dcch_msg.message.present           = UL_DCCH_MessageType_PR_c1;
  ul_dcch_msg.message.choice.c1.present = UL_DCCH_MessageType__c1_PR_ueCapabilityInformation;
1126
  ul_dcch_msg.message.choice.c1.choice.ueCapabilityInformation.rrc_TransactionIdentifier = UECapabilityEnquiry->rrc_TransactionIdentifier;
1127 1128 1129

  ue_CapabilityRAT_Container.rat_Type = RAT_Type_eutra;
  OCTET_STRING_fromBuf(&ue_CapabilityRAT_Container.ueCapabilityRAT_Container,
1130 1131
      (const char*)UE_rrc_inst[Mod_id].UECapability,
      UE_rrc_inst[Mod_id].UECapability_size);
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
  //  ue_CapabilityRAT_Container.ueCapabilityRAT_Container.buf  = UE_rrc_inst[Mod_id].UECapability;
  // ue_CapabilityRAT_Container.ueCapabilityRAT_Container.size = UE_rrc_inst[Mod_id].UECapability_size;
  

  if (UECapabilityEnquiry->criticalExtensions.present == UECapabilityEnquiry__criticalExtensions_PR_c1) {
    if (UECapabilityEnquiry->criticalExtensions.choice.c1.present == UECapabilityEnquiry__criticalExtensions__c1_PR_ueCapabilityEnquiry_r8) {
      ul_dcch_msg.message.choice.c1.choice.ueCapabilityInformation.criticalExtensions.present           = UECapabilityInformation__criticalExtensions_PR_c1;
      ul_dcch_msg.message.choice.c1.choice.ueCapabilityInformation.criticalExtensions.choice.c1.present = UECapabilityInformation__criticalExtensions__c1_PR_ueCapabilityInformation_r8;
      ul_dcch_msg.message.choice.c1.choice.ueCapabilityInformation.criticalExtensions.choice.c1.choice.ueCapabilityInformation_r8.ue_CapabilityRAT_ContainerList.list.count=0;

      for (i=0;i<UECapabilityEnquiry->criticalExtensions.choice.c1.choice.ueCapabilityEnquiry_r8.ue_CapabilityRequest.list.count;i++) {

1144 1145 1146 1147 1148
        if (*UECapabilityEnquiry->criticalExtensions.choice.c1.choice.ueCapabilityEnquiry_r8.ue_CapabilityRequest.list.array[i]
            == RAT_Type_eutra) {
          ASN_SEQUENCE_ADD(
              &ul_dcch_msg.message.choice.c1.choice.ueCapabilityInformation.criticalExtensions.choice.c1.choice.ueCapabilityInformation_r8.ue_CapabilityRAT_ContainerList.list,
              &ue_CapabilityRAT_Container);
1149

1150
          enc_rval = uper_encode_to_buffer(&asn_DEF_UL_DCCH_Message, (void*) &ul_dcch_msg, buffer, 100);
1151 1152

#ifdef XER_PRINT
1153
          xer_fprint(stdout, &asn_DEF_UL_DCCH_Message, (void*)&ul_dcch_msg);
1154
#endif
1155 1156

#ifdef USER_MODE
1157
          LOG_D(RRC,"UECapabilityInformation Encoded %d bits (%d bytes)\n",enc_rval.encoded,(enc_rval.encoded+7)/8);
1158
#endif
1159 1160 1161 1162 1163
          for (i = 0; i < (enc_rval.encoded + 7) / 8; i++)
            LOG_T(RRC, "%02x.", buffer[i]);
          LOG_T(RRC, "\n");
          pdcp_rrc_data_req (Mod_id + NB_eNB_INST, frame, 0, DCCH, rrc_mui++, 0, (enc_rval.encoded + 7) / 8, buffer, 1);
        }
1164 1165 1166 1167 1168
      }
    }
  }
}

1169

1170
void rrc_ue_process_rrcConnectionReconfiguration(u8 Mod_id, u32 frame,
1171 1172
                                                 RRCConnectionReconfiguration_t *rrcConnectionReconfiguration,
                                                 u8 eNB_index) {
1173 1174

  LOG_I(RRC,"[UE %d] Frame %d: Receiving from SRB1 (DL-DCCH), Processing RRCConnectionReconfiguration (eNB %d)\n",
1175
      Mod_id,frame,eNB_index);
1176
  if (rrcConnectionReconfiguration->criticalExtensions.present == RRCConnectionReconfiguration__criticalExtensions_PR_c1) {
1177 1178 1179
    if (rrcConnectionReconfiguration->criticalExtensions.choice.c1.present ==
        RRCConnectionReconfiguration__criticalExtensions__c1_PR_rrcConnectionReconfiguration_r8) {
      RRCConnectionReconfiguration_r8_IEs_t *rrcConnectionReconfiguration_r8 = &rrcConnectionReconfiguration->criticalExtensions.choice.c1.choice.rrcConnectionReconfiguration_r8;
1180

1181 1182 1183
      if (rrcConnectionReconfiguration_r8->mobilityControlInfo) {
        LOG_I(RRC,"Mobility Control Information is present\n");
        rrc_ue_process_mobilityControlInfo(Mod_id,frame, eNB_index, rrcConnectionReconfiguration_r8->mobilityControlInfo);
1184 1185

      }
1186 1187 1188 1189
      if (rrcConnectionReconfiguration_r8->measConfig != NULL) {
        LOG_I(RRC,"Measurement Configuration is present\n");
        rrc_ue_process_measConfig(Mod_id,frame, eNB_index,
            rrcConnectionReconfiguration_r8->measConfig);
1190
      }
1191 1192 1193
      if (rrcConnectionReconfiguration_r8->radioResourceConfigDedicated) {
        LOG_I(RRC,"Radio Resource Configuration is present\n");
        rrc_ue_process_radioResourceConfigDedicated(Mod_id,frame,eNB_index, rrcConnectionReconfiguration_r8->radioResourceConfigDedicated);
1194
      }
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218

#if defined(ENABLE_ITTI)
      /* Check if there is dedicated NAS information to forward to NAS */
      if (rrcConnectionReconfiguration_r8->dedicatedInfoNASList != NULL) {
        int list_count;
        uint32_t pdu_length;
        uint8_t *pdu_buffer;
        MessageDef *msg_p;

        for (list_count = 0; list_count < rrcConnectionReconfiguration_r8->dedicatedInfoNASList->list.count; list_count++) {
          pdu_length = rrcConnectionReconfiguration_r8->dedicatedInfoNASList->list.array[list_count]->size;
          pdu_buffer = rrcConnectionReconfiguration_r8->dedicatedInfoNASList->list.array[list_count]->buf;

          msg_p = itti_alloc_new_message(TASK_RRC_UE, NAS_DOWNLINK_DATA_IND);
          NAS_DOWNLINK_DATA_IND(msg_p).UEid = Mod_id; // TODO set the UEid to something else ?
          NAS_DOWNLINK_DATA_IND(msg_p).nasMsg.length = pdu_length;
          NAS_DOWNLINK_DATA_IND(msg_p).nasMsg.data = pdu_buffer;

          itti_send_msg_to_task(TASK_NAS_UE, Mod_id + NB_eNB_INST, msg_p);
        }

        free (rrcConnectionReconfiguration_r8->dedicatedInfoNASList);
      }
#endif
1219 1220 1221 1222
    } // c1 present
  } // critical extensions present
}

1223
/* 36.331, 5.3.5.4      Reception of an RRCConnectionReconfiguration including the mobilityControlInfo by the UE (handover) */
1224
void   rrc_ue_process_mobilityControlInfo(u8 Mod_id,u32 frame, u8 eNB_index,struct MobilityControlInfo *mobilityControlInfo) {
1225
  /*
1226 1227
  DRB_ToReleaseList_t*  drb2release_list;
  DRB_Identity_t *lcid;
1228
  */
1229 1230 1231 1232 1233 1234
  LOG_N(RRC,"Note: This function needs some updates\n");
  if(UE_rrc_inst[Mod_id].Info[eNB_index].T310_active == 1)
    UE_rrc_inst[Mod_id].Info[eNB_index].T310_active = 0;
  UE_rrc_inst[Mod_id].Info[eNB_index].T304_active = 1;
  UE_rrc_inst[Mod_id].Info[eNB_index].T304_cnt = T304[mobilityControlInfo->t304];

1235
  /*
1236 1237 1238
  drb2release_list = CALLOC (1, sizeof (*drb2release_list));
  lcid= CALLOC (1, sizeof (DRB_Identity_t)); // long
  for (*lcid=0;*lcid<NB_RB_MAX;*lcid++)
1239
  {
1240
    ASN_SEQUENCE_ADD (&(drb2release_list)->list,lcid);
1241 1242
  }
  */
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297

  //Removing SRB1 and SRB2 and DRB0
  LOG_N(RRC,"[UE %d] : Update needed for rrc_pdcp_config_req (deprecated) and rrc_rlc_config_req commands(deprecated)\n",Mod_id);
  rrc_pdcp_config_req (Mod_id+NB_eNB_INST, frame, 0, ACTION_REMOVE, Mod_id+DCCH,UNDEF_SECURITY_MODE);
  rrc_rlc_config_req(Mod_id+NB_eNB_INST,frame,0,ACTION_REMOVE,Mod_id+DCCH,SIGNALLING_RADIO_BEARER,Rlc_info_am_config);

  rrc_pdcp_config_req (Mod_id+NB_eNB_INST, frame, 0, ACTION_REMOVE, Mod_id+DCCH1,UNDEF_SECURITY_MODE);
  rrc_rlc_config_req(Mod_id+NB_eNB_INST,frame,0,ACTION_REMOVE,Mod_id+DCCH1,SIGNALLING_RADIO_BEARER,Rlc_info_am_config);

  rrc_pdcp_config_req (Mod_id+NB_eNB_INST, frame, 0, ACTION_REMOVE, Mod_id+DTCH,UNDEF_SECURITY_MODE);
  rrc_rlc_config_req(Mod_id+NB_eNB_INST,frame,0,ACTION_REMOVE,Mod_id+DTCH,RADIO_ACCESS_BEARER,Rlc_info_um);
  /*
  rrc_pdcp_config_asn1_req(NB_eNB_INST+Mod_id,frame, 0,eNB_index,
			   NULL, // SRB_ToAddModList
			   NULL, // DRB_ToAddModList
			   drb2release_list,
			   0, // security mode
			   NULL, // key rrc encryption
			   NULL, // key rrc integrity
			   NULL // key encryption
#ifdef Rel10
			   ,NULL
#endif
			   );
    
  rrc_rlc_config_asn1_req(NB_eNB_INST+Mod_id, frame,0,eNB_index,
			  NULL,// SRB_ToAddModList
			  NULL,// DRB_ToAddModList
			  drb2release_list // DRB_ToReleaseList
#ifdef Rel10
			  ,NULL
#endif 
			  );
   */
  

  //A little cleanup at RRC...
  //Copying current queue config to free RRC index
  /*
    memcpy((void *)UE_rrc_inst[Mod_id].SRB1_config[~(7<<eNB_index)],(void *)UE_rrc_inst[Mod_id].SRB1_config[7<<eNB_index],sizeof(SRB_ToAddMod_t));
    memcpy((void *)UE_rrc_inst[Mod_id].SRB2_config[~(7<<eNB_index)],(void *)UE_rrc_inst[Mod_id].SRB2_config[7<<eNB_index],sizeof(SRB_ToAddMod_t));
    memcpy((void *)UE_rrc_inst[Mod_id].DRB_config[~(7<<eNB_index)][0],(void *)UE_rrc_inst[Mod_id].DRB_config[7<<eNB_index][0],sizeof(DRB_ToAddMod_t));
  */
  /*
  LOG_N(RRC,"Not sure if Freeing the current queue config works properly: Fix me\n");
  free((void *)&UE_rrc_inst[Mod_id].SRB1_config[eNB_index]);
  free((void *)&UE_rrc_inst[Mod_id].SRB2_config[eNB_index]);
  free((void *)&UE_rrc_inst[Mod_id].DRB_config[eNB_index][0]);
 
  UE_rrc_inst[Mod_id].SRB1_config[eNB_index] = NULL;
  UE_rrc_inst[Mod_id].SRB2_config[eNB_index] = NULL;
  UE_rrc_inst[Mod_id].DRB_config[eNB_index][0] = NULL;
  */   
  //Synchronisation to DL of target cell
  LOG_D(RRC,"HO: Reset PDCP and RLC for configured RBs.. \n[MSC_MSG][FRAME %05d][RRC_UE][MOD %02d][][--- MAC_CONFIG_REQ  (SRB2 eNB %d) --->][MAC_UE][MOD %02d][]\n",
1298
      frame, Mod_id, eNB_index, Mod_id);
1299 1300 1301
  
    // Reset MAC and configure PHY
  rrc_mac_config_req(Mod_id,0,0,eNB_index,
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
      (RadioResourceConfigCommonSIB_t *)NULL,
      (struct PhysicalConfigDedicated *)NULL,
      (MeasObjectToAddMod_t **)NULL,
      (MAC_MainConfig_t *)NULL,
      0,
      (struct LogicalChannelConfig *)NULL,
      (MeasGapConfig_t *)NULL,
      (TDD_Config_t *)NULL,
      mobilityControlInfo,
      (u8 *)NULL,
      (u16 *)NULL,
      NULL,
      NULL,
      NULL,
      NULL
#ifdef Rel10
      ,0,
      (MBSFN_AreaInfoList_r9_t *)NULL,
      (PMCH_InfoList_r9_t *)NULL
1321 1322
#endif
#ifdef CBA
1323 1324
      ,0,
      0
1325 1326 1327 1328
#endif
		     );
  
  // Re-establish PDCP for all RBs that are established
1329 1330 1331
  // rrc_pdcp_config_req (Mod_id+NB_eNB_INST, frame, 0, ACTION_ADD, Mod_id+DCCH);
  // rrc_pdcp_config_req (Mod_id+NB_eNB_INST, frame, 0, ACTION_ADD, Mod_id+DCCH1);
  // rrc_pdcp_config_req (Mod_id+NB_eNB_INST, frame, 0, ACTION_ADD, Mod_id+DTCH);
1332 1333 1334
  
  
  // Re-establish RLC for all RBs that are established
1335 1336 1337
  // rrc_rlc_config_req(Mod_id+NB_eNB_INST,frame,0,ACTION_ADD,Mod_id+DCCH,SIGNALLING_RADIO_BEARER,Rlc_info_am_config);
  // rrc_rlc_config_req(Mod_id+NB_eNB_INST,frame,0,ACTION_ADD,Mod_id+DCCH1,SIGNALLING_RADIO_BEARER,Rlc_info_am_config);
  // rrc_rlc_config_req(Mod_id+NB_eNB_INST,frame,0,ACTION_ADD,Mod_id+DTCH,RADIO_ACCESS_BEARER,Rlc_info_um);
1338 1339 1340 1341 1342
  
  UE_rrc_inst[Mod_id].Info[eNB_index].State = RRC_SI_RECEIVED;

}
void rrc_detach_from_eNB(u8 Mod_id,u8 eNB_index) {
1343
     //UE_rrc_inst[Mod_id].DRB_config[eNB_index]
1344 1345
}

1346 1347 1348 1349 1350 1351 1352
/*------------------------------------------------------------------------------------------*/
void  rrc_ue_decode_dcch(u8 Mod_id,u32 frame,u8 Srb_id, u8 *Buffer,u8 eNB_index){
  /*------------------------------------------------------------------------------------------*/

  //DL_DCCH_Message_t dldcchmsg;
  DL_DCCH_Message_t *dl_dcch_msg=NULL;//&dldcchmsg;
  //  asn_dec_rval_t dec_rval;
1353
  // int i;
1354
  u8 target_eNB_index=0xFF;
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
  if (Srb_id != 1) {
    LOG_E(RRC,"[UE %d] Frame %d: Received message on DL-DCCH (SRB1), should not have ...\n",Mod_id,frame);
    return;
  }

  //memset(dl_dcch_msg,0,sizeof(DL_DCCH_Message_t));

  // decode messages
  //  LOG_D(RRC,"[UE %d] Decoding DL-DCCH message\n",Mod_id);
  /*
  for (i=0;i<30;i++)
    LOG_T(RRC,"%x.",Buffer[i]);
  LOG_T(RRC, "\n");
  */
  uper_decode(NULL,
1370 1371 1372 1373
      &asn_DEF_DL_DCCH_Message,
      (void**)&dl_dcch_msg,
      (uint8_t*)Buffer,
      RRC_BUF_SIZE,0,0);
1374 1375 1376 1377 1378

#ifdef XER_PRINT
  xer_fprint(stdout,&asn_DEF_DL_DCCH_Message,(void*)dl_dcch_msg);
#endif

1379
#if defined(ENABLE_ITTI)
1380
# if defined(DISABLE_ITTI_XER_PRINT)
1381 1382 1383 1384
  {
    MessageDef *message_p;

    message_p = itti_alloc_new_message (TASK_RRC_UE, RRC_DL_DCCH_MESSAGE);
1385
    memcpy (&message_p->ittiMsg, (void *) dl_dcch_msg, sizeof(RrcDlDcchMessage));
1386

1387
    itti_send_msg_to_task (TASK_UNKNOWN, Mod_id + NB_eNB_INST, message_p);
1388
  }
1389 1390
# else
  {
1391 1392
    char        message_string[20000];
    size_t      message_string_size;
1393

1394
    if ((message_string_size = xer_sprint(message_string, sizeof(message_string), &asn_DEF_DL_DCCH_Message, (void *)dl_dcch_msg)) > 0)
1395 1396 1397 1398 1399 1400
    {
      MessageDef *message_p;

      message_p = itti_alloc_new_message_sized (TASK_RRC_UE, GENERIC_LOG, message_string_size);
      memcpy(&message_p->ittiMsg.generic_log, message_string, message_string_size);

1401
      itti_send_msg_to_task(TASK_UNKNOWN, Mod_id + NB_eNB_INST, message_p);
1402 1403 1404
    }
  }
# endif
1405 1406
#endif

1407 1408
  if (dl_dcch_msg->message.present == DL_DCCH_MessageType_PR_c1) {

1409
    if (UE_rrc_inst[Mod_id].Info[eNB_index].State >= RRC_CONNECTED) {
1410 1411 1412

      switch (dl_dcch_msg->message.choice.c1.present) {

1413 1414 1415
        case DL_DCCH_MessageType__c1_PR_NOTHING:
          LOG_I(RRC, "[UE %d] Frame %d : Received PR_NOTHING on DL-DCCH-Message\n", Mod_id, frame);
          return;
winckel's avatar
RRC:  
winckel committed
1416

1417 1418 1419 1420
        case DL_DCCH_MessageType__c1_PR_csfbParametersResponseCDMA2000:
          break;

        case DL_DCCH_MessageType__c1_PR_dlInformationTransfer: {
winckel's avatar
RRC:  
winckel committed
1421
#if defined(ENABLE_ITTI)
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
          DLInformationTransfer_t *dlInformationTransfer = &dl_dcch_msg->message.choice.c1.choice.dlInformationTransfer;

          if ((dlInformationTransfer->criticalExtensions.present == DLInformationTransfer__criticalExtensions_PR_c1)
              && (dlInformationTransfer->criticalExtensions.choice.c1.present
                  == DLInformationTransfer__criticalExtensions__c1_PR_dlInformationTransfer_r8)
              && (dlInformationTransfer->criticalExtensions.choice.c1.choice.dlInformationTransfer_r8.dedicatedInfoType.present
                  == DLInformationTransfer_r8_IEs__dedicatedInfoType_PR_dedicatedInfoNAS)) {
            /* This message hold a dedicated info NAS payload, forward it to NAS */
            struct DLInformationTransfer_r8_IEs__dedicatedInfoType *dedicatedInfoType =
                &dlInformationTransfer->criticalExtensions.choice.c1.choice.dlInformationTransfer_r8.dedicatedInfoType;
            uint32_t pdu_length;
            uint8_t *pdu_buffer;
            MessageDef *msg_p;

            pdu_length = dedicatedInfoType->choice.dedicatedInfoNAS.size;
            pdu_buffer = dedicatedInfoType->choice.dedicatedInfoNAS.buf;

            msg_p = itti_alloc_new_message(TASK_RRC_UE, NAS_DOWNLINK_DATA_IND);
            NAS_DOWNLINK_DATA_IND(msg_p).UEid = Mod_id; // TODO set the UEid to something else ?
            NAS_DOWNLINK_DATA_IND(msg_p).nasMsg.length = pdu_length;
            NAS_DOWNLINK_DATA_IND(msg_p).nasMsg.data = pdu_buffer;

            itti_send_msg_to_task(TASK_NAS_UE, Mod_id + NB_eNB_INST, msg_p);
          }
winckel's avatar
RRC:  
winckel committed
1446
#endif
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
          break;
        }

        case DL_DCCH_MessageType__c1_PR_handoverFromEUTRAPreparationRequest:
          break;

        case DL_DCCH_MessageType__c1_PR_mobilityFromEUTRACommand:
          break;

        case DL_DCCH_MessageType__c1_PR_rrcConnectionReconfiguration:
          // first check if mobilityControlInfo  is present
          if (dl_dcch_msg->message.choice.c1.choice.rrcConnectionReconfiguration.criticalExtensions.choice.c1.choice.rrcConnectionReconfiguration_r8.mobilityControlInfo
              != NULL) {
            /* 36.331, 5.3.5.4 Reception of an RRCConnectionReconfiguration including the mobilityControlInfo by the UE (handover)*/
            if (UE_rrc_inst[Mod_id].HandoverInfoUe.targetCellId
                != dl_dcch_msg->message.choice.c1.choice.rrcConnectionReconfiguration.criticalExtensions.choice.c1.choice.rrcConnectionReconfiguration_r8.mobilityControlInfo->targetPhysCellId) {
              LOG_W(RRC,
                  "[UE %d] Frame %d: Handover target (%d) is different from RSRP measured target (%d)..\n", Mod_id, frame, dl_dcch_msg->message.choice.c1.choice.rrcConnectionReconfiguration.criticalExtensions.choice.c1.choice.rrcConnectionReconfiguration_r8.mobilityControlInfo->targetPhysCellId, UE_rrc_inst[Mod_id].HandoverInfoUe.targetCellId);
              return;
            }
            else
              if ((target_eNB_index = get_adjacent_cell_mod_id(UE_rrc_inst[Mod_id].HandoverInfoUe.targetCellId))
                  == 0xFF) {
                LOG_W(RRC,
                    "[UE %d] Frame %d: Mod_id of the target eNB not found, check the network topology\n", Mod_id, frame);
                return;
              }
              else {
                LOG_I(RRC,
                    "[UE% d] Frame %d: Received rrcConnectionReconfiguration with mobilityControlInfo \n", Mod_id, frame);
                UE_rrc_inst[Mod_id].HandoverInfoUe.measFlag = 1; // Ready to send more MeasReports if required
              }
          }
          rrc_ue_process_rrcConnectionReconfiguration(Mod_id, frame,
              &dl_dcch_msg->message.choice.c1.choice.rrcConnectionReconfiguration, eNB_index);
          if (target_eNB_index != 0xFF) {
            rrc_ue_generate_RRCConnectionReconfigurationComplete(Mod_id, frame, target_eNB_index,
                dl_dcch_msg->message.choice.c1.choice.rrcConnectionReconfiguration.rrc_TransactionIdentifier);
            UE_rrc_inst[Mod_id].Info[eNB_index].State = RRC_HO_EXECUTION;
            UE_rrc_inst[Mod_id].Info[target_eNB_index].State = RRC_RECONFIGURED;
            LOG_I(RRC, "[UE %d] State = RRC_RECONFIGURED during HO (eNB %d)\n", Mod_id, target_eNB_index);
          }
          else {
            rrc_ue_generate_RRCConnectionReconfigurationComplete(Mod_id, frame, eNB_index,
                dl_dcch_msg->message.choice.c1.choice.rrcConnectionReconfiguration.rrc_TransactionIdentifier);
            UE_rrc_inst[Mod_id].Info[eNB_index].State = RRC_RECONFIGURED;
            LOG_I(RRC, "[UE %d] State = RRC_RECONFIGURED (eNB %d)\n", Mod_id, eNB_index);
          }
          break;

        case DL_DCCH_MessageType__c1_PR_rrcConnectionRelease:
          break;

        case DL_DCCH_MessageType__c1_PR_securityModeCommand:
          LOG_I(RRC, "[UE %d] Received securityModeCommand (eNB %d)\n", Mod_id, eNB_index);
          rrc_ue_process_securityModeCommand(Mod_id, frame, &dl_dcch_msg->message.choice.c1.choice.securityModeCommand,
              eNB_index);
          break;

        case DL_DCCH_MessageType__c1_PR_ueCapabilityEnquiry:
          LOG_I(RRC, "[UE %d] Received Capability Enquiry (eNB %d)\n", Mod_id, eNB_index);
          rrc_ue_process_ueCapabilityEnquiry(Mod_id, frame, &dl_dcch_msg->message.choice.c1.choice.ueCapabilityEnquiry,
              eNB_index);
          break;

        case DL_DCCH_MessageType__c1_PR_counterCheck:
          break;
winckel's avatar
RRC:  
winckel committed
1514

1515
#ifdef Rel10
1516 1517 1518 1519 1520 1521
          case DL_DCCH_MessageType__c1_PR_ueInformationRequest_r9:
          break;
          case DL_DCCH_MessageType__c1_PR_loggedMeasurementConfiguration_r10:
          break;
          case DL_DCCH_MessageType__c1_PR_rnReconfiguration_r10:
          break;
1522
#endif
1523 1524 1525 1526 1527 1528 1529 1530 1531

        case DL_DCCH_MessageType__c1_PR_spare1:
        case DL_DCCH_MessageType__c1_PR_spare2:
        case DL_DCCH_MessageType__c1_PR_spare3:
        case DL_DCCH_MessageType__c1_PR_spare4:
          break;

        default:
          break;
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
      }
    }
  }
#ifndef NO_RRM
    send_msg(&S_rrc,msg_rrc_end_scan_req(Mod_id,eNB_index));
#endif
}

const char siWindowLength[7][5] = {"1ms\0","2ms\0","5ms\0","10ms\0","15ms\0","20ms\0","40ms\0"};
const char siWindowLength_int[7] = {1,2,5,10,15,20,40};

const char SIBType[16][6] ={"SIB3\0","SIB4\0","SIB5\0","SIB6\0","SIB7\0","SIB8\0","SIB9\0","SIB10\0","SIB11\0","SIB12\0","SIB13\0","Sp2\0","Sp3\0","Sp4\0"};
const char SIBPeriod[7][7]= {"80ms\0","160ms\0","320ms\0","640ms\0","1280ms\0","2560ms\0","5120ms\0"};
int siPeriod_int[7] = {80,160,320,640,1280,2560,5120};

int decode_BCCH_DLSCH_Message(u8 Mod_id,u32 frame,u8 eNB_index,u8 *Sdu,u8 Sdu_len) {

  //BCCH_DL_SCH_Message_t bcch_message;
  BCCH_DL_SCH_Message_t *bcch_message=NULL;//_ptr=&bcch_message;
  SystemInformationBlockType1_t **sib1=&UE_rrc_inst[Mod_id].sib1[eNB_index];
  SystemInformation_t **si=UE_rrc_inst[Mod_id].si[eNB_index];
  asn_dec_rval_t dec_rval;
  uint32_t si_window;//, sib1_decoded=0, si_decoded=0;
1555
  // int i;
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574

  vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_DECODE_BCCH, VCD_FUNCTION_IN);


  if ((UE_rrc_inst[Mod_id].Info[eNB_index].SIB1Status == 1) &&
      (UE_rrc_inst[Mod_id].Info[eNB_index].SIStatus == 1)) {
    // Avoid decoding to prevent memory bloating
    vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_DECODE_BCCH, VCD_FUNCTION_OUT);
    return 0;
  } 
  else {

    //memset(&bcch_message,0,sizeof(BCCH_DL_SCH_Message_t));
    //  LOG_D(RRC,"[UE %d] Decoding DL_BCCH_DLSCH_Message\n",Mod_id)
    /*
    for (i=0;i<Sdu_len;i++)
      printf("%x.",Sdu[i]);
      printf("\n");*/
    dec_rval = uper_decode_complete(NULL,
1575 1576 1577 1578
        &