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_remote_device.h"
24 #include "bluetooth_observer_list.h"
25 #include "iservice_registry.h"
26 #include "system_ability_definition.h"
27 #include "i_bluetooth_host.h"
28
29 namespace OHOS {
30 namespace Bluetooth {
31 class MseServiceObserver : public BluetoothMapMseObserverStub {
32 public:
MseServiceObserver(BluetoothObserverList<MapServerObserver> * observers)33 MseServiceObserver(BluetoothObserverList<MapServerObserver> *observers) : observers_(observers)
34 {
35 HILOGI("%{public}s", __func__);
36 }
37
38 ~MseServiceObserver() override = default;
39
OnConnectionStateChanged(const BluetoothRawAddress & device,int32_t state)40 void OnConnectionStateChanged(const BluetoothRawAddress &device, int32_t state) override
41 {
42 BluetoothRemoteDevice remoteDevice(device.GetAddress(), bluetooth::BTTransport::ADAPTER_BREDR);
43 observers_->ForEach([remoteDevice, state](std::shared_ptr<MapServerObserver> observer) {
44 observer->OnConnectionStateChanged(remoteDevice, state);
45 });
46 }
47
OnPermission(const BluetoothRawAddress & device)48 void OnPermission(const BluetoothRawAddress &device) override
49 {
50 BluetoothRemoteDevice remoteDevice(device.GetAddress(), bluetooth::BTTransport::ADAPTER_BREDR);
51 observers_->ForEach([remoteDevice](std::shared_ptr<MapServerObserver> observer) {
52 observer->OnPermission(remoteDevice);
53 });
54 }
55
56 private:
57 BluetoothObserverList<MapServerObserver> *observers_;
58 };
59
60 struct MapServer::impl {
61 impl();
~implOHOS::Bluetooth::MapServer::impl62 ~impl()
63 {
64 if (proxy_ != nullptr) {
65 proxy_->DeregisterObserver(serviceObserver_);
66 proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
67 }
68 }
69
70 std::mutex mutex_;
71 sptr<IBluetoothMapMse> proxy_;
72 class BluetoothMapMseDeathRecipient;
73 sptr<BluetoothMapMseDeathRecipient> deathRecipient_ = nullptr;
74 BluetoothObserverList<MapServerObserver> observers_;
75 sptr<MseServiceObserver> serviceObserver_ = new MseServiceObserver(&observers_);
76 };
77
78 class MapServer::impl::BluetoothMapMseDeathRecipient final : public IRemoteObject::DeathRecipient {
79 public:
BluetoothMapMseDeathRecipient(MapServer::impl & MapMse)80 BluetoothMapMseDeathRecipient(MapServer::impl &MapMse) : MapMse_(MapMse) {};
81 ~BluetoothMapMseDeathRecipient() final = default;
82 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothMapMseDeathRecipient);
83
OnRemoteDied(const wptr<IRemoteObject> & remote)84 void OnRemoteDied(const wptr<IRemoteObject> &remote) final
85 {
86 HILOGI("MapServer::impl::BluetoothMapMseDeathRecipient::OnRemoteDied starts");
87 MapMse_.proxy_->AsObject()->RemoveDeathRecipient(MapMse_.deathRecipient_);
88 MapMse_.proxy_ = nullptr;
89 }
90
91 private:
92 MapServer::impl &MapMse_;
93 };
94
impl()95 MapServer::impl::impl()
96 {
97 HILOGI("MapServer::impl::impl starts");
98 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
99 sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
100
101 if (!hostRemote) {
102 HILOGE("MapServer::impl:impl() failed: no hostRemote");
103 return;
104 }
105 sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
106 sptr<IRemoteObject> remote = hostProxy->GetProfile(PROFILE_MAP_MSE);
107
108 if (!remote) {
109 HILOGE("MapServer::impl:impl() failed: no remote");
110 return;
111 }
112 HILOGI("MapServer::impl:impl() remote obtained");
113
114 proxy_ = iface_cast<IBluetoothMapMse>(remote);
115
116 deathRecipient_ = new BluetoothMapMseDeathRecipient(*this);
117 proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
118 if (proxy_ != nullptr) {
119 proxy_->RegisterObserver(serviceObserver_);
120 }
121 }
122
MapServer()123 MapServer::MapServer() : pimpl(nullptr)
124 {
125 HILOGI("%{public}s :excute", __func__);
126 pimpl = std::make_unique<impl>();
127 }
128
~MapServer()129 MapServer::~MapServer()
130 {}
131
GetProfile()132 MapServer *MapServer::GetProfile()
133 {
134 static MapServer instance;
135 return &instance;
136 }
137
RegisterObserver(MapServerObserver & observer)138 void MapServer::RegisterObserver(MapServerObserver &observer)
139 {
140 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
141 std::shared_ptr<MapServerObserver> pointer(&observer, [](MapServerObserver *) {});
142 pimpl->observers_.Register(pointer);
143 }
144
DeregisterObserver(MapServerObserver & observer)145 void MapServer::DeregisterObserver(MapServerObserver &observer)
146 {
147 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
148 std::shared_ptr<MapServerObserver> pointer(&observer, [](MapServerObserver *) {});
149 pimpl->observers_.Deregister(pointer);
150 }
151
GetState() const152 int MapServer::GetState() const
153 {
154 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
155 int32_t ret = RET_NO_ERROR;
156 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
157 pimpl->proxy_->GetState(ret);
158 }
159 return ret;
160 }
161
Disconnect(const BluetoothRemoteDevice & device)162 bool MapServer::Disconnect(const BluetoothRemoteDevice &device)
163 {
164 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
165 if (!device.IsValidBluetoothRemoteDevice()) {
166 HILOGE("%{public}s :BluetoothRemoteDevice erro", __func__);
167 return false;
168 }
169 int32_t ret = RET_NO_ERROR;
170 BluetoothRawAddress rawAddress(device.GetDeviceAddr());
171 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
172 pimpl->proxy_->Disconnect(rawAddress, ret);
173 }
174 return ret == RET_NO_ERROR;
175 }
176
IsConnected(const BluetoothRemoteDevice & device)177 bool MapServer::IsConnected(const BluetoothRemoteDevice &device)
178 {
179 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
180 if (!device.IsValidBluetoothRemoteDevice()) {
181 HILOGE("%{public}s :BluetoothRemoteDevice erro", __func__);
182 return false;
183 }
184 bool ret = false;
185 BluetoothRawAddress rawAddress(device.GetDeviceAddr());
186 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
187 pimpl->proxy_->IsConnected(rawAddress, ret);
188 }
189 return ret;
190 }
191
GetConnectedDevices() const192 std::vector<BluetoothRemoteDevice> MapServer::GetConnectedDevices() const
193 {
194 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
195 std::vector<BluetoothRemoteDevice> btDeviceList;
196 std::vector<BluetoothRawAddress> btDevice;
197 if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
198 pimpl->proxy_->GetConnectedDevices(btDevice);
199 for (auto it = btDevice.begin(); it != btDevice.end(); it++) {
200 btDeviceList.push_back(BluetoothRemoteDevice(it->GetAddress(), 0));
201 }
202 }
203 return btDeviceList;
204 }
205
GetDevicesByStates(std::vector<int> states) const206 std::vector<BluetoothRemoteDevice> MapServer::GetDevicesByStates(std::vector<int> states) const
207 {
208 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
209 std::vector<BluetoothRemoteDevice> btDeviceList;
210 std::vector<BluetoothRawAddress> btDevice;
211 if ((pimpl->proxy_ != nullptr) && IS_BT_ENABLED()) {
212 pimpl->proxy_->GetDevicesByStates(states, btDevice);
213 for (auto it = btDevice.begin(); it != btDevice.end(); it++) {
214 btDeviceList.push_back(BluetoothRemoteDevice(it->GetAddress(), 0));
215 }
216 }
217 return btDeviceList;
218 }
219
GetConnectionState(const BluetoothRemoteDevice & device) const220 int MapServer::GetConnectionState(const BluetoothRemoteDevice &device) const
221 {
222 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
223 if (!device.IsValidBluetoothRemoteDevice()) {
224 HILOGE("%{public}s :BluetoothRemoteDevice erro", __func__);
225 return (int)BTConnectState::DISCONNECTED;
226 }
227 int32_t ret = RET_NO_ERROR;
228 BluetoothRawAddress rawAddress(device.GetDeviceAddr());
229 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
230 pimpl->proxy_->GetConnectionState(rawAddress, ret);
231 }
232 return ret;
233 }
234
SetConnectionStrategy(const BluetoothRemoteDevice & device,int strategy)235 bool MapServer::SetConnectionStrategy(const BluetoothRemoteDevice &device, int strategy)
236 {
237 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
238 if (!device.IsValidBluetoothRemoteDevice()) {
239 HILOGE("%{public}s :BluetoothRemoteDevice erro", __func__);
240 return false;
241 }
242 bool ret = false;
243 BluetoothRawAddress rawAddress(device.GetDeviceAddr());
244 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
245 pimpl->proxy_->SetConnectionStrategy(rawAddress, strategy, ret);
246 }
247 return ret;
248 }
249
GetConnectionStrategy(const BluetoothRemoteDevice & device) const250 int MapServer::GetConnectionStrategy(const BluetoothRemoteDevice &device) const
251 {
252 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
253 if (!device.IsValidBluetoothRemoteDevice()) {
254 HILOGE("%{public}s :BluetoothRemoteDevice erro", __func__);
255 return (int)BTStrategyType::CONNECTION_FORBIDDEN;
256 }
257 int32_t ret = (int)BTStrategyType::CONNECTION_FORBIDDEN;
258 BluetoothRawAddress rawAddress(device.GetDeviceAddr());
259 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
260 pimpl->proxy_->GetConnectionStrategy(rawAddress, ret);
261 }
262 return ret;
263 }
264
GrantPermission(const BluetoothRemoteDevice & device,bool allow,bool save)265 void MapServer::GrantPermission(const BluetoothRemoteDevice &device, bool allow, bool save)
266 {
267 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
268 if (!device.IsValidBluetoothRemoteDevice()) {
269 HILOGE("%{public}s :BluetoothRemoteDevice erro", __func__);
270 return;
271 }
272 BluetoothRawAddress rawAddress(device.GetDeviceAddr());
273 if (pimpl->proxy_ != nullptr && IS_BT_ENABLED()) {
274 pimpl->proxy_->GrantPermission(rawAddress, allow, save);
275 }
276 }
277 } // namespace Bluetooth
278 } // namespace OHOS