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 "bluetooth_call_client.h"
17 
18 #include <memory>
19 
20 #include "system_ability_definition.h"
21 
22 #include "telephony_errors.h"
23 
24 #include "bluetooth_call_proxy.h"
25 #include "call_manager_proxy.h"
26 
27 namespace OHOS {
28 namespace Telephony {
29 static std::shared_ptr<CallManagerProxy> g_callManagerProxyPtr = nullptr;
30 static sptr<IBluetoothCall> g_bluetoothCallProxyPtr = nullptr;
31 
BluetoothCallClient()32 BluetoothCallClient::BluetoothCallClient() {}
33 
~BluetoothCallClient()34 BluetoothCallClient::~BluetoothCallClient() {}
35 
Init()36 void BluetoothCallClient::Init()
37 {
38     std::lock_guard<std::mutex> lock(mutex_);
39     TELEPHONY_LOGI("BluetoothCallClient init:");
40     if (g_callManagerProxyPtr == nullptr) {
41         g_callManagerProxyPtr = DelayedSingleton<CallManagerProxy>::GetInstance();
42         if (g_callManagerProxyPtr == nullptr) {
43             TELEPHONY_LOGE("g_callManagerProxyPtr is nullptr");
44             return;
45         }
46         g_callManagerProxyPtr->Init(TELEPHONY_CALL_MANAGER_SYS_ABILITY_ID);
47     }
48     sptr<IRemoteObject> iRemoteObjectPtr = g_callManagerProxyPtr->GetProxyObjectPtr(PROXY_BLUETOOTH_CALL);
49     if (iRemoteObjectPtr == nullptr) {
50         TELEPHONY_LOGE("GetProxyObjectPtr failed!");
51         return;
52     }
53     g_bluetoothCallProxyPtr = iface_cast<IBluetoothCall>(iRemoteObjectPtr);
54     if (g_bluetoothCallProxyPtr == nullptr) {
55         TELEPHONY_LOGE("iface_cast<ICallManagerService> failed!");
56         return;
57     }
58     TELEPHONY_LOGI("BluetoothCallClient init success!");
59 }
60 
UnInit()61 void BluetoothCallClient::UnInit()
62 {
63     std::lock_guard<std::mutex> lock(mutex_);
64     if (g_callManagerProxyPtr != nullptr) {
65         g_callManagerProxyPtr->UnInit();
66     } else {
67         TELEPHONY_LOGE("init first please!");
68     }
69 }
70 
RegisterCallBack(std::unique_ptr<CallManagerCallback> callback)71 int32_t BluetoothCallClient::RegisterCallBack(std::unique_ptr<CallManagerCallback> callback)
72 {
73     std::lock_guard<std::mutex> lock(mutex_);
74     if (g_callManagerProxyPtr != nullptr) {
75         return g_callManagerProxyPtr->RegisterCallBack(std::move(callback));
76     } else {
77         TELEPHONY_LOGE("init first please!");
78         return TELEPHONY_ERR_UNINIT;
79     }
80 }
81 
UnRegisterCallBack()82 int32_t BluetoothCallClient::UnRegisterCallBack()
83 {
84     std::lock_guard<std::mutex> lock(mutex_);
85     if (g_callManagerProxyPtr != nullptr) {
86         return g_callManagerProxyPtr->UnRegisterCallBack();
87     } else {
88         TELEPHONY_LOGE("init first please!");
89         return TELEPHONY_ERR_UNINIT;
90     }
91 }
92 
DialCall(std::u16string number, AppExecFwk::PacMap &extras)93 int32_t BluetoothCallClient::DialCall(std::u16string number, AppExecFwk::PacMap &extras)
94 {
95     std::lock_guard<std::mutex> lock(mutex_);
96     if (g_callManagerProxyPtr != nullptr) {
97         return g_callManagerProxyPtr->DialCall(number, extras);
98     } else {
99         TELEPHONY_LOGE("init first please!");
100         return TELEPHONY_ERR_UNINIT;
101     }
102 }
103 
AnswerCall()104 int32_t BluetoothCallClient::AnswerCall()
105 {
106     std::lock_guard<std::mutex> lock(mutex_);
107     if (g_bluetoothCallProxyPtr != nullptr) {
108         return g_bluetoothCallProxyPtr->AnswerCall();
109     } else {
110         TELEPHONY_LOGE("init first please!");
111         return TELEPHONY_ERR_UNINIT;
112     }
113 }
114 
RejectCall()115 int32_t BluetoothCallClient::RejectCall()
116 {
117     std::lock_guard<std::mutex> lock(mutex_);
118     if (g_bluetoothCallProxyPtr != nullptr) {
119         return g_bluetoothCallProxyPtr->RejectCall();
120     } else {
121         TELEPHONY_LOGE("init first please!");
122         return TELEPHONY_ERR_UNINIT;
123     }
124 }
125 
HangUpCall()126 int32_t BluetoothCallClient::HangUpCall()
127 {
128     std::lock_guard<std::mutex> lock(mutex_);
129     if (g_bluetoothCallProxyPtr != nullptr) {
130         return g_bluetoothCallProxyPtr->HangUpCall();
131     } else {
132         TELEPHONY_LOGE("init first please!");
133         return TELEPHONY_ERR_UNINIT;
134     }
135 }
136 
GetCallState()137 int32_t BluetoothCallClient::GetCallState()
138 {
139     std::lock_guard<std::mutex> lock(mutex_);
140     if (g_bluetoothCallProxyPtr != nullptr) {
141         return g_bluetoothCallProxyPtr->GetCallState();
142     } else {
143         TELEPHONY_LOGE("init first please!");
144         return TELEPHONY_ERR_UNINIT;
145     }
146 }
147 
HoldCall()148 int32_t BluetoothCallClient::HoldCall()
149 {
150     std::lock_guard<std::mutex> lock(mutex_);
151     if (g_bluetoothCallProxyPtr != nullptr) {
152         return g_bluetoothCallProxyPtr->HoldCall();
153     } else {
154         TELEPHONY_LOGE("init first please!");
155         return TELEPHONY_ERR_UNINIT;
156     }
157 }
158 
UnHoldCall()159 int32_t BluetoothCallClient::UnHoldCall()
160 {
161     std::lock_guard<std::mutex> lock(mutex_);
162     if (g_bluetoothCallProxyPtr != nullptr) {
163         return g_bluetoothCallProxyPtr->UnHoldCall();
164     } else {
165         TELEPHONY_LOGE("init first please!");
166         return TELEPHONY_ERR_UNINIT;
167     }
168 }
169 
SwitchCall()170 int32_t BluetoothCallClient::SwitchCall()
171 {
172     std::lock_guard<std::mutex> lock(mutex_);
173     if (g_bluetoothCallProxyPtr != nullptr) {
174         return g_bluetoothCallProxyPtr->SwitchCall();
175     } else {
176         TELEPHONY_LOGE("init first please!");
177         return TELEPHONY_ERR_UNINIT;
178     }
179 }
180 
CombineConference()181 int32_t BluetoothCallClient::CombineConference()
182 {
183     std::lock_guard<std::mutex> lock(mutex_);
184     if (g_bluetoothCallProxyPtr != nullptr) {
185         return g_bluetoothCallProxyPtr->CombineConference();
186     } else {
187         TELEPHONY_LOGE("init first please!");
188         return TELEPHONY_ERR_UNINIT;
189     }
190 }
191 
SeparateConference()192 int32_t BluetoothCallClient::SeparateConference()
193 {
194     std::lock_guard<std::mutex> lock(mutex_);
195     if (g_bluetoothCallProxyPtr != nullptr) {
196         return g_bluetoothCallProxyPtr->SeparateConference();
197     } else {
198         TELEPHONY_LOGE("init first please!");
199         return TELEPHONY_ERR_UNINIT;
200     }
201 }
202 
KickOutFromConference()203 int32_t BluetoothCallClient::KickOutFromConference()
204 {
205     std::lock_guard<std::mutex> lock(mutex_);
206     if (g_bluetoothCallProxyPtr != nullptr) {
207         return g_bluetoothCallProxyPtr->KickOutFromConference();
208     } else {
209         TELEPHONY_LOGE("init first please!");
210         return TELEPHONY_ERR_UNINIT;
211     }
212 }
213 
StartDtmf(char str)214 int32_t BluetoothCallClient::StartDtmf(char str)
215 {
216     std::lock_guard<std::mutex> lock(mutex_);
217     if (g_bluetoothCallProxyPtr != nullptr) {
218         return g_bluetoothCallProxyPtr->StartDtmf(str);
219     } else {
220         TELEPHONY_LOGE("init first please!");
221         return TELEPHONY_ERR_UNINIT;
222     }
223 }
224 
StopDtmf()225 int32_t BluetoothCallClient::StopDtmf()
226 {
227     std::lock_guard<std::mutex> lock(mutex_);
228     if (g_bluetoothCallProxyPtr != nullptr) {
229         return g_bluetoothCallProxyPtr->StopDtmf();
230     } else {
231         TELEPHONY_LOGE("init first please!");
232         return TELEPHONY_ERR_UNINIT;
233     }
234 }
235 
IsRinging(bool &enabled)236 int32_t BluetoothCallClient::IsRinging(bool &enabled)
237 {
238     std::lock_guard<std::mutex> lock(mutex_);
239     if (g_callManagerProxyPtr != nullptr) {
240         return g_callManagerProxyPtr->IsRinging(enabled);
241     } else {
242         TELEPHONY_LOGE("init first please!");
243         return TELEPHONY_ERR_UNINIT;
244     }
245 }
246 
HasCall()247 bool BluetoothCallClient::HasCall()
248 {
249     std::lock_guard<std::mutex> lock(mutex_);
250     if (g_callManagerProxyPtr != nullptr) {
251         return g_callManagerProxyPtr->HasCall();
252     } else {
253         TELEPHONY_LOGE("init first please!");
254         return TELEPHONY_ERR_UNINIT;
255     }
256 }
257 
IsNewCallAllowed(bool &enabled)258 int32_t BluetoothCallClient::IsNewCallAllowed(bool &enabled)
259 {
260     std::lock_guard<std::mutex> lock(mutex_);
261     if (g_callManagerProxyPtr != nullptr) {
262         return g_callManagerProxyPtr->IsNewCallAllowed(enabled);
263     } else {
264         TELEPHONY_LOGE("init first please!");
265         return TELEPHONY_ERR_UNINIT;
266     }
267 }
268 
IsInEmergencyCall(bool &enabled)269 int32_t BluetoothCallClient::IsInEmergencyCall(bool &enabled)
270 {
271     std::lock_guard<std::mutex> lock(mutex_);
272     if (g_callManagerProxyPtr != nullptr) {
273         return g_callManagerProxyPtr->IsInEmergencyCall(enabled);
274     } else {
275         TELEPHONY_LOGE("init first please!");
276         return TELEPHONY_ERR_UNINIT;
277     }
278 }
279 
SetMuted(bool isMute)280 int32_t BluetoothCallClient::SetMuted(bool isMute)
281 {
282     std::lock_guard<std::mutex> lock(mutex_);
283     if (g_callManagerProxyPtr != nullptr) {
284         return g_callManagerProxyPtr->SetMuted(isMute);
285     } else {
286         TELEPHONY_LOGE("init first please!");
287         return TELEPHONY_ERR_UNINIT;
288     }
289 }
290 
MuteRinger()291 int32_t BluetoothCallClient::MuteRinger()
292 {
293     std::lock_guard<std::mutex> lock(mutex_);
294     if (g_callManagerProxyPtr != nullptr) {
295         return g_callManagerProxyPtr->MuteRinger();
296     } else {
297         TELEPHONY_LOGE("init first please!");
298         return TELEPHONY_ERR_UNINIT;
299     }
300 }
301 
SetAudioDevice(AudioDeviceType deviceType, const std::string &bluetoothAddress)302 int32_t BluetoothCallClient::SetAudioDevice(AudioDeviceType deviceType, const std::string &bluetoothAddress)
303 {
304     std::lock_guard<std::mutex> lock(mutex_);
305     AudioDevice device;
306     if (memset_s(&device, sizeof(AudioDevice), 0, sizeof(AudioDevice)) != EOK) {
307         TELEPHONY_LOGE("memset_s fail");
308         return TELEPHONY_ERR_MEMSET_FAIL;
309     }
310     device.deviceType = static_cast<AudioDeviceType>(deviceType);
311     if (bluetoothAddress.length() > kMaxAddressLen) {
312         TELEPHONY_LOGE("address is not too long");
313         return TELEPHONY_ERR_ARGUMENT_INVALID;
314     }
315     if (memcpy_s(device.address, kMaxAddressLen, bluetoothAddress.c_str(), bluetoothAddress.length()) != EOK) {
316         TELEPHONY_LOGE("memcpy_s address fail");
317         return TELEPHONY_ERR_MEMCPY_FAIL;
318     }
319 
320     if (g_callManagerProxyPtr != nullptr) {
321         return g_callManagerProxyPtr->SetAudioDevice(device);
322     } else {
323         TELEPHONY_LOGE("init first please!");
324         return TELEPHONY_ERR_UNINIT;
325     }
326 }
327 
GetCurrentCallList(int32_t slotId)328 std::vector<CallAttributeInfo> BluetoothCallClient::GetCurrentCallList(int32_t slotId)
329 {
330     std::lock_guard<std::mutex> lock(mutex_);
331     std::vector<CallAttributeInfo> callVec;
332     callVec.clear();
333     if (g_bluetoothCallProxyPtr != nullptr) {
334         return g_bluetoothCallProxyPtr->GetCurrentCallList(slotId);
335     } else {
336         TELEPHONY_LOGE("init first please!");
337         return callVec;
338     }
339 }
340 } // namespace Telephony
341 } // namespace OHOS