xref: /btstack/src/classic/hid_host.h (revision 98451c7b102094e15e0a72ca2f7098d91aed2017)
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_parser.h"
45 #include "classic/hid.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 hid_descriptor_offset;
101     uint16_t hid_descriptor_len;
102     uint16_t hid_descriptor_max_len;
103     uint8_t  hid_descriptor_status;     // ERROR_CODE_SUCCESS if descriptor available,
104                                         // ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if not, and
105                                         // ERROR_CODE_MEMORY_CAPACITY_EXCEEDED if descriptor is larger then the available space
106 
107     uint8_t   user_request_can_send_now;
108 
109     // get report
110     hid_report_type_t report_type;
111     uint8_t           report_id;
112 
113     // control message, bit mask:
114     // SUSSPEND             1
115     // EXIT_SUSSPEND        2
116     // VIRTUAL_CABLE_UNPLUG 4
117     uint8_t control_tasks;
118 
119     // set report
120     const uint8_t * report;
121     uint16_t  report_len;
122 } hid_host_connection_t;
123 
124 /* API_START */
125 
126 /**
127  * @brief Set up HID Host
128  * @param hid_descriptor_storage
129  * @param hid_descriptor_storage_len
130  */
131 void hid_host_init(uint8_t * hid_descriptor_storage, uint16_t hid_descriptor_storage_len);
132 
133 /**
134  * @brief Register callback for the HID Host.
135  * @param callback
136  */
137 void hid_host_register_packet_handler(btstack_packet_handler_t callback);
138 
139 /*
140  * @brief Create HID connection to HID Device and emit HID_SUBEVENT_CONNECTION_OPENED event with status code,
141  * followed by HID_SUBEVENT_DESCRIPTOR_AVAILABLE that informs if the HID Descriptor was found. In the case of incoming
142  * connection, i.e. HID Device initiating the connection, the HID_SUBEVENT_DESCRIPTOR_AVAILABLE is delayed, and the reports
143  * may already come via HID_SUBEVENT_REPORT event. It is up to the application code if
144  * these reports should be buffered or ignored until the descriptor is available.
145  * @note  HID_PROTOCOL_MODE_REPORT_WITH_FALLBACK_TO_BOOT will try ti set up REPORT mode, but fallback to BOOT mode if necessary.
146  * @note  HID_SUBEVENT_DESCRIPTOR_AVAILABLE possible status values are:
147  * - ERROR_CODE_SUCCESS if descriptor available,
148  * - ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE if not, and
149  * - ERROR_CODE_MEMORY_CAPACITY_EXCEEDED if descriptor is larger then the available space
150  * @param remote_addr
151  * @param protocol_mode see hid_protocol_mode_t in hid.h
152  * @param hid_cid to use for other commands
153  * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_COMMAND_DISALLOWED, BTSTACK_MEMORY_ALLOC_FAILED
154  */
155 uint8_t hid_host_connect(bd_addr_t remote_addr, hid_protocol_mode_t protocol_mode, uint16_t * hid_cid);
156 
157 
158 /*
159  * @brief Accept incoming HID connection, this should be called upon receiving HID_SUBEVENT_INCOMING_CONNECTION event.
160  * @param hid_cid
161  * @param protocol_mode see hid_protocol_mode_t in hid.h
162  * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED
163  */
164 uint8_t hid_host_accept_connection(uint16_t hid_cid, hid_protocol_mode_t protocol_mode);
165 
166 /*
167  * @brief Decline incoming HID connection, this should be called upon receiving HID_SUBEVENT_INCOMING_CONNECTION event.
168  * @param hid_cid
169  * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED
170  */
171 uint8_t hid_host_decline_connection(uint16_t hid_cid);
172 
173 /*
174  * @brief Disconnect from HID Device and emit HID_SUBEVENT_CONNECTION_CLOSED event.
175  * @param hid_cid
176  */
177 void hid_host_disconnect(uint16_t hid_cid);
178 
179 // Control messages:
180 
181 /*
182  * @brief Send SUSPEND control signal to connected HID Device. A Bluetooth HID Device which receives a SUSPEND control signal
183  * may optionally disconnect from the Bluetooth HID Host.
184  * @param hid_cid
185  * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED
186  */
187 uint8_t hid_host_send_suspend(uint16_t hid_cid);
188 
189 /*
190  * @brief Order connected HID Device to exit suspend mode.
191  * The Bluetooth HID Device shall send a report to the Bluetooth HID Host.
192  * @param hid_cid
193  * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED
194  */
195 uint8_t hid_host_send_exit_suspend(uint16_t hid_cid);
196 
197 /*
198  * @brief Unplug connected HID Device.
199  * @note This is the only command that can be also received from HID Device. It will be indicated by receiving
200  * HID_SUBEVENT_VIRTUAL_CABLE_UNPLUG event, as well as disconnecting HID Host from device.
201  * @param hid_cid
202  * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED
203  */
204 uint8_t hid_host_send_virtual_cable_unplug(uint16_t hid_cid);
205 
206 /*
207  * @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
208  * @param hid_cid
209  * @param protocol_mode see hid_protocol_mode_t in hid.h
210  * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED
211  */
212 uint8_t hid_host_send_set_protocol_mode(uint16_t hid_cid, hid_protocol_mode_t protocol_mode);
213 
214 /*
215  * @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
216  * @param hid_cid
217  * @param protocol_mode see hid_protocol_mode_t in hid.h
218  * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED
219  */
220 uint8_t hid_host_send_get_protocol(uint16_t hid_cid);
221 
222 /*
223 
224  * @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.
225  * @param hid_cid
226  * @param report_type see hid_report_type_t in hid.h
227  * @param report_id
228  * @param report
229  * @param report_len
230  * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED
231  */
232 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);
233 
234 /*
235  * @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.
236  * Polling Bluetooth HID Devices using the GET_REPORT transfer is costly in terms of time and overhead,
237  * and should be avoided whenever possible. The GET_REPORT transfer is typically only used by applications
238  * to determine the initial state of a Bluetooth HID Device. If the state of a report changes frequently,
239  * then the report should be reported over the more efficient Interrupt channel, see hid_host_send_report.
240  * @param hid_cid
241  * @param report_type see hid_report_type_t in hid.h
242  * @param report_id
243  * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED
244  */
245 uint8_t hid_host_send_get_report(uint16_t hid_cid, hid_report_type_t report_type, uint8_t report_id);
246 
247 /**
248  * @brief Send HID output report on interrupt channel.
249  * @param hid_cid
250  * @param report_id
251  * @param report
252  * @param report_len
253  * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED
254  */
255 uint8_t hid_host_send_report(uint16_t hid_cid, uint8_t report_id, const uint8_t * report, uint8_t report_len);
256 
257 /*
258  * @brief Get descriptor data
259  * @param hid_cid
260  * @result data
261  */
262 const uint8_t * hid_descriptor_storage_get_descriptor_data(uint16_t hid_cid);
263 
264 /*
265  * @brief Get descriptor length
266  * @param hid_cid
267  * @result length
268  */
269 uint16_t hid_descriptor_storage_get_descriptor_len(uint16_t hid_cid);
270 
271 /* API_END */
272 
273 
274 #if defined __cplusplus
275 }
276 #endif
277 
278 #endif
279