rrc_gNB.c 144 KB
Newer Older
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
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
/*
 * 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 rrc_gNB.c
 * \brief rrc procedures for gNB
 * \author Navid Nikaein and  Raymond Knopp , WEI-TAI CHEN
 * \date 2011 - 2014 , 2018
 * \version 1.0
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
27 28
 * \company Eurecom, NTUST
 * \email: navid.nikaein@eurecom.fr and raymond.knopp@eurecom.fr, kroempa@gmail.com
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
29
 */
30
#define RRC_GNB_C
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
31 32
#define RRC_GNB_C

33
#include "nr_rrc_config.h"
34 35
#include "nr_rrc_defs.h"
#include "nr_rrc_extern.h"
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
36 37 38 39 40 41
#include "assertions.h"
#include "common/ran_context.h"
#include "asn1_conversions.h"

#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
#include "LAYER2/RLC/rlc.h"
42
#include "LAYER2/NR_MAC_gNB/mac_proto.h"
Raymond Knopp's avatar
Raymond Knopp committed
43
#include "common/utils/LOG/log.h"
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
44
#include "COMMON/mac_rrc_primitives.h"
45 46 47 48 49
#include "RRC/NR/MESSAGES/asn1_msg.h"

#include "NR_BCCH-BCH-Message.h"
#include "NR_UL-DCCH-Message.h"
#include "NR_DL-DCCH-Message.h"
50
#include "NR_CellGroupConfig.h"
51
#include "NR_MeasResults.h"
52 53
#include "LTE_UECapabilityInformation.h"
#include "LTE_UL-DCCH-Message.h"
54 55
#include "NR_UL-CCCH-Message.h"
#include "NR_RRCSetupRequest-IEs.h"
Xue Song's avatar
Xue Song committed
56
#include "NR_RRCSetupComplete-IEs.h"
57
#include "NR_RRCReestablishmentRequest-IEs.h"
58
#include "NR_MIB.h"
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
59 60 61 62 63

#include "rlc.h"
#include "rrc_eNB_UE_context.h"
#include "platform_types.h"
#include "msc.h"
Raymond Knopp's avatar
Raymond Knopp committed
64
#include "common/utils/LOG/vcd_signal_dumper.h"
65

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
66 67 68 69 70 71 72
#include "T.h"

#include "RRC/NAS/nas_config.h"
#include "RRC/NAS/rb_config.h"
#include "OCG.h"
#include "OCG_extern.h"

Xue Song's avatar
Xue Song committed
73
#include "UTIL/OSA/osa_defs.h"
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
74

75
#include "rrc_eNB_S1AP.h"
Xue Song's avatar
Xue Song committed
76
#include "rrc_gNB_NGAP.h"
Xue Song's avatar
Xue Song committed
77

78 79
#include "rrc_eNB_GTPV1U.h"

80
#include "nr_pdcp/nr_pdcp_entity.h"
81
#include "pdcp.h"
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
82 83
#include "gtpv1u_eNB_task.h"

84

85
#include "intertask_interface.h"
86
#include "SIMULATION/TOOLS/sim.h" // for taus
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
87

88
#include "executables/softmodem-common.h"
laurent's avatar
laurent committed
89
#include <openair2/RRC/NR/rrc_gNB_UE_context.h>
Laurent's avatar
Laurent committed
90
#include <openair2/X2AP/x2ap_eNB.h>
91

Xue Song's avatar
Xue Song committed
92 93
#include "BIT_STRING.h"
#include "assertions.h"
94

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
95 96 97 98
//#define XER_PRINT

extern RAN_CONTEXT_t RC;

Xue Song's avatar
Xue Song committed
99 100 101 102 103 104 105 106
extern boolean_t nr_rrc_pdcp_config_asn1_req(
    const protocol_ctxt_t *const  ctxt_pP,
    NR_SRB_ToAddModList_t  *const srb2add_list,
    NR_DRB_ToAddModList_t  *const drb2add_list,
    NR_DRB_ToReleaseList_t *const drb2release_list,
    const uint8_t                   security_modeP,
    uint8_t                  *const kRRCenc,
    uint8_t                  *const kRRCint,
107 108
    uint8_t                  *const kUPenc,
    uint8_t                  *const kUPint
Xue Song's avatar
Xue Song committed
109 110 111 112 113 114 115 116 117 118 119
  #if (LTE_RRC_VERSION >= MAKE_VERSION(9, 0, 0))
    ,LTE_PMCH_InfoList_r9_t  *pmch_InfoList_r9
  #endif
    ,rb_id_t                 *const defaultDRB,
    struct NR_CellGroupConfig__rlc_BearerToAddModList *rlc_bearer2add_list);

extern rlc_op_status_t nr_rrc_rlc_config_asn1_req (const protocol_ctxt_t   * const ctxt_pP,
    const NR_SRB_ToAddModList_t   * const srb2add_listP,
    const NR_DRB_ToAddModList_t   * const drb2add_listP,
    const NR_DRB_ToReleaseList_t  * const drb2release_listP,
    const LTE_PMCH_InfoList_r9_t * const pmch_InfoList_r9_pP,
120 121
    struct NR_CellGroupConfig__rlc_BearerToAddModList *rlc_srb_bearer2add_list,
    struct NR_CellGroupConfig__rlc_BearerToAddModList *rlc_drb_bearer2add_list);
Xue Song's avatar
Xue Song committed
122

Xue Song's avatar
Xue Song committed
123 124
static inline uint64_t bitStr_to_uint64(BIT_STRING_t *asn);

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
125
mui_t                               rrc_gNB_mui = 0;
126
uint8_t first_rrcreconfiguration = 0;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
127

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
128 129
///---------------------------------------------------------------------------------------------------------------///
///---------------------------------------------------------------------------------------------------------------///
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
130

131
void openair_nr_rrc_on(const protocol_ctxt_t *const ctxt_pP) {
132
  LOG_I(NR_RRC, PROTOCOL_NR_RRC_CTXT_FMT" gNB:OPENAIR NR RRC IN....\n",PROTOCOL_NR_RRC_CTXT_ARGS(ctxt_pP));
133 134 135 136
  rrc_config_nr_buffer (&RC.nrrrc[ctxt_pP->module_id]->carrier.SI, BCCH, 1);
  RC.nrrrc[ctxt_pP->module_id]->carrier.SI.Active = 1;
  rrc_config_nr_buffer (&RC.nrrrc[ctxt_pP->module_id]->carrier.Srb0, CCCH, 1);
  RC.nrrrc[ctxt_pP->module_id]->carrier.Srb0.Active = 1;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
137 138
}

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
139 140 141
///---------------------------------------------------------------------------------------------------------------///
///---------------------------------------------------------------------------------------------------------------///

142 143 144 145
void rrc_gNB_process_SgNBAdditionRequest(
  const protocol_ctxt_t  *const ctxt_pP,
  rrc_gNB_ue_context_t   *ue_context_pP
) {
146 147 148
  rrc_gNB_generate_SgNBAdditionRequestAcknowledge(ctxt_pP,ue_context_pP);
}

149 150 151
void rrc_gNB_generate_SgNBAdditionRequestAcknowledge(
  const protocol_ctxt_t  *const ctxt_pP,
  rrc_gNB_ue_context_t   *const ue_context_pP) {
152 153 154 155
  //uint8_t size;
  //uint8_t buffer[100];
  //int     CC_id = ue_context_pP->ue_context.primaryCC_id;
  //OCTET_STRING_t                                      *secondaryCellGroup;
156
  NR_CellGroupConfig_t                                *cellGroupconfig;
157 158 159 160
  struct NR_CellGroupConfig__rlc_BearerToAddModList   *rlc_BearerToAddModList;
  struct NR_MAC_CellGroupConfig                       *mac_CellGroupConfig;
  struct NR_PhysicalCellGroupConfig                   *physicalCellGroupConfig;
  struct NR_SpCellConfig                              *spCellConfig;
161
  //struct NR_CellGroupConfig__sCellToAddModList        *sCellToAddModList;
162 163 164 165 166
  cellGroupconfig                           = CALLOC(1,sizeof(NR_CellGroupConfig_t));
  cellGroupconfig->rlc_BearerToAddModList   = CALLOC(1,sizeof(struct NR_CellGroupConfig__rlc_BearerToAddModList));
  cellGroupconfig->mac_CellGroupConfig      = CALLOC(1,sizeof(struct NR_MAC_CellGroupConfig));
  cellGroupconfig->physicalCellGroupConfig  = CALLOC(1,sizeof(struct NR_PhysicalCellGroupConfig));
  cellGroupconfig->spCellConfig             = CALLOC(1,sizeof(struct NR_SpCellConfig));
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
167
  //cellGroupconfig->sCellToAddModList        = CALLOC(1,sizeof(struct NR_CellGroupConfig__sCellToAddModList));
168 169 170 171
  rlc_BearerToAddModList   = cellGroupconfig->rlc_BearerToAddModList;
  mac_CellGroupConfig      = cellGroupconfig->mac_CellGroupConfig;
  physicalCellGroupConfig  = cellGroupconfig->physicalCellGroupConfig;
  spCellConfig             = cellGroupconfig->spCellConfig;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
172
  //sCellToAddModList        = cellGroupconfig->sCellToAddModList;
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  rlc_bearer_config_t *rlc_config;
  rlc_config = CALLOC(1,sizeof(rlc_bearer_config_t));
  //Fill rlc_bearer config value
  rrc_config_rlc_bearer(ctxt_pP->module_id,
                        ue_context_pP->ue_context.primaryCC_id,
                        rlc_config
                       );
  //Fill rlc_bearer config to structure
  do_RLC_BEARER(ctxt_pP->module_id,
                ue_context_pP->ue_context.primaryCC_id,
                rlc_BearerToAddModList,
                rlc_config);
  mac_cellgroup_t *mac_cellgroup_config;
  mac_cellgroup_config = CALLOC(1,sizeof(mac_cellgroup_t));
  //Fill mac_cellgroup_config config value
  rrc_config_mac_cellgroup(ctxt_pP->module_id,
                           ue_context_pP->ue_context.primaryCC_id,
                           mac_cellgroup_config
                          );
  //Fill mac_cellgroup config to structure
  do_MAC_CELLGROUP(ctxt_pP->module_id,
                   ue_context_pP->ue_context.primaryCC_id,
                   mac_CellGroupConfig,
                   mac_cellgroup_config);
  physicalcellgroup_t *physicalcellgroup_config;
  physicalcellgroup_config = CALLOC(1,sizeof(physicalcellgroup_t));
  //Fill physicalcellgroup_config config value
  rrc_config_physicalcellgroup(ctxt_pP->module_id,
                               ue_context_pP->ue_context.primaryCC_id,
                               physicalcellgroup_config
                              );
  //Fill physicalcellgroup config to structure
  do_PHYSICALCELLGROUP(ctxt_pP->module_id,
                       ue_context_pP->ue_context.primaryCC_id,
                       physicalCellGroupConfig,
                       physicalcellgroup_config);
209
  do_SpCellConfig(RC.nrrrc[ctxt_pP->module_id],
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
210
                  spCellConfig);
211 212 213 214 215
}

///---------------------------------------------------------------------------------------------------------------///
///---------------------------------------------------------------------------------------------------------------///

216
static void init_NR_SI(gNB_RRC_INST *rrc, gNB_RrcConfigurationReq *configuration) {
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
217
  LOG_D(RRC,"%s()\n\n\n\n",__FUNCTION__);
218 219 220 221
  if (NODE_IS_DU(rrc->node_type) || NODE_IS_MONOLITHIC(rrc->node_type)) {
    rrc->carrier.MIB             = (uint8_t *) malloc16(4);
    rrc->carrier.sizeof_MIB      = do_MIB_NR(rrc,0);
  }
222

223
    if((get_softmodem_params()->sa) && ( (NODE_IS_DU(rrc->node_type) || NODE_IS_MONOLITHIC(rrc->node_type)))) {
224 225 226
    rrc->carrier.sizeof_SIB1 = do_SIB1_NR(&rrc->carrier,configuration);
  }

227 228 229 230 231 232 233
  if (!NODE_IS_DU(rrc->node_type)) {
    rrc->carrier.SIB23 = (uint8_t *) malloc16(100);
    AssertFatal(rrc->carrier.SIB23 != NULL, "cannot allocate memory for SIB");
    rrc->carrier.sizeof_SIB23 = do_SIB23_NR(&rrc->carrier, configuration);
    LOG_I(NR_RRC,"do_SIB23_NR, size %d \n ", rrc->carrier.sizeof_SIB23);
    AssertFatal(rrc->carrier.sizeof_SIB23 != 255,"FATAL, RC.nrrrc[mod].carrier[CC_id].sizeof_SIB23 == 255");
  }
234

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
235
  LOG_I(NR_RRC,"Done init_NR_SI\n");
236

237
  if (NODE_IS_MONOLITHIC(rrc->node_type)){
Xue Song's avatar
Xue Song committed
238
    rrc_mac_config_req_gNB(rrc->module_id,
239 240 241 242 243 244 245 246
			   rrc->carrier.ssb_SubcarrierOffset,
			   rrc->carrier.pdsch_AntennaPorts,
			   rrc->carrier.pusch_AntennaPorts,
			   (NR_ServingCellConfigCommon_t *)rrc->carrier.servingcellconfigcommon,
			   0,
			   0, // WIP hardcoded rnti
			   (NR_CellGroupConfig_t *)NULL
			   );
Xue Song's avatar
Xue Song committed
247
  }
Raymond Knopp's avatar
Raymond Knopp committed
248

249 250 251 252 253 254
  /* set flag to indicate that cell information is configured. This is required
   * in DU to trigger F1AP_SETUP procedure */
  pthread_mutex_lock(&rrc->cell_info_mutex);
  rrc->cell_info_configured=1;
  pthread_mutex_unlock(&rrc->cell_info_mutex);

255
  if (get_softmodem_params()->phy_test > 0 || get_softmodem_params()->do_ra > 0) {
256 257 258
    // This is for phytest only, emulate first X2 message if uecap.raw file is present
    FILE *fd;
    fd = fopen("uecap.raw","r");
259

260 261 262 263 264 265
    if (fd != NULL) {
      char buffer[4096];
      int msg_len=fread(buffer,1,4096,fd);
      LOG_I(RRC,"Read in %d bytes for uecap\n",msg_len);
      LTE_UL_DCCH_Message_t *LTE_UL_DCCH_Message;
      asn_dec_rval_t dec_rval = uper_decode_complete( NULL,
266 267 268 269 270
                                &asn_DEF_LTE_UL_DCCH_Message,
                                (void **)&LTE_UL_DCCH_Message,
                                (uint8_t *)buffer,
                                msg_len);

271 272
      if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) {
        AssertFatal(1==0,"NR_UL_DCCH_MESSAGE decode error\n");
273 274 275 276 277
        // free the memory
        SEQUENCE_free( &asn_DEF_LTE_UL_DCCH_Message, LTE_UL_DCCH_Message, 1 );
        return;
      }

278 279 280 281
      fclose(fd);
      xer_fprint(stdout,&asn_DEF_LTE_UL_DCCH_Message, LTE_UL_DCCH_Message);
      // recreate enough of X2 EN-DC Container
      AssertFatal(LTE_UL_DCCH_Message->message.choice.c1.present == LTE_UL_DCCH_MessageType__c1_PR_ueCapabilityInformation,
282
                  "ueCapabilityInformation not present\n");
283 284 285 286 287 288 289
      NR_CG_ConfigInfo_t *CG_ConfigInfo = calloc(1,sizeof(*CG_ConfigInfo));
      CG_ConfigInfo->criticalExtensions.present = NR_CG_ConfigInfo__criticalExtensions_PR_c1;
      CG_ConfigInfo->criticalExtensions.choice.c1 = calloc(1,sizeof(*CG_ConfigInfo->criticalExtensions.choice.c1));
      CG_ConfigInfo->criticalExtensions.choice.c1->present = NR_CG_ConfigInfo__criticalExtensions__c1_PR_cg_ConfigInfo;
      CG_ConfigInfo->criticalExtensions.choice.c1->choice.cg_ConfigInfo = calloc(1,sizeof(*CG_ConfigInfo->criticalExtensions.choice.c1->choice.cg_ConfigInfo));
      NR_CG_ConfigInfo_IEs_t *cg_ConfigInfo = CG_ConfigInfo->criticalExtensions.choice.c1->choice.cg_ConfigInfo;
      cg_ConfigInfo->ue_CapabilityInfo = calloc(1,sizeof(*cg_ConfigInfo->ue_CapabilityInfo));
290 291
      asn_enc_rval_t enc_rval = uper_encode_to_buffer(&asn_DEF_LTE_UE_CapabilityRAT_ContainerList,NULL,
                                (void *)&LTE_UL_DCCH_Message->message.choice.c1.choice.ueCapabilityInformation.criticalExtensions.choice.c1.choice.ueCapabilityInformation_r8.ue_CapabilityRAT_ContainerList,buffer,4096);
292
      AssertFatal (enc_rval.encoded > 0, "ASN1 message encoding failed (%s, %jd)!\n",
293
                   enc_rval.failed_type->name, enc_rval.encoded);
294
      OCTET_STRING_fromBuf(cg_ConfigInfo->ue_CapabilityInfo,
295 296
                           (const char *)buffer,
                           (enc_rval.encoded+7)>>3);
297
      parse_CG_ConfigInfo(rrc,CG_ConfigInfo,NULL);
298
    } else {
299
      struct rrc_gNB_ue_context_s *ue_context_p = rrc_gNB_allocate_new_UE_context(rrc);
300 301
      ue_context_p->ue_context.spCellConfig = calloc(1, sizeof(struct NR_SpCellConfig));
      ue_context_p->ue_context.spCellConfig->spCellConfigDedicated = configuration->scd;
302
      LOG_I(NR_RRC,"Adding new user (%p)\n",ue_context_p);
Xue Song's avatar
Xue Song committed
303 304 305
      if (!NODE_IS_CU(RC.nrrrc[0]->node_type)) {
        rrc_add_nsa_user(rrc,ue_context_p,NULL);
      }
306
    }
307
  }
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
308
}
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
309

310
char openair_rrc_gNB_configuration(const module_id_t gnb_mod_idP, gNB_RrcConfigurationReq *configuration) {
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
311
  protocol_ctxt_t      ctxt;
312
  gNB_RRC_INST         *rrc=RC.nrrrc[gnb_mod_idP];
313
  PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, gnb_mod_idP, GNB_FLAG_YES, NOT_A_RNTI, 0, 0,gnb_mod_idP);
314 315 316
  LOG_I(NR_RRC,
        PROTOCOL_NR_RRC_CTXT_FMT" Init...\n",
        PROTOCOL_NR_RRC_CTXT_ARGS(&ctxt));
317

318 319 320 321 322 323
  AssertFatal(rrc != NULL, "RC.nrrrc not initialized!");
  AssertFatal(NUMBER_OF_UE_MAX < (module_id_t)0xFFFFFFFFFFFFFFFF, " variable overflow");
  AssertFatal(configuration!=NULL,"configuration input is null\n");
  rrc->module_id = gnb_mod_idP;
  rrc->Nb_ue = 0;
  rrc->carrier.Srb0.Active = 0;
Francesco Mani's avatar
Francesco Mani committed
324
  nr_uid_linear_allocator_init(&rrc->uid_allocator);
325 326 327
  RB_INIT(&rrc->rrc_ue_head);
  rrc->initial_id2_s1ap_ids = hashtable_create (NUMBER_OF_UE_MAX * 2, NULL, NULL);
  rrc->s1ap_id2_s1ap_ids    = hashtable_create (NUMBER_OF_UE_MAX * 2, NULL, NULL);
328 329
  rrc->initial_id2_ngap_ids = hashtable_create (NUMBER_OF_UE_MAX * 2, NULL, NULL);
  rrc->ngap_id2_ngap_ids    = hashtable_create (NUMBER_OF_UE_MAX * 2, NULL, NULL);
330
  rrc->carrier.servingcellconfigcommon = configuration->scc;
331 332
  rrc->carrier.ssb_SubcarrierOffset = configuration->ssb_SubcarrierOffset;
  rrc->carrier.pdsch_AntennaPorts = configuration->pdsch_AntennaPorts;
333
  rrc->carrier.pusch_AntennaPorts = configuration->pusch_AntennaPorts;
334
   /// System Information INIT
335 336
  pthread_mutex_init(&rrc->cell_info_mutex,NULL);
  rrc->cell_info_configured = 0;
337
  LOG_I(NR_RRC, PROTOCOL_NR_RRC_CTXT_FMT" Checking release \n",PROTOCOL_NR_RRC_CTXT_ARGS(&ctxt));
338
  init_NR_SI(rrc, configuration);
339
  rrc_init_nr_global_param();
340
  openair_nr_rrc_on(&ctxt);
341
  return 0;
342
}//END openair_rrc_gNB_configuration
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
343 344


345
void rrc_gNB_process_AdditionRequestInformation(const module_id_t gnb_mod_idP, x2ap_ENDC_sgnb_addition_req_t *m) {
346 347 348 349 350 351 352
  struct NR_CG_ConfigInfo *cg_configinfo = NULL;
  asn_dec_rval_t dec_rval = uper_decode_complete(NULL,
                            &asn_DEF_NR_CG_ConfigInfo,
                            (void **)&cg_configinfo,
                            (uint8_t *)m->rrc_buffer,
                            (int) m->rrc_buffer_size);//m->rrc_buffer_size);
  gNB_RRC_INST         *rrc=RC.nrrrc[gnb_mod_idP];
353

354 355 356 357 358 359
  if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) {
    AssertFatal(1==0,"NR_UL_DCCH_MESSAGE decode error\n");
    // free the memory
    SEQUENCE_free(&asn_DEF_NR_CG_ConfigInfo, cg_configinfo, 1);
    return;
  }
360

361 362 363 364 365
  xer_fprint(stdout,&asn_DEF_NR_CG_ConfigInfo, cg_configinfo);
  // recreate enough of X2 EN-DC Container
  AssertFatal(cg_configinfo->criticalExtensions.choice.c1->present == NR_CG_ConfigInfo__criticalExtensions__c1_PR_cg_ConfigInfo,
              "ueCapabilityInformation not present\n");
  parse_CG_ConfigInfo(rrc,cg_configinfo,m);
366 367
}

Cedric Roux's avatar
Cedric Roux committed
368

369
//-----------------------------------------------------------------------------
Xue Song's avatar
Xue Song committed
370 371 372
uint8_t
rrc_gNB_get_next_transaction_identifier(
    module_id_t gnb_mod_idP
373 374 375
)
//-----------------------------------------------------------------------------
{
376 377 378 379
  static uint8_t                      nr_rrc_transaction_identifier[NUMBER_OF_gNB_MAX];
  nr_rrc_transaction_identifier[gnb_mod_idP] = (nr_rrc_transaction_identifier[gnb_mod_idP] + 1) % NR_RRC_TRANSACTION_IDENTIFIER_NUMBER;
  LOG_T(NR_RRC, "generated xid is %d\n", nr_rrc_transaction_identifier[gnb_mod_idP]);
  return nr_rrc_transaction_identifier[gnb_mod_idP];
380 381
}

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
void apply_macrlc_config(gNB_RRC_INST *rrc,
                         rrc_gNB_ue_context_t         *const ue_context_pP,
                         const protocol_ctxt_t        *const ctxt_pP ) {

      rrc_mac_config_req_gNB(rrc->module_id,
                             rrc->carrier.ssb_SubcarrierOffset,
                             rrc->carrier.pdsch_AntennaPorts,
                             rrc->carrier.pusch_AntennaPorts,
                             NULL,
                             0,
                             ue_context_pP->ue_context.rnti,
                             ue_context_pP->ue_context.masterCellGroup
                             );
      nr_rrc_rlc_config_asn1_req(ctxt_pP,
                                 ue_context_pP->ue_context.SRB_configList,
                                 NULL,
                                 NULL,
                                 NULL,
                                 ue_context_pP->ue_context.masterCellGroup->rlc_BearerToAddModList,
                                 NULL);
}

void apply_pdcp_config(rrc_gNB_ue_context_t         *const ue_context_pP,
                       const protocol_ctxt_t        *const ctxt_pP ) {

      nr_rrc_pdcp_config_asn1_req(ctxt_pP,
                                  ue_context_pP->ue_context.SRB_configList,
                                  NULL,
                                  NULL,
                                  0xff,
                                  NULL,
                                  NULL,
                                  NULL,
                                  NULL,
                                  NULL,
                                  NULL,
                                  ue_context_pP->ue_context.masterCellGroup->rlc_BearerToAddModList);

}

422 423 424
//-----------------------------------------------------------------------------
void
rrc_gNB_generate_RRCSetup(
425 426 427 428 429
    const protocol_ctxt_t        *const ctxt_pP,
    rrc_gNB_ue_context_t         *const ue_context_pP,
    OCTET_STRING_t               *masterCellGroup_from_DU,
    NR_ServingCellConfigCommon_t *scc,
    const int                    CC_id
430 431 432
)
//-----------------------------------------------------------------------------
{
433
  LOG_I(NR_RRC, "rrc_gNB_generate_RRCSetup \n");
yaojie's avatar
yaojie committed
434
  NR_SRB_ToAddModList_t        **SRB_configList = NULL;
435
  MessageDef                    *message_p;
436 437 438 439 440 441 442

  // T(T_GNB_RRC_SETUP,
  //   T_INT(ctxt_pP->module_id),
  //   T_INT(ctxt_pP->frame),
  //   T_INT(ctxt_pP->subframe),
  //   T_INT(ctxt_pP->rnti));
  gNB_RRC_UE_t *ue_p = &ue_context_pP->ue_context;
443 444 445 446 447
  ue_p->Srb0.Tx_buffer.payload_size = do_RRCSetup(ue_context_pP,
						  (uint8_t *) ue_p->Srb0.Tx_buffer.Payload,
						  rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id),
						  masterCellGroup_from_DU,
						  scc);
448 449 450 451 452

  LOG_DUMPMSG(NR_RRC, DEBUG_RRC,
              (char *)(ue_p->Srb0.Tx_buffer.Payload),
              ue_p->Srb0.Tx_buffer.payload_size,
              "[MSG] RRC Setup\n");
453 454
  gNB_RRC_INST *rrc = RC.nrrrc[ctxt_pP->module_id];
  switch (rrc->node_type) {
Xue Song's avatar
Xue Song committed
455 456 457
    case ngran_gNB_CU:
      // create an ITTI message
      /* TODO: F1 IDs ar missing in RRC */
458 459 460 461 462 463 464 465 466 467 468 469
      nr_rrc_pdcp_config_asn1_req(ctxt_pP,
				  ue_context_pP->ue_context.SRB_configList,
				  NULL,
				  NULL,
				  0xff,
				  NULL,
				  NULL,
				  NULL,
				  NULL,
				  NULL,
				  NULL,
				  NULL);
470 471 472 473 474 475 476 477 478 479 480 481 482 483
      message_p = itti_alloc_new_message (TASK_RRC_GNB, 0, F1AP_DL_RRC_MESSAGE);
      F1AP_DL_RRC_MESSAGE (message_p).rrc_container        =  (uint8_t *)ue_p->Srb0.Tx_buffer.Payload;
      F1AP_DL_RRC_MESSAGE (message_p).rrc_container_length = ue_p->Srb0.Tx_buffer.payload_size;
      F1AP_DL_RRC_MESSAGE (message_p).gNB_CU_ue_id         = 0;
      F1AP_DL_RRC_MESSAGE (message_p).gNB_DU_ue_id         = 0;
      F1AP_DL_RRC_MESSAGE (message_p).old_gNB_DU_ue_id     = 0xFFFFFFFF; // unknown
      F1AP_DL_RRC_MESSAGE (message_p).rnti                 = ue_p->rnti;
      F1AP_DL_RRC_MESSAGE (message_p).srb_id               = CCCH;
      F1AP_DL_RRC_MESSAGE (message_p).execute_duplication  = 1;
      F1AP_DL_RRC_MESSAGE (message_p).RAT_frequency_priority_information.en_dc = 0;
      itti_send_msg_to_task (TASK_CU_F1, ctxt_pP->module_id, message_p);
      LOG_D(NR_RRC, "Send F1AP_DL_RRC_MESSAGE with ITTI\n");
  
    break;
Xue Song's avatar
Xue Song committed
484 485 486 487 488 489 490

    case ngran_gNB_DU:
      // nothing to do for DU
      AssertFatal(1==0,"nothing to do for DU\n");
      break;

    case ngran_gNB:
491
    {
Xue Song's avatar
Xue Song committed
492
      // rrc_mac_config_req_gNB
493

494
#ifdef ITTI_SIM
495 496 497 498
      LOG_I(NR_RRC,
            PROTOCOL_NR_RRC_CTXT_UE_FMT" [RAPROC] Logical Channel DL-CCCH, Generating RRCSetup (bytes %d)\n",
            PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
            ue_p->Srb0.Tx_buffer.payload_size);
499
      uint8_t *message_buffer;
500
      message_buffer = itti_malloc (TASK_RRC_GNB, TASK_RRC_UE_SIM,
501 502
                ue_p->Srb0.Tx_buffer.payload_size);
      memcpy (message_buffer, (uint8_t*)ue_p->Srb0.Tx_buffer.Payload, ue_p->Srb0.Tx_buffer.payload_size);
503
      message_p = itti_alloc_new_message (TASK_RRC_GNB, 0, GNB_RRC_CCCH_DATA_IND);
504 505 506
      GNB_RRC_CCCH_DATA_IND (message_p).sdu = message_buffer;
      GNB_RRC_CCCH_DATA_IND (message_p).size  = ue_p->Srb0.Tx_buffer.payload_size;
      itti_send_msg_to_task (TASK_RRC_UE_SIM, ctxt_pP->instance, message_p);
507
#else
508
      LOG_D(NR_RRC,
509 510
	    PROTOCOL_NR_RRC_CTXT_UE_FMT" RRC_gNB --- MAC_CONFIG_REQ  (SRB1) ---> MAC_gNB\n",
	    PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
511
      MSC_LOG_TX_MESSAGE(
512 513 514 515 516 517 518 519
			 MSC_RRC_GNB,
			 MSC_RRC_UE,
			 ue_p->Srb0.Tx_buffer.Header, // LG WARNING
			 ue_p->Srb0.Tx_buffer.payload_size,
			 MSC_AS_TIME_FMT" RRCSetup UE %x size %u",
			 MSC_AS_TIME_ARGS(ctxt_pP),
			 ue_context_pP->ue_context.rnti,
			 ue_p->Srb0.Tx_buffer.payload_size);
520
      LOG_I(NR_RRC,
521 522 523
	    PROTOCOL_NR_RRC_CTXT_UE_FMT" [RAPROC] Logical Channel DL-CCCH, Generating RRCSetup (bytes %d)\n",
	    PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
	    ue_p->Srb0.Tx_buffer.payload_size);
524 525 526 527 528 529
      // activate release timer, if RRCSetupComplete not received after 100 frames, remove UE
      ue_context_pP->ue_context.ue_release_timer = 1;
      // remove UE after 10 frames after RRCConnectionRelease is triggered
      ue_context_pP->ue_context.ue_release_timer_thres = 1000;
      /* init timers */
      //   ue_context_pP->ue_context.ue_rrc_inactivity_timer = 0;
530

531
      // configure MAC
532 533 534
      apply_macrlc_config(rrc,ue_context_pP,ctxt_pP);

      apply_pdcp_config(ue_context_pP,ctxt_pP);
535
#endif
536
    }
537
    break;
538

539 540 541
    default:
      LOG_W(NR_RRC, "Unknown node type %d\n", rrc->node_type);
      break;
542
  }
543 544
}

545 546 547 548 549 550 551 552 553
//-----------------------------------------------------------------------------
void
rrc_gNB_generate_RRCSetup_for_RRCReestablishmentRequest(
    const protocol_ctxt_t    *const ctxt_pP,
    const int                CC_id
)
//-----------------------------------------------------------------------------
{
  LOG_I(NR_RRC, "generate RRCSetup for RRCReestablishmentRequest \n");
554
  NR_SRB_ToAddModList_t        **SRB_configList = NULL;
555 556
  rrc_gNB_ue_context_t         *ue_context_pP   = NULL;
  gNB_RRC_INST                 *rrc_instance_p = RC.nrrrc[ctxt_pP->module_id];
557
  NR_ServingCellConfigCommon_t *scc=rrc_instance_p->carrier.servingcellconfigcommon;
558 559 560 561

  ue_context_pP = rrc_gNB_get_next_free_ue_context(ctxt_pP, rrc_instance_p, 0);

  gNB_RRC_UE_t *ue_p = &ue_context_pP->ue_context;
562
  SRB_configList = &ue_p->SRB_configList;
563 564 565 566 567
  ue_p->Srb0.Tx_buffer.payload_size = do_RRCSetup(ue_context_pP,
						  (uint8_t *) ue_p->Srb0.Tx_buffer.Payload,
						  rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id),
						  NULL,
						  scc);
568 569 570 571 572 573 574 575 576 577 578 579 580

  LOG_DUMPMSG(NR_RRC, DEBUG_RRC,
              (char *)(ue_p->Srb0.Tx_buffer.Payload),
              ue_p->Srb0.Tx_buffer.payload_size,
              "[MSG] RRC Setup\n");

  LOG_D(NR_RRC,
          PROTOCOL_NR_RRC_CTXT_UE_FMT" RRC_gNB --- MAC_CONFIG_REQ  (SRB1) ---> MAC_gNB\n",
          PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));

  rrc_mac_config_req_gNB(rrc_instance_p->module_id,
                         rrc_instance_p->carrier.ssb_SubcarrierOffset,
                         rrc_instance_p->carrier.pdsch_AntennaPorts,
581
                         rrc_instance_p->carrier.pusch_AntennaPorts,
582 583 584 585
                         (NR_ServingCellConfigCommon_t *)rrc_instance_p->carrier.servingcellconfigcommon,
                         0,
                         ue_context_pP->ue_context.rnti,
                         (NR_CellGroupConfig_t *)NULL
586 587 588 589 590 591 592 593 594 595 596 597
			 );
  
  MSC_LOG_TX_MESSAGE(
		     MSC_RRC_GNB,
		     MSC_RRC_UE,
		     ue_p->Srb0.Tx_buffer.Header, // LG WARNING
		     ue_p->Srb0.Tx_buffer.payload_size,
		     MSC_AS_TIME_FMT" RRCSetup UE %x size %u",
		     MSC_AS_TIME_ARGS(ctxt_pP),
		     ue_context_pP->ue_context.rnti,
		     ue_p->Srb0.Tx_buffer.payload_size);
  LOG_I(NR_RRC,
598 599 600
        PROTOCOL_NR_RRC_CTXT_UE_FMT" [RAPROC] Logical Channel DL-CCCH, Generating RRCSetup (bytes %d)\n",
        PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
        ue_p->Srb0.Tx_buffer.payload_size);
601 602 603 604 605 606
  // activate release timer, if RRCSetupComplete not received after 100 frames, remove UE
  ue_context_pP->ue_context.ue_release_timer = 1;
  // remove UE after 10 frames after RRCConnectionRelease is triggered
  ue_context_pP->ue_context.ue_release_timer_thres = 1000;
  /* init timers */
  //   ue_context_pP->ue_context.ue_rrc_inactivity_timer = 0;
607
#ifdef ITTI_SIM
608 609 610 611 612 613 614 615 616
  MessageDef *message_p;
  uint8_t *message_buffer;
  message_buffer = itti_malloc (TASK_RRC_GNB, TASK_RRC_UE_SIM,
				ue_p->Srb0.Tx_buffer.payload_size);
  memcpy (message_buffer, (uint8_t*)ue_p->Srb0.Tx_buffer.Payload, ue_p->Srb0.Tx_buffer.payload_size);
  message_p = itti_alloc_new_message (TASK_RRC_GNB, 0, GNB_RRC_CCCH_DATA_IND);
  GNB_RRC_CCCH_DATA_IND (message_p).sdu = message_buffer;
  GNB_RRC_CCCH_DATA_IND (message_p).size  = ue_p->Srb0.Tx_buffer.payload_size;
  itti_send_msg_to_task (TASK_RRC_UE_SIM, ctxt_pP->instance, message_p);
617 618 619
#endif
}

620 621
void
rrc_gNB_generate_RRCReject(
Xue Song's avatar
Xue Song committed
622 623 624
    const protocol_ctxt_t    *const ctxt_pP,
    rrc_gNB_ue_context_t     *const ue_context_pP,
    const int                CC_id
625 626 627
)
//-----------------------------------------------------------------------------
{
628 629
  LOG_I(NR_RRC, "rrc_gNB_generate_RRCReject \n");
  gNB_RRC_UE_t *ue_p = &ue_context_pP->ue_context;
630
  MessageDef   *message_p;
631

632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
  ue_p->Srb0.Tx_buffer.payload_size = do_RRCReject(ctxt_pP->module_id,
                                                  (uint8_t *)ue_p->Srb0.Tx_buffer.Payload);
  LOG_DUMPMSG(NR_RRC, DEBUG_RRC,
              (char *)(ue_p->Srb0.Tx_buffer.Payload),
              ue_p->Srb0.Tx_buffer.payload_size,
              "[MSG] RRCReject \n");
  MSC_LOG_TX_MESSAGE(MSC_RRC_GNB,
                    MSC_RRC_UE,
                    ue_p->Srb0.Tx_buffer.Header,
                    ue_p->Srb0.Tx_buffer.payload_size,
                    MSC_AS_TIME_FMT" NR_RRCReject UE %x size %u",
                    MSC_AS_TIME_ARGS(ctxt_pP),
                    ue_context_pP == NULL ? -1 : ue_context_pP->ue_context.rnti,
                    ue_p->Srb0.Tx_buffer.payload_size);
  LOG_I(NR_RRC,
      PROTOCOL_NR_RRC_CTXT_UE_FMT" [RAPROC] Logical Channel DL-CCCH, Generating NR_RRCReject (bytes %d)\n",
      PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
      ue_p->Srb0.Tx_buffer.payload_size);
Xue Song's avatar
Xue Song committed
650

651 652 653
  switch (RC.nrrrc[ctxt_pP->module_id]->node_type) {
    case ngran_gNB_CU:
      // create an ITTI message
654
      message_p = itti_alloc_new_message (TASK_RRC_GNB, 0, F1AP_DL_RRC_MESSAGE);
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
      F1AP_DL_RRC_MESSAGE (message_p).rrc_container        = (uint8_t *)ue_p->Srb0.Tx_buffer.Payload;
      F1AP_DL_RRC_MESSAGE (message_p).rrc_container_length = ue_p->Srb0.Tx_buffer.payload_size;
      F1AP_DL_RRC_MESSAGE (message_p).gNB_CU_ue_id         = 0;
      F1AP_DL_RRC_MESSAGE (message_p).gNB_DU_ue_id         = 0;
      F1AP_DL_RRC_MESSAGE (message_p).old_gNB_DU_ue_id     = 0xFFFFFFFF; // unknown
      F1AP_DL_RRC_MESSAGE (message_p).rnti                 = ue_p->rnti;
      F1AP_DL_RRC_MESSAGE (message_p).srb_id               = CCCH;
      F1AP_DL_RRC_MESSAGE (message_p).execute_duplication  = 1;
      F1AP_DL_RRC_MESSAGE (message_p).RAT_frequency_priority_information.en_dc = 0;
      itti_send_msg_to_task (TASK_CU_F1, ctxt_pP->module_id, message_p);
      LOG_D(NR_RRC, "Send F1AP_DL_RRC_MESSAGE with ITTI\n");
      break;

    case ngran_gNB_DU:
      // nothing to do for DU
      AssertFatal(1==0,"nothing to do for DU\n");
      break;

    case ngran_gNB:
674
    {
Xue Song's avatar
Xue Song committed
675
#ifdef ITTI_SIM
676
      uint8_t *message_buffer;
677
      message_buffer = itti_malloc (TASK_RRC_GNB, TASK_RRC_UE_SIM,
678 679
                ue_p->Srb0.Tx_buffer.payload_size);
      memcpy (message_buffer, (uint8_t*)ue_p->Srb0.Tx_buffer.Payload, ue_p->Srb0.Tx_buffer.payload_size);
680
      message_p = itti_alloc_new_message (TASK_RRC_GNB, 0, GNB_RRC_CCCH_DATA_IND);
681 682 683
      GNB_RRC_CCCH_DATA_IND (message_p).sdu = message_buffer;
      GNB_RRC_CCCH_DATA_IND (message_p).size  = ue_p->Srb0.Tx_buffer.payload_size;
      itti_send_msg_to_task (TASK_RRC_UE_SIM, ctxt_pP->instance, message_p);
Xue Song's avatar
Xue Song committed
684
#endif
685
      // rrc_mac_config_req_gNB
686
    }
687 688 689 690 691
      break;

    default :
      LOG_W(NR_RRC, "Unknown node type %d\n", RC.nrrrc[ctxt_pP->module_id]->node_type);
  }
692 693
}

Xue Song's avatar
Xue Song committed
694 695 696 697 698 699 700 701 702 703 704 705
//-----------------------------------------------------------------------------
/*
* Process the rrc setup complete message from UE (SRB1 Active)
*/
void
rrc_gNB_process_RRCSetupComplete(
  const protocol_ctxt_t     *const ctxt_pP,
  rrc_gNB_ue_context_t      *ue_context_pP,
  NR_RRCSetupComplete_IEs_t *rrcSetupComplete
)
//-----------------------------------------------------------------------------
{
706 707 708
  LOG_I(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" [RAPROC] Logical Channel UL-DCCH, " "processing NR_RRCSetupComplete from UE (SRB1 Active)\n",
      PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
  ue_context_pP->ue_context.Srb1.Active = 1;
709
  ue_context_pP->ue_context.Srb1.Srb_info.Srb_id = 1;
710
  ue_context_pP->ue_context.Status = NR_RRC_CONNECTED;
Xue Song's avatar
Xue Song committed
711

712
  if (AMF_MODE_ENABLED) {
Xue Song's avatar
Xue Song committed
713
    rrc_gNB_send_NGAP_NAS_FIRST_REQ(ctxt_pP, ue_context_pP, rrcSetupComplete);
714 715 716
  } else {
    rrc_gNB_generate_SecurityModeCommand(ctxt_pP, ue_context_pP);
  }
Xue Song's avatar
Xue Song committed
717 718
}

Xue Song's avatar
Xue Song committed
719 720 721
//-----------------------------------------------------------------------------
void 
rrc_gNB_generate_defaultRRCReconfiguration(
722 723
  const protocol_ctxt_t     *const ctxt_pP,
  rrc_gNB_ue_context_t      *ue_context_pP
Xue Song's avatar
Xue Song committed
724 725 726
)
//-----------------------------------------------------------------------------
{
727 728
  uint8_t                       buffer[RRC_BUF_SIZE];
  uint16_t                      size;
729
  /*NR_SRB_ToAddModList_t        **SRB_configList2 = NULL;
730
  NR_SRB_ToAddModList_t        *SRB_configList  = ue_context_pP->ue_context.SRB_configList;
731 732
  NR_DRB_ToAddModList_t        **DRB_configList  = NULL;
  NR_DRB_ToAddModList_t        **DRB_configList2 = NULL;
733 734
  NR_SRB_ToAddMod_t            *SRB2_config     = NULL;
  NR_DRB_ToAddMod_t            *DRB_config      = NULL;
735
  NR_SDAP_Config_t             *sdap_config     = NULL;*/
736 737 738 739 740 741 742 743
  struct NR_RRCReconfiguration_v1530_IEs__dedicatedNAS_MessageList
                               *dedicatedNAS_MessageList = NULL;
  NR_DedicatedNAS_Message_t    *dedicatedNAS_Message = NULL;

  uint8_t xid = rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id);

  /******************** Radio Bearer Config ********************/
  /* Configure SRB2 */
744
  /*SRB_configList2 = &ue_context_pP->ue_context.SRB_configList2[xid];
745 746
  if (*SRB_configList2) {
    free(*SRB_configList2);
747
  }
748 749
  *SRB_configList2 = CALLOC(1, sizeof(**SRB_configList2));
  memset(*SRB_configList2, 0, sizeof(**SRB_configList2));
750 751
  SRB2_config = CALLOC(1, sizeof(*SRB2_config));
  SRB2_config->srb_Identity = 2;
752
  ASN_SEQUENCE_ADD(&(*SRB_configList2)->list, SRB2_config);
753
  ASN_SEQUENCE_ADD(&SRB_configList->list, SRB2_config);*/
754 755

  /* Configure DRB */
756
  /*DRB_configList = &ue_context_pP->ue_context.DRB_configList;
757 758
  if (*DRB_configList) {
      free(*DRB_configList);
759
  }
760 761
  *DRB_configList = CALLOC(1, sizeof(**DRB_configList));
  memset(*DRB_configList, 0, sizeof(**DRB_configList));
762

763 764 765
  DRB_configList2 = &ue_context_pP->ue_context.DRB_configList2[xid];
  if (*DRB_configList2) {
      free(*DRB_configList2);
766
  }
767 768
  *DRB_configList2 = CALLOC(1, sizeof(**DRB_configList2));
  memset(*DRB_configList2, 0, sizeof(**DRB_configList2));
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783

  DRB_config = CALLOC(1, sizeof(*DRB_config));
  DRB_config->drb_Identity = 1;
  DRB_config->cnAssociation = CALLOC(1, sizeof(*DRB_config->cnAssociation));
  DRB_config->cnAssociation->present = NR_DRB_ToAddMod__cnAssociation_PR_sdap_Config;
  // TODO sdap_Config
  sdap_config = CALLOC(1, sizeof(NR_SDAP_Config_t));
  memset(sdap_config, 0, sizeof(NR_SDAP_Config_t));
  DRB_config->cnAssociation->choice.sdap_Config = sdap_config;
  // TODO pdcp_Config
  DRB_config->reestablishPDCP = NULL;
  DRB_config->recoverPDCP = NULL;
  DRB_config->pdcp_Config = calloc(1, sizeof(*DRB_config->pdcp_Config));
  DRB_config->pdcp_Config->drb = calloc(1,sizeof(*DRB_config->pdcp_Config->drb));
  DRB_config->pdcp_Config->drb->discardTimer = calloc(1, sizeof(*DRB_config->pdcp_Config->drb->discardTimer));
784
  *DRB_config->pdcp_Config->drb->discardTimer = NR_PDCP_Config__drb__discardTimer_infinity;
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
  DRB_config->pdcp_Config->drb->pdcp_SN_SizeUL = calloc(1, sizeof(*DRB_config->pdcp_Config->drb->pdcp_SN_SizeUL));
  *DRB_config->pdcp_Config->drb->pdcp_SN_SizeUL = NR_PDCP_Config__drb__pdcp_SN_SizeUL_len18bits;
  DRB_config->pdcp_Config->drb->pdcp_SN_SizeDL = calloc(1, sizeof(*DRB_config->pdcp_Config->drb->pdcp_SN_SizeDL));
  *DRB_config->pdcp_Config->drb->pdcp_SN_SizeDL = NR_PDCP_Config__drb__pdcp_SN_SizeDL_len18bits;
  DRB_config->pdcp_Config->drb->headerCompression.present = NR_PDCP_Config__drb__headerCompression_PR_notUsed;
  DRB_config->pdcp_Config->drb->headerCompression.choice.notUsed = 0;

  DRB_config->pdcp_Config->drb->integrityProtection = NULL;
  DRB_config->pdcp_Config->drb->statusReportRequired = NULL;
  DRB_config->pdcp_Config->drb->outOfOrderDelivery = NULL;
  DRB_config->pdcp_Config->moreThanOneRLC = NULL;

  DRB_config->pdcp_Config->t_Reordering = calloc(1, sizeof(*DRB_config->pdcp_Config->t_Reordering));
  *DRB_config->pdcp_Config->t_Reordering = NR_PDCP_Config__t_Reordering_ms0;
  DRB_config->pdcp_Config->ext1 = NULL;

801
  ASN_SEQUENCE_ADD(&(*DRB_configList)->list, DRB_config);
802
  ASN_SEQUENCE_ADD(&(*DRB_configList2)->list, DRB_config);*/
803

804 805
  dedicatedNAS_MessageList = CALLOC(1, sizeof(struct NR_RRCReconfiguration_v1530_IEs__dedicatedNAS_MessageList));

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
  /* Add all NAS PDUs to the list */
  for (int i = 0; i < ue_context_pP->ue_context.nb_of_pdusessions; i++) {
    if (ue_context_pP->ue_context.pdusession[i].param.nas_pdu.buffer != NULL) {
      dedicatedNAS_Message = CALLOC(1, sizeof(NR_DedicatedNAS_Message_t));
      memset(dedicatedNAS_Message, 0, sizeof(OCTET_STRING_t));
      OCTET_STRING_fromBuf(dedicatedNAS_Message,
                            (char *)ue_context_pP->ue_context.pdusession[i].param.nas_pdu.buffer,
                            ue_context_pP->ue_context.pdusession[i].param.nas_pdu.length);
      ASN_SEQUENCE_ADD(&dedicatedNAS_MessageList->list, dedicatedNAS_Message);
    }

    ue_context_pP->ue_context.pdusession[i].status = PDU_SESSION_STATUS_DONE;
    LOG_D(NR_RRC, "setting the status for the default DRB (index %d) to (%d,%s)\n",
          i, ue_context_pP->ue_context.pdusession[i].status, "PDU_SESSION_STATUS_DONE");
  }

822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
  if (ue_context_pP->ue_context.nas_pdu_flag == 1) {
    dedicatedNAS_Message = CALLOC(1, sizeof(NR_DedicatedNAS_Message_t));
    memset(dedicatedNAS_Message, 0, sizeof(OCTET_STRING_t));
    OCTET_STRING_fromBuf(dedicatedNAS_Message,
                          (char *)ue_context_pP->ue_context.nas_pdu.buffer,
                          ue_context_pP->ue_context.nas_pdu.length);
    ASN_SEQUENCE_ADD(&dedicatedNAS_MessageList->list, dedicatedNAS_Message);
  }

  /* If list is empty free the list and reset the address */
  if (dedicatedNAS_MessageList->list.count == 0) {
    free(dedicatedNAS_MessageList);
    dedicatedNAS_MessageList = NULL;
  }

  memset(buffer, 0, RRC_BUF_SIZE);
  size = do_RRCReconfiguration(ctxt_pP, buffer,
                                xid,
840 841
                                NULL,//*SRB_configList2,
                                NULL,//*DRB_configList,
842 843 844 845 846
                                NULL,
                                NULL,
                                NULL,
                                NULL,
                                dedicatedNAS_MessageList,
847
                                NULL,
848
                                NULL);
849 850 851

  free(ue_context_pP->ue_context.nas_pdu.buffer);

852
  LOG_DUMPMSG(NR_RRC, DEBUG_RRC,(char *)buffer, size, "[MSG] RRC Reconfiguration\n");
853 854 855 856 857 858 859 860 861

  /* Free all NAS PDUs */
  for (int i = 0; i < ue_context_pP->ue_context.nb_of_pdusessions; i++) {
    if (ue_context_pP->ue_context.pdusession[i].param.nas_pdu.buffer != NULL) {
      free(ue_context_pP->ue_context.pdusession[i].param.nas_pdu.buffer);
      ue_context_pP->ue_context.pdusession[i].param.nas_pdu.buffer = NULL;
    }
  }

862 863 864 865 866
  LOG_I(NR_RRC, "[gNB %d] Frame %d, Logical Channel DL-DCCH, Generate NR_RRCReconfiguration (bytes %d, UE id %x)\n",
          ctxt_pP->module_id,
          ctxt_pP->frame,
          size,
          ue_context_pP->ue_context.rnti);
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
  switch (RC.nrrrc[ctxt_pP->module_id]->node_type) {
    case ngran_gNB_CU:
      nr_rrc_data_req(ctxt_pP,
                  DCCH,
                  rrc_gNB_mui++,
                  SDU_CONFIRM_NO,
                  size,
                  buffer,
                  PDCP_TRANSMISSION_MODE_CONTROL);
      // rrc_pdcp_config_asn1_req

      break;

    case ngran_gNB_DU:
      // nothing to do for DU
      AssertFatal(1==0,"nothing to do for DU\n");
      break;

    case ngran_gNB:
886
    {
887
#ifdef ITTI_SIM
Xue Song's avatar
Xue Song committed
888
      MessageDef *message_p;
889
      uint8_t *message_buffer;
890
      message_buffer = itti_malloc (TASK_RRC_GNB, TASK_RRC_UE_SIM, size);
891
      memcpy (message_buffer, buffer, size);
892
      message_p = itti_alloc_new_message (TASK_RRC_GNB, 0, GNB_RRC_DCCH_DATA_IND);
893 894 895 896 897 898
      GNB_RRC_DCCH_DATA_IND (message_p).rbid = DCCH;
      GNB_RRC_DCCH_DATA_IND (message_p).sdu = message_buffer;
      GNB_RRC_DCCH_DATA_IND (message_p).size	= size;
      itti_send_msg_to_task (TASK_RRC_UE_SIM, ctxt_pP->instance, message_p);
#else
      LOG_D(NR_RRC, "[FRAME %05d][RRC_gNB][MOD %u][][--- PDCP_DATA_REQ/%d Bytes (rrcReconfiguration to UE %x MUI %d) --->][PDCP][MOD %u][RB %u]\n",
899 900 901 902 903 904 905
          ctxt_pP->frame,
          ctxt_pP->module_id,
          size,
          ue_context_pP->ue_context.rnti,
          rrc_gNB_mui,
          ctxt_pP->module_id,
          DCCH);
906
      MSC_LOG_TX_MESSAGE(MSC_RRC_GNB,
907 908 909 910 911 912 913 914
          MSC_RRC_UE,
          buffer,
          size,
          MSC_AS_TIME_FMT" NR_RRCReconfiguration UE %x MUI %d size %u",
          MSC_AS_TIME_ARGS(ctxt_pP),
          ue_context_pP->ue_context.rnti,
          rrc_gNB_mui,
          size);
915
      nr_rrc_data_req(ctxt_pP,
916 917 918 919 920 921
          DCCH,
          rrc_gNB_mui++,
          SDU_CONFIRM_NO,
          size,
          buffer,
          PDCP_TRANSMISSION_MODE_CONTROL);
922
      // rrc_pdcp_config_asn1_req
923
#endif
924
      // rrc_rlc_config_asn1_req
925
    }
926 927 928 929
    break;
    
  default :
    LOG_W(NR_RRC, "Unknown node type %d\n", RC.nrrrc[ctxt_pP->module_id]->node_type);
930
  }
Xue Song's avatar
Xue Song committed
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
//-----------------------------------------------------------------------------
void 
rrc_gNB_generate_dedicatedRRCReconfiguration(
    const protocol_ctxt_t     *const ctxt_pP,
    rrc_gNB_ue_context_t      *ue_context_pP
)
//-----------------------------------------------------------------------------
{
  NR_DRB_ToAddMod_t             *DRB_config           = NULL;
  NR_SRB_ToAddMod_t             *SRB2_config          = NULL;
  NR_SDAP_Config_t              *sdap_config          = NULL;
  NR_DRB_ToAddModList_t        **DRB_configList  = NULL;
  NR_DRB_ToAddModList_t        **DRB_configList2 = NULL;
  NR_SRB_ToAddModList_t        **SRB_configList2 = NULL;
  NR_SRB_ToAddModList_t        *SRB_configList  = ue_context_pP->ue_context.SRB_configList; 
  struct NR_RRCReconfiguration_v1530_IEs__dedicatedNAS_MessageList
                                *dedicatedNAS_MessageList = NULL;
  NR_DedicatedNAS_Message_t     *dedicatedNAS_Message = NULL;
  uint8_t                        buffer[RRC_BUF_SIZE];
  uint16_t                       size;
  int                            qos_flow_index = 0;
  NR_QFI_t                       qfi = 0;
  int                            pdu_sessions_done = 0;
  int i;
957
  NR_CellGroupConfig_t *cellGroupConfig;
958 959 960 961 962

  uint8_t xid = rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id);

  /* Configure SRB2 */
  SRB_configList2 = &ue_context_pP->ue_context.SRB_configList2[xid];
963 964 965 966 967 968 969
  if (*SRB_configList2 == NULL) {
    *SRB_configList2 = CALLOC(1, sizeof(**SRB_configList2));
    memset(*SRB_configList2, 0, sizeof(**SRB_configList2));
    SRB2_config = CALLOC(1, sizeof(*SRB2_config));
    SRB2_config->srb_Identity = 2;
    ASN_SEQUENCE_ADD(&(*SRB_configList2)->list, SRB2_config);
    ASN_SEQUENCE_ADD(&SRB_configList->list, SRB2_config);
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 995 996 997 998 999 1000 1001 1002 1003 1004
  }

  DRB_configList = &ue_context_pP->ue_context.DRB_configList;
  if (*DRB_configList) {
      free(*DRB_configList);
  }
  *DRB_configList = CALLOC(1, sizeof(**DRB_configList));
  memset(*DRB_configList, 0, sizeof(**DRB_configList));

  DRB_configList2 = &ue_context_pP->ue_context.DRB_configList2[xid];
  if (*DRB_configList2) {
      free(*DRB_configList2);
  }
  *DRB_configList2 = CALLOC(1, sizeof(**DRB_configList2));
  memset(*DRB_configList2, 0, sizeof(**DRB_configList2));

  dedicatedNAS_MessageList = CALLOC(1, sizeof(struct NR_RRCReconfiguration_v1530_IEs__dedicatedNAS_MessageList));

  for (i = 0; i < ue_context_pP->ue_context.setup_pdu_sessions; i++) {
    if (pdu_sessions_done >= ue_context_pP->ue_context.nb_of_pdusessions) {
      break;
    }

    if (ue_context_pP->ue_context.pdusession[i].status >= PDU_SESSION_STATUS_DONE) {
      continue;
    }

    DRB_config = CALLOC(1, sizeof(*DRB_config));
    DRB_config->drb_Identity = i+1;
    DRB_config->cnAssociation = CALLOC(1, sizeof(*DRB_config->cnAssociation));
    DRB_config->cnAssociation->present = NR_DRB_ToAddMod__cnAssociation_PR_sdap_Config;
    // sdap_Config
    sdap_config = CALLOC(1, sizeof(NR_SDAP_Config_t));
    memset(sdap_config, 0, sizeof(NR_SDAP_Config_t));
    sdap_config->pdu_Session = ue_context_pP->ue_context.pdusession[i].param.pdusession_id;
1005
    sdap_config->sdap_HeaderDL = NR_SDAP_Config__sdap_HeaderDL_absent;
1006
    sdap_config->sdap_HeaderUL = NR_SDAP_Config__sdap_HeaderUL_absent;
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
    sdap_config->defaultDRB = TRUE;
    sdap_config->mappedQoS_FlowsToAdd = calloc(1, sizeof(struct NR_SDAP_Config__mappedQoS_FlowsToAdd));
    memset(sdap_config->mappedQoS_FlowsToAdd, 0, sizeof(struct NR_SDAP_Config__mappedQoS_FlowsToAdd));

    for (qos_flow_index = 0; qos_flow_index < ue_context_pP->ue_context.pdusession[i].param.nb_qos; qos_flow_index++) {
      qfi = ue_context_pP->ue_context.pdusession[i].param.qos[qos_flow_index].qfi;
      ASN_SEQUENCE_ADD(&sdap_config->mappedQoS_FlowsToAdd->list, &qfi);
    }
    sdap_config->mappedQoS_FlowsToRelease = NULL;
    DRB_config->cnAssociation->choice.sdap_Config = sdap_config;

    // pdcp_Config
    DRB_config->reestablishPDCP = NULL;
    DRB_config->recoverPDCP = NULL;
    DRB_config->pdcp_Config = calloc(1, sizeof(*DRB_config->pdcp_Config));
    DRB_config->pdcp_Config->drb = calloc(1,sizeof(*DRB_config->pdcp_Config->drb));
    DRB_config->pdcp_Config->drb->discardTimer = calloc(1, sizeof(*DRB_config->pdcp_Config->drb->discardTimer));
    *DRB_config->pdcp_Config->drb->discardTimer = NR_PDCP_Config__drb__discardTimer_infinity;
    DRB_config->pdcp_Config->drb->pdcp_SN_SizeUL = calloc(1, sizeof(*DRB_config->pdcp_Config->drb->pdcp_SN_SizeUL));
    *DRB_config->pdcp_Config->drb->pdcp_SN_SizeUL = NR_PDCP_Config__drb__pdcp_SN_SizeUL_len18bits;
    DRB_config->pdcp_Config->drb->pdcp_SN_SizeDL = calloc(1, sizeof(*DRB_config->pdcp_Config->drb->pdcp_SN_SizeDL));
    *DRB_config->pdcp_Config->drb->pdcp_SN_SizeDL = NR_PDCP_Config__drb__pdcp_SN_SizeDL_len18bits;
    DRB_config->pdcp_Config->drb->headerCompression.present = NR_PDCP_Config__drb__headerCompression_PR_notUsed;
    DRB_config->pdcp_Config->drb->headerCompression.choice.notUsed = 0;

    DRB_config->pdcp_Config->drb->integrityProtection = NULL;
    DRB_config->pdcp_Config->drb->statusReportRequired = NULL;
    DRB_config->pdcp_Config->drb->outOfOrderDelivery = NULL;
    DRB_config->pdcp_Config->moreThanOneRLC = NULL;

    DRB_config->pdcp_Config->t_Reordering = calloc(1, sizeof(*DRB_config->pdcp_Config->t_Reordering));
    *DRB_config->pdcp_Config->t_Reordering = NR_PDCP_Config__t_Reordering_ms750;
    DRB_config->pdcp_Config->ext1 = NULL;

    // Reference TS23501 Table 5.7.4-1: Standardized 5QI to QoS characteristics mapping
    for (qos_flow_index = 0; qos_flow_index < ue_context_pP->ue_context.pdusession[i].param.nb_qos; qos_flow_index++) {
      switch (ue_context_pP->ue_context.pdusession[i].param.qos[qos_flow_index].fiveQI) {
        case 1: //100ms
        case 2: //150ms
        case 3: //50ms
        case 4: //300ms
        case 5: //100ms
        case 6: //300ms
        case 7: //100ms
        case 8: //300ms
        case 9: //300ms Video (Buffered Streaming)TCP-based (e.g., www, e-mail, chat, ftp, p2p file sharing, progressive video, etc.)
          // TODO
          break;

        default:
1057
          LOG_E(NR_RRC,"not supported 5qi %lu\n", ue_context_pP->ue_context.pdusession[i].param.qos[qos_flow_index].fiveQI);
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
          ue_context_pP->ue_context.pdusession[i].status = PDU_SESSION_STATUS_FAILED;
          ue_context_pP->ue_context.pdusession[i].xid = xid;
          pdu_sessions_done++;
          free(DRB_config);
          continue;
      }
    }

    ASN_SEQUENCE_ADD(&(*DRB_configList)->list, DRB_config);
    ASN_SEQUENCE_ADD(&(*DRB_configList2)->list, DRB_config);

    ue_context_pP->ue_context.pdusession[i].status = PDU_SESSION_STATUS_DONE;
    ue_context_pP->ue_context.pdusession[i].xid = xid;

    if (ue_context_pP->ue_context.pdusession[i].param.nas_pdu.buffer != NULL) {
      dedicatedNAS_Message = CALLOC(1, sizeof(NR_DedicatedNAS_Message_t));
      memset(dedicatedNAS_Message, 0, sizeof(OCTET_STRING_t));
      OCTET_STRING_fromBuf(dedicatedNAS_Message,
                            (char *)ue_context_pP->ue_context.pdusession[i].param.nas_pdu.buffer,
                            ue_context_pP->ue_context.pdusession[i].param.nas_pdu.length);
      ASN_SEQUENCE_ADD(&dedicatedNAS_MessageList->list, dedicatedNAS_Message);

      LOG_I(NR_RRC,"add NAS info with size %d (pdusession id %d)\n",ue_context_pP->ue_context.pdusession[i].param.nas_pdu.length, i);
    } else {
      // TODO
      LOG_E(NR_RRC,"no NAS info (pdusession id %d)\n", i);
    }
  }

  /* If list is empty free the list and reset the address */
  if (dedicatedNAS_MessageList->list.count == 0) {
    free(dedicatedNAS_MessageList);
    dedicatedNAS_MessageList = NULL;
  }

  memset(buffer, 0, RRC_BUF_SIZE);
1094
  cellGroupConfig = calloc(1, sizeof(NR_CellGroupConfig_t));
1095
  fill_mastercellGroupConfig(cellGroupConfig, ue_context_pP->ue_context.masterCellGroup);
1096 1097 1098 1099 1100 1101 1102 1103 1104
  size = do_RRCReconfiguration(ctxt_pP, buffer,
                                xid,
                                *SRB_configList2, 
                                *DRB_configList,
                                NULL,
                                NULL,
                                NULL,
                                NULL,
                                dedicatedNAS_MessageList,
1105 1106
                                NULL,
                                cellGroupConfig);
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 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
  LOG_DUMPMSG(NR_RRC,DEBUG_RRC,(char *)buffer,size,"[MSG] RRC Reconfiguration\n");

  /* Free all NAS PDUs */
  for (i = 0; i < ue_context_pP->ue_context.nb_of_pdusessions; i++) {
    if (ue_context_pP->ue_context.pdusession[i].param.nas_pdu.buffer != NULL) {
      /* Free the NAS PDU buffer and invalidate it */
      free(ue_context_pP->ue_context.pdusession[i].param.nas_pdu.buffer);
      ue_context_pP->ue_context.pdusession[i].param.nas_pdu.buffer = NULL;
    }
  }

  LOG_I(NR_RRC,
        "[gNB %d] Frame %d, Logical Channel DL-DCCH, Generate RRCReconfiguration (bytes %d, UE RNTI %x)\n",
        ctxt_pP->module_id, ctxt_pP->frame, size, ue_context_pP->ue_context.rnti);
  LOG_D(NR_RRC,
        "[FRAME %05d][RRC_gNB][MOD %u][][--- PDCP_DATA_REQ/%d Bytes (rrcReconfiguration to UE %x MUI %d) --->][PDCP][MOD %u][RB %u]\n",
        ctxt_pP->frame, ctxt_pP->module_id, size, ue_context_pP->ue_context.rnti, rrc_gNB_mui, ctxt_pP->module_id, DCCH);
  MSC_LOG_TX_MESSAGE(
    MSC_RRC_GNB,
    MSC_RRC_UE,
    buffer,
    size,
    MSC_AS_TIME_FMT" dedicated RRCReconfiguration UE %x MUI %d size %u",
    MSC_AS_TIME_ARGS(ctxt_pP),
    ue_context_pP->ue_context.rnti,
    rrc_gNB_mui,
    size);

#ifdef ITTI_SIM
  MessageDef *message_p;
  uint8_t *message_buffer;
  message_buffer = itti_malloc (TASK_RRC_GNB_SIM, TASK_RRC_UE_SIM, size);
  memcpy (message_buffer, buffer, size);
  message_p = itti_alloc_new_message (TASK_RRC_GNB_SIM, 0, GNB_RRC_DCCH_DATA_IND);
  GNB_RRC_DCCH_DATA_IND (message_p).rbid = DCCH;
  GNB_RRC_DCCH_DATA_IND (message_p).sdu = message_buffer;
  GNB_RRC_DCCH_DATA_IND (message_p).size	= size;
  itti_send_msg_to_task (TASK_RRC_UE_SIM, ctxt_pP->instance, message_p);
#else
  nr_rrc_data_req(
    ctxt_pP,
    DCCH,
    rrc_gNB_mui++,
    SDU_CONFIRM_NO,
    size,
    buffer,
    PDCP_TRANSMISSION_MODE_CONTROL);
#endif
}

1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
//-----------------------------------------------------------------------------
void
rrc_gNB_generate_dedicatedRRCReconfiguration_release(
    const protocol_ctxt_t   *const ctxt_pP,
    rrc_gNB_ue_context_t    *const ue_context_pP,
    uint8_t                  xid,
    uint32_t                 nas_length,
    uint8_t                 *nas_buffer)
//-----------------------------------------------------------------------------
{
  uint8_t                             buffer[RRC_BUF_SIZE];
  int                                 i;
  uint16_t                            size  = 0;
1170
  NR_DRB_ToReleaseList_t             **DRB_Release_configList2 = NULL;
1171
  NR_DRB_Identity_t                  *DRB_release;
1172 1173 1174
  struct NR_RRCReconfiguration_v1530_IEs__dedicatedNAS_MessageList
                                     *dedicatedNAS_MessageList = NULL;
  NR_DedicatedNAS_Message_t          *dedicatedNAS_Message     = NULL;
1175

1176 1177 1178
  DRB_Release_configList2 = &ue_context_pP->ue_context.DRB_Release_configList2[xid];
  if (*DRB_Release_configList2) {
    free(*DRB_Release_configList2);
1179 1180
  }

1181
  *DRB_Release_configList2 = CALLOC(1, sizeof(**DRB_Release_configList2));
1182 1183 1184 1185
  for(i = 0; i < NB_RB_MAX; i++) {
    if((ue_context_pP->ue_context.pdusession[i].status == PDU_SESSION_STATUS_TORELEASE) && ue_context_pP->ue_context.pdusession[i].xid == xid) {
      DRB_release = CALLOC(1, sizeof(NR_DRB_Identity_t));
      *DRB_release = i+1;
1186
      ASN_SEQUENCE_ADD(&(*DRB_Release_configList2)->list, DRB_release);
1187 1188 1189 1190 1191
    }
  }

  /* If list is empty free the list and reset the address */
  if (nas_length > 0) {
1192 1193 1194 1195 1196 1197 1198
    dedicatedNAS_MessageList = CALLOC(1, sizeof(struct NR_RRCReconfiguration_v1530_IEs__dedicatedNAS_MessageList));
    dedicatedNAS_Message = CALLOC(1, sizeof(NR_DedicatedNAS_Message_t));
    memset(dedicatedNAS_Message, 0, sizeof(OCTET_STRING_t));
    OCTET_STRING_fromBuf(dedicatedNAS_Message,
                         (char *)nas_buffer,
                         nas_length);
    ASN_SEQUENCE_ADD(&dedicatedNAS_MessageList->list, dedicatedNAS_Message);
1199 1200 1201 1202 1203 1204
    LOG_I(NR_RRC,"add NAS info with size %d\n", nas_length);
  } else {
    LOG_W(NR_RRC,"dedlicated NAS list is empty\n");
  }

  memset(buffer, 0, RRC_BUF_SIZE);
1205 1206 1207
  size = do_RRCReconfiguration(ctxt_pP, buffer, xid,
                               NULL,
                               NULL,
1208
                               *DRB_Release_configList2,
1209 1210 1211 1212
                               NULL,
                               NULL,
                               NULL,
                               dedicatedNAS_MessageList,
1213
                               NULL,
1214
                               NULL);
1215

1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
  ue_context_pP->ue_context.pdu_session_release_command_flag = 1;
  LOG_DUMPMSG(NR_RRC,DEBUG_RRC,(char *)buffer,size,
              "[MSG] RRC Reconfiguration\n");

  /* Free all NAS PDUs */
  if (nas_length > 0) {
    /* Free the NAS PDU buffer and invalidate it */
    free(nas_buffer);
  }

  LOG_I(NR_RRC,
        "[gNB %d] Frame %d, Logical Channel DL-DCCH, Generate NR_RRCReconfiguration (bytes %d, UE RNTI %x)\n",
        ctxt_pP->module_id, ctxt_pP->frame, size, ue_context_pP->ue_context.rnti);
  LOG_D(NR_RRC,
        "[FRAME %05d][RRC_gNB][MOD %u][][--- PDCP_DATA_REQ/%d Bytes (rrcReconfiguration to UE %x MUI %d) --->][PDCP][MOD %u][RB %u]\n",
        ctxt_pP->frame, ctxt_pP->module_id, size, ue_context_pP->ue_context.rnti, rrc_gNB_mui, ctxt_pP->module_id, DCCH);
  MSC_LOG_TX_MESSAGE(
    MSC_RRC_GNB,
    MSC_RRC_UE,
    buffer,
    size,
    MSC_AS_TIME_FMT" dedicated NR_RRCReconfiguration UE %x MUI %d size %u",
    MSC_AS_TIME_ARGS(ctxt_pP),
    ue_context_pP->ue_context.rnti,
    rrc_gNB_mui,
    size);
#ifdef ITTI_SIM
1243 1244
  MessageDef *message_p;
  uint8_t *message_buffer;
yaojie's avatar
yaojie committed
1245
  message_buffer = itti_malloc (TASK_RRC_GNB, TASK_RRC_UE_SIM, size);
1246
  memcpy (message_buffer, buffer, size);
1247
  message_p = itti_alloc_new_message (TASK_RRC_GNB, 0, GNB_RRC_DCCH_DATA_IND);
1248 1249 1250 1251
  GNB_RRC_DCCH_DATA_IND (message_p).rbid = DCCH;
  GNB_RRC_DCCH_DATA_IND (message_p).sdu = message_buffer;
  GNB_RRC_DCCH_DATA_IND (message_p).size	= size;
  itti_send_msg_to_task (TASK_RRC_UE_SIM, ctxt_pP->instance, message_p);
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
#else
  nr_rrc_data_req(
    ctxt_pP,
    DCCH,
    rrc_gNB_mui++,
    SDU_CONFIRM_NO,
    size,
    buffer,
    PDCP_TRANSMISSION_MODE_CONTROL);
#endif
}

1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
//-----------------------------------------------------------------------------
/*
* Process the RRC Reconfiguration Complete from the UE
*/
void
rrc_gNB_process_RRCReconfigurationComplete(
    const protocol_ctxt_t *const ctxt_pP,
    rrc_gNB_ue_context_t  *ue_context_pP,
    const uint8_t xid
)
{
1275 1276 1277 1278 1279 1280 1281 1282
  int                                 drb_id;
  uint8_t                            *kRRCenc = NULL;
  uint8_t                            *kRRCint = NULL;
  uint8_t                            *kUPenc = NULL;
  NR_DRB_ToAddModList_t              *DRB_configList = ue_context_pP->ue_context.DRB_configList2[xid];
  NR_SRB_ToAddModList_t              *SRB_configList = ue_context_pP->ue_context.SRB_configList2[xid];
  NR_DRB_ToReleaseList_t             *DRB_Release_configList2 = ue_context_pP->ue_context.DRB_Release_configList2[xid];
  NR_DRB_Identity_t                  *drb_id_p      = NULL;
1283
//  uint8_t                             nr_DRB2LCHAN[8];
1284
  gNB_RRC_INST *rrc = RC.nrrrc[ctxt_pP->module_id];
1285 1286

  ue_context_pP->ue_context.ue_reestablishment_timer = 0;
1287

1288
#ifndef PHYSIM
1289 1290 1291 1292 1293 1294
  /* Derive the keys from kgnb */
  if (DRB_configList != NULL) {
      derive_key_up_enc(ue_context_pP->ue_context.ciphering_algorithm,
                      ue_context_pP->ue_context.kgnb,
                      &kUPenc);
  }
1295

1296 1297 1298 1299 1300 1301
  derive_key_rrc_enc(ue_context_pP->ue_context.ciphering_algorithm,
                      ue_context_pP->ue_context.kgnb,
                      &kRRCenc);
  derive_key_rrc_int(ue_context_pP->ue_context.integrity_algorithm,
                      ue_context_pP->ue_context.kgnb,
                      &kRRCint);
1302
#endif
1303 1304 1305 1306
  /* Refresh SRBs/DRBs */
  MSC_LOG_TX_MESSAGE(MSC_RRC_GNB, MSC_PDCP_ENB, NULL, 0, MSC_AS_TIME_FMT" CONFIG_REQ UE %x DRB (security unchanged)",
                   MSC_AS_TIME_ARGS(ctxt_pP),
                   ue_context_pP->ue_context.rnti);
1307

Xue Song's avatar
Xue Song committed
1308
#ifndef ITTI_SIM
1309 1310
  LOG_I(NR_RRC,"Configuring PDCP DRBs/SRBs for UE %x\n",ue_context_pP->ue_context.rnti);
  
1311 1312 1313 1314
  nr_rrc_pdcp_config_asn1_req(ctxt_pP,
                              SRB_configList, // NULL,
                              DRB_configList,
                              DRB_Release_configList2,
1315
                              0, // already configured during the securitymodecommand
1316 1317 1318 1319 1320
                              kRRCenc,
                              kRRCint,
                              kUPenc,
                              NULL,
                              NULL,
1321
                              NULL,
1322
                              ue_context_pP->ue_context.masterCellGroup->rlc_BearerToAddModList);
1323
  /* Refresh SRBs/DRBs */
Xue Song's avatar
Xue Song committed
1324
  if (!NODE_IS_CU(RC.nrrrc[ctxt_pP->module_id]->node_type)) {
1325 1326 1327 1328 1329 1330 1331 1332 1333
    rrc_mac_config_req_gNB(rrc->module_id,
                           rrc->carrier.ssb_SubcarrierOffset,
                           rrc->carrier.pdsch_AntennaPorts,
                           rrc->carrier.pusch_AntennaPorts,
                           NULL,
                           0,
                           ue_context_pP->ue_context.rnti,
                           ue_context_pP->ue_context.masterCellGroup
                           );
1334
    LOG_I(NR_RRC,"Configuring RLC DRBs/SRBs for UE %x\n",ue_context_pP->ue_context.rnti);
Xue Song's avatar
Xue Song committed
1335
    nr_rrc_rlc_config_asn1_req(ctxt_pP,
1336 1337 1338 1339
                          SRB_configList, // NULL,
                          DRB_configList,
                          DRB_Release_configList2,
                          NULL,
1340
                          ue_context_pP->ue_context.masterCellGroup->rlc_BearerToAddModList,
1341
                          NULL);
Xue Song's avatar
Xue Song committed
1342
  }
Xue Song's avatar
Xue Song committed
1343
#endif
1344

1345 1346 1347 1348 1349
  /* Set the SRB active in UE context */
  if (SRB_configList != NULL) {
    for (int i = 0; (i < SRB_configList->list.count) && (i < 3); i++) {
      if (SRB_configList->list.array[i]->srb_Identity == 1) {
        ue_context_pP->ue_context.Srb1.Active = 1;
1350
        ue_context_pP->ue_context.Srb1.Srb_info.Srb_id = 1;
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
      } else if (SRB_configList->list.array[i]->srb_Identity == 2) {
        ue_context_pP->ue_context.Srb2.Active = 1;
        ue_context_pP->ue_context.Srb2.Srb_info.Srb_id = 2;
        LOG_I(NR_RRC,"[gNB %d] Frame      %d CC %d : SRB2 is now active\n",
              ctxt_pP->module_id,
              ctxt_pP->frame,
              ue_context_pP->ue_context.primaryCC_id);
      } else {
        LOG_W(NR_RRC,"[gNB %d] Frame      %d CC %d : invalide SRB identity %ld\n",
              ctxt_pP->module_id,
              ctxt_pP->frame,
              ue_context_pP->ue_context.primaryCC_id,
              SRB_configList->list.array[i]->srb_Identity);
yaojie's avatar
yaojie committed
1364 1365
      }
    }
1366

1367 1368 1369
    free(SRB_configList);
    ue_context_pP->ue_context.SRB_configList2[xid] = NULL;
  }
1370

1371 1372 1373 1374 1375
  /* Loop through DRBs and establish if necessary */
  if (DRB_configList != NULL) {
    for (int i = 0; i < DRB_configList->list.count; i++) {
      if (DRB_configList->list.array[i]) {
        drb_id = (int)DRB_configList->list.array[i]->drb_Identity;
yaojie's avatar
yaojie committed
1376
        LOG_I(NR_RRC, "[gNB %d] Frame  %d : Logical Channel UL-DCCH, Received NR_RRCReconfigurationComplete from UE rnti %x, reconfiguring DRB %d\n",
1377 1378 1379
            ctxt_pP->module_id,
            ctxt_pP->frame,
            ctxt_pP->rnti,
yaojie's avatar
yaojie committed
1380 1381
            (int)DRB_configList->list.array[i]->drb_Identity);
            //(int)*DRB_configList->list.array[i]->pdcp_Config->moreThanOneRLC->primaryPath.logicalChannel);
1382

1383 1384 1385 1386
        if (ue_context_pP->ue_context.DRB_active[drb_id] == 0) {
          ue_context_pP->ue_context.DRB_active[drb_id] = 1;
          LOG_D(NR_RRC, "[gNB %d] Frame %d: Establish RLC UM Bidirectional, DRB %d Active\n",
                  ctxt_pP->module_id, ctxt_pP->frame, (int)DRB_configList->list.array[i]->drb_Identity);
1387

1388 1389 1390
          LOG_D(NR_RRC,
                  PROTOCOL_NR_RRC_CTXT_UE_FMT" RRC_gNB --- MAC_CONFIG_REQ  (DRB) ---> MAC_gNB\n",
                  PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
1391

yaojie's avatar
yaojie committed
1392 1393 1394
          //if (DRB_configList->list.array[i]->pdcp_Config->moreThanOneRLC->primaryPath.logicalChannel) {
          //  nr_DRB2LCHAN[i] = (uint8_t) * DRB_configList->list.array[i]->pdcp_Config->moreThanOneRLC->primaryPath.logicalChannel;
          //}
1395

1396 1397 1398
            // rrc_mac_config_req_eNB
        } else {        // remove LCHAN from MAC/PHY
          if (ue_context_pP->ue_context.DRB_active[drb_id] == 1) {
yoshio.inoue's avatar
yoshio.inoue committed
1399
            /* TODO : It may be needed if gNB goes into full stack working. */
1400 1401 1402 1403
            // DRB has just been removed so remove RLC + PDCP for DRB
            /*      rrc_pdcp_config_req (ctxt_pP->module_id, frameP, 1, CONFIG_ACTION_REMOVE,
            (ue_mod_idP * NB_RB_MAX) + DRB2LCHAN[i],UNDEF_SECURITY_MODE);
            */
yaojie's avatar
yaojie committed
1404
            /*rrc_rlc_config_req(ctxt_pP,
1405 1406 1407 1408
                                SRB_FLAG_NO,
                                MBMS_FLAG_NO,
                                CONFIG_ACTION_REMOVE,
                                nr_DRB2LCHAN[i],
yaojie's avatar
yaojie committed
1409
                                Rlc_info_um);*/
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
          }

          ue_context_pP->ue_context.DRB_active[drb_id] = 0;
          LOG_D(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" RRC_eNB --- MAC_CONFIG_REQ  (DRB) ---> MAC_eNB\n",
                  PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));

          // rrc_mac_config_req_eNB

        } // end else of if (ue_context_pP->ue_context.DRB_active[drb_id] == 0)
      } // end if (DRB_configList->list.array[i])
    } // end for (int i = 0; i < DRB_configList->list.count; i++)

    free(DRB_configList);
    ue_context_pP->ue_context.DRB_configList2[xid] = NULL;
  } // end if DRB_configList != NULL

  if(DRB_Release_configList2 != NULL) {
    for (int i = 0; i < DRB_Release_configList2->list.count; i++) {
      if (DRB_Release_configList2->list.array[i]) {
        drb_id_p = DRB_Release_configList2->list.array[i];
        drb_id = *drb_id_p;

        if (ue_context_pP->ue_context.DRB_active[drb_id] == 1) {
        ue_context_pP->ue_context.DRB_active[drb_id] = 0;
1434
        }
1435
      }
1436
    }
1437 1438 1439 1440

    free(DRB_Release_configList2);
    ue_context_pP->ue_context.DRB_Release_configList2[xid] = NULL;
  }
1441
}
1442

yaojie's avatar
yaojie committed
1443 1444 1445 1446 1447 1448 1449 1450
//-----------------------------------------------------------------------------
void
rrc_gNB_generate_RRCReestablishment(
  const protocol_ctxt_t *const ctxt_pP,
  rrc_gNB_ue_context_t  *const ue_context_pP,
  const int             CC_id)
//-----------------------------------------------------------------------------
{
1451
    // int UE_id = -1;
yaojie's avatar
yaojie committed
1452 1453
    //NR_LogicalChannelConfig_t  *SRB1_logicalChannelConfig = NULL;
    NR_SRB_ToAddModList_t      **SRB_configList;
1454
    // NR_SRB_ToAddMod_t          *SRB1_config = NULL;
yaojie's avatar
yaojie committed
1455 1456 1457
    //rrc_gNB_carrier_data_t     *carrier = NULL;
    gNB_RRC_UE_t               *ue_context = NULL;
    module_id_t                 module_id = ctxt_pP->module_id;
1458
    // uint16_t                    rnti = ctxt_pP->rnti;
yaojie's avatar
yaojie committed
1459 1460 1461 1462 1463 1464 1465 1466 1467
    
    SRB_configList = &(ue_context_pP->ue_context.SRB_configList);
    //carrier = &(RC.nrrrc[ctxt_pP->module_id]->carrier);
    ue_context = &(ue_context_pP->ue_context);
    ue_context->Srb0.Tx_buffer.payload_size = do_RRCReestablishment(ctxt_pP,
        ue_context_pP,
        CC_id,
        (uint8_t *) ue_context->Srb0.Tx_buffer.Payload,
        //(uint8_t) carrier->p_gNB, // at this point we do not have the UE capability information, so it can only be TM1 or TM2
1468 1469
        rrc_gNB_get_next_transaction_identifier(module_id),
        SRB_configList
yaojie's avatar
yaojie committed
1470 1471 1472 1473 1474
        //&(ue_context->physicalConfigDedicated)
        );

    /* Configure SRB1 for UE */
    if (*SRB_configList != NULL) {
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
      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];
        }

        LOG_D(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" RRC_gNB --- MAC_CONFIG_REQ  (SRB1) ---> MAC_gNB\n",
              PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));

        // rrc_mac_config_req_eNB
      }
yaojie's avatar
yaojie committed
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
    }  // if (*SRB_configList != NULL)
    
    MSC_LOG_TX_MESSAGE(MSC_RRC_GNB,
                       MSC_RRC_UE,
                       ue_context->Srb0.Tx_buffer.Header,
                       ue_context->Srb0.Tx_buffer.payload_size,
                       MSC_AS_TIME_FMT" NR_RRCReestablishment UE %x size %u",
                       MSC_AS_TIME_ARGS(ctxt_pP),
                       ue_context->rnti,
                       ue_context->Srb0.Tx_buffer.payload_size);
    LOG_I(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" [RAPROC] Logical Channel DL-DCCH, Generating NR_RRCReestablishment (bytes %d)\n",
          PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
          ue_context->Srb0.Tx_buffer.payload_size);
1498
#if(0)
yoshio.inoue's avatar
yoshio.inoue committed
1499
    /* TODO : It may be needed if gNB goes into full stack working. */
1500
    UE_id = find_nr_UE_id(module_id, rnti);
yaojie's avatar
yaojie committed
1501 1502
    if (UE_id != -1) {
      /* Activate reject timer, if RRCComplete not received after 10 frames, reject UE */
1503
      RC.nrmac[module_id]->UE_info.UE_sched_ctrl[UE_id].ue_reestablishment_reject_timer = 1;
yaojie's avatar
yaojie committed
1504
      /* Reject UE after 10 frames, LTE_RRCConnectionReestablishmentReject is triggered */
1505
      RC.nrmac[module_id]->UE_info.UE_sched_ctrl[UE_id].ue_reestablishment_reject_timer_thres = 100;
yaojie's avatar
yaojie committed
1506 1507 1508 1509 1510
    } else {
      LOG_E(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" Generating NR_RRCReestablishment without UE_id(MAC) rnti %x\n",
            PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
            rnti);
    }
1511
#endif
yaojie's avatar
yaojie committed
1512 1513 1514 1515 1516
#ifdef ITTI_SIM
        MessageDef *message_p;
        uint8_t *message_buffer;
        message_buffer = itti_malloc (TASK_RRC_GNB, TASK_RRC_UE_SIM, ue_context->Srb0.Tx_buffer.payload_size);
        memcpy (message_buffer, (uint8_t *) ue_context->Srb0.Tx_buffer.Payload, ue_context->Srb0.Tx_buffer.payload_size);
1517
        message_p = itti_alloc_new_message (TASK_RRC_GNB, 0, GNB_RRC_DCCH_DATA_IND);
yaojie's avatar
yaojie committed
1518 1519 1520 1521 1522 1523 1524 1525
        GNB_RRC_DCCH_DATA_IND (message_p).rbid = DCCH;
        GNB_RRC_DCCH_DATA_IND (message_p).sdu = message_buffer;
        GNB_RRC_DCCH_DATA_IND (message_p).size  = ue_context->Srb0.Tx_buffer.payload_size;
        itti_send_msg_to_task (TASK_RRC_UE_SIM, ctxt_pP->instance, message_p);
#endif

}

1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
//-----------------------------------------------------------------------------
void
rrc_gNB_process_RRCConnectionReestablishmentComplete(
  const protocol_ctxt_t *const ctxt_pP,
  const rnti_t reestablish_rnti,
  rrc_gNB_ue_context_t         *ue_context_pP,
  const uint8_t xid
)
//-----------------------------------------------------------------------------
{
  LOG_I(NR_RRC,
        PROTOCOL_RRC_CTXT_UE_FMT" [RAPROC] Logical Channel UL-DCCH, processing NR_RRCConnectionReestablishmentComplete from UE (SRB1 Active)\n",
        PROTOCOL_RRC_CTXT_UE_ARGS(ctxt_pP));

  NR_DRB_ToAddModList_t                 *DRB_configList = ue_context_pP->ue_context.DRB_configList;
  NR_SRB_ToAddModList_t                 *SRB_configList = ue_context_pP->ue_context.SRB_configList;
  NR_SRB_ToAddModList_t                **SRB_configList2 = NULL;
  NR_DRB_ToAddModList_t                **DRB_configList2 = NULL;
  NR_SRB_ToAddMod_t                     *SRB2_config     = NULL;
  NR_DRB_ToAddMod_t                     *DRB_config      = NULL;
  //NR_SDAP_Config_t                      *sdap_config     = NULL;
  int i = 0;
  uint8_t                             buffer[RRC_BUF_SIZE];
  uint16_t                            size;

  uint8_t next_xid = rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id);
  int ret = 0;
1553
  ue_context_pP->ue_context.Status = NR_RRC_CONNECTED;
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
  ue_context_pP->ue_context.ue_rrc_inactivity_timer = 1; // set rrc inactivity when UE goes into RRC_CONNECTED
  ue_context_pP->ue_context.reestablishment_xid = next_xid;
  SRB_configList2 = &ue_context_pP->ue_context.SRB_configList2[xid];

  // get old configuration of SRB2
  if (*SRB_configList2 != NULL) {
    if((*SRB_configList2)->list.count!=0) {
      LOG_D(NR_RRC, "SRB_configList2(%p) count is %d\n           SRB_configList2->list.array[0] addr is %p",
            SRB_configList2, (*SRB_configList2)->list.count,  (*SRB_configList2)->list.array[0]);
    }

    for (i = 0; (i < (*SRB_configList2)->list.count) && (i < 3); i++) {
      if ((*SRB_configList2)->list.array[i]->srb_Identity == 2 ) {
        LOG_D(NR_RRC, "get SRB2_config from (ue_context_pP->ue_context.SRB_configList2[%d])\n", xid);
        SRB2_config = (*SRB_configList2)->list.array[i];
        break;
      }
    }
  }

1574 1575 1576
  // SRB2_config = CALLOC(1, sizeof(*SRB2_config));
  // SRB2_config->srb_Identity = 2;

1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
  SRB_configList2 = &(ue_context_pP->ue_context.SRB_configList2[next_xid]);
  DRB_configList2 = &(ue_context_pP->ue_context.DRB_configList2[next_xid]);

  if (*SRB_configList2) {
    free(*SRB_configList2);
    LOG_D(NR_RRC, "free(ue_context_pP->ue_context.SRB_configList2[%d])\n", next_xid);
  }

  *SRB_configList2 = CALLOC(1, sizeof(**SRB_configList2));

  if (SRB2_config != NULL) {
    // Add SRB2 to SRB configuration list
    ASN_SEQUENCE_ADD(&SRB_configList->list, SRB2_config);
    ASN_SEQUENCE_ADD(&(*SRB_configList2)->list, SRB2_config);
    LOG_D(NR_RRC, "Add SRB2_config (srb_Identity:%ld) to ue_context_pP->ue_context.SRB_configList\n",
          SRB2_config->srb_Identity);
    LOG_D(NR_RRC, "Add SRB2_config (srb_Identity:%ld) to ue_context_pP->ue_context.SRB_configList2[%d]\n",
          SRB2_config->srb_Identity, next_xid);
  } else {
    // SRB configuration list only contains SRB1.
    LOG_W(NR_RRC,"SRB2 configuration does not exist in SRB configuration list\n");
  }

  if (*DRB_configList2) {
    free(*DRB_configList2);
    LOG_D(NR_RRC, "free(ue_context_pP->ue_context.DRB_configList2[%d])\n", next_xid);
  }

  *DRB_configList2 = CALLOC(1, sizeof(**DRB_configList2));

  if (DRB_configList != NULL) {
    LOG_D(NR_RRC, "get DRB_config from (ue_context_pP->ue_context.DRB_configList)\n");

    for (i = 0; (i < DRB_configList->list.count) && (i < 3); i++) {
      DRB_config = DRB_configList->list.array[i];
      // Add DRB to DRB configuration list, for LTE_RRCConnectionReconfigurationComplete
      ASN_SEQUENCE_ADD(&(*DRB_configList2)->list, DRB_config);
    }
  }

  ue_context_pP->ue_context.Srb1.Active = 1;
  //ue_context_pP->ue_context.Srb2.Srb_info.Srb_id = 2;

  if (AMF_MODE_ENABLED) {
    hashtable_rc_t    h_rc;
    int               j;
    rrc_ue_ngap_ids_t *rrc_ue_ngap_ids_p = NULL;
    uint16_t ue_initial_id = ue_context_pP->ue_context.ue_initial_id;
    uint32_t gNB_ue_ngap_id = ue_context_pP->ue_context.gNB_ue_ngap_id;
    gNB_RRC_INST *rrc_instance_p = RC.nrrrc[GNB_INSTANCE_TO_MODULE_ID(ctxt_pP->instance)];

    if (gNB_ue_ngap_id > 0) {
      h_rc = hashtable_get(rrc_instance_p->ngap_id2_ngap_ids, (hash_key_t)gNB_ue_ngap_id, (void **)&rrc_ue_ngap_ids_p);

      if  (h_rc == HASH_TABLE_OK) {
    	  rrc_ue_ngap_ids_p->ue_rnti = ctxt_pP->rnti;
      }
    }

    if (ue_initial_id != 0) {
      h_rc = hashtable_get(rrc_instance_p->initial_id2_ngap_ids, (hash_key_t)ue_initial_id, (void **)&rrc_ue_ngap_ids_p);

      if  (h_rc == HASH_TABLE_OK) {
    	  rrc_ue_ngap_ids_p->ue_rnti = ctxt_pP->rnti;
      }
    }

    gtpv1u_gnb_create_tunnel_req_t  create_tunnel_req;
    /* Save e RAB information for later */
    memset(&create_tunnel_req, 0, sizeof(create_tunnel_req));

    for ( j = 0, i = 0; i < NB_RB_MAX; i++) {
      if (ue_context_pP->ue_context.pdusession[i].status == PDU_SESSION_STATUS_ESTABLISHED || ue_context_pP->ue_context.pdusession[i].status == PDU_SESSION_STATUS_DONE) {
        create_tunnel_req.pdusession_id[j]   = ue_context_pP->ue_context.pdusession[i].param.pdusession_id;
        create_tunnel_req.upf_NGu_teid[j]  = ue_context_pP->ue_context.pdusession[i].param.gtp_teid;
        memcpy(create_tunnel_req.upf_addr[j].buffer,
               ue_context_pP->ue_context.pdusession[i].param.upf_addr.buffer,
                sizeof(uint8_t)*20);
        create_tunnel_req.upf_addr[j].length = ue_context_pP->ue_context.pdusession[i].param.upf_addr.length;
        j++;
      }
    }

    create_tunnel_req.rnti       = ctxt_pP->rnti; // warning put zero above
    create_tunnel_req.num_tunnels    = j;
    ret = gtpv1u_update_ngu_tunnel(
            ctxt_pP->instance,
            &create_tunnel_req,
            reestablish_rnti);

    if ( ret != 0 ) {
1668
      LOG_E(NR_RRC,"gtpv1u_update_ngu_tunnel failed,start to release UE %x\n",reestablish_rnti);
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731

      // update s1u tunnel failed,reset rnti?
      if (gNB_ue_ngap_id > 0) {
        h_rc = hashtable_get(rrc_instance_p->ngap_id2_ngap_ids, (hash_key_t)gNB_ue_ngap_id, (void **)&rrc_ue_ngap_ids_p);

        if (h_rc == HASH_TABLE_OK ) {
        	rrc_ue_ngap_ids_p->ue_rnti = reestablish_rnti;
        }
      }

      if (ue_initial_id != 0) {
        h_rc = hashtable_get(rrc_instance_p->initial_id2_ngap_ids, (hash_key_t)ue_initial_id, (void **)&rrc_ue_ngap_ids_p);

        if (h_rc == HASH_TABLE_OK ) {
          rrc_ue_ngap_ids_p->ue_rnti = reestablish_rnti;
        }
      }

      ue_context_pP->ue_context.ue_release_timer_s1 = 1;
      ue_context_pP->ue_context.ue_release_timer_thres_s1 = 100;
      ue_context_pP->ue_context.ue_release_timer = 0;
      ue_context_pP->ue_context.ue_reestablishment_timer = 0;
      ue_context_pP->ue_context.ul_failure_timer = 20000; // set ul_failure to 20000 for triggering rrc_eNB_send_S1AP_UE_CONTEXT_RELEASE_REQ
      ue_context_pP->ue_context.ul_failure_timer = 0;
      return;
    }
  } /* AMF_MODE_ENABLED */

  /* Update RNTI in ue_context */
  ue_context_pP->ue_id_rnti                    = ctxt_pP->rnti; // here ue_id_rnti is just a key, may be something else
  ue_context_pP->ue_context.rnti               = ctxt_pP->rnti;

  if (AMF_MODE_ENABLED) {
    uint8_t send_security_mode_command = FALSE;
    nr_rrc_pdcp_config_security(
      ctxt_pP,
      ue_context_pP,
      send_security_mode_command);
    LOG_D(NR_RRC, "set security successfully \n");
  }

  /* Add all NAS PDUs to the list */
  for (i = 0; i < ue_context_pP->ue_context.nb_of_pdusessions; i++) {

    /* TODO parameters yet to process ... */
    /* TODO should test if pdu session are Ok before! */
    ue_context_pP->ue_context.pdusession[i].status = PDU_SESSION_STATUS_DONE;
    ue_context_pP->ue_context.pdusession[i].xid    = xid;
    LOG_D(NR_RRC, "setting the status for the default DRB (index %d) to (%d,%s)\n",
          i, ue_context_pP->ue_context.pdusession[i].status, "PDU_SESSION_STATUS_DONE");
  }

  memset(buffer, 0, RRC_BUF_SIZE);

  size = do_RRCReconfiguration(ctxt_pP, buffer,
                                xid,
                               *SRB_configList2,
                                DRB_configList,
                                NULL,
                                NULL,
                                NULL,
                                NULL, // MeasObj_list,
                                NULL,
1732
                                NULL,
1733 1734
                                NULL);
  LOG_DUMPMSG(NR_RRC,DEBUG_RRC,(char *)buffer,size,
1735
              "[MSG] RRC Reconfiguration\n");
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746

  /* Free all NAS PDUs */
  for (i = 0; i < ue_context_pP->ue_context.nb_of_pdusessions; i++) {
    if (ue_context_pP->ue_context.pdusession[i].param.nas_pdu.buffer != NULL) {
      /* Free the NAS PDU buffer and invalidate it */
      free(ue_context_pP->ue_context.pdusession[i].param.nas_pdu.buffer);
      ue_context_pP->ue_context.pdusession[i].param.nas_pdu.buffer = NULL;
    }
  }

  if(size==65535) {
1747
    LOG_E(NR_RRC,"RRC decode err!!! do_RRCReconfiguration\n");
1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
    return;
  } else {
    LOG_I(NR_RRC,
          "[gNB %d] Frame %d, Logical Channel DL-DCCH, Generate NR_RRCConnectionReconfiguration (bytes %d, UE id %x)\n",
          ctxt_pP->module_id, ctxt_pP->frame, size, ue_context_pP->ue_context.rnti);
    LOG_D(NR_RRC,
          "[FRAME %05d][RRC_gNB][MOD %u][][--- PDCP_DATA_REQ/%d Bytes (rrcConnectionReconfiguration to UE %x MUI %d) --->][PDCP][MOD %u][RB %u]\n",
          ctxt_pP->frame, ctxt_pP->module_id, size, ue_context_pP->ue_context.rnti, rrc_gNB_mui, ctxt_pP->module_id, DCCH);
    MSC_LOG_TX_MESSAGE(
      MSC_RRC_GNB,
      MSC_RRC_UE,
      buffer,
      size,
      MSC_AS_TIME_FMT" LTE_RRCConnectionReconfiguration UE %x MUI %d size %u",
      MSC_AS_TIME_ARGS(ctxt_pP),
      ue_context_pP->ue_context.rnti,
      rrc_gNB_mui,
      size);
1766
#ifdef ITTI_SIM
1767 1768 1769 1770 1771 1772 1773 1774 1775
  MessageDef *message_p;
  uint8_t *message_buffer;
  message_buffer = itti_malloc (TASK_RRC_GNB, TASK_RRC_UE_SIM, size);
  memcpy (message_buffer, buffer, size);
  message_p = itti_alloc_new_message (TASK_RRC_GNB, 0, GNB_RRC_DCCH_DATA_IND);
  GNB_RRC_DCCH_DATA_IND (message_p).rbid = DCCH;
  GNB_RRC_DCCH_DATA_IND (message_p).sdu = message_buffer;
  GNB_RRC_DCCH_DATA_IND (message_p).size	= size;
  itti_send_msg_to_task (TASK_RRC_UE_SIM, ctxt_pP->instance, message_p);
1776
#else
1777 1778 1779 1780 1781 1782 1783 1784
    nr_rrc_data_req(
      ctxt_pP,
      DCCH,
      rrc_gNB_mui++,
      SDU_CONFIRM_NO,
      size,
      buffer,
      PDCP_TRANSMISSION_MODE_CONTROL);
1785
#endif
1786 1787
  }
}
1788 1789
//-----------------------------------------------------------------------------

1790
/*------------------------------------------------------------------------------*/
Xue Song's avatar
Xue Song committed
1791 1792 1793
int nr_rrc_gNB_decode_ccch(protocol_ctxt_t    *const ctxt_pP,
                           const uint8_t      *buffer,
                           int                buffer_length,
1794
			   OCTET_STRING_t     *du_to_cu_rrc_container,
Xue Song's avatar
Xue Song committed
1795
                           const int          CC_id)
1796
{
1797
  module_id_t                                       Idx;
1798 1799 1800 1801 1802
  asn_dec_rval_t                                    dec_rval;
  NR_UL_CCCH_Message_t                             *ul_ccch_msg = NULL;
  struct rrc_gNB_ue_context_s                      *ue_context_p = NULL;
  gNB_RRC_INST                                     *gnb_rrc_inst = RC.nrrrc[ctxt_pP->module_id];
  NR_RRCSetupRequest_IEs_t                         *rrcSetupRequest = NULL;
1803 1804 1805
  NR_RRCReestablishmentRequest_IEs_t                rrcReestablishmentRequest;
  uint64_t                                          random_value = 0;
  int i;
1806

1807 1808 1809 1810 1811 1812 1813
    dec_rval = uper_decode( NULL,
                            &asn_DEF_NR_UL_CCCH_Message,
                            (void **)&ul_ccch_msg,
                            (uint8_t *) buffer,
                            100,
                            0,
                            0);
1814

1815
    if (dec_rval.consumed == 0) {
yaojie's avatar
yaojie committed
1816 1817 1818 1819 1820
        /* TODO */
        LOG_E(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" FATAL Error in receiving CCCH\n",
                   PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
        return -1;
    }
1821

yaojie's avatar
yaojie committed
1822 1823
    if (ul_ccch_msg->message.present == NR_UL_CCCH_MessageType_PR_c1) {
     switch (ul_ccch_msg->message.choice.c1->present) {
1824
      case NR_UL_CCCH_MessageType__c1_PR_NOTHING:
1825
        /* TODO */
1826 1827 1828 1829 1830 1831
        LOG_I(NR_RRC,
            PROTOCOL_NR_RRC_CTXT_FMT" Received PR_NOTHING on UL-CCCH-Message\n",
            PROTOCOL_NR_RRC_CTXT_ARGS(ctxt_pP));
        break;

      case NR_UL_CCCH_MessageType__c1_PR_rrcSetupRequest:
1832
        LOG_D(NR_RRC, "Received RRCSetupRequest on UL-CCCH-Message (UE rnti %x)\n", ctxt_pP->rnti);
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
        ue_context_p = rrc_gNB_get_ue_context(gnb_rrc_inst, ctxt_pP->rnti);
        if (ue_context_p != NULL) {
          rrc_gNB_free_mem_UE_context(ctxt_pP, ue_context_p);
          MSC_LOG_RX_DISCARDED_MESSAGE(
              MSC_RRC_GNB,
              MSC_RRC_UE,
              buffer,
              dec_rval.consumed,
              MSC_AS_TIME_FMT" NR_RRCSetupRequest UE %x size %u (UE already in context)",
              MSC_AS_TIME_ARGS(ctxt_pP),
              ue_context_p->ue_context.rnti,
              dec_rval.consumed);
        } else {
          rrcSetupRequest = &ul_ccch_msg->message.choice.c1->choice.rrcSetupRequest->rrcSetupRequest;
          if (NR_InitialUE_Identity_PR_randomValue == rrcSetupRequest->ue_Identity.present) {
            /* randomValue                         BIT STRING (SIZE (39)) */
            if (rrcSetupRequest->ue_Identity.choice.randomValue.size != 5) { // 39-bit random value
              LOG_E(NR_RRC, "wrong InitialUE-Identity randomValue size, expected 5, provided %lu",
                          (long unsigned int)rrcSetupRequest->ue_Identity.choice.randomValue.size);
              return -1;
            }
1854

1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918
            memcpy(((uint8_t *) & random_value) + 3,
                    rrcSetupRequest->ue_Identity.choice.randomValue.buf,
                    rrcSetupRequest->ue_Identity.choice.randomValue.size);

            /* if there is already a registered UE (with another RNTI) with this random_value,
            * the current one must be removed from MAC/PHY (zombie UE)
            */
            if ((ue_context_p = rrc_gNB_ue_context_random_exist(RC.nrrrc[ctxt_pP->module_id], random_value))) {
              LOG_W(NR_RRC, "new UE rnti %x (coming with random value) is already there as UE %x, removing %x from MAC/PHY\n",
                      ctxt_pP->rnti, ue_context_p->ue_context.rnti, ue_context_p->ue_context.rnti);
              ue_context_p->ue_context.ul_failure_timer = 20000;
            }

            ue_context_p = rrc_gNB_get_next_free_ue_context(ctxt_pP, RC.nrrrc[ctxt_pP->module_id], random_value);
            ue_context_p->ue_context.Srb0.Srb_id = 0;
            ue_context_p->ue_context.Srb0.Active = 1;
            memcpy(ue_context_p->ue_context.Srb0.Rx_buffer.Payload,
                    buffer,
                    buffer_length);
            ue_context_p->ue_context.Srb0.Rx_buffer.payload_size = buffer_length;
          } else if (NR_InitialUE_Identity_PR_ng_5G_S_TMSI_Part1 == rrcSetupRequest->ue_Identity.present) {
            /* TODO */
            /* <5G-S-TMSI> = <AMF Set ID><AMF Pointer><5G-TMSI> 48-bit */
            /* ng-5G-S-TMSI-Part1                  BIT STRING (SIZE (39)) */
            if (rrcSetupRequest->ue_Identity.choice.ng_5G_S_TMSI_Part1.size != 5) {
              LOG_E(NR_RRC, "wrong ng_5G_S_TMSI_Part1 size, expected 5, provided %lu \n",
                          (long unsigned int)rrcSetupRequest->ue_Identity.choice.ng_5G_S_TMSI_Part1.size);
              return -1;
            }

            uint64_t s_tmsi_part1 = bitStr_to_uint64(&rrcSetupRequest->ue_Identity.choice.ng_5G_S_TMSI_Part1);

            // memcpy(((uint8_t *) & random_value) + 3,
            //         rrcSetupRequest->ue_Identity.choice.ng_5G_S_TMSI_Part1.buf,
            //         rrcSetupRequest->ue_Identity.choice.ng_5G_S_TMSI_Part1.size);

            if ((ue_context_p = rrc_gNB_ue_context_5g_s_tmsi_exist(RC.nrrrc[ctxt_pP->module_id], s_tmsi_part1))) {
              LOG_I(NR_RRC, " 5G-S-TMSI-Part1 exists, ue_context_p %p, old rnti %x => %x\n",ue_context_p, ue_context_p->ue_context.rnti, ctxt_pP->rnti);

              nr_rrc_mac_remove_ue(ctxt_pP->module_id, ue_context_p->ue_context.rnti);

              /* replace rnti in the context */
              /* for that, remove the context from the RB tree */
              RB_REMOVE(rrc_nr_ue_tree_s, &RC.nrrrc[ctxt_pP->module_id]->rrc_ue_head, ue_context_p);
              /* and insert again, after changing rnti everywhere it has to be changed */
              ue_context_p->ue_id_rnti = ctxt_pP->rnti;
              ue_context_p->ue_context.rnti = ctxt_pP->rnti;
              RB_INSERT(rrc_nr_ue_tree_s, &RC.nrrrc[ctxt_pP->module_id]->rrc_ue_head, ue_context_p);
              /* reset timers */
              ue_context_p->ue_context.ul_failure_timer = 0;
              ue_context_p->ue_context.ue_release_timer = 0;
              ue_context_p->ue_context.ue_reestablishment_timer = 0;
              ue_context_p->ue_context.ue_release_timer_s1 = 0;
              ue_context_p->ue_context.ue_release_timer_rrc = 0;
            } else {
              LOG_I(NR_RRC, " 5G-S-TMSI-Part1 doesn't exist, setting ng_5G_S_TMSI_Part1 to %p => %ld\n",
                              ue_context_p, s_tmsi_part1);

              ue_context_p = rrc_gNB_get_next_free_ue_context(ctxt_pP, RC.nrrrc[ctxt_pP->module_id], s_tmsi_part1);

              if (ue_context_p == NULL) {
                  LOG_E(RRC, "%s:%d:%s: rrc_gNB_get_next_free_ue_context returned NULL\n", __FILE__, __LINE__, __FUNCTION__);
              }

1919 1920 1921
	      if (ue_context_p != NULL) {
		ue_context_p->ue_context.Initialue_identity_5g_s_TMSI.presence = TRUE;
		ue_context_p->ue_context.ng_5G_S_TMSI_Part1 = s_tmsi_part1;
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
              }
            }
          } else {
            /* TODO */
            memcpy(((uint8_t *) & random_value) + 3,
                    rrcSetupRequest->ue_Identity.choice.randomValue.buf,
                    rrcSetupRequest->ue_Identity.choice.randomValue.size);

            rrc_gNB_get_next_free_ue_context(ctxt_pP, RC.nrrrc[ctxt_pP->module_id], random_value);
            LOG_E(NR_RRC,
                    PROTOCOL_NR_RRC_CTXT_UE_FMT" RRCSetupRequest without random UE identity or S-TMSI not supported, let's reject the UE\n",
                    PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
            rrc_gNB_generate_RRCReject(ctxt_pP,
                                       rrc_gNB_get_ue_context(gnb_rrc_inst, ctxt_pP->rnti),
                                       CC_id);
            break;
          }

          ue_context_p->ue_context.establishment_cause = rrcSetupRequest->establishmentCause;

1942 1943
          rrc_gNB_generate_RRCSetup(ctxt_pP,
                                    rrc_gNB_get_ue_context(gnb_rrc_inst, ctxt_pP->rnti),
1944 1945 1946
                                    du_to_cu_rrc_container,
				    gnb_rrc_inst->carrier.servingcellconfigcommon,
				    CC_id);
1947 1948

          // FIXME: Check the best place to perform this DRB configuration
1949
          //nr_DRB_preconfiguration(ctxt_pP->rnti);
1950
        }
1951 1952 1953
        break;

      case NR_UL_CCCH_MessageType__c1_PR_rrcResumeRequest:
yaojie's avatar
yaojie committed
1954
                LOG_I(NR_RRC, "receive rrcResumeRequest message \n");
1955 1956 1957 1958
        break;

      case NR_UL_CCCH_MessageType__c1_PR_rrcReestablishmentRequest:
        LOG_I(NR_RRC, "receive rrcReestablishmentRequest message \n");
yaojie's avatar
yaojie committed
1959 1960 1961 1962 1963
        LOG_DUMPMSG(NR_RRC, DEBUG_RRC,(char *)(buffer), buffer_length,
              "[MSG] RRC Reestablishment Request\n");
        LOG_D(NR_RRC,
              PROTOCOL_NR_RRC_CTXT_UE_FMT"MAC_gNB--- MAC_DATA_IND (rrcReestablishmentRequest on SRB0) --> RRC_gNB\n",
              PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
1964

yaojie's avatar
yaojie committed
1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
        rrcReestablishmentRequest = ul_ccch_msg->message.choice.c1->choice.rrcReestablishmentRequest->rrcReestablishmentRequest;
        LOG_I(NR_RRC,
          PROTOCOL_NR_RRC_CTXT_UE_FMT" NR_RRCReestablishmentRequest cause %s\n",
          PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
          ((rrcReestablishmentRequest.reestablishmentCause == NR_ReestablishmentCause_otherFailure) ?   "Other Failure" :
          (rrcReestablishmentRequest.reestablishmentCause == NR_ReestablishmentCause_handoverFailure) ? "Handover Failure" :
          "reconfigurationFailure"));
        {
          uint16_t                          c_rnti = 0;
          if (rrcReestablishmentRequest.ue_Identity.physCellId != RC.nrrrc[ctxt_pP->module_id]->carrier.physCellId) {
            /* UE was moving from previous cell so quickly that RRCReestablishment for previous cell was recieved in this cell */
            LOG_E(NR_RRC,
                  PROTOCOL_NR_RRC_CTXT_UE_FMT" NR_RRCReestablishmentRequest ue_Identity.physCellId(%ld) is not equal to current physCellId(%d), fallback to RRC establishment\n",
                  PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
                  rrcReestablishmentRequest.ue_Identity.physCellId,
                  RC.nrrrc[ctxt_pP->module_id]->carrier.physCellId);
            rrc_gNB_generate_RRCSetup_for_RRCReestablishmentRequest(ctxt_pP, CC_id);
            break;
          }
1984

yaojie's avatar
yaojie committed
1985
          LOG_D(NR_RRC, "physCellId is %ld\n", rrcReestablishmentRequest.ue_Identity.physCellId);
1986

yaojie's avatar
yaojie committed
1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
          for (i = 0; i < rrcReestablishmentRequest.ue_Identity.shortMAC_I.size; i++) {
            LOG_D(NR_RRC, "rrcReestablishmentRequest.ue_Identity.shortMAC_I.buf[%d] = %x\n",
                  i, rrcReestablishmentRequest.ue_Identity.shortMAC_I.buf[i]);
          }

          if (rrcReestablishmentRequest.ue_Identity.c_RNTI < 0 ||
              rrcReestablishmentRequest.ue_Identity.c_RNTI > 65535) {
            /* c_RNTI range error should not happen */
            LOG_E(NR_RRC,
                  PROTOCOL_NR_RRC_CTXT_UE_FMT" NR_RRCReestablishmentRequest c_RNTI range error, fallback to RRC establishment\n",
                  PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
            rrc_gNB_generate_RRCSetup_for_RRCReestablishmentRequest(ctxt_pP, CC_id);
            break;
          }
2001

yaojie's avatar
yaojie committed
2002 2003 2004
          c_rnti = rrcReestablishmentRequest.ue_Identity.c_RNTI;
          LOG_I(NR_RRC, "c_rnti is %x\n", c_rnti);
          ue_context_p = rrc_gNB_get_ue_context(gnb_rrc_inst, c_rnti);
2005

yaojie's avatar
yaojie committed
2006 2007 2008 2009 2010 2011 2012
          if (ue_context_p == NULL) {
            LOG_E(NR_RRC,
                  PROTOCOL_NR_RRC_CTXT_UE_FMT" NR_RRCReestablishmentRequest without UE context, fallback to RRC establishment\n",
                  PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
            rrc_gNB_generate_RRCSetup_for_RRCReestablishmentRequest(ctxt_pP, CC_id);
            break;
          }
2013
#if(0)
yoshio.inoue's avatar
yoshio.inoue committed
2014
          /* TODO : It may be needed if gNB goes into full stack working. */
yaojie's avatar
yaojie committed
2015
          int UE_id = find_nr_UE_id(ctxt_pP->module_id, c_rnti);
2016

yaojie's avatar
yaojie committed
2017 2018 2019 2020 2021 2022 2023
          if(UE_id == -1) {
            LOG_E(NR_RRC,
                  PROTOCOL_NR_RRC_CTXT_UE_FMT" NR_RRCReestablishmentRequest without UE_id(MAC) rnti %x, fallback to RRC establishment\n",
                  PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),c_rnti);
            rrc_gNB_generate_RRCSetup_for_RRCReestablishmentRequest(ctxt_pP, CC_id);
            break;
          }
2024

yaojie's avatar
yaojie committed
2025 2026
          //previous rnti
          rnti_t previous_rnti = 0;
2027

yaojie's avatar
yaojie committed
2028 2029 2030 2031 2032 2033
          for (i = 0; i < MAX_MOBILES_PER_ENB; i++) {
            if (reestablish_rnti_map[i][1] == c_rnti) {
              previous_rnti = reestablish_rnti_map[i][0];
              break;
            }
          }
2034

yaojie's avatar
yaojie committed
2035 2036
          if(previous_rnti != 0) {
            UE_id = find_nr_UE_id(ctxt_pP->module_id, previous_rnti);
2037

yaojie's avatar
yaojie committed
2038 2039 2040 2041 2042 2043 2044 2045
            if(UE_id == -1) {
              LOG_E(NR_RRC,
                    PROTOCOL_NR_RRC_CTXT_UE_FMT" RRCReestablishmentRequest without UE_id(MAC) previous rnti %x, fallback to RRC establishment\n",
                    PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),previous_rnti);
              rrc_gNB_generate_RRCSetup_for_RRCReestablishmentRequest(ctxt_pP, CC_id);
              break;
            }
          }
2046
#endif
yaojie's avatar
yaojie committed
2047 2048 2049 2050 2051 2052 2053 2054
          //c-plane not end
          if((ue_context_p->ue_context.Status != NR_RRC_RECONFIGURED) && (ue_context_p->ue_context.reestablishment_cause == NR_ReestablishmentCause_spare1)) {
            LOG_E(NR_RRC,
                  PROTOCOL_NR_RRC_CTXT_UE_FMT" NR_RRCReestablishmentRequest (UE %x c-plane is not end), RRC establishment failed \n",
                  PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),c_rnti);
            /* TODO RRC Release ? */
            break;
          }
2055

yaojie's avatar
yaojie committed
2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072
          if(ue_context_p->ue_context.ue_reestablishment_timer > 0) {
            LOG_E(NR_RRC,
                  PROTOCOL_NR_RRC_CTXT_UE_FMT" RRRCReconfigurationComplete(Previous) don't receive, delete the Previous UE,\nprevious Status %d, new Status NR_RRC_RECONFIGURED\n",
                  PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
                  ue_context_p->ue_context.Status
                  );
            ue_context_p->ue_context.Status = NR_RRC_RECONFIGURED;
            protocol_ctxt_t  ctxt_old_p;
            PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt_old_p,
                                          ctxt_pP->instance,
                                          GNB_FLAG_YES,
                                          c_rnti,
                                          ctxt_pP->frame,
                                          ctxt_pP->subframe);
            rrc_gNB_process_RRCReconfigurationComplete(&ctxt_old_p,
                ue_context_p,
                ue_context_p->ue_context.reestablishment_xid);
2073

yaojie's avatar
yaojie committed
2074 2075 2076 2077 2078
            for (uint8_t pdusessionid = 0; pdusessionid < ue_context_p->ue_context.nb_of_pdusessions; pdusessionid++) {
              if (ue_context_p->ue_context.pdusession[pdusessionid].status == PDU_SESSION_STATUS_DONE) {
                ue_context_p->ue_context.pdusession[pdusessionid].status = PDU_SESSION_STATUS_ESTABLISHED;
              } else {
                ue_context_p->ue_context.pdusession[pdusessionid].status = PDU_SESSION_STATUS_FAILED;
2079
              }
yaojie's avatar
yaojie committed
2080 2081
            }
          }
2082

yaojie's avatar
yaojie committed
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104
          LOG_D(NR_RRC,
                PROTOCOL_NR_RRC_CTXT_UE_FMT" UE context: %p\n",
                PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
                ue_context_p);
          /* reset timers */
          ue_context_p->ue_context.ul_failure_timer = 0;
          ue_context_p->ue_context.ue_release_timer = 0;
          ue_context_p->ue_context.ue_reestablishment_timer = 0;
          // ue_context_p->ue_context.ue_release_timer_s1 = 0;
          ue_context_p->ue_context.ue_release_timer_rrc = 0;
          ue_context_p->ue_context.reestablishment_xid = -1;

          // insert C-RNTI to map
          for (i = 0; i < MAX_MOBILES_PER_ENB; i++) {
            if (reestablish_rnti_map[i][0] == 0) {
              reestablish_rnti_map[i][0] = ctxt_pP->rnti;
              reestablish_rnti_map[i][1] = c_rnti;
              LOG_D(NR_RRC, "reestablish_rnti_map[%d] [0] %x, [1] %x\n",
                    i, reestablish_rnti_map[i][0], reestablish_rnti_map[i][1]);
              break;
            }
          }
2105

yaojie's avatar
yaojie committed
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
          ue_context_p->ue_context.reestablishment_cause = rrcReestablishmentRequest.reestablishmentCause;
          LOG_D(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" Accept reestablishment request from UE physCellId %ld cause %ld\n",
                PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
                rrcReestablishmentRequest.ue_Identity.physCellId,
                ue_context_p->ue_context.reestablishment_cause);

          ue_context_p->ue_context.primaryCC_id = CC_id;
          //LG COMMENT Idx = (ue_mod_idP * NB_RB_MAX) + DCCH;
          Idx = DCCH;
          // SRB1
          ue_context_p->ue_context.Srb1.Active = 1;
          ue_context_p->ue_context.Srb1.Srb_info.Srb_id = Idx;
          memcpy(&ue_context_p->ue_context.Srb1.Srb_info.Lchan_desc[0],
                &DCCH_LCHAN_DESC,
                LCHAN_DESC_SIZE);
          memcpy(&ue_context_p->ue_context.Srb1.Srb_info.Lchan_desc[1],
                &DCCH_LCHAN_DESC,
                LCHAN_DESC_SIZE);
          // SRB2: set  it to go through SRB1 with id 1 (DCCH)
          ue_context_p->ue_context.Srb2.Active = 1;
          ue_context_p->ue_context.Srb2.Srb_info.Srb_id = Idx;
          memcpy(&ue_context_p->ue_context.Srb2.Srb_info.Lchan_desc[0],
                &DCCH_LCHAN_DESC,
                LCHAN_DESC_SIZE);
          memcpy(&ue_context_p->ue_context.Srb2.Srb_info.Lchan_desc[1],
                &DCCH_LCHAN_DESC,
                LCHAN_DESC_SIZE);

          rrc_gNB_generate_RRCReestablishment(ctxt_pP, ue_context_p, CC_id);

          LOG_I(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT"CALLING RLC CONFIG SRB1 (rbid %d)\n",
                PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
                Idx);
          MSC_LOG_TX_MESSAGE(MSC_RRC_GNB,
                            MSC_PDCP_GNB,
                            NULL,
                            0,
                            MSC_AS_TIME_FMT" CONFIG_REQ UE %x SRB",
                            MSC_AS_TIME_ARGS(ctxt_pP),
                            ue_context_p->ue_context.rnti);
          // nr_rrc_pdcp_config_asn1_req(ctxt_pP,
          //                         ue_context_p->ue_context.SRB_configList,
          //                         NULL,
          //                         NULL,
          //                         0xff,
          //                         NULL,
          //                         NULL,
          //                         NULL,
          //                         NULL,
          //                         NULL,
2156
          //                         NULL,
yaojie's avatar
yaojie committed
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
          //                         NULL);

          // if (!NODE_IS_CU(RC.nrrrc[ctxt_pP->module_id]->node_type)) {
            // nr_rrc_rlc_config_asn1_req(ctxt_pP,
            //                         ue_context_p->ue_context.SRB_configList,
            //                         NULL,
            //                         NULL,
            //                         NULL,
            //                         NULL);
          // }
        }
2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178
        break;

      case NR_UL_CCCH_MessageType__c1_PR_rrcSystemInfoRequest:
        LOG_I(NR_RRC, "receive rrcSystemInfoRequest message \n");
        /* TODO */
        break;

      default:
        LOG_E(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" Unknown message\n",
                   PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
        break;
2179
    }
2180 2181
  }
  return 0;
2182 2183
}

Xue Song's avatar
Xue Song committed
2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206
/*! \fn uint64_t bitStr_to_uint64(BIT_STRING_t *)
 *\brief  This function extract at most a 64 bits value from a BIT_STRING_t object, the exact bits number depend on the BIT_STRING_t contents.
 *\param[in] pointer to the BIT_STRING_t object.
 *\return the extracted value.
 */
static inline uint64_t bitStr_to_uint64(BIT_STRING_t *asn) {
  uint64_t result = 0;
  int index;
  int shift;

  DevCheck ((asn->size > 0) && (asn->size <= 8), asn->size, 0, 0);

  shift = ((asn->size - 1) * 8) - asn->bits_unused;
  for (index = 0; index < (asn->size - 1); index++) {
    result |= (uint64_t)asn->buf[index] << shift;
    shift -= 8;
  }

  result |= asn->buf[index] >> asn->bits_unused;

  return result;
}

Xue Song's avatar
Xue Song committed
2207 2208 2209 2210 2211 2212 2213 2214 2215 2216
//-----------------------------------------------------------------------------
int
rrc_gNB_decode_dcch(
  const protocol_ctxt_t *const ctxt_pP,
  const rb_id_t                Srb_id,
  const uint8_t    *const      Rx_sdu,
  const sdu_size_t             sdu_sizeP
)
//-----------------------------------------------------------------------------
{
2217 2218 2219
  asn_dec_rval_t                      dec_rval;
  NR_UL_DCCH_Message_t                *ul_dcch_msg  = NULL;
  struct rrc_gNB_ue_context_s         *ue_context_p = NULL;
2220 2221
  MessageDef                         *msg_delete_tunnels_p = NULL;
  uint8_t                             xid;
Xue Song's avatar
Xue Song committed
2222

2223
  int i;
Xue Song's avatar
Xue Song committed
2224

2225
  if ((Srb_id != 1) && (Srb_id != 2)) {
Xue Song's avatar
Xue Song committed
2226
    LOG_E(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" Received message on SRB%ld, should not have ...\n",
2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
          PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
          Srb_id);
  } else {
    LOG_D(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" Received message on SRB%ld\n",
              PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
              Srb_id);
  }

  LOG_D(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" Decoding UL-DCCH Message\n",
                  PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
2237 2238
  for (int i=0;i<sdu_sizeP;i++) printf("%02x ",Rx_sdu[i]);
  printf("\n");
2239 2240 2241 2242 2243 2244 2245 2246
  dec_rval = uper_decode(
                  NULL,
                  &asn_DEF_NR_UL_DCCH_Message,
                  (void **)&ul_dcch_msg,
                  Rx_sdu,
                  sdu_sizeP,
                  0,
                  0);
2247
  xer_fprint(stdout, &asn_DEF_NR_UL_DCCH_Message, (void *)ul_dcch_msg);
2248 2249 2250 2251

  {
    for (i = 0; i < sdu_sizeP; i++) {
      LOG_T(NR_RRC, "%x.", Rx_sdu[i]);
Xue Song's avatar
Xue Song committed
2252 2253
    }

2254 2255
    LOG_T(NR_RRC, "\n");
  }
Xue Song's avatar
Xue Song committed
2256

2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
  if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) {
    LOG_E(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" Failed to decode UL-DCCH (%zu bytes)\n",
        PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
        dec_rval.consumed);
    return -1;
  }

  ue_context_p = rrc_gNB_get_ue_context(RC.nrrrc[ctxt_pP->module_id],
                                          ctxt_pP->rnti);

  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:
        LOG_I(NR_RRC,
            PROTOCOL_NR_RRC_CTXT_FMT" Received PR_NOTHING on UL-DCCH-Message\n",
            PROTOCOL_NR_RRC_CTXT_ARGS(ctxt_pP));
        break;
Xue Song's avatar
Xue Song committed
2274

2275
      case NR_UL_DCCH_MessageType__c1_PR_rrcReconfigurationComplete:
yaojie's avatar
yaojie committed
2276
        LOG_I(NR_RRC, "Receive RRC Reconfiguration Complete message UE %x\n", ctxt_pP->rnti);
2277
        if(!ue_context_p) {
yaojie's avatar
yaojie committed
2278
          LOG_E(NR_RRC, "Processing NR_RRCReconfigurationComplete UE %x, ue_context_p is NULL\n", ctxt_pP->rnti);
2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295
          break;
        }

        LOG_DUMPMSG(NR_RRC, DEBUG_RRC, (char *)(Rx_sdu), sdu_sizeP,
                    "[MSG] RRC Connection Reconfiguration Complete\n");
        MSC_LOG_RX_MESSAGE(
        MSC_RRC_GNB,
        MSC_RRC_UE,
        Rx_sdu,
        sdu_sizeP,
        MSC_AS_TIME_FMT" NR_RRCReconfigurationComplete UE %x size %u",
        MSC_AS_TIME_ARGS(ctxt_pP),
        ue_context_p->ue_context.rnti,
        sdu_sizeP);
        LOG_D(NR_RRC,
            PROTOCOL_NR_RRC_CTXT_UE_FMT" RLC RB %02d --- RLC_DATA_IND %d bytes "
            "(RRCReconfigurationComplete) ---> RRC_gNB]\n",
Xue Song's avatar
Xue Song committed
2296
            PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307
            DCCH,
            sdu_sizeP);

        if (ul_dcch_msg->message.choice.c1->present == NR_UL_DCCH_MessageType__c1_PR_rrcReconfigurationComplete) {
          if (ul_dcch_msg->message.choice.c1->choice.rrcReconfigurationComplete->criticalExtensions.present ==
            NR_RRCReconfigurationComplete__criticalExtensions_PR_rrcReconfigurationComplete)
            rrc_gNB_process_RRCReconfigurationComplete(
                ctxt_pP,
                ue_context_p,
                ul_dcch_msg->message.choice.c1->choice.rrcReconfigurationComplete->rrc_TransactionIdentifier);
        }
Xue Song's avatar
Xue Song committed
2308

yaojie's avatar
yaojie committed
2309 2310 2311 2312 2313
        if (AMF_MODE_ENABLED) {
          if(ue_context_p->ue_context.pdu_session_release_command_flag == 1) {
            xid = ul_dcch_msg->message.choice.c1->choice.rrcReconfigurationComplete->rrc_TransactionIdentifier;
            ue_context_p->ue_context.pdu_session_release_command_flag = 0;
            //gtp tunnel delete
2314
            msg_delete_tunnels_p = itti_alloc_new_message(TASK_RRC_GNB, 0, GTPV1U_GNB_DELETE_TUNNEL_REQ);
yaojie's avatar
yaojie committed
2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330
            memset(&GTPV1U_GNB_DELETE_TUNNEL_REQ(msg_delete_tunnels_p), 0, sizeof(GTPV1U_GNB_DELETE_TUNNEL_REQ(msg_delete_tunnels_p)));
            GTPV1U_GNB_DELETE_TUNNEL_REQ(msg_delete_tunnels_p).rnti = ue_context_p->ue_context.rnti;

            for(i = 0; i < NB_RB_MAX; i++) {
              if(xid == ue_context_p->ue_context.pdusession[i].xid) {
                GTPV1U_GNB_DELETE_TUNNEL_REQ(msg_delete_tunnels_p).pdusession_id[GTPV1U_GNB_DELETE_TUNNEL_REQ(msg_delete_tunnels_p).num_pdusession++] =
                  ue_context_p->ue_context.gnb_gtp_psi[i];
                ue_context_p->ue_context.gnb_gtp_teid[i] = 0;
                memset(&ue_context_p->ue_context.gnb_gtp_addrs[i], 0, sizeof(ue_context_p->ue_context.gnb_gtp_addrs[i]));
                ue_context_p->ue_context.gnb_gtp_psi[i]  = 0;
              }
            }

            itti_send_msg_to_task(TASK_GTPV1_U, ctxt_pP->instance, msg_delete_tunnels_p);
            //NGAP_PDUSESSION_RELEASE_RESPONSE
            rrc_gNB_send_NGAP_PDUSESSION_RELEASE_RESPONSE(ctxt_pP, ue_context_p, xid);
2331 2332 2333 2334 2335 2336
          } else if (ue_context_p->ue_context.established_pdu_sessions_flag != 1) {
            if (ue_context_p->ue_context.setup_pdu_sessions > 0) {
              rrc_gNB_send_NGAP_PDUSESSION_SETUP_RESP(ctxt_pP,
                ue_context_p,
                ul_dcch_msg->message.choice.c1->choice.rrcReconfigurationComplete->rrc_TransactionIdentifier);
            }
yaojie's avatar
yaojie committed
2337 2338 2339 2340 2341 2342
          }
        }
        if (first_rrcreconfiguration == 0){
          first_rrcreconfiguration = 1;
          rrc_gNB_send_NGAP_INITIAL_CONTEXT_SETUP_RESP(ctxt_pP, ue_context_p);
        }
2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354

        break;

      case NR_UL_DCCH_MessageType__c1_PR_rrcSetupComplete:
        if(!ue_context_p) {
          LOG_I(NR_RRC, "Processing NR_RRCSetupComplete UE %x, ue_context_p is NULL\n", ctxt_pP->rnti);
          break;
        }

        LOG_DUMPMSG(NR_RRC, DEBUG_RRC,(char *)Rx_sdu,sdu_sizeP,
                    "[MSG] RRC SetupComplete\n");
        MSC_LOG_RX_MESSAGE(
2355 2356 2357 2358
                MSC_RRC_GNB,
                MSC_RRC_UE,
                Rx_sdu,
                sdu_sizeP,
2359
                MSC_AS_TIME_FMT" NR_RRCSetupComplete UE %x size %u",
2360 2361 2362
                MSC_AS_TIME_ARGS(ctxt_pP),
                ue_context_p->ue_context.rnti,
                sdu_sizeP);
2363
        LOG_D(NR_RRC,
Xue Song's avatar
Xue Song committed
2364
                PROTOCOL_NR_RRC_CTXT_UE_FMT" RLC RB %02d --- RLC_DATA_IND %d bytes "
2365
                "(RRCSetupComplete) ---> RRC_gNB\n",
Xue Song's avatar
Xue Song committed
2366 2367 2368
                PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
                DCCH,
                sdu_sizeP);
2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423

        if (ul_dcch_msg->message.choice.c1->choice.rrcSetupComplete->criticalExtensions.present ==
              NR_RRCSetupComplete__criticalExtensions_PR_rrcSetupComplete) {
          if (ul_dcch_msg->message.choice.c1->choice.rrcSetupComplete->criticalExtensions.choice.
            rrcSetupComplete->ng_5G_S_TMSI_Value != NULL) {
            if (ul_dcch_msg->message.choice.c1->choice.rrcSetupComplete->criticalExtensions.choice.
            rrcSetupComplete->ng_5G_S_TMSI_Value->present == NR_RRCSetupComplete_IEs__ng_5G_S_TMSI_Value_PR_ng_5G_S_TMSI_Part2) {
            // ng-5G-S-TMSI-Part2                  BIT STRING (SIZE (9))
              if (ul_dcch_msg->message.choice.c1->choice.rrcSetupComplete->criticalExtensions.choice.
                rrcSetupComplete->ng_5G_S_TMSI_Value->choice.ng_5G_S_TMSI_Part2.size != 2) {
                LOG_E(NR_RRC, "wrong ng_5G_S_TMSI_Part2 size, expected 2, provided %lu",
                            (long unsigned int)ul_dcch_msg->message.choice.c1->choice.rrcSetupComplete->
                            criticalExtensions.choice.rrcSetupComplete->
                            ng_5G_S_TMSI_Value->choice.ng_5G_S_TMSI_Part2.size);
                return -1;
              }

              if (ue_context_p->ue_context.ng_5G_S_TMSI_Part1 != 0) {
                ue_context_p->ue_context.ng_5G_S_TMSI_Part2 =
                                BIT_STRING_to_uint16(&ul_dcch_msg->message.choice.c1->choice.rrcSetupComplete->
                                    criticalExtensions.choice.rrcSetupComplete->
                                    ng_5G_S_TMSI_Value->choice.ng_5G_S_TMSI_Part2);
              }

            /* TODO */
            } else if (ul_dcch_msg->message.choice.c1->choice.rrcSetupComplete->criticalExtensions.choice.
              rrcSetupComplete->ng_5G_S_TMSI_Value->present == NR_RRCSetupComplete_IEs__ng_5G_S_TMSI_Value_PR_ng_5G_S_TMSI) {
              // NG-5G-S-TMSI ::=                         BIT STRING (SIZE (48))
              if (ul_dcch_msg->message.choice.c1->choice.rrcSetupComplete->criticalExtensions.choice.
                rrcSetupComplete->ng_5G_S_TMSI_Value->choice.ng_5G_S_TMSI.size != 6) {
                LOG_E(NR_RRC, "wrong ng_5G_S_TMSI size, expected 6, provided %lu",
                            (long unsigned int)ul_dcch_msg->message.choice.c1->choice.rrcSetupComplete->
                            criticalExtensions.choice.rrcSetupComplete->
                            ng_5G_S_TMSI_Value->choice.ng_5G_S_TMSI.size);
                return -1;
              }

              uint64_t fiveg_s_TMSI = bitStr_to_uint64(&ul_dcch_msg->message.choice.c1->choice.rrcSetupComplete->
                  criticalExtensions.choice.rrcSetupComplete->ng_5G_S_TMSI_Value->choice.ng_5G_S_TMSI);
              LOG_I(NR_RRC, "Received rrcSetupComplete, 5g_s_TMSI: 0x%lX, amf_set_id: 0x%lX(%ld), amf_pointer: 0x%lX(%ld), 5g TMSI: 0x%X \n",
                  fiveg_s_TMSI, fiveg_s_TMSI >> 38, fiveg_s_TMSI >> 38,
                  (fiveg_s_TMSI >> 32) & 0x3F, (fiveg_s_TMSI >> 32) & 0x3F,
                  (uint32_t)fiveg_s_TMSI);
              if (ue_context_p->ue_context.Initialue_identity_5g_s_TMSI.presence == TRUE) {
                  ue_context_p->ue_context.Initialue_identity_5g_s_TMSI.amf_set_id = fiveg_s_TMSI >> 38;
                  ue_context_p->ue_context.Initialue_identity_5g_s_TMSI.amf_pointer = (fiveg_s_TMSI >> 32) & 0x3F;
                  ue_context_p->ue_context.Initialue_identity_5g_s_TMSI.fiveg_tmsi = (uint32_t)fiveg_s_TMSI;
              }
            }
          }

          rrc_gNB_process_RRCSetupComplete(
                  ctxt_pP,
                  ue_context_p,
                  ul_dcch_msg->message.choice.c1->choice.rrcSetupComplete->criticalExtensions.choice.rrcSetupComplete);
yaojie's avatar
yaojie committed
2424
          LOG_I(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" UE State = NR_RRC_CONNECTED \n",
2425 2426 2427 2428 2429
              PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
        }

        ue_context_p->ue_context.ue_release_timer = 0;
        break;
wujing's avatar
wujing committed
2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458

        case NR_UL_DCCH_MessageType__c1_PR_ulInformationTransfer:
            LOG_I(NR_RRC,"Recived RRC GNB UL Information Transfer \n");
            if(!ue_context_p) {
                LOG_I(NR_RRC, "Processing ulInformationTransfer UE %x, ue_context_p is NULL\n", ctxt_pP->rnti);
                break;
            }

            LOG_D(NR_RRC,"[MSG] RRC UL Information Transfer \n");
            LOG_DUMPMSG(RRC,DEBUG_RRC,(char *)Rx_sdu,sdu_sizeP,
                        "[MSG] RRC UL Information Transfer \n");
            MSC_LOG_RX_MESSAGE(
              MSC_RRC_GNB,
              MSC_RRC_UE,
              Rx_sdu,
              sdu_sizeP,
              MSC_AS_TIME_FMT" ulInformationTransfer UE %x size %u",
              MSC_AS_TIME_ARGS(ctxt_pP),
              ue_context_p->ue_context.rnti,
              sdu_sizeP);

            if (AMF_MODE_ENABLED == 1) {
                rrc_gNB_send_NGAP_UPLINK_NAS(ctxt_pP,
                                          ue_context_p,
                                          ul_dcch_msg);
            }
            break;

        case NR_UL_DCCH_MessageType__c1_PR_securityModeComplete:
2459
        // to avoid segmentation fault
wujing's avatar
wujing committed
2460 2461 2462 2463
           if(!ue_context_p) {
              LOG_I(NR_RRC, "Processing securityModeComplete UE %x, ue_context_p is NULL\n", ctxt_pP->rnti);
              break;
           }
2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482

        LOG_I(NR_RRC,
              PROTOCOL_NR_RRC_CTXT_UE_FMT" received securityModeComplete on UL-DCCH %d from UE\n",
              PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
              DCCH);
        LOG_D(NR_RRC,
              PROTOCOL_NR_RRC_CTXT_UE_FMT" RLC RB %02d --- RLC_DATA_IND %d bytes "
              "(securityModeComplete) ---> RRC_eNB\n",
              PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
              DCCH,
              sdu_sizeP);

        if ( LOG_DEBUGFLAG(DEBUG_ASN1) ) {
          xer_fprint(stdout, &asn_DEF_NR_UL_DCCH_Message, (void *)ul_dcch_msg);
        }

        rrc_gNB_generate_UECapabilityEnquiry(ctxt_pP, ue_context_p);
        //rrc_gNB_generate_defaultRRCReconfiguration(ctxt_pP, ue_context_p);
        break;
wujing's avatar
wujing committed
2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507
        case NR_UL_DCCH_MessageType__c1_PR_securityModeFailure:
            LOG_DUMPMSG(NR_RRC,DEBUG_RRC,(char *)Rx_sdu,sdu_sizeP,
                       "[MSG] NR RRC Security Mode Failure\n");
            MSC_LOG_RX_MESSAGE(
                MSC_RRC_GNB,
                MSC_RRC_UE,
                Rx_sdu,
                sdu_sizeP,
                MSC_AS_TIME_FMT" securityModeFailure UE %x size %u",
                MSC_AS_TIME_ARGS(ctxt_pP),
                ue_context_p->ue_context.rnti,
                sdu_sizeP);
            LOG_W(NR_RRC,
                  PROTOCOL_RRC_CTXT_UE_FMT" RLC RB %02d --- RLC_DATA_IND %d bytes "
                  "(securityModeFailure) ---> RRC_gNB\n",
                  PROTOCOL_RRC_CTXT_UE_ARGS(ctxt_pP),
                  DCCH,
                  sdu_sizeP);
            
            if ( LOG_DEBUGFLAG(DEBUG_ASN1) ) {
              xer_fprint(stdout, &asn_DEF_NR_UL_DCCH_Message, (void *)ul_dcch_msg);
            }
            
            rrc_gNB_generate_UECapabilityEnquiry(ctxt_pP, ue_context_p);
            break;
2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612

      case NR_UL_DCCH_MessageType__c1_PR_ueCapabilityInformation:
        if(!ue_context_p) {
          LOG_I(NR_RRC, "Processing ueCapabilityInformation UE %x, ue_context_p is NULL\n", ctxt_pP->rnti);
          break;
        }

        LOG_DUMPMSG(NR_RRC,DEBUG_RRC,(char *)Rx_sdu,sdu_sizeP,
                "[MSG] NR_RRC UECapablility Information\n");
            MSC_LOG_RX_MESSAGE(
            MSC_RRC_GNB,
            MSC_RRC_UE,
            Rx_sdu,
            sdu_sizeP,
            MSC_AS_TIME_FMT" ueCapabilityInformation UE %x size %u",
            MSC_AS_TIME_ARGS(ctxt_pP),
            ue_context_p->ue_context.rnti,
            sdu_sizeP);
        LOG_I(NR_RRC,
            PROTOCOL_NR_RRC_CTXT_UE_FMT" received ueCapabilityInformation on UL-DCCH %d from UE\n",
            PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
            DCCH);
        LOG_D(RRC,
        PROTOCOL_NR_RRC_CTXT_UE_FMT" RLC RB %02d --- RLC_DATA_IND %d bytes "
        "(UECapabilityInformation) ---> RRC_eNB\n",
        PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
        DCCH,
        sdu_sizeP);
        if ( LOG_DEBUGFLAG(DEBUG_ASN1) ) {
            xer_fprint(stdout, &asn_DEF_NR_UL_DCCH_Message, (void *)ul_dcch_msg);
        }
        LOG_I(NR_RRC, "got UE capabilities for UE %x\n", ctxt_pP->rnti);
        int eutra_index = -1;

        if( ul_dcch_msg->message.choice.c1->choice.ueCapabilityInformation->criticalExtensions.present ==
        NR_UECapabilityInformation__criticalExtensions_PR_ueCapabilityInformation ) {
          for(i = 0;i < ul_dcch_msg->message.choice.c1->choice.ueCapabilityInformation->criticalExtensions.choice.ueCapabilityInformation->ue_CapabilityRAT_ContainerList->list.count; i++){
            if(ul_dcch_msg->message.choice.c1->choice.ueCapabilityInformation->criticalExtensions.choice.ueCapabilityInformation->ue_CapabilityRAT_ContainerList->list.array[i]->rat_Type ==
              NR_RAT_Type_nr){
              if(ue_context_p->ue_context.UE_Capability_nr){
                ASN_STRUCT_FREE(asn_DEF_NR_UE_NR_Capability,ue_context_p->ue_context.UE_Capability_nr);
                ue_context_p->ue_context.UE_Capability_nr = 0;
              }

              dec_rval = uper_decode(NULL,
                                      &asn_DEF_NR_UE_NR_Capability,
                                      (void**)&ue_context_p->ue_context.UE_Capability_nr,
                                      ul_dcch_msg->message.choice.c1->choice.ueCapabilityInformation->criticalExtensions.choice.ueCapabilityInformation->ue_CapabilityRAT_ContainerList->list.array[i]->ue_CapabilityRAT_Container.buf,
                                      ul_dcch_msg->message.choice.c1->choice.ueCapabilityInformation->criticalExtensions.choice.ueCapabilityInformation->ue_CapabilityRAT_ContainerList->list.array[i]->ue_CapabilityRAT_Container.size,
                                      0,0);
              if(LOG_DEBUGFLAG(DEBUG_ASN1)){
                xer_fprint(stdout,&asn_DEF_NR_UE_NR_Capability,ue_context_p->ue_context.UE_Capability_nr);
              }

              if((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)){
                LOG_E(NR_RRC,PROTOCOL_NR_RRC_CTXT_UE_FMT" Failed to decode nr UE capabilities (%zu bytes)\n",
                PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),dec_rval.consumed);
                ASN_STRUCT_FREE(asn_DEF_NR_UE_NR_Capability,ue_context_p->ue_context.UE_Capability_nr);
                ue_context_p->ue_context.UE_Capability_nr = 0;
              }

              ue_context_p->ue_context.UE_Capability_size =
              ul_dcch_msg->message.choice.c1->choice.ueCapabilityInformation->criticalExtensions.choice.ueCapabilityInformation->ue_CapabilityRAT_ContainerList->list.array[i]->ue_CapabilityRAT_Container.size;
              if(eutra_index != -1){
                LOG_E(NR_RRC,"fatal: more than 1 eutra capability\n");
                exit(1);
              }
              eutra_index = i;
            }

            if(ul_dcch_msg->message.choice.c1->choice.ueCapabilityInformation->criticalExtensions.choice.ueCapabilityInformation->ue_CapabilityRAT_ContainerList->list.array[i]->rat_Type ==
            NR_RAT_Type_eutra_nr){
            if(ue_context_p->ue_context.UE_Capability_MRDC){
              ASN_STRUCT_FREE(asn_DEF_NR_UE_MRDC_Capability,ue_context_p->ue_context.UE_Capability_MRDC);
              ue_context_p->ue_context.UE_Capability_MRDC = 0;
            }
            dec_rval = uper_decode(NULL,
                                    &asn_DEF_NR_UE_MRDC_Capability,
                                    (void**)&ue_context_p->ue_context.UE_Capability_MRDC,
                                    ul_dcch_msg->message.choice.c1->choice.ueCapabilityInformation->criticalExtensions.choice.ueCapabilityInformation->ue_CapabilityRAT_ContainerList->list.array[i]->ue_CapabilityRAT_Container.buf,
                                    ul_dcch_msg->message.choice.c1->choice.ueCapabilityInformation->criticalExtensions.choice.ueCapabilityInformation->ue_CapabilityRAT_ContainerList->list.array[i]->ue_CapabilityRAT_Container.size,
                                    0,0);

            if(LOG_DEBUGFLAG(DEBUG_ASN1)){
              xer_fprint(stdout,&asn_DEF_NR_UE_MRDC_Capability,ue_context_p->ue_context.UE_Capability_MRDC);
            }

            if((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)){
              LOG_E(NR_RRC,PROTOCOL_NR_RRC_CTXT_FMT" Failed to decode nr UE capabilities (%zu bytes)\n",
                  PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),dec_rval.consumed);
              ASN_STRUCT_FREE(asn_DEF_NR_UE_MRDC_Capability,ue_context_p->ue_context.UE_Capability_MRDC);
              ue_context_p->ue_context.UE_Capability_MRDC = 0;
            }
              ue_context_p->ue_context.UE_MRDC_Capability_size =
              ul_dcch_msg->message.choice.c1->choice.ueCapabilityInformation->criticalExtensions.choice.ueCapabilityInformation->ue_CapabilityRAT_ContainerList->list.array[i]->ue_CapabilityRAT_Container.size;
            }

            if(ul_dcch_msg->message.choice.c1->choice.ueCapabilityInformation->criticalExtensions.choice.ueCapabilityInformation->ue_CapabilityRAT_ContainerList->list.array[i]->rat_Type ==
            NR_RAT_Type_eutra){
              //TODO
            }
          }

          if(eutra_index == -1)
          break;
wujing's avatar
wujing committed
2613 2614 2615 2616 2617 2618
      }
      if (AMF_MODE_ENABLED == 1) {
          rrc_gNB_send_NGAP_UE_CAPABILITIES_IND(ctxt_pP,
                                    ue_context_p,
                                    ul_dcch_msg);
      }
2619 2620 2621 2622 2623 2624 2625

      if (ue_context_p->ue_context.established_pdu_sessions_flag == 1) {
        rrc_gNB_generate_dedicatedRRCReconfiguration(ctxt_pP, ue_context_p);
      } else {
        rrc_gNB_generate_defaultRRCReconfiguration(ctxt_pP, ue_context_p);
      }

wujing's avatar
wujing committed
2626
      break;
2627

2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666
            case NR_UL_DCCH_MessageType__c1_PR_rrcReestablishmentComplete:
              LOG_DUMPMSG(NR_RRC,DEBUG_RRC,(char *)Rx_sdu,sdu_sizeP,
                          "[MSG] NR_RRC Connection Reestablishment Complete\n");
              MSC_LOG_RX_MESSAGE(
                MSC_RRC_GNB,
                MSC_RRC_UE,
                Rx_sdu,
                sdu_sizeP,
                MSC_AS_TIME_FMT" NR_RRCConnectionReestablishmentComplete UE %x size %u",
                MSC_AS_TIME_ARGS(ctxt_pP),
                ue_context_p->ue_context.rnti,
                sdu_sizeP);
              LOG_I(NR_RRC,
                    PROTOCOL_RRC_CTXT_UE_FMT" RLC RB %02d --- RLC_DATA_IND %d bytes "
                    "(rrcConnectionReestablishmentComplete) ---> RRC_gNB\n",
                    PROTOCOL_RRC_CTXT_UE_ARGS(ctxt_pP),
                    DCCH,
                    sdu_sizeP);
              {
                rnti_t reestablish_rnti = 0;

                // select C-RNTI from map
                for (i = 0; i < MAX_MOBILES_PER_ENB; i++) {
                  if (reestablish_rnti_map[i][0] == ctxt_pP->rnti) {
                    reestablish_rnti = reestablish_rnti_map[i][1];
                    ue_context_p = rrc_gNB_get_ue_context(
                                     RC.nrrrc[ctxt_pP->module_id],
                                     reestablish_rnti);
                    // clear currentC-RNTI from map
                    reestablish_rnti_map[i][0] = 0;
                    reestablish_rnti_map[i][1] = 0;
                    LOG_D(NR_RRC, "reestablish_rnti_map[%d] [0] %x, [1] %x\n",
                          i, reestablish_rnti_map[i][0], reestablish_rnti_map[i][1]);
                    break;
                  }
                }

                if (!ue_context_p) {
                  LOG_E(NR_RRC,
2667 2668
                        PROTOCOL_NR_RRC_CTXT_UE_FMT" NR_RRCConnectionReestablishmentComplete without UE context, falt\n",
                        PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
2669 2670 2671
                  break;
                }

2672
#if(0)
yoshio.inoue's avatar
yoshio.inoue committed
2673
                /* TODO : It may be needed if gNB goes into full stack working. */
2674
                //clear
2675
                int UE_id = find_nr_UE_id(ctxt_pP->module_id, ctxt_pP->rnti);
2676 2677 2678 2679 2680 2681 2682 2683

                if(UE_id == -1) {
                  LOG_E(NR_RRC,
                        PROTOCOL_RRC_CTXT_UE_FMT" NR_RRCConnectionReestablishmentComplete without UE_id(MAC) rnti %x, fault\n",
                        PROTOCOL_RRC_CTXT_UE_ARGS(ctxt_pP),ctxt_pP->rnti);
                  break;
                }

2684 2685
                RC.nrmac[ctxt_pP->module_id]->UE_info.UE_sched_ctrl[UE_id].ue_reestablishment_reject_timer = 0;
#endif
2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700
                ue_context_p->ue_context.reestablishment_xid = -1;

                if (ul_dcch_msg->message.choice.c1->choice.rrcReestablishmentComplete->criticalExtensions.present ==
                    NR_RRCReestablishmentComplete__criticalExtensions_PR_rrcReestablishmentComplete) {
                  rrc_gNB_process_RRCConnectionReestablishmentComplete(ctxt_pP, reestablish_rnti, ue_context_p,
                      ul_dcch_msg->message.choice.c1->choice.rrcReestablishmentComplete->rrc_TransactionIdentifier);

                }

                //ue_context_p->ue_context.ue_release_timer = 0;
                ue_context_p->ue_context.ue_reestablishment_timer = 1;
                // remove UE after 100 frames after LTE_RRCConnectionReestablishmentRelease is triggered
                ue_context_p->ue_context.ue_reestablishment_timer_thres = 1000;
              }
              break;
2701 2702
      default:
        break;
Xue Song's avatar
Xue Song committed
2703
    }
2704 2705
  }
  return 0;
Xue Song's avatar
Xue Song committed
2706 2707
}

Xue Song's avatar
Xue Song committed
2708 2709 2710
void rrc_gNB_process_f1_setup_req(f1ap_setup_req_t *f1_setup_req) {
  LOG_I(NR_RRC,"Received F1 Setup Request from gNB_DU %llu (%s)\n",(unsigned long long int)f1_setup_req->gNB_DU_id,f1_setup_req->gNB_DU_name);
  int cu_cell_ind = 0;
2711
  MessageDef *msg_p = NULL,*msg_p2=NULL;
Xue Song's avatar
Xue Song committed
2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726

  for (int i = 0; i < f1_setup_req->num_cells_available; i++) {
    int found_cell=0;
    for (int j=0; j<RC.nb_nr_inst; j++) {
      gNB_RRC_INST *rrc = RC.nrrrc[j];

      if (rrc->configuration.mcc[0] == f1_setup_req->mcc[i] &&
          rrc->configuration.mnc[0] == f1_setup_req->mnc[i] &&
          rrc->nr_cellid == f1_setup_req->nr_cellid[i]) {
        // check that CU rrc instance corresponds to mcc/mnc/cgi (normally cgi should be enough, but just in case)
        rrc->carrier.MIB = malloc(f1_setup_req->mib_length[i]);
        rrc->carrier.sizeof_MIB = f1_setup_req->mib_length[i];
        LOG_W(NR_RRC, "instance %d mib length %d\n", i, f1_setup_req->mib_length[i]);
        LOG_W(NR_RRC, "instance %d sib1 length %d\n", i, f1_setup_req->sib1_length[i]);
        memcpy((void *)rrc->carrier.MIB,f1_setup_req->mib[i],f1_setup_req->mib_length[i]);
2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763
        asn_dec_rval_t dec_rval = uper_decode_complete(NULL,
                                  &asn_DEF_NR_BCCH_BCH_Message,
                                  (void **)&rrc->carrier.mib_DU,
                                  f1_setup_req->mib[i],
                                  f1_setup_req->mib_length[i]);
        AssertFatal(dec_rval.code == RC_OK,
                    "[gNB_DU %"PRIu8"] Failed to decode NR_BCCH_BCH_MESSAGE (%zu bits)\n",
                    j,
                    dec_rval.consumed );
        NR_BCCH_BCH_Message_t *mib = &rrc->carrier.mib;
        NR_BCCH_BCH_Message_t *mib_DU = rrc->carrier.mib_DU;
        mib->message.present = NR_BCCH_BCH_MessageType_PR_mib;
        mib->message.choice.mib = CALLOC(1,sizeof(struct NR_MIB));
        memset(mib->message.choice.mib,0,sizeof(struct NR_MIB));
        memcpy(mib->message.choice.mib, mib_DU->message.choice.mib, sizeof(struct NR_MIB));

        rrc->carrier.SIB1 = malloc(f1_setup_req->sib1_length[i]);
        rrc->carrier.sizeof_SIB1 = f1_setup_req->sib1_length[i];
        memcpy((void *)rrc->carrier.SIB1,f1_setup_req->sib1[i],f1_setup_req->sib1_length[i]);
        dec_rval = uper_decode_complete(NULL,
                                        &asn_DEF_NR_BCCH_DL_SCH_Message,
                                        (void **)&rrc->carrier.siblock1_DU,
                                        f1_setup_req->sib1[i],
                                        f1_setup_req->sib1_length[i]);
        AssertFatal(dec_rval.code == RC_OK,
                    "[gNB_DU %"PRIu8"] Failed to decode NR_BCCH_DLSCH_MESSAGE (%zu bits)\n",
                    j,
                    dec_rval.consumed );

        // Parse message and extract SystemInformationBlockType1 field
        NR_BCCH_DL_SCH_Message_t *bcch_message = rrc->carrier.siblock1_DU;
        AssertFatal(bcch_message->message.present == NR_BCCH_DL_SCH_MessageType_PR_c1,
                    "bcch_message->message.present != NR_BCCH_DL_SCH_MessageType_PR_c1\n");
        AssertFatal(bcch_message->message.choice.c1->present == NR_BCCH_DL_SCH_MessageType__c1_PR_systemInformationBlockType1,
                    "bcch_message->message.choice.c1->present != NR_BCCH_DL_SCH_MessageType__c1_PR_systemInformationBlockType1\n");
        rrc->carrier.sib1 = bcch_message->message.choice.c1->choice.systemInformationBlockType1;
        rrc->carrier.physCellId = f1_setup_req->nr_pci[i];
2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783
	if (cu_cell_ind == 0) {
	  // prepare F1_SETUP_RESPONSE + GNB_CU_CONFIGURATION_UPDATE
	  if (msg_p == NULL) {
	    msg_p = itti_alloc_new_message (TASK_RRC_GNB, 0, F1AP_SETUP_RESP);
	  }
	  if (msg_p2 == NULL) {
	    msg_p2 = itti_alloc_new_message (TASK_RRC_GNB, 0, F1AP_GNB_CU_CONFIGURATION_UPDATE);
	  }
	  
	  F1AP_SETUP_RESP (msg_p).gNB_CU_name                                = rrc->node_name;
	  F1AP_SETUP_RESP (msg_p).num_cells_to_activate = 0;

	}

	F1AP_GNB_CU_CONFIGURATION_UPDATE (msg_p2).gNB_CU_name                                = rrc->node_name;	
	F1AP_GNB_CU_CONFIGURATION_UPDATE (msg_p2).cells_to_activate[cu_cell_ind].mcc                           = rrc->configuration.mcc[0];
	F1AP_GNB_CU_CONFIGURATION_UPDATE (msg_p2).cells_to_activate[cu_cell_ind].mnc                           = rrc->configuration.mnc[0];
	F1AP_GNB_CU_CONFIGURATION_UPDATE (msg_p2).cells_to_activate[cu_cell_ind].mnc_digit_length              = rrc->configuration.mnc_digit_length[0];
	F1AP_GNB_CU_CONFIGURATION_UPDATE (msg_p2).cells_to_activate[cu_cell_ind].nr_cellid                     = rrc->nr_cellid;
	F1AP_GNB_CU_CONFIGURATION_UPDATE (msg_p2).cells_to_activate[cu_cell_ind].nrpci                         = f1_setup_req->nr_pci[i];
Xue Song's avatar
Xue Song committed
2784 2785
        int num_SI= 0;

2786
        if (rrc->carrier.SIB23) {
2787 2788
          F1AP_GNB_CU_CONFIGURATION_UPDATE (msg_p2).cells_to_activate[cu_cell_ind].SI_container[2]        = rrc->carrier.SIB23;
          F1AP_GNB_CU_CONFIGURATION_UPDATE (msg_p2).cells_to_activate[cu_cell_ind].SI_container_length[2] = rrc->carrier.sizeof_SIB23;
2789 2790
          num_SI++;
        }
Xue Song's avatar
Xue Song committed
2791

2792
        F1AP_GNB_CU_CONFIGURATION_UPDATE (msg_p2).cells_to_activate[cu_cell_ind].num_SI = num_SI;
Xue Song's avatar
Xue Song committed
2793 2794
        cu_cell_ind++;
        found_cell=1;
2795 2796
	F1AP_GNB_CU_CONFIGURATION_UPDATE (msg_p2).num_cells_to_activate = cu_cell_ind;
	// send 
Xue Song's avatar
Xue Song committed
2797 2798
        break;
      } else {// setup_req mcc/mnc match rrc internal list element
Xue Song's avatar
Xue Song committed
2799
        LOG_W(NR_RRC,"[Inst %d] No matching MCC/MNC: rrc->mcc/f1_setup_req->mcc %d/%d rrc->mnc/f1_setup_req->mnc %d/%d rrc->nr_cellid/f1_setup_req->nr_cellid %ld/%ld \n",
Xue Song's avatar
Xue Song committed
2800 2801 2802
              j, rrc->configuration.mcc[0], f1_setup_req->mcc[i],
                 rrc->configuration.mnc[0], f1_setup_req->mnc[i],
                 rrc->nr_cellid, f1_setup_req->nr_cellid[i]);
Xue Song's avatar
Xue Song committed
2803 2804 2805 2806 2807 2808
      }
    }// for (int j=0;j<RC.nb_inst;j++)

    if (found_cell == 0) {
      AssertFatal(1 == 0, "No cell found\n");
    }
2809 2810 2811 2812 2813 2814 2815
    else {
      // send ITTI message to F1AP-CU task
      itti_send_msg_to_task (TASK_CU_F1, 0, msg_p);

      itti_send_msg_to_task (TASK_CU_F1, 0, msg_p2);

    }
Xue Song's avatar
Xue Song committed
2816 2817 2818 2819 2820

    // handle other failure cases
  }//for (int i=0;i<f1_setup_req->num_cells_available;i++)
}

Cedric Roux's avatar
Cedric Roux committed
2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836
void rrc_gNB_process_release_request(const module_id_t gnb_mod_idP, x2ap_ENDC_sgnb_release_request_t *m)
{
  gNB_RRC_INST *rrc = RC.nrrrc[gnb_mod_idP];
  rrc_remove_nsa_user(rrc, m->rnti);
}

void rrc_gNB_process_dc_overall_timeout(const module_id_t gnb_mod_idP, x2ap_ENDC_dc_overall_timeout_t *m)
{
  gNB_RRC_INST *rrc = RC.nrrrc[gnb_mod_idP];
  rrc_remove_nsa_user(rrc, m->rnti);
}

void nr_rrc_subframe_process(protocol_ctxt_t *const ctxt_pP, const int CC_id) {
  MessageDef *msg;

  /* send a tick to x2ap */
2837
  if (is_x2ap_enabled()){
2838
    msg = itti_alloc_new_message(TASK_RRC_ENB, 0, X2AP_SUBFRAME_PROCESS);
2839 2840
    itti_send_msg_to_task(TASK_X2AP, ctxt_pP->module_id, msg);
  }
Cedric Roux's avatar
Cedric Roux committed
2841 2842
}

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
2843 2844
///---------------------------------------------------------------------------------------------------------------///
///---------------------------------------------------------------------------------------------------------------///
2845
void *rrc_gnb_task(void *args_p) {
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
2846 2847
  MessageDef                         *msg_p;
  const char                         *msg_name_p;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
2848 2849
  instance_t                         instance;
  int                                result;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
2850 2851
  //SRB_INFO                           *srb_info_p;
  //int                                CC_id;
2852 2853 2854 2855 2856 2857 2858 2859 2860 2861
  protocol_ctxt_t ctxt={.module_id=0,
                        .enb_flag=1,
                        .instance=0,
                        .rnti=0,
                        .frame=-1,
                        .subframe=-1,
                        .eNB_index=0,
                        .configured=true,
                        .brOption=false
                       };
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
2862 2863
  itti_mark_task_ready(TASK_RRC_GNB);
  LOG_I(NR_RRC,"Entering main loop of NR_RRC message task\n");
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
2864 2865

  while (1) {
2866
    // Wait for a message
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
2867
    itti_receive_msg(TASK_RRC_GNB, &msg_p);
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
2868
    msg_name_p = ITTI_MSG_NAME(msg_p);
2869
    instance = ITTI_MSG_DESTINATION_INSTANCE(msg_p);
Cedric Roux's avatar
Cedric Roux committed
2870 2871 2872

    /* RRC_SUBFRAME_PROCESS is sent every subframe, do not log it */
    if (ITTI_MSG_ID(msg_p) != RRC_SUBFRAME_PROCESS)
2873
      LOG_I(NR_RRC,"Received message %s\n",msg_name_p);
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
2874 2875

    switch (ITTI_MSG_ID(msg_p)) {
2876
      case TERMINATE_MESSAGE:
Xue Song's avatar
Xue Song committed
2877
        LOG_W(NR_RRC, " *** Exiting NR_RRC thread\n");
2878 2879
        itti_exit_task();
        break;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
2880

2881
      case MESSAGE_TEST:
2882
        LOG_I(NR_RRC, "[gNB %ld] Received %s\n", instance, msg_name_p);
2883
        break;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
2884

Cedric Roux's avatar
Cedric Roux committed
2885 2886 2887 2888
      case RRC_SUBFRAME_PROCESS:
        nr_rrc_subframe_process(&RRC_SUBFRAME_PROCESS(msg_p).ctxt, RRC_SUBFRAME_PROCESS(msg_p).CC_id);
        break;

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
2889
      /* Messages from MAC */
2890
      case NR_RRC_MAC_CCCH_DATA_IND:
2891
        PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt,
2892 2893 2894 2895 2896
            NR_RRC_MAC_CCCH_DATA_IND(msg_p).gnb_index,
            GNB_FLAG_YES,
            NR_RRC_MAC_CCCH_DATA_IND(msg_p).rnti,
            msg_p->ittiMsgHeader.lte_time.frame,
            msg_p->ittiMsgHeader.lte_time.slot);
2897
        LOG_I(NR_RRC,"Decoding CCCH : ue %d, inst %ld, CC_id %d, ctxt %p, sib_info_p->Rx_buffer.payload_size %d\n",
2898 2899 2900 2901 2902
            ctxt.rnti,
            instance,
            NR_RRC_MAC_CCCH_DATA_IND(msg_p).CC_id,
            &ctxt,
            NR_RRC_MAC_CCCH_DATA_IND(msg_p).sdu_size);
Xue Song's avatar
Xue Song committed
2903 2904

        if (NR_RRC_MAC_CCCH_DATA_IND(msg_p).sdu_size >= CCCH_SDU_SIZE) {
2905 2906 2907
          LOG_I(NR_RRC, "CCCH message has size %d > %d\n",
              NR_RRC_MAC_CCCH_DATA_IND(msg_p).sdu_size,CCCH_SDU_SIZE);
          break;
2908 2909
      }

Xue Song's avatar
Xue Song committed
2910
      nr_rrc_gNB_decode_ccch(&ctxt,
2911 2912 2913 2914 2915 2916 2917 2918
			     (uint8_t *)NR_RRC_MAC_CCCH_DATA_IND(msg_p).sdu,
			     NR_RRC_MAC_CCCH_DATA_IND(msg_p).sdu_size,
			     NR_RRC_MAC_CCCH_DATA_IND(msg_p).du_to_cu_rrc_container,
			     NR_RRC_MAC_CCCH_DATA_IND(msg_p).CC_id);
      if (NR_RRC_MAC_CCCH_DATA_IND(msg_p).du_to_cu_rrc_container) {
	free(NR_RRC_MAC_CCCH_DATA_IND(msg_p).du_to_cu_rrc_container->buf);
	free(NR_RRC_MAC_CCCH_DATA_IND(msg_p).du_to_cu_rrc_container);
      }
2919
      break;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
2920 2921

      /* Messages from PDCP */
Xue Song's avatar
Xue Song committed
2922
      case NR_RRC_DCCH_DATA_IND:
2923 2924 2925 2926 2927 2928
        PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt,
                                      instance,
                                      GNB_FLAG_YES,
                                      NR_RRC_DCCH_DATA_IND(msg_p).rnti,
                                      msg_p->ittiMsgHeader.lte_time.frame,
                                      msg_p->ittiMsgHeader.lte_time.slot);
2929
        LOG_I(NR_RRC,"Decoding DCCH : ue %d, inst %ld, ctxt %p, size %d\n",
2930 2931 2932 2933
                ctxt.rnti,
                instance,
                &ctxt,
                NR_RRC_DCCH_DATA_IND(msg_p).sdu_size);
Xue Song's avatar
Xue Song committed
2934 2935 2936 2937 2938 2939 2940 2941
        LOG_D(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" Received on DCCH %d %s\n",
                PROTOCOL_NR_RRC_CTXT_UE_ARGS(&ctxt),
                NR_RRC_DCCH_DATA_IND(msg_p).dcch_index,
                msg_name_p);
        rrc_gNB_decode_dcch(&ctxt,
                            NR_RRC_DCCH_DATA_IND(msg_p).dcch_index,
                            NR_RRC_DCCH_DATA_IND(msg_p).sdu_p,
                            NR_RRC_DCCH_DATA_IND(msg_p).sdu_size);
2942 2943
        result = itti_free(ITTI_MSG_ORIGIN_ID(msg_p), NR_RRC_DCCH_DATA_IND(msg_p).sdu_p);

Xue Song's avatar
Xue Song committed
2944
        break;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
2945

Xue Song's avatar
Xue Song committed
2946 2947 2948 2949
      case NGAP_DOWNLINK_NAS:
        rrc_gNB_process_NGAP_DOWNLINK_NAS(msg_p, msg_name_p, instance, &rrc_gNB_mui);
        break;

2950 2951 2952 2953
      case NGAP_PDUSESSION_SETUP_REQ:
        rrc_gNB_process_NGAP_PDUSESSION_SETUP_REQ(msg_p, msg_name_p, instance);
        break;

2954 2955 2956 2957 2958 2959 2960
      case NGAP_PDUSESSION_RELEASE_COMMAND:
        rrc_gNB_process_NGAP_PDUSESSION_RELEASE_COMMAND(msg_p, msg_name_p, instance);
        break;

      case GTPV1U_GNB_DELETE_TUNNEL_RESP:
        break;

2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017
      /*
      #if defined(ENABLE_USE_MME)

            // Messages from S1AP
          case S1AP_DOWNLINK_NAS:
            rrc_eNB_process_S1AP_DOWNLINK_NAS(msg_p, msg_name_p, instance, &rrc_gNB_mui);
            break;

          case S1AP_INITIAL_CONTEXT_SETUP_REQ:
            rrc_eNB_process_S1AP_INITIAL_CONTEXT_SETUP_REQ(msg_p, msg_name_p, instance);
            break;

          case S1AP_UE_CTXT_MODIFICATION_REQ:
            rrc_eNB_process_S1AP_UE_CTXT_MODIFICATION_REQ(msg_p, msg_name_p, instance);
            break;

          case S1AP_PAGING_IND:
            LOG_D(RRC, "[eNB %d] Received Paging message from S1AP: %s\n", instance, msg_name_p);
            rrc_eNB_process_PAGING_IND(msg_p, msg_name_p, instance);
            break;

          case S1AP_E_RAB_SETUP_REQ:
            rrc_eNB_process_S1AP_E_RAB_SETUP_REQ(msg_p, msg_name_p, instance);
            LOG_D(RRC, "[eNB %d] Received the message %s\n", instance, msg_name_p);
            break;

          case S1AP_E_RAB_MODIFY_REQ:
            rrc_eNB_process_S1AP_E_RAB_MODIFY_REQ(msg_p, msg_name_p, instance);
            break;

          case S1AP_E_RAB_RELEASE_COMMAND:
            rrc_eNB_process_S1AP_E_RAB_RELEASE_COMMAND(msg_p, msg_name_p, instance);
            break;

          case S1AP_UE_CONTEXT_RELEASE_REQ:
            rrc_eNB_process_S1AP_UE_CONTEXT_RELEASE_REQ(msg_p, msg_name_p, instance);
            break;

          case S1AP_UE_CONTEXT_RELEASE_COMMAND:
            rrc_eNB_process_S1AP_UE_CONTEXT_RELEASE_COMMAND(msg_p, msg_name_p, instance);
            break;

          case GTPV1U_ENB_DELETE_TUNNEL_RESP:
            ///Nothing to do. Apparently everything is done in S1AP processing
            //LOG_I(RRC, "[eNB %d] Received message %s, not processed because procedure not synched\n",
            //instance, msg_name_p);
            if (rrc_eNB_get_ue_context(RC.nrrrc[instance], GTPV1U_ENB_DELETE_TUNNEL_RESP(msg_p).rnti)
                && rrc_eNB_get_ue_context(RC.nrrrc[instance], GTPV1U_ENB_DELETE_TUNNEL_RESP(msg_p).rnti)->ue_context.ue_release_timer_rrc > 0) {
              rrc_eNB_get_ue_context(RC.nrrrc[instance], GTPV1U_ENB_DELETE_TUNNEL_RESP(msg_p).rnti)->ue_context.ue_release_timer_rrc =
              rrc_eNB_get_ue_context(RC.nrrrc[instance], GTPV1U_ENB_DELETE_TUNNEL_RESP(msg_p).rnti)->ue_context.ue_release_timer_thres_rrc;
            }
            break;

      #endif
      */
      /* Messages from gNB app */
      case NRRRC_CONFIGURATION_REQ:
3018
        LOG_I(NR_RRC, "[gNB %ld] Received %s : %p\n", instance, msg_name_p,&NRRRC_CONFIGURATION_REQ(msg_p));
3019 3020 3021
        openair_rrc_gNB_configuration(GNB_INSTANCE_TO_MODULE_ID(instance), &NRRRC_CONFIGURATION_REQ(msg_p));
        break;

Xue Song's avatar
Xue Song committed
3022 3023 3024 3025
      /* Messages from F1AP task */
      case F1AP_SETUP_REQ:
        AssertFatal(NODE_IS_CU(RC.nrrrc[instance]->node_type),
                    "should not receive F1AP_SETUP_REQUEST, need call by CU!\n");
3026
        LOG_I(NR_RRC,"[gNB %ld] Received %s : %p\n", instance, msg_name_p, &F1AP_SETUP_REQ(msg_p));
Xue Song's avatar
Xue Song committed
3027 3028 3029
        rrc_gNB_process_f1_setup_req(&F1AP_SETUP_REQ(msg_p));
        break;

3030
      /* Messages from X2AP */
3031
      case X2AP_ENDC_SGNB_ADDITION_REQ:
3032 3033 3034
        LOG_I(NR_RRC, "Received ENDC sgNB addition request from X2AP \n");
        rrc_gNB_process_AdditionRequestInformation(GNB_INSTANCE_TO_MODULE_ID(instance), &X2AP_ENDC_SGNB_ADDITION_REQ(msg_p));
        break;
3035

3036
      case X2AP_ENDC_SGNB_RECONF_COMPLETE:
3037 3038
        LOG_I(NR_RRC, "Handling of reconfiguration complete message at RRC gNB is pending \n");
        break;
Xue Song's avatar
Xue Song committed
3039

3040 3041
      case NGAP_INITIAL_CONTEXT_SETUP_REQ:
        rrc_gNB_process_NGAP_INITIAL_CONTEXT_SETUP_REQ(msg_p, msg_name_p, instance);
yaojie's avatar
yaojie committed
3042
        break;
3043

Cedric Roux's avatar
Cedric Roux committed
3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055
      case X2AP_ENDC_SGNB_RELEASE_REQUEST:
        LOG_I(NR_RRC, "Received ENDC sgNB release request from X2AP \n");
        rrc_gNB_process_release_request(GNB_INSTANCE_TO_MODULE_ID(instance), &X2AP_ENDC_SGNB_RELEASE_REQUEST(msg_p));
        break;

      case X2AP_ENDC_DC_OVERALL_TIMEOUT:
        rrc_gNB_process_dc_overall_timeout(GNB_INSTANCE_TO_MODULE_ID(instance), &X2AP_ENDC_DC_OVERALL_TIMEOUT(msg_p));
        break;

      /* Messages from GTP */
      case GTPV1U_ENB_DELETE_TUNNEL_RESP:
        /* nothing to do? */
3056
        break;
3057

3058 3059 3060 3061 3062 3063 3064 3065
      case NGAP_UE_CONTEXT_RELEASE_REQ:
        rrc_gNB_process_NGAP_UE_CONTEXT_RELEASE_REQ(msg_p, msg_name_p, instance);
        break;

      case NGAP_UE_CONTEXT_RELEASE_COMMAND:
        rrc_gNB_process_NGAP_UE_CONTEXT_RELEASE_COMMAND(msg_p, msg_name_p, instance);
        break;

3066
      default:
3067
        LOG_E(NR_RRC, "[gNB %ld] Received unexpected message %s\n", instance, msg_name_p);
3068
        break;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
3069 3070 3071 3072 3073 3074 3075
    }

    result = itti_free(ITTI_MSG_ORIGIN_ID(msg_p), msg_p);
    AssertFatal(result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
    msg_p = NULL;
  }
}
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
3076

heshanyun's avatar
heshanyun committed
3077 3078 3079 3080
//-----------------------------------------------------------------------------
void
rrc_gNB_generate_SecurityModeCommand(
  const protocol_ctxt_t *const ctxt_pP,
Xue Song's avatar
Xue Song committed
3081
  rrc_gNB_ue_context_t  *const ue_context_pP
heshanyun's avatar
heshanyun committed
3082 3083 3084 3085 3086
)
//-----------------------------------------------------------------------------
{
  uint8_t                             buffer[100];
  uint8_t                             size;
3087

heshanyun's avatar
heshanyun committed
3088 3089 3090 3091 3092 3093
  T(T_ENB_RRC_SECURITY_MODE_COMMAND, T_INT(ctxt_pP->module_id), T_INT(ctxt_pP->frame),
    T_INT(ctxt_pP->subframe), T_INT(ctxt_pP->rnti));
  NR_IntegrityProtAlgorithm_t integrity_algorithm = (NR_IntegrityProtAlgorithm_t)ue_context_pP->ue_context.integrity_algorithm;
  size = do_NR_SecurityModeCommand(
           ctxt_pP,
           buffer,
heshanyun's avatar
heshanyun committed
3094
           rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id),
heshanyun's avatar
heshanyun committed
3095 3096 3097 3098 3099 3100 3101
           ue_context_pP->ue_context.ciphering_algorithm,
           &integrity_algorithm);
  LOG_DUMPMSG(NR_RRC,DEBUG_RRC,(char *)buffer,size,"[MSG] RRC Security Mode Command\n");
  LOG_I(NR_RRC,
        PROTOCOL_NR_RRC_CTXT_UE_FMT" Logical Channel DL-DCCH, Generate SecurityModeCommand (bytes %d)\n",
        PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
        size);
3102 3103 3104 3105

  switch (RC.nrrrc[ctxt_pP->module_id]->node_type) {
    case ngran_gNB_CU:
      // create an ITTI message
Xue Song's avatar
Xue Song committed
3106 3107
      memcpy(ue_context_pP->ue_context.Srb1.Srb_info.Tx_buffer.Payload, buffer, size);
      ue_context_pP->ue_context.Srb1.Srb_info.Tx_buffer.payload_size = size;
3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125

      LOG_I(NR_RRC,"calling rrc_data_req :securityModeCommand\n");
      nr_rrc_data_req(ctxt_pP,
                  DCCH,
                  rrc_gNB_mui++,
                  SDU_CONFIRM_NO,
                  size,
                  buffer,
                  PDCP_TRANSMISSION_MODE_CONTROL);
      break;

    case ngran_gNB_DU:
      // nothing to do for DU
      AssertFatal(1==0,"nothing to do for DU\n");
      break;

    case ngran_gNB:
      LOG_D(NR_RRC,
heshanyun's avatar
heshanyun committed
3126 3127 3128 3129 3130
        PROTOCOL_NR_RRC_CTXT_UE_FMT" --- PDCP_DATA_REQ/%d Bytes (securityModeCommand to UE MUI %d) --->[PDCP][RB %02d]\n",
        PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
        size,
        rrc_gNB_mui,
        DCCH);
3131 3132 3133 3134 3135 3136 3137 3138 3139 3140
      MSC_LOG_TX_MESSAGE(
        MSC_RRC_GNB,
        MSC_RRC_UE,
        buffer,
        size,
        MSC_AS_TIME_FMT" securityModeCommand UE %x MUI %d size %u",
        MSC_AS_TIME_ARGS(ctxt_pP),
        ue_context_pP->ue_context.rnti,
        rrc_gNB_mui,
        size);
3141
#ifdef ITTI_SIM
3142 3143
  MessageDef *message_p;
  uint8_t *message_buffer;
yaojie's avatar
yaojie committed
3144
  message_buffer = itti_malloc (TASK_RRC_GNB, TASK_RRC_UE_SIM,size);
3145
  memcpy (message_buffer, buffer, size);
3146
  message_p = itti_alloc_new_message (TASK_RRC_GNB, 0, GNB_RRC_DCCH_DATA_IND);
3147 3148 3149 3150
  GNB_RRC_DCCH_DATA_IND (message_p).rbid = DCCH;
  GNB_RRC_DCCH_DATA_IND (message_p).sdu = message_buffer;
  GNB_RRC_DCCH_DATA_IND (message_p).size	= size;
  itti_send_msg_to_task (TASK_RRC_UE_SIM, ctxt_pP->instance, message_p);
3151
#else
3152 3153 3154 3155 3156 3157 3158 3159
  LOG_I(NR_RRC,"calling rrc_data_req :securityModeCommand\n");
  nr_rrc_data_req(ctxt_pP,
      DCCH,
      rrc_gNB_mui++,
      SDU_CONFIRM_NO,
      size,
      buffer,
      PDCP_TRANSMISSION_MODE_CONTROL);
3160
#endif
3161
      break;
3162

3163 3164 3165
    default :
        LOG_W(NR_RRC, "Unknown node type %d\n", RC.nrrrc[ctxt_pP->module_id]->node_type);
  }
heshanyun's avatar
heshanyun committed
3166 3167
}

zhanghuiqin's avatar
zhanghuiqin committed
3168 3169 3170 3171 3172 3173 3174 3175 3176
void
rrc_gNB_generate_UECapabilityEnquiry(
  const protocol_ctxt_t *const ctxt_pP,
  rrc_gNB_ue_context_t          *const ue_context_pP
)
//-----------------------------------------------------------------------------
{
  uint8_t                             buffer[100];
  uint8_t                             size;
3177

zhanghuiqin's avatar
zhanghuiqin committed
3178 3179
  T(T_ENB_RRC_UE_CAPABILITY_ENQUIRY, T_INT(ctxt_pP->module_id), T_INT(ctxt_pP->frame),
    T_INT(ctxt_pP->subframe), T_INT(ctxt_pP->rnti));
Xue Song's avatar
Xue Song committed
3180
  size = do_NR_SA_UECapabilityEnquiry(
zhanghuiqin's avatar
zhanghuiqin committed
3181 3182 3183
           ctxt_pP,
           buffer,
           rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id));
3184 3185 3186
  LOG_I(NR_RRC,
        PROTOCOL_NR_RRC_CTXT_UE_FMT" Logical Channel DL-DCCH, Generate NR UECapabilityEnquiry (bytes %d)\n",
        PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
zhanghuiqin's avatar
zhanghuiqin committed
3187
        size);
3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209
  switch (RC.nrrrc[ctxt_pP->module_id]->node_type) {
    case ngran_gNB_CU:
      nr_rrc_data_req(
        ctxt_pP,
        DCCH,
        rrc_gNB_mui++,
        SDU_CONFIRM_NO,
        size,
        buffer,
        PDCP_TRANSMISSION_MODE_CONTROL);
      break;

    case ngran_gNB_DU:
      // nothing to do for DU
      AssertFatal(1==0,"nothing to do for DU\n");
      break;

    case ngran_gNB:
      // rrc_mac_config_req_gNB
      LOG_D(NR_RRC,
        PROTOCOL_NR_RRC_CTXT_UE_FMT" --- PDCP_DATA_REQ/%d Bytes (NR UECapabilityEnquiry MUI %d) --->[PDCP][RB %02d]\n",
        PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
zhanghuiqin's avatar
zhanghuiqin committed
3210 3211 3212
        size,
        rrc_gNB_mui,
        DCCH);
3213 3214 3215 3216 3217 3218 3219 3220 3221 3222
      MSC_LOG_TX_MESSAGE(
        MSC_RRC_GNB,
        MSC_RRC_UE,
        buffer,
        size,
        MSC_AS_TIME_FMT" rrcNRUECapabilityEnquiry UE %x MUI %d size %u",
        MSC_AS_TIME_ARGS(ctxt_pP),
        ue_context_pP->ue_context.rnti,
        rrc_gNB_mui,
        size);
3223
#ifdef ITTI_SIM
3224 3225
  MessageDef *message_p;
  uint8_t *message_buffer;
yaojie's avatar
yaojie committed
3226
  message_buffer = itti_malloc (TASK_RRC_GNB, TASK_RRC_UE_SIM, size);
3227
  memcpy (message_buffer, buffer, size);
3228
  message_p = itti_alloc_new_message (TASK_RRC_GNB, 0, GNB_RRC_DCCH_DATA_IND);
3229 3230 3231 3232
  GNB_RRC_DCCH_DATA_IND (message_p).rbid = DCCH;
  GNB_RRC_DCCH_DATA_IND (message_p).sdu = message_buffer;
  GNB_RRC_DCCH_DATA_IND (message_p).size  = size;
  itti_send_msg_to_task (TASK_RRC_UE_SIM, ctxt_pP->instance, message_p);
3233
#else
3234 3235 3236 3237 3238 3239 3240 3241
  nr_rrc_data_req(
      ctxt_pP,
      DCCH,
      rrc_gNB_mui++,
      SDU_CONFIRM_NO,
      size,
      buffer,
      PDCP_TRANSMISSION_MODE_CONTROL);
3242
#endif
3243
  break;
3244 3245 3246 3247

    default :
        LOG_W(NR_RRC, "Unknown node type %d\n", RC.nrrrc[ctxt_pP->module_id]->node_type);
  }
zhanghuiqin's avatar
zhanghuiqin committed
3248
}
heshanyun's avatar
heshanyun committed
3249 3250 3251 3252 3253 3254 3255

//-----------------------------------------------------------------------------
/*
* Generate the RRC Connection Release to UE.
* If received, UE should switch to RRC_IDLE mode.
*/
void
Xue Song's avatar
Xue Song committed
3256
rrc_gNB_generate_RRCRelease(
heshanyun's avatar
heshanyun committed
3257
  const protocol_ctxt_t *const ctxt_pP,
Xue Song's avatar
Xue Song committed
3258
  rrc_gNB_ue_context_t  *const ue_context_pP
heshanyun's avatar
heshanyun committed
3259 3260 3261 3262 3263 3264 3265 3266
)
//-----------------------------------------------------------------------------
{
  uint8_t buffer[RRC_BUF_SIZE];
  uint16_t size = 0;

  memset(buffer, 0, RRC_BUF_SIZE);

Xue Song's avatar
Xue Song committed
3267
  size = do_NR_RRCRelease(buffer,rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id));
heshanyun's avatar
heshanyun committed
3268 3269 3270
  ue_context_pP->ue_context.ue_reestablishment_timer = 0;
  ue_context_pP->ue_context.ue_release_timer = 0;
  LOG_I(NR_RRC,
Xue Song's avatar
Xue Song committed
3271 3272
        PROTOCOL_NR_RRC_CTXT_UE_FMT" Logical Channel DL-DCCH, Generate RRCRelease (bytes %d)\n",
        PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
heshanyun's avatar
heshanyun committed
3273 3274
        size);
  LOG_D(NR_RRC,
Xue Song's avatar
Xue Song committed
3275 3276
        PROTOCOL_NR_RRC_CTXT_UE_FMT" --- PDCP_DATA_REQ/%d Bytes (rrcRelease MUI %d) --->[PDCP][RB %u]\n",
        PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
heshanyun's avatar
heshanyun committed
3277 3278 3279 3280 3281 3282 3283 3284
        size,
        rrc_gNB_mui,
        DCCH);
  MSC_LOG_TX_MESSAGE(
    MSC_RRC_GNB,
    MSC_RRC_UE,
    buffer,
    size,
Xue Song's avatar
Xue Song committed
3285
    MSC_AS_TIME_FMT" NR_RRCRelease UE %x MUI %d size %u",
heshanyun's avatar
heshanyun committed
3286 3287 3288 3289 3290
    MSC_AS_TIME_ARGS(ctxt_pP),
    ue_context_pP->ue_context.rnti,
    rrc_gNB_mui,
    size);

3291 3292 3293
#ifdef ITTI_SIM
    MessageDef *message_p;
    uint8_t *message_buffer;
3294
    message_buffer = itti_malloc (TASK_RRC_GNB, TASK_RRC_UE_SIM, size);
3295
    memcpy (message_buffer, buffer, size);
3296
    message_p = itti_alloc_new_message (TASK_RRC_GNB, 0, GNB_RRC_DCCH_DATA_IND);
3297 3298 3299 3300 3301
    GNB_RRC_DCCH_DATA_IND (message_p).rbid = DCCH;
    GNB_RRC_DCCH_DATA_IND (message_p).sdu = message_buffer;
    GNB_RRC_DCCH_DATA_IND (message_p).size  = size;
    itti_send_msg_to_task (TASK_RRC_UE_SIM, ctxt_pP->instance, message_p);
#else
3302
  if (NODE_IS_CU(RC.nrrrc[ctxt_pP->module_id]->node_type)) {
3303
    MessageDef *m = itti_alloc_new_message(TASK_RRC_GNB, 0, F1AP_UE_CONTEXT_RELEASE_CMD);
heshanyun's avatar
heshanyun committed
3304 3305 3306 3307 3308 3309 3310
    F1AP_UE_CONTEXT_RELEASE_CMD(m).rnti = ctxt_pP->rnti;
    F1AP_UE_CONTEXT_RELEASE_CMD(m).cause = F1AP_CAUSE_RADIO_NETWORK;
    F1AP_UE_CONTEXT_RELEASE_CMD(m).cause_value = 10; // 10 = F1AP_CauseRadioNetwork_normal_release
    F1AP_UE_CONTEXT_RELEASE_CMD(m).rrc_container = buffer;
    F1AP_UE_CONTEXT_RELEASE_CMD(m).rrc_container_length = size;
    itti_send_msg_to_task(TASK_CU_F1, ctxt_pP->module_id, m);
  } else {
3311
    nr_rrc_data_req(ctxt_pP,
heshanyun's avatar
heshanyun committed
3312 3313 3314 3315 3316 3317 3318
                 DCCH,
                 rrc_gNB_mui++,
                 SDU_CONFIRM_NO,
                 size,
                 buffer,
                 PDCP_TRANSMISSION_MODE_CONTROL);
  }
3319
#endif
heshanyun's avatar
heshanyun committed
3320
}
Cedric Roux's avatar
Cedric Roux committed
3321 3322 3323
void nr_rrc_trigger(protocol_ctxt_t *ctxt, int CC_id, int frame, int subframe)
{
  MessageDef *message_p;
3324
  message_p = itti_alloc_new_message(TASK_RRC_GNB, 0, RRC_SUBFRAME_PROCESS);
Cedric Roux's avatar
Cedric Roux committed
3325 3326 3327 3328
  RRC_SUBFRAME_PROCESS(message_p).ctxt  = *ctxt;
  RRC_SUBFRAME_PROCESS(message_p).CC_id = CC_id;
  itti_send_msg_to_task(TASK_RRC_GNB, ctxt->module_id, message_p);
}