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