1 /*
2 * Copyright (C) 2021-2022 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 "sim_account_manager.h"
17
18 #include "string_ex.h"
19
20 namespace OHOS {
21 namespace Telephony {
SimAccountManager(std::shared_ptr<Telephony::ITelRilManager> telRilManager,std::shared_ptr<SimStateManager> simStateManager,std::shared_ptr<SimFileManager> simFileManager)22 SimAccountManager::SimAccountManager(std::shared_ptr<Telephony::ITelRilManager> telRilManager,
23 std::shared_ptr<SimStateManager> simStateManager, std::shared_ptr<SimFileManager> simFileManager)
24 : telRilManager_(telRilManager), simStateManager_(simStateManager), simFileManager_(simFileManager)
25 {
26 TELEPHONY_LOGI("SimAccountManager construct");
27 }
28
~SimAccountManager()29 SimAccountManager::~SimAccountManager()
30 {
31 if (simStateTracker_ != nullptr) {
32 simStateTracker_->UnRegisterForIccLoaded();
33 simStateTracker_->UnRegisterOpkeyLoaded();
34 simStateTracker_->UnregisterOperatorCacheDel();
35 simStateTracker_->UnRegisterOperatorConfigUpdate();
36 }
37 if (operatorConfigCache_ != nullptr) {
38 operatorConfigCache_->UnRegisterForIccChange();
39 }
40 }
41
Init(int32_t slotId)42 void SimAccountManager::Init(int32_t slotId)
43 {
44 TELEPHONY_LOGI("SimAccountManager::Init = %{public}d", slotId);
45 if ((telRilManager_ == nullptr) || (simFileManager_ == nullptr) || (simStateManager_ == nullptr)) {
46 TELEPHONY_LOGE("can not init simAccountManager");
47 return;
48 }
49 if (!IsValidSlotId(slotId)) {
50 TELEPHONY_LOGE("SimAccountManager::init SimAccountManager invalid slotId = %{public}d", slotId);
51 return;
52 }
53 operatorConfigCache_ =
54 std::make_shared<OperatorConfigCache>(std::weak_ptr<SimFileManager>(simFileManager_), simStateManager_, slotId);
55 if (operatorConfigCache_ == nullptr) {
56 TELEPHONY_LOGE("SimAccountManager::operatorConfigCache_ is null");
57 return;
58 }
59 operatorConfigCache_->RegisterForIccChange();
60 simStateTracker_ =
61 std::make_shared<SimStateTracker>(std::weak_ptr<SimFileManager>(simFileManager_), operatorConfigCache_, slotId);
62 if (simStateTracker_ == nullptr) {
63 TELEPHONY_LOGE("SimAccountManager::simStateTracker_ is null");
64 return;
65 }
66 simStateTracker_->RegisterForIccLoaded();
67 simStateTracker_->RegisterOpkeyLoaded();
68 simStateTracker_->RegisterOperatorCacheDel();
69 simStateTracker_->RegisterOperatorConfigUpdate();
70 }
71
GetOperatorConfigs(int32_t slotId,OHOS::Telephony::OperatorConfig & poc)72 int32_t SimAccountManager::GetOperatorConfigs(int32_t slotId, OHOS::Telephony::OperatorConfig &poc)
73 {
74 TELEPHONY_LOGD("SimAccountManager::GetOperatorConfigs");
75 if (operatorConfigCache_ == nullptr) {
76 TELEPHONY_LOGE("SimAccountManager::GetOperatorConfigs operatorConfigCache_ is null");
77 return TELEPHONY_ERR_LOCAL_PTR_NULL;
78 }
79 return operatorConfigCache_->GetOperatorConfigs(static_cast<int32_t>(slotId), poc);
80 }
81
UpdateOperatorConfigs(int32_t slotId)82 int32_t SimAccountManager::UpdateOperatorConfigs(int32_t slotId)
83 {
84 if (operatorConfigCache_ == nullptr) {
85 TELEPHONY_LOGE("operatorConfigCache_ is null");
86 return TELEPHONY_ERR_LOCAL_PTR_NULL;
87 }
88 return operatorConfigCache_->UpdateOperatorConfigs(slotId);
89 }
90
IsValidSlotId(int32_t slotId)91 bool SimAccountManager::IsValidSlotId(int32_t slotId)
92 {
93 int32_t count = SIM_SLOT_COUNT;
94 if ((slotId >= DEFAULT_SIM_SLOT_ID) && (slotId < count)) {
95 return true;
96 } else {
97 TELEPHONY_LOGE("SimAccountManager slotId is InValid = %{public}d", slotId);
98 return false;
99 }
100 }
101
IsValidSlotIdForDefault(int32_t slotId)102 bool SimAccountManager::IsValidSlotIdForDefault(int32_t slotId)
103 {
104 int32_t count = SIM_SLOT_COUNT;
105 if ((slotId >= DEFAULT_SIM_SLOT_ID_REMOVE) && (slotId < count)) {
106 return true;
107 } else {
108 return false;
109 }
110 }
111
HasOperatorPrivileges(const int32_t slotId,bool & hasOperatorPrivileges)112 int32_t SimAccountManager::HasOperatorPrivileges(const int32_t slotId, bool &hasOperatorPrivileges)
113 {
114 TELEPHONY_LOGD("SimAccountManager::HasOperatorPrivileges begin");
115 if (privilegeController_ != nullptr) {
116 return privilegeController_->HasOperatorPrivileges(hasOperatorPrivileges);
117 }
118 if ((telRilManager_ == nullptr) || (simStateManager_ == nullptr)) {
119 TELEPHONY_LOGE("has nullptr at telRilManager_ or simStateManager_");
120 return TELEPHONY_ERR_LOCAL_PTR_NULL;
121 }
122 auto controller = std::make_shared<IccOperatorPrivilegeController>(telRilManager_, simStateManager_);
123 if (controller == nullptr) {
124 TELEPHONY_LOGE("Make IccOperatorPrivilegeController fail!!");
125 return TELEPHONY_ERR_LOCAL_PTR_NULL;
126 }
127 controller->Init(slotId);
128 privilegeController_ = controller;
129 return controller->HasOperatorPrivileges(hasOperatorPrivileges);
130 }
131
UpdateImsCapFromChip(int32_t slotId,const ImsCapFromChip & imsCapFromChip)132 void SimAccountManager::UpdateImsCapFromChip(int32_t slotId, const ImsCapFromChip &imsCapFromChip)
133 {
134 if (operatorConfigCache_ == nullptr) {
135 TELEPHONY_LOGE("operatorConfigCache_ is null");
136 return;
137 }
138 operatorConfigCache_->UpdateImsCapFromChip(slotId, imsCapFromChip);
139 }
140 } // namespace Telephony
141 } // namespace OHOS
142