• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "bluetooth_hfp_ag.h"
17 #include <unistd.h>
18 #include "bluetooth_device.h"
19 #include "bluetooth_errorcode.h"
20 #include "bluetooth_host.h"
21 #include "bluetooth_load_system_ability.h"
22 #include "bluetooth_log.h"
23 #include "bluetooth_utils.h"
24 #include "bluetooth_observer_list.h"
25 #include "i_bluetooth_hfp_ag.h"
26 #include "bluetooth_hfp_ag_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_hfpProxyMutex;
34 class AgServiceObserver : public BluetoothHfpAgObserverStub {
35 public:
AgServiceObserver(BluetoothObserverList<HandsFreeAudioGatewayObserver> & observers)36     explicit AgServiceObserver(BluetoothObserverList<HandsFreeAudioGatewayObserver> &observers) : observers_(observers)
37     {
38         HILOGI("enter");
39     }
~AgServiceObserver()40     ~AgServiceObserver() override
41     {
42         HILOGI("enter");
43     };
44 
OnConnectionStateChanged(const BluetoothRawAddress & device,int32_t state)45     void OnConnectionStateChanged(const BluetoothRawAddress &device, int32_t state) override
46     {
47         HILOGD("hfpAg conn state, device: %{public}s, state: %{public}s",
48             GetEncryptAddr((device).GetAddress()).c_str(), GetProfileConnStateName(state).c_str());
49         BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
50         observers_.ForEach([remoteDevice, state](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
51             observer->OnConnectionStateChanged(remoteDevice, state);
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}u", GetEncryptAddr((device).GetAddress()).c_str(), state);
58         BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
59         observers_.ForEach([remoteDevice, state](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
60             observer->OnScoStateChanged(remoteDevice, state);
61         });
62     }
63 
OnActiveDeviceChanged(const BluetoothRawAddress & device)64     void OnActiveDeviceChanged(const BluetoothRawAddress &device) override
65     {
66         HILOGI("enter, device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
67         BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
68         observers_.ForEach([remoteDevice](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
69             observer->OnActiveDeviceChanged(remoteDevice);
70         });
71     }
72 
OnHfEnhancedDriverSafetyChanged(const BluetoothRawAddress & device,int32_t indValue)73     void OnHfEnhancedDriverSafetyChanged(
74         const BluetoothRawAddress &device, int32_t indValue) override
75     {
76         HILOGI("enter, device: %{public}s, indValue: %{public}d",
77             GetEncryptAddr((device).GetAddress()).c_str(), indValue);
78         BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
79         observers_.ForEach([remoteDevice, indValue](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
80             observer->OnHfEnhancedDriverSafetyChanged(remoteDevice, indValue);
81         });
82     }
83 
84 private:
85     BluetoothObserverList<HandsFreeAudioGatewayObserver> &observers_;
86     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(AgServiceObserver);
87 };
88 
89 std::string HfpAgServiceName = "bluetooth-hfp-ag-server";
90 
91 struct HandsFreeAudioGateway::impl {
92     impl();
93     ~impl();
94     bool InitHfpAgProxy(void);
95 
GetConnectedDevicesOHOS::Bluetooth::HandsFreeAudioGateway::impl96     int32_t GetConnectedDevices(std::vector<BluetoothRemoteDevice>& devices)
97     {
98         HILOGI("enter");
99         if (!proxy_) {
100             HILOGE("proxy_ is nullptr.");
101             return BT_ERR_SERVICE_DISCONNECTED;
102         }
103         std::vector<BluetoothRawAddress> ori;
104         int32_t ret = proxy_->GetConnectDevices(ori);
105         if (ret != BT_NO_ERROR) {
106             HILOGE("inner error.");
107             return ret;
108         }
109         for (auto it = ori.begin(); it != ori.end(); it++) {
110             devices.push_back(BluetoothRemoteDevice(it->GetAddress(), 0));
111         }
112         return BT_NO_ERROR;
113     }
114 
GetDevicesByStatesOHOS::Bluetooth::HandsFreeAudioGateway::impl115     std::vector<BluetoothRemoteDevice> GetDevicesByStates(std::vector<int> states)
116     {
117         HILOGI("enter");
118         std::vector<BluetoothRemoteDevice> remoteDevices;
119         if (proxy_ != nullptr) {
120             std::vector<BluetoothRawAddress> rawDevices;
121             std::vector<int32_t> tmpstates;
122             for (int state : states) {
123                 int32_t value = (int32_t)state;
124                 tmpstates.push_back(value);
125             }
126             proxy_->GetDevicesByStates(tmpstates, rawDevices);
127             for (BluetoothRawAddress rawDevice : rawDevices) {
128                 BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 0);
129                 remoteDevices.push_back(remoteDevice);
130             }
131         }
132         return remoteDevices;
133     }
134 
GetDeviceStateOHOS::Bluetooth::HandsFreeAudioGateway::impl135     int32_t GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state)
136     {
137         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
138         if (proxy_ == nullptr || !device.IsValidBluetoothRemoteDevice()) {
139             HILOGE("invalid param.");
140             return BT_ERR_INVALID_PARAM;
141         }
142 
143         return proxy_->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()), state);
144     }
145 
ConnectOHOS::Bluetooth::HandsFreeAudioGateway::impl146     int32_t Connect(const BluetoothRemoteDevice &device)
147     {
148         HILOGI("hfp connect remote device: %{public}s", GET_ENCRYPT_ADDR(device));
149         if (proxy_ == nullptr || !device.IsValidBluetoothRemoteDevice()) {
150             HILOGE("invalid param.");
151             return BT_ERR_INVALID_PARAM;
152         }
153 
154         int cod = 0;
155         int32_t err = device.GetDeviceClass(cod);
156         if (err != BT_NO_ERROR) {
157             HILOGE("GetDeviceClass Failed.");
158             return BT_ERR_INVALID_PARAM;
159         }
160         BluetoothDeviceClass devClass = BluetoothDeviceClass(cod);
161         if (!devClass.IsProfileSupported(BluetoothDevice::PROFILE_HEADSET)) {
162             HILOGE("hfp connect failed. The remote device does not support HFP service.");
163             return BT_ERR_PROFILE_DISABLED;
164         }
165         return proxy_->Connect(BluetoothRawAddress(device.GetDeviceAddr()));
166     }
167 
DisconnectOHOS::Bluetooth::HandsFreeAudioGateway::impl168     int32_t Disconnect(const BluetoothRemoteDevice &device)
169     {
170         HILOGI("hfp disconnect remote device: %{public}s", GET_ENCRYPT_ADDR(device));
171         if (proxy_ == nullptr || !device.IsValidBluetoothRemoteDevice()) {
172             HILOGE("invalid param.");
173             return BT_ERR_INVALID_PARAM;
174         }
175         return proxy_->Disconnect(BluetoothRawAddress(device.GetDeviceAddr()));
176     }
177 
GetScoStateOHOS::Bluetooth::HandsFreeAudioGateway::impl178     int GetScoState(const BluetoothRemoteDevice &device)
179     {
180         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
181         if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
182             return proxy_->GetScoState(BluetoothRawAddress(device.GetDeviceAddr()));
183         }
184         return HFP_AG_SCO_STATE_DISCONNECTED;
185     }
186 
ConnectScoOHOS::Bluetooth::HandsFreeAudioGateway::impl187     bool ConnectSco()
188     {
189         HILOGI("enter");
190         if (proxy_ != nullptr) {
191             return proxy_->ConnectSco();
192         }
193         return false;
194     }
195 
DisconnectScoOHOS::Bluetooth::HandsFreeAudioGateway::impl196     bool DisconnectSco()
197     {
198         HILOGI("enter");
199         if (proxy_ != nullptr) {
200             return proxy_->DisconnectSco();
201         }
202         return false;
203     }
204 
PhoneStateChangedOHOS::Bluetooth::HandsFreeAudioGateway::impl205     void PhoneStateChanged(
206         int numActive, int numHeld, int callState, const std::string &number, int type, const std::string &name)
207     {
208         HILOGI("numActive: %{public}d, numHeld: %{public}d, callState: %{public}d, type: %{public}d",
209             numActive, numHeld, callState, type);
210         if (proxy_ != nullptr) {
211             proxy_->PhoneStateChanged(numActive, numHeld, callState, number, type, name);
212         }
213     }
214 
ClccResponseOHOS::Bluetooth::HandsFreeAudioGateway::impl215     void ClccResponse(int index, int direction, int status, int mode, bool mpty, std::string number, int type)
216     {
217         HILOGI("enter, index: %{public}d, direction: %{public}d, status: %{public}d, mode: %{public}d, mpty: "
218             "%{public}d, type: %{public}d", index, direction, status, mode, mpty, type);
219         if (proxy_ != nullptr) {
220             proxy_->ClccResponse(index, direction, status, mode, mpty, number, type);
221         }
222     }
223 
OpenVoiceRecognitionOHOS::Bluetooth::HandsFreeAudioGateway::impl224     bool OpenVoiceRecognition(const BluetoothRemoteDevice &device)
225     {
226         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
227         if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
228             return proxy_->OpenVoiceRecognition(BluetoothRawAddress(device.GetDeviceAddr()));
229         }
230         return false;
231     }
232 
CloseVoiceRecognitionOHOS::Bluetooth::HandsFreeAudioGateway::impl233     bool CloseVoiceRecognition(const BluetoothRemoteDevice &device)
234     {
235         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
236         if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
237             return proxy_->CloseVoiceRecognition(BluetoothRawAddress(device.GetDeviceAddr()));
238         }
239         return false;
240     }
241 
SetActiveDeviceOHOS::Bluetooth::HandsFreeAudioGateway::impl242     bool SetActiveDevice(const BluetoothRemoteDevice &device)
243     {
244         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
245         if (proxy_ != nullptr && device.IsValidBluetoothRemoteDevice()) {
246             return proxy_->SetActiveDevice(BluetoothRawAddress(device.GetDeviceAddr()));
247         }
248         return false;
249     }
250 
IntoMockOHOS::Bluetooth::HandsFreeAudioGateway::impl251     bool IntoMock(const BluetoothRemoteDevice &device, int state)
252     {
253         HILOGI("enter");
254         if (proxy_ != nullptr && IS_BT_ENABLED()) {
255             return proxy_->IntoMock(BluetoothRawAddress(device.GetDeviceAddr()), state);
256         }
257         return false;
258     }
259 
SendNoCarrierOHOS::Bluetooth::HandsFreeAudioGateway::impl260     bool SendNoCarrier(const BluetoothRemoteDevice &device)
261     {
262         HILOGI("enter");
263         if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
264             return proxy_->SendNoCarrier(BluetoothRawAddress(device.GetDeviceAddr()));
265         }
266         return false;
267     }
268 
GetActiveDeviceOHOS::Bluetooth::HandsFreeAudioGateway::impl269     BluetoothRemoteDevice GetActiveDevice()
270     {
271         HILOGI("enter");
272         BluetoothRemoteDevice device;
273         if (proxy_ != nullptr) {
274             std::string address = proxy_->GetActiveDevice();
275             BluetoothRemoteDevice remoteDevice(address, 0);
276             device = remoteDevice;
277         }
278         return device;
279     }
280 
SetConnectStrategyOHOS::Bluetooth::HandsFreeAudioGateway::impl281     int SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy)
282     {
283         HILOGI("enter");
284         return proxy_->SetConnectStrategy(BluetoothRawAddress(device.GetDeviceAddr()), strategy);
285     }
286 
GetConnectStrategyOHOS::Bluetooth::HandsFreeAudioGateway::impl287     int GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const
288     {
289         HILOGI("enter");
290         return proxy_->GetConnectStrategy(BluetoothRawAddress(device.GetDeviceAddr()), strategy);
291     }
292 
RegisterObserverOHOS::Bluetooth::HandsFreeAudioGateway::impl293     void RegisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
294     {
295         HILOGI("enter");
296         observers_.Register(observer);
297     }
298 
DeregisterObserverOHOS::Bluetooth::HandsFreeAudioGateway::impl299     void DeregisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
300     {
301         HILOGI("enter");
302         observers_.Deregister(observer);
303         HILOGI("end");
304     }
305 
306     sptr<IBluetoothHfpAg> proxy_;
307 private:
308     const static int HFP_AG_SLC_STATE_DISCONNECTED = static_cast<int>(BTConnectState::DISCONNECTED);
309     const static int HFP_AG_SCO_STATE_DISCONNECTED = 3;
310 
311     BluetoothObserverList<HandsFreeAudioGatewayObserver> observers_;
312     sptr<AgServiceObserver> serviceObserver_;
313     class HandsFreeAudioGatewayDeathRecipient;
314     sptr<HandsFreeAudioGatewayDeathRecipient> deathRecipient_;
315 };
316 
317 class HandsFreeAudioGateway::impl::HandsFreeAudioGatewayDeathRecipient final : public IRemoteObject::DeathRecipient {
318 public:
HandsFreeAudioGatewayDeathRecipient(HandsFreeAudioGateway::impl & impl)319     explicit HandsFreeAudioGatewayDeathRecipient(HandsFreeAudioGateway::impl &impl) : impl_(impl)
320     {};
321     ~HandsFreeAudioGatewayDeathRecipient() final = default;
322     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(HandsFreeAudioGatewayDeathRecipient);
323 
OnRemoteDied(const wptr<IRemoteObject> & remote)324     void OnRemoteDied(const wptr<IRemoteObject> &remote) final
325     {
326         HILOGI("starts");
327         std::lock_guard<std::mutex> lock(g_hfpProxyMutex);
328         if (!impl_.proxy_) {
329             return;
330         }
331         impl_.proxy_ = nullptr;
332     }
333 
334 private:
335     HandsFreeAudioGateway::impl &impl_;
336 };
337 
impl()338 HandsFreeAudioGateway::impl::impl()
339 {
340     HILOGI("start");
341     if (proxy_) {
342         return;
343     }
344     BluetootLoadSystemAbility::GetInstance().RegisterNotifyMsg(PROFILE_ID_HFP_AG);
345     if (!BluetootLoadSystemAbility::GetInstance().HasSubscribedBluetoothSystemAbility()) {
346         BluetootLoadSystemAbility::GetInstance().SubScribeBluetoothSystemAbility();
347         return;
348     }
349     InitHfpAgProxy();
350 }
351 
~impl()352 HandsFreeAudioGateway::impl::~impl()
353 {
354     HILOGI("enter");
355     if (proxy_ == nullptr) {
356         return;
357     }
358     proxy_->DeregisterObserver(serviceObserver_);
359     proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
360 }
361 
InitHfpAgProxy(void)362 bool HandsFreeAudioGateway::impl::InitHfpAgProxy(void)
363 {
364     std::lock_guard<std::mutex> lock(g_hfpProxyMutex);
365     if (proxy_) {
366         return true;
367     }
368     HILOGI("enter");
369     proxy_ = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
370     if (!proxy_) {
371         HILOGE("get HfpAg proxy failed");
372         return false;
373     }
374 
375     serviceObserver_ = new AgServiceObserver(observers_);
376     if (serviceObserver_ != nullptr) {
377         proxy_->RegisterObserver(serviceObserver_);
378     }
379 
380     deathRecipient_ = new HandsFreeAudioGatewayDeathRecipient(*this);
381     if (deathRecipient_ != nullptr) {
382         proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
383     }
384     return true;
385 }
386 
GetProfile()387 HandsFreeAudioGateway *HandsFreeAudioGateway::GetProfile()
388 {
389     HILOGI("enter");
390     static HandsFreeAudioGateway instance;
391     return &instance;
392 }
393 
HandsFreeAudioGateway()394 HandsFreeAudioGateway::HandsFreeAudioGateway()
395 {
396     HILOGI("enter");
397     pimpl = std::make_unique<impl>();
398 }
399 
~HandsFreeAudioGateway()400 HandsFreeAudioGateway::~HandsFreeAudioGateway()
401 {
402     HILOGI("enter");
403 }
404 
Init()405 void HandsFreeAudioGateway::Init()
406 {
407     if (!pimpl) {
408         HILOGE("fails: no pimpl");
409         return;
410     }
411     if (!pimpl->InitHfpAgProxy()) {
412         HILOGE("HfpAG proxy is nullptr");
413         return;
414     }
415 }
416 
GetConnectedDevices() const417 std::vector<BluetoothRemoteDevice> HandsFreeAudioGateway::GetConnectedDevices() const
418 {
419     std::vector<BluetoothRemoteDevice> devices;
420     if (!IS_BT_ENABLED()) {
421         HILOGE("bluetooth is off.");
422         return devices;
423     }
424 
425     if (pimpl == nullptr || !pimpl->proxy_) {
426         HILOGE("pimpl or hfpAG proxy is nullptr");
427         return devices;
428     }
429 
430     pimpl->GetConnectedDevices(devices);
431     return devices;
432 }
433 
GetConnectedDevices(std::vector<BluetoothRemoteDevice> & devices)434 int32_t HandsFreeAudioGateway::GetConnectedDevices(std::vector<BluetoothRemoteDevice> &devices)
435 {
436     if (!IS_BT_ENABLED()) {
437         HILOGE("bluetooth is off.");
438         return BT_ERR_INVALID_STATE;
439     }
440 
441     if (pimpl == nullptr || !pimpl->proxy_) {
442         HILOGE("pimpl or hfpAG proxy is nullptr");
443         return BT_ERR_UNAVAILABLE_PROXY;
444     }
445 
446     return pimpl->GetConnectedDevices(devices);
447 }
448 
GetDevicesByStates(std::vector<int> states)449 std::vector<BluetoothRemoteDevice> HandsFreeAudioGateway::GetDevicesByStates(std::vector<int> states)
450 {
451     if (!IS_BT_ENABLED()) {
452         HILOGE("bluetooth is off.");
453         return std::vector<BluetoothRemoteDevice>();
454     }
455 
456     if (pimpl == nullptr || !pimpl->proxy_) {
457         HILOGE("pimpl or hfpAG proxy is nullptr");
458         return std::vector<BluetoothRemoteDevice>();
459     }
460 
461     return pimpl->GetDevicesByStates(states);
462 }
463 
GetDeviceState(const BluetoothRemoteDevice & device,int32_t & state)464 int32_t HandsFreeAudioGateway::GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state)
465 {
466     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
467     if (!IS_BT_ENABLED()) {
468         HILOGE("bluetooth is off.");
469         return BT_ERR_INVALID_STATE;
470     }
471 
472     if (pimpl == nullptr || !pimpl->proxy_) {
473         HILOGE("pimpl or hfpAG proxy is nullptr");
474         return BT_ERR_UNAVAILABLE_PROXY;
475     }
476 
477     return pimpl->GetDeviceState(device, state);
478 }
479 
Connect(const BluetoothRemoteDevice & device)480 int32_t HandsFreeAudioGateway::Connect(const BluetoothRemoteDevice &device)
481 {
482     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
483     if (!IS_BT_ENABLED()) {
484         HILOGE("bluetooth is off.");
485         return BT_ERR_INVALID_STATE;
486     }
487 
488     if (pimpl == nullptr || !pimpl->proxy_) {
489         HILOGE("pimpl or hfpAG proxy is nullptr");
490         return BT_ERR_UNAVAILABLE_PROXY;
491     }
492 
493     return pimpl->Connect(device);
494 }
495 
Disconnect(const BluetoothRemoteDevice & device)496 int32_t HandsFreeAudioGateway::Disconnect(const BluetoothRemoteDevice &device)
497 {
498     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
499     if (!IS_BT_ENABLED()) {
500         HILOGE("bluetooth is off.");
501         return BT_ERR_INVALID_STATE;
502     }
503 
504     if (pimpl == nullptr || !pimpl->proxy_) {
505         HILOGE("pimpl or hfpAG proxy is nullptr");
506         return BT_ERR_UNAVAILABLE_PROXY;
507     }
508 
509     return pimpl->Disconnect(device);
510 }
511 
GetScoState(const BluetoothRemoteDevice & device) const512 int HandsFreeAudioGateway::GetScoState(const BluetoothRemoteDevice &device) const
513 {
514     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
515     if (!IS_BT_ENABLED()) {
516         HILOGE("bluetooth is off.");
517         return static_cast<int>(HfpScoConnectState::SCO_DISCONNECTED);
518     }
519 
520     if (pimpl == nullptr || !pimpl->proxy_) {
521         HILOGE("pimpl or hfpAG proxy is nullptr");
522         return static_cast<int>(HfpScoConnectState::SCO_DISCONNECTED);
523     }
524 
525     return pimpl->GetScoState(device);
526 }
527 
ConnectSco()528 bool HandsFreeAudioGateway::ConnectSco()
529 {
530     if (!IS_BT_ENABLED()) {
531         HILOGE("bluetooth is off.");
532         return false;
533     }
534 
535     if (pimpl == nullptr || !pimpl->proxy_) {
536         HILOGE("pimpl or hfpAG proxy is nullptr");
537         return false;
538     }
539 
540     return pimpl->ConnectSco();
541 }
542 
DisconnectSco()543 bool HandsFreeAudioGateway::DisconnectSco()
544 {
545     if (!IS_BT_ENABLED()) {
546         HILOGE("bluetooth is off.");
547         return false;
548     }
549 
550     if (pimpl == nullptr || !pimpl->proxy_) {
551         HILOGE("pimpl or hfpAG proxy is nullptr");
552         return false;
553     }
554 
555     return pimpl->DisconnectSco();
556 }
557 
PhoneStateChanged(int numActive,int numHeld,int callState,const std::string & number,int type,const std::string & name)558 void HandsFreeAudioGateway::PhoneStateChanged(
559     int numActive, int numHeld, int callState, const std::string &number, int type, const std::string &name)
560 {
561     if (!IS_BT_ENABLED()) {
562         HILOGE("bluetooth is off.");
563         return;
564     }
565 
566     if (pimpl == nullptr || !pimpl->proxy_) {
567         HILOGE("pimpl or hfpAG proxy is nullptr");
568         return;
569     }
570 
571     pimpl->PhoneStateChanged(numActive, numHeld, callState, number, type, name);
572 }
573 
ClccResponse(int index,int direction,int status,int mode,bool mpty,const std::string & number,int type)574 void HandsFreeAudioGateway::ClccResponse(
575     int index, int direction, int status, int mode, bool mpty, const std::string &number, int type)
576 {
577     if (!IS_BT_ENABLED()) {
578         HILOGE("bluetooth is off.");
579         return;
580     }
581 
582     if (pimpl == nullptr || !pimpl->proxy_) {
583         HILOGE("pimpl or hfpAG proxy is nullptr");
584         return;
585     }
586 
587     pimpl->ClccResponse(index, direction, status, mode, mpty, number, type);
588 }
589 
OpenVoiceRecognition(const BluetoothRemoteDevice & device)590 bool HandsFreeAudioGateway::OpenVoiceRecognition(const BluetoothRemoteDevice &device)
591 {
592     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
593     if (!IS_BT_ENABLED()) {
594         HILOGE("bluetooth is off.");
595         return false;
596     }
597 
598     if (pimpl == nullptr || !pimpl->proxy_) {
599         HILOGE("pimpl or hfpAG proxy is nullptr");
600         return false;
601     }
602 
603     return pimpl->OpenVoiceRecognition(device);
604 }
605 
CloseVoiceRecognition(const BluetoothRemoteDevice & device)606 bool HandsFreeAudioGateway::CloseVoiceRecognition(const BluetoothRemoteDevice &device)
607 {
608     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
609     if (!IS_BT_ENABLED()) {
610         HILOGE("bluetooth is off.");
611         return false;
612     }
613 
614     if (pimpl == nullptr || !pimpl->proxy_) {
615         HILOGE("pimpl or hfpAG proxy is nullptr");
616         return false;
617     }
618 
619     return pimpl->CloseVoiceRecognition(device);
620 }
621 
SetActiveDevice(const BluetoothRemoteDevice & device)622 bool HandsFreeAudioGateway::SetActiveDevice(const BluetoothRemoteDevice &device)
623 {
624     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
625     if (!IS_BT_ENABLED()) {
626         HILOGE("bluetooth is off.");
627         return false;
628     }
629 
630     if (pimpl == nullptr || !pimpl->proxy_) {
631         HILOGE("pimpl or hfpAG proxy is nullptr");
632         return false;
633     }
634 
635     return pimpl->SetActiveDevice(device);
636 }
637 
GetActiveDevice() const638 BluetoothRemoteDevice HandsFreeAudioGateway::GetActiveDevice() const
639 {
640     BluetoothRemoteDevice device;
641     if (!IS_BT_ENABLED()) {
642         HILOGE("bluetooth is off.");
643         return device;
644     }
645 
646     if (pimpl == nullptr || !pimpl->proxy_) {
647         HILOGE("pimpl or hfpAG proxy is nullptr");
648         return device;
649     }
650 
651     device = pimpl->GetActiveDevice();
652     return device;
653 }
654 
SetConnectStrategy(const BluetoothRemoteDevice & device,int strategy)655 int HandsFreeAudioGateway::SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy)
656 {
657     HILOGI("enter, device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
658     if (!IS_BT_ENABLED()) {
659         HILOGE("bluetooth is off.");
660         return BT_ERR_INVALID_STATE;
661     }
662 
663     if (pimpl == nullptr || !pimpl->proxy_) {
664         HILOGE("pimpl or hfpAG proxy is nullptr");
665         return BT_ERR_UNAVAILABLE_PROXY;
666     }
667 
668     if ((!device.IsValidBluetoothRemoteDevice()) || (
669         (strategy != static_cast<int>(BTStrategyType::CONNECTION_ALLOWED)) &&
670         (strategy != static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN)))) {
671         HILOGI("input parameter error.");
672         return BT_ERR_INVALID_PARAM;
673     }
674     return pimpl->SetConnectStrategy(device, strategy);
675 }
676 
GetConnectStrategy(const BluetoothRemoteDevice & device,int & strategy) const677 int HandsFreeAudioGateway::GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const
678 {
679     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
680     if (!IS_BT_ENABLED()) {
681         HILOGE("bluetooth is off.");
682         return BT_ERR_INVALID_STATE;
683     }
684 
685     if (pimpl == nullptr || !pimpl->proxy_) {
686         HILOGE("pimpl or hfpAG proxy is nullptr");
687         return BT_ERR_UNAVAILABLE_PROXY;
688     }
689 
690     if (!device.IsValidBluetoothRemoteDevice()) {
691         HILOGI("input parameter error.");
692         return BT_ERR_INVALID_PARAM;
693     }
694     return pimpl->GetConnectStrategy(device, strategy);
695 }
696 
RegisterObserver(HandsFreeAudioGatewayObserver * observer)697 void HandsFreeAudioGateway::RegisterObserver(HandsFreeAudioGatewayObserver *observer)
698 {
699     HILOGI("enter");
700     std::shared_ptr<HandsFreeAudioGatewayObserver> observerPtr(observer, [](HandsFreeAudioGatewayObserver *) {});
701     pimpl->RegisterObserver(observerPtr);
702 }
703 
DeregisterObserver(HandsFreeAudioGatewayObserver * observer)704 void HandsFreeAudioGateway::DeregisterObserver(HandsFreeAudioGatewayObserver *observer)
705 {
706     HILOGI("enter");
707     std::shared_ptr<HandsFreeAudioGatewayObserver> observerPtr(observer, [](HandsFreeAudioGatewayObserver *) {});
708     if (pimpl == nullptr) {
709         HILOGI("pimpl is nullptr!");
710         return;
711     }
712     pimpl->DeregisterObserver(observerPtr);
713     HILOGI("end");
714 }
715 }  // namespace Bluetooth
716 }  // namespace OHOS