• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     }
~HfServiceObserver()37     ~HfServiceObserver() override
38     {
39         HILOGI("enter");
40     }
41 
OnConnectionStateChanged(const BluetoothRawAddress & device,int32_t state,int32_t cause)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 
OnScoStateChanged(const BluetoothRawAddress & device,int32_t state)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 
OnCallChanged(const BluetoothRawAddress & device,const BluetoothHfpHfCall & call)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 
OnSignalStrengthChanged(const BluetoothRawAddress & device,int32_t signal)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 
OnRegistrationStatusChanged(const BluetoothRawAddress & device,int32_t status)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 
OnRoamingStatusChanged(const BluetoothRawAddress & device,int32_t status)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 
OnOperatorSelectionChanged(const BluetoothRawAddress & device,const std::string & name)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 
OnSubscriberNumberChanged(const BluetoothRawAddress & device,const std::string & number)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 
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 
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     HfServiceObserver serviceObserver_ {HfServiceObserver(observers_)};
448 };
449 
impl()450 HandsFreeUnit::impl::impl()
451 {
452     profileRegisterId = BluetoothProfileManager::GetInstance().RegisterFunc(PROFILE_HFP_HF,
453         [this](sptr<IRemoteObject> remote) {
454         sptr<IBluetoothHfpHf> proxy = iface_cast<IBluetoothHfpHf>(remote);
455         CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
456         proxy->RegisterObserver(&serviceObserver_);
457     });
458 }
459 
~impl()460 HandsFreeUnit::impl::~impl()
461 {
462     HILOGI("enter");
463     BluetoothProfileManager::GetInstance().DeregisterFunc(profileRegisterId);
464     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
465     CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
466     proxy->DeregisterObserver(&serviceObserver_);
467 }
468 
HandsFreeUnit()469 HandsFreeUnit::HandsFreeUnit()
470 {
471     pimpl = std::make_unique<impl>();
472 }
473 
~HandsFreeUnit()474 HandsFreeUnit::~HandsFreeUnit()
475 {}
476 
GetProfile()477 HandsFreeUnit *HandsFreeUnit::GetProfile()
478 {
479 #ifdef DTFUZZ_TEST
480     static BluetoothNoDestructor<HandsFreeUnit> instance;
481     return instance.get();
482 #else
483     static HandsFreeUnit instance;
484     return &instance;
485 #endif
486 }
487 
ConnectSco(const BluetoothRemoteDevice & device)488 bool HandsFreeUnit::ConnectSco(const BluetoothRemoteDevice &device)
489 {
490     if (!IS_BT_ENABLED()) {
491         HILOGE("bluetooth is off.");
492         return false;
493     }
494 
495     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
496     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
497 
498     return pimpl->ConnectSco(device);
499 }
500 
DisconnectSco(const BluetoothRemoteDevice & device)501 bool HandsFreeUnit::DisconnectSco(const BluetoothRemoteDevice &device)
502 {
503     if (!IS_BT_ENABLED()) {
504         HILOGE("bluetooth is off.");
505         return false;
506     }
507 
508     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
509     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
510 
511     return pimpl->DisconnectSco(device);
512 }
513 
GetDevicesByStates(std::vector<int> states) const514 std::vector<BluetoothRemoteDevice> HandsFreeUnit::GetDevicesByStates(std::vector<int> states) const
515 {
516     if (!IS_BT_ENABLED()) {
517         HILOGE("bluetooth is off.");
518         return std::vector<BluetoothRemoteDevice>();
519     }
520     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
521     CHECK_AND_RETURN_LOG_RET(proxy != nullptr,
522         std::vector<BluetoothRemoteDevice>(), "failed: no proxy");
523 
524     return pimpl->GetDevicesByStates(states);
525 }
526 
GetDeviceState(const BluetoothRemoteDevice & device) const527 int HandsFreeUnit::GetDeviceState(const BluetoothRemoteDevice &device) const
528 {
529     if (!IS_BT_ENABLED()) {
530         HILOGE("bluetooth is off.");
531         return static_cast<int>(BTConnectState::DISCONNECTED);
532     }
533     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
534     CHECK_AND_RETURN_LOG_RET(proxy != nullptr,
535         static_cast<int>(BTConnectState::DISCONNECTED), "failed: no proxy");
536 
537     return pimpl->GetDeviceState(device);
538 }
539 
GetScoState(const BluetoothRemoteDevice & device) const540 int HandsFreeUnit::GetScoState(const BluetoothRemoteDevice &device) const
541 {
542     if (!IS_BT_ENABLED()) {
543         HILOGE("bluetooth is off.");
544         return static_cast<int>(HfpScoConnectState::SCO_DISCONNECTED);
545     }
546     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
547     CHECK_AND_RETURN_LOG_RET(proxy != nullptr,
548         static_cast<int>(HfpScoConnectState::SCO_DISCONNECTED), "failed: no proxy");
549 
550     return pimpl->GetScoState(device);
551 }
552 
SendDTMFTone(const BluetoothRemoteDevice & device,uint8_t code)553 bool HandsFreeUnit::SendDTMFTone(const BluetoothRemoteDevice &device, uint8_t code)
554 {
555     if (!IS_BT_ENABLED()) {
556         HILOGE("bluetooth is off.");
557         return false;
558     }
559     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
560     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
561 
562     return pimpl->SendDTMFTone(device, code);
563 }
564 
Connect(const BluetoothRemoteDevice & device)565 bool HandsFreeUnit::Connect(const BluetoothRemoteDevice &device)
566 {
567     if (!IS_BT_ENABLED()) {
568         HILOGE("bluetooth is off.");
569         return false;
570     }
571 
572     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
573     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
574 
575     return pimpl->Connect(device);
576 }
577 
Disconnect(const BluetoothRemoteDevice & device)578 bool HandsFreeUnit::Disconnect(const BluetoothRemoteDevice &device)
579 {
580     if (!IS_BT_ENABLED()) {
581         HILOGE("bluetooth is off.");
582         return false;
583     }
584 
585     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
586     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
587 
588     return pimpl->Disconnect(device);
589 }
590 
OpenVoiceRecognition(const BluetoothRemoteDevice & device)591 bool HandsFreeUnit::OpenVoiceRecognition(const BluetoothRemoteDevice &device)
592 {
593     if (!IS_BT_ENABLED()) {
594         HILOGE("bluetooth is off.");
595         return false;
596     }
597 
598     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
599     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
600 
601     return pimpl->OpenVoiceRecognition(device);
602 }
603 
CloseVoiceRecognition(const BluetoothRemoteDevice & device)604 bool HandsFreeUnit::CloseVoiceRecognition(const BluetoothRemoteDevice &device)
605 {
606     if (!IS_BT_ENABLED()) {
607         HILOGE("bluetooth is off.");
608         return false;
609     }
610 
611     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
612     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
613 
614     return pimpl->CloseVoiceRecognition(device);
615 }
616 
GetExistingCalls(const BluetoothRemoteDevice & device)617 std::vector<HandsFreeUnitCall> HandsFreeUnit::GetExistingCalls(const BluetoothRemoteDevice &device)
618 {
619     if (!IS_BT_ENABLED()) {
620         HILOGE("bluetooth is off.");
621         return std::vector<HandsFreeUnitCall>();
622     }
623 
624     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
625     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, std::vector<HandsFreeUnitCall>(), "failed: no proxy");
626 
627     return pimpl->GetExistingCalls(device);
628 }
629 
AcceptIncomingCall(const BluetoothRemoteDevice & device,int flag)630 bool HandsFreeUnit::AcceptIncomingCall(const BluetoothRemoteDevice &device, int flag)
631 {
632     if (!IS_BT_ENABLED()) {
633         HILOGE("bluetooth is off.");
634         return false;
635     }
636     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
637     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
638 
639     return pimpl->AcceptIncomingCall(device, flag);
640 }
641 
HoldActiveCall(const BluetoothRemoteDevice & device)642 bool HandsFreeUnit::HoldActiveCall(const BluetoothRemoteDevice &device)
643 {
644     if (!IS_BT_ENABLED()) {
645         HILOGE("bluetooth is off.");
646         return false;
647     }
648 
649     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
650     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
651 
652     return pimpl->HoldActiveCall(device);
653 }
654 
RejectIncomingCall(const BluetoothRemoteDevice & device)655 bool HandsFreeUnit::RejectIncomingCall(const BluetoothRemoteDevice &device)
656 {
657     if (!IS_BT_ENABLED()) {
658         HILOGE("bluetooth is off.");
659         return false;
660     }
661 
662     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
663     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
664 
665     return pimpl->RejectIncomingCall(device);
666 }
667 
SendKeyPressed(const BluetoothRemoteDevice & device)668 bool HandsFreeUnit::SendKeyPressed(const BluetoothRemoteDevice &device)
669 {
670     return pimpl->SendKeyPressed(device);
671 }
672 
HandleIncomingCall(const BluetoothRemoteDevice & device,int flag)673 bool HandsFreeUnit::HandleIncomingCall(const BluetoothRemoteDevice &device, int flag)
674 {
675     if (!IS_BT_ENABLED()) {
676         HILOGE("bluetooth is off.");
677         return false;
678     }
679 
680     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
681     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
682 
683     return pimpl->HandleIncomingCall(device, flag);
684 }
685 
HandleMultiCall(const BluetoothRemoteDevice & device,int flag,int index)686 bool HandsFreeUnit::HandleMultiCall(const BluetoothRemoteDevice &device, int flag, int index)
687 {
688     if (!IS_BT_ENABLED()) {
689         HILOGE("bluetooth is off.");
690         return false;
691     }
692 
693     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
694     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
695 
696     return pimpl->HandleMultiCall(device, flag, index);
697 }
698 
DialLastNumber(const BluetoothRemoteDevice & device)699 bool HandsFreeUnit::DialLastNumber(const BluetoothRemoteDevice &device)
700 {
701     if (!IS_BT_ENABLED()) {
702         HILOGE("bluetooth is off.");
703         return false;
704     }
705 
706     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
707     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
708 
709     return pimpl->DialLastNumber(device);
710 }
711 
DialMemory(const BluetoothRemoteDevice & device,int index)712 bool HandsFreeUnit::DialMemory(const BluetoothRemoteDevice &device, int index)
713 {
714     if (!IS_BT_ENABLED()) {
715         HILOGE("bluetooth is off.");
716         return false;
717     }
718 
719     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
720     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
721 
722     return pimpl->DialMemory(device, index);
723 }
724 
SendVoiceTag(const BluetoothRemoteDevice & device,int index)725 bool HandsFreeUnit::SendVoiceTag(const BluetoothRemoteDevice &device, int index)
726 {
727     return pimpl->SendVoiceTag(device, index);
728 }
729 
730 
FinishActiveCall(const BluetoothRemoteDevice & device,const HandsFreeUnitCall & call)731 bool HandsFreeUnit::FinishActiveCall(const BluetoothRemoteDevice &device, const HandsFreeUnitCall &call)
732 {
733     if (!IS_BT_ENABLED()) {
734         HILOGE("bluetooth is off.");
735         return false;
736     }
737 
738     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
739     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
740 
741     return pimpl->FinishActiveCall(device, call);
742 }
743 
StartDial(const BluetoothRemoteDevice & device,const std::string & number)744 std::optional<HandsFreeUnitCall> HandsFreeUnit::StartDial(
745     const BluetoothRemoteDevice &device, const std::string &number)
746 {
747     if (!IS_BT_ENABLED()) {
748         HILOGE("bluetooth is off.");
749         return std::nullopt;
750     }
751     sptr<IBluetoothHfpHf> proxy = GetRemoteProxy<IBluetoothHfpHf>(PROFILE_HFP_HF);
752     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, std::nullopt, "failed: no proxy");
753 
754     return pimpl->StartDial(device, number);
755 }
756 
RegisterObserver(std::shared_ptr<HandsFreeUnitObserver> observer)757 void HandsFreeUnit::RegisterObserver(std::shared_ptr<HandsFreeUnitObserver> observer)
758 {
759     HILOGD("enter");
760     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
761     pimpl->RegisterObserver(observer);
762 }
763 
DeregisterObserver(std::shared_ptr<HandsFreeUnitObserver> observer)764 void HandsFreeUnit::DeregisterObserver(std::shared_ptr<HandsFreeUnitObserver> observer)
765 {
766     HILOGD("enter");
767     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
768     pimpl->DeregisterObserver(observer);
769 }
770 }  // namespace Bluetooth
771 }  // namespace OHOS