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_hfp_hf.h"
17 #include "bluetooth_host.h"
18 #include "bluetooth_load_system_ability.h"
19 #include "bluetooth_log.h"
20 #include "bluetooth_utils.h"
21 #include "bluetooth_observer_list.h"
22 #include "i_bluetooth_hfp_hf.h"
23 #include "bluetooth_hfp_hf_observer_stub.h"
24 #include "i_bluetooth_host.h"
25 #include "iservice_registry.h"
26 #include "system_ability_definition.h"
27
28 namespace OHOS {
29 namespace Bluetooth {
30 std::mutex g_hfpHFProxyMutex;
31 class HfServiceObserver : public BluetoothHfpHfObserverStub {
32 public:
HfServiceObserver(BluetoothObserverList<HandsFreeUnitObserver> & observers)33 explicit HfServiceObserver(BluetoothObserverList<HandsFreeUnitObserver> &observers) : observers_(observers)
34 {
35 HILOGI("enter");
36 }
~HfServiceObserver()37 ~HfServiceObserver() override
38 {
39 HILOGI("enter");
40 }
41
OnConnectionStateChanged(const BluetoothRawAddress & device,int32_t state)42 void OnConnectionStateChanged(const BluetoothRawAddress &device, int32_t state) override
43 {
44 HILOGD("enter, device: %{public}s, state: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), state);
45 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
46 observers_.ForEach([remoteDevice, state](std::shared_ptr<HandsFreeUnitObserver> observer) {
47 observer->OnConnectionStateChanged(remoteDevice, state);
48 });
49 }
50
OnScoStateChanged(const BluetoothRawAddress & device,int32_t state)51 void OnScoStateChanged(const BluetoothRawAddress &device, int32_t state) override
52 {
53 HILOGI("enter, device: %{public}s, state: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), state);
54 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
55 observers_.ForEach([remoteDevice, state](std::shared_ptr<HandsFreeUnitObserver> observer) {
56 observer->OnScoStateChanged(remoteDevice, state);
57 });
58 }
59
OnCallChanged(const BluetoothRawAddress & device,const BluetoothHfpHfCall & call)60 void OnCallChanged(const BluetoothRawAddress &device,
61 const BluetoothHfpHfCall &call) override
62 {
63 HILOGI("enter, device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
64 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
65 UUID uuid = UUID::ConvertFrom128Bits(call.GetUuid().ConvertTo128Bits());
66 HandsFreeUnitCall tmpCall(call.GetRemoteDevice(),
67 call.GetId(),
68 call.GetState(),
69 call.GetNumber(),
70 uuid,
71 call.IsMultiParty(),
72 call.IsOutgoing(),
73 call.IsInBandRing(),
74 call.GetCreationTime());
75 observers_.ForEach([remoteDevice, tmpCall](std::shared_ptr<HandsFreeUnitObserver> observer) {
76 observer->OnCallChanged(remoteDevice, tmpCall);
77 });
78 }
79
OnSignalStrengthChanged(const BluetoothRawAddress & device,int32_t signal)80 void OnSignalStrengthChanged(const BluetoothRawAddress &device, int32_t signal) override
81 {
82 HILOGI("enter, device: %{public}s, signal: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), signal);
83 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
84 observers_.ForEach([remoteDevice, signal](std::shared_ptr<HandsFreeUnitObserver> observer) {
85 observer->OnSignalStrengthChanged(remoteDevice, signal);
86 });
87 }
88
OnRegistrationStatusChanged(const BluetoothRawAddress & device,int32_t status)89 void OnRegistrationStatusChanged(const BluetoothRawAddress &device, int32_t status) override
90 {
91 HILOGI("enter, device: %{public}s, status: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), status);
92 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
93 observers_.ForEach([remoteDevice, status](std::shared_ptr<HandsFreeUnitObserver> observer) {
94 observer->OnRegistrationStatusChanged(remoteDevice, status);
95 });
96 }
97
OnRoamingStatusChanged(const BluetoothRawAddress & device,int32_t status)98 void OnRoamingStatusChanged(const BluetoothRawAddress &device, int32_t status) override
99 {
100 HILOGI("enter, device: %{public}s, status: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), status);
101 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
102 observers_.ForEach([remoteDevice, status](std::shared_ptr<HandsFreeUnitObserver> observer) {
103 observer->OnRoamingStatusChanged(remoteDevice, status);
104 });
105 }
106
OnOperatorSelectionChanged(const BluetoothRawAddress & device,const std::string & name)107 void OnOperatorSelectionChanged(
108 const BluetoothRawAddress &device, const std::string &name) override
109 {
110 HILOGI("enter, device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
111 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
112 observers_.ForEach([remoteDevice, name](std::shared_ptr<HandsFreeUnitObserver> observer) {
113 observer->OnOperatorSelectionChanged(remoteDevice, name);
114 });
115 }
116
OnSubscriberNumberChanged(const BluetoothRawAddress & device,const std::string & number)117 void OnSubscriberNumberChanged(
118 const BluetoothRawAddress &device, const std::string &number) override
119 {
120 HILOGI("enter, device: %{public}s, number: %{public}s",
121 GetEncryptAddr((device).GetAddress()).c_str(), number.c_str());
122 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
123 observers_.ForEach([remoteDevice, number](std::shared_ptr<HandsFreeUnitObserver> observer) {
124 observer->OnSubscriberNumberChanged(remoteDevice, number);
125 });
126 }
127
OnVoiceRecognitionStatusChanged(const BluetoothRawAddress & device,int32_t status)128 void OnVoiceRecognitionStatusChanged(
129 const BluetoothRawAddress &device, int32_t status) override
130 {
131 HILOGI("enter, device: %{public}s, status: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), status);
132 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
133 observers_.ForEach([remoteDevice, status](std::shared_ptr<HandsFreeUnitObserver> observer) {
134 observer->OnVoiceRecognitionStatusChanged(remoteDevice, status);
135 });
136 }
137
OnInBandRingToneChanged(const BluetoothRawAddress & device,int32_t status)138 void OnInBandRingToneChanged(const BluetoothRawAddress &device, int32_t status) override
139 {
140 HILOGI("enter, device: %{public}s, status: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), status);
141 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
142 observers_.ForEach([remoteDevice, status](std::shared_ptr<HandsFreeUnitObserver> observer) {
143 observer->OnInBandRingToneChanged(remoteDevice, status);
144 });
145 }
146
147 private:
148 BluetoothObserverList<HandsFreeUnitObserver> &observers_;
149 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(HfServiceObserver);
150 };
151
152 std::string HfpHfServiceName = "bluetooth-hfp-hf-server";
153
154 struct HandsFreeUnit::impl {
155 impl();
156 ~impl();
157 bool InitHfpHfProxy(void);
158
ConnectScoOHOS::Bluetooth::HandsFreeUnit::impl159 bool ConnectSco(const BluetoothRemoteDevice &device)
160 {
161 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
162 if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
163 return proxy_->ConnectSco(BluetoothRawAddress(device.GetDeviceAddr()));
164 }
165 return false;
166 }
167
DisconnectScoOHOS::Bluetooth::HandsFreeUnit::impl168 bool DisconnectSco(const BluetoothRemoteDevice &device)
169 {
170 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
171 if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
172 return proxy_->DisconnectSco(BluetoothRawAddress(device.GetDeviceAddr()));
173 }
174 return false;
175 }
176
GetDevicesByStatesOHOS::Bluetooth::HandsFreeUnit::impl177 std::vector<BluetoothRemoteDevice> GetDevicesByStates(std::vector<int> states)
178 {
179 HILOGI("enter");
180 std::vector<BluetoothRemoteDevice> remoteDevices;
181 if (proxy_ != nullptr) {
182 std::vector<BluetoothRawAddress> rawDevices;
183 std::vector<int32_t> tmpStates;
184 for (int state : states) {
185 tmpStates.push_back((int32_t)state);
186 }
187
188 proxy_->GetDevicesByStates(tmpStates, rawDevices);
189 for (BluetoothRawAddress rawDevice : rawDevices) {
190 BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 0);
191 remoteDevices.push_back(remoteDevice);
192 }
193 }
194 return remoteDevices;
195 }
196
GetDeviceStateOHOS::Bluetooth::HandsFreeUnit::impl197 int GetDeviceState(const BluetoothRemoteDevice &device)
198 {
199 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
200 if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
201 return proxy_->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()));
202 }
203 return HFP_HF_SLC_STATE_DISCONNECTED;
204 }
205
GetScoStateOHOS::Bluetooth::HandsFreeUnit::impl206 int GetScoState(const BluetoothRemoteDevice &device)
207 {
208 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
209 if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
210 return proxy_->GetScoState(BluetoothRawAddress(device.GetDeviceAddr()));
211 }
212 return HFP_HF_SCO_STATE_DISCONNECTED;
213 }
214
SendDTMFToneOHOS::Bluetooth::HandsFreeUnit::impl215 bool SendDTMFTone(const BluetoothRemoteDevice &device, uint8_t code)
216 {
217 HILOGI("enter, device: %{public}s, code: %{public}d", GET_ENCRYPT_ADDR(device), code);
218 if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
219 return proxy_->SendDTMFTone(BluetoothRawAddress(device.GetDeviceAddr()), code);
220 }
221 return false;
222 }
223
ConnectOHOS::Bluetooth::HandsFreeUnit::impl224 bool Connect(const BluetoothRemoteDevice &device)
225 {
226 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
227 if (proxy_ != nullptr && !BluetoothHost::GetDefaultHost().IsBtDiscovering() &&
228 device.IsValidBluetoothRemoteDevice()) {
229 return proxy_->Connect(BluetoothRawAddress(device.GetDeviceAddr()));
230 }
231 HILOGE("fw return false!");
232 return false;
233 }
234
DisconnectOHOS::Bluetooth::HandsFreeUnit::impl235 bool Disconnect(const BluetoothRemoteDevice &device)
236 {
237 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
238 if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
239 return proxy_->Disconnect(BluetoothRawAddress(device.GetDeviceAddr()));
240 }
241 HILOGE("fw return false!");
242 return false;
243 }
244
OpenVoiceRecognitionOHOS::Bluetooth::HandsFreeUnit::impl245 bool OpenVoiceRecognition(const BluetoothRemoteDevice &device)
246 {
247 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
248 if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
249 return proxy_->OpenVoiceRecognition(BluetoothRawAddress(device.GetDeviceAddr()));
250 }
251 return false;
252 }
253
CloseVoiceRecognitionOHOS::Bluetooth::HandsFreeUnit::impl254 bool CloseVoiceRecognition(const BluetoothRemoteDevice &device)
255 {
256 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
257 if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
258 return proxy_->CloseVoiceRecognition(BluetoothRawAddress(device.GetDeviceAddr()));
259 }
260 return false;
261 }
262
GetExistingCallsOHOS::Bluetooth::HandsFreeUnit::impl263 std::vector<HandsFreeUnitCall> GetExistingCalls(const BluetoothRemoteDevice &device)
264 {
265 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
266 std::vector<HandsFreeUnitCall> calls;
267 std::vector<BluetoothHfpHfCall> callsList;
268 if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
269 proxy_->GetCurrentCallList(BluetoothRawAddress(device.GetDeviceAddr()), callsList);
270 for (BluetoothHfpHfCall call : callsList) {
271 UUID uuid = UUID::ConvertFrom128Bits(call.GetUuid().ConvertTo128Bits());
272 HandsFreeUnitCall tmpCall(call.GetRemoteDevice(),
273 call.GetId(),
274 call.GetState(),
275 call.GetNumber(),
276 uuid,
277 call.IsMultiParty(),
278 call.IsOutgoing(),
279 call.IsInBandRing(),
280 call.GetCreationTime());
281 calls.push_back(tmpCall);
282 }
283 }
284 return calls;
285 }
286
AcceptIncomingCallOHOS::Bluetooth::HandsFreeUnit::impl287 bool AcceptIncomingCall(const BluetoothRemoteDevice &device, int flag)
288 {
289 HILOGI("enter, device: %{public}s, flag: %{public}d", GET_ENCRYPT_ADDR(device), flag);
290 if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
291 return proxy_->AcceptIncomingCall(BluetoothRawAddress(device.GetDeviceAddr()), (int32_t)flag);
292 }
293 return false;
294 }
295
HoldActiveCallOHOS::Bluetooth::HandsFreeUnit::impl296 bool HoldActiveCall(const BluetoothRemoteDevice &device)
297 {
298 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
299 if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
300 return proxy_->HoldActiveCall(BluetoothRawAddress(device.GetDeviceAddr()));
301 }
302 return false;
303 }
304
RejectIncomingCallOHOS::Bluetooth::HandsFreeUnit::impl305 bool RejectIncomingCall(const BluetoothRemoteDevice &device)
306 {
307 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
308 if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
309 return proxy_->RejectIncomingCall(BluetoothRawAddress(device.GetDeviceAddr()));
310 }
311 return false;
312 }
313
SendKeyPressedOHOS::Bluetooth::HandsFreeUnit::impl314 bool SendKeyPressed(const BluetoothRemoteDevice &device)
315 {
316 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
317 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
318 return proxy_->SendKeyPressed(BluetoothRawAddress(device.GetDeviceAddr()));
319 }
320 return false;
321 }
322
HandleIncomingCallOHOS::Bluetooth::HandsFreeUnit::impl323 bool HandleIncomingCall(const BluetoothRemoteDevice &device, int flag)
324 {
325 HILOGI("Enter!");
326 if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
327 return proxy_->HandleIncomingCall(BluetoothRawAddress(device.GetDeviceAddr()), (int32_t)flag);
328 }
329 return false;
330 }
331
HandleMultiCallOHOS::Bluetooth::HandsFreeUnit::impl332 bool HandleMultiCall(const BluetoothRemoteDevice &device, int flag, int index)
333 {
334 HILOGI("Enter!");
335 if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
336 return proxy_->HandleMultiCall(BluetoothRawAddress(device.GetDeviceAddr()), (int32_t)flag, (int32_t)index);
337 }
338 return false;
339 }
340
DialLastNumberOHOS::Bluetooth::HandsFreeUnit::impl341 bool DialLastNumber(const BluetoothRemoteDevice &device)
342 {
343 HILOGI("Enter!");
344 if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
345 return proxy_->DialLastNumber(BluetoothRawAddress(device.GetDeviceAddr()));
346 }
347 return false;
348 }
349
DialMemoryOHOS::Bluetooth::HandsFreeUnit::impl350 bool DialMemory(const BluetoothRemoteDevice &device, int index)
351 {
352 HILOGD("Enter! index = %{public}d", index);
353 if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
354 return proxy_->DialMemory(BluetoothRawAddress(device.GetDeviceAddr()), index);
355 }
356 return false;
357 }
358
SendVoiceTagOHOS::Bluetooth::HandsFreeUnit::impl359 bool SendVoiceTag(const BluetoothRemoteDevice &device, int index)
360 {
361 HILOGD("[%{public}s]: %{public}s(): Enter! index = %{public}d", __FILE__, __FUNCTION__, index);
362 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
363 return proxy_->SendVoiceTag(BluetoothRawAddress(device.GetDeviceAddr()), index);
364 }
365 return false;
366 }
367
FinishActiveCallOHOS::Bluetooth::HandsFreeUnit::impl368 bool FinishActiveCall(const BluetoothRemoteDevice &device, const HandsFreeUnitCall &call)
369 {
370 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
371 if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
372 bluetooth::Uuid uuid = bluetooth::Uuid::ConvertFrom128Bits(call.GetUuid().ConvertTo128Bits());
373 bluetooth::HandsFreeUnitCalls calls(call.GetRemoteDevice(),
374 call.GetId(),
375 call.GetState(),
376 call.GetNumber(),
377 uuid,
378 call.IsMultiParty(),
379 call.IsOutgoing(),
380 call.IsInBandRing(),
381 call.GetCreationTime());
382 return proxy_->FinishActiveCall(BluetoothRawAddress(device.GetDeviceAddr()), calls);
383 }
384 return false;
385 }
386
StartDialOHOS::Bluetooth::HandsFreeUnit::impl387 std::optional<HandsFreeUnitCall> StartDial(const BluetoothRemoteDevice &device, const std::string &number)
388 {
389 HILOGI("enter, device: %{public}s, number: %{public}s", GET_ENCRYPT_ADDR(device), number.c_str());
390 if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
391 BluetoothHfpHfCall calls;
392 proxy_->StartDial(BluetoothRawAddress(device.GetDeviceAddr()), number, calls);
393 UUID uuid = UUID::ConvertFrom128Bits(calls.GetUuid().ConvertTo128Bits());
394 HandsFreeUnitCall call(calls.GetRemoteDevice(),
395 calls.GetId(),
396 calls.GetState(),
397 calls.GetNumber(),
398 uuid,
399 calls.IsMultiParty(),
400 calls.IsOutgoing(),
401 calls.IsInBandRing(),
402 calls.GetCreationTime());
403 return call;
404 }
405 return std::nullopt;
406 }
407
RegisterObserverOHOS::Bluetooth::HandsFreeUnit::impl408 void RegisterObserver(std::shared_ptr<HandsFreeUnitObserver> observer)
409 {
410 HILOGI("enter");
411 observers_.Register(observer);
412 }
413
DeregisterObserverOHOS::Bluetooth::HandsFreeUnit::impl414 void DeregisterObserver(std::shared_ptr<HandsFreeUnitObserver> observer)
415 {
416 HILOGI("enter");
417 observers_.Deregister(observer);
418 }
419 sptr<IBluetoothHfpHf> proxy_;
420 private:
421 const static int HFP_HF_SLC_STATE_DISCONNECTED = static_cast<int>(BTConnectState::DISCONNECTED);
422 const static int HFP_HF_SCO_STATE_DISCONNECTED = 3;
423
424 BluetoothObserverList<HandsFreeUnitObserver> observers_;
425 HfServiceObserver serviceObserver_ {HfServiceObserver(observers_)};
426 class HandsFreeUnitDeathRecipient;
427 sptr<HandsFreeUnitDeathRecipient> deathRecipient_;
428 };
429
430 class HandsFreeUnit::impl::HandsFreeUnitDeathRecipient final : public IRemoteObject::DeathRecipient {
431 public:
HandsFreeUnitDeathRecipient(HandsFreeUnit::impl & impl)432 explicit HandsFreeUnitDeathRecipient(HandsFreeUnit::impl &impl) : impl_(impl)
433 {};
434 ~HandsFreeUnitDeathRecipient() final = default;
435 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(HandsFreeUnitDeathRecipient);
436
OnRemoteDied(const wptr<IRemoteObject> & remote)437 void OnRemoteDied(const wptr<IRemoteObject> &remote) final
438 {
439 HILOGI("starts");
440 std::lock_guard<std::mutex> lock(g_hfpHFProxyMutex);
441 if (!impl_.proxy_) {
442 return;
443 }
444 impl_.proxy_ = nullptr;
445 }
446
447 private:
448 HandsFreeUnit::impl &impl_;
449 };
450
impl()451 HandsFreeUnit::impl::impl()
452 {
453 if (proxy_) {
454 return;
455 }
456 BluetootLoadSystemAbility::GetInstance().RegisterNotifyMsg(PROFILE_ID_HFP_HF);
457 if (!BluetootLoadSystemAbility::GetInstance().HasSubscribedBluetoothSystemAbility()) {
458 BluetootLoadSystemAbility::GetInstance().SubScribeBluetoothSystemAbility();
459 return;
460 }
461 InitHfpHfProxy();
462 }
463
~impl()464 HandsFreeUnit::impl::~impl()
465 {
466 HILOGI("enter");
467 if (proxy_ == nullptr) {
468 return;
469 }
470 proxy_->DeregisterObserver(&serviceObserver_);
471 proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
472 }
473
InitHfpHfProxy(void)474 bool HandsFreeUnit::impl::InitHfpHfProxy(void)
475 {
476 std::lock_guard<std::mutex> lock(g_hfpHFProxyMutex);
477 if (proxy_) {
478 return true;
479 }
480 HILOGI("enter");
481 proxy_ = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
482 if (!proxy_) {
483 HILOGE("get HfpHf proxy fail");
484 return false;
485 }
486
487 proxy_->RegisterObserver(&serviceObserver_);
488 deathRecipient_ = new HandsFreeUnitDeathRecipient(*this);
489 if (deathRecipient_ != nullptr) {
490 proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
491 }
492 return true;
493 }
494
HandsFreeUnit()495 HandsFreeUnit::HandsFreeUnit()
496 {
497 pimpl = std::make_unique<impl>();
498 }
499
~HandsFreeUnit()500 HandsFreeUnit::~HandsFreeUnit()
501 {}
502
Init()503 void HandsFreeUnit::Init()
504 {
505 if (!pimpl) {
506 HILOGE("fails: no pimpl");
507 return;
508 }
509 if (!pimpl->InitHfpHfProxy()) {
510 HILOGE("HfpHf proxy is nullptr");
511 return;
512 }
513 }
514
GetProfile()515 HandsFreeUnit *HandsFreeUnit::GetProfile()
516 {
517 static HandsFreeUnit instance;
518 return &instance;
519 }
520
ConnectSco(const BluetoothRemoteDevice & device)521 bool HandsFreeUnit::ConnectSco(const BluetoothRemoteDevice &device)
522 {
523 if (!IS_BT_ENABLED()) {
524 HILOGE("bluetooth is off.");
525 return false;
526 }
527
528 if (pimpl == nullptr || !pimpl->proxy_) {
529 HILOGE("pimpl or hfpHf proxy is nullptr");
530 return false;
531 }
532
533 return pimpl->ConnectSco(device);
534 }
535
DisconnectSco(const BluetoothRemoteDevice & device)536 bool HandsFreeUnit::DisconnectSco(const BluetoothRemoteDevice &device)
537 {
538 if (!IS_BT_ENABLED()) {
539 HILOGE("bluetooth is off.");
540 return false;
541 }
542
543 if (pimpl == nullptr || !pimpl->proxy_) {
544 HILOGE("pimpl or hfpHf proxy is nullptr");
545 return false;
546 }
547
548 return pimpl->DisconnectSco(device);
549 }
550
GetDevicesByStates(std::vector<int> states) const551 std::vector<BluetoothRemoteDevice> HandsFreeUnit::GetDevicesByStates(std::vector<int> states) const
552 {
553 if (!IS_BT_ENABLED()) {
554 HILOGE("bluetooth is off.");
555 return std::vector<BluetoothRemoteDevice>();
556 }
557
558 if (pimpl == nullptr || !pimpl->proxy_) {
559 HILOGE("pimpl or hfpHf proxy is nullptr");
560 return std::vector<BluetoothRemoteDevice>();
561 }
562
563 return pimpl->GetDevicesByStates(states);
564 }
565
GetDeviceState(const BluetoothRemoteDevice & device) const566 int HandsFreeUnit::GetDeviceState(const BluetoothRemoteDevice &device) const
567 {
568 if (!IS_BT_ENABLED()) {
569 HILOGE("bluetooth is off.");
570 return static_cast<int>(BTConnectState::DISCONNECTED);
571 }
572
573 if (pimpl == nullptr || !pimpl->proxy_) {
574 HILOGE("pimpl or hfpHf proxy is nullptr");
575 return static_cast<int>(BTConnectState::DISCONNECTED);
576 }
577
578 return pimpl->GetDeviceState(device);
579 }
580
GetScoState(const BluetoothRemoteDevice & device) const581 int HandsFreeUnit::GetScoState(const BluetoothRemoteDevice &device) const
582 {
583 if (!IS_BT_ENABLED()) {
584 HILOGE("bluetooth is off.");
585 return static_cast<int>(HfpScoConnectState::SCO_DISCONNECTED);
586 }
587
588 if (pimpl == nullptr || !pimpl->proxy_) {
589 HILOGE("pimpl or hfpHf proxy is nullptr");
590 return static_cast<int>(HfpScoConnectState::SCO_DISCONNECTED);
591 }
592
593 return pimpl->GetScoState(device);
594 }
595
SendDTMFTone(const BluetoothRemoteDevice & device,uint8_t code)596 bool HandsFreeUnit::SendDTMFTone(const BluetoothRemoteDevice &device, uint8_t code)
597 {
598 if (!IS_BT_ENABLED()) {
599 HILOGE("bluetooth is off.");
600 return false;
601 }
602
603 if (pimpl == nullptr || !pimpl->proxy_) {
604 HILOGE("pimpl or hfpHf proxy is nullptr");
605 return false;
606 }
607
608 return pimpl->SendDTMFTone(device, code);
609 }
610
Connect(const BluetoothRemoteDevice & device)611 bool HandsFreeUnit::Connect(const BluetoothRemoteDevice &device)
612 {
613 if (!IS_BT_ENABLED()) {
614 HILOGE("bluetooth is off.");
615 return false;
616 }
617
618 if (pimpl == nullptr || !pimpl->proxy_) {
619 HILOGE("pimpl or hfpHf proxy is nullptr");
620 return false;
621 }
622
623 return pimpl->Connect(device);
624 }
625
Disconnect(const BluetoothRemoteDevice & device)626 bool HandsFreeUnit::Disconnect(const BluetoothRemoteDevice &device)
627 {
628 if (!IS_BT_ENABLED()) {
629 HILOGE("bluetooth is off.");
630 return false;
631 }
632
633 if (pimpl == nullptr || !pimpl->proxy_) {
634 HILOGE("pimpl or hfpHf proxy is nullptr");
635 return false;
636 }
637
638 return pimpl->Disconnect(device);
639 }
640
OpenVoiceRecognition(const BluetoothRemoteDevice & device)641 bool HandsFreeUnit::OpenVoiceRecognition(const BluetoothRemoteDevice &device)
642 {
643 if (!IS_BT_ENABLED()) {
644 HILOGE("bluetooth is off.");
645 return false;
646 }
647
648 if (pimpl == nullptr || !pimpl->proxy_) {
649 HILOGE("pimpl or hfpHf proxy is nullptr");
650 return false;
651 }
652
653 return pimpl->OpenVoiceRecognition(device);
654 }
655
CloseVoiceRecognition(const BluetoothRemoteDevice & device)656 bool HandsFreeUnit::CloseVoiceRecognition(const BluetoothRemoteDevice &device)
657 {
658 if (!IS_BT_ENABLED()) {
659 HILOGE("bluetooth is off.");
660 return false;
661 }
662
663 if (pimpl == nullptr || !pimpl->proxy_) {
664 HILOGE("pimpl or hfpHf proxy is nullptr");
665 return false;
666 }
667
668 return pimpl->CloseVoiceRecognition(device);
669 }
670
GetExistingCalls(const BluetoothRemoteDevice & device)671 std::vector<HandsFreeUnitCall> HandsFreeUnit::GetExistingCalls(const BluetoothRemoteDevice &device)
672 {
673 if (!IS_BT_ENABLED()) {
674 HILOGE("bluetooth is off.");
675 return std::vector<HandsFreeUnitCall>();
676 }
677
678 if (pimpl == nullptr || !pimpl->proxy_) {
679 HILOGE("pimpl or hfpHf proxy is nullptr");
680 return std::vector<HandsFreeUnitCall>();
681 }
682
683 return pimpl->GetExistingCalls(device);
684 }
685
AcceptIncomingCall(const BluetoothRemoteDevice & device,int flag)686 bool HandsFreeUnit::AcceptIncomingCall(const BluetoothRemoteDevice &device, int flag)
687 {
688 if (!IS_BT_ENABLED()) {
689 HILOGE("bluetooth is off.");
690 return false;
691 }
692
693 if (pimpl == nullptr || !pimpl->proxy_) {
694 HILOGE("pimpl or hfpHf proxy is nullptr");
695 return false;
696 }
697
698 return pimpl->AcceptIncomingCall(device, flag);
699 }
700
HoldActiveCall(const BluetoothRemoteDevice & device)701 bool HandsFreeUnit::HoldActiveCall(const BluetoothRemoteDevice &device)
702 {
703 if (!IS_BT_ENABLED()) {
704 HILOGE("bluetooth is off.");
705 return false;
706 }
707
708 if (pimpl == nullptr || !pimpl->proxy_) {
709 HILOGE("pimpl or hfpHf proxy is nullptr");
710 return false;
711 }
712
713 return pimpl->HoldActiveCall(device);
714 }
715
RejectIncomingCall(const BluetoothRemoteDevice & device)716 bool HandsFreeUnit::RejectIncomingCall(const BluetoothRemoteDevice &device)
717 {
718 if (!IS_BT_ENABLED()) {
719 HILOGE("bluetooth is off.");
720 return false;
721 }
722
723 if (pimpl == nullptr || !pimpl->proxy_) {
724 HILOGE("pimpl or hfpHf proxy is nullptr");
725 return false;
726 }
727
728 return pimpl->RejectIncomingCall(device);
729 }
730
SendKeyPressed(const BluetoothRemoteDevice & device)731 bool HandsFreeUnit::SendKeyPressed(const BluetoothRemoteDevice &device)
732 {
733 return pimpl->SendKeyPressed(device);
734 }
735
HandleIncomingCall(const BluetoothRemoteDevice & device,int flag)736 bool HandsFreeUnit::HandleIncomingCall(const BluetoothRemoteDevice &device, int flag)
737 {
738 if (!IS_BT_ENABLED()) {
739 HILOGE("bluetooth is off.");
740 return false;
741 }
742
743 if (pimpl == nullptr || !pimpl->proxy_) {
744 HILOGE("pimpl or hfpHf proxy is nullptr");
745 return false;
746 }
747
748 return pimpl->HandleIncomingCall(device, flag);
749 }
750
HandleMultiCall(const BluetoothRemoteDevice & device,int flag,int index)751 bool HandsFreeUnit::HandleMultiCall(const BluetoothRemoteDevice &device, int flag, int index)
752 {
753 if (!IS_BT_ENABLED()) {
754 HILOGE("bluetooth is off.");
755 return false;
756 }
757
758 if (pimpl == nullptr || !pimpl->proxy_) {
759 HILOGE("pimpl or hfpHf proxy is nullptr");
760 return false;
761 }
762
763 return pimpl->HandleMultiCall(device, flag, index);
764 }
765
DialLastNumber(const BluetoothRemoteDevice & device)766 bool HandsFreeUnit::DialLastNumber(const BluetoothRemoteDevice &device)
767 {
768 if (!IS_BT_ENABLED()) {
769 HILOGE("bluetooth is off.");
770 return false;
771 }
772
773 if (pimpl == nullptr || !pimpl->proxy_) {
774 HILOGE("pimpl or hfpHf proxy is nullptr");
775 return false;
776 }
777
778 return pimpl->DialLastNumber(device);
779 }
780
DialMemory(const BluetoothRemoteDevice & device,int index)781 bool HandsFreeUnit::DialMemory(const BluetoothRemoteDevice &device, int index)
782 {
783 if (!IS_BT_ENABLED()) {
784 HILOGE("bluetooth is off.");
785 return false;
786 }
787
788 if (pimpl == nullptr || !pimpl->proxy_) {
789 HILOGE("pimpl or hfpHf proxy is nullptr");
790 return false;
791 }
792
793 return pimpl->DialMemory(device, index);
794 }
795
SendVoiceTag(const BluetoothRemoteDevice & device,int index)796 bool HandsFreeUnit::SendVoiceTag(const BluetoothRemoteDevice &device, int index)
797 {
798 return pimpl->SendVoiceTag(device, index);
799 }
800
801
FinishActiveCall(const BluetoothRemoteDevice & device,const HandsFreeUnitCall & call)802 bool HandsFreeUnit::FinishActiveCall(const BluetoothRemoteDevice &device, const HandsFreeUnitCall &call)
803 {
804 if (!IS_BT_ENABLED()) {
805 HILOGE("bluetooth is off.");
806 return false;
807 }
808
809 if (pimpl == nullptr || !pimpl->proxy_) {
810 HILOGE("pimpl or hfpHf proxy is nullptr");
811 return false;
812 }
813
814 return pimpl->FinishActiveCall(device, call);
815 }
816
StartDial(const BluetoothRemoteDevice & device,const std::string & number)817 std::optional<HandsFreeUnitCall> HandsFreeUnit::StartDial(
818 const BluetoothRemoteDevice &device, const std::string &number)
819 {
820 if (!IS_BT_ENABLED()) {
821 HILOGE("bluetooth is off.");
822 return std::nullopt;
823 }
824
825 if (pimpl == nullptr || !pimpl->proxy_) {
826 HILOGE("pimpl or hfpHf proxy is nullptr");
827 return std::nullopt;
828 }
829
830 return pimpl->StartDial(device, number);
831 }
832
RegisterObserver(HandsFreeUnitObserver * observer)833 void HandsFreeUnit::RegisterObserver(HandsFreeUnitObserver *observer)
834 {
835 std::shared_ptr<HandsFreeUnitObserver> observerPtr(observer, [](HandsFreeUnitObserver *) {});
836 return pimpl->RegisterObserver(observerPtr);
837 }
838
DeregisterObserver(HandsFreeUnitObserver * observer)839 void HandsFreeUnit::DeregisterObserver(HandsFreeUnitObserver *observer)
840 {
841 std::shared_ptr<HandsFreeUnitObserver> observerPtr(observer, [](HandsFreeUnitObserver *) {});
842 return pimpl->DeregisterObserver(observerPtr);
843 }
844 } // namespace Bluetooth
845 } // namespace OHOS