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 "btstack_defines.h" 47 #include "btstack_hid.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 // called if BOOT mode 71 HIDS_CLIENT_STATE_W2_SET_PROTOCOL_MODE_WITHOUT_RESPONSE, 72 73 // for each REPORT_MAP characteristic, read HID Descriptor (Report Map Characteristic Value) 74 HIDS_CLIENT_STATE_W2_READ_REPORT_MAP_HID_DESCRIPTOR, 75 HIDS_CLIENT_STATE_W4_REPORT_MAP_HID_DESCRIPTOR, 76 77 // for REPORT_MAP characteristic, discover descriptor 78 HIDS_CLIENT_STATE_W2_REPORT_MAP_DISCOVER_CHARACTERISTIC_DESCRIPTORS, 79 HIDS_CLIENT_STATE_W4_REPORT_MAP_CHARACTERISTIC_DESCRIPTORS_RESULT, 80 81 // for REPORT_MAP characteristic, read External Report Reference Characteristic Descriptor 82 HIDS_CLIENT_STATE_W2_REPORT_MAP_READ_EXTERNAL_REPORT_REFERENCE_UUID, 83 HIDS_CLIENT_STATE_W4_REPORT_MAP_EXTERNAL_REPORT_REFERENCE_UUID, 84 85 // for every external report reference uuid, discover external Report characteristic 86 HIDS_CLIENT_STATE_W2_DISCOVER_EXTERNAL_REPORT_CHARACTERISTIC, 87 HIDS_CLIENT_STATE_W4_EXTERNAL_REPORT_CHARACTERISTIC_RESULT, 88 89 // for each Report characteristics, discover Report characteristic descriptor 90 HIDS_CLIENT_STATE_W2_FIND_REPORT, 91 HIDS_CLIENT_STATE_W4_REPORT_FOUND, 92 93 // then read value of Report characteristic descriptor to get report ID and type 94 HIDS_CLIENT_STATE_W2_READ_REPORT_ID_AND_TYPE, 95 HIDS_CLIENT_STATE_W4_REPORT_ID_AND_TYPE, 96 97 HIDS_CLIENT_STATE_W2_ENABLE_INPUT_REPORTS, 98 HIDS_CLIENT_STATE_W4_INPUT_REPORTS_ENABLED, 99 100 HIDS_CLIENT_STATE_CONNECTED, 101 102 HIDS_CLIENT_W2_SEND_WRITE_REPORT, 103 HIDS_CLIENT_W4_WRITE_REPORT_DONE, 104 105 HIDS_CLIENT_W2_SEND_GET_REPORT, 106 HIDS_CLIENT_W4_GET_REPORT_RESULT, 107 108 HIDS_CLIENT_W2_READ_VALUE_OF_CHARACTERISTIC, 109 HIDS_CLIENT_W4_VALUE_OF_CHARACTERISTIC_RESULT, 110 111 HIDS_CLIENT_W2_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE, 112 113 HIDS_CLIENT_STATE_W2_CONFIGURE_NOTIFICATIONS, 114 HIDS_CLIENT_STATE_W4_NOTIFICATIONS_CONFIGURED, 115 116 #ifdef ENABLE_TESTING_SUPPORT 117 HIDS_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION, 118 HIDS_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT, 119 #endif 120 121 } hid_service_client_state_t; 122 123 124 typedef struct { 125 // GATT cache 126 127 //reuse as descriptor_handle 128 uint16_t value_handle; 129 uint16_t end_handle; 130 uint16_t properties; 131 132 // UUID of external Report characteristic, stored in Report Map descriptor EXTERNAL_REPORT_REFERENCE 133 uint16_t external_report_reference_uuid; 134 135 #ifdef ENABLE_TESTING_SUPPORT 136 uint16_t ccc_handle; 137 #endif 138 139 // service mapping 140 uint8_t service_index; 141 uint8_t report_id; 142 hid_report_type_t report_type; 143 uint8_t boot_report; 144 gatt_client_notification_t notification_listener; 145 } hids_client_report_t; 146 147 typedef struct { 148 hid_protocol_mode_t protocol_mode; 149 150 uint16_t start_handle; 151 uint16_t end_handle; 152 153 uint16_t report_map_value_handle; 154 uint16_t report_map_end_handle; 155 156 uint16_t hid_information_value_handle; 157 uint16_t control_point_value_handle; 158 uint16_t protocol_mode_value_handle; 159 160 // descriptor storage 161 uint16_t hid_descriptor_offset; 162 uint16_t hid_descriptor_len; 163 uint16_t hid_descriptor_max_len; 164 uint8_t hid_descriptor_status; // ERROR_CODE_SUCCESS if descriptor available, 165 // ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if not, and 166 // ERROR_CODE_MEMORY_CAPACITY_EXCEEDED if descriptor is larger then the available space 167 } hid_service_t; 168 169 typedef struct { 170 btstack_linked_item_t item; 171 172 hci_con_handle_t con_handle; 173 uint16_t cid; 174 175 hid_service_client_state_t state; 176 btstack_packet_handler_t client_handler; 177 178 uint8_t num_instances; 179 hid_service_t services[MAX_NUM_HID_SERVICES]; 180 181 // used for discovering characteristics 182 uint8_t service_index; 183 hid_protocol_mode_t required_protocol_mode; 184 185 // send report 186 hids_client_report_t reports[HIDS_CLIENT_NUM_REPORTS]; 187 uint8_t num_reports; 188 189 hids_client_report_t external_reports[HIDS_CLIENT_NUM_REPORTS]; 190 uint8_t num_external_reports; 191 192 // index used for report and report map search 193 uint8_t report_index; 194 uint16_t report_len; 195 const uint8_t * report; 196 // used to write control_point and protocol_mode 197 uint16_t handle; 198 uint8_t value; 199 } hids_client_t; 200 201 /* API_START */ 202 203 /** 204 * @brief Initialize HID Service Client. The HID Descriptor storage is shared between all connections. 205 * 206 * @param hid_descriptor_storage 207 * @param hid_descriptor_storage_len 208 */ 209 void hids_client_init(uint8_t * hid_descriptor_storage, uint16_t hid_descriptor_storage_len); 210 211 /* @brief Connect to HID Services of remote device. Event GATTSERVICE_SUBEVENT_HID_SERVICE_CONNECTED will be emitted 212 * after all remote HID services and characteristics are found, and notifications for all input reports are enabled. 213 * Status code can be ERROR_CODE_SUCCES if at least one HID service is found, otherwise either ATT errors or 214 * ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if no service or report map or report are found. 215 * It also contains the number of individual HIDS Services. 216 * 217 * @param con_handle 218 * @param packet_handler 219 * @param protocol_mode see hid_protocol_mode_t in btstack_hid.h 220 * @param hids_cid (out) to use for other functions 221 * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_COMMAND_DISALLOWED if there is already a client 222 * associated with con_handle, or BTSTACK_MEMORY_ALLOC_FAILED. 223 */ 224 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); 225 226 /** 227 * @brief Send HID report. 228 * 229 * @param hids_cid 230 * @param report_id 231 * @param report_type 232 * @param report 233 * @param report_len 234 * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, 235 * ERROR_CODE_COMMAND_DISALLOWED if client is in wrong state, 236 * ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if no report with given type and ID is found, or 237 * ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE if report length exceeds MTU. 238 */ 239 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); 240 241 /** 242 * @brief Get HID report. Event GATTSERVICE_SUBEVENT_HID_REPORT is emitted. 243 * 244 * @param hids_cid 245 * @param report_id 246 * @param report_type 247 * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, 248 * ERROR_CODE_COMMAND_DISALLOWED if client is in wrong state, 249 * ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if no report with given type and ID is found, or 250 * ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE if report length exceeds MTU. 251 */ 252 uint8_t hids_client_send_get_report(uint16_t hids_cid, uint8_t report_id, hid_report_type_t report_type); 253 254 /** 255 * @brief Get HID Information. Event GATTSERVICE_SUBEVENT_HID_INFORMATION is emitted. 256 * 257 * @param hids_cid 258 * @param service_index 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, or 261 * ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if no report with given type and ID is found. 262 */ 263 uint8_t hids_client_get_hid_information(uint16_t hids_cid, uint8_t service_index); 264 265 /** 266 * @brief Get Protocol Mode. Event GATTSERVICE_SUBEVENT_HID_PROTOCOL_MODE is emitted. 267 * 268 * @param hids_cid 269 * @param service_index 270 * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, 271 * ERROR_CODE_COMMAND_DISALLOWED if client is in wrong state, or 272 * ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if no report with given type and ID is found. 273 */ 274 uint8_t hids_client_get_protocol_mode(uint16_t hids_cid, uint8_t service_index); 275 276 /** 277 * @brief Set Protocol Mode. 278 * 279 * @param hids_cid 280 * @param service_index 281 * @param protocol_mode 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_send_set_protocol_mode(uint16_t hids_cid, uint8_t service_index, hid_protocol_mode_t protocol_mode); 287 288 /** 289 * @brief Send Suspend to remote HID service. 290 * 291 * @param hids_cid 292 * @param service_index 293 * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, 294 * ERROR_CODE_COMMAND_DISALLOWED if client is in wrong state, or 295 * ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if no report with given type and ID is found. 296 */ 297 uint8_t hids_client_send_suspend(uint16_t hids_cid, uint8_t service_index); 298 299 /** 300 * @brief Send Exit Suspend to remote HID service. 301 * 302 * @param hids_cid 303 * @param service_index 304 * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, 305 * ERROR_CODE_COMMAND_DISALLOWED if client is in wrong state, or 306 * ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if no report with given type and ID is found. 307 */ 308 uint8_t hids_client_send_exit_suspend(uint16_t hids_cid, uint8_t service_index); 309 310 /** 311 * @brief Enable all notifications. Event GATTSERVICE_SUBEVENT_HID_SERVICE_REPORTS_NOTIFICATION reports current configuration. 312 * 313 * @param hids_cid 314 * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, or 315 * ERROR_CODE_COMMAND_DISALLOWED if client is in wrong state. 316 */ 317 uint8_t hids_client_enable_notifications(uint16_t hids_cid); 318 319 /** 320 * @brief Disable all notifications. Event GATTSERVICE_SUBEVENT_HID_SERVICE_REPORTS_NOTIFICATION reports current configuration. 321 * 322 * @param hids_cid 323 * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, or 324 * ERROR_CODE_COMMAND_DISALLOWED if client is in wrong state. 325 */ 326 uint8_t hids_client_disable_notifications(uint16_t hids_cid); 327 328 /** 329 * @brief Disconnect from HID Service. 330 * 331 * @param hids_cid 332 * @return status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER 333 */ 334 uint8_t hids_client_disconnect(uint16_t hids_cid); 335 336 /* 337 * @brief Get descriptor data. For services in boot mode without a Report Map, a default HID Descriptor for Keyboard/Mouse is provided. 338 * 339 * @param hid_cid 340 * @return data 341 */ 342 const uint8_t * hids_client_descriptor_storage_get_descriptor_data(uint16_t hids_cid, uint8_t service_index); 343 344 /* 345 * @brief Get descriptor length 346 * 347 * @param hid_cid 348 * @return length 349 */ 350 uint16_t hids_client_descriptor_storage_get_descriptor_len(uint16_t hids_cid, uint8_t service_index); 351 352 /** 353 * @brief De-initialize HID Service Client. 354 * 355 */ 356 void hids_client_deinit(void); 357 358 /* API_END */ 359 360 #if defined __cplusplus 361 } 362 #endif 363 364 #endif 365