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

相关文章

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

Python中Json和其他类型相互转换的实现示例

《Python中Json和其他类型相互转换的实现示例》本文介绍了在Python中使用json模块实现json数据与dict、object之间的高效转换,包括loads(),load(),dumps()... 项目中经常会用到json格式转为object对象、dict字典格式等。在此做个记录,方便后续用到该方

使用Java读取本地文件并转换为MultipartFile对象的方法

《使用Java读取本地文件并转换为MultipartFile对象的方法》在许多JavaWeb应用中,我们经常会遇到将本地文件上传至服务器或其他系统的需求,在这种场景下,MultipartFile对象非... 目录1. 基本需求2. 自定义 MultipartFile 类3. 实现代码4. 代码解析5. 自定

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

Python学习笔记之getattr和hasattr用法示例详解

《Python学习笔记之getattr和hasattr用法示例详解》在Python中,hasattr()、getattr()和setattr()是一组内置函数,用于对对象的属性进行操作和查询,这篇文章... 目录1.getattr用法详解1.1 基本作用1.2 示例1.3 原理2.hasattr用法详解2.

Python进行JSON和Excel文件转换处理指南

《Python进行JSON和Excel文件转换处理指南》在数据交换与系统集成中,JSON与Excel是两种极为常见的数据格式,本文将介绍如何使用Python实现将JSON转换为格式化的Excel文件,... 目录将 jsON 导入为格式化 Excel将 Excel 导出为结构化 JSON处理嵌套 JSON:

Kotlin Map映射转换问题小结

《KotlinMap映射转换问题小结》文章介绍了Kotlin集合转换的多种方法,包括map(一对一转换)、mapIndexed(带索引)、mapNotNull(过滤null)、mapKeys/map... 目录Kotlin 集合转换:map、mapIndexed、mapNotNull、mapKeys、map

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方