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 #include "bt_def.h"
34
35 namespace OHOS {
36 namespace Bluetooth {
37 std::mutex g_hfpProxyMutex;
38 class AgServiceObserver : public BluetoothHfpAgObserverStub {
39 public:
AgServiceObserver(BluetoothObserverList<HandsFreeAudioGatewayObserver> & observers)40 explicit AgServiceObserver(BluetoothObserverList<HandsFreeAudioGatewayObserver> &observers) : observers_(observers)
41 {
42 HILOGD("enter");
43 }
~AgServiceObserver()44 ~AgServiceObserver() override
45 {
46 HILOGD("enter");
47 };
48
OnConnectionStateChanged(const BluetoothRawAddress & device,int32_t state,int32_t cause)49 void OnConnectionStateChanged(const BluetoothRawAddress &device, int32_t state, int32_t cause) override
50 {
51 HILOGD("hfpAg conn state, device: %{public}s, state: %{public}s, cause: %{public}d",
52 GET_ENCRYPT_RAW_ADDR(device), GetProfileConnStateName(state).c_str(), cause);
53 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
54 observers_.ForEach([remoteDevice, state, cause](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
55 observer->OnConnectionStateChanged(remoteDevice, state, cause);
56 });
57 }
58
OnScoStateChanged(const BluetoothRawAddress & device,int32_t state,int32_t reason)59 void OnScoStateChanged(const BluetoothRawAddress &device, int32_t state, int32_t reason) override
60 {
61 HILOGI("enter, device: %{public}s, state: %{public}u, reason: %{public}u",
62 GET_ENCRYPT_RAW_ADDR(device), state, reason);
63 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
64 observers_.ForEach([remoteDevice, state, reason](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
65 observer->OnScoStateChanged(remoteDevice, state, reason);
66 });
67 }
68
OnActiveDeviceChanged(const BluetoothRawAddress & device)69 void OnActiveDeviceChanged(const BluetoothRawAddress &device) override
70 {
71 HILOGD("enter, device: %{public}s", GET_ENCRYPT_RAW_ADDR(device));
72 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
73 observers_.ForEach([remoteDevice](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
74 observer->OnActiveDeviceChanged(remoteDevice);
75 });
76 }
77
OnHfEnhancedDriverSafetyChanged(const BluetoothRawAddress & device,int32_t indValue)78 void OnHfEnhancedDriverSafetyChanged(
79 const BluetoothRawAddress &device, int32_t indValue) override
80 {
81 HILOGI("enter, device: %{public}s, indValue: %{public}d",
82 GET_ENCRYPT_RAW_ADDR(device), indValue);
83 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
84 observers_.ForEach([remoteDevice, indValue](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
85 observer->OnHfEnhancedDriverSafetyChanged(remoteDevice, indValue);
86 });
87 }
88
OnHfpStackChanged(const BluetoothRawAddress & device,int32_t action)89 void OnHfpStackChanged(const BluetoothRawAddress &device, int32_t action) override
90 {
91 HILOGI("enter, device: %{public}s, action: %{public}s",
92 GET_ENCRYPT_RAW_ADDR(device), GetUpdateOutputStackActionName(action).c_str());
93 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
94 observers_.ForEach([remoteDevice, action](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
95 observer->OnHfpStackChanged(remoteDevice, action);
96 });
97 }
98
OnVirtualDeviceChanged(int32_t action,std::string address)99 void OnVirtualDeviceChanged(int32_t action, std::string address) override
100 {
101 HILOGI("enter, device: %{public}s, action: %{public}d", GetEncryptAddr(address).c_str(), action);
102 observers_.ForEach([action, address](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
103 observer->OnVirtualDeviceChanged(action, address);
104 });
105 }
106
107 private:
108 BluetoothObserverList<HandsFreeAudioGatewayObserver> &observers_;
109 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(AgServiceObserver);
110 };
111
112 std::string HfpAgServiceName = "bluetooth-hfp-ag-server";
113
114 struct HandsFreeAudioGateway::impl {
115 impl();
116 ~impl();
117
GetConnectedDevicesOHOS::Bluetooth::HandsFreeAudioGateway::impl118 int32_t GetConnectedDevices(std::vector<BluetoothRemoteDevice>& devices)
119 {
120 HILOGD("enter");
121 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
122 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_SERVICE_DISCONNECTED, "failed: no proxy");
123 std::vector<BluetoothRawAddress> ori;
124 int32_t ret = proxy->GetConnectDevices(ori);
125 if (ret != BT_NO_ERROR) {
126 HILOGE("inner error.");
127 return ret;
128 }
129 for (auto it = ori.begin(); it != ori.end(); it++) {
130 devices.push_back(BluetoothRemoteDevice(it->GetAddress(), 0));
131 }
132 return BT_NO_ERROR;
133 }
134
GetDevicesByStatesOHOS::Bluetooth::HandsFreeAudioGateway::impl135 std::vector<BluetoothRemoteDevice> GetDevicesByStates(std::vector<int> states)
136 {
137 HILOGD("enter");
138 std::vector<BluetoothRemoteDevice> remoteDevices;
139 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
140 if (proxy != nullptr) {
141 std::vector<BluetoothRawAddress> rawDevices;
142 std::vector<int32_t> tmpstates;
143 for (int state : states) {
144 int32_t value = (int32_t)state;
145 tmpstates.push_back(value);
146 }
147 proxy->GetDevicesByStates(tmpstates, rawDevices);
148 for (BluetoothRawAddress rawDevice : rawDevices) {
149 BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 0);
150 remoteDevices.push_back(remoteDevice);
151 }
152 }
153 return remoteDevices;
154 }
155
GetDeviceStateOHOS::Bluetooth::HandsFreeAudioGateway::impl156 int32_t GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state)
157 {
158 HILOGD("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
159 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
160 if (proxy == nullptr || !device.IsValidBluetoothRemoteDevice()) {
161 HILOGE("invalid param.");
162 return BT_ERR_INVALID_PARAM;
163 }
164
165 return proxy->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()), state);
166 }
167
ConnectOHOS::Bluetooth::HandsFreeAudioGateway::impl168 int32_t Connect(const BluetoothRemoteDevice &device)
169 {
170 HILOGI("hfp connect remote device: %{public}s", GET_ENCRYPT_ADDR(device));
171 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
172 if (proxy == nullptr || !device.IsValidBluetoothRemoteDevice()) {
173 HILOGE("invalid param.");
174 return BT_ERR_INVALID_PARAM;
175 }
176 return proxy->Connect(BluetoothRawAddress(device.GetDeviceAddr()));
177 }
178
DisconnectOHOS::Bluetooth::HandsFreeAudioGateway::impl179 int32_t Disconnect(const BluetoothRemoteDevice &device)
180 {
181 HILOGI("hfp disconnect remote device: %{public}s", GET_ENCRYPT_ADDR(device));
182 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
183 if (proxy == nullptr || !device.IsValidBluetoothRemoteDevice()) {
184 HILOGE("invalid param.");
185 return BT_ERR_INVALID_PARAM;
186 }
187 return proxy->Disconnect(BluetoothRawAddress(device.GetDeviceAddr()));
188 }
189
GetScoStateOHOS::Bluetooth::HandsFreeAudioGateway::impl190 int GetScoState(const BluetoothRemoteDevice &device)
191 {
192 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
193 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
194 if (proxy != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
195 return proxy->GetScoState(BluetoothRawAddress(device.GetDeviceAddr()));
196 }
197 return HFP_AG_SCO_STATE_DISCONNECTED;
198 }
199
ConnectScoOHOS::Bluetooth::HandsFreeAudioGateway::impl200 int32_t ConnectSco(uint8_t callType)
201 {
202 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
203 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_PARAM, "failed: no proxy");
204 return proxy->ConnectSco(callType);
205 }
206
DisconnectScoOHOS::Bluetooth::HandsFreeAudioGateway::impl207 int32_t DisconnectSco(uint8_t callType)
208 {
209 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
210 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_PARAM, "failed: no proxy");
211 return proxy->DisconnectSco(callType);
212 }
213
ConnectScoOHOS::Bluetooth::HandsFreeAudioGateway::impl214 bool ConnectSco()
215 {
216 HILOGD("enter");
217 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
218 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
219 return proxy->ConnectSco();
220 }
221
DisconnectScoOHOS::Bluetooth::HandsFreeAudioGateway::impl222 bool DisconnectSco()
223 {
224 HILOGD("enter");
225 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
226 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
227 return proxy->DisconnectSco();
228 }
229
IsValidCallTypeOHOS::Bluetooth::HandsFreeAudioGateway::impl230 bool IsValidCallType(uint8_t callType)
231 {
232 if (callType == static_cast<uint8_t>(BTCallType::CALL_TYPE_CELLULAR) ||
233 callType == static_cast<uint8_t>(BTCallType::CALL_TYPE_VIRTUAL) ||
234 callType == static_cast<uint8_t>(BTCallType::CALL_TYPE_RECOGNITION)) {
235 return true;
236 }
237 return false;
238 }
239
PhoneStateChangedOHOS::Bluetooth::HandsFreeAudioGateway::impl240 void PhoneStateChanged(BluetoothPhoneState &phoneState)
241 {
242 HILOG_COMM_INFO("numActive: %{public}d, numHeld: %{public}d, callState: %{public}d, type: %{public}d",
243 phoneState.GetActiveNum(), phoneState.GetHeldNum(), phoneState.GetCallState(), phoneState.GetCallType());
244 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
245 CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is null");
246 proxy->PhoneStateChanged(phoneState);
247 }
248
ClccResponseOHOS::Bluetooth::HandsFreeAudioGateway::impl249 void ClccResponse(int index, int direction, int status, int mode, bool mpty, std::string number, int type)
250 {
251 HILOGI("enter, index: %{public}d, direction: %{public}d, status: %{public}d, mode: %{public}d, mpty: "
252 "%{public}d, type: %{public}d", index, direction, status, mode, mpty, type);
253 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
254 CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is null");
255 proxy->ClccResponse(index, direction, status, mode, mpty, number, type);
256 }
257
OpenVoiceRecognitionOHOS::Bluetooth::HandsFreeAudioGateway::impl258 bool OpenVoiceRecognition(const BluetoothRemoteDevice &device)
259 {
260 HILOG_COMM_INFO("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
261 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
262 if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
263 return proxy->OpenVoiceRecognition(BluetoothRawAddress(device.GetDeviceAddr()));
264 }
265 return false;
266 }
267
CloseVoiceRecognitionOHOS::Bluetooth::HandsFreeAudioGateway::impl268 bool CloseVoiceRecognition(const BluetoothRemoteDevice &device)
269 {
270 HILOG_COMM_INFO("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
271 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
272 if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
273 return proxy->CloseVoiceRecognition(BluetoothRawAddress(device.GetDeviceAddr()));
274 }
275 return false;
276 }
277
SetActiveDeviceOHOS::Bluetooth::HandsFreeAudioGateway::impl278 bool SetActiveDevice(const BluetoothRemoteDevice &device)
279 {
280 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
281 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
282 if (proxy != nullptr && device.IsValidBluetoothRemoteDevice()) {
283 return proxy->SetActiveDevice(BluetoothRawAddress(device.GetDeviceAddr()));
284 }
285 return false;
286 }
287
IntoMockOHOS::Bluetooth::HandsFreeAudioGateway::impl288 bool IntoMock(const BluetoothRemoteDevice &device, int state)
289 {
290 HILOGD("enter");
291 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
292 if (proxy != nullptr && IS_BT_ENABLED()) {
293 return proxy->IntoMock(BluetoothRawAddress(device.GetDeviceAddr()), state);
294 }
295 return false;
296 }
297
SendNoCarrierOHOS::Bluetooth::HandsFreeAudioGateway::impl298 bool SendNoCarrier(const BluetoothRemoteDevice &device)
299 {
300 HILOGD("enter");
301 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
302 if (proxy != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
303 return proxy->SendNoCarrier(BluetoothRawAddress(device.GetDeviceAddr()));
304 }
305 return false;
306 }
307
GetActiveDeviceOHOS::Bluetooth::HandsFreeAudioGateway::impl308 BluetoothRemoteDevice GetActiveDevice()
309 {
310 HILOGD("enter");
311 BluetoothRemoteDevice device;
312 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
313 if (proxy != nullptr) {
314 std::string address = proxy->GetActiveDevice();
315 BluetoothRemoteDevice remoteDevice(address, 0);
316 device = remoteDevice;
317 }
318 return device;
319 }
320
SetConnectStrategyOHOS::Bluetooth::HandsFreeAudioGateway::impl321 int SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy)
322 {
323 HILOGD("enter");
324 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
325 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is null");
326 return proxy->SetConnectStrategy(BluetoothRawAddress(device.GetDeviceAddr()), strategy);
327 }
328
GetConnectStrategyOHOS::Bluetooth::HandsFreeAudioGateway::impl329 int GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const
330 {
331 HILOGD("enter");
332 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
333 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is null");
334 return proxy->GetConnectStrategy(BluetoothRawAddress(device.GetDeviceAddr()), strategy);
335 }
336
IsInbandRingingEnabledOHOS::Bluetooth::HandsFreeAudioGateway::impl337 int IsInbandRingingEnabled(bool &isEnabled) const
338 {
339 HILOGD("enter");
340 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
341 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is null");
342 return proxy->IsInbandRingingEnabled(isEnabled);
343 }
344
CallDetailsChangedOHOS::Bluetooth::HandsFreeAudioGateway::impl345 void CallDetailsChanged(int callId, int callState)
346 {
347 HILOGD("enter");
348 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
349 CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is null");
350 proxy->CallDetailsChanged(callId, callState);
351 }
352
IsVgsSupportedOHOS::Bluetooth::HandsFreeAudioGateway::impl353 int IsVgsSupported(const BluetoothRemoteDevice &device, bool &isSupported) const
354 {
355 HILOGD("enter");
356 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
357 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is null");
358 return proxy->IsHfpFeatureSupported(BluetoothRawAddress(device.GetDeviceAddr()), isSupported,
359 static_cast<int>(bluetooth::HfpFeatureType::VGS));
360 }
361
EnableBtCallLogOHOS::Bluetooth::HandsFreeAudioGateway::impl362 void EnableBtCallLog(bool state)
363 {
364 HILOGD("enter");
365 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
366 CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is null");
367 proxy->EnableBtCallLog(state);
368 }
369
RegisterObserverOHOS::Bluetooth::HandsFreeAudioGateway::impl370 void RegisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
371 {
372 HILOGD("enter");
373 observers_.Register(observer);
374 }
375
DeregisterObserverOHOS::Bluetooth::HandsFreeAudioGateway::impl376 void DeregisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
377 {
378 HILOGD("enter");
379 observers_.Deregister(observer);
380 HILOGI("end");
381 }
382
IsVoiceRecognitionSupportedOHOS::Bluetooth::HandsFreeAudioGateway::impl383 int IsVoiceRecognitionSupported(const BluetoothRemoteDevice &device, bool &isSupported) const
384 {
385 HILOGD("enter");
386 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
387 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is null");
388 return proxy->IsHfpFeatureSupported(BluetoothRawAddress(device.GetDeviceAddr()), isSupported,
389 static_cast<int>(bluetooth::HfpFeatureType::VOICE_RECOGNITION));
390 }
391
GetCurrentCallTypeOHOS::Bluetooth::HandsFreeAudioGateway::impl392 int GetCurrentCallType(int &callType)
393 {
394 HILOGD("enter");
395 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
396 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is null");
397 return proxy->GetCurrentCallType(callType);
398 }
399
400 int32_t profileRegisterId = 0;
401 private:
402 const static int HFP_AG_SLC_STATE_DISCONNECTED = static_cast<int>(BTConnectState::DISCONNECTED);
403 const static int HFP_AG_SCO_STATE_DISCONNECTED = 3;
404
405 BluetoothObserverList<HandsFreeAudioGatewayObserver> observers_;
406 sptr<AgServiceObserver> serviceObserver_;
407 };
408
impl()409 HandsFreeAudioGateway::impl::impl()
410 {
411 serviceObserver_ = new AgServiceObserver(observers_);
412 profileRegisterId = BluetoothProfileManager::GetInstance().RegisterFunc(PROFILE_HFP_AG,
413 [this](sptr<IRemoteObject> remote) {
414 sptr<IBluetoothHfpAg> proxy = iface_cast<IBluetoothHfpAg>(remote);
415 CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
416 proxy->RegisterObserver(serviceObserver_);
417 });
418 }
419
~impl()420 HandsFreeAudioGateway::impl::~impl()
421 {
422 HILOGD("enter");
423 BluetoothProfileManager::GetInstance().DeregisterFunc(profileRegisterId);
424 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
425 CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
426 proxy->DeregisterObserver(serviceObserver_);
427 }
428
GetProfile()429 HandsFreeAudioGateway *HandsFreeAudioGateway::GetProfile()
430 {
431 HILOGD("enter");
432 #ifdef DTFUZZ_TEST
433 static BluetoothNoDestructor<HandsFreeAudioGateway> instance;
434 return instance.get();
435 #else
436 static HandsFreeAudioGateway instance;
437 return &instance;
438 #endif
439 }
440
HandsFreeAudioGateway()441 HandsFreeAudioGateway::HandsFreeAudioGateway()
442 {
443 HILOGD("enter");
444 pimpl = std::make_unique<impl>();
445 }
446
~HandsFreeAudioGateway()447 HandsFreeAudioGateway::~HandsFreeAudioGateway()
448 {
449 HILOGD("enter");
450 }
451
GetConnectedDevices() const452 std::vector<BluetoothRemoteDevice> HandsFreeAudioGateway::GetConnectedDevices() const
453 {
454 std::vector<BluetoothRemoteDevice> devices;
455 if (!IS_BT_ENABLED()) {
456 HILOGE("bluetooth is off.");
457 return devices;
458 }
459 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
460 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, devices, "failed: no proxy");
461
462 pimpl->GetConnectedDevices(devices);
463 return devices;
464 }
465
GetConnectedDevices(std::vector<BluetoothRemoteDevice> & devices)466 int32_t HandsFreeAudioGateway::GetConnectedDevices(std::vector<BluetoothRemoteDevice> &devices)
467 {
468 if (!IS_BT_ENABLED()) {
469 HILOGE("bluetooth is off.");
470 return BT_ERR_INVALID_STATE;
471 }
472
473 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
474 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
475
476 return pimpl->GetConnectedDevices(devices);
477 }
478
GetDevicesByStates(std::vector<int> states)479 std::vector<BluetoothRemoteDevice> HandsFreeAudioGateway::GetDevicesByStates(std::vector<int> states)
480 {
481 if (!IS_BT_ENABLED()) {
482 HILOGE("bluetooth is off.");
483 return std::vector<BluetoothRemoteDevice>();
484 }
485 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
486 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, std::vector<BluetoothRemoteDevice>(), "failed: no proxy");
487
488 return pimpl->GetDevicesByStates(states);
489 }
490
GetDeviceState(const BluetoothRemoteDevice & device,int32_t & state)491 int32_t HandsFreeAudioGateway::GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state)
492 {
493 HILOGD("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
494 if (!IS_BT_ENABLED()) {
495 HILOGE("bluetooth is off.");
496 return BT_ERR_INVALID_STATE;
497 }
498
499 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
500 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
501
502 return pimpl->GetDeviceState(device, state);
503 }
504
Connect(const BluetoothRemoteDevice & device)505 int32_t HandsFreeAudioGateway::Connect(const BluetoothRemoteDevice &device)
506 {
507 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
508 if (!IS_BT_ENABLED()) {
509 HILOGE("bluetooth is off.");
510 return BT_ERR_INVALID_STATE;
511 }
512
513 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
514 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
515
516 return pimpl->Connect(device);
517 }
518
Disconnect(const BluetoothRemoteDevice & device)519 int32_t HandsFreeAudioGateway::Disconnect(const BluetoothRemoteDevice &device)
520 {
521 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
522 if (!IS_BT_ENABLED()) {
523 HILOGE("bluetooth is off.");
524 return BT_ERR_INVALID_STATE;
525 }
526 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
527 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
528
529 return pimpl->Disconnect(device);
530 }
531
GetScoState(const BluetoothRemoteDevice & device) const532 int HandsFreeAudioGateway::GetScoState(const BluetoothRemoteDevice &device) const
533 {
534 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
535 if (!IS_BT_ENABLED()) {
536 HILOGE("bluetooth is off.");
537 return static_cast<int>(HfpScoConnectState::SCO_DISCONNECTED);
538 }
539 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
540 CHECK_AND_RETURN_LOG_RET(proxy != nullptr,
541 static_cast<int>(HfpScoConnectState::SCO_DISCONNECTED), "failed: no proxy");
542
543 return pimpl->GetScoState(device);
544 }
545
ConnectSco(uint8_t callType)546 int32_t HandsFreeAudioGateway::ConnectSco(uint8_t callType)
547 {
548 HILOGI("enter, callType: %{public}d", callType);
549 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
550 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
551 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY,
552 "hfpAG proxy is nullptr.");
553 CHECK_AND_RETURN_LOG_RET((pimpl->IsValidCallType(callType)), BT_ERR_INVALID_PARAM,
554 "connect sco call type error.");
555 return pimpl->ConnectSco(callType);
556 }
557
DisconnectSco(uint8_t callType)558 int32_t HandsFreeAudioGateway::DisconnectSco(uint8_t callType)
559 {
560 HILOGI("enter, callType: %{public}d", callType);
561 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
562 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
563 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY,
564 "hfpAG proxy is nullptr.");
565 CHECK_AND_RETURN_LOG_RET((pimpl->IsValidCallType(callType)), BT_ERR_INVALID_PARAM,
566 "disconnect sco call type error.");
567 return pimpl->DisconnectSco(callType);
568 }
569
ConnectSco()570 bool HandsFreeAudioGateway::ConnectSco()
571 {
572 return true;
573 }
574
DisconnectSco()575 bool HandsFreeAudioGateway::DisconnectSco()
576 {
577 return true;
578 }
579
PhoneStateChanged(int numActive,int numHeld,int callState,const std::string & number,int type,const std::string & name)580 void HandsFreeAudioGateway::PhoneStateChanged(
581 int numActive, int numHeld, int callState, const std::string &number, int type, const std::string &name)
582 {
583 CHECK_AND_RETURN_LOG(IS_BT_ENABLED(), "bluetooth is off.");
584 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
585 CHECK_AND_RETURN_LOG(proxy != nullptr, "hfpAG proxy is nullptr.");
586
587 BluetoothPhoneState phoneState;
588 phoneState.SetActiveNum(numActive);
589 phoneState.SetHeldNum(numHeld);
590 phoneState.SetCallState(callState);
591 phoneState.SetNumber(number);
592 phoneState.SetCallType(type);
593 phoneState.SetName(name);
594 pimpl->PhoneStateChanged(phoneState);
595 }
596
ClccResponse(int index,int direction,int status,int mode,bool mpty,const std::string & number,int type)597 void HandsFreeAudioGateway::ClccResponse(
598 int index, int direction, int status, int mode, bool mpty, const std::string &number, int type)
599 {
600 if (!IS_BT_ENABLED()) {
601 HILOGE("bluetooth is off.");
602 return;
603 }
604
605 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
606 CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
607
608 pimpl->ClccResponse(index, direction, status, mode, mpty, number, type);
609 }
610
OpenVoiceRecognition(const BluetoothRemoteDevice & device)611 bool HandsFreeAudioGateway::OpenVoiceRecognition(const BluetoothRemoteDevice &device)
612 {
613 HILOG_COMM_INFO("OpenVoiceRecognition: %{public}s", GET_ENCRYPT_ADDR(device));
614 if (!IS_BT_ENABLED()) {
615 HILOGE("bluetooth is off.");
616 return false;
617 }
618
619 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
620 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
621
622 return pimpl->OpenVoiceRecognition(device);
623 }
624
CloseVoiceRecognition(const BluetoothRemoteDevice & device)625 bool HandsFreeAudioGateway::CloseVoiceRecognition(const BluetoothRemoteDevice &device)
626 {
627 HILOG_COMM_INFO("CloseVoiceRecognition: %{public}s", GET_ENCRYPT_ADDR(device));
628 if (!IS_BT_ENABLED()) {
629 HILOGE("bluetooth is off.");
630 return false;
631 }
632
633 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
634 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
635
636 return pimpl->CloseVoiceRecognition(device);
637 }
638
SetActiveDevice(const BluetoothRemoteDevice & device)639 bool HandsFreeAudioGateway::SetActiveDevice(const BluetoothRemoteDevice &device)
640 {
641 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
642 if (!IS_BT_ENABLED()) {
643 HILOGE("bluetooth is off.");
644 return false;
645 }
646
647 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
648 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
649
650 return pimpl->SetActiveDevice(device);
651 }
652
GetActiveDevice() const653 BluetoothRemoteDevice HandsFreeAudioGateway::GetActiveDevice() const
654 {
655 BluetoothRemoteDevice device;
656 if (!IS_BT_ENABLED()) {
657 HILOGE("bluetooth is off.");
658 return device;
659 }
660
661 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
662 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, device, "failed: no proxy");
663
664 device = pimpl->GetActiveDevice();
665 return device;
666 }
667
SetConnectStrategy(const BluetoothRemoteDevice & device,int strategy)668 int HandsFreeAudioGateway::SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy)
669 {
670 HILOGI("enter, device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
671 if (!IS_BT_ENABLED()) {
672 HILOGE("bluetooth is off.");
673 return BT_ERR_INVALID_STATE;
674 }
675
676 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
677 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
678
679 if ((!device.IsValidBluetoothRemoteDevice()) || (
680 (strategy != static_cast<int>(BTStrategyType::CONNECTION_ALLOWED)) &&
681 (strategy != static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN)))) {
682 HILOGI("input parameter error.");
683 return BT_ERR_INVALID_PARAM;
684 }
685 return pimpl->SetConnectStrategy(device, strategy);
686 }
687
GetConnectStrategy(const BluetoothRemoteDevice & device,int & strategy) const688 int HandsFreeAudioGateway::GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const
689 {
690 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
691 if (!IS_BT_ENABLED()) {
692 HILOGE("bluetooth is off.");
693 return BT_ERR_INVALID_STATE;
694 }
695
696 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
697 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
698
699 if (!device.IsValidBluetoothRemoteDevice()) {
700 HILOGI("input parameter error.");
701 return BT_ERR_INVALID_PARAM;
702 }
703 return pimpl->GetConnectStrategy(device, strategy);
704 }
705
IsInbandRingingEnabled(bool & isEnabled) const706 int HandsFreeAudioGateway::IsInbandRingingEnabled(bool &isEnabled) const
707 {
708 HILOGI("enter");
709 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
710 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
711 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "hfpAG proxy is nullptr");
712 return pimpl->IsInbandRingingEnabled(isEnabled);
713 }
714
CallDetailsChanged(int callId,int callState)715 void HandsFreeAudioGateway::CallDetailsChanged(int callId, int callState)
716 {
717 HILOGI("enter, callId: %{public}d, callState: %{public}d", callId, callState);
718 CHECK_AND_RETURN_LOG(IS_BT_ENABLED(), "bluetooth is off.");
719 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
720 CHECK_AND_RETURN_LOG(proxy != nullptr, "hfpAG proxy is nullptr");
721 pimpl->CallDetailsChanged(callId, callState);
722 }
723
EnableBtCallLog(bool state)724 void HandsFreeAudioGateway::EnableBtCallLog(bool state)
725 {
726 HILOGI("enter");
727 CHECK_AND_RETURN_LOG(IS_BT_ENABLED(), "bluetooth is off.");
728 pimpl->EnableBtCallLog(state);
729 }
730
IsVgsSupported(const BluetoothRemoteDevice & device,bool & isSupported) const731 int HandsFreeAudioGateway::IsVgsSupported(const BluetoothRemoteDevice &device, bool &isSupported) const
732 {
733 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
734 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
735 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
736 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
737 CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "input parameter error.");
738 return pimpl->IsVgsSupported(device, isSupported);
739 }
740
GetVirtualDeviceList(std::vector<std::string> & devices) const741 void HandsFreeAudioGateway::GetVirtualDeviceList(std::vector<std::string> &devices) const
742 {
743 CHECK_AND_RETURN_LOG(IS_BT_ENABLED(), "bluetooth is off.");
744 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
745 CHECK_AND_RETURN_LOG(proxy != nullptr, "hfpAG proxy is nullptr");
746 proxy->GetVirtualDeviceList(devices);
747 }
748
RegisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)749 void HandsFreeAudioGateway::RegisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
750 {
751 HILOGD("enter");
752 CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
753 CHECK_AND_RETURN_LOG(observer != nullptr, "observer is null.");
754 pimpl->RegisterObserver(observer);
755 }
756
DeregisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)757 void HandsFreeAudioGateway::DeregisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
758 {
759 HILOGD("enter");
760 CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
761 pimpl->DeregisterObserver(observer);
762 }
763
GetCurrentCallType(int & callType)764 int HandsFreeAudioGateway::GetCurrentCallType(int &callType)
765 {
766 HILOGD("enter");
767 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
768 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
769 return proxy->GetCurrentCallType(callType);
770 }
771
IsVoiceRecognitionSupported(const BluetoothRemoteDevice & device,bool & isSupported) const772 int HandsFreeAudioGateway::IsVoiceRecognitionSupported(const BluetoothRemoteDevice &device, bool &isSupported) const
773 {
774 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
775 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
776 sptr<IBluetoothHfpAg> proxy = GetRemoteProxy<IBluetoothHfpAg>(PROFILE_HFP_AG);
777 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is null");
778 CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "input parameter error.");
779 return proxy->IsHfpFeatureSupported(BluetoothRawAddress(device.GetDeviceAddr()), isSupported,
780 static_cast<int>(bluetooth::HfpFeatureType::VOICE_RECOGNITION));
781 }
782 } // namespace Bluetooth
783 } // namespace OHOS