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_errorcode.h"
18 #include "bluetooth_log.h"
19 #include "bluetooth_utils_server.h"
20 #include "interface_profile.h"
21 #include "interface_profile_hid_host.h"
22 #include "i_bluetooth_host_observer.h"
23 #include "permission_utils.h"
24 #include "remote_observer_list.h"
25 #include "hilog/log.h"
26 #include "bluetooth_hid_host_server.h"
27
28 namespace OHOS {
29 namespace Bluetooth {
30 class BluetoothHidHostCallback : public bluetooth::IHidHostObserver {
31 public:
32 BluetoothHidHostCallback() = default;
33 ~BluetoothHidHostCallback() override = default;
34
OnConnectionStateChanged(const RawAddress & device,int state)35 void OnConnectionStateChanged(const RawAddress &device, int state) override
36 {
37 // Reference "BTConnectState"
38 HILOGI("addr:%{public}s, state:%{public}d", GET_ENCRYPT_ADDR(device), state);
39 observers_->ForEach([device, state](sptr<IBluetoothHidHostObserver> observer) {
40 observer->OnConnectionStateChanged(device, state);
41 });
42 }
43
SetObserver(RemoteObserverList<IBluetoothHidHostObserver> * observers)44 void SetObserver(RemoteObserverList<IBluetoothHidHostObserver> *observers)
45 {
46 observers_ = observers;
47 }
48
49 private:
50 RemoteObserverList<IBluetoothHidHostObserver> *observers_;
51 };
52
53 struct BluetoothHidHostServer::impl {
54 impl();
55 ~impl();
56
57 class SystemStateObserver;
58 std::unique_ptr<SystemStateObserver> systemStateObserver_ = nullptr;
59
60 RemoteObserverList<IBluetoothHidHostObserver> observers_;
61 std::unique_ptr<BluetoothHidHostCallback> observerImp_ = std::make_unique<BluetoothHidHostCallback>();
62 IProfileHidHost *hidHostService_ = nullptr;
63 std::vector<sptr<IBluetoothHidHostObserver>> advCallBack_;
64
GetServicePtrOHOS::Bluetooth::BluetoothHidHostServer::impl65 IProfileHidHost *GetServicePtr()
66 {
67 if (IProfileManager::GetInstance() == nullptr) {
68 return nullptr;
69 }
70 return static_cast<IProfileHidHost *>(
71 IProfileManager::GetInstance()->GetProfileService(PROFILE_NAME_HID_HOST));
72 }
73 };
74
75 class BluetoothHidHostServer::impl::SystemStateObserver : public ISystemStateObserver {
76 public:
SystemStateObserver(BluetoothHidHostServer::impl * pimpl)77 explicit SystemStateObserver(BluetoothHidHostServer::impl *pimpl) : pimpl_(pimpl) {};
OnSystemStateChange(const BTSystemState state)78 void OnSystemStateChange(const BTSystemState state) override
79 {
80 HILOGI("start, BTSystemState:%{public}d", state);
81 switch (state) {
82 case BTSystemState::ON:
83 pimpl_->hidHostService_ = pimpl_->GetServicePtr();
84 if (pimpl_->hidHostService_ != nullptr) {
85 pimpl_->hidHostService_->RegisterObserver(*pimpl_->observerImp_.get());
86 }
87 break;
88 case BTSystemState::OFF:
89 pimpl_->hidHostService_ = nullptr;
90 break;
91 default:
92 break;
93 }
94 };
95
96 private:
97 BluetoothHidHostServer::impl *pimpl_ = nullptr;
98 };
99
impl()100 BluetoothHidHostServer::impl::impl()
101 {
102 HILOGI("enter");
103 }
104
~impl()105 BluetoothHidHostServer::impl::~impl()
106 {
107 HILOGI("enter");
108 }
109
BluetoothHidHostServer()110 BluetoothHidHostServer::BluetoothHidHostServer()
111 {
112 HILOGI("start");
113 pimpl = std::make_unique<impl>();
114 pimpl->observerImp_->SetObserver(&(pimpl->observers_));
115 pimpl->systemStateObserver_ = std::make_unique<impl::SystemStateObserver>(pimpl.get());
116 IAdapterManager::GetInstance()->RegisterSystemStateObserver(*(pimpl->systemStateObserver_));
117
118 pimpl->hidHostService_ = pimpl->GetServicePtr();
119 if (pimpl->hidHostService_ != nullptr) {
120 pimpl->hidHostService_->RegisterObserver(*pimpl->observerImp_.get());
121 }
122 }
123
~BluetoothHidHostServer()124 BluetoothHidHostServer::~BluetoothHidHostServer()
125 {
126 HILOGI("start");
127 IAdapterManager::GetInstance()->DeregisterSystemStateObserver(*(pimpl->systemStateObserver_));
128 if (pimpl->hidHostService_ != nullptr) {
129 pimpl->hidHostService_->DeregisterObserver(*pimpl->observerImp_.get());
130 }
131 }
132
RegisterObserver(const sptr<IBluetoothHidHostObserver> observer)133 ErrCode BluetoothHidHostServer::RegisterObserver(const sptr<IBluetoothHidHostObserver> observer)
134 {
135 HILOGI("start");
136
137 if (observer == nullptr) {
138 HILOGE("observer is null");
139 return ERR_INVALID_VALUE;
140 }
141 if (pimpl == nullptr) {
142 HILOGE("pimpl is null");
143 return ERR_NO_INIT;
144 }
145 auto func = std::bind(&BluetoothHidHostServer::DeregisterObserver, this, std::placeholders::_1);
146 pimpl->observers_.Register(observer, func);
147 pimpl->advCallBack_.push_back(observer);
148 return ERR_OK;
149 }
150
DeregisterObserver(const sptr<IBluetoothHidHostObserver> observer)151 ErrCode BluetoothHidHostServer::DeregisterObserver(const sptr<IBluetoothHidHostObserver> observer)
152 {
153 HILOGI("start");
154 if (observer == nullptr) {
155 HILOGE("observer is null");
156 return ERR_INVALID_VALUE;
157 }
158 if (pimpl == nullptr) {
159 HILOGE("pimpl is null");
160 return ERR_NO_INIT;
161 }
162 for (auto iter = pimpl->advCallBack_.begin(); iter != pimpl->advCallBack_.end(); ++iter) {
163 if ((*iter)->AsObject() == observer->AsObject()) {
164 if (pimpl != nullptr) {
165 pimpl->observers_.Deregister(*iter);
166 pimpl->advCallBack_.erase(iter);
167 break;
168 }
169 }
170 }
171 pimpl->hidHostService_->DeregisterObserver(*pimpl->observerImp_.get());
172 return ERR_OK;
173 }
174
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRawAddress> & result)175 int32_t BluetoothHidHostServer::GetDevicesByStates(
176 const std::vector<int32_t> &states, std::vector<BluetoothRawAddress>& result)
177 {
178 HILOGI("start");
179 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
180 HILOGE("check permission failed");
181 return BT_ERR_PERMISSION_FAILED;
182 }
183 if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
184 HILOGI("hidHostService_ is null");
185 return BT_ERR_INTERNAL_ERROR;
186 }
187
188 std::vector<bluetooth::RawAddress> serviceDeviceList = pimpl->hidHostService_->GetDevicesByStates(states);
189 for (auto &device : serviceDeviceList) {
190 BluetoothRawAddress bluetoothDevice(device.GetAddress());
191 result.push_back(bluetoothDevice);
192 HILOGI("%{public}s", GET_ENCRYPT_ADDR(bluetoothDevice));
193 }
194 return NO_ERROR;
195 }
196
GetDeviceState(const BluetoothRawAddress & device,int32_t & state)197 int32_t BluetoothHidHostServer::GetDeviceState(const BluetoothRawAddress &device, int32_t &state)
198 {
199 HILOGI("start, addr:%{public}s", GET_ENCRYPT_ADDR(device));
200 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
201 HILOGE("check permission failed");
202 return BT_ERR_PERMISSION_FAILED;
203 }
204 if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
205 HILOGI("hidHostService_ is null");
206 return BT_ERR_INTERNAL_ERROR;
207 }
208 state = pimpl->hidHostService_->GetDeviceState(device);
209 HILOGI("end, result:%{public}d", state);
210 return NO_ERROR;
211 }
212
Connect(const BluetoothRawAddress & device)213 int32_t BluetoothHidHostServer::Connect(const BluetoothRawAddress &device)
214 {
215 HILOGI("start, addr:%{public}s", GET_ENCRYPT_ADDR(device));
216 if (!PermissionUtils::CheckSystemHapApp()) {
217 HILOGE("check system api failed.");
218 return BT_ERR_SYSTEM_PERMISSION_FAILED;
219 }
220 if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
221 HILOGE("check permission failed");
222 return BT_ERR_PERMISSION_FAILED;
223 }
224 if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
225 HILOGE("hidHostService_ is null");
226 return BT_ERR_INTERNAL_ERROR;
227 }
228 return pimpl->hidHostService_->Connect(device);
229 }
230
Disconnect(const BluetoothRawAddress & device)231 int32_t BluetoothHidHostServer::Disconnect(const BluetoothRawAddress &device)
232 {
233 HILOGI("start, addr:%{public}s", GET_ENCRYPT_ADDR(device));
234 if (!PermissionUtils::CheckSystemHapApp()) {
235 HILOGE("check system api failed.");
236 return BT_ERR_SYSTEM_PERMISSION_FAILED;
237 }
238 if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
239 HILOGE("check permission failed");
240 return BT_ERR_PERMISSION_FAILED;
241 }
242 if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
243 HILOGI("hidHostService_ is null");
244 return BT_ERR_INTERNAL_ERROR;
245 }
246 return pimpl->hidHostService_->Disconnect(device);
247 }
248
HidHostVCUnplug(std::string & device,uint8_t & id,uint16_t & size,uint8_t & type,int & result)249 ErrCode BluetoothHidHostServer::HidHostVCUnplug(std::string &device,
250 uint8_t &id, uint16_t &size, uint8_t &type, int& result)
251 {
252 HILOGI("start");
253 if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
254 HILOGE("check permission failed");
255 return ERR_PERMISSION_DENIED;
256 }
257 if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
258 HILOGI("hidHostService_ is null");
259 return ERR_NO_INIT;
260 }
261 result = pimpl->hidHostService_->HidHostVCUnplug(device, id, size, type);
262 HILOGI("end, result:%{public}d", result);
263 return ERR_OK;
264 }
265
HidHostSendData(std::string & device,uint8_t & id,uint16_t & size,uint8_t & type,int & result)266 ErrCode BluetoothHidHostServer::HidHostSendData(std::string &device,
267 uint8_t &id, uint16_t &size, uint8_t &type, int& result)
268 {
269 HILOGI("start");
270 if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
271 HILOGE("check permission failed");
272 return ERR_PERMISSION_DENIED;
273 }
274 if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
275 HILOGI("hidHostService_ is null");
276 return ERR_NO_INIT;
277 }
278 result = pimpl->hidHostService_->HidHostSendData(device, id, size, type);
279 HILOGI("end, result:%{public}d", result);
280 return ERR_OK;
281 }
282
HidHostSetReport(std::string & device,uint8_t & type,uint16_t & size,uint8_t & report,int & result)283 ErrCode BluetoothHidHostServer::HidHostSetReport(std::string &device,
284 uint8_t &type, uint16_t &size, uint8_t &report, int& result)
285 {
286 HILOGI("start");
287 if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
288 HILOGE("check permission failed");
289 return ERR_PERMISSION_DENIED;
290 }
291 if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
292 HILOGI("hidHostService_ is null");
293 return ERR_NO_INIT;
294 }
295 result = pimpl->hidHostService_->HidHostSetReport(device, type, size, &report);
296 HILOGI("end, result:%{public}d", result);
297 return ERR_OK;
298 }
299
HidHostGetReport(std::string & device,uint8_t & id,uint16_t & size,uint8_t & type,int & result)300 ErrCode BluetoothHidHostServer::HidHostGetReport(std::string &device,
301 uint8_t &id, uint16_t &size, uint8_t &type, int& result)
302 {
303 HILOGI("start");
304 if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
305 HILOGE("check permission failed");
306 return ERR_PERMISSION_DENIED;
307 }
308 if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
309 HILOGI("hidHostService_ is null");
310 return ERR_NO_INIT;
311 }
312 result = pimpl->hidHostService_->HidHostGetReport(device, id, size, type);
313 HILOGI("end, result:%{public}d", result);
314 return ERR_OK;
315 }
316
SetConnectStrategy(const BluetoothRawAddress & device,int strategy)317 int32_t BluetoothHidHostServer::SetConnectStrategy(const BluetoothRawAddress &device, int strategy)
318 {
319 HILOGI("target device:%{public}s()", GET_ENCRYPT_ADDR(device));
320 if (!PermissionUtils::CheckSystemHapApp()) {
321 HILOGE("check system api failed.");
322 return BT_ERR_SYSTEM_PERMISSION_FAILED;
323 }
324 return NO_ERROR;
325 }
326
GetConnectStrategy(const BluetoothRawAddress & device,int & strategy)327 int32_t BluetoothHidHostServer::GetConnectStrategy(const BluetoothRawAddress &device, int &strategy)
328 {
329 return NO_ERROR;
330 }
331 } // namespace Bluetooth
332 } // namespace OHOS
333