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 "bluetooth_map_mse_server.h"
16 #include "interface_adapter_manager.h"
17 #include "interface_profile_manager.h"
18 #include "interface_profile_map_mse.h"
19 #include "bluetooth_log.h"
20 #include "bluetooth_utils_server.h"
21 #include "remote_observer_list.h"
22 #include "permission_utils.h"
23
24 using namespace OHOS::bluetooth;
25
26 namespace OHOS {
27 namespace Bluetooth {
28 class BluetoothMapMseObserverImpl : public IMapMseObserver {
29 public:
BluetoothMapMseObserverImpl(RemoteObserverList<IBluetoothMapMseObserver> * observers)30 BluetoothMapMseObserverImpl(RemoteObserverList<IBluetoothMapMseObserver> *observers) : observers_(observers)
31 {
32 HILOGI("enter");
33 }
34
35 ~BluetoothMapMseObserverImpl() override = default;
36
OnConnectionStateChanged(const RawAddress & remoteAddr,int state)37 void OnConnectionStateChanged(const RawAddress &remoteAddr, int state) override
38 {
39 HILOGI("addr: %{public}s, state: %{public}d", GetEncryptAddr(remoteAddr.GetAddress()).c_str(), state);
40 observers_->ForEach([remoteAddr, state](IBluetoothMapMseObserver *observer) {
41 observer->OnConnectionStateChanged(remoteAddr, state);
42 });
43 }
44
OnPermission(const RawAddress & remoteAddr)45 void OnPermission(const RawAddress &remoteAddr) override
46 {
47 HILOGI("addr: %{public}s", GetEncryptAddr(remoteAddr.GetAddress()).c_str());
48 observers_->ForEach([remoteAddr](IBluetoothMapMseObserver *observer) { observer->OnPermission(remoteAddr); });
49 }
50
51 private:
52 RemoteObserverList<IBluetoothMapMseObserver> *observers_;
53 };
54
55 struct BluetoothMapMseServer::impl {
56 impl();
57 RemoteObserverList<IBluetoothMapMseObserver> observers_;
58 std::unique_ptr<BluetoothMapMseObserverImpl> observerImp_ = nullptr;
59 IProfileMapMse *mapMseService_ = nullptr;
60 class MapMseSystemStateObserver;
61 std::unique_ptr<MapMseSystemStateObserver> systemStateObserver_ = nullptr;
62 };
63
64 class BluetoothMapMseServer::impl::MapMseSystemStateObserver : public ISystemStateObserver {
65 public:
MapMseSystemStateObserver(BluetoothMapMseServer::impl * pimpl)66 MapMseSystemStateObserver(BluetoothMapMseServer::impl *pimpl) : pimpl_(pimpl) {};
67 ~MapMseSystemStateObserver() override = default;
OnSystemStateChange(const BTSystemState state)68 void OnSystemStateChange(const BTSystemState state) override
69 {
70 HILOGI("enter");
71 switch (state) {
72 case BTSystemState::ON: {
73 IProfileManager *serviceMgr = IProfileManager::GetInstance();
74 if (serviceMgr != nullptr) {
75 pimpl_->mapMseService_ = (IProfileMapMse *)serviceMgr->GetProfileService(PROFILE_NAME_MAP_MSE);
76 if (pimpl_->mapMseService_ != nullptr) {
77 pimpl_->mapMseService_->RegisterObserver(*(pimpl_->observerImp_));
78 }
79 }
80 } break;
81 case BTSystemState::OFF:
82 pimpl_->mapMseService_ = nullptr;
83 break;
84 default:
85 break;
86 }
87 }
88
89 private:
90 BluetoothMapMseServer::impl *pimpl_;
91 };
92
impl()93 BluetoothMapMseServer::impl::impl()
94 {
95 systemStateObserver_ = std::make_unique<impl::MapMseSystemStateObserver>(this);
96 IAdapterManager::GetInstance()->RegisterSystemStateObserver(*systemStateObserver_);
97 }
98
BluetoothMapMseServer()99 BluetoothMapMseServer::BluetoothMapMseServer()
100 {
101 HILOGI("enter");
102 pimpl = std::make_unique<impl>();
103
104 IProfileManager *serviceMgr = IProfileManager::GetInstance();
105 if (serviceMgr != nullptr) {
106 pimpl->mapMseService_ = (IProfileMapMse *)serviceMgr->GetProfileService(PROFILE_NAME_MAP_MSE);
107 if (pimpl->mapMseService_ != nullptr) {
108 pimpl->observerImp_ = std::make_unique<BluetoothMapMseObserverImpl>(&(pimpl->observers_));
109 pimpl->mapMseService_->RegisterObserver(*(pimpl->observerImp_));
110 }
111 }
112 }
~BluetoothMapMseServer()113 BluetoothMapMseServer::~BluetoothMapMseServer()
114 {}
115
116 // IBluetoothMapMse overrides:
RegisterObserver(const sptr<IBluetoothMapMseObserver> & observer)117 void BluetoothMapMseServer::RegisterObserver(
118 const sptr<IBluetoothMapMseObserver> &observer)
119 {
120 HILOGI("enter");
121 if (!observer) {
122 HILOGE("RegisterObserver called with NULL . Ignoring.");
123 }
124 pimpl->observers_.Register(observer);
125 }
126
DeregisterObserver(const sptr<IBluetoothMapMseObserver> & observer)127 void BluetoothMapMseServer::DeregisterObserver(
128 const sptr<IBluetoothMapMseObserver> &observer)
129 {
130 HILOGI("enter");
131 if (!observer) {
132 HILOGE("UnregisterObserver called with NULL . Ignoring.");
133 }
134 pimpl->observers_.Deregister(observer);
135 }
136
GetState(int32_t & ret)137 void BluetoothMapMseServer::GetState(int32_t &ret)
138 {
139 HILOGI("ret: %{public}d", ret);
140 ret = -1;
141 if (pimpl->mapMseService_ != nullptr) {
142 ret = pimpl->mapMseService_->GetState();
143 }
144 }
145
Disconnect(const BluetoothRawAddress & device,int32_t & ret)146 void BluetoothMapMseServer::Disconnect(
147 const BluetoothRawAddress &device, int32_t &ret)
148 {
149 HILOGI("device: %{public}s, ret: %{public}d", GetEncryptAddr(device.GetAddress()).c_str(), ret);
150 ret = -1;
151 std::string addString = device.GetAddress();
152 bluetooth::RawAddress addr(addString);
153 if (pimpl->mapMseService_ != nullptr) {
154 ret = pimpl->mapMseService_->Disconnect(addr);
155 }
156 }
157
IsConnected(const BluetoothRawAddress & device,bool & ret)158 void BluetoothMapMseServer::IsConnected(
159 const BluetoothRawAddress &device, bool &ret)
160 {
161 HILOGI("device: %{public}s, ret: %{public}d", GetEncryptAddr(device.GetAddress()).c_str(), ret);
162 ret = false;
163 std::string addString = device.GetAddress();
164 bluetooth::RawAddress addr(addString);
165 if (pimpl->mapMseService_ != nullptr) {
166 ret = pimpl->mapMseService_->IsConnected(addr);
167 }
168 }
169
GetConnectedDevices(std::vector<BluetoothRawAddress> & devices)170 void BluetoothMapMseServer::GetConnectedDevices(
171 std::vector<BluetoothRawAddress> &devices)
172 {
173 HILOGI("enter");
174 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
175 HILOGE("false, check permission failed");
176 return;
177 }
178 if (pimpl->mapMseService_ != nullptr) {
179 for (auto &device : pimpl->mapMseService_->GetConnectDevices()) {
180 devices.push_back(device);
181 }
182 }
183 }
184
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRawAddress> & devices)185 void BluetoothMapMseServer::GetDevicesByStates(
186 const std::vector<int32_t> &states, std::vector<BluetoothRawAddress> &devices)
187 {
188 HILOGI("enter");
189 if (pimpl->mapMseService_ != nullptr) {
190 for (auto &device : pimpl->mapMseService_->GetDevicesByStates(states)) {
191 devices.push_back(device);
192 }
193 }
194 }
195
GetConnectionState(const BluetoothRawAddress & device,int32_t & ret)196 void BluetoothMapMseServer::GetConnectionState(
197 const BluetoothRawAddress &device, int32_t &ret)
198 {
199 HILOGI("device: %{public}s, ret: %{public}d", GetEncryptAddr(device.GetAddress()).c_str(), ret);
200 ret = -1;
201 std::string addString = device.GetAddress();
202 bluetooth::RawAddress addr(addString);
203 if (pimpl->mapMseService_ != nullptr) {
204 ret = pimpl->mapMseService_->GetConnectionState(addr);
205 }
206 }
207
SetConnectionStrategy(const BluetoothRawAddress & device,int32_t strategy,bool & ret)208 void BluetoothMapMseServer::SetConnectionStrategy(
209 const BluetoothRawAddress &device, int32_t strategy, bool &ret)
210 {
211 HILOGI("device: %{public}s, strategy: %{public}d, ret: %{public}d",
212 GetEncryptAddr(device.GetAddress()).c_str(), strategy, ret);
213 ret = false;
214 std::string addString = device.GetAddress();
215 bluetooth::RawAddress addr(addString);
216 if (pimpl->mapMseService_ != nullptr) {
217 ret = pimpl->mapMseService_->SetConnectionStrategy(addr, strategy);
218 }
219 }
220
GetConnectionStrategy(const BluetoothRawAddress & device,int32_t & ret)221 void BluetoothMapMseServer::GetConnectionStrategy(
222 const BluetoothRawAddress &device, int32_t &ret)
223 {
224 HILOGI("device: %{public}s, ret: %{public}d", GetEncryptAddr(device.GetAddress()).c_str(), ret);
225 ret = -1;
226 std::string addString = device.GetAddress();
227 bluetooth::RawAddress addr(addString);
228 if (pimpl->mapMseService_ != nullptr) {
229 ret = pimpl->mapMseService_->GetConnectionStrategy(addr);
230 }
231 }
232
GrantPermission(const BluetoothRawAddress & device,bool allow,bool save)233 void BluetoothMapMseServer::GrantPermission(
234 const BluetoothRawAddress &device, bool allow, bool save)
235 {
236 HILOGI("device: %{public}s allow: %{public}d save: %{public}d",
237 GetEncryptAddr(device.GetAddress()).c_str(), allow, save);
238 std::string addString = device.GetAddress();
239 bluetooth::RawAddress addr(addString);
240 if (pimpl->mapMseService_ != nullptr) {
241 pimpl->mapMseService_->GrantPermission(addr, allow, save);
242 }
243 }
244 } // namespace Bluetooth
245 } // namespace OHOS
246