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