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