f1ap_du_rrc_message_transfer.c 62 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
/*
 * 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.1  (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
 */

/*! \file f1ap_du_rrc_message_transfer.c
 * \brief f1ap rrc message transfer for DU
 * \author EURECOM/NTUST
 * \date 2018
 * \version 0.1
 * \company Eurecom
 * \email: navid.nikaein@eurecom.fr, bing-kai.hong@eurecom.fr
 * \note
 * \warning
 */

#include "f1ap_common.h"
34 35
#include "f1ap_encoder.h"
#include "f1ap_decoder.h"
36
#include "f1ap_itti_messaging.h"
37

38
#include "f1ap_du_rrc_message_transfer.h"
39

40

41 42 43
#include "LTE_DL-CCCH-Message.h"
#include "LTE_DL-DCCH-Message.h"
#include "LTE_UL-DCCH-Message.h"
44

45 46 47 48
#include "NR_DL-CCCH-Message.h"
#include "NR_UL-CCCH-Message.h"
#include "NR_DL-DCCH-Message.h"
#include "NR_UL-DCCH-Message.h"
49 50 51 52
// for SRB1_logicalChannelConfig_defaultValue
#include "rrc_extern.h"
#include "common/ran_context.h"

53
#include "rrc_eNB_UE_context.h"
54
#include "openair2/RRC/NR/rrc_gNB_UE_context.h"
55
#include "asn1_msg.h"
56
#include "intertask_interface.h"
57
#include "LAYER2/NR_MAC_gNB/mac_proto.h"
58

59 60 61 62
// undefine C_RNTI from
// openair1/PHY/LTE_TRANSPORT/transport_common.h which
// replaces in ie->value.choice.C_RNTI, causing
// a compile error
63

64 65 66
#undef C_RNTI 

extern f1ap_setup_req_t *f1ap_du_data;
67
extern RAN_CONTEXT_t RC;
Robert Schmidt's avatar
Robert Schmidt committed
68
extern f1ap_cudu_inst_t f1ap_du_inst[MAX_eNB];
69

Xue Song's avatar
Xue Song committed
70
uint8_t du_ccch_flag = 1;
71

Xue Song's avatar
Xue Song committed
72 73 74 75
int DU_handle_DL_NR_RRC_MESSAGE_TRANSFER(instance_t       instance,
                                         uint32_t         assoc_id,
                                         uint32_t         stream,
                                         F1AP_F1AP_PDU_t *pdu);
76

77
/*  DL RRC Message Transfer */
78 79 80 81
int DU_handle_DL_RRC_MESSAGE_TRANSFER(instance_t       instance,
                                      uint32_t         assoc_id,
                                      uint32_t         stream,
                                      F1AP_F1AP_PDU_t *pdu) {
Xue Song's avatar
Xue Song committed
82 83 84 85 86 87 88

  if (RC.nrrrc[instance]->node_type == ngran_gNB_DU) {
    LOG_I(F1AP, "node is gNB DU, call DU_handle_DL_NR_RRC_MESSAGE_TRANSFER \n");
    DU_handle_DL_NR_RRC_MESSAGE_TRANSFER(instance, assoc_id, stream, pdu);
    return 0;
  }

89
  LOG_D(F1AP, "DU_handle_DL_RRC_MESSAGE_TRANSFER \n");
90 91 92 93 94 95 96 97
  
  F1AP_DLRRCMessageTransfer_t    *container;
  F1AP_DLRRCMessageTransferIEs_t *ie;

  uint64_t        cu_ue_f1ap_id;
  uint64_t        du_ue_f1ap_id;
  uint64_t        srb_id;
  int             executeDuplication;
98
  sdu_size_t      rrc_dl_sdu_len;
99 100
  //uint64_t        subscriberProfileIDforRFP;
  //uint64_t        rAT_FrequencySelectionPriority;
101 102

  DevAssert(pdu != NULL);
103

104 105 106 107 108 109 110 111 112 113 114 115 116
  if (stream != 0) {
    LOG_E(F1AP, "[SCTP %d] Received F1 on stream != 0 (%d)\n",
               assoc_id, stream);
    return -1;
  }

  container = &pdu->choice.initiatingMessage->value.choice.DLRRCMessageTransfer;


  /* GNB_CU_UE_F1AP_ID */
  F1AP_FIND_PROTOCOLIE_BY_ID(F1AP_DLRRCMessageTransferIEs_t, ie, container,
                             F1AP_ProtocolIE_ID_id_gNB_CU_UE_F1AP_ID, true);
  cu_ue_f1ap_id = ie->value.choice.GNB_CU_UE_F1AP_ID;
117
  LOG_D(F1AP, "cu_ue_f1ap_id %lu \n", cu_ue_f1ap_id);
118 119 120 121 122 123


  /* GNB_DU_UE_F1AP_ID */
  F1AP_FIND_PROTOCOLIE_BY_ID(F1AP_DLRRCMessageTransferIEs_t, ie, container,
                             F1AP_ProtocolIE_ID_id_gNB_DU_UE_F1AP_ID, true);
  du_ue_f1ap_id = ie->value.choice.GNB_DU_UE_F1AP_ID;
124 125 126
  LOG_D(F1AP, "du_ue_f1ap_id %lu associated with UE RNTI %x \n",
        du_ue_f1ap_id,
        f1ap_get_rnti_by_du_id(&f1ap_du_inst[instance], du_ue_f1ap_id)); // this should be the one transmitted via initial ul rrc message transfer
127

Robert Schmidt's avatar
Robert Schmidt committed
128
  if (f1ap_du_add_cu_ue_id(&f1ap_du_inst[instance],du_ue_f1ap_id, cu_ue_f1ap_id) < 0 ) {
129
    LOG_E(F1AP, "Failed to find the F1AP UID \n");
130 131
    //return -1;
  }
132

133 134 135 136 137 138 139 140 141 142 143 144
  /* optional */
  /* oldgNB_DU_UE_F1AP_ID */
  if (0) {
    F1AP_FIND_PROTOCOLIE_BY_ID(F1AP_DLRRCMessageTransferIEs_t, ie, container,
                             F1AP_ProtocolIE_ID_id_oldgNB_DU_UE_F1AP_ID, true);
  }

  /* mandatory */
  /* SRBID */
  F1AP_FIND_PROTOCOLIE_BY_ID(F1AP_DLRRCMessageTransferIEs_t, ie, container,
                             F1AP_ProtocolIE_ID_id_SRBID, true);
  srb_id = ie->value.choice.SRBID;
145
  LOG_D(F1AP, "srb_id %lu \n", srb_id);
146 147 148 149 150 151 152

  /* optional */
  /* ExecuteDuplication */
  if (0) {
    F1AP_FIND_PROTOCOLIE_BY_ID(F1AP_DLRRCMessageTransferIEs_t, ie, container,
                             F1AP_ProtocolIE_ID_id_ExecuteDuplication, true);
    executeDuplication = ie->value.choice.ExecuteDuplication;
153
    LOG_D(F1AP, "ExecuteDuplication %d \n", executeDuplication);
154 155 156 157 158 159 160 161 162
  }

  // issue in here
  /* mandatory */
  /* RRC Container */
  F1AP_FIND_PROTOCOLIE_BY_ID(F1AP_DLRRCMessageTransferIEs_t, ie, container,
                             F1AP_ProtocolIE_ID_id_RRCContainer, true);
  // BK: need check
  // create an ITTI message and copy SDU
163

164
  //  message_p = itti_alloc_new_message (TASK_CU_F1, 0, RRC_MAC_CCCH_DATA_IND);
165 166 167 168
  //  memset (RRC_MAC_CCCH_DATA_IND (message_p).sdu, 0, CCCH_SDU_SIZE);
  rrc_dl_sdu_len = ie->value.choice.RRCContainer.size;
  //  memcpy(RRC_MAC_CCCH_DATA_IND (message_p).sdu, ie->value.choice.RRCContainer.buf,
  //         ccch_sdu_len);
169 170 171 172 173

  //LOG_I(F1AP, "%s() RRCContainer size %lu: ", __func__, ie->value.choice.RRCContainer.size);
  //for (int i = 0;i < ie->value.choice.RRCContainer.size; i++)
  //  printf("%02x ", ie->value.choice.RRCContainer.buf[i]);
  //printf("\n");
174 175 176 177 178 179 180 181

  /* optional */
  /* RAT_FrequencyPriorityInformation */
  if (0) {
    F1AP_FIND_PROTOCOLIE_BY_ID(F1AP_DLRRCMessageTransferIEs_t, ie, container,
                             F1AP_ProtocolIE_ID_id_RAT_FrequencyPriorityInformation, true);

    switch(ie->value.choice.RAT_FrequencyPriorityInformation.present) {
Xue Song's avatar
Xue Song committed
182
      case F1AP_RAT_FrequencyPriorityInformation_PR_eNDC:
183
        //subscriberProfileIDforRFP = ie->value.choice.RAT_FrequencyPriorityInformation.choice.subscriberProfileIDforRFP;
184
        break;
Xue Song's avatar
Xue Song committed
185
      case F1AP_RAT_FrequencyPriorityInformation_PR_nGRAN:
186
        //rAT_FrequencySelectionPriority = ie->value.choice.RAT_FrequencyPriorityInformation.choice.rAT_FrequencySelectionPriority;
187
        break;
188
      default:
189
        LOG_W(F1AP, "unhandled IE RAT_FrequencyPriorityInformation.present\n");
190
        break;
191 192 193
    }
  }

194 195 196
  // decode RRC Container and act on the message type
  AssertFatal(srb_id<3,"illegal srb_id\n");

197
  protocol_ctxt_t ctxt;
Robert Schmidt's avatar
Robert Schmidt committed
198
  ctxt.rnti      = f1ap_get_rnti_by_du_id(&f1ap_du_inst[instance], du_ue_f1ap_id);
199 200 201 202
  ctxt.module_id = instance;
  ctxt.instance  = instance;
  ctxt.enb_flag  = 1;

203 204 205 206
 struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(
                                                RC.rrc[ctxt.module_id],
                                                ctxt.rnti);

207
  if (srb_id == 0) {
208
    LTE_DL_CCCH_Message_t* dl_ccch_msg=NULL;
209 210
    asn_dec_rval_t dec_rval;
    dec_rval = uper_decode(NULL,
211
         &asn_DEF_LTE_DL_CCCH_Message,
212 213 214
         (void**)&dl_ccch_msg,
         ie->value.choice.RRCContainer.buf,
         rrc_dl_sdu_len,0,0);
215
    AssertFatal(dec_rval.code == RC_OK, "could not decode F1AP message\n");
216
    switch (dl_ccch_msg->message.choice.c1.present) {
217

218
      case LTE_DL_CCCH_MessageType__c1_PR_NOTHING:
219
        LOG_I(F1AP, "Received PR_NOTHING on DL-CCCH-Message\n");
220 221
        break;

222
      case LTE_DL_CCCH_MessageType__c1_PR_rrcConnectionReestablishment:
223 224
        LOG_I(F1AP,
              "Logical Channel DL-CCCH (SRB0), Received RRCConnectionReestablishment\n");
225 226
        break;

227
      case LTE_DL_CCCH_MessageType__c1_PR_rrcConnectionReestablishmentReject:
228 229
        LOG_I(F1AP,
              "Logical Channel DL-CCCH (SRB0), Received RRCConnectionReestablishmentReject\n");
230 231
        break;

232
      case LTE_DL_CCCH_MessageType__c1_PR_rrcConnectionReject:
233 234
        LOG_I(F1AP,
              "Logical Channel DL-CCCH (SRB0), Received RRCConnectionReject \n");
235 236
        break;

237
      case LTE_DL_CCCH_MessageType__c1_PR_rrcConnectionSetup:
238
      {
239 240 241 242
        LOG_I(F1AP,
              "Logical Channel DL-CCCH (SRB0), Received RRCConnectionSetup DU_ID %lx/RNTI %x\n",
              du_ue_f1ap_id,
              f1ap_get_rnti_by_du_id(&f1ap_du_inst[instance], du_ue_f1ap_id));
243 244
          // Get configuration

245
        LTE_RRCConnectionSetup_t* rrcConnectionSetup = &dl_ccch_msg->message.choice.c1.choice.rrcConnectionSetup;
246
        AssertFatal(rrcConnectionSetup!=NULL,"rrcConnectionSetup is null\n");
247
        LTE_RadioResourceConfigDedicated_t* radioResourceConfigDedicated = &rrcConnectionSetup->criticalExtensions.choice.c1.choice.rrcConnectionSetup_r8.radioResourceConfigDedicated;
248 249

        // get SRB logical channel information
250 251
        LTE_SRB_ToAddModList_t *SRB_configList;
        LTE_SRB_ToAddMod_t *SRB1_config;
Robert Schmidt's avatar
Robert Schmidt committed
252
        LTE_LogicalChannelConfig_t *SRB1_logicalChannelConfig = NULL;
253
        SRB_configList                 = radioResourceConfigDedicated->srb_ToAddModList;
254

255 256 257 258 259 260 261
        AssertFatal(SRB_configList!=NULL,"SRB_configList is null\n");
        for (int cnt = 0; cnt < (SRB_configList)->list.count; cnt++) {
          if ((SRB_configList)->list.array[cnt]->srb_Identity == 1) {
            SRB1_config = (SRB_configList)->list.array[cnt];

            if (SRB1_config->logicalChannelConfig) {
              if (SRB1_config->logicalChannelConfig->present ==
262
                LTE_SRB_ToAddMod__logicalChannelConfig_PR_explicitValue) {
263 264 265 266 267 268 269 270 271 272 273
                SRB1_logicalChannelConfig = &SRB1_config->logicalChannelConfig->choice.explicitValue;
              } else {
                SRB1_logicalChannelConfig = &SRB1_logicalChannelConfig_defaultValue;
              }
            } else {
              SRB1_logicalChannelConfig = &SRB1_logicalChannelConfig_defaultValue;
            }
          }
        } // for
        rrc_rlc_config_asn1_req(&ctxt,
          SRB_configList,
274 275 276
          (LTE_DRB_ToAddModList_t*) NULL,
          (LTE_DRB_ToReleaseList_t*) NULL
          , (LTE_PMCH_InfoList_r9_t *) NULL,
277 278 279 280
          0,0
          );

      // This should be somewhere in the f1ap_cudu_ue_inst_t
281
      /*int macrlc_instance = 0; 
282

Robert Schmidt's avatar
Robert Schmidt committed
283
      rnti_t rnti = f1ap_get_rnti_by_du_id(&f1ap_du_inst[0], du_ue_f1ap_id);
284
      struct rrc_eNB_ue_context_s *ue_context_p = rrc_eNB_get_ue_context(RC.rrc[macrlc_instance],rnti);
285
      */  
286 287 288 289 290
      eNB_RRC_UE_t *ue_p = &ue_context_p->ue_context; 
      AssertFatal(ue_p->Srb0.Active == 1,"SRB0 is not active\n");

      memcpy((void*)ue_p->Srb0.Tx_buffer.Payload,
             (void*)ie->value.choice.RRCContainer.buf,
291
             rrc_dl_sdu_len); // ie->value.choice.RRCContainer.size
292 293 294

      ue_p->Srb0.Tx_buffer.payload_size = rrc_dl_sdu_len;

Robert Schmidt's avatar
Robert Schmidt committed
295 296 297 298
      LTE_MAC_MainConfig_t    *mac_MainConfig  = NULL;
      if (radioResourceConfigDedicated->mac_MainConfig)
        mac_MainConfig = &radioResourceConfigDedicated->mac_MainConfig->choice.explicitValue;

299
      rrc_mac_config_req_eNB(
300
          ctxt.module_id,
301
          0, //primaryCC_id,
302
          0,0,0,0,0,0,
303
          ctxt.rnti,
304 305 306
          (LTE_BCCH_BCH_Message_t *) NULL,
          (LTE_RadioResourceConfigCommonSIB_t *) NULL,
          (LTE_RadioResourceConfigCommonSIB_t *) NULL,
307
          radioResourceConfigDedicated->physicalConfigDedicated,
308
          (LTE_SCellToAddMod_r10_t *)NULL,
309
          //(struct PhysicalConfigDedicatedSCell_r10 *)NULL,
310
          (LTE_MeasObjectToAddMod_t **) NULL,
Robert Schmidt's avatar
Robert Schmidt committed
311
          mac_MainConfig,
312 313 314
          1,
          SRB1_logicalChannelConfig,
          NULL, // measGapConfig,
315
          (LTE_TDD_Config_t *) NULL,
316
          NULL,
317 318
          (LTE_SchedulingInfoList_t *) NULL,
          0, NULL, NULL, (LTE_MBSFN_SubframeConfigList_t *) NULL
319
          , 0, (LTE_MBSFN_AreaInfoList_r9_t *) NULL, (LTE_PMCH_InfoList_r9_t *) NULL,
320
          (LTE_SystemInformationBlockType1_v1310_IEs_t *)NULL
321 322 323 324 325 326
                        ,
                        0,
                        (LTE_BCCH_DL_SCH_Message_MBMS_t *) NULL,
                        (LTE_SchedulingInfo_MBMS_r14_t *) NULL,
                        (struct LTE_NonMBSFN_SubframeConfig_r14 *) NULL,
                        (LTE_SystemInformationBlockType1_MBMS_r14_t *) NULL,
327 328
                        (LTE_MBSFN_AreaInfoList_r9_t *) NULL,
                        (LTE_MBSFNAreaConfiguration_r9_t*) NULL
329 330 331
          );
          break;
      } // case
332

333 334 335 336 337
      default:
        AssertFatal(1==0,
        "Unknown message\n");
        break;
    }// switch case
338
    return(0);
339 340
  } else if (srb_id == 1) { 

341
    LTE_DL_DCCH_Message_t* dl_dcch_msg=NULL;
342 343
    asn_dec_rval_t dec_rval;
    dec_rval = uper_decode(NULL,
344
         &asn_DEF_LTE_DL_DCCH_Message,
345
         (void**)&dl_dcch_msg,
346
         &ie->value.choice.RRCContainer.buf[1], // buf[0] includes the pdcp header
347
         rrc_dl_sdu_len,0,0);
348 349
    
    if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) 
350
      LOG_E(F1AP," Failed to decode DL-DCCH (%zu bytes)\n",dec_rval.consumed);
351
    else
352 353
      LOG_D(F1AP, "Received message: present %d and c1 present %d\n",
            dl_dcch_msg->message.present, dl_dcch_msg->message.choice.c1.present);
354

355
    if (dl_dcch_msg->message.present == LTE_DL_DCCH_MessageType_PR_c1) {
356 357 358
     
      switch (dl_dcch_msg->message.choice.c1.present) {
	
359
      case LTE_DL_DCCH_MessageType__c1_PR_NOTHING:
360
        LOG_I(F1AP, "Received PR_NOTHING on DL-DCCH-Message\n");
361
        return 0;
362
      case LTE_DL_DCCH_MessageType__c1_PR_dlInformationTransfer:
363
        LOG_I(F1AP,"Received NAS DL Information Transfer\n");
364
        break;	
365
      case LTE_DL_DCCH_MessageType__c1_PR_csfbParametersResponseCDMA2000:
366
        LOG_I(F1AP,"Received NAS sfbParametersResponseCDMA2000\n");
367
        break;  
368
      case LTE_DL_DCCH_MessageType__c1_PR_handoverFromEUTRAPreparationRequest:
369
        LOG_I(F1AP,"Received NAS andoverFromEUTRAPreparationRequest\n");
370
        break;  
371
      case LTE_DL_DCCH_MessageType__c1_PR_mobilityFromEUTRACommand:
372
        LOG_I(F1AP,"Received NAS mobilityFromEUTRACommand\n");
373
        break;
374
      case LTE_DL_DCCH_MessageType__c1_PR_rrcConnectionReconfiguration:
375
	     // handle RRCConnectionReconfiguration
376 377 378 379
        LOG_I(F1AP,
              "Logical Channel DL-DCCH (SRB1), Received RRCConnectionReconfiguration DU_ID %lx/RNTI %x\n",
              du_ue_f1ap_id,
              f1ap_get_rnti_by_du_id(&f1ap_du_inst[instance], du_ue_f1ap_id));
380
	
381
        LTE_RRCConnectionReconfiguration_t* rrcConnectionReconfiguration = &dl_dcch_msg->message.choice.c1.choice.rrcConnectionReconfiguration;
382

383
        if (rrcConnectionReconfiguration->criticalExtensions.present == LTE_RRCConnectionReconfiguration__criticalExtensions_PR_c1) {
384
	        if (rrcConnectionReconfiguration->criticalExtensions.choice.c1.present ==
385 386
	         LTE_RRCConnectionReconfiguration__criticalExtensions__c1_PR_rrcConnectionReconfiguration_r8) {
	          LTE_RRCConnectionReconfiguration_r8_IEs_t* rrcConnectionReconfiguration_r8 =
387
	          &rrcConnectionReconfiguration->criticalExtensions.choice.c1.choice.rrcConnectionReconfiguration_r8;
388
	    
389
            if (rrcConnectionReconfiguration_r8->mobilityControlInfo) {
390
	            LOG_I(F1AP, "Mobility Control Information is present\n");
391 392 393
	            AssertFatal(1==0,"Can't handle this yet in DU\n");
	          }
	          if (rrcConnectionReconfiguration_r8->measConfig != NULL) {
394
	            LOG_I(F1AP, "Measurement Configuration is present\n");
395
	          } 
396
	    
397
      	    if (rrcConnectionReconfiguration_r8->radioResourceConfigDedicated) {
398
              LOG_I(F1AP, "Radio Resource Configuration is present\n");
399
      	      uint8_t DRB2LCHAN[8];
400 401
              long drb_id;
              int i;
402 403 404
              LTE_DRB_ToAddModList_t  *DRB_configList  = rrcConnectionReconfiguration_r8->radioResourceConfigDedicated->drb_ToAddModList;
              LTE_SRB_ToAddModList_t  *SRB_configList  = rrcConnectionReconfiguration_r8->radioResourceConfigDedicated->srb_ToAddModList;
              LTE_DRB_ToReleaseList_t *DRB_ReleaseList = rrcConnectionReconfiguration_r8->radioResourceConfigDedicated->drb_ToReleaseList;
Robert Schmidt's avatar
Robert Schmidt committed
405
              LTE_MAC_MainConfig_t    *mac_MainConfig  = NULL;
406
              
407 408 409
              for (i = 0; i< 8; i++){
                DRB2LCHAN[i] = 0;
              }
410

411
              if (rrcConnectionReconfiguration_r8->radioResourceConfigDedicated->mac_MainConfig) {
LouisAdrien's avatar
LouisAdrien committed
412
                LOG_D(F1AP, "MAC Main Configuration is present\n");
413

Robert Schmidt's avatar
Robert Schmidt committed
414
                mac_MainConfig = &rrcConnectionReconfiguration_r8->radioResourceConfigDedicated->mac_MainConfig->choice.explicitValue;
415

416
                /* CDRX Configuration */
417 418 419
                if (mac_MainConfig->drx_Config == NULL) {
                  LOG_W(F1AP, "drx_Configuration parameter is NULL, cannot configure local UE parameters or CDRX is deactivated\n");
                } else {
420
                  MessageDef *message_p = NULL;
421

422
                  /* Send DRX configuration to MAC task to configure timers of local UE context */
423
                  message_p = itti_alloc_new_message(TASK_DU_F1, 0, RRC_MAC_DRX_CONFIG_REQ);
424 425 426
                  RRC_MAC_DRX_CONFIG_REQ(message_p).rnti = ctxt.rnti;
                  RRC_MAC_DRX_CONFIG_REQ(message_p).drx_Configuration = mac_MainConfig->drx_Config;
                  itti_send_msg_to_task(TASK_MAC_ENB, ctxt.module_id, message_p);
LouisAdrien's avatar
LouisAdrien committed
427
                  LOG_D(F1AP, "DRX configured in MAC Main Configuration for RRC Connection Reconfiguration\n");
428
                }
429
                /* End of CDRX configuration */
430
              }
431

432
              LTE_MeasGapConfig_t     *measGapConfig   = NULL;
Robert Schmidt's avatar
Robert Schmidt committed
433
              struct LTE_PhysicalConfigDedicated* physicalConfigDedicated = rrcConnectionReconfiguration_r8->radioResourceConfigDedicated->physicalConfigDedicated;
434 435 436 437
              rrc_rlc_config_asn1_req(
                &ctxt,
                SRB_configList, // NULL,  //LG-RK 14/05/2014 SRB_configList,
                DRB_configList,
438
                DRB_ReleaseList, (LTE_PMCH_InfoList_r9_t *) NULL, 0, 0
439 440 441 442 443 444 445 446 447 448
                );

              if (SRB_configList != NULL) {
                for (i = 0; (i < SRB_configList->list.count) && (i < 3); i++) {
                  if (SRB_configList->list.array[i]->srb_Identity == 1 ){
                    ue_context_p->ue_context.Srb1.Active=1;
                  }
                  else if (SRB_configList->list.array[i]->srb_Identity == 2 )  {
                    ue_context_p->ue_context.Srb2.Active=1;
                    ue_context_p->ue_context.Srb2.Srb_info.Srb_id=2;
449
                    LOG_I(F1AP, "[DU %d] SRB2 is now active\n",ctxt.module_id);
450
                  } else {
451
                    LOG_W(F1AP, "[DU %d] invalide SRB identity %ld\n",ctxt.module_id,
452 453 454 455 456 457 458 459 460
                   SRB_configList->list.array[i]->srb_Identity);
                  }
                }
              }

              if (DRB_configList != NULL) {
                for (i = 0; i < DRB_configList->list.count; i++) {  // num max DRB (11-3-8)
                  if (DRB_configList->list.array[i]) {
                    drb_id = (int)DRB_configList->list.array[i]->drb_Identity;
461 462 463 464 465 466
                    LOG_I(F1AP,
                          "[DU %d] Logical Channel UL-DCCH, Received RRCConnectionReconfiguration for UE rnti %x, reconfiguring DRB %d/LCID %d\n",
                          ctxt.module_id,
                          ctxt.rnti,
                          (int)DRB_configList->list.array[i]->drb_Identity,
                          (int)*DRB_configList->list.array[i]->logicalChannelIdentity);
467 468 469 470 471 472 473 474 475

                  if (ue_context_p->ue_context.DRB_active[drb_id] == 0) {
                    ue_context_p->ue_context.DRB_active[drb_id] = 1;

                    if (DRB_configList->list.array[i]->logicalChannelIdentity) {
                      DRB2LCHAN[i] = (uint8_t) * DRB_configList->list.array[i]->logicalChannelIdentity;
                    }

                    rrc_mac_config_req_eNB(
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
					   ctxt.module_id,
					   0,0,0,0,0,0,
					   0,
					   ue_context_p->ue_context.rnti,
					   (LTE_BCCH_BCH_Message_t *) NULL,
					   (LTE_RadioResourceConfigCommonSIB_t *) NULL,
					   (LTE_RadioResourceConfigCommonSIB_t *) NULL,
					   physicalConfigDedicated,
					   (LTE_SCellToAddMod_r10_t *)NULL,
					   //(struct PhysicalConfigDedicatedSCell_r10 *)NULL,
					   (LTE_MeasObjectToAddMod_t **) NULL,
					   mac_MainConfig,
					   DRB2LCHAN[i],
					   DRB_configList->list.array[i]->logicalChannelConfig,
					   measGapConfig,
					   (LTE_TDD_Config_t *) NULL,
					   NULL,
					   (LTE_SchedulingInfoList_t *) NULL,
					   0, NULL, NULL, (LTE_MBSFN_SubframeConfigList_t *) NULL
					   , 0, (LTE_MBSFN_AreaInfoList_r9_t *) NULL, (LTE_PMCH_InfoList_r9_t *) NULL,
					   (LTE_SystemInformationBlockType1_v1310_IEs_t *)NULL,
					   0,
					   (LTE_BCCH_DL_SCH_Message_MBMS_t *) NULL,
					   (LTE_SchedulingInfo_MBMS_r14_t *) NULL,
					   (struct LTE_NonMBSFN_SubframeConfig_r14 *) NULL,
					   (LTE_SystemInformationBlockType1_MBMS_r14_t *) NULL,
					   (LTE_MBSFN_AreaInfoList_r9_t *) NULL,
					   (LTE_MBSFNAreaConfiguration_r9_t*) NULL
					   );
505
                  }
506

507 508 509 510 511 512 513 514
                } else {        // remove LCHAN from MAC/PHY
                  AssertFatal(1==0,"Can't handle this yet in DU\n");  
                } 
        	     }
        	   }
           }
         }
       }
515
	    break;
516
      case LTE_DL_DCCH_MessageType__c1_PR_rrcConnectionRelease:
517
  	    // handle RRCConnectionRelease
518
        LOG_I(F1AP, "Received RRCConnectionRelease\n");
519
  	    break;
520
      case LTE_DL_DCCH_MessageType__c1_PR_securityModeCommand:
521
        LOG_I(F1AP, "Received securityModeCommand\n");
522
          break; 
523
      case LTE_DL_DCCH_MessageType__c1_PR_ueCapabilityEnquiry:
524
        LOG_I(F1AP, "Received ueCapabilityEnquiry\n");
525
          break;
526 527 528 529 530 531
      case LTE_DL_DCCH_MessageType__c1_PR_counterCheck:
      case LTE_DL_DCCH_MessageType__c1_PR_loggedMeasurementConfiguration_r10:
      case LTE_DL_DCCH_MessageType__c1_PR_rnReconfiguration_r10:
      case LTE_DL_DCCH_MessageType__c1_PR_spare1:
      case LTE_DL_DCCH_MessageType__c1_PR_spare2:
      case LTE_DL_DCCH_MessageType__c1_PR_spare3:
532
  	    break;
533
      case LTE_DL_DCCH_MessageType__c1_PR_ueInformationRequest_r9:
534
        LOG_I(F1AP, "Received ueInformationRequest_r9\n");
535
        break;
536
      case LTE_DL_DCCH_MessageType__c1_PR_rrcConnectionResume_r13:
537
        LOG_I(F1AP, "Received rrcConnectionResume_r13\n");
538 539
	   } 
	 }	
540 541 542 543 544
  }
  else if (srb_id == 2) {
    
  }

545
  LOG_I(F1AP, "Received DL RRC Transfer on srb_id %ld\n", srb_id);
546 547 548 549
  rlc_op_status_t    rlc_status;
  boolean_t          ret             = TRUE;
  mem_block_t       *pdcp_pdu_p      = NULL; 
  pdcp_pdu_p = get_free_mem_block(rrc_dl_sdu_len, __func__);
550

551 552 553 554 555 556 557 558
  //LOG_I(F1AP, "PRRCContainer size %lu:", ie->value.choice.RRCContainer.size);
  //for (int i = 0; i < ie->value.choice.RRCContainer.size; i++)
  //  printf("%02x ", ie->value.choice.RRCContainer.buf[i]);

  //printf (", PDCP PDU size %d:", rrc_dl_sdu_len);
  //for (int i=0;i<rrc_dl_sdu_len;i++) printf("%2x ",pdcp_pdu_p->data[i]);
  //printf("\n");

559

560 561 562
  if (pdcp_pdu_p != NULL) {
    memset(pdcp_pdu_p->data, 0, rrc_dl_sdu_len);
    memcpy(&pdcp_pdu_p->data[0], ie->value.choice.RRCContainer.buf, rrc_dl_sdu_len);
563

564 565 566 567 568 569 570 571 572 573 574 575 576
      rlc_status = rlc_data_req(&ctxt
                                , 1
                                , MBMS_FLAG_NO
                                , srb_id
                                , 0
                                , 0
                                , rrc_dl_sdu_len
                                , pdcp_pdu_p
                                ,NULL
                                ,NULL
                                );
      switch (rlc_status) {
        case RLC_OP_STATUS_OK:
577
          //LOG_I(F1AP, "Data sending request over RLC succeeded!\n");
578 579 580 581
          ret=TRUE;
          break;

        case RLC_OP_STATUS_BAD_PARAMETER:
582
          LOG_W(F1AP, "Data sending request over RLC failed with 'Bad Parameter' reason!\n");
583 584
          ret= FALSE;
          break;
585

586
        case RLC_OP_STATUS_INTERNAL_ERROR:
587
          LOG_W(F1AP, "Data sending request over RLC failed with 'Internal Error' reason!\n");
588 589
          ret= FALSE;
          break;
590

591
        case RLC_OP_STATUS_OUT_OF_RESSOURCES:
592
          LOG_W(F1AP, "Data sending request over RLC failed with 'Out of Resources' reason!\n");
593 594 595 596
          ret= FALSE;
          break;

        default:
597
          LOG_W(F1AP, "RLC returned an unknown status code after PDCP placed the order to send some data (Status Code:%d)\n", rlc_status);
598 599 600 601 602 603
          ret= FALSE;
          break;
      } // switch case
      return ret; 
    } // if pdcp_pdu_p
  
604 605
  return 0;
  
606 607
}

608
int DU_send_UL_RRC_MESSAGE_TRANSFER(instance_t instance, 
609
                                    const f1ap_ul_rrc_message_t *msg) {
610
  const rnti_t rnti = msg->rnti;
611

612 613 614 615
  F1AP_F1AP_PDU_t                pdu;
  F1AP_ULRRCMessageTransfer_t    *out;
  F1AP_ULRRCMessageTransferIEs_t *ie;

616 617
  uint8_t *buffer = NULL;
  uint32_t len;
618

619

620
  LOG_I(F1AP, "[DU %ld] %s: size %d UE RNTI %x in SRB %d\n",
621
        instance, __func__, msg->rrc_container_length, rnti, msg->srb_id);
622

623 624 625 626 627
  //LOG_I(F1AP, "%s() RRCContainer size %d: ", __func__, msg->rrc_container_length);
  //for (int i = 0;i < msg->rrc_container_length; i++)
  //  printf("%02x ", msg->rrc_container[i]);
  //printf("\n");

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
  /* Create */
  /* 0. Message Type */
  memset(&pdu, 0, sizeof(pdu));
  pdu.present = F1AP_F1AP_PDU_PR_initiatingMessage;
  pdu.choice.initiatingMessage = (F1AP_InitiatingMessage_t *)calloc(1, sizeof(F1AP_InitiatingMessage_t));
  pdu.choice.initiatingMessage->procedureCode = F1AP_ProcedureCode_id_ULRRCMessageTransfer;
  pdu.choice.initiatingMessage->criticality   = F1AP_Criticality_ignore;
  pdu.choice.initiatingMessage->value.present = F1AP_InitiatingMessage__value_PR_ULRRCMessageTransfer;
  out = &pdu.choice.initiatingMessage->value.choice.ULRRCMessageTransfer;
  
  /* mandatory */
  /* c1. GNB_CU_UE_F1AP_ID */
  ie = (F1AP_ULRRCMessageTransferIEs_t *)calloc(1, sizeof(F1AP_ULRRCMessageTransferIEs_t));
  ie->id                             = F1AP_ProtocolIE_ID_id_gNB_CU_UE_F1AP_ID;
  ie->criticality                    = F1AP_Criticality_reject;
  ie->value.present                  = F1AP_ULRRCMessageTransferIEs__value_PR_GNB_CU_UE_F1AP_ID;
644

Robert Schmidt's avatar
Robert Schmidt committed
645
  ie->value.choice.GNB_CU_UE_F1AP_ID = f1ap_get_cu_ue_f1ap_id(&f1ap_du_inst[instance], rnti);
646

647 648 649 650 651 652 653 654
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);

  /* mandatory */
  /* c2. GNB_DU_UE_F1AP_ID */
  ie = (F1AP_ULRRCMessageTransferIEs_t *)calloc(1, sizeof(F1AP_ULRRCMessageTransferIEs_t));
  ie->id                             = F1AP_ProtocolIE_ID_id_gNB_DU_UE_F1AP_ID;
  ie->criticality                    = F1AP_Criticality_reject;
  ie->value.present                  = F1AP_ULRRCMessageTransferIEs__value_PR_GNB_DU_UE_F1AP_ID;
Robert Schmidt's avatar
Robert Schmidt committed
655
  ie->value.choice.GNB_DU_UE_F1AP_ID = f1ap_get_du_ue_f1ap_id(&f1ap_du_inst[instance], rnti);
656 657 658 659 660 661 662 663
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);

  /* mandatory */
  /* c3. SRBID */
  ie = (F1AP_ULRRCMessageTransferIEs_t *)calloc(1, sizeof(F1AP_ULRRCMessageTransferIEs_t));
  ie->id                            = F1AP_ProtocolIE_ID_id_SRBID;
  ie->criticality                   = F1AP_Criticality_reject;
  ie->value.present                 = F1AP_ULRRCMessageTransferIEs__value_PR_SRBID;
664
  ie->value.choice.SRBID            = msg->srb_id;
665 666 667 668 669 670 671 672 673
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);

  // issue in here
  /* mandatory */
  /* c4. RRCContainer */
  ie = (F1AP_ULRRCMessageTransferIEs_t *)calloc(1, sizeof(F1AP_ULRRCMessageTransferIEs_t));
  ie->id                            = F1AP_ProtocolIE_ID_id_RRCContainer;
  ie->criticality                   = F1AP_Criticality_reject;
  ie->value.present                 = F1AP_ULRRCMessageTransferIEs__value_PR_RRCContainer;
674 675 676
  OCTET_STRING_fromBuf(&ie->value.choice.RRCContainer,
                       (const char *) msg->rrc_container,
                       msg->rrc_container_length);
677 678
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);

679 680
  if (msg->srb_id == 1 || msg->srb_id == 2) {
    struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_get_ue_context(RC.rrc[instance], rnti);
681 682

   
683
    LTE_UL_DCCH_Message_t* ul_dcch_msg=NULL;
684 685
    asn_dec_rval_t dec_rval;
    dec_rval = uper_decode(NULL,
686
         &asn_DEF_LTE_UL_DCCH_Message,
687 688
         (void**)&ul_dcch_msg,
         &ie->value.choice.RRCContainer.buf[1], // buf[0] includes the pdcp header
689
         msg->rrc_container_length, 0, 0);
690 691
    
    if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) 
692
      LOG_E(F1AP, " Failed to decode UL-DCCH (%zu bytes)\n",dec_rval.consumed);
693
    else
694 695
      LOG_I(F1AP, "Received message: present %d and c1 present %d\n",
            ul_dcch_msg->message.present, ul_dcch_msg->message.choice.c1.present);
696

697
    if (ul_dcch_msg->message.present == LTE_UL_DCCH_MessageType_PR_c1) {
698 699

      switch (ul_dcch_msg->message.choice.c1.present) {
700
      case LTE_UL_DCCH_MessageType__c1_PR_NOTHING:   /* No components present */
701 702
        break;

703
      case LTE_UL_DCCH_MessageType__c1_PR_csfbParametersRequestCDMA2000:
704 705
        break;

706
      case LTE_UL_DCCH_MessageType__c1_PR_measurementReport:
707 708
        break;

709
      case LTE_UL_DCCH_MessageType__c1_PR_rrcConnectionReconfigurationComplete:
710
        LOG_I(F1AP, "[MSG] RRC UL rrcConnectionReconfigurationComplete\n");
711
        
712
        /* CDRX: activated when RRC Connection Reconfiguration Complete is received */
713 714 715
        int UE_id_mac = find_UE_id(instance, rnti);
        
        if (UE_id_mac == -1) {
716
          LOG_E(F1AP, "Can't find UE_id(MAC) of UE rnti %x\n", rnti);
717
          break;
718 719
        }
        
720
        UE_sched_ctrl_t *UE_scheduling_control = &(RC.mac[instance]->UE_info.UE_sched_ctrl[UE_id_mac]);
721
        
722
        if (UE_scheduling_control->cdrx_waiting_ack == TRUE) {
723
          UE_scheduling_control->cdrx_waiting_ack = FALSE;
724
          UE_scheduling_control->cdrx_configured = TRUE; // Set to TRUE when RRC Connection Reconfiguration Complete is received
LouisAdrien's avatar
LouisAdrien committed
725
          LOG_I(F1AP, "CDRX configuration activated after RRC Connection Reconfiguration Complete reception\n");
726
        }
727 728
        /* End of CDRX processing */
        
729 730
        break;

731
      case LTE_UL_DCCH_MessageType__c1_PR_rrcConnectionReestablishmentComplete:
732 733
        break;

734
      case LTE_UL_DCCH_MessageType__c1_PR_rrcConnectionSetupComplete:
735
        LOG_I(F1AP, "[MSG] RRC UL rrcConnectionSetupComplete \n");
736 737
        
        if(!ue_context_p){
738
          LOG_E(F1AP, "Did not find the UE context associated with UE RNTOI %x, ue_context_p is NULL\n", rnti);
739

740
        } else {
741
          LOG_I(F1AP, "Processing RRCConnectionSetupComplete UE %x\n", rnti);
742
          ue_context_p->ue_context.Status = RRC_CONNECTED;
743
        }
744 745
        break;

746
      case LTE_UL_DCCH_MessageType__c1_PR_securityModeComplete:
747
        LOG_I(F1AP, "[MSG] RRC securityModeComplete \n");
748 749
        break;

750
      case LTE_UL_DCCH_MessageType__c1_PR_securityModeFailure:
751 752
        break;

753
      case LTE_UL_DCCH_MessageType__c1_PR_ueCapabilityInformation:
754
        LOG_I(F1AP, "[MSG] RRC ueCapabilityInformation \n");
755 756
        break;

757
      case LTE_UL_DCCH_MessageType__c1_PR_ulHandoverPreparationTransfer:
758 759
        break;

760
      case LTE_UL_DCCH_MessageType__c1_PR_ulInformationTransfer:
761
        LOG_I(F1AP,"[MSG] RRC UL Information Transfer \n");
762 763
        break;

764
      case LTE_UL_DCCH_MessageType__c1_PR_counterCheckResponse:
765 766
        break;

767
      case LTE_UL_DCCH_MessageType__c1_PR_ueInformationResponse_r9:
768 769
        break;

770
      case LTE_UL_DCCH_MessageType__c1_PR_proximityIndication_r9:
771 772
       break;

773
      case LTE_UL_DCCH_MessageType__c1_PR_rnReconfigurationComplete_r10:
774 775
        break;

776
      case LTE_UL_DCCH_MessageType__c1_PR_mbmsCountingResponse_r10:
777
       break;
778

779
      case LTE_UL_DCCH_MessageType__c1_PR_interFreqRSTDMeasurementIndication_r10:
780 781 782 783
       break;
      }
    }
  }
784 785
    /* encode */
  if (f1ap_encode_pdu(&pdu, &buffer, &len) < 0) {
Xue Song's avatar
Xue Song committed
786
    LOG_E(F1AP, "Failed to encode F1 UL RRC MESSAGE TRANSFER\n");
787
    return -1;
788 789
  }

790
  du_f1ap_itti_send_sctp_data_req(instance, f1ap_du_data->assoc_id, buffer, len, f1ap_du_data->default_sctp_stream_id);
791
  return 0;
792 793
}

794

795
/*  UL RRC Message Transfer */
796 797 798 799
int DU_send_INITIAL_UL_RRC_MESSAGE_TRANSFER(module_id_t     module_idP,
                                            int             CC_idP,
                                            int             UE_id,
                                            rnti_t          rntiP,
800
                                            const uint8_t   *sduP,
801 802 803
                                            sdu_size_t      sdu_lenP,
					    const uint8_t   *sdu2P,
					    sdu_size_t      sdu2_lenP) {
804 805 806 807 808 809
  F1AP_F1AP_PDU_t                       pdu;
  F1AP_InitialULRRCMessageTransfer_t    *out;
  F1AP_InitialULRRCMessageTransferIEs_t *ie;

  uint8_t  *buffer;
  uint32_t  len;
Robert Schmidt's avatar
Robert Schmidt committed
810
  int f1ap_uid = f1ap_add_ue (&f1ap_du_inst[module_idP], module_idP, CC_idP,UE_id, rntiP);
811 812

  if (f1ap_uid  < 0 ) {
813
    LOG_E(F1AP, "Failed to add UE \n");
814 815
    return -1;
  }
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833

  /* Create */
  /* 0. Message Type */
  memset(&pdu, 0, sizeof(pdu));
  pdu.present = F1AP_F1AP_PDU_PR_initiatingMessage;
  pdu.choice.initiatingMessage = (F1AP_InitiatingMessage_t *)calloc(1, sizeof(F1AP_InitiatingMessage_t));
  pdu.choice.initiatingMessage->procedureCode = F1AP_ProcedureCode_id_InitialULRRCMessageTransfer;
  pdu.choice.initiatingMessage->criticality   = F1AP_Criticality_ignore;
  pdu.choice.initiatingMessage->value.present = F1AP_InitiatingMessage__value_PR_InitialULRRCMessageTransfer;
  out = &pdu.choice.initiatingMessage->value.choice.InitialULRRCMessageTransfer;
  

  /* mandatory */
  /* c1. GNB_DU_UE_F1AP_ID */
  ie = (F1AP_InitialULRRCMessageTransferIEs_t *)calloc(1, sizeof(F1AP_InitialULRRCMessageTransferIEs_t));
  ie->id                             = F1AP_ProtocolIE_ID_id_gNB_DU_UE_F1AP_ID;
  ie->criticality                    = F1AP_Criticality_reject;
  ie->value.present                  = F1AP_InitialULRRCMessageTransferIEs__value_PR_GNB_DU_UE_F1AP_ID;
Robert Schmidt's avatar
Robert Schmidt committed
834
  ie->value.choice.GNB_DU_UE_F1AP_ID = f1ap_du_inst[module_idP].f1ap_ue[f1ap_uid].du_ue_f1ap_id;
835 836 837 838 839 840 841 842 843 844
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);

  /* mandatory */
  /* c2. NRCGI */
  ie = (F1AP_InitialULRRCMessageTransferIEs_t *)calloc(1, sizeof(F1AP_InitialULRRCMessageTransferIEs_t));
  ie->id                             = F1AP_ProtocolIE_ID_id_NRCGI;
  ie->criticality                    = F1AP_Criticality_reject;
  ie->value.present                  = F1AP_InitialULRRCMessageTransferIEs__value_PR_NRCGI;

  F1AP_NRCGI_t nRCGI;
845
  memset(&nRCGI, 0, sizeof(F1AP_NRCGI_t));
846 847
  MCC_MNC_TO_PLMNID(f1ap_du_data->mcc[0], f1ap_du_data->mnc[0], f1ap_du_data->mnc_digit_length[0],
                                         &nRCGI.pLMN_Identity);
848
  NR_CELL_ID_TO_BIT_STRING(f1ap_du_data->nr_cellid[0], &nRCGI.nRCellIdentity);
849 850 851 852 853 854 855 856 857 858
  ie->value.choice.NRCGI = nRCGI;

  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);

  /* mandatory */
  /* c3. C_RNTI */  // 16
  ie = (F1AP_InitialULRRCMessageTransferIEs_t *)calloc(1, sizeof(F1AP_InitialULRRCMessageTransferIEs_t));
  ie->id                             = F1AP_ProtocolIE_ID_id_C_RNTI;
  ie->criticality                    = F1AP_Criticality_reject;
  ie->value.present                  = F1AP_InitialULRRCMessageTransferIEs__value_PR_C_RNTI;
Xue Song's avatar
Xue Song committed
859
  ie->value.choice.C_RNTI=rntiP;
860 861 862 863 864 865 866 867
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);

  /* mandatory */
  /* c4. RRCContainer */
  ie = (F1AP_InitialULRRCMessageTransferIEs_t *)calloc(1, sizeof(F1AP_InitialULRRCMessageTransferIEs_t));
  ie->id                            = F1AP_ProtocolIE_ID_id_RRCContainer;
  ie->criticality                   = F1AP_Criticality_reject;
  ie->value.present                 = F1AP_InitialULRRCMessageTransferIEs__value_PR_RRCContainer;
Robert Schmidt's avatar
Robert Schmidt committed
868
  OCTET_STRING_fromBuf(&ie->value.choice.RRCContainer, (const char *)sduP, sdu_lenP);
869 870 871 872
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);

  /* optional */
  /* c5. DUtoCURRCContainer */
873
  if (sdu2P) {
874 875 876 877
    ie = (F1AP_InitialULRRCMessageTransferIEs_t *)calloc(1, sizeof(F1AP_InitialULRRCMessageTransferIEs_t));
    ie->id                             = F1AP_ProtocolIE_ID_id_DUtoCURRCContainer;
    ie->criticality                    = F1AP_Criticality_reject;
    ie->value.present                  = F1AP_InitialULRRCMessageTransferIEs__value_PR_DUtoCURRCContainer;
878 879 880
    OCTET_STRING_fromBuf(&ie->value.choice.DUtoCURRCContainer, 
			 sdu2P,
			 sdu2_lenP);
881
    ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
882 883
  }    

884 885 886

    /* encode */
  if (f1ap_encode_pdu(&pdu, &buffer, &len) < 0) {
Xue Song's avatar
Xue Song committed
887
    LOG_E(F1AP, "Failed to encode F1 INITIAL UL RRC MESSAGE TRANSFER\n");
888
    return -1;
889 890
  }

Xue Song's avatar
Xue Song committed
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
  if (RC.nrrrc[module_idP]->node_type == ngran_gNB_DU) {
    struct rrc_gNB_ue_context_s* ue_context_p = rrc_gNB_allocate_new_UE_context(RC.nrrrc[module_idP]);
    ue_context_p->ue_id_rnti                    = rntiP; 
    ue_context_p->ue_context.rnti               = rntiP;
    ue_context_p->ue_context.random_ue_identity = rntiP;
    ue_context_p->ue_context.Srb0.Active        = 1;
    RB_INSERT(rrc_nr_ue_tree_s, &RC.nrrrc[module_idP]->rrc_ue_head, ue_context_p);
  } else {
    struct rrc_eNB_ue_context_s* ue_context_p = rrc_eNB_allocate_new_UE_context(RC.rrc[module_idP]);
    ue_context_p->ue_id_rnti                    = rntiP; 
    ue_context_p->ue_context.rnti               = rntiP;
    ue_context_p->ue_context.random_ue_identity = rntiP;
    ue_context_p->ue_context.Srb0.Active        = 1;
    RB_INSERT(rrc_ue_tree_s, &RC.rrc[module_idP]->rrc_ue_head, ue_context_p);
  }
906 907
  du_f1ap_itti_send_sctp_data_req(module_idP, f1ap_du_data->assoc_id, buffer, len,  f1ap_du_data->default_sctp_stream_id);

908
  return 0;
909
}
910
    
911

912
void init_f1ap_du_ue_inst (void) {
913

Robert Schmidt's avatar
Robert Schmidt committed
914
   memset(f1ap_du_inst, 0, sizeof(f1ap_du_inst));
915 916
}

917 918 919 920 921 922 923 924 925 926 927 928
int DU_send_UL_NR_RRC_MESSAGE_TRANSFER(instance_t instance, 
                                    const f1ap_ul_rrc_message_t *msg) {
  const rnti_t rnti = msg->rnti;

  F1AP_F1AP_PDU_t                pdu;
  F1AP_ULRRCMessageTransfer_t    *out;
  F1AP_ULRRCMessageTransferIEs_t *ie;

  uint8_t *buffer = NULL;
  uint32_t len;


929
  LOG_I(F1AP, "[DU %ld] %s: size %d UE RNTI %x in SRB %d\n",
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
        instance, __func__, msg->rrc_container_length, rnti, msg->srb_id);

  //LOG_I(F1AP, "%s() RRCContainer size %d: ", __func__, msg->rrc_container_length);
  //for (int i = 0;i < msg->rrc_container_length; i++)
  //  printf("%02x ", msg->rrc_container[i]);
  //printf("\n");

  /* Create */
  /* 0. Message Type */
  memset(&pdu, 0, sizeof(pdu));
  pdu.present = F1AP_F1AP_PDU_PR_initiatingMessage;
  pdu.choice.initiatingMessage = (F1AP_InitiatingMessage_t *)calloc(1, sizeof(F1AP_InitiatingMessage_t));
  pdu.choice.initiatingMessage->procedureCode = F1AP_ProcedureCode_id_ULRRCMessageTransfer;
  pdu.choice.initiatingMessage->criticality   = F1AP_Criticality_ignore;
  pdu.choice.initiatingMessage->value.present = F1AP_InitiatingMessage__value_PR_ULRRCMessageTransfer;
  out = &pdu.choice.initiatingMessage->value.choice.ULRRCMessageTransfer;
  
  /* mandatory */
  /* c1. GNB_CU_UE_F1AP_ID */
  ie = (F1AP_ULRRCMessageTransferIEs_t *)calloc(1, sizeof(F1AP_ULRRCMessageTransferIEs_t));
  ie->id                             = F1AP_ProtocolIE_ID_id_gNB_CU_UE_F1AP_ID;
  ie->criticality                    = F1AP_Criticality_reject;
  ie->value.present                  = F1AP_ULRRCMessageTransferIEs__value_PR_GNB_CU_UE_F1AP_ID;

  ie->value.choice.GNB_CU_UE_F1AP_ID = f1ap_get_cu_ue_f1ap_id(&f1ap_du_inst[instance], rnti);

  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);

  /* mandatory */
  /* c2. GNB_DU_UE_F1AP_ID */
  ie = (F1AP_ULRRCMessageTransferIEs_t *)calloc(1, sizeof(F1AP_ULRRCMessageTransferIEs_t));
  ie->id                             = F1AP_ProtocolIE_ID_id_gNB_DU_UE_F1AP_ID;
  ie->criticality                    = F1AP_Criticality_reject;
  ie->value.present                  = F1AP_ULRRCMessageTransferIEs__value_PR_GNB_DU_UE_F1AP_ID;
  ie->value.choice.GNB_DU_UE_F1AP_ID = f1ap_get_du_ue_f1ap_id(&f1ap_du_inst[instance], rnti);
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);

  /* mandatory */
  /* c3. SRBID */
  ie = (F1AP_ULRRCMessageTransferIEs_t *)calloc(1, sizeof(F1AP_ULRRCMessageTransferIEs_t));
  ie->id                            = F1AP_ProtocolIE_ID_id_SRBID;
  ie->criticality                   = F1AP_Criticality_reject;
  ie->value.present                 = F1AP_ULRRCMessageTransferIEs__value_PR_SRBID;
  ie->value.choice.SRBID            = msg->srb_id;
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);

  // issue in here
  /* mandatory */
  /* c4. RRCContainer */
  ie = (F1AP_ULRRCMessageTransferIEs_t *)calloc(1, sizeof(F1AP_ULRRCMessageTransferIEs_t));
  ie->id                            = F1AP_ProtocolIE_ID_id_RRCContainer;
  ie->criticality                   = F1AP_Criticality_reject;
  ie->value.present                 = F1AP_ULRRCMessageTransferIEs__value_PR_RRCContainer;
  OCTET_STRING_fromBuf(&ie->value.choice.RRCContainer,
                       (const char *) msg->rrc_container,
                       msg->rrc_container_length);
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);

  if (msg->srb_id == 1 || msg->srb_id == 2) {
    struct rrc_gNB_ue_context_s* ue_context_p = rrc_gNB_get_ue_context(RC.nrrrc[instance], rnti);

   
    NR_UL_DCCH_Message_t* ul_dcch_msg=NULL;
    asn_dec_rval_t dec_rval;
    dec_rval = uper_decode(NULL,
995
         &asn_DEF_NR_UL_DCCH_Message,
996 997 998 999
         (void**)&ul_dcch_msg,
         &ie->value.choice.RRCContainer.buf[1], // buf[0] includes the pdcp header
         msg->rrc_container_length, 0, 0);
    
Xue Song's avatar
Xue Song committed
1000
    if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) {
1001
      LOG_E(F1AP, " Failed to decode UL-DCCH (%zu bytes)\n",dec_rval.consumed);
Xue Song's avatar
Xue Song committed
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
      /* for rfsim, because UE send RRCSetupRequest in SRB1 */
      // NR_UL_CCCH_Message_t *ul_ccch_msg;
      // dec_rval = uper_decode(NULL,
      //    &asn_DEF_NR_UL_CCCH_Message,
      //    (void**)&ul_ccch_msg,
      //    &ie->value.choice.RRCContainer.buf[1], // buf[0] includes the pdcp header
      //    msg->rrc_container_length, 0, 0);
      // if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) {
      //   LOG_E(F1AP, " Failed to decode UL-CCCH (%zu bytes)\n",dec_rval.consumed);
      // } else {
      //   LOG_I(F1AP, "decode UL-CCCH success \n");
      //   LOG_I(F1AP, "Received message: present %d and c1 present %d\n",
      //       ul_ccch_msg->message.present, ul_ccch_msg->message.choice.c1->present);

      //   if (ul_ccch_msg->message.present == NR_UL_CCCH_MessageType_PR_c1) {
      //     if (ul_ccch_msg->message.choice.c1->present == NR_UL_CCCH_MessageType__c1_PR_rrcSetupRequest) {
      //       LOG_I(F1AP, "[MSG] RRC Setup Request\n");

      //     }
      //   }
      // }
    }
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 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
    else
      LOG_I(F1AP, "Received message: present %d and c1 present %d\n",
            ul_dcch_msg->message.present, ul_dcch_msg->message.choice.c1->present);

    if (ul_dcch_msg->message.present == NR_UL_DCCH_MessageType_PR_c1) {

      switch (ul_dcch_msg->message.choice.c1->present) {
      case NR_UL_DCCH_MessageType__c1_PR_NOTHING:   /* No components present */
        break;

      case NR_UL_DCCH_MessageType__c1_PR_measurementReport:
        break;

      case NR_UL_DCCH_MessageType__c1_PR_rrcReconfigurationComplete:
        LOG_I(F1AP, "[MSG] RRC UL rrcReconfigurationComplete\n");
        
        /* CDRX: activated when RRC Connection Reconfiguration Complete is received */
#if(0)
        int UE_id_mac = find_nr_UE_id(instance, rnti);
        
        if (UE_id_mac == -1) {
          LOG_E(F1AP, "Can't find UE_id(MAC) of UE rnti %x\n", rnti);
          break;
        }
        
        UE_sched_ctrl_t *UE_scheduling_control = &(RC.nrmac[instance]->UE_info.UE_sched_ctrl[UE_id_mac]);
        
        if (UE_scheduling_control->cdrx_waiting_ack == TRUE) {
          UE_scheduling_control->cdrx_waiting_ack = FALSE;
          UE_scheduling_control->cdrx_configured = TRUE; // Set to TRUE when RRC Connection Reconfiguration Complete is received
          LOG_I(F1AP, "CDRX configuration activated after RRC Connection Reconfiguration Complete reception\n");
        }
        /* End of CDRX processing */
#endif
        break;

      case NR_UL_DCCH_MessageType__c1_PR_rrcSetupComplete:
        LOG_I(F1AP, "[MSG] RRC UL rrcSetupComplete \n");
        
        if(!ue_context_p){
          LOG_E(F1AP, "Did not find the UE context associated with UE RNTOI %x, ue_context_p is NULL\n", rnti);

        } else {
          LOG_I(F1AP, "Processing RRCSetupComplete UE %x\n", rnti);
          ue_context_p->ue_context.Status = NR_RRC_CONNECTED;
        }
        break;

      case NR_UL_DCCH_MessageType__c1_PR_rrcReestablishmentComplete:
        LOG_I(F1AP, "[MSG] RRC ReestablishmentComplete \n");
        break;

      case NR_UL_DCCH_MessageType__c1_PR_rrcResumeComplete:
        LOG_I(F1AP, "[MSG] RRC ResumeComplete \n");
        break;

      case NR_UL_DCCH_MessageType__c1_PR_securityModeComplete:
        LOG_I(F1AP, "[MSG] RRC securityModeComplete \n");
        break;

      case NR_UL_DCCH_MessageType__c1_PR_securityModeFailure:
        LOG_I(F1AP, "[MSG] RRC securityModeFailure \n");
        break;

      case NR_UL_DCCH_MessageType__c1_PR_ulInformationTransfer:
        LOG_I(F1AP, "[MSG] RRC UL Information Transfer \n");
        break;

      case NR_UL_DCCH_MessageType__c1_PR_locationMeasurementIndication:
        break;

      case NR_UL_DCCH_MessageType__c1_PR_ueCapabilityInformation:
        LOG_I(F1AP, "[MSG] RRC ueCapabilityInformation \n");
        break;

      case NR_UL_DCCH_MessageType__c1_PR_ueAssistanceInformation:
        break;

      case NR_UL_DCCH_MessageType__c1_PR_failureInformation:
       break;

      case NR_UL_DCCH_MessageType__c1_PR_scgFailureInformation:
        break;

      case NR_UL_DCCH_MessageType__c1_PR_scgFailureInformationEUTRA:
       break;

      default:
        LOG_E(NR_RRC, "Unknown UL DCCH message type, present %d \n", ul_dcch_msg->message.choice.c1->present);
       break;
      }
    }
  }
    /* encode */
  if (f1ap_encode_pdu(&pdu, &buffer, &len) < 0) {
    LOG_E(F1AP, "Failed to encode F1 UL RRC MESSAGE TRANSFER \n");
    return -1;
  }

  du_f1ap_itti_send_sctp_data_req(instance, f1ap_du_data->assoc_id, buffer, len, f1ap_du_data->default_sctp_stream_id);
  return 0;
}
1126

1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 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 1219 1220 1221 1222 1223 1224
/*  DL NR RRC Message Transfer */
int DU_handle_DL_NR_RRC_MESSAGE_TRANSFER(instance_t       instance,
                                         uint32_t         assoc_id,
                                         uint32_t         stream,
                                         F1AP_F1AP_PDU_t *pdu) {
  LOG_D(F1AP, "DU_handle_DL_NR_RRC_MESSAGE_TRANSFER \n");
  
  F1AP_DLRRCMessageTransfer_t    *container;
  F1AP_DLRRCMessageTransferIEs_t *ie;

  uint64_t        cu_ue_f1ap_id;
  uint64_t        du_ue_f1ap_id;
  uint64_t        srb_id;
  int             executeDuplication;
  sdu_size_t      rrc_dl_sdu_len;
  //uint64_t        subscriberProfileIDforRFP;
  //uint64_t        rAT_FrequencySelectionPriority;

  DevAssert(pdu != NULL);

  if (stream != 0) {
    LOG_E(F1AP, "[SCTP %d] Received F1 on stream != 0 (%d)\n",
               assoc_id, stream);
    return -1;
  }

  container = &pdu->choice.initiatingMessage->value.choice.DLRRCMessageTransfer;


  /* GNB_CU_UE_F1AP_ID */
  F1AP_FIND_PROTOCOLIE_BY_ID(F1AP_DLRRCMessageTransferIEs_t, ie, container,
                             F1AP_ProtocolIE_ID_id_gNB_CU_UE_F1AP_ID, true);
  cu_ue_f1ap_id = ie->value.choice.GNB_CU_UE_F1AP_ID;
  LOG_D(F1AP, "cu_ue_f1ap_id %lu \n", cu_ue_f1ap_id);


  /* GNB_DU_UE_F1AP_ID */
  F1AP_FIND_PROTOCOLIE_BY_ID(F1AP_DLRRCMessageTransferIEs_t, ie, container,
                             F1AP_ProtocolIE_ID_id_gNB_DU_UE_F1AP_ID, true);
  du_ue_f1ap_id = ie->value.choice.GNB_DU_UE_F1AP_ID;
  LOG_D(F1AP, "du_ue_f1ap_id %lu associated with UE RNTI %x \n",
        du_ue_f1ap_id,
        f1ap_get_rnti_by_du_id(&f1ap_du_inst[instance], du_ue_f1ap_id)); // this should be the one transmitted via initial ul rrc message transfer

  if (f1ap_du_add_cu_ue_id(&f1ap_du_inst[instance],du_ue_f1ap_id, cu_ue_f1ap_id) < 0 ) {
    LOG_E(F1AP, "Failed to find the F1AP UID \n");
    //return -1;
  }

  /* optional */
  /* oldgNB_DU_UE_F1AP_ID */
  if (0) {
    F1AP_FIND_PROTOCOLIE_BY_ID(F1AP_DLRRCMessageTransferIEs_t, ie, container,
                             F1AP_ProtocolIE_ID_id_oldgNB_DU_UE_F1AP_ID, true);
  }

  /* mandatory */
  /* SRBID */
  F1AP_FIND_PROTOCOLIE_BY_ID(F1AP_DLRRCMessageTransferIEs_t, ie, container,
                             F1AP_ProtocolIE_ID_id_SRBID, true);
  srb_id = ie->value.choice.SRBID;
  LOG_D(F1AP, "srb_id %lu \n", srb_id);

  /* optional */
  /* ExecuteDuplication */
  if (0) {
    F1AP_FIND_PROTOCOLIE_BY_ID(F1AP_DLRRCMessageTransferIEs_t, ie, container,
                             F1AP_ProtocolIE_ID_id_ExecuteDuplication, true);
    executeDuplication = ie->value.choice.ExecuteDuplication;
    LOG_D(F1AP, "ExecuteDuplication %d \n", executeDuplication);
  }

  // issue in here
  /* mandatory */
  /* RRC Container */
  F1AP_FIND_PROTOCOLIE_BY_ID(F1AP_DLRRCMessageTransferIEs_t, ie, container,
                             F1AP_ProtocolIE_ID_id_RRCContainer, true);
  // BK: need check
  // create an ITTI message and copy SDU

  //  message_p = itti_alloc_new_message (TASK_CU_F1, RRC_MAC_CCCH_DATA_IND);
  //  memset (RRC_MAC_CCCH_DATA_IND (message_p).sdu, 0, CCCH_SDU_SIZE);
  rrc_dl_sdu_len = ie->value.choice.RRCContainer.size;
  //  memcpy(RRC_MAC_CCCH_DATA_IND (message_p).sdu, ie->value.choice.RRCContainer.buf,
  //         ccch_sdu_len);

  //LOG_I(F1AP, "%s() RRCContainer size %lu: ", __func__, ie->value.choice.RRCContainer.size);
  //for (int i = 0;i < ie->value.choice.RRCContainer.size; i++)
  //  printf("%02x ", ie->value.choice.RRCContainer.buf[i]);
  //printf("\n");

  /* optional */
  /* RAT_FrequencyPriorityInformation */
  if (0) {
    F1AP_FIND_PROTOCOLIE_BY_ID(F1AP_DLRRCMessageTransferIEs_t, ie, container,
                             F1AP_ProtocolIE_ID_id_RAT_FrequencyPriorityInformation, true);

    switch(ie->value.choice.RAT_FrequencyPriorityInformation.present) {
Xue Song's avatar
Xue Song committed
1225
      case F1AP_RAT_FrequencyPriorityInformation_PR_eNDC:
1226 1227
        //subscriberProfileIDforRFP = ie->value.choice.RAT_FrequencyPriorityInformation.choice.subscriberProfileIDforRFP;
        break;
Xue Song's avatar
Xue Song committed
1228
      case F1AP_RAT_FrequencyPriorityInformation_PR_nGRAN:
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
        //rAT_FrequencySelectionPriority = ie->value.choice.RAT_FrequencyPriorityInformation.choice.rAT_FrequencySelectionPriority;
        break;
      default:
        LOG_W(F1AP, "unhandled IE RAT_FrequencyPriorityInformation.present\n");
        break;
    }
  }

  // decode RRC Container and act on the message type
  AssertFatal(srb_id<3,"illegal srb_id\n");

  protocol_ctxt_t ctxt;
  ctxt.rnti      = f1ap_get_rnti_by_du_id(&f1ap_du_inst[instance], du_ue_f1ap_id);
  ctxt.module_id = instance;
  ctxt.instance  = instance;
  ctxt.enb_flag  = 1;

1246 1247 1248
  struct rrc_gNB_ue_context_s* ue_context_p = rrc_gNB_get_ue_context(
								     RC.nrrrc[ctxt.module_id],
								     ctxt.rnti);
1249

1250
  gNB_RRC_INST *rrc = &RC.nrrrc[ctxt.module_id];
Xue Song's avatar
Xue Song committed
1251
  if (srb_id == 0) {
1252 1253 1254 1255 1256 1257 1258
    NR_DL_CCCH_Message_t* dl_ccch_msg=NULL;
    asn_dec_rval_t dec_rval;
    dec_rval = uper_decode(NULL,
                           &asn_DEF_NR_DL_CCCH_Message,
			   (void**)&dl_ccch_msg,
			   ie->value.choice.RRCContainer.buf,
			   rrc_dl_sdu_len,0,0);
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
    AssertFatal(dec_rval.code == RC_OK, "could not decode F1AP message\n");
    switch (dl_ccch_msg->message.choice.c1->present) {

      case NR_DL_CCCH_MessageType__c1_PR_NOTHING:
        LOG_I(F1AP, "Received PR_NOTHING on DL-CCCH-Message\n");
        break;

      case NR_DL_CCCH_MessageType__c1_PR_rrcReject:
        LOG_I(F1AP,
              "Logical Channel DL-CCCH (SRB0), Received RRCReject\n");
        break;

      case NR_DL_CCCH_MessageType__c1_PR_rrcSetup:
      {
        LOG_I(F1AP,
              "Logical Channel DL-CCCH (SRB0), Received RRCSetup DU_ID %lx/RNTI %x\n",
              du_ue_f1ap_id,
              f1ap_get_rnti_by_du_id(&f1ap_du_inst[instance], du_ue_f1ap_id));
          // Get configuration

        NR_RRCSetup_t* rrcSetup = dl_ccch_msg->message.choice.c1->choice.rrcSetup;
        AssertFatal(rrcSetup!=NULL, "rrcSetup is null\n");
        NR_RRCSetup_IEs_t *rrcSetup_ies = rrcSetup->criticalExtensions.choice.rrcSetup;

1283
	ue_context_p->ue_context.SRB_configList = rrcSetup_ies->radioBearerConfig.srb_ToAddModList;
1284
        AssertFatal(rrcSetup_ies->masterCellGroup.buf!=NULL,"masterCellGroup is null\n");
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
	asn_dec_rval_t dec_rval;
	dec_rval = uper_decode(NULL,
			       &asn_DEF_NR_CellGroupConfig,
			       (void**)&ue_context_p->ue_context.masterCellGroup,
			       rrcSetup_ies->masterCellGroup.buf,
			       rrcSetup_ies->masterCellGroup.size,0,0);
	AssertFatal(dec_rval.code == RC_OK, "could not decode masterCellGroup\n");

      // configure MAC
	rrc_mac_config_req_gNB(ctxt.module_id,
			       rrc->carrier.ssb_SubcarrierOffset,
			       rrc->carrier.pdsch_AntennaPorts,
1297
			       rrc->carrier.pusch_AntennaPorts,
1298
			       NULL,
1299 1300 1301 1302
			       0,
			       ue_context_p->ue_context.rnti,
			       ue_context_p->ue_context.masterCellGroup
			       );
1303
        // rrc_rlc_config_asn1_req
1304 1305 1306 1307 1308 1309 1310
	nr_rrc_rlc_config_asn1_req(&ctxt,
				   ue_context_p->ue_context.SRB_configList, 
				   NULL,
				   NULL,
				   NULL,
				   NULL,
				   NULL);
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
      // This should be somewhere in the f1ap_cudu_ue_inst_t
      /*int macrlc_instance = 0; 

      rnti_t rnti = f1ap_get_rnti_by_du_id(&f1ap_du_inst[0], du_ue_f1ap_id);
      struct rrc_gNB_ue_context_s *ue_context_p = rrc_gNB_get_ue_context(RC.nrrrc[macrlc_instance],rnti);
      */  
      gNB_RRC_UE_t *ue_p = &ue_context_p->ue_context; 
      AssertFatal(ue_p->Srb0.Active == 1,"SRB0 is not active\n");

      memcpy((void*)ue_p->Srb0.Tx_buffer.Payload,
             (void*)ie->value.choice.RRCContainer.buf,
             rrc_dl_sdu_len); // ie->value.choice.RRCContainer.size

      ue_p->Srb0.Tx_buffer.payload_size = rrc_dl_sdu_len;

Xue Song's avatar
Xue Song committed
1326 1327 1328
      // NR_MAC_CellGroupConfig_t *mac_CellGroupConfig  = NULL;
      // if (cellGroupConfig->mac_CellGroupConfig)
      //   mac_CellGroupConfig = cellGroupConfig->mac_CellGroupConfig;
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348

      // rrc_mac_config_req_gNB
          break;
      } // case

      case NR_DL_CCCH_MessageType__c1_PR_spare2:
        LOG_I(F1AP,
              "Logical Channel DL-CCCH (SRB0), Received spare2\n");
        break;

      case NR_DL_CCCH_MessageType__c1_PR_spare1:
        LOG_I(F1AP,
              "Logical Channel DL-CCCH (SRB0), Received spare1\n");
        break;

      default:
        AssertFatal(1==0,
        "Unknown message\n");
        break;
    }// switch case
1349
    return(0);
1350 1351 1352 1353 1354 1355 1356
  } else if (srb_id == 1) { 

    NR_DL_DCCH_Message_t* dl_dcch_msg=NULL;
    asn_dec_rval_t dec_rval;
    dec_rval = uper_decode(NULL,
         &asn_DEF_NR_DL_DCCH_Message,
         (void**)&dl_dcch_msg,
Xue Song's avatar
Xue Song committed
1357 1358
         &ie->value.choice.RRCContainer.buf[2], // buf[0] includes the pdcp header
         rrc_dl_sdu_len-6,0,0);
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378

    if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) 
      LOG_E(F1AP," Failed to decode DL-DCCH (%zu bytes)\n",dec_rval.consumed);
    else
      LOG_D(F1AP, "Received message: present %d and c1 present %d\n",
            dl_dcch_msg->message.present, dl_dcch_msg->message.choice.c1->present);

    if (dl_dcch_msg->message.present == NR_DL_DCCH_MessageType_PR_c1) {
      switch (dl_dcch_msg->message.choice.c1->present) {
        case NR_DL_DCCH_MessageType__c1_PR_NOTHING:
          LOG_I(F1AP, "Received PR_NOTHING on DL-DCCH-Message\n");
          return 0;

        case NR_DL_DCCH_MessageType__c1_PR_rrcReconfiguration:
        // handle RRCReconfiguration
          LOG_I(F1AP,
                "Logical Channel DL-DCCH (SRB1), Received RRCReconfiguration DU_ID %lx/RNTI %x\n",
                du_ue_f1ap_id,
                f1ap_get_rnti_by_du_id(&f1ap_du_inst[instance], du_ue_f1ap_id));
    
Xue Song's avatar
Xue Song committed
1379
          NR_RRCReconfiguration_t* rrcReconfiguration = dl_dcch_msg->message.choice.c1->choice.rrcReconfiguration;
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394

          if (rrcReconfiguration->criticalExtensions.present == NR_RRCReconfiguration__criticalExtensions_PR_rrcReconfiguration) {
            NR_RRCReconfiguration_IEs_t* rrcReconfiguration_ies =
              rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration;

            if (rrcReconfiguration_ies->measConfig != NULL) {
              LOG_I(F1AP, "Measurement Configuration is present\n");
            }

              if (rrcReconfiguration_ies->radioBearerConfig) {
                LOG_I(F1AP, "Radio Resource Configuration is present\n");
                long drb_id;
                int i;
                NR_DRB_ToAddModList_t  *DRB_configList  = rrcReconfiguration_ies->radioBearerConfig->drb_ToAddModList;
                NR_SRB_ToAddModList_t  *SRB_configList  = rrcReconfiguration_ies->radioBearerConfig->srb_ToAddModList;
Xue Song's avatar
Xue Song committed
1395
                // NR_DRB_ToReleaseList_t *DRB_ReleaseList = rrcReconfiguration_ies->radioBearerConfig->drb_ToReleaseList;
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 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

                // rrc_rlc_config_asn1_req

                if (SRB_configList != NULL) {
                  for (i = 0; (i < SRB_configList->list.count) && (i < 3); i++) {
                    if (SRB_configList->list.array[i]->srb_Identity == 1 ){
                      ue_context_p->ue_context.Srb1.Active=1;
                    }
                    else if (SRB_configList->list.array[i]->srb_Identity == 2 )  {
                      ue_context_p->ue_context.Srb2.Active=1;
                      ue_context_p->ue_context.Srb2.Srb_info.Srb_id=2;
                      LOG_I(F1AP, "[DU %d] SRB2 is now active\n",ctxt.module_id);
                    } else {
                      LOG_W(F1AP, "[DU %d] invalide SRB identity %ld\n",ctxt.module_id,
                      SRB_configList->list.array[i]->srb_Identity);
                    }
                  }
                }

                if (DRB_configList != NULL) {
                  for (i = 0; i < DRB_configList->list.count; i++) {  // num max DRB (11-3-8)
                    if (DRB_configList->list.array[i]) {
                      drb_id = (int)DRB_configList->list.array[i]->drb_Identity;
                      LOG_I(F1AP,
                            "[DU %d] Logical Channel UL-DCCH, Received RRCConnectionReconfiguration for UE rnti %x, reconfiguring DRB %d\n",
                            ctxt.module_id,
                            ctxt.rnti,
                            (int)DRB_configList->list.array[i]->drb_Identity);
                            // (int)*DRB_configList->list.array[i]->logicalChannelIdentity);

                    if (ue_context_p->ue_context.DRB_active[drb_id] == 0) {
                      ue_context_p->ue_context.DRB_active[drb_id] = 1;

                      // logicalChannelIdentity
                      // rrc_mac_config_req_eNB
                    }

                  } else {        // remove LCHAN from MAC/PHY
                    AssertFatal(1==0,"Can't handle this yet in DU\n");  
                  } 
                }
              }
            }
        }
        break;
      case NR_DL_DCCH_MessageType__c1_PR_rrcResume:
        LOG_I(F1AP,"Received rrcResume\n");
        break;	
      case NR_DL_DCCH_MessageType__c1_PR_rrcRelease:
        LOG_I(F1AP,"Received rrcRelease\n");
        break;  
      case NR_DL_DCCH_MessageType__c1_PR_rrcReestablishment:
        LOG_I(F1AP,"Received rrcReestablishment\n");
        break;  
      case NR_DL_DCCH_MessageType__c1_PR_securityModeCommand:
        LOG_I(F1AP,"Received securityModeCommand\n");
        break;
      case NR_DL_DCCH_MessageType__c1_PR_dlInformationTransfer:
        LOG_I(F1AP, "Received dlInformationTransfer\n");
  	    break;
      case NR_DL_DCCH_MessageType__c1_PR_ueCapabilityEnquiry:
        LOG_I(F1AP, "Received ueCapabilityEnquiry\n");
          break; 
      case NR_DL_DCCH_MessageType__c1_PR_counterCheck:
        LOG_I(F1AP, "Received counterCheck\n");
          break;
      case NR_DL_DCCH_MessageType__c1_PR_mobilityFromNRCommand:
      case NR_DL_DCCH_MessageType__c1_PR_dlDedicatedMessageSegment_r16:
      case NR_DL_DCCH_MessageType__c1_PR_ueInformationRequest_r16:
      case NR_DL_DCCH_MessageType__c1_PR_dlInformationTransferMRDC_r16:
      case NR_DL_DCCH_MessageType__c1_PR_loggedMeasurementConfiguration_r16:
      case NR_DL_DCCH_MessageType__c1_PR_spare3:
      case NR_DL_DCCH_MessageType__c1_PR_spare2:
      case NR_DL_DCCH_MessageType__c1_PR_spare1:
        break;
    }
   }
  }
  else if (srb_id == 2) {
    // TODO
  }

  LOG_I(F1AP, "Received DL RRC Transfer on srb_id %ld\n", srb_id);
Xue Song's avatar
Xue Song committed
1479

1480

Xue Song's avatar
Xue Song committed
1481 1482
//   rlc_op_status_t    rlc_status;
//   boolean_t          ret             = TRUE;
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
  mem_block_t       *pdcp_pdu_p      = NULL; 
  pdcp_pdu_p = get_free_mem_block(rrc_dl_sdu_len, __func__);

  //LOG_I(F1AP, "PRRCContainer size %lu:", ie->value.choice.RRCContainer.size);
  //for (int i = 0; i < ie->value.choice.RRCContainer.size; i++)
  //  printf("%02x ", ie->value.choice.RRCContainer.buf[i]);

  //printf (", PDCP PDU size %d:", rrc_dl_sdu_len);
  //for (int i=0;i<rrc_dl_sdu_len;i++) printf("%2x ",pdcp_pdu_p->data[i]);
  //printf("\n");

  if (pdcp_pdu_p != NULL) {
    memset(pdcp_pdu_p->data, 0, rrc_dl_sdu_len);
    memcpy(&pdcp_pdu_p->data[0], ie->value.choice.RRCContainer.buf, rrc_dl_sdu_len);

Xue Song's avatar
Xue Song committed
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
    /* for rfsim */
    du_rlc_data_req(&ctxt, 1, 0x00, 1, 1, 0, rrc_dl_sdu_len, pdcp_pdu_p);
    //   rlc_status = rlc_data_req(&ctxt
    //                             , 1
    //                             , MBMS_FLAG_NO
    //                             , srb_id
    //                             , 0
    //                             , 0
    //                             , rrc_dl_sdu_len
    //                             , pdcp_pdu_p
    //                             ,NULL
    //                             ,NULL
    //                             );
    //   switch (rlc_status) {
    //     case RLC_OP_STATUS_OK:
    //       //LOG_I(F1AP, "Data sending request over RLC succeeded!\n");
    //       ret=TRUE;
    //       break;

    //     case RLC_OP_STATUS_BAD_PARAMETER:
    //       LOG_W(F1AP, "Data sending request over RLC failed with 'Bad Parameter' reason!\n");
    //       ret= FALSE;
    //       break;

    //     case RLC_OP_STATUS_INTERNAL_ERROR:
    //       LOG_W(F1AP, "Data sending request over RLC failed with 'Internal Error' reason!\n");
    //       ret= FALSE;
    //       break;

    //     case RLC_OP_STATUS_OUT_OF_RESSOURCES:
    //       LOG_W(F1AP, "Data sending request over RLC failed with 'Out of Resources' reason!\n");
    //       ret= FALSE;
    //       break;

    //     default:
    //       LOG_W(F1AP, "RLC returned an unknown status code after PDCP placed the order to send some data (Status Code:%d)\n", rlc_status);
    //       ret= FALSE;
    //       break;
    //   } // switch case
    //   return ret; 
1538
    } // if pdcp_pdu_p
Xue Song's avatar
Xue Song committed
1539

1540 1541
  return 0;
}