hids.h
Go to the documentation of this file.
1 /**
2  *******************************************************************************
3  *
4  * @file hids.h
5  *
6  * @brief Human Interface Device Service API
7  *
8  *******************************************************************************
9  * @attention
10  #####Copyright (c) 2019 GOODIX
11  All rights reserved.
12 
13  Redistribution and use in source and binary forms, with or without
14  modification, are permitted provided that the following conditions are met:
15  * Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17  * Redistributions in binary form must reproduce the above copyright
18  notice, this list of conditions and the following disclaimer in the
19  documentation and/or other materials provided with the distribution.
20  * Neither the name of GOODIX nor the names of its contributors may be used
21  to endorse or promote products derived from this software without
22  specific prior written permission.
23 
24  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  POSSIBILITY OF SUCH DAMAGE.
35  *****************************************************************************************
36  */
37 
38 /**
39  * @addtogroup BLE_SRV BLE Services
40  * @{
41  * @brief Definitions and prototypes for the BLE Service interface.
42  */
43 
44 /**
45  * @defgroup BLE_SDK_HIDS Human Input Device Service (HIDS)
46  * @{
47  * @brief Definitions and prototypes for the HIDS interface.
48  *
49  * @details The HID Service exposes data and associated formatting for HID Devices
50  * and HID Hosts. This module implements the HID Service with HID Information
51  * characteristic, HID Control Point characteristic, Report Map characteristic,
52  * Input/Output/Feature Report characteristics, Boot Keyboard Input characteristic,
53  * Boot Keyboard Output characteristic, Boot Mouse Input Report characteristic.
54  *
55  * After \ref hids_init_t variable is initialized, the application must call \ref hids_service_init()
56  * to add the HID Service and the characteristics to the BLE Stack database. However
57  * the array of Report map locates in user space, application must make sure the
58  * array is available.
59  *
60  * If Notify is enabled, the value of Input Report characteristic is sent to the
61  * peer when application calls \ref hids_input_rep_send() function. The application is reponsible
62  * for encoding Input Report data as “USB HID Spec”. If an event hanlder is provided by the application,
63  * HID Service will pass HIDS events to the application, e.g. Output Report characteristic is written.
64  */
65 
66 #ifndef __HIDS_H__
67 #define __HIDS_H__
68 
69 #include "ble_prf_utils.h"
70 #include "gr55xx_sys.h"
71 #include "custom_config.h"
72 #include <stdint.h>
73 
74 /**
75  * @defgroup HIDS_MACRO Defines
76  * @{
77  */
78 
79 #define HIDS_CONNECTION_MAX (10 < CFG_MAX_CONNECTIONS ?\
80  10 : CFG_MAX_CONNECTIONS) /**< Maximum number of Heart Rate Service connections. */
81 
82 #define HIDS_REPORT_MAX_SIZE 20 /**< Maximum length of report. */
83 #define HIDS_REPORT_MAP_MAX_SIZE 512 /**< Limitation of length, as per Section 2.6.1 in HIDS Spec, version 1.0 */
84 
85 /**
86  * @defgroup HIDS_REPORT_TYPE Report Type values
87  * @{
88  * @brief HIDS Report Type values define.
89  */
90 #define HIDS_REP_TYPE_INPUT 1 /**< The input report type. */
91 #define HIDS_REP_TYPE_OUTPUT 2 /**< The output report type. */
92 #define HIDS_REP_TYPE_FEATURE 3 /**< The feature report type. */
93 /** @} */
94 
95 /**
96  * @defgroup HIDS_INFOR_FLAGS Information Flags
97  * @{
98  * @brief HIDS Information Flags define.
99  */
100 #define HID_INFO_FLAG_REMOTE_WAKE_MSK 0x01 /**< Bit mask of Remote Wake flag in HIDS information. */
101 #define HID_INFO_FLAG_NORMALLY_CONNECTABLE_MSK 0x02 /**< Bit mask of Normally Connectable flag in HIDS information. */
102 /** @} */
103 /** @} */
104 
105 
106 /**
107  * @defgroup HIDS_ENUM Enumerations
108  * @{
109  */
110 
111 /**@brief HID Service event type. */
112 typedef enum
113 {
114  HIDS_EVT_INVALID, /**< Invalid event. */
115  HIDS_EVT_IN_REP_NOTIFY_ENABLED, /**< Input report notification enabled event. */
116  HIDS_EVT_IN_REP_NOTIFY_DISABLED, /**< Input report notification disabled event. */
117  HIDS_EVT_HOST_SUSP, /**< Suspend command received. */
118  HIDS_EVT_HOST_EXIT_SUSP, /**< Exit suspend command received. */
119  HIDS_EVT_BOOT_MODE_ENTERED, /**< Boot mode entered */
120  HIDS_EVT_REPORT_MODE_ENTERED, /**< Report mode entered */
121  HIDS_EVT_REP_CHAR_WRITE, /**< New value has been written to a report characteristic */
123 
124 
125 /**@brief HID Service write report type. */
126 typedef enum
127 {
128  HIDS_REPORT_TYPE_RESERVED, /**< The reserved report type. */
129  HIDS_REPORT_TYPE_IN1, /**< The input report1 type. */
130  HIDS_REPORT_TYPE_IN2, /**< The input report2 type. */
131  HIDS_REPORT_TYPE_IN3, /**< The input report3 type. */
132  HIDS_REPORT_TYPE_OUT, /**< The output report type. */
133  HIDS_REPORT_TYPE_FEATURE, /**< The feature report type. */
134  HIDS_REPORT_TYPE_KB_IN, /**< The boot keyboard input report type. */
135  HIDS_REPORT_TYPE_KB_OUT, /**< The boot keyboard output report type. */
136  HIDS_REPORT_TYPE_MOUSE_IN, /**< The boot mouse inputreport type. */
138 /** @} */
139 
140 
141 /**
142  * @defgroup HIDS_TYPEDEF Typedefs
143  * @{
144  */
145 
146 /**@brief HID Service event. */
147 typedef struct
148 {
149  hids_evt_type_t evt_type; /**< Type of event. */
150  uint8_t conn_idx; /**< Connect index. */
151  hids_report_type_t report_type; /**< Type of report, see @ref hids_report_type_t. */
152  uint16_t offset; /**< Offset for the write operation. */
153  uint16_t len; /**< Length of the incoming data. */
154  uint8_t const * data; /**< Incoming data, variable length */
155 }hids_evt_t;
156 
157 /**@brief HID Information characteristic value. */
158 typedef struct
159 {
160  uint16_t bcd_hid; /**< 16-bit unsigned integer representing version number of base USB HID Specification implemented by HID Device */
161  uint8_t b_country_code; /**< Identifies which country the hardware is localized for. Most hardware is not localized and thus this value would be zero (0). */
162  uint8_t flags; /**< See http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.hid_information.xml */
164 
165 
166 /**@brief Value of a Report Reference descriptor.
167  *
168  * @details This is mapping information that maps the parent characteristic to the Report ID(s) and
169  * Report Type(s) defined within a Report Map characteristic.
170  */
171 typedef struct
172 {
173  uint8_t report_id; /**< Non-zero value if there is more than one instance of the same Report Type */
174  uint8_t report_type; /**< Type of Report characteristic (see @ref HIDS_REPORT_TYPE) */
176 
177 
178 /**@brief HID Service Report characteristic define. */
179 typedef struct
180 {
181  uint16_t value_len; /**< Length of characteristic value. */
182  hids_report_ref_t ref; /**< Value of a Report Reference descriptor, see @ref hids_report_ref_t. */
184 
185 
186 /**@brief HID Service Report Map characteristic value. */
187 typedef struct
188 {
189  uint8_t *p_map; /**< Pointer to the report map. */
190  uint16_t len; /**< The length of report map. */
192 
193 
194 /**@brief HID Service event handler type.
195  *
196  * @param[in] p_evt Pointer to a HID Service event variable.
197  */
198 typedef void (*hids_evt_handler_t)(hids_evt_t *p_evt);
199 
200 
201 /**@brief HID Service initialization variable. */
202 typedef struct
203 {
204  hids_evt_handler_t evt_handler; /**< Handle events in HID Service. */
205  bool is_kb; /**< TRUE if device is operating as a keyboard, FALSE if it is not. */
206  bool is_mouse; /**< TRUE if device is operating as a mouse, FALSE if it is not. */
207  hids_hid_info_t hid_info; /**< Value of HID information characteristic. */
208  hids_report_map_t report_map; /**< HID Service Report Map characteristic value. */
209  uint8_t input_report_count; /**< Number of Input Report characteristics. */
210  hids_report_int_t input_report_array[3]; /**< HID input Report Reference value. */
211  bool out_report_sup; /**< TRUE if output Report characteristic suport, FALSE if it is nonsupport. */
212  hids_report_int_t output_report; /**< HID output Report Reference value. */
213  bool feature_report_sup; /**< TRUE if feature Report characteristic suport, FALSE if it is nonsupport. */
214  hids_report_int_t feature_report; /**< HID feature Report Reference value. */
215 } hids_init_t;
216 /** @} */
217 
218 /**
219  * @defgroup HIDS_FUNCTION Functions
220  * @{
221  */
222 
223 /**
224  *****************************************************************************************
225  * @brief Initialize a HID Service instance in ATT DB.
226  *
227  * @param[in] p_hids_init: Pointer to a HID Service initialization variable.
228  *
229  * @return Result of service initialization.
230  *****************************************************************************************
231  */
233 
234 /**
235  *****************************************************************************************
236  * @brief Send an input report.
237  *
238  * @param[in] conn_idx: Connection index.
239  * @param[in] rep_idx: Input report inedx.
240  * @param[in] p_data: Pointer to data to be sent.
241  * @param[in] length: Length of data to be sent.
242  *
243  * @return BLE_SDK_SUCCESS on success, otherwise an error code.
244  *****************************************************************************************
245  */
246 sdk_err_t hids_input_rep_send(uint8_t conn_idx, uint8_t rep_idx, uint8_t *p_data, uint16_t length);
247 
248 /**
249  *****************************************************************************************
250  * @brief Send boot keyboard input report.
251  *
252  * @param[in] conn_idx: Connection index.
253  * @param[in] p_data: Pointer to data to be sent.
254  * @param[in] length: Length of data to be sent.
255  *
256  * @return BLE_SDK_SUCCESS on success, otherwise an error code.
257  *****************************************************************************************
258  */
259 sdk_err_t hids_boot_kb_in_rep_send(uint8_t conn_idx, uint8_t *p_data, uint16_t length);
260 
261 /**
262  *****************************************************************************************
263  * @brief Send boot mouse input report.
264  *
265  * @param[in] conn_idx: Connection index.
266  * @param[in] p_data: Pointer to data to be sent.
267  * @param[in] length: Length of data to be sent.
268  *
269  * @return BLE_SDK_SUCCESS on success, otherwise an error code.
270  *****************************************************************************************
271  */
272 sdk_err_t hids_boot_mouse_in_rep_send(uint8_t conn_idx, uint8_t *p_data, uint16_t length);
273 /** @} */
274 
275 #endif
276 /** @} */
277 /** @} */
278 
HIDS_EVT_REP_CHAR_WRITE
@ HIDS_EVT_REP_CHAR_WRITE
New value has been written to a report characteristic.
Definition: hids.h:121
hids_report_type_t
hids_report_type_t
HID Service write report type.
Definition: hids.h:127
hids_init_t::is_kb
bool is_kb
TRUE if device is operating as a keyboard, FALSE if it is not.
Definition: hids.h:205
hids_init_t::feature_report
hids_report_int_t feature_report
HID feature Report Reference value.
Definition: hids.h:214
hids_hid_info_t::bcd_hid
uint16_t bcd_hid
16-bit unsigned integer representing version number of base USB HID Specification implemented by HID ...
Definition: hids.h:160
HIDS_REPORT_TYPE_KB_IN
@ HIDS_REPORT_TYPE_KB_IN
The boot keyboard input report type.
Definition: hids.h:134
HIDS_REPORT_TYPE_IN2
@ HIDS_REPORT_TYPE_IN2
The input report2 type.
Definition: hids.h:130
hids_evt_t::conn_idx
uint8_t conn_idx
Connect index.
Definition: hids.h:150
HIDS_EVT_REPORT_MODE_ENTERED
@ HIDS_EVT_REPORT_MODE_ENTERED
Report mode entered.
Definition: hids.h:120
HIDS_EVT_IN_REP_NOTIFY_ENABLED
@ HIDS_EVT_IN_REP_NOTIFY_ENABLED
Input report notification enabled event.
Definition: hids.h:115
HIDS_REPORT_TYPE_IN1
@ HIDS_REPORT_TYPE_IN1
The input report1 type.
Definition: hids.h:129
HIDS_EVT_HOST_EXIT_SUSP
@ HIDS_EVT_HOST_EXIT_SUSP
Exit suspend command received.
Definition: hids.h:118
hids_report_int_t
HID Service Report characteristic define.
Definition: hids.h:180
hids_report_ref_t::report_type
uint8_t report_type
Type of Report characteristic (see Report Type values)
Definition: hids.h:174
HIDS_EVT_HOST_SUSP
@ HIDS_EVT_HOST_SUSP
Suspend command received.
Definition: hids.h:117
hids_service_init
sdk_err_t hids_service_init(hids_init_t *p_hids_init)
Initialize a HID Service instance in ATT DB.
hids_init_t::out_report_sup
bool out_report_sup
TRUE if output Report characteristic suport, FALSE if it is nonsupport.
Definition: hids.h:211
hids_init_t::evt_handler
hids_evt_handler_t evt_handler
Handle events in HID Service.
Definition: hids.h:204
HIDS_REPORT_TYPE_RESERVED
@ HIDS_REPORT_TYPE_RESERVED
The reserved report type.
Definition: hids.h:128
gr55xx_sys.h
GR55XX System API.
hids_hid_info_t
HID Information characteristic value.
Definition: hids.h:159
hids_report_int_t::value_len
uint16_t value_len
Length of characteristic value.
Definition: hids.h:181
HIDS_EVT_IN_REP_NOTIFY_DISABLED
@ HIDS_EVT_IN_REP_NOTIFY_DISABLED
Input report notification disabled event.
Definition: hids.h:116
hids_report_map_t::len
uint16_t len
The length of report map.
Definition: hids.h:190
hids_evt_t::len
uint16_t len
Length of the incoming data.
Definition: hids.h:153
HIDS_EVT_INVALID
@ HIDS_EVT_INVALID
Invalid event.
Definition: hids.h:114
hids_evt_t::evt_type
hids_evt_type_t evt_type
Type of event.
Definition: hids.h:149
hids_init_t::feature_report_sup
bool feature_report_sup
TRUE if feature Report characteristic suport, FALSE if it is nonsupport.
Definition: hids.h:213
hids_evt_handler_t
void(* hids_evt_handler_t)(hids_evt_t *p_evt)
HID Service event handler type.
Definition: hids.h:198
hids_input_rep_send
sdk_err_t hids_input_rep_send(uint8_t conn_idx, uint8_t rep_idx, uint8_t *p_data, uint16_t length)
Send an input report.
hids_evt_t::offset
uint16_t offset
Offset for the write operation.
Definition: hids.h:152
hids_evt_type_t
hids_evt_type_t
HID Service event type.
Definition: hids.h:113
hids_report_map_t
HID Service Report Map characteristic value.
Definition: hids.h:188
hids_report_ref_t
Value of a Report Reference descriptor.
Definition: hids.h:172
hids_init_t::output_report
hids_report_int_t output_report
HID output Report Reference value.
Definition: hids.h:212
hids_init_t::is_mouse
bool is_mouse
TRUE if device is operating as a mouse, FALSE if it is not.
Definition: hids.h:206
hids_init_t::report_map
hids_report_map_t report_map
HID Service Report Map characteristic value.
Definition: hids.h:208
sdk_err_t
uint16_t sdk_err_t
SDK API result type.
Definition: ble_error.h:243
hids_hid_info_t::flags
uint8_t flags
See http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer....
Definition: hids.h:162
hids_report_ref_t::report_id
uint8_t report_id
Non-zero value if there is more than one instance of the same Report Type.
Definition: hids.h:173
HIDS_EVT_BOOT_MODE_ENTERED
@ HIDS_EVT_BOOT_MODE_ENTERED
Boot mode entered.
Definition: hids.h:119
HIDS_REPORT_TYPE_MOUSE_IN
@ HIDS_REPORT_TYPE_MOUSE_IN
The boot mouse inputreport type.
Definition: hids.h:136
hids_report_int_t::ref
hids_report_ref_t ref
Value of a Report Reference descriptor, see hids_report_ref_t.
Definition: hids.h:182
HIDS_REPORT_TYPE_KB_OUT
@ HIDS_REPORT_TYPE_KB_OUT
The boot keyboard output report type.
Definition: hids.h:135
hids_evt_t::data
uint8_t const * data
Incoming data, variable length.
Definition: hids.h:154
hids_evt_t
HID Service event.
Definition: hids.h:148
HIDS_REPORT_TYPE_OUT
@ HIDS_REPORT_TYPE_OUT
The output report type.
Definition: hids.h:132
hids_report_map_t::p_map
uint8_t * p_map
Pointer to the report map.
Definition: hids.h:189
ble_prf_utils.h
Profile/Service Utilities API.
HIDS_REPORT_TYPE_IN3
@ HIDS_REPORT_TYPE_IN3
The input report3 type.
Definition: hids.h:131
hids_evt_t::report_type
hids_report_type_t report_type
Type of report, see hids_report_type_t.
Definition: hids.h:151
hids_boot_mouse_in_rep_send
sdk_err_t hids_boot_mouse_in_rep_send(uint8_t conn_idx, uint8_t *p_data, uint16_t length)
Send boot mouse input report.
HIDS_REPORT_TYPE_FEATURE
@ HIDS_REPORT_TYPE_FEATURE
The feature report type.
Definition: hids.h:133
hids_hid_info_t::b_country_code
uint8_t b_country_code
Identifies which country the hardware is localized for.
Definition: hids.h:161
hids_init_t
HID Service initialization variable.
Definition: hids.h:203
hids_init_t::hid_info
hids_hid_info_t hid_info
Value of HID information characteristic.
Definition: hids.h:207
hids_init_t::input_report_count
uint8_t input_report_count
Number of Input Report characteristics.
Definition: hids.h:209
hids_boot_kb_in_rep_send
sdk_err_t hids_boot_kb_in_rep_send(uint8_t conn_idx, uint8_t *p_data, uint16_t length)
Send boot keyboard input report.