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 #include "bluetooth_a2dp_snk.h"
16 #include <cstdint>
17 #include "bluetooth_a2dp_sink_observer_stub.h"
18 #include "bluetooth_def.h"
19 #include "bluetooth_host.h"
20 #include "bluetooth_profile_manager.h"
21 #include "bluetooth_log.h"
22 #include "bluetooth_observer_list.h"
23 #include "bluetooth_remote_device.h"
24 #include "bluetooth_types.h"
25 #include "bluetooth_utils.h"
26 #include "functional"
27 #include "i_bluetooth_a2dp_sink.h"
28 #include "i_bluetooth_a2dp_sink_observer.h"
29 #include "i_bluetooth_host.h"
30 #include "if_system_ability_manager.h"
31 #include "iosfwd"
32 #include "iremote_broker.h"
33 #include "iremote_object.h"
34 #include "iservice_registry.h"
35 #include "list"
36 #include "memory"
37 #include "new"
38 #include "raw_address.h"
39 #include "refbase.h"
40 #include "string"
41 #include "system_ability_definition.h"
42 #include "vector"
43
44 namespace OHOS {
45 namespace Bluetooth {
46 using namespace OHOS::bluetooth;
47 std::mutex g_a2dpSnkProxyMutex;
48 struct A2dpSink::impl {
49 impl();
50 ~impl();
51 BluetoothObserverList<A2dpSinkObserver> observers_;
52 class BluetoothA2dpSinkObserverImp;
53 sptr<BluetoothA2dpSinkObserverImp> observerImp_ = nullptr;
54 int32_t profileRegisterId = 0;
55 };
56
57 class A2dpSink::impl::BluetoothA2dpSinkObserverImp : public BluetoothA2dpSinkObserverStub {
58 public:
BluetoothA2dpSinkObserverImp(A2dpSink::impl & a2dpSink)59 explicit BluetoothA2dpSinkObserverImp(A2dpSink::impl &a2dpSink) : a2dpSink_(a2dpSink)
60 {};
~BluetoothA2dpSinkObserverImp()61 ~BluetoothA2dpSinkObserverImp() override
62 {};
63
Register(std::shared_ptr<A2dpSinkObserver> & observer)64 void Register(std::shared_ptr<A2dpSinkObserver> &observer)
65 {
66 HILOGI("enter");
67 a2dpSink_.observers_.Register(observer);
68 }
69
Deregister(std::shared_ptr<A2dpSinkObserver> & observer)70 void Deregister(std::shared_ptr<A2dpSinkObserver> &observer)
71 {
72 HILOGI("enter");
73 a2dpSink_.observers_.Deregister(observer);
74 }
75
OnConnectionStateChanged(const RawAddress & device,int state)76 void OnConnectionStateChanged(const RawAddress &device, int state) override
77 {
78 HILOGD("device: %{public}s, state: %{public}d", GetEncryptAddr(device.GetAddress()).c_str(), state);
79 a2dpSink_.observers_.ForEach([device, state](std::shared_ptr<A2dpSinkObserver> observer) {
80 observer->OnConnectionStateChanged(BluetoothRemoteDevice(device.GetAddress(), 0), state);
81 });
82 }
83
84 private:
85 A2dpSink::impl &a2dpSink_;
86 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothA2dpSinkObserverImp);
87 };
88
impl()89 A2dpSink::impl::impl()
90 {
91 observerImp_ = new (std::nothrow) BluetoothA2dpSinkObserverImp(*this);
92 CHECK_AND_RETURN_LOG(observerImp_ != nullptr, "observerImp_ is nullptr");
93 profileRegisterId = DelayedSingleton<BluetoothProfileManager>::GetInstance()->RegisterFunc(PROFILE_A2DP_SINK,
94 [this](sptr<IRemoteObject> remote) {
95 sptr<IBluetoothA2dpSink> proxy = iface_cast<IBluetoothA2dpSink>(remote);
96 CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
97 proxy->RegisterObserver(observerImp_);
98 });
99 };
100
~impl()101 A2dpSink::impl::~impl()
102 {
103 HILOGD("start");
104 DelayedSingleton<BluetoothProfileManager>::GetInstance()->DeregisterFunc(profileRegisterId);
105 sptr<IBluetoothA2dpSink> proxy = GetRemoteProxy<IBluetoothA2dpSink>(PROFILE_A2DP_SINK);
106 CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
107 proxy->DeregisterObserver(observerImp_);
108 }
109
A2dpSink()110 A2dpSink::A2dpSink()
111 {
112 pimpl = std::make_unique<impl>();
113 if (!pimpl) {
114 HILOGE("fails: no pimpl");
115 }
116 }
117
~A2dpSink()118 A2dpSink::~A2dpSink()
119 {
120 HILOGD("start");
121 }
122
RegisterObserver(std::shared_ptr<A2dpSinkObserver> observer)123 void A2dpSink::RegisterObserver(std::shared_ptr<A2dpSinkObserver> observer)
124 {
125 HILOGD("enter");
126 CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
127 pimpl->observers_.Register(observer);
128 }
129
DeregisterObserver(std::shared_ptr<A2dpSinkObserver> observer)130 void A2dpSink::DeregisterObserver(std::shared_ptr<A2dpSinkObserver> observer)
131 {
132 HILOGD("enter");
133 CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
134 pimpl->observers_.Deregister(observer);
135 }
136
GetDeviceState(const BluetoothRemoteDevice & device) const137 int A2dpSink::GetDeviceState(const BluetoothRemoteDevice &device) const
138 {
139 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
140 if (!IS_BT_ENABLED()) {
141 HILOGE("bluetooth is off.");
142 return RET_BAD_STATUS;
143 }
144 sptr<IBluetoothA2dpSink> proxy = GetRemoteProxy<IBluetoothA2dpSink>(PROFILE_A2DP_SINK);
145 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "A2dpSink proxy is nullptr");
146 if (!device.IsValidBluetoothRemoteDevice()) {
147 HILOGE("input parameter error.");
148 return RET_BAD_PARAM;
149 }
150
151 return proxy->GetDeviceState(RawAddress(device.GetDeviceAddr()));
152 }
153
GetDevicesByStates(std::vector<int> states) const154 std::vector<BluetoothRemoteDevice> A2dpSink::GetDevicesByStates(std::vector<int> states) const
155 {
156 HILOGI("enter");
157
158 if (!IS_BT_ENABLED()) {
159 HILOGE("bluetooth is off.");
160 return std::vector<BluetoothRemoteDevice>();
161 }
162
163 sptr<IBluetoothA2dpSink> proxy = GetRemoteProxy<IBluetoothA2dpSink>(PROFILE_A2DP_SINK);
164 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, std::vector<BluetoothRemoteDevice>(),
165 "A2dpSink proxy is nullptr");
166
167 std::vector<int32_t> convertStates;
168 for (auto state : states) {
169 convertStates.push_back(static_cast<int32_t>(state));
170 }
171 std::vector<BluetoothRemoteDevice> devices;
172 std::vector<RawAddress> rawAddrs = proxy->GetDevicesByStates(convertStates);
173 for (auto rawAddr : rawAddrs) {
174 BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
175 devices.push_back(device);
176 }
177 return devices;
178 }
179
GetPlayingState(const BluetoothRemoteDevice & device) const180 int A2dpSink::GetPlayingState(const BluetoothRemoteDevice &device) const
181 {
182 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
183 if (!IS_BT_ENABLED()) {
184 HILOGE("bluetooth is off.");
185 return RET_BAD_STATUS;
186 }
187
188 sptr<IBluetoothA2dpSink> proxy = GetRemoteProxy<IBluetoothA2dpSink>(PROFILE_A2DP_SINK);
189 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "A2dpSink proxy is nullptr");
190
191 if (!device.IsValidBluetoothRemoteDevice()) {
192 HILOGE("input parameter error.");
193 return RET_BAD_PARAM;
194 }
195
196 int ret = RET_BAD_STATUS;
197 proxy->GetPlayingState(RawAddress(device.GetDeviceAddr()), ret);
198 return ret;
199 }
200
GetPlayingState(const BluetoothRemoteDevice & device,int & state) const201 int A2dpSink::GetPlayingState(const BluetoothRemoteDevice &device, int &state) const
202 {
203 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
204 if (!IS_BT_ENABLED()) {
205 HILOGE("bluetooth is off.");
206 return RET_BAD_STATUS;
207 }
208
209 sptr<IBluetoothA2dpSink> proxy = GetRemoteProxy<IBluetoothA2dpSink>(PROFILE_A2DP_SINK);
210 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "A2dpSink proxy is nullptr");
211
212 if (!device.IsValidBluetoothRemoteDevice()) {
213 HILOGE("input parameter error.");
214 return RET_BAD_PARAM;
215 }
216
217 return proxy->GetPlayingState(RawAddress(device.GetDeviceAddr()), state);
218 }
219
Connect(const BluetoothRemoteDevice & device)220 bool A2dpSink::Connect(const BluetoothRemoteDevice &device)
221 {
222 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
223 if (!IS_BT_ENABLED()) {
224 HILOGE("bluetooth is off.");
225 return false;
226 }
227
228 sptr<IBluetoothA2dpSink> proxy = GetRemoteProxy<IBluetoothA2dpSink>(PROFILE_A2DP_SINK);
229 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "A2dpSink proxy is nullptr");
230
231 if (!device.IsValidBluetoothRemoteDevice()) {
232 HILOGE("input parameter error.");
233 return false;
234 }
235
236 int ret = proxy->Connect(RawAddress(device.GetDeviceAddr()));
237 return (ret == RET_NO_ERROR);
238 }
239
Disconnect(const BluetoothRemoteDevice & device)240 bool A2dpSink::Disconnect(const BluetoothRemoteDevice &device)
241 {
242 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
243 if (!IS_BT_ENABLED()) {
244 HILOGE("bluetooth is off.");
245 return false;
246 }
247
248 sptr<IBluetoothA2dpSink> proxy = GetRemoteProxy<IBluetoothA2dpSink>(PROFILE_A2DP_SINK);
249 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "A2dpSink proxy is nullptr");
250
251 if (!device.IsValidBluetoothRemoteDevice()) {
252 HILOGE("input parameter error.");
253 return false;
254 }
255
256 int ret = proxy->Disconnect(RawAddress(device.GetDeviceAddr()));
257 return (ret == RET_NO_ERROR);
258 }
259
GetProfile()260 A2dpSink *A2dpSink::GetProfile()
261 {
262 HILOGI("enter");
263 static A2dpSink service;
264 return &service;
265 }
266
SetConnectStrategy(const BluetoothRemoteDevice & device,int strategy)267 bool A2dpSink::SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy)
268 {
269 HILOGI("enter, device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
270 if (!IS_BT_ENABLED()) {
271 HILOGE("bluetooth is off.");
272 return false;
273 }
274 sptr<IBluetoothA2dpSink> proxy = GetRemoteProxy<IBluetoothA2dpSink>(PROFILE_A2DP_SINK);
275 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "A2dpSink proxy is nullptr");
276
277 if ((!device.IsValidBluetoothRemoteDevice()) ||
278 ((strategy != static_cast<int>(BTStrategyType::CONNECTION_ALLOWED)) &&
279 (strategy != static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN)))) {
280 HILOGE("input parameter error.");
281 return false;
282 }
283
284 int ret = proxy->SetConnectStrategy(RawAddress(device.GetDeviceAddr()), strategy);
285 return (ret == RET_NO_ERROR);
286 }
287
GetConnectStrategy(const BluetoothRemoteDevice & device) const288 int A2dpSink::GetConnectStrategy(const BluetoothRemoteDevice &device) const
289 {
290 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
291 if (!IS_BT_ENABLED()) {
292 HILOGE("bluetooth is off.");
293 return RET_BAD_STATUS;
294 }
295 sptr<IBluetoothA2dpSink> proxy = GetRemoteProxy<IBluetoothA2dpSink>(PROFILE_A2DP_SINK);
296 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "A2dpSink proxy is nullptr");
297
298 if (!device.IsValidBluetoothRemoteDevice()) {
299 HILOGE("input parameter error.");
300 return RET_BAD_PARAM;
301 }
302
303 int ret = proxy->GetConnectStrategy(RawAddress(device.GetDeviceAddr()));
304 return ret;
305 }
306
SendDelay(const BluetoothRemoteDevice & device,uint16_t delayValue)307 bool A2dpSink::SendDelay(const BluetoothRemoteDevice &device, uint16_t delayValue)
308 {
309 HILOGI("enter, device: %{public}s, delayValue: %{public}d", GET_ENCRYPT_ADDR(device), delayValue);
310 if (!IS_BT_ENABLED()) {
311 HILOGE("bluetooth is off.");
312 return false;
313 }
314 sptr<IBluetoothA2dpSink> proxy = GetRemoteProxy<IBluetoothA2dpSink>(PROFILE_A2DP_SINK);
315 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "A2dpSink proxy is nullptr");
316
317 if (!device.IsValidBluetoothRemoteDevice()) {
318 HILOGE("input parameter error.");
319 return false;
320 }
321
322 int ret = proxy->SendDelay(RawAddress(device.GetDeviceAddr()), (int32_t)delayValue);
323 return (ret == RET_NO_ERROR);
324 }
325 } // namespace Bluetooth
326 } // namespace OHOS