From 49c44a2bd4d7d2c08a06673334941f2a2bbd6758 Mon Sep 17 00:00:00 2001 From: Pedro Alvarez Date: Mon, 24 Sep 2018 14:08:00 +0100 Subject: [PATCH] Moved int_helpers to lib so that the EPC can use those functions too. --- lib/include/srslte/common/int_helpers.h | 66 +++++++++++++++ srsenb/hdr/upper/common_enb.h | 106 ------------------------ srsenb/src/upper/rrc.cc | 4 +- srsenb/src/upper/s1ap.cc | 5 ++ srsenb/test/upper/plmn_test.cc | 9 +- 5 files changed, 79 insertions(+), 111 deletions(-) create mode 100644 lib/include/srslte/common/int_helpers.h diff --git a/lib/include/srslte/common/int_helpers.h b/lib/include/srslte/common/int_helpers.h new file mode 100644 index 000000000..e6ac48155 --- /dev/null +++ b/lib/include/srslte/common/int_helpers.h @@ -0,0 +1,66 @@ +/** + * + * \section COPYRIGHT + * + * Copyright 2013-2015 Software Radio Systems Limited + * + * \section LICENSE + * + * This file is part of the srsUE library. + * + * srsUE is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * srsUE is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * A copy of the GNU Affero General Public License can be found in + * the LICENSE file in the top-level directory of this distribution + * and at http://www.gnu.org/licenses/. + * + */ + +#ifndef SRSLTE_INT_HELPERS_H +#define SRSLTE_INT_HELPERS_H + +namespace srslte { + +/****************************************************************************** + * Safe conversions between byte buffers and integer types. + * Note: these don't perform endian conversion - use e.g. htonl/ntohl if required + *****************************************************************************/ +inline void uint8_to_uint32(uint8_t *buf, uint32_t *i) +{ + *i = (uint32_t)buf[0] << 24 | + (uint32_t)buf[1] << 16 | + (uint32_t)buf[2] << 8 | + (uint32_t)buf[3]; +} + +inline void uint32_to_uint8(uint32_t i, uint8_t *buf) +{ + buf[0] = (i >> 24) & 0xFF; + buf[1] = (i >> 16) & 0xFF; + buf[2] = (i >> 8) & 0xFF; + buf[3] = i & 0xFF; +} + +inline void uint8_to_uint16(uint8_t *buf, uint16_t *i) +{ + *i = (uint32_t)buf[0] << 8 | + (uint32_t)buf[1]; +} + +inline void uint16_to_uint8(uint16_t i, uint8_t *buf) +{ + buf[0] = (i >> 8) & 0xFF; + buf[1] = i & 0xFF; +} + +}; //namespace + +#endif // SRSLTE_INT_HELPERS_H diff --git a/srsenb/hdr/upper/common_enb.h b/srsenb/hdr/upper/common_enb.h index 2e0b28917..2458cbc19 100644 --- a/srsenb/hdr/upper/common_enb.h +++ b/srsenb/hdr/upper/common_enb.h @@ -36,7 +36,6 @@ namespace srsenb { #define ENB_METRICS_MAX_USERS 64 - #define SRSENB_RRC_MAX_N_PLMN_IDENTITIES 6 #define SRSENB_N_SRB 3 @@ -74,111 +73,6 @@ static const char rb_id_text[RB_ID_N_ITEMS][20] = { "SRB0", #define SRSENB_MAX_BUFFER_SIZE_BITS 102048 #define SRSENB_MAX_BUFFER_SIZE_BYTES 12756 #define SRSENB_BUFFER_HEADER_OFFSET 1024 - -/****************************************************************************** - * Convert PLMN to BCD-coded MCC and MNC. - * Digits are represented by 4-bit nibbles. Unused nibbles are filled with 0xF. - * MNC 001 represented as 0xF001 - * MNC 01 represented as 0xFF01 - * PLMN encoded as per TS 36.413 sec 9.2.3.8 - *****************************************************************************/ -inline void s1ap_plmn_to_mccmnc(uint32_t plmn, uint16_t *mcc, uint16_t *mnc) -{ - uint8_t nibbles[6]; - nibbles[0] = (plmn & 0xF00000) >> 20; - nibbles[1] = (plmn & 0x0F0000) >> 16; - nibbles[2] = (plmn & 0x00F000) >> 12; - nibbles[3] = (plmn & 0x000F00) >> 8; - nibbles[4] = (plmn & 0x0000F0) >> 4; - nibbles[5] = (plmn & 0x00000F); - - *mcc = 0xF000; - *mnc = 0xF000; - *mcc |= nibbles[1] << 8; // MCC digit 1 - *mcc |= nibbles[0] << 4; // MCC digit 2 - *mcc |= nibbles[3]; // MCC digit 3 - - if(nibbles[2] == 0xF) { - // 2-digit MNC - *mnc |= 0x0F00; // MNC digit 1 - *mnc |= nibbles[5] << 4; // MNC digit 2 - *mnc |= nibbles[4]; // MNC digit 3 - } else { - // 3-digit MNC - *mnc |= nibbles[5] << 8; // MNC digit 1 - *mnc |= nibbles[4] << 4; // MNC digit 2 - *mnc |= nibbles[2] ; // MNC digit 3 - } -} - -/****************************************************************************** - * Convert BCD-coded MCC and MNC to PLMN. - * Digits are represented by 4-bit nibbles. Unused nibbles are filled with 0xF. - * MNC 001 represented as 0xF001 - * MNC 01 represented as 0xFF01 - * PLMN encoded as per TS 36.413 sec 9.2.3.8 - *****************************************************************************/ -inline void s1ap_mccmnc_to_plmn(uint16_t mcc, uint16_t mnc, uint32_t *plmn) -{ - uint8_t nibbles[6]; - nibbles[1] = (mcc & 0x0F00) >> 8; // MCC digit 1 - nibbles[0] = (mcc & 0x00F0) >> 4; // MCC digit 2 - nibbles[3] = (mcc & 0x000F); // MCC digit 3 - - if((mnc & 0xFF00) == 0xFF00) { - // 2-digit MNC - nibbles[2] = 0x0F; // MNC digit 1 - nibbles[5] = (mnc & 0x00F0) >> 4; // MNC digit 2 - nibbles[4] = (mnc & 0x000F); // MNC digit 3 - } else { - // 3-digit MNC - nibbles[5] = (mnc & 0x0F00) >> 8; // MNC digit 1 - nibbles[4] = (mnc & 0x00F0) >> 4; // MNC digit 2 - nibbles[2] = (mnc & 0x000F); // MNC digit 3 - } - - *plmn = 0x000000; - *plmn |= nibbles[0] << 20; - *plmn |= nibbles[1] << 16; - *plmn |= nibbles[2] << 12; - *plmn |= nibbles[3] << 8; - *plmn |= nibbles[4] << 4; - *plmn |= nibbles[5]; -} - -/****************************************************************************** - * Safe conversions between byte buffers and integer types. - * Note: these don't perform endian conversion - use e.g. htonl/ntohl if required - *****************************************************************************/ - -inline void uint8_to_uint32(uint8_t *buf, uint32_t *i) -{ - *i = (uint32_t)buf[0] << 24 | - (uint32_t)buf[1] << 16 | - (uint32_t)buf[2] << 8 | - (uint32_t)buf[3]; -} - -inline void uint32_to_uint8(uint32_t i, uint8_t *buf) -{ - buf[0] = (i >> 24) & 0xFF; - buf[1] = (i >> 16) & 0xFF; - buf[2] = (i >> 8) & 0xFF; - buf[3] = i & 0xFF; -} - -inline void uint8_to_uint16(uint8_t *buf, uint16_t *i) -{ - *i = (uint32_t)buf[0] << 8 | - (uint32_t)buf[1]; -} - -inline void uint16_to_uint8(uint16_t i, uint8_t *buf) -{ - buf[0] = (i >> 8) & 0xFF; - buf[1] = i & 0xFF; -} - } // namespace srsenb #endif // SRSENB_COMMON_ENB_H diff --git a/srsenb/src/upper/rrc.cc b/srsenb/src/upper/rrc.cc index 2e481ce6e..b7c3ea7a0 100644 --- a/srsenb/src/upper/rrc.cc +++ b/srsenb/src/upper/rrc.cc @@ -29,10 +29,12 @@ #include "srsenb/hdr/upper/rrc.h" #include "srslte/srslte.h" #include "srslte/asn1/liblte_mme.h" - +#include "srslte/common/int_helpers.h" using srslte::byte_buffer_t; using srslte::bit_buffer_t; +using srslte::uint32_to_uint8; +using srslte::uint8_to_uint32; namespace srsenb { diff --git a/srsenb/src/upper/s1ap.cc b/srsenb/src/upper/s1ap.cc index 1e2b25987..e2faf83f0 100644 --- a/srsenb/src/upper/s1ap.cc +++ b/srsenb/src/upper/s1ap.cc @@ -26,6 +26,8 @@ #include "srsenb/hdr/upper/s1ap.h" #include "srsenb/hdr/upper/common_enb.h" +#include "srslte/common/bcd_helpers.h" +#include "srslte/common/int_helpers.h" #include #include @@ -36,6 +38,9 @@ #include #include //for inet_ntop() +using srslte::s1ap_mccmnc_to_plmn; +using srslte::uint32_to_uint8; + namespace srsenb{ bool s1ap::init(s1ap_args_t args_, rrc_interface_s1ap *rrc_, srslte::log *s1ap_log_) diff --git a/srsenb/test/upper/plmn_test.cc b/srsenb/test/upper/plmn_test.cc index c8e9bdd69..2f27febc3 100644 --- a/srsenb/test/upper/plmn_test.cc +++ b/srsenb/test/upper/plmn_test.cc @@ -10,6 +10,7 @@ #include #include "srsenb/hdr/upper/common_enb.h" #include "srslte/asn1/liblte_rrc.h" +#include "srslte/common/bcd_helpers.h" void rrc_plmn_test() { @@ -55,17 +56,17 @@ void s1ap_plmn_test() uint32_t plmn; // 2-digit MNC test - srsenb::s1ap_mccmnc_to_plmn(mcc, mnc, &plmn); + srslte::s1ap_mccmnc_to_plmn(mcc, mnc, &plmn); assert(plmn == 0x21F354); - srsenb::s1ap_plmn_to_mccmnc(plmn, &mcc, &mnc); + srslte::s1ap_plmn_to_mccmnc(plmn, &mcc, &mnc); assert(mcc == 0xF123); assert(mnc == 0xFF45); // 3-digit MNC test mnc = 0xF456; - srsenb::s1ap_mccmnc_to_plmn(mcc, mnc, &plmn); + srslte::s1ap_mccmnc_to_plmn(mcc, mnc, &plmn); assert(plmn == 0x216354); - srsenb::s1ap_plmn_to_mccmnc(plmn, &mcc, &mnc); + srslte::s1ap_plmn_to_mccmnc(plmn, &mcc, &mnc); assert(mcc == 0xF123); assert(mnc == 0xF456); }