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 /**
17  * @addtogroup Bluetooth
18  * @{
19  *
20  * @brief Defines a bluetooth system that provides basic bluetooth connection and profile functions,
21  *        including A2DP, AVRCP, BLE, GATT, HFP, MAP, PBAP, and SPP, etc.
22  *
23  */
24 
25 /**
26  * @file bluetooth_hid_host.h
27  *
28  * @brief Declares HID HOST role framework functions, including basic and observer functions.
29  *
30  */
31 #ifndef BLUETOOTH_HID_HOST_H
32 #define BLUETOOTH_HID_HOST_H
33 
34 #include <string>
35 #include <vector>
36 #include <memory>
37 
38 #include "bluetooth_def.h"
39 #include "bluetooth_types.h"
40 #include "bluetooth_remote_device.h"
41 #include "bluetooth_no_destructor.h"
42 namespace OHOS {
43 namespace Bluetooth {
44 /**
45  * @brief Class for Hid Host observer functions.
46  *
47  */
48 class HidHostObserver {
49 public:
50     /**
51      * @brief The observer function to notify connection state changed.
52      *
53      * @param device Remote device object.
54      * @param state Connection state.
55      * @param cause Connection cause.
56      */
OnConnectionStateChanged(const BluetoothRemoteDevice & device,int state,int cause)57     virtual void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state, int cause)
58     {}
59 
60     /**
61      * @brief Destroy the HidHostObserver object.
62      *
63      */
~HidHostObserver()64     virtual ~HidHostObserver()
65     {}
66 };
67 
68 /**
69  * @brief Class for Hid Host API.
70  *
71  */
72 class BLUETOOTH_API HidHost {
73 public:
74     /**
75      * @brief Get the instance of HidHost object.
76      *
77      * @return Returns the pointer to the HidHost instance.
78      */
79     static HidHost *GetProfile();
80 
81     /**
82      * @brief Get remote Hid device list which are in the specified states.
83      *
84      * @param states List of remote device states.
85      * @return Returns the list of devices.
86      */
87     int32_t GetDevicesByStates(std::vector<int> states, std::vector<BluetoothRemoteDevice> &result);
88 
89     /**
90      * @brief Get the connection state of the specified remote Hid device.
91      *
92      * @param device Remote device object.
93      * @return Returns the connection state of the remote device.
94      */
95     int32_t GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state);
96 
97     /**
98      * @brief Initiate the establishment of a service level connection to remote Hid device.
99      *
100      * @param device Remote device object.
101      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
102      */
103     int32_t Connect(const BluetoothRemoteDevice &device);
104 
105     /**
106      * @brief Release the connection from remote Hid device.
107      *
108      * @param device Remote device object.
109      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
110      */
111     int32_t Disconnect(const BluetoothRemoteDevice &device);
112 
113     /**
114      * @brief Set connection strategy for peer bluetooth device.
115      *        If peer device is connected and the policy is set not allowed,then perform disconnect operation.
116      *        If peer device is disconnected and the policy is set allowed,then perform connect operation.
117      *
118      * @param device The address of the peer bluetooth device.
119      * @param strategy The device connect strategy.
120      * @return Returns <b>RET_NO_ERROR</b> if the operation is successful.
121      *         Returns <b>BT_ERR_PERMISSION_FAILED</b> Permission denied.
122      *         Returns <b>BT_ERR_INVALID_PARAM</b> Input error.
123      *         Returns <b>BT_ERR_INVALID_STATE</b> BT_ERR_INVALID_STATE.
124      *         Returns <b>BT_ERR_INTERNAL_ERROR</b> Operation failed.
125      * @since 10.0
126      */
127     int SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy);
128 
129     /**
130      * @brief Get connection strategy of peer bluetooth device.
131      *
132      * @param device The address of the peer bluetooth device.
133      * @param strategy The device connect strategy.
134      * @return Returns <b>RET_NO_ERROR</b> if the operation is successful.
135      *         Returns <b>BT_ERR_PERMISSION_FAILED</b> Permission denied.
136      *         Returns <b>BT_ERR_INVALID_PARAM</b> Input error.
137      *         Returns <b>BT_ERR_INVALID_STATE</b> BT_ERR_INVALID_STATE.
138      *         Returns <b>BT_ERR_INTERNAL_ERROR</b> Operation failed.
139      * @since 10.0
140      */
141     int GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const;
142 
143         /**
144      * @brief Register Hid Host observer instance.
145      *
146      * @param observer Hid Host observer instance.
147      */
148     void RegisterObserver(std::shared_ptr<HidHostObserver> observer);
149 
150     /**
151      * @brief Deregister Hid Host observer instance.
152      *
153      * @param observer Hid Host observer instance.
154      */
155     void DeregisterObserver(std::shared_ptr<HidHostObserver> observer);
156 
157     /**
158      * @brief Hid Host VCUnplug.
159      *
160      * @param observer Hid Host device id size type.
161      */
162     void HidHostVCUnplug(std::string device, uint8_t id, uint16_t size, uint8_t type);
163 
164     /**
165      * @brief Hid Host Send Data.
166      *
167      * @param observer Hid Host device id size type.
168      */
169     void HidHostSendData(std::string device, uint8_t id, uint16_t size, uint8_t type);
170 
171     /**
172      * @brief Hid Host Set Report.
173      *
174      * @param observer Hid Host device type size report.
175      */
176     void HidHostSetReport(std::string device, uint8_t type, std::string &report);
177 
178     /**
179      * @brief Hid Host Get Report.
180      *
181      * @param observer Hid Host device id size type.
182      */
183     void HidHostGetReport(std::string device, uint8_t id, uint16_t size, uint8_t type);
184 
185     /**
186      * @brief The external process calls the HidHost profile interface before the Bluetooth process starts. At this
187      * time, it needs to monitor the start of the Bluetooth process, and then call this interface to initialize the
188      * HidHost proflie.
189      */
190     void Init();
191 
192     /**
193      * @brief Static Hid Host observer instance.
194      *
195      */
196     static HidHostObserver *instance_;
197 
198 private:
199     HidHost();
200     ~HidHost();
201     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(HidHost);
202     BLUETOOTH_DECLARE_IMPL();
203 
204 #ifdef DTFUZZ_TEST
205     friend class BluetoothNoDestructor<HidHost>;
206 #endif
207 };
208 }  // namespace Bluetooth
209 }  // namespace OHOS
210 #endif  // BLUETOOTH_HID_HOST_H