xref: /btstack/src/ble/gatt-service/hids_client.h (revision af2241c2c01b038fbeb91dad56ed2c7b1d3b8df8)
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 MATTHIAS
24  * RINGWALD 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 #ifndef HIDS_CLIENT_H
39 #define HIDS_CLIENT_H
40 
41 #if defined __cplusplus
42 extern "C" {
43 #endif
44 
45 #include <stdint.h>
46 #include "hid.h"
47 #include "btstack_defines.h"
48 #include "bluetooth.h"
49 #include "btstack_linked_list.h"
50 #include "ble/gatt_client.h"
51 
52 #ifndef MAX_NUM_HID_SERVICES
53 #define MAX_NUM_HID_SERVICES 3
54 #endif
55 #define HIDS_CLIENT_INVALID_REPORT_INDEX 0xFF
56 
57 #define HIDS_CLIENT_NUM_REPORTS 15
58 
59 typedef enum {
60     HIDS_CLIENT_STATE_IDLE,
61 
62     // get all HID services
63     HIDS_CLIENT_STATE_W2_QUERY_SERVICE,
64     HIDS_CLIENT_STATE_W4_SERVICE_RESULT,
65 
66     // for each service, discover all characteristics
67     HIDS_CLIENT_STATE_W2_QUERY_CHARACTERISTIC,
68     HIDS_CLIENT_STATE_W4_CHARACTERISTIC_RESULT,
69 
70     HIDS_CLIENT_STATE_W2_SET_BOOT_PROTOCOL_MODE,
71 
72     // for each REPORT_MAP characteristic, read HID Descriptor (Report Map Characteristic Value)
73     HIDS_CLIENT_STATE_W2_READ_REPORT_MAP_HID_DESCRIPTOR,
74     HIDS_CLIENT_STATE_W4_REPORT_MAP_HID_DESCRIPTOR,
75 
76     // for REPORT_MAP characteristic, discover descriptor
77     HIDS_CLIENT_STATE_W2_REPORT_MAP_DISCOVER_CHARACTERISTIC_DESCRIPTORS,
78     HIDS_CLIENT_STATE_W4_REPORT_MAP_CHARACTERISTIC_DESCRIPTORS_RESULT,
79 
80     // for REPORT_MAP characteristic, read External Report Reference Characteristic Descriptor
81     HIDS_CLIENT_STATE_W2_REPORT_MAP_READ_EXTERNAL_REPORT_REFERENCE_UUID,
82     HIDS_CLIENT_STATE_W4_REPORT_MAP_EXTERNAL_REPORT_REFERENCE_UUID,
83 
84     // for every external report reference uuid, discover external Report characteristic
85     HIDS_CLIENT_STATE_W2_DISCOVER_EXTERNAL_REPORT_CHARACTERISTIC,
86     HIDS_CLIENT_STATE_W4_EXTERNAL_REPORT_CHARACTERISTIC_RESULT,
87 
88     // for each Report characteristics, discover Report characteristic descriptor
89     HIDS_CLIENT_STATE_W2_FIND_REPORT,
90     HIDS_CLIENT_STATE_W4_REPORT_FOUND,
91 
92     // then read value of Report characteristic descriptor to get report ID and type
93     HIDS_CLIENT_STATE_W2_READ_REPORT_ID_AND_TYPE,
94     HIDS_CLIENT_STATE_W4_REPORT_ID_AND_TYPE,
95 
96     HIDS_CLIENT_STATE_W2_ENABLE_INPUT_REPORTS,
97     HIDS_CLIENT_STATE_W4_INPUT_REPORTS_ENABLED,
98 
99     HIDS_CLIENT_STATE_CONNECTED,
100     HIDS_CLIENT_W2_SEND_REPORT,
101 
102     HIDS_CLIENT_W2_SEND_GET_REPORT,
103     HIDS_CLIENT_W4_GET_REPORT_RESULT,
104 
105     HIDS_CLIENT_W2_READ_VALUE_OF_CHARACTERISTIC,
106     HIDS_CLIENT_W4_VALUE_OF_CHARACTERISTIC_RESULT,
107 
108 #ifdef ENABLE_TESTING_SUPPORT
109     HIDS_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION,
110     HIDS_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT,
111 #endif
112 
113 } hid_service_client_state_t;
114 
115 
116 typedef struct {
117     // GATT cache
118 
119     //reuse as descriptor_handle
120     uint16_t value_handle;
121     uint16_t end_handle;
122     uint16_t properties;
123 
124     // UUID of external Report characteristic, stored in Report Map descriptor EXTERNAL_REPORT_REFERENCE
125     uint16_t external_report_reference_uuid;
126 
127 #ifdef ENABLE_TESTING_SUPPORT
128     uint16_t ccc_handle;
129 #endif
130 
131     // service mapping
132     uint8_t service_index;
133     uint8_t report_id;
134     hid_report_type_t report_type;
135     uint8_t boot_report;
136     gatt_client_notification_t notification_listener;
137 } hids_client_report_t;
138 
139 typedef struct {
140     uint16_t start_handle;
141     uint16_t end_handle;
142 
143     uint16_t report_map_value_handle;
144     uint16_t report_map_end_handle;
145 
146     uint16_t hid_information_value_handle;
147     uint16_t control_point_value_handle;
148     uint16_t protocol_mode_value_handle;
149 
150     // descriptor storage
151     uint16_t hid_descriptor_offset;
152     uint16_t hid_descriptor_len;
153     uint16_t hid_descriptor_max_len;
154     uint8_t  hid_descriptor_status;     // ERROR_CODE_SUCCESS if descriptor available,
155                                         // ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if not, and
156                                         // ERROR_CODE_MEMORY_CAPACITY_EXCEEDED if descriptor is larger then the available space
157 } hid_service_t;
158 
159 typedef struct {
160     btstack_linked_item_t item;
161 
162     hci_con_handle_t  con_handle;
163     uint16_t          cid;
164     hid_protocol_mode_t protocol_mode;
165     hid_service_client_state_t state;
166     btstack_packet_handler_t   client_handler;
167 
168     uint8_t num_instances;
169     hid_service_t services[MAX_NUM_HID_SERVICES];
170 
171     // used for discovering characteristics
172     uint8_t service_index;
173     hid_protocol_mode_t required_protocol_mode;
174 
175     uint16_t protocol_mode_value_handle;
176 
177     // send report
178     hids_client_report_t reports[HIDS_CLIENT_NUM_REPORTS];
179     uint8_t num_reports;
180 
181     hids_client_report_t external_reports[HIDS_CLIENT_NUM_REPORTS];
182     uint8_t num_external_reports;
183 
184     // index used for report and report map search
185     uint8_t   report_index;
186     uint16_t  handle;
187     uint16_t  report_len;
188     const uint8_t * report;
189 
190 } hids_client_t;
191 
192 /* API_START */
193 
194 /**
195  * @brief Initialize Battery Service.
196  * @param hid_descriptor_storage
197  * @param hid_descriptor_storage_len
198  */
199 void hids_client_init(uint8_t * hid_descriptor_storage, uint16_t hid_descriptor_storage_len);
200 
201 /* @brief Connect to HID Services of remote device.
202  *
203  * @param con_handle
204  * @param packet_handler
205  * @param protocol_mode see hid_protocol_mode_t in hid.h
206  * @param hids_cid
207  * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_COMMAND_DISALLOWED if there is already a client associated with con_handle, or BTSTACK_MEMORY_ALLOC_FAILED
208  */
209 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);
210 
211 /**
212  * @brief Send HID output report.
213  * @param hids_cid
214  * @param report_id
215  * @param report
216  * @param report_len
217  * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED
218  */
219 uint8_t hids_client_send_report(uint16_t hids_cid, uint8_t report_id, const uint8_t * report, uint8_t report_len);
220 
221 uint8_t hids_client_send_get_report(uint16_t hids_cid, uint8_t report_id);
222 
223 uint8_t hids_client_get_hid_information(uint16_t hids_cid, uint8_t service_index);
224 
225 uint8_t hids_client_get_protocol_mode(uint16_t hids_cid, uint8_t service_index);
226 
227 /**
228  * @brief Disconnect from Battery Service.
229  * @param hids_cid
230  * @return status
231  */
232 uint8_t hids_client_disconnect(uint16_t hids_cid);
233 
234 /*
235  * @brief Get descriptor data
236  * @param hid_cid
237  * @result data
238  */
239 const uint8_t * hids_client_descriptor_storage_get_descriptor_data(uint16_t hids_cid, uint8_t service_index);
240 
241 /*
242  * @brief Get descriptor length
243  * @param hid_cid
244  * @result length
245  */
246 uint16_t hids_client_descriptor_storage_get_descriptor_len(uint16_t hids_cid, uint8_t service_index);
247 
248 /**
249  * @brief De-initialize Battery Service.
250  */
251 void hids_client_deinit(void);
252 
253 /* API_END */
254 
255 #if defined __cplusplus
256 }
257 #endif
258 
259 #endif
260