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     }
36     if (operatorConfigCache_ != nullptr) {
37         operatorConfigCache_->UnRegisterForIccChange();
38     }
39 }
40 
Init(int32_t slotId)41 void SimAccountManager::Init(int32_t slotId)
42 {
43     TELEPHONY_LOGI("SimAccountManager::Init = %{public}d", slotId);
44     if ((telRilManager_ == nullptr) || (simFileManager_ == nullptr) || (simStateManager_ == nullptr)) {
45         TELEPHONY_LOGE("can not init simAccountManager");
46         return;
47     }
48     if (!IsValidSlotId(slotId)) {
49         TELEPHONY_LOGE("SimAccountManager::init SimAccountManager invalid slotId = %{public}d", slotId);
50         return;
51     }
52     operatorConfigCache_ =
53         std::make_shared<OperatorConfigCache>(std::weak_ptr<SimFileManager>(simFileManager_), slotId);
54     if (operatorConfigCache_ == nullptr) {
55         TELEPHONY_LOGE("SimAccountManager::operatorConfigCache_ is null");
56         return;
57     }
58     operatorConfigCache_->RegisterForIccChange();
59     simStateTracker_ =
60         std::make_shared<SimStateTracker>(std::weak_ptr<SimFileManager>(simFileManager_), operatorConfigCache_, slotId);
61     if (simStateTracker_ == nullptr) {
62         TELEPHONY_LOGE("SimAccountManager::simStateTracker_ is null");
63         return;
64     }
65     simStateTracker_->RegisterForIccLoaded();
66     simStateTracker_->RegisterOpkeyLoaded();
67     simStateTracker_->RegisterOperatorCacheDel();
68 }
69 
GetOperatorConfigs(int32_t slotId,OHOS::Telephony::OperatorConfig & poc)70 int32_t SimAccountManager::GetOperatorConfigs(int32_t slotId, OHOS::Telephony::OperatorConfig &poc)
71 {
72     TELEPHONY_LOGD("SimAccountManager::GetOperatorConfigs");
73     if (operatorConfigCache_ == nullptr) {
74         TELEPHONY_LOGE("SimAccountManager::GetOperatorConfigs operatorConfigCache_ is null");
75         return TELEPHONY_ERR_LOCAL_PTR_NULL;
76     }
77     return operatorConfigCache_->GetOperatorConfigs(static_cast<int32_t>(slotId), poc);
78 }
79 
UpdateOperatorConfigs(int32_t slotId)80 int32_t SimAccountManager::UpdateOperatorConfigs(int32_t slotId)
81 {
82     if (operatorConfigCache_ == nullptr) {
83         TELEPHONY_LOGE("operatorConfigCache_ is null");
84         return TELEPHONY_ERR_LOCAL_PTR_NULL;
85     }
86     return operatorConfigCache_->UpdateOperatorConfigs(slotId);
87 }
88 
IsValidSlotId(int32_t slotId)89 bool SimAccountManager::IsValidSlotId(int32_t slotId)
90 {
91     int32_t count = SIM_SLOT_COUNT;
92     if ((slotId >= DEFAULT_SIM_SLOT_ID) && (slotId < count)) {
93         return true;
94     } else {
95         TELEPHONY_LOGE("SimAccountManager slotId is InValid = %{public}d", slotId);
96         return false;
97     }
98 }
99 
IsValidSlotIdForDefault(int32_t slotId)100 bool SimAccountManager::IsValidSlotIdForDefault(int32_t slotId)
101 {
102     int32_t count = SIM_SLOT_COUNT;
103     if ((slotId >= DEFAULT_SIM_SLOT_ID_REMOVE) && (slotId < count)) {
104         return true;
105     } else {
106         return false;
107     }
108 }
109 
HasOperatorPrivileges(const int32_t slotId,bool & hasOperatorPrivileges)110 int32_t SimAccountManager::HasOperatorPrivileges(const int32_t slotId, bool &hasOperatorPrivileges)
111 {
112     TELEPHONY_LOGD("SimAccountManager::HasOperatorPrivileges begin");
113     if (privilegeController_ != nullptr) {
114         return privilegeController_->HasOperatorPrivileges(hasOperatorPrivileges);
115     }
116     if ((telRilManager_ == nullptr) || (simStateManager_ == nullptr)) {
117         TELEPHONY_LOGE("has nullptr at telRilManager_ or simStateManager_");
118         return TELEPHONY_ERR_LOCAL_PTR_NULL;
119     }
120     auto controller = std::make_shared<IccOperatorPrivilegeController>(telRilManager_, simStateManager_);
121     if (controller == nullptr) {
122         TELEPHONY_LOGE("Make IccOperatorPrivilegeController fail!!");
123         return TELEPHONY_ERR_LOCAL_PTR_NULL;
124     }
125     controller->Init(slotId);
126     privilegeController_ = controller;
127     return controller->HasOperatorPrivileges(hasOperatorPrivileges);
128 }
129 } // namespace Telephony
130 } // namespace OHOS
131