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