Commit 4c38dcad authored by Tien Thinh NGUYEN's avatar Tien Thinh NGUYEN

Code refactor for NSSAI

parent 6e336ecb
......@@ -54,7 +54,7 @@ int _5GSRegistrationType::encode2Buffer(uint8_t* buf, int len) {
Logger::nas_mm().error("Encoding 5gsregistrationtype IE");
if (len < kType1IeSize) {
Logger::nas_mm().error(
"Buffer length is less than the minimum length of this IE (%s octet)",
"Buffer length is less than the minimum length of this IE (%d octet)",
kType1IeSize);
return KEncodeDecodeError;
}
......@@ -80,7 +80,7 @@ int _5GSRegistrationType::decodeFromBuffer(
uint8_t* buf, int len, bool is_option) {
if (len < kType1IeSize) {
Logger::nas_mm().error(
"Buffer length is less than the minimum length of this IE (%s octet)",
"Buffer length is less than the minimum length of this IE (%d octet)",
kType1IeSize);
return KEncodeDecodeError;
}
......
......@@ -53,7 +53,7 @@ uint8_t ExtendedProtocolDiscriminator::Get() const {
int ExtendedProtocolDiscriminator::Encode(uint8_t* buf, const uint32_t& len) {
if (len < kType1IeSize) {
Logger::nas_mm().error(
"Buffer length is less than the minimum length of this IE (%s octet)",
"Buffer length is less than the minimum length of this IE (%d octet)",
kType1IeSize);
return KEncodeDecodeError;
}
......@@ -67,7 +67,7 @@ int ExtendedProtocolDiscriminator::Decode(
const uint8_t* const buf, const uint32_t& len) {
if (len < kType1IeSize) {
Logger::nas_mm().error(
"Buffer length is less than the minimum length of this IE (%s octet)",
"Buffer length is less than the minimum length of this IE (%d octet)",
kType1IeSize);
return KEncodeDecodeError;
}
......
......@@ -21,10 +21,11 @@
#include "NSSAI.hpp"
#include <vector>
#include "logger.hpp"
#include "3gpp_24.501.hpp"
#include "amf.hpp"
#include "logger.hpp"
#include <vector>
using namespace nas;
......@@ -35,11 +36,12 @@ NSSAI::NSSAI(uint8_t iei) {
}
//------------------------------------------------------------------------------
NSSAI::NSSAI(const uint8_t iei, std::vector<struct SNSSAI_s> nssai) {
NSSAI::NSSAI(const uint8_t iei, const std::vector<struct SNSSAI_s>& nssai) {
_iei = iei;
length = 0;
S_NSSAIs.assign(nssai.begin(), nssai.end());
for (int i = 0; i < nssai.size(); i++) {
// TODO: do the calculation based on length of nssai
length += 2; // 1 for IEI and 1 for sst
if (nssai[i].sd != SD_NO_VALUE) length += SD_LENGTH;
if (nssai[i].mHplmnSst != -1) length += SST_LENGTH;
......@@ -53,9 +55,6 @@ NSSAI::NSSAI() : _iei(), length(), S_NSSAIs() {}
//------------------------------------------------------------------------------
NSSAI::~NSSAI() {}
//------------------------------------------------------------------------------
void NSSAI::setS_NAASI(uint8_t SST) {}
//------------------------------------------------------------------------------
void NSSAI::getValue(std::vector<struct SNSSAI_s>& nssai) {
nssai.assign(S_NSSAIs.begin(), S_NSSAIs.end());
......@@ -64,10 +63,15 @@ void NSSAI::getValue(std::vector<struct SNSSAI_s>& nssai) {
//------------------------------------------------------------------------------
int NSSAI::encode2Buffer(uint8_t* buf, int len) {
Logger::nas_mm().debug("Encoding NSSAI IEI (0x%x)", _iei);
if (len < length) {
Logger::nas_mm().error("len is less than %d", length);
return 0;
int total_length = (_iei > 0) ? (length + 2) : (length + 1);
if (len < total_length) {
Logger::nas_mm().error(
"Buffer length is less than the length of this IE (%d octet)",
total_length);
return KEncodeDecodeError;
}
int encoded_size = 0;
if (_iei) {
ENCODE_U8(buf + encoded_size, _iei, encoded_size);
......@@ -76,40 +80,53 @@ int NSSAI::encode2Buffer(uint8_t* buf, int len) {
ENCODE_U8(buf + encoded_size, length, encoded_size);
for (int i = 0; i < S_NSSAIs.size(); i++) {
// TODO: Define encode for SNSSAI_s
int len_s_nssai = SST_LENGTH;
encoded_size++;
*(buf + encoded_size) = S_NSSAIs.at(i).sst;
encoded_size++;
encoded_size++; // Store the length of S-NSSAI contents later
ENCODE_U8(buf + encoded_size, S_NSSAIs.at(i).sst, encoded_size);
if (S_NSSAIs.at(i).sd != SD_NO_VALUE) {
len_s_nssai += SD_LENGTH;
*(buf + encoded_size) = (S_NSSAIs.at(i).sd & 0x00ff0000) >> 16;
encoded_size++;
ENCODE_U8(
buf + encoded_size, (S_NSSAIs.at(i).sd & 0x00ff0000) >> 16,
encoded_size);
Logger::nas_mm().debug(
"Encoded NSSAI SD first octet (%x)", *(buf + encoded_size - 1));
*(buf + encoded_size) = (S_NSSAIs.at(i).sd & 0x0000ff00) >> 8;
encoded_size++;
"Encoded NSSAI SD first octet (0x%x)",
(S_NSSAIs.at(i).sd & 0x00ff0000) >> 16);
ENCODE_U8(
buf + encoded_size, (S_NSSAIs.at(i).sd & 0x0000ff00) >> 8,
encoded_size);
Logger::nas_mm().debug(
"Encoded NSSAI SD second octet (%x)", *(buf + encoded_size - 1));
*(buf + encoded_size) = S_NSSAIs.at(i).sd & 0x000000ff;
encoded_size++;
ENCODE_U8(
buf + encoded_size, S_NSSAIs.at(i).sd & 0x000000ff, encoded_size);
Logger::nas_mm().debug(
"Encoded NSSAI SD third octet (%x)", *(buf + encoded_size - 1));
}
if (S_NSSAIs.at(i).mHplmnSst != -1) {
len_s_nssai += SST_LENGTH;
*(buf + encoded_size) = S_NSSAIs.at(i).mHplmnSst;
encoded_size++;
ENCODE_U8(buf + encoded_size, S_NSSAIs.at(i).mHplmnSst, encoded_size);
}
if (S_NSSAIs.at(i).mHplmnSd != SD_NO_VALUE) {
len_s_nssai += SD_LENGTH;
*(buf + encoded_size) = (S_NSSAIs.at(i).mHplmnSd & 0x00ff0000) >> 16;
encoded_size++;
*(buf + encoded_size) = (S_NSSAIs.at(i).mHplmnSd & 0x0000ff00) >> 8;
encoded_size++;
*(buf + encoded_size) = S_NSSAIs.at(i).mHplmnSd & 0x000000ff;
encoded_size++;
ENCODE_U8(
buf + encoded_size, (S_NSSAIs.at(i).mHplmnSd & 0x00ff0000) >> 16,
encoded_size);
ENCODE_U8(
buf + encoded_size, (S_NSSAIs.at(i).mHplmnSd & 0x0000ff00) >> 8,
encoded_size);
ENCODE_U8(
buf + encoded_size, S_NSSAIs.at(i).mHplmnSd & 0x000000ff,
encoded_size);
}
*(buf + encoded_size - len_s_nssai - 1) = len_s_nssai;
// Length of S-NSSAI contents
int encoded_len_ie = 0;
ENCODE_U8(
buf + encoded_size - len_s_nssai - 1, len_s_nssai, encoded_len_ie);
}
Logger::nas_mm().debug("Encoded NSSAI len (%d)", encoded_size);
......@@ -122,97 +139,72 @@ int NSSAI::decodeFromBuffer(uint8_t* buf, int len, bool is_option) {
int decoded_size = 0;
SNSSAI_s a = {0, 0, 0, 0};
if (is_option) {
decoded_size++;
// IEI
DECODE_U8(buf + decoded_size, _iei, decoded_size);
}
length = *(buf + decoded_size);
decoded_size++;
// Length
DECODE_U8(buf + decoded_size, length, decoded_size);
int length_tmp = length;
while (length_tmp) {
switch (*(buf + decoded_size)) {
case 1: {
decoded_size++; // snssai—length
length_tmp--;
a.sst = *(buf + decoded_size);
switch (*(buf + decoded_size)) { // Length of SNSSAI
case 1: { // Only SST
// Length of SNSSAI
decoded_size++;
length_tmp--;
// SST
DECODE_U8(buf + decoded_size, a.sst, decoded_size);
length_tmp--;
a.sd = SD_NO_VALUE;
a.mHplmnSst = 0;
a.mHplmnSd = 0;
} break;
case 4: {
decoded_size++;
length_tmp--;
a.sst = *(buf + decoded_size);
decoded_size++;
length_tmp--;
a.sd |= *(buf + decoded_size);
decoded_size++;
length_tmp--;
a.sd <<= 8;
a.sd |= *(buf + decoded_size);
case 4: { // SST and SD
// Length of SNSSAI
decoded_size++;
length_tmp--;
a.sd <<= 8;
a.sd |= *(buf + decoded_size);
decoded_size++;
// SST
DECODE_U8(buf + decoded_size, a.sst, decoded_size);
length_tmp--;
// SD
DECODE_U24(buf + decoded_size, a.sd, decoded_size);
length_tmp -= 3;
a.mHplmnSst = 0;
a.mHplmnSd = 0;
} break;
case 5: {
decoded_size++;
length_tmp--;
a.sst = *(buf + decoded_size);
decoded_size++;
length_tmp--;
a.sd |= *(buf + decoded_size);
case 5: { // SST, SD and HPLMN SST
// Length of SNSSAI
decoded_size++;
length_tmp--;
a.sd <<= 8;
a.sd |= *(buf + decoded_size);
decoded_size++;
length_tmp--;
a.sd <<= 8;
a.sd |= *(buf + decoded_size);
decoded_size++;
// SST
DECODE_U8(buf + decoded_size, a.sst, decoded_size);
length_tmp--;
a.mHplmnSst = *(buf + decoded_size);
decoded_size++;
// SD
DECODE_U24(buf + decoded_size, a.sd, decoded_size);
length_tmp -= 3;
// HPLMN SST
DECODE_U8(buf + decoded_size, a.mHplmnSst, decoded_size);
length_tmp--;
a.mHplmnSd = SD_NO_VALUE;
} break;
case 8: {
// Length of SNSSAI
decoded_size++;
length_tmp--;
a.sst = *(buf + decoded_size);
decoded_size++;
length_tmp--;
a.sd |= *(buf + decoded_size);
decoded_size++;
length_tmp--;
a.sd <<= 8;
a.sd |= *(buf + decoded_size);
decoded_size++;
length_tmp--;
a.sd <<= 8;
a.sd |= *(buf + decoded_size);
decoded_size++;
length_tmp--;
a.mHplmnSst = *(buf + decoded_size);
decoded_size++;
length_tmp--;
a.mHplmnSd |= *(buf + decoded_size);
decoded_size++;
// SST
DECODE_U8(buf + decoded_size, a.sst, decoded_size);
length_tmp--;
a.mHplmnSd <<= 8;
a.mHplmnSd |= *(buf + decoded_size);
decoded_size++;
length_tmp--;
a.mHplmnSd <<= 8;
a.mHplmnSd |= *(buf + decoded_size);
decoded_size++;
// SD
DECODE_U24(buf + decoded_size, a.sd, decoded_size);
length_tmp -= 3;
// HPLMN SST
DECODE_U8(buf + decoded_size, a.mHplmnSst, decoded_size);
length_tmp--;
// HPLMN SD
DECODE_U24(buf + decoded_size, a.mHplmnSd, decoded_size);
length_tmp -= 3;
} break;
}
......
......@@ -19,15 +19,8 @@
* contact@openairinterface.org
*/
/*! \file
\brief
\author Keliang DU, BUPT
\date 2020
\email: contact@openairinterface.org
*/
#ifndef __NSSAI_H_
#define __NSSAI_H_
#ifndef _NSSAI_H_
#define _NSSAI_H_
#include <stdint.h>
#include <vector>
......@@ -40,9 +33,8 @@ class NSSAI {
public:
NSSAI();
NSSAI(uint8_t iei);
NSSAI(const uint8_t iei, std::vector<struct SNSSAI_s> nssai);
NSSAI(const uint8_t iei, const std::vector<struct SNSSAI_s>& nssai);
~NSSAI();
void setS_NAASI(uint8_t SST);
int encode2Buffer(uint8_t* buf, int len);
int decodeFromBuffer(uint8_t* buf, int len, bool is_option);
void getValue(std::vector<struct SNSSAI_s>& nssai);
......
......@@ -54,7 +54,7 @@ int NasKeySetIdentifier::encode2Buffer(uint8_t* buf, const int& len) {
Logger::nas_mm().debug("Encoding NasKeySetIdentifier IE (IEI 0x%x)", iei_);
if (len < kType1IeSize) {
Logger::nas_mm().error(
"Buffer length is less than the minimum length of this IE (%s octet)",
"Buffer length is less than the minimum length of this IE (%d octet)",
kType1IeSize);
return KEncodeDecodeError;
}
......@@ -81,7 +81,7 @@ int NasKeySetIdentifier::decodeFromBuffer(
if (len < kType1IeSize) {
Logger::nas_mm().error(
"Buffer length is less than the minimum length of this IE (%s octet)",
"Buffer length is less than the minimum length of this IE (%d octet)",
kType1IeSize);
return KEncodeDecodeError;
}
......
......@@ -53,7 +53,7 @@ uint8_t NasMessageType::Get() const {
int NasMessageType::Encode(uint8_t* buf, const uint32_t& len) {
if (len < kType1IeSize) {
Logger::nas_mm().error(
"Buffer length is less than the minimum length of this IE (%s octet)",
"Buffer length is less than the minimum length of this IE (%d octet)",
kType1IeSize);
return KEncodeDecodeError;
}
......@@ -66,7 +66,7 @@ int NasMessageType::Encode(uint8_t* buf, const uint32_t& len) {
int NasMessageType::Decode(const uint8_t* const buf, const uint32_t& len) {
if (len < kType1IeSize) {
Logger::nas_mm().error(
"Buffer length is less than the minimum length of this IE (%s octet)",
"Buffer length is less than the minimum length of this IE (%d octet)",
kType1IeSize);
return KEncodeDecodeError;
}
......
......@@ -54,7 +54,7 @@ uint8_t SecurityHeaderType::Get() const {
int SecurityHeaderType::Encode(uint8_t* buf, const uint32_t& len) {
if (len < kType1IeSize) {
Logger::nas_mm().error(
"Buffer length is less than the minimum length of this IE (%s octet)",
"Buffer length is less than the minimum length of this IE (%d octet)",
kType1IeSize);
return KEncodeDecodeError;
}
......@@ -69,7 +69,7 @@ int SecurityHeaderType::Encode(uint8_t* buf, const uint32_t& len) {
int SecurityHeaderType::Decode(const uint8_t* const buf, const uint32_t& len) {
if (len < kType1IeSize) {
Logger::nas_mm().error(
"Buffer length is less than the minimum length of this IE (%s octet)",
"Buffer length is less than the minimum length of this IE (%d octet)",
kType1IeSize);
return KEncodeDecodeError;
}
......
......@@ -33,7 +33,7 @@ RegistrationRequest::RegistrationRequest()
ie_non_current_native_nas_ksi = std::nullopt;
ie_5g_mm_capability = std::nullopt;
ie_ue_security_capability = std::nullopt;
ie_requested_NSSAI = nullptr;
ie_requested_NSSAI = std::nullopt;
ie_s1_ue_network_capability = nullptr;
ie_uplink_data_status = nullptr;
ie_last_visited_registered_TAI = nullptr;
......@@ -254,14 +254,14 @@ bool RegistrationRequest::getUeSecurityCapability(
//------------------------------------------------------------------------------
void RegistrationRequest::setRequested_NSSAI(
std::vector<struct SNSSAI_s> nssai) {
ie_requested_NSSAI = new NSSAI(0x2F, nssai);
ie_requested_NSSAI = std::make_optional<NSSAI>(0x2F, nssai);
}
//------------------------------------------------------------------------------
bool RegistrationRequest::getRequestedNssai(
std::vector<struct SNSSAI_s>& nssai) {
if (ie_requested_NSSAI) {
ie_requested_NSSAI->getValue(nssai);
if (ie_requested_NSSAI.has_value()) {
ie_requested_NSSAI.value().getValue(nssai);
} else {
return false;
}
......@@ -608,17 +608,19 @@ int RegistrationRequest::encode2Buffer(uint8_t* buf, int len) {
}
}
if (!ie_requested_NSSAI) {
Logger::nas_mm().warn("IE ie_requested_NSSAI is not available");
// Requested NSSAI
if (!ie_requested_NSSAI.has_value()) {
Logger::nas_mm().warn("IE Requested NSSAI is not available");
} else {
if (int size = ie_requested_NSSAI->encode2Buffer(
buf + encoded_size, len - encoded_size)) {
encoded_size += size;
if ((encoded_ie_size = ie_requested_NSSAI.value().encode2Buffer(
buf + encoded_size, len - encoded_size)) == KEncodeDecodeError) {
Logger::nas_mm().error("Encoding Requested NSSAI error");
return KEncodeDecodeError;
} else {
Logger::nas_mm().error("encoding ie_requested_NSSAI error");
return 0;
encoded_size += encoded_ie_size;
}
}
if (!ie_last_visited_registered_TAI) {
Logger::nas_mm().warn("IE ie_Last_visited_registered_TAI is not available");
} else {
......@@ -913,10 +915,11 @@ int RegistrationRequest::decodeFromBuffer(uint8_t* buf, int len) {
} break;
case 0x2F: {
Logger::nas_mm().debug("Decoding IEI (0x2F)");
ie_requested_NSSAI = new NSSAI();
decoded_size += ie_requested_NSSAI->decodeFromBuffer(
NSSAI ie_requested_NSSAI_tmp = {};
decoded_size += ie_requested_NSSAI_tmp.decodeFromBuffer(
buf + decoded_size, len - decoded_size, true);
octet = *(buf + decoded_size);
ie_requested_NSSAI = std::optional<NSSAI>(ie_requested_NSSAI_tmp);
octet = *(buf + decoded_size);
Logger::nas_mm().debug("Next IEI 0x%x", octet);
} break;
case 0x52: {
......
......@@ -155,7 +155,7 @@ class RegistrationRequest : public NasMmPlainHeader {
std::optional<NasKeySetIdentifier> ie_non_current_native_nas_ksi; // Optional
std::optional<_5GMMCapability> ie_5g_mm_capability; // Optional
std::optional<UESecurityCapability> ie_ue_security_capability; // Optional
NSSAI* ie_requested_NSSAI; // Optional
std::optional<NSSAI> ie_requested_NSSAI; // Optional
// TODO: Last visited registered TAI
UENetworkCapability* ie_s1_ue_network_capability; // Optional
UplinkDataStatus* ie_uplink_data_status; // Optional
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment