• 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_host.h"
20 #include "bluetooth_profile_manager.h"
21 #include "bluetooth_log.h"
22 #include "bluetooth_utils.h"
23 #include "bluetooth_observer_list.h"
24 #include "bluetooth_phone_state.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         HILOGD("enter");
39     }
~AgServiceObserver()40     ~AgServiceObserver() override
41     {
42         HILOGD("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 
OnHfpStackChanged(const BluetoothRawAddress & device,int32_t action)84     void OnHfpStackChanged(const BluetoothRawAddress &device, int32_t action) override
85     {
86         HILOGI("enter, device: %{public}s, action: %{public}s",
87             GET_ENCRYPT_RAW_ADDR(device), GetUpdateOutputStackActionName(action).c_str());
88         BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
89         observers_.ForEach([remoteDevice, action](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
90             observer->OnHfpStackChanged(remoteDevice, action);
91         });
92     }
93 
94 private:
95     BluetoothObserverList<HandsFreeAudioGatewayObserver> &observers_;
96     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(AgServiceObserver);
97 };
98 
99 std::string HfpAgServiceName = "bluetooth-hfp-ag-server";
100 
101 struct HandsFreeAudioGateway::impl {
102     impl();
103     ~impl();
104 
GetConnectedDevicesOHOS::Bluetooth::HandsFreeAudioGateway::impl105     int32_t GetConnectedDevices(std::vector<BluetoothRemoteDevice>& devices)
106     {
107         HILOGD("enter");
108         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
109         CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_SERVICE_DISCONNECTED, "failed: no proxy");
110         std::vector<BluetoothRawAddress> ori;
111         int32_t ret = proxy->GetConnectDevices(ori);
112         if (ret != BT_NO_ERROR) {
113             HILOGE("inner error.");
114             return ret;
115         }
116         for (auto it = ori.begin(); it != ori.end(); it++) {
117             devices.push_back(BluetoothRemoteDevice(it->GetAddress(), 0));
118         }
119         return BT_NO_ERROR;
120     }
121 
GetDevicesByStatesOHOS::Bluetooth::HandsFreeAudioGateway::impl122     std::vector<BluetoothRemoteDevice> GetDevicesByStates(std::vector<int> states)
123     {
124         HILOGD("enter");
125         std::vector<BluetoothRemoteDevice> remoteDevices;
126         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
127         if (proxy != nullptr) {
128             std::vector<BluetoothRawAddress> rawDevices;
129             std::vector<int32_t> tmpstates;
130             for (int state : states) {
131                 int32_t value = (int32_t)state;
132                 tmpstates.push_back(value);
133             }
134             proxy->GetDevicesByStates(tmpstates, rawDevices);
135             for (BluetoothRawAddress rawDevice : rawDevices) {
136                 BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 0);
137                 remoteDevices.push_back(remoteDevice);
138             }
139         }
140         return remoteDevices;
141     }
142 
GetDeviceStateOHOS::Bluetooth::HandsFreeAudioGateway::impl143     int32_t GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state)
144     {
145         HILOGD("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
146         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
147         if (proxy == nullptr || !device.IsValidBluetoothRemoteDevice()) {
148             HILOGE("invalid param.");
149             return BT_ERR_INVALID_PARAM;
150         }
151 
152         return proxy->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()), state);
153     }
154 
ConnectOHOS::Bluetooth::HandsFreeAudioGateway::impl155     int32_t Connect(const BluetoothRemoteDevice &device)
156     {
157         HILOGI("hfp connect remote device: %{public}s", GET_ENCRYPT_ADDR(device));
158         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
159         if (proxy == nullptr || !device.IsValidBluetoothRemoteDevice()) {
160             HILOGE("invalid param.");
161             return BT_ERR_INVALID_PARAM;
162         }
163         return proxy->Connect(BluetoothRawAddress(device.GetDeviceAddr()));
164     }
165 
DisconnectOHOS::Bluetooth::HandsFreeAudioGateway::impl166     int32_t Disconnect(const BluetoothRemoteDevice &device)
167     {
168         HILOGI("hfp disconnect remote device: %{public}s", GET_ENCRYPT_ADDR(device));
169         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
170         if (proxy == nullptr || !device.IsValidBluetoothRemoteDevice()) {
171             HILOGE("invalid param.");
172             return BT_ERR_INVALID_PARAM;
173         }
174         return proxy->Disconnect(BluetoothRawAddress(device.GetDeviceAddr()));
175     }
176 
GetScoStateOHOS::Bluetooth::HandsFreeAudioGateway::impl177     int GetScoState(const BluetoothRemoteDevice &device)
178     {
179         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
180         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
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     int32_t ConnectSco(uint8_t callType)
188     {
189         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
190         CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_PARAM, "failed: no proxy");
191         return proxy->ConnectSco(callType);
192     }
193 
DisconnectScoOHOS::Bluetooth::HandsFreeAudioGateway::impl194     int32_t DisconnectSco(uint8_t callType)
195     {
196         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
197         CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_PARAM, "failed: no proxy");
198         return proxy->DisconnectSco(callType);
199     }
200 
ConnectScoOHOS::Bluetooth::HandsFreeAudioGateway::impl201     bool ConnectSco()
202     {
203         HILOGD("enter");
204         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
205         CHECK_AND_RETURN_LOG_RET(proxy != nullptr, proxy->ConnectSco(), "failed: no proxy");
206         return false;
207     }
208 
DisconnectScoOHOS::Bluetooth::HandsFreeAudioGateway::impl209     bool DisconnectSco()
210     {
211         HILOGD("enter");
212         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
213         CHECK_AND_RETURN_LOG_RET(proxy != nullptr, proxy->DisconnectSco(), "failed: no proxy");
214         return false;
215     }
216 
IsValidCallTypeOHOS::Bluetooth::HandsFreeAudioGateway::impl217     bool IsValidCallType(uint8_t callType)
218     {
219         if (callType == static_cast<uint8_t>(BTCallType::CALL_TYPE_CELLULAR) ||
220             callType == static_cast<uint8_t>(BTCallType::CALL_TYPE_VIRTUAL) ||
221             callType == static_cast<uint8_t>(BTCallType::CALL_TYPE_RECOGNITION)) {
222             return true;
223         }
224         return false;
225     }
226 
PhoneStateChangedOHOS::Bluetooth::HandsFreeAudioGateway::impl227     void PhoneStateChanged(BluetoothPhoneState &phoneState)
228     {
229         HILOGI("numActive: %{public}d, numHeld: %{public}d, callState: %{public}d, type: %{public}d",
230             phoneState.GetActiveNum(), phoneState.GetHeldNum(), phoneState.GetCallState(), phoneState.GetCallType());
231         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
232         CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is null");
233         proxy->PhoneStateChanged(phoneState);
234     }
235 
ClccResponseOHOS::Bluetooth::HandsFreeAudioGateway::impl236     void ClccResponse(int index, int direction, int status, int mode, bool mpty, std::string number, int type)
237     {
238         HILOGI("enter, index: %{public}d, direction: %{public}d, status: %{public}d, mode: %{public}d, mpty: "
239             "%{public}d, type: %{public}d", index, direction, status, mode, mpty, type);
240         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
241         CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is null");
242         proxy->ClccResponse(index, direction, status, mode, mpty, number, type);
243     }
244 
OpenVoiceRecognitionOHOS::Bluetooth::HandsFreeAudioGateway::impl245     bool OpenVoiceRecognition(const BluetoothRemoteDevice &device)
246     {
247         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
248         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
249         if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
250             return proxy->OpenVoiceRecognition(BluetoothRawAddress(device.GetDeviceAddr()));
251         }
252         return false;
253     }
254 
CloseVoiceRecognitionOHOS::Bluetooth::HandsFreeAudioGateway::impl255     bool CloseVoiceRecognition(const BluetoothRemoteDevice &device)
256     {
257         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
258         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
259         if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
260             return proxy->CloseVoiceRecognition(BluetoothRawAddress(device.GetDeviceAddr()));
261         }
262         return false;
263     }
264 
SetActiveDeviceOHOS::Bluetooth::HandsFreeAudioGateway::impl265     bool SetActiveDevice(const BluetoothRemoteDevice &device)
266     {
267         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
268         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
269         if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
270             return proxy->SetActiveDevice(BluetoothRawAddress(device.GetDeviceAddr()));
271         }
272         return false;
273     }
274 
IntoMockOHOS::Bluetooth::HandsFreeAudioGateway::impl275     bool IntoMock(const BluetoothRemoteDevice &device, int state)
276     {
277         HILOGD("enter");
278         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
279         if (proxy != nullptr && IS_BT_ENABLED()) {
280             return proxy->IntoMock(BluetoothRawAddress(device.GetDeviceAddr()), state);
281         }
282         return false;
283     }
284 
SendNoCarrierOHOS::Bluetooth::HandsFreeAudioGateway::impl285     bool SendNoCarrier(const BluetoothRemoteDevice &device)
286     {
287         HILOGD("enter");
288         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
289         if (proxy != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
290             return proxy->SendNoCarrier(BluetoothRawAddress(device.GetDeviceAddr()));
291         }
292         return false;
293     }
294 
GetActiveDeviceOHOS::Bluetooth::HandsFreeAudioGateway::impl295     BluetoothRemoteDevice GetActiveDevice()
296     {
297         HILOGD("enter");
298         BluetoothRemoteDevice device;
299         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
300         if (proxy != nullptr) {
301             std::string address = proxy->GetActiveDevice();
302             BluetoothRemoteDevice remoteDevice(address, 0);
303             device = remoteDevice;
304         }
305         return device;
306     }
307 
SetConnectStrategyOHOS::Bluetooth::HandsFreeAudioGateway::impl308     int SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy)
309     {
310         HILOGD("enter");
311         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
312         CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is null");
313         return proxy->SetConnectStrategy(BluetoothRawAddress(device.GetDeviceAddr()), strategy);
314     }
315 
GetConnectStrategyOHOS::Bluetooth::HandsFreeAudioGateway::impl316     int GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const
317     {
318         HILOGD("enter");
319         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
320         CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is null");
321         return proxy->GetConnectStrategy(BluetoothRawAddress(device.GetDeviceAddr()), strategy);
322     }
323 
IsInbandRingingEnabledOHOS::Bluetooth::HandsFreeAudioGateway::impl324     int IsInbandRingingEnabled(bool &isEnabled) const
325     {
326         HILOGI("enter");
327         sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
328         CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is null");
329         return proxy->IsInbandRingingEnabled(isEnabled);
330     }
331 
RegisterObserverOHOS::Bluetooth::HandsFreeAudioGateway::impl332     void RegisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
333     {
334         HILOGD("enter");
335         observers_.Register(observer);
336     }
337 
DeregisterObserverOHOS::Bluetooth::HandsFreeAudioGateway::impl338     void DeregisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
339     {
340         HILOGD("enter");
341         observers_.Deregister(observer);
342         HILOGI("end");
343     }
344 
345     int32_t profileRegisterId = 0;
346 private:
347     const static int HFP_AG_SLC_STATE_DISCONNECTED = static_cast<int>(BTConnectState::DISCONNECTED);
348     const static int HFP_AG_SCO_STATE_DISCONNECTED = 3;
349 
350     BluetoothObserverList<HandsFreeAudioGatewayObserver> observers_;
351     sptr<AgServiceObserver> serviceObserver_;
352 };
353 
impl()354 HandsFreeAudioGateway::impl::impl()
355 {
356     serviceObserver_ = new AgServiceObserver(observers_);
357     profileRegisterId = DelayedSingleton<BluetoothProfileManager>::GetInstance()->RegisterFunc(PROFILE_HFP_AG,
358         [this](sptr<IRemoteObject> remote) {
359         sptr<IBluetoothHfpAg> proxy = iface_cast<IBluetoothHfpAg>(remote);
360         CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
361         proxy->RegisterObserver(serviceObserver_);
362     });
363 }
364 
~impl()365 HandsFreeAudioGateway::impl::~impl()
366 {
367     HILOGD("enter");
368     DelayedSingleton<BluetoothProfileManager>::GetInstance()->DeregisterFunc(profileRegisterId);
369     sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
370     CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
371     proxy->DeregisterObserver(serviceObserver_);
372 }
373 
GetProfile()374 HandsFreeAudioGateway *HandsFreeAudioGateway::GetProfile()
375 {
376     HILOGD("enter");
377     static HandsFreeAudioGateway instance;
378     return &instance;
379 }
380 
HandsFreeAudioGateway()381 HandsFreeAudioGateway::HandsFreeAudioGateway()
382 {
383     HILOGD("enter");
384     pimpl = std::make_unique<impl>();
385 }
386 
~HandsFreeAudioGateway()387 HandsFreeAudioGateway::~HandsFreeAudioGateway()
388 {
389     HILOGD("enter");
390 }
391 
GetConnectedDevices() const392 std::vector<BluetoothRemoteDevice> HandsFreeAudioGateway::GetConnectedDevices() const
393 {
394     std::vector<BluetoothRemoteDevice> devices;
395     if (!IS_BT_ENABLED()) {
396         HILOGE("bluetooth is off.");
397         return devices;
398     }
399     sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
400     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, devices, "failed: no proxy");
401 
402     pimpl->GetConnectedDevices(devices);
403     return devices;
404 }
405 
GetConnectedDevices(std::vector<BluetoothRemoteDevice> & devices)406 int32_t HandsFreeAudioGateway::GetConnectedDevices(std::vector<BluetoothRemoteDevice> &devices)
407 {
408     if (!IS_BT_ENABLED()) {
409         HILOGE("bluetooth is off.");
410         return BT_ERR_INVALID_STATE;
411     }
412 
413     sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
414     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
415 
416     return pimpl->GetConnectedDevices(devices);
417 }
418 
GetDevicesByStates(std::vector<int> states)419 std::vector<BluetoothRemoteDevice> HandsFreeAudioGateway::GetDevicesByStates(std::vector<int> states)
420 {
421     if (!IS_BT_ENABLED()) {
422         HILOGE("bluetooth is off.");
423         return std::vector<BluetoothRemoteDevice>();
424     }
425     sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
426     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, std::vector<BluetoothRemoteDevice>(), "failed: no proxy");
427 
428     return pimpl->GetDevicesByStates(states);
429 }
430 
GetDeviceState(const BluetoothRemoteDevice & device,int32_t & state)431 int32_t HandsFreeAudioGateway::GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state)
432 {
433     HILOGD("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
434     if (!IS_BT_ENABLED()) {
435         HILOGE("bluetooth is off.");
436         return BT_ERR_INVALID_STATE;
437     }
438 
439     sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
440     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
441 
442     return pimpl->GetDeviceState(device, state);
443 }
444 
Connect(const BluetoothRemoteDevice & device)445 int32_t HandsFreeAudioGateway::Connect(const BluetoothRemoteDevice &device)
446 {
447     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
448     if (!IS_BT_ENABLED()) {
449         HILOGE("bluetooth is off.");
450         return BT_ERR_INVALID_STATE;
451     }
452 
453     sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
454     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
455 
456     return pimpl->Connect(device);
457 }
458 
Disconnect(const BluetoothRemoteDevice & device)459 int32_t HandsFreeAudioGateway::Disconnect(const BluetoothRemoteDevice &device)
460 {
461     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
462     if (!IS_BT_ENABLED()) {
463         HILOGE("bluetooth is off.");
464         return BT_ERR_INVALID_STATE;
465     }
466     sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
467     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
468 
469     return pimpl->Disconnect(device);
470 }
471 
GetScoState(const BluetoothRemoteDevice & device) const472 int HandsFreeAudioGateway::GetScoState(const BluetoothRemoteDevice &device) const
473 {
474     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
475     if (!IS_BT_ENABLED()) {
476         HILOGE("bluetooth is off.");
477         return static_cast<int>(HfpScoConnectState::SCO_DISCONNECTED);
478     }
479     sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
480     CHECK_AND_RETURN_LOG_RET(proxy != nullptr,
481         static_cast<int>(HfpScoConnectState::SCO_DISCONNECTED), "failed: no proxy");
482 
483     return pimpl->GetScoState(device);
484 }
485 
ConnectSco(uint8_t callType)486 int32_t HandsFreeAudioGateway::ConnectSco(uint8_t callType)
487 {
488     HILOGI("enter, callType: %{public}d", callType);
489     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
490     sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
491     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY,
492         "hfpAG proxy is nullptr.");
493     CHECK_AND_RETURN_LOG_RET((pimpl->IsValidCallType(callType)), BT_ERR_INVALID_PARAM,
494         "connect sco call type error.");
495     return pimpl->ConnectSco(callType);
496 }
497 
DisconnectSco(uint8_t callType)498 int32_t HandsFreeAudioGateway::DisconnectSco(uint8_t callType)
499 {
500     HILOGI("enter, callType: %{public}d", callType);
501     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
502     sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
503     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY,
504         "hfpAG proxy is nullptr.");
505     CHECK_AND_RETURN_LOG_RET((pimpl->IsValidCallType(callType)), BT_ERR_INVALID_PARAM,
506         "disconnect sco call type error.");
507     return pimpl->DisconnectSco(callType);
508 }
509 
ConnectSco()510 bool HandsFreeAudioGateway::ConnectSco()
511 {
512     return true;
513 }
514 
DisconnectSco()515 bool HandsFreeAudioGateway::DisconnectSco()
516 {
517     return true;
518 }
519 
PhoneStateChanged(int numActive,int numHeld,int callState,const std::string & number,int type,const std::string & name)520 void HandsFreeAudioGateway::PhoneStateChanged(
521     int numActive, int numHeld, int callState, const std::string &number, int type, const std::string &name)
522 {
523     CHECK_AND_RETURN_LOG(IS_BT_ENABLED(), "bluetooth is off.");
524     sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
525     CHECK_AND_RETURN_LOG(proxy != nullptr, "hfpAG proxy is nullptr.");
526 
527     BluetoothPhoneState phoneState;
528     phoneState.SetActiveNum(numActive);
529     phoneState.SetHeldNum(numHeld);
530     phoneState.SetCallState(callState);
531     phoneState.SetNumber(number);
532     phoneState.SetCallType(type);
533     phoneState.SetName(name);
534     pimpl->PhoneStateChanged(phoneState);
535 }
536 
ClccResponse(int index,int direction,int status,int mode,bool mpty,const std::string & number,int type)537 void HandsFreeAudioGateway::ClccResponse(
538     int index, int direction, int status, int mode, bool mpty, const std::string &number, int type)
539 {
540     if (!IS_BT_ENABLED()) {
541         HILOGE("bluetooth is off.");
542         return;
543     }
544 
545     sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
546     CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
547 
548     pimpl->ClccResponse(index, direction, status, mode, mpty, number, type);
549 }
550 
OpenVoiceRecognition(const BluetoothRemoteDevice & device)551 bool HandsFreeAudioGateway::OpenVoiceRecognition(const BluetoothRemoteDevice &device)
552 {
553     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
554     if (!IS_BT_ENABLED()) {
555         HILOGE("bluetooth is off.");
556         return false;
557     }
558 
559     sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
560     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
561 
562     return pimpl->OpenVoiceRecognition(device);
563 }
564 
CloseVoiceRecognition(const BluetoothRemoteDevice & device)565 bool HandsFreeAudioGateway::CloseVoiceRecognition(const BluetoothRemoteDevice &device)
566 {
567     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
568     if (!IS_BT_ENABLED()) {
569         HILOGE("bluetooth is off.");
570         return false;
571     }
572 
573     sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
574     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
575 
576     return pimpl->CloseVoiceRecognition(device);
577 }
578 
SetActiveDevice(const BluetoothRemoteDevice & device)579 bool HandsFreeAudioGateway::SetActiveDevice(const BluetoothRemoteDevice &device)
580 {
581     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
582     if (!IS_BT_ENABLED()) {
583         HILOGE("bluetooth is off.");
584         return false;
585     }
586 
587     sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
588     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
589 
590     return pimpl->SetActiveDevice(device);
591 }
592 
GetActiveDevice() const593 BluetoothRemoteDevice HandsFreeAudioGateway::GetActiveDevice() const
594 {
595     BluetoothRemoteDevice device;
596     if (!IS_BT_ENABLED()) {
597         HILOGE("bluetooth is off.");
598         return device;
599     }
600 
601     sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
602     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, device, "failed: no proxy");
603 
604     device = pimpl->GetActiveDevice();
605     return device;
606 }
607 
SetConnectStrategy(const BluetoothRemoteDevice & device,int strategy)608 int HandsFreeAudioGateway::SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy)
609 {
610     HILOGI("enter, device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
611     if (!IS_BT_ENABLED()) {
612         HILOGE("bluetooth is off.");
613         return BT_ERR_INVALID_STATE;
614     }
615 
616     sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
617     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
618 
619     if ((!device.IsValidBluetoothRemoteDevice()) || (
620         (strategy != static_cast<int>(BTStrategyType::CONNECTION_ALLOWED)) &&
621         (strategy != static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN)))) {
622         HILOGI("input parameter error.");
623         return BT_ERR_INVALID_PARAM;
624     }
625     return pimpl->SetConnectStrategy(device, strategy);
626 }
627 
GetConnectStrategy(const BluetoothRemoteDevice & device,int & strategy) const628 int HandsFreeAudioGateway::GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const
629 {
630     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
631     if (!IS_BT_ENABLED()) {
632         HILOGE("bluetooth is off.");
633         return BT_ERR_INVALID_STATE;
634     }
635 
636     sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
637     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
638 
639     if (!device.IsValidBluetoothRemoteDevice()) {
640         HILOGI("input parameter error.");
641         return BT_ERR_INVALID_PARAM;
642     }
643     return pimpl->GetConnectStrategy(device, strategy);
644 }
645 
IsInbandRingingEnabled(bool & isEnabled) const646 int HandsFreeAudioGateway::IsInbandRingingEnabled(bool &isEnabled) const
647 {
648     HILOGI("enter");
649     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
650     sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
651     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "hfpAG proxy is nullptr");
652     return pimpl->IsInbandRingingEnabled(isEnabled);
653 }
654 
RegisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)655 void HandsFreeAudioGateway::RegisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
656 {
657     HILOGD("enter");
658     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
659     pimpl->RegisterObserver(observer);
660 }
661 
DeregisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)662 void HandsFreeAudioGateway::DeregisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
663 {
664     HILOGD("enter");
665     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
666     pimpl->DeregisterObserver(observer);
667 }
668 }  // namespace Bluetooth
669 }  // namespace OHOS