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

相关文章

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、方

利用Python脚本实现批量将图片转换为WebP格式

《利用Python脚本实现批量将图片转换为WebP格式》Python语言的简洁语法和库支持使其成为图像处理的理想选择,本文将介绍如何利用Python实现批量将图片转换为WebP格式的脚本,WebP作为... 目录简介1. python在图像处理中的应用2. WebP格式的原理和优势2.1 WebP格式与传统

java Long 与long之间的转换流程

《javaLong与long之间的转换流程》Long类提供了一些方法,用于在long和其他数据类型(如String)之间进行转换,本文将详细介绍如何在Java中实现Long和long之间的转换,感... 目录概述流程步骤1:将long转换为Long对象步骤2:将Longhttp://www.cppcns.c

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

在Java中将XLS转换为XLSX的实现方案

《在Java中将XLS转换为XLSX的实现方案》在本文中,我们将探讨传统ExcelXLS格式与现代XLSX格式的结构差异,并为Java开发者提供转换方案,通过了解底层原理、性能优势及实用工具,您将掌握... 目录为什么升级XLS到XLSX值得投入?实际转换过程解析推荐技术方案对比Apache POI实现编程

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

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