L2_interface.c 29.9 KB
Newer Older
1
2
3
4
5
/*
 * 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
Cedric Roux's avatar
Cedric Roux committed
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 * 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
knopp's avatar
knopp 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
#ifdef DEBUG_RRC
85
  int i;
86
  LOG_I(RRC,"[eNB %d] mac_rrc_data_req to SRB ID=%d\n",Mod_idP,Srb_id);
87
88
#endif

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

93
  if( enb_flagP == ENB_FLAG_YES) {
94

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

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

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

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

knopp's avatar
knopp committed
113
#if 0 //defined(ENABLE_ITTI)
114
115
        {
          MessageDef *message_p;
116
          int sib1_size = RC.rrc[Mod_idP]->carrier[CC_id].sizeof_SIB1;
117
118
119
120
121
122
123
124
125
126
          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
127
          memset (RRC_MAC_BCCH_DATA_REQ (message_p).sdu, 0, BCCH_SDU_SIZE);
128
          memcpy (RRC_MAC_BCCH_DATA_REQ (message_p).sdu,
129
                  RC.rrc[Mod_idP]->carrier[CC_id].SIB1,
130
                  sib1_size);
131
132
          RRC_MAC_BCCH_DATA_REQ (message_p).enb_index = eNB_index;

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

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

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

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

147
        return (RC.rrc[Mod_idP]->carrier[CC_id].sizeof_SIB1);
148
149
      } // All RFN mod 8 transmit SIB2-3 in SF 5
      else if ((frameP%8) == 1) {
150
      //LOG_D(RRC, "%s() frameP mod 8==1 (frameP:%d) copy into buffer SIB23 size:%d\n", __FUNCTION__, frameP, RC.rrc[Mod_idP]->carrier[CC_id].sizeof_SIB23);
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 0 //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
      // check if data is there for MAC
      if(Srb_info->Tx_buffer.payload_size>0) { //Fill buffer
knopp's avatar
knopp committed
221
        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

knopp's avatar
knopp committed
223
#if 0 // 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

knopp's avatar
knopp committed
261
#if 0 // 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
308
309
310
311
312
313
314

#ifdef Rel14
    if ((Srb_id & RAB_OFFSET) == BCCH_SIB1_BR){
        memcpy(&buffer_pP[0],
               RC.rrc[Mod_idP]->carrier[CC_id].SIB1_BR,
               RC.rrc[Mod_idP]->carrier[CC_id].sizeof_SIB1_BR);
        return (RC.rrc[Mod_idP]->carrier[CC_id].sizeof_SIB1_BR);
    }
315
 
316
317
318
319
320
321
322
323
324
    if ((Srb_id & RAB_OFFSET) == BCCH_SI_BR){ // First SI message with SIB2/3
        memcpy(&buffer_pP[0],
               RC.rrc[Mod_idP]->carrier[CC_id].SIB23_BR,
               RC.rrc[Mod_idP]->carrier[CC_id].sizeof_SIB23_BR);
        return (RC.rrc[Mod_idP]->carrier[CC_id].sizeof_SIB23_BR);
    }

#endif

325
  } else {  //This is an UE
326

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
327
328
329
330
331
332
333
334
335
336
337
338
#ifdef Rel14
     LOG_D(RRC,"[UE %d] Frame %d Filling SL DISCOVERY 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].SL_Discovery[eNB_index].Tx_buffer.payload_size);

   //TTN (for D2D)
     if ((Srb_id & RAB_OFFSET) == SL_DISCOVERY){
        memcpy(&buffer_pP[0],&UE_rrc_inst[Mod_idP].SL_Discovery[eNB_index].Tx_buffer.Payload[0],UE_rrc_inst[Mod_idP].SL_Discovery[eNB_index].Tx_buffer.payload_size);
        uint8_t Ret_size=UE_rrc_inst[Mod_idP].SL_Discovery[eNB_index].Tx_buffer.payload_size;
        //      msg("[RRC][UE %d] Sending SL_Discovery\n",Mod_id);
        return(Ret_size);
     }
#endif
339

340
341
    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);
342

343
344

    if( (UE_rrc_inst[Mod_idP].Srb0[eNB_index].Tx_buffer.payload_size > 0) ) {
345
346

#if defined(ENABLE_ITTI)
347
348
349
350
      {
        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);
351

352
353
354
        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
355
        }
356
357
358
359

        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
360
        memset (RRC_MAC_CCCH_DATA_REQ (message_p).sdu, 0, CCCH_SDU_SIZE);
361
362
363
        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;

364
        itti_send_msg_to_task (TASK_MAC_UE, UE_MODULE_ID_TO_INSTANCE(Mod_idP), message_p);
365
366
367
368
369
370
371
372
373
374
375
376
      }
#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;
377
    }
378
379
380
  }

  return(0);
381
382
}

383
384
//------------------------------------------------------------------------------
int8_t
385
mac_rrc_data_ind(
386
  const module_id_t     module_idP,
387
  const int             CC_id,
388
  const frame_t         frameP,
gauthier's avatar
gauthier committed
389
  const sub_frame_t     sub_frameP,
390
391
392
393
394
395
396
397
398
  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
)
//--------------------------------------------------------------------------
399
400
{
  SRB_INFO *Srb_info;
401
  protocol_ctxt_t ctxt;
402
  sdu_size_t      sdu_size = 0;
Cedric Roux's avatar
Cedric Roux committed
403
404
405
406

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

407
  /*
408
  int si_window;
409
   */
gauthier's avatar
gauthier committed
410
  PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, module_idP, eNB_flagP, rntiP, frameP, sub_frameP,eNB_indexP);
411

412
413
  if(eNB_flagP == ENB_FLAG_NO) {
    if(srb_idP == BCCH) {
414
      LOG_D(RRC,"[UE %d] Received SDU for BCCH on SRB %d from eNB %d\n",module_idP,srb_idP,eNB_indexP);
415

gauthier's avatar
   
gauthier committed
416
#if defined(ENABLE_ITTI)
417
418
419
420
      {
        MessageDef *message_p;
        int msg_sdu_size = sizeof(RRC_MAC_BCCH_DATA_IND (message_p).sdu);

421
422
        if (sdu_lenP > msg_sdu_size) {
          LOG_E(RRC, "SDU larger than BCCH SDU buffer size (%d, %d)", sdu_lenP, msg_sdu_size);
423
          sdu_size = msg_sdu_size;
424
425
        } else {
          sdu_size = sdu_lenP;
426
427
428
        }

        message_p = itti_alloc_new_message (TASK_MAC_UE, RRC_MAC_BCCH_DATA_IND);
Cedric Roux's avatar
Cedric Roux committed
429
        memset (RRC_MAC_BCCH_DATA_IND (message_p).sdu, 0, BCCH_SDU_SIZE);
gauthier's avatar
gauthier committed
430
431
432
        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;
433
434
        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
435
436
        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 */;
437

438
        itti_send_msg_to_task (TASK_RRC_UE, ctxt.instance, message_p);
439
      }
440
#else
Cedric Roux's avatar
Cedric Roux committed
441
      decode_BCCH_DLSCH_Message(&ctxt,eNB_indexP,(uint8_t*)sduP,sdu_lenP, 0, 0);
442
#endif
443
    }
444

445
446
447
448
    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);
    }
449
450
451
    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);
452
453

#if defined(ENABLE_ITTI)
454
455
        {
          MessageDef *message_p;
456
          int msg_sdu_size = CCCH_SDU_SIZE;
457

458
          if (sdu_lenP > msg_sdu_size) {
459
460
            LOG_E(RRC, "SDU larger than CCCH SDU buffer size (%d, %d)", sdu_size, msg_sdu_size);
            sdu_size = msg_sdu_size;
461
462
          } else {
            sdu_size =  sdu_lenP;
463
464
465
          }

          message_p = itti_alloc_new_message (TASK_MAC_UE, RRC_MAC_CCCH_DATA_IND);
466
467
          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
468
469
470
          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;
471
472
473
          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);
474
        }
475
#else
476
477
478
479
        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);
480
#endif
481
482
      }
    }
483

Cedric Roux's avatar
Cedric Roux committed
484
#if defined(Rel10) || defined(Rel14)
485

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

gauthier's avatar
   
gauthier committed
490
#if defined(ENABLE_ITTI)
491
492
493
494
495
496
497
498
499
500
501
      {
        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
502
        RRC_MAC_MCCH_DATA_IND (message_p).sub_frame       = sub_frameP;
503
        RRC_MAC_MCCH_DATA_IND (message_p).sdu_size        = sdu_lenP;
Cedric Roux's avatar
Cedric Roux committed
504
        memset (RRC_MAC_MCCH_DATA_IND (message_p).sdu, 0, MCCH_SDU_SIZE);
505
506
507
508
        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);
509
      }
510
#else
511
      decode_MCCH_Message(&ctxt, eNB_indexP, sduP, sdu_lenP, mbsfn_sync_areaP);
512
#endif
513
514
    }

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
515
516
517
518
519
    //TTN (for D2D)
    if((srb_idP & RAB_OFFSET) == SL_DISCOVERY) {
       decode_SL_Discovery_Message(&ctxt, eNB_indexP, sduP, sdu_lenP);
    }

Cedric Roux's avatar
Cedric Roux committed
520
#endif // Rel10 || Rel14
521

522
  } else { // This is an eNB
523
    Srb_info = &RC.rrc[module_idP]->carrier[CC_id].Srb0;
knopp's avatar
knopp committed
524
    LOG_D(RRC,"[eNB %d] Received SDU for CCCH on SRB %d\n",module_idP,Srb_info->Srb_id);
525
526
    
#if 0 //defined(ENABLE_ITTI)
527
528
529
530
    {
      MessageDef *message_p;
      int msg_sdu_size = sizeof(RRC_MAC_CCCH_DATA_IND (message_p).sdu);

531
532
      if (sdu_lenP > msg_sdu_size) {
        LOG_E(RRC, "SDU larger than CCCH SDU buffer size (%d, %d)", sdu_lenP, msg_sdu_size);
533
        sdu_size = msg_sdu_size;
534
535
      } else {
        sdu_size = sdu_lenP;
536
537
538
      }

      message_p = itti_alloc_new_message (TASK_MAC_ENB, RRC_MAC_CCCH_DATA_IND);
gauthier's avatar
gauthier committed
539
540
541
542
      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;
543
      RRC_MAC_CCCH_DATA_IND (message_p).CC_id = CC_id;
Cedric Roux's avatar
Cedric Roux committed
544
      memset (RRC_MAC_CCCH_DATA_IND (message_p).sdu, 0, CCCH_SDU_SIZE);
545
      memcpy (RRC_MAC_CCCH_DATA_IND (message_p).sdu, sduP, sdu_size);
knopp's avatar
knopp committed
546
      LOG_D(RRC,"[eNB %d] Sending message to RRC task\n",module_idP);
547
      itti_send_msg_to_task (TASK_RRC_ENB, ctxt.instance, message_p);
548
    }
549
#else
550
551

    //    msg("\n******INST %d Srb_info %p, Srb_id=%d****\n\n",Mod_id,Srb_info,Srb_info->Srb_id);
552
553
554
    if (sdu_lenP > 0) {
      memcpy(Srb_info->Rx_buffer.Payload,sduP,sdu_lenP);
      Srb_info->Rx_buffer.payload_size = sdu_lenP;
555
      rrc_eNB_decode_ccch(&ctxt, Srb_info, CC_id);
gauthier's avatar
   
gauthier committed
556
    }
557

558
559
560
561
#endif
  }

  return(0);
562
563
564

}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
565

566
//-------------------------------------------------------------------------------------------//
567
// this function is Not USED anymore
568
void mac_sync_ind(module_id_t Mod_idP,uint8_t Status)
569
570
{
  //-------------------------------------------------------------------------------------------//
571
572
}

573
574
//------------------------------------------------------------------------------
uint8_t
575
rrc_data_req(
576
577
578
579
580
581
582
583
584
  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
)
//------------------------------------------------------------------------------
585
{
gauthier's avatar
gauthier committed
586
  MSC_LOG_TX_MESSAGE(
587
588
589
590
591
592
593
594
595
    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
596

597
#if defined(ENABLE_ITTI)
598
599
600
601
602
  {
    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;

603
604
605
606
607
608
    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);
609

610
611
612
    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;
613
614
615
    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;
616
    RRC_DCCH_DATA_REQ (message_p).sdu_size  = sdu_sizeP;
617
    RRC_DCCH_DATA_REQ (message_p).sdu_p     = message_buffer;
618
619
620
    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;
621
    RRC_DCCH_DATA_REQ (message_p).eNB_index = ctxt_pP->eNB_index;
622
623
624
625
626

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

  }
630
#else
631
632
633
634
635
636
637
638
639
  return pdcp_data_req (
           ctxt_pP,
           SRB_FLAG_YES,
           rb_idP,
           muiP,
           confirmP,
           sdu_sizeP,
           buffer_pP,
           modeP);
640
#endif
641
642
}

643
644
//------------------------------------------------------------------------------
void
645
rrc_data_ind(
646
647
648
649
650
651
  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
)
//------------------------------------------------------------------------------
652
653
{
  rb_id_t    DCCH_index = Srb_id;
654

655
  if (ctxt_pP->enb_flag == ENB_FLAG_NO) {
656
    LOG_N(RRC, "[UE %x] Frame %d: received a DCCH %d message on SRB %d with Size %d from eNB %d\n",
657
          ctxt_pP->module_id, ctxt_pP->frame, DCCH_index,Srb_id,sdu_sizeP,  ctxt_pP->eNB_index);
658
  } else {
Cedric Roux's avatar
fix log    
Cedric Roux committed
659
    LOG_N(RRC, "[eNB %d] Frame %d: received a DCCH %d message on SRB %d with Size %d from UE %x\n",
660
661
662
          ctxt_pP->module_id,
          ctxt_pP->frame,
          DCCH_index,
663
          Srb_id,
664
665
          sdu_sizeP,
          ctxt_pP->rnti);
666
  }
667
668

#if defined(ENABLE_ITTI)
669
670
671
672
673
  {
    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;

674
    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);
675
676
    memcpy (message_buffer, buffer_pP, sdu_sizeP);

677
678
    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;
679
680
681
    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;
682
683
    RRC_DCCH_DATA_IND (message_p).rnti       = ctxt_pP->rnti;
    RRC_DCCH_DATA_IND (message_p).module_id  = ctxt_pP->module_id;
684
685
    RRC_DCCH_DATA_IND (message_p).eNB_index  = ctxt_pP->eNB_index;

686
    itti_send_msg_to_task (ctxt_pP->enb_flag ? TASK_RRC_ENB : TASK_RRC_UE, ctxt_pP->instance, message_p);
687
688
  }
#else
gauthier's avatar
   
gauthier committed
689

690
691
692
693
694
695
  if (ctxt_pP->enb_flag == ENB_FLAG_YES) {
    rrc_eNB_decode_dcch(
      ctxt_pP,
      DCCH_index,
      buffer_pP,
      sdu_sizeP);
696
  } else {
Cedric Roux's avatar
Cedric Roux committed
697
//#warning "LG put 0 to arg4 that is eNB index"
698
699
700
701
702
    rrc_ue_decode_dcch(
      ctxt_pP,
      DCCH_index,
      buffer_pP,
      0);
703
  }
gauthier's avatar
   
gauthier committed
704

705
#endif
706
707
}

708
//-------------------------------------------------------------------------------------------//
709
void rrc_in_sync_ind(module_id_t Mod_idP, frame_t frameP, uint16_t eNB_index)
710
711
{
  //-------------------------------------------------------------------------------------------//
712
#if defined(ENABLE_ITTI)
713
714
  {
    MessageDef *message_p;
715
    //LOG_I(RRC,"sending a message to task_mac_ue\n");
716
717
718
    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;
719

720
    itti_send_msg_to_task (TASK_RRC_UE, UE_MODULE_ID_TO_INSTANCE(Mod_idP), message_p);
721
  }
722
#else
723
724
  UE_rrc_inst[Mod_idP].Info[eNB_index].N310_cnt=0;

725
  if (UE_rrc_inst[Mod_idP].Info[eNB_index].T310_active==1) {
726
    UE_rrc_inst[Mod_idP].Info[eNB_index].N311_cnt++;
727
  }
728

729
#endif
730
731
}

732
//-------------------------------------------------------------------------------------------//
733
void rrc_out_of_sync_ind(module_id_t Mod_idP, frame_t frameP, uint16_t eNB_index)
734
735
{
  //-------------------------------------------------------------------------------------------//
736
737
738
739
740
741
742
743
  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);
  
744
#if defined(ENABLE_ITTI)
745
746
  {
    MessageDef *message_p;
747

748
749
750
    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;
751

752
    itti_send_msg_to_task (TASK_RRC_UE, UE_MODULE_ID_TO_INSTANCE(Mod_idP), message_p);
753
  }
754
#else
755
  UE_rrc_inst[Mod_idP].Info[eNB_index].N310_cnt++;
756
#endif
757
758
}

759
760
//------------------------------------------------------------------------------
int
761
mac_eNB_get_rrc_status(
762
763
764
765
  const module_id_t Mod_idP,
  const rnti_t      rntiP
)
//------------------------------------------------------------------------------
766
{
767
768
  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
  ue_context_p = rrc_eNB_get_ue_context(
769
                   RC.rrc[Mod_idP],
770
771
772
773
                   rntiP);

  if (ue_context_p != NULL) {
    return(ue_context_p->ue_context.Status);
774
  } else {
775
    return RRC_INACTIVE;
776
  }
777
778
}

779
780
void mac_eNB_rrc_ul_failure(const module_id_t Mod_instP,
			    const int CC_idP,
781
782
			    const frame_t frameP,
			    const sub_frame_t subframeP,
783
784
			    const rnti_t rntiP)
{
785
786
  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
  ue_context_p = rrc_eNB_get_ue_context(
787
                   RC.rrc[Mod_instP],
788
789
790
                   rntiP);

  if (ue_context_p != NULL) {
Florian Kaltenberger's avatar
Florian Kaltenberger committed
791
    LOG_I(RRC,"Frame %d, Subframe %d: UE %x UL failure, activating timer\n",frameP,subframeP,rntiP);
792
793
794
    ue_context_p->ue_context.ul_failure_timer=1;
  }
  else {
795
    LOG_W(RRC,"Frame %d, Subframe %d: UL failure: UE %x unknown \n",frameP,subframeP,rntiP);
796
  }
797
  rrc_mac_remove_ue(Mod_instP,rntiP);
798
}
Florian Kaltenberger's avatar
Florian Kaltenberger committed
799
800

void mac_eNB_rrc_ul_in_sync(const module_id_t Mod_instP, 
801
			    const int CC_idP, 
Florian Kaltenberger's avatar
Florian Kaltenberger committed
802
803
			    const frame_t frameP,
			    const sub_frame_t subframeP,
804
805
			    const rnti_t rntiP)
{
806
807
  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
  ue_context_p = rrc_eNB_get_ue_context(
808
                   RC.rrc[Mod_instP],
809
810
811
                   rntiP);

  if (ue_context_p != NULL) {
812
813
814
815
816
817
    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);
818
  }
Florian Kaltenberger's avatar
Florian Kaltenberger committed
819
}
820
821
//------------------------------------------------------------------------------
int
822
mac_UE_get_rrc_status(
823
824
825
826
827
828
829
830
  const module_id_t Mod_idP,
  const uint8_t     indexP
)
//------------------------------------------------------------------------------
{
  return(UE_rrc_inst[Mod_idP].Info[indexP].State);
}

831
//-------------------------------------------------------------------------------------------//
832
833
834
int mac_ue_ccch_success_ind(module_id_t Mod_idP, uint8_t eNB_index)
{
  //-------------------------------------------------------------------------------------------//
835
#if defined(ENABLE_ITTI)
836
837
  {
    MessageDef *message_p;
838

839
840
    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;
841

842
    itti_send_msg_to_task (TASK_RRC_UE, UE_MODULE_ID_TO_INSTANCE(Mod_idP), message_p);
843
  }
844
#else
845
846
  // 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;
847
#endif
848
  return 0;
849
}