L2_nr_interface.c 14.3 KB
Newer Older
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

/*! \file l2_nr_interface.c
 * \brief layer 2 interface, used to support different RRC sublayer
 * \author Raymond Knopp and Navid Nikaein, WEI-TAI CHEN
 * \date 2010-2014, 2018
 * \version 1.0
 * \company Eurecom, NTUST
 * \email: raymond.knopp@eurecom.fr, kroempa@gmail.com
 */

31
#include "platform_types.h"
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
32 33
#include "nr_rrc_defs.h"
#include "nr_rrc_extern.h"
Raymond Knopp's avatar
Raymond Knopp committed
34
#include "common/utils/LOG/log.h"
35 36 37
#include "pdcp.h"
#include "msc.h"
#include "common/ran_context.h"
38 39
#include "LAYER2/NR_MAC_COMMON/nr_mac_common.h"
#include "LAYER2/NR_MAC_COMMON/nr_mac_extern.h"
40

41
#include "intertask_interface.h"
42

Raymond Knopp's avatar
Raymond Knopp committed
43 44
#include "NR_MIB.h"
#include "NR_BCCH-BCH-Message.h"
rmagueta's avatar
rmagueta committed
45
#include "rrc_gNB_UE_context.h"
46 47
#include <openair2/RRC/NR/MESSAGES/asn1_msg.h>

Raymond Knopp's avatar
Raymond Knopp committed
48

49 50
extern RAN_CONTEXT_t RC;

51 52 53 54 55
int generate_pdcch_ConfigSIB1(NR_PDCCH_ConfigSIB1_t *pdcch_ConfigSIB1,
                              long ssbSubcarrierSpacing,
                              long subCarrierSpacingCommon,
                              channel_bandwidth_t min_channel_bw) {

rmagueta's avatar
rmagueta committed
56
  nr_ssb_and_cset_mux_pattern_type_t mux_pattern = 0;
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

  switch (ssbSubcarrierSpacing) {

    case NR_SubcarrierSpacing_kHz15:
      if (subCarrierSpacingCommon == NR_SubcarrierSpacing_kHz15) {
        pdcch_ConfigSIB1->controlResourceSetZero = rand() % TABLE_38213_13_1_NUM_INDEXES;
        mux_pattern = table_38213_13_1_c1[pdcch_ConfigSIB1->controlResourceSetZero];
      } else if (subCarrierSpacingCommon == NR_SubcarrierSpacing_kHz30) {
        pdcch_ConfigSIB1->controlResourceSetZero = rand() % TABLE_38213_13_2_NUM_INDEXES;
        mux_pattern = table_38213_13_2_c1[pdcch_ConfigSIB1->controlResourceSetZero];
      } else {
        AssertFatal(true,"Invalid subCarrierSpacingCommon\n");
      }
      break;

    case NR_SubcarrierSpacing_kHz30:
      if (subCarrierSpacingCommon == NR_SubcarrierSpacing_kHz15) {

        if ( (min_channel_bw == bw_5MHz) || (min_channel_bw == bw_10MHz) ) {
          pdcch_ConfigSIB1->controlResourceSetZero = rand() % TABLE_38213_13_3_NUM_INDEXES;
          mux_pattern = table_38213_13_3_c1[pdcch_ConfigSIB1->controlResourceSetZero];
        } else if (min_channel_bw == bw_40MHz) {
          pdcch_ConfigSIB1->controlResourceSetZero = rand() % TABLE_38213_13_5_NUM_INDEXES;
          mux_pattern = table_38213_13_5_c1[pdcch_ConfigSIB1->controlResourceSetZero];
        } else {
          AssertFatal(true,"Invalid min_bandwidth\n");
        }

      } else if (subCarrierSpacingCommon == NR_SubcarrierSpacing_kHz30) {

        if ( (min_channel_bw == bw_5MHz) || (min_channel_bw == bw_10MHz) ) {
          pdcch_ConfigSIB1->controlResourceSetZero = rand() % TABLE_38213_13_4_NUM_INDEXES;
          mux_pattern = table_38213_13_4_c1[pdcch_ConfigSIB1->controlResourceSetZero];
        } else if (min_channel_bw == bw_40MHz) {
          pdcch_ConfigSIB1->controlResourceSetZero = rand() % TABLE_38213_13_6_NUM_INDEXES;
          mux_pattern = table_38213_13_6_c1[pdcch_ConfigSIB1->controlResourceSetZero];
        } else {
          AssertFatal(true,"Invalid min_bandwidth\n");
        }

      } else {
        AssertFatal(true,"Invalid subCarrierSpacingCommon\n");
      }
      break;

    case NR_SubcarrierSpacing_kHz120:
      if (subCarrierSpacingCommon == NR_SubcarrierSpacing_kHz60) {
        pdcch_ConfigSIB1->controlResourceSetZero = rand() % TABLE_38213_13_7_NUM_INDEXES;
        mux_pattern = table_38213_13_7_c1[pdcch_ConfigSIB1->controlResourceSetZero];
      } else if (subCarrierSpacingCommon == NR_SubcarrierSpacing_kHz120) {
        pdcch_ConfigSIB1->controlResourceSetZero = rand() % TABLE_38213_13_8_NUM_INDEXES;
        mux_pattern = table_38213_13_8_c1[pdcch_ConfigSIB1->controlResourceSetZero];
      } else {
        AssertFatal(true,"Invalid subCarrierSpacingCommon\n");
      }
      break;

    case NR_SubcarrierSpacing_kHz240:
      if (subCarrierSpacingCommon == NR_SubcarrierSpacing_kHz60) {
        pdcch_ConfigSIB1->controlResourceSetZero = rand() % TABLE_38213_13_9_NUM_INDEXES;
        mux_pattern = table_38213_13_9_c1[pdcch_ConfigSIB1->controlResourceSetZero];
      } else if (subCarrierSpacingCommon == NR_SubcarrierSpacing_kHz120) {
        pdcch_ConfigSIB1->controlResourceSetZero = rand() % TABLE_38213_13_10_NUM_INDEXES;
        mux_pattern = table_38213_13_10_c1[pdcch_ConfigSIB1->controlResourceSetZero];
      } else {
        AssertFatal(true,"Invalid subCarrierSpacingCommon\n");
      }
      break;

    default:
      AssertFatal(true,"Invalid ssbSubcarrierSpacing\n");
      break;
  }


  frequency_range_t frequency_range = FR1;
  if(ssbSubcarrierSpacing>=60) {
    frequency_range = FR2;
  }

  pdcch_ConfigSIB1->searchSpaceZero = 0;
  if(mux_pattern == NR_SSB_AND_CSET_MUX_PATTERN_TYPE1 && frequency_range == FR1){
    pdcch_ConfigSIB1->searchSpaceZero = rand() % TABLE_38213_13_11_NUM_INDEXES;
  }
  if(mux_pattern == NR_SSB_AND_CSET_MUX_PATTERN_TYPE1 && frequency_range == FR2){
    pdcch_ConfigSIB1->searchSpaceZero = rand() % TABLE_38213_13_12_NUM_INDEXES;
  }

  return 0;
}

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
int
nr_rrc_mac_remove_ue(module_id_t mod_idP,
                  rnti_t rntiP){
  // todo
  return 0;
}

//------------------------------------------------------------------------------
uint8_t
nr_rrc_data_req(
  const protocol_ctxt_t   *const ctxt_pP,
  const rb_id_t                  rb_idP,
  const mui_t                    muiP,
  const confirm_t                confirmP,
  const sdu_size_t               sdu_sizeP,
  uint8_t                 *const buffer_pP,
  const pdcp_transmission_mode_t modeP
)
//------------------------------------------------------------------------------
{
  if(sdu_sizeP == 255) {
Xue Song's avatar
Xue Song committed
169
    LOG_I(NR_RRC,"sdu_sizeP == 255");
170 171 172 173
    return FALSE;
  }

  MSC_LOG_TX_MESSAGE(
174
    ctxt_pP->enb_flag ? MSC_RRC_GNB : MSC_RRC_UE,
175 176 177
    ctxt_pP->enb_flag ? MSC_PDCP_ENB : MSC_PDCP_UE,
    buffer_pP,
    sdu_sizeP,
178
    MSC_AS_TIME_FMT"RRC_DCCH_DATA_REQ UE %x MUI %d size %u",
179 180 181 182 183 184 185 186
    MSC_AS_TIME_ARGS(ctxt_pP),
    ctxt_pP->rnti,
    muiP,
    sdu_sizeP);
  MessageDef *message_p;
  // Uses a new buffer to avoid issue with PDCP buffer content that could be changed by PDCP (asynchronous message handling).
  uint8_t *message_buffer;
  message_buffer = itti_malloc (
187
                     ctxt_pP->enb_flag ? TASK_RRC_GNB : TASK_RRC_UE,
188 189 190
                     ctxt_pP->enb_flag ? TASK_PDCP_ENB : TASK_PDCP_UE,
                     sdu_sizeP);
  memcpy (message_buffer, buffer_pP, sdu_sizeP);
191
  message_p = itti_alloc_new_message (ctxt_pP->enb_flag ? TASK_RRC_GNB : TASK_RRC_UE, 0, RRC_DCCH_DATA_REQ);
192 193 194 195 196 197 198
  RRC_DCCH_DATA_REQ (message_p).frame     = ctxt_pP->frame;
  RRC_DCCH_DATA_REQ (message_p).enb_flag  = ctxt_pP->enb_flag;
  RRC_DCCH_DATA_REQ (message_p).rb_id     = rb_idP;
  RRC_DCCH_DATA_REQ (message_p).muip      = muiP;
  RRC_DCCH_DATA_REQ (message_p).confirmp  = confirmP;
  RRC_DCCH_DATA_REQ (message_p).sdu_size  = sdu_sizeP;
  RRC_DCCH_DATA_REQ (message_p).sdu_p     = message_buffer;
Xue Song's avatar
Xue Song committed
199
  //memcpy (NR_RRC_DCCH_DATA_REQ (message_p).sdu_p, buffer_pP, sdu_sizeP);
200 201 202 203
  RRC_DCCH_DATA_REQ (message_p).mode      = modeP;
  RRC_DCCH_DATA_REQ (message_p).module_id = ctxt_pP->module_id;
  RRC_DCCH_DATA_REQ (message_p).rnti      = ctxt_pP->rnti;
  RRC_DCCH_DATA_REQ (message_p).eNB_index = ctxt_pP->eNB_index;
204 205 206 207
  itti_send_msg_to_task (
    ctxt_pP->enb_flag ? TASK_PDCP_ENB : TASK_PDCP_UE,
    ctxt_pP->instance,
    message_p);
Xue Song's avatar
Xue Song committed
208
  LOG_I(NR_RRC,"send RRC_DCCH_DATA_REQ to PDCP\n");
209 210 211 212

  /* Hack: only trigger PDCP if in CU, otherwise it is triggered by RU threads
   * Ideally, PDCP would not neet to be triggered like this but react to ITTI
   * messages automatically */
Xue Song's avatar
Xue Song committed
213 214
  if (ctxt_pP->enb_flag && NODE_IS_CU(RC.nrrrc[ctxt_pP->module_id]->node_type))
    pdcp_run(ctxt_pP);
215 216 217 218

  return TRUE; // TODO should be changed to a CNF message later, currently RRC lite does not used the returned value anyway.
}

219 220 221 222
int8_t mac_rrc_nr_data_req(const module_id_t Mod_idP,
                           const int         CC_id,
                           const frame_t     frameP,
                           const rb_id_t     Srb_id,
rmagueta's avatar
rmagueta committed
223
                           const rnti_t      rnti,
224 225 226 227
                           const uint8_t     Nb_tb,
                           uint8_t *const    buffer_pP ){

#ifdef DEBUG_RRC
laurent's avatar
laurent committed
228
  LOG_D(RRC,"[eNB %d] mac_rrc_data_req to SRB ID=%ld\n",Mod_idP,Srb_id);
229 230
#endif

rmagueta's avatar
rmagueta committed
231
    // MIBCH
232 233
    if ((Srb_id & RAB_OFFSET) == MIBCH) {

rmagueta's avatar
rmagueta committed
234 235 236 237 238
      asn_enc_rval_t enc_rval;
      uint8_t sfn_msb = (uint8_t)((frameP>>4)&0x3f);
      rrc_gNB_carrier_data_t *carrier = &RC.nrrrc[Mod_idP]->carrier;
      NR_BCCH_BCH_Message_t *mib = &carrier->mib;

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
        // Currently we are getting the pdcch_ConfigSIB1 from the configuration file.
        // Uncomment this function for a dynamic pdcch_ConfigSIB1.
        //channel_bandwidth_t min_channel_bw = bw_10MHz; // Must be obtained based on TS 38.101-1 Table 5.3.5-1
        //generate_pdcch_ConfigSIB1(carrier->pdcch_ConfigSIB1,
        //                          *carrier->servingcellconfigcommon->ssbSubcarrierSpacing,
        //                          carrier->mib.message.choice.mib->subCarrierSpacingCommon,
        //                          min_channel_bw);

        mib->message.choice.mib->pdcch_ConfigSIB1.controlResourceSetZero = carrier->pdcch_ConfigSIB1->controlResourceSetZero;
        mib->message.choice.mib->pdcch_ConfigSIB1.searchSpaceZero = carrier->pdcch_ConfigSIB1->searchSpaceZero;

        mib->message.choice.mib->systemFrameNumber.buf[0] = sfn_msb << 2;
        enc_rval = uper_encode_to_buffer(&asn_DEF_NR_BCCH_BCH_Message,
                                         NULL,
                                         (void *) mib,
                                         carrier->MIB,
                                         24);
        LOG_D(NR_RRC, "Encoded MIB for frame %d sfn_msb %d (%p), bits %lu\n", frameP, sfn_msb, carrier->MIB,
              enc_rval.encoded);
        buffer_pP[0] = carrier->MIB[0];
        buffer_pP[1] = carrier->MIB[1];
        buffer_pP[2] = carrier->MIB[2];
        LOG_D(NR_RRC, "MIB PDU buffer_pP[0]=%x , buffer_pP[1]=%x, buffer_pP[2]=%x\n", buffer_pP[0], buffer_pP[1],
              buffer_pP[2]);
        AssertFatal (enc_rval.encoded > 0, "ASN1 message encoding failed (%s, %lu)!\n",
                     enc_rval.failed_type->name, enc_rval.encoded);
        return (3);
    }
267

rmagueta's avatar
rmagueta committed
268
  // TODO BCCH SIB1 SIBs
269
  if ((Srb_id & RAB_OFFSET ) == BCCH) {
270 271 272
    memcpy(&buffer_pP[0],
           RC.nrrrc[Mod_idP]->carrier.SIB1,
           RC.nrrrc[Mod_idP]->carrier.sizeof_SIB1);
273

274
    return RC.nrrrc[Mod_idP]->carrier.sizeof_SIB1;
275
  }
276

rmagueta's avatar
rmagueta committed
277
  // CCCH
278 279
  if( (Srb_id & RAB_OFFSET ) == CCCH) {

rmagueta's avatar
rmagueta committed
280 281 282 283 284
    char *payload_pP;
    uint8_t Sdu_size = 0;
    struct rrc_gNB_ue_context_s *ue_context_p = rrc_gNB_get_ue_context(RC.nrrrc[Mod_idP], rnti);

    LOG_D(NR_RRC,"[gNB %d] Frame %d CCCH request (Srb_id %ld)\n", Mod_idP, frameP, Srb_id);
285

286
    AssertFatal(ue_context_p!=NULL,"failed to get ue_context\n");
rmagueta's avatar
rmagueta committed
287
    char payload_size = ue_context_p->ue_context.Srb0.Tx_buffer.payload_size;
288 289 290

    // check if data is there for MAC
    if (payload_size > 0) {
rmagueta's avatar
rmagueta committed
291 292
      payload_pP = ue_context_p->ue_context.Srb0.Tx_buffer.Payload;
      LOG_D(NR_RRC,"[gNB %d] CCCH has %d bytes (dest: %p, src %p)\n", Mod_idP, payload_size, buffer_pP, payload_pP);
293
      // Fill buffer
cig's avatar
cig committed
294
      memcpy((void *)buffer_pP, (void*)payload_pP, payload_size);
295
      Sdu_size = payload_size;
rmagueta's avatar
rmagueta committed
296
      ue_context_p->ue_context.Srb0.Tx_buffer.payload_size = 0;
297 298 299
    }
    return Sdu_size;
  }
300 301 302

  return(0);

303
}
304 305 306 307 308 309 310 311 312 313 314 315

int8_t nr_mac_rrc_data_ind(const module_id_t     module_idP,
                           const int             CC_id,
                           const frame_t         frameP,
                           const sub_frame_t     sub_frameP,
                           const int             UE_id,
                           const rnti_t          rntiP,
                           const rb_id_t         srb_idP,
                           const uint8_t        *sduP,
                           const sdu_size_t      sdu_lenP,
                           const boolean_t   brOption) {

316 317 318
  if (NODE_IS_DU(RC.nrrrc[module_idP]->node_type)) {
    LOG_W(RRC,"[DU %d][RAPROC] Received SDU for CCCH on SRB %ld length %d for UE id %d RNTI %x \n",
          module_idP, srb_idP, sdu_lenP, UE_id, rntiP);
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

    // Generate DUtoCURRCContainer
    // call do_RRCSetup like full procedure and extract masterCellGroup
    NR_CellGroupConfig_t cellGroupConfig;
    NR_ServingCellConfigCommon_t *scc=RC.nrrrc[module_idP]->carrier.servingcellconfigcommon;
    uint8_t sdu2[100];
    memset(&cellGroupConfig,0,sizeof(cellGroupConfig));
    fill_initial_cellGroupConfig(rntiP,&cellGroupConfig,scc);
    asn_enc_rval_t enc_rval = uper_encode_to_buffer(&asn_DEF_NR_CellGroupConfig,
						    NULL,
						    (void *)&cellGroupConfig,
						    sdu2,
						    100);

    int sdu2_len = (enc_rval.encoded+7)/8;
    if (enc_rval.encoded == -1) {
      LOG_I(F1AP,"Could not encoded cellGroupConfig, failed element %s\n",enc_rval.failed_type->name);
      exit(-1);
    }
338 339 340 341 342 343 344
    /* do ITTI message */
    DU_send_INITIAL_UL_RRC_MESSAGE_TRANSFER(
      module_idP,
      CC_id,
      UE_id,
      rntiP,
      sduP,
345 346 347
      sdu_lenP,
      sdu2,
      sdu2_len
348 349 350 351
    );
    return(0);
  }

352 353 354 355 356 357 358
  protocol_ctxt_t ctxt;
  PROTOCOL_CTXT_SET_BY_MODULE_ID(&ctxt, module_idP, GNB_FLAG_YES, rntiP, frameP, sub_frameP,0);

  if((srb_idP & RAB_OFFSET) == CCCH) {
    LOG_D(NR_RRC, "[gNB %d] Received SDU for CCCH on SRB %ld\n", module_idP, srb_idP);
    ctxt.brOption = brOption;
    if (sdu_lenP > 0) {
359
      nr_rrc_gNB_decode_ccch(&ctxt, sduP, sdu_lenP, NULL, CC_id);
360 361 362 363
    }
  }

  return 0;
364
}