rrc_gNB.c 192 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

Laurent THOMAS's avatar
Laurent THOMAS committed
33 34 35 36
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

37
#include "nr_rrc_config.h"
38 39
#include "nr_rrc_defs.h"
#include "nr_rrc_extern.h"
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
40 41 42
#include "assertions.h"
#include "common/ran_context.h"
#include "asn1_conversions.h"
43
#include "rrc_gNB_radio_bearers.h"
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
44 45 46

#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
#include "LAYER2/RLC/rlc.h"
47
#include "LAYER2/NR_MAC_gNB/mac_proto.h"
Raymond Knopp's avatar
Raymond Knopp committed
48
#include "common/utils/LOG/log.h"
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
49
#include "COMMON/mac_rrc_primitives.h"
50 51 52 53 54
#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"
55 56 57 58 59 60
#include "NR_DL-CCCH-Message.h"
#include "NR_UL-CCCH-Message.h"
#include "NR_RRCReject.h"
#include "NR_RejectWaitTime.h"
#include "NR_RRCSetup.h"

61
#include "NR_CellGroupConfig.h"
62
#include "NR_MeasResults.h"
63 64
#include "LTE_UECapabilityInformation.h"
#include "LTE_UL-DCCH-Message.h"
65 66
#include "NR_UL-CCCH-Message.h"
#include "NR_RRCSetupRequest-IEs.h"
Xue Song's avatar
Xue Song committed
67
#include "NR_RRCSetupComplete-IEs.h"
68
#include "NR_RRCReestablishmentRequest-IEs.h"
69
#include "NR_MIB.h"
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
70 71 72 73

#include "rlc.h"
#include "rrc_eNB_UE_context.h"
#include "platform_types.h"
Raymond Knopp's avatar
Raymond Knopp committed
74
#include "common/utils/LOG/vcd_signal_dumper.h"
75

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
76 77 78 79 80 81 82
#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
83
#include "UTIL/OSA/osa_defs.h"
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
84

85
#include "rrc_eNB_S1AP.h"
Xue Song's avatar
Xue Song committed
86
#include "rrc_gNB_NGAP.h"
Xue Song's avatar
Xue Song committed
87

88 89
#include "rrc_eNB_GTPV1U.h"

90
#include "nr_pdcp/nr_pdcp_entity.h"
91
#include "pdcp.h"
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
92 93
#include "gtpv1u_eNB_task.h"

94

95
#include "intertask_interface.h"
96
#include "SIMULATION/TOOLS/sim.h" // for taus
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
97

98
#include "executables/softmodem-common.h"
laurent's avatar
laurent committed
99
#include <openair2/RRC/NR/rrc_gNB_UE_context.h>
Laurent's avatar
Laurent committed
100
#include <openair2/X2AP/x2ap_eNB.h>
101
#include <openair3/ocp-gtpu/gtp_itf.h>
102
#include <openair2/RRC/NR/nr_rrc_proto.h>
103
#include "LAYER2/nr_rlc/nr_rlc_oai_api.h"
104

Xue Song's avatar
Xue Song committed
105 106
#include "BIT_STRING.h"
#include "assertions.h"
107

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
108 109 110 111
//#define XER_PRINT

extern RAN_CONTEXT_t RC;

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

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
114
mui_t                               rrc_gNB_mui = 0;
115
uint8_t first_rrcreconfiguration = 0;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
116

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
117 118
///---------------------------------------------------------------------------------------------------------------///
///---------------------------------------------------------------------------------------------------------------///
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
119

120 121 122 123 124 125 126 127 128 129
bool DURecvCb(protocol_ctxt_t  *ctxt_pP,
              const srb_flag_t     srb_flagP,
              const rb_id_t        rb_idP,
              const mui_t          muiP,
              const confirm_t      confirmP,
              const sdu_size_t     sdu_buffer_sizeP,
              unsigned char *const sdu_buffer_pP,
              const pdcp_transmission_mode_t modeP,
              const uint32_t *sourceL2Id,
              const uint32_t *destinationL2Id) {
130 131 132 133 134 135 136
  // The buffer comes from the stack in gtp-u thread, we have a make a separate buffer to enqueue in a inter-thread message queue
  mem_block_t *sdu=get_free_mem_block(sdu_buffer_sizeP, __func__);
  memcpy(sdu->data,  sdu_buffer_pP,  sdu_buffer_sizeP);
  du_rlc_data_req(ctxt_pP,srb_flagP, false,  rb_idP,muiP, confirmP,  sdu_buffer_sizeP, sdu);
  return true;
}

137
void openair_nr_rrc_on(const protocol_ctxt_t *const ctxt_pP) {
138
  LOG_I(NR_RRC, PROTOCOL_NR_RRC_CTXT_FMT" gNB:OPENAIR NR RRC IN....\n",PROTOCOL_NR_RRC_CTXT_ARGS(ctxt_pP));
139 140 141 142
  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
143 144
}

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
145 146 147
///---------------------------------------------------------------------------------------------------------------///
///---------------------------------------------------------------------------------------------------------------///

148
static void init_NR_SI(gNB_RRC_INST *rrc, gNB_RrcConfigurationReq *configuration) {
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
149
  LOG_D(RRC,"%s()\n\n\n\n",__FUNCTION__);
150 151 152 153
  if (NODE_IS_DU(rrc->node_type) || NODE_IS_MONOLITHIC(rrc->node_type)) {
    rrc->carrier.MIB             = (uint8_t *) malloc16(4);
    rrc->carrier.sizeof_MIB      = do_MIB_NR(rrc,0);
  }
154

155
    if((get_softmodem_params()->sa) && ( (NODE_IS_DU(rrc->node_type) || NODE_IS_MONOLITHIC(rrc->node_type)))) {
156 157 158
    rrc->carrier.sizeof_SIB1 = do_SIB1_NR(&rrc->carrier,configuration);
  }

159 160 161 162 163 164 165
  if (!NODE_IS_DU(rrc->node_type)) {
    rrc->carrier.SIB23 = (uint8_t *) malloc16(100);
    AssertFatal(rrc->carrier.SIB23 != NULL, "cannot allocate memory for SIB");
    rrc->carrier.sizeof_SIB23 = do_SIB23_NR(&rrc->carrier, configuration);
    LOG_I(NR_RRC,"do_SIB23_NR, size %d \n ", rrc->carrier.sizeof_SIB23);
    AssertFatal(rrc->carrier.sizeof_SIB23 != 255,"FATAL, RC.nrrrc[mod].carrier[CC_id].sizeof_SIB23 == 255");
  }
166

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

169
  if (NODE_IS_MONOLITHIC(rrc->node_type) || NODE_IS_DU(rrc->node_type)){
Xue Song's avatar
Xue Song committed
170
    rrc_mac_config_req_gNB(rrc->module_id,
171 172 173 174 175 176
                           rrc->configuration.pdsch_AntennaPorts,
                           rrc->configuration.pusch_AntennaPorts,
                           rrc->configuration.sib1_tda,
                           rrc->configuration.minRXTXTIME,
                           rrc->carrier.servingcellconfigcommon,
                           &rrc->carrier.mib,
177
                           rrc->carrier.siblock1,
178 179 180
                           0,
                           0, // WIP hardcoded rnti
                           NULL);
Xue Song's avatar
Xue Song committed
181
  }
Raymond Knopp's avatar
Raymond Knopp committed
182

183 184 185 186 187 188
  /* set flag to indicate that cell information is configured. This is required
   * in DU to trigger F1AP_SETUP procedure */
  pthread_mutex_lock(&rrc->cell_info_mutex);
  rrc->cell_info_configured=1;
  pthread_mutex_unlock(&rrc->cell_info_mutex);

189
  if (get_softmodem_params()->phy_test > 0 || get_softmodem_params()->do_ra > 0) {
190 191 192 193 194 195
    struct rrc_gNB_ue_context_s *ue_context_p = rrc_gNB_allocate_new_UE_context(rrc);
    ue_context_p->ue_context.spCellConfig = calloc(1, sizeof(struct NR_SpCellConfig));
    ue_context_p->ue_context.spCellConfig->spCellConfigDedicated = configuration->scd;
    LOG_I(NR_RRC,"Adding new user (%p)\n",ue_context_p);
    if (!NODE_IS_CU(RC.nrrrc[0]->node_type)) {
      rrc_add_nsa_user(rrc,ue_context_p,NULL);
196
    }
197
  }
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
198
}
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
199

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
static void rrc_gNB_mac_rrc_init(gNB_RRC_INST *rrc)
{
  switch (rrc->node_type) {
    case ngran_gNB_CU:
      mac_rrc_dl_f1ap_init(&rrc->mac_rrc);
      break;
    case ngran_gNB_DU:
      /* silently drop this, as we currently still need the RRC at the DU. As
       * soon as this is not the case anymore, we can add the AssertFatal() */
      //AssertFatal(1==0,"nothing to do for DU\n");
      break;
    case ngran_gNB:
      mac_rrc_dl_direct_init(&rrc->mac_rrc);
      break;
    default:
      AssertFatal(0 == 1, "Unknown node type %d\n", rrc->node_type);
      break;
  }
}

220
char openair_rrc_gNB_configuration(const module_id_t gnb_mod_idP, gNB_RrcConfigurationReq *configuration) {
221
  protocol_ctxt_t      ctxt = { 0 };
222
  gNB_RRC_INST         *rrc=RC.nrrrc[gnb_mod_idP];
223
  PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, gnb_mod_idP, GNB_FLAG_YES, NOT_A_RNTI, 0, 0,gnb_mod_idP);
224 225 226
  LOG_I(NR_RRC,
        PROTOCOL_NR_RRC_CTXT_FMT" Init...\n",
        PROTOCOL_NR_RRC_CTXT_ARGS(&ctxt));
227 228 229 230 231 232
  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;
233
  rrc_gNB_mac_rrc_init(rrc);
234
  uid_linear_allocator_init(&rrc->uid_allocator);
235 236 237
  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);
238 239
  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);
240
  rrc->configuration = *configuration;
241
  rrc->carrier.servingcellconfigcommon = configuration->scc;
242
  rrc->carrier.servingcellconfig = configuration->scd;
243
  nr_rrc_config_ul_tda(configuration->scc,configuration->minRXTXTIME);
244
   /// System Information INIT
245 246
  pthread_mutex_init(&rrc->cell_info_mutex,NULL);
  rrc->cell_info_configured = 0;
247
  LOG_I(NR_RRC, PROTOCOL_NR_RRC_CTXT_FMT" Checking release \n",PROTOCOL_NR_RRC_CTXT_ARGS(&ctxt));
248
  init_NR_SI(rrc, configuration);
249
  rrc_init_nr_global_param();
250
  openair_nr_rrc_on(&ctxt);
251
  return 0;
252
}//END openair_rrc_gNB_configuration
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
253 254


255
void rrc_gNB_process_AdditionRequestInformation(const module_id_t gnb_mod_idP, x2ap_ENDC_sgnb_addition_req_t *m) {
256 257 258 259 260 261 262
  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];
263

264 265 266 267 268 269
  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;
  }
270

271 272 273 274 275
  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);
276 277
  LOG_A(NR_RRC, "Successfully parsed CG_ConfigInfo of size %zu bits. (%zu bytes)\n",
        dec_rval.consumed, (dec_rval.consumed +7/8));
278 279
}

Cedric Roux's avatar
Cedric Roux committed
280

281
//-----------------------------------------------------------------------------
Xue Song's avatar
Xue Song committed
282 283 284
uint8_t
rrc_gNB_get_next_transaction_identifier(
    module_id_t gnb_mod_idP
285 286 287
)
//-----------------------------------------------------------------------------
{
288 289 290 291
  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];
292 293
}

294 295 296 297
void apply_macrlc_config(gNB_RRC_INST *rrc,
                         rrc_gNB_ue_context_t         *const ue_context_pP,
                         const protocol_ctxt_t        *const ctxt_pP ) {

298 299 300 301 302 303 304 305
  NR_CellGroupConfig_t *cgc = get_softmodem_params()->sa ? ue_context_pP->ue_context.masterCellGroup : NULL;
  rrc_mac_config_req_gNB(rrc->module_id,
                         rrc->configuration.pdsch_AntennaPorts,
                         rrc->configuration.pusch_AntennaPorts,
                         rrc->configuration.sib1_tda,
                         rrc->configuration.minRXTXTIME,
                         NULL,
                         NULL,
306
                         NULL,
307 308 309
                         0,
                         ue_context_pP->ue_context.rnti,
                         cgc);
310

311 312 313 314 315 316
  nr_rrc_rlc_config_asn1_req(ctxt_pP,
                             ue_context_pP->ue_context.SRB_configList,
                             ue_context_pP->ue_context.DRB_configList,
                             NULL,
                             NULL,
                             get_softmodem_params()->sa ? cgc->rlc_BearerToAddModList : NULL);
317

318 319 320 321 322 323 324 325 326
}

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

      nr_rrc_pdcp_config_asn1_req(ctxt_pP,
                                  ue_context_pP->ue_context.SRB_configList,
                                  NULL,
                                  NULL,
Cedric Roux's avatar
Cedric Roux committed
327
                                  0,
328 329 330 331 332 333
                                  NULL,
                                  NULL,
                                  NULL,
                                  NULL,
                                  NULL,
                                  NULL,
rmagueta's avatar
rmagueta committed
334
                                  get_softmodem_params()->sa ? ue_context_pP->ue_context.masterCellGroup->rlc_BearerToAddModList : NULL);
335 336 337

}

338 339 340
//-----------------------------------------------------------------------------
void
rrc_gNB_generate_RRCSetup(
341 342
    const protocol_ctxt_t        *const ctxt_pP,
    rrc_gNB_ue_context_t         *const ue_context_pP,
343 344 345
    const uint8_t                *masterCellGroup,
    int                           masterCellGroup_len,
    NR_ServingCellConfigCommon_t *scc)
346 347
//-----------------------------------------------------------------------------
{
348
  LOG_I(NR_RRC, "rrc_gNB_generate_RRCSetup for RNTI %04x\n", ctxt_pP->rnti);
349 350 351 352 353 354

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

356
  gNB_RRC_UE_t *ue_p = &ue_context_pP->ue_context;
Eurecom's avatar
Eurecom committed
357
  gNB_RRC_INST *rrc = RC.nrrrc[ctxt_pP->module_id];
358 359 360 361 362 363 364 365 366
  const NR_ServingCellConfig_t *sccd = rrc->configuration.scd;
  ue_p->Srb0.Tx_buffer.payload_size = do_RRCSetup(ue_context_pP,
                                                  (uint8_t *) ue_p->Srb0.Tx_buffer.Payload,
                                                  rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id),
                                                  masterCellGroup,
                                                  masterCellGroup_len,
                                                  scc,
                                                  sccd,
                                                  &rrc->configuration);
367 368 369 370 371

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

373 374 375 376
  // 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;
377

378 379
  /* TODO: this should go through the E1 interface */
  apply_pdcp_config(ue_context_pP,ctxt_pP);
380

381 382 383 384 385 386 387 388
  f1ap_dl_rrc_message_t dl_rrc = {
    .old_gNB_DU_ue_id = 0xFFFFFF,
    .rrc_container = (uint8_t *)ue_p->Srb0.Tx_buffer.Payload,
    .rrc_container_length = ue_p->Srb0.Tx_buffer.payload_size,
    .rnti = ue_p->rnti,
    .srb_id = CCCH
  };
  rrc->mac_rrc.dl_rrc_message_transfer(ctxt_pP->module_id, &dl_rrc);
389 390
}

391 392 393 394 395 396 397 398 399 400 401
//-----------------------------------------------------------------------------
void
rrc_gNB_generate_RRCSetup_for_RRCReestablishmentRequest(
    const protocol_ctxt_t    *const ctxt_pP,
    const int                CC_id
)
//-----------------------------------------------------------------------------
{
  LOG_I(NR_RRC, "generate RRCSetup for RRCReestablishmentRequest \n");
  rrc_gNB_ue_context_t         *ue_context_pP   = NULL;
  gNB_RRC_INST                 *rrc_instance_p = RC.nrrrc[ctxt_pP->module_id];
402 403
  const NR_ServingCellConfigCommon_t *scc=rrc_instance_p->carrier.servingcellconfigcommon;
  const NR_ServingCellConfig_t       *sccd = rrc_instance_p->configuration.scd;
404 405 406 407

  ue_context_pP = rrc_gNB_get_next_free_ue_context(ctxt_pP, rrc_instance_p, 0);

  gNB_RRC_UE_t *ue_p = &ue_context_pP->ue_context;
408 409 410 411
  int16_t ret = do_RRCSetup(ue_context_pP,
                            (uint8_t *) ue_p->Srb0.Tx_buffer.Payload,
                            rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id),
                            NULL,
412 413 414 415
                            0,
                            scc,
                            sccd,
                            &rrc_instance_p->configuration);
416 417 418 419

  AssertFatal(ret>0,"Error generating RRCSetup for RRCReestablishmentRequest\n");

  ue_p->Srb0.Tx_buffer.payload_size = ret;
420 421 422 423 424 425 426 427 428 429 430

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

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

  rrc_mac_config_req_gNB(rrc_instance_p->module_id,
431 432 433 434 435
                         rrc_instance_p->configuration.pdsch_AntennaPorts,
                         rrc_instance_p->configuration.pusch_AntennaPorts,
                         rrc_instance_p->configuration.sib1_tda,
                         rrc_instance_p->configuration.minRXTXTIME,
                         rrc_instance_p->carrier.servingcellconfigcommon,
436
                         &rrc_instance_p->carrier.mib,
437
                         rrc_instance_p->carrier.siblock1,
438 439
                         0,
                         ue_context_pP->ue_context.rnti,
440
                         NULL);
441

442
  LOG_I(NR_RRC,
443 444 445
        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);
446 447 448 449 450 451
  // 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;
452 453
}

454 455
void
rrc_gNB_generate_RRCReject(
Xue Song's avatar
Xue Song committed
456
    const protocol_ctxt_t    *const ctxt_pP,
457
    rrc_gNB_ue_context_t     *const ue_context_pP)
458 459
//-----------------------------------------------------------------------------
{
460 461
  LOG_I(NR_RRC, "rrc_gNB_generate_RRCReject \n");
  gNB_RRC_UE_t *ue_p = &ue_context_pP->ue_context;
462
  MessageDef   *message_p;
463

464 465 466 467 468 469 470 471 472 473
  ue_p->Srb0.Tx_buffer.payload_size = do_RRCReject(ctxt_pP->module_id,
                                                  (uint8_t *)ue_p->Srb0.Tx_buffer.Payload);
  LOG_DUMPMSG(NR_RRC, DEBUG_RRC,
              (char *)(ue_p->Srb0.Tx_buffer.Payload),
              ue_p->Srb0.Tx_buffer.payload_size,
              "[MSG] RRCReject \n");
  LOG_I(NR_RRC,
      PROTOCOL_NR_RRC_CTXT_UE_FMT" [RAPROC] Logical Channel DL-CCCH, Generating NR_RRCReject (bytes %d)\n",
      PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
      ue_p->Srb0.Tx_buffer.payload_size);
Xue Song's avatar
Xue Song committed
474

475 476 477
  switch (RC.nrrrc[ctxt_pP->module_id]->node_type) {
    case ngran_gNB_CU:
      // create an ITTI message
478
      message_p = itti_alloc_new_message (TASK_RRC_GNB, 0, F1AP_DL_RRC_MESSAGE);
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
      F1AP_DL_RRC_MESSAGE (message_p).rrc_container        = (uint8_t *)ue_p->Srb0.Tx_buffer.Payload;
      F1AP_DL_RRC_MESSAGE (message_p).rrc_container_length = ue_p->Srb0.Tx_buffer.payload_size;
      F1AP_DL_RRC_MESSAGE (message_p).gNB_CU_ue_id         = 0;
      F1AP_DL_RRC_MESSAGE (message_p).gNB_DU_ue_id         = 0;
      F1AP_DL_RRC_MESSAGE (message_p).old_gNB_DU_ue_id     = 0xFFFFFFFF; // unknown
      F1AP_DL_RRC_MESSAGE (message_p).rnti                 = ue_p->rnti;
      F1AP_DL_RRC_MESSAGE (message_p).srb_id               = CCCH;
      F1AP_DL_RRC_MESSAGE (message_p).execute_duplication  = 1;
      F1AP_DL_RRC_MESSAGE (message_p).RAT_frequency_priority_information.en_dc = 0;
      itti_send_msg_to_task (TASK_CU_F1, ctxt_pP->module_id, message_p);
      LOG_D(NR_RRC, "Send F1AP_DL_RRC_MESSAGE with ITTI\n");
      break;

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

    case ngran_gNB:
498
    {
Robert Schmidt's avatar
Robert Schmidt committed
499 500
      // NOTE: there was ITTI_SIM which has been removed. This case is maybe
      // not necessary anymore
501
      // rrc_mac_config_req_gNB
502
    }
503 504 505 506 507
      break;

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

Xue Song's avatar
Xue Song committed
510 511 512 513 514 515 516 517 518 519 520 521
//-----------------------------------------------------------------------------
/*
* 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
)
//-----------------------------------------------------------------------------
{
522
  LOG_A(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" [RAPROC] Logical Channel UL-DCCH, " "processing NR_RRCSetupComplete from UE (SRB1 Active)\n",
523 524
      PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
  ue_context_pP->ue_context.Srb1.Active = 1;
525
  ue_context_pP->ue_context.Srb1.Srb_info.Srb_id = 1;
526
  ue_context_pP->ue_context.StatusRrc = NR_RRC_CONNECTED;
Xue Song's avatar
Xue Song committed
527

528
  if (get_softmodem_params()->sa) {
Xue Song's avatar
Xue Song committed
529
    rrc_gNB_send_NGAP_NAS_FIRST_REQ(ctxt_pP, ue_context_pP, rrcSetupComplete);
530 531 532
  } else {
    rrc_gNB_generate_SecurityModeCommand(ctxt_pP, ue_context_pP);
  }
Xue Song's avatar
Xue Song committed
533 534
}

Xue Song's avatar
Xue Song committed
535 536 537
//-----------------------------------------------------------------------------
void 
rrc_gNB_generate_defaultRRCReconfiguration(
538 539
  const protocol_ctxt_t     *const ctxt_pP,
  rrc_gNB_ue_context_t      *ue_context_pP
Xue Song's avatar
Xue Song committed
540 541 542
)
//-----------------------------------------------------------------------------
{
543
  uint8_t                       buffer[RRC_BUF_SIZE];
544
  int16_t                       size;
545
  /*NR_SRB_ToAddModList_t        **SRB_configList2 = NULL;
546
  NR_SRB_ToAddModList_t        *SRB_configList  = ue_context_pP->ue_context.SRB_configList;
547 548
  NR_DRB_ToAddModList_t        **DRB_configList  = NULL;
  NR_DRB_ToAddModList_t        **DRB_configList2 = NULL;
549 550
  NR_SRB_ToAddMod_t            *SRB2_config     = NULL;
  NR_DRB_ToAddMod_t            *DRB_config      = NULL;
551
  NR_SDAP_Config_t             *sdap_config     = NULL;*/
552 553 554 555 556 557 558 559
  struct NR_RRCReconfiguration_v1530_IEs__dedicatedNAS_MessageList
                               *dedicatedNAS_MessageList = NULL;
  NR_DedicatedNAS_Message_t    *dedicatedNAS_Message = NULL;

  uint8_t xid = rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id);

  /******************** Radio Bearer Config ********************/
  /* Configure SRB2 */
560
  /*SRB_configList2 = &ue_context_pP->ue_context.SRB_configList2[xid];
561 562
  if (*SRB_configList2) {
    free(*SRB_configList2);
563
  }
564 565
  *SRB_configList2 = CALLOC(1, sizeof(**SRB_configList2));
  memset(*SRB_configList2, 0, sizeof(**SRB_configList2));
566 567
  SRB2_config = CALLOC(1, sizeof(*SRB2_config));
  SRB2_config->srb_Identity = 2;
568
  ASN_SEQUENCE_ADD(&(*SRB_configList2)->list, SRB2_config);
569
  ASN_SEQUENCE_ADD(&SRB_configList->list, SRB2_config);*/
570 571

  /* Configure DRB */
572
  /*DRB_configList = &ue_context_pP->ue_context.DRB_configList;
573 574
  if (*DRB_configList) {
      free(*DRB_configList);
575
  }
576 577
  *DRB_configList = CALLOC(1, sizeof(**DRB_configList));
  memset(*DRB_configList, 0, sizeof(**DRB_configList));
578

579 580 581
  DRB_configList2 = &ue_context_pP->ue_context.DRB_configList2[xid];
  if (*DRB_configList2) {
      free(*DRB_configList2);
582
  }
583 584
  *DRB_configList2 = CALLOC(1, sizeof(**DRB_configList2));
  memset(*DRB_configList2, 0, sizeof(**DRB_configList2));
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599

  DRB_config = CALLOC(1, sizeof(*DRB_config));
  DRB_config->drb_Identity = 1;
  DRB_config->cnAssociation = CALLOC(1, sizeof(*DRB_config->cnAssociation));
  DRB_config->cnAssociation->present = NR_DRB_ToAddMod__cnAssociation_PR_sdap_Config;
  // TODO sdap_Config
  sdap_config = CALLOC(1, sizeof(NR_SDAP_Config_t));
  memset(sdap_config, 0, sizeof(NR_SDAP_Config_t));
  DRB_config->cnAssociation->choice.sdap_Config = sdap_config;
  // TODO pdcp_Config
  DRB_config->reestablishPDCP = NULL;
  DRB_config->recoverPDCP = NULL;
  DRB_config->pdcp_Config = calloc(1, sizeof(*DRB_config->pdcp_Config));
  DRB_config->pdcp_Config->drb = calloc(1,sizeof(*DRB_config->pdcp_Config->drb));
  DRB_config->pdcp_Config->drb->discardTimer = calloc(1, sizeof(*DRB_config->pdcp_Config->drb->discardTimer));
600
  *DRB_config->pdcp_Config->drb->discardTimer = NR_PDCP_Config__drb__discardTimer_infinity;
601 602 603 604 605 606 607 608 609 610 611 612 613
  DRB_config->pdcp_Config->drb->pdcp_SN_SizeUL = calloc(1, sizeof(*DRB_config->pdcp_Config->drb->pdcp_SN_SizeUL));
  *DRB_config->pdcp_Config->drb->pdcp_SN_SizeUL = NR_PDCP_Config__drb__pdcp_SN_SizeUL_len18bits;
  DRB_config->pdcp_Config->drb->pdcp_SN_SizeDL = calloc(1, sizeof(*DRB_config->pdcp_Config->drb->pdcp_SN_SizeDL));
  *DRB_config->pdcp_Config->drb->pdcp_SN_SizeDL = NR_PDCP_Config__drb__pdcp_SN_SizeDL_len18bits;
  DRB_config->pdcp_Config->drb->headerCompression.present = NR_PDCP_Config__drb__headerCompression_PR_notUsed;
  DRB_config->pdcp_Config->drb->headerCompression.choice.notUsed = 0;

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

  DRB_config->pdcp_Config->t_Reordering = calloc(1, sizeof(*DRB_config->pdcp_Config->t_Reordering));
614
  *DRB_config->pdcp_Config->t_Reordering = NR_PDCP_Config__t_Reordering_ms100;
615 616
  DRB_config->pdcp_Config->ext1 = NULL;

617
  ASN_SEQUENCE_ADD(&(*DRB_configList)->list, DRB_config);
618
  ASN_SEQUENCE_ADD(&(*DRB_configList2)->list, DRB_config);*/
619

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

622 623
  /* Add all NAS PDUs to the list */
  for (int i = 0; i < ue_context_pP->ue_context.nb_of_pdusessions; i++) {
624
    if (ue_context_pP->ue_context.pduSession[i].param.nas_pdu.buffer != NULL) {
625 626 627
      dedicatedNAS_Message = CALLOC(1, sizeof(NR_DedicatedNAS_Message_t));
      memset(dedicatedNAS_Message, 0, sizeof(OCTET_STRING_t));
      OCTET_STRING_fromBuf(dedicatedNAS_Message,
628 629
                            (char *)ue_context_pP->ue_context.pduSession[i].param.nas_pdu.buffer,
                            ue_context_pP->ue_context.pduSession[i].param.nas_pdu.length);
630 631 632
      ASN_SEQUENCE_ADD(&dedicatedNAS_MessageList->list, dedicatedNAS_Message);
    }

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

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
  if (ue_context_pP->ue_context.nas_pdu_flag == 1) {
    dedicatedNAS_Message = CALLOC(1, sizeof(NR_DedicatedNAS_Message_t));
    memset(dedicatedNAS_Message, 0, sizeof(OCTET_STRING_t));
    OCTET_STRING_fromBuf(dedicatedNAS_Message,
                          (char *)ue_context_pP->ue_context.nas_pdu.buffer,
                          ue_context_pP->ue_context.nas_pdu.length);
    ASN_SEQUENCE_ADD(&dedicatedNAS_MessageList->list, dedicatedNAS_Message);
  }

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

653
  gNB_RRC_INST *rrc = RC.nrrrc[ctxt_pP->module_id];
654 655
  NR_MeasConfig_t *measconfig = get_defaultMeasConfig(&rrc->configuration);

656
  gNB_RRC_UE_t *ue_p = &ue_context_pP->ue_context;
657 658
  memset(buffer, 0, sizeof(buffer));
  size = do_RRCReconfiguration(ctxt_pP, buffer, sizeof(buffer),
659
                                xid,
660 661
                                NULL, //*SRB_configList2,
                                NULL, //*DRB_configList,
662 663 664
                                NULL,
                                NULL,
                                NULL,
665
                                measconfig,
666
                                dedicatedNAS_MessageList,
667 668
                                ue_context_pP,
                                &rrc->carrier,
669
                                &rrc->configuration,
670
                                NULL,
671
                                ue_p->masterCellGroup);
672 673 674 675
  AssertFatal(size > 0, "cannot encode RRCReconfiguration in %s()\n", __func__);
  LOG_W(RRC, "do_RRCReconfiguration(): size %d\n", size);

  xer_fprint(stdout,&asn_DEF_NR_CellGroupConfig, ue_p->masterCellGroup);
676 677 678

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

679
  LOG_DUMPMSG(NR_RRC, DEBUG_RRC,(char *)buffer, size, "[MSG] RRC Reconfiguration\n");
680 681 682

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

689 690 691 692 693
  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);
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
  switch (RC.nrrrc[ctxt_pP->module_id]->node_type) {
    case ngran_gNB_CU:
      nr_rrc_data_req(ctxt_pP,
                  DCCH,
                  rrc_gNB_mui++,
                  SDU_CONFIRM_NO,
                  size,
                  buffer,
                  PDCP_TRANSMISSION_MODE_CONTROL);
      // rrc_pdcp_config_asn1_req

      break;

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

    case ngran_gNB:
713
    {
714
      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",
715 716 717 718 719 720 721
          ctxt_pP->frame,
          ctxt_pP->module_id,
          size,
          ue_context_pP->ue_context.rnti,
          rrc_gNB_mui,
          ctxt_pP->module_id,
          DCCH);
722
      nr_rrc_data_req(ctxt_pP,
723 724 725 726 727 728
          DCCH,
          rrc_gNB_mui++,
          SDU_CONFIRM_NO,
          size,
          buffer,
          PDCP_TRANSMISSION_MODE_CONTROL);
729 730
      // rrc_pdcp_config_asn1_req
      // rrc_rlc_config_asn1_req
731
    }
732
    break;
733

734 735
  default :
    LOG_W(NR_RRC, "Unknown node type %d\n", RC.nrrrc[ctxt_pP->module_id]->node_type);
736
  }
luis_pereira87's avatar
luis_pereira87 committed
737 738

  if (NODE_IS_DU(rrc->node_type) || NODE_IS_MONOLITHIC(rrc->node_type)) {
Cedric Roux's avatar
Cedric Roux committed
739
    gNB_RRC_UE_t *ue_p = &ue_context_pP->ue_context;
740 741 742 743 744 745 746 747 748 749 750 751
    rrc_mac_config_req_gNB(rrc->module_id,
                           rrc->configuration.pdsch_AntennaPorts,
                           rrc->configuration.pusch_AntennaPorts,
                           rrc->configuration.sib1_tda,
                           rrc->configuration.minRXTXTIME,
                           NULL,
                           NULL,
                           NULL,
                           0,
                           ue_p->rnti,
                           ue_p->masterCellGroup);

luis_pereira87's avatar
luis_pereira87 committed
752 753 754 755 756 757
    uint32_t delay_ms = ue_context_pP->ue_context.masterCellGroup &&
                        ue_context_pP->ue_context.masterCellGroup->spCellConfig &&
                        ue_context_pP->ue_context.masterCellGroup->spCellConfig->spCellConfigDedicated &&
                        ue_context_pP->ue_context.masterCellGroup->spCellConfig->spCellConfigDedicated->downlinkBWP_ToAddModList ?
                        NR_RRC_RECONFIGURATION_DELAY_MS + NR_RRC_BWP_SWITCHING_DELAY_MS : NR_RRC_RECONFIGURATION_DELAY_MS;

luis_pereira87's avatar
luis_pereira87 committed
758 759 760
    nr_mac_enable_ue_rrc_processing_timer(ctxt_pP->module_id,
                                          ue_context_pP->ue_context.rnti,
                                          *rrc->carrier.servingcellconfigcommon->ssbSubcarrierSpacing,
luis_pereira87's avatar
luis_pereira87 committed
761
                                          delay_ms);
luis_pereira87's avatar
luis_pereira87 committed
762
  }
Xue Song's avatar
Xue Song committed
763 764
}

765
//-----------------------------------------------------------------------------
766
void
767 768
rrc_gNB_generate_dedicatedRRCReconfiguration(
    const protocol_ctxt_t     *const ctxt_pP,
769 770
    rrc_gNB_ue_context_t      *ue_context_pP,
    NR_CellGroupConfig_t      *cell_groupConfig_from_DU
771 772 773
)
//-----------------------------------------------------------------------------
{
Cedric Roux's avatar
Cedric Roux committed
774
  gNB_RRC_INST                  *rrc = RC.nrrrc[ctxt_pP->module_id];
775
  gNB_RRC_UE_t                  *ue_p = &ue_context_pP->ue_context;
776 777
  NR_DRB_ToAddModList_t        **DRB_configList  = NULL;
  NR_DRB_ToAddModList_t        **DRB_configList2 = NULL;
778
  NR_SRB_ToAddModList_t        *SRB_configList  = ue_context_pP->ue_context.SRB_configList;
779 780 781 782 783 784 785 786
  struct NR_RRCReconfiguration_v1530_IEs__dedicatedNAS_MessageList
                                *dedicatedNAS_MessageList = NULL;
  NR_DedicatedNAS_Message_t     *dedicatedNAS_Message = NULL;
  uint8_t                        buffer[RRC_BUF_SIZE];
  uint16_t                       size;
  int                            qos_flow_index = 0;
  int                            pdu_sessions_done = 0;
  int i;
787
  uint8_t drb_id_to_setup_start = 1;
788
  uint8_t nb_drb_to_setup = rrc->configuration.drbs;
789
  long drb_priority[NGAP_MAX_DRBS_PER_UE];
790
  NR_CellGroupConfig_t *cellGroupConfig = NULL;
791 792 793

  uint8_t xid = rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id);

794
  NR_SRB_ToAddModList_t **SRB_configList2 = generateSRB2_confList(ue_p, SRB_configList, xid);
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816

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

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

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

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

817
    if (ue_context_pP->ue_context.pduSession[i].status >= PDU_SESSION_STATUS_DONE) {
818 819 820
      continue;
    }

821
    for(long drb_id_add = 1; drb_id_add <= nb_drb_to_setup; drb_id_add++){
822
      uint8_t drb_id;
823 824 825 826

      // Reference TS23501 Table 5.7.4-1: Standardized 5QI to QoS characteristics mapping
      for (qos_flow_index = 0; qos_flow_index < ue_context_pP->ue_context.pduSession[i].param.nb_qos; qos_flow_index++) {
        switch (ue_context_pP->ue_context.pduSession[i].param.qos[qos_flow_index].fiveQI) {
827
          case 1 ... 4:  /* GBR */
828
            drb_id = next_available_drb(ue_p, ue_context_pP->ue_context.pduSession[i].param.pdusession_id, GBR_FLOW);
829
            break;
830
          case 5 ... 9:  /* Non-GBR */
831 832 833 834
            if(rrc->configuration.drbs > 1) /* Force the creation from gNB Conf file - Should be used only in noS1 mode and rfsim for testing purposes. */
              drb_id = next_available_drb(ue_p, ue_context_pP->ue_context.pduSession[i].param.pdusession_id, GBR_FLOW);
            else
              drb_id = next_available_drb(ue_p, ue_context_pP->ue_context.pduSession[i].param.pdusession_id, NONGBR_FLOW);
835 836 837 838 839 840 841 842 843
            break;

          default:
            LOG_E(NR_RRC,"not supported 5qi %lu\n", ue_context_pP->ue_context.pduSession[i].param.qos[qos_flow_index].fiveQI);
            ue_context_pP->ue_context.pduSession[i].status = PDU_SESSION_STATUS_FAILED;
            ue_context_pP->ue_context.pduSession[i].xid = xid;
            pdu_sessions_done++;
            continue;
        }
844

845 846
        switch(ue_context_pP->ue_context.pduSession[i].param.qos[qos_flow_index].allocation_retention_priority.priority_level) {
          case NGAP_PRIORITY_LEVEL_HIGHEST:
847
            drb_priority[drb_id-1] = 1;
848
            break;
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
          case NGAP_PRIORITY_LEVEL_2:
            drb_priority[drb_id-1] = 2;
            break;
          case NGAP_PRIORITY_LEVEL_3:
            drb_priority[drb_id-1] = 3;
            break;
          case NGAP_PRIORITY_LEVEL_4:
            drb_priority[drb_id-1] = 4;
            break;
          case NGAP_PRIORITY_LEVEL_5:
            drb_priority[drb_id-1] = 5;
            break;
          case NGAP_PRIORITY_LEVEL_6:
            drb_priority[drb_id-1] = 6;
            break;
          case NGAP_PRIORITY_LEVEL_7:
            drb_priority[drb_id-1] = 7;
            break;
          case NGAP_PRIORITY_LEVEL_8:
            drb_priority[drb_id-1] = 8;
            break;
          case NGAP_PRIORITY_LEVEL_9:
            drb_priority[drb_id-1] = 9;
            break;
          case NGAP_PRIORITY_LEVEL_10:
            drb_priority[drb_id-1] = 10;
            break;
          case NGAP_PRIORITY_LEVEL_11:
            drb_priority[drb_id-1] = 11;
            break;
          case NGAP_PRIORITY_LEVEL_12:
            drb_priority[drb_id-1] = 12;
            break;
          case NGAP_PRIORITY_LEVEL_13:
            drb_priority[drb_id-1] = 13;
            break;
885
          case NGAP_PRIORITY_LEVEL_LOWEST:
886
            drb_priority[drb_id-1] = 14;
887 888
            break;
          case NGAP_PRIORITY_LEVEL_NO_PRIORITY:
889
            drb_priority[drb_id-1] = 15;
890 891 892
            break;

          default:
893
            LOG_E(NR_RRC,"Not supported priority level\n");
894 895 896
            break;
        }

897 898 899 900 901 902 903 904 905 906 907 908
        if(drb_is_active(ue_p, drb_id)){ /* Non-GBR flow using the same DRB or a GBR flow with no available DRBs*/
          nb_drb_to_setup--;
        } else {
          NR_DRB_ToAddMod_t *DRB_config = generateDRB(ue_p,
                                                    drb_id,
                                                    &ue_context_pP->ue_context.pduSession[i],
                                                    rrc->configuration.enable_sdap,
                                                    rrc->security.do_drb_integrity,
                                                    rrc->security.do_drb_ciphering);
          ASN_SEQUENCE_ADD(&(*DRB_configList)->list, DRB_config);
          ASN_SEQUENCE_ADD(&(*DRB_configList2)->list, DRB_config);
        }
909
      }
910
    }
911

912 913
    ue_context_pP->ue_context.pduSession[i].status = PDU_SESSION_STATUS_DONE;
    ue_context_pP->ue_context.pduSession[i].xid = xid;
914

915
    if (ue_context_pP->ue_context.pduSession[i].param.nas_pdu.buffer != NULL) {
916 917 918
      dedicatedNAS_Message = CALLOC(1, sizeof(NR_DedicatedNAS_Message_t));
      memset(dedicatedNAS_Message, 0, sizeof(OCTET_STRING_t));
      OCTET_STRING_fromBuf(dedicatedNAS_Message,
919 920
                            (char *)ue_context_pP->ue_context.pduSession[i].param.nas_pdu.buffer,
                            ue_context_pP->ue_context.pduSession[i].param.nas_pdu.length);
921 922
      ASN_SEQUENCE_ADD(&dedicatedNAS_MessageList->list, dedicatedNAS_Message);

923
      LOG_I(NR_RRC,"add NAS info with size %d (pdusession id %d)\n",ue_context_pP->ue_context.pduSession[i].param.nas_pdu.length, i);
924 925 926 927 928 929 930 931 932 933 934 935
    } else {
      // TODO
      LOG_E(NR_RRC,"no NAS info (pdusession id %d)\n", i);
    }
  }

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

936
  memset(buffer, 0, sizeof(buffer));
937 938
  if(cell_groupConfig_from_DU == NULL){
    cellGroupConfig = calloc(1, sizeof(NR_CellGroupConfig_t));
939 940
    // FIXME: fill_mastercellGroupConfig() won't fill the right priorities or
    // bearer IDs for the DRBs
941
    fill_mastercellGroupConfig(cellGroupConfig, ue_context_pP->ue_context.masterCellGroup, rrc->um_on_default_drb, (drb_id_to_setup_start < 2) ? 1 : 0, drb_id_to_setup_start, nb_drb_to_setup, drb_priority);
942 943
  }
  else{
944
    LOG_I(NR_RRC, "Master cell group originating from the DU \n");
945 946
    cellGroupConfig = cell_groupConfig_from_DU;
  }
947

948
  size = do_RRCReconfiguration(ctxt_pP, buffer, sizeof(buffer),
949
                                xid,
950
                                *SRB_configList2,
951 952 953 954 955 956
                                *DRB_configList,
                                NULL,
                                NULL,
                                NULL,
                                NULL,
                                dedicatedNAS_MessageList,
957 958
                                ue_context_pP,
                                &rrc->carrier,
959
                                &rrc->configuration,
960 961
                                NULL,
                                cellGroupConfig);
962 963 964 965
  LOG_DUMPMSG(NR_RRC,DEBUG_RRC,(char *)buffer,size,"[MSG] RRC Reconfiguration\n");

  /* Free all NAS PDUs */
  for (i = 0; i < ue_context_pP->ue_context.nb_of_pdusessions; i++) {
966
    if (ue_context_pP->ue_context.pduSession[i].param.nas_pdu.buffer != NULL) {
967
      /* Free the NAS PDU buffer and invalidate it */
968 969
      free(ue_context_pP->ue_context.pduSession[i].param.nas_pdu.buffer);
      ue_context_pP->ue_context.pduSession[i].param.nas_pdu.buffer = NULL;
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
    }
  }

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

  nr_rrc_data_req(
    ctxt_pP,
    DCCH,
    rrc_gNB_mui++,
    SDU_CONFIRM_NO,
    size,
    buffer,
    PDCP_TRANSMISSION_MODE_CONTROL);
luis_pereira87's avatar
luis_pereira87 committed
988 989

  if (NODE_IS_DU(rrc->node_type) || NODE_IS_MONOLITHIC(rrc->node_type)) {
990 991 992 993 994 995 996 997 998 999 1000 1001
    rrc_mac_config_req_gNB(rrc->module_id,
                           rrc->configuration.pdsch_AntennaPorts,
                           rrc->configuration.pusch_AntennaPorts,
                           rrc->configuration.sib1_tda,
                           rrc->configuration.minRXTXTIME,
                           NULL,
                           NULL,
                           NULL,
                           0,
                           ue_context_pP->ue_context.rnti,
                           ue_context_pP->ue_context.masterCellGroup);

luis_pereira87's avatar
luis_pereira87 committed
1002 1003 1004 1005 1006 1007
    uint32_t delay_ms = ue_context_pP->ue_context.masterCellGroup &&
                        ue_context_pP->ue_context.masterCellGroup->spCellConfig &&
                        ue_context_pP->ue_context.masterCellGroup->spCellConfig->spCellConfigDedicated &&
                        ue_context_pP->ue_context.masterCellGroup->spCellConfig->spCellConfigDedicated->downlinkBWP_ToAddModList ?
                        NR_RRC_RECONFIGURATION_DELAY_MS + NR_RRC_BWP_SWITCHING_DELAY_MS : NR_RRC_RECONFIGURATION_DELAY_MS;

luis_pereira87's avatar
luis_pereira87 committed
1008 1009 1010
    nr_mac_enable_ue_rrc_processing_timer(ctxt_pP->module_id,
                                          ue_context_pP->ue_context.rnti,
                                          *rrc->carrier.servingcellconfigcommon->ssbSubcarrierSpacing,
luis_pereira87's avatar
luis_pereira87 committed
1011
                                          delay_ms);
luis_pereira87's avatar
luis_pereira87 committed
1012
  }
1013 1014
}

1015
//-----------------------------------------------------------------------------
Masayuki Harada's avatar
Masayuki Harada committed
1016
void
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
rrc_gNB_modify_dedicatedRRCReconfiguration(
  const protocol_ctxt_t     *const ctxt_pP,
  rrc_gNB_ue_context_t      *ue_context_pP)
//-----------------------------------------------------------------------------
{
  NR_DRB_ToAddMod_t             *DRB_config           = NULL;
  NR_DRB_ToAddModList_t        **DRB_configList  = NULL;
  NR_DRB_ToAddModList_t         *DRB_configList2 = NULL;
  struct NR_RRCReconfiguration_v1530_IEs__dedicatedNAS_MessageList
                                *dedicatedNAS_MessageList = NULL;
  NR_DedicatedNAS_Message_t     *dedicatedNAS_Message = NULL;
  uint8_t                        buffer[RRC_BUF_SIZE];
  uint16_t                       size;
  int                            qos_flow_index = 0;
  int i, j;

  uint8_t xid = rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id);
  DRB_configList = &ue_context_pP->ue_context.DRB_configList;

  DRB_configList2 = CALLOC(1, sizeof(NR_DRB_ToAddModList_t));
  memset(DRB_configList2, 0, sizeof(NR_DRB_ToAddModList_t));

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

  for (i = 0; i < ue_context_pP->ue_context.nb_of_modify_pdusessions; i++) {
    // bypass the new and already configured pdu sessions
    if (ue_context_pP->ue_context.modify_pdusession[i].status >= PDU_SESSION_STATUS_DONE) {
      ue_context_pP->ue_context.modify_pdusession[i].xid = xid;
      continue;
    }

    if (ue_context_pP->ue_context.modify_pdusession[i].cause != NGAP_CAUSE_NOTHING) {
      // set xid of failure pdu session
      ue_context_pP->ue_context.modify_pdusession[i].xid = xid;
      ue_context_pP->ue_context.modify_pdusession[i].status = PDU_SESSION_STATUS_FAILED;
      continue;
    }

    // search exist DRB_config
    for (j = 0; j < (*DRB_configList)->list.count; j++) {
      if ((*DRB_configList)->list.array[j]->cnAssociation->choice.sdap_Config->pdu_Session == 
        ue_context_pP->ue_context.modify_pdusession[i].param.pdusession_id) {
        DRB_config = (*DRB_configList)->list.array[j];
        break;
      }
    }

    if (DRB_config == NULL) {
      ue_context_pP->ue_context.modify_pdusession[i].xid         = xid;
      ue_context_pP->ue_context.modify_pdusession[i].status      = PDU_SESSION_STATUS_FAILED;
      ue_context_pP->ue_context.modify_pdusession[i].cause       = NGAP_CAUSE_RADIO_NETWORK;
      ue_context_pP->ue_context.modify_pdusession[i].cause_value = NGAP_CauseRadioNetwork_unspecified;
      ue_context_pP->ue_context.nb_of_failed_pdusessions++;
      continue;
    }

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

        default:
          LOG_E(NR_RRC,"not supported 5qi %lu\n", ue_context_pP->ue_context.modify_pdusession[i].param.qos[qos_flow_index].fiveQI);
          ue_context_pP->ue_context.modify_pdusession[i].status = PDU_SESSION_STATUS_FAILED;
          ue_context_pP->ue_context.modify_pdusession[i].xid = xid;
          ue_context_pP->ue_context.modify_pdusession[i].cause       = NGAP_CAUSE_RADIO_NETWORK;
          ue_context_pP->ue_context.modify_pdusession[i].cause_value = NGAP_CauseRadioNetwork_not_supported_5QI_value;
          ue_context_pP->ue_context.nb_of_failed_pdusessions++;
          continue;
      }

      LOG_I(NR_RRC, "PDU SESSION ID %ld, DRB ID %ld (index %d), QOS flow %d, 5QI %ld \n",
          DRB_config->cnAssociation->choice.sdap_Config->pdu_Session,
          DRB_config->drb_Identity, i,
          qos_flow_index,
          ue_context_pP->ue_context.modify_pdusession[i].param.qos[qos_flow_index].fiveQI
         );
    }

    ASN_SEQUENCE_ADD(&DRB_configList2->list, DRB_config);

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

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

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

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

1133 1134
  memset(buffer, 0, sizeof(buffer));
  size = do_RRCReconfiguration(ctxt_pP, buffer, sizeof(buffer),
1135 1136 1137 1138 1139 1140 1141 1142 1143
                                xid,
                                NULL,
                                DRB_configList2,
                                NULL,
                                NULL,
                                NULL,
                                NULL,
                                dedicatedNAS_MessageList,
                                NULL,
1144 1145
                                NULL,
                                NULL,
1146
                                NULL,
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
                                NULL);
  LOG_DUMPMSG(NR_RRC, DEBUG_RRC, (char *)buffer, size, "[MSG] RRC Reconfiguration\n");

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

  LOG_I(NR_RRC,
        "[gNB %d] Frame %d, Logical Channel DL-DCCH, Generate RRCReconfiguration (bytes %d, UE RNTI %x)\n",
        ctxt_pP->module_id, ctxt_pP->frame, size, ue_context_pP->ue_context.rnti);
  LOG_D(NR_RRC,
        "[FRAME %05d][RRC_gNB][MOD %u][][--- PDCP_DATA_REQ/%d Bytes (rrcReconfiguration to UE %x MUI %d) --->][PDCP][MOD %u][RB %u]\n",
        ctxt_pP->frame, ctxt_pP->module_id, size, ue_context_pP->ue_context.rnti, rrc_gNB_mui, ctxt_pP->module_id, DCCH);
1165 1166 1167 1168 1169 1170 1171 1172 1173

  nr_rrc_data_req(
    ctxt_pP,
    DCCH,
    rrc_gNB_mui++,
    SDU_CONFIRM_NO,
    size,
    buffer,
    PDCP_TRANSMISSION_MODE_CONTROL);
luis_pereira87's avatar
luis_pereira87 committed
1174 1175

  if (NODE_IS_DU(RC.nrrrc[ctxt_pP->module_id]->node_type) || NODE_IS_MONOLITHIC(RC.nrrrc[ctxt_pP->module_id]->node_type)) {
luis_pereira87's avatar
luis_pereira87 committed
1176 1177 1178 1179 1180 1181
    uint32_t delay_ms = ue_context_pP->ue_context.masterCellGroup &&
                        ue_context_pP->ue_context.masterCellGroup->spCellConfig &&
                        ue_context_pP->ue_context.masterCellGroup->spCellConfig->spCellConfigDedicated &&
                        ue_context_pP->ue_context.masterCellGroup->spCellConfig->spCellConfigDedicated->downlinkBWP_ToAddModList ?
                        NR_RRC_RECONFIGURATION_DELAY_MS + NR_RRC_BWP_SWITCHING_DELAY_MS : NR_RRC_RECONFIGURATION_DELAY_MS;

luis_pereira87's avatar
luis_pereira87 committed
1182 1183 1184
    nr_mac_enable_ue_rrc_processing_timer(ctxt_pP->module_id,
                                          ue_context_pP->ue_context.rnti,
                                          *RC.nrrrc[ctxt_pP->module_id]->carrier.servingcellconfigcommon->ssbSubcarrierSpacing,
luis_pereira87's avatar
luis_pereira87 committed
1185
                                          delay_ms);
luis_pereira87's avatar
luis_pereira87 committed
1186
  }
1187 1188
}

1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
//-----------------------------------------------------------------------------
void
rrc_gNB_generate_dedicatedRRCReconfiguration_release(
    const protocol_ctxt_t   *const ctxt_pP,
    rrc_gNB_ue_context_t    *const ue_context_pP,
    uint8_t                  xid,
    uint32_t                 nas_length,
    uint8_t                 *nas_buffer)
//-----------------------------------------------------------------------------
{
  uint8_t                             buffer[RRC_BUF_SIZE];
  int                                 i;
  uint16_t                            size  = 0;
1202
  NR_DRB_ToReleaseList_t             **DRB_Release_configList2 = NULL;
1203
  NR_DRB_Identity_t                  *DRB_release;
1204 1205 1206
  struct NR_RRCReconfiguration_v1530_IEs__dedicatedNAS_MessageList
                                     *dedicatedNAS_MessageList = NULL;
  NR_DedicatedNAS_Message_t          *dedicatedNAS_Message     = NULL;
1207

1208 1209 1210
  DRB_Release_configList2 = &ue_context_pP->ue_context.DRB_Release_configList2[xid];
  if (*DRB_Release_configList2) {
    free(*DRB_Release_configList2);
1211 1212
  }

1213
  *DRB_Release_configList2 = CALLOC(1, sizeof(**DRB_Release_configList2));
1214
  for(i = 0; i < NB_RB_MAX; i++) {
1215
    if((ue_context_pP->ue_context.pduSession[i].status == PDU_SESSION_STATUS_TORELEASE) && ue_context_pP->ue_context.pduSession[i].xid == xid) {
1216 1217
      DRB_release = CALLOC(1, sizeof(NR_DRB_Identity_t));
      *DRB_release = i+1;
1218
      ASN_SEQUENCE_ADD(&(*DRB_Release_configList2)->list, DRB_release);
1219 1220 1221 1222 1223
    }
  }

  /* If list is empty free the list and reset the address */
  if (nas_length > 0) {
1224 1225 1226 1227 1228 1229 1230
    dedicatedNAS_MessageList = CALLOC(1, sizeof(struct NR_RRCReconfiguration_v1530_IEs__dedicatedNAS_MessageList));
    dedicatedNAS_Message = CALLOC(1, sizeof(NR_DedicatedNAS_Message_t));
    memset(dedicatedNAS_Message, 0, sizeof(OCTET_STRING_t));
    OCTET_STRING_fromBuf(dedicatedNAS_Message,
                         (char *)nas_buffer,
                         nas_length);
    ASN_SEQUENCE_ADD(&dedicatedNAS_MessageList->list, dedicatedNAS_Message);
1231 1232 1233 1234 1235
    LOG_I(NR_RRC,"add NAS info with size %d\n", nas_length);
  } else {
    LOG_W(NR_RRC,"dedlicated NAS list is empty\n");
  }

1236 1237
  memset(buffer, 0, sizeof(buffer));
  size = do_RRCReconfiguration(ctxt_pP, buffer, sizeof(buffer), xid,
1238 1239
                               NULL,
                               NULL,
1240
                               *DRB_Release_configList2,
1241 1242 1243 1244
                               NULL,
                               NULL,
                               NULL,
                               dedicatedNAS_MessageList,
1245
                               NULL,
1246 1247
                               NULL,
                               NULL,
1248
                               NULL,
1249
                               NULL);
1250

1251
  ue_context_pP->ue_context.pdu_session_release_command_flag = 1;
1252

1253
  LOG_DUMPMSG(NR_RRC,DEBUG_RRC,(char *)buffer,size, "[MSG] RRC Reconfiguration\n");
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274

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

  LOG_I(NR_RRC,
        "[gNB %d] Frame %d, Logical Channel DL-DCCH, Generate NR_RRCReconfiguration (bytes %d, UE RNTI %x)\n",
        ctxt_pP->module_id, ctxt_pP->frame, size, ue_context_pP->ue_context.rnti);
  LOG_D(NR_RRC,
        "[FRAME %05d][RRC_gNB][MOD %u][][--- PDCP_DATA_REQ/%d Bytes (rrcReconfiguration to UE %x MUI %d) --->][PDCP][MOD %u][RB %u]\n",
        ctxt_pP->frame, ctxt_pP->module_id, size, ue_context_pP->ue_context.rnti, rrc_gNB_mui, ctxt_pP->module_id, DCCH);
  nr_rrc_data_req(
    ctxt_pP,
    DCCH,
    rrc_gNB_mui++,
    SDU_CONFIRM_NO,
    size,
    buffer,
    PDCP_TRANSMISSION_MODE_CONTROL);
luis_pereira87's avatar
luis_pereira87 committed
1275 1276

  if (NODE_IS_DU(RC.nrrrc[ctxt_pP->module_id]->node_type) || NODE_IS_MONOLITHIC(RC.nrrrc[ctxt_pP->module_id]->node_type)) {
luis_pereira87's avatar
luis_pereira87 committed
1277 1278 1279 1280 1281 1282
    uint32_t delay_ms = ue_context_pP->ue_context.masterCellGroup &&
                        ue_context_pP->ue_context.masterCellGroup->spCellConfig &&
                        ue_context_pP->ue_context.masterCellGroup->spCellConfig->spCellConfigDedicated &&
                        ue_context_pP->ue_context.masterCellGroup->spCellConfig->spCellConfigDedicated->downlinkBWP_ToAddModList ?
                        NR_RRC_RECONFIGURATION_DELAY_MS + NR_RRC_BWP_SWITCHING_DELAY_MS : NR_RRC_RECONFIGURATION_DELAY_MS;

luis_pereira87's avatar
luis_pereira87 committed
1283 1284 1285
    nr_mac_enable_ue_rrc_processing_timer(ctxt_pP->module_id,
                                          ue_context_pP->ue_context.rnti,
                                          *RC.nrrrc[ctxt_pP->module_id]->carrier.servingcellconfigcommon->ssbSubcarrierSpacing,
luis_pereira87's avatar
luis_pereira87 committed
1286
                                          delay_ms);
luis_pereira87's avatar
luis_pereira87 committed
1287
  }
1288 1289
}

1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
//-----------------------------------------------------------------------------
/*
* 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
)
{
1301 1302 1303 1304
  int                                 drb_id;
  uint8_t                            *kRRCenc = NULL;
  uint8_t                            *kRRCint = NULL;
  uint8_t                            *kUPenc = NULL;
Cedric Roux's avatar
Cedric Roux committed
1305
  uint8_t                            *kUPint = NULL;
1306 1307 1308 1309
  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;
1310
  //  uint8_t                             nr_DRB2LCHAN[8];
1311 1312

  ue_context_pP->ue_context.ue_reestablishment_timer = 0;
1313

1314 1315
  /* Derive the keys from kgnb */
  if (DRB_configList != NULL) {
Cedric Roux's avatar
Cedric Roux committed
1316 1317
    nr_derive_key_up_enc(ue_context_pP->ue_context.ciphering_algorithm,
                         ue_context_pP->ue_context.kgnb,
1318
                         &kUPenc);
Cedric Roux's avatar
Cedric Roux committed
1319 1320
    nr_derive_key_up_int(ue_context_pP->ue_context.integrity_algorithm,
                         ue_context_pP->ue_context.kgnb,
1321
                         &kUPint);
1322
  }
1323

Cedric Roux's avatar
Cedric Roux committed
1324 1325
  nr_derive_key_rrc_enc(ue_context_pP->ue_context.ciphering_algorithm,
                        ue_context_pP->ue_context.kgnb,
1326
                        &kRRCenc);
Cedric Roux's avatar
Cedric Roux committed
1327 1328
  nr_derive_key_rrc_int(ue_context_pP->ue_context.integrity_algorithm,
                        ue_context_pP->ue_context.kgnb,
1329
                        &kRRCint);
1330
  /* Refresh SRBs/DRBs */
1331

rmagueta's avatar
rmagueta committed
1332
  LOG_D(NR_RRC,"Configuring PDCP DRBs/SRBs for UE %x\n",ue_context_pP->ue_context.rnti);
1333

1334 1335 1336 1337
  nr_rrc_pdcp_config_asn1_req(ctxt_pP,
                              SRB_configList, // NULL,
                              DRB_configList,
                              DRB_Release_configList2,
Cedric Roux's avatar
Cedric Roux committed
1338 1339
                              (ue_context_pP->ue_context.integrity_algorithm << 4)
                              | ue_context_pP->ue_context.ciphering_algorithm,
1340 1341 1342
                              kRRCenc,
                              kRRCint,
                              kUPenc,
Cedric Roux's avatar
Cedric Roux committed
1343
                              kUPint,
1344
                              NULL,
1345
                              NULL,
1346
                              get_softmodem_params()->sa ? ue_context_pP->ue_context.masterCellGroup->rlc_BearerToAddModList : NULL);
1347
  /* Refresh SRBs/DRBs */
Xue Song's avatar
Xue Song committed
1348
  if (!NODE_IS_CU(RC.nrrrc[ctxt_pP->module_id]->node_type)) {
rmagueta's avatar
rmagueta committed
1349
    LOG_D(NR_RRC,"Configuring RLC DRBs/SRBs for UE %x\n",ue_context_pP->ue_context.rnti);
Xue Song's avatar
Xue Song committed
1350
    nr_rrc_rlc_config_asn1_req(ctxt_pP,
1351 1352 1353 1354 1355
                               SRB_configList, // NULL,
                               DRB_configList,
                               DRB_Release_configList2,
                               NULL,
                               get_softmodem_params()->sa ? ue_context_pP->ue_context.masterCellGroup->rlc_BearerToAddModList : NULL);
Xue Song's avatar
Xue Song committed
1356
  }
1357

1358 1359 1360 1361 1362
  /* 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;
1363
        ue_context_pP->ue_context.Srb1.Srb_info.Srb_id = 1;
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
      } else if (SRB_configList->list.array[i]->srb_Identity == 2) {
        ue_context_pP->ue_context.Srb2.Active = 1;
        ue_context_pP->ue_context.Srb2.Srb_info.Srb_id = 2;
        LOG_I(NR_RRC,"[gNB %d] Frame      %d CC %d : SRB2 is now active\n",
              ctxt_pP->module_id,
              ctxt_pP->frame,
              ue_context_pP->ue_context.primaryCC_id);
      } else {
        LOG_W(NR_RRC,"[gNB %d] Frame      %d CC %d : invalide SRB identity %ld\n",
              ctxt_pP->module_id,
              ctxt_pP->frame,
              ue_context_pP->ue_context.primaryCC_id,
              SRB_configList->list.array[i]->srb_Identity);
yaojie's avatar
yaojie committed
1377 1378
      }
    }
1379

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

1384 1385 1386 1387 1388
  /* Loop through DRBs and establish if necessary */
  if (DRB_configList != NULL) {
    for (int i = 0; i < DRB_configList->list.count; i++) {
      if (DRB_configList->list.array[i]) {
        drb_id = (int)DRB_configList->list.array[i]->drb_Identity;
1389
        LOG_A(NR_RRC, "[gNB %d] Frame  %d : Logical Channel UL-DCCH, Received NR_RRCReconfigurationComplete from UE rnti %x, reconfiguring DRB %d\n",
1390 1391 1392
            ctxt_pP->module_id,
            ctxt_pP->frame,
            ctxt_pP->rnti,
yaojie's avatar
yaojie committed
1393 1394
            (int)DRB_configList->list.array[i]->drb_Identity);
            //(int)*DRB_configList->list.array[i]->pdcp_Config->moreThanOneRLC->primaryPath.logicalChannel);
1395

1396 1397 1398 1399
        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);
1400

1401 1402 1403
          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));
1404

yaojie's avatar
yaojie committed
1405 1406 1407
          //if (DRB_configList->list.array[i]->pdcp_Config->moreThanOneRLC->primaryPath.logicalChannel) {
          //  nr_DRB2LCHAN[i] = (uint8_t) * DRB_configList->list.array[i]->pdcp_Config->moreThanOneRLC->primaryPath.logicalChannel;
          //}
1408

1409 1410 1411
            // rrc_mac_config_req_eNB
        } else {        // remove LCHAN from MAC/PHY
          if (ue_context_pP->ue_context.DRB_active[drb_id] == 1) {
yoshio.inoue's avatar
yoshio.inoue committed
1412
            /* TODO : It may be needed if gNB goes into full stack working. */
1413 1414 1415 1416
            // DRB has just been removed so remove RLC + PDCP for DRB
            /*      rrc_pdcp_config_req (ctxt_pP->module_id, frameP, 1, CONFIG_ACTION_REMOVE,
            (ue_mod_idP * NB_RB_MAX) + DRB2LCHAN[i],UNDEF_SECURITY_MODE);
            */
yaojie's avatar
yaojie committed
1417
            /*rrc_rlc_config_req(ctxt_pP,
1418 1419 1420 1421
                                SRB_FLAG_NO,
                                MBMS_FLAG_NO,
                                CONFIG_ACTION_REMOVE,
                                nr_DRB2LCHAN[i],
yaojie's avatar
yaojie committed
1422
                                Rlc_info_um);*/
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
          }

          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;
1447
        }
1448
      }
1449
    }
1450 1451 1452 1453

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

yaojie's avatar
yaojie committed
1456 1457 1458 1459 1460 1461 1462 1463
//-----------------------------------------------------------------------------
void
rrc_gNB_generate_RRCReestablishment(
  const protocol_ctxt_t *const ctxt_pP,
  rrc_gNB_ue_context_t  *const ue_context_pP,
  const int             CC_id)
//-----------------------------------------------------------------------------
{
1464
    // int UE_id = -1;
yaojie's avatar
yaojie committed
1465 1466
    //NR_LogicalChannelConfig_t  *SRB1_logicalChannelConfig = NULL;
    NR_SRB_ToAddModList_t      **SRB_configList;
1467
    // NR_SRB_ToAddMod_t          *SRB1_config = NULL;
yaojie's avatar
yaojie committed
1468 1469 1470
    //rrc_gNB_carrier_data_t     *carrier = NULL;
    gNB_RRC_UE_t               *ue_context = NULL;
    module_id_t                 module_id = ctxt_pP->module_id;
1471
    // uint16_t                    rnti = ctxt_pP->rnti;
yaojie's avatar
yaojie committed
1472 1473 1474 1475 1476 1477 1478 1479
    
    SRB_configList = &(ue_context_pP->ue_context.SRB_configList);
    //carrier = &(RC.nrrrc[ctxt_pP->module_id]->carrier);
    ue_context = &(ue_context_pP->ue_context);
    ue_context->Srb0.Tx_buffer.payload_size = do_RRCReestablishment(ctxt_pP,
        ue_context_pP,
        CC_id,
        (uint8_t *) ue_context->Srb0.Tx_buffer.Payload,
1480
        sizeof(ue_context->Srb0.Tx_buffer.Payload),
yaojie's avatar
yaojie committed
1481
        //(uint8_t) carrier->p_gNB, // at this point we do not have the UE capability information, so it can only be TM1 or TM2
1482 1483
        rrc_gNB_get_next_transaction_identifier(module_id),
        SRB_configList
yaojie's avatar
yaojie committed
1484 1485 1486 1487 1488
        //&(ue_context->physicalConfigDedicated)
        );

    /* Configure SRB1 for UE */
    if (*SRB_configList != NULL) {
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
      for (int cnt = 0; cnt < (*SRB_configList)->list.count; cnt++) {
        if ((*SRB_configList)->list.array[cnt]->srb_Identity == 1) {
          // SRB1_config = (*SRB_configList)->list.array[cnt];
        }

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

        // rrc_mac_config_req_eNB
      }
yaojie's avatar
yaojie committed
1499 1500 1501 1502 1503
    }  // if (*SRB_configList != NULL)
    
    LOG_I(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" [RAPROC] Logical Channel DL-DCCH, Generating NR_RRCReestablishment (bytes %d)\n",
          PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
          ue_context->Srb0.Tx_buffer.payload_size);
1504
#if(0)
yoshio.inoue's avatar
yoshio.inoue committed
1505
    /* TODO : It may be needed if gNB goes into full stack working. */
Laurent THOMAS's avatar
Laurent THOMAS committed
1506
    UE = find_nr_UE(module_id, rnti);
yaojie's avatar
yaojie committed
1507 1508
    if (UE_id != -1) {
      /* Activate reject timer, if RRCComplete not received after 10 frames, reject UE */
1509
      RC.nrmac[module_id]->UE_info.UE_sched_ctrl[UE_id].ue_reestablishment_reject_timer = 1;
yaojie's avatar
yaojie committed
1510
      /* Reject UE after 10 frames, LTE_RRCConnectionReestablishmentReject is triggered */
1511
      RC.nrmac[module_id]->UE_info.UE_sched_ctrl[UE_id].ue_reestablishment_reject_timer_thres = 100;
yaojie's avatar
yaojie committed
1512 1513 1514 1515 1516
    } else {
      LOG_E(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" Generating NR_RRCReestablishment without UE_id(MAC) rnti %x\n",
            PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
            rnti);
    }
1517
#endif
yaojie's avatar
yaojie committed
1518 1519
}

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

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

  uint8_t next_xid = rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id);
  int ret = 0;
1547
  ue_context_pP->ue_context.StatusRrc = NR_RRC_CONNECTED;
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
  ue_context_pP->ue_context.ue_rrc_inactivity_timer = 1; // set rrc inactivity when UE goes into RRC_CONNECTED
  ue_context_pP->ue_context.reestablishment_xid = next_xid;
  SRB_configList2 = &ue_context_pP->ue_context.SRB_configList2[xid];

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

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

1568 1569 1570
  // SRB2_config = CALLOC(1, sizeof(*SRB2_config));
  // SRB2_config->srb_Identity = 2;

1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
  SRB_configList2 = &(ue_context_pP->ue_context.SRB_configList2[next_xid]);
  DRB_configList2 = &(ue_context_pP->ue_context.DRB_configList2[next_xid]);

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

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

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

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

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

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

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

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

1614
  if (get_softmodem_params()->sa) {
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
    hashtable_rc_t    h_rc;
    int               j;
    rrc_ue_ngap_ids_t *rrc_ue_ngap_ids_p = NULL;
    uint16_t ue_initial_id = ue_context_pP->ue_context.ue_initial_id;
    uint32_t gNB_ue_ngap_id = ue_context_pP->ue_context.gNB_ue_ngap_id;
    gNB_RRC_INST *rrc_instance_p = RC.nrrrc[GNB_INSTANCE_TO_MODULE_ID(ctxt_pP->instance)];

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

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

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

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

1638
    gtpv1u_gnb_create_tunnel_req_t  create_tunnel_req={0};
1639 1640 1641
    /* Save e RAB information for later */

    for ( j = 0, i = 0; i < NB_RB_MAX; i++) {
1642 1643
      if (ue_context_pP->ue_context.pduSession[i].status == PDU_SESSION_STATUS_ESTABLISHED || ue_context_pP->ue_context.pduSession[i].status == PDU_SESSION_STATUS_DONE) {
        create_tunnel_req.pdusession_id[j]   = ue_context_pP->ue_context.pduSession[i].param.pdusession_id;
1644
        create_tunnel_req.incoming_rb_id[j]  = i+1;
1645
        create_tunnel_req.outgoing_teid[j]  = ue_context_pP->ue_context.pduSession[i].param.gtp_teid;
1646 1647
        // to be developped, use the first QFI only
        create_tunnel_req.outgoing_qfi[j]  = ue_context_pP->ue_context.pduSession[i].param.qos[0].qfi;
1648
        memcpy(create_tunnel_req.dst_addr[j].buffer,
1649
               ue_context_pP->ue_context.pduSession[i].param.upf_addr.buffer,
1650
                sizeof(uint8_t)*20);
1651
        create_tunnel_req.dst_addr[j].length = ue_context_pP->ue_context.pduSession[i].param.upf_addr.length;
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
        j++;
      }
    }

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

    if ( ret != 0 ) {
1664
      LOG_E(NR_RRC,"gtpv1u_update_ngu_tunnel failed,start to release UE %x\n",reestablish_rnti);
1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690

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

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

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

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

      ue_context_pP->ue_context.ue_release_timer_s1 = 1;
      ue_context_pP->ue_context.ue_release_timer_thres_s1 = 100;
      ue_context_pP->ue_context.ue_release_timer = 0;
      ue_context_pP->ue_context.ue_reestablishment_timer = 0;
      ue_context_pP->ue_context.ul_failure_timer = 20000; // set ul_failure to 20000 for triggering rrc_eNB_send_S1AP_UE_CONTEXT_RELEASE_REQ
      ue_context_pP->ue_context.ul_failure_timer = 0;
      return;
    }
1691
  } 
1692 1693 1694 1695 1696

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

1697
  if (get_softmodem_params()->sa) {
1698
    uint8_t send_security_mode_command = false;
1699 1700 1701
    nr_rrc_pdcp_config_security(
      ctxt_pP,
      ue_context_pP,
Cedric Roux's avatar
Cedric Roux committed
1702
      send_security_mode_command ? 0 : 1);
1703 1704 1705 1706 1707 1708 1709 1710
    LOG_D(NR_RRC, "set security successfully \n");
  }

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

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

1717
  memset(buffer, 0, sizeof(buffer));
1718

1719
  size = do_RRCReconfiguration(ctxt_pP, buffer, sizeof(buffer),
1720 1721 1722 1723 1724 1725 1726 1727
                                xid,
                               *SRB_configList2,
                                DRB_configList,
                                NULL,
                                NULL,
                                NULL,
                                NULL, // MeasObj_list,
                                NULL,
1728
                                NULL,
1729 1730
                                NULL,
                                NULL,
1731
                                NULL,
1732
                                NULL);
1733

1734
  LOG_DUMPMSG(NR_RRC,DEBUG_RRC,(char *)buffer,size, "[MSG] RRC Reconfiguration\n");
1735 1736 1737

  /* Free all NAS PDUs */
  for (i = 0; i < ue_context_pP->ue_context.nb_of_pdusessions; i++) {
1738
    if (ue_context_pP->ue_context.pduSession[i].param.nas_pdu.buffer != NULL) {
1739
      /* Free the NAS PDU buffer and invalidate it */
1740 1741
      free(ue_context_pP->ue_context.pduSession[i].param.nas_pdu.buffer);
      ue_context_pP->ue_context.pduSession[i].param.nas_pdu.buffer = NULL;
1742 1743 1744 1745
    }
  }

  if(size==65535) {
1746
    LOG_E(NR_RRC,"RRC decode err!!! do_RRCReconfiguration\n");
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763
    return;
  } else {
    LOG_I(NR_RRC,
          "[gNB %d] Frame %d, Logical Channel DL-DCCH, Generate NR_RRCConnectionReconfiguration (bytes %d, UE id %x)\n",
          ctxt_pP->module_id, ctxt_pP->frame, size, ue_context_pP->ue_context.rnti);
    LOG_D(NR_RRC,
          "[FRAME %05d][RRC_gNB][MOD %u][][--- PDCP_DATA_REQ/%d Bytes (rrcConnectionReconfiguration to UE %x MUI %d) --->][PDCP][MOD %u][RB %u]\n",
          ctxt_pP->frame, ctxt_pP->module_id, size, ue_context_pP->ue_context.rnti, rrc_gNB_mui, ctxt_pP->module_id, DCCH);
    nr_rrc_data_req(
      ctxt_pP,
      DCCH,
      rrc_gNB_mui++,
      SDU_CONFIRM_NO,
      size,
      buffer,
      PDCP_TRANSMISSION_MODE_CONTROL);
  }
luis_pereira87's avatar
luis_pereira87 committed
1764 1765

  if (NODE_IS_DU(RC.nrrrc[ctxt_pP->module_id]->node_type) || NODE_IS_MONOLITHIC(RC.nrrrc[ctxt_pP->module_id]->node_type)) {
luis_pereira87's avatar
luis_pereira87 committed
1766 1767 1768 1769 1770 1771
    uint32_t delay_ms = ue_context_pP->ue_context.masterCellGroup &&
                        ue_context_pP->ue_context.masterCellGroup->spCellConfig &&
                        ue_context_pP->ue_context.masterCellGroup->spCellConfig->spCellConfigDedicated &&
                        ue_context_pP->ue_context.masterCellGroup->spCellConfig->spCellConfigDedicated->downlinkBWP_ToAddModList ?
                        NR_RRC_RECONFIGURATION_DELAY_MS + NR_RRC_BWP_SWITCHING_DELAY_MS : NR_RRC_RECONFIGURATION_DELAY_MS;

luis_pereira87's avatar
luis_pereira87 committed
1772 1773 1774
    nr_mac_enable_ue_rrc_processing_timer(ctxt_pP->module_id,
                                          ue_context_pP->ue_context.rnti,
                                          *RC.nrrrc[ctxt_pP->module_id]->carrier.servingcellconfigcommon->ssbSubcarrierSpacing,
luis_pereira87's avatar
luis_pereira87 committed
1775
                                          delay_ms);
luis_pereira87's avatar
luis_pereira87 committed
1776
  }
1777
}
1778 1779
//-----------------------------------------------------------------------------

1780 1781
int nr_rrc_reconfiguration_req(rrc_gNB_ue_context_t         *const ue_context_pP,
                               protocol_ctxt_t              *const ctxt_pP,
1782 1783
                               const int                    dl_bwp_id,
                               const int                    ul_bwp_id) {
1784 1785 1786 1787 1788 1789

  uint8_t buffer[RRC_BUF_SIZE];
  memset(buffer, 0, sizeof(buffer));
  uint8_t xid = rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id);

  NR_CellGroupConfig_t *masterCellGroup = ue_context_pP->ue_context.masterCellGroup;
1790 1791 1792 1793 1794 1795 1796
  if (dl_bwp_id >= 0) {
    *masterCellGroup->spCellConfig->spCellConfigDedicated->firstActiveDownlinkBWP_Id = dl_bwp_id;
    *masterCellGroup->spCellConfig->spCellConfigDedicated->defaultDownlinkBWP_Id = dl_bwp_id;
  }
  if (ul_bwp_id >= 0) {
    *masterCellGroup->spCellConfig->spCellConfigDedicated->uplinkConfig->firstActiveUplinkBWP_Id = ul_bwp_id;
  }
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808

  uint16_t  size = do_RRCReconfiguration(ctxt_pP,
                                         buffer,
                                         sizeof(buffer),
                                         xid,
                                         NULL,
                                         NULL,
                                         NULL,
                                         NULL,
                                         NULL,
                                         NULL,
                                         NULL,
1809
                                         ue_context_pP,
luis_pereira87's avatar
luis_pereira87 committed
1810 1811
                                         NULL,
                                         NULL,
luis_pereira87's avatar
luis_pereira87 committed
1812
                                         NULL,
1813 1814
                                         masterCellGroup);

1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
  gNB_RrcConfigurationReq *configuration = &RC.nrrrc[ctxt_pP->module_id]->configuration;
  rrc_mac_config_req_gNB(ctxt_pP->module_id,
                         configuration->pdsch_AntennaPorts,
                         configuration->pusch_AntennaPorts,
                         configuration->sib1_tda,
                         configuration->minRXTXTIME,
                         NULL,
                         NULL,
                         NULL,
                         0,
                         ue_context_pP->ue_context.rnti,
                         masterCellGroup);

luis_pereira87's avatar
luis_pereira87 committed
1828 1829 1830 1831 1832 1833 1834
  nr_rrc_data_req(ctxt_pP,
                  DCCH,
                  rrc_gNB_mui++,
                  SDU_CONFIRM_NO,
                  size,
                  buffer,
                  PDCP_TRANSMISSION_MODE_CONTROL);
luis_pereira87's avatar
luis_pereira87 committed
1835

luis_pereira87's avatar
luis_pereira87 committed
1836
  if (NODE_IS_DU(RC.nrrrc[ctxt_pP->module_id]->node_type) || NODE_IS_MONOLITHIC(RC.nrrrc[ctxt_pP->module_id]->node_type)) {
1837 1838 1839 1840 1841 1842
    uint32_t delay_ms = ue_context_pP->ue_context.masterCellGroup &&
                        ue_context_pP->ue_context.masterCellGroup->spCellConfig &&
                        ue_context_pP->ue_context.masterCellGroup->spCellConfig->spCellConfigDedicated &&
                        ue_context_pP->ue_context.masterCellGroup->spCellConfig->spCellConfigDedicated->downlinkBWP_ToAddModList ?
                        NR_RRC_RECONFIGURATION_DELAY_MS + NR_RRC_BWP_SWITCHING_DELAY_MS : NR_RRC_RECONFIGURATION_DELAY_MS;

luis_pereira87's avatar
luis_pereira87 committed
1843 1844 1845 1846
    nr_mac_enable_ue_rrc_processing_timer(ctxt_pP->module_id,
                                          ue_context_pP->ue_context.rnti,
                                          *RC.nrrrc[ctxt_pP->module_id]->carrier.servingcellconfigcommon->ssbSubcarrierSpacing,
                                          delay_ms);
1847
  }
1848

1849 1850 1851
  return 0;
}

1852
/*------------------------------------------------------------------------------*/
Xue Song's avatar
Xue Song committed
1853 1854 1855
int nr_rrc_gNB_decode_ccch(protocol_ctxt_t    *const ctxt_pP,
                           const uint8_t      *buffer,
                           int                buffer_length,
1856 1857
                           const uint8_t      *du_to_cu_rrc_container,
                           int                du_to_cu_rrc_container_len)
1858
{
1859
  module_id_t                                       Idx;
1860 1861 1862 1863 1864
  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;
1865 1866
  NR_RRCReestablishmentRequest_IEs_t                rrcReestablishmentRequest;
  uint64_t                                          random_value = 0;
1867

1868 1869
  LOG_I(NR_RRC, "Decoding CCCH: RNTI %04x, inst %ld, payload_size %d\n", ctxt_pP->rnti, ctxt_pP->instance, buffer_length);
  dec_rval = uper_decode(NULL, &asn_DEF_NR_UL_CCCH_Message, (void **) &ul_ccch_msg, buffer, buffer_length, 0, 0);
1870

1871 1872 1873 1874 1875 1876
  if (dec_rval.code != RC_OK || dec_rval.consumed == 0) {
    LOG_E(NR_RRC,
          PROTOCOL_NR_RRC_CTXT_UE_FMT" FATAL Error in receiving CCCH\n",
          PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
    return -1;
  }
1877

yaojie's avatar
yaojie committed
1878 1879
    if (ul_ccch_msg->message.present == NR_UL_CCCH_MessageType_PR_c1) {
     switch (ul_ccch_msg->message.choice.c1->present) {
1880
      case NR_UL_CCCH_MessageType__c1_PR_NOTHING:
1881
        /* TODO */
1882 1883 1884 1885 1886 1887
        LOG_I(NR_RRC,
            PROTOCOL_NR_RRC_CTXT_FMT" Received PR_NOTHING on UL-CCCH-Message\n",
            PROTOCOL_NR_RRC_CTXT_ARGS(ctxt_pP));
        break;

      case NR_UL_CCCH_MessageType__c1_PR_rrcSetupRequest:
1888
        LOG_D(NR_RRC, "Received RRCSetupRequest on UL-CCCH-Message (UE rnti %x)\n", ctxt_pP->rnti);
1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
        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);
        } else {
          rrcSetupRequest = &ul_ccch_msg->message.choice.c1->choice.rrcSetupRequest->rrcSetupRequest;
          if (NR_InitialUE_Identity_PR_randomValue == rrcSetupRequest->ue_Identity.present) {
            /* randomValue                         BIT STRING (SIZE (39)) */
            if (rrcSetupRequest->ue_Identity.choice.randomValue.size != 5) { // 39-bit random value
              LOG_E(NR_RRC, "wrong InitialUE-Identity randomValue size, expected 5, provided %lu",
                          (long unsigned int)rrcSetupRequest->ue_Identity.choice.randomValue.size);
              return -1;
            }
1901

1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965
            memcpy(((uint8_t *) & random_value) + 3,
                    rrcSetupRequest->ue_Identity.choice.randomValue.buf,
                    rrcSetupRequest->ue_Identity.choice.randomValue.size);

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

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

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

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

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

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

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

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

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

1966
              if (ue_context_p != NULL) {
1967
                ue_context_p->ue_context.Initialue_identity_5g_s_TMSI.presence = true;
1968
                ue_context_p->ue_context.ng_5G_S_TMSI_Part1 = s_tmsi_part1;
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
              }
            }
          } else {
            /* TODO */
            memcpy(((uint8_t *) & random_value) + 3,
                    rrcSetupRequest->ue_Identity.choice.randomValue.buf,
                    rrcSetupRequest->ue_Identity.choice.randomValue.size);

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

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

1988 1989
          rrc_gNB_generate_RRCSetup(ctxt_pP,
                                    rrc_gNB_get_ue_context(gnb_rrc_inst, ctxt_pP->rnti),
1990
                                    du_to_cu_rrc_container,
1991 1992
                                    du_to_cu_rrc_container_len,
                                    gnb_rrc_inst->carrier.servingcellconfigcommon);
1993
        }
1994 1995 1996
        break;

      case NR_UL_CCCH_MessageType__c1_PR_rrcResumeRequest:
yaojie's avatar
yaojie committed
1997
                LOG_I(NR_RRC, "receive rrcResumeRequest message \n");
1998 1999 2000 2001
        break;

      case NR_UL_CCCH_MessageType__c1_PR_rrcReestablishmentRequest:
        LOG_I(NR_RRC, "receive rrcReestablishmentRequest message \n");
yaojie's avatar
yaojie committed
2002 2003 2004 2005 2006
        LOG_DUMPMSG(NR_RRC, DEBUG_RRC,(char *)(buffer), buffer_length,
              "[MSG] RRC Reestablishment Request\n");
        LOG_D(NR_RRC,
              PROTOCOL_NR_RRC_CTXT_UE_FMT"MAC_gNB--- MAC_DATA_IND (rrcReestablishmentRequest on SRB0) --> RRC_gNB\n",
              PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
2007

yaojie's avatar
yaojie committed
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
        rrcReestablishmentRequest = ul_ccch_msg->message.choice.c1->choice.rrcReestablishmentRequest->rrcReestablishmentRequest;
        LOG_I(NR_RRC,
          PROTOCOL_NR_RRC_CTXT_UE_FMT" NR_RRCReestablishmentRequest cause %s\n",
          PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
          ((rrcReestablishmentRequest.reestablishmentCause == NR_ReestablishmentCause_otherFailure) ?   "Other Failure" :
          (rrcReestablishmentRequest.reestablishmentCause == NR_ReestablishmentCause_handoverFailure) ? "Handover Failure" :
          "reconfigurationFailure"));
        {
          uint16_t                          c_rnti = 0;
          if (rrcReestablishmentRequest.ue_Identity.physCellId != RC.nrrrc[ctxt_pP->module_id]->carrier.physCellId) {
            /* UE was moving from previous cell so quickly that RRCReestablishment for previous cell was recieved in this cell */
            LOG_E(NR_RRC,
                  PROTOCOL_NR_RRC_CTXT_UE_FMT" NR_RRCReestablishmentRequest ue_Identity.physCellId(%ld) is not equal to current physCellId(%d), fallback to RRC establishment\n",
                  PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
                  rrcReestablishmentRequest.ue_Identity.physCellId,
                  RC.nrrrc[ctxt_pP->module_id]->carrier.physCellId);
2024
            rrc_gNB_generate_RRCSetup_for_RRCReestablishmentRequest(ctxt_pP, 0);
yaojie's avatar
yaojie committed
2025 2026
            break;
          }
2027

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

2030
          for (int i = 0; i < rrcReestablishmentRequest.ue_Identity.shortMAC_I.size; i++) {
yaojie's avatar
yaojie committed
2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
            LOG_D(NR_RRC, "rrcReestablishmentRequest.ue_Identity.shortMAC_I.buf[%d] = %x\n",
                  i, rrcReestablishmentRequest.ue_Identity.shortMAC_I.buf[i]);
          }

          if (rrcReestablishmentRequest.ue_Identity.c_RNTI < 0 ||
              rrcReestablishmentRequest.ue_Identity.c_RNTI > 65535) {
            /* c_RNTI range error should not happen */
            LOG_E(NR_RRC,
                  PROTOCOL_NR_RRC_CTXT_UE_FMT" NR_RRCReestablishmentRequest c_RNTI range error, fallback to RRC establishment\n",
                  PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
2041
            rrc_gNB_generate_RRCSetup_for_RRCReestablishmentRequest(ctxt_pP, 0);
yaojie's avatar
yaojie committed
2042 2043
            break;
          }
2044

yaojie's avatar
yaojie committed
2045 2046 2047
          c_rnti = rrcReestablishmentRequest.ue_Identity.c_RNTI;
          LOG_I(NR_RRC, "c_rnti is %x\n", c_rnti);
          ue_context_p = rrc_gNB_get_ue_context(gnb_rrc_inst, c_rnti);
2048

yaojie's avatar
yaojie committed
2049 2050 2051 2052
          if (ue_context_p == NULL) {
            LOG_E(NR_RRC,
                  PROTOCOL_NR_RRC_CTXT_UE_FMT" NR_RRCReestablishmentRequest without UE context, fallback to RRC establishment\n",
                  PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
2053
            rrc_gNB_generate_RRCSetup_for_RRCReestablishmentRequest(ctxt_pP, 0);
yaojie's avatar
yaojie committed
2054 2055
            break;
          }
2056
#if(0)
yoshio.inoue's avatar
yoshio.inoue committed
2057
          /* TODO : It may be needed if gNB goes into full stack working. */
yaojie's avatar
yaojie committed
2058
          int UE_id = find_nr_UE_id(ctxt_pP->module_id, c_rnti);
2059

yaojie's avatar
yaojie committed
2060 2061 2062 2063
          if(UE_id == -1) {
            LOG_E(NR_RRC,
                  PROTOCOL_NR_RRC_CTXT_UE_FMT" NR_RRCReestablishmentRequest without UE_id(MAC) rnti %x, fallback to RRC establishment\n",
                  PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),c_rnti);
2064
            rrc_gNB_generate_RRCSetup_for_RRCReestablishmentRequest(ctxt_pP, 0);
yaojie's avatar
yaojie committed
2065 2066
            break;
          }
2067

yaojie's avatar
yaojie committed
2068 2069
          //previous rnti
          rnti_t previous_rnti = 0;
2070

2071
          for (int i = 0; i < MAX_MOBILES_PER_ENB; i++) {
yaojie's avatar
yaojie committed
2072 2073 2074 2075 2076
            if (reestablish_rnti_map[i][1] == c_rnti) {
              previous_rnti = reestablish_rnti_map[i][0];
              break;
            }
          }
2077

yaojie's avatar
yaojie committed
2078 2079
          if(previous_rnti != 0) {
            UE_id = find_nr_UE_id(ctxt_pP->module_id, previous_rnti);
2080

yaojie's avatar
yaojie committed
2081 2082 2083 2084
            if(UE_id == -1) {
              LOG_E(NR_RRC,
                    PROTOCOL_NR_RRC_CTXT_UE_FMT" RRCReestablishmentRequest without UE_id(MAC) previous rnti %x, fallback to RRC establishment\n",
                    PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),previous_rnti);
2085
              rrc_gNB_generate_RRCSetup_for_RRCReestablishmentRequest(ctxt_pP, 0);
yaojie's avatar
yaojie committed
2086 2087 2088
              break;
            }
          }
2089
#endif
yaojie's avatar
yaojie committed
2090
          //c-plane not end
2091
          if((ue_context_p->ue_context.StatusRrc != NR_RRC_RECONFIGURED) && (ue_context_p->ue_context.reestablishment_cause == NR_ReestablishmentCause_spare1)) {
yaojie's avatar
yaojie committed
2092 2093 2094 2095 2096 2097
            LOG_E(NR_RRC,
                  PROTOCOL_NR_RRC_CTXT_UE_FMT" NR_RRCReestablishmentRequest (UE %x c-plane is not end), RRC establishment failed \n",
                  PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),c_rnti);
            /* TODO RRC Release ? */
            break;
          }
2098

yaojie's avatar
yaojie committed
2099 2100 2101 2102
          if(ue_context_p->ue_context.ue_reestablishment_timer > 0) {
            LOG_E(NR_RRC,
                  PROTOCOL_NR_RRC_CTXT_UE_FMT" RRRCReconfigurationComplete(Previous) don't receive, delete the Previous UE,\nprevious Status %d, new Status NR_RRC_RECONFIGURED\n",
                  PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
2103
                  ue_context_p->ue_context.StatusRrc
yaojie's avatar
yaojie committed
2104
                  );
2105
            ue_context_p->ue_context.StatusRrc = NR_RRC_RECONFIGURED;
yaojie's avatar
yaojie committed
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
            protocol_ctxt_t  ctxt_old_p;
            PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt_old_p,
                                          ctxt_pP->instance,
                                          GNB_FLAG_YES,
                                          c_rnti,
                                          ctxt_pP->frame,
                                          ctxt_pP->subframe);
            rrc_gNB_process_RRCReconfigurationComplete(&ctxt_old_p,
                ue_context_p,
                ue_context_p->ue_context.reestablishment_xid);
2116

yaojie's avatar
yaojie committed
2117
            for (uint8_t pdusessionid = 0; pdusessionid < ue_context_p->ue_context.nb_of_pdusessions; pdusessionid++) {
2118 2119
              if (ue_context_p->ue_context.pduSession[pdusessionid].status == PDU_SESSION_STATUS_DONE) {
                ue_context_p->ue_context.pduSession[pdusessionid].status = PDU_SESSION_STATUS_ESTABLISHED;
yaojie's avatar
yaojie committed
2120
              } else {
2121
                ue_context_p->ue_context.pduSession[pdusessionid].status = PDU_SESSION_STATUS_FAILED;
2122
              }
yaojie's avatar
yaojie committed
2123 2124
            }
          }
2125

yaojie's avatar
yaojie committed
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
          LOG_D(NR_RRC,
                PROTOCOL_NR_RRC_CTXT_UE_FMT" UE context: %p\n",
                PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
                ue_context_p);
          /* reset timers */
          ue_context_p->ue_context.ul_failure_timer = 0;
          ue_context_p->ue_context.ue_release_timer = 0;
          ue_context_p->ue_context.ue_reestablishment_timer = 0;
          // ue_context_p->ue_context.ue_release_timer_s1 = 0;
          ue_context_p->ue_context.ue_release_timer_rrc = 0;
          ue_context_p->ue_context.reestablishment_xid = -1;

          // insert C-RNTI to map
2139
          for (int i = 0; i < MAX_MOBILES_PER_ENB; i++) {
yaojie's avatar
yaojie committed
2140 2141 2142 2143 2144 2145 2146 2147
            if (reestablish_rnti_map[i][0] == 0) {
              reestablish_rnti_map[i][0] = ctxt_pP->rnti;
              reestablish_rnti_map[i][1] = c_rnti;
              LOG_D(NR_RRC, "reestablish_rnti_map[%d] [0] %x, [1] %x\n",
                    i, reestablish_rnti_map[i][0], reestablish_rnti_map[i][1]);
              break;
            }
          }
2148

yaojie's avatar
yaojie committed
2149 2150 2151 2152 2153 2154
          ue_context_p->ue_context.reestablishment_cause = rrcReestablishmentRequest.reestablishmentCause;
          LOG_D(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" Accept reestablishment request from UE physCellId %ld cause %ld\n",
                PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
                rrcReestablishmentRequest.ue_Identity.physCellId,
                ue_context_p->ue_context.reestablishment_cause);

2155
          ue_context_p->ue_context.primaryCC_id = 0;
yaojie's avatar
yaojie committed
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176
          //LG COMMENT Idx = (ue_mod_idP * NB_RB_MAX) + DCCH;
          Idx = DCCH;
          // SRB1
          ue_context_p->ue_context.Srb1.Active = 1;
          ue_context_p->ue_context.Srb1.Srb_info.Srb_id = Idx;
          memcpy(&ue_context_p->ue_context.Srb1.Srb_info.Lchan_desc[0],
                &DCCH_LCHAN_DESC,
                LCHAN_DESC_SIZE);
          memcpy(&ue_context_p->ue_context.Srb1.Srb_info.Lchan_desc[1],
                &DCCH_LCHAN_DESC,
                LCHAN_DESC_SIZE);
          // SRB2: set  it to go through SRB1 with id 1 (DCCH)
          ue_context_p->ue_context.Srb2.Active = 1;
          ue_context_p->ue_context.Srb2.Srb_info.Srb_id = Idx;
          memcpy(&ue_context_p->ue_context.Srb2.Srb_info.Lchan_desc[0],
                &DCCH_LCHAN_DESC,
                LCHAN_DESC_SIZE);
          memcpy(&ue_context_p->ue_context.Srb2.Srb_info.Lchan_desc[1],
                &DCCH_LCHAN_DESC,
                LCHAN_DESC_SIZE);

2177
          rrc_gNB_generate_RRCReestablishment(ctxt_pP, ue_context_p, 0);
yaojie's avatar
yaojie committed
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191

          LOG_I(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT"CALLING RLC CONFIG SRB1 (rbid %d)\n",
                PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
                Idx);
          // nr_rrc_pdcp_config_asn1_req(ctxt_pP,
          //                         ue_context_p->ue_context.SRB_configList,
          //                         NULL,
          //                         NULL,
          //                         0xff,
          //                         NULL,
          //                         NULL,
          //                         NULL,
          //                         NULL,
          //                         NULL,
2192
          //                         NULL,
yaojie's avatar
yaojie committed
2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203
          //                         NULL);

          // if (!NODE_IS_CU(RC.nrrrc[ctxt_pP->module_id]->node_type)) {
            // nr_rrc_rlc_config_asn1_req(ctxt_pP,
            //                         ue_context_p->ue_context.SRB_configList,
            //                         NULL,
            //                         NULL,
            //                         NULL,
            //                         NULL);
          // }
        }
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214
        break;

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

      default:
        LOG_E(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" Unknown message\n",
                   PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
        break;
2215
    }
2216 2217
  }
  return 0;
2218 2219
}

Xue Song's avatar
Xue Song committed
2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242
/*! \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;
}

2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268
static void rrc_gNB_process_MeasurementReport(rrc_gNB_ue_context_t *ue_context, const NR_MeasurementReport_t *measurementReport)
{
  if (LOG_DEBUGFLAG(DEBUG_ASN1))
    xer_fprint(stdout, &asn_DEF_NR_MeasurementReport, (void *)measurementReport);

  DevAssert(measurementReport->criticalExtensions.present == NR_MeasurementReport__criticalExtensions_PR_measurementReport
            && measurementReport->criticalExtensions.choice.measurementReport != NULL);

  const NR_MeasResults_t *measresults = &measurementReport->criticalExtensions.choice.measurementReport->measResults;

  AssertFatal(measresults->measId == 1, "unexpected MeasResult for MeasurementId %ld received\n", measresults->measId);
  DevAssert(measresults->measResultServingMOList.list.count >= 1);
  if (measresults->measResultServingMOList.list.count > 1)
    LOG_W(RRC, "Received %d MeasResultServMO, but handling only 1!\n", measresults->measResultServingMOList.list.count);

  NR_MeasResultServMO_t *measresultservmo = measresults->measResultServingMOList.list.array[0];
  NR_MeasResultNR_t *measresultnr = &measresultservmo->measResultServingCell;
  NR_MeasQuantityResults_t *mqr = measresultnr->measResult.cellResults.resultsSSB_Cell;

  LOG_I(RRC, "RNTI %04x servingCellId %ld MeasResultNR for phyCellId %ld:\n", ue_context->ue_context.rnti, measresultservmo->servCellId, *measresultnr->physCellId);
  if (mqr != NULL)
    LOG_I(RRC, "    resultSSB: RSRP %ld dBm RSRQ %.1f dB SINR %.1f dB\n", *mqr->rsrp - 156, (float) (*mqr->rsrq - 87) / 2.0f, (float) (*mqr->sinr - 46) / 2.0f);
  else
    LOG_I(RRC, "    resultSSB: NOT PROVIDED\n");
}

Xue Song's avatar
Xue Song committed
2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
//-----------------------------------------------------------------------------
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
)
//-----------------------------------------------------------------------------
{
2279 2280 2281
  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;
2282
  uint8_t                             xid;
Xue Song's avatar
Xue Song committed
2283

2284
  int i;
Xue Song's avatar
Xue Song committed
2285

2286
  if ((Srb_id != 1) && (Srb_id != 2)) {
Xue Song's avatar
Xue Song committed
2287
    LOG_E(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" Received message on SRB%ld, should not have ...\n",
2288 2289 2290 2291 2292 2293 2294 2295 2296 2297
          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));
rmagueta's avatar
rmagueta committed
2298 2299 2300 2301

  //for (int i=0;i<sdu_sizeP;i++) printf("%02x ",Rx_sdu[i]);
  //printf("\n");

2302 2303 2304 2305 2306 2307 2308 2309
  dec_rval = uper_decode(
                  NULL,
                  &asn_DEF_NR_UL_DCCH_Message,
                  (void **)&ul_dcch_msg,
                  Rx_sdu,
                  sdu_sizeP,
                  0,
                  0);
2310 2311 2312 2313

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

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

2320 2321
    LOG_T(NR_RRC, "\n");
  }
Xue Song's avatar
Xue Song committed
2322

2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339
  if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) {
    LOG_E(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" Failed to decode UL-DCCH (%zu bytes)\n",
        PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
        dec_rval.consumed);
    return -1;
  }

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

  if (ul_dcch_msg->message.present == NR_UL_DCCH_MessageType_PR_c1) {
    switch (ul_dcch_msg->message.choice.c1->present) {
      case NR_UL_DCCH_MessageType__c1_PR_NOTHING:
        LOG_I(NR_RRC,
            PROTOCOL_NR_RRC_CTXT_FMT" Received PR_NOTHING on UL-DCCH-Message\n",
            PROTOCOL_NR_RRC_CTXT_ARGS(ctxt_pP));
        break;
Xue Song's avatar
Xue Song committed
2340

2341
      case NR_UL_DCCH_MessageType__c1_PR_rrcReconfigurationComplete:
yaojie's avatar
yaojie committed
2342
        LOG_I(NR_RRC, "Receive RRC Reconfiguration Complete message UE %x\n", ctxt_pP->rnti);
2343
        if(!ue_context_p) {
yaojie's avatar
yaojie committed
2344
          LOG_E(NR_RRC, "Processing NR_RRCReconfigurationComplete UE %x, ue_context_p is NULL\n", ctxt_pP->rnti);
2345 2346 2347 2348 2349 2350 2351 2352
          break;
        }

        LOG_DUMPMSG(NR_RRC, DEBUG_RRC, (char *)(Rx_sdu), sdu_sizeP,
                    "[MSG] RRC Connection Reconfiguration Complete\n");
        LOG_D(NR_RRC,
            PROTOCOL_NR_RRC_CTXT_UE_FMT" RLC RB %02d --- RLC_DATA_IND %d bytes "
            "(RRCReconfigurationComplete) ---> RRC_gNB]\n",
Xue Song's avatar
Xue Song committed
2353
            PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364
            DCCH,
            sdu_sizeP);

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

2366
        if (get_softmodem_params()->sa) {
yaojie's avatar
yaojie committed
2367 2368 2369 2370
          if(ue_context_p->ue_context.pdu_session_release_command_flag == 1) {
            xid = ul_dcch_msg->message.choice.c1->choice.rrcReconfigurationComplete->rrc_TransactionIdentifier;
            ue_context_p->ue_context.pdu_session_release_command_flag = 0;
            //gtp tunnel delete
Laurent THOMAS's avatar
Laurent THOMAS committed
2371
	    gtpv1u_gnb_delete_tunnel_req_t req={0};
yaojie's avatar
yaojie committed
2372
            for(i = 0; i < NB_RB_MAX; i++) {
2373
              if(xid == ue_context_p->ue_context.pduSession[i].xid) {
Laurent THOMAS's avatar
Laurent THOMAS committed
2374
                req.pdusession_id[req.num_pdusession++] =
yaojie's avatar
yaojie committed
2375 2376 2377 2378 2379 2380
                  ue_context_p->ue_context.gnb_gtp_psi[i];
                ue_context_p->ue_context.gnb_gtp_teid[i] = 0;
                memset(&ue_context_p->ue_context.gnb_gtp_addrs[i], 0, sizeof(ue_context_p->ue_context.gnb_gtp_addrs[i]));
                ue_context_p->ue_context.gnb_gtp_psi[i]  = 0;
              }
            }
Laurent THOMAS's avatar
Laurent THOMAS committed
2381
	    gtpv1u_delete_ngu_tunnel(ctxt_pP->instance, &req);
yaojie's avatar
yaojie committed
2382 2383
            //NGAP_PDUSESSION_RELEASE_RESPONSE
            rrc_gNB_send_NGAP_PDUSESSION_RELEASE_RESPONSE(ctxt_pP, ue_context_p, xid);
2384
          } else if (ue_context_p->ue_context.established_pdu_sessions_flag != 1) {
2385
            if (ue_context_p->ue_context.nb_of_pdusessions > 0) {
2386 2387 2388
              rrc_gNB_send_NGAP_PDUSESSION_SETUP_RESP(ctxt_pP,
                ue_context_p,
                ul_dcch_msg->message.choice.c1->choice.rrcReconfigurationComplete->rrc_TransactionIdentifier);
2389
              ue_context_p->ue_context.nb_of_pdusessions = 0;
2390
            }
2391 2392
          }
          if (ue_context_p->ue_context.nb_of_modify_pdusessions > 0) {
2393 2394 2395 2396 2397 2398 2399 2400 2401
            rrc_gNB_send_NGAP_PDUSESSION_MODIFY_RESP(ctxt_pP,
                                                     ue_context_p,
                                                     ul_dcch_msg->message.choice.c1->choice.rrcReconfigurationComplete->rrc_TransactionIdentifier);
            ue_context_p->ue_context.nb_of_modify_pdusessions = 0;
            ue_context_p->ue_context.nb_of_failed_pdusessions = 0;
            memset(ue_context_p->ue_context.modify_pdusession, 0, sizeof(ue_context_p->ue_context.modify_pdusession));

            for(int i = 0; i < NR_NB_RB_MAX; i++) {
              ue_context_p->ue_context.modify_pdusession[i].xid = -1;
2402
            }
yaojie's avatar
yaojie committed
2403 2404 2405 2406 2407 2408
          }
        }
        if (first_rrcreconfiguration == 0){
          first_rrcreconfiguration = 1;
          rrc_gNB_send_NGAP_INITIAL_CONTEXT_SETUP_RESP(ctxt_pP, ue_context_p);
        }
2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420

        break;

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

        LOG_DUMPMSG(NR_RRC, DEBUG_RRC,(char *)Rx_sdu,sdu_sizeP,
                    "[MSG] RRC SetupComplete\n");
        LOG_D(NR_RRC,
Xue Song's avatar
Xue Song committed
2421
                PROTOCOL_NR_RRC_CTXT_UE_FMT" RLC RB %02d --- RLC_DATA_IND %d bytes "
2422
                "(RRCSetupComplete) ---> RRC_gNB\n",
Xue Song's avatar
Xue Song committed
2423 2424 2425
                PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
                DCCH,
                sdu_sizeP);
2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468

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

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

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

              uint64_t fiveg_s_TMSI = bitStr_to_uint64(&ul_dcch_msg->message.choice.c1->choice.rrcSetupComplete->
                  criticalExtensions.choice.rrcSetupComplete->ng_5G_S_TMSI_Value->choice.ng_5G_S_TMSI);
              LOG_I(NR_RRC, "Received rrcSetupComplete, 5g_s_TMSI: 0x%lX, amf_set_id: 0x%lX(%ld), amf_pointer: 0x%lX(%ld), 5g TMSI: 0x%X \n",
                  fiveg_s_TMSI, fiveg_s_TMSI >> 38, fiveg_s_TMSI >> 38,
                  (fiveg_s_TMSI >> 32) & 0x3F, (fiveg_s_TMSI >> 32) & 0x3F,
                  (uint32_t)fiveg_s_TMSI);
2469
              if (ue_context_p->ue_context.Initialue_identity_5g_s_TMSI.presence == true) {
2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480
                  ue_context_p->ue_context.Initialue_identity_5g_s_TMSI.amf_set_id = fiveg_s_TMSI >> 38;
                  ue_context_p->ue_context.Initialue_identity_5g_s_TMSI.amf_pointer = (fiveg_s_TMSI >> 32) & 0x3F;
                  ue_context_p->ue_context.Initialue_identity_5g_s_TMSI.fiveg_tmsi = (uint32_t)fiveg_s_TMSI;
              }
            }
          }

          rrc_gNB_process_RRCSetupComplete(
                  ctxt_pP,
                  ue_context_p,
                  ul_dcch_msg->message.choice.c1->choice.rrcSetupComplete->criticalExtensions.choice.rrcSetupComplete);
yaojie's avatar
yaojie committed
2481
          LOG_I(NR_RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" UE State = NR_RRC_CONNECTED \n",
2482 2483 2484 2485 2486
              PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
        }

        ue_context_p->ue_context.ue_release_timer = 0;
        break;
wujing's avatar
wujing committed
2487

2488 2489 2490 2491 2492 2493 2494 2495
      case NR_UL_DCCH_MessageType__c1_PR_measurementReport:
        DevAssert(ul_dcch_msg != NULL
                  && ul_dcch_msg->message.present == NR_UL_DCCH_MessageType_PR_c1
                  && ul_dcch_msg->message.choice.c1
                  && ul_dcch_msg->message.choice.c1->present == NR_UL_DCCH_MessageType__c1_PR_measurementReport);
        rrc_gNB_process_MeasurementReport(ue_context_p, ul_dcch_msg->message.choice.c1->choice.measurementReport);
        break;

wujing's avatar
wujing committed
2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506
        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");

2507
            if (get_softmodem_params()->sa) {
wujing's avatar
wujing committed
2508 2509 2510 2511 2512 2513 2514
                rrc_gNB_send_NGAP_UPLINK_NAS(ctxt_pP,
                                          ue_context_p,
                                          ul_dcch_msg);
            }
            break;

        case NR_UL_DCCH_MessageType__c1_PR_securityModeComplete:
2515
        // to avoid segmentation fault
wujing's avatar
wujing committed
2516 2517 2518 2519
           if(!ue_context_p) {
              LOG_I(NR_RRC, "Processing securityModeComplete UE %x, ue_context_p is NULL\n", ctxt_pP->rnti);
              break;
           }
2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535

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

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

Cedric Roux's avatar
Cedric Roux committed
2536 2537 2538
        /* configure ciphering */
        nr_rrc_pdcp_config_security(ctxt_pP, ue_context_p, 1);

2539 2540 2541
        rrc_gNB_generate_UECapabilityEnquiry(ctxt_pP, ue_context_p);
        //rrc_gNB_generate_defaultRRCReconfiguration(ctxt_pP, ue_context_p);
        break;
wujing's avatar
wujing committed
2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557
        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");
            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;
2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653

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

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

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

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

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

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

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

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

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

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

          if(eutra_index == -1)
          break;
wujing's avatar
wujing committed
2654
      }
2655
      if (get_softmodem_params()->sa) {
wujing's avatar
wujing committed
2656 2657 2658 2659
          rrc_gNB_send_NGAP_UE_CAPABILITIES_IND(ctxt_pP,
                                    ue_context_p,
                                    ul_dcch_msg);
      }
2660

2661 2662
      if(!NODE_IS_CU(RC.nrrrc[ctxt_pP->module_id]->node_type)){
        if (ue_context_p->ue_context.established_pdu_sessions_flag == 1) {
2663
          rrc_gNB_generate_dedicatedRRCReconfiguration(ctxt_pP, ue_context_p, NULL);
2664
        } else {
2665
          rrc_gNB_generate_defaultRRCReconfiguration(ctxt_pP, ue_context_p);
2666
        }
2667
      }
2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708
      else{
        /*Generate a UE context setup request message towards the DU to provide the UE
         *capability info and get the updates on master cell group config from the DU*/
        MessageDef *message_p;
        message_p = itti_alloc_new_message (TASK_RRC_GNB, 0, F1AP_UE_CONTEXT_SETUP_REQ);
        f1ap_ue_context_setup_t *req=&F1AP_UE_CONTEXT_SETUP_REQ (message_p);
        //UE_IDs will be extracted from F1AP layer
        req->gNB_CU_ue_id     = 0;
        req->gNB_DU_ue_id = 0;
        req->rnti             = ue_context_p->ue_context.rnti;
        req->mcc              = RC.nrrrc[ctxt_pP->module_id]->configuration.mcc[0];
        req->mnc              = RC.nrrrc[ctxt_pP->module_id]->configuration.mnc[0];
        req->mnc_digit_length = RC.nrrrc[ctxt_pP->module_id]->configuration.mnc_digit_length[0];
        req->nr_cellid        = RC.nrrrc[ctxt_pP->module_id]->nr_cellid;

        if (ue_context_p->ue_context.established_pdu_sessions_flag == 1) {
          /*Instruction towards the DU for SRB2 configuration*/
          req->srbs_to_be_setup = malloc(1*sizeof(f1ap_srb_to_be_setup_t));
          req->srbs_to_be_setup_length = 1;
          f1ap_srb_to_be_setup_t *SRBs=req->srbs_to_be_setup;
          SRBs[0].srb_id = 2;
          SRBs[0].lcid = 2;

          /*Instruction towards the DU for DRB configuration and tunnel creation*/
          req->drbs_to_be_setup = malloc(1*sizeof(f1ap_drb_to_be_setup_t));
          req->drbs_to_be_setup_length = 1;
          f1ap_drb_to_be_setup_t *DRBs=req->drbs_to_be_setup;
          LOG_I(RRC, "Length of DRB list:%d \n", req->drbs_to_be_setup_length);
          DRBs[0].drb_id = 1;
          DRBs[0].rlc_mode = RLC_MODE_AM;
          DRBs[0].up_ul_tnl[0].tl_address = inet_addr(RC.nrrrc[ctxt_pP->module_id]->eth_params_s.my_addr);
          DRBs[0].up_ul_tnl[0].port=RC.nrrrc[ctxt_pP->module_id]->eth_params_s.my_portd;
          DRBs[0].up_ul_tnl_length = 1;
          DRBs[0].up_dl_tnl[0].tl_address = inet_addr(RC.nrrrc[ctxt_pP->module_id]->eth_params_s.remote_addr);
          DRBs[0].up_dl_tnl[0].port=RC.nrrrc[ctxt_pP->module_id]->eth_params_s.remote_portd;
          DRBs[0].up_dl_tnl_length = 1;
        }
        if( ul_dcch_msg->message.choice.c1->choice.ueCapabilityInformation->criticalExtensions.present ==
                NR_UECapabilityInformation__criticalExtensions_PR_ueCapabilityInformation ) {
          struct NR_UE_CapabilityRAT_ContainerList  *ue_CapabilityRAT_ContainerList =
              ul_dcch_msg->message.choice.c1->choice.ueCapabilityInformation->criticalExtensions.choice.ueCapabilityInformation->ue_CapabilityRAT_ContainerList;
2709 2710 2711 2712 2713
          if(ue_CapabilityRAT_ContainerList!=NULL){
            LOG_I(NR_RRC, "ue_CapabilityRAT_ContainerList is present \n");
            req->cu_to_du_rrc_information = calloc(1,sizeof(cu_to_du_rrc_information_t));
            req->cu_to_du_rrc_information->uE_CapabilityRAT_ContainerList = calloc(1,4096);
            asn_enc_rval_t enc_rval = uper_encode_to_buffer(&asn_DEF_NR_UE_CapabilityRAT_ContainerList,
2714
              NULL,
2715
              ue_CapabilityRAT_ContainerList,
2716
              req->cu_to_du_rrc_information->uE_CapabilityRAT_ContainerList,
2717
              4096);
2718
            AssertFatal (enc_rval.encoded > 0, "ASN1 ue_CapabilityRAT_ContainerList encoding failed (%s, %jd)!\n",
2719
                             enc_rval.failed_type->name, enc_rval.encoded);
2720
            req->cu_to_du_rrc_information->uE_CapabilityRAT_ContainerList_length = (enc_rval.encoded+7)>>3;
2721 2722 2723 2724
          }
          else{
            LOG_I(NR_RRC, "ue_CapabilityRAT_ContainerList is not present \n");
          }
2725 2726 2727 2728
        }
        itti_send_msg_to_task (TASK_CU_F1, ctxt_pP->module_id, message_p);
      }

2729

wujing's avatar
wujing committed
2730
      break;
2731

2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761
            case NR_UL_DCCH_MessageType__c1_PR_rrcReestablishmentComplete:
              LOG_DUMPMSG(NR_RRC,DEBUG_RRC,(char *)Rx_sdu,sdu_sizeP,
                          "[MSG] NR_RRC Connection Reestablishment Complete\n");
              LOG_I(NR_RRC,
                    PROTOCOL_RRC_CTXT_UE_FMT" RLC RB %02d --- RLC_DATA_IND %d bytes "
                    "(rrcConnectionReestablishmentComplete) ---> RRC_gNB\n",
                    PROTOCOL_RRC_CTXT_UE_ARGS(ctxt_pP),
                    DCCH,
                    sdu_sizeP);
              {
                rnti_t reestablish_rnti = 0;

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

                if (!ue_context_p) {
                  LOG_E(NR_RRC,
2762 2763
                        PROTOCOL_NR_RRC_CTXT_UE_FMT" NR_RRCConnectionReestablishmentComplete without UE context, falt\n",
                        PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
2764 2765 2766
                  break;
                }

2767
#if(0)
yoshio.inoue's avatar
yoshio.inoue committed
2768
                /* TODO : It may be needed if gNB goes into full stack working. */
2769
                //clear
2770
                int UE_id = find_nr_UE_id(ctxt_pP->module_id, ctxt_pP->rnti);
2771 2772 2773 2774 2775 2776 2777 2778

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

2779 2780
                RC.nrmac[ctxt_pP->module_id]->UE_info.UE_sched_ctrl[UE_id].ue_reestablishment_reject_timer = 0;
#endif
2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795
                ue_context_p->ue_context.reestablishment_xid = -1;

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

                }

                //ue_context_p->ue_context.ue_release_timer = 0;
                ue_context_p->ue_context.ue_reestablishment_timer = 1;
                // remove UE after 100 frames after LTE_RRCConnectionReestablishmentRelease is triggered
                ue_context_p->ue_context.ue_reestablishment_timer_thres = 1000;
              }
              break;
2796 2797
      default:
        break;
Xue Song's avatar
Xue Song committed
2798
    }
2799 2800
  }
  return 0;
Xue Song's avatar
Xue Song committed
2801 2802
}

Xue Song's avatar
Xue Song committed
2803 2804 2805
void rrc_gNB_process_f1_setup_req(f1ap_setup_req_t *f1_setup_req) {
  LOG_I(NR_RRC,"Received F1 Setup Request from gNB_DU %llu (%s)\n",(unsigned long long int)f1_setup_req->gNB_DU_id,f1_setup_req->gNB_DU_name);
  int cu_cell_ind = 0;
2806 2807 2808
  MessageDef *msg_p =itti_alloc_new_message (TASK_RRC_GNB, 0, F1AP_SETUP_RESP);
  F1AP_SETUP_RESP (msg_p).num_cells_to_activate = 0;
  MessageDef *msg_p2=itti_alloc_new_message (TASK_RRC_GNB, 0, F1AP_GNB_CU_CONFIGURATION_UPDATE);
Xue Song's avatar
Xue Song committed
2809 2810 2811 2812 2813

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

2814 2815 2816
      if (rrc->configuration.mcc[0] == f1_setup_req->cell[i].mcc &&
          rrc->configuration.mnc[0] == f1_setup_req->cell[i].mnc &&
          rrc->nr_cellid == f1_setup_req->cell[i].nr_cellid) {
2817 2818
	//fixme: multi instance is not consistent here
	F1AP_SETUP_RESP (msg_p).gNB_CU_name  = rrc->node_name;
Xue Song's avatar
Xue Song committed
2819 2820 2821 2822 2823 2824
        // check that CU rrc instance corresponds to mcc/mnc/cgi (normally cgi should be enough, but just in case)
        rrc->carrier.MIB = malloc(f1_setup_req->mib_length[i]);
        rrc->carrier.sizeof_MIB = f1_setup_req->mib_length[i];
        LOG_W(NR_RRC, "instance %d mib length %d\n", i, f1_setup_req->mib_length[i]);
        LOG_W(NR_RRC, "instance %d sib1 length %d\n", i, f1_setup_req->sib1_length[i]);
        memcpy((void *)rrc->carrier.MIB,f1_setup_req->mib[i],f1_setup_req->mib_length[i]);
2825 2826 2827 2828 2829 2830
        asn_dec_rval_t dec_rval = uper_decode_complete(NULL,
                                  &asn_DEF_NR_BCCH_BCH_Message,
                                  (void **)&rrc->carrier.mib_DU,
                                  f1_setup_req->mib[i],
                                  f1_setup_req->mib_length[i]);
        AssertFatal(dec_rval.code == RC_OK,
Laurent THOMAS's avatar
Laurent THOMAS committed
2831
                    "[gNB_CU %"PRIu8"] Failed to decode NR_BCCH_BCH_MESSAGE (%zu bits)\n",
2832 2833 2834 2835 2836 2837 2838 2839 2840 2841
                    j,
                    dec_rval.consumed );
        NR_BCCH_BCH_Message_t *mib = &rrc->carrier.mib;
        NR_BCCH_BCH_Message_t *mib_DU = rrc->carrier.mib_DU;
        mib->message.present = NR_BCCH_BCH_MessageType_PR_mib;
        mib->message.choice.mib = CALLOC(1,sizeof(struct NR_MIB));
        memset(mib->message.choice.mib,0,sizeof(struct NR_MIB));
        memcpy(mib->message.choice.mib, mib_DU->message.choice.mib, sizeof(struct NR_MIB));

        dec_rval = uper_decode_complete(NULL,
2842
                                        &asn_DEF_NR_SIB1, //&asn_DEF_NR_BCCH_DL_SCH_Message,
2843 2844 2845 2846 2847 2848 2849 2850 2851
                                        (void **)&rrc->carrier.siblock1_DU,
                                        f1_setup_req->sib1[i],
                                        f1_setup_req->sib1_length[i]);
        AssertFatal(dec_rval.code == RC_OK,
                    "[gNB_DU %"PRIu8"] Failed to decode NR_BCCH_DLSCH_MESSAGE (%zu bits)\n",
                    j,
                    dec_rval.consumed );

        // Parse message and extract SystemInformationBlockType1 field
2852 2853 2854 2855 2856 2857
        rrc->carrier.sib1 = rrc->carrier.siblock1_DU;
        if ( LOG_DEBUGFLAG(DEBUG_ASN1)){
          LOG_I(NR_RRC, "Printing received SIB1 container inside F1 setup request message:\n");
          xer_fprint(stdout, &asn_DEF_NR_SIB1,(void *)rrc->carrier.sib1);
        }

2858
        rrc->carrier.physCellId = f1_setup_req->cell[i].nr_pci;
2859

2860
	F1AP_GNB_CU_CONFIGURATION_UPDATE (msg_p2).gNB_CU_name                                = rrc->node_name;
2861 2862 2863 2864
	F1AP_GNB_CU_CONFIGURATION_UPDATE (msg_p2).cells_to_activate[cu_cell_ind].mcc                           = rrc->configuration.mcc[0];
	F1AP_GNB_CU_CONFIGURATION_UPDATE (msg_p2).cells_to_activate[cu_cell_ind].mnc                           = rrc->configuration.mnc[0];
	F1AP_GNB_CU_CONFIGURATION_UPDATE (msg_p2).cells_to_activate[cu_cell_ind].mnc_digit_length              = rrc->configuration.mnc_digit_length[0];
	F1AP_GNB_CU_CONFIGURATION_UPDATE (msg_p2).cells_to_activate[cu_cell_ind].nr_cellid                     = rrc->nr_cellid;
2865
	F1AP_GNB_CU_CONFIGURATION_UPDATE (msg_p2).cells_to_activate[cu_cell_ind].nrpci                         = f1_setup_req->cell[i].nr_pci;
Xue Song's avatar
Xue Song committed
2866 2867
        int num_SI= 0;

2868
        if (rrc->carrier.SIB23) {
2869 2870
          F1AP_GNB_CU_CONFIGURATION_UPDATE (msg_p2).cells_to_activate[cu_cell_ind].SI_container[2]        = rrc->carrier.SIB23;
          F1AP_GNB_CU_CONFIGURATION_UPDATE (msg_p2).cells_to_activate[cu_cell_ind].SI_container_length[2] = rrc->carrier.sizeof_SIB23;
2871 2872
          num_SI++;
        }
Xue Song's avatar
Xue Song committed
2873

2874
        F1AP_GNB_CU_CONFIGURATION_UPDATE (msg_p2).cells_to_activate[cu_cell_ind].num_SI = num_SI;
Xue Song's avatar
Xue Song committed
2875
        cu_cell_ind++;
2876
	F1AP_GNB_CU_CONFIGURATION_UPDATE (msg_p2).num_cells_to_activate = cu_cell_ind;
2877
	// send
Xue Song's avatar
Xue Song committed
2878 2879
        break;
      } else {// setup_req mcc/mnc match rrc internal list element
Xue Song's avatar
Xue Song committed
2880
        LOG_W(NR_RRC,"[Inst %d] No matching MCC/MNC: rrc->mcc/f1_setup_req->mcc %d/%d rrc->mnc/f1_setup_req->mnc %d/%d rrc->nr_cellid/f1_setup_req->nr_cellid %ld/%ld \n",
2881 2882 2883
              j, rrc->configuration.mcc[0], f1_setup_req->cell[i].mcc,
                 rrc->configuration.mnc[0], f1_setup_req->cell[i].mnc,
                 rrc->nr_cellid, f1_setup_req->cell[i].nr_cellid);
Xue Song's avatar
Xue Song committed
2884 2885 2886
      }
    }// for (int j=0;j<RC.nb_inst;j++)

2887
    if (cu_cell_ind == 0) {
Xue Song's avatar
Xue Song committed
2888
      AssertFatal(1 == 0, "No cell found\n");
2889
    }  else {
2890 2891 2892 2893 2894 2895
      // send ITTI message to F1AP-CU task
      itti_send_msg_to_task (TASK_CU_F1, 0, msg_p);

      itti_send_msg_to_task (TASK_CU_F1, 0, msg_p2);

    }
Xue Song's avatar
Xue Song committed
2896 2897 2898 2899 2900

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

2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932
void rrc_gNB_process_initial_ul_rrc_message(const f1ap_initial_ul_rrc_message_t *ul_rrc)
{
  // first get RRC instance (note, no the ITTI instance)
  module_id_t i = 0;
  for (i=0; i < RC.nb_nr_inst; i++) {
    gNB_RRC_INST *rrc = RC.nrrrc[i];
    if (rrc->nr_cellid == ul_rrc->nr_cellid)
      break;
  }
  //AssertFatal(i != RC.nb_nr_inst, "Cell_id not found\n");
  // TODO REMOVE_DU_RRC in monolithic mode, the MAC does not have the
  // nr_cellid. Thus, the above check would fail. For the time being, just put
  // a warning, as we handle one DU only anyway
  if (i == RC.nb_nr_inst) {
    i = 0;
    LOG_W(RRC, "initial UL RRC message nr_cellid %ld does not match RRC's %ld\n", ul_rrc->nr_cellid, RC.nrrrc[0]->nr_cellid);
  }
  protocol_ctxt_t ctxt = { 0 };
  PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt, i, GNB_FLAG_YES, ul_rrc->crnti, 0, 0);

  nr_rrc_gNB_decode_ccch(&ctxt,
                         ul_rrc->rrc_container,
                         ul_rrc->rrc_container_length,
                         ul_rrc->du2cu_rrc_container,
                         ul_rrc->du2cu_rrc_container_length);

  if (ul_rrc->rrc_container)
    free(ul_rrc->rrc_container);
  if (ul_rrc->du2cu_rrc_container)
    free(ul_rrc->du2cu_rrc_container);
}

Cedric Roux's avatar
Cedric Roux committed
2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944
void rrc_gNB_process_release_request(const module_id_t gnb_mod_idP, x2ap_ENDC_sgnb_release_request_t *m)
{
  gNB_RRC_INST *rrc = RC.nrrrc[gnb_mod_idP];
  rrc_remove_nsa_user(rrc, m->rnti);
}

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

2945 2946
static int  rrc_process_DU_DL(MessageDef *msg_p, const char *msg_name, instance_t instance) {
  NRDuDlReq_t * req=&NRDuDlReq(msg_p);
2947 2948 2949 2950 2951 2952 2953
  protocol_ctxt_t ctxt = {
    .rnti = req->rnti,
    .module_id = instance,
    .instance  = instance,
    .enb_flag  = 1,
    .eNB_index = instance
  };
2954 2955 2956 2957 2958
  gNB_RRC_INST *rrc = RC.nrrrc[ctxt.module_id];
  struct rrc_gNB_ue_context_s *ue_context_p =
    rrc_gNB_get_ue_context(rrc, ctxt.rnti);

  if (req->srb_id == 0) {
2959
    AssertFatal(0 == 1, "should pass through dl_rrc_message()\n");
2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088
  } else if (req->srb_id == 1) {
    NR_DL_DCCH_Message_t *dl_dcch_msg=NULL;
    asn_dec_rval_t dec_rval;
    dec_rval = uper_decode(NULL,
			   &asn_DEF_NR_DL_DCCH_Message,
			   (void **)&dl_dcch_msg,
			   &req->buf->data[2], // buf[0] includes the pdcp header
			   req->buf->size-6,0,0);
    
    if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0))
      LOG_E(F1AP," Failed to decode DL-DCCH (%zu bytes)\n",dec_rval.consumed);
    else
      LOG_D(F1AP, "Received message: present %d and c1 present %d\n",
	    dl_dcch_msg->message.present, dl_dcch_msg->message.choice.c1->present);
    
    if (dl_dcch_msg->message.present == NR_DL_DCCH_MessageType_PR_c1) {
      switch (dl_dcch_msg->message.choice.c1->present) {
      case NR_DL_DCCH_MessageType__c1_PR_NOTHING:
	LOG_I(F1AP, "Received PR_NOTHING on DL-DCCH-Message\n");
	return 0;
	
      case NR_DL_DCCH_MessageType__c1_PR_rrcReconfiguration:
	// handle RRCReconfiguration
	LOG_I(F1AP, "Logical Channel DL-DCCH (SRB1), Received RRCReconfiguration RNTI %x\n",
	      req->rnti);
	NR_RRCReconfiguration_t *rrcReconfiguration = dl_dcch_msg->message.choice.c1->choice.rrcReconfiguration;
	
	if (rrcReconfiguration->criticalExtensions.present == NR_RRCReconfiguration__criticalExtensions_PR_rrcReconfiguration) {
	  NR_RRCReconfiguration_IEs_t *rrcReconfiguration_ies =
	    rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration;
	  
	  if (rrcReconfiguration_ies->measConfig != NULL) {
	    LOG_I(F1AP, "Measurement Configuration is present\n");
	  }
	  
	  if (rrcReconfiguration_ies->radioBearerConfig) {
	    LOG_I(F1AP, "Radio Resource Configuration is present\n");
	    long drb_id;
	    int i;
	    NR_DRB_ToAddModList_t  *DRB_configList  = rrcReconfiguration_ies->radioBearerConfig->drb_ToAddModList;
	    NR_SRB_ToAddModList_t  *SRB_configList  = rrcReconfiguration_ies->radioBearerConfig->srb_ToAddModList;
	    
	    // NR_DRB_ToReleaseList_t *DRB_ReleaseList = rrcReconfiguration_ies->radioBearerConfig->drb_ToReleaseList;
	    
	    // rrc_rlc_config_asn1_req
	    
	    if (SRB_configList != NULL) {
	      for (i = 0; (i < SRB_configList->list.count) && (i < 3); i++) {
		if (SRB_configList->list.array[i]->srb_Identity == 1 ) {
		  ue_context_p->ue_context.Srb1.Active=1;
		} else if (SRB_configList->list.array[i]->srb_Identity == 2 )  {
		  ue_context_p->ue_context.Srb2.Active=1;
		  ue_context_p->ue_context.Srb2.Srb_info.Srb_id=2;
		  LOG_I(F1AP, "[DU %d] SRB2 is now active\n",ctxt.module_id);
		} else {
		  LOG_W(F1AP, "[DU %d] invalide SRB identity %ld\n",ctxt.module_id,
			SRB_configList->list.array[i]->srb_Identity);
		}
	      }
	    }
	    
	    if (DRB_configList != NULL) {
	      for (i = 0; i < DRB_configList->list.count; i++) {  // num max DRB (11-3-8)
		if (DRB_configList->list.array[i]) {
		  drb_id = (int)DRB_configList->list.array[i]->drb_Identity;
		  LOG_I(F1AP,
			"[DU %d] Logical Channel UL-DCCH, Received RRCConnectionReconfiguration for UE rnti %x, reconfiguring DRB %d\n",
			ctxt.module_id,
			ctxt.rnti,
			(int)DRB_configList->list.array[i]->drb_Identity);
		  
		  // (int)*DRB_configList->list.array[i]->logicalChannelIdentity);
		  
		  if (ue_context_p->ue_context.DRB_active[drb_id] == 0) {
		    ue_context_p->ue_context.DRB_active[drb_id] = 1;
		    // logicalChannelIdentity
		    // rrc_mac_config_req_eNB
		  }
		} else {        // remove LCHAN from MAC/PHY
		  AssertFatal(1==0,"Can't handle this yet in DU\n");
		}
	      }
	    }
	  }
	}
	
	break;
	
      case NR_DL_DCCH_MessageType__c1_PR_rrcResume:
	LOG_I(F1AP,"Received rrcResume\n");
	break;
	
      case NR_DL_DCCH_MessageType__c1_PR_rrcRelease:
	LOG_I(F1AP,"Received rrcRelease\n");
	break;
	
      case NR_DL_DCCH_MessageType__c1_PR_rrcReestablishment:
	LOG_I(F1AP,"Received rrcReestablishment\n");
	break;
	
      case NR_DL_DCCH_MessageType__c1_PR_securityModeCommand:
	LOG_I(F1AP,"Received securityModeCommand\n");
	break;
	
      case NR_DL_DCCH_MessageType__c1_PR_dlInformationTransfer:
	LOG_I(F1AP, "Received dlInformationTransfer\n");
	break;
	
      case NR_DL_DCCH_MessageType__c1_PR_ueCapabilityEnquiry:
	LOG_I(F1AP, "Received ueCapabilityEnquiry\n");
	break;
	
      case NR_DL_DCCH_MessageType__c1_PR_counterCheck:
	LOG_I(F1AP, "Received counterCheck\n");
	break;
	
      case NR_DL_DCCH_MessageType__c1_PR_mobilityFromNRCommand:
      case NR_DL_DCCH_MessageType__c1_PR_dlDedicatedMessageSegment_r16:
      case NR_DL_DCCH_MessageType__c1_PR_ueInformationRequest_r16:
      case NR_DL_DCCH_MessageType__c1_PR_dlInformationTransferMRDC_r16:
      case NR_DL_DCCH_MessageType__c1_PR_loggedMeasurementConfiguration_r16:
      case NR_DL_DCCH_MessageType__c1_PR_spare3:
      case NR_DL_DCCH_MessageType__c1_PR_spare2:
      case NR_DL_DCCH_MessageType__c1_PR_spare1:
	break;
      }
    }
  } else if (req->srb_id == 2) {
    // TODO
Laurent THOMAS's avatar
Laurent THOMAS committed
3089
    //abort();
3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102
  }
  
  LOG_I(F1AP, "Received DL RRC Transfer on srb_id %ld\n", req->srb_id);
  //   rlc_op_status_t    rlc_status;
  
  //LOG_I(F1AP, "PRRCContainer size %lu:", ie->value.choice.RRCContainer.size);
  //for (int i = 0; i < ie->value.choice.RRCContainer.size; i++)
  //  printf("%02x ", ie->value.choice.RRCContainer.buf[i]);
  
  //printf (", PDCP PDU size %d:", rrc_dl_sdu_len);
  //for (int i=0;i<rrc_dl_sdu_len;i++) printf("%2x ",pdcp_pdu_p->data[i]);
  //printf("\n");
  
Laurent THOMAS's avatar
Laurent THOMAS committed
3103
  du_rlc_data_req(&ctxt, 1, 0, req->srb_id , 1, 0, req->buf->size, req->buf);
3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117
  //   rlc_status = rlc_data_req(&ctxt
  //                             , 1
  //                             , MBMS_FLAG_NO
  //                             , srb_id
  //                             , 0
  //                             , 0
  //                             , rrc_dl_sdu_len
  //                             , pdcp_pdu_p
  //                             ,NULL
  //                             ,NULL
  //                             );
  //   switch (rlc_status) {
  //     case RLC_OP_STATUS_OK:
  //       //LOG_I(F1AP, "Data sending request over RLC succeeded!\n");
3118
  //       ret=true;
3119 3120 3121
  //       break;
  //     case RLC_OP_STATUS_BAD_PARAMETER:
  //       LOG_W(F1AP, "Data sending request over RLC failed with 'Bad Parameter' reason!\n");
3122
  //       ret= false;
3123 3124 3125
  //       break;
  //     case RLC_OP_STATUS_INTERNAL_ERROR:
  //       LOG_W(F1AP, "Data sending request over RLC failed with 'Internal Error' reason!\n");
3126
  //       ret= false;
3127 3128 3129
  //       break;
  //     case RLC_OP_STATUS_OUT_OF_RESSOURCES:
  //       LOG_W(F1AP, "Data sending request over RLC failed with 'Out of Resources' reason!\n");
3130
  //       ret= false;
3131 3132 3133
  //       break;
  //     default:
  //       LOG_W(F1AP, "RLC returned an unknown status code after PDCP placed the order to send some data (Status Code:%d)\n", rlc_status);
3134
  //       ret= false;
3135 3136 3137 3138 3139 3140
  //       break;
  //   } // switch case
  //   return ret;
return 0;
}

3141 3142
static void rrc_DU_process_ue_context_setup_request(MessageDef *msg_p, const char *msg_name, instance_t instance){

3143
  f1ap_ue_context_setup_t * req=&F1AP_UE_CONTEXT_SETUP_REQ(msg_p);
3144 3145 3146 3147 3148 3149 3150
  protocol_ctxt_t ctxt = {
    .rnti = req->rnti,
    .module_id = instance,
    .instance  = instance,
    .enb_flag  = 1,
    .eNB_index = instance
  };
3151
  gNB_RRC_INST *rrc = RC.nrrrc[ctxt.module_id];
3152
  gNB_MAC_INST *mac = RC.nrmac[ctxt.module_id];
3153 3154
  struct rrc_gNB_ue_context_s *ue_context_p =
      rrc_gNB_get_ue_context(rrc, ctxt.rnti);
3155 3156
  MessageDef *message_p;
  message_p = itti_alloc_new_message (TASK_RRC_GNB, 0, F1AP_UE_CONTEXT_SETUP_RESP);
3157
  f1ap_ue_context_setup_t * resp=&F1AP_UE_CONTEXT_SETUP_RESP(message_p);
3158
  uint32_t incoming_teid = 0;
3159

3160 3161 3162
  if(req->cu_to_du_rrc_information!=NULL){
    if(req->cu_to_du_rrc_information->uE_CapabilityRAT_ContainerList!=NULL){
      LOG_I(NR_RRC, "Length of ue_CapabilityRAT_ContainerList is: %d \n", (int) req->cu_to_du_rrc_information->uE_CapabilityRAT_ContainerList_length);
3163
      struct NR_UE_CapabilityRAT_ContainerList  *ue_CapabilityRAT_ContainerList = NULL;
3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175
      asn_dec_rval_t dec_rval = uper_decode_complete( NULL,
          &asn_DEF_NR_UE_CapabilityRAT_ContainerList,
          (void **)&ue_CapabilityRAT_ContainerList,
          (uint8_t *)req->cu_to_du_rrc_information->uE_CapabilityRAT_ContainerList,
          (int) req->cu_to_du_rrc_information->uE_CapabilityRAT_ContainerList_length);

      if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) {
        AssertFatal(1==0,"UE Capability RAT ContainerList decode error\n");
        // free the memory
        SEQUENCE_free( &asn_DEF_NR_UE_CapabilityRAT_ContainerList, ue_CapabilityRAT_ContainerList, 1 );
        return;
      }
3176
      //To fill ue_context.UE_Capability_MRDC, ue_context.UE_Capability_nr ...
3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246
      int NR_index = -1;
      for(int i = 0;i < ue_CapabilityRAT_ContainerList->list.count; i++){
        if(ue_CapabilityRAT_ContainerList->list.array[i]->rat_Type ==
          NR_RAT_Type_nr){
          LOG_I(NR_RRC, "DU received NR_RAT_Type_nr UE capabilities Info through the UE Context Setup Request from the CU \n");
          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,
                                  ue_CapabilityRAT_ContainerList->list.array[i]->ue_CapabilityRAT_Container.buf,
                                  ue_CapabilityRAT_ContainerList->list.array[i]->ue_CapabilityRAT_Container.size,
                                  0,0);
          if(LOG_DEBUGFLAG(DEBUG_ASN1)){
            xer_fprint(stdout,&asn_DEF_NR_UE_NR_Capability,ue_context_p->ue_context.UE_Capability_nr);
          }

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

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

        if(ue_CapabilityRAT_ContainerList->list.array[i]->rat_Type ==
        NR_RAT_Type_eutra_nr){
          LOG_I(NR_RRC, "DU received NR_RAT_Type_eutra_nr UE capabilities Info through the UE Context Setup Request from the CU \n");
          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,
              ue_CapabilityRAT_ContainerList->list.array[i]->ue_CapabilityRAT_Container.buf,
              ue_CapabilityRAT_ContainerList->list.array[i]->ue_CapabilityRAT_Container.size,
              0,0);
          if(LOG_DEBUGFLAG(DEBUG_ASN1)){
            xer_fprint(stdout,&asn_DEF_NR_UE_MRDC_Capability,ue_context_p->ue_context.UE_Capability_MRDC);
          }

          if((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)){
            LOG_E(NR_RRC,PROTOCOL_NR_RRC_CTXT_FMT" Failed to decode nr UE capabilities (%zu bytes)\n",
              PROTOCOL_NR_RRC_CTXT_UE_ARGS(&ctxt),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 =
          ue_CapabilityRAT_ContainerList->list.array[i]->ue_CapabilityRAT_Container.size;
        }

        if(ue_CapabilityRAT_ContainerList->list.array[i]->rat_Type ==
        NR_RAT_Type_eutra){
          //TODO
        }
      }
    }
  }
3247

3248 3249 3250
  uint8_t drb_id_to_setup_start = 0;
  uint8_t nb_drb_to_setup = 0;
  long drb_priority[1] = {13}; // For now, we assume only one drb per pdu sessions with a default preiority (will be dynamique in future)
3251 3252 3253

  /* Configure SRB2 */
  NR_SRB_ToAddMod_t            *SRB2_config          = NULL;
3254 3255 3256 3257 3258 3259 3260 3261 3262 3263
  NR_SRB_ToAddModList_t        *SRB_configList       = NULL;
  uint8_t SRBs_before_new_addition = 0;

  if(req->srbs_to_be_setup_length>0){
    if(ue_context_p->ue_context.SRB_configList == NULL){
      LOG_W(NR_RRC, "The SRB list of the UE context is empty before the addition of new SRB at the DU \n");
      ue_context_p->ue_context.SRB_configList = CALLOC(1, sizeof(*ue_context_p->ue_context.SRB_configList));
    }
    SRB_configList = ue_context_p->ue_context.SRB_configList;
    SRBs_before_new_addition = SRB_configList->list.count;
3264 3265 3266 3267 3268 3269
    for (int i=0; i<req->srbs_to_be_setup_length; i++){
      SRB2_config = CALLOC(1, sizeof(*SRB2_config));
      SRB2_config->srb_Identity = req->srbs_to_be_setup[i].srb_id;
      ASN_SEQUENCE_ADD(&SRB_configList->list, SRB2_config);
    }
  }
3270

3271 3272
  /* Configure DRB */
  NR_DRB_ToAddMod_t            *DRB_config          = NULL;
3273 3274 3275 3276 3277 3278
  NR_DRB_ToAddModList_t        *DRB_configList      = NULL;
  if(req->drbs_to_be_setup_length>0){
    if(ue_context_p->ue_context.DRB_configList == NULL){
      ue_context_p->ue_context.DRB_configList = CALLOC(1, sizeof(*ue_context_p->ue_context.DRB_configList));
    }
    DRB_configList = ue_context_p->ue_context.DRB_configList;
3279
    nb_drb_to_setup = req->drbs_to_be_setup_length;
3280 3281 3282 3283 3284 3285 3286 3287 3288
    for (int i=0; i<req->drbs_to_be_setup_length; i++){
      DRB_config = CALLOC(1, sizeof(*DRB_config));
      DRB_config->drb_Identity = req->drbs_to_be_setup[i].drb_id;
      ASN_SEQUENCE_ADD(&DRB_configList->list, DRB_config);
      f1ap_drb_to_be_setup_t drb_p = req->drbs_to_be_setup[i];
      transport_layer_addr_t addr;
      memcpy(addr.buffer, &drb_p.up_ul_tnl[0].tl_address, sizeof(drb_p.up_ul_tnl[0].tl_address));
      addr.length=sizeof(drb_p.up_ul_tnl[0].tl_address)*8;
      extern instance_t DUuniqInstance;
3289
      if (!drb_id_to_setup_start) drb_id_to_setup_start = drb_p.drb_id;
3290 3291 3292 3293 3294 3295 3296 3297 3298 3299
      incoming_teid = newGtpuCreateTunnel(DUuniqInstance,
                                          req->rnti,
                                          drb_p.drb_id,
                                          drb_p.drb_id,
                                          drb_p.up_ul_tnl[0].teid,
                                          -1, // no qfi
                                          addr,
                                          drb_p.up_ul_tnl[0].port,
                                          DURecvCb,
                                          NULL);
3300
    }
3301
  }
3302

3303 3304
  NR_CellGroupConfig_t *cellGroupConfig = calloc(1, sizeof(NR_CellGroupConfig_t));
  if (req->srbs_to_be_setup_length > 0 || req->drbs_to_be_setup_length>0)
3305 3306
    // FIXME: fill_mastercellGroupConfig() won't fill the right priorities or
    // bearer IDs for the DRBs
3307
    fill_mastercellGroupConfig(cellGroupConfig, ue_context_p->ue_context.masterCellGroup, rrc->um_on_default_drb, SRB2_config ? 1 : 0, drb_id_to_setup_start, nb_drb_to_setup, drb_priority);
3308

3309
  apply_macrlc_config(rrc, ue_context_p, &ctxt);
3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321
  
  if(req->rrc_container_length > 0){
    mem_block_t *pdcp_pdu_p = get_free_mem_block(req->rrc_container_length, __func__);
    memcpy(&pdcp_pdu_p->data[0], req->rrc_container, req->rrc_container_length);
    du_rlc_data_req(&ctxt, 1, 0x00, 1, 1, 0, req->rrc_container_length, pdcp_pdu_p);
    LOG_I(F1AP, "Printing RRC Container of UE context setup request: \n");
    for (int j=0; j<req->rrc_container_length; j++){
      printf("%02x ", pdcp_pdu_p->data[j]);
    }
    printf("\n");
  }

3322 3323 3324
  /* Fill the UE context setup response ITTI message to send to F1AP */
  resp->gNB_CU_ue_id = req->gNB_CU_ue_id;
  resp->rnti = ctxt.rnti;
3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339
  if(DRB_configList){ 
    if(DRB_configList->list.count > 0){
      resp->drbs_to_be_setup = calloc(1,DRB_configList->list.count*sizeof(f1ap_drb_to_be_setup_t));
      resp->drbs_to_be_setup_length = DRB_configList->list.count;
      for (int i=0; i<DRB_configList->list.count; i++){
        resp->drbs_to_be_setup[i].drb_id = DRB_configList->list.array[i]->drb_Identity;
        resp->drbs_to_be_setup[i].rlc_mode = RLC_MODE_AM;
        resp->drbs_to_be_setup[i].up_dl_tnl[0].teid = incoming_teid;
        resp->drbs_to_be_setup[i].up_dl_tnl[0].tl_address = inet_addr(mac->eth_params_n.my_addr);
        resp->drbs_to_be_setup[i].up_dl_tnl_length = 1;
      }
    }
    else{
      LOG_W(NR_RRC, "No DRB added upon reception of F1 UE context setup request with a DRB to setup list\n");
    }
3340
  }
3341 3342 3343 3344 3345 3346 3347 3348 3349 3350
  if(SRB_configList){
    if(SRB_configList->list.count >0 && SRBs_before_new_addition < SRB_configList->list.count){
      resp->srbs_to_be_setup = calloc(1,req->srbs_to_be_setup_length*sizeof(f1ap_srb_to_be_setup_t));
      resp->srbs_to_be_setup_length = req->srbs_to_be_setup_length;
      for (int i=SRBs_before_new_addition; i<SRB_configList->list.count; i++){
        resp->srbs_to_be_setup[i-SRBs_before_new_addition].srb_id = SRB_configList->list.array[i]->srb_Identity;
      }
    }
    else{
      LOG_W(NR_RRC, "No SRB added upon reception of F1 UE Context setup request at the DU\n");
3351 3352 3353
    }
  }
  else{
3354
    LOG_W(NR_RRC, "No SRB added upon reception of F1 UE Context setup request at the DU\n");
3355
  }
3356
  /* fixme:
matzakos's avatar
matzakos committed
3357
   * Here we should be encoding the updates on cellgroupconfig based on the content of UE capabilities
3358
   */
matzakos's avatar
matzakos committed
3359 3360
  resp->du_to_cu_rrc_information = calloc(1, sizeof(du_to_cu_rrc_information_t));
  resp->du_to_cu_rrc_information->cellGroupConfig = calloc(1,1024);
3361 3362
  asn_enc_rval_t enc_rval = uper_encode_to_buffer(&asn_DEF_NR_CellGroupConfig,
                                NULL,
3363
                                ue_context_p->ue_context.masterCellGroup, //(void *)cellGroupConfig,
matzakos's avatar
matzakos committed
3364
                                resp->du_to_cu_rrc_information->cellGroupConfig,
3365
                                1024);
3366 3367 3368 3369
  if (enc_rval.encoded == -1) {
        LOG_E(F1AP,"Could not encode ue_context.masterCellGroup, failed element %s\n",enc_rval.failed_type->name);
        exit(-1);
  }
matzakos's avatar
matzakos committed
3370
  resp->du_to_cu_rrc_information->cellGroupConfig_length = (enc_rval.encoded+7)>>3;
3371
  free(cellGroupConfig);
3372
  itti_send_msg_to_task (TASK_DU_F1, ctxt.module_id, message_p);
3373 3374
}

3375 3376 3377
static void rrc_DU_process_ue_context_modification_request(MessageDef *msg_p, const char *msg_name, instance_t instance){

  f1ap_ue_context_setup_t * req=&F1AP_UE_CONTEXT_MODIFICATION_REQ(msg_p);
3378 3379 3380 3381 3382 3383 3384
  protocol_ctxt_t ctxt = {
    .rnti = req->rnti,
    .module_id = instance,
    .instance  = instance,
    .enb_flag  = 1,
    .eNB_index = instance
  };
3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430
  gNB_RRC_INST *rrc = RC.nrrrc[ctxt.module_id];
  gNB_MAC_INST *mac = RC.nrmac[ctxt.module_id];
  struct rrc_gNB_ue_context_s *ue_context_p =
      rrc_gNB_get_ue_context(rrc, ctxt.rnti);
  MessageDef *message_p;
  message_p = itti_alloc_new_message (TASK_RRC_GNB, 0, F1AP_UE_CONTEXT_MODIFICATION_RESP);
  f1ap_ue_context_setup_t * resp=&F1AP_UE_CONTEXT_MODIFICATION_RESP(message_p);
  uint32_t incoming_teid = 0;
  NR_CellGroupConfig_t *cellGroupConfig = NULL;

  /* Configure SRB2 */
  NR_SRB_ToAddMod_t            *SRB2_config          = NULL;
  NR_SRB_ToAddModList_t        *SRB_configList       = NULL;
  uint8_t SRBs_before_new_addition = 0;

  if(req->srbs_to_be_setup_length>0){
    if(ue_context_p->ue_context.SRB_configList == NULL){
      LOG_W(NR_RRC, "The SRB list of the UE context is empty before the addition of new SRB at the DU \n");
      ue_context_p->ue_context.SRB_configList = CALLOC(1, sizeof(*ue_context_p->ue_context.SRB_configList));
    }
    SRB_configList = ue_context_p->ue_context.SRB_configList;
    SRBs_before_new_addition = SRB_configList->list.count;
    for (int i=0; i<req->srbs_to_be_setup_length; i++){
      SRB2_config = CALLOC(1, sizeof(*SRB2_config));
      SRB2_config->srb_Identity = req->srbs_to_be_setup[i].srb_id;
      ASN_SEQUENCE_ADD(&SRB_configList->list, SRB2_config);
    }
  }

  /* Configure DRB */
  NR_DRB_ToAddMod_t            *DRB_config          = NULL;
  NR_DRB_ToAddModList_t        *DRB_configList      = NULL;
  if(req->drbs_to_be_setup_length>0){
    if(ue_context_p->ue_context.DRB_configList == NULL){
      ue_context_p->ue_context.DRB_configList = CALLOC(1, sizeof(*ue_context_p->ue_context.DRB_configList));
    }
    DRB_configList = ue_context_p->ue_context.DRB_configList;
    for (int i=0; i<req->drbs_to_be_setup_length; i++){
      DRB_config = CALLOC(1, sizeof(*DRB_config));
      DRB_config->drb_Identity = req->drbs_to_be_setup[i].drb_id;
      ASN_SEQUENCE_ADD(&DRB_configList->list, DRB_config);
      f1ap_drb_to_be_setup_t drb_p = req->drbs_to_be_setup[i];
      transport_layer_addr_t addr;
      memcpy(addr.buffer, &drb_p.up_ul_tnl[0].tl_address, sizeof(drb_p.up_ul_tnl[0].tl_address));
      addr.length=sizeof(drb_p.up_ul_tnl[0].tl_address)*8;
      extern instance_t DUuniqInstance;
3431 3432 3433 3434 3435 3436 3437 3438 3439 3440
      incoming_teid = newGtpuCreateTunnel(DUuniqInstance,
                                          req->rnti,
                                          drb_p.drb_id,
                                          drb_p.drb_id,
                                          drb_p.up_ul_tnl[0].teid,
                                          -1, // no qfi
                                          addr,
                                          drb_p.up_ul_tnl[0].port,
                                          DURecvCb,
                                          NULL);
3441 3442 3443 3444 3445
    }
  }

  if(req->srbs_to_be_setup_length>0 || req->drbs_to_be_setup_length>0){
    cellGroupConfig = calloc(1, sizeof(NR_CellGroupConfig_t));
3446 3447 3448 3449 3450 3451 3452 3453 3454
    uint8_t drb_id_to_setup_start = 1;
    long drb_priority[1] = {13}; // For now, we assume only one drb per pdu sessions with a default preiority (will be dynamique in future)
    fill_mastercellGroupConfig(cellGroupConfig,
                               ue_context_p->ue_context.masterCellGroup,
                               rrc->um_on_default_drb,
                               drb_id_to_setup_start < 2 ? 1 : 0,
                               drb_id_to_setup_start,
                               req->drbs_to_be_setup_length,
                               drb_priority);
3455
     apply_macrlc_config(rrc, ue_context_p, &ctxt);
3456 3457 3458 3459 3460 3461
  }
  if(req->ReconfigComplOutcome == RRCreconf_failure){
    LOG_W(NR_RRC, "CU reporting RRC Reconfiguration failure \n");
  }
  else if(req->ReconfigComplOutcome == RRCreconf_success){
    LOG_I(NR_RRC, "CU reporting RRC Reconfiguration success \n");
3462 3463 3464 3465
    if(ue_context_p->ue_context.DRB_configList!=NULL){
      LOG_I(NR_RRC, "Send first DDD buffer status reporting towards the CU through an ITTI message to gtp-u \n");
      uint8_t drb_id = ue_context_p->ue_context.DRB_configList->list.array[0]->drb_Identity;
      rnti_t rnti   = ue_context_p->ue_context.rnti;
3466
      int rlc_tx_buffer_space = nr_rlc_get_available_tx_space(rnti, drb_id);
3467 3468 3469 3470 3471 3472
      LOG_I(NR_RRC, "Reported in DDD drb_id:%d, rnti:%d\n", drb_id, rnti);
      MessageDef *msg = itti_alloc_new_message_sized(TASK_RRC_GNB, 0, GTPV1U_DU_BUFFER_REPORT_REQ,
                                     sizeof(gtpv1u_gnb_tunnel_data_req_t));
      gtpv1u_DU_buffer_report_req_t *req=&GTPV1U_DU_BUFFER_REPORT_REQ(msg);
      req->pdusession_id = drb_id;
      req->rnti = rnti;
3473
      req->buffer_availability = rlc_tx_buffer_space; //10000000; //Hardcoding to be removed and read the actual RLC buffer availability instead
3474 3475 3476 3477
      extern instance_t DUuniqInstance;
      itti_send_msg_to_task(TASK_GTPV1_U, DUuniqInstance, msg);

    }
3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514
  }

  /* Fill the UE context setup response ITTI message to send to F1AP */
  resp->gNB_CU_ue_id = req->gNB_CU_ue_id;
  resp->rnti = ctxt.rnti;
  if(DRB_configList){
    if(DRB_configList->list.count > 0){
      resp->drbs_to_be_setup = calloc(1,DRB_configList->list.count*sizeof(f1ap_drb_to_be_setup_t));
      resp->drbs_to_be_setup_length = DRB_configList->list.count;
      for (int i=0; i<DRB_configList->list.count; i++){
        resp->drbs_to_be_setup[i].drb_id = DRB_configList->list.array[i]->drb_Identity;
        resp->drbs_to_be_setup[i].rlc_mode = RLC_MODE_AM;
        resp->drbs_to_be_setup[i].up_dl_tnl[0].teid = incoming_teid;
        resp->drbs_to_be_setup[i].up_dl_tnl[0].tl_address = inet_addr(mac->eth_params_n.my_addr);
        resp->drbs_to_be_setup[i].up_dl_tnl_length = 1;
      }
    }
    else{
      LOG_W(NR_RRC, "No DRB added upon reception of F1 UE context modification request with a DRB to setup list\n");
    }
  }
  if(SRB_configList){
    if(SRB_configList->list.count >0 && SRBs_before_new_addition < SRB_configList->list.count){
      resp->srbs_to_be_setup = calloc(1,req->srbs_to_be_setup_length*sizeof(f1ap_srb_to_be_setup_t));
      resp->srbs_to_be_setup_length = req->srbs_to_be_setup_length;
      for (int i=SRBs_before_new_addition; i<SRB_configList->list.count; i++){
        resp->srbs_to_be_setup[i-SRBs_before_new_addition].srb_id = SRB_configList->list.array[i]->srb_Identity;
      }
    }
    else{
      LOG_W(NR_RRC, "No SRB added upon reception of F1 UE Context modification request at the DU\n");
    }
  }
  else{
    LOG_W(NR_RRC, "No SRB added upon reception of F1 UE Context modification request at the DU\n");
  }

3515
  //if(cellGroupConfig != NULL) {
matzakos's avatar
matzakos committed
3516 3517
    resp->du_to_cu_rrc_information = calloc(1,sizeof(du_to_cu_rrc_information_t));
    resp->du_to_cu_rrc_information->cellGroupConfig = calloc(1,1024);
3518 3519
    asn_enc_rval_t enc_rval = uper_encode_to_buffer(&asn_DEF_NR_CellGroupConfig,
                                NULL,
3520
                                ue_context_p->ue_context.masterCellGroup, //(void *)cellGroupConfig,
matzakos's avatar
matzakos committed
3521
                                resp->du_to_cu_rrc_information->cellGroupConfig,
3522
                                1024);
matzakos's avatar
matzakos committed
3523
    resp->du_to_cu_rrc_information->cellGroupConfig_length = (enc_rval.encoded+7)>>3;
3524
  //}
3525 3526 3527
  itti_send_msg_to_task (TASK_DU_F1, ctxt.module_id, message_p);
}

3528 3529
static void rrc_CU_process_ue_context_setup_response(MessageDef *msg_p, const char *msg_name, instance_t instance){

3530
  f1ap_ue_context_setup_t * resp=&F1AP_UE_CONTEXT_SETUP_RESP(msg_p);
3531 3532 3533 3534 3535 3536 3537
  protocol_ctxt_t ctxt = {
    .rnti = resp->rnti,
    .module_id = instance,
    .instance  = instance,
    .enb_flag  = 1,
    .eNB_index = instance
  };
3538 3539 3540 3541 3542 3543 3544
  gNB_RRC_INST *rrc = RC.nrrrc[ctxt.module_id];
  struct rrc_gNB_ue_context_s *ue_context_p = rrc_gNB_get_ue_context(rrc, ctxt.rnti);
  NR_CellGroupConfig_t *cellGroupConfig = NULL;

  asn_dec_rval_t dec_rval = uper_decode_complete( NULL,
    &asn_DEF_NR_CellGroupConfig,
    (void **)&cellGroupConfig,
matzakos's avatar
matzakos committed
3545 3546
    (uint8_t *)resp->du_to_cu_rrc_information->cellGroupConfig,
    (int) resp->du_to_cu_rrc_information->cellGroupConfig_length);
3547 3548 3549 3550 3551 3552 3553

  if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) {
    AssertFatal(1==0,"Cell group config decode error\n");
    // free the memory
    SEQUENCE_free( &asn_DEF_NR_CellGroupConfig, cellGroupConfig, 1 );
    return;
  }
3554
  //xer_fprint(stdout,&asn_DEF_NR_CellGroupConfig, cellGroupConfig);
3555 3556 3557 3558 3559 3560

  if(ue_context_p->ue_context.masterCellGroup == NULL){
    ue_context_p->ue_context.masterCellGroup = calloc(1, sizeof(NR_CellGroupConfig_t));
  }
  if(cellGroupConfig->rlc_BearerToAddModList!=NULL){
    if(ue_context_p->ue_context.masterCellGroup->rlc_BearerToAddModList != NULL){
3561 3562 3563 3564 3565
      int ue_ctxt_rlc_Bearers = ue_context_p->ue_context.masterCellGroup->rlc_BearerToAddModList->list.count;
      for(int i=ue_ctxt_rlc_Bearers; i<ue_ctxt_rlc_Bearers + cellGroupConfig->rlc_BearerToAddModList->list.count; i++){
        ASN_SEQUENCE_ADD(&ue_context_p->ue_context.masterCellGroup->rlc_BearerToAddModList->list,
          cellGroupConfig->rlc_BearerToAddModList->list.array[i-ue_ctxt_rlc_Bearers]);
      }
3566
    }
3567 3568 3569 3570
    else{
      LOG_W(NR_RRC, "Empty rlc_BearerToAddModList at ue_context of the CU before filling the updates from UE context setup response \n");
      ue_context_p->ue_context.masterCellGroup->rlc_BearerToAddModList = calloc(1, sizeof(*cellGroupConfig->rlc_BearerToAddModList));
      memcpy(ue_context_p->ue_context.masterCellGroup->rlc_BearerToAddModList, cellGroupConfig->rlc_BearerToAddModList,
3571
        sizeof(*cellGroupConfig->rlc_BearerToAddModList));
3572
    }
3573
  }
3574
  xer_fprint(stdout,&asn_DEF_NR_CellGroupConfig, ue_context_p->ue_context.masterCellGroup);
3575

3576 3577 3578
  if (ue_context_p->ue_context.established_pdu_sessions_flag == 1) {
    rrc_gNB_generate_dedicatedRRCReconfiguration(&ctxt, ue_context_p, cellGroupConfig);
  } else {
3579
    rrc_gNB_generate_defaultRRCReconfiguration(&ctxt, ue_context_p);
3580 3581
  }

3582 3583 3584 3585 3586
  free(cellGroupConfig->rlc_BearerToAddModList);
  free(cellGroupConfig);

}

3587
static void rrc_CU_process_ue_context_modification_response(MessageDef *msg_p, const char *msg_name, instance_t instance){
3588 3589

  f1ap_ue_context_setup_t * resp=&F1AP_UE_CONTEXT_SETUP_RESP(msg_p);
3590 3591 3592 3593 3594 3595 3596
  protocol_ctxt_t ctxt = {
    .rnti = resp->rnti,
    .module_id = instance,
    .instance  = instance,
    .enb_flag  = 1,
    .eNB_index = instance
  };
3597 3598 3599 3600
  gNB_RRC_INST *rrc = RC.nrrrc[ctxt.module_id];
  struct rrc_gNB_ue_context_s *ue_context_p = rrc_gNB_get_ue_context(rrc, ctxt.rnti);
  NR_CellGroupConfig_t *cellGroupConfig = NULL;

matzakos's avatar
matzakos committed
3601
  if(resp->du_to_cu_rrc_information->cellGroupConfig!=NULL){
3602 3603 3604
    asn_dec_rval_t dec_rval = uper_decode_complete( NULL,
      &asn_DEF_NR_CellGroupConfig,
      (void **)&cellGroupConfig,
matzakos's avatar
matzakos committed
3605 3606
      (uint8_t *)resp->du_to_cu_rrc_information->cellGroupConfig,
      (int) resp->du_to_cu_rrc_information->cellGroupConfig_length);
3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618

    if((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) {
      AssertFatal(1==0,"Cell group config decode error\n");
      // free the memory
      SEQUENCE_free( &asn_DEF_NR_CellGroupConfig, cellGroupConfig, 1 );
      return;
    }
    //xer_fprint(stdout,&asn_DEF_NR_CellGroupConfig, cellGroupConfig);

    if(ue_context_p->ue_context.masterCellGroup == NULL){
      ue_context_p->ue_context.masterCellGroup = calloc(1, sizeof(NR_CellGroupConfig_t));
    }
matzakos's avatar
matzakos committed
3619

3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633
    if(cellGroupConfig->rlc_BearerToAddModList!=NULL){
      if(ue_context_p->ue_context.masterCellGroup->rlc_BearerToAddModList != NULL){
        int ue_ctxt_rlc_Bearers = ue_context_p->ue_context.masterCellGroup->rlc_BearerToAddModList->list.count;
        for(int i=ue_ctxt_rlc_Bearers; i<ue_ctxt_rlc_Bearers + cellGroupConfig->rlc_BearerToAddModList->list.count; i++){
          ASN_SEQUENCE_ADD(&ue_context_p->ue_context.masterCellGroup->rlc_BearerToAddModList->list,
              cellGroupConfig->rlc_BearerToAddModList->list.array[i-ue_ctxt_rlc_Bearers]);
        }
      }
      else{
        LOG_W(NR_RRC, "Empty rlc_BearerToAddModList at ue_context of the CU before filling the updates from UE context setup response \n");
        ue_context_p->ue_context.masterCellGroup->rlc_BearerToAddModList = calloc(1, sizeof(*cellGroupConfig->rlc_BearerToAddModList));
        memcpy(ue_context_p->ue_context.masterCellGroup->rlc_BearerToAddModList, cellGroupConfig->rlc_BearerToAddModList,
          sizeof(*cellGroupConfig->rlc_BearerToAddModList));
      }
3634
    }
3635
    LOG_I(NR_RRC, "Updated master cell group configuration stored at the UE context of the CU:\n");
3636 3637 3638 3639 3640 3641 3642
    xer_fprint(stdout,&asn_DEF_NR_CellGroupConfig, ue_context_p->ue_context.masterCellGroup);

    rrc_gNB_generate_dedicatedRRCReconfiguration(&ctxt, ue_context_p, cellGroupConfig);

    free(cellGroupConfig->rlc_BearerToAddModList);
    free(cellGroupConfig);
  }
3643 3644
}

3645 3646 3647 3648
unsigned int mask_flip(unsigned int x) {
  return((((x>>8) + (x<<8))&0xffff)>>6);
}

3649 3650
static unsigned int get_dl_bw_mask(const gNB_RRC_INST *rrc, const NR_UE_NR_Capability_t *cap)
{
3651
  int common_band = *rrc->carrier.servingcellconfigcommon->downlinkConfigCommon->frequencyInfoDL->frequencyBandList.list.array[0];
3652
  int common_scs  = rrc->carrier.servingcellconfigcommon->downlinkConfigCommon->frequencyInfoDL->scs_SpecificCarrierList.list.array[0]->subcarrierSpacing;
3653 3654 3655 3656 3657 3658
  for (int i=0;i<cap->rf_Parameters.supportedBandListNR.list.count;i++) {
     NR_BandNR_t *bandNRinfo = cap->rf_Parameters.supportedBandListNR.list.array[i];
     if (bandNRinfo->bandNR == common_band) {
       if (common_band < 257) { // FR1
          switch (common_scs) {
            case NR_SubcarrierSpacing_kHz15 :
3659
               if (bandNRinfo->channelBWs_DL &&
3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680
                   bandNRinfo->channelBWs_DL->choice.fr1 &&
                   bandNRinfo->channelBWs_DL->choice.fr1->scs_15kHz)
                     return(mask_flip((unsigned int)*(uint16_t*)bandNRinfo->channelBWs_DL->choice.fr1->scs_15kHz->buf));
 	      break;
            case NR_SubcarrierSpacing_kHz30 :
               if (bandNRinfo->channelBWs_DL &&
                   bandNRinfo->channelBWs_DL->choice.fr1 &&
                   bandNRinfo->channelBWs_DL->choice.fr1->scs_30kHz)
                     return(mask_flip((unsigned int)*(uint16_t*)bandNRinfo->channelBWs_DL->choice.fr1->scs_30kHz->buf));
              break;
            case NR_SubcarrierSpacing_kHz60 :
               if (bandNRinfo->channelBWs_DL &&
                   bandNRinfo->channelBWs_DL->choice.fr1 &&
                   bandNRinfo->channelBWs_DL->choice.fr1->scs_60kHz)
                     return(mask_flip((unsigned int)*(uint16_t*)bandNRinfo->channelBWs_DL->choice.fr1->scs_60kHz->buf));
              break;
          }
       }
       else {
          switch (common_scs) {
            case NR_SubcarrierSpacing_kHz60 :
3681
               if (bandNRinfo->channelBWs_DL &&
3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698
                   bandNRinfo->channelBWs_DL->choice.fr2 &&
                   bandNRinfo->channelBWs_DL->choice.fr2->scs_60kHz)
                     return(mask_flip((unsigned int)*(uint16_t*)bandNRinfo->channelBWs_DL->choice.fr2->scs_60kHz->buf));
              break;
            case NR_SubcarrierSpacing_kHz120 :
               if (bandNRinfo->channelBWs_DL &&
                   bandNRinfo->channelBWs_DL->choice.fr2 &&
                   bandNRinfo->channelBWs_DL->choice.fr2->scs_120kHz)
                     return(mask_flip((unsigned int)*(uint16_t*)bandNRinfo->channelBWs_DL->choice.fr2->scs_120kHz->buf));
              break;
       }
     }
   }
  }
  return(0);
}

3699 3700
static unsigned int get_ul_bw_mask(const gNB_RRC_INST *rrc, const NR_UE_NR_Capability_t *cap)
{
3701
  int common_band = *rrc->carrier.servingcellconfigcommon->uplinkConfigCommon->frequencyInfoUL->frequencyBandList->list.array[0];
3702
  int common_scs  = rrc->carrier.servingcellconfigcommon->uplinkConfigCommon->frequencyInfoUL->scs_SpecificCarrierList.list.array[0]->subcarrierSpacing;
3703 3704 3705 3706 3707 3708
  for (int i=0;i<cap->rf_Parameters.supportedBandListNR.list.count;i++) {
     NR_BandNR_t *bandNRinfo = cap->rf_Parameters.supportedBandListNR.list.array[i];
     if (bandNRinfo->bandNR == common_band) {
       if (common_band < 257) { // FR1
          switch (common_scs) {
            case NR_SubcarrierSpacing_kHz15 :
3709
               if (bandNRinfo->channelBWs_UL &&
3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730
                   bandNRinfo->channelBWs_UL->choice.fr1 &&
                   bandNRinfo->channelBWs_UL->choice.fr1->scs_15kHz)
                     return(mask_flip((unsigned int)*(uint16_t*)bandNRinfo->channelBWs_UL->choice.fr1->scs_15kHz->buf));
 	      break;
            case NR_SubcarrierSpacing_kHz30 :
               if (bandNRinfo->channelBWs_UL &&
                   bandNRinfo->channelBWs_UL->choice.fr1 &&
                   bandNRinfo->channelBWs_UL->choice.fr1->scs_30kHz)
                     return(mask_flip((unsigned int)*(uint16_t*)bandNRinfo->channelBWs_UL->choice.fr1->scs_30kHz->buf));
              break;
            case NR_SubcarrierSpacing_kHz60 :
               if (bandNRinfo->channelBWs_UL &&
                   bandNRinfo->channelBWs_UL->choice.fr1 &&
                   bandNRinfo->channelBWs_UL->choice.fr1->scs_60kHz)
                     return(mask_flip((unsigned int)*(uint16_t*)bandNRinfo->channelBWs_UL->choice.fr1->scs_60kHz->buf));
              break;
          }
       }
       else {
          switch (common_scs) {
            case NR_SubcarrierSpacing_kHz60 :
3731
               if (bandNRinfo->channelBWs_UL &&
3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748
                   bandNRinfo->channelBWs_UL->choice.fr2 &&
                   bandNRinfo->channelBWs_UL->choice.fr2->scs_60kHz)
                     return(mask_flip((unsigned int)*(uint16_t*)bandNRinfo->channelBWs_UL->choice.fr2->scs_60kHz->buf));
              break;
            case NR_SubcarrierSpacing_kHz120 :
               if (bandNRinfo->channelBWs_UL &&
                   bandNRinfo->channelBWs_UL->choice.fr2 &&
                   bandNRinfo->channelBWs_UL->choice.fr2->scs_120kHz)
                     return(mask_flip((unsigned int)*(uint16_t*)bandNRinfo->channelBWs_UL->choice.fr2->scs_120kHz->buf));
              break;
       }
     }
   }
  }
  return(0);
}

3749 3750
static int get_ul_mimo_layersCB(const gNB_RRC_INST *rrc, const NR_UE_NR_Capability_t *cap)
{
3751
  int common_scs  = rrc->carrier.servingcellconfigcommon->uplinkConfigCommon->frequencyInfoUL->scs_SpecificCarrierList.list.array[0]->subcarrierSpacing;
3752

3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766
  // check featureSet
  NR_FeatureSets_t *fs=cap->featureSets;
  if (fs) {
    // go through UL feature sets and look for one with current SCS
    for (int i=0;i<fs->featureSetsUplinkPerCC->list.count;i++) {
       if (fs->featureSetsUplinkPerCC->list.array[i]->supportedSubcarrierSpacingUL == common_scs &&
           fs->featureSetsUplinkPerCC->list.array[i]->mimo_CB_PUSCH &&
           fs->featureSetsUplinkPerCC->list.array[i]->mimo_CB_PUSCH->maxNumberMIMO_LayersCB_PUSCH)
           return(1<<*fs->featureSetsUplinkPerCC->list.array[i]->mimo_CB_PUSCH->maxNumberMIMO_LayersCB_PUSCH);
    }
  }
  return(1);
}

3767 3768
static int get_ul_mimo_layers(const gNB_RRC_INST *rrc, const NR_UE_NR_Capability_t *cap)
{
3769
  int common_scs  = rrc->carrier.servingcellconfigcommon->uplinkConfigCommon->frequencyInfoUL->scs_SpecificCarrierList.list.array[0]->subcarrierSpacing;
3770

3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783
  // check featureSet
  NR_FeatureSets_t *fs=cap->featureSets;
  if (fs) {
    // go through UL feature sets and look for one with current SCS
    for (int i=0;i<fs->featureSetsUplinkPerCC->list.count;i++) {
       if (fs->featureSetsUplinkPerCC->list.array[i]->supportedSubcarrierSpacingUL == common_scs &&
           fs->featureSetsUplinkPerCC->list.array[i]->maxNumberMIMO_LayersNonCB_PUSCH)
           return(1<<*fs->featureSetsUplinkPerCC->list.array[i]->maxNumberMIMO_LayersNonCB_PUSCH);
    }
  }
  return(1);
}

3784 3785
static int get_dl_mimo_layers(const gNB_RRC_INST *rrc, const NR_UE_NR_Capability_t *cap)
{
3786
  int common_scs  = rrc->carrier.servingcellconfigcommon->downlinkConfigCommon->frequencyInfoDL->scs_SpecificCarrierList.list.array[0]->subcarrierSpacing;
3787

3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799
  // check featureSet
  NR_FeatureSets_t *fs=cap->featureSets;
  if (fs) {
    // go through UL feature sets and look for one with current SCS
    for (int i=0;i<fs->featureSetsDownlinkPerCC->list.count;i++) {
       if (fs->featureSetsUplinkPerCC->list.array[i]->supportedSubcarrierSpacingUL == common_scs &&
           fs->featureSetsDownlinkPerCC->list.array[i]->maxNumberMIMO_LayersPDSCH)
           return(2<<*fs->featureSetsDownlinkPerCC->list.array[i]->maxNumberMIMO_LayersPDSCH);
    }
  }
  return(1);
}
3800

Cedric Roux's avatar
Cedric Roux committed
3801
void nr_rrc_subframe_process(protocol_ctxt_t *const ctxt_pP, const int CC_id) {
rmagueta's avatar
rmagueta committed
3802

Cedric Roux's avatar
Cedric Roux committed
3803
  MessageDef *msg;
3804 3805 3806
  rrc_gNB_ue_context_t *ue_context_p = NULL;
  RB_FOREACH(ue_context_p, rrc_nr_ue_tree_s, &(RC.nrrrc[ctxt_pP->module_id]->rrc_ue_head)) {
    ctxt_pP->rnti = ue_context_p->ue_id_rnti;
Laurent THOMAS's avatar
Laurent THOMAS committed
3807
    gNB_MAC_INST *nrmac=RC.nrmac[ctxt_pP->module_id]; //WHAT A BEAUTIFULL RACE CONDITION !!!
3808 3809 3810 3811 3812 3813 3814 3815

    if (ue_context_p->ue_context.ul_failure_timer > 0) {
      ue_context_p->ue_context.ul_failure_timer++;

      if (ue_context_p->ue_context.ul_failure_timer >= 20000) {
        // remove UE after 20 seconds after MAC (or else) has indicated UL failure
        LOG_I(RRC, "Removing UE %x instance, because of uplink failure timer timeout\n",
              ue_context_p->ue_context.rnti);
3816
        if(ue_context_p->ue_context.StatusRrc >= NR_RRC_CONNECTED){
3817 3818 3819 3820
          rrc_gNB_send_NGAP_UE_CONTEXT_RELEASE_REQ(
                   ctxt_pP->module_id,
                   ue_context_p,
                   NGAP_CAUSE_RADIO_NETWORK,
3821
                   NGAP_CAUSE_RADIO_NETWORK_RADIO_CONNECTION_WITH_UE_LOST);
3822 3823 3824 3825
        }

        // Remove here the MAC and RRC context when RRC is not connected or gNB is not connected to CN5G
        if(ue_context_p->ue_context.StatusRrc < NR_RRC_CONNECTED || ue_context_p->ue_context.gNB_ue_ngap_id == 0) {
3826
          if(!NODE_IS_CU(RC.nrrrc[ctxt_pP->instance]->node_type)){
3827
            mac_remove_nr_ue(nrmac, ctxt_pP->rnti);
3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846
            rrc_rlc_remove_ue(ctxt_pP);
            pdcp_remove_UE(ctxt_pP);

            /* remove RRC UE Context */
            ue_context_p = rrc_gNB_get_ue_context(RC.nrrrc[ctxt_pP->module_id], ctxt_pP->rnti);
            if (ue_context_p) {
              rrc_gNB_remove_ue_context(ctxt_pP, RC.nrrrc[ctxt_pP->module_id], ue_context_p);
              LOG_I(NR_RRC, "remove UE %x \n", ctxt_pP->rnti);
            }
          }
          // In case of CU trigger UE context release command towards the DU
          else{
            MessageDef *message_p;
            message_p = itti_alloc_new_message (TASK_RRC_GNB, 0, F1AP_UE_CONTEXT_RELEASE_CMD);
            f1ap_ue_context_release_cmd_t *rel_cmd=&F1AP_UE_CONTEXT_RELEASE_CMD (message_p);
            rel_cmd->rnti = ctxt_pP->rnti;
            rel_cmd->cause = F1AP_CAUSE_RADIO_NETWORK;
            rel_cmd->cause_value = 10; // 10 = F1AP_CauseRadioNetwork_normal_release
            itti_send_msg_to_task(TASK_CU_F1, ctxt_pP->module_id, message_p);
3847 3848
          }
        }
3849

3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861
        break; // break RB_FOREACH
      }
    }

    if (ue_context_p->ue_context.ue_release_timer_rrc > 0) {
      ue_context_p->ue_context.ue_release_timer_rrc++;

      if (ue_context_p->ue_context.ue_release_timer_rrc >= ue_context_p->ue_context.ue_release_timer_thres_rrc) {
        LOG_I(NR_RRC, "Removing UE %x instance after UE_CONTEXT_RELEASE_Complete (ue_release_timer_rrc timeout)\n",
              ue_context_p->ue_context.rnti);
        ue_context_p->ue_context.ue_release_timer_rrc = 0;

Laurent THOMAS's avatar
Laurent THOMAS committed
3862
        mac_remove_nr_ue(nrmac, ctxt_pP->rnti);
3863 3864
        rrc_rlc_remove_ue(ctxt_pP);
        pdcp_remove_UE(ctxt_pP);
3865
        newGtpuDeleteAllTunnels(ctxt_pP->instance, ctxt_pP->rnti);
3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878

        /* remove RRC UE Context */
        ue_context_p = rrc_gNB_get_ue_context(RC.nrrrc[ctxt_pP->module_id], ctxt_pP->rnti);
        if (ue_context_p) {
          rrc_gNB_remove_ue_context(ctxt_pP, RC.nrrrc[ctxt_pP->module_id], ue_context_p);
          LOG_I(NR_RRC, "remove UE %x \n", ctxt_pP->rnti);
        }

        break; // break RB_FOREACH
      }
    }
  }

Cedric Roux's avatar
Cedric Roux committed
3879
  /* send a tick to x2ap */
3880
  if (is_x2ap_enabled()){
3881
    msg = itti_alloc_new_message(TASK_RRC_ENB, 0, X2AP_SUBFRAME_PROCESS);
3882 3883
    itti_send_msg_to_task(TASK_X2AP, ctxt_pP->module_id, msg);
  }
Cedric Roux's avatar
Cedric Roux committed
3884 3885
}

3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921
static void write_rrc_stats(const gNB_RRC_INST *rrc)
{
  const char *filename = "nrRRC_stats.log";
  FILE *f = fopen(filename, "w");
  if (f == NULL) {
    LOG_E(NR_RRC, "cannot open %s for writing\n", filename);
    return;
  }

  rrc_gNB_ue_context_t *ue_context_p = NULL;
  /* cast is necessary to eliminate warning "discards ‘const’ qualifier" */
  RB_FOREACH(ue_context_p, rrc_nr_ue_tree_s, &((gNB_RRC_INST *)rrc)->rrc_ue_head) {
    const rnti_t rnti = ue_context_p->ue_id_rnti;
    const gNB_RRC_UE_t *ue_ctxt = &ue_context_p->ue_context;

    fprintf(f, "NR RRC UE rnti %04x:", rnti);

    if (ue_ctxt->Initialue_identity_5g_s_TMSI.presence)
      fprintf(f, " S-TMSI %x\n", ue_ctxt->Initialue_identity_5g_s_TMSI.fiveg_tmsi);

    fprintf(f, " failure timer %d/8\n", ue_ctxt->ul_failure_timer);

    if (ue_ctxt->UE_Capability_nr) {
      fprintf(f,
              "    UE cap: BW DL %x. BW UL %x, DL MIMO Layers %d UL MIMO Layers (CB) %d UL MIMO Layers (nonCB) %d\n",
              get_dl_bw_mask(rrc, ue_ctxt->UE_Capability_nr),
              get_ul_bw_mask(rrc, ue_ctxt->UE_Capability_nr),
              get_dl_mimo_layers(rrc, ue_ctxt->UE_Capability_nr),
              get_ul_mimo_layersCB(rrc, ue_ctxt->UE_Capability_nr),
              get_ul_mimo_layers(rrc, ue_ctxt->UE_Capability_nr));
    }
  }

  fclose(f);
}

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
3922 3923
///---------------------------------------------------------------------------------------------------------------///
///---------------------------------------------------------------------------------------------------------------///
3924
void *rrc_gnb_task(void *args_p) {
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
3925 3926
  MessageDef                         *msg_p;
  const char                         *msg_name_p;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
3927 3928
  instance_t                         instance;
  int                                result;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
3929 3930
  //SRB_INFO                           *srb_info_p;
  //int                                CC_id;
3931 3932 3933 3934 3935 3936 3937 3938 3939
  protocol_ctxt_t ctxt={.module_id=0,
                        .enb_flag=1,
                        .instance=0,
                        .rnti=0,
                        .frame=-1,
                        .subframe=-1,
                        .eNB_index=0,
                        .brOption=false
                       };
3940 3941 3942 3943 3944

  /* timer to write stats to file */
  long stats_timer_id = 1;
  timer_setup(1, 0, TASK_RRC_GNB, 0, TIMER_PERIODIC, NULL, &stats_timer_id);

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
3945 3946
  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
3947 3948

  while (1) {
3949
    // Wait for a message
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
3950
    itti_receive_msg(TASK_RRC_GNB, &msg_p);
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
3951
    msg_name_p = ITTI_MSG_NAME(msg_p);
3952
    instance = ITTI_MSG_DESTINATION_INSTANCE(msg_p);
Cedric Roux's avatar
Cedric Roux committed
3953

WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
3954
    switch (ITTI_MSG_ID(msg_p)) {
3955
      case TERMINATE_MESSAGE:
Xue Song's avatar
Xue Song committed
3956
        LOG_W(NR_RRC, " *** Exiting NR_RRC thread\n");
3957 3958
        itti_exit_task();
        break;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
3959

3960
      case MESSAGE_TEST:
3961
        LOG_I(NR_RRC, "[gNB %ld] Received %s\n", instance, msg_name_p);
3962
        break;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
3963

3964 3965 3966 3967 3968 3969
      case TIMER_HAS_EXPIRED:
        /* only this one handled for now */
        DevAssert(TIMER_HAS_EXPIRED(msg_p).timer_id == stats_timer_id);
        write_rrc_stats(RC.nrrrc[0]);
        break;

Cedric Roux's avatar
Cedric Roux committed
3970 3971 3972 3973
      case RRC_SUBFRAME_PROCESS:
        nr_rrc_subframe_process(&RRC_SUBFRAME_PROCESS(msg_p).ctxt, RRC_SUBFRAME_PROCESS(msg_p).CC_id);
        break;

3974 3975 3976 3977 3978
      case F1AP_INITIAL_UL_RRC_MESSAGE:
        AssertFatal(NODE_IS_CU(RC.nrrrc[instance]->node_type) || NODE_IS_MONOLITHIC(RC.nrrrc[instance]->node_type),
                    "should not receive F1AP_INITIAL_UL_RRC_MESSAGE, need call by CU!\n");
        rrc_gNB_process_initial_ul_rrc_message(&F1AP_INITIAL_UL_RRC_MESSAGE(msg_p));
        break;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
3979 3980

      /* Messages from PDCP */
3981
      case F1AP_UL_RRC_MESSAGE:
3982 3983 3984
        PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt,
                                      instance,
                                      GNB_FLAG_YES,
3985 3986 3987 3988 3989
                                      F1AP_UL_RRC_MESSAGE(msg_p).rnti,
                                      0,
                                      0);
        LOG_D(NR_RRC,"Decoding DCCH %d: ue %04x, inst %ld, ctxt %p, size %d\n",
                F1AP_UL_RRC_MESSAGE(msg_p).srb_id,
3990 3991 3992
                ctxt.rnti,
                instance,
                &ctxt,
3993
                F1AP_UL_RRC_MESSAGE(msg_p).rrc_container_length);
Xue Song's avatar
Xue Song committed
3994
        rrc_gNB_decode_dcch(&ctxt,
3995 3996 3997 3998
                            F1AP_UL_RRC_MESSAGE(msg_p).srb_id,
                            F1AP_UL_RRC_MESSAGE(msg_p).rrc_container,
                            F1AP_UL_RRC_MESSAGE(msg_p).rrc_container_length);
        free(F1AP_UL_RRC_MESSAGE(msg_p).rrc_container);
Xue Song's avatar
Xue Song committed
3999
        break;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
4000

Xue Song's avatar
Xue Song committed
4001 4002 4003 4004
      case NGAP_DOWNLINK_NAS:
        rrc_gNB_process_NGAP_DOWNLINK_NAS(msg_p, msg_name_p, instance, &rrc_gNB_mui);
        break;

4005 4006 4007 4008
      case NGAP_PDUSESSION_SETUP_REQ:
        rrc_gNB_process_NGAP_PDUSESSION_SETUP_REQ(msg_p, msg_name_p, instance);
        break;

4009 4010
      case NGAP_PDUSESSION_MODIFY_REQ:
        rrc_gNB_process_NGAP_PDUSESSION_MODIFY_REQ(msg_p, msg_name_p, instance);
4011 4012 4013 4014 4015 4016
        break;

      case NGAP_PDUSESSION_RELEASE_COMMAND:
        rrc_gNB_process_NGAP_PDUSESSION_RELEASE_COMMAND(msg_p, msg_name_p, instance);
        break;

4017 4018
      /* Messages from gNB app */
      case NRRRC_CONFIGURATION_REQ:
4019
        LOG_I(NR_RRC, "[gNB %ld] Received %s : %p\n", instance, msg_name_p,&NRRRC_CONFIGURATION_REQ(msg_p));
4020 4021 4022
        openair_rrc_gNB_configuration(GNB_INSTANCE_TO_MODULE_ID(instance), &NRRRC_CONFIGURATION_REQ(msg_p));
        break;

Xue Song's avatar
Xue Song committed
4023 4024 4025 4026
      /* Messages from F1AP task */
      case F1AP_SETUP_REQ:
        AssertFatal(NODE_IS_CU(RC.nrrrc[instance]->node_type),
                    "should not receive F1AP_SETUP_REQUEST, need call by CU!\n");
4027
        LOG_I(NR_RRC,"[gNB %ld] Received %s : %p\n", instance, msg_name_p, &F1AP_SETUP_REQ(msg_p));
Xue Song's avatar
Xue Song committed
4028 4029
        rrc_gNB_process_f1_setup_req(&F1AP_SETUP_REQ(msg_p));
        break;
4030
	
4031 4032 4033
      case NR_DU_RRC_DL_INDICATION:
        rrc_process_DU_DL(msg_p, msg_name_p, instance);
        break;
4034
      
4035 4036 4037
      case F1AP_UE_CONTEXT_SETUP_REQ:
        rrc_DU_process_ue_context_setup_request(msg_p, msg_name_p, instance);
        break;
4038

4039 4040 4041
      case F1AP_UE_CONTEXT_SETUP_RESP:
        rrc_CU_process_ue_context_setup_response(msg_p, msg_name_p, instance);
        break;
4042

4043 4044 4045 4046
      case F1AP_UE_CONTEXT_MODIFICATION_RESP:
        rrc_CU_process_ue_context_modification_response(msg_p, msg_name_p, instance);
        break;

4047 4048 4049
      case F1AP_UE_CONTEXT_MODIFICATION_REQ:
        rrc_DU_process_ue_context_modification_request(msg_p, msg_name_p, instance);
        break;
4050

4051 4052 4053 4054
      case F1AP_UE_CONTEXT_RELEASE_CMD:
        LOG_W(NR_RRC, "Received F1AP_UE_CONTEXT_RELEASE_CMD for processing at the RRC layer of the DU. Processing function "
            "implementation is pending\n");
        break;
Xue Song's avatar
Xue Song committed
4055

4056
      /* Messages from X2AP */
4057
      case X2AP_ENDC_SGNB_ADDITION_REQ:
4058 4059 4060
        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;
4061

4062
      case X2AP_ENDC_SGNB_RECONF_COMPLETE:
Melissa Elkadi's avatar
Melissa Elkadi committed
4063
        LOG_A(NR_RRC, "Handling of reconfiguration complete message at RRC gNB is pending \n");
4064
        break;
Xue Song's avatar
Xue Song committed
4065

4066 4067
      case NGAP_INITIAL_CONTEXT_SETUP_REQ:
        rrc_gNB_process_NGAP_INITIAL_CONTEXT_SETUP_REQ(msg_p, msg_name_p, instance);
yaojie's avatar
yaojie committed
4068
        break;
4069

Cedric Roux's avatar
Cedric Roux committed
4070 4071 4072 4073 4074 4075 4076 4077 4078
      case X2AP_ENDC_SGNB_RELEASE_REQUEST:
        LOG_I(NR_RRC, "Received ENDC sgNB release request from X2AP \n");
        rrc_gNB_process_release_request(GNB_INSTANCE_TO_MODULE_ID(instance), &X2AP_ENDC_SGNB_RELEASE_REQUEST(msg_p));
        break;

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

4079 4080 4081 4082 4083 4084 4085 4086
      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;

4087
      default:
4088
        LOG_E(NR_RRC, "[gNB %ld] Received unexpected message %s\n", instance, msg_name_p);
4089
        break;
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
4090 4091 4092 4093 4094 4095 4096
    }

    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
4097

heshanyun's avatar
heshanyun committed
4098 4099 4100 4101
//-----------------------------------------------------------------------------
void
rrc_gNB_generate_SecurityModeCommand(
  const protocol_ctxt_t *const ctxt_pP,
Xue Song's avatar
Xue Song committed
4102
  rrc_gNB_ue_context_t  *const ue_context_pP
heshanyun's avatar
heshanyun committed
4103 4104 4105 4106 4107
)
//-----------------------------------------------------------------------------
{
  uint8_t                             buffer[100];
  uint8_t                             size;
4108

heshanyun's avatar
heshanyun committed
4109 4110 4111 4112 4113 4114
  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
4115
           rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id),
heshanyun's avatar
heshanyun committed
4116 4117 4118 4119 4120 4121 4122
           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);
4123 4124 4125 4126

  switch (RC.nrrrc[ctxt_pP->module_id]->node_type) {
    case ngran_gNB_CU:
      // create an ITTI message
Xue Song's avatar
Xue Song committed
4127 4128
      memcpy(ue_context_pP->ue_context.Srb1.Srb_info.Tx_buffer.Payload, buffer, size);
      ue_context_pP->ue_context.Srb1.Srb_info.Tx_buffer.payload_size = size;
4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146

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

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

    case ngran_gNB:
      LOG_D(NR_RRC,
heshanyun's avatar
heshanyun committed
4147 4148 4149 4150 4151
        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);
rmagueta's avatar
rmagueta committed
4152
  LOG_D(NR_RRC,"calling rrc_data_req :securityModeCommand\n");
4153
  nr_rrc_data_req(ctxt_pP,
rmagueta's avatar
rmagueta committed
4154 4155 4156 4157 4158 4159
                  DCCH,
                  rrc_gNB_mui++,
                  SDU_CONFIRM_NO,
                  size,
                  buffer,
                  PDCP_TRANSMISSION_MODE_CONTROL);
4160
      break;
4161

4162 4163 4164
    default :
        LOG_W(NR_RRC, "Unknown node type %d\n", RC.nrrrc[ctxt_pP->module_id]->node_type);
  }
heshanyun's avatar
heshanyun committed
4165 4166
}

zhanghuiqin's avatar
zhanghuiqin committed
4167 4168 4169 4170 4171 4172 4173 4174 4175
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;
4176

zhanghuiqin's avatar
zhanghuiqin committed
4177 4178
  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
4179
  size = do_NR_SA_UECapabilityEnquiry(
zhanghuiqin's avatar
zhanghuiqin committed
4180 4181 4182
           ctxt_pP,
           buffer,
           rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id));
4183 4184 4185
  LOG_I(NR_RRC,
        PROTOCOL_NR_RRC_CTXT_UE_FMT" Logical Channel DL-DCCH, Generate NR UECapabilityEnquiry (bytes %d)\n",
        PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
zhanghuiqin's avatar
zhanghuiqin committed
4186
        size);
4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208
  switch (RC.nrrrc[ctxt_pP->module_id]->node_type) {
    case ngran_gNB_CU:
      nr_rrc_data_req(
        ctxt_pP,
        DCCH,
        rrc_gNB_mui++,
        SDU_CONFIRM_NO,
        size,
        buffer,
        PDCP_TRANSMISSION_MODE_CONTROL);
      break;

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

    case ngran_gNB:
      // rrc_mac_config_req_gNB
      LOG_D(NR_RRC,
        PROTOCOL_NR_RRC_CTXT_UE_FMT" --- PDCP_DATA_REQ/%d Bytes (NR UECapabilityEnquiry MUI %d) --->[PDCP][RB %02d]\n",
        PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
zhanghuiqin's avatar
zhanghuiqin committed
4209 4210 4211
        size,
        rrc_gNB_mui,
        DCCH);
rmagueta's avatar
rmagueta committed
4212 4213 4214 4215 4216 4217 4218
  nr_rrc_data_req(ctxt_pP,
                  DCCH,
                  rrc_gNB_mui++,
                  SDU_CONFIRM_NO,
                  size,
                  buffer,
                  PDCP_TRANSMISSION_MODE_CONTROL);
4219
  break;
4220 4221 4222 4223

    default :
        LOG_W(NR_RRC, "Unknown node type %d\n", RC.nrrrc[ctxt_pP->module_id]->node_type);
  }
zhanghuiqin's avatar
zhanghuiqin committed
4224
}
heshanyun's avatar
heshanyun committed
4225 4226 4227 4228 4229 4230 4231

//-----------------------------------------------------------------------------
/*
* Generate the RRC Connection Release to UE.
* If received, UE should switch to RRC_IDLE mode.
*/
void
Xue Song's avatar
Xue Song committed
4232
rrc_gNB_generate_RRCRelease(
heshanyun's avatar
heshanyun committed
4233
  const protocol_ctxt_t *const ctxt_pP,
Xue Song's avatar
Xue Song committed
4234
  rrc_gNB_ue_context_t  *const ue_context_pP
heshanyun's avatar
heshanyun committed
4235 4236 4237 4238 4239 4240
)
//-----------------------------------------------------------------------------
{
  uint8_t buffer[RRC_BUF_SIZE];
  uint16_t size = 0;

4241
  memset(buffer, 0, sizeof(buffer));
heshanyun's avatar
heshanyun committed
4242

4243 4244
  size = do_NR_RRCRelease(buffer, sizeof(buffer),
                          rrc_gNB_get_next_transaction_identifier(ctxt_pP->module_id));
heshanyun's avatar
heshanyun committed
4245 4246
  ue_context_pP->ue_context.ue_reestablishment_timer = 0;
  ue_context_pP->ue_context.ue_release_timer = 0;
4247 4248
  ue_context_pP->ue_context.ul_failure_timer = 0;
  ue_context_pP->ue_context.ue_release_timer_rrc = 0;
heshanyun's avatar
heshanyun committed
4249
  LOG_I(NR_RRC,
Xue Song's avatar
Xue Song committed
4250 4251
        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
4252 4253
        size);
  LOG_D(NR_RRC,
Xue Song's avatar
Xue Song committed
4254 4255
        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
4256 4257 4258 4259
        size,
        rrc_gNB_mui,
        DCCH);

4260
  if (NODE_IS_CU(RC.nrrrc[ctxt_pP->module_id]->node_type)) {
4261 4262
    uint8_t *message_buffer = itti_malloc (TASK_RRC_GNB, TASK_CU_F1, size);
    memcpy (message_buffer, buffer, size);
4263
    MessageDef *m = itti_alloc_new_message(TASK_RRC_GNB, 0, F1AP_UE_CONTEXT_RELEASE_CMD);
heshanyun's avatar
heshanyun committed
4264 4265 4266
    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
4267
    F1AP_UE_CONTEXT_RELEASE_CMD(m).rrc_container = message_buffer;
heshanyun's avatar
heshanyun committed
4268 4269 4270
    F1AP_UE_CONTEXT_RELEASE_CMD(m).rrc_container_length = size;
    itti_send_msg_to_task(TASK_CU_F1, ctxt_pP->module_id, m);
  } else {
4271
    nr_rrc_data_req(ctxt_pP,
heshanyun's avatar
heshanyun committed
4272 4273 4274 4275 4276 4277
                 DCCH,
                 rrc_gNB_mui++,
                 SDU_CONFIRM_NO,
                 size,
                 buffer,
                 PDCP_TRANSMISSION_MODE_CONTROL);
4278 4279 4280

    rrc_gNB_send_NGAP_UE_CONTEXT_RELEASE_COMPLETE(ctxt_pP->instance, ue_context_pP->ue_context.gNB_ue_ngap_id);
    ue_context_pP->ue_context.ue_release_timer_rrc = 1;
heshanyun's avatar
heshanyun committed
4281 4282
  }
}
Cedric Roux's avatar
Cedric Roux committed
4283 4284 4285
void nr_rrc_trigger(protocol_ctxt_t *ctxt, int CC_id, int frame, int subframe)
{
  MessageDef *message_p;
4286
  message_p = itti_alloc_new_message(TASK_RRC_GNB, 0, RRC_SUBFRAME_PROCESS);
Cedric Roux's avatar
Cedric Roux committed
4287 4288
  RRC_SUBFRAME_PROCESS(message_p).ctxt  = *ctxt;
  RRC_SUBFRAME_PROCESS(message_p).CC_id = CC_id;
4289
  LOG_D(NR_RRC, "Time in RRC: %u/ %u \n", frame, subframe);
Cedric Roux's avatar
Cedric Roux committed
4290 4291
  itti_send_msg_to_task(TASK_RRC_GNB, ctxt->module_id, message_p);
}