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_log.h"
19 #include "bluetooth_utils.h"
20 #include "bluetooth_observer_list.h"
21 #include "i_bluetooth_hfp_hf.h"
22 #include "bluetooth_hfp_hf_observer_stub.h"
23 #include "i_bluetooth_host.h"
24 #include "iservice_registry.h"
25 #include "system_ability_definition.h"
26
27 namespace OHOS {
28 namespace Bluetooth {
29 class HfServiceObserver : public BluetoothHfpHfObserverStub {
30 public:
HfServiceObserver(BluetoothObserverList<HandsFreeUnitObserver> & observers)31 explicit HfServiceObserver(BluetoothObserverList<HandsFreeUnitObserver> &observers) : observers_(observers)
32 {
33 HILOGI("enter");
34 }
~HfServiceObserver()35 ~HfServiceObserver() override
36 {
37 HILOGI("enter");
38 }
39
OnConnectionStateChanged(const BluetoothRawAddress & device,int32_t state)40 void OnConnectionStateChanged(const BluetoothRawAddress &device, int32_t state) override
41 {
42 HILOGI("enter, device: %{public}s, state: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), state);
43 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
44 observers_.ForEach([remoteDevice, state](std::shared_ptr<HandsFreeUnitObserver> observer) {
45 observer->OnConnectionStateChanged(remoteDevice, state);
46 });
47 }
48
OnScoStateChanged(const BluetoothRawAddress & device,int32_t state)49 void OnScoStateChanged(const BluetoothRawAddress &device, int32_t state) override
50 {
51 HILOGI("enter, device: %{public}s, state: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), state);
52 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
53 observers_.ForEach([remoteDevice, state](std::shared_ptr<HandsFreeUnitObserver> observer) {
54 observer->OnScoStateChanged(remoteDevice, state);
55 });
56 }
57
OnCallChanged(const BluetoothRawAddress & device,const BluetoothHfpHfCall & call)58 void OnCallChanged(const BluetoothRawAddress &device,
59 const BluetoothHfpHfCall &call) override
60 {
61 HILOGI("enter, device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
62 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
63 UUID uuid = UUID::ConvertFrom128Bits(call.GetUuid().ConvertTo128Bits());
64 HandsFreeUnitCall tmpCall(call.GetRemoteDevice(),
65 call.GetId(),
66 call.GetState(),
67 call.GetNumber(),
68 uuid,
69 call.IsMultiParty(),
70 call.IsOutgoing(),
71 call.IsInBandRing(),
72 call.GetCreationTime());
73 observers_.ForEach([remoteDevice, tmpCall](std::shared_ptr<HandsFreeUnitObserver> observer) {
74 observer->OnCallChanged(remoteDevice, tmpCall);
75 });
76 }
77
OnSignalStrengthChanged(const BluetoothRawAddress & device,int32_t signal)78 void OnSignalStrengthChanged(const BluetoothRawAddress &device, int32_t signal) override
79 {
80 HILOGI("enter, device: %{public}s, signal: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), signal);
81 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
82 observers_.ForEach([remoteDevice, signal](std::shared_ptr<HandsFreeUnitObserver> observer) {
83 observer->OnSignalStrengthChanged(remoteDevice, signal);
84 });
85 }
86
OnRegistrationStatusChanged(const BluetoothRawAddress & device,int32_t status)87 void OnRegistrationStatusChanged(const BluetoothRawAddress &device, int32_t status) override
88 {
89 HILOGI("enter, device: %{public}s, status: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), status);
90 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
91 observers_.ForEach([remoteDevice, status](std::shared_ptr<HandsFreeUnitObserver> observer) {
92 observer->OnRegistrationStatusChanged(remoteDevice, status);
93 });
94 }
95
OnRoamingStatusChanged(const BluetoothRawAddress & device,int32_t status)96 void OnRoamingStatusChanged(const BluetoothRawAddress &device, int32_t status) override
97 {
98 HILOGI("enter, device: %{public}s, status: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), status);
99 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
100 observers_.ForEach([remoteDevice, status](std::shared_ptr<HandsFreeUnitObserver> observer) {
101 observer->OnRoamingStatusChanged(remoteDevice, status);
102 });
103 }
104
OnOperatorSelectionChanged(const BluetoothRawAddress & device,const std::string & name)105 void OnOperatorSelectionChanged(
106 const BluetoothRawAddress &device, const std::string &name) override
107 {
108 HILOGI("enter, device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
109 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
110 observers_.ForEach([remoteDevice, name](std::shared_ptr<HandsFreeUnitObserver> observer) {
111 observer->OnOperatorSelectionChanged(remoteDevice, name);
112 });
113 }
114
OnSubscriberNumberChanged(const BluetoothRawAddress & device,const std::string & number)115 void OnSubscriberNumberChanged(
116 const BluetoothRawAddress &device, const std::string &number) override
117 {
118 HILOGI("enter, device: %{public}s, number: %{public}s",
119 GetEncryptAddr((device).GetAddress()).c_str(), number.c_str());
120 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
121 observers_.ForEach([remoteDevice, number](std::shared_ptr<HandsFreeUnitObserver> observer) {
122 observer->OnSubscriberNumberChanged(remoteDevice, number);
123 });
124 }
125
OnVoiceRecognitionStatusChanged(const BluetoothRawAddress & device,int32_t status)126 void OnVoiceRecognitionStatusChanged(
127 const BluetoothRawAddress &device, int32_t status) override
128 {
129 HILOGI("enter, device: %{public}s, status: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), status);
130 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
131 observers_.ForEach([remoteDevice, status](std::shared_ptr<HandsFreeUnitObserver> observer) {
132 observer->OnVoiceRecognitionStatusChanged(remoteDevice, status);
133 });
134 }
135
OnInBandRingToneChanged(const BluetoothRawAddress & device,int32_t status)136 void OnInBandRingToneChanged(const BluetoothRawAddress &device, int32_t status) override
137 {
138 HILOGI("enter, device: %{public}s, status: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), status);
139 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
140 observers_.ForEach([remoteDevice, status](std::shared_ptr<HandsFreeUnitObserver> observer) {
141 observer->OnInBandRingToneChanged(remoteDevice, status);
142 });
143 }
144
145 private:
146 BluetoothObserverList<HandsFreeUnitObserver> &observers_;
147 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(HfServiceObserver);
148 };
149
150 std::string HfpHfServiceName = "bluetooth-hfp-hf-server";
151
152 struct HandsFreeUnit::impl {
153 impl();
154 ~impl();
155
ConnectScoOHOS::Bluetooth::HandsFreeUnit::impl156 bool ConnectSco(const BluetoothRemoteDevice &device)
157 {
158 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
159 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
160 return proxy_->ConnectSco(BluetoothRawAddress(device.GetDeviceAddr()));
161 }
162 return false;
163 }
164
DisconnectScoOHOS::Bluetooth::HandsFreeUnit::impl165 bool DisconnectSco(const BluetoothRemoteDevice &device)
166 {
167 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
168 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
169 return proxy_->DisconnectSco(BluetoothRawAddress(device.GetDeviceAddr()));
170 }
171 return false;
172 }
173
GetDevicesByStatesOHOS::Bluetooth::HandsFreeUnit::impl174 std::vector<BluetoothRemoteDevice> GetDevicesByStates(std::vector<int> states)
175 {
176 HILOGI("enter");
177 std::vector<BluetoothRemoteDevice> remoteDevices;
178 if (proxy_ != nullptr && IS_BT_ENABLED()) {
179 std::vector<BluetoothRawAddress> rawDevices;
180 std::vector<int32_t> tmpStates;
181 for (int state : states) {
182 tmpStates.push_back((int32_t)state);
183 }
184
185 proxy_->GetDevicesByStates(tmpStates, rawDevices);
186 for (BluetoothRawAddress rawDevice : rawDevices) {
187 BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 0);
188 remoteDevices.push_back(remoteDevice);
189 }
190 }
191 return remoteDevices;
192 }
193
GetDeviceStateOHOS::Bluetooth::HandsFreeUnit::impl194 int GetDeviceState(const BluetoothRemoteDevice &device)
195 {
196 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
197 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
198 return proxy_->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()));
199 }
200 return HFP_HF_SLC_STATE_DISCONNECTED;
201 }
202
GetScoStateOHOS::Bluetooth::HandsFreeUnit::impl203 int GetScoState(const BluetoothRemoteDevice &device)
204 {
205 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
206 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
207 return proxy_->GetScoState(BluetoothRawAddress(device.GetDeviceAddr()));
208 }
209 return HFP_HF_SCO_STATE_DISCONNECTED;
210 }
211
SendDTMFToneOHOS::Bluetooth::HandsFreeUnit::impl212 bool SendDTMFTone(const BluetoothRemoteDevice &device, uint8_t code)
213 {
214 HILOGI("enter, device: %{public}s, code: %{public}d", GET_ENCRYPT_ADDR(device), code);
215 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
216 return proxy_->SendDTMFTone(BluetoothRawAddress(device.GetDeviceAddr()), code);
217 }
218 return false;
219 }
220
ConnectOHOS::Bluetooth::HandsFreeUnit::impl221 bool Connect(const BluetoothRemoteDevice &device)
222 {
223 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
224 if (proxy_ != nullptr && IS_BT_ENABLED() && !BluetoothHost::GetDefaultHost().IsBtDiscovering() &&
225 device.IsValidBluetoothRemoteDevice()) {
226 return proxy_->Connect(BluetoothRawAddress(device.GetDeviceAddr()));
227 }
228 HILOGE("fw return false!");
229 return false;
230 }
231
DisconnectOHOS::Bluetooth::HandsFreeUnit::impl232 bool Disconnect(const BluetoothRemoteDevice &device)
233 {
234 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
235 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
236 return proxy_->Disconnect(BluetoothRawAddress(device.GetDeviceAddr()));
237 }
238 HILOGE("fw return false!");
239 return false;
240 }
241
OpenVoiceRecognitionOHOS::Bluetooth::HandsFreeUnit::impl242 bool OpenVoiceRecognition(const BluetoothRemoteDevice &device)
243 {
244 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
245 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
246 return proxy_->OpenVoiceRecognition(BluetoothRawAddress(device.GetDeviceAddr()));
247 }
248 return false;
249 }
250
CloseVoiceRecognitionOHOS::Bluetooth::HandsFreeUnit::impl251 bool CloseVoiceRecognition(const BluetoothRemoteDevice &device)
252 {
253 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
254 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
255 return proxy_->CloseVoiceRecognition(BluetoothRawAddress(device.GetDeviceAddr()));
256 }
257 return false;
258 }
259
GetExistingCallsOHOS::Bluetooth::HandsFreeUnit::impl260 std::vector<HandsFreeUnitCall> GetExistingCalls(const BluetoothRemoteDevice &device)
261 {
262 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
263 std::vector<HandsFreeUnitCall> calls;
264 std::vector<BluetoothHfpHfCall> callsList;
265 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
266 proxy_->GetCurrentCallList(BluetoothRawAddress(device.GetDeviceAddr()), callsList);
267 for (BluetoothHfpHfCall call : callsList) {
268 UUID uuid = UUID::ConvertFrom128Bits(call.GetUuid().ConvertTo128Bits());
269 HandsFreeUnitCall tmpCall(call.GetRemoteDevice(),
270 call.GetId(),
271 call.GetState(),
272 call.GetNumber(),
273 uuid,
274 call.IsMultiParty(),
275 call.IsOutgoing(),
276 call.IsInBandRing(),
277 call.GetCreationTime());
278 calls.push_back(tmpCall);
279 }
280 }
281 return calls;
282 }
283
AcceptIncomingCallOHOS::Bluetooth::HandsFreeUnit::impl284 bool AcceptIncomingCall(const BluetoothRemoteDevice &device, int flag)
285 {
286 HILOGI("enter, device: %{public}s, flag: %{public}d", GET_ENCRYPT_ADDR(device), flag);
287 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
288 return proxy_->AcceptIncomingCall(BluetoothRawAddress(device.GetDeviceAddr()), (int32_t)flag);
289 }
290 return false;
291 }
292
HoldActiveCallOHOS::Bluetooth::HandsFreeUnit::impl293 bool HoldActiveCall(const BluetoothRemoteDevice &device)
294 {
295 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
296 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
297 return proxy_->HoldActiveCall(BluetoothRawAddress(device.GetDeviceAddr()));
298 }
299 return false;
300 }
301
RejectIncomingCallOHOS::Bluetooth::HandsFreeUnit::impl302 bool RejectIncomingCall(const BluetoothRemoteDevice &device)
303 {
304 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
305 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
306 return proxy_->RejectIncomingCall(BluetoothRawAddress(device.GetDeviceAddr()));
307 }
308 return false;
309 }
310
SendKeyPressedOHOS::Bluetooth::HandsFreeUnit::impl311 bool SendKeyPressed(const BluetoothRemoteDevice &device)
312 {
313 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
314 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
315 return proxy_->SendKeyPressed(BluetoothRawAddress(device.GetDeviceAddr()));
316 }
317 return false;
318 }
319
HandleIncomingCallOHOS::Bluetooth::HandsFreeUnit::impl320 bool HandleIncomingCall(const BluetoothRemoteDevice &device, int flag)
321 {
322 HILOGI("Enter!");
323 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
324 return proxy_->HandleIncomingCall(BluetoothRawAddress(device.GetDeviceAddr()), (int32_t)flag);
325 }
326 return false;
327 }
328
HandleMultiCallOHOS::Bluetooth::HandsFreeUnit::impl329 bool HandleMultiCall(const BluetoothRemoteDevice &device, int flag, int index)
330 {
331 HILOGI("Enter!");
332 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
333 return proxy_->HandleMultiCall(BluetoothRawAddress(device.GetDeviceAddr()), (int32_t)flag, (int32_t)index);
334 }
335 return false;
336 }
337
DialLastNumberOHOS::Bluetooth::HandsFreeUnit::impl338 bool DialLastNumber(const BluetoothRemoteDevice &device)
339 {
340 HILOGI("Enter!");
341 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
342 return proxy_->DialLastNumber(BluetoothRawAddress(device.GetDeviceAddr()));
343 }
344 return false;
345 }
346
DialMemoryOHOS::Bluetooth::HandsFreeUnit::impl347 bool DialMemory(const BluetoothRemoteDevice &device, int index)
348 {
349 HILOGD("Enter! index = %{public}d", index);
350 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
351 return proxy_->DialMemory(BluetoothRawAddress(device.GetDeviceAddr()), index);
352 }
353 return false;
354 }
355
SendVoiceTagOHOS::Bluetooth::HandsFreeUnit::impl356 bool SendVoiceTag(const BluetoothRemoteDevice &device, int index)
357 {
358 HILOGD("[%{public}s]: %{public}s(): Enter! index = %{public}d", __FILE__, __FUNCTION__, index);
359 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
360 return proxy_->SendVoiceTag(BluetoothRawAddress(device.GetDeviceAddr()), index);
361 }
362 return false;
363 }
364
FinishActiveCallOHOS::Bluetooth::HandsFreeUnit::impl365 bool FinishActiveCall(const BluetoothRemoteDevice &device, const HandsFreeUnitCall &call)
366 {
367 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
368 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
369 bluetooth::Uuid uuid = bluetooth::Uuid::ConvertFrom128Bits(call.GetUuid().ConvertTo128Bits());
370 bluetooth::HandsFreeUnitCalls calls(call.GetRemoteDevice(),
371 call.GetId(),
372 call.GetState(),
373 call.GetNumber(),
374 uuid,
375 call.IsMultiParty(),
376 call.IsOutgoing(),
377 call.IsInBandRing(),
378 call.GetCreationTime());
379 return proxy_->FinishActiveCall(BluetoothRawAddress(device.GetDeviceAddr()), calls);
380 }
381 return false;
382 }
383
StartDialOHOS::Bluetooth::HandsFreeUnit::impl384 std::optional<HandsFreeUnitCall> StartDial(const BluetoothRemoteDevice &device, const std::string &number)
385 {
386 HILOGI("enter, device: %{public}s, number: %{public}s", GET_ENCRYPT_ADDR(device), number.c_str());
387 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
388 BluetoothHfpHfCall calls;
389 proxy_->StartDial(BluetoothRawAddress(device.GetDeviceAddr()), number, calls);
390 UUID uuid = UUID::ConvertFrom128Bits(calls.GetUuid().ConvertTo128Bits());
391 HandsFreeUnitCall call(calls.GetRemoteDevice(),
392 calls.GetId(),
393 calls.GetState(),
394 calls.GetNumber(),
395 uuid,
396 calls.IsMultiParty(),
397 calls.IsOutgoing(),
398 calls.IsInBandRing(),
399 calls.GetCreationTime());
400 return call;
401 }
402 return std::nullopt;
403 }
404
RegisterObserverOHOS::Bluetooth::HandsFreeUnit::impl405 void RegisterObserver(std::shared_ptr<HandsFreeUnitObserver> observer)
406 {
407 HILOGI("enter");
408 observers_.Register(observer);
409 }
410
DeregisterObserverOHOS::Bluetooth::HandsFreeUnit::impl411 void DeregisterObserver(std::shared_ptr<HandsFreeUnitObserver> observer)
412 {
413 HILOGI("enter");
414 observers_.Deregister(observer);
415 }
416
417 private:
418 const static int HFP_HF_SLC_STATE_DISCONNECTED = (int)BTConnectState::DISCONNECTED;
419 const static int HFP_HF_SCO_STATE_DISCONNECTED = 3;
420
421 BluetoothObserverList<HandsFreeUnitObserver> observers_;
422 HfServiceObserver serviceObserver_ {HfServiceObserver(observers_)};
423 sptr<IBluetoothHfpHf> proxy_;
424 class HandsFreeUnitDeathRecipient;
425 sptr<HandsFreeUnitDeathRecipient> deathRecipient_;
426 };
427
428 class HandsFreeUnit::impl::HandsFreeUnitDeathRecipient final : public IRemoteObject::DeathRecipient {
429 public:
HandsFreeUnitDeathRecipient(HandsFreeUnit::impl & impl)430 HandsFreeUnitDeathRecipient(HandsFreeUnit::impl &impl) : impl_(impl)
431 {};
432 ~HandsFreeUnitDeathRecipient() final = default;
433 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(HandsFreeUnitDeathRecipient);
434
OnRemoteDied(const wptr<IRemoteObject> & remote)435 void OnRemoteDied(const wptr<IRemoteObject> &remote) final
436 {
437 HILOGI("starts");
438 impl_.proxy_->AsObject()->RemoveDeathRecipient(impl_.deathRecipient_);
439 impl_.proxy_ = nullptr;
440 }
441
442 private:
443 HandsFreeUnit::impl &impl_;
444 };
445
impl()446 HandsFreeUnit::impl::impl()
447 {
448 HILOGI("enter");
449 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
450 sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
451
452 if (!hostRemote) {
453 HILOGE("failed: no hostRemote");
454 return;
455 }
456 sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
457 sptr<IRemoteObject> remote = hostProxy->GetProfile(PROFILE_HFP_HF);
458
459 if (!remote) {
460 HILOGE("failed: no remote");
461 return;
462 }
463 HILOGI("remote obtained");
464
465 proxy_ = iface_cast<IBluetoothHfpHf>(remote);
466 proxy_->RegisterObserver(&serviceObserver_);
467 deathRecipient_ = new HandsFreeUnitDeathRecipient(*this);
468 proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
469 }
470
~impl()471 HandsFreeUnit::impl::~impl()
472 {
473 HILOGI("enter");
474 if (proxy_ == nullptr) {
475 return;
476 }
477 proxy_->DeregisterObserver(&serviceObserver_);
478 proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
479 }
480
HandsFreeUnit()481 HandsFreeUnit::HandsFreeUnit()
482 {
483 pimpl = std::make_unique<impl>();
484 }
485
~HandsFreeUnit()486 HandsFreeUnit::~HandsFreeUnit()
487 {}
488
GetProfile()489 HandsFreeUnit *HandsFreeUnit::GetProfile()
490 {
491 static HandsFreeUnit instance;
492 return &instance;
493 }
494
ConnectSco(const BluetoothRemoteDevice & device)495 bool HandsFreeUnit::ConnectSco(const BluetoothRemoteDevice &device)
496 {
497 return pimpl->ConnectSco(device);
498 }
499
DisconnectSco(const BluetoothRemoteDevice & device)500 bool HandsFreeUnit::DisconnectSco(const BluetoothRemoteDevice &device)
501 {
502 return pimpl->DisconnectSco(device);
503 }
504
GetDevicesByStates(std::vector<int> states) const505 std::vector<BluetoothRemoteDevice> HandsFreeUnit::GetDevicesByStates(std::vector<int> states) const
506 {
507 return pimpl->GetDevicesByStates(states);
508 }
509
GetDeviceState(const BluetoothRemoteDevice & device) const510 int HandsFreeUnit::GetDeviceState(const BluetoothRemoteDevice &device) const
511 {
512 return pimpl->GetDeviceState(device);
513 }
514
GetScoState(const BluetoothRemoteDevice & device) const515 int HandsFreeUnit::GetScoState(const BluetoothRemoteDevice &device) const
516 {
517 return pimpl->GetScoState(device);
518 }
519
SendDTMFTone(const BluetoothRemoteDevice & device,uint8_t code)520 bool HandsFreeUnit::SendDTMFTone(const BluetoothRemoteDevice &device, uint8_t code)
521 {
522 return pimpl->SendDTMFTone(device, code);
523 }
524
Connect(const BluetoothRemoteDevice & device)525 bool HandsFreeUnit::Connect(const BluetoothRemoteDevice &device)
526 {
527 return pimpl->Connect(device);
528 }
529
Disconnect(const BluetoothRemoteDevice & device)530 bool HandsFreeUnit::Disconnect(const BluetoothRemoteDevice &device)
531 {
532 return pimpl->Disconnect(device);
533 }
534
OpenVoiceRecognition(const BluetoothRemoteDevice & device)535 bool HandsFreeUnit::OpenVoiceRecognition(const BluetoothRemoteDevice &device)
536 {
537 return pimpl->OpenVoiceRecognition(device);
538 }
539
CloseVoiceRecognition(const BluetoothRemoteDevice & device)540 bool HandsFreeUnit::CloseVoiceRecognition(const BluetoothRemoteDevice &device)
541 {
542 return pimpl->CloseVoiceRecognition(device);
543 }
544
GetExistingCalls(const BluetoothRemoteDevice & device)545 std::vector<HandsFreeUnitCall> HandsFreeUnit::GetExistingCalls(const BluetoothRemoteDevice &device)
546 {
547 return pimpl->GetExistingCalls(device);
548 }
549
AcceptIncomingCall(const BluetoothRemoteDevice & device,int flag)550 bool HandsFreeUnit::AcceptIncomingCall(const BluetoothRemoteDevice &device, int flag)
551 {
552 return pimpl->AcceptIncomingCall(device, flag);
553 }
554
HoldActiveCall(const BluetoothRemoteDevice & device)555 bool HandsFreeUnit::HoldActiveCall(const BluetoothRemoteDevice &device)
556 {
557 return pimpl->HoldActiveCall(device);
558 }
559
RejectIncomingCall(const BluetoothRemoteDevice & device)560 bool HandsFreeUnit::RejectIncomingCall(const BluetoothRemoteDevice &device)
561 {
562 return pimpl->RejectIncomingCall(device);
563 }
564
SendKeyPressed(const BluetoothRemoteDevice & device)565 bool HandsFreeUnit::SendKeyPressed(const BluetoothRemoteDevice &device)
566 {
567 return pimpl->SendKeyPressed(device);
568 }
569
HandleIncomingCall(const BluetoothRemoteDevice & device,int flag)570 bool HandsFreeUnit::HandleIncomingCall(const BluetoothRemoteDevice &device, int flag)
571 {
572 return pimpl->HandleIncomingCall(device, flag);
573 }
574
HandleMultiCall(const BluetoothRemoteDevice & device,int flag,int index)575 bool HandsFreeUnit::HandleMultiCall(const BluetoothRemoteDevice &device, int flag, int index)
576 {
577 return pimpl->HandleMultiCall(device, flag, index);
578 }
579
DialLastNumber(const BluetoothRemoteDevice & device)580 bool HandsFreeUnit::DialLastNumber(const BluetoothRemoteDevice &device)
581 {
582 return pimpl->DialLastNumber(device);
583 }
584
DialMemory(const BluetoothRemoteDevice & device,int index)585 bool HandsFreeUnit::DialMemory(const BluetoothRemoteDevice &device, int index)
586 {
587 return pimpl->DialMemory(device, index);
588 }
589
SendVoiceTag(const BluetoothRemoteDevice & device,int index)590 bool HandsFreeUnit::SendVoiceTag(const BluetoothRemoteDevice &device, int index)
591 {
592 return pimpl->SendVoiceTag(device, index);
593 }
594
FinishActiveCall(const BluetoothRemoteDevice & device,const HandsFreeUnitCall & call)595 bool HandsFreeUnit::FinishActiveCall(const BluetoothRemoteDevice &device, const HandsFreeUnitCall &call)
596 {
597 return pimpl->FinishActiveCall(device, call);
598 }
599
StartDial(const BluetoothRemoteDevice & device,const std::string & number)600 std::optional<HandsFreeUnitCall> HandsFreeUnit::StartDial(
601 const BluetoothRemoteDevice &device, const std::string &number)
602 {
603 return pimpl->StartDial(device, number);
604 }
605
RegisterObserver(HandsFreeUnitObserver * observer)606 void HandsFreeUnit::RegisterObserver(HandsFreeUnitObserver *observer)
607 {
608 std::shared_ptr<HandsFreeUnitObserver> observerPtr(observer, [](HandsFreeUnitObserver *) {});
609 return pimpl->RegisterObserver(observerPtr);
610 }
611
DeregisterObserver(HandsFreeUnitObserver * observer)612 void HandsFreeUnit::DeregisterObserver(HandsFreeUnitObserver *observer)
613 {
614 std::shared_ptr<HandsFreeUnitObserver> observerPtr(observer, [](HandsFreeUnitObserver *) {});
615 return pimpl->DeregisterObserver(observerPtr);
616 }
617 } // namespace Bluetooth
618 } // namespace OHOS
619