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