• 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         });
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     std::lock_guard<std::mutex> lock(oblock_);
137 
138     if (observer == nullptr) {
139         HILOGE("observer is null");
140         return ERR_INVALID_VALUE;
141     }
142     if (pimpl == nullptr) {
143         HILOGE("pimpl is null");
144         return ERR_NO_INIT;
145     }
146     auto func = std::bind(&BluetoothHidHostServer::DeregisterObserver, this, std::placeholders::_1);
147     pimpl->observers_.Register(observer, func);
148     pimpl->advCallBack_.push_back(observer);
149     return ERR_OK;
150 }
151 
DeregisterObserver(const sptr<IBluetoothHidHostObserver> observer)152 ErrCode BluetoothHidHostServer::DeregisterObserver(const sptr<IBluetoothHidHostObserver> observer)
153 {
154     HILOGI("start");
155     std::lock_guard<std::mutex> lock(oblock_);
156     if (observer == nullptr) {
157         HILOGE("observer is null");
158         return ERR_INVALID_VALUE;
159     }
160     if (pimpl == nullptr) {
161         HILOGE("pimpl is null");
162         return ERR_NO_INIT;
163     }
164     for (auto iter = pimpl->advCallBack_.begin(); iter != pimpl->advCallBack_.end(); ++iter) {
165         if ((*iter)->AsObject() != nullptr && (*iter)->AsObject() == observer->AsObject()) {
166             if (pimpl != nullptr) {
167                 pimpl->observers_.Deregister(*iter);
168                 pimpl->advCallBack_.erase(iter);
169                 break;
170             }
171         }
172     }
173     pimpl->hidHostService_->DeregisterObserver(*pimpl->observerImp_.get());
174     return ERR_OK;
175 }
176 
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRawAddress> & result)177 int32_t BluetoothHidHostServer::GetDevicesByStates(
178     const std::vector<int32_t> &states, std::vector<BluetoothRawAddress>& result)
179 {
180     HILOGI("start");
181     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
182         HILOGE("check permission failed");
183         return BT_ERR_PERMISSION_FAILED;
184     }
185     if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
186         HILOGI("hidHostService_ is null");
187         return BT_ERR_INTERNAL_ERROR;
188     }
189 
190     std::vector<bluetooth::RawAddress> serviceDeviceList = pimpl->hidHostService_->GetDevicesByStates(states);
191     for (auto &device : serviceDeviceList) {
192         BluetoothRawAddress bluetoothDevice(device.GetAddress());
193         result.push_back(bluetoothDevice);
194         HILOGI("%{public}s", GET_ENCRYPT_ADDR(bluetoothDevice));
195     }
196     return NO_ERROR;
197 }
198 
GetDeviceState(const BluetoothRawAddress & device,int32_t & state)199 int32_t BluetoothHidHostServer::GetDeviceState(const BluetoothRawAddress &device, int32_t &state)
200 {
201     HILOGI("start, addr:%{public}s", GET_ENCRYPT_ADDR(device));
202     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
203         HILOGE("check permission failed");
204         return BT_ERR_PERMISSION_FAILED;
205     }
206     if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
207         HILOGI("hidHostService_ is null");
208         return BT_ERR_INTERNAL_ERROR;
209     }
210     state = pimpl->hidHostService_->GetDeviceState(device);
211     HILOGI("end, result:%{public}d", state);
212     return NO_ERROR;
213 }
214 
Connect(const BluetoothRawAddress & device)215 int32_t BluetoothHidHostServer::Connect(const BluetoothRawAddress &device)
216 {
217     HILOGI("start, addr:%{public}s", GET_ENCRYPT_ADDR(device));
218     if (!PermissionUtils::CheckSystemHapApp()) {
219         HILOGE("check system api failed.");
220         return BT_ERR_SYSTEM_PERMISSION_FAILED;
221     }
222     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
223         HILOGE("check permission failed");
224         return BT_ERR_PERMISSION_FAILED;
225     }
226     if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
227         HILOGE("hidHostService_ is null");
228         return BT_ERR_INTERNAL_ERROR;
229     }
230     return pimpl->hidHostService_->Connect(device);
231 }
232 
Disconnect(const BluetoothRawAddress & device)233 int32_t BluetoothHidHostServer::Disconnect(const BluetoothRawAddress &device)
234 {
235     HILOGI("start, addr:%{public}s", GET_ENCRYPT_ADDR(device));
236     if (!PermissionUtils::CheckSystemHapApp()) {
237         HILOGE("check system api failed.");
238         return BT_ERR_SYSTEM_PERMISSION_FAILED;
239     }
240     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
241         HILOGE("check permission failed");
242         return BT_ERR_PERMISSION_FAILED;
243     }
244     if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
245         HILOGI("hidHostService_ is null");
246         return BT_ERR_INTERNAL_ERROR;
247     }
248     return pimpl->hidHostService_->Disconnect(device);
249 }
250 
HidHostVCUnplug(std::string & device,uint8_t & id,uint16_t & size,uint8_t & type,int & result)251 ErrCode BluetoothHidHostServer::HidHostVCUnplug(std::string &device,
252     uint8_t &id, uint16_t &size, uint8_t &type, int& result)
253 {
254     HILOGI("start");
255     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
256         HILOGE("check permission failed");
257         return ERR_PERMISSION_DENIED;
258     }
259     if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
260         HILOGI("hidHostService_ is null");
261         return ERR_NO_INIT;
262     }
263     result = pimpl->hidHostService_->HidHostVCUnplug(device, id, size, type);
264     HILOGI("end, result:%{public}d", result);
265     return ERR_OK;
266 }
267 
HidHostSendData(std::string & device,uint8_t & id,uint16_t & size,uint8_t & type,int & result)268 ErrCode BluetoothHidHostServer::HidHostSendData(std::string &device,
269     uint8_t &id, uint16_t &size, uint8_t &type, int& result)
270 {
271     HILOGI("start");
272     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
273         HILOGE("check permission failed");
274         return ERR_PERMISSION_DENIED;
275     }
276     if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
277         HILOGI("hidHostService_ is null");
278         return ERR_NO_INIT;
279     }
280     result = pimpl->hidHostService_->HidHostSendData(device, id, size, type);
281     HILOGI("end, result:%{public}d", result);
282     return ERR_OK;
283 }
284 
HidHostSetReport(std::string & device,uint8_t & type,uint16_t & size,uint8_t & report,int & result)285 ErrCode BluetoothHidHostServer::HidHostSetReport(std::string &device,
286     uint8_t &type, uint16_t &size, uint8_t &report, int& result)
287 {
288     HILOGI("start");
289     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
290         HILOGE("check permission failed");
291         return ERR_PERMISSION_DENIED;
292     }
293     if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
294         HILOGI("hidHostService_ is null");
295         return ERR_NO_INIT;
296     }
297     result = pimpl->hidHostService_->HidHostSetReport(device, type, size, &report);
298     HILOGI("end, result:%{public}d", result);
299     return ERR_OK;
300 }
301 
HidHostGetReport(std::string & device,uint8_t & id,uint16_t & size,uint8_t & type,int & result)302 ErrCode BluetoothHidHostServer::HidHostGetReport(std::string &device,
303     uint8_t &id, uint16_t &size, uint8_t &type, int& result)
304 {
305     HILOGI("start");
306     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
307         HILOGE("check permission failed");
308         return ERR_PERMISSION_DENIED;
309     }
310     if (pimpl == nullptr || pimpl->hidHostService_ == nullptr) {
311         HILOGI("hidHostService_ is null");
312         return ERR_NO_INIT;
313     }
314     result = pimpl->hidHostService_->HidHostGetReport(device, id, size, type);
315     HILOGI("end, result:%{public}d", result);
316     return ERR_OK;
317 }
318 
SetConnectStrategy(const BluetoothRawAddress & device,int strategy)319 int32_t BluetoothHidHostServer::SetConnectStrategy(const BluetoothRawAddress &device, int strategy)
320 {
321     return NO_ERROR;
322 }
323 
GetConnectStrategy(const BluetoothRawAddress & device,int & strategy)324 int32_t BluetoothHidHostServer::GetConnectStrategy(const BluetoothRawAddress &device, int &strategy)
325 {
326     return NO_ERROR;
327 }
328 }  // namespace Bluetooth
329 }  // namespace OHOS
330