L2_interface.c 28.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.0  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

22
/*! \file l2_interface.c
gauthier's avatar
 
gauthier committed
23 24 25 26 27 28 29
 * \brief layer 2 interface, used to support different RRC sublayer
 * \author Raymond Knopp and Navid Nikaein
 * \date 2010-2014
 * \version 1.0
 * \company Eurecom
 * \email: raymond.knopp@eurecom.fr
 */
nikaeinn's avatar
nikaeinn committed
30

gauthier's avatar
gauthier committed
31
#include "platform_types.h"
32 33 34 35 36 37 38 39
//#include "openair_defs.h"
//#include "openair_proto.h"
#include "defs.h"
#include "extern.h"
//#include "mac_lchan_interface.h"
//#include "openair_rrc_utils.h"
//#include "openair_rrc_main.h"
#include "UTIL/LOG/log.h"
40
#include "rrc_eNB_UE_context.h"
41
#include "pdcp.h"
42
#include "msc.h"
43
#include "common/ran_context.h"
44 45 46 47 48 49 50 51

#ifdef PHY_EMUL
#include "SIMULATION/simulation_defs.h"
extern EMULATION_VARS *Emul_vars;
extern eNB_MAC_INST *eNB_mac_inst;
extern UE_MAC_INST *UE_mac_inst;
#endif

52 53 54 55
#if defined(ENABLE_ITTI)
# include "intertask_interface.h"
#endif

56
//#define RRC_DATA_REQ_DEBUG
gauthier's avatar
define  
gauthier committed
57
#define DEBUG_RRC 1
58

gauthier's avatar
gauthier committed
59
mui_t mui=0;
60

61 62
extern RAN_CONTEXT_t RC;

63 64
//------------------------------------------------------------------------------
int8_t
65
mac_rrc_data_req(
66
  const module_id_t Mod_idP,
67
  const int         CC_id,
68 69 70 71 72 73 74 75 76
  const frame_t     frameP,
  const rb_id_t     Srb_id,
  const uint8_t     Nb_tb,
  uint8_t*    const buffer_pP,
  const eNB_flag_t  enb_flagP,
  const uint8_t     eNB_index,
  const uint8_t     mbsfn_sync_area
)
//--------------------------------------------------------------------------
77
{
78
  asn_enc_rval_t enc_rval;
79
  SRB_INFO *Srb_info;
80 81
  uint8_t Sdu_size                = 0;
  uint8_t sfn                     = (uint8_t)((frameP>>2)&0xff);
82 83 84


  
85
#ifdef DEBUG_RRC
86
  int i;
87
  LOG_T(RRC,"[eNB %d] mac_rrc_data_req to SRB ID=%d\n",Mod_idP,Srb_id);
88 89
#endif

90 91 92 93
  eNB_RRC_INST *rrc;
  rrc_eNB_carrier_data_t *carrier;
  BCCH_BCH_Message_t *mib;

94
  if( enb_flagP == ENB_FLAG_YES) {
95

96 97 98 99
    rrc     = RC.rrc[Mod_idP];
    carrier = &rrc->carrier[0];
    mib     = &carrier->mib;

100
    if((Srb_id & RAB_OFFSET) == BCCH) {
101
      if(RC.rrc[Mod_idP]->carrier[CC_id].SI.Active==0) {
102 103
        return 0;
      }
104

105
      // All even frames transmit SIB in SF 5
106 107
      AssertFatal(RC.rrc[Mod_idP]->carrier[CC_id].sizeof_SIB1 != 255, 
		  "[eNB %d] MAC Request for SIB1 and SIB1 not initialized\n",Mod_idP);
108 109

      if ((frameP%2) == 0) {
110
        memcpy(&buffer_pP[0],
111 112
               RC.rrc[Mod_idP]->carrier[CC_id].SIB1,
               RC.rrc[Mod_idP]->carrier[CC_id].sizeof_SIB1);
113 114

#if defined(ENABLE_ITTI)
115 116
        {
          MessageDef *message_p;
117
          int sib1_size = RC.rrc[Mod_idP]->carrier[CC_id].sizeof_SIB1;
118 119 120 121 122 123 124 125 126 127
          int sdu_size = sizeof(RRC_MAC_BCCH_DATA_REQ (message_p).sdu);

          if (sib1_size > sdu_size) {
            LOG_E(RRC, "SIB1 SDU larger than BCCH SDU buffer size (%d, %d)", sib1_size, sdu_size);
            sib1_size = sdu_size;
          }

          message_p = itti_alloc_new_message (TASK_RRC_ENB, RRC_MAC_BCCH_DATA_REQ);
          RRC_MAC_BCCH_DATA_REQ (message_p).frame    = frameP;
          RRC_MAC_BCCH_DATA_REQ (message_p).sdu_size = sib1_size;
Cedric Roux's avatar
Cedric Roux committed
128
          memset (RRC_MAC_BCCH_DATA_REQ (message_p).sdu, 0, BCCH_SDU_SIZE);
129
          memcpy (RRC_MAC_BCCH_DATA_REQ (message_p).sdu,
130
                  RC.rrc[Mod_idP]->carrier[CC_id].SIB1,
131
                  sib1_size);
132 133
          RRC_MAC_BCCH_DATA_REQ (message_p).enb_index = eNB_index;

134
          itti_send_msg_to_task (TASK_MAC_ENB, ENB_MODULE_ID_TO_INSTANCE(Mod_idP), message_p);
135
        }
136 137
#endif

138
#ifdef DEBUG_RRC
139
        LOG_T(RRC,"[eNB %d] Frame %d : BCCH request => SIB 1\n",Mod_idP,frameP);
140

141
        for (i=0; i<RC.rrc[Mod_idP]->carrier[CC_id].sizeof_SIB1; i++) {
142
          LOG_T(RRC,"%x.",buffer_pP[i]);
143
        }
144 145

        LOG_T(RRC,"\n");
146 147
#endif

148
        return (RC.rrc[Mod_idP]->carrier[CC_id].sizeof_SIB1);
149 150
      } // All RFN mod 8 transmit SIB2-3 in SF 5
      else if ((frameP%8) == 1) {
151
        memcpy(&buffer_pP[0],
152 153
               RC.rrc[Mod_idP]->carrier[CC_id].SIB23,
               RC.rrc[Mod_idP]->carrier[CC_id].sizeof_SIB23);
154 155

#if defined(ENABLE_ITTI)
156 157
        {
          MessageDef *message_p;
158
          int sib23_size = RC.rrc[Mod_idP]->carrier[CC_id].sizeof_SIB23;
159 160 161 162 163 164 165 166 167 168
          int sdu_size = sizeof(RRC_MAC_BCCH_DATA_REQ (message_p).sdu);

          if (sib23_size > sdu_size) {
            LOG_E(RRC, "SIB23 SDU larger than BCCH SDU buffer size (%d, %d)", sib23_size, sdu_size);
            sib23_size = sdu_size;
          }

          message_p = itti_alloc_new_message (TASK_RRC_ENB, RRC_MAC_BCCH_DATA_REQ);
          RRC_MAC_BCCH_DATA_REQ (message_p).frame = frameP;
          RRC_MAC_BCCH_DATA_REQ (message_p).sdu_size = sib23_size;
Cedric Roux's avatar
Cedric Roux committed
169
          memset (RRC_MAC_BCCH_DATA_REQ (message_p).sdu, 0, BCCH_SDU_SIZE);
170
          memcpy (RRC_MAC_BCCH_DATA_REQ (message_p).sdu,
171
                  RC.rrc[Mod_idP]->carrier[CC_id].SIB23,
172
                  sib23_size);
173 174
          RRC_MAC_BCCH_DATA_REQ (message_p).enb_index = eNB_index;

175
          itti_send_msg_to_task (TASK_MAC_ENB, ENB_MODULE_ID_TO_INSTANCE(Mod_idP), message_p);
176
        }
177 178
#endif

179
#ifdef DEBUG_RRC
180
        LOG_T(RRC,"[eNB %d] Frame %d BCCH request => SIB 2-3\n",Mod_idP,frameP);
181

182
        for (i=0; i<RC.rrc[Mod_idP]->carrier[CC_id].sizeof_SIB23; i++) {
183
          LOG_T(RRC,"%x.",buffer_pP[i]);
184
        }
185 186

        LOG_T(RRC,"\n");
187
#endif
188
        return(RC.rrc[Mod_idP]->carrier[CC_id].sizeof_SIB23);
189
      } else {
190
        return(0);
191
      }
192
    }
193 194 195 196 197 198
    if( (Srb_id & RAB_OFFSET ) == MIBCH) {

        mib->message.systemFrameNumber.buf = &sfn;
	enc_rval = uper_encode_to_buffer(&asn_DEF_BCCH_BCH_Message,
					 (void*)mib,
					 carrier->MIB,
199
					 24);
200
	LOG_D(RRC,"Encoded MIB for frame %d (%p), bits %lu\n",sfn,carrier->MIB,enc_rval.encoded);
201 202 203 204 205 206 207
	buffer_pP[0]=carrier->MIB[0];
	buffer_pP[1]=carrier->MIB[1];
	buffer_pP[2]=carrier->MIB[2];
	AssertFatal (enc_rval.encoded > 0, "ASN1 message encoding failed (%s, %lu)!\n",
		     enc_rval.failed_type->name, enc_rval.encoded);
	return(3);
    }
208

209
    if( (Srb_id & RAB_OFFSET ) == CCCH) {
210
      LOG_T(RRC,"[eNB %d] Frame %d CCCH request (Srb_id %d)\n",Mod_idP,frameP, Srb_id);
211

212
      if(RC.rrc[Mod_idP]->carrier[CC_id].Srb0.Active==0) {
213 214 215 216
        LOG_E(RRC,"[eNB %d] CCCH Not active\n",Mod_idP);
        return -1;
      }

217
      Srb_info=&RC.rrc[Mod_idP]->carrier[CC_id].Srb0;
218

219 220 221
      // check if data is there for MAC
      if(Srb_info->Tx_buffer.payload_size>0) { //Fill buffer
        LOG_D(RRC,"[eNB %d] CCCH (%p) has %d bytes (dest: %p, src %p)\n",Mod_idP,Srb_info,Srb_info->Tx_buffer.payload_size,buffer_pP,Srb_info->Tx_buffer.Payload);
222 223

#if defined(ENABLE_ITTI)
224 225 226 227 228 229 230 231 232 233 234 235 236
        {
          MessageDef *message_p;
          int ccch_size = Srb_info->Tx_buffer.payload_size;
          int sdu_size = sizeof(RRC_MAC_CCCH_DATA_REQ (message_p).sdu);

          if (ccch_size > sdu_size) {
            LOG_E(RRC, "SDU larger than CCCH SDU buffer size (%d, %d)", ccch_size, sdu_size);
            ccch_size = sdu_size;
          }

          message_p = itti_alloc_new_message (TASK_RRC_ENB, RRC_MAC_CCCH_DATA_REQ);
          RRC_MAC_CCCH_DATA_REQ (message_p).frame = frameP;
          RRC_MAC_CCCH_DATA_REQ (message_p).sdu_size = ccch_size;
Cedric Roux's avatar
Cedric Roux committed
237
          memset (RRC_MAC_CCCH_DATA_REQ (message_p).sdu, 0, CCCH_SDU_SIZE);
238 239 240
          memcpy (RRC_MAC_CCCH_DATA_REQ (message_p).sdu, Srb_info->Tx_buffer.Payload, ccch_size);
          RRC_MAC_CCCH_DATA_REQ (message_p).enb_index = eNB_index;

241
          itti_send_msg_to_task (TASK_MAC_ENB, ENB_MODULE_ID_TO_INSTANCE(Mod_idP), message_p);
242
        }
243 244
#endif

245 246 247 248
        memcpy(buffer_pP,Srb_info->Tx_buffer.Payload,Srb_info->Tx_buffer.payload_size);
        Sdu_size = Srb_info->Tx_buffer.payload_size;
        Srb_info->Tx_buffer.payload_size=0;
      }
249

250 251
      return (Sdu_size);
    }
252

Cedric Roux's avatar
Cedric Roux committed
253
#if defined(Rel10) || defined(Rel14)
254 255

    if((Srb_id & RAB_OFFSET) == MCCH) {
256
      if(RC.rrc[Mod_idP]->carrier[CC_id].MCCH_MESS[mbsfn_sync_area].Active==0) {
257 258
        return 0;  // this parameter is set in function init_mcch in rrc_eNB.c
      }
259

260 261

#if defined(ENABLE_ITTI)
262 263
      {
        MessageDef *message_p;
264
        int mcch_size = RC.rrc[Mod_idP]->carrier[CC_id].sizeof_MCCH_MESSAGE[mbsfn_sync_area];
265 266 267 268 269 270 271 272 273 274
        int sdu_size = sizeof(RRC_MAC_MCCH_DATA_REQ (message_p).sdu);

        if (mcch_size > sdu_size) {
          LOG_E(RRC, "SDU larger than MCCH SDU buffer size (%d, %d)", mcch_size, sdu_size);
          mcch_size = sdu_size;
        }

        message_p = itti_alloc_new_message (TASK_RRC_ENB, RRC_MAC_MCCH_DATA_REQ);
        RRC_MAC_MCCH_DATA_REQ (message_p).frame = frameP;
        RRC_MAC_MCCH_DATA_REQ (message_p).sdu_size = mcch_size;
Cedric Roux's avatar
Cedric Roux committed
275
        memset (RRC_MAC_MCCH_DATA_REQ (message_p).sdu, 0, MCCH_SDU_SIZE);
276
        memcpy (RRC_MAC_MCCH_DATA_REQ (message_p).sdu,
277
                RC.rrc[Mod_idP]->carrier[CC_id].MCCH_MESSAGE[mbsfn_sync_area],
278
                mcch_size);
279 280 281
        RRC_MAC_MCCH_DATA_REQ (message_p).enb_index = eNB_index;
        RRC_MAC_MCCH_DATA_REQ (message_p).mbsfn_sync_area = mbsfn_sync_area;

282
        itti_send_msg_to_task (TASK_MAC_ENB, ENB_MODULE_ID_TO_INSTANCE(Mod_idP), message_p);
283
      }
284 285
#endif

286
      memcpy(&buffer_pP[0],
287 288
             RC.rrc[Mod_idP]->carrier[CC_id].MCCH_MESSAGE[mbsfn_sync_area],
             RC.rrc[Mod_idP]->carrier[CC_id].sizeof_MCCH_MESSAGE[mbsfn_sync_area]);
gauthier's avatar
 
gauthier committed
289

290
#ifdef DEBUG_RRC
291 292
      LOG_D(RRC,"[eNB %d] Frame %d : MCCH request => MCCH_MESSAGE \n",Mod_idP,frameP);

293
      for (i=0; i<RC.rrc[Mod_idP]->carrier[CC_id].sizeof_MCCH_MESSAGE[mbsfn_sync_area]; i++) {
294
        LOG_T(RRC,"%x.",buffer_pP[i]);
295
      }
296 297

      LOG_T(RRC,"\n");
298
#endif
gauthier's avatar
 
gauthier committed
299

300
      return (RC.rrc[Mod_idP]->carrier[CC_id].sizeof_MCCH_MESSAGE[mbsfn_sync_area]);
301 302 303
      //      }
      //else
      //return(0);
304 305
    }

Cedric Roux's avatar
Cedric Roux committed
306
#endif //Rel10 || Rel14
307
  } else {  //This is an UE
308

309

310 311
    LOG_D(RRC,"[UE %d] Frame %d Filling CCCH SRB_ID %d\n",Mod_idP,frameP,Srb_id);
    LOG_D(RRC,"[UE %d] Frame %d buffer_pP status %d,\n",Mod_idP,frameP, UE_rrc_inst[Mod_idP].Srb0[eNB_index].Tx_buffer.payload_size);
312

313 314

    if( (UE_rrc_inst[Mod_idP].Srb0[eNB_index].Tx_buffer.payload_size > 0) ) {
315 316

#if defined(ENABLE_ITTI)
317 318 319 320
      {
        MessageDef *message_p;
        int ccch_size = UE_rrc_inst[Mod_idP].Srb0[eNB_index].Tx_buffer.payload_size;
        int sdu_size = sizeof(RRC_MAC_CCCH_DATA_REQ (message_p).sdu);
321

322 323 324
        if (ccch_size > sdu_size) {
          LOG_E(RRC, "SDU larger than CCCH SDU buffer size (%d, %d)", ccch_size, sdu_size);
          ccch_size = sdu_size;
gauthier's avatar
 
gauthier committed
325
        }
326 327 328 329

        message_p = itti_alloc_new_message (TASK_RRC_UE, RRC_MAC_CCCH_DATA_REQ);
        RRC_MAC_CCCH_DATA_REQ (message_p).frame = frameP;
        RRC_MAC_CCCH_DATA_REQ (message_p).sdu_size = ccch_size;
Cedric Roux's avatar
Cedric Roux committed
330
        memset (RRC_MAC_CCCH_DATA_REQ (message_p).sdu, 0, CCCH_SDU_SIZE);
331 332 333
        memcpy (RRC_MAC_CCCH_DATA_REQ (message_p).sdu, UE_rrc_inst[Mod_idP].Srb0[eNB_index].Tx_buffer.Payload, ccch_size);
        RRC_MAC_CCCH_DATA_REQ (message_p).enb_index = eNB_index;

334
        itti_send_msg_to_task (TASK_MAC_UE, UE_MODULE_ID_TO_INSTANCE(Mod_idP), message_p);
335 336 337 338 339 340 341 342 343 344 345 346
      }
#endif

      memcpy(&buffer_pP[0],&UE_rrc_inst[Mod_idP].Srb0[eNB_index].Tx_buffer.Payload[0],UE_rrc_inst[Mod_idP].Srb0[eNB_index].Tx_buffer.payload_size);
      uint8_t Ret_size=UE_rrc_inst[Mod_idP].Srb0[eNB_index].Tx_buffer.payload_size;
      //   UE_rrc_inst[Mod_id].Srb0[eNB_index].Tx_buffer.payload_size=0;
      UE_rrc_inst[Mod_idP].Info[eNB_index].T300_active = 1;
      UE_rrc_inst[Mod_idP].Info[eNB_index].T300_cnt = 0;
      //      msg("[RRC][UE %d] Sending rach\n",Mod_id);
      return(Ret_size);
    } else {
      return 0;
347
    }
348 349 350
  }

  return(0);
351 352
}

353 354
//------------------------------------------------------------------------------
int8_t
355
mac_rrc_data_ind(
356
  const module_id_t     module_idP,
357
  const int             CC_id,
358
  const frame_t         frameP,
gauthier's avatar
gauthier committed
359
  const sub_frame_t     sub_frameP,
360 361 362 363 364 365 366 367 368
  const rnti_t          rntiP,
  const rb_id_t         srb_idP,
  const uint8_t*        sduP,
  const sdu_size_t      sdu_lenP,
  const eNB_flag_t      eNB_flagP,
  const mac_enb_index_t eNB_indexP,
  const uint8_t         mbsfn_sync_areaP
)
//--------------------------------------------------------------------------
369 370
{
  SRB_INFO *Srb_info;
371
  protocol_ctxt_t ctxt;
372
  sdu_size_t      sdu_size = 0;
Cedric Roux's avatar
Cedric Roux committed
373 374 375 376

  /* for no gcc warnings */
  (void)sdu_size;

377
  /*
378
  int si_window;
379
   */
gauthier's avatar
gauthier committed
380
  PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, module_idP, eNB_flagP, rntiP, frameP, sub_frameP,eNB_indexP);
381

382 383
  if(eNB_flagP == ENB_FLAG_NO) {
    if(srb_idP == BCCH) {
384
      LOG_D(RRC,"[UE %d] Received SDU for BCCH on SRB %d from eNB %d\n",module_idP,srb_idP,eNB_indexP);
385

gauthier's avatar
 
gauthier committed
386
#if defined(ENABLE_ITTI)
387 388 389 390
      {
        MessageDef *message_p;
        int msg_sdu_size = sizeof(RRC_MAC_BCCH_DATA_IND (message_p).sdu);

391 392
        if (sdu_lenP > msg_sdu_size) {
          LOG_E(RRC, "SDU larger than BCCH SDU buffer size (%d, %d)", sdu_lenP, msg_sdu_size);
393
          sdu_size = msg_sdu_size;
394 395
        } else {
          sdu_size = sdu_lenP;
396 397 398
        }

        message_p = itti_alloc_new_message (TASK_MAC_UE, RRC_MAC_BCCH_DATA_IND);
Cedric Roux's avatar
Cedric Roux committed
399
        memset (RRC_MAC_BCCH_DATA_IND (message_p).sdu, 0, BCCH_SDU_SIZE);
gauthier's avatar
gauthier committed
400 401 402
        RRC_MAC_BCCH_DATA_IND (message_p).frame     = frameP;
        RRC_MAC_BCCH_DATA_IND (message_p).sub_frame = sub_frameP;
        RRC_MAC_BCCH_DATA_IND (message_p).sdu_size  = sdu_size;
403 404
        memcpy (RRC_MAC_BCCH_DATA_IND (message_p).sdu, sduP, sdu_size);
        RRC_MAC_BCCH_DATA_IND (message_p).enb_index = eNB_indexP;
gauthier's avatar
gauthier committed
405 406
        RRC_MAC_BCCH_DATA_IND (message_p).rsrq      = 30 /* TODO change phy to report rspq */;
        RRC_MAC_BCCH_DATA_IND (message_p).rsrp      = 45 /* TODO change phy to report rspp */;
407

408
        itti_send_msg_to_task (TASK_RRC_UE, ctxt.instance, message_p);
409
      }
410
#else
Cedric Roux's avatar
Cedric Roux committed
411
      decode_BCCH_DLSCH_Message(&ctxt,eNB_indexP,(uint8_t*)sduP,sdu_lenP, 0, 0);
412
#endif
413
    }
414

415 416 417 418
    if(srb_idP == PCCH) {
      LOG_D(RRC,"[UE %d] Received SDU for PCCH on SRB %d from eNB %d\n",module_idP,srb_idP,eNB_indexP);
      decode_PCCH_DLSCH_Message(&ctxt,eNB_indexP,(uint8_t*)sduP,sdu_lenP);
    }
419 420 421
    if((srb_idP & RAB_OFFSET) == CCCH) {
      if (sdu_lenP>0) {
        LOG_T(RRC,"[UE %d] Received SDU for CCCH on SRB %d from eNB %d\n",module_idP,srb_idP & RAB_OFFSET,eNB_indexP);
422 423

#if defined(ENABLE_ITTI)
424 425
        {
          MessageDef *message_p;
426
          int msg_sdu_size = CCCH_SDU_SIZE;
427

428
          if (sdu_lenP > msg_sdu_size) {
429 430
            LOG_E(RRC, "SDU larger than CCCH SDU buffer size (%d, %d)", sdu_size, msg_sdu_size);
            sdu_size = msg_sdu_size;
431 432
          } else {
            sdu_size =  sdu_lenP;
433 434 435
          }

          message_p = itti_alloc_new_message (TASK_MAC_UE, RRC_MAC_CCCH_DATA_IND);
436 437
          memset (RRC_MAC_CCCH_DATA_IND (message_p).sdu, 0, CCCH_SDU_SIZE);
          memcpy (RRC_MAC_CCCH_DATA_IND (message_p).sdu, sduP, sdu_size);
gauthier's avatar
gauthier committed
438 439 440
          RRC_MAC_CCCH_DATA_IND (message_p).frame     = frameP;
          RRC_MAC_CCCH_DATA_IND (message_p).sub_frame = sub_frameP;
          RRC_MAC_CCCH_DATA_IND (message_p).sdu_size  = sdu_size;
441 442 443
          RRC_MAC_CCCH_DATA_IND (message_p).enb_index = eNB_indexP;
          RRC_MAC_CCCH_DATA_IND (message_p).rnti      = rntiP;
          itti_send_msg_to_task (TASK_RRC_UE, ctxt.instance, message_p);
444
        }
445
#else
446 447 448 449
        Srb_info = &UE_rrc_inst[module_idP].Srb0[eNB_indexP];
        memcpy(Srb_info->Rx_buffer.Payload,sduP,sdu_lenP);
        Srb_info->Rx_buffer.payload_size = sdu_lenP;
        rrc_ue_decode_ccch(&ctxt, Srb_info, eNB_indexP);
450
#endif
451 452
      }
    }
453

Cedric Roux's avatar
Cedric Roux committed
454
#if defined(Rel10) || defined(Rel14)
455

456
    if ((srb_idP & RAB_OFFSET) == MCCH) {
457
      LOG_T(RRC,"[UE %d] Frame %d: Received SDU on MBSFN sync area %d for MCCH on SRB %d from eNB %d\n",
458
            module_idP,frameP, mbsfn_sync_areaP, srb_idP & RAB_OFFSET,eNB_indexP);
459

gauthier's avatar
 
gauthier committed
460
#if defined(ENABLE_ITTI)
461 462 463 464 465 466 467 468 469 470 471
      {
        MessageDef *message_p;
        int msg_sdu_size = sizeof(RRC_MAC_MCCH_DATA_IND (message_p).sdu);

        if (sdu_size > msg_sdu_size) {
          LOG_E(RRC, "SDU larger than MCCH SDU buffer size (%d, %d)", sdu_size, msg_sdu_size);
          sdu_size = msg_sdu_size;
        }

        message_p = itti_alloc_new_message (TASK_MAC_UE, RRC_MAC_MCCH_DATA_IND);
        RRC_MAC_MCCH_DATA_IND (message_p).frame           = frameP;
gauthier's avatar
gauthier committed
472
        RRC_MAC_MCCH_DATA_IND (message_p).sub_frame       = sub_frameP;
473
        RRC_MAC_MCCH_DATA_IND (message_p).sdu_size        = sdu_lenP;
Cedric Roux's avatar
Cedric Roux committed
474
        memset (RRC_MAC_MCCH_DATA_IND (message_p).sdu, 0, MCCH_SDU_SIZE);
475 476 477 478
        memcpy (RRC_MAC_MCCH_DATA_IND (message_p).sdu, sduP, sdu_lenP);
        RRC_MAC_MCCH_DATA_IND (message_p).enb_index       = eNB_indexP;
        RRC_MAC_MCCH_DATA_IND (message_p).mbsfn_sync_area = mbsfn_sync_areaP;
        itti_send_msg_to_task (TASK_RRC_UE, ctxt.instance, message_p);
479
      }
480
#else
481
      decode_MCCH_Message(&ctxt, eNB_indexP, sduP, sdu_lenP, mbsfn_sync_areaP);
482
#endif
483 484
    }

Cedric Roux's avatar
Cedric Roux committed
485
#endif // Rel10 || Rel14
486

487
  } else { // This is an eNB
488
    Srb_info = &RC.rrc[module_idP]->carrier[CC_id].Srb0;
489
    LOG_T(RRC,"[eNB %d] Received SDU for CCCH on SRB %d\n",module_idP,Srb_info->Srb_id);
490

491
#if defined(ENABLE_ITTI)
492 493 494 495
    {
      MessageDef *message_p;
      int msg_sdu_size = sizeof(RRC_MAC_CCCH_DATA_IND (message_p).sdu);

496 497
      if (sdu_lenP > msg_sdu_size) {
        LOG_E(RRC, "SDU larger than CCCH SDU buffer size (%d, %d)", sdu_lenP, msg_sdu_size);
498
        sdu_size = msg_sdu_size;
499 500
      } else {
        sdu_size = sdu_lenP;
501 502 503
      }

      message_p = itti_alloc_new_message (TASK_MAC_ENB, RRC_MAC_CCCH_DATA_IND);
gauthier's avatar
gauthier committed
504 505 506 507
      RRC_MAC_CCCH_DATA_IND (message_p).frame     = frameP;
      RRC_MAC_CCCH_DATA_IND (message_p).sub_frame = sub_frameP;
      RRC_MAC_CCCH_DATA_IND (message_p).rnti      = rntiP;
      RRC_MAC_CCCH_DATA_IND (message_p).sdu_size  = sdu_size;
508
      RRC_MAC_CCCH_DATA_IND (message_p).CC_id = CC_id;
Cedric Roux's avatar
Cedric Roux committed
509
      memset (RRC_MAC_CCCH_DATA_IND (message_p).sdu, 0, CCCH_SDU_SIZE);
510
      memcpy (RRC_MAC_CCCH_DATA_IND (message_p).sdu, sduP, sdu_size);
511
      itti_send_msg_to_task (TASK_RRC_ENB, ctxt.instance, message_p);
512
    }
513
#else
514 515

    //    msg("\n******INST %d Srb_info %p, Srb_id=%d****\n\n",Mod_id,Srb_info,Srb_info->Srb_id);
516 517 518
    if (sdu_lenP > 0) {
      memcpy(Srb_info->Rx_buffer.Payload,sduP,sdu_lenP);
      Srb_info->Rx_buffer.payload_size = sdu_lenP;
519
      rrc_eNB_decode_ccch(&ctxt, Srb_info, CC_id);
gauthier's avatar
 
gauthier committed
520
    }
521

522 523 524 525
#endif
  }

  return(0);
526 527 528 529

}

//-------------------------------------------------------------------------------------------//
530
// this function is Not USED anymore
531
void mac_sync_ind(module_id_t Mod_idP,uint8_t Status)
532 533
{
  //-------------------------------------------------------------------------------------------//
534 535
}

536 537
//------------------------------------------------------------------------------
uint8_t
538
rrc_data_req(
539 540 541 542 543 544 545 546 547
  const protocol_ctxt_t*   const ctxt_pP,
  const rb_id_t                  rb_idP,
  const mui_t                    muiP,
  const confirm_t                confirmP,
  const sdu_size_t               sdu_sizeP,
  uint8_t*                 const buffer_pP,
  const pdcp_transmission_mode_t modeP
)
//------------------------------------------------------------------------------
548
{
gauthier's avatar
gauthier committed
549
  MSC_LOG_TX_MESSAGE(
550 551 552 553 554 555 556 557 558
    ctxt_pP->enb_flag ? MSC_RRC_ENB : MSC_RRC_UE,
    ctxt_pP->enb_flag ? MSC_PDCP_ENB : MSC_PDCP_UE,
    buffer_pP,
    sdu_sizeP,
    MSC_AS_TIME_FMT"RRC_DCCH_DATA_REQ UE %x MUI %d size %u",
    MSC_AS_TIME_ARGS(ctxt_pP),
    ctxt_pP->rnti,
    muiP,
    sdu_sizeP);
gauthier's avatar
gauthier committed
559

560
#if defined(ENABLE_ITTI)
561 562 563 564 565
  {
    MessageDef *message_p;
    // Uses a new buffer to avoid issue with PDCP buffer content that could be changed by PDCP (asynchronous message handling).
    uint8_t *message_buffer;

566 567 568 569 570 571
    message_buffer = itti_malloc (
                       ctxt_pP->enb_flag ? TASK_RRC_ENB : TASK_RRC_UE,
                       ctxt_pP->enb_flag ? TASK_PDCP_ENB : TASK_PDCP_UE,
                       sdu_sizeP);

    memcpy (message_buffer, buffer_pP, sdu_sizeP);
572

573 574 575
    message_p = itti_alloc_new_message (ctxt_pP->enb_flag ? TASK_RRC_ENB : TASK_RRC_UE, RRC_DCCH_DATA_REQ);
    RRC_DCCH_DATA_REQ (message_p).frame     = ctxt_pP->frame;
    RRC_DCCH_DATA_REQ (message_p).enb_flag  = ctxt_pP->enb_flag;
576 577 578
    RRC_DCCH_DATA_REQ (message_p).rb_id     = rb_idP;
    RRC_DCCH_DATA_REQ (message_p).muip      = muiP;
    RRC_DCCH_DATA_REQ (message_p).confirmp  = confirmP;
579
    RRC_DCCH_DATA_REQ (message_p).sdu_size  = sdu_sizeP;
580
    RRC_DCCH_DATA_REQ (message_p).sdu_p     = message_buffer;
581 582 583
    RRC_DCCH_DATA_REQ (message_p).mode      = modeP;
    RRC_DCCH_DATA_REQ (message_p).module_id = ctxt_pP->module_id;
    RRC_DCCH_DATA_REQ (message_p).rnti      = ctxt_pP->rnti;
584
    RRC_DCCH_DATA_REQ (message_p).eNB_index = ctxt_pP->eNB_index;
585 586 587 588 589

    itti_send_msg_to_task (
      ctxt_pP->enb_flag ? TASK_PDCP_ENB : TASK_PDCP_UE,
      ctxt_pP->instance,
      message_p);
590 591 592
    return TRUE; // TODO should be changed to a CNF message later, currently RRC lite does not used the returned value anyway.

  }
593
#else
594 595 596 597 598 599 600 601 602
  return pdcp_data_req (
           ctxt_pP,
           SRB_FLAG_YES,
           rb_idP,
           muiP,
           confirmP,
           sdu_sizeP,
           buffer_pP,
           modeP);
603
#endif
604 605
}

606 607
//------------------------------------------------------------------------------
void
608
rrc_data_ind(
609 610 611 612 613 614
  const protocol_ctxt_t* const ctxt_pP,
  const rb_id_t                Srb_id,
  const sdu_size_t             sdu_sizeP,
  const uint8_t*   const       buffer_pP
)
//------------------------------------------------------------------------------
615 616
{
  rb_id_t    DCCH_index = Srb_id;
617

618
  if (ctxt_pP->enb_flag == ENB_FLAG_NO) {
619
    LOG_N(RRC, "[UE %x] Frame %d: received a DCCH %d message on SRB %d with Size %d from eNB %d\n",
620
          ctxt_pP->module_id, ctxt_pP->frame, DCCH_index,Srb_id,sdu_sizeP,  ctxt_pP->eNB_index);
621
  } else {
Cedric Roux's avatar
fix log  
Cedric Roux committed
622
    LOG_N(RRC, "[eNB %d] Frame %d: received a DCCH %d message on SRB %d with Size %d from UE %x\n",
623 624 625
          ctxt_pP->module_id,
          ctxt_pP->frame,
          DCCH_index,
626
          Srb_id,
627 628
          sdu_sizeP,
          ctxt_pP->rnti);
629
  }
630 631

#if defined(ENABLE_ITTI)
632 633 634 635 636
  {
    MessageDef *message_p;
    // Uses a new buffer to avoid issue with PDCP buffer content that could be changed by PDCP (asynchronous message handling).
    uint8_t *message_buffer;

637
    message_buffer = itti_malloc (ctxt_pP->enb_flag ? TASK_PDCP_ENB : TASK_PDCP_UE, ctxt_pP->enb_flag ? TASK_RRC_ENB : TASK_RRC_UE, sdu_sizeP);
638 639
    memcpy (message_buffer, buffer_pP, sdu_sizeP);

640 641
    message_p = itti_alloc_new_message (ctxt_pP->enb_flag ? TASK_PDCP_ENB : TASK_PDCP_UE, RRC_DCCH_DATA_IND);
    RRC_DCCH_DATA_IND (message_p).frame      = ctxt_pP->frame;
642 643 644
    RRC_DCCH_DATA_IND (message_p).dcch_index = DCCH_index;
    RRC_DCCH_DATA_IND (message_p).sdu_size   = sdu_sizeP;
    RRC_DCCH_DATA_IND (message_p).sdu_p      = message_buffer;
645 646
    RRC_DCCH_DATA_IND (message_p).rnti       = ctxt_pP->rnti;
    RRC_DCCH_DATA_IND (message_p).module_id  = ctxt_pP->module_id;
647 648
    RRC_DCCH_DATA_IND (message_p).eNB_index  = ctxt_pP->eNB_index;

649
    itti_send_msg_to_task (ctxt_pP->enb_flag ? TASK_RRC_ENB : TASK_RRC_UE, ctxt_pP->instance, message_p);
650 651
  }
#else
gauthier's avatar
 
gauthier committed
652

653 654 655 656 657 658
  if (ctxt_pP->enb_flag == ENB_FLAG_YES) {
    rrc_eNB_decode_dcch(
      ctxt_pP,
      DCCH_index,
      buffer_pP,
      sdu_sizeP);
659
  } else {
Cedric Roux's avatar
Cedric Roux committed
660
//#warning "LG put 0 to arg4 that is eNB index"
661 662 663 664 665
    rrc_ue_decode_dcch(
      ctxt_pP,
      DCCH_index,
      buffer_pP,
      0);
666
  }
gauthier's avatar
 
gauthier committed
667

668
#endif
669 670
}

671
//-------------------------------------------------------------------------------------------//
672
void rrc_in_sync_ind(module_id_t Mod_idP, frame_t frameP, uint16_t eNB_index)
673 674
{
  //-------------------------------------------------------------------------------------------//
675
#if defined(ENABLE_ITTI)
676 677
  {
    MessageDef *message_p;
678
    //LOG_I(RRC,"sending a message to task_mac_ue\n");
679 680 681
    message_p = itti_alloc_new_message (TASK_MAC_UE, RRC_MAC_IN_SYNC_IND);
    RRC_MAC_IN_SYNC_IND (message_p).frame = frameP;
    RRC_MAC_IN_SYNC_IND (message_p).enb_index = eNB_index;
682

683
    itti_send_msg_to_task (TASK_RRC_UE, UE_MODULE_ID_TO_INSTANCE(Mod_idP), message_p);
684
  }
685
#else
686 687
  UE_rrc_inst[Mod_idP].Info[eNB_index].N310_cnt=0;

688
  if (UE_rrc_inst[Mod_idP].Info[eNB_index].T310_active==1) {
689
    UE_rrc_inst[Mod_idP].Info[eNB_index].N311_cnt++;
690
  }
691

692
#endif
693 694
}

695
//-------------------------------------------------------------------------------------------//
696
void rrc_out_of_sync_ind(module_id_t Mod_idP, frame_t frameP, uint16_t eNB_index)
697 698
{
  //-------------------------------------------------------------------------------------------//
699 700 701 702 703 704 705 706
  if (UE_rrc_inst[Mod_idP].Info[eNB_index].N310_cnt>10)
    LOG_I(RRC,"[UE %d] Frame %d: OUT OF SYNC FROM eNB %d (T310 active %d : T310 %d, N310 %d, N311 %d)\n ",
	  Mod_idP,frameP,eNB_index,
	  UE_rrc_inst[Mod_idP].Info[eNB_index].T300_active,
	  UE_rrc_inst[Mod_idP].Info[eNB_index].T310_cnt,
	  UE_rrc_inst[Mod_idP].Info[eNB_index].N310_cnt,
	  UE_rrc_inst[Mod_idP].Info[eNB_index].N311_cnt);
  
707
#if defined(ENABLE_ITTI)
708 709
  {
    MessageDef *message_p;
710

711 712 713
    message_p = itti_alloc_new_message (TASK_MAC_UE, RRC_MAC_OUT_OF_SYNC_IND);
    RRC_MAC_OUT_OF_SYNC_IND (message_p).frame = frameP;
    RRC_MAC_OUT_OF_SYNC_IND (message_p).enb_index = eNB_index;
714

715
    itti_send_msg_to_task (TASK_RRC_UE, UE_MODULE_ID_TO_INSTANCE(Mod_idP), message_p);
716
  }
717
#else
718
  UE_rrc_inst[Mod_idP].Info[eNB_index].N310_cnt++;
719
#endif
720 721
}

722 723
//------------------------------------------------------------------------------
int
724
mac_eNB_get_rrc_status(
725 726 727 728
  const module_id_t Mod_idP,
  const rnti_t      rntiP
)
//------------------------------------------------------------------------------
729
{
730 731
  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
  ue_context_p = rrc_eNB_get_ue_context(
732
                   RC.rrc[Mod_idP],
733 734 735 736
                   rntiP);

  if (ue_context_p != NULL) {
    return(ue_context_p->ue_context.Status);
737
  } else {
738
    return RRC_INACTIVE;
739
  }
740 741
}

742 743
void mac_eNB_rrc_ul_failure(const module_id_t Mod_instP,
			    const int CC_idP,
744 745
			    const frame_t frameP,
			    const sub_frame_t subframeP,
746 747
			    const rnti_t rntiP)
{
748 749
  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
  ue_context_p = rrc_eNB_get_ue_context(
750
                   RC.rrc[Mod_instP],
751 752 753
                   rntiP);

  if (ue_context_p != NULL) {
Florian Kaltenberger's avatar
Florian Kaltenberger committed
754
    LOG_I(RRC,"Frame %d, Subframe %d: UE %x UL failure, activating timer\n",frameP,subframeP,rntiP);
755 756 757
    ue_context_p->ue_context.ul_failure_timer=1;
  }
  else {
758
    LOG_W(RRC,"Frame %d, Subframe %d: UL failure: UE %x unknown \n",frameP,subframeP,rntiP);
759
  }
760
  rrc_mac_remove_ue(Mod_instP,rntiP);
761
}
Florian Kaltenberger's avatar
Florian Kaltenberger committed
762 763

void mac_eNB_rrc_ul_in_sync(const module_id_t Mod_instP, 
764
			    const int CC_idP, 
Florian Kaltenberger's avatar
Florian Kaltenberger committed
765 766
			    const frame_t frameP,
			    const sub_frame_t subframeP,
767 768
			    const rnti_t rntiP)
{
769 770
  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
  ue_context_p = rrc_eNB_get_ue_context(
771
                   RC.rrc[Mod_instP],
772 773 774
                   rntiP);

  if (ue_context_p != NULL) {
775 776 777 778 779 780
    LOG_I(RRC,"Frame %d, Subframe %d: UE %x to UL in synch\n",
          frameP, subframeP, rntiP);
    ue_context_p->ue_context.ul_failure_timer = 0;
  } else {
    LOG_E(RRC,"Frame %d, Subframe %d: UE %x unknown \n",
          frameP, subframeP, rntiP);
781
  }
Florian Kaltenberger's avatar
Florian Kaltenberger committed
782
}
783 784
//------------------------------------------------------------------------------
int
785
mac_UE_get_rrc_status(
786 787 788 789 790 791 792 793
  const module_id_t Mod_idP,
  const uint8_t     indexP
)
//------------------------------------------------------------------------------
{
  return(UE_rrc_inst[Mod_idP].Info[indexP].State);
}

794
//-------------------------------------------------------------------------------------------//
795 796 797
int mac_ue_ccch_success_ind(module_id_t Mod_idP, uint8_t eNB_index)
{
  //-------------------------------------------------------------------------------------------//
798
#if defined(ENABLE_ITTI)
799 800
  {
    MessageDef *message_p;
801

802 803
    message_p = itti_alloc_new_message (TASK_MAC_UE, RRC_MAC_CCCH_DATA_CNF);
    RRC_MAC_CCCH_DATA_CNF (message_p).enb_index = eNB_index;
804

805
    itti_send_msg_to_task (TASK_RRC_UE, UE_MODULE_ID_TO_INSTANCE(Mod_idP), message_p);
806
  }
807
#else
808 809
  // reset the tx buffer to indicate RRC that ccch was successfully transmitted (for example if contention resolution succeeds)
  UE_rrc_inst[Mod_idP].Srb0[eNB_index].Tx_buffer.payload_size=0;
810
#endif
811
  return 0;
812
}