• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "cellular_data_utils.h"
17 
18 #include "telephony_common_utils.h"
19 #include "telephony_log_wrapper.h"
20 #include <charconv>
21 
22 namespace OHOS {
23 namespace Telephony {
24 using namespace NetManagerStandard;
ParseIpAddr(const std::string & address)25 std::vector<AddressInfo> CellularDataUtils::ParseIpAddr(const std::string &address)
26 {
27     std::vector<AddressInfo> ipInfoArray;
28     std::vector<std::string> ipArray = Split(address, " ");
29     for (std::string &ipItem: ipArray) {
30         AddressInfo ipInfo;
31         std::string flag = (ipItem.find('.') == std::string::npos) ? ":" : ".";
32         std::vector<std::string> ipData = Split(ipItem, "/");
33         if (ipData.size() == 0) {
34             TELEPHONY_LOGE("ParseIpAddr ipData is empty");
35             continue;
36         }
37         ipInfo.ip = ipData[0];
38         if (flag == ".") {
39             std::vector<std::string> ipSubData = Split(ipInfo.ip, flag);
40             ipInfo.type = (ipSubData.size() > MIN_IPV4_ITEM) ? INetAddr::IpType::IPV6 : INetAddr::IpType::IPV4;
41             ipInfo.prefixLen = (ipSubData.size() > MIN_IPV4_ITEM) ? IPV6_BIT : IPV4_BIT;
42         } else {
43             ipInfo.type = INetAddr::IpType::IPV6;
44             ipInfo.prefixLen = IPV6_BIT;
45         }
46         if ((ipData.size() >= VALID_IP_SIZE) && IsValidDecValue(ipData[1].c_str())) {
47             ConvertStrToUint(ipData[1].c_str(), ipInfo.prefixLen);
48         }
49         ipInfoArray.push_back(ipInfo);
50     }
51     return ipInfoArray;
52 }
53 
ParseNormalIpAddr(const std::string & address)54 std::vector<AddressInfo> CellularDataUtils::ParseNormalIpAddr(const std::string &address)
55 {
56     std::vector<AddressInfo> ipInfoArray;
57     std::vector<std::string> ipArray = Split(address, " ");
58     for (size_t i = 0; i < ipArray.size(); ++i) {
59         AddressInfo ipInfo;
60         if (ipArray[i].find(':') == std::string::npos) {
61             ipInfo.prefixLen = IPV4_BIT;
62             ipInfo.type = INetAddr::IpType::IPV4;
63         } else {
64             ipInfo.prefixLen = IPV6_BIT;
65             ipInfo.type = INetAddr::IpType::IPV6;
66         }
67         ipInfo.ip = ipArray[i];
68         ipInfoArray.push_back(ipInfo);
69     }
70     return ipInfoArray;
71 }
72 
ParseRoute(const std::string & address)73 std::vector<RouteInfo> CellularDataUtils::ParseRoute(const std::string &address)
74 {
75     std::vector<RouteInfo> routeInfoArray;
76     std::vector<std::string> routeArray = Split(address, " ");
77     for (size_t i = 0; i < routeArray.size(); ++i) {
78         RouteInfo route;
79         if (routeArray[i].find(':') == std::string::npos) {
80             route.type = INetAddr::IpType::IPV4;
81             route.destination = ROUTED_IPV4;
82         } else {
83             route.type = INetAddr::IpType::IPV6;
84             route.destination = ROUTED_IPV6;
85         }
86         route.ip = routeArray[i];
87         routeInfoArray.push_back(route);
88     }
89     return routeInfoArray;
90 }
91 
Split(const std::string & input,const std::string & flag)92 std::vector<std::string> CellularDataUtils::Split(const std::string &input, const std::string &flag)
93 {
94     std::vector<std::string> vec;
95     if (input.empty()) {
96         TELEPHONY_LOGE("input is null");
97         return vec;
98     }
99     std::string::size_type start = 0;
100     std::string::size_type pos = 0;
101     while ((pos = input.find(flag, start)) != std::string::npos) {
102         vec.push_back(input.substr(start, pos - start));
103         start = pos + flag.size();
104     }
105     if (start != input.size()) {
106         vec.push_back(input.substr(start, input.size() - start));
107     }
108     return vec;
109 }
110 
GetPrefixLen(const std::string & netmask,const std::string & flag)111 int32_t CellularDataUtils::GetPrefixLen(const std::string &netmask, const std::string& flag)
112 {
113     std::vector<std::string> mask = Split(netmask, flag);
114     return GetPrefixLen(mask, 0);
115 }
116 
GetPrefixLen(const std::vector<std::string> & netmask,const size_t start)117 int32_t CellularDataUtils::GetPrefixLen(const std::vector<std::string> &netmask, const size_t start)
118 {
119     int32_t prefixLen = 0;
120     for (size_t i = start; i < netmask.size(); ++i) {
121         if (!IsValidDecValue(netmask[i].c_str())) {
122             break;
123         }
124         int32_t value = 0;
125         ConvertStrToInt(netmask[i].c_str(), value);
126         int32_t maskValue = ((static_cast<uint32_t>(value)) & 0x00FF);
127         if (maskValue == 0) {
128             break;
129         }
130         while ((maskValue & 0x80) != 0) {
131             prefixLen++;
132             maskValue = (maskValue << 1);
133         }
134         if ((prefixLen % MASK_BYTE_BIT) != 0) {
135             break;
136         }
137     }
138     return prefixLen;
139 }
140 
GetDefaultMobileMtuConfig()141 int CellularDataUtils::GetDefaultMobileMtuConfig()
142 {
143     char mobile_mtu[MIN_BUFFER_SIZE] = {0};
144     GetParameter(CONFIG_MOBILE_MTU, DEFAULT_MOBILE_MTU, mobile_mtu, MIN_BUFFER_SIZE);
145     return std::atoi(mobile_mtu);
146 }
147 
GetDefaultPreferApnConfig()148 bool CellularDataUtils::GetDefaultPreferApnConfig()
149 {
150     char preferApn[MIN_BUFFER_SIZE] = {0};
151     GetParameter(CONFIG_PREFERAPN, DEFAULT_PREFER_APN, preferApn, MIN_BUFFER_SIZE);
152     return std::atoi(preferApn);
153 }
154 
GetDefaultMultipleConnectionsConfig()155 bool CellularDataUtils::GetDefaultMultipleConnectionsConfig()
156 {
157     char connections[MIN_BUFFER_SIZE] = {0};
158     GetParameter(CONFIG_MULTIPLE_CONNECTIONS, DEFAULT_MULTIPLE_CONNECTIONS, connections, MIN_BUFFER_SIZE);
159     return std::atoi(connections) > 0 ? true : false;
160 }
161 
ConvertRadioTechToRadioName(const int32_t radioTech)162 std::string CellularDataUtils::ConvertRadioTechToRadioName(const int32_t radioTech)
163 {
164     std::string radioName = "unknown";
165     switch (radioTech) {
166         case static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_UNKNOWN):
167             radioName = "unknown";
168             break;
169         case static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_GSM):
170             radioName = "EDGE";
171             break;
172         case static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_1XRTT):
173             radioName = "1xRTT";
174             break;
175         case static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_WCDMA):
176             radioName = "UMTS";
177             break;
178         case static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_HSPA):
179             radioName = "HSPA";
180             break;
181         case static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_HSPAP):
182             radioName = "HSPAP";
183             break;
184         case static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_TD_SCDMA):
185             radioName = "TD-SCDMA";
186             break;
187         case static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_EVDO):
188             radioName = "EVDO";
189             break;
190         case static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_EHRPD):
191             radioName = "eHRPD";
192             break;
193         case static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_LTE):
194             radioName = "LTE";
195             break;
196         case static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_LTE_CA):
197             radioName = "LTE_CA";
198             break;
199         case static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_IWLAN):
200             radioName = "IWAN";
201             break;
202         case static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_NR):
203             radioName = "NR";
204             break;
205         default:
206             break;
207     }
208     return radioName;
209 }
210 
ConvertStrToInt(const std::string & str,int32_t & value)211 bool CellularDataUtils::ConvertStrToInt(const std::string& str, int32_t& value)
212 {
213     if (str.empty()) {
214         return false;
215     }
216     auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), value, 10);  // 10: 十进制
217     bool succ = ec == std::errc{} && ptr == str.data() + str.size();
218     if (!succ) {
219         TELEPHONY_LOGE("ConvertStrToInt failed: str: %{public}s", str.c_str());
220     }
221     return succ;
222 }
223 
ConvertStrToUint(const std::string & str,uint8_t & value)224 bool CellularDataUtils::ConvertStrToUint(const std::string& str, uint8_t& value)
225 {
226     if (str.empty()) {
227         return false;
228     }
229     auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), value, 10);  // 10: 十进制
230     bool succ = ec == std::errc{} && ptr == str.data() + str.size();
231     if (!succ) {
232         TELEPHONY_LOGE("ConvertStrToInt failed: str: %{public}s", str.c_str());
233     }
234     return succ;
235 }
236 } // namespace Telephony
237 } // namespace OHOS