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