xref: /btstack/src/ble/gatt-service/hids_client.h (revision be78df3b6c7394390fa6d2bc04f991dd7ac2de2c)
1 /*
2  * Copyright (C) 2021 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 /**
39  * @title HID Service Client
40  *
41  */
42 
43 #ifndef HIDS_CLIENT_H
44 #define HIDS_CLIENT_H
45 
46 #include <stdint.h>
47 #include "btstack_defines.h"
48 #include "btstack_hid.h"
49 #include "bluetooth.h"
50 #include "btstack_linked_list.h"
51 #include "ble/gatt_client.h"
52 
53 #if defined __cplusplus
54 extern "C" {
55 #endif
56 
57 /**
58  * @text The HID Service Client is used on the HID Host to receive reports and other HID data.
59  */
60 
61 #ifndef MAX_NUM_HID_SERVICES
62 #define MAX_NUM_HID_SERVICES 3
63 #endif
64 #define HIDS_CLIENT_INVALID_REPORT_INDEX 0xFF
65 
66 #define HIDS_CLIENT_NUM_REPORTS 15
67 
68 typedef enum {
69     HIDS_CLIENT_STATE_IDLE,
70 
71     // get all HID services
72     HIDS_CLIENT_STATE_W2_QUERY_SERVICE,
73     HIDS_CLIENT_STATE_W4_SERVICE_RESULT,
74 
75     // for each service, discover all characteristics
76     HIDS_CLIENT_STATE_W2_QUERY_CHARACTERISTIC,
77     HIDS_CLIENT_STATE_W4_CHARACTERISTIC_RESULT,
78 
79     // called if BOOT mode
80     HIDS_CLIENT_STATE_W2_SET_PROTOCOL_MODE_WITHOUT_RESPONSE,
81 
82     // for each REPORT_MAP characteristic, read HID Descriptor (Report Map Characteristic Value)
83     HIDS_CLIENT_STATE_W2_READ_REPORT_MAP_HID_DESCRIPTOR,
84     HIDS_CLIENT_STATE_W4_REPORT_MAP_HID_DESCRIPTOR,
85 
86     // for REPORT_MAP characteristic, discover descriptor
87     HIDS_CLIENT_STATE_W2_REPORT_MAP_DISCOVER_CHARACTERISTIC_DESCRIPTORS,
88     HIDS_CLIENT_STATE_W4_REPORT_MAP_CHARACTERISTIC_DESCRIPTORS_RESULT,
89 
90     // for REPORT_MAP characteristic, read External Report Reference Characteristic Descriptor
91     HIDS_CLIENT_STATE_W2_REPORT_MAP_READ_EXTERNAL_REPORT_REFERENCE_UUID,
92     HIDS_CLIENT_STATE_W4_REPORT_MAP_EXTERNAL_REPORT_REFERENCE_UUID,
93 
94     // for every external report reference uuid, discover external Report characteristic
95     HIDS_CLIENT_STATE_W2_DISCOVER_EXTERNAL_REPORT_CHARACTERISTIC,
96     HIDS_CLIENT_STATE_W4_EXTERNAL_REPORT_CHARACTERISTIC_RESULT,
97 
98     // for each Report characteristics, discover Report characteristic descriptor
99     HIDS_CLIENT_STATE_W2_FIND_REPORT,
100     HIDS_CLIENT_STATE_W4_REPORT_FOUND,
101 
102     // then read value of Report characteristic descriptor to get report ID and type
103     HIDS_CLIENT_STATE_W2_READ_REPORT_ID_AND_TYPE,
104     HIDS_CLIENT_STATE_W4_REPORT_ID_AND_TYPE,
105 
106     HIDS_CLIENT_STATE_W2_ENABLE_INPUT_REPORTS,
107     HIDS_CLIENT_STATE_W4_INPUT_REPORTS_ENABLED,
108 
109     HIDS_CLIENT_STATE_CONNECTED,
110 
111     HIDS_CLIENT_W2_SEND_WRITE_REPORT,
112     HIDS_CLIENT_W4_WRITE_REPORT_DONE,
113 
114     HIDS_CLIENT_W2_SEND_GET_REPORT,
115     HIDS_CLIENT_W4_GET_REPORT_RESULT,
116 
117     HIDS_CLIENT_W2_READ_VALUE_OF_CHARACTERISTIC,
118     HIDS_CLIENT_W4_VALUE_OF_CHARACTERISTIC_RESULT,
119 
120     HIDS_CLIENT_W2_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE,
121 
122     HIDS_CLIENT_STATE_W2_CONFIGURE_NOTIFICATIONS,
123     HIDS_CLIENT_STATE_W4_NOTIFICATIONS_CONFIGURED,
124 
125 #ifdef ENABLE_TESTING_SUPPORT
126     HIDS_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION,
127     HIDS_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT,
128 #endif
129 
130 } hid_service_client_state_t;
131 
132 
133 typedef struct {
134     // GATT cache
135 
136     //reuse as descriptor_handle
137     uint16_t value_handle;
138     uint16_t end_handle;
139     uint16_t properties;
140 
141     // UUID of external Report characteristic, stored in Report Map descriptor EXTERNAL_REPORT_REFERENCE
142     uint16_t external_report_reference_uuid;
143 
144 #ifdef ENABLE_TESTING_SUPPORT
145     uint16_t ccc_handle;
146 #endif
147 
148     // service mapping
149     uint8_t service_index;
150     uint8_t report_id;
151     hid_report_type_t report_type;
152     uint8_t boot_report;
153     gatt_client_notification_t notification_listener;
154 } hids_client_report_t;
155 
156 typedef struct {
157     hid_protocol_mode_t protocol_mode;
158 
159     uint16_t start_handle;
160     uint16_t end_handle;
161 
162     uint16_t report_map_value_handle;
163     uint16_t report_map_end_handle;
164 
165     uint16_t hid_information_value_handle;
166     uint16_t control_point_value_handle;
167     uint16_t protocol_mode_value_handle;
168 
169     // descriptor storage
170     uint16_t hid_descriptor_offset;
171     uint16_t hid_descriptor_len;
172     uint16_t hid_descriptor_max_len;
173     uint8_t  hid_descriptor_status;     // ERROR_CODE_SUCCESS if descriptor available,
174                                         // ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if not, and
175                                         // ERROR_CODE_MEMORY_CAPACITY_EXCEEDED if descriptor is larger then the available space
176 } hid_service_t;
177 
178 typedef struct {
179     btstack_linked_item_t item;
180 
181     hci_con_handle_t  con_handle;
182     uint16_t          cid;
183 
184     hid_service_client_state_t state;
185     btstack_packet_handler_t   client_handler;
186 
187     uint8_t num_instances;
188     hid_service_t services[MAX_NUM_HID_SERVICES];
189 
190     // used for discovering characteristics
191     uint8_t service_index;
192     hid_protocol_mode_t required_protocol_mode;
193 
194     // send report
195     hids_client_report_t reports[HIDS_CLIENT_NUM_REPORTS];
196     uint8_t num_reports;
197 
198     hids_client_report_t external_reports[HIDS_CLIENT_NUM_REPORTS];
199     uint8_t num_external_reports;
200 
201     btstack_context_callback_registration_t write_without_response_request;
202 
203     // index used for report and report map search
204     uint8_t   report_index;
205     uint16_t  report_len;
206     const uint8_t * report;
207     // used to write control_point and  protocol_mode
208     uint16_t handle;
209     uint8_t  value;
210 } hids_client_t;
211 
212 /* API_START */
213 
214 /**
215  * @brief Initialize HID Service Client. The HID Descriptor storage is shared between all connections.
216  *
217  * @param hid_descriptor_storage
218  * @param hid_descriptor_storage_len
219  */
220 void hids_client_init(uint8_t * hid_descriptor_storage, uint16_t hid_descriptor_storage_len);
221 
222 /**
223  * @brief Connect to HID Services of remote device. Event GATTSERVICE_SUBEVENT_HID_SERVICE_CONNECTED will be emitted
224  * after all remote HID services and characteristics are found, and notifications for all input reports are enabled.
225  * Status code can be ERROR_CODE_SUCCES if at least one HID service is found, otherwise either ATT errors or
226  * ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if no service or report map or report are found.
227  * It also contains the number of individual HIDS Services.
228  *
229  * @param con_handle
230  * @param packet_handler
231  * @param protocol_mode see hid_protocol_mode_t in btstack_hid.h
232  * @param hids_cid (out) to use for other functions
233  * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_COMMAND_DISALLOWED if there is already a client
234  *         associated with con_handle, or BTSTACK_MEMORY_ALLOC_FAILED.
235  */
236 uint8_t hids_client_connect(hci_con_handle_t con_handle, btstack_packet_handler_t packet_handler, hid_protocol_mode_t protocol_mode, uint16_t * hids_cid);
237 
238 /**
239  * @brief Send HID report. Event GATTSERVICE_SUBEVENT_HID_REPORT is emitted with empty report
240  *
241  * @param hids_cid
242  * @param report_id
243  * @param report_type
244  * @param report
245  * @param report_len
246  * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER,
247  * ERROR_CODE_COMMAND_DISALLOWED if client is in wrong state,
248  * ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if no report with given type and ID is found, or
249  * ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE if report length exceeds MTU.
250  */
251 uint8_t hids_client_send_write_report(uint16_t hids_cid, uint8_t report_id, hid_report_type_t report_type, const uint8_t * report, uint8_t report_len);
252 
253 /**
254  * @brief Get HID report. Event GATTSERVICE_SUBEVENT_HID_REPORT is emitted.
255  *
256  * @param hids_cid
257  * @param report_id
258  * @param report_type
259  * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER,
260  * ERROR_CODE_COMMAND_DISALLOWED if client is in wrong state,
261  * ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if no report with given type and ID is found, or
262  * ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE if report length exceeds MTU.
263  */
264 uint8_t hids_client_send_get_report(uint16_t hids_cid, uint8_t report_id, hid_report_type_t report_type);
265 
266 /**
267  * @brief Get HID Information. Event GATTSERVICE_SUBEVENT_HID_INFORMATION is emitted.
268  *
269  * @param hids_cid
270  * @param service_index
271  * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER,
272  * ERROR_CODE_COMMAND_DISALLOWED if client is in wrong state, or
273  * ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if no report with given type and ID is found.
274  */
275 uint8_t hids_client_get_hid_information(uint16_t hids_cid, uint8_t service_index);
276 
277 /**
278  * @brief Get Protocol Mode. Event GATTSERVICE_SUBEVENT_HID_PROTOCOL_MODE is emitted.
279  *
280  * @param hids_cid
281  * @param service_index
282  * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER,
283  * ERROR_CODE_COMMAND_DISALLOWED if client is in wrong state, or
284  * ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if no report with given type and ID is found.
285  */
286 uint8_t hids_client_get_protocol_mode(uint16_t hids_cid, uint8_t service_index);
287 
288 /**
289  * @brief Set Protocol Mode.
290  *
291  * @param hids_cid
292  * @param service_index
293  * @param protocol_mode
294  * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER,
295  * ERROR_CODE_COMMAND_DISALLOWED if client is in wrong state, or
296  * ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if no report with given type and ID is found.
297  */
298 uint8_t hids_client_send_set_protocol_mode(uint16_t hids_cid, uint8_t service_index, hid_protocol_mode_t protocol_mode);
299 
300 /**
301  * @brief Send Suspend to remote HID service.
302  *
303  * @param hids_cid
304  * @param service_index
305  * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER,
306  * ERROR_CODE_COMMAND_DISALLOWED if client is in wrong state, or
307  * ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if no report with given type and ID is found.
308  */
309 uint8_t hids_client_send_suspend(uint16_t hids_cid, uint8_t service_index);
310 
311 /**
312  * @brief Send Exit Suspend to remote HID service.
313  *
314  * @param hids_cid
315  * @param service_index
316  * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER,
317  * ERROR_CODE_COMMAND_DISALLOWED if client is in wrong state, or
318  * ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if no report with given type and ID is found.
319  */
320 uint8_t hids_client_send_exit_suspend(uint16_t hids_cid, uint8_t service_index);
321 
322 /**
323  * @brief Enable all notifications. Event GATTSERVICE_SUBEVENT_HID_SERVICE_REPORTS_NOTIFICATION reports current configuration.
324  *
325  * @param hids_cid
326  * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, or
327  * ERROR_CODE_COMMAND_DISALLOWED if client is in wrong state.
328  */
329 uint8_t hids_client_enable_notifications(uint16_t hids_cid);
330 
331 /**
332  * @brief Disable all notifications. Event GATTSERVICE_SUBEVENT_HID_SERVICE_REPORTS_NOTIFICATION reports current configuration.
333  *
334  * @param hids_cid
335  * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, or
336  * ERROR_CODE_COMMAND_DISALLOWED if client is in wrong state.
337  */
338 uint8_t hids_client_disable_notifications(uint16_t hids_cid);
339 
340 /**
341  * @brief Disconnect from HID Service.
342  *
343  * @param hids_cid
344  * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER
345  */
346 uint8_t hids_client_disconnect(uint16_t hids_cid);
347 
348 /*
349  * @brief Get descriptor data. For services in boot mode without a Report Map, a default HID Descriptor for Keyboard/Mouse is provided.
350  *
351  * @param hid_cid
352  * @return data
353  */
354 const uint8_t * hids_client_descriptor_storage_get_descriptor_data(uint16_t hids_cid, uint8_t service_index);
355 
356 /*
357  * @brief Get descriptor length
358  *
359  * @param hid_cid
360  * @return length
361  */
362 uint16_t hids_client_descriptor_storage_get_descriptor_len(uint16_t hids_cid, uint8_t service_index);
363 
364 /**
365  * @brief De-initialize HID Service Client.
366  *
367  */
368 void hids_client_deinit(void);
369 
370 /* API_END */
371 
372 #if defined __cplusplus
373 }
374 #endif
375 
376 #endif
377