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 "apn_item.h"
17
18 #include <securec.h>
19
20 #include "telephony_log_wrapper.h"
21
22 #include "cellular_data_utils.h"
23 #include "pdp_profile_data.h"
24
25 namespace OHOS {
26 namespace Telephony {
27 ApnItem::ApnItem() = default;
28
29 ApnItem::~ApnItem() = default;
30
GetApnTypes() const31 std::vector<std::string> ApnItem::GetApnTypes() const
32 {
33 return apnTypes_;
34 }
35
MarkBadApn(bool badApn)36 void ApnItem::MarkBadApn(bool badApn)
37 {
38 badApn_ = badApn;
39 }
40
IsBadApn() const41 bool ApnItem::IsBadApn() const
42 {
43 return badApn_;
44 }
45
CanDealWithType(const std::string & type) const46 bool ApnItem::CanDealWithType(const std::string &type) const
47 {
48 for (std::string apnType : apnTypes_) {
49 transform(apnType.begin(), apnType.end(), apnType.begin(), ::tolower);
50 if (type == apnType) {
51 return true;
52 }
53 if (type == DATA_CONTEXT_ROLE_INTERNAL_DEFAULT && apnType == DATA_CONTEXT_ROLE_DEFAULT) {
54 return true;
55 }
56 if ((type != DATA_CONTEXT_ROLE_IA) && (apnType == DATA_CONTEXT_ROLE_ALL)) {
57 return true;
58 }
59 if (type == DATA_CONTEXT_ROLE_BIP && apnType == DATA_CONTEXT_ROLE_DEFAULT) {
60 return true;
61 }
62 }
63 return false;
64 }
65
MakeDefaultApn(const std::string & apnType)66 sptr<ApnItem> ApnItem::MakeDefaultApn(const std::string &apnType)
67 {
68 sptr<ApnItem> apnItem = std::make_unique<ApnItem>().release();
69 if (apnItem == nullptr) {
70 TELEPHONY_LOGE("apn is null");
71 return nullptr;
72 }
73 Attribute attr = {"", "46002", DATA_PROFILE_DEFAULT, "IPV4V6", "IPV4V6",
74 DEFAULT_AUTH_TYPE, "cmnet", "CMNET", "", "", false, "", "", "", false, "", 0, "", 0, 0};
75 apnItem->apnTypes_ = CellularDataUtils::Split(apnType, ",");
76 apnItem->attr_ = attr;
77 if (strcpy_s(apnItem->attr_.types_, ALL_APN_ITEM_CHAR_LENGTH, apnType.c_str()) != EOK) {
78 TELEPHONY_LOGE("types_ copy fail");
79 return nullptr;
80 }
81 if (apnType == "mms") {
82 std::string apn = "cmwap";
83 if (strcpy_s(apnItem->attr_.apn_, ALL_APN_ITEM_CHAR_LENGTH, apn.c_str()) != EOK) {
84 TELEPHONY_LOGE("types_ copy fail");
85 return nullptr;
86 }
87 }
88 TELEPHONY_LOGI("type = %{public}s", apnItem->attr_.types_);
89 return apnItem;
90 }
91
MakeApn(const PdpProfile & apnData)92 sptr<ApnItem> ApnItem::MakeApn(const PdpProfile &apnData)
93 {
94 sptr<ApnItem> apnItem = std::make_unique<ApnItem>().release();
95 if (apnItem == nullptr) {
96 TELEPHONY_LOGE("apn is null");
97 return nullptr;
98 }
99 apnItem->apnTypes_ = CellularDataUtils::Split(apnData.apnTypes, ",");
100 TELEPHONY_LOGI("MakeApn apnTypes_ = %{public}s", apnData.apnTypes.c_str());
101 apnItem->attr_.profileId_ = apnData.profileId;
102 apnItem->attr_.authType_ = apnData.authType;
103 apnItem->attr_.isRoamingApn_ = apnData.isRoamingApn;
104 apnItem->attr_.isEdited_ = apnData.edited;
105 if (strcpy_s(apnItem->attr_.types_, ALL_APN_ITEM_CHAR_LENGTH, apnData.apnTypes.c_str()) != EOK) {
106 TELEPHONY_LOGE("types_ copy fail");
107 return nullptr;
108 }
109 std::string numeric = apnData.mcc + apnData.mnc;
110 if (strcpy_s(apnItem->attr_.numeric_, ALL_APN_ITEM_CHAR_LENGTH, numeric.c_str()) != EOK) {
111 TELEPHONY_LOGE("numeric_ copy fail");
112 return nullptr;
113 }
114 if (strcpy_s(apnItem->attr_.protocol_, ALL_APN_ITEM_CHAR_LENGTH, apnData.pdpProtocol.c_str()) != EOK) {
115 TELEPHONY_LOGE("protocol_ copy fail");
116 return nullptr;
117 }
118 if (strcpy_s(apnItem->attr_.roamingProtocol_, ALL_APN_ITEM_CHAR_LENGTH,
119 apnData.roamPdpProtocol.c_str()) != EOK) {
120 TELEPHONY_LOGE("roamingProtocol_ copy fail");
121 return nullptr;
122 }
123 if (strcpy_s(apnItem->attr_.apn_, ALL_APN_ITEM_CHAR_LENGTH, apnData.apn.c_str()) != EOK) {
124 TELEPHONY_LOGE("apn_ copy fail");
125 return nullptr;
126 }
127 if (strcpy_s(apnItem->attr_.apnName_, ALL_APN_ITEM_CHAR_LENGTH, apnData.profileName.c_str()) != EOK) {
128 TELEPHONY_LOGE("apnName_ copy fail");
129 return nullptr;
130 }
131 if (strcpy_s(apnItem->attr_.user_, ALL_APN_ITEM_CHAR_LENGTH, apnData.authUser.c_str()) != EOK) {
132 TELEPHONY_LOGE("user_ copy fail");
133 return nullptr;
134 }
135 if (strcpy_s(apnItem->attr_.password_, ALL_APN_ITEM_CHAR_LENGTH, apnData.authPwd.c_str()) != EOK) {
136 TELEPHONY_LOGE("password_ copy fail");
137 return nullptr;
138 }
139 return BuildOtherApnAttributes(apnItem, apnData);
140 }
141
BuildOtherApnAttributes(sptr<ApnItem> & apnItem,const PdpProfile & apnData)142 sptr<ApnItem> ApnItem::BuildOtherApnAttributes(sptr<ApnItem> &apnItem, const PdpProfile &apnData)
143 {
144 if (strcpy_s(apnItem->attr_.homeUrl_, ALL_APN_ITEM_CHAR_LENGTH, apnData.homeUrl.c_str()) != EOK) {
145 TELEPHONY_LOGE("homeUrl_ copy fail");
146 return nullptr;
147 }
148 if (strcpy_s(apnItem->attr_.proxyIpAddress_, ALL_APN_ITEM_CHAR_LENGTH, apnData.proxyIpAddress.c_str()) != EOK) {
149 TELEPHONY_LOGE("proxyIpAddress_ copy fail");
150 return nullptr;
151 }
152 if (strcpy_s(apnItem->attr_.mmsIpAddress_, ALL_APN_ITEM_CHAR_LENGTH, apnData.mmsIpAddress.c_str()) != EOK) {
153 TELEPHONY_LOGE("mmsIpAddress_ copy fail");
154 return nullptr;
155 }
156 TELEPHONY_LOGI("The APN name is:%{public}s", apnItem->attr_.apnName_);
157 return apnItem;
158 }
159
IsSimilarPdpProfile(const PdpProfile & newPdpProfile,const PdpProfile & oldPdpProfile)160 bool ApnItem::IsSimilarPdpProfile(const PdpProfile &newPdpProfile, const PdpProfile &oldPdpProfile)
161 {
162 if ((newPdpProfile.apnTypes.find(DATA_CONTEXT_ROLE_DEFAULT) != std::string::npos) &&
163 (oldPdpProfile.apnTypes.find(DATA_CONTEXT_ROLE_DEFAULT) != std::string::npos)) {
164 return false;
165 }
166 return (newPdpProfile.apn == oldPdpProfile.apn) &&
167 IsSimilarProperty(newPdpProfile.proxyIpAddress, oldPdpProfile.proxyIpAddress) &&
168 IsSimilarProtocol(newPdpProfile.pdpProtocol, oldPdpProfile.pdpProtocol) &&
169 IsSimilarProtocol(newPdpProfile.roamPdpProtocol, oldPdpProfile.roamPdpProtocol) &&
170 (newPdpProfile.mvnoType == oldPdpProfile.mvnoType) &&
171 (newPdpProfile.mvnoMatchData == oldPdpProfile.mvnoMatchData) &&
172 IsSimilarProperty(newPdpProfile.homeUrl, oldPdpProfile.homeUrl) &&
173 IsSimilarProperty(newPdpProfile.mmsIpAddress, oldPdpProfile.mmsIpAddress);
174 }
175
IsSimilarProtocol(const std::string & newProtocol,const std::string & oldProtocol)176 bool ApnItem::IsSimilarProtocol(const std::string &newProtocol, const std::string &oldProtocol)
177 {
178 return (newProtocol == oldProtocol) ||
179 (newProtocol == PROTOCOL_IPV4V6 && oldProtocol == PROTOCOL_IPV4) ||
180 (newProtocol == PROTOCOL_IPV4V6 && oldProtocol == PROTOCOL_IPV6) ||
181 (newProtocol == PROTOCOL_IPV4 && oldProtocol == PROTOCOL_IPV4V6) ||
182 (newProtocol == PROTOCOL_IPV6 && oldProtocol == PROTOCOL_IPV4V6);
183 }
184 } // namespace Telephony
185 } // namespace OHOS