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_manager.h"
17
18 #include "cellular_data_utils.h"
19 #include "core_manager_inner.h"
20 #include "net_specifier.h"
21 #include "string_ex.h"
22 #include "tel_profile_util.h"
23 #include "telephony_log_wrapper.h"
24
25 namespace OHOS {
26 namespace Telephony {
27 const std::map<std::string, int32_t> ApnManager::apnIdApnNameMap_ {
28 {DATA_CONTEXT_ROLE_ALL, DATA_CONTEXT_ROLE_ALL_ID},
29 {DATA_CONTEXT_ROLE_DEFAULT, DATA_CONTEXT_ROLE_DEFAULT_ID},
30 {DATA_CONTEXT_ROLE_MMS, DATA_CONTEXT_ROLE_MMS_ID},
31 {DATA_CONTEXT_ROLE_SUPL, DATA_CONTEXT_ROLE_SUPL_ID},
32 {DATA_CONTEXT_ROLE_DUN, DATA_CONTEXT_ROLE_DUN_ID},
33 {DATA_CONTEXT_ROLE_IMS, DATA_CONTEXT_ROLE_IMS_ID},
34 {DATA_CONTEXT_ROLE_IA, DATA_CONTEXT_ROLE_IA_ID},
35 {DATA_CONTEXT_ROLE_EMERGENCY, DATA_CONTEXT_ROLE_EMERGENCY_ID}
36 };
37 constexpr const char *CT_MCC_MNC_1 = "46003";
38 constexpr const char *CT_MCC_MNC_2 = "46011";
39 constexpr const char *GC_ICCID = "8985231";
40 constexpr const char *GC_MCC_MNC = "45431";
41 constexpr const char *GC_SPN = "CTExcel";
42 constexpr const char *MO_ICCID_1 = "8985302";
43 constexpr const char *MO_ICCID_2 = "8985307";
44 constexpr const char *MO_UNICOM_MCCMNC = "46001";
45 constexpr int32_t ICCID_LEN_MINIMUM = 7;
46
47 ApnManager::ApnManager() = default;
48
49 ApnManager::~ApnManager() = default;
50
InitApnHolders()51 void ApnManager::InitApnHolders()
52 {
53 AddApnHolder(DATA_CONTEXT_ROLE_DEFAULT, static_cast<int32_t>(DataContextPriority::PRIORITY_LOW));
54 AddApnHolder(DATA_CONTEXT_ROLE_MMS, static_cast<int32_t>(DataContextPriority::PRIORITY_NORMAL));
55 }
56
FindApnHolderById(const int32_t id) const57 sptr<ApnHolder> ApnManager::FindApnHolderById(const int32_t id) const
58 {
59 if (apnIdApnHolderMap_.empty()) {
60 TELEPHONY_LOGE("apnIdApnHolderMap_ empty");
61 return nullptr;
62 }
63 if (id == DATA_CONTEXT_ROLE_INVALID_ID) {
64 TELEPHONY_LOGE("find a invalid capability!");
65 return nullptr;
66 }
67 std::map<int32_t, sptr<ApnHolder>>::const_iterator it = apnIdApnHolderMap_.find(id);
68 if (it != apnIdApnHolderMap_.end()) {
69 return it->second;
70 }
71 return nullptr;
72 }
73
FindApnIdByApnName(const std::string & type)74 int32_t ApnManager::FindApnIdByApnName(const std::string &type)
75 {
76 std::map<std::string, int32_t>::const_iterator it = apnIdApnNameMap_.find(type);
77 if (it != apnIdApnNameMap_.end()) {
78 return it->second;
79 }
80 TELEPHONY_LOGE("apnType %{public}s is not exist!", type.c_str());
81 return DATA_CONTEXT_ROLE_INVALID_ID;
82 }
83
FindApnNameByApnId(const int32_t id)84 std::string ApnManager::FindApnNameByApnId(const int32_t id)
85 {
86 for (std::pair<const std::string, int32_t> it : apnIdApnNameMap_) {
87 if (it.second == id) {
88 return it.first;
89 }
90 }
91 TELEPHONY_LOGI("apnId %{public}d is not exist!", id);
92 return DATA_CONTEXT_ROLE_DEFAULT;
93 }
94
FindApnIdByCapability(const uint64_t capability)95 int32_t ApnManager::FindApnIdByCapability(const uint64_t capability)
96 {
97 switch (capability) {
98 case NetManagerStandard::NetCap::NET_CAPABILITY_INTERNET:
99 return DATA_CONTEXT_ROLE_DEFAULT_ID;
100 case NetManagerStandard::NetCap::NET_CAPABILITY_MMS:
101 return DATA_CONTEXT_ROLE_MMS_ID;
102 default:
103 return DATA_CONTEXT_ROLE_INVALID_ID;
104 }
105 }
106
AddApnHolder(const std::string & apnType,const int32_t priority)107 void ApnManager::AddApnHolder(const std::string &apnType, const int32_t priority)
108 {
109 int32_t apnId = FindApnIdByApnName(apnType);
110 if (apnId == DATA_CONTEXT_ROLE_INVALID_ID) {
111 TELEPHONY_LOGE("APN INVALID ID");
112 return;
113 }
114 sptr<ApnHolder> apnHolder = std::make_unique<ApnHolder>(apnType, priority).release();
115 if (apnHolder == nullptr) {
116 TELEPHONY_LOGE("apnHolder is null, type: %{public}s", apnType.c_str());
117 return;
118 }
119 apnHolder->SetApnState(PROFILE_STATE_IDLE);
120 apnHolders_.push_back(apnHolder);
121 apnIdApnHolderMap_.insert(std::pair<int32_t, sptr<ApnHolder>>(apnId, apnHolder));
122 sortedApnHolders_.emplace_back(apnHolder);
123 sort(sortedApnHolders_.begin(), sortedApnHolders_.end(),
124 [](const sptr<ApnHolder> &c1, const sptr<ApnHolder> &c2) {
125 return c2->GetPriority() - c1->GetPriority();
126 });
127 TELEPHONY_LOGI("The Apn holder type:%{public}s, size:%{public}zu", apnType.c_str(), sortedApnHolders_.size());
128 }
129
GetApnHolder(const std::string & apnType) const130 sptr<ApnHolder> ApnManager::GetApnHolder(const std::string &apnType) const
131 {
132 int32_t apnId = FindApnIdByApnName(apnType);
133 if (DATA_CONTEXT_ROLE_INVALID_ID == apnId) {
134 TELEPHONY_LOGE("APN INVALID ID");
135 return nullptr;
136 }
137 std::map<int32_t, sptr<ApnHolder>>::const_iterator it = apnIdApnHolderMap_.find(apnId);
138 if (it != apnIdApnHolderMap_.end()) {
139 return it->second;
140 }
141 TELEPHONY_LOGE("apnType %{public}s is not exist!", apnType.c_str());
142 return nullptr;
143 }
144
GetAllApnHolder() const145 std::vector<sptr<ApnHolder>> ApnManager::GetAllApnHolder() const
146 {
147 return apnHolders_;
148 }
149
GetSortApnHolder() const150 std::vector<sptr<ApnHolder>> ApnManager::GetSortApnHolder() const
151 {
152 return sortedApnHolders_;
153 }
154
CreateAllApnItem()155 void ApnManager::CreateAllApnItem()
156 {
157 std::lock_guard<std::mutex> lock(mutex_);
158 allApnItem_.clear();
159 sptr<ApnItem> defaultApnItem = ApnItem::MakeDefaultApn(DATA_CONTEXT_ROLE_DEFAULT);
160 if (defaultApnItem != nullptr) {
161 allApnItem_.push_back(defaultApnItem);
162 }
163 sptr<ApnItem> mmsApnItem = ApnItem::MakeDefaultApn(DATA_CONTEXT_ROLE_MMS);
164 if (mmsApnItem != nullptr) {
165 allApnItem_.push_back(mmsApnItem);
166 }
167 sptr<ApnItem> suplApnItem = ApnItem::MakeDefaultApn(DATA_CONTEXT_ROLE_SUPL);
168 if (suplApnItem != nullptr) {
169 allApnItem_.push_back(suplApnItem);
170 }
171 sptr<ApnItem> dunApnItem = ApnItem::MakeDefaultApn(DATA_CONTEXT_ROLE_DUN);
172 if (dunApnItem != nullptr) {
173 allApnItem_.push_back(dunApnItem);
174 }
175 sptr<ApnItem> imsApnItem = ApnItem::MakeDefaultApn(DATA_CONTEXT_ROLE_IMS);
176 if (imsApnItem != nullptr) {
177 allApnItem_.push_back(imsApnItem);
178 }
179 sptr<ApnItem> iaApnItem = ApnItem::MakeDefaultApn(DATA_CONTEXT_ROLE_IA);
180 if (iaApnItem != nullptr) {
181 allApnItem_.push_back(iaApnItem);
182 }
183 sptr<ApnItem> emergencyApnItem = ApnItem::MakeDefaultApn(DATA_CONTEXT_ROLE_EMERGENCY);
184 if (emergencyApnItem != nullptr) {
185 allApnItem_.push_back(emergencyApnItem);
186 }
187 }
188
CreateAllApnItemByDatabase(int32_t slotId)189 int32_t ApnManager::CreateAllApnItemByDatabase(int32_t slotId)
190 {
191 int32_t count = 0;
192 std::u16string operatorNumeric;
193 CoreManagerInner::GetInstance().GetSimOperatorNumeric(slotId, operatorNumeric);
194 std::string numeric = Str16ToStr8(operatorNumeric);
195 GetCTOperator(slotId, numeric);
196 if (numeric.empty()) {
197 TELEPHONY_LOGE("numeric is empty!!!");
198 return count;
199 }
200 TELEPHONY_LOGI("current slotId = %{public}d, numeric = %{public}s", slotId, numeric.c_str());
201 auto helper = CellularDataRdbHelper::GetInstance();
202 if (helper == nullptr) {
203 TELEPHONY_LOGE("get cellularDataRdbHelper failed");
204 return count;
205 }
206 preferId_ = INVALID_PROFILE_ID;
207 std::vector<PdpProfile> preferApnVec;
208 if (helper->QueryPreferApn(slotId, preferApnVec)) {
209 preferId_ = preferApnVec[0].profileId;
210 TELEPHONY_LOGI("query preferId_ = %{public}d", preferId_);
211 }
212 std::string mcc = numeric.substr(0, DEFAULT_MCC_SIZE);
213 std::string mnc = numeric.substr(mcc.size(), numeric.size() - mcc.size());
214 int32_t mvnoCount = CreateMvnoApnItems(slotId, mcc, mnc);
215 if (mvnoCount > 0) {
216 return mvnoCount;
217 }
218 std::vector<PdpProfile> apnVec;
219 if (!helper->QueryApns(mcc, mnc, apnVec)) {
220 TELEPHONY_LOGE("query apns from data ability fail");
221 return count;
222 }
223 return MakeSpecificApnItem(apnVec);
224 }
225
GetCTOperator(int32_t slotId,std::string & numeric)226 void ApnManager::GetCTOperator(int32_t slotId, std::string &numeric)
227 {
228 bool isCTSimCard = false;
229 CoreManagerInner::GetInstance().IsCTSimCard(slotId, isCTSimCard);
230 if (isCTSimCard && numeric.compare(CT_MCC_MNC_2)) {
231 numeric = CT_MCC_MNC_1;
232 }
233 if (!numeric.compare(CT_MCC_MNC_1)) {
234 std::u16string tempIccId;
235 CoreManagerInner::GetInstance().GetSimIccId(slotId, tempIccId);
236 std::string iccId = Str16ToStr8(tempIccId);
237 std::u16string tempSpn;
238 CoreManagerInner::GetInstance().GetSimSpn(slotId, tempSpn);
239 std::string spn = Str16ToStr8(tempSpn);
240 if (!iccId.compare(0, ICCID_LEN_MINIMUM, GC_ICCID) || !spn.compare(GC_SPN)) {
241 numeric = GC_MCC_MNC;
242 } else if (!iccId.compare(0, ICCID_LEN_MINIMUM, MO_ICCID_1) ||
243 !iccId.compare(0, ICCID_LEN_MINIMUM, MO_ICCID_2)) {
244 std::u16string tempPlmn;
245 CoreManagerInner::GetInstance().GetSimOperatorNumeric(slotId, tempPlmn);
246 std::string plmn = Str16ToStr8(tempPlmn);
247 if (!plmn.empty() && !plmn.compare(MO_UNICOM_MCCMNC)) {
248 numeric = MO_UNICOM_MCCMNC;
249 }
250 }
251 }
252 }
253
CreateMvnoApnItems(int32_t slotId,const std::string & mcc,const std::string & mnc)254 int32_t ApnManager::CreateMvnoApnItems(int32_t slotId, const std::string &mcc, const std::string &mnc)
255 {
256 int32_t count = 0;
257 auto helper = CellularDataRdbHelper::GetInstance();
258 if (helper == nullptr) {
259 TELEPHONY_LOGE("get cellularDataRdbHelper failed");
260 return count;
261 }
262 std::vector<PdpProfile> mvnoApnVec;
263 std::u16string spn;
264 CoreManagerInner::GetInstance().GetSimSpn(slotId, spn);
265 if (!helper->QueryMvnoApnsByType(mcc, mnc, MvnoType::SPN, Str16ToStr8(spn), mvnoApnVec)) {
266 TELEPHONY_LOGE("query mvno apns by spn fail");
267 return count;
268 }
269 std::u16string imsi;
270 CoreManagerInner::GetInstance().GetIMSI(slotId, imsi);
271 if (!helper->QueryMvnoApnsByType(mcc, mnc, MvnoType::IMSI, Str16ToStr8(imsi), mvnoApnVec)) {
272 TELEPHONY_LOGE("query mvno apns by imsi fail");
273 return count;
274 }
275 std::u16string gid1;
276 CoreManagerInner::GetInstance().GetSimGid1(slotId, gid1);
277 if (!helper->QueryMvnoApnsByType(mcc, mnc, MvnoType::GID1, Str16ToStr8(gid1), mvnoApnVec)) {
278 TELEPHONY_LOGE("query mvno apns by gid1 fail");
279 return count;
280 }
281 std::u16string iccId;
282 CoreManagerInner::GetInstance().GetSimIccId(slotId, iccId);
283 if (!helper->QueryMvnoApnsByType(mcc, mnc, MvnoType::ICCID, Str16ToStr8(iccId), mvnoApnVec)) {
284 TELEPHONY_LOGE("query mvno apns by iccId fail");
285 return count;
286 }
287 return MakeSpecificApnItem(mvnoApnVec);
288 }
289
MakeSpecificApnItem(std::vector<PdpProfile> & apnVec)290 int32_t ApnManager::MakeSpecificApnItem(std::vector<PdpProfile> &apnVec)
291 {
292 std::lock_guard<std::mutex> lock(mutex_);
293 allApnItem_.clear();
294 int32_t count = 0;
295 for (PdpProfile &apnData : apnVec) {
296 TELEPHONY_LOGI("profileId = %{public}d, profileName = %{public}s, mvnoType = %{public}s", apnData.profileId,
297 apnData.profileName.c_str(), apnData.mvnoType.c_str());
298 if (apnData.profileId == preferId_ && apnData.apnTypes.empty()) {
299 apnData.apnTypes = DATA_CONTEXT_ROLE_DEFAULT;
300 }
301 sptr<ApnItem> apnItem = ApnItem::MakeApn(apnData);
302 if (apnItem != nullptr) {
303 allApnItem_.push_back(apnItem);
304 count++;
305 }
306 }
307 int32_t preferId = preferId_;
308 auto it = std::find_if(allApnItem_.begin(), allApnItem_.end(),
309 [preferId](auto &apn) { return apn != nullptr && apn->attr_.profileId_ == preferId; });
310 if (it != allApnItem_.end()) {
311 sptr<ApnItem> apnItem = *it;
312 allApnItem_.erase(it);
313 allApnItem_.insert(allApnItem_.begin(), apnItem);
314 }
315 return count;
316 }
317
FilterMatchedApns(const std::string & requestApnType)318 std::vector<sptr<ApnItem>> ApnManager::FilterMatchedApns(const std::string &requestApnType)
319 {
320 std::lock_guard<std::mutex> lock(mutex_);
321 std::vector<sptr<ApnItem>> matchApnItemList;
322 for (const sptr<ApnItem> &apnItem : allApnItem_) {
323 if (apnItem->CanDealWithType(requestApnType)) {
324 matchApnItemList.push_back(apnItem);
325 }
326 }
327 TELEPHONY_LOGI("apn size is :%{public}zu", matchApnItemList.size());
328 return matchApnItemList;
329 }
330
IsDataConnectionNotUsed(const std::shared_ptr<CellularDataStateMachine> & stateMachine) const331 bool ApnManager::IsDataConnectionNotUsed(const std::shared_ptr<CellularDataStateMachine> &stateMachine) const
332 {
333 if (stateMachine == nullptr) {
334 TELEPHONY_LOGE("CellularDataHandler:stateMachine is null");
335 return false;
336 }
337 for (const sptr<ApnHolder> &apnHolder : apnHolders_) {
338 if (apnHolder == nullptr) {
339 TELEPHONY_LOGE("apn holder is null");
340 continue;
341 }
342 std::shared_ptr<CellularDataStateMachine> cellularDataStateMachine = apnHolder->GetCellularDataStateMachine();
343 if (cellularDataStateMachine != nullptr && stateMachine == cellularDataStateMachine) {
344 TELEPHONY_LOGE("cellularDataStateMachine in use");
345 return false;
346 }
347 }
348 return true;
349 }
350
HasAnyConnectedState() const351 bool ApnManager::HasAnyConnectedState() const
352 {
353 for (const sptr<ApnHolder> &apnHolder : apnHolders_) {
354 if (apnHolder == nullptr) {
355 TELEPHONY_LOGE("apn holder is null");
356 continue;
357 }
358 if (apnHolder->GetApnState() == ApnProfileState::PROFILE_STATE_CONNECTED ||
359 apnHolder->GetApnState() == ApnProfileState::PROFILE_STATE_DISCONNECTING) {
360 return true;
361 }
362 }
363 return false;
364 }
365
GetOverallApnState() const366 ApnProfileState ApnManager::GetOverallApnState() const
367 {
368 if (apnHolders_.empty()) {
369 TELEPHONY_LOGE("apn overall state is STATE_IDLE");
370 return ApnProfileState::PROFILE_STATE_IDLE;
371 }
372 if (HasAnyConnectedState()) {
373 TELEPHONY_LOGI("apn overall state is STATE_CONNECTED");
374 return ApnProfileState::PROFILE_STATE_CONNECTED;
375 }
376 bool failState = false;
377 for (const sptr<ApnHolder> &apnHolder : apnHolders_) {
378 if (apnHolder == nullptr) {
379 continue;
380 }
381 if (apnHolder->GetApnState() == ApnProfileState::PROFILE_STATE_CONNECTING ||
382 apnHolder->GetApnState() == ApnProfileState::PROFILE_STATE_RETRYING) {
383 TELEPHONY_LOGI("apn overall state is STATE_CONNECTING");
384 return ApnProfileState::PROFILE_STATE_CONNECTING;
385 } else if (apnHolder->GetApnState() == ApnProfileState::PROFILE_STATE_IDLE) {
386 failState = true;
387 }
388 }
389 if (failState) {
390 TELEPHONY_LOGD("apn overall state is STATE_IDLE");
391 return ApnProfileState::PROFILE_STATE_IDLE;
392 }
393 TELEPHONY_LOGI("apn overall state is STATE_FAILED");
394 return ApnProfileState::PROFILE_STATE_FAILED;
395 }
396
GetRilAttachApn()397 sptr<ApnItem> ApnManager::GetRilAttachApn()
398 {
399 std::lock_guard<std::mutex> lock(mutex_);
400 if (allApnItem_.empty()) {
401 TELEPHONY_LOGE("apn item is null");
402 return nullptr;
403 }
404 sptr<ApnItem> attachApn = nullptr;
405 for (const sptr<ApnItem> &apnItem : allApnItem_) {
406 if (apnItem->CanDealWithType(DATA_CONTEXT_ROLE_IA)) {
407 attachApn = apnItem;
408 break;
409 }
410 if (attachApn == nullptr && apnItem->CanDealWithType(DATA_CONTEXT_ROLE_DEFAULT)) {
411 attachApn = apnItem;
412 }
413 }
414 if (attachApn == nullptr) {
415 attachApn = allApnItem_[0];
416 }
417 return attachApn;
418 }
419
ResetApns()420 bool ApnManager::ResetApns()
421 {
422 auto helper = CellularDataRdbHelper::GetInstance();
423 if (helper == nullptr) {
424 TELEPHONY_LOGE("get cellularDataRdbHelper failed");
425 return false;
426 }
427 return helper->ResetApns();
428 }
429 } // namespace Telephony
430 } // namespace OHOS
431