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", "", ""};
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("MakeDefaultApn: 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("MakeDefaultApn: types_ copy fail");
78 return nullptr;
79 }
80 }
81 TELEPHONY_LOGI("MakeDefaultApn: 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 apnItem->attr_.profileId_ = apnData.profileId;
94 apnItem->attr_.authType_ = apnData.authType;
95 if (strcpy_s(apnItem->attr_.types_, ALL_APN_ITEM_CHAR_LENGTH, apnData.apnTypes.c_str()) != EOK) {
96 TELEPHONY_LOGE("MakeApn: types_ copy fail");
97 return nullptr;
98 }
99 std::string numeric = apnData.mcc + apnData.mnc;
100 if (strcpy_s(apnItem->attr_.numeric_, ALL_APN_ITEM_CHAR_LENGTH, numeric.c_str()) != EOK) {
101 TELEPHONY_LOGE("MakeApn: types_ copy fail");
102 return nullptr;
103 }
104 if (strcpy_s(apnItem->attr_.protocol_, ALL_APN_ITEM_CHAR_LENGTH, apnData.pdpProtocol.c_str()) != EOK) {
105 TELEPHONY_LOGE("MakeApn: protocol_ copy fail");
106 return nullptr;
107 }
108 if (strcpy_s(apnItem->attr_.roamingProtocol_, ALL_APN_ITEM_CHAR_LENGTH,
109 apnData.roamPdpProtocol.c_str()) != EOK) {
110 TELEPHONY_LOGE("MakeApn: roamingProtocol_ copy fail");
111 return nullptr;
112 }
113 if (strcpy_s(apnItem->attr_.apn_, ALL_APN_ITEM_CHAR_LENGTH, apnData.apn.c_str()) != EOK) {
114 TELEPHONY_LOGE("MakeApn: apn_ copy fail");
115 return nullptr;
116 }
117 if (strcpy_s(apnItem->attr_.apnName_, ALL_APN_ITEM_CHAR_LENGTH, apnData.profileName.c_str()) != EOK) {
118 TELEPHONY_LOGE("MakeApn: apnName_ copy fail");
119 return nullptr;
120 }
121 if (strcpy_s(apnItem->attr_.user_, ALL_APN_ITEM_CHAR_LENGTH, apnData.authUser.c_str()) != EOK) {
122 TELEPHONY_LOGE("MakeApn: user_ copy fail");
123 return nullptr;
124 }
125 if (strcpy_s(apnItem->attr_.password_, ALL_APN_ITEM_CHAR_LENGTH, apnData.authPwd.c_str()) != EOK) {
126 TELEPHONY_LOGE("MakeApn: password_ copy fail");
127 return nullptr;
128 }
129 TELEPHONY_LOGI("MakeApn: The APN name is:%{public}s", apnItem->attr_.apnName_);
130 return apnItem;
131 }
132 } // namespace Telephony
133 } // namespace OHOS