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