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 "module_service_utils.h"
17
18 #include "ipc_skeleton.h"
19 #include "string_ex.h"
20
21 #include "cellular_call_register.h"
22 #include "ims_callback_stub.h"
23 #include "ims_death_recipient.h"
24 #include "telephony_log_wrapper.h"
25 #include "telephony_types.h"
26
27 namespace OHOS {
28 namespace Telephony {
GetRadioState(int32_t slotId)29 bool ModuleServiceUtils::GetRadioState(int32_t slotId)
30 {
31 int32_t ret = CoreManagerInner::GetInstance().GetRadioState(slotId);
32 if (ret != ModemPowerState::CORE_SERVICE_POWER_ON) {
33 TELEPHONY_LOGE("ModuleServiceUtils::GetRadioState radio off.");
34 return false;
35 }
36 return true;
37 }
38
GetNetworkStatus(int32_t slotId)39 PhoneType ModuleServiceUtils::GetNetworkStatus(int32_t slotId)
40 {
41 return CoreManagerInner::GetInstance().GetPhoneType(slotId);
42 }
43
GetIsoCountryCode(int32_t slotId)44 std::string ModuleServiceUtils::GetIsoCountryCode(int32_t slotId)
45 {
46 return Str16ToStr8(CoreManagerInner::GetInstance().GetISOCountryCodeForSim(slotId));
47 }
48
GetNetworkCountryCode(int32_t slotId)49 std::string ModuleServiceUtils::GetNetworkCountryCode(int32_t slotId)
50 {
51 return Str16ToStr8(CoreManagerInner::GetInstance().GetIsoCountryCodeForNetwork(slotId));
52 }
53
GetImsRegistrationState(int32_t slotId)54 bool ModuleServiceUtils::GetImsRegistrationState(int32_t slotId)
55 {
56 return CoreManagerInner::GetInstance().GetImsRegStatus(slotId);
57 }
58
GetSlotInfo()59 std::vector<int32_t> ModuleServiceUtils::GetSlotInfo()
60 {
61 const int32_t slotSingle = 1;
62 const int32_t slotDouble = 2;
63 std::vector<int32_t> slotVector;
64 if (SIM_SLOT_COUNT == slotSingle) {
65 slotVector.push_back(DEFAULT_SIM_SLOT_ID);
66 } else if (SIM_SLOT_COUNT == slotDouble) {
67 slotVector.push_back(SIM_SLOT_0);
68 slotVector.push_back(SIM_SLOT_1);
69 }
70 return slotVector;
71 }
72
NeedCallImsService() const73 bool ModuleServiceUtils::NeedCallImsService() const
74 {
75 auto remoteObject = GetImsServiceRemoteObject();
76 if (remoteObject == nullptr) {
77 TELEPHONY_LOGE("NeedCallImsService return, remote service not exists.");
78 return false;
79 }
80 if (!remoteObject->IsCallBackExists()) {
81 TELEPHONY_LOGE("NeedCallImsService return, ims callBack not exists");
82 RegisterImsCallBackAgain();
83 return false;
84 }
85 return true;
86 }
87
GetImsServiceRemoteObject() const88 sptr<ImsInterface> ModuleServiceUtils::GetImsServiceRemoteObject() const
89 {
90 auto managerPtr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
91 if (managerPtr == nullptr) {
92 TELEPHONY_LOGE("GetImsServiceRemoteObject return, get system ability manager error.");
93 return nullptr;
94 }
95 auto remoteObjectPtr = managerPtr->CheckSystemAbility(TELEPHONY_IMS_SYS_ABILITY_ID);
96 if (remoteObjectPtr == nullptr) {
97 TELEPHONY_LOGE("GetImsServiceRemoteObject return, remote service not exists.");
98 return nullptr;
99 }
100 return iface_cast<ImsInterface>(remoteObjectPtr);
101 }
102
RegisterImsCallBackAgain() const103 void ModuleServiceUtils::RegisterImsCallBackAgain() const
104 {
105 TELEPHONY_LOGI("RegisterImsCallBackAgain entry, register ims callback again.");
106 DelayedSingleton<CellularCallRegister>::GetInstance()->RegisterImsCallBack();
107 }
108
ConnectImsService()109 int32_t ModuleServiceUtils::ConnectImsService()
110 {
111 auto managerPtr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
112 if (managerPtr == nullptr) {
113 TELEPHONY_LOGE("ConnectImsService return, get system ability manager error.");
114 return TELEPHONY_ERROR;
115 }
116 int32_t imsSaId = TELEPHONY_IMS_SYS_ABILITY_ID;
117 auto iRemoteObjectPtr = managerPtr->GetSystemAbility(imsSaId);
118 if (iRemoteObjectPtr == nullptr) {
119 TELEPHONY_LOGE("ConnectImsService return, remote service not exists.");
120 return TELEPHONY_ERROR;
121 }
122
123 std::weak_ptr<ModuleServiceUtils> weakPtr = shared_from_this();
124 auto deathCallback = [weakPtr](const wptr<IRemoteObject> &object) {
125 auto sharedPtr = weakPtr.lock();
126 if (sharedPtr) {
127 sharedPtr->NotifyDeath();
128 }
129 };
130 sptr<IRemoteObject::DeathRecipient> imsRecipient_ = (std::make_unique<ImsDeathRecipient>(deathCallback)).release();
131 if (imsRecipient_ == nullptr) {
132 TELEPHONY_LOGE("ConnectImsService return, imsRecipient_ is nullptr.");
133 return TELEPHONY_ERROR;
134 }
135 if (!iRemoteObjectPtr->AddDeathRecipient(imsRecipient_)) {
136 TELEPHONY_LOGE("ConnectImsService return, add death recipient fail.");
137 return TELEPHONY_ERROR;
138 }
139 return TELEPHONY_SUCCESS;
140 }
141
NotifyDeath()142 void ModuleServiceUtils::NotifyDeath()
143 {
144 TELEPHONY_LOGI("service is dead, connect again");
145 for (uint32_t i = 0; i < CONNECT_MAX_TRY_COUNT; i++) {
146 std::this_thread::sleep_for(std::chrono::milliseconds(CONNECT_SERVICE_WAIT_TIME));
147 int32_t result = ConnectImsService();
148 if (result != TELEPHONY_SUCCESS) {
149 TELEPHONY_LOGI("connect Ims service successful");
150 return;
151 }
152 }
153 TELEPHONY_LOGI("connect cellular call service failed");
154 }
155 } // namespace Telephony
156 } // namespace OHOS