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 IsDeviceWearing(const BluetoothRemoteDevice &device);
33     int IsWearDetectionSupported(const BluetoothRemoteDevice &device, bool &isSupported);
34     int SendDeviceSelection(const BluetoothRemoteDevice &device, int useA2dp, int useHfp, int userSelection);
35 };
36 
impl()37 BluetoothAudioManager::impl::impl()
38 {}
39 
BluetoothAudioManager()40 BluetoothAudioManager::BluetoothAudioManager():pimpl(std::make_unique<impl>())
41 {}
42 
EnableWearDetection(const std::string &deviceId)43 int BluetoothAudioManager::impl::EnableWearDetection(const std::string &deviceId)
44 {
45     sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
46     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
47     return proxy->EnableWearDetection(deviceId);
48 }
49 
DisableWearDetection(const std::string &deviceId)50 int BluetoothAudioManager::impl::DisableWearDetection(const std::string &deviceId)
51 {
52     sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
53     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
54     return proxy->DisableWearDetection(deviceId);
55 }
56 
GetWearDetectionState(const std::string &deviceId, int32_t &ability)57 int BluetoothAudioManager::impl::GetWearDetectionState(const std::string &deviceId, int32_t &ability)
58 {
59     sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
60     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
61     return proxy->GetWearDetectionState(deviceId, ability);
62 }
63 
IsDeviceWearing(const BluetoothRemoteDevice &device)64 int BluetoothAudioManager::impl::IsDeviceWearing(const BluetoothRemoteDevice &device)
65 {
66     sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
67     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
68     return proxy->IsDeviceWearing(BluetoothRawAddress(device.GetDeviceAddr()));
69 }
70 
IsWearDetectionSupported(const BluetoothRemoteDevice &device, bool &isSupported)71 int BluetoothAudioManager::impl::IsWearDetectionSupported(const BluetoothRemoteDevice &device, bool &isSupported)
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->IsWearDetectionSupported(BluetoothRawAddress(device.GetDeviceAddr()), isSupported);
76 }
77 
SendDeviceSelection(const BluetoothRemoteDevice &device, int useA2dp, int useHfp, int userSelection)78 int BluetoothAudioManager::impl::SendDeviceSelection(const BluetoothRemoteDevice &device,
79     int useA2dp, int useHfp, int userSelection)
80 {
81     sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
82     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
83     return proxy->SendDeviceSelection(BluetoothRawAddress(device.GetDeviceAddr()), useA2dp, useHfp, userSelection);
84 }
85 
EnableWearDetection(const std::string &deviceId)86 int BluetoothAudioManager::EnableWearDetection(const std::string &deviceId)
87 {
88     sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
89     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
90     return pimpl->EnableWearDetection(deviceId);
91 }
92 
DisableWearDetection(const std::string &deviceId)93 int BluetoothAudioManager::DisableWearDetection(const std::string &deviceId)
94 {
95     sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
96     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
97     return pimpl->DisableWearDetection(deviceId);
98 }
99 
GetWearDetectionState(const std::string &deviceId, int32_t &ability)100 int BluetoothAudioManager::GetWearDetectionState(const std::string &deviceId, int32_t &ability)
101 {
102     sptr<IBluetoothAudioManager> proxy = GetRemoteProxy<IBluetoothAudioManager>(PROFILE_AUDIO_MANAGER);
103     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INVALID_STATE, "failed: no proxy");
104     return pimpl->GetWearDetectionState(deviceId, ability);
105 }
106 
IsDeviceWearing(const BluetoothRemoteDevice &device)107 int BluetoothAudioManager::IsDeviceWearing(const BluetoothRemoteDevice &device)
108 {
109     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
110     CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
111     CHECK_AND_RETURN_LOG_RET(device.IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_STATE, "input parameter error");
112     CHECK_AND_RETURN_LOG_RET(pimpl != nullptr, BT_ERR_INVALID_STATE, "pimpl is null");
113 
114     return pimpl->IsDeviceWearing(device);
115 }
116 
IsWearDetectionSupported(const BluetoothRemoteDevice &device, bool &isSupported)117 int BluetoothAudioManager::IsWearDetectionSupported(const BluetoothRemoteDevice &device, bool &isSupported)
118 {
119     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
120     if (!IS_BT_ENABLED()) {
121         HILOGE("bluetooth is off.");
122         return BT_ERR_INVALID_STATE;
123     }
124 
125     if (!device.IsValidBluetoothRemoteDevice()) {
126         HILOGE("input parameter error.");
127         return BT_ERR_INVALID_PARAM;
128     }
129 
130     if (pimpl == nullptr) {
131         HILOGE("pimpl is null");
132         return BT_ERR_INVALID_STATE;
133     }
134     return pimpl->IsWearDetectionSupported(device, isSupported);
135 }
136 
SendDeviceSelection(const BluetoothRemoteDevice &device, int useA2dp, int useHfp, int userSelection) const137 int BluetoothAudioManager::SendDeviceSelection(const BluetoothRemoteDevice &device,
138     int useA2dp, int useHfp, int userSelection) const
139 {
140     HILOGI("enter, device: %{public}s, useA2dp: %{public}d, useHfp: %{public}d, userSelection:%{public}d",
141         GET_ENCRYPT_ADDR(device), useA2dp, useHfp, userSelection);
142     if (!IS_BT_ENABLED()) {
143         HILOGE("bluetooth is off.");
144         return BT_ERR_INVALID_STATE;
145     }
146 
147     if (!device.IsValidBluetoothRemoteDevice()) {
148         HILOGE("input parameter error.");
149         return BT_ERR_INVALID_PARAM;
150     }
151 
152     if (pimpl == nullptr) {
153         HILOGE("pimpl is null");
154         return BT_ERR_INVALID_STATE;
155     }
156     return pimpl->SendDeviceSelection(device, useA2dp, useHfp, userSelection);
157 }
158 
GetInstance()159 BluetoothAudioManager &BluetoothAudioManager::GetInstance()
160 {
161 #ifdef DTFUZZ_TEST
162     static BluetoothNoDestructor<BluetoothAudioManager> instance;
163     return *instance;
164 #else
165     static BluetoothAudioManager instance;
166     return instance;
167 #endif
168 }
169 
170 }  // namespace Bluetooth
171 }  // namespace OHOS
172