1 /* 2 * Copyright (C) 2020 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 HID_HOST_H 39 #define HID_HOST_H 40 41 #include <stdint.h> 42 #include "btstack_defines.h" 43 #include "bluetooth.h" 44 #include "btstack_hid.h" 45 #include "btstack_hid_parser.h" 46 47 #if defined __cplusplus 48 extern "C" { 49 #endif 50 51 52 typedef enum { 53 HID_HOST_IDLE, 54 HID_HOST_W2_SEND_SDP_QUERY, 55 HID_HOST_W4_SDP_QUERY_RESULT, 56 57 HID_HOST_W4_CONTROL_CONNECTION_ESTABLISHED, 58 HID_HOST_CONTROL_CONNECTION_ESTABLISHED, 59 60 HID_HOST_W4_SET_BOOT_MODE, 61 HID_HOST_W4_INCOMING_INTERRUPT_CONNECTION, 62 HID_HOST_W4_INTERRUPT_CONNECTION_ESTABLISHED, 63 HID_HOST_CONNECTION_ESTABLISHED, 64 65 HID_HOST_W2_SEND_GET_REPORT, 66 HID_HOST_W4_GET_REPORT_RESPONSE, 67 HID_HOST_W2_SEND_SET_REPORT, 68 HID_HOST_W4_SET_REPORT_RESPONSE, 69 HID_HOST_W2_SEND_GET_PROTOCOL, 70 HID_HOST_W4_GET_PROTOCOL_RESPONSE, 71 72 HID_HOST_W2_SEND_REPORT, 73 HID_HOST_W4_SEND_REPORT_RESPONSE, 74 75 HID_HOST_W4_INTERRUPT_CONNECTION_DISCONNECTED, 76 HID_HOST_W4_CONTROL_CONNECTION_DISCONNECTED 77 } hid_host_state_t; 78 79 typedef struct { 80 btstack_linked_item_t item; 81 82 uint16_t hid_cid; 83 hci_con_handle_t con_handle; 84 85 bd_addr_t remote_addr; 86 87 uint16_t control_cid; 88 uint16_t control_psm; 89 uint16_t interrupt_cid; 90 uint16_t interrupt_psm; 91 92 hid_host_state_t state; 93 bool incoming; 94 hid_protocol_mode_t protocol_mode; 95 96 bool set_protocol; 97 bool w4_set_protocol_response; 98 hid_protocol_mode_t requested_protocol_mode; 99 100 uint16_t host_max_latency; 101 uint16_t host_min_timeout; 102 103 uint16_t hid_descriptor_offset; 104 uint16_t hid_descriptor_len; 105 uint16_t hid_descriptor_max_len; 106 uint8_t hid_descriptor_status; // ERROR_CODE_SUCCESS if descriptor available, 107 // ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if not, and 108 // ERROR_CODE_MEMORY_CAPACITY_EXCEEDED if descriptor is larger then the available space 109 110 uint8_t user_request_can_send_now; 111 112 // get report 113 hid_report_type_t report_type; 114 uint8_t report_id; 115 116 // control message, bit mask: 117 // SUSSPEND 1 118 // EXIT_SUSSPEND 2 119 // VIRTUAL_CABLE_UNPLUG 4 120 uint8_t control_tasks; 121 122 // set report 123 const uint8_t * report; 124 uint16_t report_len; 125 } hid_host_connection_t; 126 127 /* API_START */ 128 129 /** 130 * @brief Set up HID Host 131 * @param hid_descriptor_storage 132 * @param hid_descriptor_storage_len 133 */ 134 void hid_host_init(uint8_t * hid_descriptor_storage, uint16_t hid_descriptor_storage_len); 135 136 /** 137 * @brief Register callback for the HID Host. 138 * @param callback 139 */ 140 void hid_host_register_packet_handler(btstack_packet_handler_t callback); 141 142 /* 143 * @brief Create HID connection to HID Device and emit HID_SUBEVENT_CONNECTION_OPENED event with status code, 144 * followed by HID_SUBEVENT_DESCRIPTOR_AVAILABLE that informs if the HID Descriptor was found. In the case of incoming 145 * connection, i.e. HID Device initiating the connection, the HID_SUBEVENT_DESCRIPTOR_AVAILABLE is delayed, and the reports 146 * may already come via HID_SUBEVENT_REPORT event. It is up to the application code if 147 * these reports should be buffered or ignored until the descriptor is available. 148 * @note HID_PROTOCOL_MODE_REPORT_WITH_FALLBACK_TO_BOOT will try ti set up REPORT mode, but fallback to BOOT mode if necessary. 149 * @note HID_SUBEVENT_DESCRIPTOR_AVAILABLE possible status values are: 150 * - ERROR_CODE_SUCCESS if descriptor available, 151 * - ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if not, and 152 * - ERROR_CODE_MEMORY_CAPACITY_EXCEEDED if descriptor is larger then the available space 153 * @param remote_addr 154 * @param protocol_mode see hid_protocol_mode_t in btstack_hid.h 155 * @param hid_cid to use for other commands 156 * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_COMMAND_DISALLOWED, BTSTACK_MEMORY_ALLOC_FAILED 157 */ 158 uint8_t hid_host_connect(bd_addr_t remote_addr, hid_protocol_mode_t protocol_mode, uint16_t * hid_cid); 159 160 161 /* 162 * @brief Accept incoming HID connection, this should be called upon receiving HID_SUBEVENT_INCOMING_CONNECTION event. 163 * @param hid_cid 164 * @param protocol_mode see hid_protocol_mode_t in btstack_hid.h 165 * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED 166 */ 167 uint8_t hid_host_accept_connection(uint16_t hid_cid, hid_protocol_mode_t protocol_mode); 168 169 /* 170 * @brief Decline incoming HID connection, this should be called upon receiving HID_SUBEVENT_INCOMING_CONNECTION event. 171 * @param hid_cid 172 * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED 173 */ 174 uint8_t hid_host_decline_connection(uint16_t hid_cid); 175 176 /* 177 * @brief Disconnect from HID Device and emit HID_SUBEVENT_CONNECTION_CLOSED event. 178 * @param hid_cid 179 */ 180 void hid_host_disconnect(uint16_t hid_cid); 181 182 // Control messages: 183 184 /* 185 * @brief Send SUSPEND control signal to connected HID Device. A Bluetooth HID Device which receives a SUSPEND control signal 186 * may optionally disconnect from the Bluetooth HID Host. 187 * @param hid_cid 188 * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED 189 */ 190 uint8_t hid_host_send_suspend(uint16_t hid_cid); 191 192 /* 193 * @brief Order connected HID Device to exit suspend mode. 194 * The Bluetooth HID Device shall send a report to the Bluetooth HID Host. 195 * @param hid_cid 196 * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED 197 */ 198 uint8_t hid_host_send_exit_suspend(uint16_t hid_cid); 199 200 /* 201 * @brief Unplug connected HID Device. 202 * @note This is the only command that can be also received from HID Device. It will be indicated by receiving 203 * HID_SUBEVENT_VIRTUAL_CABLE_UNPLUG event, as well as disconnecting HID Host from device. 204 * @param hid_cid 205 * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED 206 */ 207 uint8_t hid_host_send_virtual_cable_unplug(uint16_t hid_cid); 208 209 /* 210 * @brief Set Protocol Mode on the Bluetooth HID Device and emit HID_SUBEVENT_SET_PROTOCOL_RESPONSE event with handshake_status, see hid_handshake_param_type_t 211 * @param hid_cid 212 * @param protocol_mode see hid_protocol_mode_t in btstack_hid.h 213 * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED 214 */ 215 uint8_t hid_host_send_set_protocol_mode(uint16_t hid_cid, hid_protocol_mode_t protocol_mode); 216 217 /* 218 * @brief Retrieve the Protocol Mode of the Bluetooth HID Device and emit HID_SUBEVENT_GET_PROTOCOL_RESPONSE with handshake_status, see hid_handshake_param_type_t 219 * @param hid_cid 220 * @param protocol_mode see hid_protocol_mode_t in btstack_hid.h 221 * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED 222 */ 223 uint8_t hid_host_send_get_protocol(uint16_t hid_cid); 224 225 /* 226 227 * @brief Send report to a Bluetooth HID Device and emit HID_SUBEVENT_SET_REPORT_RESPONSE with handshake_status, see hid_handshake_param_type_t. The Bluetooth HID Host shall send complete reports. 228 * @param hid_cid 229 * @param report_type see hid_report_type_t in btstack_hid.h 230 * @param report_id 231 * @param report 232 * @param report_len 233 * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED 234 */ 235 uint8_t hid_host_send_set_report(uint16_t hid_cid, hid_report_type_t report_type, uint8_t report_id, const uint8_t * report, uint8_t report_len); 236 237 /* 238 * @brief Request a HID report from the Bluetooth HID Device and emit HID_SUBEVENT_GET_REPORT_RESPONSE event with with handshake_status, see hid_handshake_param_type_t. 239 * Polling Bluetooth HID Devices using the GET_REPORT transfer is costly in terms of time and overhead, 240 * and should be avoided whenever possible. The GET_REPORT transfer is typically only used by applications 241 * to determine the initial state of a Bluetooth HID Device. If the state of a report changes frequently, 242 * then the report should be reported over the more efficient Interrupt channel, see hid_host_send_report. 243 * @param hid_cid 244 * @param report_type see hid_report_type_t in btstack_hid.h 245 * @param report_id 246 * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED 247 */ 248 uint8_t hid_host_send_get_report(uint16_t hid_cid, hid_report_type_t report_type, uint8_t report_id); 249 250 /** 251 * @brief Send HID output report on interrupt channel. 252 * @param hid_cid 253 * @param report_id 254 * @param report 255 * @param report_len 256 * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED 257 */ 258 uint8_t hid_host_send_report(uint16_t hid_cid, uint8_t report_id, const uint8_t * report, uint8_t report_len); 259 260 /* 261 * @brief Get descriptor data 262 * @param hid_cid 263 * @result data 264 */ 265 const uint8_t * hid_descriptor_storage_get_descriptor_data(uint16_t hid_cid); 266 267 /* 268 * @brief Get descriptor length 269 * @param hid_cid 270 * @result length 271 */ 272 uint16_t hid_descriptor_storage_get_descriptor_len(uint16_t hid_cid); 273 274 /* API_END */ 275 276 277 #if defined __cplusplus 278 } 279 #endif 280 281 #endif 282