rrc_UE.c 128 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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
 */

22 23 24 25 26 27 28 29 30
/* \file rrc_UE.c
 * \brief RRC procedures
 * \author R. Knopp, K.H. HSU
 * \date 2018
 * \version 0.1
 * \company Eurecom / NTUST
 * \email: knopp@eurecom.fr, kai-hsiang.hsu@eurecom.fr
 * \note
 * \warning
31 32 33 34 35
 */

#define RRC_UE
#define RRC_UE_C

yaojie's avatar
yaojie committed
36 37
#include "asn1_conversions.h"

38 39 40 41 42 43
#include "NR_DL-DCCH-Message.h"        //asn_DEF_NR_DL_DCCH_Message
#include "NR_DL-CCCH-Message.h"        //asn_DEF_NR_DL_CCCH_Message
#include "NR_BCCH-BCH-Message.h"       //asn_DEF_NR_BCCH_BCH_Message
#include "NR_BCCH-DL-SCH-Message.h"    //asn_DEF_NR_BCCH_DL_SCH_Message
#include "NR_CellGroupConfig.h"        //asn_DEF_NR_CellGroupConfig
#include "NR_BWP-Downlink.h"           //asn_DEF_NR_BWP_Downlink
44 45
#include "NR_RRCReconfiguration.h"
#include "NR_MeasConfig.h"
Xue Song's avatar
Xue Song committed
46
#include "NR_UL-DCCH-Message.h"
47

48 49 50 51
#include "rrc_list.h"
#include "rrc_defs.h"
#include "rrc_proto.h"
#include "rrc_vars.h"
52
#include "LAYER2/NR_MAC_UE/mac_proto.h"
53

54 55
#include "intertask_interface.h"

56
#include "executables/softmodem-common.h"
yaojie's avatar
yaojie committed
57
#include "plmn_data.h"
58
#include "pdcp.h"
Xue Song's avatar
Xue Song committed
59
#include "UTIL/OSA/osa_defs.h"
yaojie's avatar
yaojie committed
60 61
#include "common/utils/LOG/log.h"
#include "common/utils/LOG/vcd_signal_dumper.h"
Xue Song's avatar
Xue Song committed
62

yaojie's avatar
yaojie committed
63 64 65 66 67 68 69 70
#ifndef CELLULAR
  #include "RRC/NR/MESSAGES/asn1_msg.h"
#endif

#include "RRC/NAS/nas_config.h"
#include "RRC/NAS/rb_config.h"
#include "SIMULATION/TOOLS/sim.h" // for taus

71 72 73 74
#if ITTI_SIM
#include "nr_nas_msg_sim.h"
#endif

yaojie's avatar
yaojie committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
/* NAS Attach request with IMSI */
static const char  nr_nas_attach_req_imsi[] = {
  0x07, 0x41,
  /* EPS Mobile identity = IMSI */
  0x71, 0x08, 0x29, 0x80, 0x43, 0x21, 0x43, 0x65, 0x87,
  0xF9,
  /* End of EPS Mobile Identity */
  0x02, 0xE0, 0xE0, 0x00, 0x20, 0x02, 0x03,
  0xD0, 0x11, 0x27, 0x1A, 0x80, 0x80, 0x21, 0x10, 0x01, 0x00, 0x00,
  0x10, 0x81, 0x06, 0x00, 0x00, 0x00, 0x00, 0x83, 0x06, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x0A, 0x00, 0x52, 0x12, 0xF2,
  0x01, 0x27, 0x11,
};

extern void pdcp_config_set_security(
  const protocol_ctxt_t *const  ctxt_pP,
  pdcp_t          *const pdcp_pP,
  const rb_id_t         rb_idP,
  const uint16_t        lc_idP,
  const uint8_t         security_modeP,
  uint8_t         *const kRRCenc,
  uint8_t         *const kRRCint,
  uint8_t         *const  kUPenc
);

void
nr_rrc_ue_process_ueCapabilityEnquiry(
  const protocol_ctxt_t *const ctxt_pP,
  NR_UECapabilityEnquiry_t *UECapabilityEnquiry,
  uint8_t gNB_index
);

void
nr_sa_rrc_ue_process_radioBearerConfig(
    const protocol_ctxt_t *const       ctxt_pP,
    const uint8_t                      gNB_index,
    NR_RadioBearerConfig_t *const      radioBearerConfig
);

Xue Song's avatar
Xue Song committed
114 115 116 117 118
uint8_t do_NR_RRCReconfigurationComplete(
                        const protocol_ctxt_t *const ctxt_pP,
                        uint8_t *buffer,
                        const uint8_t Transaction_id
                      );
yaojie's avatar
yaojie committed
119

Xue Song's avatar
Xue Song committed
120
mui_t nr_rrc_mui=0;
121

yaojie's avatar
yaojie committed
122
static Rrc_State_NR_t nr_rrc_get_state (module_id_t ue_mod_idP) {
123 124 125 126
  return NR_UE_rrc_inst[ue_mod_idP].nrRrcState;
}


yaojie's avatar
yaojie committed
127
static Rrc_Sub_State_NR_t nr_rrc_get_sub_state (module_id_t ue_mod_idP) {
128 129 130
  return NR_UE_rrc_inst[ue_mod_idP].nrRrcSubState;
}

yaojie's avatar
yaojie committed
131 132
static int nr_rrc_set_state (module_id_t ue_mod_idP, Rrc_State_NR_t state) {
  AssertFatal ((RRC_STATE_FIRST_NR <= state) && (state <= RRC_STATE_LAST_NR),
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
               "Invalid state %d!\n", state);

  if (NR_UE_rrc_inst[ue_mod_idP].nrRrcState != state) {
    NR_UE_rrc_inst[ue_mod_idP].nrRrcState = state;
    return (1);
  }

  return (0);
}

static int nr_rrc_set_sub_state( module_id_t ue_mod_idP, Rrc_Sub_State_NR_t subState ) {
  if (AMF_MODE_ENABLED) {
    switch (NR_UE_rrc_inst[ue_mod_idP].nrRrcState) {
      case RRC_STATE_INACTIVE_NR:
        AssertFatal ((RRC_SUB_STATE_INACTIVE_FIRST_NR <= subState) && (subState <= RRC_SUB_STATE_INACTIVE_LAST_NR),
                     "Invalid nr sub state %d for state %d!\n", subState, NR_UE_rrc_inst[ue_mod_idP].nrRrcState);
        break;

      case RRC_STATE_IDLE_NR:
        AssertFatal ((RRC_SUB_STATE_IDLE_FIRST_NR <= subState) && (subState <= RRC_SUB_STATE_IDLE_LAST_NR),
                     "Invalid nr sub state %d for state %d!\n", subState, NR_UE_rrc_inst[ue_mod_idP].nrRrcState);
        break;

      case RRC_STATE_CONNECTED_NR:
        AssertFatal ((RRC_SUB_STATE_CONNECTED_FIRST_NR <= subState) && (subState <= RRC_SUB_STATE_CONNECTED_LAST_NR),
                     "Invalid nr sub state %d for state %d!\n", subState, NR_UE_rrc_inst[ue_mod_idP].nrRrcState);
        break;
    }
  }

  if (NR_UE_rrc_inst[ue_mod_idP].nrRrcSubState != subState) {
    NR_UE_rrc_inst[ue_mod_idP].nrRrcSubState = subState;
    return (1);
  }

  return (0);
}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
extern boolean_t nr_rrc_pdcp_config_asn1_req(
    const protocol_ctxt_t *const  ctxt_pP,
    NR_SRB_ToAddModList_t  *const srb2add_list,
    NR_DRB_ToAddModList_t  *const drb2add_list,
    NR_DRB_ToReleaseList_t *const drb2release_list,
    const uint8_t                   security_modeP,
    uint8_t                  *const kRRCenc,
    uint8_t                  *const kRRCint,
    uint8_t                  *const kUPenc
  #if (LTE_RRC_VERSION >= MAKE_VERSION(9, 0, 0))
    ,LTE_PMCH_InfoList_r9_t  *pmch_InfoList_r9
  #endif
    ,rb_id_t                 *const defaultDRB,
    struct NR_CellGroupConfig__rlc_BearerToAddModList *rlc_bearer2add_list);

extern rlc_op_status_t nr_rrc_rlc_config_asn1_req (const protocol_ctxt_t   * const ctxt_pP,
    const NR_SRB_ToAddModList_t   * const srb2add_listP,
    const NR_DRB_ToAddModList_t   * const drb2add_listP,
    const NR_DRB_ToReleaseList_t  * const drb2release_listP,
    const LTE_PMCH_InfoList_r9_t * const pmch_InfoList_r9_pP,
    struct NR_CellGroupConfig__rlc_BearerToAddModList *rlc_bearer2add_list);
192

193
// from LTE-RRC DL-DCCH RRCConnectionReconfiguration nr-secondary-cell-group-config (encoded)
194
int8_t nr_rrc_ue_decode_secondary_cellgroup_config(
195
    const uint8_t *buffer,
196 197 198 199
    const uint32_t size ){
    
    NR_CellGroupConfig_t *cell_group_config = NULL;
    uint32_t i;
200

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    asn_dec_rval_t dec_rval = uper_decode_complete( NULL,
                                                    &asn_DEF_NR_CellGroupConfig,
                                                    (void **)&cell_group_config,
                                                    (uint8_t *)buffer,
                                                    size ); 

    if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) {
            printf("NR_CellGroupConfig decode error\n");
            for (i=0; i<size; i++){
                printf("%02x ",buffer[i]);
            }
            printf("\n");
            // free the memory
            SEQUENCE_free( &asn_DEF_NR_CellGroupConfig, (void *)cell_group_config, 1 );
            return -1;
    }
217

218
    if(NR_UE_rrc_inst->cell_group_config == NULL){
219 220
        NR_UE_rrc_inst->cell_group_config = cell_group_config;
        nr_rrc_ue_process_scg_config(cell_group_config);
221
    }else{
222 223
        nr_rrc_ue_process_scg_config(cell_group_config);
        SEQUENCE_free(&asn_DEF_NR_CellGroupConfig, (void *)cell_group_config, 0);
224
    }
225

226
    //nr_rrc_mac_config_req_ue( 0,0,0,NULL, cell_group_config->mac_CellGroupConfig, cell_group_config->physicalCellGroupConfig, cell_group_config->spCellConfig );
227 228

    return 0;
229 230
}

231 232 233 234 235 236 237 238
int8_t nr_rrc_ue_process_RadioBearerConfig(NR_RadioBearerConfig_t *RadioBearerConfig){


  xer_fprint(stdout, &asn_DEF_NR_RadioBearerConfig, (const void*)RadioBearerConfig);
  // Configure PDCP

  return 0;
}
239 240 241

// from LTE-RRC DL-DCCH RRCConnectionReconfiguration nr-secondary-cell-group-config (decoded)
// RRCReconfiguration
242
int8_t nr_rrc_ue_process_rrcReconfiguration(NR_RRCReconfiguration_t *rrcReconfiguration){
243

244 245
    switch(rrcReconfiguration->criticalExtensions.present){
        case NR_RRCReconfiguration__criticalExtensions_PR_rrcReconfiguration:
246
            if(rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration->radioBearerConfig != NULL){
247
                if(NR_UE_rrc_inst->radio_bearer_config == NULL){
248
                    NR_UE_rrc_inst->radio_bearer_config = rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration->radioBearerConfig;                
249
                }else{
250
                    nr_rrc_ue_process_RadioBearerConfig(rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration->radioBearerConfig);
251
                }
252 253
            }

254
            if(rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration->secondaryCellGroup != NULL){
255
                NR_CellGroupConfig_t *cellGroupConfig = NULL;
256
                uper_decode(NULL,
257
                            &asn_DEF_NR_CellGroupConfig,   //might be added prefix later
258
                            (void **)&cellGroupConfig,
259 260
                            (uint8_t *)rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration->secondaryCellGroup->buf,
                            rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration->secondaryCellGroup->size, 0, 0); 
261

262 263
		xer_fprint(stdout, &asn_DEF_NR_CellGroupConfig, (const void*)cellGroupConfig);

264
                if(NR_UE_rrc_inst->cell_group_config == NULL){
265 266 267 268 269
                    //  first time receive the configuration, just use the memory allocated from uper_decoder. TODO this is not good implementation, need to maintain RRC_INST own structure every time.
                    NR_UE_rrc_inst->cell_group_config = cellGroupConfig;
                    nr_rrc_ue_process_scg_config(cellGroupConfig);
                }else{
                    //  after first time, update it and free the memory after.
270 271
                    SEQUENCE_free(&asn_DEF_NR_CellGroupConfig, (void *)NR_UE_rrc_inst->cell_group_config, 0);
                    NR_UE_rrc_inst->cell_group_config = cellGroupConfig;
272 273 274
                    nr_rrc_ue_process_scg_config(cellGroupConfig);
                }
                
275 276
            }

277
            if(rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration->measConfig != NULL){
278
                if(NR_UE_rrc_inst->meas_config == NULL){
279
                    NR_UE_rrc_inst->meas_config = rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration->measConfig;
280 281
                }else{
                    //  if some element need to be updated
282
                    nr_rrc_ue_process_meas_config(rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration->measConfig);
283 284
                }
               
285 286
            }

287
            if(rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration->lateNonCriticalExtension != NULL){
288 289 290
                //  unuse now
            }

291
            if(rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration->nonCriticalExtension != NULL){
292 293 294
                // unuse now
            }
            break;
295 296
        case NR_RRCReconfiguration__criticalExtensions_PR_NOTHING:
        case NR_RRCReconfiguration__criticalExtensions_PR_criticalExtensionsFuture:
297 298 299
        default:
            break;
    }
300 301 302
    //nr_rrc_mac_config_req_ue(); 

    return 0;
303 304
}

305 306


307
int8_t nr_rrc_ue_process_meas_config(NR_MeasConfig_t *meas_config){
308

309
    return 0;
310
}
311

312 313 314
int8_t nr_rrc_ue_process_scg_config(NR_CellGroupConfig_t *cell_group_config){
    int i;
    if(NR_UE_rrc_inst->cell_group_config==NULL){
315
      //  initial list
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
        if(cell_group_config->spCellConfig != NULL){
            if(cell_group_config->spCellConfig->spCellConfigDedicated != NULL){
                if(cell_group_config->spCellConfig->spCellConfigDedicated->downlinkBWP_ToAddModList != NULL){
                    for(i=0; i<cell_group_config->spCellConfig->spCellConfigDedicated->downlinkBWP_ToAddModList->list.count; ++i){
                        RRC_LIST_MOD_ADD(NR_UE_rrc_inst->BWP_Downlink_list, cell_group_config->spCellConfig->spCellConfigDedicated->downlinkBWP_ToAddModList->list.array[i], bwp_Id);
                    }
                }
            }
        } 
    }else{
        //  maintain list
        if(cell_group_config->spCellConfig != NULL){
            if(cell_group_config->spCellConfig->spCellConfigDedicated != NULL){
                //  process element of list to be add by RRC message
                if(cell_group_config->spCellConfig->spCellConfigDedicated->downlinkBWP_ToAddModList != NULL){
                    for(i=0; i<cell_group_config->spCellConfig->spCellConfigDedicated->downlinkBWP_ToAddModList->list.count; ++i){
                        RRC_LIST_MOD_ADD(NR_UE_rrc_inst->BWP_Downlink_list, cell_group_config->spCellConfig->spCellConfigDedicated->downlinkBWP_ToAddModList->list.array[i], bwp_Id);
                    }
                }
                

                //  process element of list to be release by RRC message
                if(cell_group_config->spCellConfig->spCellConfigDedicated->downlinkBWP_ToReleaseList != NULL){
                    for(i=0; i<cell_group_config->spCellConfig->spCellConfigDedicated->downlinkBWP_ToReleaseList->list.count; ++i){
                        NR_BWP_Downlink_t *freeP = NULL;
                        RRC_LIST_MOD_REL(NR_UE_rrc_inst->BWP_Downlink_list, bwp_Id, *cell_group_config->spCellConfig->spCellConfigDedicated->downlinkBWP_ToReleaseList->list.array[i], freeP);
                        if(freeP != NULL){
                            SEQUENCE_free(&asn_DEF_NR_BWP_Downlink, (void *)freeP, 0);
                        }
                    }
                }
            }
        }
    } 
350

351
    return 0;
352
}
353

354 355


356 357 358 359 360 361

void process_nsa_message(NR_UE_RRC_INST_t *rrc, nsa_message_t nsa_message_type, void *message,int msg_len) {

  switch (nsa_message_type) {
    case nr_SecondaryCellGroupConfig_r15:
      {
362
	NR_RRCReconfiguration_t *RRCReconfiguration=NULL;
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	asn_dec_rval_t dec_rval = uper_decode_complete( NULL,
							&asn_DEF_NR_RRCReconfiguration,
							(void **)&RRCReconfiguration,
							(uint8_t *)message,
							msg_len); 
	
	if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) {
	  LOG_E(RRC,"NR_RRCReconfiguration decode error\n");
	  // free the memory
	  SEQUENCE_free( &asn_DEF_NR_RRCReconfiguration, RRCReconfiguration, 1 );
	  return;
	}      
	nr_rrc_ue_process_rrcReconfiguration(RRCReconfiguration);
      }
      break;
    case nr_RadioBearerConfigX_r15:
      {
380
	NR_RadioBearerConfig_t *RadioBearerConfig=NULL;
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
	asn_dec_rval_t dec_rval = uper_decode_complete( NULL,
							&asn_DEF_NR_RadioBearerConfig,
							(void **)&RadioBearerConfig,
							(uint8_t *)message,
							msg_len); 
	
	if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) {
	  LOG_E(RRC,"NR_RadioBearerConfig decode error\n");
	  // free the memory
	  SEQUENCE_free( &asn_DEF_NR_RadioBearerConfig, RadioBearerConfig, 1 );
	  return;
	}      
	nr_rrc_ue_process_RadioBearerConfig(RadioBearerConfig);
      }
      break;
    default:
      AssertFatal(1==0,"Unknown message %d\n",nsa_message_type);
      break;
  }

}

403
NR_UE_RRC_INST_t* openair_rrc_top_init_ue_nr(char* rrc_config_path){
404 405 406 407 408 409

    if(NB_NR_UE_INST > 0){
        NR_UE_rrc_inst = (NR_UE_RRC_INST_t *)malloc(NB_NR_UE_INST * sizeof(NR_UE_RRC_INST_t));
        memset(NR_UE_rrc_inst, 0, NB_NR_UE_INST * sizeof(NR_UE_RRC_INST_t));

        // fill UE-NR-Capability @ UE-CapabilityRAT-Container here.
Xue Song's avatar
Xue Song committed
410
        NR_UE_rrc_inst[0].selected_plmn_identity = 1;
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
        //  init RRC lists
        RRC_LIST_INIT(NR_UE_rrc_inst->RLC_Bearer_Config_list, NR_maxLC_ID);
        RRC_LIST_INIT(NR_UE_rrc_inst->SchedulingRequest_list, NR_maxNrofSR_ConfigPerCellGroup);
        RRC_LIST_INIT(NR_UE_rrc_inst->TAG_list, NR_maxNrofTAGs);
        RRC_LIST_INIT(NR_UE_rrc_inst->TDD_UL_DL_SlotConfig_list, NR_maxNrofSlots);
        RRC_LIST_INIT(NR_UE_rrc_inst->BWP_Downlink_list, NR_maxNrofBWPs);
        RRC_LIST_INIT(NR_UE_rrc_inst->ControlResourceSet_list[0], 3);   //  for init-dl-bwp
        RRC_LIST_INIT(NR_UE_rrc_inst->ControlResourceSet_list[1], 3);   //  for dl-bwp id=0
        RRC_LIST_INIT(NR_UE_rrc_inst->ControlResourceSet_list[2], 3);   //  for dl-bwp id=1
        RRC_LIST_INIT(NR_UE_rrc_inst->ControlResourceSet_list[3], 3);   //  for dl-bwp id=2
        RRC_LIST_INIT(NR_UE_rrc_inst->ControlResourceSet_list[4], 3);   //  for dl-bwp id=3
        RRC_LIST_INIT(NR_UE_rrc_inst->SearchSpace_list[0], 10);
        RRC_LIST_INIT(NR_UE_rrc_inst->SearchSpace_list[1], 10);
        RRC_LIST_INIT(NR_UE_rrc_inst->SearchSpace_list[2], 10);
        RRC_LIST_INIT(NR_UE_rrc_inst->SearchSpace_list[3], 10);
        RRC_LIST_INIT(NR_UE_rrc_inst->SearchSpace_list[4], 10);
        RRC_LIST_INIT(NR_UE_rrc_inst->SlotFormatCombinationsPerCell_list[0], NR_maxNrofAggregatedCellsPerCellGroup);
        RRC_LIST_INIT(NR_UE_rrc_inst->SlotFormatCombinationsPerCell_list[1], NR_maxNrofAggregatedCellsPerCellGroup);
        RRC_LIST_INIT(NR_UE_rrc_inst->SlotFormatCombinationsPerCell_list[2], NR_maxNrofAggregatedCellsPerCellGroup);
        RRC_LIST_INIT(NR_UE_rrc_inst->SlotFormatCombinationsPerCell_list[3], NR_maxNrofAggregatedCellsPerCellGroup);
        RRC_LIST_INIT(NR_UE_rrc_inst->SlotFormatCombinationsPerCell_list[4], NR_maxNrofAggregatedCellsPerCellGroup);
        RRC_LIST_INIT(NR_UE_rrc_inst->TCI_State_list[0], NR_maxNrofTCI_States);
        RRC_LIST_INIT(NR_UE_rrc_inst->TCI_State_list[1], NR_maxNrofTCI_States);
        RRC_LIST_INIT(NR_UE_rrc_inst->TCI_State_list[2], NR_maxNrofTCI_States);
        RRC_LIST_INIT(NR_UE_rrc_inst->TCI_State_list[3], NR_maxNrofTCI_States);
        RRC_LIST_INIT(NR_UE_rrc_inst->TCI_State_list[4], NR_maxNrofTCI_States);
        RRC_LIST_INIT(NR_UE_rrc_inst->RateMatchPattern_list[0], NR_maxNrofRateMatchPatterns);
        RRC_LIST_INIT(NR_UE_rrc_inst->RateMatchPattern_list[1], NR_maxNrofRateMatchPatterns);
        RRC_LIST_INIT(NR_UE_rrc_inst->RateMatchPattern_list[2], NR_maxNrofRateMatchPatterns);
        RRC_LIST_INIT(NR_UE_rrc_inst->RateMatchPattern_list[3], NR_maxNrofRateMatchPatterns);
        RRC_LIST_INIT(NR_UE_rrc_inst->RateMatchPattern_list[4], NR_maxNrofRateMatchPatterns);
        RRC_LIST_INIT(NR_UE_rrc_inst->ZP_CSI_RS_Resource_list[0], NR_maxNrofZP_CSI_RS_Resources);
        RRC_LIST_INIT(NR_UE_rrc_inst->ZP_CSI_RS_Resource_list[1], NR_maxNrofZP_CSI_RS_Resources);
        RRC_LIST_INIT(NR_UE_rrc_inst->ZP_CSI_RS_Resource_list[2], NR_maxNrofZP_CSI_RS_Resources);
        RRC_LIST_INIT(NR_UE_rrc_inst->ZP_CSI_RS_Resource_list[3], NR_maxNrofZP_CSI_RS_Resources);
        RRC_LIST_INIT(NR_UE_rrc_inst->ZP_CSI_RS_Resource_list[4], NR_maxNrofZP_CSI_RS_Resources);
447 448 449 450 451 452 453 454 455 456
        RRC_LIST_INIT(NR_UE_rrc_inst->Aperidic_ZP_CSI_RS_ResourceSet_list[0], NR_maxNrofZP_CSI_RS_ResourceSets);
        RRC_LIST_INIT(NR_UE_rrc_inst->Aperidic_ZP_CSI_RS_ResourceSet_list[1], NR_maxNrofZP_CSI_RS_ResourceSets);
        RRC_LIST_INIT(NR_UE_rrc_inst->Aperidic_ZP_CSI_RS_ResourceSet_list[2], NR_maxNrofZP_CSI_RS_ResourceSets);
        RRC_LIST_INIT(NR_UE_rrc_inst->Aperidic_ZP_CSI_RS_ResourceSet_list[3], NR_maxNrofZP_CSI_RS_ResourceSets);
        RRC_LIST_INIT(NR_UE_rrc_inst->Aperidic_ZP_CSI_RS_ResourceSet_list[4], NR_maxNrofZP_CSI_RS_ResourceSets);
        RRC_LIST_INIT(NR_UE_rrc_inst->SP_ZP_CSI_RS_ResourceSet_list[0], NR_maxNrofZP_CSI_RS_ResourceSets);
        RRC_LIST_INIT(NR_UE_rrc_inst->SP_ZP_CSI_RS_ResourceSet_list[1], NR_maxNrofZP_CSI_RS_ResourceSets);
        RRC_LIST_INIT(NR_UE_rrc_inst->SP_ZP_CSI_RS_ResourceSet_list[2], NR_maxNrofZP_CSI_RS_ResourceSets);
        RRC_LIST_INIT(NR_UE_rrc_inst->SP_ZP_CSI_RS_ResourceSet_list[3], NR_maxNrofZP_CSI_RS_ResourceSets);
        RRC_LIST_INIT(NR_UE_rrc_inst->SP_ZP_CSI_RS_ResourceSet_list[4], NR_maxNrofZP_CSI_RS_ResourceSets);
457 458 459 460 461 462 463
        RRC_LIST_INIT(NR_UE_rrc_inst->NZP_CSI_RS_Resource_list, NR_maxNrofNZP_CSI_RS_Resources);
        RRC_LIST_INIT(NR_UE_rrc_inst->NZP_CSI_RS_ResourceSet_list, NR_maxNrofNZP_CSI_RS_ResourceSets);
        RRC_LIST_INIT(NR_UE_rrc_inst->CSI_IM_Resource_list, NR_maxNrofCSI_IM_Resources);
        RRC_LIST_INIT(NR_UE_rrc_inst->CSI_IM_ResourceSet_list, NR_maxNrofCSI_IM_ResourceSets);
        RRC_LIST_INIT(NR_UE_rrc_inst->CSI_SSB_ResourceSet_list, NR_maxNrofCSI_SSB_ResourceSets);
        RRC_LIST_INIT(NR_UE_rrc_inst->CSI_ResourceConfig_list, NR_maxNrofCSI_ResourceConfigurations);
        RRC_LIST_INIT(NR_UE_rrc_inst->CSI_ReportConfig_list, NR_maxNrofCSI_ReportConfigurations);
464

465
	if (get_softmodem_params()->phy_test==1 || get_softmodem_params()->do_ra==1) {
466 467
	  // read in files for RRCReconfiguration and RBconfig
	  FILE *fd;
468 469 470 471 472 473
	  char filename[1024];
	  if (rrc_config_path)
	    sprintf(filename,"%s/reconfig.raw",rrc_config_path);
	  else
	    sprintf(filename,"reconfig.raw");
	  fd = fopen(filename,"r");
474
          char buffer[1024];
475 476 477 478 479
	  AssertFatal(fd,
	              "cannot read file %s: errno %d, %s\n",
	              filename,
	              errno,
	              strerror(errno));
480 481 482
	  int msg_len=fread(buffer,1,1024,fd);
	  fclose(fd);
	  process_nsa_message(NR_UE_rrc_inst, nr_SecondaryCellGroupConfig_r15, buffer,msg_len);
483 484 485 486 487
	  if (rrc_config_path)
	    sprintf(filename,"%s/rbconfig.raw",rrc_config_path);
	  else
	    sprintf(filename,"rbconfig.raw");
	  fd = fopen(filename,"r");
488 489 490 491 492
	  AssertFatal(fd,
	              "cannot read file %s: errno %d, %s\n",
	              filename,
	              errno,
	              strerror(errno));
493 494 495 496
	  msg_len=fread(buffer,1,1024,fd);
	  fclose(fd);
	  process_nsa_message(NR_UE_rrc_inst, nr_RadioBearerConfigX_r15, buffer,msg_len); 
	}
497
    }else{
498
        NR_UE_rrc_inst = NULL;
499
    }
500

501
    return NR_UE_rrc_inst;
502 503 504
}


505
int8_t nr_ue_process_rlc_bearer_list(NR_CellGroupConfig_t *cell_group_config){
506

507
    return 0;
508
}
509

510
int8_t nr_ue_process_secondary_cell_list(NR_CellGroupConfig_t *cell_group_config){
511

512
    return 0;
513
}
514

515
int8_t nr_ue_process_mac_cell_group_config(NR_MAC_CellGroupConfig_t *mac_cell_group_config){
516

517
    return 0;
518
}
519

520
int8_t nr_ue_process_physical_cell_group_config(NR_PhysicalCellGroupConfig_t *phy_cell_group_config){
521

522
    return 0;
523
}
524

525
int8_t nr_ue_process_spcell_config(NR_SpCellConfig_t *spcell_config){
526

527
    return 0;
528
}
529 530 531 532 533 534

/*brief decode BCCH-BCH (MIB) message*/
int8_t nr_rrc_ue_decode_NR_BCCH_BCH_Message(
    const module_id_t module_id,
    const uint8_t     gNB_index,
    uint8_t           *const bufferP,
535
    const uint8_t     buffer_len ){
536 537 538 539
    int i;
    NR_BCCH_BCH_Message_t *bcch_message = NULL;
    NR_MIB_t *mib = NR_UE_rrc_inst->mib;

540 541 542 543
    if(mib != NULL){
        SEQUENCE_free( &asn_DEF_NR_BCCH_BCH_Message, (void *)mib, 1 );
    }

544

Calvin HSU's avatar
Calvin HSU committed
545 546 547
    for(i=0; i<buffer_len; ++i){
        printf("[RRC] MIB PDU : %d\n", bufferP[i]);
    }
548

549
    asn_dec_rval_t dec_rval = uper_decode_complete( NULL,
550 551 552 553
                                                    &asn_DEF_NR_BCCH_BCH_Message,
                                                   (void **)&bcch_message,
                                                   (const void *)bufferP,
                                                   buffer_len );
554

555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
    if ((dec_rval.code != RC_OK) || (dec_rval.consumed == 0)) {
      printf("NR_BCCH_BCH decode error\n");
      for (i=0; i<buffer_len; i++){
	printf("%02x ",bufferP[i]);
      }
      printf("\n");
      // free the memory
      SEQUENCE_free( &asn_DEF_NR_BCCH_BCH_Message, (void *)bcch_message, 1 );
      return -1;
    }
    else {
      //  link to rrc instance
      mib = bcch_message->message.choice.mib;
      //memcpy( (void *)mib,
      //    (void *)&bcch_message->message.choice.mib,
      //    sizeof(NR_MIB_t) );
      
572
      nr_rrc_mac_config_req_ue( 0, 0, 0, mib, NULL);
573
    }
574
    
575 576 577
    return 0;
}

yaojie's avatar
yaojie committed
578 579
const char  siWindowLength[10][5] = {"5s", "10s", "20s", "40s", "80s", "160s", "320s", "640s", "1280s","ERR"};// {"1ms","2ms","5ms","10ms","15ms","20ms","40ms","80ms","ERR"};
const short siWindowLength_int[9] = {5,10,20,40,80,160,320,640,1280};//{1,2,5,10,15,20,40,80};
yaojie's avatar
yaojie committed
580

yaojie's avatar
yaojie committed
581 582 583
const char SIBType[12][6]        = {"SIB3","SIB4","SIB5","SIB6","SIB7","SIB8","SIB9","SIB10","SIB11","SIB12","SIB13","Spare"};
const char SIBPeriod[8][6]       = {"rf8","rf16","rf32","rf64","rf128","rf256","rf512","ERR"};
int   siPeriod_int[7]            = {80,160,320,640,1280,2560,5120};
yaojie's avatar
yaojie committed
584

yaojie's avatar
yaojie committed
585
const char *nr_SIBreserved( long value ) {
yaojie's avatar
yaojie committed
586 587 588 589 590 591 592 593 594
  if (value < 0 || value > 1)
    return "ERR";

  if (value)
    return "notReserved";

  return "reserved";
}

yaojie's avatar
yaojie committed
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
void nr_dump_sib2( NR_SIB2_t *sib2 ){
//cellReselectionInfoCommon
  //nrofSS_BlocksToAverage
  if( sib2->cellReselectionInfoCommon.nrofSS_BlocksToAverage)
    LOG_I( RRC, "cellReselectionInfoCommon.nrofSS_BlocksToAverage : %ld\n",
           *sib2->cellReselectionInfoCommon.nrofSS_BlocksToAverage );
  else
    LOG_I( RRC, "cellReselectionInfoCommon->nrofSS_BlocksToAverage : not defined\n" );

  //absThreshSS_BlocksConsolidation
  if( sib2->cellReselectionInfoCommon.absThreshSS_BlocksConsolidation){
    LOG_I( RRC, "absThreshSS_BlocksConsolidation.thresholdRSRP : %ld\n",
           *sib2->cellReselectionInfoCommon.absThreshSS_BlocksConsolidation->thresholdRSRP );
    LOG_I( RRC, "absThreshSS_BlocksConsolidation.thresholdRSRQ : %ld\n",
           *sib2->cellReselectionInfoCommon.absThreshSS_BlocksConsolidation->thresholdRSRQ );
    LOG_I( RRC, "absThreshSS_BlocksConsolidation.thresholdSINR : %ld\n",
           *sib2->cellReselectionInfoCommon.absThreshSS_BlocksConsolidation->thresholdSINR );
  } else
    LOG_I( RRC, "cellReselectionInfoCommon->absThreshSS_BlocksConsolidation : not defined\n" );

  //q_Hyst
  LOG_I( RRC, "cellReselectionInfoCommon.q_Hyst : %ld\n",
           sib2->cellReselectionInfoCommon.q_Hyst );

  //speedStateReselectionPars
  if( sib2->cellReselectionInfoCommon.speedStateReselectionPars){
    LOG_I( RRC, "speedStateReselectionPars->mobilityStateParameters.t_Evaluation : %ld\n",
           sib2->cellReselectionInfoCommon.speedStateReselectionPars->mobilityStateParameters.t_Evaluation);
    LOG_I( RRC, "speedStateReselectionPars->mobilityStateParameters.t_HystNormal  : %ld\n",
           sib2->cellReselectionInfoCommon.speedStateReselectionPars->mobilityStateParameters.t_HystNormal);
    LOG_I( RRC, "speedStateReselectionPars->mobilityStateParameters.n_CellChangeMedium : %ld\n",
           sib2->cellReselectionInfoCommon.speedStateReselectionPars->mobilityStateParameters.n_CellChangeMedium);
    LOG_I( RRC, "speedStateReselectionPars->mobilityStateParameters.n_CellChangeHigh : %ld\n",
           sib2->cellReselectionInfoCommon.speedStateReselectionPars->mobilityStateParameters.n_CellChangeHigh);
    LOG_I( RRC, "speedStateReselectionPars->q_HystSF.sf_Medium : %ld\n",
           sib2->cellReselectionInfoCommon.speedStateReselectionPars->q_HystSF.sf_Medium);
    LOG_I( RRC, "speedStateReselectionPars->q_HystSF.sf_High : %ld\n",
           sib2->cellReselectionInfoCommon.speedStateReselectionPars->q_HystSF.sf_High);
  } else
    LOG_I( RRC, "cellReselectionInfoCommon->speedStateReselectionPars : not defined\n" );

//cellReselectionServingFreqInfo
  if( sib2->cellReselectionServingFreqInfo.s_NonIntraSearchP)
    LOG_I( RRC, "cellReselectionServingFreqInfo.s_NonIntraSearchP : %ld\n",
           *sib2->cellReselectionServingFreqInfo.s_NonIntraSearchP );
  else
    LOG_I( RRC, "cellReselectionServingFreqInfo->s_NonIntraSearchP : not defined\n" );
 
  if( sib2->cellReselectionServingFreqInfo.s_NonIntraSearchQ)
    LOG_I( RRC, "cellReselectionServingFreqInfo.s_NonIntraSearchQ : %ld\n",
           *sib2->cellReselectionServingFreqInfo.s_NonIntraSearchQ );
  else
    LOG_I( RRC, "cellReselectionServingFreqInfo->s_NonIntraSearchQ : not defined\n" );
  
  LOG_I( RRC, "cellReselectionServingFreqInfo.threshServingLowP : %ld\n",
         sib2->cellReselectionServingFreqInfo.threshServingLowP );
  
  if( sib2->cellReselectionServingFreqInfo.threshServingLowQ)
    LOG_I( RRC, "cellReselectionServingFreqInfo.threshServingLowQ : %ld\n",
           *sib2->cellReselectionServingFreqInfo.threshServingLowQ );
  else
    LOG_I( RRC, "cellReselectionServingFreqInfo->threshServingLowQ : not defined\n" );
  
  LOG_I( RRC, "cellReselectionServingFreqInfo.cellReselectionPriority : %ld\n",
         sib2->cellReselectionServingFreqInfo.cellReselectionPriority );
  if( sib2->cellReselectionServingFreqInfo.cellReselectionSubPriority)
    LOG_I( RRC, "cellReselectionServingFreqInfo.cellReselectionSubPriority : %ld\n",
           *sib2->cellReselectionServingFreqInfo.cellReselectionSubPriority );
  else
    LOG_I( RRC, "cellReselectionServingFreqInfo->cellReselectionSubPriority : not defined\n" );

//intraFreqCellReselectionInfo
  LOG_I( RRC, "intraFreqCellReselectionInfo.q_RxLevMin : %ld\n",
         sib2->intraFreqCellReselectionInfo.q_RxLevMin );
  
  if( sib2->intraFreqCellReselectionInfo.q_RxLevMinSUL)
    LOG_I( RRC, "intraFreqCellReselectionInfo.q_RxLevMinSUL : %ld\n",
           *sib2->intraFreqCellReselectionInfo.q_RxLevMinSUL );
  else
    LOG_I( RRC, "intraFreqCellReselectionInfo->q_RxLevMinSUL : not defined\n" );
  
  if( sib2->intraFreqCellReselectionInfo.q_QualMin)
    LOG_I( RRC, "intraFreqCellReselectionInfo.q_QualMin : %ld\n",
           *sib2->intraFreqCellReselectionInfo.q_QualMin );
  else
    LOG_I( RRC, "intraFreqCellReselectionInfo->q_QualMin : not defined\n" );
  
  LOG_I( RRC, "intraFreqCellReselectionInfo.s_IntraSearchP : %ld\n",
         sib2->intraFreqCellReselectionInfo.s_IntraSearchP );
  
  if( sib2->intraFreqCellReselectionInfo.s_IntraSearchQ)
    LOG_I( RRC, "intraFreqCellReselectionInfo.s_IntraSearchQ : %ld\n",
           *sib2->intraFreqCellReselectionInfo.s_IntraSearchQ );
  else
    LOG_I( RRC, "intraFreqCellReselectionInfo->s_IntraSearchQ : not defined\n" );
  
  LOG_I( RRC, "intraFreqCellReselectionInfo.t_ReselectionNR : %ld\n",
         sib2->intraFreqCellReselectionInfo.t_ReselectionNR );

  if( sib2->intraFreqCellReselectionInfo.frequencyBandList)
    LOG_I( RRC, "intraFreqCellReselectionInfo.frequencyBandList : %p\n",
           sib2->intraFreqCellReselectionInfo.frequencyBandList );
  else
    LOG_I( RRC, "intraFreqCellReselectionInfo->frequencyBandList : not defined\n" );
 
  if( sib2->intraFreqCellReselectionInfo.frequencyBandListSUL)
    LOG_I( RRC, "intraFreqCellReselectionInfo.frequencyBandListSUL : %p\n",
           sib2->intraFreqCellReselectionInfo.frequencyBandListSUL );
  else
    LOG_I( RRC, "intraFreqCellReselectionInfo->frequencyBandListSUL : not defined\n" );
  
  if( sib2->intraFreqCellReselectionInfo.p_Max)
    LOG_I( RRC, "intraFreqCellReselectionInfo.p_Max : %ld\n",
           *sib2->intraFreqCellReselectionInfo.p_Max );
  else
    LOG_I( RRC, "intraFreqCellReselectionInfo->p_Max : not defined\n" );
 
  if( sib2->intraFreqCellReselectionInfo.smtc)
    LOG_I( RRC, "intraFreqCellReselectionInfo.smtc : %p\n",
           sib2->intraFreqCellReselectionInfo.smtc );
  else
    LOG_I( RRC, "intraFreqCellReselectionInfo->smtc : not defined\n" );
 
  if( sib2->intraFreqCellReselectionInfo.ss_RSSI_Measurement)
    LOG_I( RRC, "intraFreqCellReselectionInfo.ss_RSSI_Measurement : %p\n",
           sib2->intraFreqCellReselectionInfo.ss_RSSI_Measurement );
  else
    LOG_I( RRC, "intraFreqCellReselectionInfo->ss_RSSI_Measurement : not defined\n" );

  if( sib2->intraFreqCellReselectionInfo.ssb_ToMeasure)
    LOG_I( RRC, "intraFreqCellReselectionInfo.ssb_ToMeasure : %p\n",
           sib2->intraFreqCellReselectionInfo.ssb_ToMeasure );
  else
    LOG_I( RRC, "intraFreqCellReselectionInfo->ssb_ToMeasure : not defined\n" );
  
  LOG_I( RRC, "intraFreqCellReselectionInfo.deriveSSB_IndexFromCell : %d\n",
         sib2->intraFreqCellReselectionInfo.deriveSSB_IndexFromCell );
yaojie's avatar
yaojie committed
732 733 734

}

yaojie's avatar
yaojie committed
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
void nr_dump_sib3( NR_SIB3_t *sib3 ) {
//intraFreqNeighCellList
  if( sib3->intraFreqNeighCellList){
    LOG_I( RRC, "intraFreqNeighCellList : %p\n",
           sib3->intraFreqNeighCellList );    
    const int n = sib3->intraFreqNeighCellList->list.count;
    for (int i = 0; i < n; ++i){
      LOG_I( RRC, "intraFreqNeighCellList->physCellId : %ld\n",
             sib3->intraFreqNeighCellList->list.array[i]->physCellId );
      LOG_I( RRC, "intraFreqNeighCellList->q_OffsetCell : %ld\n",
             sib3->intraFreqNeighCellList->list.array[i]->q_OffsetCell );

      if( sib3->intraFreqNeighCellList->list.array[i]->q_RxLevMinOffsetCell)
        LOG_I( RRC, "intraFreqNeighCellList->q_RxLevMinOffsetCell : %ld\n",
               *sib3->intraFreqNeighCellList->list.array[i]->q_RxLevMinOffsetCell );
      else
        LOG_I( RRC, "intraFreqNeighCellList->q_RxLevMinOffsetCell : not defined\n" );
      
      if( sib3->intraFreqNeighCellList->list.array[i]->q_RxLevMinOffsetCellSUL)
        LOG_I( RRC, "intraFreqNeighCellList->q_RxLevMinOffsetCellSUL : %ld\n",
               *sib3->intraFreqNeighCellList->list.array[i]->q_RxLevMinOffsetCellSUL );
      else
        LOG_I( RRC, "intraFreqNeighCellList->q_RxLevMinOffsetCellSUL : not defined\n" );
      
      if( sib3->intraFreqNeighCellList->list.array[i]->q_QualMinOffsetCell)
        LOG_I( RRC, "intraFreqNeighCellList->q_QualMinOffsetCell : %ld\n",
               *sib3->intraFreqNeighCellList->list.array[i]->q_QualMinOffsetCell );
      else
        LOG_I( RRC, "intraFreqNeighCellList->q_QualMinOffsetCell : not defined\n" );
    }
  } else{
    LOG_I( RRC, "intraFreqCellReselectionInfo : not defined\n" );
  }
yaojie's avatar
yaojie committed
768

yaojie's avatar
yaojie committed
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
//intraFreqBlackCellList
  if( sib3->intraFreqBlackCellList){
    LOG_I( RRC, "intraFreqBlackCellList : %p\n",
           sib3->intraFreqBlackCellList );    
    const int n = sib3->intraFreqBlackCellList->list.count;
    for (int i = 0; i < n; ++i){
      LOG_I( RRC, "intraFreqBlackCellList->start : %ld\n",
             sib3->intraFreqBlackCellList->list.array[i]->start );

      if( sib3->intraFreqBlackCellList->list.array[i]->range)
        LOG_I( RRC, "intraFreqBlackCellList->range : %ld\n",
             *sib3->intraFreqBlackCellList->list.array[i]->range );
      else
        LOG_I( RRC, "intraFreqBlackCellList->range : not defined\n" );
    }
   } else{
    LOG_I( RRC, "intraFreqBlackCellList : not defined\n" );
   }

//lateNonCriticalExtension
  if( sib3->lateNonCriticalExtension)
    LOG_I( RRC, "lateNonCriticalExtension : %p\n",
           sib3->lateNonCriticalExtension );
  else
    LOG_I( RRC, "lateNonCriticalExtension : not defined\n" );
yaojie's avatar
yaojie committed
794 795 796
}

int nr_decode_SI( const protocol_ctxt_t *const ctxt_pP, const uint8_t gNB_index ) {
yaojie's avatar
yaojie committed
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 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 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
  NR_SystemInformation_t **si = &NR_UE_rrc_inst[ctxt_pP->module_id].si[gNB_index];
  int new_sib = 0;
  NR_SIB1_t *sib1 = NR_UE_rrc_inst[ctxt_pP->module_id].sib1[gNB_index];
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_RRC_UE_DECODE_SI, VCD_FUNCTION_IN );

  // Dump contents
  if ((*si)->criticalExtensions.present == NR_SystemInformation__criticalExtensions_PR_systemInformation ||
      (*si)->criticalExtensions.present == NR_SystemInformation__criticalExtensions_PR_criticalExtensionsFuture_r16) {
    LOG_D( RRC, "[UE] (*si)->criticalExtensions.choice.NR_SystemInformation_t->sib_TypeAndInfo.list.count %d\n",
           (*si)->criticalExtensions.choice.systemInformation->sib_TypeAndInfo.list.count );
  } else {
    LOG_D( RRC, "[UE] Unknown criticalExtension version (not Rel16)\n" );
    return -1;
  }

  for (int i=0; i<(*si)->criticalExtensions.choice.systemInformation->sib_TypeAndInfo.list.count; i++) {
    SystemInformation_IEs__sib_TypeAndInfo__Member *typeandinfo;
    typeandinfo = (*si)->criticalExtensions.choice.systemInformation->sib_TypeAndInfo.list.array[i];

    switch(typeandinfo->present) {
      case NR_SystemInformation_IEs__sib_TypeAndInfo__Member_PR_sib2:
        if ((NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus&2) == 0) {
          NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus|=2;
          //new_sib=1;
          memcpy( NR_UE_rrc_inst[ctxt_pP->module_id].sib2[gNB_index], &typeandinfo->choice.sib2, sizeof(NR_SIB2_t) );
          LOG_I( RRC, "[UE %"PRIu8"] Frame %"PRIu32" Found SIB2 from gNB %"PRIu8"\n", ctxt_pP->module_id, ctxt_pP->frame, gNB_index );
          nr_dump_sib2( NR_UE_rrc_inst[ctxt_pP->module_id].sib2[gNB_index] );
          LOG_I( RRC, "[FRAME %05"PRIu32"][RRC_UE][MOD %02"PRIu8"][][--- MAC_CONFIG_REQ (SIB2 params  gNB %"PRIu8") --->][MAC_UE][MOD %02"PRIu8"][]\n",
                 ctxt_pP->frame, ctxt_pP->module_id, gNB_index, ctxt_pP->module_id );
          //TODO rrc_mac_config_req_ue

          // After SI is received, prepare RRCConnectionRequest
          if (NR_UE_rrc_inst[ctxt_pP->module_id].MBMS_flag < 3) // see -Q option
            if (AMF_MODE_ENABLED) {
              rrc_ue_generate_RRCSetupRequest( ctxt_pP, gNB_index );
            }

          if (NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].State == NR_RRC_IDLE) {
            LOG_I( RRC, "[UE %d] Received SIB1/SIB2/SIB3 Switching to RRC_SI_RECEIVED\n", ctxt_pP->module_id );
            NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].State = NR_RRC_SI_RECEIVED;
#if ENABLE_RAL
 /* TODO          {
              MessageDef                            *message_ral_p = NULL;
              rrc_ral_system_information_ind_t       ral_si_ind;
              message_ral_p = itti_alloc_new_message (TASK_RRC_UE, RRC_RAL_SYSTEM_INFORMATION_IND);
              memset(&ral_si_ind, 0, sizeof(rrc_ral_system_information_ind_t));
              ral_si_ind.plmn_id.MCCdigit2 = '0';
              ral_si_ind.plmn_id.MCCdigit1 = '2';
              ral_si_ind.plmn_id.MNCdigit3 = '0';
              ral_si_ind.plmn_id.MCCdigit3 = '8';
              ral_si_ind.plmn_id.MNCdigit2 = '9';
              ral_si_ind.plmn_id.MNCdigit1 = '9';
              ral_si_ind.cell_id        = 1;
              ral_si_ind.dbm            = 0;
              //ral_si_ind.dbm            = fifo_dump_emos_UE.PHY_measurements->rx_rssi_dBm[gNB_index];
              // TO DO
              ral_si_ind.sinr           = 0;
              //ral_si_ind.sinr           = fifo_dump_emos_UE.PHY_measurements->subband_cqi_dB[gNB_index][phy_vars_ue->lte_frame_parms.nb_antennas_rx][0];
              // TO DO
              ral_si_ind.link_data_rate = 0;
              memcpy (&message_ral_p->ittiMsg, (void *) &ral_si_ind, sizeof(rrc_ral_system_information_ind_t));
#warning "ue_mod_idP ? for instance ?"
              itti_send_msg_to_task (TASK_RAL_UE, UE_MODULE_ID_TO_INSTANCE(ctxt_pP->module_id), message_ral_p);
            }*/
#endif
          }
        }

        break; // case SystemInformation_r8_IEs__sib_TypeAndInfo__Member_PR_sib2

      case NR_SystemInformation_IEs__sib_TypeAndInfo__Member_PR_sib3:
        if ((NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus&4) == 0) {
          NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus|=4;
          new_sib=1;
          memcpy( NR_UE_rrc_inst[ctxt_pP->module_id].sib3[gNB_index], &typeandinfo->choice.sib3, sizeof(LTE_SystemInformationBlockType3_t) );
          LOG_I( RRC, "[UE %"PRIu8"] Frame %"PRIu32" Found SIB3 from gNB %"PRIu8"\n", ctxt_pP->module_id, ctxt_pP->frame, gNB_index );
          nr_dump_sib3( NR_UE_rrc_inst[ctxt_pP->module_id].sib3[gNB_index] );
        }

        break;

      case NR_SystemInformation_IEs__sib_TypeAndInfo__Member_PR_sib4:
        if ((NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus&8) == 0) {
          NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus|=8;
          new_sib=1;
          memcpy( NR_UE_rrc_inst[ctxt_pP->module_id].sib4[gNB_index], typeandinfo->choice.sib4, sizeof(NR_SIB4_t) );
          LOG_I( RRC, "[UE %"PRIu8"] Frame %"PRIu32" Found SIB4 from gNB %"PRIu8"\n", ctxt_pP->module_id, ctxt_pP->frame, gNB_index );
        }

        break;

      case NR_SystemInformation_IEs__sib_TypeAndInfo__Member_PR_sib5:
        if ((NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus&16) == 0) {
          NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus|=16;
          new_sib=1;
          memcpy( NR_UE_rrc_inst[ctxt_pP->module_id].sib5[gNB_index], typeandinfo->choice.sib5, sizeof(NR_SIB5_t) );
          LOG_I( RRC, "[UE %"PRIu8"] Frame %"PRIu32" Found SIB5 from gNB %"PRIu8"\n", ctxt_pP->module_id, ctxt_pP->frame, gNB_index );
          //dump_sib5(NR_UE_rrc_inst[ctxt_pP->module_id].sib5[gNB_index]);
        }

        break;

      case NR_SystemInformation_IEs__sib_TypeAndInfo__Member_PR_sib6:
        if ((NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus&32) == 0) {
          NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus|=32;
          new_sib=1;
          memcpy( NR_UE_rrc_inst[ctxt_pP->module_id].sib6[gNB_index], typeandinfo->choice.sib6, sizeof(NR_SIB6_t) );
          LOG_I( RRC, "[UE %"PRIu8"] Frame %"PRIu32" Found SIB6 from gNB %"PRIu8"\n", ctxt_pP->module_id, ctxt_pP->frame, gNB_index );
        }

        break;

      case NR_SystemInformation_IEs__sib_TypeAndInfo__Member_PR_sib7:
        if ((NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus&64) == 0) {
          NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus|=64;
          new_sib=1;
          memcpy( NR_UE_rrc_inst[ctxt_pP->module_id].sib7[gNB_index], typeandinfo->choice.sib7, sizeof(NR_SIB7_t) );
          LOG_I( RRC, "[UE %"PRIu8"] Frame %"PRIu32" Found SIB7 from gNB %"PRIu8"\n", ctxt_pP->module_id, ctxt_pP->frame, gNB_index );
        }

        break;

      case NR_SystemInformation_IEs__sib_TypeAndInfo__Member_PR_sib8:
        if ((NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus&128) == 0) {
          NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus|=128;
          new_sib=1;
          memcpy( NR_UE_rrc_inst[ctxt_pP->module_id].sib8[gNB_index], typeandinfo->choice.sib8, sizeof(NR_SIB8_t) );
          LOG_I( RRC, "[UE %"PRIu8"] Frame %"PRIu32" Found SIB8 from gNB %"PRIu8"\n", ctxt_pP->module_id, ctxt_pP->frame, gNB_index );
        }

        break;

      case NR_SystemInformation_IEs__sib_TypeAndInfo__Member_PR_sib9:
        if ((NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus&256) == 0) {
          NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus|=256;
          new_sib=1;
          memcpy( NR_UE_rrc_inst[ctxt_pP->module_id].sib9[gNB_index], typeandinfo->choice.sib9, sizeof(NR_SIB9_t) );
          LOG_I( RRC, "[UE %"PRIu8"] Frame %"PRIu32" Found SIB9 from gNB %"PRIu8"\n", ctxt_pP->module_id, ctxt_pP->frame, gNB_index );
        }

        break;

      case NR_SystemInformation_IEs__sib_TypeAndInfo__Member_PR_sib10_v1610:
        if ((NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus&512) == 0) {
          NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus|=512;
          new_sib=1;
          memcpy( NR_UE_rrc_inst[ctxt_pP->module_id].sib10[gNB_index], typeandinfo->choice.sib10_v1610, sizeof(NR_SIB10_r16_t) );
          LOG_I( RRC, "[UE %"PRIu8"] Frame %"PRIu32" Found SIB10 from gNB %"PRIu8"\n", ctxt_pP->module_id, ctxt_pP->frame, gNB_index );
        }

        break;

      case NR_SystemInformation_IEs__sib_TypeAndInfo__Member_PR_sib11_v1610:
        if ((NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus&1024) == 0) {
          NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus|=1024;
          new_sib=1;
          memcpy( NR_UE_rrc_inst[ctxt_pP->module_id].sib11[gNB_index], typeandinfo->choice.sib11_v1610, sizeof(NR_SIB11_r16_t) );
          LOG_I( RRC, "[UE %"PRIu8"] Frame %"PRIu32" Found SIB11 from gNB %"PRIu8"\n", ctxt_pP->module_id, ctxt_pP->frame, gNB_index );
        }

        break;

      case NR_SystemInformation_IEs__sib_TypeAndInfo__Member_PR_sib12_v1610:
        if ((NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus&2048) == 0) {
          NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus|=2048;
          new_sib=1;
          memcpy( NR_UE_rrc_inst[ctxt_pP->module_id].sib12[gNB_index], typeandinfo->choice.sib12_v1610, sizeof(NR_SIB12_r16_t) );
          LOG_I( RRC, "[UE %"PRIu8"] Frame %"PRIu32" Found SIB12 from gNB %"PRIu8"\n", ctxt_pP->module_id, ctxt_pP->frame, gNB_index );
        }

        break;

      case NR_SystemInformation_IEs__sib_TypeAndInfo__Member_PR_sib13_v1610:
        if ((NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus&4096) == 0) {
          NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus|=4096;
          new_sib=1;
          memcpy( NR_UE_rrc_inst[ctxt_pP->module_id].sib13[gNB_index], typeandinfo->choice.sib13_v1610, sizeof(NR_SIB13_r16_t) );
          LOG_I( RRC, "[UE %"PRIu8"] Frame %"PRIu32" Found SIB13 from gNB %"PRIu8"\n", ctxt_pP->module_id, ctxt_pP->frame, gNB_index );
          //dump_sib13( NR_UE_rrc_inst[ctxt_pP->module_id].sib13[gNB_index] );
          // adding here function to store necessary parameters for using in decode_MCCH_Message + maybe transfer to PHY layer
          LOG_I( RRC, "[FRAME %05"PRIu32"][RRC_UE][MOD %02"PRIu8"][][--- MAC_CONFIG_REQ (SIB13 params gNB %"PRIu8") --->][MAC_UE][MOD %02"PRIu8"][]\n",
                 ctxt_pP->frame, ctxt_pP->module_id, gNB_index, ctxt_pP->module_id);
          // TODO rrc_mac_config_req_ue
        }
        break;

      case NR_SystemInformation_IEs__sib_TypeAndInfo__Member_PR_sib14_v1610:
        if ((NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus&8192) == 0) {
          NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus|=8192;
          new_sib=1;
          memcpy( NR_UE_rrc_inst[ctxt_pP->module_id].sib12[gNB_index], typeandinfo->choice.sib14_v1610, sizeof(NR_SIB14_r16_t) );
          LOG_I( RRC, "[UE %"PRIu8"] Frame %"PRIu32" Found SIB14 from gNB %"PRIu8"\n", ctxt_pP->module_id, ctxt_pP->frame, gNB_index );
        }
      
      break;

      default:
        break;
    }
    if (new_sib == 1) {
      NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIcnt++;
  
      if (NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIcnt == sib1->si_SchedulingInfo->schedulingInfoList.list.count)
        nr_rrc_set_sub_state( ctxt_pP->module_id, RRC_SUB_STATE_IDLE_SIB_COMPLETE );
  
Xue Song's avatar
Xue Song committed
1002
      LOG_I(NR_RRC,"SIStatus %x, SIcnt %d/%d\n",
yaojie's avatar
yaojie committed
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
            NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus,
            NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIcnt,
            sib1->si_SchedulingInfo->schedulingInfoList.list.count);
    }
  }

  //if (new_sib == 1) {
  //  NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIcnt++;

  //  if (NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIcnt == sib1->schedulingInfoList.list.count)
  //    rrc_set_sub_state( ctxt_pP->module_id, RRC_SUB_STATE_IDLE_SIB_COMPLETE );

  //  LOG_I(RRC,"SIStatus %x, SIcnt %d/%d\n",
  //        NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus,
  //        NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIcnt,
  //        sib1->schedulingInfoList.list.count);
  //}

  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_RRC_UE_DECODE_SI, VCD_FUNCTION_OUT);
1022 1023 1024
  return 0;
}

yaojie's avatar
yaojie committed
1025 1026 1027 1028 1029 1030
int nr_decode_SIB1( const protocol_ctxt_t *const ctxt_pP, const uint8_t gNB_index, const uint8_t rsrq, const uint8_t rsrp ) {
  NR_SIB1_t *sib1 = NR_UE_rrc_inst[ctxt_pP->module_id].sib1[gNB_index];
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_RRC_UE_DECODE_SIB1, VCD_FUNCTION_IN );
  LOG_I( RRC, "[UE %d] : Dumping SIB 1\n", ctxt_pP->module_id );
  const int n = sib1->cellAccessRelatedInfo.plmn_IdentityList.list.count;
  for (int i = 0; i < n; ++i) {
yaojie's avatar
yaojie committed
1031
    NR_PLMN_Identity_t *PLMN_identity = sib1->cellAccessRelatedInfo.plmn_IdentityList.list.array[i]->plmn_IdentityList.list.array[0];
yaojie's avatar
yaojie committed
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
    int mccdigits = PLMN_identity->mcc->list.count;
    int mncdigits = PLMN_identity->mnc.list.count;

    int mcc;
    if (mccdigits == 2) {
      mcc = *PLMN_identity->mcc->list.array[0]*10 + *PLMN_identity->mcc->list.array[1];
    } else {
      mcc = *PLMN_identity->mcc->list.array[0]*100 + *PLMN_identity->mcc->list.array[1]*10 + *PLMN_identity->mcc->list.array[2];
    }

    int mnc;
    if (mncdigits == 2) {
      mnc = *PLMN_identity->mnc.list.array[0]*10 + *PLMN_identity->mnc.list.array[1];
    } else {
      mnc = *PLMN_identity->mnc.list.array[0]*100 + *PLMN_identity->mnc.list.array[1]*10 + *PLMN_identity->mnc.list.array[2];
    }

    LOG_I( RRC, "PLMN %d MCC %0*d, MNC %0*d\n", i + 1, mccdigits, mcc, mncdigits, mnc);
    // search internal table for provider name
    int plmn_ind = 0;

    while (plmn_data[plmn_ind].mcc > 0) {
      if ((plmn_data[plmn_ind].mcc == mcc) && (plmn_data[plmn_ind].mnc == mnc)) {
        LOG_I( RRC, "Found %s (name from internal table)\n", plmn_data[plmn_ind].oper_short );
        break;
      }

      plmn_ind++;
    }
  }
  LOG_I( RRC, "TAC 0x%04x\n",
         ((sib1->cellAccessRelatedInfo.plmn_IdentityList.list.array[0]->trackingAreaCode->size == 2)?((sib1->cellAccessRelatedInfo.plmn_IdentityList.list.array[0]->trackingAreaCode->buf[0]<<8) + sib1->cellAccessRelatedInfo.plmn_IdentityList.list.array[0]->trackingAreaCode->buf[1]):0));
  LOG_I( RRC, "cellReservedForOperatorUse                 : raw:%ld decoded:%s\n", sib1->cellAccessRelatedInfo.plmn_IdentityList.list.array[0]->cellReservedForOperatorUse,
yaojie's avatar
yaojie committed
1065
         nr_SIBreserved(sib1->cellAccessRelatedInfo.plmn_IdentityList.list.array[0]->cellReservedForOperatorUse) );
yaojie's avatar
yaojie committed
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

  LOG_I( RRC, "cellAccessRelatedInfo.cellIdentity         : raw:%"PRIu32" decoded:%02x.%02x.%02x.%02x\n",
         BIT_STRING_to_uint32( &sib1->cellAccessRelatedInfo.plmn_IdentityList.list.array[0]->cellIdentity ),
         sib1->cellAccessRelatedInfo.plmn_IdentityList.list.array[0]->cellIdentity.buf[0],
         sib1->cellAccessRelatedInfo.plmn_IdentityList.list.array[0]->cellIdentity.buf[1],
         sib1->cellAccessRelatedInfo.plmn_IdentityList.list.array[0]->cellIdentity.buf[2],
         sib1->cellAccessRelatedInfo.plmn_IdentityList.list.array[0]->cellIdentity.buf[3] >> sib1->cellAccessRelatedInfo.plmn_IdentityList.list.array[0]->cellIdentity.bits_unused);
  //LOG_I( RRC, "cellAccessRelatedInfo.cellBarred           : raw:%ld decoded:%s\n", sib1->cellAccessRelatedInfo.cellBarred, SIBbarred(sib1->cellAccessRelatedInfo.cellBarred) );
  //LOG_I( RRC, "cellAccessRelatedInfo.intraFreqReselection : raw:%ld decoded:%s\n", sib1->cellAccessRelatedInfo.intraFreqReselection, SIBallowed(sib1->cellAccessRelatedInfo.intraFreqReselection) );
  //LOG_I( RRC, "cellAccessRelatedInfo.csg_Indication       : %d\n", sib1->cellAccessRelatedInfo.csg_Indication );

  //if (sib1->cellAccessRelatedInfo.csg_Identity)
  //  LOG_I( RRC, "cellAccessRelatedInfo.csg_Identity         : %"PRIu32"\n", BIT_STRING_to_uint32(sib1->cellAccessRelatedInfo.csg_Identity) );
  //else
  //  LOG_I( RRC, "cellAccessRelatedInfo.csg_Identity         : not defined\n" );

  LOG_I( RRC, "cellSelectionInfo.q_RxLevMin               : %ld\n", sib1->cellSelectionInfo->q_RxLevMin );

  if (sib1->cellSelectionInfo->q_RxLevMinOffset)
    LOG_I( RRC, "cellSelectionInfo.q_RxLevMinOffset         : %ld\n", *sib1->cellSelectionInfo->q_RxLevMinOffset );
  else
    LOG_I( RRC, "cellSelectionInfo.q_RxLevMinOffset         : not defined\n" );

  //if (sib1->p_Max)
  //  LOG_I( RRC, "p_Max                                      : %ld\n", *sib1->p_Max );
  //else
  //  LOG_I( RRC, "p_Max                                      : not defined\n" );

  //LOG_I( RRC, "freqBandIndicator                          : %ld\n", sib1->freqBandIndicator );

  if (sib1->si_SchedulingInfo->schedulingInfoList.list.count > 0) {
    for (int i=0; i<sib1->si_SchedulingInfo->schedulingInfoList.list.count; i++) {
      LOG_I( RRC, "si_Periodicity[%d]                          : %s\n", i, SIBPeriod[min(sib1->si_SchedulingInfo->schedulingInfoList.list.array[i]->si_Periodicity,7)]);

      if (sib1->si_SchedulingInfo->schedulingInfoList.list.array[i]->sib_MappingInfo.list.count > 0) {
        char temp[32 * sizeof(SIBType[0])] = {0}; // maxSIB==32

        for (int j=0; j<sib1->si_SchedulingInfo->schedulingInfoList.list.array[i]->sib_MappingInfo.list.count; j++) {
          sprintf( temp + j*sizeof(SIBType[0]), "%*s ", (int)sizeof(SIBType[0])-1, SIBType[min(sib1->si_SchedulingInfo->schedulingInfoList.list.array[i]->sib_MappingInfo.list.array[0]->type,11)] );
        }

        LOG_I( RRC, "siSchedulingInfoSIBType[%d]                 : %s\n", i, temp );
      } else {
        LOG_I( RRC, "mapping list %d is null\n", i );
      }
    }
  } else {
    LOG_E( RRC, "siSchedulingInfoPeriod[0]                  : PROBLEM!!!\n" );
    return -1;
  }

yaojie's avatar
yaojie committed
1117 1118 1119
  if (sib1->servingCellConfigCommon->tdd_UL_DL_ConfigurationCommon) {
      //TODO
  }
yaojie's avatar
yaojie committed
1120 1121 1122 1123 1124

  LOG_I( RRC, "siWindowLength                             : %s\n", siWindowLength[min(sib1->si_SchedulingInfo->si_WindowLength,8)] );
  //LOG_I( RRC, "systemInfoValueTag                         : %ld\n", sib1->systemInfoValueTag );
  NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIperiod     = siPeriod_int[sib1->si_SchedulingInfo->schedulingInfoList.list.array[0]->si_Periodicity];
  NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIwindowsize = siWindowLength_int[sib1->si_SchedulingInfo->si_WindowLength];
yaojie's avatar
yaojie committed
1125
  LOG_I( RRC, "[FRAME unknown][RRC_UE][MOD %02"PRIu8"][][--- MAC_CONFIG_REQ (SIB1 params gNB %"PRIu8") --->][MAC_UE][MOD %02"PRIu8"][]\n",
yaojie's avatar
yaojie committed
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
         ctxt_pP->module_id, gNB_index, ctxt_pP->module_id );
  //rrc_mac_config_req_ue
  LOG_I(RRC,"Setting SIStatus bit 0 to 1\n");
  NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus = 1;
  //NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIB1systemInfoValueTag = sib1->systemInfoValueTag;

  if (AMF_MODE_ENABLED) {
    int cell_valid = 0;

    //if (sib1->cellAccessRelatedInfo.cellBarred == LTE_SystemInformationBlockType1__cellAccessRelatedInfo__cellBarred_notBarred) {
      /* Cell is not barred */
      int plmn;
      int plmn_number;
      plmn_number = sib1->cellAccessRelatedInfo.plmn_IdentityList.list.count;

      /* Compare requested PLMN and PLMNs from SIB1*/
      for (plmn = 0; plmn < plmn_number; plmn++) {
        NR_PLMN_Identity_t *plmn_Identity;
        plmn_Identity = sib1->cellAccessRelatedInfo.plmn_IdentityList.list.array[plmn]->plmn_IdentityList.list.array[0];

        if (
          (
            (plmn_Identity->mcc == NULL)
            ||
            (
              (NR_UE_rrc_inst[ctxt_pP->module_id].plmnID.MCCdigit1 == *(plmn_Identity->mcc->list.array[0])) &&
              (NR_UE_rrc_inst[ctxt_pP->module_id].plmnID.MCCdigit2 == *(plmn_Identity->mcc->list.array[1])) &&
              (NR_UE_rrc_inst[ctxt_pP->module_id].plmnID.MCCdigit3 == *(plmn_Identity->mcc->list.array[2]))
            )
          )
          &&
          (NR_UE_rrc_inst[ctxt_pP->module_id].plmnID.MNCdigit1 == *(plmn_Identity->mnc.list.array[0]))
          &&
          (NR_UE_rrc_inst[ctxt_pP->module_id].plmnID.MNCdigit2 == *(plmn_Identity->mnc.list.array[1]))
          &&
          (
            ((NR_UE_rrc_inst[ctxt_pP->module_id].plmnID.MNCdigit3 == 0xf) && (plmn_Identity->mnc.list.count == 2))
            ||
            (NR_UE_rrc_inst[ctxt_pP->module_id].plmnID.MNCdigit3 == *(plmn_Identity->mnc.list.array[2]))
          )
        ) {
          /* PLMN match, send a confirmation to NAS */
          MessageDef  *msg_p;
1169
          msg_p = itti_alloc_new_message(TASK_RRC_NRUE, NAS_CELL_SELECTION_CNF);
yaojie's avatar
yaojie committed
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
          NAS_CELL_SELECTION_CNF (msg_p).errCode = AS_SUCCESS;
          NAS_CELL_SELECTION_CNF (msg_p).cellID = BIT_STRING_to_uint32(&sib1->cellAccessRelatedInfo.plmn_IdentityList.list.array[0]->cellIdentity);
          NAS_CELL_SELECTION_CNF (msg_p).tac = BIT_STRING_to_uint16(sib1->cellAccessRelatedInfo.plmn_IdentityList.list.array[0]->trackingAreaCode);
          NAS_CELL_SELECTION_CNF (msg_p).rat = 0xFF;
          NAS_CELL_SELECTION_CNF (msg_p).rsrq = rsrq;
          NAS_CELL_SELECTION_CNF (msg_p).rsrp = rsrp;
          itti_send_msg_to_task(TASK_NAS_UE, ctxt_pP->instance, msg_p);
          cell_valid = 1;
          NR_UE_rrc_inst[ctxt_pP->module_id].selected_plmn_identity = plmn + 1;
          break;
        }
      }
    //}

    if (cell_valid == 0) {
      /* Cell can not be used, ask PHY to try the next one */
      MessageDef  *msg_p;
1187
      msg_p = itti_alloc_new_message(TASK_RRC_NRUE, PHY_FIND_NEXT_CELL_REQ);
yaojie's avatar
yaojie committed
1188 1189 1190
      itti_send_msg_to_task(TASK_PHY_UE, ctxt_pP->instance, msg_p);
      LOG_E(RRC,
            "Synched with a cell, but PLMN doesn't match our SIM "
yaojie's avatar
yaojie committed
1191
            "(selected_plmn_identity %ld), the message PHY_FIND_NEXT_CELL_REQ "
yaojie's avatar
yaojie committed
1192 1193 1194 1195 1196 1197
            "is sent but lost in current UE implementation!\n",
            NR_UE_rrc_inst[ctxt_pP->module_id].selected_plmn_identity);
    }
  }

  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_RRC_UE_DECODE_SIB1, VCD_FUNCTION_OUT );
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
  return 0;
}
int nr_decode_BCCH_DLSCH_Message(
  const protocol_ctxt_t *const ctxt_pP,
  const uint8_t                gNB_index,
  uint8_t               *const Sdu,
  const uint8_t                Sdu_len,
  const uint8_t                rsrq,
  const uint8_t                rsrp ) {
  NR_BCCH_DL_SCH_Message_t *bcch_message = NULL;
  NR_SIB1_t *sib1 = NR_UE_rrc_inst[ctxt_pP->module_id].sib1[gNB_index];
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_DECODE_BCCH, VCD_FUNCTION_IN );

  if (((NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus&1) == 1) &&  // SIB1 received
      (NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIcnt == sib1->si_SchedulingInfo->schedulingInfoList.list.count)) {
    // to prevent memory bloating
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_DECODE_BCCH, VCD_FUNCTION_OUT );
    return 0;
  }

  nr_rrc_set_sub_state( ctxt_pP->module_id, RRC_SUB_STATE_IDLE_RECEIVING_SIB_NR );

  if ( LOG_DEBUGFLAG(DEBUG_ASN1) ) {
    xer_fprint(stdout, &asn_DEF_NR_BCCH_DL_SCH_Message,(void *)bcch_message );
  }

  asn_dec_rval_t dec_rval = uper_decode_complete( NULL,
                            &asn_DEF_NR_BCCH_DL_SCH_Message,
                            (void **)&bcch_message,
                            (const void *)Sdu,
                            Sdu_len );

  if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) {
    LOG_E( RRC, "[UE %"PRIu8"] Failed to decode BCCH_DLSCH_MESSAGE (%zu bits)\n",
           ctxt_pP->module_id,
           dec_rval.consumed );
    log_dump(RRC, Sdu, Sdu_len, LOG_DUMP_CHAR,"   Received bytes:\n" );
    // free the memory
    SEQUENCE_free( &asn_DEF_LTE_BCCH_DL_SCH_Message, (void *)bcch_message, 1 );
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_DECODE_BCCH, VCD_FUNCTION_OUT );
    return -1;
  }

  if (bcch_message->message.present == NR_BCCH_DL_SCH_MessageType_PR_c1) {
    switch (bcch_message->message.choice.c1->present) {
      case NR_BCCH_DL_SCH_MessageType__c1_PR_systemInformationBlockType1:
        if ((ctxt_pP->frame % 2) == 0) {
          // even frame
          if ((NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus&1) == 0) {
            NR_SIB1_t *sib1 = NR_UE_rrc_inst[ctxt_pP->module_id].sib1[gNB_index];
            memcpy( (void *)sib1,
                    (void *)bcch_message->message.choice.c1->choice.systemInformationBlockType1,
                    sizeof(NR_SIB1_t) );
            LOG_D( RRC, "[UE %"PRIu8"] Decoding First SIB1\n", ctxt_pP->module_id );
            nr_decode_SIB1( ctxt_pP, gNB_index, rsrq, rsrp );
          }
        }

        break;

      case NR_BCCH_DL_SCH_MessageType__c1_PR_systemInformation:
        if ((NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].SIStatus&1) == 1) {
          // SIB1 with schedulingInfoList is available
          NR_SystemInformation_t *si = NR_UE_rrc_inst[ctxt_pP->module_id].si[gNB_index];
          memcpy( si,
                  bcch_message->message.choice.c1->choice.systemInformation,
                  sizeof(NR_SystemInformation_t) );
          LOG_I( RRC, "[UE %"PRIu8"] Decoding SI for frameP %"PRIu32"\n",
                 ctxt_pP->module_id,
                 ctxt_pP->frame );
          nr_decode_SI( ctxt_pP, gNB_index );
          //if (nfapi_mode == 3)
          //UE_mac_inst[ctxt_pP->module_id].SI_Decoded = 1;
        }

        break;

      case NR_BCCH_DL_SCH_MessageType__c1_PR_NOTHING:
      default:
        break;
    }
  }

  if (nr_rrc_get_sub_state(ctxt_pP->module_id) == RRC_SUB_STATE_IDLE_SIB_COMPLETE_NR) {
    //if ( (NR_UE_rrc_inst[ctxt_pP->module_id].initialNasMsg.data != NULL) || (!AMF_MODE_ENABLED)) {
      rrc_ue_generate_RRCSetupRequest(ctxt_pP, 0);
      nr_rrc_set_sub_state( ctxt_pP->module_id, RRC_SUB_STATE_IDLE_CONNECTING );
    //}
  }

  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_DECODE_BCCH, VCD_FUNCTION_OUT );
  return 0;
}
1291

yaojie's avatar
yaojie committed
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
//-----------------------------------------------------------------------------
void
nr_rrc_ue_process_masterCellGroup(
  const protocol_ctxt_t *const ctxt_pP,
  uint8_t gNB_index,
  OCTET_STRING_t *masterCellGroup
)
//-----------------------------------------------------------------------------
{
    NR_CellGroupConfig_t *cellGroupConfig = (NR_CellGroupConfig_t *)masterCellGroup;
    if( cellGroupConfig->spCellConfig != NULL &&  cellGroupConfig->spCellConfig->reconfigurationWithSync != NULL){
	    //TODO (perform Reconfiguration with sync according to 5.3.5.5.2)
	    //TODO (resume all suspended radio bearers and resume SCG transmission for all radio bearers, if suspended)
    }

    if( cellGroupConfig->rlc_BearerToReleaseList != NULL){
      //TODO (perform RLC bearer release as specified in 5.3.5.5.3)
    }

    if( cellGroupConfig->rlc_BearerToAddModList != NULL){
      //TODO (perform the RLC bearer addition/modification as specified in 5.3.5.5.4)
    }

    if( cellGroupConfig->mac_CellGroupConfig != NULL){
      //TODO (configure the MAC entity of this cell group as specified in 5.3.5.5.5)
    }

    if( cellGroupConfig->sCellToReleaseList != NULL){
      //TODO (perform SCell release as specified in 5.3.5.5.8)
    }

    if( cellGroupConfig->spCellConfig != NULL){
1324 1325 1326 1327 1328 1329
      if (NR_UE_rrc_inst[ctxt_pP->module_id].cell_group_config->spCellConfig) {
        memcpy(NR_UE_rrc_inst[ctxt_pP->module_id].cell_group_config->spCellConfig,cellGroupConfig->spCellConfig,
               sizeof(struct NR_SpCellConfig));
      } else {
        NR_UE_rrc_inst[ctxt_pP->module_id].cell_group_config->spCellConfig = cellGroupConfig->spCellConfig;
      }
yaojie's avatar
yaojie committed
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
      //TODO (configure the SpCell as specified in 5.3.5.5.7)
    }

    if( cellGroupConfig->sCellToAddModList != NULL){
      //TODO (perform SCell addition/modification as specified in 5.3.5.5.9)
    }

    if( cellGroupConfig->ext2->bh_RLC_ChannelToReleaseList_r16 != NULL){
      //TODO (perform the BH RLC channel addition/modification as specified in 5.3.5.5.11)
    }

    if( cellGroupConfig->ext2->bh_RLC_ChannelToAddModList_r16 != NULL){
      //TODO (perform the BH RLC channel addition/modification as specified in 5.3.5.5.11)
    }

}

1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
/*--------------------------------------------------*/
static void rrc_ue_generate_RRCSetupComplete(
    const protocol_ctxt_t *const ctxt_pP,
    const uint8_t gNB_index,
    const uint8_t Transaction_id,
    uint8_t sel_plmn_id){
        uint8_t buffer[100];
        uint8_t size;
        const char *nas_msg;
        int   nas_msg_length;
yaojie's avatar
yaojie committed
1357
       if (AMF_MODE_ENABLED) {
1358 1359 1360 1361 1362 1363
#if ITTI_SIM
          as_nas_info_t initialNasMsg;
          generateRegistrationRequest(&initialNasMsg);
          nas_msg = (char*)initialNasMsg.data;
          nas_msg_length = initialNasMsg.length;
#else
yaojie's avatar
yaojie committed
1364 1365
          nas_msg         = (char *) NR_UE_rrc_inst[ctxt_pP->module_id].initialNasMsg.data;
          nas_msg_length  = NR_UE_rrc_inst[ctxt_pP->module_id].initialNasMsg.length;
1366
#endif
yaojie's avatar
yaojie committed
1367 1368 1369 1370
           } else {
          nas_msg         = nr_nas_attach_req_imsi;
          nas_msg_length  = sizeof(nr_nas_attach_req_imsi);
       }
1371
       size = do_RRCSetupComplete(ctxt_pP->module_id,buffer,Transaction_id,sel_plmn_id,nas_msg_length,nas_msg);
Xue Song's avatar
Xue Song committed
1372
       LOG_I(NR_RRC,"[UE %d][RAPROC] Frame %d : Logical Channel UL-DCCH (SRB1), Generating RRCSetupComplete (bytes%d, gNB %d)\n",
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
        ctxt_pP->module_id,ctxt_pP->frame, size, gNB_index);
       LOG_D(RLC,
            "[FRAME %05d][RRC_UE][MOD %02d][][--- PDCP_DATA_REQ/%d Bytes (RRCConnectionSetupComplete to gNB %d MUI %d) --->][PDCP][MOD %02d][RB %02d]\n",
            ctxt_pP->frame, ctxt_pP->module_id+NB_RN_INST, size, gNB_index, nr_rrc_mui, ctxt_pP->module_id+NB_eNB_INST, DCCH);
        // ctxt_pP_local.rnti = ctxt_pP->rnti;
        rrc_data_req_ue(
            ctxt_pP,
            DCCH,
            nr_rrc_mui++,
            SDU_CONFIRM_NO,
            size,
            buffer,
            PDCP_TRANSMISSION_MODE_CONTROL);
Xue Song's avatar
Xue Song committed
1386 1387 1388
#ifdef ITTI_SIM
        MessageDef *message_p;
        uint8_t *message_buffer;
1389
        message_buffer = itti_malloc (TASK_RRC_NRUE, TASK_RRC_GNB_SIM, size);
Xue Song's avatar
Xue Song committed
1390
        memcpy (message_buffer, buffer, size);
1391
        message_p = itti_alloc_new_message (TASK_RRC_NRUE, UE_RRC_DCCH_DATA_IND);
Xue Song's avatar
Xue Song committed
1392 1393 1394 1395 1396
        UE_RRC_DCCH_DATA_IND (message_p).rbid = 1;
        UE_RRC_DCCH_DATA_IND (message_p).sdu = message_buffer;
        UE_RRC_DCCH_DATA_IND (message_p).size  = size;
        itti_send_msg_to_task (TASK_RRC_GNB_SIM, ctxt_pP->instance, message_p);
#endif
1397 1398
}

yaojie's avatar
yaojie committed
1399
int8_t nr_rrc_ue_decode_ccch( const protocol_ctxt_t *const ctxt_pP, const NR_SRB_INFO *const Srb_info, const uint8_t gNB_index ){
yaojie's avatar
yaojie committed
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
    NR_DL_CCCH_Message_t *dl_ccch_msg=NULL;
    asn_dec_rval_t dec_rval;
    int rval=0;
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_DECODE_CCCH, VCD_FUNCTION_IN);
    //    LOG_D(RRC,"[UE %d] Decoding DL-CCCH message (%d bytes), State %d\n",ue_mod_idP,Srb_info->Rx_buffer.payload_size,
    //    NR_UE_rrc_inst[ue_mod_idP].Info[gNB_index].State);
    dec_rval = uper_decode(NULL,
                           &asn_DEF_NR_DL_CCCH_Message,
                           (void **)&dl_ccch_msg,
                           (uint8_t *)Srb_info->Rx_buffer.Payload,
                           100,0,0);
    
Xue Song's avatar
Xue Song committed
1412
    // if ( LOG_DEBUGFLAG(DEBUG_ASN1) ) {
yaojie's avatar
yaojie committed
1413
      xer_fprint(stdout,&asn_DEF_NR_DL_CCCH_Message,(void *)dl_ccch_msg);
Xue Song's avatar
Xue Song committed
1414
    // }
yaojie's avatar
yaojie committed
1415 1416 1417 1418 1419 1420 1421 1422
    
    if ((dec_rval.code != RC_OK) && (dec_rval.consumed==0)) {
      LOG_E(RRC,"[UE %d] Frame %d : Failed to decode DL-CCCH-Message (%zu bytes)\n",ctxt_pP->module_id,ctxt_pP->frame,dec_rval.consumed);
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_DECODE_CCCH, VCD_FUNCTION_OUT);
      return -1;
    }
    
    if (dl_ccch_msg->message.present == NR_DL_CCCH_MessageType_PR_c1) {
yaojie's avatar
yaojie committed
1423
      if (NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].State == NR_RRC_SI_RECEIVED) {
yaojie's avatar
yaojie committed
1424 1425
        switch (dl_ccch_msg->message.choice.c1->present) {
          case NR_DL_CCCH_MessageType__c1_PR_NOTHING:
Xue Song's avatar
Xue Song committed
1426
            LOG_I(NR_RRC, "[UE%d] Frame %d : Received PR_NOTHING on DL-CCCH-Message\n",
yaojie's avatar
yaojie committed
1427 1428 1429 1430 1431 1432
                  ctxt_pP->module_id,
                  ctxt_pP->frame);
            rval = 0;
            break;
    
          case NR_DL_CCCH_MessageType__c1_PR_rrcReject:
Xue Song's avatar
Xue Song committed
1433
            LOG_I(NR_RRC,
yaojie's avatar
yaojie committed
1434 1435 1436 1437 1438 1439 1440
                  "[UE%d] Frame %d : Logical Channel DL-CCCH (SRB0), Received RRCConnectionReject \n",
                  ctxt_pP->module_id,
                  ctxt_pP->frame);
            rval = 0;
            break;
    
          case NR_DL_CCCH_MessageType__c1_PR_rrcSetup:
Xue Song's avatar
Xue Song committed
1441
            LOG_I(NR_RRC,
yaojie's avatar
yaojie committed
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
                  "[UE%d][RAPROC] Frame %d : Logical Channel DL-CCCH (SRB0), Received NR_RRCSetup RNTI %x\n",
                  ctxt_pP->module_id,
                  ctxt_pP->frame,
                  ctxt_pP->rnti);
            // Get configuration
            // Release T300 timer
            NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].T300_active = 0;
            
            nr_rrc_ue_process_masterCellGroup(
              ctxt_pP,
              gNB_index,
              &dl_ccch_msg->message.choice.c1->choice.rrcSetup->criticalExtensions.choice.rrcSetup->masterCellGroup);
1454
            nr_sa_rrc_ue_process_radioBearerConfig(
yaojie's avatar
yaojie committed
1455 1456 1457
              ctxt_pP,
              gNB_index,
              &dl_ccch_msg->message.choice.c1->choice.rrcSetup->criticalExtensions.choice.rrcSetup->radioBearerConfig);
1458 1459
            nr_rrc_set_state (ctxt_pP->module_id, RRC_STATE_CONNECTED);
            nr_rrc_set_sub_state (ctxt_pP->module_id, RRC_SUB_STATE_CONNECTED);
yaojie's avatar
yaojie committed
1460
            NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].rnti = ctxt_pP->rnti;
1461
            rrc_ue_generate_RRCSetupComplete(
yaojie's avatar
yaojie committed
1462 1463
              ctxt_pP,
              gNB_index,
1464 1465
              dl_ccch_msg->message.choice.c1->choice.rrcSetup->rrc_TransactionIdentifier,
              NR_UE_rrc_inst[ctxt_pP->module_id].selected_plmn_identity);
yaojie's avatar
yaojie committed
1466 1467 1468 1469
            rval = 0;
            break;
    
          default:
Xue Song's avatar
Xue Song committed
1470
            LOG_E(NR_RRC, "[UE%d] Frame %d : Unknown message\n",
yaojie's avatar
yaojie committed
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
                  ctxt_pP->module_id,
                  ctxt_pP->frame);
            rval = -1;
            break;
        }
      }
    }
    
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_DECODE_CCCH, VCD_FUNCTION_OUT);
    return rval;

}

1484 1485 1486 1487 1488 1489

// from NR SRB3
int8_t nr_rrc_ue_decode_NR_DL_DCCH_Message(
    const module_id_t module_id,
    const uint8_t     gNB_index,
    const uint8_t    *bufferP,
1490
    const uint32_t    buffer_len ){
1491
    //  uper_decode by nr R15 rrc_connection_reconfiguration
1492
    
1493 1494
    int32_t i;
    NR_DL_DCCH_Message_t *nr_dl_dcch_msg = NULL;
Qiu DeZhi's avatar
Qiu DeZhi committed
1495
    MessageDef *msg_p;
1496 1497 1498 1499 1500

    asn_dec_rval_t dec_rval = uper_decode(  NULL,
                                            &asn_DEF_NR_DL_DCCH_Message,    
                                            (void**)&nr_dl_dcch_msg,
                                            (uint8_t *)bufferP,
1501
                                            buffer_len, 0, 0);
1502

1503
    if ((dec_rval.code != RC_OK) || (dec_rval.consumed == 0)) {
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517

        for (i=0; i<buffer_len; i++)
            printf("%02x ",bufferP[i]);

        printf("\n");
        // free the memory
        SEQUENCE_free( &asn_DEF_NR_DL_DCCH_Message, (void *)nr_dl_dcch_msg, 1 );
        return -1;
    }

    if(nr_dl_dcch_msg != NULL){
        switch(nr_dl_dcch_msg->message.present){            
            case NR_DL_DCCH_MessageType_PR_c1:

1518
                switch(nr_dl_dcch_msg->message.choice.c1->present){
1519
                    case NR_DL_DCCH_MessageType__c1_PR_rrcReconfiguration:
1520
                        nr_rrc_ue_process_rrcReconfiguration(nr_dl_dcch_msg->message.choice.c1->choice.rrcReconfiguration);
1521 1522 1523
                        break;

                    case NR_DL_DCCH_MessageType__c1_PR_NOTHING:
1524 1525
                    case NR_DL_DCCH_MessageType__c1_PR_rrcResume:
                    case NR_DL_DCCH_MessageType__c1_PR_rrcRelease:
Qiu DeZhi's avatar
Qiu DeZhi committed
1526
                        msg_p = itti_alloc_new_message(TASK_RRC_NRUE, NAS_CONN_RELEASE_IND);
Qiu DeZhi's avatar
Qiu DeZhi committed
1527 1528 1529 1530 1531 1532 1533 1534

                        if((nr_dl_dcch_msg->message.choice.c1->choice.rrcRelease->criticalExtensions.present == NR_RRCRelease__criticalExtensions_PR_rrcRelease) &&
                           (nr_dl_dcch_msg->message.choice.c1->present == NR_DL_DCCH_MessageType__c1_PR_rrcRelease)){
                             nr_dl_dcch_msg->message.choice.c1->choice.rrcRelease->criticalExtensions.choice.rrcRelease->deprioritisationReq->deprioritisationTimer =
                             NR_RRCRelease_IEs__deprioritisationReq__deprioritisationTimer_min5;
                             nr_dl_dcch_msg->message.choice.c1->choice.rrcRelease->criticalExtensions.choice.rrcRelease->deprioritisationReq->deprioritisationType =
                             NR_RRCRelease_IEs__deprioritisationReq__deprioritisationType_frequency;
                           }
Qiu DeZhi's avatar
Qiu DeZhi committed
1535
			   itti_send_msg_to_task(TASK_RRC_NRUE,module_id,msg_p);
Qiu DeZhi's avatar
Qiu DeZhi committed
1536
                        break;
1537 1538 1539 1540 1541 1542
                    case NR_DL_DCCH_MessageType__c1_PR_rrcReestablishment:
                    case NR_DL_DCCH_MessageType__c1_PR_securityModeCommand:
                    case NR_DL_DCCH_MessageType__c1_PR_dlInformationTransfer:
                    case NR_DL_DCCH_MessageType__c1_PR_ueCapabilityEnquiry:
                    case NR_DL_DCCH_MessageType__c1_PR_counterCheck:
                    case NR_DL_DCCH_MessageType__c1_PR_mobilityFromNRCommand:
1543 1544 1545 1546
                    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:
1547 1548 1549 1550
                    case NR_DL_DCCH_MessageType__c1_PR_spare3:
                    case NR_DL_DCCH_MessageType__c1_PR_spare2:
                    case NR_DL_DCCH_MessageType__c1_PR_spare1:
                    default:
1551
                        //  not supported or unused
1552 1553 1554 1555 1556 1557
                        break;
                }   
                break;
            case NR_DL_DCCH_MessageType_PR_NOTHING:
            case NR_DL_DCCH_MessageType_PR_messageClassExtension:
            default:
1558
                //  not supported or unused
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
                break;
        }
        
        //  release memory allocation
        SEQUENCE_free( &asn_DEF_NR_DL_DCCH_Message, (void *)nr_dl_dcch_msg, 1 );
    }else{
        //  log..
    }

    return 0;

1570
}
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585


//-----------------------------------------------------------------------------
void
nr_rrc_ue_process_securityModeCommand(
  const protocol_ctxt_t *const ctxt_pP,
  NR_SecurityModeCommand_t *const securityModeCommand,
  const uint8_t                gNB_index
)
//-----------------------------------------------------------------------------
{
  asn_enc_rval_t enc_rval;
  NR_UL_DCCH_Message_t ul_dcch_msg;
  uint8_t buffer[200];
  int i, securityMode;
Xue Song's avatar
Xue Song committed
1586
  LOG_I(NR_RRC,"[UE %d] SFN/SF %d/%d: Receiving from SRB1 (DL-DCCH), Processing securityModeCommand (eNB %d)\n",
1587 1588
        ctxt_pP->module_id,ctxt_pP->frame, ctxt_pP->subframe, gNB_index);

Xue Song's avatar
Xue Song committed
1589
  switch (securityModeCommand->criticalExtensions.choice.securityModeCommand->securityConfigSMC.securityAlgorithmConfig.cipheringAlgorithm) {
1590
    case NR_CipheringAlgorithm_nea0:
Xue Song's avatar
Xue Song committed
1591
      LOG_I(NR_RRC,"[UE %d] Security algorithm is set to nea0\n",
1592 1593 1594 1595 1596
            ctxt_pP->module_id);
      securityMode= NR_CipheringAlgorithm_nea0;
      break;

    case NR_CipheringAlgorithm_nea1:
Xue Song's avatar
Xue Song committed
1597
      LOG_I(NR_RRC,"[UE %d] Security algorithm is set to nea1\n",ctxt_pP->module_id);
1598 1599 1600 1601
      securityMode= NR_CipheringAlgorithm_nea1;
      break;

    case NR_CipheringAlgorithm_nea2:
Xue Song's avatar
Xue Song committed
1602
      LOG_I(NR_RRC,"[UE %d] Security algorithm is set to nea2\n",
1603 1604 1605 1606 1607
            ctxt_pP->module_id);
      securityMode = NR_CipheringAlgorithm_nea2;
      break;

    default:
Xue Song's avatar
Xue Song committed
1608
      LOG_I(NR_RRC,"[UE %d] Security algorithm is set to none\n",ctxt_pP->module_id);
1609 1610 1611 1612
      securityMode = NR_CipheringAlgorithm_spare1;
      break;
  }

Xue Song's avatar
Xue Song committed
1613
  switch (*securityModeCommand->criticalExtensions.choice.securityModeCommand->securityConfigSMC.securityAlgorithmConfig.integrityProtAlgorithm) {
1614
    case NR_IntegrityProtAlgorithm_nia1:
Xue Song's avatar
Xue Song committed
1615
      LOG_I(NR_RRC,"[UE %d] Integrity protection algorithm is set to nia1\n",ctxt_pP->module_id);
1616 1617 1618 1619
      securityMode |= 1 << 5;
      break;

    case NR_IntegrityProtAlgorithm_nia2:
Xue Song's avatar
Xue Song committed
1620
      LOG_I(NR_RRC,"[UE %d] Integrity protection algorithm is set to nia2\n",ctxt_pP->module_id);
1621 1622 1623 1624
      securityMode |= 1 << 6;
      break;

    default:
Xue Song's avatar
Xue Song committed
1625
      LOG_I(NR_RRC,"[UE %d] Integrity protection algorithm is set to none\n",ctxt_pP->module_id);
1626 1627 1628 1629
      securityMode |= 0x70 ;
      break;
  }

Xue Song's avatar
Xue Song committed
1630
  LOG_D(NR_RRC,"[UE %d] security mode is %x \n",ctxt_pP->module_id, securityMode);
1631
  NR_UE_rrc_inst->cipheringAlgorithm =
Xue Song's avatar
Xue Song committed
1632
    securityModeCommand->criticalExtensions.choice.securityModeCommand->securityConfigSMC.securityAlgorithmConfig.cipheringAlgorithm;
1633
  NR_UE_rrc_inst->integrityProtAlgorithm =
Xue Song's avatar
Xue Song committed
1634
    *securityModeCommand->criticalExtensions.choice.securityModeCommand->securityConfigSMC.securityAlgorithmConfig.integrityProtAlgorithm;
Xue Song's avatar
Xue Song committed
1635
  memset((void *)&ul_dcch_msg,0,sizeof(NR_UL_DCCH_Message_t));
1636
  //memset((void *)&SecurityModeCommand,0,sizeof(SecurityModeCommand_t));
Xue Song's avatar
Xue Song committed
1637 1638
  ul_dcch_msg.message.present           = NR_UL_DCCH_MessageType_PR_c1;
  ul_dcch_msg.message.choice.c1         = calloc(1, sizeof(*ul_dcch_msg.message.choice.c1));
1639 1640

  if (securityMode >= NO_SECURITY_MODE) {
Xue Song's avatar
Xue Song committed
1641
    LOG_I(NR_RRC, "rrc_ue_process_securityModeCommand, security mode complete case \n");
Xue Song's avatar
Xue Song committed
1642
    ul_dcch_msg.message.choice.c1->present = NR_UL_DCCH_MessageType__c1_PR_securityModeComplete;
1643
  } else {
Xue Song's avatar
Xue Song committed
1644
    LOG_I(NR_RRC, "rrc_ue_process_securityModeCommand, security mode failure case \n");
Xue Song's avatar
Xue Song committed
1645
    ul_dcch_msg.message.choice.c1->present = NR_UL_DCCH_MessageType__c1_PR_securityModeFailure;
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
  }

  uint8_t *kRRCenc = NULL;
  uint8_t *kUPenc = NULL;
  uint8_t *kRRCint = NULL;
  pdcp_t *pdcp_p = NULL;
  hash_key_t key = HASHTABLE_NOT_A_KEY_VALUE;
  hashtable_rc_t h_rc;
  key = PDCP_COLL_KEY_VALUE(ctxt_pP->module_id, ctxt_pP->rnti,
                            ctxt_pP->enb_flag, DCCH, SRB_FLAG_YES);
  h_rc = hashtable_get(pdcp_coll_p, key, (void **) &pdcp_p);

  if (h_rc == HASH_TABLE_OK) {
Xue Song's avatar
Xue Song committed
1659 1660
    LOG_D(NR_RRC, "PDCP_COLL_KEY_VALUE() returns valid key = %ld\n", key);
    LOG_D(NR_RRC, "driving kRRCenc, kRRCint and kUPenc from KgNB="
1661 1662 1663 1664 1665 1666 1667 1668
          "%02x%02x%02x%02x"
          "%02x%02x%02x%02x"
          "%02x%02x%02x%02x"
          "%02x%02x%02x%02x"
          "%02x%02x%02x%02x"
          "%02x%02x%02x%02x"
          "%02x%02x%02x%02x"
          "%02x%02x%02x%02x\n",
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
          NR_UE_rrc_inst->kgnb[0],  NR_UE_rrc_inst->kgnb[1],  NR_UE_rrc_inst->kgnb[2],  NR_UE_rrc_inst->kgnb[3],
          NR_UE_rrc_inst->kgnb[4],  NR_UE_rrc_inst->kgnb[5],  NR_UE_rrc_inst->kgnb[6],  NR_UE_rrc_inst->kgnb[7],
          NR_UE_rrc_inst->kgnb[8],  NR_UE_rrc_inst->kgnb[9],  NR_UE_rrc_inst->kgnb[10], NR_UE_rrc_inst->kgnb[11],
          NR_UE_rrc_inst->kgnb[12], NR_UE_rrc_inst->kgnb[13], NR_UE_rrc_inst->kgnb[14], NR_UE_rrc_inst->kgnb[15],
          NR_UE_rrc_inst->kgnb[16], NR_UE_rrc_inst->kgnb[17], NR_UE_rrc_inst->kgnb[18], NR_UE_rrc_inst->kgnb[19],
          NR_UE_rrc_inst->kgnb[20], NR_UE_rrc_inst->kgnb[21], NR_UE_rrc_inst->kgnb[22], NR_UE_rrc_inst->kgnb[23],
          NR_UE_rrc_inst->kgnb[24], NR_UE_rrc_inst->kgnb[25], NR_UE_rrc_inst->kgnb[26], NR_UE_rrc_inst->kgnb[27],
          NR_UE_rrc_inst->kgnb[28], NR_UE_rrc_inst->kgnb[29], NR_UE_rrc_inst->kgnb[30], NR_UE_rrc_inst->kgnb[31]);
    derive_key_rrc_enc(NR_UE_rrc_inst->cipheringAlgorithm,NR_UE_rrc_inst->kgnb, &kRRCenc);
    derive_key_rrc_int(NR_UE_rrc_inst->integrityProtAlgorithm,NR_UE_rrc_inst->kgnb, &kRRCint);
    derive_key_up_enc(NR_UE_rrc_inst->cipheringAlgorithm,NR_UE_rrc_inst->kgnb, &kUPenc);
1680 1681 1682 1683 1684 1685 1686

    if (securityMode != 0xff) {
      pdcp_config_set_security(ctxt_pP, pdcp_p, 0, 0,
                               NR_UE_rrc_inst->cipheringAlgorithm
                               | (NR_UE_rrc_inst->integrityProtAlgorithm << 4),
                               kRRCenc, kRRCint, kUPenc);
    } else {
Xue Song's avatar
Xue Song committed
1687
      LOG_I(NR_RRC, "skipped pdcp_config_set_security() as securityMode == 0x%02x",
1688 1689 1690
            securityMode);
    }
  } else {
Xue Song's avatar
Xue Song committed
1691
    LOG_I(NR_RRC, "Could not get PDCP instance where key=0x%ld\n", key);
1692 1693
  }

Xue Song's avatar
Xue Song committed
1694
  if (securityModeCommand->criticalExtensions.present == NR_SecurityModeCommand__criticalExtensions_PR_securityModeCommand) {
1695
    ul_dcch_msg.message.choice.c1->choice.securityModeComplete = CALLOC(1, sizeof(NR_SecurityModeComplete_t));
Xue Song's avatar
Xue Song committed
1696 1697
    ul_dcch_msg.message.choice.c1->choice.securityModeComplete->rrc_TransactionIdentifier = securityModeCommand->rrc_TransactionIdentifier;
    ul_dcch_msg.message.choice.c1->choice.securityModeComplete->criticalExtensions.present = NR_SecurityModeComplete__criticalExtensions_PR_securityModeComplete;
1698
    ul_dcch_msg.message.choice.c1->choice.securityModeComplete->criticalExtensions.choice.securityModeComplete = CALLOC(1, sizeof(NR_SecurityModeComplete_IEs_t));
1699
    ul_dcch_msg.message.choice.c1->choice.securityModeComplete->criticalExtensions.choice.securityModeComplete->nonCriticalExtension =NULL;
yaojie's avatar
yaojie committed
1700
    LOG_I(NR_RRC,"[UE %d] SFN/SF %d/%d: Receiving from SRB1 (DL-DCCH), encoding securityModeComplete (gNB %d), rrc_TransactionIdentifier: %ld\n",
1701 1702 1703 1704 1705 1706 1707 1708 1709
          ctxt_pP->module_id,ctxt_pP->frame, ctxt_pP->subframe, gNB_index, securityModeCommand->rrc_TransactionIdentifier);
    enc_rval = uper_encode_to_buffer(&asn_DEF_NR_UL_DCCH_Message,
                                     NULL,
                                     (void *)&ul_dcch_msg,
                                     buffer,
                                     100);
    AssertFatal (enc_rval.encoded > 0, "ASN1 message encoding failed (%s, %jd)!\n",
                 enc_rval.failed_type->name, enc_rval.encoded);

Xue Song's avatar
Xue Song committed
1710
   if ( LOG_DEBUGFLAG(DEBUG_ASN1) ) {
1711
      xer_fprint(stdout, &asn_DEF_NR_UL_DCCH_Message, (void *)&ul_dcch_msg);
Xue Song's avatar
Xue Song committed
1712
   }
1713

Xue Song's avatar
Xue Song committed
1714
    LOG_D(NR_RRC, "securityModeComplete Encoded %zd bits (%zd bytes)\n", enc_rval.encoded, (enc_rval.encoded+7)/8);
1715 1716

    for (i = 0; i < (enc_rval.encoded + 7) / 8; i++) {
Xue Song's avatar
Xue Song committed
1717
      LOG_T(NR_RRC, "%02x.", buffer[i]);
1718 1719
    }

Xue Song's avatar
Xue Song committed
1720
    LOG_T(NR_RRC, "\n");
1721 1722 1723
#ifdef ITTI_SIM
		MessageDef *message_p;
		uint8_t *message_buffer;
1724
		message_buffer = itti_malloc (TASK_RRC_NRUE,TASK_RRC_GNB_SIM,
1725 1726 1727
						   (enc_rval.encoded + 7) / 8);
		memcpy (message_buffer, buffer, (enc_rval.encoded + 7) / 8);

1728
		message_p = itti_alloc_new_message (TASK_RRC_NRUE, UE_RRC_DCCH_DATA_IND);
1729
		GNB_RRC_DCCH_DATA_IND (message_p).rbid  = DCCH;
yaojie's avatar
yaojie committed
1730
		GNB_RRC_DCCH_DATA_IND (message_p).sdu   = message_buffer;
1731 1732 1733
		GNB_RRC_DCCH_DATA_IND (message_p).size	= (enc_rval.encoded + 7) / 8;
		itti_send_msg_to_task (TASK_RRC_GNB_SIM, ctxt_pP->instance, message_p);
#else
1734 1735 1736
    rrc_data_req (
      ctxt_pP,
      DCCH,
Xue Song's avatar
Xue Song committed
1737
      nr_rrc_mui++,
1738 1739 1740 1741
      SDU_CONFIRM_NO,
      (enc_rval.encoded + 7) / 8,
      buffer,
      PDCP_TRANSMISSION_MODE_CONTROL);
1742
#endif
Xue Song's avatar
Xue Song committed
1743
  } else LOG_W(NR_RRC,"securityModeCommand->criticalExtensions.present (%d) != NR_SecurityModeCommand__criticalExtensions_PR_securityModeCommand\n",
1744 1745 1746
                 securityModeCommand->criticalExtensions.present);
}

Xue Song's avatar
Xue Song committed
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
//-----------------------------------------------------------------------------
void rrc_ue_generate_RRCSetupRequest( const protocol_ctxt_t *const ctxt_pP, const uint8_t gNB_index ) {
  uint8_t i=0,rv[6];

  if(NR_UE_rrc_inst[ctxt_pP->module_id].Srb0[gNB_index].Tx_buffer.payload_size ==0) {
    // Get RRCConnectionRequest, fill random for now
    // Generate random byte stream for contention resolution
    for (i=0; i<6; i++) {
#ifdef SMBV
      // if SMBV is configured the contention resolution needs to be fix for the connection procedure to succeed
      rv[i]=i;
#else
      rv[i]=taus()&0xff;
#endif
      LOG_T(NR_RRC,"%x.",rv[i]);
    }

    LOG_T(NR_RRC,"\n");
    NR_UE_rrc_inst[ctxt_pP->module_id].Srb0[gNB_index].Tx_buffer.payload_size =
      do_RRCSetupRequest(
        ctxt_pP->module_id,
        (uint8_t *)NR_UE_rrc_inst[ctxt_pP->module_id].Srb0[gNB_index].Tx_buffer.Payload,
        rv);
    LOG_I(NR_RRC,"[UE %d] : Frame %d, Logical Channel UL-CCCH (SRB0), Generating RRCSetupRequest (bytes %d, eNB %d)\n",
          ctxt_pP->module_id, ctxt_pP->frame, NR_UE_rrc_inst[ctxt_pP->module_id].Srb0[gNB_index].Tx_buffer.payload_size, gNB_index);

    for (i=0; i<NR_UE_rrc_inst[ctxt_pP->module_id].Srb0[gNB_index].Tx_buffer.payload_size; i++) {
      LOG_T(NR_RRC,"%x.",NR_UE_rrc_inst[ctxt_pP->module_id].Srb0[gNB_index].Tx_buffer.Payload[i]);
    }

    LOG_T(NR_RRC,"\n");
    /*UE_rrc_inst[ue_mod_idP].Srb0[Idx].Tx_buffer.Payload[i] = taus()&0xff;
    UE_rrc_inst[ue_mod_idP].Srb0[Idx].Tx_buffer.payload_size =i; */

#ifdef ITTI_SIM
    MessageDef *message_p;
Xue Song's avatar
Xue Song committed
1783
    uint8_t *message_buffer;
1784
    message_buffer = itti_malloc (TASK_RRC_NRUE,TASK_RRC_GNB_SIM,
Xue Song's avatar
Xue Song committed
1785 1786 1787
          NR_UE_rrc_inst[ctxt_pP->module_id].Srb0[gNB_index].Tx_buffer.payload_size);
    memcpy (message_buffer, (uint8_t*)NR_UE_rrc_inst[ctxt_pP->module_id].Srb0[gNB_index].Tx_buffer.Payload,
          NR_UE_rrc_inst[ctxt_pP->module_id].Srb0[gNB_index].Tx_buffer.payload_size);
1788
    message_p = itti_alloc_new_message (TASK_RRC_NRUE, UE_RRC_CCCH_DATA_IND);
Xue Song's avatar
Xue Song committed
1789
    GNB_RRC_CCCH_DATA_IND (message_p).sdu = message_buffer;
Xue Song's avatar
Xue Song committed
1790 1791 1792 1793 1794 1795
    GNB_RRC_CCCH_DATA_IND (message_p).size  = NR_UE_rrc_inst[ctxt_pP->module_id].Srb0[gNB_index].Tx_buffer.payload_size;
    itti_send_msg_to_task (TASK_RRC_GNB_SIM, ctxt_pP->instance, message_p);
#endif
  }
}

1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847
//-----------------------------------------------------------------------------
int32_t
nr_rrc_ue_establish_srb1(
    module_id_t       ue_mod_idP,
    frame_t           frameP,
    uint8_t           gNB_index,
    NR_SRB_ToAddMod_t *SRB_config
)
//-----------------------------------------------------------------------------
{
    // add descriptor from RRC PDU
    NR_UE_rrc_inst[ue_mod_idP].Srb1[gNB_index].Active = 1;
    NR_UE_rrc_inst[ue_mod_idP].Srb1[gNB_index].Status = RADIO_CONFIG_OK;//RADIO CFG
    NR_UE_rrc_inst[ue_mod_idP].Srb1[gNB_index].Srb_info.Srb_id = 1;
    LOG_I(NR_RRC, "[UE %d], CONFIG_SRB1 %d corresponding to gNB_index %d\n", ue_mod_idP, DCCH, gNB_index);
    return(0);
}

//-----------------------------------------------------------------------------
int32_t
nr_rrc_ue_establish_srb2(
    module_id_t       ue_mod_idP,
    frame_t           frameP,
    uint8_t           gNB_index,
    NR_SRB_ToAddMod_t *SRB_config
)
//-----------------------------------------------------------------------------
{
  // add descriptor from RRC PDU
  NR_UE_rrc_inst[ue_mod_idP].Srb2[gNB_index].Active = 1;
  NR_UE_rrc_inst[ue_mod_idP].Srb2[gNB_index].Status = RADIO_CONFIG_OK;//RADIO CFG
  NR_UE_rrc_inst[ue_mod_idP].Srb2[gNB_index].Srb_info.Srb_id = 2;
  LOG_I(NR_RRC, "[UE %d], CONFIG_SRB2 %d corresponding to gNB_index %d\n", ue_mod_idP, DCCH1, gNB_index);
  return(0);
}

//-----------------------------------------------------------------------------
int32_t
nr_rrc_ue_establish_drb(
    module_id_t       ue_mod_idP,
    frame_t           frameP,
    uint8_t           gNB_index,
    NR_DRB_ToAddMod_t *DRB_config
)
//-----------------------------------------------------------------------------
{
  // add descriptor from RRC PDU
  int oip_ifup = 0, ip_addr_offset3 = 0, ip_addr_offset4 = 0;
  /* avoid gcc warnings */
  (void)oip_ifup;
  (void)ip_addr_offset3;
  (void)ip_addr_offset4;
Xue Song's avatar
Xue Song committed
1848 1849
  LOG_I(NR_RRC,"[UE %d] Frame %d: processing RRCReconfiguration: reconfiguring DRB %ld\n",
        ue_mod_idP, frameP, DRB_config->drb_Identity);
1850

1851
   if(!AMF_MODE_ENABLED) {
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
    ip_addr_offset3 = 0;
    ip_addr_offset4 = 1;
    LOG_I(OIP, "[UE %d] trying to bring up the OAI interface %d, IP X.Y.%d.%d\n", ue_mod_idP, ip_addr_offset3+ue_mod_idP,
          ip_addr_offset3+ue_mod_idP+1, ip_addr_offset4+ue_mod_idP+1);
    oip_ifup = nas_config(ip_addr_offset3+ue_mod_idP+1,   // interface_id
                        UE_NAS_USE_TUN?1:(ip_addr_offset3+ue_mod_idP+1), // third_octet
                        ip_addr_offset4+ue_mod_idP+1, // fourth_octet
                        "oip");                        // interface suffix (when using kernel module)

    if (oip_ifup == 0 && (!UE_NAS_USE_TUN)) { // interface is up --> send a config the DRB
      LOG_I(OIP, "[UE %d] Config the ue net interface %d to send/receive pkt on DRB %ld to/from the protocol stack\n",
            ue_mod_idP,
            ip_addr_offset3+ue_mod_idP,
            (long int)((gNB_index * NR_maxDRB) + DRB_config->drb_Identity));
      rb_conf_ipv4(0,//add
                   ue_mod_idP,//cx align with the UE index
                   ip_addr_offset3+ue_mod_idP,//inst num_enb+ue_index
                   (gNB_index * NR_maxDRB) + DRB_config->drb_Identity,//rb
                   0,//dscp
                   ipv4_address(ip_addr_offset3+ue_mod_idP+1, ip_addr_offset4+ue_mod_idP+1),//saddr
                   ipv4_address(ip_addr_offset3+ue_mod_idP+1, gNB_index+1));//daddr
      LOG_D(NR_RRC,"[UE %d] State = Attached (gNB %d)\n",ue_mod_idP,gNB_index);
    }
1875
   }
1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 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

  return(0);
}

//-----------------------------------------------------------------------------
void
nr_rrc_ue_process_measConfig(
    const protocol_ctxt_t *const       ctxt_pP,
    const uint8_t                      gNB_index,
    NR_MeasConfig_t *const             measConfig
)
//-----------------------------------------------------------------------------
{
    int i;
    long ind;
    NR_MeasObjectToAddMod_t   *measObj        = NULL;
    NR_ReportConfigToAddMod_t *reportConfig   = NULL;

    if (measConfig->measObjectToRemoveList != NULL) {
        for (i = 0; i < measConfig->measObjectToRemoveList->list.count; i++) {
            ind = *measConfig->measObjectToRemoveList->list.array[i];
            free(NR_UE_rrc_inst[ctxt_pP->module_id].MeasObj[gNB_index][ind-1]);
        }
    }

    if (measConfig->measObjectToAddModList != NULL) {
        LOG_I(NR_RRC, "Measurement Object List is present\n");
        for (i = 0; i < measConfig->measObjectToAddModList->list.count; i++) {
            measObj = measConfig->measObjectToAddModList->list.array[i];
            ind     = measConfig->measObjectToAddModList->list.array[i]->measObjectId;

            if (NR_UE_rrc_inst[ctxt_pP->module_id].MeasObj[gNB_index][ind-1]) {
                LOG_D(NR_RRC, "Modifying measurement object %ld\n",ind);
                memcpy((char *)NR_UE_rrc_inst[ctxt_pP->module_id].MeasObj[gNB_index][ind-1],
                        (char *)measObj,
                        sizeof(NR_MeasObjectToAddMod_t));
            } else {
                LOG_I(NR_RRC, "Adding measurement object %ld\n", ind);

                if (measObj->measObject.present == NR_MeasObjectToAddMod__measObject_PR_measObjectNR) {
                    NR_UE_rrc_inst[ctxt_pP->module_id].MeasObj[gNB_index][ind-1]=measObj;
                }
            }
        }

        LOG_I(NR_RRC, "call rrc_mac_config_req \n");
        // rrc_mac_config_req_ue
    }

    if (measConfig->reportConfigToRemoveList != NULL) {
        for (i = 0; i < measConfig->reportConfigToRemoveList->list.count; i++) {
            ind = *measConfig->reportConfigToRemoveList->list.array[i];
            free(NR_UE_rrc_inst[ctxt_pP->module_id].ReportConfig[gNB_index][ind-1]);
        }
    }

    if (measConfig->reportConfigToAddModList != NULL) {
        LOG_I(NR_RRC,"Report Configuration List is present\n");
        for (i = 0; i < measConfig->reportConfigToAddModList->list.count; i++) {
            ind          = measConfig->reportConfigToAddModList->list.array[i]->reportConfigId;
            reportConfig = measConfig->reportConfigToAddModList->list.array[i];

            if (NR_UE_rrc_inst[ctxt_pP->module_id].ReportConfig[gNB_index][ind-1]) {
                LOG_I(NR_RRC, "Modifying Report Configuration %ld\n", ind-1);
                memcpy((char *)NR_UE_rrc_inst[ctxt_pP->module_id].ReportConfig[gNB_index][ind-1],
                        (char *)measConfig->reportConfigToAddModList->list.array[i],
                        sizeof(NR_ReportConfigToAddMod_t));
            } else {
Xue Song's avatar
Xue Song committed
1944
                LOG_D(NR_RRC,"Adding Report Configuration %ld %p \n", ind-1, measConfig->reportConfigToAddModList->list.array[i]);
1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
                if (reportConfig->reportConfig.present == NR_ReportConfigToAddMod__reportConfig_PR_reportConfigNR) {
                    NR_UE_rrc_inst[ctxt_pP->module_id].ReportConfig[gNB_index][ind-1] = measConfig->reportConfigToAddModList->list.array[i];
                }
            }
        }
    }

    if (measConfig->measIdToRemoveList != NULL) {
        for (i = 0; i < measConfig->measIdToRemoveList->list.count; i++) {
            ind = *measConfig->measIdToRemoveList->list.array[i];
            free(NR_UE_rrc_inst[ctxt_pP->module_id].MeasId[gNB_index][ind-1]);
        }
    }

    if (measConfig->measIdToAddModList != NULL) {
        for (i = 0; i < measConfig->measIdToAddModList->list.count; i++) {
            ind = measConfig->measIdToAddModList->list.array[i]->measId;

            if (NR_UE_rrc_inst[ctxt_pP->module_id].MeasId[gNB_index][ind-1]) {
                LOG_D(NR_RRC, "Modifying Measurement ID %ld\n",ind-1);
                memcpy((char *)NR_UE_rrc_inst[ctxt_pP->module_id].MeasId[gNB_index][ind-1],
                    (char *)measConfig->measIdToAddModList->list.array[i],
                    sizeof(NR_MeasIdToAddMod_t));
            } else {
                LOG_D(NR_RRC, "Adding Measurement ID %ld %p\n", ind-1, measConfig->measIdToAddModList->list.array[i]);
                NR_UE_rrc_inst[ctxt_pP->module_id].MeasId[gNB_index][ind-1] = measConfig->measIdToAddModList->list.array[i];
            }
        }
    }

    if (measConfig->quantityConfig != NULL) {
        if (NR_UE_rrc_inst[ctxt_pP->module_id].QuantityConfig[gNB_index]) {
Xue Song's avatar
Xue Song committed
1977
            LOG_D(NR_RRC,"Modifying Quantity Configuration \n");
1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
            memcpy((char *)NR_UE_rrc_inst[ctxt_pP->module_id].QuantityConfig[gNB_index],
                    (char *)measConfig->quantityConfig,
                    sizeof(NR_QuantityConfig_t));
        } else {
            LOG_D(NR_RRC, "Adding Quantity configuration\n");
            NR_UE_rrc_inst[ctxt_pP->module_id].QuantityConfig[gNB_index] = measConfig->quantityConfig;
        }
    }

    if (measConfig->measGapConfig != NULL) {
        if (NR_UE_rrc_inst[ctxt_pP->module_id].measGapConfig[gNB_index]) {
            memcpy((char *)NR_UE_rrc_inst[ctxt_pP->module_id].measGapConfig[gNB_index],
                    (char *)measConfig->measGapConfig,
                    sizeof(NR_MeasGapConfig_t));
        } else {
            NR_UE_rrc_inst[ctxt_pP->module_id].measGapConfig[gNB_index] = measConfig->measGapConfig;
        }
    }

    if (measConfig->s_MeasureConfig->present == NR_MeasConfig__s_MeasureConfig_PR_ssb_RSRP) {
        NR_UE_rrc_inst[ctxt_pP->module_id].s_measure = measConfig->s_MeasureConfig->choice.ssb_RSRP;
    } else if (measConfig->s_MeasureConfig->present == NR_MeasConfig__s_MeasureConfig_PR_csi_RSRP) {
        NR_UE_rrc_inst[ctxt_pP->module_id].s_measure = measConfig->s_MeasureConfig->choice.csi_RSRP;
    }
}

//-----------------------------------------------------------------------------
void
2006
nr_sa_rrc_ue_process_radioBearerConfig(
2007 2008 2009 2010 2011 2012 2013 2014 2015
    const protocol_ctxt_t *const       ctxt_pP,
    const uint8_t                      gNB_index,
    NR_RadioBearerConfig_t *const      radioBearerConfig
)
//-----------------------------------------------------------------------------
{
    long SRB_id, DRB_id;
    int i, cnt;

yaojie's avatar
yaojie committed
2016 2017 2018 2019
    if( radioBearerConfig->srb3_ToRelease != NULL){
      if( *radioBearerConfig->srb3_ToRelease == TRUE){
        //TODO (release the PDCP entity and the srb-Identity of the SRB3.)
      }
2020 2021 2022
    }

    if (radioBearerConfig->srb_ToAddModList != NULL) {
yaojie's avatar
yaojie committed
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032

        if (radioBearerConfig->securityConfig != NULL) {
            if (*radioBearerConfig->securityConfig->keyToUse == NR_SecurityConfig__keyToUse_master) {
                NR_UE_rrc_inst[ctxt_pP->module_id].cipheringAlgorithm =
                    radioBearerConfig->securityConfig->securityAlgorithmConfig->cipheringAlgorithm;
                NR_UE_rrc_inst[ctxt_pP->module_id].integrityProtAlgorithm =
                    *radioBearerConfig->securityConfig->securityAlgorithmConfig->integrityProtAlgorithm;
            }
        }

2033 2034
        uint8_t *kRRCenc = NULL;
        uint8_t *kRRCint = NULL;
Xue Song's avatar
Xue Song committed
2035
        derive_key_rrc_enc(NR_UE_rrc_inst[ctxt_pP->module_id].cipheringAlgorithm,
2036
                        NR_UE_rrc_inst[ctxt_pP->module_id].kgnb, &kRRCenc);
Xue Song's avatar
Xue Song committed
2037
        derive_key_rrc_int(NR_UE_rrc_inst[ctxt_pP->module_id].integrityProtAlgorithm,
2038 2039
                        NR_UE_rrc_inst[ctxt_pP->module_id].kgnb, &kRRCint);
        // Refresh SRBs
Xue Song's avatar
Xue Song committed
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
        // nr_rrc_pdcp_config_asn1_req(ctxt_pP,
        //                             radioBearerConfig->srb_ToAddModList,
        //                             NULL,
        //                             NULL,
        //                             NR_UE_rrc_inst[ctxt_pP->module_id].cipheringAlgorithm |
        //                             (NR_UE_rrc_inst[ctxt_pP->module_id].integrityProtAlgorithm << 4),
        //                             kRRCenc,
        //                             kRRCint,
        //                             NULL,
        //                             NULL,
        //                             NULL,
        //                             NULL);
2052
        // Refresh SRBs
Xue Song's avatar
Xue Song committed
2053 2054 2055 2056 2057 2058 2059
        // nr_rrc_rlc_config_asn1_req(ctxt_pP,
        //                             radioBearerConfig->srb_ToAddModList,
        //                             NULL,
        //                             NULL,
        //                             NULL,
        //                             NULL
        //                             );
2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106

        for (cnt = 0; cnt < radioBearerConfig->srb_ToAddModList->list.count; cnt++) {
            SRB_id = radioBearerConfig->srb_ToAddModList->list.array[cnt]->srb_Identity;
            LOG_D(NR_RRC,"[UE %d]: Frame %d SRB config cnt %d (SRB%ld)\n", ctxt_pP->module_id, ctxt_pP->frame, cnt, SRB_id);
            if (SRB_id == 1) {
                if (NR_UE_rrc_inst[ctxt_pP->module_id].SRB1_config[gNB_index]) {
                    memcpy(NR_UE_rrc_inst[ctxt_pP->module_id].SRB1_config[gNB_index],
                        radioBearerConfig->srb_ToAddModList->list.array[cnt], sizeof(NR_SRB_ToAddMod_t));
                } else {
                    NR_UE_rrc_inst[ctxt_pP->module_id].SRB1_config[gNB_index] = radioBearerConfig->srb_ToAddModList->list.array[cnt];
                    nr_rrc_ue_establish_srb1(ctxt_pP->module_id,
                                            ctxt_pP->frame,
                                            gNB_index,
                                            radioBearerConfig->srb_ToAddModList->list.array[cnt]);

                    LOG_I(NR_RRC, "[FRAME %05d][RRC_UE][MOD %02d][][--- MAC_CONFIG_REQ  (SRB1 gNB %d) --->][MAC_UE][MOD %02d][]\n",
                        ctxt_pP->frame, ctxt_pP->module_id, gNB_index, ctxt_pP->module_id);
                    // rrc_mac_config_req_ue
                }
            } else {
                if (NR_UE_rrc_inst[ctxt_pP->module_id].SRB2_config[gNB_index]) {
                    memcpy(NR_UE_rrc_inst[ctxt_pP->module_id].SRB2_config[gNB_index],
                        radioBearerConfig->srb_ToAddModList->list.array[cnt], sizeof(NR_SRB_ToAddMod_t));
                } else {
                    NR_UE_rrc_inst[ctxt_pP->module_id].SRB2_config[gNB_index] = radioBearerConfig->srb_ToAddModList->list.array[cnt];
                    nr_rrc_ue_establish_srb2(ctxt_pP->module_id,
                                            ctxt_pP->frame,
                                            gNB_index,
                                            radioBearerConfig->srb_ToAddModList->list.array[cnt]);

                    LOG_I(NR_RRC, "[FRAME %05d][RRC_UE][MOD %02d][][--- MAC_CONFIG_REQ  (SRB2 gNB %d) --->][MAC_UE][MOD %02d][]\n",
                        ctxt_pP->frame, ctxt_pP->module_id, gNB_index, ctxt_pP->module_id);
                    // rrc_mac_config_req_ue
                }
            } // srb2
        }
    } // srb_ToAddModList

    // Establish DRBs if present
    if (radioBearerConfig->drb_ToAddModList != NULL) {
        if ((NR_UE_rrc_inst[ctxt_pP->module_id].defaultDRB == NULL) &&
            (radioBearerConfig->drb_ToAddModList->list.count >= 1)) {
            NR_UE_rrc_inst[ctxt_pP->module_id].defaultDRB = malloc(sizeof(rb_id_t));
            *NR_UE_rrc_inst[ctxt_pP->module_id].defaultDRB = radioBearerConfig->drb_ToAddModList->list.array[0]->drb_Identity;
        }

        for (cnt = 0; cnt < radioBearerConfig->drb_ToAddModList->list.count; cnt++) {
Xue Song's avatar
Xue Song committed
2107 2108 2109
            DRB_id = radioBearerConfig->drb_ToAddModList->list.array[cnt]->drb_Identity;
            if (NR_UE_rrc_inst[ctxt_pP->module_id].DRB_config[gNB_index][DRB_id-1]) {
                memcpy(NR_UE_rrc_inst[ctxt_pP->module_id].DRB_config[gNB_index][DRB_id-1],
2110 2111
                        radioBearerConfig->drb_ToAddModList->list.array[cnt], sizeof(NR_DRB_ToAddMod_t));
            } else {
Xue Song's avatar
Xue Song committed
2112 2113
                LOG_D(NR_RRC, "Adding DRB %ld %p\n", DRB_id-1, radioBearerConfig->drb_ToAddModList->list.array[cnt]);
                NR_UE_rrc_inst[ctxt_pP->module_id].DRB_config[gNB_index][DRB_id-1] = radioBearerConfig->drb_ToAddModList->list.array[cnt];
2114 2115 2116 2117
            }
        }

        uint8_t *kUPenc = NULL;
Xue Song's avatar
Xue Song committed
2118
        derive_key_up_enc(NR_UE_rrc_inst[ctxt_pP->module_id].cipheringAlgorithm,
2119 2120 2121 2122 2123 2124 2125 2126 2127
                        NR_UE_rrc_inst[ctxt_pP->module_id].kgnb, &kUPenc);
        MSC_LOG_TX_MESSAGE(
            MSC_RRC_UE,
            MSC_PDCP_UE,
            NULL,
            0,
            MSC_AS_TIME_FMT" CONFIG_REQ UE %x DRB (security %X)",
            MSC_AS_TIME_ARGS(ctxt_pP),
            ctxt_pP->rnti,
Xue Song's avatar
Xue Song committed
2128 2129
            NR_UE_rrc_inst[ctxt_pP->module_id].cipheringAlgorithm |
            (NR_UE_rrc_inst[ctxt_pP->module_id].integrityProtAlgorithm << 4));
2130 2131

        // Refresh DRBs
Xue Song's avatar
Xue Song committed
2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143
        // nr_rrc_pdcp_config_asn1_req(ctxt_pP,
        //                             NULL,
        //                             radioBearerConfig->drb_ToAddModList,
        //                             NULL,
        //                             NR_UE_rrc_inst[ctxt_pP->module_id].cipheringAlgorithm |
        //                             (NR_UE_rrc_inst[ctxt_pP->module_id].integrityProtAlgorithm << 4),
        //                             NULL,
        //                             NULL,
        //                             kUPenc,
        //                             NULL,
        //                             NR_UE_rrc_inst[ctxt_pP->module_id].defaultDRB,
        //                             NULL);
2144
        // Refresh DRBs
Xue Song's avatar
Xue Song committed
2145 2146 2147 2148 2149 2150 2151
        // nr_rrc_rlc_config_asn1_req(ctxt_pP,
        //                             NULL,
        //                             radioBearerConfig->drb_ToAddModList,
        //                             NULL,
        //                             NULL,
        //                             NULL
        //                             );
2152 2153 2154 2155
    } // drb_ToAddModList

    if (radioBearerConfig->drb_ToReleaseList != NULL) {
        for (i = 0; i < radioBearerConfig->drb_ToReleaseList->list.count; i++) {
Xue Song's avatar
Xue Song committed
2156 2157
            DRB_id = *radioBearerConfig->drb_ToReleaseList->list.array[i];
            free(NR_UE_rrc_inst[ctxt_pP->module_id].DRB_config[gNB_index][DRB_id-1]);
2158 2159 2160 2161
        }
    }

    NR_UE_rrc_inst[ctxt_pP->module_id].Info[gNB_index].State = NR_RRC_CONNECTED;
yaojie's avatar
yaojie committed
2162
    LOG_I(NR_RRC,"[UE %d] State = NR_RRC_CONNECTED (gNB %d)\n", ctxt_pP->module_id, gNB_index);
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178
}

//-----------------------------------------------------------------------------
void
rrc_ue_process_rrcReconfiguration(
  const protocol_ctxt_t *const  ctxt_pP,
  NR_RRCReconfiguration_t       *rrcReconfiguration,
  uint8_t                       gNB_index
)
//-----------------------------------------------------------------------------
{
    LOG_I(NR_RRC, "[UE %d] Frame %d: Receiving from SRB1 (DL-DCCH), Processing RRCReconfiguration (gNB %d)\n",
        ctxt_pP->module_id, ctxt_pP->frame, gNB_index);

    NR_RRCReconfiguration_IEs_t *ie = NULL;

Xue Song's avatar
Xue Song committed
2179
    if (rrcReconfiguration->criticalExtensions.present 
2180
                        == NR_RRCReconfiguration__criticalExtensions_PR_rrcReconfiguration) {
Xue Song's avatar
Xue Song committed
2181
        ie = rrcReconfiguration->criticalExtensions.choice.rrcReconfiguration;
2182 2183 2184 2185 2186 2187 2188
        if (ie->measConfig != NULL) {
            LOG_I(NR_RRC, "Measurement Configuration is present\n");
            nr_rrc_ue_process_measConfig(ctxt_pP, gNB_index, ie->measConfig);
        }

        if (ie->radioBearerConfig != NULL) {
            LOG_I(NR_RRC, "radio Bearer Configuration is present\n");
2189
            nr_sa_rrc_ue_process_radioBearerConfig(ctxt_pP, gNB_index, ie->radioBearerConfig);
2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201
        }

        /* Check if there is dedicated NAS information to forward to NAS */
        if (ie->nonCriticalExtension->dedicatedNAS_MessageList != NULL) {
            int list_count;
            uint32_t pdu_length;
            uint8_t *pdu_buffer;
            MessageDef *msg_p;

            for (list_count = 0; list_count < ie->nonCriticalExtension->dedicatedNAS_MessageList->list.count; list_count++) {
                pdu_length = ie->nonCriticalExtension->dedicatedNAS_MessageList->list.array[list_count]->size;
                pdu_buffer = ie->nonCriticalExtension->dedicatedNAS_MessageList->list.array[list_count]->buf;
heshanyun's avatar
heshanyun committed
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
#ifdef ITTI_SIM
                uint8_t msg_type = 0;
                if((pdu_buffer + 1) != NULL){
                  if (*(pdu_buffer + 1) > 0 ) {
                    if(pdu_buffer + 9){
                       msg_type = *(pdu_buffer + 9);
                    } else {
                      LOG_W(NR_RRC, "[UE] Received invalid downlink message\n");
                      return;
                    }
                  } else {
                    if(pdu_buffer + 2){
                      msg_type = *(pdu_buffer + 2);
                    } else {
                        LOG_W(NR_RRC, "[UE] Received invalid downlink message\n");
                        return;
                    }
                  }
                }
                if(msg_type == REGISTRATION_ACCEPT){
                  LOG_I(NR_RRC, "[UE] Received REGISTRATION ACCEPT message\n");
                }
#endif
2225
                msg_p = itti_alloc_new_message(TASK_RRC_NRUE, NAS_CONN_ESTABLI_CNF);
2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
                NAS_CONN_ESTABLI_CNF(msg_p).errCode = AS_SUCCESS;
                NAS_CONN_ESTABLI_CNF(msg_p).nasMsg.length = pdu_length;
                NAS_CONN_ESTABLI_CNF(msg_p).nasMsg.data = pdu_buffer;
                itti_send_msg_to_task(TASK_NAS_UE, ctxt_pP->instance, msg_p);
            }

            free (ie->nonCriticalExtension->dedicatedNAS_MessageList);
        }
    }
}

Xue Song's avatar
Xue Song committed
2237 2238 2239 2240
//-----------------------------------------------------------------------------
void nr_rrc_ue_generate_RRCReconfigurationComplete( const protocol_ctxt_t *const ctxt_pP, const uint8_t gNB_index, const uint8_t Transaction_id ) {
  uint8_t buffer[32], size;
  size = do_NR_RRCReconfigurationComplete(ctxt_pP, buffer, Transaction_id);
Xue Song's avatar
Xue Song committed
2241
  LOG_I(NR_RRC,PROTOCOL_RRC_CTXT_UE_FMT" Logical Channel UL-DCCH (SRB1), Generating RRCReconfigurationComplete (bytes %d, gNB_index %d)\n",
Xue Song's avatar
Xue Song committed
2242 2243 2244 2245 2246 2247 2248 2249 2250 2251
        PROTOCOL_RRC_CTXT_UE_ARGS(ctxt_pP), size, gNB_index);
  LOG_D(RLC,
        "[FRAME %05d][RRC_UE][INST %02d][][--- PDCP_DATA_REQ/%d Bytes (RRCReconfigurationComplete to gNB %d MUI %d) --->][PDCP][INST %02d][RB %02d]\n",
        ctxt_pP->frame,
        UE_MODULE_ID_TO_INSTANCE(ctxt_pP->module_id),
        size,
        gNB_index,
        nr_rrc_mui,
        UE_MODULE_ID_TO_INSTANCE(ctxt_pP->module_id),
        DCCH);
2252 2253
#ifdef ITTI_SIM
  MessageDef *message_p;
yaojie's avatar
yaojie committed
2254
  uint8_t *message_buffer;
2255
  message_buffer = itti_malloc (TASK_RRC_NRUE,TASK_RRC_GNB_SIM,size);
yaojie's avatar
yaojie committed
2256 2257
  memcpy (message_buffer, buffer, size);

2258
  message_p = itti_alloc_new_message (TASK_RRC_NRUE, UE_RRC_DCCH_DATA_IND);
2259
  UE_RRC_DCCH_DATA_IND (message_p).rbid = DCCH;
yaojie's avatar
yaojie committed
2260
  UE_RRC_DCCH_DATA_IND (message_p).sdu = message_buffer;
2261 2262 2263
  UE_RRC_DCCH_DATA_IND (message_p).size  = size;
  itti_send_msg_to_task (TASK_RRC_GNB_SIM, ctxt_pP->instance, message_p);
#else
Xue Song's avatar
Xue Song committed
2264 2265 2266 2267 2268 2269 2270 2271
  rrc_data_req_ue (
    ctxt_pP,
    DCCH,
    nr_rrc_mui++,
    SDU_CONFIRM_NO,
    size,
    buffer,
    PDCP_TRANSMISSION_MODE_CONTROL);
2272 2273
#endif

Xue Song's avatar
Xue Song committed
2274 2275
}

2276 2277 2278
// from NR SRB1
//-----------------------------------------------------------------------------
int
Xue Song's avatar
Xue Song committed
2279
nr_rrc_ue_decode_dcch(
2280 2281 2282 2283 2284 2285 2286 2287 2288
  const protocol_ctxt_t *const ctxt_pP,
  const srb_id_t               Srb_id,
  const uint8_t         *const Buffer,
  const uint8_t                gNB_indexP
)
//-----------------------------------------------------------------------------
{
    asn_dec_rval_t                      dec_rval;
    NR_DL_DCCH_Message_t                *dl_dcch_msg  = NULL;
Qiu DeZhi's avatar
Qiu DeZhi committed
2289
    MessageDef *msg_p;
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312

    if (Srb_id != 1) {
        LOG_E(NR_RRC,"[UE %d] Frame %d: Received message on DL-DCCH (SRB%ld), should not have ...\n",
            ctxt_pP->module_id, ctxt_pP->frame, Srb_id);
        return -1;
    } else {
        LOG_D(NR_RRC, "Received message on SRB%ld\n", Srb_id);
    }

    LOG_D(NR_RRC, "Decoding DL-DCCH Message\n");
    dec_rval = uper_decode( NULL,
                            &asn_DEF_NR_DL_DCCH_Message,
                            (void **)&dl_dcch_msg,
                            Buffer,
                            RRC_BUF_SIZE,
                            0,
                            0);

    if ((dec_rval.code != RC_OK) && (dec_rval.consumed == 0)) {
        LOG_E(NR_RRC, "Failed to decode DL-DCCH (%zu bytes)\n", dec_rval.consumed);
        return -1;
    }

Xue Song's avatar
Xue Song committed
2313
    // if ( LOG_DEBUGFLAG(DEBUG_ASN1) ) {
2314
        xer_fprint(stdout, &asn_DEF_NR_DL_DCCH_Message,(void *)dl_dcch_msg);
Xue Song's avatar
Xue Song committed
2315
    // }
2316

Xue Song's avatar
Xue Song committed
2317 2318
    if (dl_dcch_msg->message.present == NR_DL_DCCH_MessageType_PR_c1) {
        switch (dl_dcch_msg->message.choice.c1->present) {
2319 2320 2321 2322 2323
            case NR_DL_DCCH_MessageType__c1_PR_NOTHING:
                LOG_I(NR_RRC, "Received PR_NOTHING on DL-DCCH-Message\n");
                break;

            case NR_DL_DCCH_MessageType__c1_PR_rrcReconfiguration:
2324
            {
2325 2326 2327
                rrc_ue_process_rrcReconfiguration(ctxt_pP,
                                                    dl_dcch_msg->message.choice.c1->choice.rrcReconfiguration,
                                                    gNB_indexP);
Xue Song's avatar
Xue Song committed
2328 2329 2330
                nr_rrc_ue_generate_RRCReconfigurationComplete(ctxt_pP,
                                            gNB_indexP,
                                            dl_dcch_msg->message.choice.c1->choice.rrcReconfiguration->rrc_TransactionIdentifier);
2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343
#ifdef ITTI_SIM
                as_nas_info_t initialNasMsg;
                memset(&initialNasMsg, 0, sizeof(as_nas_info_t));
                generateRegistrationComplete(&initialNasMsg, NULL);
                if(initialNasMsg.length > 0){
                    MessageDef *message_p;
                    message_p = itti_alloc_new_message(TASK_RRC_NRUE, NAS_UPLINK_DATA_REQ);
                    NAS_UPLINK_DATA_REQ(message_p).UEid          = ctxt_pP->module_id;
                    NAS_UPLINK_DATA_REQ(message_p).nasMsg.data   = (uint8_t *)initialNasMsg.data;
                    NAS_UPLINK_DATA_REQ(message_p).nasMsg.length = initialNasMsg.length;
                    itti_send_msg_to_task(TASK_RRC_NRUE, ctxt_pP->instance, message_p);
                    LOG_I(NR_RRC, " Send NAS_UPLINK_DATA_REQ message(RegistrationComplete)\n");
                }
2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355
                as_nas_info_t pduEstablishMsg;
                memset(&pduEstablishMsg, 0, sizeof(as_nas_info_t));
                generatePduSessionEstablishRequest(&pduEstablishMsg);
                if(initialNasMsg.length > 0){
                    MessageDef *message_p;
                    message_p = itti_alloc_new_message(TASK_RRC_NRUE, NAS_UPLINK_DATA_REQ);
                    NAS_UPLINK_DATA_REQ(message_p).UEid          = ctxt_pP->module_id;
                    NAS_UPLINK_DATA_REQ(message_p).nasMsg.data   = (uint8_t *)pduEstablishMsg.data;
                    NAS_UPLINK_DATA_REQ(message_p).nasMsg.length = pduEstablishMsg.length;
                    itti_send_msg_to_task(TASK_RRC_NRUE, ctxt_pP->instance, message_p);
                    LOG_I(NR_RRC, " Send NAS_UPLINK_DATA_REQ message(PduSessionEstablishRequest)\n");
                }
2356 2357
#endif
            }
Xue Song's avatar
Xue Song committed
2358
                break;
2359

Xue Song's avatar
Xue Song committed
2360
            case NR_DL_DCCH_MessageType__c1_PR_rrcResume:
2361
            case NR_DL_DCCH_MessageType__c1_PR_rrcRelease:
2362 2363 2364
              LOG_I(NR_RRC, "[UE %d] Received RRC Release (gNB %d)\n",
                      ctxt_pP->module_id, gNB_indexP);

2365
              msg_p = itti_alloc_new_message(TASK_RRC_NRUE, NAS_CONN_RELEASE_IND);
Qiu DeZhi's avatar
Qiu DeZhi committed
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376

              if((dl_dcch_msg->message.choice.c1->choice.rrcRelease->criticalExtensions.present == NR_RRCRelease__criticalExtensions_PR_rrcRelease) &&
                   (dl_dcch_msg->message.choice.c1->present == NR_DL_DCCH_MessageType__c1_PR_rrcRelease)){
                    dl_dcch_msg->message.choice.c1->choice.rrcRelease->criticalExtensions.choice.rrcRelease->deprioritisationReq->deprioritisationTimer =
                    NR_RRCRelease_IEs__deprioritisationReq__deprioritisationTimer_min5;
                    dl_dcch_msg->message.choice.c1->choice.rrcRelease->criticalExtensions.choice.rrcRelease->deprioritisationReq->deprioritisationType =
                    NR_RRCRelease_IEs__deprioritisationReq__deprioritisationType_frequency;
                }

                 itti_send_msg_to_task(TASK_NAS_UE, ctxt_pP->instance, msg_p);
                 break;
Xue Song's avatar
Xue Song committed
2377
            case NR_DL_DCCH_MessageType__c1_PR_ueCapabilityEnquiry:
Xue Song's avatar
Xue Song committed
2378
            	LOG_I(NR_RRC, "[UE %d] Received Capability Enquiry (gNB %d)\n",
zhanghuiqin's avatar
zhanghuiqin committed
2379 2380 2381
            	      ctxt_pP->module_id,gNB_indexP);
            	nr_rrc_ue_process_ueCapabilityEnquiry(
            	  ctxt_pP,
2382
            	  dl_dcch_msg->message.choice.c1->choice.ueCapabilityEnquiry,
zhanghuiqin's avatar
zhanghuiqin committed
2383 2384
            	  gNB_indexP);
            	 break;
2385
            case NR_DL_DCCH_MessageType__c1_PR_rrcReestablishment:
Xue Song's avatar
Xue Song committed
2386
                break;
2387
            case NR_DL_DCCH_MessageType__c1_PR_dlInformationTransfer:
Xue Song's avatar
Xue Song committed
2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402
            {
                NR_DLInformationTransfer_t *dlInformationTransfer = dl_dcch_msg->message.choice.c1->choice.dlInformationTransfer;

                if (dlInformationTransfer->criticalExtensions.present
                      == NR_DLInformationTransfer__criticalExtensions_PR_dlInformationTransfer) {
                  /* This message hold a dedicated info NAS payload, forward it to NAS */
                  NR_DedicatedNAS_Message_t *dedicatedNAS_Message =
                      dlInformationTransfer->criticalExtensions.choice.dlInformationTransfer->dedicatedNAS_Message;
                  uint32_t pdu_length;
                  uint8_t *pdu_buffer;
                  pdu_length = dedicatedNAS_Message->size;
                  pdu_buffer = dedicatedNAS_Message->buf;
#ifdef ITTI_SIM
                  LOG_I(NR_RRC, "[UE %d] Received %s: UEid %u, length %u , buffer %p\n", ctxt_pP->module_id,  messages_info[NAS_DOWNLINK_DATA_IND].name,
                        ctxt_pP->module_id, pdu_length, pdu_buffer);
heshanyun's avatar
heshanyun committed
2403
                  as_nas_info_t initialNasMsg;
heshanyun's avatar
heshanyun committed
2404
                  uint8_t msg_type = 0;
heshanyun's avatar
heshanyun committed
2405
                  memset(&initialNasMsg, 0, sizeof(as_nas_info_t));
Xue Song's avatar
Xue Song committed
2406 2407 2408 2409 2410 2411 2412
                  if((pdu_buffer + 1) != NULL){
                    if (*(pdu_buffer + 1) > 0 ) {
                      msg_type = *(pdu_buffer + 9);
                    } else {
                      msg_type = *(pdu_buffer + 2);
                    }
                  }
heshanyun's avatar
heshanyun committed
2413 2414 2415 2416 2417 2418 2419 2420 2421
                  if((pdu_buffer + 2) == NULL){
                    LOG_W(NR_RRC, "[UE] Received invalid downlink message\n");
                    return 0;
                  }

                  switch(msg_type){
                    case FGS_IDENTITY_REQUEST:
                       generateIdentityResponse(&initialNasMsg,*(pdu_buffer+3));
                       break;
2422 2423 2424
                    case FGS_AUTHENTICATION_REQUEST:
                       generateAuthenticationResp(&initialNasMsg, pdu_buffer);
                       break;
Xue Song's avatar
Xue Song committed
2425 2426 2427
                    case FGS_SECURITY_MODE_COMMAND:
                      generateSecurityModeComplete(&initialNasMsg);
                      break;
heshanyun's avatar
heshanyun committed
2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
                    default:
                       LOG_W(NR_RRC,"unknow message type %d\n",msg_type);
                       break;
                  }
                  if(initialNasMsg.length > 0){
                    MessageDef *message_p;
                    message_p = itti_alloc_new_message(TASK_RRC_NRUE, NAS_UPLINK_DATA_REQ);
                    NAS_UPLINK_DATA_REQ(message_p).UEid          = ctxt_pP->module_id;
                    NAS_UPLINK_DATA_REQ(message_p).nasMsg.data   = (uint8_t *)initialNasMsg.data;
                    NAS_UPLINK_DATA_REQ(message_p).nasMsg.length = initialNasMsg.length;
                    itti_send_msg_to_task(TASK_RRC_NRUE, ctxt_pP->instance, message_p);
                    LOG_I(NR_RRC, " Send NAS_UPLINK_DATA_REQ message\n");
                  }
Xue Song's avatar
Xue Song committed
2441 2442
#else
                  MessageDef *msg_p;
2443
                  msg_p = itti_alloc_new_message(TASK_RRC_NRUE, NAS_DOWNLINK_DATA_IND);
Xue Song's avatar
Xue Song committed
2444 2445 2446 2447 2448 2449 2450 2451 2452
                  NAS_DOWNLINK_DATA_IND(msg_p).UEid = ctxt_pP->module_id; // TODO set the UEid to something else ?
                  NAS_DOWNLINK_DATA_IND(msg_p).nasMsg.length = pdu_length;
                  NAS_DOWNLINK_DATA_IND(msg_p).nasMsg.data = pdu_buffer;
                  itti_send_msg_to_task(TASK_NAS_UE, ctxt_pP->instance, msg_p);
#endif
              }
            }

              break;
Xue Song's avatar
Xue Song committed
2453 2454 2455 2456 2457 2458 2459 2460
            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:
yaojie's avatar
yaojie committed
2461
            case NR_DL_DCCH_MessageType__c1_PR_counterCheck:
2462 2463
                break;
            case NR_DL_DCCH_MessageType__c1_PR_securityModeCommand:
Xue Song's avatar
Xue Song committed
2464 2465 2466 2467 2468 2469
                LOG_I(NR_RRC, "[UE %d] Received securityModeCommand (gNB %d)\n",
                      ctxt_pP->module_id, gNB_indexP);
                nr_rrc_ue_process_securityModeCommand(
                    ctxt_pP,
                    dl_dcch_msg->message.choice.c1->choice.securityModeCommand,
                    gNB_indexP);
2470

2471 2472 2473 2474 2475
                break;
        }
    }
    return 0;
}
2476

heshanyun's avatar
heshanyun committed
2477 2478 2479 2480 2481 2482
//-----------------------------------------------------------------------------
void *rrc_nrue_task( void *args_p ) {
  MessageDef   *msg_p;
  instance_t    instance;
  unsigned int  ue_mod_id;
  int           result;
yaojie's avatar
yaojie committed
2483 2484
  NR_SRB_INFO   *srb_info_p;
  protocol_ctxt_t  ctxt;
heshanyun's avatar
heshanyun committed
2485 2486 2487 2488 2489 2490 2491 2492 2493 2494
  itti_mark_task_ready (TASK_RRC_NRUE);

  while(1) {
    // Wait for a message
    itti_receive_msg (TASK_RRC_NRUE, &msg_p);
    instance = ITTI_MSG_INSTANCE (msg_p);
    ue_mod_id = UE_INSTANCE_TO_MODULE_ID(instance);

    switch (ITTI_MSG_ID(msg_p)) {
      case TERMINATE_MESSAGE:
Xue Song's avatar
Xue Song committed
2495
        LOG_W(NR_RRC, " *** Exiting RRC thread\n");
heshanyun's avatar
heshanyun committed
2496 2497 2498 2499
        itti_exit_task ();
        break;

      case MESSAGE_TEST:
Xue Song's avatar
Xue Song committed
2500
        LOG_D(NR_RRC, "[UE %d] Received %s\n", ue_mod_id, ITTI_MSG_NAME (msg_p));
heshanyun's avatar
heshanyun committed
2501 2502
        break;

2503
      case NR_RRC_MAC_BCCH_DATA_IND:
Xue Song's avatar
Xue Song committed
2504
        LOG_D(NR_RRC, "[UE %d] Received %s: frameP %d, gNB %d\n", ue_mod_id, ITTI_MSG_NAME (msg_p),
2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515
              NR_RRC_MAC_BCCH_DATA_IND (msg_p).frame, NR_RRC_MAC_BCCH_DATA_IND (msg_p).gnb_index);
        //      PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt, instance, ENB_FLAG_NO, NOT_A_RNTI, RRC_MAC_BCCH_DATA_IND (msg_p).frame, 0);
        PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, ue_mod_id, GNB_FLAG_NO, NOT_A_RNTI, NR_RRC_MAC_BCCH_DATA_IND (msg_p).frame, 0,NR_RRC_MAC_BCCH_DATA_IND (msg_p).gnb_index);
        nr_decode_BCCH_DLSCH_Message (&ctxt,
                                   NR_RRC_MAC_BCCH_DATA_IND (msg_p).gnb_index,
                                   NR_RRC_MAC_BCCH_DATA_IND (msg_p).sdu,
                                   NR_RRC_MAC_BCCH_DATA_IND (msg_p).sdu_size,
                                   NR_RRC_MAC_BCCH_DATA_IND (msg_p).rsrq,
                                   NR_RRC_MAC_BCCH_DATA_IND (msg_p).rsrp);

      case NR_RRC_MAC_CCCH_DATA_IND:
Xue Song's avatar
Xue Song committed
2516
        LOG_D(NR_RRC, "[UE %d] RNTI %x Received %s: frameP %d, gNB %d\n",
yaojie's avatar
yaojie committed
2517
              ue_mod_id,
2518
              NR_RRC_MAC_CCCH_DATA_IND (msg_p).rnti,
yaojie's avatar
yaojie committed
2519
              ITTI_MSG_NAME (msg_p),
2520 2521 2522 2523 2524 2525
              NR_RRC_MAC_CCCH_DATA_IND (msg_p).frame,
              NR_RRC_MAC_CCCH_DATA_IND (msg_p).gnb_index);
        srb_info_p = &NR_UE_rrc_inst[ue_mod_id].Srb0[NR_RRC_MAC_CCCH_DATA_IND (msg_p).gnb_index];
        memcpy (srb_info_p->Rx_buffer.Payload, NR_RRC_MAC_CCCH_DATA_IND (msg_p).sdu,
                NR_RRC_MAC_CCCH_DATA_IND (msg_p).sdu_size);
        srb_info_p->Rx_buffer.payload_size = NR_RRC_MAC_CCCH_DATA_IND (msg_p).sdu_size;
yaojie's avatar
yaojie committed
2526
        //      PROTOCOL_CTXT_SET_BY_INSTANCE(&ctxt, instance, ENB_FLAG_NO, RRC_MAC_CCCH_DATA_IND (msg_p).rnti, RRC_MAC_CCCH_DATA_IND (msg_p).frame, 0);
2527
        PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, ue_mod_id, GNB_FLAG_NO, NR_RRC_MAC_CCCH_DATA_IND (msg_p).rnti, NR_RRC_MAC_CCCH_DATA_IND (msg_p).frame, 0, NR_RRC_MAC_CCCH_DATA_IND (msg_p).gnb_index);
yaojie's avatar
yaojie committed
2528 2529
        nr_rrc_ue_decode_ccch (&ctxt,
                            srb_info_p,
2530
                            NR_RRC_MAC_CCCH_DATA_IND (msg_p).gnb_index);
yaojie's avatar
yaojie committed
2531 2532
        break;

2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553
      /* PDCP messages */
      case NR_RRC_DCCH_DATA_IND:
        PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, NR_RRC_DCCH_DATA_IND (msg_p).module_id, GNB_FLAG_NO, NR_RRC_DCCH_DATA_IND (msg_p).rnti, NR_RRC_DCCH_DATA_IND (msg_p).frame, 0,NR_RRC_DCCH_DATA_IND (msg_p).gNB_index);
        LOG_D(NR_RRC, "[UE %d] Received %s: frameP %d, DCCH %d, gNB %d\n",
              NR_RRC_DCCH_DATA_IND (msg_p).module_id,
              ITTI_MSG_NAME (msg_p),
              NR_RRC_DCCH_DATA_IND (msg_p).frame,
              NR_RRC_DCCH_DATA_IND (msg_p).dcch_index,
              NR_RRC_DCCH_DATA_IND (msg_p).gNB_index);
        LOG_D(NR_RRC, PROTOCOL_RRC_CTXT_UE_FMT"Received %s DCCH %d, gNB %d\n",
              PROTOCOL_NR_RRC_CTXT_UE_ARGS(&ctxt),
              ITTI_MSG_NAME (msg_p),
              NR_RRC_DCCH_DATA_IND (msg_p).dcch_index,
              NR_RRC_DCCH_DATA_IND (msg_p).gNB_index);
        nr_rrc_ue_decode_dcch (
          &ctxt,
          NR_RRC_DCCH_DATA_IND (msg_p).dcch_index,
          NR_RRC_DCCH_DATA_IND (msg_p).sdu_p,
          NR_RRC_DCCH_DATA_IND (msg_p).gNB_index);
        break;

Xue Song's avatar
Xue Song committed
2554 2555 2556
      case NAS_UPLINK_DATA_REQ: {
        uint32_t length;
        uint8_t *buffer;
2557
        LOG_I(NR_RRC, "[UE %d] Received %s: UEid %d\n", ue_mod_id, ITTI_MSG_NAME (msg_p), NAS_UPLINK_DATA_REQ (msg_p).UEid);
Xue Song's avatar
Xue Song committed
2558 2559 2560 2561 2562 2563 2564
        /* Create message for PDCP (ULInformationTransfer_t) */
        length = do_NR_ULInformationTransfer(&buffer, NAS_UPLINK_DATA_REQ (msg_p).nasMsg.length, NAS_UPLINK_DATA_REQ (msg_p).nasMsg.data);
        /* Transfer data to PDCP */
        PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, ue_mod_id, GNB_FLAG_NO, NR_UE_rrc_inst[ue_mod_id].Info[0].rnti, 0, 0,0);
#ifdef ITTI_SIM
        MessageDef *message_p;
        uint8_t *message_buffer;
2565
        message_buffer = itti_malloc (TASK_RRC_NRUE,TASK_RRC_GNB_SIM,length);
Xue Song's avatar
Xue Song committed
2566 2567
        memcpy (message_buffer, buffer, length);
        
2568
        message_p = itti_alloc_new_message (TASK_RRC_NRUE, UE_RRC_DCCH_DATA_IND);
Xue Song's avatar
Xue Song committed
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
        if(NR_UE_rrc_inst[ue_mod_id].SRB2_config[0] == NULL) 
          UE_RRC_DCCH_DATA_IND (message_p).rbid = DCCH;
        else
          UE_RRC_DCCH_DATA_IND (message_p).rbid = DCCH1;
        UE_RRC_DCCH_DATA_IND (message_p).sdu = message_buffer;
        UE_RRC_DCCH_DATA_IND (message_p).size  = length;
        itti_send_msg_to_task (TASK_RRC_GNB_SIM, ctxt.instance, message_p);

#else
        // check if SRB2 is created, if yes request data_req on DCCH1 (SRB2)
        if(NR_UE_rrc_inst[ue_mod_id].SRB2_config[0] == NULL) {
          rrc_data_req_ue (&ctxt,
                           DCCH,
                           nr_rrc_mui++,
                           SDU_CONFIRM_NO,
                           length, buffer,
                           PDCP_TRANSMISSION_MODE_CONTROL);
        } else {
          rrc_data_req_ue (&ctxt,
                           DCCH1,
                           nr_rrc_mui++,
                           SDU_CONFIRM_NO,
                           length, buffer,
                           PDCP_TRANSMISSION_MODE_CONTROL);
        }
#endif
        break;
      }

heshanyun's avatar
heshanyun committed
2598
      default:
Xue Song's avatar
Xue Song committed
2599
        LOG_E(NR_RRC, "[UE %d] Received unexpected message %s\n", ue_mod_id, ITTI_MSG_NAME (msg_p));
heshanyun's avatar
heshanyun committed
2600
        break;
Qiu Dezhi's avatar
Qiu Dezhi committed
2601
    }
yaojie's avatar
yaojie committed
2602
	LOG_I(NR_RRC, "[UE %d] RRC Status %d\n", ue_mod_id, nr_rrc_get_state(ue_mod_id));
heshanyun's avatar
heshanyun committed
2603 2604 2605 2606 2607
    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;
  }
}
yaojie's avatar
yaojie committed
2608
void nr_rrc_ue_process_sidelink_radioResourceConfig(
yincong's avatar
yincong committed
2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628
  module_id_t                                Mod_idP,
  uint8_t                                    gNB_index,
  NR_SetupRelease_SL_ConfigDedicatedNR_r16_t  *sl_ConfigDedicatedNR
)
{
  //process sl_CommConfig, configure MAC/PHY for transmitting SL communication (RRC_CONNECTED)
  if (sl_ConfigDedicatedNR != NULL) {
      switch (sl_ConfigDedicatedNR->present){
      case NR_SetupRelease_SL_ConfigDedicatedNR_r16_PR_setup:
          //TODO
          break;
      case NR_SetupRelease_SL_ConfigDedicatedNR_r16_PR_release:
          break;
      case NR_SetupRelease_SL_ConfigDedicatedNR_r16_PR_NOTHING:
          break;
      default:
          break;
      }
  }
}
zhanghuiqin's avatar
zhanghuiqin committed
2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643

//-----------------------------------------------------------------------------
void
nr_rrc_ue_process_ueCapabilityEnquiry(
  const protocol_ctxt_t *const ctxt_pP,
  NR_UECapabilityEnquiry_t *UECapabilityEnquiry,
  uint8_t gNB_index
)
//-----------------------------------------------------------------------------
{
  asn_enc_rval_t enc_rval;
  NR_UL_DCCH_Message_t ul_dcch_msg;
  NR_UE_CapabilityRAT_Container_t ue_CapabilityRAT_Container;
  uint8_t buffer[200];
  int i;
Xue Song's avatar
Xue Song committed
2644
  LOG_I(NR_RRC,"[UE %d] Frame %d: Receiving from SRB1 (DL-DCCH), Processing UECapabilityEnquiry (gNB %d)\n",
zhanghuiqin's avatar
zhanghuiqin committed
2645 2646 2647 2648 2649 2650 2651 2652 2653 2654
        ctxt_pP->module_id,
        ctxt_pP->frame,
        gNB_index);
  memset((void *)&ul_dcch_msg,0,sizeof(NR_UL_DCCH_Message_t));
  memset((void *)&ue_CapabilityRAT_Container,0,sizeof(NR_UE_CapabilityRAT_Container_t));
  ul_dcch_msg.message.present            = NR_UL_DCCH_MessageType_PR_c1;
  ul_dcch_msg.message.choice.c1          = CALLOC(1, sizeof(struct NR_UL_DCCH_MessageType__c1));
  ul_dcch_msg.message.choice.c1->present = NR_UL_DCCH_MessageType__c1_PR_ueCapabilityInformation;
  ul_dcch_msg.message.choice.c1->choice.ueCapabilityInformation                            = CALLOC(1, sizeof(struct NR_UECapabilityInformation));
  ul_dcch_msg.message.choice.c1->choice.ueCapabilityInformation->rrc_TransactionIdentifier = UECapabilityEnquiry->rrc_TransactionIdentifier;
2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666
  ue_CapabilityRAT_Container.rat_Type = NR_RAT_Type_nr;
  NR_UE_NR_Capability_t*             UE_Capability_nr;
  UE_Capability_nr = CALLOC(1,sizeof(NR_UE_NR_Capability_t));
  NR_BandNR_t *nr_bandnr;
  nr_bandnr  = CALLOC(1,sizeof(NR_BandNR_t));
  nr_bandnr->bandNR = 1;
  ASN_SEQUENCE_ADD(
    &UE_Capability_nr->rf_Parameters.supportedBandListNR.list,
    nr_bandnr);
  OAI_NR_UECapability_t *UECap;
  UECap = CALLOC(1,sizeof(OAI_NR_UECapability_t));
  UECap->UE_NR_Capability = UE_Capability_nr;
Xue Song's avatar
Xue Song committed
2667
  if ( LOG_DEBUGFLAG(DEBUG_ASN1) ) {
2668
    xer_fprint(stdout,&asn_DEF_NR_UE_NR_Capability,(void *)UE_Capability_nr);
Xue Song's avatar
Xue Song committed
2669
  }
2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684

  enc_rval = uper_encode_to_buffer(&asn_DEF_NR_UE_NR_Capability,
                                   NULL,
                                   (void *)UE_Capability_nr,
                                   &UECap->sdu[0],
                                   MAX_UE_NR_CAPABILITY_SIZE);
  AssertFatal (enc_rval.encoded > 0, "ASN1 message encoding failed (%s, %lu)!\n",
               enc_rval.failed_type->name, enc_rval.encoded);
  UECap->sdu_size = (enc_rval.encoded + 7) / 8;
  LOG_I(PHY, "[RRC]UE NR Capability encoded, %d bytes (%zd bits)\n",
        UECap->sdu_size, enc_rval.encoded + 7);

  NR_UE_rrc_inst[ctxt_pP->module_id].UECap = UECap;
  NR_UE_rrc_inst[ctxt_pP->module_id].UECapability = UECap->sdu;
  NR_UE_rrc_inst[ctxt_pP->module_id].UECapability_size = UECap->sdu_size; 
zhanghuiqin's avatar
zhanghuiqin committed
2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695
  OCTET_STRING_fromBuf(&ue_CapabilityRAT_Container.ue_CapabilityRAT_Container,
                       (const char *)NR_UE_rrc_inst[ctxt_pP->module_id].UECapability,
                       NR_UE_rrc_inst[ctxt_pP->module_id].UECapability_size);
  //  ue_CapabilityRAT_Container.ueCapabilityRAT_Container.buf  = UE_rrc_inst[ue_mod_idP].UECapability;
  // ue_CapabilityRAT_Container.ueCapabilityRAT_Container.size = UE_rrc_inst[ue_mod_idP].UECapability_size;
  AssertFatal(UECapabilityEnquiry->criticalExtensions.present == NR_UECapabilityEnquiry__criticalExtensions_PR_ueCapabilityEnquiry,
              "UECapabilityEnquiry->criticalExtensions.present (%d) != UECapabilityEnquiry__criticalExtensions_PR_c1 (%d)\n",
              UECapabilityEnquiry->criticalExtensions.present,NR_UECapabilityEnquiry__criticalExtensions_PR_ueCapabilityEnquiry);

  ul_dcch_msg.message.choice.c1->choice.ueCapabilityInformation->criticalExtensions.present           = NR_UECapabilityInformation__criticalExtensions_PR_ueCapabilityInformation;
  ul_dcch_msg.message.choice.c1->choice.ueCapabilityInformation->criticalExtensions.choice.ueCapabilityInformation   = CALLOC(1, sizeof(struct NR_UECapabilityInformation_IEs));
2696
  ul_dcch_msg.message.choice.c1->choice.ueCapabilityInformation->criticalExtensions.choice.ueCapabilityInformation->ue_CapabilityRAT_ContainerList             = CALLOC(1, sizeof(struct NR_UE_CapabilityRAT_ContainerList));
zhanghuiqin's avatar
zhanghuiqin committed
2697 2698 2699
  ul_dcch_msg.message.choice.c1->choice.ueCapabilityInformation->criticalExtensions.choice.ueCapabilityInformation->ue_CapabilityRAT_ContainerList->list.count = 0;

  for (i=0; i<UECapabilityEnquiry->criticalExtensions.choice.ueCapabilityEnquiry->ue_CapabilityRAT_RequestList.list.count; i++) {
2700 2701
    if (UECapabilityEnquiry->criticalExtensions.choice.ueCapabilityEnquiry->ue_CapabilityRAT_RequestList.list.array[i]->rat_Type
        == NR_RAT_Type_nr) {
zhanghuiqin's avatar
zhanghuiqin committed
2702 2703 2704 2705 2706 2707 2708
      ASN_SEQUENCE_ADD(
        &ul_dcch_msg.message.choice.c1->choice.ueCapabilityInformation->criticalExtensions.choice.ueCapabilityInformation->ue_CapabilityRAT_ContainerList->list,
        &ue_CapabilityRAT_Container);
      enc_rval = uper_encode_to_buffer(&asn_DEF_NR_UL_DCCH_Message, NULL, (void *) &ul_dcch_msg, buffer, 100);
      AssertFatal (enc_rval.encoded > 0, "ASN1 message encoding failed (%s, %jd)!\n",
                   enc_rval.failed_type->name, enc_rval.encoded);

Xue Song's avatar
Xue Song committed
2709
      if ( LOG_DEBUGFLAG(DEBUG_ASN1) ) {
zhanghuiqin's avatar
zhanghuiqin committed
2710
        xer_fprint(stdout, &asn_DEF_NR_UL_DCCH_Message, (void *)&ul_dcch_msg);
Xue Song's avatar
Xue Song committed
2711
      }
zhanghuiqin's avatar
zhanghuiqin committed
2712 2713

      LOG_I(RRC,"UECapabilityInformation Encoded %zd bits (%zd bytes)\n",enc_rval.encoded,(enc_rval.encoded+7)/8);
2714 2715 2716
#ifdef ITTI_SIM
			  MessageDef *message_p;
			  uint8_t *message_buffer;
2717
			  message_buffer = itti_malloc (TASK_RRC_NRUE,TASK_RRC_GNB_SIM,
2718 2719 2720
								 (enc_rval.encoded + 7) / 8);
			  memcpy (message_buffer, buffer, (enc_rval.encoded + 7) / 8);
	  
2721
			  message_p = itti_alloc_new_message (TASK_RRC_NRUE, UE_RRC_DCCH_DATA_IND);
2722 2723 2724 2725 2726
			  GNB_RRC_DCCH_DATA_IND (message_p).rbid  = DCCH;
			  GNB_RRC_DCCH_DATA_IND (message_p).sdu   = message_buffer;
			  GNB_RRC_DCCH_DATA_IND (message_p).size  = (enc_rval.encoded + 7) / 8;
			  itti_send_msg_to_task (TASK_RRC_GNB_SIM, ctxt_pP->instance, message_p);
#else
zhanghuiqin's avatar
zhanghuiqin committed
2727 2728 2729 2730 2731 2732 2733 2734
      rrc_data_req_ue (
        ctxt_pP,
        DCCH,
        nr_rrc_mui++,
        SDU_CONFIRM_NO,
        (enc_rval.encoded + 7) / 8,
        buffer,
        PDCP_TRANSMISSION_MODE_CONTROL);
2735
#endif
zhanghuiqin's avatar
zhanghuiqin committed
2736 2737 2738
    }
  }
}