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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_fwk_hfp_ag"
17 #endif
18
19 #include "bluetooth_hfp_ag.h"
20 #include <unistd.h>
21 #include "bluetooth_device.h"
22 #include "bluetooth_host.h"
23 #include "bluetooth_profile_manager.h"
24 #include "bluetooth_log.h"
25 #include "bluetooth_utils.h"
26 #include "bluetooth_observer_list.h"
27 #include "bluetooth_phone_state.h"
28 #include "i_bluetooth_hfp_ag.h"
29 #include "bluetooth_hfp_ag_observer_stub.h"
30 #include "i_bluetooth_host.h"
31 #include "iservice_registry.h"
32 #include "system_ability_definition.h"
33
34 namespace OHOS {
35 namespace Bluetooth {
36 std::mutex g_hfpProxyMutex;
37 class AgServiceObserver : public BluetoothHfpAgObserverStub {
38 public:
AgServiceObserver(BluetoothObserverList<HandsFreeAudioGatewayObserver> & observers)39 explicit AgServiceObserver(BluetoothObserverList<HandsFreeAudioGatewayObserver> &observers) : observers_(observers)
40 {
41 HILOGD("enter");
42 }
~AgServiceObserver()43 ~AgServiceObserver() override
44 {
45 HILOGD("enter");
46 };
47
OnConnectionStateChanged(const BluetoothRawAddress & device,int32_t state,int32_t cause)48 void OnConnectionStateChanged(const BluetoothRawAddress &device, int32_t state, int32_t cause) override
49 {
50 HILOGD("hfpAg conn state, device: %{public}s, state: %{public}s, cause: %{public}d",
51 GET_ENCRYPT_RAW_ADDR(device), GetProfileConnStateName(state).c_str(), cause);
52 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
53 observers_.ForEach([remoteDevice, state, cause](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
54 observer->OnConnectionStateChanged(remoteDevice, state, cause);
55 });
56 }
57
OnScoStateChanged(const BluetoothRawAddress & device,int32_t state,int32_t reason)58 void OnScoStateChanged(const BluetoothRawAddress &device, int32_t state, int32_t reason) override
59 {
60 HILOGI("enter, device: %{public}s, state: %{public}u, reason: %{public}u",
61 GET_ENCRYPT_RAW_ADDR(device), state, reason);
62 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
63 observers_.ForEach([remoteDevice, state, reason](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
64 observer->OnScoStateChanged(remoteDevice, state, reason);
65 });
66 }
67
OnActiveDeviceChanged(const BluetoothRawAddress & device)68 void OnActiveDeviceChanged(const BluetoothRawAddress &device) override
69 {
70 HILOGD("enter, device: %{public}s", GET_ENCRYPT_RAW_ADDR(device));
71 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
72 observers_.ForEach([remoteDevice](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
73 observer->OnActiveDeviceChanged(remoteDevice);
74 });
75 }
76
OnHfEnhancedDriverSafetyChanged(const BluetoothRawAddress & device,int32_t indValue)77 void OnHfEnhancedDriverSafetyChanged(
78 const BluetoothRawAddress &device, int32_t indValue) override
79 {
80 HILOGI("enter, device: %{public}s, indValue: %{public}d",
81 GET_ENCRYPT_RAW_ADDR(device), indValue);
82 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
83 observers_.ForEach([remoteDevice, indValue](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
84 observer->OnHfEnhancedDriverSafetyChanged(remoteDevice, indValue);
85 });
86 }
87
OnHfpStackChanged(const BluetoothRawAddress & device,int32_t action)88 void OnHfpStackChanged(const BluetoothRawAddress &device, int32_t action) override
89 {
90 HILOGI("enter, device: %{public}s, action: %{public}s",
91 GET_ENCRYPT_RAW_ADDR(device), GetUpdateOutputStackActionName(action).c_str());
92 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
93 observers_.ForEach([remoteDevice, action](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
94 observer->OnHfpStackChanged(remoteDevice, action);
95 });
96 }
97
OnVirtualDeviceChanged(int32_t action,std::string address)98 void OnVirtualDeviceChanged(int32_t action, std::string address) override
99 {
100 HILOGI("enter, device: %{public}s, action: %{public}d", GetEncryptAddr(address).c_str(), action);
101 observers_.ForEach([action, address](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
102 observer->OnVirtualDeviceChanged(action, address);
103 });
104 }
105
106 private:
107 BluetoothObserverList<HandsFreeAudioGatewayObserver> &observers_;
108 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(AgServiceObserver);
109 };
110
111 std::string HfpAgServiceName = "bluetooth-hfp-ag-server";
112
113 struct HandsFreeAudioGateway::impl {
114 impl();
115 ~impl();
116
GetConnectedDevicesOHOS::Bluetooth::HandsFreeAudioGateway::impl117 int32_t GetConnectedDevices(std::vector<BluetoothRemoteDevice>& devices)
118 {
119 HILOGD("enter");
120 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
121 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_SERVICE_DISCONNECTED, "failed: no proxy");
122 std::vector<BluetoothRawAddress> ori;
123 int32_t ret = proxy->GetConnectDevices(ori);
124 if (ret != BT_NO_ERROR) {
125 HILOGE("inner error.");
126 return ret;
127 }
128 for (auto it = ori.begin(); it != ori.end(); it++) {
129 devices.push_back(BluetoothRemoteDevice(it->GetAddress(), 0));
130 }
131 return BT_NO_ERROR;
132 }
133
GetDevicesByStatesOHOS::Bluetooth::HandsFreeAudioGateway::impl134 std::vector<BluetoothRemoteDevice> GetDevicesByStates(std::vector<int> states)
135 {
136 HILOGD("enter");
137 std::vector<BluetoothRemoteDevice> remoteDevices;
138 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
139 if (proxy != nullptr) {
140 std::vector<BluetoothRawAddress> rawDevices;
141 std::vector<int32_t> tmpstates;
142 for (int state : states) {
143 int32_t value = (int32_t)state;
144 tmpstates.push_back(value);
145 }
146 proxy->GetDevicesByStates(tmpstates, rawDevices);
147 for (BluetoothRawAddress rawDevice : rawDevices) {
148 BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 0);
149 remoteDevices.push_back(remoteDevice);
150 }
151 }
152 return remoteDevices;
153 }
154
GetDeviceStateOHOS::Bluetooth::HandsFreeAudioGateway::impl155 int32_t GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state)
156 {
157 HILOGD("enter, 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
164 return proxy->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()), state);
165 }
166
ConnectOHOS::Bluetooth::HandsFreeAudioGateway::impl167 int32_t Connect(const BluetoothRemoteDevice &device)
168 {
169 HILOGI("hfp connect remote device: %{public}s", GET_ENCRYPT_ADDR(device));
170 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
171 if (proxy == nullptr || !device.IsValidBluetoothRemoteDevice()) {
172 HILOGE("invalid param.");
173 return BT_ERR_INVALID_PARAM;
174 }
175 return proxy->Connect(BluetoothRawAddress(device.GetDeviceAddr()));
176 }
177
DisconnectOHOS::Bluetooth::HandsFreeAudioGateway::impl178 int32_t Disconnect(const BluetoothRemoteDevice &device)
179 {
180 HILOGI("hfp disconnect remote device: %{public}s", GET_ENCRYPT_ADDR(device));
181 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
182 if (proxy == nullptr || !device.IsValidBluetoothRemoteDevice()) {
183 HILOGE("invalid param.");
184 return BT_ERR_INVALID_PARAM;
185 }
186 return proxy->Disconnect(BluetoothRawAddress(device.GetDeviceAddr()));
187 }
188
GetScoStateOHOS::Bluetooth::HandsFreeAudioGateway::impl189 int GetScoState(const BluetoothRemoteDevice &device)
190 {
191 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
192 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
193 if (proxy != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
194 return proxy->GetScoState(BluetoothRawAddress(device.GetDeviceAddr()));
195 }
196 return HFP_AG_SCO_STATE_DISCONNECTED;
197 }
198
ConnectScoOHOS::Bluetooth::HandsFreeAudioGateway::impl199 int32_t ConnectSco(uint8_t callType)
200 {
201 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
202 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_PARAM, "failed: no proxy");
203 return proxy->ConnectSco(callType);
204 }
205
DisconnectScoOHOS::Bluetooth::HandsFreeAudioGateway::impl206 int32_t DisconnectSco(uint8_t callType)
207 {
208 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
209 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_PARAM, "failed: no proxy");
210 return proxy->DisconnectSco(callType);
211 }
212
ConnectScoOHOS::Bluetooth::HandsFreeAudioGateway::impl213 bool ConnectSco()
214 {
215 HILOGD("enter");
216 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
217 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
218 return proxy->ConnectSco();
219 }
220
DisconnectScoOHOS::Bluetooth::HandsFreeAudioGateway::impl221 bool DisconnectSco()
222 {
223 HILOGD("enter");
224 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
225 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
226 return proxy->DisconnectSco();
227 }
228
IsValidCallTypeOHOS::Bluetooth::HandsFreeAudioGateway::impl229 bool IsValidCallType(uint8_t callType)
230 {
231 if (callType == static_cast<uint8_t>(BTCallType::CALL_TYPE_CELLULAR) ||
232 callType == static_cast<uint8_t>(BTCallType::CALL_TYPE_VIRTUAL) ||
233 callType == static_cast<uint8_t>(BTCallType::CALL_TYPE_RECOGNITION)) {
234 return true;
235 }
236 return false;
237 }
238
PhoneStateChangedOHOS::Bluetooth::HandsFreeAudioGateway::impl239 void PhoneStateChanged(BluetoothPhoneState &phoneState)
240 {
241 HILOGI("numActive: %{public}d, numHeld: %{public}d, callState: %{public}d, type: %{public}d",
242 phoneState.GetActiveNum(), phoneState.GetHeldNum(), phoneState.GetCallState(), phoneState.GetCallType());
243 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
244 CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is null");
245 proxy->PhoneStateChanged(phoneState);
246 }
247
ClccResponseOHOS::Bluetooth::HandsFreeAudioGateway::impl248 void ClccResponse(int index, int direction, int status, int mode, bool mpty, std::string number, int type)
249 {
250 HILOGI("enter, index: %{public}d, direction: %{public}d, status: %{public}d, mode: %{public}d, mpty: "
251 "%{public}d, type: %{public}d", index, direction, status, mode, mpty, type);
252 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
253 CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is null");
254 proxy->ClccResponse(index, direction, status, mode, mpty, number, type);
255 }
256
OpenVoiceRecognitionOHOS::Bluetooth::HandsFreeAudioGateway::impl257 bool OpenVoiceRecognition(const BluetoothRemoteDevice &device)
258 {
259 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
260 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
261 if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
262 return proxy->OpenVoiceRecognition(BluetoothRawAddress(device.GetDeviceAddr()));
263 }
264 return false;
265 }
266
CloseVoiceRecognitionOHOS::Bluetooth::HandsFreeAudioGateway::impl267 bool CloseVoiceRecognition(const BluetoothRemoteDevice &device)
268 {
269 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
270 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
271 if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
272 return proxy->CloseVoiceRecognition(BluetoothRawAddress(device.GetDeviceAddr()));
273 }
274 return false;
275 }
276
SetActiveDeviceOHOS::Bluetooth::HandsFreeAudioGateway::impl277 bool SetActiveDevice(const BluetoothRemoteDevice &device)
278 {
279 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
280 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
281 if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
282 return proxy->SetActiveDevice(BluetoothRawAddress(device.GetDeviceAddr()));
283 }
284 return false;
285 }
286
IntoMockOHOS::Bluetooth::HandsFreeAudioGateway::impl287 bool IntoMock(const BluetoothRemoteDevice &device, int state)
288 {
289 HILOGD("enter");
290 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
291 if (proxy != nullptr && IS_BT_ENABLED()) {
292 return proxy->IntoMock(BluetoothRawAddress(device.GetDeviceAddr()), state);
293 }
294 return false;
295 }
296
SendNoCarrierOHOS::Bluetooth::HandsFreeAudioGateway::impl297 bool SendNoCarrier(const BluetoothRemoteDevice &device)
298 {
299 HILOGD("enter");
300 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
301 if (proxy != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
302 return proxy->SendNoCarrier(BluetoothRawAddress(device.GetDeviceAddr()));
303 }
304 return false;
305 }
306
GetActiveDeviceOHOS::Bluetooth::HandsFreeAudioGateway::impl307 BluetoothRemoteDevice GetActiveDevice()
308 {
309 HILOGD("enter");
310 BluetoothRemoteDevice device;
311 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
312 if (proxy != nullptr) {
313 std::string address = proxy->GetActiveDevice();
314 BluetoothRemoteDevice remoteDevice(address, 0);
315 device = remoteDevice;
316 }
317 return device;
318 }
319
SetConnectStrategyOHOS::Bluetooth::HandsFreeAudioGateway::impl320 int SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy)
321 {
322 HILOGD("enter");
323 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
324 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is null");
325 return proxy->SetConnectStrategy(BluetoothRawAddress(device.GetDeviceAddr()), strategy);
326 }
327
GetConnectStrategyOHOS::Bluetooth::HandsFreeAudioGateway::impl328 int GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const
329 {
330 HILOGD("enter");
331 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
332 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is null");
333 return proxy->GetConnectStrategy(BluetoothRawAddress(device.GetDeviceAddr()), strategy);
334 }
335
IsInbandRingingEnabledOHOS::Bluetooth::HandsFreeAudioGateway::impl336 int IsInbandRingingEnabled(bool &isEnabled) const
337 {
338 HILOGD("enter");
339 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
340 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is null");
341 return proxy->IsInbandRingingEnabled(isEnabled);
342 }
343
CallDetailsChangedOHOS::Bluetooth::HandsFreeAudioGateway::impl344 void CallDetailsChanged(int callId, int callState)
345 {
346 HILOGD("enter");
347 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
348 CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is null");
349 proxy->CallDetailsChanged(callId, callState);
350 }
351
IsVgsSupportedOHOS::Bluetooth::HandsFreeAudioGateway::impl352 int IsVgsSupported(const BluetoothRemoteDevice &device, bool &isSupported) const
353 {
354 HILOGD("enter");
355 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
356 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is null");
357 return proxy->IsVgsSupported(BluetoothRawAddress(device.GetDeviceAddr()), isSupported);
358 }
359
EnableBtCallLogOHOS::Bluetooth::HandsFreeAudioGateway::impl360 void EnableBtCallLog(bool state)
361 {
362 HILOGD("enter");
363 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
364 CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is null");
365 proxy->EnableBtCallLog(state);
366 }
367
RegisterObserverOHOS::Bluetooth::HandsFreeAudioGateway::impl368 void RegisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
369 {
370 HILOGD("enter");
371 observers_.Register(observer);
372 }
373
DeregisterObserverOHOS::Bluetooth::HandsFreeAudioGateway::impl374 void DeregisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
375 {
376 HILOGD("enter");
377 observers_.Deregister(observer);
378 HILOGI("end");
379 }
380
381 int32_t profileRegisterId = 0;
382 private:
383 const static int HFP_AG_SLC_STATE_DISCONNECTED = static_cast<int>(BTConnectState::DISCONNECTED);
384 const static int HFP_AG_SCO_STATE_DISCONNECTED = 3;
385
386 BluetoothObserverList<HandsFreeAudioGatewayObserver> observers_;
387 sptr<AgServiceObserver> serviceObserver_;
388 };
389
impl()390 HandsFreeAudioGateway::impl::impl()
391 {
392 serviceObserver_ = new AgServiceObserver(observers_);
393 profileRegisterId = BluetoothProfileManager::GetInstance().RegisterFunc(PROFILE_HFP_AG,
394 [this](sptr<IRemoteObject> remote) {
395 sptr<IBluetoothHfpAg> proxy = iface_cast<IBluetoothHfpAg>(remote);
396 CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
397 proxy->RegisterObserver(serviceObserver_);
398 });
399 }
400
~impl()401 HandsFreeAudioGateway::impl::~impl()
402 {
403 HILOGD("enter");
404 BluetoothProfileManager::GetInstance().DeregisterFunc(profileRegisterId);
405 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
406 CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
407 proxy->DeregisterObserver(serviceObserver_);
408 }
409
GetProfile()410 HandsFreeAudioGateway *HandsFreeAudioGateway::GetProfile()
411 {
412 HILOGD("enter");
413 #ifdef DTFUZZ_TEST
414 static BluetoothNoDestructor<HandsFreeAudioGateway> instance;
415 return instance.get();
416 #else
417 static HandsFreeAudioGateway instance;
418 return &instance;
419 #endif
420 }
421
HandsFreeAudioGateway()422 HandsFreeAudioGateway::HandsFreeAudioGateway()
423 {
424 HILOGD("enter");
425 pimpl = std::make_unique<impl>();
426 }
427
~HandsFreeAudioGateway()428 HandsFreeAudioGateway::~HandsFreeAudioGateway()
429 {
430 HILOGD("enter");
431 }
432
GetConnectedDevices() const433 std::vector<BluetoothRemoteDevice> HandsFreeAudioGateway::GetConnectedDevices() const
434 {
435 std::vector<BluetoothRemoteDevice> devices;
436 if (!IS_BT_ENABLED()) {
437 HILOGE("bluetooth is off.");
438 return devices;
439 }
440 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
441 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, devices, "failed: no proxy");
442
443 pimpl->GetConnectedDevices(devices);
444 return devices;
445 }
446
GetConnectedDevices(std::vector<BluetoothRemoteDevice> & devices)447 int32_t HandsFreeAudioGateway::GetConnectedDevices(std::vector<BluetoothRemoteDevice> &devices)
448 {
449 if (!IS_BT_ENABLED()) {
450 HILOGE("bluetooth is off.");
451 return BT_ERR_INVALID_STATE;
452 }
453
454 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
455 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
456
457 return pimpl->GetConnectedDevices(devices);
458 }
459
GetDevicesByStates(std::vector<int> states)460 std::vector<BluetoothRemoteDevice> HandsFreeAudioGateway::GetDevicesByStates(std::vector<int> states)
461 {
462 if (!IS_BT_ENABLED()) {
463 HILOGE("bluetooth is off.");
464 return std::vector<BluetoothRemoteDevice>();
465 }
466 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
467 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, std::vector<BluetoothRemoteDevice>(), "failed: no proxy");
468
469 return pimpl->GetDevicesByStates(states);
470 }
471
GetDeviceState(const BluetoothRemoteDevice & device,int32_t & state)472 int32_t HandsFreeAudioGateway::GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state)
473 {
474 HILOGD("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
475 if (!IS_BT_ENABLED()) {
476 HILOGE("bluetooth is off.");
477 return BT_ERR_INVALID_STATE;
478 }
479
480 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
481 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
482
483 return pimpl->GetDeviceState(device, state);
484 }
485
Connect(const BluetoothRemoteDevice & device)486 int32_t HandsFreeAudioGateway::Connect(const BluetoothRemoteDevice &device)
487 {
488 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
489 if (!IS_BT_ENABLED()) {
490 HILOGE("bluetooth is off.");
491 return BT_ERR_INVALID_STATE;
492 }
493
494 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
495 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
496
497 return pimpl->Connect(device);
498 }
499
Disconnect(const BluetoothRemoteDevice & device)500 int32_t HandsFreeAudioGateway::Disconnect(const BluetoothRemoteDevice &device)
501 {
502 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
503 if (!IS_BT_ENABLED()) {
504 HILOGE("bluetooth is off.");
505 return BT_ERR_INVALID_STATE;
506 }
507 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
508 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
509
510 return pimpl->Disconnect(device);
511 }
512
GetScoState(const BluetoothRemoteDevice & device) const513 int HandsFreeAudioGateway::GetScoState(const BluetoothRemoteDevice &device) const
514 {
515 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
516 if (!IS_BT_ENABLED()) {
517 HILOGE("bluetooth is off.");
518 return static_cast<int>(HfpScoConnectState::SCO_DISCONNECTED);
519 }
520 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
521 CHECK_AND_RETURN_LOG_RET(proxy != nullptr,
522 static_cast<int>(HfpScoConnectState::SCO_DISCONNECTED), "failed: no proxy");
523
524 return pimpl->GetScoState(device);
525 }
526
ConnectSco(uint8_t callType)527 int32_t HandsFreeAudioGateway::ConnectSco(uint8_t callType)
528 {
529 HILOGI("enter, callType: %{public}d", callType);
530 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
531 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
532 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY,
533 "hfpAG proxy is nullptr.");
534 CHECK_AND_RETURN_LOG_RET((pimpl->IsValidCallType(callType)), BT_ERR_INVALID_PARAM,
535 "connect sco call type error.");
536 return pimpl->ConnectSco(callType);
537 }
538
DisconnectSco(uint8_t callType)539 int32_t HandsFreeAudioGateway::DisconnectSco(uint8_t callType)
540 {
541 HILOGI("enter, callType: %{public}d", callType);
542 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
543 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
544 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY,
545 "hfpAG proxy is nullptr.");
546 CHECK_AND_RETURN_LOG_RET((pimpl->IsValidCallType(callType)), BT_ERR_INVALID_PARAM,
547 "disconnect sco call type error.");
548 return pimpl->DisconnectSco(callType);
549 }
550
ConnectSco()551 bool HandsFreeAudioGateway::ConnectSco()
552 {
553 return true;
554 }
555
DisconnectSco()556 bool HandsFreeAudioGateway::DisconnectSco()
557 {
558 return true;
559 }
560
PhoneStateChanged(int numActive,int numHeld,int callState,const std::string & number,int type,const std::string & name)561 void HandsFreeAudioGateway::PhoneStateChanged(
562 int numActive, int numHeld, int callState, const std::string &number, int type, const std::string &name)
563 {
564 CHECK_AND_RETURN_LOG(IS_BT_ENABLED(), "bluetooth is off.");
565 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
566 CHECK_AND_RETURN_LOG(proxy != nullptr, "hfpAG proxy is nullptr.");
567
568 BluetoothPhoneState phoneState;
569 phoneState.SetActiveNum(numActive);
570 phoneState.SetHeldNum(numHeld);
571 phoneState.SetCallState(callState);
572 phoneState.SetNumber(number);
573 phoneState.SetCallType(type);
574 phoneState.SetName(name);
575 pimpl->PhoneStateChanged(phoneState);
576 }
577
ClccResponse(int index,int direction,int status,int mode,bool mpty,const std::string & number,int type)578 void HandsFreeAudioGateway::ClccResponse(
579 int index, int direction, int status, int mode, bool mpty, const std::string &number, int type)
580 {
581 if (!IS_BT_ENABLED()) {
582 HILOGE("bluetooth is off.");
583 return;
584 }
585
586 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
587 CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
588
589 pimpl->ClccResponse(index, direction, status, mode, mpty, number, type);
590 }
591
OpenVoiceRecognition(const BluetoothRemoteDevice & device)592 bool HandsFreeAudioGateway::OpenVoiceRecognition(const BluetoothRemoteDevice &device)
593 {
594 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
595 if (!IS_BT_ENABLED()) {
596 HILOGE("bluetooth is off.");
597 return false;
598 }
599
600 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
601 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
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 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
615 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
616
617 return pimpl->CloseVoiceRecognition(device);
618 }
619
SetActiveDevice(const BluetoothRemoteDevice & device)620 bool HandsFreeAudioGateway::SetActiveDevice(const BluetoothRemoteDevice &device)
621 {
622 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
623 if (!IS_BT_ENABLED()) {
624 HILOGE("bluetooth is off.");
625 return false;
626 }
627
628 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
629 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
630
631 return pimpl->SetActiveDevice(device);
632 }
633
GetActiveDevice() const634 BluetoothRemoteDevice HandsFreeAudioGateway::GetActiveDevice() const
635 {
636 BluetoothRemoteDevice device;
637 if (!IS_BT_ENABLED()) {
638 HILOGE("bluetooth is off.");
639 return device;
640 }
641
642 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
643 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, device, "failed: no proxy");
644
645 device = pimpl->GetActiveDevice();
646 return device;
647 }
648
SetConnectStrategy(const BluetoothRemoteDevice & device,int strategy)649 int HandsFreeAudioGateway::SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy)
650 {
651 HILOGI("enter, device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
652 if (!IS_BT_ENABLED()) {
653 HILOGE("bluetooth is off.");
654 return BT_ERR_INVALID_STATE;
655 }
656
657 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
658 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
659
660 if ((!device.IsValidBluetoothRemoteDevice()) || (
661 (strategy != static_cast<int>(BTStrategyType::CONNECTION_ALLOWED)) &&
662 (strategy != static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN)))) {
663 HILOGI("input parameter error.");
664 return BT_ERR_INVALID_PARAM;
665 }
666 return pimpl->SetConnectStrategy(device, strategy);
667 }
668
GetConnectStrategy(const BluetoothRemoteDevice & device,int & strategy) const669 int HandsFreeAudioGateway::GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const
670 {
671 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
672 if (!IS_BT_ENABLED()) {
673 HILOGE("bluetooth is off.");
674 return BT_ERR_INVALID_STATE;
675 }
676
677 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
678 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
679
680 if (!device.IsValidBluetoothRemoteDevice()) {
681 HILOGI("input parameter error.");
682 return BT_ERR_INVALID_PARAM;
683 }
684 return pimpl->GetConnectStrategy(device, strategy);
685 }
686
IsInbandRingingEnabled(bool & isEnabled) const687 int HandsFreeAudioGateway::IsInbandRingingEnabled(bool &isEnabled) const
688 {
689 HILOGI("enter");
690 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
691 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
692 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "hfpAG proxy is nullptr");
693 return pimpl->IsInbandRingingEnabled(isEnabled);
694 }
695
CallDetailsChanged(int callId,int callState)696 void HandsFreeAudioGateway::CallDetailsChanged(int callId, int callState)
697 {
698 HILOGI("enter, callId: %{public}d, callState: %{public}d", callId, callState);
699 CHECK_AND_RETURN_LOG(IS_BT_ENABLED(), "bluetooth is off.");
700 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
701 CHECK_AND_RETURN_LOG(proxy != nullptr, "hfpAG proxy is nullptr");
702 pimpl->CallDetailsChanged(callId, callState);
703 }
704
EnableBtCallLog(bool state)705 void HandsFreeAudioGateway::EnableBtCallLog(bool state)
706 {
707 HILOGI("enter");
708 CHECK_AND_RETURN_LOG(IS_BT_ENABLED(), "bluetooth is off.");
709 pimpl->EnableBtCallLog(state);
710 }
711
IsVgsSupported(const BluetoothRemoteDevice & device,bool & isSupported) const712 int HandsFreeAudioGateway::IsVgsSupported(const BluetoothRemoteDevice &device, bool &isSupported) const
713 {
714 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
715 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
716 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
717 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
718 CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "input parameter error.");
719 return pimpl->IsVgsSupported(device, isSupported);
720 }
721
GetVirtualDeviceList(std::vector<std::string> & devices) const722 void HandsFreeAudioGateway::GetVirtualDeviceList(std::vector<std::string> &devices) const
723 {
724 CHECK_AND_RETURN_LOG(IS_BT_ENABLED(), "bluetooth is off.");
725 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
726 CHECK_AND_RETURN_LOG(proxy != nullptr, "hfpAG proxy is nullptr");
727 proxy->GetVirtualDeviceList(devices);
728 }
729
RegisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)730 void HandsFreeAudioGateway::RegisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
731 {
732 HILOGD("enter");
733 CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
734 pimpl->RegisterObserver(observer);
735 }
736
DeregisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)737 void HandsFreeAudioGateway::DeregisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
738 {
739 HILOGD("enter");
740 CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
741 pimpl->DeregisterObserver(observer);
742 }
743 } // namespace Bluetooth
744 } // namespace OHOS