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