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 #include <mutex>
16 #include "bluetooth_map_mse_observer_stub.h"
17 #include "bluetooth_map_mse_proxy.h"
18 #include "bluetooth_raw_address.h"
19 #include "bluetooth_map_mse.h"
20
21 #include "bt_def.h"
22 #include "bluetooth_host.h"
23 #include "bluetooth_load_system_ability.h"
24 #include "bluetooth_utils.h"
25 #include "bluetooth_remote_device.h"
26 #include "bluetooth_observer_list.h"
27 #include "iservice_registry.h"
28 #include "system_ability_definition.h"
29 #include "i_bluetooth_host.h"
30
31 namespace OHOS {
32 namespace Bluetooth {
33 class MseServiceObserver : public BluetoothMapMseObserverStub {
34 public:
MseServiceObserver(BluetoothObserverList<MapServerObserver> * observers)35 MseServiceObserver(BluetoothObserverList<MapServerObserver> *observers) : observers_(observers)
36 {
37 HILOGI("enter");
38 }
39
40 ~MseServiceObserver() override = default;
41
OnConnectionStateChanged(const BluetoothRawAddress & device,int32_t state)42 void OnConnectionStateChanged(const BluetoothRawAddress &device, int32_t state) override
43 {
44 BluetoothRemoteDevice remoteDevice(device.GetAddress(), bluetooth::BTTransport::ADAPTER_BREDR);
45 observers_->ForEach([remoteDevice, state](std::shared_ptr<MapServerObserver> observer) {
46 observer->OnConnectionStateChanged(remoteDevice, state);
47 });
48 }
49
OnPermission(const BluetoothRawAddress & device)50 void OnPermission(const BluetoothRawAddress &device) override
51 {
52 BluetoothRemoteDevice remoteDevice(device.GetAddress(), bluetooth::BTTransport::ADAPTER_BREDR);
53 observers_->ForEach([remoteDevice](std::shared_ptr<MapServerObserver> observer) {
54 observer->OnPermission(remoteDevice);
55 });
56 }
57
58 private:
59 BluetoothObserverList<MapServerObserver> *observers_;
60 };
61
62 struct MapServer::impl {
63 impl();
~implOHOS::Bluetooth::MapServer::impl64 ~impl()
65 {
66 if (proxy_ != nullptr) {
67 proxy_->DeregisterObserver(serviceObserver_);
68 proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
69 }
70 }
71 bool InitMapServerProxy(void);
72
73 std::mutex mutex_;
74 sptr<IBluetoothMapMse> proxy_;
75 class BluetoothMapMseDeathRecipient;
76 sptr<BluetoothMapMseDeathRecipient> deathRecipient_ = nullptr;
77 BluetoothObserverList<MapServerObserver> observers_;
78 sptr<MseServiceObserver> serviceObserver_ = new MseServiceObserver(&observers_);
79 };
80
81 class MapServer::impl::BluetoothMapMseDeathRecipient final : public IRemoteObject::DeathRecipient {
82 public:
BluetoothMapMseDeathRecipient(MapServer::impl & MapMse)83 explicit BluetoothMapMseDeathRecipient(MapServer::impl &MapMse) : MapMse_(MapMse) {};
84 ~BluetoothMapMseDeathRecipient() final = default;
85 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothMapMseDeathRecipient);
86
OnRemoteDied(const wptr<IRemoteObject> & remote)87 void OnRemoteDied(const wptr<IRemoteObject> &remote) final
88 {
89 HILOGI("starts");
90 if (!MapMse_.proxy_) {
91 return;
92 }
93 MapMse_.proxy_->DeregisterObserver(MapMse_.serviceObserver_);
94 MapMse_.proxy_->AsObject()->RemoveDeathRecipient(MapMse_.deathRecipient_);
95 MapMse_.proxy_ = nullptr;
96 }
97
98 private:
99 MapServer::impl &MapMse_;
100 };
101
impl()102 MapServer::impl::impl()
103 {
104 if (proxy_) {
105 return;
106 }
107 BluetootLoadSystemAbility::GetInstance().RegisterNotifyMsg(PROFILE_ID_MAP_MSE);
108 if (!BluetootLoadSystemAbility::GetInstance().HasSubscribedBluetoothSystemAbility()) {
109 BluetootLoadSystemAbility::GetInstance().SubScribeBluetoothSystemAbility();
110 return;
111 }
112 InitMapServerProxy();
113 }
114
MapServer()115 MapServer::MapServer() : pimpl(nullptr)
116 {
117 HILOGI("excute");
118 pimpl = std::make_unique<impl>();
119 }
120
InitMapServerProxy(void)121 bool MapServer::impl::InitMapServerProxy(void)
122 {
123 if (proxy_) {
124 return true;
125 }
126 proxy_ = GetRemoteProxy<IBluetoothMapMse>(PROFILE_MAP_MSE);
127 if (!proxy_) {
128 HILOGE("get MapServer proxy_ failed");
129 return false;
130 }
131
132 if (serviceObserver_ != nullptr) {
133 proxy_->RegisterObserver(serviceObserver_);
134 }
135
136 deathRecipient_ = new BluetoothMapMseDeathRecipient(*this);
137 if (deathRecipient_ != nullptr) {
138 proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
139 }
140 return true;
141 }
142
~MapServer()143 MapServer::~MapServer()
144 {}
145
GetProfile()146 MapServer *MapServer::GetProfile()
147 {
148 static MapServer instance;
149 return &instance;
150 }
151
Init()152 void MapServer::Init()
153 {
154 if (!pimpl) {
155 HILOGE("fails: no pimpl");
156 return;
157 }
158 if (!pimpl->InitMapServerProxy()) {
159 HILOGE("MapServer proxy_ is nullptr");
160 return;
161 }
162 }
163
RegisterObserver(MapServerObserver & observer)164 void MapServer::RegisterObserver(MapServerObserver &observer)
165 {
166 HILOGI("enter");
167 std::shared_ptr<MapServerObserver> pointer(&observer, [](MapServerObserver *) {});
168 pimpl->observers_.Register(pointer);
169 }
170
DeregisterObserver(MapServerObserver & observer)171 void MapServer::DeregisterObserver(MapServerObserver &observer)
172 {
173 HILOGI("enter");
174 std::shared_ptr<MapServerObserver> pointer(&observer, [](MapServerObserver *) {});
175 pimpl->observers_.Deregister(pointer);
176 }
177
GetState() const178 int MapServer::GetState() const
179 {
180 HILOGI("enter");
181 if (!IS_BT_ENABLED()) {
182 HILOGE("bluetooth is off.");
183 return RET_BAD_STATUS;
184 }
185
186 if (pimpl == nullptr || !pimpl->InitMapServerProxy()) {
187 HILOGE("pimpl or mapServer proxy_ is nullptr");
188 return RET_BAD_STATUS;
189 }
190
191 int32_t ret = RET_NO_ERROR;
192 pimpl->proxy_->GetState(ret);
193 return ret;
194 }
195
Disconnect(const BluetoothRemoteDevice & device)196 bool MapServer::Disconnect(const BluetoothRemoteDevice &device)
197 {
198 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
199 if (!IS_BT_ENABLED()) {
200 HILOGE("bluetooth is off.");
201 return false;
202 }
203
204 if (pimpl == nullptr || !pimpl->InitMapServerProxy()) {
205 HILOGE("pimpl or mapServer proxy_ is nullptr");
206 return false;
207 }
208
209 if (!device.IsValidBluetoothRemoteDevice()) {
210 HILOGE("BluetoothRemoteDevice error");
211 return false;
212 }
213
214 int32_t ret = RET_NO_ERROR;
215 BluetoothRawAddress rawAddress(device.GetDeviceAddr());
216 pimpl->proxy_->Disconnect(rawAddress, ret);
217 return ret == RET_NO_ERROR;
218 }
219
IsConnected(const BluetoothRemoteDevice & device)220 bool MapServer::IsConnected(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 if (pimpl == nullptr || !pimpl->InitMapServerProxy()) {
229 HILOGE("pimpl or mapServer proxy_ is nullptr");
230 return false;
231 }
232
233 if (!device.IsValidBluetoothRemoteDevice()) {
234 HILOGE("BluetoothRemoteDevice error");
235 return false;
236 }
237
238 bool ret = false;
239 BluetoothRawAddress rawAddress(device.GetDeviceAddr());
240 pimpl->proxy_->IsConnected(rawAddress, ret);
241 return ret;
242 }
243
GetConnectedDevices() const244 std::vector<BluetoothRemoteDevice> MapServer::GetConnectedDevices() const
245 {
246 HILOGI("enter");
247 std::vector<BluetoothRemoteDevice> btDeviceList;
248 if (!IS_BT_ENABLED()) {
249 HILOGE("bluetooth is off.");
250 return btDeviceList;
251 }
252
253 if (pimpl == nullptr || !pimpl->InitMapServerProxy()) {
254 HILOGE("pimpl or mapServer proxy_ is nullptr");
255 return btDeviceList;
256 }
257
258 std::vector<BluetoothRawAddress> btDevice;
259 pimpl->proxy_->GetConnectedDevices(btDevice);
260 for (auto it = btDevice.begin(); it != btDevice.end(); it++) {
261 btDeviceList.push_back(BluetoothRemoteDevice(it->GetAddress(), 0));
262 }
263 return btDeviceList;
264 }
265
GetDevicesByStates(std::vector<int> states) const266 std::vector<BluetoothRemoteDevice> MapServer::GetDevicesByStates(std::vector<int> states) const
267 {
268 HILOGI("enter");
269 std::vector<BluetoothRemoteDevice> btDeviceList;
270 if (!IS_BT_ENABLED()) {
271 HILOGE("bluetooth is off.");
272 return btDeviceList;
273 }
274
275 if (pimpl == nullptr || !pimpl->InitMapServerProxy()) {
276 HILOGE("pimpl or mapServer proxy_ is nullptr");
277 return btDeviceList;
278 }
279
280 std::vector<BluetoothRawAddress> btDevice;
281 pimpl->proxy_->GetDevicesByStates(states, btDevice);
282 for (auto it = btDevice.begin(); it != btDevice.end(); it++) {
283 btDeviceList.push_back(BluetoothRemoteDevice(it->GetAddress(), 0));
284 }
285 return btDeviceList;
286 }
287
GetConnectionState(const BluetoothRemoteDevice & device) const288 int MapServer::GetConnectionState(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 static_cast<int>(BTConnectState::DISCONNECTED);
294 }
295
296 if (pimpl == nullptr || !pimpl->InitMapServerProxy()) {
297 HILOGE("pimpl or mapServer proxy_ is nullptr");
298 return static_cast<int>(BTConnectState::DISCONNECTED);
299 }
300
301 if (!device.IsValidBluetoothRemoteDevice()) {
302 HILOGE("BluetoothRemoteDevice error");
303 return static_cast<int>(BTConnectState::DISCONNECTED);
304 }
305
306 int32_t ret = RET_NO_ERROR;
307 BluetoothRawAddress rawAddress(device.GetDeviceAddr());
308 pimpl->proxy_->GetConnectionState(rawAddress, ret);
309 return ret;
310 }
311
SetConnectionStrategy(const BluetoothRemoteDevice & device,int strategy)312 bool MapServer::SetConnectionStrategy(const BluetoothRemoteDevice &device, int strategy)
313 {
314 HILOGI("enter, device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
315 if (!IS_BT_ENABLED()) {
316 HILOGE("bluetooth is off.");
317 return false;
318 }
319
320 if (pimpl == nullptr || !pimpl->InitMapServerProxy()) {
321 HILOGE("pimpl or mapServer proxy_ is nullptr");
322 return false;
323 }
324
325 if (!device.IsValidBluetoothRemoteDevice()) {
326 HILOGE("BluetoothRemoteDevice error");
327 return false;
328 }
329
330 bool ret = false;
331 BluetoothRawAddress rawAddress(device.GetDeviceAddr());
332 pimpl->proxy_->SetConnectionStrategy(rawAddress, strategy, ret);
333 return ret;
334 }
335
GetConnectionStrategy(const BluetoothRemoteDevice & device) const336 int MapServer::GetConnectionStrategy(const BluetoothRemoteDevice &device) const
337 {
338 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
339 if (!IS_BT_ENABLED()) {
340 HILOGE("bluetooth is off.");
341 return static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN);
342 }
343
344 if (pimpl == nullptr || !pimpl->InitMapServerProxy()) {
345 HILOGE("pimpl or mapServer proxy_ is nullptr");
346 return static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN);
347 }
348
349 if (!device.IsValidBluetoothRemoteDevice()) {
350 HILOGE("BluetoothRemoteDevice error");
351 return static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN);
352 }
353
354 int32_t ret = static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN);
355 BluetoothRawAddress rawAddress(device.GetDeviceAddr());
356 pimpl->proxy_->GetConnectionStrategy(rawAddress, ret);
357 return ret;
358 }
359
GrantPermission(const BluetoothRemoteDevice & device,bool allow,bool save)360 void MapServer::GrantPermission(const BluetoothRemoteDevice &device, bool allow, bool save)
361 {
362 HILOGI("enter, device: %{public}s, allow: %{public}d, save: %{public}d", GET_ENCRYPT_ADDR(device), allow, save);
363 if (!IS_BT_ENABLED()) {
364 HILOGE("bluetooth is off.");
365 return;
366 }
367
368 if (pimpl == nullptr || !pimpl->InitMapServerProxy()) {
369 HILOGE("pimpl or mapServer proxy_ is nullptr");
370 return;
371 }
372
373 if (!device.IsValidBluetoothRemoteDevice()) {
374 HILOGE("BluetoothRemoteDevice error");
375 return;
376 }
377
378 BluetoothRawAddress rawAddress(device.GetDeviceAddr());
379 pimpl->proxy_->GrantPermission(rawAddress, allow, save);
380 }
381 } // namespace Bluetooth
382 } // namespace OHOS