1 /*
2 * Copyright (C) 2023 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 "satellite_call_client.h"
17
18 #include "cellular_call_hisysevent.h"
19 #include "iservice_registry.h"
20 #include "satellite_call_callback_stub.h"
21 #include "system_ability_definition.h"
22 #include "telephony_errors.h"
23 #include "telephony_log_wrapper.h"
24 #include "cellular_call_service.h"
25
26 namespace OHOS {
27 namespace Telephony {
28 SatelliteCallClient::SatelliteCallClient() = default;
29
~SatelliteCallClient()30 SatelliteCallClient::~SatelliteCallClient()
31 {
32 UnInit();
33 }
34
Init()35 void SatelliteCallClient::Init()
36 {
37 TELEPHONY_LOGI("Init start");
38 if (IsConnect()) {
39 TELEPHONY_LOGE("Init, IsConnect return true");
40 return;
41 }
42
43 GetSatelliteCallProxy();
44 std::lock_guard<std::mutex> lock(mutexProxy_);
45 if (satelliteCallProxy_ == nullptr) {
46 TELEPHONY_LOGE("Init, get satellite call proxy failed!");
47 return;
48 }
49 TELEPHONY_LOGI("Init successfully");
50 }
51
UnInit()52 void SatelliteCallClient::UnInit()
53 {
54 Clean();
55 std::lock_guard<std::mutex> lock(mutexMap_);
56 handlerMap_.clear();
57 }
58
GetSatelliteCallProxy()59 sptr<SatelliteCallInterface> SatelliteCallClient::GetSatelliteCallProxy()
60 {
61 std::lock_guard<std::mutex> lock(mutexProxy_);
62 if (satelliteCallProxy_ != nullptr) {
63 return satelliteCallProxy_;
64 }
65 auto managerPtr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
66 if (managerPtr == nullptr) {
67 TELEPHONY_LOGE("GetSatelliteCallProxy return, get system ability manager error.");
68 return nullptr;
69 }
70 auto remoteObjectPtr = managerPtr->CheckSystemAbility(TELEPHONY_SATELLITE_SERVICE_ABILITY_ID);
71 if (remoteObjectPtr == nullptr) {
72 TELEPHONY_LOGE("GetSatelliteCallProxy return, remote service not exists.");
73 return nullptr;
74 }
75
76 std::unique_ptr<SatelliteServiceDeathRecipient> recipient = std::make_unique<SatelliteServiceDeathRecipient>(*this);
77 if (recipient == nullptr) {
78 TELEPHONY_LOGE("recipient is null");
79 return nullptr;
80 }
81
82 sptr<IRemoteObject::DeathRecipient> dr(recipient.release());
83 if (remoteObjectPtr->IsProxyObject() && !remoteObjectPtr->AddDeathRecipient(dr)) {
84 TELEPHONY_LOGE("Failed to add death recipient");
85 return nullptr;
86 }
87
88 satelliteServiceProxy_ = iface_cast<ISatelliteService>(remoteObjectPtr);
89 if (satelliteServiceProxy_ == nullptr) {
90 TELEPHONY_LOGE("GetSatelliteCallProxy return, satelliteServiceProxy_ is nullptr.");
91 return nullptr;
92 }
93 sptr<IRemoteObject> satelliteCallRemoteObjectPtr = satelliteServiceProxy_->GetProxyObjectPtr(PROXY_SATELLITE_CALL);
94 if (satelliteCallRemoteObjectPtr == nullptr) {
95 TELEPHONY_LOGE("GetProxyObjectPtr return, satelliteCallRemoteObjectPtr is nullptr.");
96 return nullptr;
97 }
98 satelliteCallProxy_ = iface_cast<SatelliteCallInterface>(satelliteCallRemoteObjectPtr);
99 deathRecipient_ = dr;
100 RegisterSatelliteCallCallback();
101 return satelliteCallProxy_;
102 }
103
IsConnect()104 bool SatelliteCallClient::IsConnect()
105 {
106 std::lock_guard<std::mutex> lock(mutexProxy_);
107 return (satelliteCallProxy_ != nullptr);
108 }
109
RegisterSatelliteCallCallback()110 int32_t SatelliteCallClient::RegisterSatelliteCallCallback()
111 {
112 if (satelliteCallProxy_ == nullptr) {
113 TELEPHONY_LOGE("satelliteCallProxy_ is null!");
114 return TELEPHONY_ERR_LOCAL_PTR_NULL;
115 }
116 satelliteCallCallback_ = (std::make_unique<SatelliteCallCallbackStub>()).release();
117 if (satelliteCallCallback_ == nullptr) {
118 TELEPHONY_LOGE("RegisterSatelliteCallCallback return, make unique error.");
119 return TELEPHONY_ERR_LOCAL_PTR_NULL;
120 }
121 int32_t ret = satelliteCallProxy_->RegisterSatelliteCallCallback(satelliteCallCallback_);
122 if (ret) {
123 TELEPHONY_LOGE("RegisterSatelliteCallCallback return, register callback error.");
124 return TELEPHONY_ERR_FAIL;
125 }
126 TELEPHONY_LOGI("RegisterSatelliteCallCallback success.");
127 return TELEPHONY_SUCCESS;
128 }
129
RegisterSatelliteCallCallbackHandler(int32_t slotId,const std::shared_ptr<AppExecFwk::EventHandler> & handler)130 int32_t SatelliteCallClient::RegisterSatelliteCallCallbackHandler(
131 int32_t slotId, const std::shared_ptr<AppExecFwk::EventHandler> &handler)
132 {
133 if (handler == nullptr) {
134 TELEPHONY_LOGE("RegisterSatelliteCallCallbackHandler return, handler is null.");
135 return TELEPHONY_ERR_LOCAL_PTR_NULL;
136 }
137
138 std::lock_guard<std::mutex> lock(mutexMap_);
139 handlerMap_.insert(std::make_pair(slotId, handler));
140 TELEPHONY_LOGI("RegisterSatelliteCallCallbackHandler success.");
141 return TELEPHONY_SUCCESS;
142 }
143
GetHandler(int32_t slotId)144 std::shared_ptr<AppExecFwk::EventHandler> SatelliteCallClient::GetHandler(int32_t slotId)
145 {
146 std::lock_guard<std::mutex> lock(mutexMap_);
147 return handlerMap_[slotId];
148 }
149
Dial(const SatelliteCallInfo & callInfo,CLIRMode mode)150 int32_t SatelliteCallClient::Dial(const SatelliteCallInfo &callInfo, CLIRMode mode)
151 {
152 if (ReConnectService() != TELEPHONY_SUCCESS) {
153 TELEPHONY_LOGE("ipc reconnect failed!");
154 CellularCallHiSysEvent::WriteDialCallFaultEvent(callInfo.slotId, INVALID_PARAMETER, INVALID_PARAMETER,
155 TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL, "ipc reconnect failed");
156 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
157 }
158 std::lock_guard<std::mutex> lock(mutexProxy_);
159 return satelliteCallProxy_->Dial(callInfo, mode);
160 }
161
HangUp(int32_t slotId,int32_t index)162 int32_t SatelliteCallClient::HangUp(int32_t slotId, int32_t index)
163 {
164 if (ReConnectService() != TELEPHONY_SUCCESS) {
165 TELEPHONY_LOGE("ipc reconnect failed!");
166 CellularCallHiSysEvent::WriteHangUpFaultEvent(
167 slotId, INVALID_PARAMETER, TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL, "HangUp satellite ipc reconnect failed");
168 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
169 }
170 std::lock_guard<std::mutex> lock(mutexProxy_);
171 return satelliteCallProxy_->HangUp(slotId, index);
172 }
173
Reject(int32_t slotId)174 int32_t SatelliteCallClient::Reject(int32_t slotId)
175 {
176 if (ReConnectService() != TELEPHONY_SUCCESS) {
177 TELEPHONY_LOGE("ipc reconnect failed!");
178 CellularCallHiSysEvent::WriteHangUpFaultEvent(
179 slotId, INVALID_PARAMETER, TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL, "Reject satellite ipc reconnect failed");
180 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
181 }
182 std::lock_guard<std::mutex> lock(mutexProxy_);
183 return satelliteCallProxy_->Reject(slotId);
184 }
185
Answer(int32_t slotId)186 int32_t SatelliteCallClient::Answer(int32_t slotId)
187 {
188 if (ReConnectService() != TELEPHONY_SUCCESS) {
189 TELEPHONY_LOGE("ipc reconnect failed!");
190 CellularCallHiSysEvent::WriteAnswerCallFaultEvent(slotId, INVALID_PARAMETER, INVALID_PARAMETER,
191 TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL, "answer satellite ipc reconnect failed");
192 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
193 }
194 std::lock_guard<std::mutex> lock(mutexProxy_);
195 return satelliteCallProxy_->Answer(slotId);
196 }
197
GetSatelliteCallsDataRequest(int32_t slotId,int64_t lastCallsDataFlag)198 int32_t SatelliteCallClient::GetSatelliteCallsDataRequest(int32_t slotId, int64_t lastCallsDataFlag)
199 {
200 if (ReConnectService() != TELEPHONY_SUCCESS) {
201 TELEPHONY_LOGE("ipc reconnect failed!");
202 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
203 }
204 std::lock_guard<std::mutex> lock(mutexProxy_);
205 return satelliteCallProxy_->GetSatelliteCallsDataRequest(slotId);
206 }
207
ReConnectService()208 int32_t SatelliteCallClient::ReConnectService()
209 {
210 if (satelliteCallProxy_ == nullptr) {
211 TELEPHONY_LOGI("try to reconnect satellite call service now...");
212 GetSatelliteCallProxy();
213 if (satelliteCallProxy_ == nullptr) {
214 TELEPHONY_LOGE("Connect service failed");
215 return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
216 }
217 }
218 return TELEPHONY_SUCCESS;
219 }
220
Clean()221 void SatelliteCallClient::Clean()
222 {
223 std::lock_guard<std::mutex> lock(mutexProxy_);
224 if (satelliteCallProxy_ != nullptr) {
225 satelliteCallProxy_.clear();
226 satelliteCallProxy_ = nullptr;
227 }
228 if (satelliteCallCallback_ != nullptr) {
229 satelliteCallCallback_.clear();
230 satelliteCallCallback_ = nullptr;
231 }
232 }
233
OnRemoteDied(const wptr<IRemoteObject> & remote)234 void SatelliteCallClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
235 {
236 RemoveDeathRecipient(remote);
237 ClearAllCallsInfo();
238 }
239
RemoveDeathRecipient(const wptr<IRemoteObject> & remote)240 void SatelliteCallClient::RemoveDeathRecipient(const wptr<IRemoteObject> &remote)
241 {
242 if (remote == nullptr) {
243 TELEPHONY_LOGE("Remote died, remote is nullptr");
244 return;
245 }
246 std::lock_guard<std::mutex> lock(mutexProxy_);
247 if (satelliteServiceProxy_ == nullptr) {
248 TELEPHONY_LOGE("satelliteServiceProxy_ is nullptr");
249 return;
250 }
251 auto serviceRemote = satelliteServiceProxy_->AsObject();
252 if (serviceRemote == nullptr) {
253 TELEPHONY_LOGE("serviceRemote is nullptr");
254 return;
255 }
256 if (serviceRemote != remote.promote()) {
257 TELEPHONY_LOGE("Remote died serviceRemote is not same");
258 return;
259 }
260 serviceRemote->RemoveDeathRecipient(deathRecipient_);
261 satelliteServiceProxy_ = nullptr;
262 satelliteCallProxy_ = nullptr;
263 TELEPHONY_LOGI("SatelliteCallClient:RemoveDeathRecipient success");
264 }
265
ClearAllCallsInfo()266 void SatelliteCallClient::ClearAllCallsInfo()
267 {
268 auto serviceInstance = DelayedSingleton<CellularCallService>::GetInstance();
269 if (serviceInstance == nullptr) {
270 TELEPHONY_LOGE("SatelliteCallClient::serviceInstance is null");
271 return;
272 }
273 auto satelliteControl = std::make_shared<SatelliteControl>();
274 for (int slotId = DEFAULT_SIM_SLOT_ID; slotId < SIM_SLOT_COUNT; slotId++) {
275 satelliteControl = serviceInstance->GetSatelliteControl(slotId);
276 if (satelliteControl == nullptr) {
277 TELEPHONY_LOGE("SatelliteCallClient::[slot%{public}d] serviceControl is null", slotId);
278 continue;
279 }
280 satelliteControl->ReportHangUpInfo(slotId);
281 }
282 }
283
284 } // namespace Telephony
285 } // namespace OHOS