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

相关文章

Python使用FFmpeg实现高效音频格式转换工具

《Python使用FFmpeg实现高效音频格式转换工具》在数字音频处理领域,音频格式转换是一项基础但至关重要的功能,本文主要为大家介绍了Python如何使用FFmpeg实现强大功能的图形化音频转换工具... 目录概述功能详解软件效果展示主界面布局转换过程截图完成提示开发步骤详解1. 环境准备2. 项目功能结

使用Python实现网页表格转换为markdown

《使用Python实现网页表格转换为markdown》在日常工作中,我们经常需要从网页上复制表格数据,并将其转换成Markdown格式,本文将使用Python编写一个网页表格转Markdown工具,需... 在日常工作中,我们经常需要从网页上复制表格数据,并将其转换成Markdown格式,以便在文档、邮件或

8种快速易用的Python Matplotlib数据可视化方法汇总(附源码)

《8种快速易用的PythonMatplotlib数据可视化方法汇总(附源码)》你是否曾经面对一堆复杂的数据,却不知道如何让它们变得直观易懂?别慌,Python的Matplotlib库是你数据可视化的... 目录引言1. 折线图(Line Plot)——趋势分析2. 柱状图(Bar Chart)——对比分析3

Python将字符串转换为小写字母的几种常用方法

《Python将字符串转换为小写字母的几种常用方法》:本文主要介绍Python中将字符串大写字母转小写的四种方法:lower()方法简洁高效,手动ASCII转换灵活可控,str.translate... 目录一、使用内置方法 lower()(最简单)二、手动遍历 + ASCII 码转换三、使用 str.tr

Java如何将文件内容转换为MD5哈希值

《Java如何将文件内容转换为MD5哈希值》:本文主要介绍Java如何将文件内容转换为MD5哈希值的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java文件内容转换为MD5哈希值一个完整的Java示例代码代码解释注意事项总结Java文件内容转换为MD5

使用Java将实体类转换为JSON并输出到控制台的完整过程

《使用Java将实体类转换为JSON并输出到控制台的完整过程》在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用JSON格式,用Java将实体类转换为J... 在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用j

Java实现视频格式转换的完整指南

《Java实现视频格式转换的完整指南》在Java中实现视频格式的转换,通常需要借助第三方工具或库,因为视频的编解码操作复杂且性能需求较高,以下是实现视频格式转换的常用方法和步骤,需要的朋友可以参考下... 目录核心思路方法一:通过调用 FFmpeg 命令步骤示例代码说明优点方法二:使用 Jaffree(FF

重新对Java的类加载器的学习方式

《重新对Java的类加载器的学习方式》:本文主要介绍重新对Java的类加载器的学习方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、简介1.2、符号引用和直接引用1、符号引用2、直接引用3、符号转直接的过程2、加载流程3、类加载的分类3.1、显示

C语言中的常见进制转换详解(从二进制到十六进制)

《C语言中的常见进制转换详解(从二进制到十六进制)》进制转换是计算机编程中的一个常见任务,特别是在处理低级别的数据操作时,C语言作为一门底层编程语言,在进制转换方面提供了灵活的操作方式,今天,我们将深... 目录1、进制基础2、C语言中的进制转换2.1 从十进制转换为其他进制十进制转二进制十进制转八进制十进

Pandas进行周期与时间戳转换的方法

《Pandas进行周期与时间戳转换的方法》本教程将深入讲解如何在pandas中使用to_period()和to_timestamp()方法,完成时间戳与周期之间的转换,并结合实际应用场景展示这些方法的... 目录to_period() 时间戳转周期基本操作应用示例to_timestamp() 周期转时间戳基