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