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_profile_manager.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     }
37     ~HfServiceObserver() override
38     {
39         HILOGI("enter");
40     }
41 
42     void OnConnectionStateChanged(const BluetoothRawAddress &device, int32_t state, int32_t cause) override
43     {
44         HILOGD("enter, device: %{public}s, state: %{public}d, cause: %{public}d",
45             GET_ENCRYPT_RAW_ADDR(device), state, cause);
46         BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
47         observers_.ForEach([remoteDevice, state, cause](std::shared_ptr<HandsFreeUnitObserver> observer) {
48             observer->OnConnectionStateChanged(remoteDevice, state, cause);
49         });
50     }
51 
52     void OnScoStateChanged(const BluetoothRawAddress &device, int32_t state) override
53     {
54         HILOGI("enter, device: %{public}s, state: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), state);
55         BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
56         observers_.ForEach([remoteDevice, state](std::shared_ptr<HandsFreeUnitObserver> observer) {
57             observer->OnScoStateChanged(remoteDevice, state);
58         });
59     }
60 
61     void OnCallChanged(const BluetoothRawAddress &device,
62         const BluetoothHfpHfCall &call) override
63     {
64         HILOGI("enter, device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
65         BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
66         UUID uuid = UUID::ConvertFrom128Bits(call.GetUuid().ConvertTo128Bits());
67         HandsFreeUnitCall tmpCall(call.GetRemoteDevice(),
68             call.GetId(),
69             call.GetState(),
70             call.GetNumber(),
71             uuid,
72             call.IsMultiParty(),
73             call.IsOutgoing(),
74             call.IsInBandRing(),
75             call.GetCreationTime());
76         observers_.ForEach([remoteDevice, tmpCall](std::shared_ptr<HandsFreeUnitObserver> observer) {
77             observer->OnCallChanged(remoteDevice, tmpCall);
78         });
79     }
80 
81     void OnSignalStrengthChanged(const BluetoothRawAddress &device, int32_t signal) override
82     {
83         HILOGI("enter, device: %{public}s, signal: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), signal);
84         BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
85         observers_.ForEach([remoteDevice, signal](std::shared_ptr<HandsFreeUnitObserver> observer) {
86             observer->OnSignalStrengthChanged(remoteDevice, signal);
87         });
88     }
89 
90     void OnRegistrationStatusChanged(const BluetoothRawAddress &device, int32_t status) override
91     {
92         HILOGI("enter, device: %{public}s, status: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), status);
93         BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
94         observers_.ForEach([remoteDevice, status](std::shared_ptr<HandsFreeUnitObserver> observer) {
95             observer->OnRegistrationStatusChanged(remoteDevice, status);
96         });
97     }
98 
99     void OnRoamingStatusChanged(const BluetoothRawAddress &device, int32_t status) override
100     {
101         HILOGI("enter, device: %{public}s, status: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), status);
102         BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
103         observers_.ForEach([remoteDevice, status](std::shared_ptr<HandsFreeUnitObserver> observer) {
104             observer->OnRoamingStatusChanged(remoteDevice, status);
105         });
106     }
107 
108     void OnOperatorSelectionChanged(
109         const BluetoothRawAddress &device, const std::string &name) override
110     {
111         HILOGI("enter, device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
112         BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
113         observers_.ForEach([remoteDevice, name](std::shared_ptr<HandsFreeUnitObserver> observer) {
114             observer->OnOperatorSelectionChanged(remoteDevice, name);
115         });
116     }
117 
118     void OnSubscriberNumberChanged(
119         const BluetoothRawAddress &device, const std::string &number) override
120     {
121         HILOGI("enter, device: %{public}s", GetEncryptAddr((device).GetAddress()).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 
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 
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 
ConnectScoOHOS::Bluetooth::HandsFreeUnit::impl158     bool ConnectSco(const BluetoothRemoteDevice &device)
159     {
160         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
161         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
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         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
172         if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
173             return proxy->DisconnectSco(BluetoothRawAddress(device.GetDeviceAddr()));
174         }
175         return false;
176     }
177 
GetDevicesByStatesOHOS::Bluetooth::HandsFreeUnit::impl178     std::vector<BluetoothRemoteDevice> GetDevicesByStates(std::vector<int> states)
179     {
180         HILOGI("enter");
181         std::vector<BluetoothRemoteDevice> remoteDevices;
182         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
183         if (proxy != nullptr) {
184             std::vector<BluetoothRawAddress> rawDevices;
185             std::vector<int32_t> tmpStates;
186             for (int state : states) {
187                 tmpStates.push_back((int32_t)state);
188             }
189 
190             proxy->GetDevicesByStates(tmpStates, rawDevices);
191             for (BluetoothRawAddress rawDevice : rawDevices) {
192                 BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 0);
193                 remoteDevices.push_back(remoteDevice);
194             }
195         }
196         return remoteDevices;
197     }
198 
GetDeviceStateOHOS::Bluetooth::HandsFreeUnit::impl199     int GetDeviceState(const BluetoothRemoteDevice &device)
200     {
201         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
202         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
203         if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
204             return proxy->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()));
205         }
206         return HFP_HF_SLC_STATE_DISCONNECTED;
207     }
208 
GetScoStateOHOS::Bluetooth::HandsFreeUnit::impl209     int GetScoState(const BluetoothRemoteDevice &device)
210     {
211         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
212         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
213         if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
214             return proxy->GetScoState(BluetoothRawAddress(device.GetDeviceAddr()));
215         }
216         return HFP_HF_SCO_STATE_DISCONNECTED;
217     }
218 
SendDTMFToneOHOS::Bluetooth::HandsFreeUnit::impl219     bool SendDTMFTone(const BluetoothRemoteDevice &device, uint8_t code)
220     {
221         HILOGI("enter, device: %{public}s, code: %{public}d", GET_ENCRYPT_ADDR(device), code);
222         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
223         if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
224             return proxy->SendDTMFTone(BluetoothRawAddress(device.GetDeviceAddr()), code);
225         }
226         return false;
227     }
228 
ConnectOHOS::Bluetooth::HandsFreeUnit::impl229     bool Connect(const BluetoothRemoteDevice &device)
230     {
231         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
232         bool isDiscovering = false;
233         BluetoothHost::GetDefaultHost().IsBtDiscovering(isDiscovering);
234         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
235         if (proxy != nullptr && !isDiscovering && device.IsValidBluetoothRemoteDevice()) {
236             return proxy->Connect(BluetoothRawAddress(device.GetDeviceAddr()));
237         }
238         HILOGE("fw return false!");
239         return false;
240     }
241 
DisconnectOHOS::Bluetooth::HandsFreeUnit::impl242     bool Disconnect(const BluetoothRemoteDevice &device)
243     {
244         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
245         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
246         if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
247             return proxy->Disconnect(BluetoothRawAddress(device.GetDeviceAddr()));
248         }
249         HILOGE("fw return false!");
250         return false;
251     }
252 
OpenVoiceRecognitionOHOS::Bluetooth::HandsFreeUnit::impl253     bool OpenVoiceRecognition(const BluetoothRemoteDevice &device)
254     {
255         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
256         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
257         if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
258             return proxy->OpenVoiceRecognition(BluetoothRawAddress(device.GetDeviceAddr()));
259         }
260         return false;
261     }
262 
CloseVoiceRecognitionOHOS::Bluetooth::HandsFreeUnit::impl263     bool CloseVoiceRecognition(const BluetoothRemoteDevice &device)
264     {
265         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
266         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
267         if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
268             return proxy->CloseVoiceRecognition(BluetoothRawAddress(device.GetDeviceAddr()));
269         }
270         return false;
271     }
272 
GetExistingCallsOHOS::Bluetooth::HandsFreeUnit::impl273     std::vector<HandsFreeUnitCall> GetExistingCalls(const BluetoothRemoteDevice &device)
274     {
275         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
276         std::vector<HandsFreeUnitCall> calls;
277         std::vector<BluetoothHfpHfCall> callsList;
278         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
279         if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
280             proxy->GetCurrentCallList(BluetoothRawAddress(device.GetDeviceAddr()), callsList);
281             for (BluetoothHfpHfCall call : callsList) {
282                 UUID uuid = UUID::ConvertFrom128Bits(call.GetUuid().ConvertTo128Bits());
283                 HandsFreeUnitCall tmpCall(call.GetRemoteDevice(),
284                     call.GetId(),
285                     call.GetState(),
286                     call.GetNumber(),
287                     uuid,
288                     call.IsMultiParty(),
289                     call.IsOutgoing(),
290                     call.IsInBandRing(),
291                     call.GetCreationTime());
292                 calls.push_back(tmpCall);
293             }
294         }
295         return calls;
296     }
297 
AcceptIncomingCallOHOS::Bluetooth::HandsFreeUnit::impl298     bool AcceptIncomingCall(const BluetoothRemoteDevice &device, int flag)
299     {
300         HILOGI("enter, device: %{public}s, flag: %{public}d", GET_ENCRYPT_ADDR(device), flag);
301         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
302         if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
303             return proxy->AcceptIncomingCall(BluetoothRawAddress(device.GetDeviceAddr()), (int32_t)flag);
304         }
305         return false;
306     }
307 
HoldActiveCallOHOS::Bluetooth::HandsFreeUnit::impl308     bool HoldActiveCall(const BluetoothRemoteDevice &device)
309     {
310         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
311         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
312         if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
313             return proxy->HoldActiveCall(BluetoothRawAddress(device.GetDeviceAddr()));
314         }
315         return false;
316     }
317 
RejectIncomingCallOHOS::Bluetooth::HandsFreeUnit::impl318     bool RejectIncomingCall(const BluetoothRemoteDevice &device)
319     {
320         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
321         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
322         if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
323             return proxy->RejectIncomingCall(BluetoothRawAddress(device.GetDeviceAddr()));
324         }
325         return false;
326     }
327 
SendKeyPressedOHOS::Bluetooth::HandsFreeUnit::impl328     bool SendKeyPressed(const BluetoothRemoteDevice &device)
329     {
330         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE_NAME__, __FUNCTION__);
331         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
332         if (proxy != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
333             return proxy->SendKeyPressed(BluetoothRawAddress(device.GetDeviceAddr()));
334         }
335         return false;
336     }
337 
HandleIncomingCallOHOS::Bluetooth::HandsFreeUnit::impl338     bool HandleIncomingCall(const BluetoothRemoteDevice &device, int flag)
339     {
340         HILOGI("Enter!");
341         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
342         if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
343             return proxy->HandleIncomingCall(BluetoothRawAddress(device.GetDeviceAddr()), (int32_t)flag);
344         }
345         return false;
346     }
347 
HandleMultiCallOHOS::Bluetooth::HandsFreeUnit::impl348     bool HandleMultiCall(const BluetoothRemoteDevice &device, int flag, int index)
349     {
350         HILOGI("Enter!");
351         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
352         if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
353             return proxy->HandleMultiCall(BluetoothRawAddress(device.GetDeviceAddr()), (int32_t)flag, (int32_t)index);
354         }
355         return false;
356     }
357 
DialLastNumberOHOS::Bluetooth::HandsFreeUnit::impl358     bool DialLastNumber(const BluetoothRemoteDevice &device)
359     {
360         HILOGI("Enter!");
361         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
362         if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
363             return proxy->DialLastNumber(BluetoothRawAddress(device.GetDeviceAddr()));
364         }
365         return false;
366     }
367 
DialMemoryOHOS::Bluetooth::HandsFreeUnit::impl368     bool DialMemory(const BluetoothRemoteDevice &device, int index)
369     {
370         HILOGD("Enter! index = %{public}d", index);
371         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
372         if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
373             return proxy->DialMemory(BluetoothRawAddress(device.GetDeviceAddr()), index);
374         }
375         return false;
376     }
377 
SendVoiceTagOHOS::Bluetooth::HandsFreeUnit::impl378     bool SendVoiceTag(const BluetoothRemoteDevice &device, int index)
379     {
380         HILOGD("[%{public}s]: %{public}s(): Enter! index = %{public}d", __FILE_NAME__, __FUNCTION__, index);
381         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
382         if (proxy != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
383             return proxy->SendVoiceTag(BluetoothRawAddress(device.GetDeviceAddr()), index);
384         }
385         return false;
386     }
387 
FinishActiveCallOHOS::Bluetooth::HandsFreeUnit::impl388     bool FinishActiveCall(const BluetoothRemoteDevice &device, const HandsFreeUnitCall &call)
389     {
390         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
391         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
392         if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
393             bluetooth::Uuid uuid = bluetooth::Uuid::ConvertFrom128Bits(call.GetUuid().ConvertTo128Bits());
394             bluetooth::HandsFreeUnitCalls calls(call.GetRemoteDevice(),
395                 call.GetId(),
396                 call.GetState(),
397                 call.GetNumber(),
398                 uuid,
399                 call.IsMultiParty(),
400                 call.IsOutgoing(),
401                 call.IsInBandRing(),
402                 call.GetCreationTime());
403             return proxy->FinishActiveCall(BluetoothRawAddress(device.GetDeviceAddr()), calls);
404         }
405         return false;
406     }
407 
StartDialOHOS::Bluetooth::HandsFreeUnit::impl408     std::optional<HandsFreeUnitCall> StartDial(const BluetoothRemoteDevice &device, const std::string &number)
409     {
410         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
411         sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
412         if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
413             BluetoothHfpHfCall calls;
414             proxy->StartDial(BluetoothRawAddress(device.GetDeviceAddr()), number, calls);
415             UUID uuid = UUID::ConvertFrom128Bits(calls.GetUuid().ConvertTo128Bits());
416             HandsFreeUnitCall call(calls.GetRemoteDevice(),
417                 calls.GetId(),
418                 calls.GetState(),
419                 calls.GetNumber(),
420                 uuid,
421                 calls.IsMultiParty(),
422                 calls.IsOutgoing(),
423                 calls.IsInBandRing(),
424                 calls.GetCreationTime());
425             return call;
426         }
427         return std::nullopt;
428     }
429 
RegisterObserverOHOS::Bluetooth::HandsFreeUnit::impl430     void RegisterObserver(std::shared_ptr<HandsFreeUnitObserver> observer)
431     {
432         HILOGI("enter");
433         observers_.Register(observer);
434     }
435 
DeregisterObserverOHOS::Bluetooth::HandsFreeUnit::impl436     void DeregisterObserver(std::shared_ptr<HandsFreeUnitObserver> observer)
437     {
438         HILOGI("enter");
439         observers_.Deregister(observer);
440     }
441     int32_t profileRegisterId = 0;
442 private:
443     const static int HFP_HF_SLC_STATE_DISCONNECTED = static_cast<int>(BTConnectState::DISCONNECTED);
444     const static int HFP_HF_SCO_STATE_DISCONNECTED = 3;
445 
446     BluetoothObserverList<HandsFreeUnitObserver> observers_;
447     sptr<HfServiceObserver> serviceObserver_;
448 };
449 
impl()450 HandsFreeUnit::impl::impl()
451 {
452     serviceObserver_ = new HfServiceObserver(observers_);
453     profileRegisterId = BluetoothProfileManager::GetInstance().RegisterFunc(PROFILE_HFP_HF,
454         [this](sptr<IRemoteObject> remote) {
455         sptr<IBluetoothHfpHf> proxy = iface_cast<IBluetoothHfpHf>(remote);
456         CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
457         proxy->RegisterObserver(serviceObserver_);
458     });
459 }
460 
~impl()461 HandsFreeUnit::impl::~impl()
462 {
463     HILOGI("enter");
464     BluetoothProfileManager::GetInstance().DeregisterFunc(profileRegisterId);
465     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
466     CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
467     proxy->DeregisterObserver(serviceObserver_);
468 }
469 
HandsFreeUnit()470 HandsFreeUnit::HandsFreeUnit()
471 {
472     pimpl = std::make_unique<impl>();
473 }
474 
~HandsFreeUnit()475 HandsFreeUnit::~HandsFreeUnit()
476 {}
477 
GetProfile()478 HandsFreeUnit *HandsFreeUnit::GetProfile()
479 {
480 #ifdef DTFUZZ_TEST
481     static BluetoothNoDestructor<HandsFreeUnit> instance;
482     return instance.get();
483 #else
484     static HandsFreeUnit instance;
485     return &instance;
486 #endif
487 }
488 
ConnectSco(const BluetoothRemoteDevice &device)489 bool HandsFreeUnit::ConnectSco(const BluetoothRemoteDevice &device)
490 {
491     if (!IS_BT_ENABLED()) {
492         HILOGE("bluetooth is off.");
493         return false;
494     }
495 
496     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
497     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
498 
499     return pimpl->ConnectSco(device);
500 }
501 
DisconnectSco(const BluetoothRemoteDevice &device)502 bool HandsFreeUnit::DisconnectSco(const BluetoothRemoteDevice &device)
503 {
504     if (!IS_BT_ENABLED()) {
505         HILOGE("bluetooth is off.");
506         return false;
507     }
508 
509     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
510     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
511 
512     return pimpl->DisconnectSco(device);
513 }
514 
GetDevicesByStates(std::vector<int> states) const515 std::vector<BluetoothRemoteDevice> HandsFreeUnit::GetDevicesByStates(std::vector<int> states) const
516 {
517     if (!IS_BT_ENABLED()) {
518         HILOGE("bluetooth is off.");
519         return std::vector<BluetoothRemoteDevice>();
520     }
521     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
522     CHECK_AND_RETURN_LOG_RET(proxy != nullptr,
523         std::vector<BluetoothRemoteDevice>(), "failed: no proxy");
524 
525     return pimpl->GetDevicesByStates(states);
526 }
527 
GetDeviceState(const BluetoothRemoteDevice &device) const528 int HandsFreeUnit::GetDeviceState(const BluetoothRemoteDevice &device) const
529 {
530     if (!IS_BT_ENABLED()) {
531         HILOGE("bluetooth is off.");
532         return static_cast<int>(BTConnectState::DISCONNECTED);
533     }
534     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
535     CHECK_AND_RETURN_LOG_RET(proxy != nullptr,
536         static_cast<int>(BTConnectState::DISCONNECTED), "failed: no proxy");
537 
538     return pimpl->GetDeviceState(device);
539 }
540 
GetScoState(const BluetoothRemoteDevice &device) const541 int HandsFreeUnit::GetScoState(const BluetoothRemoteDevice &device) const
542 {
543     if (!IS_BT_ENABLED()) {
544         HILOGE("bluetooth is off.");
545         return static_cast<int>(HfpScoConnectState::SCO_DISCONNECTED);
546     }
547     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
548     CHECK_AND_RETURN_LOG_RET(proxy != nullptr,
549         static_cast<int>(HfpScoConnectState::SCO_DISCONNECTED), "failed: no proxy");
550 
551     return pimpl->GetScoState(device);
552 }
553 
SendDTMFTone(const BluetoothRemoteDevice &device, uint8_t code)554 bool HandsFreeUnit::SendDTMFTone(const BluetoothRemoteDevice &device, uint8_t code)
555 {
556     if (!IS_BT_ENABLED()) {
557         HILOGE("bluetooth is off.");
558         return false;
559     }
560     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
561     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
562 
563     return pimpl->SendDTMFTone(device, code);
564 }
565 
Connect(const BluetoothRemoteDevice &device)566 bool HandsFreeUnit::Connect(const BluetoothRemoteDevice &device)
567 {
568     if (!IS_BT_ENABLED()) {
569         HILOGE("bluetooth is off.");
570         return false;
571     }
572 
573     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
574     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
575 
576     return pimpl->Connect(device);
577 }
578 
Disconnect(const BluetoothRemoteDevice &device)579 bool HandsFreeUnit::Disconnect(const BluetoothRemoteDevice &device)
580 {
581     if (!IS_BT_ENABLED()) {
582         HILOGE("bluetooth is off.");
583         return false;
584     }
585 
586     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
587     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
588 
589     return pimpl->Disconnect(device);
590 }
591 
OpenVoiceRecognition(const BluetoothRemoteDevice &device)592 bool HandsFreeUnit::OpenVoiceRecognition(const BluetoothRemoteDevice &device)
593 {
594     if (!IS_BT_ENABLED()) {
595         HILOGE("bluetooth is off.");
596         return false;
597     }
598 
599     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
600     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
601 
602     return pimpl->OpenVoiceRecognition(device);
603 }
604 
CloseVoiceRecognition(const BluetoothRemoteDevice &device)605 bool HandsFreeUnit::CloseVoiceRecognition(const BluetoothRemoteDevice &device)
606 {
607     if (!IS_BT_ENABLED()) {
608         HILOGE("bluetooth is off.");
609         return false;
610     }
611 
612     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
613     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
614 
615     return pimpl->CloseVoiceRecognition(device);
616 }
617 
GetExistingCalls(const BluetoothRemoteDevice &device)618 std::vector<HandsFreeUnitCall> HandsFreeUnit::GetExistingCalls(const BluetoothRemoteDevice &device)
619 {
620     if (!IS_BT_ENABLED()) {
621         HILOGE("bluetooth is off.");
622         return std::vector<HandsFreeUnitCall>();
623     }
624 
625     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
626     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, std::vector<HandsFreeUnitCall>(), "failed: no proxy");
627 
628     return pimpl->GetExistingCalls(device);
629 }
630 
AcceptIncomingCall(const BluetoothRemoteDevice &device, int flag)631 bool HandsFreeUnit::AcceptIncomingCall(const BluetoothRemoteDevice &device, int flag)
632 {
633     if (!IS_BT_ENABLED()) {
634         HILOGE("bluetooth is off.");
635         return false;
636     }
637     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
638     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
639 
640     return pimpl->AcceptIncomingCall(device, flag);
641 }
642 
HoldActiveCall(const BluetoothRemoteDevice &device)643 bool HandsFreeUnit::HoldActiveCall(const BluetoothRemoteDevice &device)
644 {
645     if (!IS_BT_ENABLED()) {
646         HILOGE("bluetooth is off.");
647         return false;
648     }
649 
650     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
651     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
652 
653     return pimpl->HoldActiveCall(device);
654 }
655 
RejectIncomingCall(const BluetoothRemoteDevice &device)656 bool HandsFreeUnit::RejectIncomingCall(const BluetoothRemoteDevice &device)
657 {
658     if (!IS_BT_ENABLED()) {
659         HILOGE("bluetooth is off.");
660         return false;
661     }
662 
663     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
664     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
665 
666     return pimpl->RejectIncomingCall(device);
667 }
668 
SendKeyPressed(const BluetoothRemoteDevice &device)669 bool HandsFreeUnit::SendKeyPressed(const BluetoothRemoteDevice &device)
670 {
671     return pimpl->SendKeyPressed(device);
672 }
673 
HandleIncomingCall(const BluetoothRemoteDevice &device, int flag)674 bool HandsFreeUnit::HandleIncomingCall(const BluetoothRemoteDevice &device, int flag)
675 {
676     if (!IS_BT_ENABLED()) {
677         HILOGE("bluetooth is off.");
678         return false;
679     }
680 
681     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
682     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
683 
684     return pimpl->HandleIncomingCall(device, flag);
685 }
686 
HandleMultiCall(const BluetoothRemoteDevice &device, int flag, int index)687 bool HandsFreeUnit::HandleMultiCall(const BluetoothRemoteDevice &device, int flag, int index)
688 {
689     if (!IS_BT_ENABLED()) {
690         HILOGE("bluetooth is off.");
691         return false;
692     }
693 
694     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
695     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
696 
697     return pimpl->HandleMultiCall(device, flag, index);
698 }
699 
DialLastNumber(const BluetoothRemoteDevice &device)700 bool HandsFreeUnit::DialLastNumber(const BluetoothRemoteDevice &device)
701 {
702     if (!IS_BT_ENABLED()) {
703         HILOGE("bluetooth is off.");
704         return false;
705     }
706 
707     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
708     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
709 
710     return pimpl->DialLastNumber(device);
711 }
712 
DialMemory(const BluetoothRemoteDevice &device, int index)713 bool HandsFreeUnit::DialMemory(const BluetoothRemoteDevice &device, int index)
714 {
715     if (!IS_BT_ENABLED()) {
716         HILOGE("bluetooth is off.");
717         return false;
718     }
719 
720     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
721     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
722 
723     return pimpl->DialMemory(device, index);
724 }
725 
SendVoiceTag(const BluetoothRemoteDevice &device, int index)726 bool HandsFreeUnit::SendVoiceTag(const BluetoothRemoteDevice &device, int index)
727 {
728     return pimpl->SendVoiceTag(device, index);
729 }
730 
731 
FinishActiveCall(const BluetoothRemoteDevice &device, const HandsFreeUnitCall &call)732 bool HandsFreeUnit::FinishActiveCall(const BluetoothRemoteDevice &device, const HandsFreeUnitCall &call)
733 {
734     if (!IS_BT_ENABLED()) {
735         HILOGE("bluetooth is off.");
736         return false;
737     }
738 
739     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
740     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
741 
742     return pimpl->FinishActiveCall(device, call);
743 }
744 
StartDial( const BluetoothRemoteDevice &device, const std::string &number)745 std::optional<HandsFreeUnitCall> HandsFreeUnit::StartDial(
746     const BluetoothRemoteDevice &device, const std::string &number)
747 {
748     if (!IS_BT_ENABLED()) {
749         HILOGE("bluetooth is off.");
750         return std::nullopt;
751     }
752     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
753     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, std::nullopt, "failed: no proxy");
754 
755     return pimpl->StartDial(device, number);
756 }
757 
RegisterObserver(std::shared_ptr<HandsFreeUnitObserver> observer)758 void HandsFreeUnit::RegisterObserver(std::shared_ptr<HandsFreeUnitObserver> observer)
759 {
760     HILOGD("enter");
761     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
762     pimpl->RegisterObserver(observer);
763 }
764 
DeregisterObserver(std::shared_ptr<HandsFreeUnitObserver> observer)765 void HandsFreeUnit::DeregisterObserver(std::shared_ptr<HandsFreeUnitObserver> observer)
766 {
767     HILOGD("enter");
768     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
769     pimpl->DeregisterObserver(observer);
770 }
771 }  // namespace Bluetooth
772 }  // namespace OHOS