srsLTE源码学习:BCD码转换mnc、mcc、plmn

2023-10-14 09:48

本文主要是介绍srsLTE源码学习:BCD码转换mnc、mcc、plmn,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Table of Contents

bcd_helpers.h  


  1. //PLMN(Public Land Mobile Network,公共陆地移动网络)
  2. //移动设备国家地区代码 ( Mobile country code / MCC ) 
  3. // MNC(Mobile Network Code,移动网络号码),

bcd_helpers.h  

 lib\include\srslte\common    9699    4/1/2019    212

/**** \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_BCD_HELPERS_H
#define SRSLTE_BCD_HELPERS_H#include <ctype.h>
#include <srslte/asn1/rrc_asn1.h>
#include <stdint.h>
#include <string>namespace srslte {/******************************************************************************* Convert between string and BCD-coded MCC.*  BCD码(Binary-Coded Decimal‎)亦称二进码十进数或二-十进制代码。*  用4位二进制数来表示1位十进制数中的0~9这10个数码。* Digits are represented by 4-bit nibbles. Unused nibbles are filled with 0xF.* MCC 001 results in 0xF001*****************************************************************************/
inline bool string_to_mcc(std::string str, uint16_t *mcc)
{uint32_t len = (uint32_t)str.size();if(len != 3) {return false;}if(!isdigit(str[0]) || !isdigit(str[1]) || !isdigit(str[2])) {return false;}*mcc = 0xF000;*mcc |= ((uint8_t)(str[0]-'0') << 8);*mcc |= ((uint8_t)(str[1]-'0') << 4);*mcc |= ((uint8_t)(str[2]-'0'));return true;
}inline bool mcc_to_string(uint16_t mcc, std::string *str)
{if((mcc & 0xF000) != 0xF000) {return false;}*str = "";*str += ((mcc & 0x0F00) >> 8) + '0';*str += ((mcc & 0x00F0) >> 4) + '0';*str += (mcc & 0x000F) + '0';return true;
}/******************************************************************************* Convert between array of bytes and BCD-coded MCC.* Digits are represented by 4-bit nibbles. Unused nibbles are filled with 0xF.* MCC 001 results in 0xF001*****************************************************************************/
//移动设备国家地区代码 ( Mobile country code / MCC ) 
inline bool bytes_to_mcc(uint8_t* bytes, uint16_t* mcc)
{*mcc = 0xF000;*mcc |= (((uint16_t)bytes[0]) << 8u);*mcc |= (((uint16_t)bytes[1]) << 4u);*mcc |= (uint16_t)bytes[2];return true;
}//移动设备国家地区代码 ( Mobile country code / MCC ) 
inline bool mcc_to_bytes(uint16_t mcc, uint8_t* bytes)
{if ((mcc & 0xF000) != 0xF000) {return false;}bytes[0] = (uint8_t)((mcc & 0xF00) >> 8);bytes[1] = (uint8_t)((mcc & 0x0F0) >> 4);bytes[2] = (uint8_t)(mcc & 0x00F);return true;
}inline std::string mcc_bytes_to_string(asn1::rrc::mcc_l mcc_bytes)
{std::string mcc_str;uint16_t    mcc;bytes_to_mcc(&mcc_bytes[0], &mcc);if (!mcc_to_string(mcc, &mcc_str)) {mcc_str = "000";}return mcc_str;
}/******************************************************************************* Convert between string and BCD-coded MNC.* Digits are represented by 4-bit nibbles. Unused nibbles are filled with 0xF.* MNC 001 results in 0xF001* MNC 01 results in 0xFF01*****************************************************************************/
inline bool string_to_mnc(std::string str, uint16_t *mnc)
{uint32_t len = str.size();if(len != 3 && len != 2) {return false;}if(len == 3) {if(!isdigit(str[0]) || !isdigit(str[1]) || !isdigit(str[2])) {return false;}*mnc = 0xF000;*mnc |= ((uint8_t)(str[0]-'0') << 8);*mnc |= ((uint8_t)(str[1]-'0') << 4);*mnc |= ((uint8_t)(str[2]-'0'));}if(len == 2) {if(!isdigit(str[0]) || !isdigit(str[1])) {return false;}*mnc = 0xFF00;*mnc |= ((uint8_t)(str[0]-'0') << 4);*mnc |= ((uint8_t)(str[1]-'0'));}return true;
}inline bool mnc_to_string(uint16_t mnc, std::string *str)
{if((mnc & 0xF000) != 0xF000) {return false;}*str = "";if((mnc & 0xFF00) != 0xFF00) {*str += ((mnc & 0x0F00) >> 8) + '0';}*str += ((mnc & 0x00F0) >> 4) + '0';*str += (mnc & 0x000F) + '0';return true;
}/******************************************************************************* Convert between array of bytes and BCD-coded MNC.* Digits are represented by 4-bit nibbles. Unused nibbles are filled with 0xF.* MNC 001 results in 0xF001* MNC 01 results in 0xFF01*****************************************************************************/
inline bool bytes_to_mnc(uint8_t* bytes, uint16_t* mnc, uint8_t len)
{if (len != 3 && len != 2) {*mnc = 0;return false;}if (len == 3) {*mnc = 0xF000;*mnc |= ((uint16_t)bytes[0]) << 8u;*mnc |= ((uint16_t)bytes[1]) << 4u;*mnc |= ((uint16_t)bytes[2]) << 0u;}if (len == 2) {*mnc = 0xFF00;*mnc |= ((uint16_t)bytes[0]) << 4u;*mnc |= ((uint16_t)bytes[1]) << 0u;}return true;
}inline bool mnc_to_bytes(uint16_t mnc, uint8_t* bytes, uint8_t* len)
{if ((mnc & 0xF000) != 0xF000) {*len = 0;return false;}uint8_t count = 0;if ((mnc & 0xFF00) != 0xFF00) {bytes[count++] = (mnc & 0xF00) >> 8u;}bytes[count++] = (mnc & 0x00F0) >> 4u;bytes[count++] = (mnc & 0x000F);*len           = count;return true;
}template <class Vec>
bool mnc_to_bytes(uint16_t mnc, Vec& vec)
{uint8_t len;uint8_t v[3];bool    ret = mnc_to_bytes(mnc, &v[0], &len);vec.resize(len);memcpy(&vec[0], &v[0], len);return ret;
}inline std::string mnc_bytes_to_string(asn1::rrc::mnc_l mnc_bytes)
{std::string mnc_str;uint16_t    mnc;bytes_to_mnc(&mnc_bytes[0], &mnc, mnc_bytes.size());if (!mnc_to_string(mnc, &mnc_str)) {mnc_str = "000";}return mnc_str;
}inline std::string plmn_id_to_string(asn1::rrc::plmn_id_s plmn_id)
{std::string mcc_str, mnc_str;uint16_t    mnc, mcc;bytes_to_mnc(&plmn_id.mnc[0], &mnc, plmn_id.mnc.size());bytes_to_mcc(&plmn_id.mcc[0], &mcc);mnc_to_string(mnc, &mnc_str);mcc_to_string(mcc, &mcc_str);return mcc_str + mnc_str;
}inline bool string_to_plmn_id(asn1::rrc::plmn_id_s& plmn, std::string mccmnc_str)
{if (mccmnc_str.size() < 5 or mccmnc_str.size() > 6) {return false;}uint16_t mnc, mcc;if (not string_to_mcc(std::string(mccmnc_str.begin(), mccmnc_str.begin() + 3), &mcc)) {return false;}if (not string_to_mnc(std::string(mccmnc_str.begin() + 3, mccmnc_str.end()), &mnc)) {return false;}plmn.mcc_present = true;if (not mcc_to_bytes(mcc, &plmn.mcc[0])) {return false;}return mnc_to_bytes(mnc, plmn.mnc);
}/******************************************************************************* 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)
{
//PLMN(Public Land Mobile Network,公共陆地移动网络)
//移动设备国家地区代码 ( Mobile country code / MCC ) 
// MNC(Mobile Network Code,移动网络号码),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 3if(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[2] << 8;  // MNC digit 1*mnc |= nibbles[5] << 4;  // MNC digit 2*mnc |= nibbles[4] ;      // 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)
{
//PLMN(Public Land Mobile Network,公共陆地移动网络)
//移动设备国家地区代码 ( Mobile country code / MCC ) 
// MNC(Mobile Network Code,移动网络号码),uint8_t nibbles[6];nibbles[1] = (mcc & 0x0F00) >> 8; // MCC digit 1nibbles[0] = (mcc & 0x00F0) >> 4; // MCC digit 2nibbles[3] = (mcc & 0x000F);      // MCC digit 3if((mnc & 0xFF00) == 0xFF00) {// 2-digit MNCnibbles[2] = 0x0F;                // MNC digit 1nibbles[5] = (mnc & 0x00F0) >> 4; // MNC digit 2nibbles[4] = (mnc & 0x000F);      // MNC digit 3} else {// 3-digit MNCnibbles[2] = (mnc & 0x0F00) >> 8; // MNC digit 1nibbles[5] = (mnc & 0x00F0) >> 4; // MNC digit 2nibbles[4] = (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];
}} // namespace srslte#endif // SRSLTE_BCD_HELPERS_H

 

 

 

这篇关于srsLTE源码学习:BCD码转换mnc、mcc、plmn的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/209717

相关文章

Python3脚本实现Excel与TXT的智能转换

《Python3脚本实现Excel与TXT的智能转换》在数据处理的日常工作中,我们经常需要将Excel中的结构化数据转换为其他格式,本文将使用Python3实现Excel与TXT的智能转换,需要的可以... 目录场景应用:为什么需要这种转换技术解析:代码实现详解核心代码展示改进点说明实战演练:从Excel到

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操

Java数字转换工具类NumberUtil的使用

《Java数字转换工具类NumberUtil的使用》NumberUtil是一个功能强大的Java工具类,用于处理数字的各种操作,包括数值运算、格式化、随机数生成和数值判断,下面就来介绍一下Number... 目录一、NumberUtil类概述二、主要功能介绍1. 数值运算2. 格式化3. 数值判断4. 随机

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

Python实现视频转换为音频的方法详解

《Python实现视频转换为音频的方法详解》这篇文章主要为大家详细Python如何将视频转换为音频并将音频文件保存到特定文件夹下,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. python需求的任务2. Python代码的实现3. 代码修改的位置4. 运行结果5. 注意事项

使用Python实现图片和base64转换工具

《使用Python实现图片和base64转换工具》这篇文章主要为大家详细介绍了如何使用Python中的base64模块编写一个工具,可以实现图片和Base64编码之间的转换,感兴趣的小伙伴可以了解下... 简介使用python的base64模块来实现图片和Base64编码之间的转换。可以将图片转换为Bas

Go中sync.Once源码的深度讲解

《Go中sync.Once源码的深度讲解》sync.Once是Go语言标准库中的一个同步原语,用于确保某个操作只执行一次,本文将从源码出发为大家详细介绍一下sync.Once的具体使用,x希望对大家有... 目录概念简单示例源码解读总结概念sync.Once是Go语言标准库中的一个同步原语,用于确保某个操

Linux使用dd命令来复制和转换数据的操作方法

《Linux使用dd命令来复制和转换数据的操作方法》Linux中的dd命令是一个功能强大的数据复制和转换实用程序,它以较低级别运行,通常用于创建可启动的USB驱动器、克隆磁盘和生成随机数据等任务,本文... 目录简介功能和能力语法常用选项示例用法基础用法创建可启动www.chinasem.cn的 USB 驱动

Python 标准库time时间的访问和转换问题小结

《Python标准库time时间的访问和转换问题小结》time模块为Python提供了处理时间和日期的多种功能,适用于多种与时间相关的场景,包括获取当前时间、格式化时间、暂停程序执行、计算程序运行时... 目录模块介绍使用场景主要类主要函数 - time()- sleep()- localtime()- g

JAVA中整型数组、字符串数组、整型数和字符串 的创建与转换的方法

《JAVA中整型数组、字符串数组、整型数和字符串的创建与转换的方法》本文介绍了Java中字符串、字符数组和整型数组的创建方法,以及它们之间的转换方法,还详细讲解了字符串中的一些常用方法,如index... 目录一、字符串、字符数组和整型数组的创建1、字符串的创建方法1.1 通过引用字符数组来创建字符串1.2