1 /*
2 * Copyright (C) 2021 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_def.h"
17 #include "bluetooth_log.h"
18 #include "bluetooth_utils_server.h"
19 #include "interface_profile_manager.h"
20 #include "interface_profile_a2dp_snk.h"
21 #include "remote_observer_list.h"
22 #include "interface_adapter_manager.h"
23 #include "permission_utils.h"
24 #include "bluetooth_a2dp_sink_server.h"
25
26 namespace OHOS {
27 namespace Bluetooth {
28 class A2dpSinkObserver : public IA2dpObserver {
29 public:
30 A2dpSinkObserver() = default;
31 ~A2dpSinkObserver() override = default;
32
OnConnectionStateChanged(const RawAddress & device,int state)33 void OnConnectionStateChanged(const RawAddress &device, int state) override
34 {
35 observers_->ForEach([device, state](sptr<IBluetoothA2dpSinkObserver> observer) {
36 observer->OnConnectionStateChanged(device, state);
37 });
38 }
39
SetObserver(RemoteObserverList<IBluetoothA2dpSinkObserver> * observers)40 void SetObserver(RemoteObserverList<IBluetoothA2dpSinkObserver> *observers)
41 {
42 observers_ = observers;
43 }
44
45 private:
46 RemoteObserverList<IBluetoothA2dpSinkObserver> *observers_;
47 };
48
49 struct BluetoothA2dpSinkServer::impl {
50 impl();
51 ~impl();
52
53 /// sys state observer
54 class SystemStateObserver;
55 std::unique_ptr<SystemStateObserver> systemStateObserver_ = nullptr;
56
57 RemoteObserverList<IBluetoothA2dpSinkObserver> observers_;
58 std::unique_ptr<A2dpSinkObserver> observerImp_{std::make_unique<A2dpSinkObserver>()};
59 IProfileA2dpSnk *a2dpSnkService_ = nullptr;
60 };
61
62 class BluetoothA2dpSinkServer::impl::SystemStateObserver : public ISystemStateObserver {
63 public:
SystemStateObserver(BluetoothA2dpSinkServer::impl * pimpl)64 SystemStateObserver(BluetoothA2dpSinkServer::impl *pimpl) : pimpl_(pimpl) {};
65 ~SystemStateObserver() override = default;
66
OnSystemStateChange(const BTSystemState state)67 void OnSystemStateChange(const BTSystemState state) override
68 {
69 IProfileManager *serviceMgr = IProfileManager::GetInstance();
70 if (!pimpl_) {
71 HILOGI("failed: pimpl_ is null");
72 return;
73 }
74
75 switch (state) {
76 case BTSystemState::ON:
77 if (serviceMgr != nullptr) {
78 pimpl_->a2dpSnkService_ =
79 (IProfileA2dpSnk *)serviceMgr->GetProfileService(PROFILE_NAME_A2DP_SINK);
80 if (pimpl_->a2dpSnkService_ != nullptr) {
81 pimpl_->a2dpSnkService_->RegisterObserver(pimpl_->observerImp_.get());
82 }
83 }
84 break;
85 case BTSystemState::OFF:
86 pimpl_->a2dpSnkService_ = nullptr;
87 break;
88 default:
89 break;
90 }
91 }
92
93 private:
94 BluetoothA2dpSinkServer::impl *pimpl_ = nullptr;
95 };
96
impl()97 BluetoothA2dpSinkServer::impl::impl()
98 {
99 HILOGI("starts");
100 }
101
~impl()102 BluetoothA2dpSinkServer::impl::~impl()
103 {
104 HILOGI("starts");
105 }
106
BluetoothA2dpSinkServer()107 BluetoothA2dpSinkServer::BluetoothA2dpSinkServer()
108 {
109 pimpl = std::make_unique<impl>();
110 pimpl->observerImp_->SetObserver(&(pimpl->observers_));
111 pimpl->systemStateObserver_ = std::make_unique<impl::SystemStateObserver>(pimpl.get());
112 IAdapterManager::GetInstance()->RegisterSystemStateObserver(*(pimpl->systemStateObserver_));
113
114 IProfileManager *serviceMgr = IProfileManager::GetInstance();
115 if (serviceMgr != nullptr) {
116 pimpl->a2dpSnkService_ = (IProfileA2dpSnk *)serviceMgr->GetProfileService(PROFILE_NAME_A2DP_SINK);
117 if (pimpl->a2dpSnkService_ != nullptr) {
118 pimpl->a2dpSnkService_->RegisterObserver(pimpl->observerImp_.get());
119 }
120 }
121 }
122
~BluetoothA2dpSinkServer()123 BluetoothA2dpSinkServer::~BluetoothA2dpSinkServer()
124 {
125 IAdapterManager::GetInstance()->DeregisterSystemStateObserver(*(pimpl->systemStateObserver_));
126 if (pimpl->a2dpSnkService_ != nullptr) {
127 pimpl->a2dpSnkService_->DeregisterObserver(pimpl->observerImp_.get());
128 }
129 }
130
RegisterObserver(const sptr<IBluetoothA2dpSinkObserver> & observer)131 void BluetoothA2dpSinkServer::RegisterObserver(const sptr<IBluetoothA2dpSinkObserver> &observer)
132 {
133 HILOGI("starts");
134 if (observer == nullptr) {
135 HILOGI("observer is null");
136 return;
137 }
138 auto func = std::bind(&BluetoothA2dpSinkServer::DeregisterObserver, this, std::placeholders::_1);
139 pimpl->observers_.Register(observer, func);
140 }
141
DeregisterObserver(const sptr<IBluetoothA2dpSinkObserver> & observer)142 void BluetoothA2dpSinkServer::DeregisterObserver(const sptr<IBluetoothA2dpSinkObserver> &observer)
143 {
144 HILOGI("starts");
145 if (observer == nullptr) {
146 HILOGI("observer is null");
147 return;
148 }
149
150 if (pimpl != nullptr) {
151 pimpl->observers_.Deregister(observer);
152 }
153 }
154
Connect(const RawAddress & device)155 int BluetoothA2dpSinkServer::Connect(const RawAddress &device)
156 {
157 HILOGI("addr: %{public}s", GET_ENCRYPT_ADDR(device));
158 return pimpl->a2dpSnkService_->Connect(device);
159 }
160
Disconnect(const RawAddress & device)161 int BluetoothA2dpSinkServer::Disconnect(const RawAddress &device)
162 {
163 HILOGI("addr: %{public}s", GET_ENCRYPT_ADDR(device));
164 return pimpl->a2dpSnkService_->Disconnect(device);
165 }
166
GetDeviceState(const RawAddress & device)167 int BluetoothA2dpSinkServer::GetDeviceState(const RawAddress &device)
168 {
169 HILOGI("addr: %{public}s", GET_ENCRYPT_ADDR(device));
170 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
171 HILOGE("false, check permission failed");
172 return BT_FAILURE;
173 }
174 return pimpl->a2dpSnkService_->GetDeviceState(device);
175 }
176
GetDevicesByStates(const std::vector<int32_t> & states)177 std::vector<RawAddress> BluetoothA2dpSinkServer::GetDevicesByStates(const std::vector<int32_t> &states)
178 {
179 HILOGI("starts");
180 std::vector<RawAddress> rawDevices;
181 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
182 HILOGE("false, check permission failed");
183 return rawDevices;
184 }
185 std::vector<int> tmpStates;
186 for (int32_t state : states) {
187 HILOGI("state = %{public}d", state);
188 tmpStates.push_back((int)state);
189 }
190
191 rawDevices = pimpl->a2dpSnkService_->GetDevicesByStates(tmpStates);
192 return rawDevices;
193 }
194
GetPlayingState(const RawAddress & device,int & state)195 int BluetoothA2dpSinkServer::GetPlayingState(const RawAddress &device, int &state)
196 {
197 HILOGI("addr: %{public}s", GET_ENCRYPT_ADDR(device));
198 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
199 HILOGE("false, check permission failed");
200 return BT_FAILURE;
201 }
202 return pimpl->a2dpSnkService_->GetPlayingState(device, state);
203 }
204
SetConnectStrategy(const RawAddress & device,int strategy)205 int BluetoothA2dpSinkServer::SetConnectStrategy(const RawAddress &device, int strategy)
206 {
207 HILOGI("addr: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
208 return pimpl->a2dpSnkService_->SetConnectStrategy(device, strategy);
209 }
210
GetConnectStrategy(const RawAddress & device)211 int BluetoothA2dpSinkServer::GetConnectStrategy(const RawAddress &device)
212 {
213 HILOGI("addr: %{public}s", GET_ENCRYPT_ADDR(device));
214 return pimpl->a2dpSnkService_->GetConnectStrategy(device);
215 }
216
SendDelay(const RawAddress & device,int32_t delayValue)217 int BluetoothA2dpSinkServer::SendDelay(const RawAddress &device, int32_t delayValue)
218 {
219 HILOGI("addr: %{public}s, delayValue: %{public}d", GET_ENCRYPT_ADDR(device), delayValue);
220 if (pimpl->a2dpSnkService_ == nullptr) {
221 HILOGE("SendDelay but a2dpSnkService_ is nullptr return");
222 return BT_FAILURE;
223 }
224 return pimpl->a2dpSnkService_->SendDelay(device, (uint16_t)delayValue);
225 }
226 } // namespace Bluetooth
227 } // namespace OHOS