• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) && (*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     if (pimpl->observerImp_ == nullptr) {
178         HILOGE("pimpl->observerImp_ is null");
179         return ERR_NO_INIT;
180     }
181     if (pimpl->hidHostService_ == nullptr) {
182         HILOGE("pimpl->hidHostService_ is null");
183         return ERR_NO_INIT;
184     } else {
185         pimpl->hidHostService_->DeregisterObserver(*pimpl->observerImp_.get());
186     }
187     return ERR_OK;
188 }
189 
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRawAddress> & result)190 int32_t BluetoothHidHostServer::GetDevicesByStates(
191     const std::vector<int32_t> &states, std::vector<BluetoothRawAddress>& result)
192 {
193     HILOGI("start");
194     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
195         HILOGE("check permission failed");
196         return BT_ERR_PERMISSION_FAILED;
197     }
198     if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
199         HILOGI("hidHostService_ is null");
200         return BT_ERR_INTERNAL_ERROR;
201     }
202 
203     std::vector<bluetooth::RawAddress> serviceDeviceList = pimpl->hidHostService_->GetDevicesByStates(states);
204     for (auto &device : serviceDeviceList) {
205         BluetoothRawAddress bluetoothDevice(device.GetAddress());
206         result.push_back(bluetoothDevice);
207         HILOGI("%{public}s", GET_ENCRYPT_ADDR(bluetoothDevice));
208     }
209     return NO_ERROR;
210 }
211 
GetDeviceState(const BluetoothRawAddress & device,int32_t & state)212 int32_t BluetoothHidHostServer::GetDeviceState(const BluetoothRawAddress &device, int32_t &state)
213 {
214     HILOGI("start, addr:%{public}s", GET_ENCRYPT_ADDR(device));
215     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
216         HILOGE("check permission failed");
217         return BT_ERR_PERMISSION_FAILED;
218     }
219     if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
220         HILOGI("hidHostService_ is null");
221         return BT_ERR_INTERNAL_ERROR;
222     }
223     state = pimpl->hidHostService_->GetDeviceState(device);
224     HILOGI("end, result:%{public}d", state);
225     return NO_ERROR;
226 }
227 
Connect(const BluetoothRawAddress & device)228 int32_t BluetoothHidHostServer::Connect(const BluetoothRawAddress &device)
229 {
230     HILOGI("start, addr:%{public}s", GET_ENCRYPT_ADDR(device));
231     if (!PermissionUtils::CheckSystemHapApp()) {
232         HILOGE("check system api failed.");
233         return BT_ERR_SYSTEM_PERMISSION_FAILED;
234     }
235     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
236         HILOGE("check permission failed");
237         return BT_ERR_PERMISSION_FAILED;
238     }
239     if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
240         HILOGE("hidHostService_ is null");
241         return BT_ERR_INTERNAL_ERROR;
242     }
243     return pimpl->hidHostService_->Connect(device);
244 }
245 
Disconnect(const BluetoothRawAddress & device)246 int32_t BluetoothHidHostServer::Disconnect(const BluetoothRawAddress &device)
247 {
248     HILOGI("start, addr:%{public}s", GET_ENCRYPT_ADDR(device));
249     if (!PermissionUtils::CheckSystemHapApp()) {
250         HILOGE("check system api failed.");
251         return BT_ERR_SYSTEM_PERMISSION_FAILED;
252     }
253     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
254         HILOGE("check permission failed");
255         return BT_ERR_PERMISSION_FAILED;
256     }
257     if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
258         HILOGI("hidHostService_ is null");
259         return BT_ERR_INTERNAL_ERROR;
260     }
261     return pimpl->hidHostService_->Disconnect(device);
262 }
263 
HidHostVCUnplug(std::string & device,uint8_t & id,uint16_t & size,uint8_t & type,int & result)264 ErrCode BluetoothHidHostServer::HidHostVCUnplug(std::string &device,
265     uint8_t &id, uint16_t &size, uint8_t &type, int& result)
266 {
267     HILOGI("start");
268     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
269         HILOGE("check permission failed");
270         return ERR_PERMISSION_DENIED;
271     }
272     if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
273         HILOGI("hidHostService_ is null");
274         return ERR_NO_INIT;
275     }
276     result = pimpl->hidHostService_->HidHostVCUnplug(device, id, size, type);
277     HILOGI("end, result:%{public}d", result);
278     return ERR_OK;
279 }
280 
HidHostSendData(std::string & device,uint8_t & id,uint16_t & size,uint8_t & type,int & result)281 ErrCode BluetoothHidHostServer::HidHostSendData(std::string &device,
282     uint8_t &id, uint16_t &size, uint8_t &type, int& result)
283 {
284     HILOGI("start");
285     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
286         HILOGE("check permission failed");
287         return ERR_PERMISSION_DENIED;
288     }
289     if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
290         HILOGI("hidHostService_ is null");
291         return ERR_NO_INIT;
292     }
293     result = pimpl->hidHostService_->HidHostSendData(device, id, size, type);
294     HILOGI("end, result:%{public}d", result);
295     return ERR_OK;
296 }
297 
HidHostSetReport(std::string & device,uint8_t & type,std::string & report,int & result)298 ErrCode BluetoothHidHostServer::HidHostSetReport(std::string &device,
299     uint8_t &type, std::string &report, int& result)
300 {
301     HILOGI("start");
302     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
303         HILOGE("check permission failed");
304         return ERR_PERMISSION_DENIED;
305     }
306     if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
307         HILOGI("hidHostService_ is null");
308         return ERR_NO_INIT;
309     }
310     std::vector<uint8_t> data;
311     for (char ch : report) {
312         data.emplace(data.end(), static_cast<uint8_t>(ch));
313     }
314     data.emplace(data.end(), static_cast<uint8_t>('\0'));
315     result = pimpl->hidHostService_->HidHostSetReport(device, type, data.size(), data.data());
316     HILOGI("end, result:%{public}d", result);
317     return ERR_OK;
318 }
319 
HidHostGetReport(std::string & device,uint8_t & id,uint16_t & size,uint8_t & type,int & result)320 ErrCode BluetoothHidHostServer::HidHostGetReport(std::string &device,
321     uint8_t &id, uint16_t &size, uint8_t &type, int& result)
322 {
323     HILOGI("start");
324     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
325         HILOGE("check permission failed");
326         return ERR_PERMISSION_DENIED;
327     }
328     if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
329         HILOGI("hidHostService_ is null");
330         return ERR_NO_INIT;
331     }
332     result = pimpl->hidHostService_->HidHostGetReport(device, id, size, type);
333     HILOGI("end, result:%{public}d", result);
334     return ERR_OK;
335 }
336 
SetConnectStrategy(const BluetoothRawAddress & device,int strategy)337 int32_t BluetoothHidHostServer::SetConnectStrategy(const BluetoothRawAddress &device, int strategy)
338 {
339     HILOGI("target device:%{public}s()", GET_ENCRYPT_ADDR(device));
340     if (!PermissionUtils::CheckSystemHapApp()) {
341         HILOGE("check system api failed.");
342         return BT_ERR_SYSTEM_PERMISSION_FAILED;
343     }
344     return NO_ERROR;
345 }
346 
GetConnectStrategy(const BluetoothRawAddress & device,int & strategy)347 int32_t BluetoothHidHostServer::GetConnectStrategy(const BluetoothRawAddress &device, int &strategy)
348 {
349     return NO_ERROR;
350 }
351 }  // namespace Bluetooth
352 }  // namespace OHOS
353