GNB-ID.cpp 3.42 KB
Newer Older
1 2 3 4 5
/*
 * 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
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this
7 8
 * file except in compliance with the License. You may obtain a copy of the
 * License at
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 *      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
 */

dukl's avatar
dukl committed
22 23 24 25 26
#include "GNB-ID.hpp"

#include <iostream>
using namespace std;

27 28 29
namespace ngap {

//------------------------------------------------------------------------------
30 31 32
GNB_ID::GNB_ID() {
  gNBId = {};
}
33 34

//------------------------------------------------------------------------------
35
GNB_ID::~GNB_ID() {}
36 37 38

//------------------------------------------------------------------------------
void GNB_ID::setValue(uint32_t gnbId) {
39
  gNBId.id    = gnbId;
40 41 42
  uint8_t len = 0;
  for (uint32_t i = 0x00000001; i <= 0x00000400; i = i << 1, len++) {
    if ((i & gnbId)) {
43
      gNBId.bit_length = 32 - len;
44 45 46
      break;
    }
  }
47
  if (!((gNBId.bit_length >= 22) && (gNBId.bit_length <= 32))) {
48 49 50 51
    cout << "[warning][gNBID length out of range]" << endl;
  }
}

52 53 54 55 56 57
//------------------------------------------------------------------------------
void GNB_ID::setValue(uint32_t id, uint8_t bit_length) {
  gNBId.id         = id;
  gNBId.bit_length = bit_length;
}

58
//------------------------------------------------------------------------------
59
bool GNB_ID::encode2bitstring(Ngap_GNB_ID_t& gnbid) {
60 61
  gnbid.present = Ngap_GNB_ID_PR_gNB_ID;

62 63
  if (!(gNBId.bit_length % 8))
    gnbid.choice.gNB_ID.size = gNBId.bit_length / 8;
64
  else
65
    gnbid.choice.gNB_ID.size = gNBId.bit_length / 8 + 1;
66

67
  // printf("m_gNBId.size(%d)\n",m_gNBId.size);
68
  gnbid.choice.gNB_ID.bits_unused = 32 - gNBId.bit_length;
69 70
  gnbid.choice.gNB_ID.buf         = (uint8_t*) calloc(1, 4 * sizeof(uint8_t));
  if (!gnbid.choice.gNB_ID.buf) return false;
71 72 73 74 75 76 77 78 79
  gnbid.choice.gNB_ID.buf[3] = gNBId.id & 0x000000ff;
  gnbid.choice.gNB_ID.buf[2] = (gNBId.id & 0x0000ff00) >> 8;
  gnbid.choice.gNB_ID.buf[1] = (gNBId.id & 0x00ff0000) >> 16;
  gnbid.choice.gNB_ID.buf[0] = (gNBId.id & 0xff000000) >> 24;

  return true;
}

//------------------------------------------------------------------------------
80 81 82
bool GNB_ID::decodefrombitstring(Ngap_GNB_ID_t& gnbid) {
  if (gnbid.present != Ngap_GNB_ID_PR_gNB_ID) return false;
  if (!gnbid.choice.gNB_ID.buf) return false;
83 84 85 86 87 88 89 90 91 92 93 94 95 96

  gNBId.id = gnbid.choice.gNB_ID.buf[0] << 24;
  gNBId.id |= gnbid.choice.gNB_ID.buf[1] << 16;
  gNBId.id |= gnbid.choice.gNB_ID.buf[2] << 8;
  gNBId.id |= gnbid.choice.gNB_ID.buf[3];

  return true;
}

//------------------------------------------------------------------------------
long GNB_ID::getValue() {
  return gNBId.id;
}

97
}  // namespace ngap