• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "bluetooth_a2dp_snk.h"
17 #include "bluetooth_a2dp_sink_proxy.h"
18 #include "bluetooth_a2dp_sink_observer_stub.h"
19 #include "bluetooth_host_proxy.h"
20 #include "bluetooth_observer_list.h"
21 #include "raw_address.h"
22 #include "bluetooth_def.h"
23 #include "bluetooth_host.h"
24 
25 #include "bluetooth_log.h"
26 #include "iservice_registry.h"
27 #include "system_ability_definition.h"
28 
29 namespace OHOS {
30 namespace Bluetooth {
31 using namespace bluetooth;
32 
33 struct A2dpSink::impl {
34     impl();
35     ~impl();
36 
37     BluetoothObserverList<A2dpSinkObserver> observers_;
38     sptr<IBluetoothA2dpSink> proxy_ = nullptr;
39     class BluetoothA2dpSinkObserverImp;
40     sptr<BluetoothA2dpSinkObserverImp> observerImp_ = nullptr;
41     class BluetoothA2dpSinkDeathRecipient;
42     sptr<BluetoothA2dpSinkDeathRecipient> deathRecipient_ = nullptr;
43 
44 private:
45     void GetProxy();
46 };
47 
48 class A2dpSink::impl::BluetoothA2dpSinkObserverImp : public BluetoothA2dpSinkObserverStub {
49 public:
BluetoothA2dpSinkObserverImp(A2dpSink::impl & a2dpSink)50     BluetoothA2dpSinkObserverImp(A2dpSink::impl &a2dpSink) : a2dpSink_(a2dpSink)
51     {};
~BluetoothA2dpSinkObserverImp()52     ~BluetoothA2dpSinkObserverImp() override
53     {};
54 
Register(std::shared_ptr<A2dpSinkObserver> & observer)55     void Register(std::shared_ptr<A2dpSinkObserver> &observer)
56     {
57         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
58         a2dpSink_.observers_.Register(observer);
59     }
60 
Deregister(std::shared_ptr<A2dpSinkObserver> & observer)61     void Deregister(std::shared_ptr<A2dpSinkObserver> &observer)
62     {
63         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
64         a2dpSink_.observers_.Deregister(observer);
65     }
66 
OnConnectionStateChanged(const RawAddress & device,int state)67     void OnConnectionStateChanged(const RawAddress &device, int state) override
68     {
69         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
70         a2dpSink_.observers_.ForEach([device, state](std::shared_ptr<A2dpSinkObserver> observer) {
71             observer->OnConnectionStateChanged(BluetoothRemoteDevice(device.GetAddress(), 0), state);
72         });
73     }
74 
75 private:
76     A2dpSink::impl &a2dpSink_;
77     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothA2dpSinkObserverImp);
78 };
79 
80 class A2dpSink::impl::BluetoothA2dpSinkDeathRecipient final : public IRemoteObject::DeathRecipient {
81 public:
BluetoothA2dpSinkDeathRecipient(A2dpSink::impl & a2dpSinkDeath)82     BluetoothA2dpSinkDeathRecipient(A2dpSink::impl &a2dpSinkDeath) : a2dpSinkDeath_(a2dpSinkDeath)
83     {};
84     ~BluetoothA2dpSinkDeathRecipient() final = default;
85     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothA2dpSinkDeathRecipient);
86 
OnRemoteDied(const wptr<IRemoteObject> & remote)87     void OnRemoteDied(const wptr<IRemoteObject> &remote) final
88     {
89         HILOGI("A2dpSink::impl::BluetoothA2dpSinkDeathRecipient::OnRemoteDied starts");
90         a2dpSinkDeath_.proxy_->AsObject()->RemoveDeathRecipient(a2dpSinkDeath_.deathRecipient_);
91         a2dpSinkDeath_.proxy_ = nullptr;
92     }
93 
94 private:
95     A2dpSink::impl &a2dpSinkDeath_;
96 };
97 
impl()98 A2dpSink::impl::impl()
99 {
100     HILOGI("A2dpSink::impl::impl start");
101     GetProxy();
102     if (proxy_ == nullptr) {
103         HILOGI("A2dpSink::get proxy_ failed");
104         return;
105     }
106 
107     deathRecipient_ = new BluetoothA2dpSinkDeathRecipient(*this);
108     proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
109 
110     observerImp_ = new (std::nothrow) BluetoothA2dpSinkObserverImp(*this);
111     proxy_->RegisterObserver(observerImp_);
112 };
113 
~impl()114 A2dpSink::impl::~impl()
115 {
116     HILOGD("A2dpSink::impl::~impl  start");
117     if (proxy_ != nullptr) {
118         proxy_->DeregisterObserver(observerImp_);
119         proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
120     }
121 }
122 
GetProxy()123 void A2dpSink::impl::GetProxy()
124 {
125     HILOGI("A2dpSink::impl::GetProxy start");
126     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
127     if (!samgr) {
128         HILOGE("A2dpSink::impl::GetProxy error: no samgr");
129         return;
130     }
131 
132     sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
133     if (!hostRemote) {
134         HILOGE("A2dpSink::impl:GetProxy failed: no hostRemote");
135         return;
136     }
137 
138     sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
139     if (!hostProxy) {
140         HILOGE("A2dpSink::impl::GetProxy error: host no proxy");
141         return;
142     }
143 
144     sptr<IRemoteObject> remote = hostProxy->GetProfile(PROFILE_A2DP_SINK);
145     if (!remote) {
146         HILOGE("A2dpSink::impl::GetProxy error: no remote");
147         return;
148     }
149 
150     proxy_ = iface_cast<IBluetoothA2dpSink>(remote);
151     if (!proxy_) {
152         HILOGE("A2dpSink::impl::GetProxy error: no proxy");
153         return;
154     }
155 }
156 
A2dpSink()157 A2dpSink::A2dpSink()
158 {
159     pimpl = std::make_unique<impl>();
160     if (!pimpl) {
161         HILOGE("A2dpSink::A2dpSink fails: no pimpl");
162     }
163 }
164 
~A2dpSink()165 A2dpSink::~A2dpSink()
166 {
167     HILOGI("A2dpSink::~A2dpSink start");
168 }
169 
RegisterObserver(A2dpSinkObserver * observer)170 void A2dpSink::RegisterObserver(A2dpSinkObserver *observer)
171 {
172     HILOGI("[A2dpSink] %{public}s\n", __func__);
173     std::shared_ptr<A2dpSinkObserver> pointer(observer, [](A2dpSinkObserver *) {});
174     pimpl->observers_.Register(pointer);
175 }
176 
DeregisterObserver(A2dpSinkObserver * observer)177 void A2dpSink::DeregisterObserver(A2dpSinkObserver *observer)
178 {
179     HILOGI("[A2dpSink] %{public}s\n", __func__);
180     std::shared_ptr<A2dpSinkObserver> pointer(observer, [](A2dpSinkObserver *) {});
181     pimpl->observers_.Deregister(pointer);
182 }
183 
GetDeviceState(const BluetoothRemoteDevice & device) const184 int A2dpSink::GetDeviceState(const BluetoothRemoteDevice &device) const
185 {
186     HILOGI("[A2dpSink] %{public}s\n", __func__);
187 
188     if (!device.IsValidBluetoothRemoteDevice()) {
189         HILOGI("[A2dpSink] input parameter error.");
190         return RET_BAD_STATUS;
191     }
192 
193     int ret = RET_NO_ERROR;
194     if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
195         ret = pimpl->proxy_->GetDeviceState(RawAddress(device.GetDeviceAddr()));
196     } else {
197         HILOGI("[A2dpSink] proxy or bt disable.");
198         ret = RET_BAD_STATUS;
199     }
200 
201     return ret;
202 }
203 
GetDevicesByStates(std::vector<int> states) const204 std::vector<BluetoothRemoteDevice> A2dpSink::GetDevicesByStates(std::vector<int> states) const
205 {
206     HILOGI("[A2dpSink] %{public}s\n", __func__);
207     std::vector<BluetoothRemoteDevice> devices;
208     std::vector<BluetoothRawAddress> devicesRaw;
209 
210     for (int state : states) {
211         if ((static_cast<int>(BTConnectState::CONNECTED) != state) &&
212             (static_cast<int>(BTConnectState::CONNECTING) != state) &&
213             (static_cast<int>(BTConnectState::DISCONNECTING) != state) &&
214             (static_cast<int>(BTConnectState::DISCONNECTED) != state)) {
215             HILOGI("[A2dpSink] input parameter error.");
216             return devices;
217         }
218     }
219 
220     if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
221         pimpl->proxy_->GetDevicesByStates(states);
222     }
223 
224     for (RawAddress it : devicesRaw) {
225         BluetoothRemoteDevice remoteDevice(it.GetAddress(), 0);
226         devices.push_back(remoteDevice);
227     }
228 
229     return devices;
230 }
231 
GetPlayingState(const BluetoothRemoteDevice & device) const232 int A2dpSink::GetPlayingState(const BluetoothRemoteDevice &device) const
233 {
234     HILOGI("[A2dpSink] %{public}s\n", __func__);
235 
236     if (!device.IsValidBluetoothRemoteDevice()) {
237         HILOGI("[A2dpSink] input parameter error.");
238         return RET_BAD_STATUS;
239     }
240 
241     int ret = RET_NO_ERROR;
242     if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
243         ret = pimpl->proxy_->GetPlayingState(RawAddress(device.GetDeviceAddr()));
244     } else {
245         HILOGI("[A2dpSink] proxy or bt disable.");
246         ret = RET_BAD_STATUS;
247     }
248 
249     return ret;
250 }
251 
Connect(const BluetoothRemoteDevice & device)252 bool A2dpSink::Connect(const BluetoothRemoteDevice &device)
253 {
254     HILOGI("[A2dpSink] %{public}s\n", __func__);
255 
256     if (!device.IsValidBluetoothRemoteDevice()) {
257         HILOGI("[A2dpSink] input parameter error.");
258         return false;
259     }
260 
261     int ret = RET_NO_ERROR;
262     if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
263         ret = pimpl->proxy_->Connect(RawAddress(device.GetDeviceAddr()));
264     } else {
265         HILOGI("[A2dpSink] proxy or bt disable.");
266         return false;
267     }
268     return (RET_NO_ERROR == ret);
269 }
270 
Disconnect(const BluetoothRemoteDevice & device)271 bool A2dpSink::Disconnect(const BluetoothRemoteDevice &device)
272 {
273     HILOGI("[A2dpSink] %{public}s\n", __func__);
274 
275     if (!device.IsValidBluetoothRemoteDevice()) {
276         HILOGI("[A2dpSink] input parameter error.");
277         return false;
278     }
279 
280     int ret = RET_NO_ERROR;
281     if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
282         ret = pimpl->proxy_->Disconnect(RawAddress(device.GetDeviceAddr()));
283     } else {
284         HILOGI("[A2dpSink] proxy or bt disable.");
285         return false;
286     }
287     return (RET_NO_ERROR == ret);
288 }
289 
GetProfile()290 A2dpSink *A2dpSink::GetProfile()
291 {
292     HILOGI("[A2dpSink] %{public}s\n", __func__);
293     static A2dpSink service;
294     return &service;
295 }
296 
SetConnectStrategy(const BluetoothRemoteDevice & device,int strategy)297 bool A2dpSink::SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy)
298 {
299     HILOGI("[A2dpSink] %s", __func__);
300 
301     if ((!device.IsValidBluetoothRemoteDevice()) || (((int)BTStrategyType::CONNECTION_ALLOWED != strategy) &&
302                                                         ((int)BTStrategyType::CONNECTION_FORBIDDEN != strategy))) {
303         HILOGI("[A2dpSink] input parameter error.");
304         return false;
305     }
306 
307     int ret = RET_NO_ERROR;
308     if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
309         ret = pimpl->proxy_->SetConnectStrategy(RawAddress(device.GetDeviceAddr()), strategy);
310     } else {
311         HILOGI("[A2dpSink] proxy or bt disable.");
312         return false;
313     }
314 
315     return (RET_NO_ERROR == ret);
316 }
317 
GetConnectStrategy(const BluetoothRemoteDevice & device) const318 int A2dpSink::GetConnectStrategy(const BluetoothRemoteDevice &device) const
319 {
320     HILOGI("[A2dpSink] %{public}s\n", __func__);
321 
322     if (!device.IsValidBluetoothRemoteDevice()) {
323         HILOGI("[A2dpSink] input parameter error.");
324         return RET_BAD_PARAM;
325     }
326 
327     int ret = RET_NO_ERROR;
328     if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
329         ret = pimpl->proxy_->GetConnectStrategy(RawAddress(device.GetDeviceAddr()));
330     } else {
331         HILOGI("[A2dpSink] proxy or bt disable.");
332         return false;
333     }
334     return ret;
335 }
336 }  // namespace Bluetooth
337 }  // namespace OHOS