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
24 namespace OHOS {
25 namespace Telephony {
26 ApnItem::ApnItem() = default;
27
28 ApnItem::~ApnItem() = default;
29
GetApnTypes() const30 std::vector<std::string> ApnItem::GetApnTypes() const
31 {
32 return apnTypes_;
33 }
34
MarkBadApn(bool badApn)35 void ApnItem::MarkBadApn(bool badApn)
36 {
37 badApn_ = badApn;
38 }
39
IsBadApn() const40 bool ApnItem::IsBadApn() const
41 {
42 return badApn_;
43 }
44
CanDealWithType(const std::string & type) const45 bool ApnItem::CanDealWithType(const std::string &type) const
46 {
47 for (std::string apnType : apnTypes_) {
48 transform(apnType.begin(), apnType.end(), apnType.begin(), ::tolower);
49 if (type == apnType) {
50 return true;
51 }
52 if ((type != DATA_CONTEXT_ROLE_IA) && (apnType == DATA_CONTEXT_ROLE_ALL)) {
53 return true;
54 }
55 }
56 return false;
57 }
58
MakeDefaultApn(const std::string & apnType)59 sptr<ApnItem> ApnItem::MakeDefaultApn(const std::string &apnType)
60 {
61 sptr<ApnItem> apnItem = std::make_unique<ApnItem>().release();
62 if (apnItem == nullptr) {
63 TELEPHONY_LOGE("apn is null");
64 return nullptr;
65 }
66 Attribute attr = {"", "46002", DATA_PROFILE_DEFAULT, "IPV4V6", "IPV4V6",
67 DEFAULT_AUTH_TYPE, "cmnet", "CMNET", "", "", false, "", "", "", false};
68 apnItem->apnTypes_ = CellularDataUtils::Split(apnType, ",");
69 apnItem->attr_ = attr;
70 if (strcpy_s(apnItem->attr_.types_, ALL_APN_ITEM_CHAR_LENGTH, apnType.c_str()) != EOK) {
71 TELEPHONY_LOGE("types_ copy fail");
72 return nullptr;
73 }
74 if (apnType == "mms") {
75 std::string apn = "cmwap";
76 if (strcpy_s(apnItem->attr_.apn_, ALL_APN_ITEM_CHAR_LENGTH, apn.c_str()) != EOK) {
77 TELEPHONY_LOGE("types_ copy fail");
78 return nullptr;
79 }
80 }
81 TELEPHONY_LOGI("type = %{public}s", apnItem->attr_.types_);
82 return apnItem;
83 }
84
MakeApn(const PdpProfile & apnData)85 sptr<ApnItem> ApnItem::MakeApn(const PdpProfile &apnData)
86 {
87 sptr<ApnItem> apnItem = std::make_unique<ApnItem>().release();
88 if (apnItem == nullptr) {
89 TELEPHONY_LOGE("apn is null");
90 return nullptr;
91 }
92 apnItem->apnTypes_ = CellularDataUtils::Split(apnData.apnTypes, ",");
93 TELEPHONY_LOGI("MakeApn apnTypes_ = %{public}s", apnData.apnTypes.c_str());
94 apnItem->attr_.profileId_ = apnData.profileId;
95 apnItem->attr_.authType_ = apnData.authType;
96 apnItem->attr_.isRoamingApn_ = apnData.isRoamingApn;
97 apnItem->attr_.isEdited_ = apnData.edited;
98 if (strcpy_s(apnItem->attr_.types_, ALL_APN_ITEM_CHAR_LENGTH, apnData.apnTypes.c_str()) != EOK) {
99 TELEPHONY_LOGE("types_ copy fail");
100 return nullptr;
101 }
102 std::string numeric = apnData.mcc + apnData.mnc;
103 if (strcpy_s(apnItem->attr_.numeric_, ALL_APN_ITEM_CHAR_LENGTH, numeric.c_str()) != EOK) {
104 TELEPHONY_LOGE("numeric_ copy fail");
105 return nullptr;
106 }
107 if (strcpy_s(apnItem->attr_.protocol_, ALL_APN_ITEM_CHAR_LENGTH, apnData.pdpProtocol.c_str()) != EOK) {
108 TELEPHONY_LOGE("protocol_ copy fail");
109 return nullptr;
110 }
111 if (strcpy_s(apnItem->attr_.roamingProtocol_, ALL_APN_ITEM_CHAR_LENGTH,
112 apnData.roamPdpProtocol.c_str()) != EOK) {
113 TELEPHONY_LOGE("roamingProtocol_ copy fail");
114 return nullptr;
115 }
116 if (strcpy_s(apnItem->attr_.apn_, ALL_APN_ITEM_CHAR_LENGTH, apnData.apn.c_str()) != EOK) {
117 TELEPHONY_LOGE("apn_ copy fail");
118 return nullptr;
119 }
120 if (strcpy_s(apnItem->attr_.apnName_, ALL_APN_ITEM_CHAR_LENGTH, apnData.profileName.c_str()) != EOK) {
121 TELEPHONY_LOGE("apnName_ copy fail");
122 return nullptr;
123 }
124 if (strcpy_s(apnItem->attr_.user_, ALL_APN_ITEM_CHAR_LENGTH, apnData.authUser.c_str()) != EOK) {
125 TELEPHONY_LOGE("user_ copy fail");
126 return nullptr;
127 }
128 if (strcpy_s(apnItem->attr_.password_, ALL_APN_ITEM_CHAR_LENGTH, apnData.authPwd.c_str()) != EOK) {
129 TELEPHONY_LOGE("password_ copy fail");
130 return nullptr;
131 }
132 return BuildOtherApnAttributes(apnItem, apnData);
133 }
134
BuildOtherApnAttributes(sptr<ApnItem> & apnItem,const PdpProfile & apnData)135 sptr<ApnItem> ApnItem::BuildOtherApnAttributes(sptr<ApnItem> &apnItem, const PdpProfile &apnData)
136 {
137 if (strcpy_s(apnItem->attr_.homeUrl_, ALL_APN_ITEM_CHAR_LENGTH, apnData.homeUrl.c_str()) != EOK) {
138 TELEPHONY_LOGE("homeUrl_ copy fail");
139 return nullptr;
140 }
141 if (strcpy_s(apnItem->attr_.proxyIpAddress_, ALL_APN_ITEM_CHAR_LENGTH, apnData.proxyIpAddress.c_str()) != EOK) {
142 TELEPHONY_LOGE("proxyIpAddress_ copy fail");
143 return nullptr;
144 }
145 if (strcpy_s(apnItem->attr_.mmsIpAddress_, ALL_APN_ITEM_CHAR_LENGTH, apnData.mmsIpAddress.c_str()) != EOK) {
146 TELEPHONY_LOGE("mmsIpAddress_ copy fail");
147 return nullptr;
148 }
149 TELEPHONY_LOGI("The APN name is:%{public}s", apnItem->attr_.apnName_);
150 return apnItem;
151 }
152 } // namespace Telephony
153 } // namespace OHOS