• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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_hid_host.h"
17 #include <unistd.h>
18 #include "bluetooth_device.h"
19 #include "bluetooth_host.h"
20 #include "bluetooth_profile_manager.h"
21 #include "bluetooth_log.h"
22 #include "bluetooth_observer_list.h"
23 #include "bluetooth_hid_host_observer_stub.h"
24 #include "bluetooth_utils.h"
25 #include "i_bluetooth_hid_host.h"
26 #include "i_bluetooth_host.h"
27 #include "iservice_registry.h"
28 #include "system_ability_definition.h"
29 
30 namespace OHOS {
31 namespace Bluetooth {
32 std::mutex g_hidProxyMutex;
33 class HidHostInnerObserver : public BluetoothHidHostObserverStub {
34 public:
HidHostInnerObserver(BluetoothObserverList<HidHostObserver> & observers)35     explicit HidHostInnerObserver(BluetoothObserverList<HidHostObserver> &observers) : observers_(observers)
36     {
37         HILOGD("Enter!");
38     }
~HidHostInnerObserver()39     ~HidHostInnerObserver() override
40     {
41         HILOGD("Enter!");
42     }
43 
OnConnectionStateChanged(const BluetoothRawAddress & device,int32_t state)44     ErrCode OnConnectionStateChanged(const BluetoothRawAddress &device, int32_t state) override
45     {
46         HILOGD("hid conn state, device: %{public}s, state: %{public}s",
47             GetEncryptAddr((device).GetAddress()).c_str(), GetProfileConnStateName(state).c_str());
48         BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
49         observers_.ForEach([remoteDevice, state](std::shared_ptr<HidHostObserver> observer) {
50             observer->OnConnectionStateChanged(remoteDevice, state);
51         });
52         return NO_ERROR;
53     }
54 
55 private:
56     BluetoothObserverList<HidHostObserver> &observers_;
57     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(HidHostInnerObserver);
58 };
59 
60 struct HidHost::impl {
61     impl();
62     ~impl();
63 
GetDevicesByStatesOHOS::Bluetooth::HidHost::impl64     int32_t GetDevicesByStates(std::vector<int> states, std::vector<BluetoothRemoteDevice>& result)
65     {
66         HILOGI("Enter!");
67         std::vector<BluetoothRawAddress> rawDevices;
68         std::vector<int32_t> tmpStates;
69         for (int32_t state : states) {
70             tmpStates.push_back((int32_t)state);
71         }
72         sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
73         CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_SERVICE_DISCONNECTED, "failed: no proxy");
74         int32_t ret = proxy->GetDevicesByStates(tmpStates, rawDevices);
75         if (ret != BT_NO_ERROR) {
76             HILOGE("inner error.");
77             return ret;
78         }
79 
80         for (BluetoothRawAddress rawDevice : rawDevices) {
81             BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 1);
82             result.push_back(remoteDevice);
83         }
84 
85         return BT_NO_ERROR;
86     }
87 
GetDeviceStateOHOS::Bluetooth::HidHost::impl88     int32_t GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state)
89     {
90         HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
91         sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
92         if (proxy == nullptr || !device.IsValidBluetoothRemoteDevice()) {
93             HILOGE("invalid param.");
94             return BT_ERR_INVALID_PARAM;
95         }
96 
97         return proxy->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()), state);
98     }
99 
ConnectOHOS::Bluetooth::HidHost::impl100     int32_t Connect(const BluetoothRemoteDevice &device)
101     {
102         sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
103         if (proxy == nullptr || !device.IsValidBluetoothRemoteDevice()) {
104             HILOGE("invalid param.");
105             return BT_ERR_INVALID_PARAM;
106         }
107         HILOGI("hid connect remote device: %{public}s", GET_ENCRYPT_ADDR(device));
108         return proxy->Connect(BluetoothRawAddress(device.GetDeviceAddr()));
109     }
110 
DisconnectOHOS::Bluetooth::HidHost::impl111     int32_t Disconnect(const BluetoothRemoteDevice &device)
112     {
113         HILOGI("hid disconnect remote device: %{public}s", GET_ENCRYPT_ADDR(device));
114         sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
115         if (proxy == nullptr || !device.IsValidBluetoothRemoteDevice()) {
116             HILOGE("invalid param.");
117             return BT_ERR_INVALID_PARAM;
118         }
119 
120         return proxy->Disconnect(BluetoothRawAddress(device.GetDeviceAddr()));
121     }
122 
RegisterObserverOHOS::Bluetooth::HidHost::impl123     void RegisterObserver(std::shared_ptr<HidHostObserver> observer)
124     {
125         HILOGI("Enter!");
126         observers_.Register(observer);
127     }
128 
DeregisterObserverOHOS::Bluetooth::HidHost::impl129     void DeregisterObserver(std::shared_ptr<HidHostObserver> observer)
130     {
131         HILOGI("Enter!");
132         observers_.Deregister(observer);
133     }
134 
HidHostVCUnplugOHOS::Bluetooth::HidHost::impl135     void HidHostVCUnplug(std::string device, uint8_t id, uint16_t size, uint8_t type)
136     {
137         HILOGI("Enter!");
138         int result;
139         sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
140         if (proxy != nullptr) {
141             proxy->HidHostVCUnplug(device, id, size, type, result);
142         }
143     }
144 
HidHostSendDataOHOS::Bluetooth::HidHost::impl145     void HidHostSendData(std::string device, uint8_t id, uint16_t size, uint8_t type)
146     {
147         HILOGI("Enter!");
148         int result;
149         sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
150         if (proxy != nullptr) {
151             proxy->HidHostSendData(device, id, size, type, result);
152         }
153     }
154 
HidHostSetReportOHOS::Bluetooth::HidHost::impl155     void HidHostSetReport(std::string device, uint8_t type, uint16_t size, uint8_t report)
156     {
157         HILOGI("Enter!");
158         int result;
159         sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
160         if (proxy != nullptr) {
161             proxy->HidHostSetReport(device, type, size, report, result);
162         }
163     }
164 
HidHostGetReportOHOS::Bluetooth::HidHost::impl165     void HidHostGetReport(std::string device, uint8_t id, uint16_t size, uint8_t type)
166     {
167         HILOGI("Enter!");
168         int result;
169         sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
170         if (proxy != nullptr && IS_BT_ENABLED()) {
171             proxy->HidHostGetReport(device, id, size, type, result);
172         }
173     }
174 
SetConnectStrategyOHOS::Bluetooth::HidHost::impl175     int SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy)
176     {
177         HILOGI("enter");
178         sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
179         return proxy->SetConnectStrategy(BluetoothRawAddress(device.GetDeviceAddr()), strategy);
180     }
181 
GetConnectStrategyOHOS::Bluetooth::HidHost::impl182     int GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const
183     {
184         HILOGI("enter");
185         sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
186         return proxy->GetConnectStrategy(BluetoothRawAddress(device.GetDeviceAddr()), strategy);
187     }
188 
189     int32_t profileRegisterId = 0;
190 private:
191     BluetoothObserverList<HidHostObserver> observers_;
192     sptr<HidHostInnerObserver> innerObserver_;
193 };
194 
impl()195 HidHost::impl::impl()
196 {
197     innerObserver_ = new HidHostInnerObserver(observers_);
198     profileRegisterId = DelayedSingleton<BluetoothProfileManager>::GetInstance()->RegisterFunc(PROFILE_HID_HOST_SERVER,
199         [this](sptr<IRemoteObject> remote) {
200         sptr<IBluetoothHidHost> proxy = iface_cast<IBluetoothHidHost>(remote);
201         CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
202         proxy->RegisterObserver(innerObserver_);
203     });
204 }
205 
~impl()206 HidHost::impl::~impl()
207 {
208     HILOGD("start");
209     DelayedSingleton<BluetoothProfileManager>::GetInstance()->DeregisterFunc(profileRegisterId);
210     sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
211     CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
212     proxy->DeregisterObserver(innerObserver_);
213 }
214 
HidHost()215 HidHost::HidHost()
216 {
217     pimpl = std::make_unique<impl>();
218 }
219 
~HidHost()220 HidHost::~HidHost() {}
221 
GetProfile()222 HidHost *HidHost::GetProfile()
223 {
224     static HidHost instance;
225     return &instance;
226 }
227 
GetDevicesByStates(std::vector<int> states,std::vector<BluetoothRemoteDevice> & result)228 int32_t HidHost::GetDevicesByStates(std::vector<int> states, std::vector<BluetoothRemoteDevice> &result)
229 {
230     if (!IS_BT_ENABLED()) {
231         HILOGE("bluetooth is off.");
232         return BT_ERR_INVALID_STATE;
233     }
234 
235     sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
236     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
237 
238     return pimpl->GetDevicesByStates(states, result);
239 }
240 
GetDeviceState(const BluetoothRemoteDevice & device,int32_t & state)241 int32_t HidHost::GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state)
242 {
243     if (!IS_BT_ENABLED()) {
244         HILOGE("bluetooth is off.");
245         return BT_ERR_INVALID_STATE;
246     }
247 
248     sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
249     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
250 
251     return pimpl->GetDeviceState(device, state);
252 }
253 
Connect(const BluetoothRemoteDevice & device)254 int32_t HidHost::Connect(const BluetoothRemoteDevice &device)
255 {
256     if (!IS_BT_ENABLED()) {
257         HILOGE("bluetooth is off.");
258         return BT_ERR_INVALID_STATE;
259     }
260 
261     sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
262     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
263 
264     return pimpl->Connect(device);
265 }
266 
Disconnect(const BluetoothRemoteDevice & device)267 int32_t HidHost::Disconnect(const BluetoothRemoteDevice &device)
268 {
269     if (!IS_BT_ENABLED()) {
270         HILOGE("bluetooth is off.");
271         return BT_ERR_INVALID_STATE;
272     }
273 
274     sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
275     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
276 
277     return pimpl->Disconnect(device);
278 }
279 
SetConnectStrategy(const BluetoothRemoteDevice & device,int strategy)280 int HidHost::SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy)
281 {
282     HILOGI("enter, device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
283     if (!IS_BT_ENABLED()) {
284         HILOGE("bluetooth is off.");
285         return BT_ERR_INVALID_STATE;
286     }
287 
288     sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
289     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
290 
291     if ((!device.IsValidBluetoothRemoteDevice()) || (
292         (strategy != static_cast<int>(BTStrategyType::CONNECTION_ALLOWED)) &&
293         (strategy != static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN)))) {
294         HILOGI("input parameter error.");
295         return BT_ERR_INVALID_PARAM;
296     }
297     return pimpl->SetConnectStrategy(device, strategy);
298 }
299 
GetConnectStrategy(const BluetoothRemoteDevice & device,int & strategy) const300 int HidHost::GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const
301 {
302     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
303     if (!IS_BT_ENABLED()) {
304         HILOGE("bluetooth is off.");
305         return BT_ERR_INVALID_STATE;
306     }
307 
308     sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
309     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
310 
311     if (!device.IsValidBluetoothRemoteDevice()) {
312         HILOGI("input parameter error.");
313         return BT_ERR_INVALID_PARAM;
314     }
315     return pimpl->GetConnectStrategy(device, strategy);
316 }
317 
RegisterObserver(std::shared_ptr<HidHostObserver> observer)318 void HidHost::RegisterObserver(std::shared_ptr<HidHostObserver> observer)
319 {
320     HILOGD("enter");
321     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
322     pimpl->RegisterObserver(observer);
323 }
324 
DeregisterObserver(std::shared_ptr<HidHostObserver> observer)325 void HidHost::DeregisterObserver(std::shared_ptr<HidHostObserver> observer)
326 {
327     HILOGD("enter");
328     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
329     pimpl->DeregisterObserver(observer);
330 }
331 
HidHostVCUnplug(std::string device,uint8_t id,uint16_t size,uint8_t type)332 void HidHost::HidHostVCUnplug(std::string device, uint8_t id, uint16_t size, uint8_t type)
333 {
334     if (!IS_BT_ENABLED()) {
335         HILOGE("bluetooth is off.");
336         return;
337     }
338     sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
339     CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
340 
341     return pimpl->HidHostVCUnplug(device, id, size, type);
342 }
343 
HidHostSendData(std::string device,uint8_t id,uint16_t size,uint8_t type)344 void HidHost::HidHostSendData(std::string device, uint8_t id, uint16_t size, uint8_t type)
345 {
346     if (!IS_BT_ENABLED()) {
347         HILOGE("bluetooth is off.");
348         return;
349     }
350     sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
351     CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
352 
353     return pimpl->HidHostSendData(device, id, size, type);
354 }
355 
HidHostSetReport(std::string device,uint8_t type,uint16_t size,uint8_t report)356 void HidHost::HidHostSetReport(std::string device, uint8_t type, uint16_t size, uint8_t report)
357 {
358     if (!IS_BT_ENABLED()) {
359         HILOGE("bluetooth is off.");
360         return;
361     }
362     sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
363     CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
364 
365     return pimpl->HidHostSetReport(device, type, size, report);
366 }
367 
HidHostGetReport(std::string device,uint8_t id,uint16_t size,uint8_t type)368 void HidHost::HidHostGetReport(std::string device, uint8_t id, uint16_t size, uint8_t type)
369 {
370     if (!IS_BT_ENABLED()) {
371         HILOGE("bluetooth is off.");
372         return;
373     }
374     sptr<IBluetoothHidHost> proxy = GetRemoteProxy<IBluetoothHidHost>(PROFILE_HID_HOST_SERVER);
375     CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
376 
377     return pimpl->HidHostGetReport(device, id, size, type);
378 }
379 } // namespace Bluetooth
380 } // namespace OHOS