rrc_gNB.c 76.4 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"
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
57 58 59 60 61

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

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
64 65 66 67 68 69 70
#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
71
#include "UTIL/OSA/osa_defs.h"
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
72

73
#include "rrc_eNB_S1AP.h"
Xue Song's avatar
Xue Song committed
74
#include "rrc_gNB_NGAP.h"
Xue Song's avatar
Xue Song committed
75

76 77
#include "rrc_eNB_GTPV1U.h"

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
78

79
#include "pdcp.h"
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
80 81
#include "gtpv1u_eNB_task.h"

82

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

86
#include "executables/softmodem-common.h"
laurent's avatar
laurent committed
87
#include <openair2/RRC/NR/rrc_gNB_UE_context.h>
88

Xue Song's avatar
Xue Song committed
89 90 91
#include "BIT_STRING.h"
#include "assertions.h"

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
92 93 94 95
//#define XER_PRINT

extern RAN_CONTEXT_t RC;

Xue Song's avatar
Xue Song committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
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,
    uint8_t                  *const kUPenc
  #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,
    struct NR_CellGroupConfig__rlc_BearerToAddModList *rlc_bearer2add_list);

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

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
120
mui_t                               rrc_gNB_mui = 0;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
121

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
122 123
///---------------------------------------------------------------------------------------------------------------///
///---------------------------------------------------------------------------------------------------------------///
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
124

125
void openair_nr_rrc_on(const protocol_ctxt_t *const ctxt_pP) {
126
  LOG_I(NR_RRC, PROTOCOL_NR_RRC_CTXT_FMT" gNB:OPENAIR NR RRC IN....\n",PROTOCOL_NR_RRC_CTXT_ARGS(ctxt_pP));
127 128 129 130
  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
131 132
}

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
133 134 135
///---------------------------------------------------------------------------------------------------------------///
///---------------------------------------------------------------------------------------------------------------///

136 137 138 139
void rrc_gNB_process_SgNBAdditionRequest(
  const protocol_ctxt_t  *const ctxt_pP,
  rrc_gNB_ue_context_t   *ue_context_pP
) {
140 141 142
  rrc_gNB_generate_SgNBAdditionRequestAcknowledge(ctxt_pP,ue_context_pP);
}

143 144 145
void rrc_gNB_generate_SgNBAdditionRequestAcknowledge(
  const protocol_ctxt_t  *const ctxt_pP,
  rrc_gNB_ue_context_t   *const ue_context_pP) {
146 147 148 149
  //uint8_t size;
  //uint8_t buffer[100];
  //int     CC_id = ue_context_pP->ue_context.primaryCC_id;
  //OCTET_STRING_t                                      *secondaryCellGroup;
150
  NR_CellGroupConfig_t                                *cellGroupconfig;
151 152 153 154
  struct NR_CellGroupConfig__rlc_BearerToAddModList   *rlc_BearerToAddModList;
  struct NR_MAC_CellGroupConfig                       *mac_CellGroupConfig;
  struct NR_PhysicalCellGroupConfig                   *physicalCellGroupConfig;
  struct NR_SpCellConfig                              *spCellConfig;
155
  //struct NR_CellGroupConfig__sCellToAddModList        *sCellToAddModList;
156 157 158 159 160
  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
161
  //cellGroupconfig->sCellToAddModList        = CALLOC(1,sizeof(struct NR_CellGroupConfig__sCellToAddModList));
162 163 164 165
  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
166
  //sCellToAddModList        = cellGroupconfig->sCellToAddModList;
167 168 169 170 171 172 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
  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);
203
  do_SpCellConfig(RC.nrrrc[ctxt_pP->module_id],
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
204
                  spCellConfig);
205 206 207 208 209
}

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

210
static void init_NR_SI(gNB_RRC_INST *rrc) {
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
211
  LOG_D(RRC,"%s()\n\n\n\n",__FUNCTION__);
212
  rrc->carrier.MIB             = (uint8_t *) malloc16(4);
213
  rrc->carrier.sizeof_MIB      = do_MIB_NR(rrc,0);
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
214
  LOG_I(NR_RRC,"Done init_NR_SI\n");
215
  rrc_mac_config_req_gNB(rrc->module_id,
216
                         rrc->carrier.ssb_SubcarrierOffset,
217
                         rrc->carrier.pdsch_AntennaPorts,
218
                         rrc->carrier.pusch_TargetSNRx10,
219
                         rrc->carrier.pucch_TargetSNRx10,
220
                         (NR_ServingCellConfigCommon_t *)rrc->carrier.servingcellconfigcommon,
221 222 223 224
                         0,
                         0, // WIP hardcoded rnti
                         (NR_CellGroupConfig_t *)NULL
                        );
Raymond Knopp's avatar
Raymond Knopp committed
225

226
  if (get_softmodem_params()->phy_test > 0 || get_softmodem_params()->do_ra > 0) {
227 228 229
    // This is for phytest only, emulate first X2 message if uecap.raw file is present
    FILE *fd;
    fd = fopen("uecap.raw","r");
230

231 232 233 234 235 236
    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,
237 238 239 240 241
                                &asn_DEF_LTE_UL_DCCH_Message,
                                (void **)&LTE_UL_DCCH_Message,
                                (uint8_t *)buffer,
                                msg_len);

242 243
      if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) {
        AssertFatal(1==0,"NR_UL_DCCH_MESSAGE decode error\n");
244 245 246 247 248
        // free the memory
        SEQUENCE_free( &asn_DEF_LTE_UL_DCCH_Message, LTE_UL_DCCH_Message, 1 );
        return;
      }

249 250 251 252
      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,
253
                  "ueCapabilityInformation not present\n");
254 255 256 257 258 259 260
      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));
261 262
      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);
263
      AssertFatal (enc_rval.encoded > 0, "ASN1 message encoding failed (%s, %jd)!\n",
264
                   enc_rval.failed_type->name, enc_rval.encoded);
265
      OCTET_STRING_fromBuf(cg_ConfigInfo->ue_CapabilityInfo,
266 267
                           (const char *)buffer,
                           (enc_rval.encoded+7)>>3);
268
      parse_CG_ConfigInfo(rrc,CG_ConfigInfo,NULL);
269
    } else {
270
      struct rrc_gNB_ue_context_s *ue_context_p = rrc_gNB_allocate_new_UE_context(rrc);
271
      LOG_I(NR_RRC,"Adding new user (%p)\n",ue_context_p);
272
      rrc_add_nsa_user(rrc,ue_context_p,NULL);
273
    }
274
  }
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
275
}
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
276 277


278
char openair_rrc_gNB_configuration(const module_id_t gnb_mod_idP, gNB_RrcConfigurationReq *configuration) {
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
279
  protocol_ctxt_t      ctxt;
280
  gNB_RRC_INST         *rrc=RC.nrrrc[gnb_mod_idP];
281
  PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, gnb_mod_idP, GNB_FLAG_YES, NOT_A_RNTI, 0, 0,gnb_mod_idP);
282 283 284
  LOG_I(NR_RRC,
        PROTOCOL_NR_RRC_CTXT_FMT" Init...\n",
        PROTOCOL_NR_RRC_CTXT_ARGS(&ctxt));
285

286 287 288 289 290 291
  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
292
  nr_uid_linear_allocator_init(&rrc->uid_allocator);
293 294 295
  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);
296 297
  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);
298
  rrc->carrier.servingcellconfigcommon = configuration->scc;
299 300
  rrc->carrier.ssb_SubcarrierOffset = configuration->ssb_SubcarrierOffset;
  rrc->carrier.pdsch_AntennaPorts = configuration->pdsch_AntennaPorts;
301
  rrc->carrier.pusch_TargetSNRx10 = configuration->pusch_TargetSNRx10;
302
  rrc->carrier.pucch_TargetSNRx10 = configuration->pucch_TargetSNRx10;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
303
  /// System Information INIT
304
  LOG_I(NR_RRC, PROTOCOL_NR_RRC_CTXT_FMT" Checking release \n",PROTOCOL_NR_RRC_CTXT_ARGS(&ctxt));
305
  init_NR_SI(rrc);
306
  rrc_init_nr_global_param();
307
  openair_nr_rrc_on(&ctxt);
308
  return 0;
309
}//END openair_rrc_gNB_configuration
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
310 311


312
void rrc_gNB_process_AdditionRequestInformation(const module_id_t gnb_mod_idP, x2ap_ENDC_sgnb_addition_req_t *m) {
313 314 315 316 317 318 319
  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];
320

321 322 323 324 325 326
  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;
  }
327

328 329 330 331 332
  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);
333 334
}

335
//-----------------------------------------------------------------------------
Xue Song's avatar
Xue Song committed
336 337 338
uint8_t
rrc_gNB_get_next_transaction_identifier(
    module_id_t gnb_mod_idP
339 340 341
)
//-----------------------------------------------------------------------------
{
Xue Song's avatar
Xue Song committed
342 343 344 345
    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];
346 347 348 349 350
}

//-----------------------------------------------------------------------------
void
rrc_gNB_generate_RRCSetup(
Xue Song's avatar
Xue Song committed
351 352 353
    const protocol_ctxt_t    *const ctxt_pP,
    rrc_gNB_ue_context_t     *const ue_context_pP,
    const int                CC_id
354 355 356
)
//-----------------------------------------------------------------------------
{
357
    LOG_I(NR_RRC, "rrc_gNB_generate_RRCSetup \n");
358
    NR_SRB_ToAddModList_t        *SRB_configList = NULL;
Xue Song's avatar
Xue Song committed
359 360 361 362 363 364 365

    // 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;
366
    SRB_configList = ue_p->SRB_configList;
367
    ue_p->Srb0.Tx_buffer.payload_size = do_RRCSetup(ctxt_pP,
Xue Song's avatar
Xue Song committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
                ue_context_pP,
                CC_id,
                (uint8_t *) ue_p->Srb0.Tx_buffer.Payload,
                rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id),
                SRB_configList);

    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));

Xue Song's avatar
Xue Song committed
383
    // rrc_mac_config_req_eNB
Xue Song's avatar
Xue Song committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403

    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,
        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);
    // 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;
heshanyun's avatar
heshanyun committed
404 405
#ifdef ITTI_SIM
    MessageDef *message_p;
Xue Song's avatar
Xue Song committed
406 407 408 409
    uint8_t *message_buffer;
    message_buffer = itti_malloc (TASK_RRC_GNB_SIM, 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);
Xue Song's avatar
Xue Song committed
410
    message_p = itti_alloc_new_message (TASK_RRC_GNB_SIM, GNB_RRC_CCCH_DATA_IND);
Xue Song's avatar
Xue Song committed
411
    GNB_RRC_CCCH_DATA_IND (message_p).sdu = message_buffer;
heshanyun's avatar
heshanyun committed
412 413 414
    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);
#endif
415 416 417 418
}

void
rrc_gNB_generate_RRCReject(
Xue Song's avatar
Xue Song committed
419 420 421
    const protocol_ctxt_t    *const ctxt_pP,
    rrc_gNB_ue_context_t     *const ue_context_pP,
    const int                CC_id
422 423 424
)
//-----------------------------------------------------------------------------
{
Xue Song's avatar
Xue Song committed
425
    LOG_I(NR_RRC, "rrc_gNB_generate_RRCReject \n");
426 427 428
    gNB_RRC_UE_t *ue_p = &ue_context_pP->ue_context;
    ue_p->Srb0.Tx_buffer.payload_size = do_RRCReject(ctxt_pP->module_id, 
                                                    (uint8_t *)ue_p->Srb0.Tx_buffer.Payload);
Xue Song's avatar
Xue Song committed
429
    LOG_DUMPMSG(NR_RRC, DEBUG_RRC,
430 431 432
                (char *)(ue_p->Srb0.Tx_buffer.Payload),
                ue_p->Srb0.Tx_buffer.payload_size,
                "[MSG] RRCReject \n");
Xue Song's avatar
Xue Song committed
433
    MSC_LOG_TX_MESSAGE(MSC_RRC_GNB,
434 435 436 437 438 439 440
                      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);
Xue Song's avatar
Xue Song committed
441 442 443
    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),
444
          ue_p->Srb0.Tx_buffer.payload_size);
Xue Song's avatar
Xue Song committed
445 446 447

#ifdef ITTI_SIM
    MessageDef *message_p;
Xue Song's avatar
Xue Song committed
448 449 450 451
    uint8_t *message_buffer;
    message_buffer = itti_malloc (TASK_RRC_GNB_SIM, 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);
Xue Song's avatar
Xue Song committed
452
    message_p = itti_alloc_new_message (TASK_RRC_GNB_SIM, GNB_RRC_CCCH_DATA_IND);
Xue Song's avatar
Xue Song committed
453
    GNB_RRC_CCCH_DATA_IND (message_p).sdu = message_buffer;
Xue Song's avatar
Xue Song committed
454 455 456
    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);
#endif
457 458
}

Xue Song's avatar
Xue Song committed
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
//-----------------------------------------------------------------------------
/*
* 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
)
//-----------------------------------------------------------------------------
{
    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;
Xue Song's avatar
Xue Song committed
474 475 476 477 478
    ue_context_pP->ue_context.Status = NR_RRC_CONNECTED;

    if (AMF_MODE_ENABLED) {
        rrc_gNB_send_NGAP_NAS_FIRST_REQ(ctxt_pP, ue_context_pP, rrcSetupComplete);
    } else {
Xue Song's avatar
Xue Song committed
479
        rrc_gNB_generate_SecurityModeCommand(ctxt_pP, ue_context_pP);
Xue Song's avatar
Xue Song committed
480
    }
Xue Song's avatar
Xue Song committed
481 482
}

Xue Song's avatar
Xue Song committed
483 484 485 486 487 488 489 490
//-----------------------------------------------------------------------------
void 
rrc_gNB_generate_defaultRRCReconfiguration(
    const protocol_ctxt_t     *const ctxt_pP,
    rrc_gNB_ue_context_t      *ue_context_pP
)
//-----------------------------------------------------------------------------
{
491
    // gNB_RRC_UE_t           *ue_p = &ue_context_pP->ue_context;
Xue Song's avatar
Xue Song committed
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
    uint8_t                buffer[RRC_BUF_SIZE];
    uint16_t               size;
    gNB_RRC_INST           *gnb_rrc_inst = RC.nrrrc[ctxt_pP->module_id];

    size = do_RRCReconfiguration(ctxt_pP, ue_context_pP, buffer,
                                 rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id),
                                 gnb_rrc_inst);
    LOG_DUMPMSG(NR_RRC, DEBUG_RRC,(char *)buffer, size, "[MSG] RRC Reconfiguration\n");

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

    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);
    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" NR_RRCReconfiguration UE %x MUI %d size %u",
                        MSC_AS_TIME_ARGS(ctxt_pP),
                        ue_context_pP->ue_context.rnti,
                        rrc_gNB_mui,
                        size);
525 526
#ifdef ITTI_SIM
      MessageDef *message_p;
Xue Song's avatar
Xue Song committed
527 528 529
      uint8_t *message_buffer;
      message_buffer = itti_malloc (TASK_RRC_GNB_SIM, TASK_RRC_UE_SIM, size);
      memcpy (message_buffer, buffer, size);
530 531
      message_p = itti_alloc_new_message (TASK_RRC_GNB_SIM, GNB_RRC_DCCH_DATA_IND);
      GNB_RRC_DCCH_DATA_IND (message_p).rbid = DCCH;
Xue Song's avatar
Xue Song committed
532
      GNB_RRC_DCCH_DATA_IND (message_p).sdu = message_buffer;
533 534 535
      GNB_RRC_DCCH_DATA_IND (message_p).size	= size;
      itti_send_msg_to_task (TASK_RRC_UE_SIM, ctxt_pP->instance, message_p);
#else
536
    nr_rrc_data_req(ctxt_pP,
537 538 539 540 541 542
                DCCH,
                rrc_gNB_mui++,
                SDU_CONFIRM_NO,
                size,
                buffer,
                PDCP_TRANSMISSION_MODE_CONTROL);
543
#endif
Xue Song's avatar
Xue Song committed
544 545 546 547
    // rrc_pdcp_config_asn1_req
    // rrc_rlc_config_asn1_req
}

548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
//-----------------------------------------------------------------------------
/*
* 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
)
{
    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;
567
    uint8_t                             nr_DRB2LCHAN[8];
568 569 570

    ue_context_pP->ue_context.ue_reestablishment_timer = 0;

571
#ifndef PHYSIM
572
    /* Derive the keys from kgnb */
573 574
    if (DRB_configList != NULL) {
        derive_key_up_enc(ue_context_pP->ue_context.ciphering_algorithm,
575
                        ue_context_pP->ue_context.kgnb,
576 577 578 579
                        &kUPenc);
    }

    derive_key_rrc_enc(ue_context_pP->ue_context.ciphering_algorithm,
580
                        ue_context_pP->ue_context.kgnb,
581 582
                        &kRRCenc);
    derive_key_rrc_int(ue_context_pP->ue_context.integrity_algorithm,
583
                        ue_context_pP->ue_context.kgnb,
584
                        &kRRCint);
585
#endif
586 587 588 589 590
    /* 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);

Xue Song's avatar
Xue Song committed
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
#ifndef ITTI_SIM
    nr_rrc_pdcp_config_asn1_req(ctxt_pP,
                                SRB_configList, // NULL,
                                DRB_configList,
                                DRB_Release_configList2,
                                0xff, // already configured during the securitymodecommand
                                kRRCenc,
                                kRRCint,
                                kUPenc,
                                NULL,
                                NULL,
                                NULL);
    /* Refresh SRBs/DRBs */
    nr_rrc_rlc_config_asn1_req(ctxt_pP,
                            SRB_configList, // NULL,
                            DRB_configList,
                            DRB_Release_configList2,
                            NULL,
                            NULL);
#endif
611 612

    /* Loop through DRBs and establish if necessary */
yaojie's avatar
yaojie committed
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
    /* 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;
        } 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);
        }
      }

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

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
    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;
            LOG_I(NR_RRC, "[gNB %d] Frame  %d : Logical Channel UL-DCCH, Received NR_RRCReconfigurationComplete from UE rnti %x, reconfiguring DRB %d/LCID %d\n",
                ctxt_pP->module_id,
                ctxt_pP->frame,
                ctxt_pP->rnti,
                (int)DRB_configList->list.array[i]->drb_Identity,
                (int)*DRB_configList->list.array[i]->pdcp_Config->moreThanOneRLC->primaryPath.logicalChannel);

            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);

                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));

                    if (DRB_configList->list.array[i]->pdcp_Config->moreThanOneRLC->primaryPath.logicalChannel) {
659
                        nr_DRB2LCHAN[i] = (uint8_t) * DRB_configList->list.array[i]->pdcp_Config->moreThanOneRLC->primaryPath.logicalChannel;
660 661 662 663 664 665 666 667 668 669 670 671 672
                    }

                // rrc_mac_config_req_eNB
            } else {        // remove LCHAN from MAC/PHY
            if (ue_context_pP->ue_context.DRB_active[drb_id] == 1) {
                // 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);
                */
                rrc_rlc_config_req(ctxt_pP,
                                    SRB_FLAG_NO,
                                    MBMS_FLAG_NO,
                                    CONFIG_ACTION_REMOVE,
673
                                    nr_DRB2LCHAN[i],
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
                                    Rlc_info_um);
            }

            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;
                }
            }
        }

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

709
/*------------------------------------------------------------------------------*/
Xue Song's avatar
Xue Song committed
710 711 712 713
int nr_rrc_gNB_decode_ccch(protocol_ctxt_t    *const ctxt_pP,
                           const uint8_t      *buffer,
                           int                buffer_length,
                           const int          CC_id)
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
{
    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;
    uint64_t                                         random_value = 0;

    dec_rval = uper_decode( NULL,
                            &asn_DEF_NR_UL_CCCH_Message,
                            (void **)&ul_ccch_msg,
                            (uint8_t *) buffer,
                            100,
                            0,
                            0);

    if (dec_rval.consumed == 0) {
        /* TODO */
Xue Song's avatar
Xue Song committed
732 733
        LOG_E(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" FATAL Error in receiving CCCH\n",
                   PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
734 735 736 737 738 739 740
        return -1;
    }

    if (ul_ccch_msg->message.present == NR_UL_CCCH_MessageType_PR_c1) {
        switch (ul_ccch_msg->message.choice.c1->present) {
            case NR_UL_CCCH_MessageType__c1_PR_NOTHING:
                /* TODO */
Xue Song's avatar
Xue Song committed
741 742 743
                LOG_I(NR_RRC,
                        PROTOCOL_NR_RRC_CTXT_FMT" Received PR_NOTHING on UL-CCCH-Message\n",
                        PROTOCOL_NR_RRC_CTXT_ARGS(ctxt_pP));
744 745 746 747 748 749 750
                break;

            case NR_UL_CCCH_MessageType__c1_PR_rrcSetupRequest:
                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(
Xue Song's avatar
Xue Song committed
751
                        MSC_RRC_GNB,
752 753 754 755 756 757 758 759 760 761
                        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) {
Xue Song's avatar
Xue Song committed
762
                        /* randomValue                         BIT STRING (SIZE (39)) */
763
                        if (rrcSetupRequest->ue_Identity.choice.randomValue.size != 5) { // 39-bit random value
Xue Song's avatar
Xue Song committed
764
                            LOG_E(NR_RRC, "wrong InitialUE-Identity randomValue size, expected 5, provided %lu",
765 766 767 768 769 770 771 772 773 774 775 776
                                        (long unsigned int)rrcSetupRequest->ue_Identity.choice.randomValue.size);
                            return -1;
                        }

                        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))) {
Xue Song's avatar
Xue Song committed
777
                            LOG_W(NR_RRC, "new UE rnti %x (coming with random value) is already there as UE %x, removing %x from MAC/PHY\n",
778 779 780 781 782 783 784 785 786 787 788 789 790 791
                                    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 */
Xue Song's avatar
Xue Song committed
792 793
                        /* ng-5G-S-TMSI-Part1                  BIT STRING (SIZE (39)) */
                        if (rrcSetupRequest->ue_Identity.choice.ng_5G_S_TMSI_Part1.size != 5) {
Xue Song's avatar
Xue Song committed
794
                            LOG_E(NR_RRC, "wrong ng_5G_S_TMSI_Part1 size, expected 5, provided %lu \n", 
Xue Song's avatar
Xue Song committed
795
                                        (long unsigned int)rrcSetupRequest->ue_Identity.choice.ng_5G_S_TMSI_Part1.size);
796 797 798
                            return -1;
                        }

Xue Song's avatar
Xue Song committed
799
                        uint64_t s_tmsi_part1 = bitStr_to_uint64(&rrcSetupRequest->ue_Identity.choice.ng_5G_S_TMSI_Part1);
Xue Song's avatar
Xue Song committed
800

Xue Song's avatar
Xue Song committed
801 802 803 804 805 806
                        // 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);
807

808
                            nr_rrc_mac_remove_ue(ctxt_pP->module_id, ue_context_p->ue_context.rnti);
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823

                            /* 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 {
824
                            LOG_I(NR_RRC, " 5G-S-TMSI-Part1 doesn't exist, setting ng_5G_S_TMSI_Part1 to %p => %ld\n",
Xue Song's avatar
Xue Song committed
825
                                            ue_context_p, s_tmsi_part1);
826

Xue Song's avatar
Xue Song committed
827
                            ue_context_p = rrc_gNB_get_next_free_ue_context(ctxt_pP, RC.nrrrc[ctxt_pP->module_id], s_tmsi_part1);
828 829 830 831 832 833

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

                            if (ue_context_p != NULL) {
Xue Song's avatar
Xue Song committed
834 835
                                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;
836 837
                            }
                        }
Xue Song's avatar
Xue Song committed
838
                    } else {
839
                        /* TODO */
Xue Song's avatar
Xue Song committed
840 841 842 843 844
                        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);
Xue Song's avatar
Xue Song committed
845 846 847
                        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));
848 849 850 851
                        rrc_gNB_generate_RRCReject(ctxt_pP,
                                                   rrc_gNB_get_ue_context(gnb_rrc_inst, ctxt_pP->rnti),
                                                   CC_id);
                        break;
Xue Song's avatar
Xue Song committed
852
                    }
853 854 855 856 857 858 859 860 861 862 863 864
                }

                if (ue_context_p != NULL) {
                    ue_context_p->ue_context.establishment_cause = rrcSetupRequest->establishmentCause;
                }

                rrc_gNB_generate_RRCSetup(ctxt_pP,
                                          rrc_gNB_get_ue_context(gnb_rrc_inst, ctxt_pP->rnti),
                                          CC_id);
                break;

            case NR_UL_CCCH_MessageType__c1_PR_rrcResumeRequest:
Xue Song's avatar
Xue Song committed
865 866
                LOG_I(NR_RRC, "receive rrcResumeRequest message \n");
                /* TODO */
867 868 869
                break;

            case NR_UL_CCCH_MessageType__c1_PR_rrcReestablishmentRequest:
Xue Song's avatar
Xue Song committed
870 871
                LOG_I(NR_RRC, "receive rrcReestablishmentRequest message \n");
                /* TODO */
872 873 874
                break;

            case NR_UL_CCCH_MessageType__c1_PR_rrcSystemInfoRequest:
Xue Song's avatar
Xue Song committed
875 876
                LOG_I(NR_RRC, "receive rrcSystemInfoRequest message \n");
                /* TODO */
877 878 879
                break;

            default:
Xue Song's avatar
Xue Song committed
880 881
                LOG_E(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" Unknown message\n",
                           PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
882 883 884
                break;
        }
    }
885
    return 0;
886 887
}

Xue Song's avatar
Xue Song committed
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
/*! \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
911 912 913 914 915 916 917 918 919 920 921 922 923
//-----------------------------------------------------------------------------
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
)
//-----------------------------------------------------------------------------
{
    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;
924
    // NR_RRCSetupComplete_t               *rrcSetupComplete = NULL;
Xue Song's avatar
Xue Song committed
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947

    int i;

    if ((Srb_id != 1) && (Srb_id != 2)) {
    LOG_E(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" Received message on SRB%ld, should not have ...\n",
            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));
    dec_rval = uper_decode(
                    NULL,
                    &asn_DEF_NR_UL_DCCH_Message,
                    (void **)&ul_dcch_msg,
                    Rx_sdu,
                    sdu_sizeP,
                    0,
                    0);
Xue Song's avatar
Xue Song committed
948
    // xer_fprint(stdout, &asn_DEF_NR_UL_DCCH_Message, (void *)&ul_dcch_msg);
Xue Song's avatar
Xue Song committed
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976

    {
        for (i = 0; i < sdu_sizeP; i++) {
            LOG_T(NR_RRC, "%x.", Rx_sdu[i]);
        }

        LOG_T(NR_RRC, "\n");
    }

    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;

            case NR_UL_DCCH_MessageType__c1_PR_rrcReconfigurationComplete:
977
		LOG_I(NR_RRC, "Receive RRC Reconfiguration Complete message UE %x\n", ctxt_pP->rnti);
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 1005 1006 1007 1008 1009 1010 1011
                if(!ue_context_p) {
                    LOG_I(NR_RRC, "Processing NR_RRCReconfigurationComplete 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 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",
                    PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
                    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);
                }

                rrc_gNB_send_NGAP_INITIAL_CONTEXT_SETUP_RESP(ctxt_pP, ue_context_p);

Xue Song's avatar
Xue Song committed
1012 1013 1014 1015 1016
                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);
1017
                    break;
Xue Song's avatar
Xue Song committed
1018 1019
                }

1020
                LOG_DUMPMSG(NR_RRC, DEBUG_RRC,(char *)Rx_sdu,sdu_sizeP,
Xue Song's avatar
Xue Song committed
1021 1022 1023 1024 1025 1026
                            "[MSG] RRC SetupComplete\n");
                MSC_LOG_RX_MESSAGE(
                        MSC_RRC_GNB,
                        MSC_RRC_UE,
                        Rx_sdu,
                        sdu_sizeP,
1027
                        MSC_AS_TIME_FMT" NR_RRCSetupComplete UE %x size %u",
Xue Song's avatar
Xue Song committed
1028 1029 1030 1031 1032
                        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 "
1033
                        "(RRCSetupComplete) ---> RRC_gNB\n",
Xue Song's avatar
Xue Song committed
1034 1035 1036 1037 1038 1039 1040
                        PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
                        DCCH,
                        sdu_sizeP);

                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.
Xue Song's avatar
Xue Song committed
1041
                        rrcSetupComplete->ng_5G_S_TMSI_Value != NULL) {
Xue Song's avatar
Xue Song committed
1042
                        if (ul_dcch_msg->message.choice.c1->choice.rrcSetupComplete->criticalExtensions.choice.
Xue Song's avatar
Xue Song committed
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
                            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;
                            }
Xue Song's avatar
Xue Song committed
1053

Xue Song's avatar
Xue Song committed
1054 1055 1056 1057 1058 1059
                            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);
                            }
Xue Song's avatar
Xue Song committed
1060

Xue Song's avatar
Xue Song committed
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
                            /* 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;
                            }
Xue Song's avatar
Xue Song committed
1073

Xue Song's avatar
Xue Song committed
1074
                            uint64_t fiveg_s_TMSI = bitStr_to_uint64(&ul_dcch_msg->message.choice.c1->choice.rrcSetupComplete->
Xue Song's avatar
Xue Song committed
1075
                                    criticalExtensions.choice.rrcSetupComplete->ng_5G_S_TMSI_Value->choice.ng_5G_S_TMSI);
yaojie's avatar
yaojie committed
1076
                            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",
Xue Song's avatar
Xue Song committed
1077 1078 1079 1080 1081 1082 1083 1084
                                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;
                            }
Xue Song's avatar
Xue Song committed
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
                        }
                    }

                    rrc_gNB_process_RRCSetupComplete(
                            ctxt_pP,
                            ue_context_p,
                            ul_dcch_msg->message.choice.c1->choice.rrcSetupComplete->criticalExtensions.choice.rrcSetupComplete);
                    LOG_I(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" UE State = RRC_CONNECTED \n",
                        PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
                }

                ue_context_p->ue_context.ue_release_timer = 0;
                break;
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125

            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;

1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
            case NR_UL_DCCH_MessageType__c1_PR_securityModeComplete:
                // to avoid segmentation fault
                if(!ue_context_p) {
                  LOG_I(NR_RRC, "Processing securityModeComplete UE %x, ue_context_p is NULL\n", ctxt_pP->rnti);
                  break;
                }

                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);

Xue Song's avatar
Xue Song committed
1144
               if ( LOG_DEBUGFLAG(DEBUG_ASN1) ) {
1145
                  xer_fprint(stdout, &asn_DEF_NR_UL_DCCH_Message, (void *)ul_dcch_msg);
Xue Song's avatar
Xue Song committed
1146
               }
1147

1148
                rrc_gNB_generate_UECapabilityEnquiry(ctxt_pP, ue_context_p);
yaojie's avatar
yaojie committed
1149
                //rrc_gNB_generate_defaultRRCReconfiguration(ctxt_pP, ue_context_p);
1150
                break;
Xue Song's avatar
Xue Song committed
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
            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;
Xue Song's avatar
Xue Song committed
1176 1177

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

Xue Song's avatar
Xue Song committed
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
                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);
Xue Song's avatar
Xue Song committed
1204
                if ( LOG_DEBUGFLAG(DEBUG_ASN1) ) {
Xue Song's avatar
Xue Song committed
1205
                    xer_fprint(stdout, &asn_DEF_NR_UL_DCCH_Message, (void *)ul_dcch_msg);
Xue Song's avatar
Xue Song committed
1206
                }
Xue Song's avatar
Xue Song committed
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
                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);
Xue Song's avatar
Xue Song committed
1226
                            if(LOG_DEBUGFLAG(DEBUG_ASN1)){
Xue Song's avatar
Xue Song committed
1227
                                xer_fprint(stdout,&asn_DEF_NR_UE_NR_Capability,ue_context_p->ue_context.UE_Capability_nr);
Xue Song's avatar
Xue Song committed
1228
                            }
Xue Song's avatar
Xue Song committed
1229 1230 1231 1232

                            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);
1233
                            ASN_STRUCT_FREE(asn_DEF_NR_UE_NR_Capability,ue_context_p->ue_context.UE_Capability_nr);
Xue Song's avatar
Xue Song committed
1234 1235 1236 1237 1238
                            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;
1239 1240 1241 1242 1243
                            if(eutra_index != -1){
                              LOG_E(NR_RRC,"fatal: more than 1 eutra capability\n");
                              exit(1);
                            }
							eutra_index = i;
Xue Song's avatar
Xue Song committed
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
                        }

                        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);
                        }
1262

Xue Song's avatar
Xue Song committed
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
                        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){
1275
                          //TODO
Xue Song's avatar
Xue Song committed
1276
                        }
1277 1278
                    }

Xue Song's avatar
Xue Song committed
1279 1280 1281
                    if(eutra_index == -1)
                    break;
                }
1282
            
Xue Song's avatar
Xue Song committed
1283
                rrc_gNB_generate_defaultRRCReconfiguration(ctxt_pP, ue_context_p);
Xue Song's avatar
Xue Song committed
1284 1285 1286 1287 1288 1289
                break;

            default:
                break;
        }
    }
1290
    return 0;
Xue Song's avatar
Xue Song committed
1291 1292
}

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1293 1294
///---------------------------------------------------------------------------------------------------------------///
///---------------------------------------------------------------------------------------------------------------///
1295
void *rrc_gnb_task(void *args_p) {
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1296 1297
  MessageDef                         *msg_p;
  const char                         *msg_name_p;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1298 1299
  instance_t                         instance;
  int                                result;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1300 1301
  //SRB_INFO                           *srb_info_p;
  //int                                CC_id;
1302
  protocol_ctxt_t                    ctxt;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1303 1304
  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
1305 1306

  while (1) {
1307
    // Wait for a message
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1308
    itti_receive_msg(TASK_RRC_GNB, &msg_p);
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1309 1310
    msg_name_p = ITTI_MSG_NAME(msg_p);
    instance = ITTI_MSG_INSTANCE(msg_p);
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1311
    LOG_I(NR_RRC,"Received message %s\n",msg_name_p);
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1312 1313

    switch (ITTI_MSG_ID(msg_p)) {
1314
      case TERMINATE_MESSAGE:
Xue Song's avatar
Xue Song committed
1315
        LOG_W(NR_RRC, " *** Exiting NR_RRC thread\n");
1316 1317
        itti_exit_task();
        break;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1318

1319
      case MESSAGE_TEST:
Xue Song's avatar
Xue Song committed
1320
        LOG_I(NR_RRC, "[gNB %d] Received %s\n", instance, msg_name_p);
1321
        break;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1322 1323

      /* Messages from MAC */
1324 1325 1326
      case NR_RRC_MAC_CCCH_DATA_IND:
      // PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt,
      //                               NR_RRC_MAC_CCCH_DATA_IND(msg_p).gnb_index,
Xue Song's avatar
Xue Song committed
1327
      //                               GNB_FLAG_YES,
1328 1329 1330
      //                               NR_RRC_MAC_CCCH_DATA_IND(msg_p).rnti,
      //                               msg_p->ittiMsgHeader.lte_time.frame,
      //                               msg_p->ittiMsgHeader.lte_time.slot);
Xue Song's avatar
Xue Song committed
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
        LOG_I(NR_RRC,"Decoding CCCH : inst %d, CC_id %d, ctxt %p, sib_info_p->Rx_buffer.payload_size %d\n",
                instance,
                NR_RRC_MAC_CCCH_DATA_IND(msg_p).CC_id,
                &ctxt,
                NR_RRC_MAC_CCCH_DATA_IND(msg_p).sdu_size);

        if (NR_RRC_MAC_CCCH_DATA_IND(msg_p).sdu_size >= CCCH_SDU_SIZE) {
            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;
1341 1342
      }

Xue Song's avatar
Xue Song committed
1343
      nr_rrc_gNB_decode_ccch(&ctxt,
1344 1345 1346 1347
                          (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).CC_id);
      break;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1348 1349

      /* Messages from PDCP */
Xue Song's avatar
Xue Song committed
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
      case NR_RRC_DCCH_DATA_IND:
        // PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt,
        //                               instance,
        //                               ENB_FLAG_YES,
        //                               RRC_DCCH_DATA_IND(msg_p).rnti,
        //                               msg_p->ittiMsgHeader.lte_time.frame,
        //                               msg_p->ittiMsgHeader.lte_time.slot);
        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);

        break;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1367

Xue Song's avatar
Xue Song committed
1368 1369 1370 1371
      case NGAP_DOWNLINK_NAS:
        rrc_gNB_process_NGAP_DOWNLINK_NAS(msg_p, msg_name_p, instance, &rrc_gNB_mui);
        break;

1372 1373 1374 1375
      case NGAP_PDUSESSION_SETUP_REQ:
        rrc_gNB_process_NGAP_PDUSESSION_SETUP_REQ(msg_p, msg_name_p, instance);
        break;

1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
      /*
      #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:
        LOG_I(NR_RRC, "[gNB %d] Received %s : %p\n", instance, msg_name_p,&NRRRC_CONFIGURATION_REQ(msg_p));
        openair_rrc_gNB_configuration(GNB_INSTANCE_TO_MODULE_ID(instance), &NRRRC_CONFIGURATION_REQ(msg_p));
        break;

1437
      /* Messages from X2AP */
1438
      case X2AP_ENDC_SGNB_ADDITION_REQ:
1439 1440 1441
        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;
1442

1443
      case X2AP_ENDC_SGNB_RECONF_COMPLETE:
1444 1445
        LOG_I(NR_RRC, "Handling of reconfiguration complete message at RRC gNB is pending \n");
        break;
Xue Song's avatar
Xue Song committed
1446

1447 1448 1449
      case NGAP_INITIAL_CONTEXT_SETUP_REQ:
        rrc_gNB_process_NGAP_INITIAL_CONTEXT_SETUP_REQ(msg_p, msg_name_p, instance);
        break;
1450

1451 1452 1453 1454 1455 1456 1457 1458
      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;

1459 1460 1461
      default:
        LOG_E(NR_RRC, "[gNB %d] Received unexpected message %s\n", instance, msg_name_p);
        break;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1462 1463 1464 1465 1466 1467 1468
    }

    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
1469

heshanyun's avatar
heshanyun committed
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486

//-----------------------------------------------------------------------------
void
rrc_gNB_generate_SecurityModeCommand(
  const protocol_ctxt_t *const ctxt_pP,
  rrc_gNB_ue_context_t          *const ue_context_pP
)
//-----------------------------------------------------------------------------
{
  uint8_t                             buffer[100];
  uint8_t                             size;
  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
1487
           rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id),
heshanyun's avatar
heshanyun committed
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
           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);
  LOG_D(NR_RRC,
        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);
  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);

  LOG_I(NR_RRC,"calling rrc_data_req :securityModeCommand\n");
1513 1514
#ifdef ITTI_SIM
			MessageDef *message_p;
Xue Song's avatar
Xue Song committed
1515 1516 1517
      uint8_t *message_buffer;
      message_buffer = itti_malloc (TASK_RRC_GNB_SIM, TASK_RRC_UE_SIM,size);
      memcpy (message_buffer, buffer, size);
1518 1519
			message_p = itti_alloc_new_message (TASK_RRC_GNB_SIM, GNB_RRC_DCCH_DATA_IND);
			GNB_RRC_DCCH_DATA_IND (message_p).rbid = DCCH;
Xue Song's avatar
Xue Song committed
1520
			GNB_RRC_DCCH_DATA_IND (message_p).sdu = message_buffer;
1521 1522 1523
			GNB_RRC_DCCH_DATA_IND (message_p).size	= size;
			itti_send_msg_to_task (TASK_RRC_UE_SIM, ctxt_pP->instance, message_p);
#else
1524
  nr_rrc_data_req(ctxt_pP,
heshanyun's avatar
heshanyun committed
1525 1526 1527 1528 1529 1530
               DCCH,
               rrc_gNB_mui++,
               SDU_CONFIRM_NO,
               size,
               buffer,
               PDCP_TRANSMISSION_MODE_CONTROL);
1531 1532
#endif

heshanyun's avatar
heshanyun committed
1533 1534
}

zhanghuiqin's avatar
zhanghuiqin committed
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
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;
  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
1546
  size = do_NR_SA_UECapabilityEnquiry(
zhanghuiqin's avatar
zhanghuiqin committed
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
           ctxt_pP,
           buffer,
           rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id));
  LOG_I(RRC,
        PROTOCOL_RRC_CTXT_UE_FMT" Logical Channel DL-DCCH, Generate NR UECapabilityEnquiry (bytes %d)\n",
        PROTOCOL_RRC_CTXT_UE_ARGS(ctxt_pP),
        size);
  LOG_D(RRC,
        PROTOCOL_RRC_CTXT_UE_FMT" --- PDCP_DATA_REQ/%d Bytes (NR UECapabilityEnquiry MUI %d) --->[PDCP][RB %02d]\n",
        PROTOCOL_RRC_CTXT_UE_ARGS(ctxt_pP),
        size,
        rrc_gNB_mui,
        DCCH);
  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);
1570 1571
#ifdef ITTI_SIM
			  MessageDef *message_p;
Xue Song's avatar
Xue Song committed
1572 1573 1574
        uint8_t *message_buffer;
        message_buffer = itti_malloc (TASK_RRC_GNB_SIM, TASK_RRC_UE_SIM, size);
        memcpy (message_buffer, buffer, size);
1575 1576
			  message_p = itti_alloc_new_message (TASK_RRC_GNB_SIM, GNB_RRC_DCCH_DATA_IND);
			  GNB_RRC_DCCH_DATA_IND (message_p).rbid = DCCH;
Xue Song's avatar
Xue Song committed
1577
			  GNB_RRC_DCCH_DATA_IND (message_p).sdu = message_buffer;
1578 1579 1580
			  GNB_RRC_DCCH_DATA_IND (message_p).size  = size;
			  itti_send_msg_to_task (TASK_RRC_UE_SIM, ctxt_pP->instance, message_p);
#else
1581
  nr_rrc_data_req(
zhanghuiqin's avatar
zhanghuiqin committed
1582 1583 1584 1585 1586 1587 1588
    ctxt_pP,
    DCCH,
    rrc_gNB_mui++,
    SDU_CONFIRM_NO,
    size,
    buffer,
    PDCP_TRANSMISSION_MODE_CONTROL);
1589
#endif
zhanghuiqin's avatar
zhanghuiqin committed
1590
}
heshanyun's avatar
heshanyun committed
1591 1592 1593 1594 1595 1596 1597

//-----------------------------------------------------------------------------
/*
* Generate the RRC Connection Release to UE.
* If received, UE should switch to RRC_IDLE mode.
*/
void
Xue Song's avatar
Xue Song committed
1598
rrc_gNB_generate_RRCRelease(
heshanyun's avatar
heshanyun committed
1599
  const protocol_ctxt_t *const ctxt_pP,
Xue Song's avatar
Xue Song committed
1600
  rrc_gNB_ue_context_t  *const ue_context_pP
heshanyun's avatar
heshanyun committed
1601 1602 1603 1604 1605 1606 1607 1608
)
//-----------------------------------------------------------------------------
{
  uint8_t buffer[RRC_BUF_SIZE];
  uint16_t size = 0;

  memset(buffer, 0, RRC_BUF_SIZE);

Xue Song's avatar
Xue Song committed
1609
  size = do_NR_RRCRelease(buffer,rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id));
heshanyun's avatar
heshanyun committed
1610 1611 1612
  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
1613 1614
        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
1615 1616
        size);
  LOG_D(NR_RRC,
Xue Song's avatar
Xue Song committed
1617 1618
        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
1619 1620 1621 1622 1623 1624 1625 1626
        size,
        rrc_gNB_mui,
        DCCH);
  MSC_LOG_TX_MESSAGE(
    MSC_RRC_GNB,
    MSC_RRC_UE,
    buffer,
    size,
Xue Song's avatar
Xue Song committed
1627
    MSC_AS_TIME_FMT" NR_RRCRelease UE %x MUI %d size %u",
heshanyun's avatar
heshanyun committed
1628 1629 1630 1631 1632
    MSC_AS_TIME_ARGS(ctxt_pP),
    ue_context_pP->ue_context.rnti,
    rrc_gNB_mui,
    size);

1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
#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, 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
heshanyun's avatar
heshanyun committed
1644 1645 1646 1647 1648 1649 1650 1651 1652
  if (NODE_IS_CU(RC.rrc[ctxt_pP->module_id]->node_type)) {
    MessageDef *m = itti_alloc_new_message(TASK_RRC_ENB, F1AP_UE_CONTEXT_RELEASE_CMD);
    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 {
1653
    nr_rrc_data_req(ctxt_pP,
heshanyun's avatar
heshanyun committed
1654 1655 1656 1657 1658 1659 1660
                 DCCH,
                 rrc_gNB_mui++,
                 SDU_CONFIRM_NO,
                 size,
                 buffer,
                 PDCP_TRANSMISSION_MODE_CONTROL);
  }
1661
#endif
heshanyun's avatar
heshanyun committed
1662
}