1 /*
2 * Copyright (C) 2023 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_audio_manager.h"
17 #include "bluetooth_errorcode.h"
18 #include "bluetooth_log.h"
19 #include "bluetooth_host.h"
20 #include "i_bluetooth_audio_manager.h"
21 #include "i_bluetooth_host.h"
22 #include "bluetooth_utils.h"
23 #include "bluetooth_profile_manager.h"
24
25 namespace OHOS {
26 namespace Bluetooth {
27 struct BluetoothAudioManager::impl {
28 impl();
29 int EnableWearDetection(const std::string &deviceId);
30 int DisableWearDetection(const std::string &deviceId);
31 int GetWearDetectionState(const std::string &deviceId, int32_t &ability);
32 int IsWearDetectionSupported(const BluetoothRemoteDevice &device, bool &isSupported);
33 int SendDeviceSelection(const BluetoothRemoteDevice &device, int useA2dp, int useHfp, int userSelection);
34 };
35
impl()36 BluetoothAudioManager::impl::impl()
37 {}
38
BluetoothAudioManager()39 BluetoothAudioManager::BluetoothAudioManager():pimpl(std::make_unique<impl>())
40 {}
41
EnableWearDetection(const std::string & deviceId)42 int BluetoothAudioManager::impl::EnableWearDetection(const std::string &deviceId)
43 {
44 sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
45 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
46 return proxy->EnableWearDetection(deviceId);
47 }
48
DisableWearDetection(const std::string & deviceId)49 int BluetoothAudioManager::impl::DisableWearDetection(const std::string &deviceId)
50 {
51 sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
52 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
53 return proxy->DisableWearDetection(deviceId);
54 }
55
GetWearDetectionState(const std::string & deviceId,int32_t & ability)56 int BluetoothAudioManager::impl::GetWearDetectionState(const std::string &deviceId, int32_t &ability)
57 {
58 sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
59 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
60 return proxy->GetWearDetectionState(deviceId, ability);
61 }
62
IsWearDetectionSupported(const BluetoothRemoteDevice & device,bool & isSupported)63 int BluetoothAudioManager::impl::IsWearDetectionSupported(const BluetoothRemoteDevice &device, bool &isSupported)
64 {
65 sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
66 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
67 return proxy->IsWearDetectionSupported(BluetoothRawAddress(device.GetDeviceAddr()), isSupported);
68 }
69
SendDeviceSelection(const BluetoothRemoteDevice & device,int useA2dp,int useHfp,int userSelection)70 int BluetoothAudioManager::impl::SendDeviceSelection(const BluetoothRemoteDevice &device, int useA2dp,
71 int useHfp, int userSelection)
72 {
73 sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
74 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
75 return proxy->SendDeviceSelection(BluetoothRawAddress(device.GetDeviceAddr()), useA2dp, useHfp, userSelection);
76 }
77
EnableWearDetection(const std::string & deviceId)78 int BluetoothAudioManager::EnableWearDetection(const std::string &deviceId)
79 {
80 sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
81 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
82 return pimpl->EnableWearDetection(deviceId);
83 }
84
DisableWearDetection(const std::string & deviceId)85 int BluetoothAudioManager::DisableWearDetection(const std::string &deviceId)
86 {
87 sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
88 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
89 return pimpl->DisableWearDetection(deviceId);
90 }
91
GetWearDetectionState(const std::string & deviceId,int32_t & ability)92 int BluetoothAudioManager::GetWearDetectionState(const std::string &deviceId, int32_t &ability)
93 {
94 sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
95 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
96 return pimpl->GetWearDetectionState(deviceId, ability);
97 }
98
IsWearDetectionSupported(const BluetoothRemoteDevice & device,bool & isSupported)99 int BluetoothAudioManager::IsWearDetectionSupported(const BluetoothRemoteDevice &device, bool &isSupported)
100 {
101 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
102 if (!IS_BT_ENABLED()) {
103 HILOGE("bluetooth is off.");
104 return BT_ERR_INVALID_STATE;
105 }
106
107 if (!device.IsValidBluetoothRemoteDevice()) {
108 HILOGE("input parameter error.");
109 return BT_ERR_INVALID_PARAM;
110 }
111
112 if (pimpl == nullptr) {
113 HILOGE("pimpl is null");
114 return BT_ERR_INVALID_STATE;
115 }
116 return pimpl->IsWearDetectionSupported(device, isSupported);
117 }
118
SendDeviceSelection(const BluetoothRemoteDevice & device,int useA2dp,int useHfp,int userSelection) const119 int BluetoothAudioManager::SendDeviceSelection(const BluetoothRemoteDevice &device,
120 int useA2dp, int useHfp, int userSelection) const
121 {
122 HILOGI("enter, device: %{public}s, useA2dp: %{public}d, useHfp: %{public}d, userSelection:%{public}d",
123 GET_ENCRYPT_ADDR(device), useA2dp, useHfp, userSelection);
124 if (!IS_BT_ENABLED()) {
125 HILOGE("bluetooth is off.");
126 return BT_ERR_INVALID_STATE;
127 }
128
129 if (!device.IsValidBluetoothRemoteDevice()) {
130 HILOGE("input parameter error.");
131 return BT_ERR_INVALID_PARAM;
132 }
133
134 if (pimpl == nullptr) {
135 HILOGE("pimpl is null");
136 return BT_ERR_INVALID_STATE;
137 }
138 return pimpl->SendDeviceSelection(device, useA2dp, useHfp, userSelection);
139 }
140
GetInstance()141 BluetoothAudioManager &BluetoothAudioManager::GetInstance()
142 {
143 static BluetoothAudioManager instance;
144 return instance;
145 }
146
147 } // namespace Bluetooth
148 } // namespace OHOS
149