ngap_gNB_nas_procedures.c 72.1 KB
Newer Older
zhenghuangkun's avatar
zhenghuangkun committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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 ngap_gNB_nas_procedures.c
zhenghuangkun's avatar
zhenghuangkun committed
23 24 25 26
 * \brief NGAP gNb NAS procedure handler
 * \author  Yoshio INOUE, Masayuki HARADA 
 * \date 2020
 * \email: yoshio.inoue@fujitsu.com,masayuki.harada@fujitsu.com (yoshio.inoue%40fujitsu.com%2cmasayuki.harada%40fujitsu.com)
zhenghuangkun's avatar
zhenghuangkun committed
27 28 29
 * \version 1.0
 * @ingroup _ngap
 */
zhenghuangkun's avatar
zhenghuangkun committed
30 31

 
zhenghuangkun's avatar
zhenghuangkun committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#include "assertions.h"
#include "conversions.h"

#include "intertask_interface.h"

#include "ngap_common.h"
#include "ngap_gNB_defs.h"

#include "ngap_gNB_itti_messaging.h"

#include "ngap_gNB_encoder.h"
#include "ngap_gNB_nnsf.h"
#include "ngap_gNB_ue_context.h"
#include "ngap_gNB_nas_procedures.h"
#include "ngap_gNB_management_procedures.h"
#include "msc.h"

//------------------------------------------------------------------------------
int ngap_gNB_handle_nas_first_req(
    instance_t instance, ngap_nas_first_req_t *ngap_nas_first_req_p)
//------------------------------------------------------------------------------
{
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
  ngap_gNB_instance_t          *instance_p = NULL;
  struct ngap_gNB_amf_data_s   *amf_desc_p = NULL;
  struct ngap_gNB_ue_context_s *ue_desc_p  = NULL;
  NGAP_NGAP_PDU_t               pdu;
  NGAP_InitialUEMessage_t      *out;
  NGAP_InitialUEMessage_IEs_t  *ie;
  NGAP_UserLocationInformationNR_t *userinfo_nr_p = NULL;
  uint8_t  *buffer = NULL;
  uint32_t  length = 0;
  DevAssert(ngap_nas_first_req_p != NULL);
  /* Retrieve the NGAP gNB instance associated with Mod_id */
  instance_p = ngap_gNB_get_instance(instance);
  DevAssert(instance_p != NULL);
  memset(&pdu, 0, sizeof(pdu));
  pdu.present = NGAP_NGAP_PDU_PR_initiatingMessage;
  pdu.choice.initiatingMessage = (NGAP_InitiatingMessage_t *)calloc(1,sizeof(NGAP_InitiatingMessage_t));
  pdu.choice.initiatingMessage->procedureCode = NGAP_ProcedureCode_id_InitialUEMessage;
  pdu.choice.initiatingMessage->criticality = NGAP_Criticality_ignore;
  pdu.choice.initiatingMessage->value.present = NGAP_InitiatingMessage__value_PR_InitialUEMessage;
  out = &pdu.choice.initiatingMessage->value.choice.InitialUEMessage;

  /* Select the AMF corresponding to the provided GUAMI. */
  //TODO have not be test. it's should be test
  if (ngap_nas_first_req_p->ue_identity.presenceMask & NGAP_UE_IDENTITIES_guami) {
    amf_desc_p = ngap_gNB_nnsf_select_amf_by_guami(
                     instance_p,
                     ngap_nas_first_req_p->establishment_cause,
                     ngap_nas_first_req_p->ue_identity.guami);

    if (amf_desc_p) {
        NGAP_INFO("[gNB %d] Chose AMF '%s' (assoc_id %d) through GUAMI MCC %d MNC %d AMFRI %d AMFSI %d AMFPT %d\n",
                  instance,
                  amf_desc_p->amf_name,
                  amf_desc_p->assoc_id,
                  ngap_nas_first_req_p->ue_identity.guami.mcc,
                  ngap_nas_first_req_p->ue_identity.guami.mnc,
                  ngap_nas_first_req_p->ue_identity.guami.amf_region_id,
                  ngap_nas_first_req_p->ue_identity.guami.amf_set_id,
                  ngap_nas_first_req_p->ue_identity.guami.amf_pointer);
97
    }
98
  }
zhenghuangkun's avatar
zhenghuangkun committed
99

100 101 102 103 104
  if (amf_desc_p == NULL) {
    /* Select the AMF corresponding to the provided s-TMSI. */
    //TODO have not be test. it's should be test
    if (ngap_nas_first_req_p->ue_identity.presenceMask & NGAP_UE_IDENTITIES_FiveG_s_tmsi) {
        amf_desc_p = ngap_gNB_nnsf_select_amf_by_amf_setid(
zhenghuangkun's avatar
zhenghuangkun committed
105 106
                         instance_p,
                         ngap_nas_first_req_p->establishment_cause,
107 108
                         ngap_nas_first_req_p->selected_plmn_identity,
                         ngap_nas_first_req_p->ue_identity.s_tmsi.amf_set_id);
zhenghuangkun's avatar
zhenghuangkun committed
109

zhenghuangkun's avatar
zhenghuangkun committed
110
        if (amf_desc_p) {
111
            NGAP_INFO("[gNB %d] Chose AMF '%s' (assoc_id %d) through S-TMSI AMFSI %d and selected PLMN Identity index %d MCC %d MNC %d\n",
zhenghuangkun's avatar
zhenghuangkun committed
112
                      instance,
zhenghuangkun's avatar
zhenghuangkun committed
113 114
                      amf_desc_p->amf_name,
                      amf_desc_p->assoc_id,
115
                      ngap_nas_first_req_p->ue_identity.s_tmsi.amf_set_id,
zhenghuangkun's avatar
zhenghuangkun committed
116 117 118
                      ngap_nas_first_req_p->selected_plmn_identity,
                      instance_p->mcc[ngap_nas_first_req_p->selected_plmn_identity],
                      instance_p->mnc[ngap_nas_first_req_p->selected_plmn_identity]);
119
        }
zhenghuangkun's avatar
zhenghuangkun committed
120
    }
121
  }
zhenghuangkun's avatar
zhenghuangkun committed
122

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  if (amf_desc_p == NULL) {
    /* Select AMF based on the selected PLMN identity, received through RRC
     * Connection Setup Complete */
    //TODO have not be test. it's should be test
    amf_desc_p = ngap_gNB_nnsf_select_amf_by_plmn_id(
                     instance_p,
                     ngap_nas_first_req_p->establishment_cause,
                     ngap_nas_first_req_p->selected_plmn_identity);

    if (amf_desc_p) {
        NGAP_INFO("[gNB %d] Chose AMF '%s' (assoc_id %d) through selected PLMN Identity index %d MCC %d MNC %d\n",
                  instance,
                  amf_desc_p->amf_name,
                  amf_desc_p->assoc_id,
                  ngap_nas_first_req_p->selected_plmn_identity,
                  instance_p->mcc[ngap_nas_first_req_p->selected_plmn_identity],
                  instance_p->mnc[ngap_nas_first_req_p->selected_plmn_identity]);
zhenghuangkun's avatar
zhenghuangkun committed
140
    }
141
  }
zhenghuangkun's avatar
zhenghuangkun committed
142

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  if (amf_desc_p == NULL) {
    /*
     * If no AMF corresponds to the GUAMI, the s-TMSI, or the selected PLMN
     * identity, selects the AMF with the highest capacity.
     */
    //TODO have not be test. it's should be test
    amf_desc_p = ngap_gNB_nnsf_select_amf(
                     instance_p,
                     ngap_nas_first_req_p->establishment_cause);

    if (amf_desc_p) {
        NGAP_INFO("[gNB %d] Chose AMF '%s' (assoc_id %d) through highest relative capacity\n",
                  instance,
                  amf_desc_p->amf_name,
                  amf_desc_p->assoc_id);
zhenghuangkun's avatar
zhenghuangkun committed
158
    }
159
  }
zhenghuangkun's avatar
zhenghuangkun committed
160

161 162 163 164
  if (amf_desc_p == NULL) {
    /*
     * In case gNB has no AMF associated, the gNB should inform RRC and discard
     * this request.
zhenghuangkun's avatar
zhenghuangkun committed
165
     */
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    NGAP_WARN("No AMF is associated to the gNB\n");
    // TODO: Inform RRC
    return -1;
  }

  /* The gNB should allocate a unique gNB UE NGAP ID for this UE. The value
   * will be used for the duration of the connectivity.
   */
  ue_desc_p = ngap_gNB_allocate_new_UE_context();
  DevAssert(ue_desc_p != NULL);
  /* Keep a reference to the selected AMF */
  ue_desc_p->amf_ref       = amf_desc_p;
  ue_desc_p->ue_initial_id = ngap_nas_first_req_p->ue_initial_id;
  ue_desc_p->gNB_instance  = instance_p;
  ue_desc_p->selected_plmn_identity = ngap_nas_first_req_p->selected_plmn_identity;

  do {
    struct ngap_gNB_ue_context_s *collision_p;
    /* Peek a random value for the gNB_ue_ngap_id */
    ue_desc_p->gNB_ue_ngap_id = (random() + random()) & 0xffffffff;

    if ((collision_p = RB_INSERT(ngap_ue_map, &instance_p->ngap_ue_head, ue_desc_p))
            == NULL) {
        NGAP_DEBUG("Found usable gNB_ue_ngap_id: 0x%08x %u(10)\n",
                   ue_desc_p->gNB_ue_ngap_id,
                   ue_desc_p->gNB_ue_ngap_id);
        /* Break the loop as the id is not already used by another UE */
        break;
    }
  } while(1);

  /* mandatory */
  ie = (NGAP_InitialUEMessage_IEs_t *)calloc(1, sizeof(NGAP_InitialUEMessage_IEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_RAN_UE_NGAP_ID;
  ie->criticality = NGAP_Criticality_reject;
  ie->value.present = NGAP_InitialUEMessage_IEs__value_PR_RAN_UE_NGAP_ID;
  ie->value.choice.RAN_UE_NGAP_ID = ue_desc_p->gNB_ue_ngap_id;
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  /* mandatory */
  ie = (NGAP_InitialUEMessage_IEs_t *)calloc(1, sizeof(NGAP_InitialUEMessage_IEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_NAS_PDU;
  ie->criticality = NGAP_Criticality_reject;
  ie->value.present = NGAP_InitialUEMessage_IEs__value_PR_NAS_PDU;
zhenghuangkun's avatar
zhenghuangkun committed
209
#if 1
210
  ie->value.choice.NAS_PDU.buf = ngap_nas_first_req_p->nas_pdu.buffer;
zhenghuangkun's avatar
zhenghuangkun committed
211
#else
212 213 214 215
  ie->value.choice.NAS_PDU.buf = malloc(ngap_nas_first_req_p->nas_pdu.length);
  memcpy(ie->value.choice.NAS_PDU.buf,
         ngap_nas_first_req_p->nas_pdu.buffer,
         ngap_nas_first_req_p->nas_pdu.length);
zhenghuangkun's avatar
zhenghuangkun committed
216
#endif
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
  ie->value.choice.NAS_PDU.size = ngap_nas_first_req_p->nas_pdu.length;
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  
  /* mandatory */
  ie = (NGAP_InitialUEMessage_IEs_t *)calloc(1, sizeof(NGAP_InitialUEMessage_IEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_UserLocationInformation;
  ie->criticality = NGAP_Criticality_reject;
  ie->value.present = NGAP_InitialUEMessage_IEs__value_PR_UserLocationInformation;

  ie->value.choice.UserLocationInformation.present = NGAP_UserLocationInformation_PR_userLocationInformationNR;

  ie->value.choice.UserLocationInformation.choice.userLocationInformationNR = 
      CALLOC(1, sizeof(*ie->value.choice.UserLocationInformation.choice.userLocationInformationNR));
  userinfo_nr_p = ie->value.choice.UserLocationInformation.choice.userLocationInformationNR;

  /* Set nRCellIdentity. default userLocationInformationNR */
  MACRO_GNB_ID_TO_CELL_IDENTITY(instance_p->gNB_id,
                                    0, // Cell ID
                                    &userinfo_nr_p->nR_CGI.nRCellIdentity);
  MCC_MNC_TO_TBCD(instance_p->mcc[ue_desc_p->selected_plmn_identity],
                  instance_p->mnc[ue_desc_p->selected_plmn_identity],
                  instance_p->mnc_digit_length[ue_desc_p->selected_plmn_identity],
                  &userinfo_nr_p->nR_CGI.pLMNIdentity);

  /* Set TAI */
  INT24_TO_OCTET_STRING(instance_p->tac, &userinfo_nr_p->tAI.tAC);
  MCC_MNC_TO_PLMNID(instance_p->mcc[ue_desc_p->selected_plmn_identity],
zhenghuangkun's avatar
zhenghuangkun committed
244 245
                    instance_p->mnc[ue_desc_p->selected_plmn_identity],
                    instance_p->mnc_digit_length[ue_desc_p->selected_plmn_identity],
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
                    &userinfo_nr_p->tAI.pLMNIdentity);

  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);


  /* Set the establishment cause according to those provided by RRC */
  DevCheck(ngap_nas_first_req_p->establishment_cause < NGAP_RRC_CAUSE_LAST,
           ngap_nas_first_req_p->establishment_cause, NGAP_RRC_CAUSE_LAST, 0);
  
  /* mandatory */
  ie = (NGAP_InitialUEMessage_IEs_t *)calloc(1, sizeof(NGAP_InitialUEMessage_IEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_RRCEstablishmentCause;
  ie->criticality = NGAP_Criticality_ignore;
  ie->value.present = NGAP_InitialUEMessage_IEs__value_PR_RRCEstablishmentCause;
  ie->value.choice.RRCEstablishmentCause = ngap_nas_first_req_p->establishment_cause;
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);

  /* optional */
  if (ngap_nas_first_req_p->ue_identity.presenceMask & NGAP_UE_IDENTITIES_FiveG_s_tmsi) {
    NGAP_DEBUG("FIVEG_S_TMSI_PRESENT\n");
zhenghuangkun's avatar
zhenghuangkun committed
266
    ie = (NGAP_InitialUEMessage_IEs_t *)calloc(1, sizeof(NGAP_InitialUEMessage_IEs_t));
267 268 269 270 271 272 273 274 275
    ie->id = NGAP_ProtocolIE_ID_id_FiveG_S_TMSI;
    ie->criticality = NGAP_Criticality_reject;
    ie->value.present = NGAP_InitialUEMessage_IEs__value_PR_FiveG_S_TMSI;
    AMF_SETID_TO_BIT_STRING(ngap_nas_first_req_p->ue_identity.s_tmsi.amf_set_id,
                             &ie->value.choice.FiveG_S_TMSI.aMFSetID);
    AMF_SETID_TO_BIT_STRING(ngap_nas_first_req_p->ue_identity.s_tmsi.amf_pointer,
                             &ie->value.choice.FiveG_S_TMSI.aMFPointer);
    M_TMSI_TO_OCTET_STRING(ngap_nas_first_req_p->ue_identity.s_tmsi.m_tmsi,
                             &ie->value.choice.FiveG_S_TMSI.fiveG_TMSI);
zhenghuangkun's avatar
zhenghuangkun committed
276
    ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
  }


  /* optional */
  ie = (NGAP_InitialUEMessage_IEs_t *)calloc(1, sizeof(NGAP_InitialUEMessage_IEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_UEContextRequest;
  ie->criticality = NGAP_Criticality_ignore;
  ie->value.present = NGAP_InitialUEMessage_IEs__value_PR_UEContextRequest;
  ie->value.choice.UEContextRequest = NGAP_UEContextRequest_requested;
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);


  if (ngap_gNB_encode_pdu(&pdu, &buffer, &length) < 0) {
      /* Failed to encode message */
      DevMessage("Failed to encode initial UE message\n");
  }

  /* Update the current NGAP UE state */
  ue_desc_p->ue_state = NGAP_UE_WAITING_CSR;
  /* Assign a stream for this UE :
   * From 3GPP 38.412 7)Transport layers:
   *  Within the SCTP association established between one AMF and gNB pair:
   *  - a single pair of stream identifiers shall be reserved for the sole use
   *      of NGAP elementary procedures that utilize non UE-associated signalling.
   *  - At least one pair of stream identifiers shall be reserved for the sole use
   *      of NGAP elementary procedures that utilize UE-associated signallings.
   *      However a few pairs (i.e. more than one) should be reserved.
   *  - A single UE-associated signalling shall use one SCTP stream and
   *      the stream should not be changed during the communication of the
   *      UE-associated signalling.
   */
  amf_desc_p->nextstream = (amf_desc_p->nextstream + 1) % amf_desc_p->out_streams;

  if ((amf_desc_p->nextstream == 0) && (amf_desc_p->out_streams > 1)) {
    amf_desc_p->nextstream += 1;
  }

  ue_desc_p->tx_stream = amf_desc_p->nextstream;
  MSC_LOG_TX_MESSAGE(
      MSC_NGAP_GNB,
      MSC_NGAP_AMF,
      (const char *)NULL,
      0,
      MSC_AS_TIME_FMT" initialUEMessage initiatingMessage gNB_ue_ngap_id %u",
      0,0,//MSC_AS_TIME_ARGS(ctxt_pP),
      ue_desc_p->gNB_ue_ngap_id);
  /* Send encoded message over sctp */
  ngap_gNB_itti_send_sctp_data_req(instance_p->instance, amf_desc_p->assoc_id,
                                   buffer, length, ue_desc_p->tx_stream);
zhenghuangkun's avatar
zhenghuangkun committed
326

327
  return 0;
zhenghuangkun's avatar
zhenghuangkun committed
328 329 330 331 332 333 334 335
}

//------------------------------------------------------------------------------
int ngap_gNB_handle_nas_downlink(uint32_t         assoc_id,
                                 uint32_t         stream,
                                 NGAP_NGAP_PDU_t *pdu)
//------------------------------------------------------------------------------
{
336

337 338 339 340 341 342 343 344 345 346
  ngap_gNB_amf_data_t             *amf_desc_p        = NULL;
  ngap_gNB_ue_context_t           *ue_desc_p         = NULL;
  ngap_gNB_instance_t             *ngap_gNB_instance = NULL;
  NGAP_DownlinkNASTransport_t     *container;
  NGAP_DownlinkNASTransport_IEs_t *ie;
  NGAP_RAN_UE_NGAP_ID_t            gnb_ue_ngap_id;
  uint64_t                         amf_ue_ngap_id;
  DevAssert(pdu != NULL);

  /* UE-related procedure -> stream != 0 */
wujing's avatar
wujing committed
347 348 349 350 351
  // if (stream == 0) {
  //     NGAP_ERROR("[SCTP %d] Received UE-related procedure on stream == 0\n",
  //                assoc_id);
  //     return -1;
  // }
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

  if ((amf_desc_p = ngap_gNB_get_AMF(NULL, assoc_id, 0)) == NULL) {
    NGAP_ERROR(
        "[SCTP %d] Received NAS downlink message for non existing AMF context\n",
        assoc_id);
    return -1;
  }

  ngap_gNB_instance = amf_desc_p->ngap_gNB_instance;
  /* Prepare the NGAP message to encode */
  container = &pdu->choice.initiatingMessage->value.choice.DownlinkNASTransport;
  NGAP_FIND_PROTOCOLIE_BY_ID(NGAP_DownlinkNASTransport_IEs_t, ie, container,
                             NGAP_ProtocolIE_ID_id_AMF_UE_NGAP_ID, true);
  asn_INTEGER2ulong(&(ie->value.choice.AMF_UE_NGAP_ID), &amf_ue_ngap_id);


  NGAP_FIND_PROTOCOLIE_BY_ID(NGAP_DownlinkNASTransport_IEs_t, ie, container,
                             NGAP_ProtocolIE_ID_id_RAN_UE_NGAP_ID, true);
  gnb_ue_ngap_id = ie->value.choice.RAN_UE_NGAP_ID;

  if ((ue_desc_p = ngap_gNB_get_ue_context(ngap_gNB_instance,
                   gnb_ue_ngap_id)) == NULL) {
    MSC_LOG_RX_DISCARDED_MESSAGE(
zhenghuangkun's avatar
zhenghuangkun committed
375
        MSC_NGAP_GNB,
zhenghuangkun's avatar
zhenghuangkun committed
376
        MSC_NGAP_AMF,
zhenghuangkun's avatar
zhenghuangkun committed
377 378
        NULL,
        0,
zhenghuangkun's avatar
zhenghuangkun committed
379
        MSC_AS_TIME_FMT" downlinkNASTransport  gNB_ue_ngap_id %u amf_ue_ngap_id %u",
380
        gnb_ue_ngap_id,
zhenghuangkun's avatar
zhenghuangkun committed
381
        amf_ue_ngap_id);
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
    NGAP_ERROR("[SCTP %d] Received NAS downlink message for non existing UE context gNB_UE_NGAP_ID: 0x%lx\n",
               assoc_id,
               gnb_ue_ngap_id);
    return -1;
  }

  if (0 == ue_desc_p->rx_stream) {
    ue_desc_p->rx_stream = stream;
  } else if (stream != ue_desc_p->rx_stream) {
    NGAP_ERROR("[SCTP %d] Received UE-related procedure on stream %u, expecting %u\n",
               assoc_id, stream, ue_desc_p->rx_stream);
    return -1;
  }

  /* Is it the first outcome of the AMF for this UE ? If so store the amf
   * UE ngap id.
   */
  if (ue_desc_p->amf_ue_ngap_id == 0) {
    ue_desc_p->amf_ue_ngap_id = amf_ue_ngap_id;
  } else {
    /* We already have a amf ue ngap id check the received is the same */
    if (ue_desc_p->amf_ue_ngap_id != amf_ue_ngap_id) {
        NGAP_ERROR("[SCTP %d] Mismatch in AMF UE NGAP ID (0x%lx != 0x%"PRIx64"\n",
                   assoc_id,
                   amf_ue_ngap_id,
                   (uint64_t)ue_desc_p->amf_ue_ngap_id
                  );
        return -1;
    }
  }

  MSC_LOG_RX_MESSAGE(
      MSC_NGAP_GNB,
      MSC_NGAP_AMF,
      NULL,
      0,
      MSC_AS_TIME_FMT" downlinkNASTransport  gNB_ue_ngap_id %u amf_ue_ngap_id %u",
      assoc_id,
      amf_ue_ngap_id);

  NGAP_FIND_PROTOCOLIE_BY_ID(NGAP_DownlinkNASTransport_IEs_t, ie, container,
                             NGAP_ProtocolIE_ID_id_NAS_PDU, true);
  /* Forward the NAS PDU to NR-RRC */
  ngap_gNB_itti_send_nas_downlink_ind(ngap_gNB_instance->instance,
                                      ue_desc_p->ue_initial_id,
                                      ue_desc_p->gNB_ue_ngap_id,
                                      ie->value.choice.NAS_PDU.buf,
                                      ie->value.choice.NAS_PDU.size);
zhenghuangkun's avatar
zhenghuangkun committed
430

431
  return 0;
zhenghuangkun's avatar
zhenghuangkun committed
432 433 434 435 436 437
}

//------------------------------------------------------------------------------
int ngap_gNB_nas_uplink(instance_t instance, ngap_uplink_nas_t *ngap_uplink_nas_p)
//------------------------------------------------------------------------------
{
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
  struct ngap_gNB_ue_context_s  *ue_context_p;
  ngap_gNB_instance_t           *ngap_gNB_instance_p;
  NGAP_NGAP_PDU_t                pdu;
  NGAP_UplinkNASTransport_t     *out;
  NGAP_UplinkNASTransport_IEs_t *ie;
  NGAP_UserLocationInformationNR_t *userinfo_nr_p = NULL;
  uint8_t  *buffer;
  uint32_t  length;
  DevAssert(ngap_uplink_nas_p != NULL);
  /* Retrieve the NGAP gNB instance associated with Mod_id */
  ngap_gNB_instance_p = ngap_gNB_get_instance(instance);
  DevAssert(ngap_gNB_instance_p != NULL);

  if ((ue_context_p = ngap_gNB_get_ue_context(ngap_gNB_instance_p, ngap_uplink_nas_p->gNB_ue_ngap_id)) == NULL) {
    /* The context for this gNB ue ngap id doesn't exist in the map of gNB UEs */
    NGAP_WARN("Failed to find ue context associated with gNB ue ngap id: %08x\n",
              ngap_uplink_nas_p->gNB_ue_ngap_id);
    return -1;
  }

  /* Uplink NAS transport can occur either during an ngap connected state
   * or during initial attach (for example: NAS authentication).
   */
  if (!(ue_context_p->ue_state == NGAP_UE_CONNECTED ||
          ue_context_p->ue_state == NGAP_UE_WAITING_CSR)) {
    NGAP_WARN("You are attempting to send NAS data over non-connected "
              "gNB ue ngap id: %u, current state: %d\n",
              ngap_uplink_nas_p->gNB_ue_ngap_id, ue_context_p->ue_state);
    return -1;
  }

  /* Prepare the NGAP message to encode */
  memset(&pdu, 0, sizeof(pdu));
  pdu.present = NGAP_NGAP_PDU_PR_initiatingMessage;
472
  pdu.choice.initiatingMessage = (NGAP_InitiatingMessage_t *)calloc(1,sizeof(NGAP_InitiatingMessage_t));
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
  pdu.choice.initiatingMessage->procedureCode = NGAP_ProcedureCode_id_UplinkNASTransport;
  pdu.choice.initiatingMessage->criticality = NGAP_Criticality_ignore;
  pdu.choice.initiatingMessage->value.present = NGAP_InitiatingMessage__value_PR_UplinkNASTransport;
  out = &pdu.choice.initiatingMessage->value.choice.UplinkNASTransport;
  /* mandatory */
  ie = (NGAP_UplinkNASTransport_IEs_t *)calloc(1, sizeof(NGAP_UplinkNASTransport_IEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_AMF_UE_NGAP_ID;
  ie->criticality = NGAP_Criticality_reject;
  ie->value.present = NGAP_UplinkNASTransport_IEs__value_PR_AMF_UE_NGAP_ID;
  //ie->value.choice.AMF_UE_NGAP_ID = ue_context_p->amf_ue_ngap_id;
  asn_uint642INTEGER(&ie->value.choice.AMF_UE_NGAP_ID, ue_context_p->amf_ue_ngap_id);
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  /* mandatory */
  ie = (NGAP_UplinkNASTransport_IEs_t *)calloc(1, sizeof(NGAP_UplinkNASTransport_IEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_RAN_UE_NGAP_ID;
  ie->criticality = NGAP_Criticality_reject;
  ie->value.present = NGAP_UplinkNASTransport_IEs__value_PR_RAN_UE_NGAP_ID;
  ie->value.choice.RAN_UE_NGAP_ID = ue_context_p->gNB_ue_ngap_id;
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  /* mandatory */
  ie = (NGAP_UplinkNASTransport_IEs_t *)calloc(1, sizeof(NGAP_UplinkNASTransport_IEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_NAS_PDU;
  ie->criticality = NGAP_Criticality_reject;
  ie->value.present = NGAP_UplinkNASTransport_IEs__value_PR_NAS_PDU;
  ie->value.choice.NAS_PDU.buf = ngap_uplink_nas_p->nas_pdu.buffer;
  ie->value.choice.NAS_PDU.size = ngap_uplink_nas_p->nas_pdu.length;
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  /* mandatory */
  ie = (NGAP_UplinkNASTransport_IEs_t *)calloc(1, sizeof(NGAP_UplinkNASTransport_IEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_UserLocationInformation;
  ie->criticality = NGAP_Criticality_ignore;
  ie->value.present = NGAP_UplinkNASTransport_IEs__value_PR_UserLocationInformation;

  ie->value.choice.UserLocationInformation.present = NGAP_UserLocationInformation_PR_userLocationInformationNR;
507
  ie->value.choice.UserLocationInformation.choice.userLocationInformationNR = CALLOC(1,sizeof(struct NGAP_UserLocationInformationNR));
508 509 510 511 512 513 514 515 516 517 518 519 520 521
  userinfo_nr_p = ie->value.choice.UserLocationInformation.choice.userLocationInformationNR;

  /* Set nRCellIdentity. default userLocationInformationNR */
  MACRO_GNB_ID_TO_CELL_IDENTITY(ngap_gNB_instance_p->gNB_id,
                                    0, // Cell ID
                                    &userinfo_nr_p->nR_CGI.nRCellIdentity);
  MCC_MNC_TO_TBCD(ngap_gNB_instance_p->mcc[ue_context_p->selected_plmn_identity],
                  ngap_gNB_instance_p->mnc[ue_context_p->selected_plmn_identity],
                  ngap_gNB_instance_p->mnc_digit_length[ue_context_p->selected_plmn_identity],
                  &userinfo_nr_p->nR_CGI.pLMNIdentity);

  /* Set TAI */
  INT24_TO_OCTET_STRING(ngap_gNB_instance_p->tac, &userinfo_nr_p->tAI.tAC);
  MCC_MNC_TO_PLMNID(ngap_gNB_instance_p->mcc[ue_context_p->selected_plmn_identity],
522 523
                    ngap_gNB_instance_p->mnc[ue_context_p->selected_plmn_identity],
                    ngap_gNB_instance_p->mnc_digit_length[ue_context_p->selected_plmn_identity],
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
                    &userinfo_nr_p->tAI.pLMNIdentity);


  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);

  if (ngap_gNB_encode_pdu(&pdu, &buffer, &length) < 0) {
    NGAP_ERROR("Failed to encode uplink NAS transport\n");
    /* Encode procedure has failed... */
    return -1;
  }

  MSC_LOG_TX_MESSAGE(
      MSC_NGAP_GNB,
      MSC_NGAP_AMF,
      (const char *)NULL,
      0,
      MSC_AS_TIME_FMT" uplinkNASTransport initiatingMessage gNB_ue_ngap_id %u amf_ue_ngap_id %u",
      0,0,//MSC_AS_TIME_ARGS(ctxt_pP),
      ue_context_p->gNB_ue_ngap_id,
      ue_context_p->amf_ue_ngap_id);
  /* UE associated signalling -> use the allocated stream */
  ngap_gNB_itti_send_sctp_data_req(ngap_gNB_instance_p->instance,
                                   ue_context_p->amf_ref->assoc_id, buffer,
                                   length, ue_context_p->tx_stream);
548

549
  return 0;
zhenghuangkun's avatar
zhenghuangkun committed
550 551 552 553 554 555 556 557
}


//------------------------------------------------------------------------------
int ngap_gNB_nas_non_delivery_ind(instance_t instance,
                                  ngap_nas_non_delivery_ind_t *ngap_nas_non_delivery_ind)
//------------------------------------------------------------------------------
{
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
  struct ngap_gNB_ue_context_s        *ue_context_p;
  ngap_gNB_instance_t                 *ngap_gNB_instance_p;
  NGAP_NGAP_PDU_t                      pdu;
  NGAP_NASNonDeliveryIndication_t     *out;
  NGAP_NASNonDeliveryIndication_IEs_t *ie;
  uint8_t  *buffer;
  uint32_t  length;
  DevAssert(ngap_nas_non_delivery_ind != NULL);
  /* Retrieve the NGAP gNB instance associated with Mod_id */
  ngap_gNB_instance_p = ngap_gNB_get_instance(instance);
  DevAssert(ngap_gNB_instance_p != NULL);

  if ((ue_context_p = ngap_gNB_get_ue_context(ngap_gNB_instance_p, ngap_nas_non_delivery_ind->gNB_ue_ngap_id)) == NULL) {
    /* The context for this gNB ue ngap id doesn't exist in the map of gNB UEs */
    NGAP_WARN("Failed to find ue context associated with gNB ue ngap id: %08x\n",
              ngap_nas_non_delivery_ind->gNB_ue_ngap_id);
    MSC_LOG_EVENT(
zhenghuangkun's avatar
zhenghuangkun committed
575
        MSC_NGAP_GNB,
576 577 578 579 580 581 582 583
        MSC_AS_TIME_FMT" Sent of NAS_NON_DELIVERY_IND to AMF failed, no context for gNB_ue_ngap_id %08x",
        ngap_nas_non_delivery_ind->gNB_ue_ngap_id);
    return -1;
  }

  /* Prepare the NGAP message to encode */
  memset(&pdu, 0, sizeof(pdu));
  pdu.present = NGAP_NGAP_PDU_PR_initiatingMessage;
wujing's avatar
wujing committed
584
  pdu.choice.initiatingMessage = (NGAP_InitiatingMessage_t *)calloc(1,sizeof(NGAP_InitiatingMessage_t));
585 586 587 588 589 590 591 592 593 594 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
  pdu.choice.initiatingMessage->procedureCode = NGAP_ProcedureCode_id_NASNonDeliveryIndication;
  pdu.choice.initiatingMessage->criticality = NGAP_Criticality_ignore;
  pdu.choice.initiatingMessage->value.present = NGAP_InitiatingMessage__value_PR_NASNonDeliveryIndication;
  out = &pdu.choice.initiatingMessage->value.choice.NASNonDeliveryIndication;
  /* mandatory */
  ie = (NGAP_NASNonDeliveryIndication_IEs_t *)calloc(1, sizeof(NGAP_NASNonDeliveryIndication_IEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_AMF_UE_NGAP_ID;
  ie->criticality = NGAP_Criticality_reject;
  ie->value.present = NGAP_NASNonDeliveryIndication_IEs__value_PR_AMF_UE_NGAP_ID;
  asn_uint642INTEGER(&ie->value.choice.AMF_UE_NGAP_ID, ue_context_p->amf_ue_ngap_id);
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  /* mandatory */
  ie = (NGAP_NASNonDeliveryIndication_IEs_t *)calloc(1, sizeof(NGAP_NASNonDeliveryIndication_IEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_RAN_UE_NGAP_ID;
  ie->criticality = NGAP_Criticality_reject;
  ie->value.present = NGAP_NASNonDeliveryIndication_IEs__value_PR_RAN_UE_NGAP_ID;
  ie->value.choice.RAN_UE_NGAP_ID = ue_context_p->gNB_ue_ngap_id;
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  /* mandatory */
  ie = (NGAP_NASNonDeliveryIndication_IEs_t *)calloc(1, sizeof(NGAP_NASNonDeliveryIndication_IEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_NAS_PDU;
  ie->criticality = NGAP_Criticality_ignore;
  ie->value.present = NGAP_NASNonDeliveryIndication_IEs__value_PR_NAS_PDU;
  ie->value.choice.NAS_PDU.buf = ngap_nas_non_delivery_ind->nas_pdu.buffer;
  ie->value.choice.NAS_PDU.size = ngap_nas_non_delivery_ind->nas_pdu.length;
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  /* mandatory */
  ie = (NGAP_NASNonDeliveryIndication_IEs_t *)calloc(1, sizeof(NGAP_NASNonDeliveryIndication_IEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_Cause;
  ie->criticality = NGAP_Criticality_ignore;
  /* Send a dummy cause */
  ie->value.present = NGAP_NASNonDeliveryIndication_IEs__value_PR_Cause;
  ie->value.choice.Cause.present = NGAP_Cause_PR_radioNetwork;
  ie->value.choice.Cause.choice.radioNetwork = NGAP_CauseRadioNetwork_radio_connection_with_ue_lost;
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);

  if (ngap_gNB_encode_pdu(&pdu, &buffer, &length) < 0) {
    NGAP_ERROR("Failed to encode NAS NON delivery indication\n");
    /* Encode procedure has failed... */
    MSC_LOG_EVENT(
        MSC_NGAP_GNB,
        MSC_AS_TIME_FMT" Sent of NAS_NON_DELIVERY_IND to AMF failed (encoding)");
    return -1;
  }

  MSC_LOG_TX_MESSAGE(
      MSC_NGAP_GNB,
      MSC_NGAP_AMF,
      (const char *)buffer,
      length,
      MSC_AS_TIME_FMT" NASNonDeliveryIndication initiatingMessage gNB_ue_ngap_id %u amf_ue_ngap_id %lu",
      0,0,//MSC_AS_TIME_ARGS(ctxt_pP),
      ue_context_p->gNB_ue_ngap_id,
      ue_context_p->amf_ue_ngap_id);
  /* UE associated signalling -> use the allocated stream */
  ngap_gNB_itti_send_sctp_data_req(ngap_gNB_instance_p->instance,
                                   ue_context_p->amf_ref->assoc_id, buffer,
                                   length, ue_context_p->tx_stream);

  return 0;
zhenghuangkun's avatar
zhenghuangkun committed
645 646 647 648 649 650 651
}

//------------------------------------------------------------------------------
int ngap_gNB_initial_ctxt_resp(
    instance_t instance, ngap_initial_context_setup_resp_t *initial_ctxt_resp_p)
//------------------------------------------------------------------------------
{
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
  ngap_gNB_instance_t                   *ngap_gNB_instance_p = NULL;
  struct ngap_gNB_ue_context_s          *ue_context_p        = NULL;
  NGAP_NGAP_PDU_t                        pdu;
  NGAP_InitialContextSetupResponse_t    *out;
  NGAP_InitialContextSetupResponseIEs_t *ie;
  uint8_t  *buffer = NULL;
  uint32_t length;
  int      i;
  asn_encode_to_new_buffer_result_t res = { NULL, {0, NULL, NULL} };

  
  /* Retrieve the NGAP gNB instance associated with Mod_id */
  ngap_gNB_instance_p = ngap_gNB_get_instance(instance);
  DevAssert(initial_ctxt_resp_p != NULL);
  DevAssert(ngap_gNB_instance_p != NULL);

  if ((ue_context_p = ngap_gNB_get_ue_context(ngap_gNB_instance_p,
                      initial_ctxt_resp_p->gNB_ue_ngap_id)) == NULL) {
    /* The context for this gNB ue ngap id doesn't exist in the map of gNB UEs */
    NGAP_WARN("Failed to find ue context associated with gNB ue ngap id: 0x%08x\n",
              initial_ctxt_resp_p->gNB_ue_ngap_id);
    return -1;
  }

  /* Uplink NAS transport can occur either during an ngap connected state
   * or during initial attach (for example: NAS authentication).
   */
  if (!(ue_context_p->ue_state == NGAP_UE_CONNECTED ||
          ue_context_p->ue_state == NGAP_UE_WAITING_CSR)) {
    NGAP_WARN("You are attempting to send NAS data over non-connected "
              "gNB ue ngap id: %08x, current state: %d\n",
              initial_ctxt_resp_p->gNB_ue_ngap_id, ue_context_p->ue_state);
    return -1;
  }

  /* Prepare the NGAP message to encode */
  memset(&pdu, 0, sizeof(pdu));
  pdu.present = NGAP_NGAP_PDU_PR_successfulOutcome;
691
  pdu.choice.successfulOutcome = (NGAP_SuccessfulOutcome_t *)calloc(1,sizeof(struct NGAP_SuccessfulOutcome));
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
  pdu.choice.successfulOutcome->procedureCode = NGAP_ProcedureCode_id_InitialContextSetup;
  pdu.choice.successfulOutcome->criticality = NGAP_Criticality_reject;
  pdu.choice.successfulOutcome->value.present = NGAP_SuccessfulOutcome__value_PR_InitialContextSetupResponse;
  out = &pdu.choice.successfulOutcome->value.choice.InitialContextSetupResponse;
  /* mandatory */
  ie = (NGAP_InitialContextSetupResponseIEs_t *)calloc(1, sizeof(NGAP_InitialContextSetupResponseIEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_AMF_UE_NGAP_ID;
  ie->criticality = NGAP_Criticality_ignore;
  ie->value.present = NGAP_InitialContextSetupResponseIEs__value_PR_AMF_UE_NGAP_ID;
  //ie->value.choice.AMF_UE_NGAP_ID = ue_context_p->amf_ue_ngap_id;
  asn_uint642INTEGER(&ie->value.choice.AMF_UE_NGAP_ID, ue_context_p->amf_ue_ngap_id);
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  /* mandatory */
  ie = (NGAP_InitialContextSetupResponseIEs_t *)calloc(1, sizeof(NGAP_InitialContextSetupResponseIEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_RAN_UE_NGAP_ID;
  ie->criticality = NGAP_Criticality_ignore;
  ie->value.present = NGAP_InitialContextSetupResponseIEs__value_PR_RAN_UE_NGAP_ID;
  ie->value.choice.RAN_UE_NGAP_ID = initial_ctxt_resp_p->gNB_ue_ngap_id;
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  /* optional */
  ie = (NGAP_InitialContextSetupResponseIEs_t *)calloc(1, sizeof(NGAP_InitialContextSetupResponseIEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_PDUSessionResourceSetupListCxtRes;
  ie->criticality = NGAP_Criticality_ignore;
  ie->value.present = NGAP_InitialContextSetupResponseIEs__value_PR_PDUSessionResourceSetupListCxtRes;

717 718 719 720
  if (initial_ctxt_resp_p->nb_of_pdusessions){
    for (i = 0; i < initial_ctxt_resp_p->nb_of_pdusessions; i++) {
      NGAP_PDUSessionResourceSetupItemCxtRes_t *item;
      NGAP_PDUSessionResourceSetupResponseTransfer_t     *pdusessionTransfer_p = NULL;
721

722 723
      /* mandatory */
      item = (NGAP_PDUSessionResourceSetupItemCxtRes_t *)calloc(1, sizeof(NGAP_PDUSessionResourceSetupItemCxtRes_t));
724

725 726
      /* pDUSessionID */
      item->pDUSessionID = initial_ctxt_resp_p->pdusessions[i].pdusession_id;
727

728 729 730 731
      /* dLQosFlowPerTNLInformation */
      pdusessionTransfer_p = (NGAP_PDUSessionResourceSetupResponseTransfer_t *)calloc(1, sizeof(NGAP_PDUSessionResourceSetupResponseTransfer_t));

      pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.present = NGAP_UPTransportLayerInformation_PR_gTPTunnel;
732
     
733 734 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
      GTP_TEID_TO_ASN1(initial_ctxt_resp_p->pdusessions[i].gtp_teid, &pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel->gTP_TEID);

      pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel->transportLayerAddress.buf = malloc(initial_ctxt_resp_p->pdusessions[i].gNB_addr.length);

      memcpy(pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel->transportLayerAddress.buf,
             initial_ctxt_resp_p->pdusessions[i].gNB_addr.buffer,
             initial_ctxt_resp_p->pdusessions[i].gNB_addr.length);
      pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel->transportLayerAddress.size = initial_ctxt_resp_p->pdusessions[i].gNB_addr.length;
      pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel->transportLayerAddress.bits_unused = 0;

      NGAP_DEBUG("initial_ctxt_resp_p: pdusession ID %ld, gnb_addr %d.%d.%d.%d, SIZE %ld \n",
                 item->pDUSessionID,
                 pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel->transportLayerAddress.buf[0],
                 pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel->transportLayerAddress.buf[1],
                 pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel->transportLayerAddress.buf[2],
                 pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel->transportLayerAddress.buf[3],
                 pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel->transportLayerAddress.size);

      /* associatedQosFlowList. number of 1? */
      for(int j=0; j < initial_ctxt_resp_p->pdusessions[i].nb_of_qos_flow; j++) {
        NGAP_AssociatedQosFlowItem_t *ass_qos_item_p;
        ass_qos_item_p = (NGAP_AssociatedQosFlowItem_t *)calloc(1, sizeof(NGAP_AssociatedQosFlowItem_t));

        /* qosFlowIdentifier */
        ass_qos_item_p->qosFlowIdentifier = initial_ctxt_resp_p->pdusessions[i].associated_qos_flows[j].qfi;

        /* qosFlowMappingIndication */
        if(initial_ctxt_resp_p->pdusessions[i].associated_qos_flows[j].qos_flow_mapping_ind != QOSFLOW_MAPPING_INDICATION_NON) {
          ass_qos_item_p->qosFlowMappingIndication = malloc(sizeof(*ass_qos_item_p->qosFlowMappingIndication));
          *ass_qos_item_p->qosFlowMappingIndication = initial_ctxt_resp_p->pdusessions[i].associated_qos_flows[j].qos_flow_mapping_ind;
        }
        ASN_SEQUENCE_ADD(&pdusessionTransfer_p->dLQosFlowPerTNLInformation.associatedQosFlowList.list, ass_qos_item_p);
765 766
      }
               
767 768 769 770
      memset(&res, 0, sizeof(res));
      res = asn_encode_to_new_buffer(NULL, ATS_ALIGNED_CANONICAL_PER, &asn_DEF_NGAP_PDUSessionResourceSetupResponseTransfer, pdusessionTransfer_p);
      item->pDUSessionResourceSetupResponseTransfer.buf = res.buffer;
      item->pDUSessionResourceSetupResponseTransfer.size = res.result.encoded;
771

772
      ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_NGAP_PDUSessionResourceSetupResponseTransfer, pdusessionTransfer_p);
773
    
774
      ASN_SEQUENCE_ADD(&ie->value.choice.PDUSessionResourceSetupListCxtRes.list, item);
zhenghuangkun's avatar
zhenghuangkun committed
775 776 777
    }

    ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
778
  }
779 780
  /* optional */
  if (initial_ctxt_resp_p->nb_of_pdusessions_failed) {
zhenghuangkun's avatar
zhenghuangkun committed
781
    ie = (NGAP_InitialContextSetupResponseIEs_t *)calloc(1, sizeof(NGAP_InitialContextSetupResponseIEs_t));
782
    ie->id = NGAP_ProtocolIE_ID_id_PDUSessionResourceFailedToSetupListCxtRes;
zhenghuangkun's avatar
zhenghuangkun committed
783
    ie->criticality = NGAP_Criticality_ignore;
784
    ie->value.present = NGAP_InitialContextSetupResponseIEs__value_PR_PDUSessionResourceFailedToSetupListCxtRes;
zhenghuangkun's avatar
zhenghuangkun committed
785

786 787 788 789
    for (i = 0; i < initial_ctxt_resp_p->nb_of_pdusessions_failed; i++) {
        NGAP_PDUSessionResourceFailedToSetupItemCxtRes_t *item;
        NGAP_PDUSessionResourceSetupUnsuccessfulTransfer_t *pdusessionUnTransfer_p = NULL;
    
zhenghuangkun's avatar
zhenghuangkun committed
790
        /* mandatory */
791
        item = (NGAP_PDUSessionResourceFailedToSetupItemCxtRes_t *)calloc(1, sizeof(NGAP_PDUSessionResourceFailedToSetupItemCxtRes_t));
792
        /* pDUSessionID */
793 794 795 796 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
        item->pDUSessionID = initial_ctxt_resp_p->pdusessions_failed[i].pdusession_id;

        /* cause */
        pdusessionUnTransfer_p = (NGAP_PDUSessionResourceSetupUnsuccessfulTransfer_t *)calloc(1, sizeof(NGAP_PDUSessionResourceSetupUnsuccessfulTransfer_t));
        pdusessionUnTransfer_p->cause.present = initial_ctxt_resp_p->pdusessions_failed[i].cause;
        switch(pdusessionUnTransfer_p->cause.present) {
          case NGAP_Cause_PR_radioNetwork:
              pdusessionUnTransfer_p->cause.choice.radioNetwork = initial_ctxt_resp_p->pdusessions_failed[i].cause_value;
              break;

          case NGAP_Cause_PR_transport:
              pdusessionUnTransfer_p->cause.choice.transport = initial_ctxt_resp_p->pdusessions_failed[i].cause_value;
              break;

          case NGAP_Cause_PR_nas:
              pdusessionUnTransfer_p->cause.choice.nas = initial_ctxt_resp_p->pdusessions_failed[i].cause_value;
              break;

          case NGAP_Cause_PR_protocol:
              pdusessionUnTransfer_p->cause.choice.protocol = initial_ctxt_resp_p->pdusessions_failed[i].cause_value;
              break;

          case NGAP_Cause_PR_misc:
              pdusessionUnTransfer_p->cause.choice.misc = initial_ctxt_resp_p->pdusessions_failed[i].cause_value;
              break;

          case NGAP_Cause_PR_NOTHING:
          default:
              break;
822
        }
zhenghuangkun's avatar
zhenghuangkun committed
823

824
        NGAP_DEBUG("initial context setup response: failed pdusession ID %ld\n", item->pDUSessionID);
zhenghuangkun's avatar
zhenghuangkun committed
825

826 827 828 829
        memset(&res, 0, sizeof(res));
        res = asn_encode_to_new_buffer(NULL, ATS_ALIGNED_CANONICAL_PER, &asn_DEF_NGAP_PDUSessionResourceSetupUnsuccessfulTransfer, pdusessionUnTransfer_p);
        item->pDUSessionResourceSetupUnsuccessfulTransfer.buf = res.buffer;
        item->pDUSessionResourceSetupUnsuccessfulTransfer.size = res.result.encoded;
zhenghuangkun's avatar
zhenghuangkun committed
830

831 832 833
        ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_NGAP_PDUSessionResourceSetupUnsuccessfulTransfer, pdusessionUnTransfer_p);
    
        ASN_SEQUENCE_ADD(&ie->value.choice.PDUSessionResourceFailedToSetupListCxtRes.list, item);
zhenghuangkun's avatar
zhenghuangkun committed
834 835
    }

836 837
    ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  }
zhenghuangkun's avatar
zhenghuangkun committed
838

839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
  /* optional */
  if (0) {
    ie = (NGAP_InitialContextSetupResponseIEs_t *)calloc(1, sizeof(NGAP_InitialContextSetupResponseIEs_t));
    ie->id = NGAP_ProtocolIE_ID_id_CriticalityDiagnostics;
    ie->criticality = NGAP_Criticality_ignore;
    ie->value.present = NGAP_InitialContextSetupResponseIEs__value_PR_CriticalityDiagnostics;
    // ie->value.choice.CriticalityDiagnostics =;
    ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  }

  if (ngap_gNB_encode_pdu(&pdu, &buffer, &length) < 0) {
    NGAP_ERROR("Failed to encode InitialContextSetupResponse\n");
    /* Encode procedure has failed... */
    return -1;
  }

zhenghuangkun's avatar
zhenghuangkun committed
855 856
    MSC_LOG_TX_MESSAGE(
        MSC_NGAP_GNB,
zhenghuangkun's avatar
zhenghuangkun committed
857
        MSC_NGAP_AMF,
zhenghuangkun's avatar
zhenghuangkun committed
858 859
        (const char *)buffer,
        length,
zhenghuangkun's avatar
zhenghuangkun committed
860
        MSC_AS_TIME_FMT" InitialContextSetup successfulOutcome gNB_ue_ngap_id %u amf_ue_ngap_id %u",
zhenghuangkun's avatar
zhenghuangkun committed
861 862
        0,0,//MSC_AS_TIME_ARGS(ctxt_pP),
        initial_ctxt_resp_p->gNB_ue_ngap_id,
zhenghuangkun's avatar
zhenghuangkun committed
863
        ue_context_p->amf_ue_ngap_id);
zhenghuangkun's avatar
zhenghuangkun committed
864
    /* UE associated signalling -> use the allocated stream */
865
    LOG_I(NR_RRC,"Send message to sctp: NGAP_InitialContextSetupResponse\n");
zhenghuangkun's avatar
zhenghuangkun committed
866
    ngap_gNB_itti_send_sctp_data_req(ngap_gNB_instance_p->instance,
zhenghuangkun's avatar
zhenghuangkun committed
867
                                     ue_context_p->amf_ref->assoc_id, buffer,
zhenghuangkun's avatar
zhenghuangkun committed
868
                                     length, ue_context_p->tx_stream);
869

zhenghuangkun's avatar
zhenghuangkun committed
870 871 872 873 874 875 876 877
    return 0;
}

//------------------------------------------------------------------------------
int ngap_gNB_ue_capabilities(instance_t instance,
                             ngap_ue_cap_info_ind_t *ue_cap_info_ind_p)
//------------------------------------------------------------------------------
{
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
  ngap_gNB_instance_t          *ngap_gNB_instance_p;
  struct ngap_gNB_ue_context_s *ue_context_p;
  NGAP_NGAP_PDU_t                       pdu;
  NGAP_UERadioCapabilityInfoIndication_t    *out;
  NGAP_UERadioCapabilityInfoIndicationIEs_t *ie;
  uint8_t  *buffer;
  uint32_t length;
  /* Retrieve the NGAP gNB instance associated with Mod_id */
  ngap_gNB_instance_p = ngap_gNB_get_instance(instance);
  DevAssert(ue_cap_info_ind_p != NULL);
  DevAssert(ngap_gNB_instance_p != NULL);

  if ((ue_context_p = ngap_gNB_get_ue_context(ngap_gNB_instance_p,
                      ue_cap_info_ind_p->gNB_ue_ngap_id)) == NULL) {
    /* The context for this gNB ue ngap id doesn't exist in the map of gNB UEs */
    NGAP_WARN("Failed to find ue context associated with gNB ue ngap id: %u\n",
              ue_cap_info_ind_p->gNB_ue_ngap_id);
    return -1;
  }

  /* UE radio capabilities message can occur either during an ngap connected state
   * or during initial attach (for example: NAS authentication).
   */
  if (!(ue_context_p->ue_state == NGAP_UE_CONNECTED ||
          ue_context_p->ue_state == NGAP_UE_WAITING_CSR)) {
    NGAP_WARN("You are attempting to send NAS data over non-connected "
              "gNB ue ngap id: %u, current state: %d\n",
              ue_cap_info_ind_p->gNB_ue_ngap_id, ue_context_p->ue_state);
    return -1;
  }

  /* Prepare the NGAP message to encode */
  memset(&pdu, 0, sizeof(pdu));
  pdu.present = NGAP_NGAP_PDU_PR_initiatingMessage;
912
  pdu.choice.initiatingMessage = (NGAP_InitiatingMessage_t *)calloc(1,sizeof(NGAP_InitiatingMessage_t));
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
  pdu.choice.initiatingMessage->procedureCode = NGAP_ProcedureCode_id_UERadioCapabilityInfoIndication;
  pdu.choice.initiatingMessage->criticality = NGAP_Criticality_ignore;
  pdu.choice.initiatingMessage->value.present = NGAP_InitiatingMessage__value_PR_UERadioCapabilityInfoIndication;
  out = &pdu.choice.initiatingMessage->value.choice.UERadioCapabilityInfoIndication;
  /* mandatory */
  ie = (NGAP_UERadioCapabilityInfoIndicationIEs_t *)calloc(1, sizeof(NGAP_UERadioCapabilityInfoIndicationIEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_AMF_UE_NGAP_ID;
  ie->criticality = NGAP_Criticality_reject;
  ie->value.present = NGAP_UERadioCapabilityInfoIndicationIEs__value_PR_AMF_UE_NGAP_ID;
  //ie->value.choice.AMF_UE_NGAP_ID = ue_context_p->amf_ue_ngap_id;
  asn_uint642INTEGER(&ie->value.choice.AMF_UE_NGAP_ID, ue_context_p->amf_ue_ngap_id);
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  /* mandatory */
  ie = (NGAP_UERadioCapabilityInfoIndicationIEs_t *)calloc(1, sizeof(NGAP_UERadioCapabilityInfoIndicationIEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_RAN_UE_NGAP_ID;
  ie->criticality = NGAP_Criticality_reject;
  ie->value.present = NGAP_UERadioCapabilityInfoIndicationIEs__value_PR_RAN_UE_NGAP_ID;
  ie->value.choice.RAN_UE_NGAP_ID = ue_cap_info_ind_p->gNB_ue_ngap_id;
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  /* mandatory */
  ie = (NGAP_UERadioCapabilityInfoIndicationIEs_t *)calloc(1, sizeof(NGAP_UERadioCapabilityInfoIndicationIEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_UERadioCapability;
  ie->criticality = NGAP_Criticality_reject;
  ie->value.present = NGAP_UERadioCapabilityInfoIndicationIEs__value_PR_UERadioCapability;
  ie->value.choice.UERadioCapability.buf = ue_cap_info_ind_p->ue_radio_cap.buffer;
  ie->value.choice.UERadioCapability.size = ue_cap_info_ind_p->ue_radio_cap.length;
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  /* optional */
  //NGAP_UERadioCapabilityForPaging TBD
  if (ngap_gNB_encode_pdu(&pdu, &buffer, &length) < 0) {
    /* Encode procedure has failed... */
    NGAP_ERROR("Failed to encode UE radio capabilities indication\n");
    return -1;
  }

  MSC_LOG_TX_MESSAGE(
      MSC_NGAP_GNB,
      MSC_NGAP_AMF,
      (const char *)buffer,
      length,
      MSC_AS_TIME_FMT" UECapabilityInfoIndication initiatingMessage gNB_ue_ngap_id %u amf_ue_ngap_id %u",
      0,0,//MSC_AS_TIME_ARGS(ctxt_pP),
      ue_cap_info_ind_p->gNB_ue_ngap_id,
      ue_context_p->amf_ue_ngap_id);
  /* UE associated signalling -> use the allocated stream */
  ngap_gNB_itti_send_sctp_data_req(ngap_gNB_instance_p->instance,
                                   ue_context_p->amf_ref->assoc_id, buffer,
                                   length, ue_context_p->tx_stream);
  return 0;
zhenghuangkun's avatar
zhenghuangkun committed
962 963 964
}

//------------------------------------------------------------------------------
zhenghuangkun's avatar
zhenghuangkun committed
965 966
int ngap_gNB_pdusession_setup_resp(instance_t instance,
                              ngap_pdusession_setup_resp_t *pdusession_setup_resp_p)
zhenghuangkun's avatar
zhenghuangkun committed
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 1002 1003 1004 1005
  ngap_gNB_instance_t          *ngap_gNB_instance_p = NULL;
  struct ngap_gNB_ue_context_s *ue_context_p        = NULL;
  NGAP_NGAP_PDU_t               pdu;
  NGAP_PDUSessionResourceSetupResponse_t    *out;
  NGAP_PDUSessionResourceSetupResponseIEs_t *ie;
  uint8_t  *buffer  = NULL;
  uint32_t length;
  int      i;
  asn_encode_to_new_buffer_result_t res = { NULL, {0, NULL, NULL} };

  /* Retrieve the NGAP gNB instance associated with Mod_id */
  ngap_gNB_instance_p = ngap_gNB_get_instance(instance);
  DevAssert(pdusession_setup_resp_p != NULL);
  DevAssert(ngap_gNB_instance_p != NULL);

  if ((ue_context_p = ngap_gNB_get_ue_context(ngap_gNB_instance_p,
                      pdusession_setup_resp_p->gNB_ue_ngap_id)) == NULL) {
    /* The context for this gNB ue ngap id doesn't exist in the map of gNB UEs */
    NGAP_WARN("Failed to find ue context associated with gNB ue ngap id: 0x%08x\n",
              pdusession_setup_resp_p->gNB_ue_ngap_id);
    return -1;
  }

  /* Uplink NAS transport can occur either during an ngap connected state
   * or during initial attach (for example: NAS authentication).
   */
  if (!(ue_context_p->ue_state == NGAP_UE_CONNECTED ||
          ue_context_p->ue_state == NGAP_UE_WAITING_CSR)) {
    NGAP_WARN("You are attempting to send NAS data over non-connected "
              "gNB ue ngap id: %08x, current state: %d\n",
              pdusession_setup_resp_p->gNB_ue_ngap_id, ue_context_p->ue_state);
    return -1;
  }

  /* Prepare the NGAP message to encode */
  memset(&pdu, 0, sizeof(pdu));
  pdu.present = NGAP_NGAP_PDU_PR_successfulOutcome;
1006
  pdu.choice.successfulOutcome = (NGAP_SuccessfulOutcome_t *)calloc(1,sizeof(struct NGAP_SuccessfulOutcome));
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
  pdu.choice.successfulOutcome->procedureCode = NGAP_ProcedureCode_id_PDUSessionResourceSetup;
  pdu.choice.successfulOutcome->criticality = NGAP_Criticality_reject;
  pdu.choice.successfulOutcome->value.present = NGAP_SuccessfulOutcome__value_PR_PDUSessionResourceSetupResponse;
  out = &pdu.choice.successfulOutcome->value.choice.PDUSessionResourceSetupResponse;
  /* mandatory */
  ie = (NGAP_PDUSessionResourceSetupResponseIEs_t *)calloc(1, sizeof(NGAP_PDUSessionResourceSetupResponseIEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_AMF_UE_NGAP_ID;
  ie->criticality = NGAP_Criticality_ignore;
  ie->value.present = NGAP_PDUSessionResourceSetupResponseIEs__value_PR_AMF_UE_NGAP_ID;
  asn_uint642INTEGER(&ie->value.choice.AMF_UE_NGAP_ID, ue_context_p->amf_ue_ngap_id);
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  /* mandatory */
  ie = (NGAP_PDUSessionResourceSetupResponseIEs_t *)calloc(1, sizeof(NGAP_PDUSessionResourceSetupResponseIEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_RAN_UE_NGAP_ID;
  ie->criticality = NGAP_Criticality_ignore;
  ie->value.present = NGAP_PDUSessionResourceSetupResponseIEs__value_PR_RAN_UE_NGAP_ID;
  ie->value.choice.RAN_UE_NGAP_ID = pdusession_setup_resp_p->gNB_ue_ngap_id;
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);

  /* optional */
  if (pdusession_setup_resp_p->nb_of_pdusessions > 0) {
yincong's avatar
yincong committed
1028
    ie = (NGAP_PDUSessionResourceSetupResponseIEs_t *)calloc(1, sizeof(NGAP_PDUSessionResourceSetupResponseIEs_t));
1029
    ie->id = NGAP_ProtocolIE_ID_id_PDUSessionResourceSetupListSURes;
zhenghuangkun's avatar
zhenghuangkun committed
1030
    ie->criticality = NGAP_Criticality_ignore;
1031
    ie->value.present = NGAP_PDUSessionResourceSetupResponseIEs__value_PR_PDUSessionResourceSetupListSURes;
zhenghuangkun's avatar
zhenghuangkun committed
1032

1033 1034 1035
    for (i = 0; i < pdusession_setup_resp_p->nb_of_pdusessions; i++) {
      NGAP_PDUSessionResourceSetupItemSURes_t *item;
      NGAP_PDUSessionResourceSetupResponseTransfer_t     *pdusessionTransfer_p = NULL;
zhenghuangkun's avatar
zhenghuangkun committed
1036

1037 1038
      /* mandatory */
      item = (NGAP_PDUSessionResourceSetupItemSURes_t *)calloc(1, sizeof(NGAP_PDUSessionResourceSetupItemSURes_t));
yincong's avatar
yincong committed
1039

1040 1041
      /* pDUSessionID */
      item->pDUSessionID = pdusession_setup_resp_p->pdusessions[i].pdusession_id;
yincong's avatar
yincong committed
1042

1043 1044
      /* dLQosFlowPerTNLInformation */
      pdusessionTransfer_p = (NGAP_PDUSessionResourceSetupResponseTransfer_t *)calloc(1, sizeof(NGAP_PDUSessionResourceSetupResponseTransfer_t));
yincong's avatar
yincong committed
1045

1046
      pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.present = NGAP_UPTransportLayerInformation_PR_gTPTunnel;
1047
      pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel =
1048
                calloc(1, sizeof(struct NGAP_GTPTunnel));
yincong's avatar
yincong committed
1049

1050
      GTP_TEID_TO_ASN1(pdusession_setup_resp_p->pdusessions[i].gtp_teid, &pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel->gTP_TEID);
yincong's avatar
yincong committed
1051

1052
      pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel->transportLayerAddress.buf = malloc(pdusession_setup_resp_p->pdusessions[i].gNB_addr.length);
zhenghuangkun's avatar
zhenghuangkun committed
1053

1054 1055 1056 1057 1058
      memcpy(pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel->transportLayerAddress.buf,
             pdusession_setup_resp_p->pdusessions[i].gNB_addr.buffer,
             pdusession_setup_resp_p->pdusessions[i].gNB_addr.length);
      pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel->transportLayerAddress.size = pdusession_setup_resp_p->pdusessions[i].gNB_addr.length;
      pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel->transportLayerAddress.bits_unused = 0;
yincong's avatar
yincong committed
1059

1060 1061 1062 1063 1064 1065 1066
      NGAP_DEBUG("pdusession_setup_resp_p: pdusession ID %ld, gnb_addr %d.%d.%d.%d, SIZE %ld \n",
                 item->pDUSessionID,
                 pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel->transportLayerAddress.buf[0],
                 pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel->transportLayerAddress.buf[1],
                 pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel->transportLayerAddress.buf[2],
                 pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel->transportLayerAddress.buf[3],
                 pdusessionTransfer_p->dLQosFlowPerTNLInformation.uPTransportLayerInformation.choice.gTPTunnel->transportLayerAddress.size);
yincong's avatar
yincong committed
1067

1068 1069 1070 1071
      /* associatedQosFlowList. number of 1? */
      for(int j=0; j < pdusession_setup_resp_p->pdusessions[i].nb_of_qos_flow; j++) {
        NGAP_AssociatedQosFlowItem_t *ass_qos_item_p;
        ass_qos_item_p = (NGAP_AssociatedQosFlowItem_t *)calloc(1, sizeof(NGAP_AssociatedQosFlowItem_t));
yincong's avatar
yincong committed
1072

1073
        /* qosFlowIdentifier */
1074
        ass_qos_item_p->qosFlowIdentifier = pdusession_setup_resp_p->pdusessions[i].associated_qos_flows[j].qfi;
yincong's avatar
yincong committed
1075

1076
        /* qosFlowMappingIndication */
1077 1078 1079 1080
        //if(pdusession_setup_resp_p->pdusessions[i].associated_qos_flows[j].qos_flow_mapping_ind != QOSFLOW_MAPPING_INDICATION_NON) {
        //  ass_qos_item_p->qosFlowMappingIndication = malloc(sizeof(*ass_qos_item_p->qosFlowMappingIndication));
        //  *ass_qos_item_p->qosFlowMappingIndication = pdusession_setup_resp_p->pdusessions[i].associated_qos_flows[j].qos_flow_mapping_ind;
        //}
1081 1082
        ASN_SEQUENCE_ADD(&pdusessionTransfer_p->dLQosFlowPerTNLInformation.associatedQosFlowList.list, ass_qos_item_p);
      }
yincong's avatar
yincong committed
1083

1084
      memset(&res, 0, sizeof(res));
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
//      res = asn_encode_to_new_buffer(NULL, ATS_ALIGNED_CANONICAL_PER, &asn_DEF_NGAP_PDUSessionResourceSetupResponseTransfer, pdusessionTransfer_p);
//      item->pDUSessionResourceSetupResponseTransfer.buf = res.buffer;
//      item->pDUSessionResourceSetupResponseTransfer.size = res.result.encoded;
      uint8_t buffer[100];
      asn_enc_rval_t  enc_rval = aper_encode_to_buffer(&asn_DEF_NGAP_PDUSessionResourceSetupResponseTransfer, 
                                  NULL, 
                                  pdusessionTransfer_p, 
                                  buffer,100);
      AssertFatal (enc_rval.encoded > 0, "ASN1 message encoding failed (%s, %lu)!\n", enc_rval.failed_type->name, enc_rval.encoded);
      item->pDUSessionResourceSetupResponseTransfer.buf = buffer;
      item->pDUSessionResourceSetupResponseTransfer.size = enc_rval.encoded;
yincong's avatar
yincong committed
1096

1097
      ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_NGAP_PDUSessionResourceSetupResponseTransfer, pdusessionTransfer_p);
yincong's avatar
yincong committed
1098

1099
      ASN_SEQUENCE_ADD(&ie->value.choice.PDUSessionResourceSetupListSURes.list, item);
zhenghuangkun's avatar
zhenghuangkun committed
1100
    }
1101 1102
    ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  }
zhenghuangkun's avatar
zhenghuangkun committed
1103

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
  /* optional */
  if (pdusession_setup_resp_p->nb_of_pdusessions_failed > 0) {
    ie = (NGAP_PDUSessionResourceSetupResponseIEs_t *)calloc(1, sizeof(NGAP_PDUSessionResourceSetupResponseIEs_t));
    ie->id = NGAP_ProtocolIE_ID_id_PDUSessionResourceFailedToSetupListSURes;
    ie->criticality = NGAP_Criticality_ignore;
    ie->value.present = NGAP_PDUSessionResourceSetupResponseIEs__value_PR_PDUSessionResourceFailedToSetupListSURes;

    for (i = 0; i < pdusession_setup_resp_p->nb_of_pdusessions_failed; i++) {
      NGAP_PDUSessionResourceFailedToSetupItemSURes_t *item;
      NGAP_PDUSessionResourceSetupUnsuccessfulTransfer_t *pdusessionUnTransfer_p = NULL;

      /* mandatory */
      item = (NGAP_PDUSessionResourceFailedToSetupItemSURes_t *)calloc(1, sizeof(NGAP_PDUSessionResourceFailedToSetupItemSURes_t));
      /* pDUSessionID */
      item->pDUSessionID = pdusession_setup_resp_p->pdusessions_failed[i].pdusession_id;

      /* cause */
      pdusessionUnTransfer_p = (NGAP_PDUSessionResourceSetupUnsuccessfulTransfer_t *)calloc(1, sizeof(NGAP_PDUSessionResourceSetupUnsuccessfulTransfer_t));
      pdusessionUnTransfer_p->cause.present = pdusession_setup_resp_p->pdusessions_failed[i].cause;
      switch(pdusessionUnTransfer_p->cause.present) {
        case NGAP_Cause_PR_radioNetwork:
            pdusessionUnTransfer_p->cause.choice.radioNetwork = pdusession_setup_resp_p->pdusessions_failed[i].cause_value;
            break;
zhenghuangkun's avatar
zhenghuangkun committed
1127

1128 1129 1130
        case NGAP_Cause_PR_transport:
            pdusessionUnTransfer_p->cause.choice.transport = pdusession_setup_resp_p->pdusessions_failed[i].cause_value;
            break;
zhenghuangkun's avatar
zhenghuangkun committed
1131

1132 1133 1134
        case NGAP_Cause_PR_nas:
            pdusessionUnTransfer_p->cause.choice.nas = pdusession_setup_resp_p->pdusessions_failed[i].cause_value;
            break;
zhenghuangkun's avatar
zhenghuangkun committed
1135

1136 1137 1138
        case NGAP_Cause_PR_protocol:
            pdusessionUnTransfer_p->cause.choice.protocol = pdusession_setup_resp_p->pdusessions_failed[i].cause_value;
            break;
zhenghuangkun's avatar
zhenghuangkun committed
1139

1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
        case NGAP_Cause_PR_misc:
            pdusessionUnTransfer_p->cause.choice.misc = pdusession_setup_resp_p->pdusessions_failed[i].cause_value;
            break;

        case NGAP_Cause_PR_NOTHING:
        default:
            break;
      }

      NGAP_DEBUG("pdusession setup response: failed pdusession ID %ld\n", item->pDUSessionID);

      memset(&res, 0, sizeof(res));
      res = asn_encode_to_new_buffer(NULL, ATS_ALIGNED_CANONICAL_PER, &asn_DEF_NGAP_PDUSessionResourceSetupUnsuccessfulTransfer, pdusessionUnTransfer_p);
      item->pDUSessionResourceSetupUnsuccessfulTransfer.buf = res.buffer;
      item->pDUSessionResourceSetupUnsuccessfulTransfer.size = res.result.encoded;

      ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_NGAP_PDUSessionResourceSetupUnsuccessfulTransfer, pdusessionUnTransfer_p);

      ASN_SEQUENCE_ADD(&ie->value.choice.PDUSessionResourceFailedToSetupListSURes.list, item);
zhenghuangkun's avatar
zhenghuangkun committed
1159 1160
    }

1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
    ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  }

  /* optional */
  if (0) {
      ie = (NGAP_PDUSessionResourceSetupResponseIEs_t *)calloc(1, sizeof(NGAP_PDUSessionResourceSetupResponseIEs_t));
      ie->id = NGAP_ProtocolIE_ID_id_CriticalityDiagnostics;
      ie->criticality = NGAP_Criticality_ignore;
      ie->value.present = NGAP_PDUSessionResourceSetupResponseIEs__value_PR_CriticalityDiagnostics;
      // ie->value.choice.CriticalityDiagnostics = ;
      ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  }


  if (ngap_gNB_encode_pdu(&pdu, &buffer, &length) < 0) {
      NGAP_ERROR("Failed to encode uplink transport\n");
      /* Encode procedure has failed... */
      return -1;
  }

  MSC_LOG_TX_MESSAGE(
      MSC_NGAP_GNB,
      MSC_NGAP_AMF,
      (const char *)buffer,
      length,
      MSC_AS_TIME_FMT" PduSession Setup successfulOutcome gNB_ue_ngap_id %u amf_ue_ngap_id %u",
      0,0,//MSC_AS_TIME_ARGS(ctxt_pP),
      pdusession_setup_resp_p->gNB_ue_ngap_id,
      ue_context_p->amf_ue_ngap_id);
  /* UE associated signalling -> use the allocated stream */
  ngap_gNB_itti_send_sctp_data_req(ngap_gNB_instance_p->instance,
                                   ue_context_p->amf_ref->assoc_id, buffer,
                                   length, ue_context_p->tx_stream);
  return 0;
zhenghuangkun's avatar
zhenghuangkun committed
1195 1196 1197
}

//------------------------------------------------------------------------------
zhenghuangkun's avatar
zhenghuangkun committed
1198 1199
int ngap_gNB_pdusession_modify_resp(instance_t instance,
                               ngap_pdusession_modify_resp_t *pdusession_modify_resp_p)
zhenghuangkun's avatar
zhenghuangkun committed
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
  ngap_gNB_instance_t                         *ngap_gNB_instance_p = NULL;
  struct ngap_gNB_ue_context_s                *ue_context_p        = NULL;
  NGAP_NGAP_PDU_t                             pdu;
  NGAP_PDUSessionResourceModifyResponse_t     *out;
  NGAP_PDUSessionResourceModifyResponseIEs_t  *ie;
  uint8_t  *buffer  = NULL;
  uint32_t length;
  int      i;
  asn_encode_to_new_buffer_result_t res = { NULL, {0, NULL, NULL} };
  
  /* Retrieve the NGAP gNB instance associated with Mod_id */
  ngap_gNB_instance_p = ngap_gNB_get_instance(instance);
  DevAssert(pdusession_modify_resp_p != NULL);
  DevAssert(ngap_gNB_instance_p != NULL);

  if ((ue_context_p = ngap_gNB_get_ue_context(ngap_gNB_instance_p,
                      pdusession_modify_resp_p->gNB_ue_ngap_id)) == NULL) {
    /* The context for this gNB ue ngap id doesn't exist in the map of gNB UEs */
    NGAP_WARN("Failed to find ue context associated with gNB ue ngap id: 0x%08x\n",
              pdusession_modify_resp_p->gNB_ue_ngap_id);
    return -1;
  }

  /* Uplink NAS transport can occur either during an ngap connected state
   * or during initial attach (for example: NAS authentication).
   */
  if (!(ue_context_p->ue_state == NGAP_UE_CONNECTED ||
          ue_context_p->ue_state == NGAP_UE_WAITING_CSR)) {
    NGAP_WARN("You are attempting to send NAS data over non-connected "
              "gNB ue ngap id: %08x, current state: %d\n",
              pdusession_modify_resp_p->gNB_ue_ngap_id, ue_context_p->ue_state);
    return -1;
  }

  /* Prepare the NGAP message to encode */
  memset(&pdu, 0, sizeof(pdu));
  pdu.present = NGAP_NGAP_PDU_PR_successfulOutcome;
1239
  pdu.choice.successfulOutcome = (NGAP_SuccessfulOutcome_t *)calloc(1,sizeof(struct NGAP_SuccessfulOutcome));
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
  pdu.choice.successfulOutcome->procedureCode = NGAP_ProcedureCode_id_PDUSessionResourceModify;
  pdu.choice.successfulOutcome->criticality = NGAP_Criticality_reject;
  pdu.choice.successfulOutcome->value.present = NGAP_SuccessfulOutcome__value_PR_PDUSessionResourceModifyResponse;
  out = &pdu.choice.successfulOutcome->value.choice.PDUSessionResourceModifyResponse;
  /* mandatory */
  ie = (NGAP_PDUSessionResourceModifyResponseIEs_t *)calloc(1, sizeof(NGAP_PDUSessionResourceModifyResponseIEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_AMF_UE_NGAP_ID;
  ie->criticality = NGAP_Criticality_ignore;
  ie->value.present = NGAP_PDUSessionResourceModifyResponseIEs__value_PR_AMF_UE_NGAP_ID;
  asn_uint642INTEGER(&ie->value.choice.AMF_UE_NGAP_ID, ue_context_p->amf_ue_ngap_id);
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  /* mandatory */
  ie = (NGAP_PDUSessionResourceModifyResponseIEs_t *)calloc(1, sizeof(NGAP_PDUSessionResourceModifyResponseIEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_RAN_UE_NGAP_ID;
  ie->criticality = NGAP_Criticality_ignore;
  ie->value.present = NGAP_PDUSessionResourceModifyResponseIEs__value_PR_RAN_UE_NGAP_ID;
  ie->value.choice.RAN_UE_NGAP_ID = pdusession_modify_resp_p->gNB_ue_ngap_id;
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);

  /* optional */
  if (pdusession_modify_resp_p->nb_of_pdusessions > 0) {
    ie = (NGAP_PDUSessionResourceModifyResponseIEs_t *)calloc(1, sizeof(NGAP_PDUSessionResourceModifyResponseIEs_t));
    ie->id = NGAP_ProtocolIE_ID_id_PDUSessionResourceModifyListModRes;
    ie->criticality = NGAP_Criticality_ignore;
    ie->value.present = NGAP_PDUSessionResourceModifyResponseIEs__value_PR_PDUSessionResourceModifyListModRes;
zhenghuangkun's avatar
zhenghuangkun committed
1265

1266 1267 1268 1269 1270 1271 1272 1273
    for (i = 0; i < pdusession_modify_resp_p->nb_of_pdusessions; i++) {
        NGAP_PDUSessionResourceModifyItemModRes_t *item;
    
        item = (NGAP_PDUSessionResourceModifyItemModRes_t *)calloc(1, sizeof(NGAP_PDUSessionResourceModifyItemModRes_t));
        item->pDUSessionID = pdusession_modify_resp_p->pdusessions[i].pdusession_id;
        
        NGAP_DEBUG("pdusession_modify_resp: modified pdusession ID %ld\n", item->pDUSessionID);
        ASN_SEQUENCE_ADD(&ie->value.choice.PDUSessionResourceModifyListModRes.list, item);
zhenghuangkun's avatar
zhenghuangkun committed
1274 1275 1276
    }

    ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
1277 1278 1279 1280
  }

  /* optional */
  if (pdusession_modify_resp_p->nb_of_pdusessions_failed > 0) {
1281
    ie = (NGAP_PDUSessionResourceModifyResponseIEs_t *)calloc(1, sizeof(NGAP_PDUSessionResourceModifyResponseIEs_t));
1282
    ie->id = NGAP_ProtocolIE_ID_id_PDUSessionResourceFailedToModifyListModRes;
zhenghuangkun's avatar
zhenghuangkun committed
1283
    ie->criticality = NGAP_Criticality_ignore;
1284
    ie->value.present = NGAP_PDUSessionResourceModifyResponseIEs__value_PR_PDUSessionResourceFailedToModifyListModRes;
zhenghuangkun's avatar
zhenghuangkun committed
1285

1286 1287 1288 1289 1290 1291
    for (i = 0; i < pdusession_modify_resp_p->nb_of_pdusessions_failed; i++) {
      NGAP_PDUSessionResourceFailedToModifyItemModRes_t *item;
      NGAP_PDUSessionResourceModifyUnsuccessfulTransfer_t *pdusessionTransfer_p = NULL;
  
      item = (NGAP_PDUSessionResourceFailedToModifyItemModRes_t *)calloc(1, sizeof(NGAP_PDUSessionResourceFailedToModifyItemModRes_t));
      item->pDUSessionID = pdusession_modify_resp_p->pdusessions_failed[i].pdusession_id;
zhenghuangkun's avatar
zhenghuangkun committed
1292

1293 1294 1295 1296 1297 1298 1299
      pdusessionTransfer_p = (NGAP_PDUSessionResourceModifyUnsuccessfulTransfer_t *)calloc(1, sizeof(NGAP_PDUSessionResourceModifyUnsuccessfulTransfer_t));
      pdusessionTransfer_p->cause.present = pdusession_modify_resp_p->pdusessions_failed[i].cause;
      
      switch(pdusessionTransfer_p->cause.present) {
      case NGAP_Cause_PR_radioNetwork:
          pdusessionTransfer_p->cause.choice.radioNetwork  = pdusession_modify_resp_p->pdusessions_failed[i].cause_value;
          break;
zhenghuangkun's avatar
zhenghuangkun committed
1300

1301 1302 1303
      case NGAP_Cause_PR_transport:
          pdusessionTransfer_p->cause.choice.transport     = pdusession_modify_resp_p->pdusessions_failed[i].cause_value;
          break;
zhenghuangkun's avatar
zhenghuangkun committed
1304

1305 1306 1307
      case NGAP_Cause_PR_nas:
          pdusessionTransfer_p->cause.choice.nas           = pdusession_modify_resp_p->pdusessions_failed[i].cause_value;
          break;
zhenghuangkun's avatar
zhenghuangkun committed
1308

1309 1310 1311
      case NGAP_Cause_PR_protocol:
          pdusessionTransfer_p->cause.choice.protocol      = pdusession_modify_resp_p->pdusessions_failed[i].cause_value;
          break;
zhenghuangkun's avatar
zhenghuangkun committed
1312

1313 1314 1315
      case NGAP_Cause_PR_misc:
          pdusessionTransfer_p->cause.choice.misc          = pdusession_modify_resp_p->pdusessions_failed[i].cause_value;
          break;
zhenghuangkun's avatar
zhenghuangkun committed
1316

1317 1318 1319 1320
      case NGAP_Cause_PR_NOTHING:
      default:
          break;
      }
zhenghuangkun's avatar
zhenghuangkun committed
1321

1322 1323 1324 1325
      memset(&res, 0, sizeof(res));
      res = asn_encode_to_new_buffer(NULL, ATS_ALIGNED_CANONICAL_PER, &asn_DEF_NGAP_PDUSessionResourceModifyUnsuccessfulTransfer, pdusessionTransfer_p);
      item->pDUSessionResourceModifyUnsuccessfulTransfer.buf = res.buffer;
      item->pDUSessionResourceModifyUnsuccessfulTransfer.size = res.result.encoded;
zhenghuangkun's avatar
zhenghuangkun committed
1326

1327 1328 1329 1330 1331
      ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_NGAP_PDUSessionResourceModifyUnsuccessfulTransfer, pdusessionTransfer_p);

      
      NGAP_DEBUG("pdusession_modify_resp: failed pdusession ID %ld\n", item->pDUSessionID);
      ASN_SEQUENCE_ADD(&ie->value.choice.PDUSessionResourceFailedToModifyListModRes.list, item);
zhenghuangkun's avatar
zhenghuangkun committed
1332 1333
    }

1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
    ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  }

  /* optional */
  if (0) {
    ie = (NGAP_PDUSessionResourceModifyResponseIEs_t *)calloc(1, sizeof(NGAP_PDUSessionResourceModifyResponseIEs_t));
    ie->id = NGAP_ProtocolIE_ID_id_CriticalityDiagnostics;
    ie->criticality = NGAP_Criticality_ignore;
    ie->value.present = NGAP_PDUSessionResourceModifyResponseIEs__value_PR_CriticalityDiagnostics;
    // ie->value.choice.CriticalityDiagnostics = ;
    ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  }

  fprintf(stderr, "start encode\n");

  if (ngap_gNB_encode_pdu(&pdu, &buffer, &length) < 0) {
    NGAP_ERROR("Failed to encode uplink transport\n");
    /* Encode procedure has failed... */
    return -1;
  }

  MSC_LOG_TX_MESSAGE(
      MSC_NGAP_GNB,
      MSC_NGAP_AMF,
      (const char *)buffer,
      length,
      MSC_AS_TIME_FMT" E_RAN Modify successful Outcome gNB_ue_ngap_id %u amf_ue_ngap_id %u",
      0,0,//MSC_AS_TIME_ARGS(ctxt_pP),
      pdusession_modify_resp_p->gNB_ue_ngap_id,
      ue_context_p->amf_ue_ngap_id);
  /* UE associated signalling -> use the allocated stream */
  ngap_gNB_itti_send_sctp_data_req(ngap_gNB_instance_p->instance,
                                   ue_context_p->amf_ref->assoc_id, buffer,
                                   length, ue_context_p->tx_stream);

  return 0;
zhenghuangkun's avatar
zhenghuangkun committed
1370 1371
}
//------------------------------------------------------------------------------
zhenghuangkun's avatar
zhenghuangkun committed
1372 1373
int ngap_gNB_pdusession_release_resp(instance_t instance,
                                ngap_pdusession_release_resp_t *pdusession_release_resp_p)
zhenghuangkun's avatar
zhenghuangkun committed
1374 1375
//------------------------------------------------------------------------------
{
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
  ngap_gNB_instance_t            *ngap_gNB_instance_p = NULL;
  struct ngap_gNB_ue_context_s   *ue_context_p        = NULL;
  NGAP_NGAP_PDU_t                 pdu;
  NGAP_PDUSessionResourceReleaseResponse_t    *out;
  NGAP_PDUSessionResourceReleaseResponseIEs_t *ie;
  uint8_t  *buffer  = NULL;
  uint32_t length;
  int      i;
  /* Retrieve the NGAP gNB instance associated with Mod_id */
  ngap_gNB_instance_p = ngap_gNB_get_instance(instance);
  DevAssert(pdusession_release_resp_p != NULL);
  DevAssert(ngap_gNB_instance_p != NULL);

  if ((ue_context_p = ngap_gNB_get_ue_context(ngap_gNB_instance_p,
                      pdusession_release_resp_p->gNB_ue_ngap_id)) == NULL) {
    /* The context for this gNB ue ngap id doesn't exist in the map of gNB UEs */
    NGAP_WARN("Failed to find ue context associated with gNB ue ngap id: 0x%08x\n",
              pdusession_release_resp_p->gNB_ue_ngap_id);
    return -1;
  }

  /* Prepare the NGAP message to encode */
  memset(&pdu, 0, sizeof(pdu));
  pdu.present = NGAP_NGAP_PDU_PR_successfulOutcome;
1400
  pdu.choice.successfulOutcome = (NGAP_SuccessfulOutcome_t *)calloc(1,sizeof(struct NGAP_SuccessfulOutcome));
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
  pdu.choice.successfulOutcome->procedureCode = NGAP_ProcedureCode_id_PDUSessionResourceRelease;
  pdu.choice.successfulOutcome->criticality = NGAP_Criticality_reject;
  pdu.choice.successfulOutcome->value.present = NGAP_SuccessfulOutcome__value_PR_PDUSessionResourceReleaseResponse;
  out = &pdu.choice.successfulOutcome->value.choice.PDUSessionResourceReleaseResponse;
  /* mandatory */
  ie = (NGAP_PDUSessionResourceReleaseResponseIEs_t *)calloc(1, sizeof(NGAP_PDUSessionResourceReleaseResponseIEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_AMF_UE_NGAP_ID;
  ie->criticality = NGAP_Criticality_ignore;
  ie->value.present = NGAP_PDUSessionResourceReleaseResponseIEs__value_PR_AMF_UE_NGAP_ID;
  asn_uint642INTEGER(&ie->value.choice.AMF_UE_NGAP_ID, ue_context_p->amf_ue_ngap_id);
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  /* mandatory */
  ie = (NGAP_PDUSessionResourceReleaseResponseIEs_t *)calloc(1, sizeof(NGAP_PDUSessionResourceReleaseResponseIEs_t));
  ie->id = NGAP_ProtocolIE_ID_id_RAN_UE_NGAP_ID;
  ie->criticality = NGAP_Criticality_ignore;
  ie->value.present = NGAP_PDUSessionResourceReleaseResponseIEs__value_PR_RAN_UE_NGAP_ID;
  ie->value.choice.RAN_UE_NGAP_ID = pdusession_release_resp_p->gNB_ue_ngap_id;
  ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);

zhenghuangkun's avatar
zhenghuangkun committed
1420
    /* optional */
zhenghuangkun's avatar
zhenghuangkun committed
1421
    if (pdusession_release_resp_p->nb_of_pdusessions_released > 0) {
1422 1423
        ie = (NGAP_PDUSessionResourceReleaseResponseIEs_t *)calloc(1, sizeof(NGAP_PDUSessionResourceReleaseResponseIEs_t));
        ie->id = NGAP_ProtocolIE_ID_id_PDUSessionResourceReleasedListRelRes;
zhenghuangkun's avatar
zhenghuangkun committed
1424
        ie->criticality = NGAP_Criticality_ignore;
1425
        ie->value.present = NGAP_PDUSessionResourceReleaseResponseIEs__value_PR_PDUSessionResourceReleasedListRelRes;
zhenghuangkun's avatar
zhenghuangkun committed
1426

zhenghuangkun's avatar
zhenghuangkun committed
1427
        for (i = 0; i < pdusession_release_resp_p->nb_of_pdusessions_released; i++) {
1428 1429 1430 1431
            NGAP_PDUSessionResourceReleasedItemRelRes_t *item;
            item = (NGAP_PDUSessionResourceReleasedItemRelRes_t *)calloc(1, sizeof(NGAP_PDUSessionResourceReleasedItemRelRes_t));
        
            item->pDUSessionID = pdusession_release_resp_p->pdusession_release[i].pdusession_id;
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
            if(pdusession_release_resp_p->pdusession_release[i].transfer_length > 0){
                item->pDUSessionResourceReleaseResponseTransfer.size = pdusession_release_resp_p->pdusession_release[i].transfer_length;
                item->pDUSessionResourceReleaseResponseTransfer.buf = malloc(sizeof(uint8_t) * pdusession_release_resp_p->pdusession_release[i].transfer_length);
                memcpy(item->pDUSessionResourceReleaseResponseTransfer.buf,
                       pdusession_release_resp_p->pdusession_release[i].transfer_buffer,
                       pdusession_release_resp_p->pdusession_release[i].transfer_length);
            }else {
                item->pDUSessionResourceReleaseResponseTransfer.size = 0;
                item->pDUSessionResourceReleaseResponseTransfer.buf = NULL;
                NGAP_DEBUG("pdusession_release_resp: transfer_buffer is NULL!\n");
1442 1443 1444 1445 1446 1447 1448 1449 1450
#ifdef ITTI_SIM
                //For testing only
                item->pDUSessionResourceReleaseResponseTransfer.buf = malloc(sizeof(uint8_t) * 1);
                item->pDUSessionResourceReleaseResponseTransfer.size = 1;
                uint8_t temp = 0;
                memcpy(item->pDUSessionResourceReleaseResponseTransfer.buf,
                       &temp,
                       item->pDUSessionResourceReleaseResponseTransfer.size);    
#endif
1451
            }
1452 1453
            NGAP_DEBUG("pdusession_release_resp: pdusession ID %ld\n", item->pDUSessionID);
            ASN_SEQUENCE_ADD(&ie->value.choice.PDUSessionResourceReleasedListRelRes.list, item);
zhenghuangkun's avatar
zhenghuangkun committed
1454 1455
        }

1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
    ASN_SEQUENCE_ADD(&out->protocolIEs.list, ie);
  }


  if (ngap_gNB_encode_pdu(&pdu, &buffer, &length) < 0) {
    NGAP_ERROR("Failed to encode release response\n");
    /* Encode procedure has failed... */
    return -1;
  }

  MSC_LOG_TX_MESSAGE(
      MSC_NGAP_GNB,
      MSC_NGAP_AMF,
      (const char *)buffer,
      length,
      MSC_AS_TIME_FMT" E_RAN Release successfulOutcome gNB_ue_ngap_id %u amf_ue_ngap_id %lu",
      0,0,//MSC_AS_TIME_ARGS(ctxt_pP),
      pdusession_release_resp_p->gNB_ue_ngap_id,
      (uint64_t)ue_context_p->amf_ue_ngap_id);
  /* UE associated signalling -> use the allocated stream */
  ngap_gNB_itti_send_sctp_data_req(ngap_gNB_instance_p->instance,
                                   ue_context_p->amf_ref->assoc_id, buffer,
                                   length, ue_context_p->tx_stream);
  NGAP_INFO("pdusession_release_response sended gNB_UE_NGAP_ID %u  amf_ue_ngap_id %lu nb_of_pdusessions_released %d nb_of_pdusessions_failed %d\n",
            pdusession_release_resp_p->gNB_ue_ngap_id, (uint64_t)ue_context_p->amf_ue_ngap_id,pdusession_release_resp_p->nb_of_pdusessions_released,pdusession_release_resp_p->nb_of_pdusessions_failed);
zhenghuangkun's avatar
zhenghuangkun committed
1481

1482
  return 0;
zhenghuangkun's avatar
zhenghuangkun committed
1483 1484 1485 1486 1487 1488
}

int ngap_gNB_path_switch_req(instance_t instance,
                             ngap_path_switch_req_t *path_switch_req_p)
//------------------------------------------------------------------------------
{
1489
  //TODO
zhenghuangkun's avatar
zhenghuangkun committed
1490

heshanyun's avatar
heshanyun committed
1491
  return 0;
zhenghuangkun's avatar
zhenghuangkun committed
1492 1493
}

zhenghuangkun's avatar
zhenghuangkun committed
1494
int ngap_gNB_generate_PDUSESSION_Modification_Indication(
zhenghuangkun's avatar
zhenghuangkun committed
1495
		instance_t instance,
zhenghuangkun's avatar
zhenghuangkun committed
1496
  ngap_pdusession_modification_ind_t *pdusession_modification_ind)
zhenghuangkun's avatar
zhenghuangkun committed
1497 1498
//-----------------------------------------------------------------------------
{
1499
  //TODO
heshanyun's avatar
heshanyun committed
1500
  return 0;
zhenghuangkun's avatar
zhenghuangkun committed
1501 1502 1503
}