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