From d562fa43d6f61c0a6ad4fb79e91c90cf571884f3 Mon Sep 17 00:00:00 2001 From: Serhii Kostiuk Date: Tue, 18 May 2021 17:55:03 +0300 Subject: [GP-1111] mPower R. Apr 2021: +CEMODE shall be set to CEMODE=2 Fixed carrier MCC/MNC table lookup for networks with two-digit conuntry codes. The original MCC/MNC population script used the following encoding for MCC/MNC values: - MCC - interpret the original value as hex; - 2-digit MNC - append "f" and interpret the resulting value as hex; - 3-digit MNC - interpret the original value as hex. So during lookup the system shall also use this conversion logic before querying the information from the table. --- scripts/create-mcc-mnc-table.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'scripts') diff --git a/scripts/create-mcc-mnc-table.py b/scripts/create-mcc-mnc-table.py index 761454c..cc30fef 100644 --- a/scripts/create-mcc-mnc-table.py +++ b/scripts/create-mcc-mnc-table.py @@ -43,9 +43,13 @@ print "}" print "" print "Json::Value MccMncTable::lookup(const std::string& sMcc, const std::string& sMnc) {" print " uint32_t iMcc, iMnc;" +print " std::string sNormalizedMnc = sMnc;" print ' printTrace("[MCCMNC] MCCx[%s] MNCx[%s]", sMcc.c_str(), sMnc.c_str());' -print " if(!MTS::Text::parseHex(iMcc, sMcc)) { return Json::Value::null; }" -print " if(!MTS::Text::parseHex(iMnc, sMnc)) { return Json::Value::null; }" +print " if (sMnc.length() == 2) {" +print " sNormalizedMnc += 'f';" +print " }" +print " if (!MTS::Text::parseHex(iMcc, sMcc)) { return Json::Value::null; }" +print " if (!MTS::Text::parseHex(iMnc, sNormalizedMnc)) { return Json::Value::null; }" print ' printTrace("[MCCMNC] MCC0X[%d] MNC0X[%d]", iMcc, iMnc);' print " if (m_mTable.count(iMcc)) {" print " if(m_mTable[iMcc].count(iMnc)) {" -- cgit v1.2.3 From e37ef41001d2f9de47efc66e4ea573b88aa972c6 Mon Sep 17 00:00:00 2001 From: Serhii Kostiuk Date: Fri, 21 May 2021 12:42:52 +0300 Subject: [GP-1111] mPower R. Apr 2021: +CEMODE shall be set to CEMODE=2 Ported the "generate_mcc_mnc_cpp.py" script to Python 3. Python 2 is not supported anymore and is not included to the modern distributions of Linux. The transformation is made mainly by `2to3` without refactoring and with only minimal changes required to support Python 3. --- scripts/create-mcc-mnc-table.py | 140 +++++++++++++++++++++------------------- 1 file changed, 72 insertions(+), 68 deletions(-) (limited to 'scripts') diff --git a/scripts/create-mcc-mnc-table.py b/scripts/create-mcc-mnc-table.py index cc30fef..a5af7c4 100644 --- a/scripts/create-mcc-mnc-table.py +++ b/scripts/create-mcc-mnc-table.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + #Generates MTS_IO_MccMncTable.cpp file by pulling latest MCC/MNC values #from http://mcc-mnc.com @@ -5,73 +7,75 @@ import re -import urllib2 +import urllib.request +import urllib.parse import datetime -print "/*!" -print " \\file MTS_IO_MccMncTable.cpp" -print " \\brief Auto-Generated MCC-MNC Lookup Table" -print " \\date " + str(datetime.date.today()) -print " \\author sgodinez" -print "" -print " An Auto-Generated MCC-MNC Lookup Table" -print "*/" -print "" -print "#include " -print "#include " -print "#include " -print "" -print "using namespace MTS::IO;" -print "" -print "MTS::AutoPtr MccMncTable::m_apLock(new MTS::Lock());" -print "MccMncTable* MccMncTable::m_pInstance = NULL;" -print "" -print "MccMncTable* MccMncTable::getInstance() {" -print " if(m_pInstance == NULL) {" -print " m_apLock->lock();" -print " if (m_pInstance == NULL) {" -print " m_pInstance = new MccMncTable();" -print " }" -print " m_apLock->unlock();" -print " }" -print " return m_pInstance;" -print "}" -print "" -print "MccMncTable::MccMncTable() {" -print " createTable();" -print "}" -print "" -print "Json::Value MccMncTable::lookup(const std::string& sMcc, const std::string& sMnc) {" -print " uint32_t iMcc, iMnc;" -print " std::string sNormalizedMnc = sMnc;" -print ' printTrace("[MCCMNC] MCCx[%s] MNCx[%s]", sMcc.c_str(), sMnc.c_str());' -print " if (sMnc.length() == 2) {" -print " sNormalizedMnc += 'f';" -print " }" -print " if (!MTS::Text::parseHex(iMcc, sMcc)) { return Json::Value::null; }" -print " if (!MTS::Text::parseHex(iMnc, sNormalizedMnc)) { return Json::Value::null; }" -print ' printTrace("[MCCMNC] MCC0X[%d] MNC0X[%d]", iMcc, iMnc);' -print " if (m_mTable.count(iMcc)) {" -print " if(m_mTable[iMcc].count(iMnc)) {" -print " std::vector vJson = MTS::Text::split(m_mTable[iMcc][iMnc], ',');" -print " Json::Value j;" -print ' j["iso"] = vJson[0];' -print ' j["country"] = vJson[1];' -print ' j["code"] = vJson[2];' -print ' j["carrier"] = vJson[3];' -print " return j;" -print " }" -print " }" -print "" -print " return Json::Value::null;" -print "}" -print "" -print "void MccMncTable::createTable() {" -print " std::string sData;" +print("/*!") +print(" \\file MTS_IO_MccMncTable.cpp") +print(" \\brief Auto-Generated MCC-MNC Lookup Table") +print(" \\date " + str(datetime.date.today())) +print(" \\author sgodinez") +print("") +print(" An Auto-Generated MCC-MNC Lookup Table") +print("*/") +print("") +print("#include ") +print("#include ") +print("#include ") +print("") +print("using namespace MTS::IO;") +print("") +print("MTS::AutoPtr MccMncTable::m_apLock(new MTS::Lock());") +print("MccMncTable* MccMncTable::m_pInstance = NULL;") +print("") +print("MccMncTable* MccMncTable::getInstance() {") +print(" if(m_pInstance == NULL) {") +print(" m_apLock->lock();") +print(" if (m_pInstance == NULL) {") +print(" m_pInstance = new MccMncTable();") +print(" }") +print(" m_apLock->unlock();") +print(" }") +print(" return m_pInstance;") +print("}") +print("") +print("MccMncTable::MccMncTable() {") +print(" createTable();") +print("}") +print("") +print("Json::Value MccMncTable::lookup(const std::string& sMcc, const std::string& sMnc) {") +print(" uint32_t iMcc, iMnc;") +print(" std::string sNormalizedMnc = sMnc;") +print(' printTrace("[MCCMNC] MCCx[%s] MNCx[%s]", sMcc.c_str(), sMnc.c_str());') +print(" if (sMnc.length() == 2) {") +print(" sNormalizedMnc += 'f';") +print(" }") +print(" if (!MTS::Text::parseHex(iMcc, sMcc)) { return Json::Value::null; }") +print(" if (!MTS::Text::parseHex(iMnc, sNormalizedMnc)) { return Json::Value::null; }") +print(' printTrace("[MCCMNC] MCC0X[%d] MNC0X[%d]", iMcc, iMnc);') +print(" if (m_mTable.count(iMcc)) {") +print(" if(m_mTable[iMcc].count(iMnc)) {") +print(" std::vector vJson = MTS::Text::split(m_mTable[iMcc][iMnc], ',');") +print(" Json::Value j;") +print(' j["iso"] = vJson[0];') +print(' j["country"] = vJson[1];') +print(' j["code"] = vJson[2];') +print(' j["carrier"] = vJson[3];') +print(" return j;") +print(" }") +print(" }") +print("") +print(" return Json::Value::null;") +print("}") +print("") +print("void MccMncTable::createTable() {") +print(" std::string sData;") td_re = re.compile('([^<]*)'*6) -html = urllib2.urlopen('http://mcc-mnc.com/').read() +html_bytes = urllib.request.urlopen('http://mcc-mnc.com/').read() # type: bytes +html = html_bytes.decode(encoding='utf-8') tbody_start = False for line in html.split('\n'): @@ -94,15 +98,15 @@ for line in html.split('\n'): mnc_int = int(mnc + 'f', 16) else: mnc_int = int(mnc, 16) - print ' m_mTable[' + str(mcc_int) + '][' + str(mnc_int) + '] = "' + \ - iso + ',' + countryCode + ',' + country + ',' + carrier + '";' - else: - print " //MCC(" + mcc + ') MNC(N/A) ISO(' + iso + ') Country Code(' + countryCode + ') Country(' + country + ') Carrier(' + carrier + ')' + print(' m_mTable[' + str(mcc_int) + '][' + str(mnc_int) + '] = "' + \ + iso + ',' + countryCode + ',' + country + ',' + carrier + '";') + else: + print(" //MCC(" + mcc + ') MNC(N/A) ISO(' + iso + ') Country Code(' + countryCode + ') Country(' + country + ') Carrier(' + carrier + ')') -print "}" -print "" +print("}") +print("") -- cgit v1.2.3 From 93e494dd3476133c81b019e96c277ddb71f6eb89 Mon Sep 17 00:00:00 2001 From: Serhii Kostiuk Date: Fri, 21 May 2021 18:45:41 +0300 Subject: [GP-1111] mPower R. Apr 2021: +CEMODE shall be set to CEMODE=2 Refactored the `create-mcc-mnc-table.py` script to support generating `MTS_IO_MccMncTable.cpp` from the website and (in the future) csv files. The previous script contained a lot of logic related to generating the MCC/MNC tables in C++ code: 1) printing constant data - functions, comments, etc; 2) formatting and printing the lines with MCC/MNC data; 3) converting MCC and MNC values to unique integers (hashes); 4) parsing the data from the website. The updated implementation of the script have separated step 4 from all the other steps which allows to extend the MCC/MNC building logic with loading MCC/MNC values from a `csv` file in the future. The behaviour of the script is NOT changed during refactoring. Both the old and the new version of the script generate exactly the same code when started on the same source data (website). --- scripts/create-mcc-mnc-table.py | 309 +++++++++++++++++++++++++++------------- 1 file changed, 210 insertions(+), 99 deletions(-) (limited to 'scripts') diff --git a/scripts/create-mcc-mnc-table.py b/scripts/create-mcc-mnc-table.py index a5af7c4..1f26fe4 100644 --- a/scripts/create-mcc-mnc-table.py +++ b/scripts/create-mcc-mnc-table.py @@ -1,114 +1,225 @@ #!/usr/bin/env python3 -#Generates MTS_IO_MccMncTable.cpp file by pulling latest MCC/MNC values -#from http://mcc-mnc.com +""" +Generates MTS_IO_MccMncTable.cpp file by pulling latest MCC/MNC values +from http://mcc-mnc.com or the csv file. -#Original Source Idea: https://github.com/musalbas/mcc-mnc-table +Original Source Idea: https://github.com/musalbas/mcc-mnc-table +""" +######################################################################################################################## import re import urllib.request import urllib.parse import datetime -print("/*!") -print(" \\file MTS_IO_MccMncTable.cpp") -print(" \\brief Auto-Generated MCC-MNC Lookup Table") -print(" \\date " + str(datetime.date.today())) -print(" \\author sgodinez") -print("") -print(" An Auto-Generated MCC-MNC Lookup Table") -print("*/") -print("") -print("#include ") -print("#include ") -print("#include ") -print("") -print("using namespace MTS::IO;") -print("") -print("MTS::AutoPtr MccMncTable::m_apLock(new MTS::Lock());") -print("MccMncTable* MccMncTable::m_pInstance = NULL;") -print("") -print("MccMncTable* MccMncTable::getInstance() {") -print(" if(m_pInstance == NULL) {") -print(" m_apLock->lock();") -print(" if (m_pInstance == NULL) {") -print(" m_pInstance = new MccMncTable();") -print(" }") -print(" m_apLock->unlock();") -print(" }") -print(" return m_pInstance;") -print("}") -print("") -print("MccMncTable::MccMncTable() {") -print(" createTable();") -print("}") -print("") -print("Json::Value MccMncTable::lookup(const std::string& sMcc, const std::string& sMnc) {") -print(" uint32_t iMcc, iMnc;") -print(" std::string sNormalizedMnc = sMnc;") -print(' printTrace("[MCCMNC] MCCx[%s] MNCx[%s]", sMcc.c_str(), sMnc.c_str());') -print(" if (sMnc.length() == 2) {") -print(" sNormalizedMnc += 'f';") -print(" }") -print(" if (!MTS::Text::parseHex(iMcc, sMcc)) { return Json::Value::null; }") -print(" if (!MTS::Text::parseHex(iMnc, sNormalizedMnc)) { return Json::Value::null; }") -print(' printTrace("[MCCMNC] MCC0X[%d] MNC0X[%d]", iMcc, iMnc);') -print(" if (m_mTable.count(iMcc)) {") -print(" if(m_mTable[iMcc].count(iMnc)) {") -print(" std::vector vJson = MTS::Text::split(m_mTable[iMcc][iMnc], ',');") -print(" Json::Value j;") -print(' j["iso"] = vJson[0];') -print(' j["country"] = vJson[1];') -print(' j["code"] = vJson[2];') -print(' j["carrier"] = vJson[3];') -print(" return j;") -print(" }") -print(" }") -print("") -print(" return Json::Value::null;") -print("}") -print("") -print("void MccMncTable::createTable() {") -print(" std::string sData;") - -td_re = re.compile('([^<]*)'*6) - -html_bytes = urllib.request.urlopen('http://mcc-mnc.com/').read() # type: bytes -html = html_bytes.decode(encoding='utf-8') - -tbody_start = False -for line in html.split('\n'): - if '' in line: - tbody_start = True - elif '' in line: - break - elif tbody_start: - td_search = td_re.search(line) - mcc = td_search.group(1).strip().replace(',','') - mnc = td_search.group(2).strip().replace(',','') - iso = td_search.group(3).strip().replace(',','') - countryCode = td_search.group(4).strip().replace(',','') - country = td_search.group(5).strip().replace(',','') - carrier = td_search.group(6).strip().replace(',','') - - if mnc != "n/a": - mcc_int = int(mcc, 16) - if len(mnc) == 2: - mnc_int = int(mnc + 'f', 16) - else: - mnc_int = int(mnc, 16) - print(' m_mTable[' + str(mcc_int) + '][' + str(mnc_int) + '] = "' + \ - iso + ',' + countryCode + ',' + country + ',' + carrier + '";') - else: - print(" //MCC(" + mcc + ') MNC(N/A) ISO(' + iso + ') Country Code(' + countryCode + ') Country(' + country + ') Carrier(' + carrier + ')') - - - -print("}") -print("") +from typing import Iterable, Generator +from collections import namedtuple + +######################################################################################################################## + +PREAMBLE_TEMPLATE = """\ +/* + * Copyright (C) 2015 by Multi-Tech Systems + * + * This file is part of libmts-io. + * + * libmts-io is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * libmts-io 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libmts-io. If not, see . + * + */ + +/*! + \\file MTS_IO_MccMncTable.cpp + \\brief Auto-Generated MCC-MNC Lookup Table + \\date {today} + \\author sgodinez + + An Auto-Generated MCC-MNC Lookup Table +*/ +""" + +GENERAL_CODE = """\ +#include +#include +#include + +using namespace MTS::IO; + +MTS::AutoPtr MccMncTable::m_apLock(new MTS::Lock()); +MccMncTable* MccMncTable::m_pInstance = NULL; + +MccMncTable* MccMncTable::getInstance() { + if(m_pInstance == NULL) { + m_apLock->lock(); + if (m_pInstance == NULL) { + m_pInstance = new MccMncTable(); + } + m_apLock->unlock(); + } + return m_pInstance; +} + +MccMncTable::MccMncTable() { + createTable(); +} + +Json::Value MccMncTable::lookup(const std::string& sMcc, const std::string& sMnc) { + uint32_t iMcc, iMnc; + std::string sNormalizedMnc = sMnc; + printTrace("[MCCMNC] MCCx[%s] MNCx[%s]", sMcc.c_str(), sMnc.c_str()); + if (sMnc.length() == 2) { + sNormalizedMnc += 'f'; + } + if (!MTS::Text::parseHex(iMcc, sMcc)) { return Json::Value::null; } + if (!MTS::Text::parseHex(iMnc, sNormalizedMnc)) { return Json::Value::null; } + printTrace("[MCCMNC] MCC0X[%d] MNC0X[%d]", iMcc, iMnc); + if (m_mTable.count(iMcc)) { + if(m_mTable[iMcc].count(iMnc)) { + std::vector vJson = MTS::Text::split(m_mTable[iMcc][iMnc], ','); + Json::Value j; + j["iso"] = vJson[0]; + j["country"] = vJson[1]; + j["code"] = vJson[2]; + j["carrier"] = vJson[3]; + return j; + } + } + + return Json::Value::null; +} +""" + +######################################################################################################################## + +MccMncElement = namedtuple( + 'MccMncData', + field_names=('mcc', 'mcc_int', 'mnc', 'mnc_int', 'iso', 'country', 'country_code', 'network') +) + + +######################################################################################################################## + + +def print_cpp_preamble() -> None: + print(PREAMBLE_TEMPLATE.format(today=datetime.date.today())) + + +def print_cpp_general_code() -> None: + print(GENERAL_CODE) + + +def format_mcc_mnc_line(el: MccMncElement) -> str: + if el.mnc.upper() != "N/A": + return ' m_mTable[{mcc_int}][{mnc_int}] = "{iso},{country},{country_code},{network}";'.format( + mcc_int=el.mcc_int, + mnc_int=el.mnc_int, + iso=el.iso, + country=el.country, + country_code=el.country_code, + network=el.network + ) + else: + # TODO: The Country and Country Code values were swapped in the original implementation due to a bug. + # Left as is for compatibility reasons. + return ' //MCC({mcc}) MNC(N/A) ISO({iso}) Country Code({country}) Country({country_code}) Carrier({network})'.format( + mcc=el.mcc, + iso=el.iso, + country=el.country, + country_code=el.country_code, + network=el.network + ) + + +def print_cpp_mcc_mnc_table(data: Iterable[MccMncElement]) -> None: + print("void MccMncTable::createTable() {") + print(" std::string sData;") + + for el in data: + print(format_mcc_mnc_line(el)) + + print("}") + print("") + + +def mcc_to_mcc_int(src: str) -> str: + if src.upper() == "N/A": + return src + + hash_ = int(src, 16) + return "{:d}".format(hash_) + + +def mnc_to_mnc_int(src: str) -> str: + if src.upper() == "N/A": + return src + + src_norm = src + if len(src) == 2: + src_norm += 'f' + + hash_ = int(src_norm, 16) + return "{:d}".format(hash_) + + +def mcc_mnc_from_website(url: str) -> Generator[MccMncElement, None, None]: + td_re = re.compile('([^<]*)' * 6) + html_bytes = urllib.request.urlopen(url).read() # type: bytes + html = html_bytes.decode(encoding='utf-8') + + tbody_start = False + + for line in html.split('\n'): + if '' in line: + tbody_start = True + elif '' in line: + break + elif tbody_start: + td_search = td_re.search(line) + mcc = td_search.group(1).strip().replace(',', '') + mnc = td_search.group(2).strip().replace(',', '') + iso = td_search.group(3).strip().replace(',', '') + country = td_search.group(4).strip().replace(',', '') + country_code = td_search.group(5).strip().replace(',', '') + carrier = td_search.group(6).strip().replace(',', '') + mcc_int = mcc_to_mcc_int(mcc) + mnc_int = mnc_to_mnc_int(mnc) + yield MccMncElement( + mcc=mcc, + mcc_int=mcc_int, + mnc=mnc, + mnc_int=mnc_int, + iso=iso, + country=country, + country_code=country_code, + network=carrier + ) +######################################################################################################################## +def main(): + print_cpp_preamble() + print_cpp_general_code() + print_cpp_mcc_mnc_table( + mcc_mnc_from_website('http://mcc-mnc.com/') + ) + + +######################################################################################################################## + +if __name__ == "__main__": + main() -- cgit v1.2.3 From f0931133aa7960d56767578ed5d8c24f66920a60 Mon Sep 17 00:00:00 2001 From: Serhii Kostiuk Date: Mon, 24 May 2021 10:55:59 +0300 Subject: [GP-1111] mPower R. Apr 2021: +CEMODE shall be set to CEMODE=2 Extended `create-mcc-mnc-table.py` for CSV support: - refactoring: renamed the 'network' variable to 'carrier' as it's named 'carrier' in the CPP code; - new feature: added support of command-line arguments to select the source between the website and CSV; the website is used by default for compatibility reasons. --- scripts/create-mcc-mnc-table.py | 69 ++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 11 deletions(-) (limited to 'scripts') diff --git a/scripts/create-mcc-mnc-table.py b/scripts/create-mcc-mnc-table.py index 1f26fe4..fdc9833 100644 --- a/scripts/create-mcc-mnc-table.py +++ b/scripts/create-mcc-mnc-table.py @@ -13,6 +13,8 @@ import re import urllib.request import urllib.parse import datetime +import csv +import argparse from typing import Iterable, Generator from collections import namedtuple @@ -105,12 +107,18 @@ Json::Value MccMncTable::lookup(const std::string& sMcc, const std::string& sMnc MccMncElement = namedtuple( 'MccMncData', - field_names=('mcc', 'mcc_int', 'mnc', 'mnc_int', 'iso', 'country', 'country_code', 'network') + field_names=('mcc', 'mcc_int', 'mnc', 'mnc_int', 'iso', 'country', 'country_code', 'carrier') ) ######################################################################################################################## +def init_argparse() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser(description='Generate MCC/MNC table file from Website or CSV') + parser.add_argument('-w', '--website', action='store_true') + parser.add_argument('-c', '--csv', type=str) + return parser + def print_cpp_preamble() -> None: print(PREAMBLE_TEMPLATE.format(today=datetime.date.today())) @@ -122,23 +130,23 @@ def print_cpp_general_code() -> None: def format_mcc_mnc_line(el: MccMncElement) -> str: if el.mnc.upper() != "N/A": - return ' m_mTable[{mcc_int}][{mnc_int}] = "{iso},{country},{country_code},{network}";'.format( + return ' m_mTable[{mcc_int}][{mnc_int}] = "{iso},{country},{country_code},{carrier}";'.format( mcc_int=el.mcc_int, mnc_int=el.mnc_int, iso=el.iso, country=el.country, country_code=el.country_code, - network=el.network + carrier=el.carrier ) else: # TODO: The Country and Country Code values were swapped in the original implementation due to a bug. # Left as is for compatibility reasons. - return ' //MCC({mcc}) MNC(N/A) ISO({iso}) Country Code({country}) Country({country_code}) Carrier({network})'.format( + return ' //MCC({mcc}) MNC(N/A) ISO({iso}) Country Code({country}) Country({country_code}) Carrier({carrier})'.format( mcc=el.mcc, iso=el.iso, country=el.country, country_code=el.country_code, - network=el.network + carrier=el.carrier ) @@ -205,21 +213,60 @@ def mcc_mnc_from_website(url: str) -> Generator[MccMncElement, None, None]: iso=iso, country=country, country_code=country_code, - network=carrier + carrier=carrier + ) + + +def mcc_mnc_from_csv(path: str) -> Generator[MccMncElement, None, None]: + with open(path) as f: + csv_reader = csv.DictReader(f) + for row in csv_reader: + mcc = row['MCC'] + mnc = row['MNC'] + iso = row['ISO'] + country = row['Country'] + country_code = row['Country Code'] + carrier = row['Carrier'] + + mcc_int = mcc_to_mcc_int(mcc) + mnc_int = mnc_to_mnc_int(mnc) + + yield MccMncElement( + mcc=mcc, + mcc_int=mcc_int, + mnc=mnc, + mnc_int=mnc_int, + iso=iso, + country=country, + country_code=country_code, + carrier=carrier ) ######################################################################################################################## -def main(): +def main() -> int: + parser = init_argparse() + args = parser.parse_args() + + if (args.csv is not None) and args.website: + parser.error('Only one source can be used at a time.') + return 1 + + if args.csv is None: + source = mcc_mnc_from_website('http://mcc-mnc.com/') + else: + source = mcc_mnc_from_csv(args.csv) + print_cpp_preamble() print_cpp_general_code() - print_cpp_mcc_mnc_table( - mcc_mnc_from_website('http://mcc-mnc.com/') - ) + print_cpp_mcc_mnc_table(source) + + return 0 ######################################################################################################################## if __name__ == "__main__": - main() + ret = main() + exit(ret) -- cgit v1.2.3 From adc19a08871964a23a27a20c384439776a52891f Mon Sep 17 00:00:00 2001 From: Serhii Kostiuk Date: Mon, 24 May 2021 11:59:45 +0300 Subject: [GP-1111] mPower R. Apr 2021: +CEMODE shall be set to CEMODE=2 Extended `create-mcc-mnc-table.py` to support writing the resulting CPP code directly to the target file: - refactoring: renamed `print_cpp_mcc_mnc_table` to `print_cpp_mcc_mnc_create_table` to better represent its purpose - printing the body of `MccMncTable::createTable()`; - refactoring: grouped all the `print_cpp_*` functions to the single `print_cpp` function; - new feature: printing the code directly to the target file without using stdout; printing to stdout is used by default for compatibility reasons. --- scripts/create-mcc-mnc-table.py | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) (limited to 'scripts') diff --git a/scripts/create-mcc-mnc-table.py b/scripts/create-mcc-mnc-table.py index fdc9833..c2aee14 100644 --- a/scripts/create-mcc-mnc-table.py +++ b/scripts/create-mcc-mnc-table.py @@ -16,7 +16,7 @@ import datetime import csv import argparse -from typing import Iterable, Generator +from typing import Iterable, Generator, Optional, TextIO from collections import namedtuple ######################################################################################################################## @@ -117,15 +117,16 @@ def init_argparse() -> argparse.ArgumentParser: parser = argparse.ArgumentParser(description='Generate MCC/MNC table file from Website or CSV') parser.add_argument('-w', '--website', action='store_true') parser.add_argument('-c', '--csv', type=str) + parser.add_argument('-t', '--target', type=str, default='-') return parser -def print_cpp_preamble() -> None: - print(PREAMBLE_TEMPLATE.format(today=datetime.date.today())) +def print_cpp_preamble(*, target: Optional[TextIO] = None) -> None: + print(PREAMBLE_TEMPLATE.format(today=datetime.date.today()), file=target) -def print_cpp_general_code() -> None: - print(GENERAL_CODE) +def print_cpp_general_code(*, target: Optional[TextIO] = None) -> None: + print(GENERAL_CODE, file=target) def format_mcc_mnc_line(el: MccMncElement) -> str: @@ -150,15 +151,21 @@ def format_mcc_mnc_line(el: MccMncElement) -> str: ) -def print_cpp_mcc_mnc_table(data: Iterable[MccMncElement]) -> None: - print("void MccMncTable::createTable() {") - print(" std::string sData;") +def print_cpp_mcc_mnc_create_table(source: Iterable[MccMncElement], *, target: Optional[TextIO] = None) -> None: + print("void MccMncTable::createTable() {", file=target) + print(" std::string sData;", file=target) - for el in data: - print(format_mcc_mnc_line(el)) + for el in source: + print(format_mcc_mnc_line(el), file=target) - print("}") - print("") + print("}", file=target) + print("", file=target) + + +def print_cpp(source: Iterable[MccMncElement], *, target: Optional[TextIO] = None) -> None: + print_cpp_preamble(target=target) + print_cpp_general_code(target=target) + print_cpp_mcc_mnc_create_table(source, target=target) def mcc_to_mcc_int(src: str) -> str: @@ -258,9 +265,13 @@ def main() -> int: else: source = mcc_mnc_from_csv(args.csv) - print_cpp_preamble() - print_cpp_general_code() - print_cpp_mcc_mnc_table(source) + if args.target == '-': + # Print to stdout + print_cpp(source, target=None) + else: + # Print to file + with open(args.target, 'w') as f: + print_cpp(source, target=f) return 0 -- cgit v1.2.3 From fa849130585bd56b94528bcc043c4bf05d3d2f40 Mon Sep 17 00:00:00 2001 From: Serhii Kostiuk Date: Mon, 24 May 2021 12:26:52 +0300 Subject: [GP-1111] mPower R. Apr 2021: +CEMODE shall be set to CEMODE=2 Restored the original table of MCC/MNC values that was used to populate the `MTS_IO_MccMncTable.cpp` file. This step is required to extend the format of MCC/MNC table with additional fields and re-generate the file based on new data instead of changing all the lines by hand. Also added a script that restores the MCC/MNC table from a CPP file. --- scripts/restore-mcc-mnc-from-cpp.py | 115 ++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 scripts/restore-mcc-mnc-from-cpp.py (limited to 'scripts') diff --git a/scripts/restore-mcc-mnc-from-cpp.py b/scripts/restore-mcc-mnc-from-cpp.py new file mode 100644 index 0000000..041be70 --- /dev/null +++ b/scripts/restore-mcc-mnc-from-cpp.py @@ -0,0 +1,115 @@ +#!/usr/bin/env python3 + +""" +Restores the original data that was used to populate the 'MTS_IO_MccMncTable.cpp' file. +Stores the result in a .csv file. +""" + +######################################################################################################################## + +import re +import csv +import argparse + +from typing import Dict, Iterable, Generator + + +######################################################################################################################## + +def init_argparse() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser(description='Generate MCC/MNC table file from Website or CSV') + parser.add_argument('source', type=str) + parser.add_argument('target', type=str) + return parser + + +def mcc_int_to_mcc(src: str) -> str: + hash_ = int(src) + return "{:03x}".format(hash_) + + +def mnc_int_to_mnc(src: str) -> str: + hash_ = int(src) + hash_str = "{:03x}".format(hash_) + + if hash_ & 0xf != 0xf: + return hash_str + else: + return hash_str[:-1] + + +def parse_code_line(line: str) -> Dict[str, str]: + left, right = line.split(' = ') + mcc_int, mnc_int = re.findall(r'\[(\d+)\]', left) + info = re.findall(r'"(.+)"', right)[0] + iso, country, country_code, carrier = info.split(',') + + mcc = mcc_int_to_mcc(mcc_int) + mnc = mnc_int_to_mnc(mnc_int) + + return { + 'MCC': mcc, + 'MNC': mnc, + 'ISO': iso, + 'Country': country, + 'Country Code': country_code, + 'Carrier': carrier + } + + +def parse_comment_line(line: str) -> Dict[str, str]: + match = re.search( + r'MCC\((.*)\) MNC\((.*)\) ISO\((.*)\) Country Code\((.*)\) Country\((.*)\) Carrier\((.*)\)', + line + ) + + # NOTE: The original file contains values of the "Code" and "Country" fields swapped. + mcc, mnc, iso, country, country_code, carrier = match.groups() + + return { + 'MCC': mcc, + 'MNC': mnc, + 'ISO': iso, + 'Country': country, + 'Country Code': country_code, + 'Carrier': carrier + } + + +def mcc_mnc_from_cpp(src: str) -> Generator[Dict[str, str], None, None]: + with open(src) as f: + for line in f: + if line.startswith(" //MCC"): + yield parse_comment_line(line) + + elif line.startswith(" m_mTable["): + yield parse_code_line(line) + + +def write_mcc_mnc_to_scv(data: Iterable[Dict[str, str]], target: str) -> None: + with open(target, 'w') as f: + fieldnames = ['MCC', 'MNC', 'ISO', 'Country', 'Country Code', 'Carrier'] + writer = csv.DictWriter(f, fieldnames, delimiter=',', quotechar='"', lineterminator='\n', quoting=csv.QUOTE_ALL) + writer.writeheader() + + for d in data: + writer.writerow(d) + + +######################################################################################################################## + +def main() -> int: + parser = init_argparse() + args = parser.parse_args() + + data = mcc_mnc_from_cpp(args.source) + write_mcc_mnc_to_scv(data, args.target) + + return 0 + + +######################################################################################################################## + +if __name__ == "__main__": + ret = main() + exit(ret) -- cgit v1.2.3 From d3766835ac77eb2f7eb4530e9aa4cc0d437f27e5 Mon Sep 17 00:00:00 2001 From: Serhii Kostiuk Date: Mon, 24 May 2021 22:28:35 +0300 Subject: [GP-1111] mPower R. Apr 2021: +CEMODE shall be set to CEMODE=2 Added the 'Carrier Code' field to the MCC/MNC table. The new field is supposed to be a constant Multitech-specific indentifier that is constant between the firmware versions and does not necessarily depends on a carrier name. In particular the new field is needed to detect when there is an AT&T SIM card inserted and 'UE mode of operation' shall be set to CS/PS mode 2. --- scripts/create-mcc-mnc-table.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'scripts') diff --git a/scripts/create-mcc-mnc-table.py b/scripts/create-mcc-mnc-table.py index c2aee14..c7afb56 100644 --- a/scripts/create-mcc-mnc-table.py +++ b/scripts/create-mcc-mnc-table.py @@ -95,6 +95,7 @@ Json::Value MccMncTable::lookup(const std::string& sMcc, const std::string& sMnc j["country"] = vJson[1]; j["code"] = vJson[2]; j["carrier"] = vJson[3]; + j["carrierCode"] = vJson[4]; return j; } } @@ -107,7 +108,7 @@ Json::Value MccMncTable::lookup(const std::string& sMcc, const std::string& sMnc MccMncElement = namedtuple( 'MccMncData', - field_names=('mcc', 'mcc_int', 'mnc', 'mnc_int', 'iso', 'country', 'country_code', 'carrier') + field_names=('mcc', 'mcc_int', 'mnc', 'mnc_int', 'iso', 'country', 'country_code', 'carrier', 'carrier_code') ) @@ -131,23 +132,25 @@ def print_cpp_general_code(*, target: Optional[TextIO] = None) -> None: def format_mcc_mnc_line(el: MccMncElement) -> str: if el.mnc.upper() != "N/A": - return ' m_mTable[{mcc_int}][{mnc_int}] = "{iso},{country},{country_code},{carrier}";'.format( + return ' m_mTable[{mcc_int}][{mnc_int}] = "{iso},{country},{country_code},{carrier},{carrier_code}";'.format( mcc_int=el.mcc_int, mnc_int=el.mnc_int, iso=el.iso, country=el.country, country_code=el.country_code, - carrier=el.carrier + carrier=el.carrier, + carrier_code=el.carrier_code ) else: # TODO: The Country and Country Code values were swapped in the original implementation due to a bug. # Left as is for compatibility reasons. - return ' //MCC({mcc}) MNC(N/A) ISO({iso}) Country Code({country}) Country({country_code}) Carrier({carrier})'.format( + return ' //MCC({mcc}) MNC(N/A) ISO({iso}) Country Code({country}) Country({country_code}) Carrier({carrier}) Carrier Code({carrier_code})'.format( mcc=el.mcc, iso=el.iso, country=el.country, country_code=el.country_code, - carrier=el.carrier + carrier=el.carrier, + carrier_code=el.carrier_code ) @@ -220,7 +223,8 @@ def mcc_mnc_from_website(url: str) -> Generator[MccMncElement, None, None]: iso=iso, country=country, country_code=country_code, - carrier=carrier + carrier=carrier, + carrier_code="" # Multitech-specific, not populated from this source ) @@ -234,6 +238,7 @@ def mcc_mnc_from_csv(path: str) -> Generator[MccMncElement, None, None]: country = row['Country'] country_code = row['Country Code'] carrier = row['Carrier'] + carrier_code = row['Carrier Code'] mcc_int = mcc_to_mcc_int(mcc) mnc_int = mnc_to_mnc_int(mnc) @@ -246,7 +251,8 @@ def mcc_mnc_from_csv(path: str) -> Generator[MccMncElement, None, None]: iso=iso, country=country, country_code=country_code, - carrier=carrier + carrier=carrier, + carrier_code=carrier_code ) -- cgit v1.2.3