xref: /btstack/src/classic/hid_device.h (revision 662cddc29ed3a05cbca695da2cc5e792ead69785)
1d40c9ac6SMatthias Ringwald /*
2d40c9ac6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3d40c9ac6SMatthias Ringwald  *
4d40c9ac6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5d40c9ac6SMatthias Ringwald  * modification, are permitted provided that the following conditions
6d40c9ac6SMatthias Ringwald  * are met:
7d40c9ac6SMatthias Ringwald  *
8d40c9ac6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9d40c9ac6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10d40c9ac6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11d40c9ac6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12d40c9ac6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13d40c9ac6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14d40c9ac6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15d40c9ac6SMatthias Ringwald  *    from this software without specific prior written permission.
16d40c9ac6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17d40c9ac6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18d40c9ac6SMatthias Ringwald  *    monetary gain.
19d40c9ac6SMatthias Ringwald  *
20d40c9ac6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21d40c9ac6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22d40c9ac6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23d40c9ac6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24d40c9ac6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25d40c9ac6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26d40c9ac6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27d40c9ac6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28d40c9ac6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29d40c9ac6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30d40c9ac6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31d40c9ac6SMatthias Ringwald  * SUCH DAMAGE.
32d40c9ac6SMatthias Ringwald  *
33d40c9ac6SMatthias Ringwald  * Please inquire about commercial licensing options at
34d40c9ac6SMatthias Ringwald  * [email protected]
35d40c9ac6SMatthias Ringwald  *
36d40c9ac6SMatthias Ringwald  */
37d40c9ac6SMatthias Ringwald 
38fcaf12e7SMilanka Ringwald #ifndef __HID_DEVICE_H
39fcaf12e7SMilanka Ringwald #define __HID_DEVICE_H
40fe9125ceSMilanka Ringwald 
41d40c9ac6SMatthias Ringwald #include <stdint.h>
428eb8d463SMatthias Ringwald #include "btstack_defines.h"
4357c643eeSMatthias Ringwald #include "bluetooth.h"
44*662cddc2SMilanka Ringwald #include "btstack_hid_parser.h"
4557c643eeSMatthias Ringwald 
46fe9125ceSMilanka Ringwald #if defined __cplusplus
47fe9125ceSMilanka Ringwald extern "C" {
48fe9125ceSMilanka Ringwald #endif
49fe9125ceSMilanka Ringwald 
50fcaf12e7SMilanka Ringwald /* API_START */
51fcaf12e7SMilanka Ringwald 
527d26fe66SMilanka Ringwald #define HID_BOOT_MODE_KEYBOARD_ID 1
537d26fe66SMilanka Ringwald #define HID_BOOT_MODE_MOUSE_ID    2
547d26fe66SMilanka Ringwald 
55fcaf12e7SMilanka Ringwald typedef enum {
56fcaf12e7SMilanka Ringwald     HID_MESSAGE_TYPE_HANDSHAKE = 0,
57fcaf12e7SMilanka Ringwald     HID_MESSAGE_TYPE_HID_CONTROL,
58fcaf12e7SMilanka Ringwald     HID_MESSAGE_TYPE_RESERVED_2,
59fcaf12e7SMilanka Ringwald     HID_MESSAGE_TYPE_RESERVED_3,
60fcaf12e7SMilanka Ringwald     HID_MESSAGE_TYPE_GET_REPORT,
61fcaf12e7SMilanka Ringwald     HID_MESSAGE_TYPE_SET_REPORT,
62fcaf12e7SMilanka Ringwald     HID_MESSAGE_TYPE_GET_PROTOCOL,
63fcaf12e7SMilanka Ringwald     HID_MESSAGE_TYPE_SET_PROTOCOL,
64fcaf12e7SMilanka Ringwald     HID_MESSAGE_TYPE_GET_IDLE_DEPRECATED,
65fcaf12e7SMilanka Ringwald     HID_MESSAGE_TYPE_SET_IDLE_DEPRECATED,
66fcaf12e7SMilanka Ringwald     HID_MESSAGE_TYPE_DATA,
67fcaf12e7SMilanka Ringwald     HID_MESSAGE_TYPE_DATC_DEPRECATED
68fcaf12e7SMilanka Ringwald } hid_message_type_t;
69fcaf12e7SMilanka Ringwald 
70fcaf12e7SMilanka Ringwald typedef enum {
71fcaf12e7SMilanka Ringwald     HID_HANDSHAKE_PARAM_TYPE_SUCCESSFUL = 0x00,        // This code is used to acknowledge requests. A device that has correctly received SET_REPORT, SET_IDLE or SET_PROTOCOL payload transmits an acknowledgment to the host.
72fcaf12e7SMilanka Ringwald     HID_HANDSHAKE_PARAM_TYPE_NOT_READY,                // This code indicates that a device is too busy to accept data. The Bluetooth HID Host should retransmit the data the next time it communicates with the device.
73fcaf12e7SMilanka Ringwald     HID_HANDSHAKE_PARAM_TYPE_ERR_INVALID_REPORT_ID,    // Invalid report ID transmitted.
74fcaf12e7SMilanka Ringwald     HID_HANDSHAKE_PARAM_TYPE_ERR_UNSUPPORTED_REQUEST,  // The device does not support the request. This result code shall be used if the HIDP message type is unsupported.
75fcaf12e7SMilanka Ringwald     HID_HANDSHAKE_PARAM_TYPE_ERR_INVALID_PARAMETER,    // A parameter value is out of range or inappropriate for the request.
76fcaf12e7SMilanka Ringwald     HID_HANDSHAKE_PARAM_TYPE_ERR_UNKNOWN = 0x0E        // Device could not identify the error condition. 0xF = ERR_FATAL. Restart is essential to resume functionality.
77fcaf12e7SMilanka Ringwald } hid_handshake_param_type_t;
78fcaf12e7SMilanka Ringwald 
79fcaf12e7SMilanka Ringwald typedef enum {
806510739bSMilanka Ringwald     HID_CONTROL_PARAM_NOP_DEPRECATED = 0,              // Deprecated: No Operation.
816510739bSMilanka Ringwald     HID_CONTROL_PARAM_HARD_RESET_DEPRECATED,           // Deprecated: Device performs Power On System Test (POST) then initializes all internal variables and initiates normal operations.
826510739bSMilanka Ringwald     HID_CONTROL_PARAM_SOFT_RESET_DEPRECATED,           // Deprecated: Device initializes all internal variables and initiates normal operations.
836510739bSMilanka Ringwald     HID_CONTROL_PARAM_SUSPEND = 0x03,                  // Go to reduced power mode.
846510739bSMilanka Ringwald     HID_CONTROL_PARAM_EXIT_SUSPEND,                    // Exit reduced power mode.
856510739bSMilanka Ringwald     HID_CONTROL_PARAM_VIRTUAL_CABLE_UNPLUG
86fcaf12e7SMilanka Ringwald } hid_control_param_t;
87fcaf12e7SMilanka Ringwald 
88fcaf12e7SMilanka Ringwald typedef enum {
89ba9a58feSMilanka Ringwald     HID_PROTOCOL_MODE_BOOT = 0,
90ba9a58feSMilanka Ringwald     HID_PROTOCOL_MODE_REPORT
91ba9a58feSMilanka Ringwald } hid_protocol_mode_t;
92ba9a58feSMilanka Ringwald 
93a78157ddSMilanka Ringwald typedef enum {
94a78157ddSMilanka Ringwald     HID_REPORT_ID_UNDECLARED,
95a78157ddSMilanka Ringwald     HID_REPORT_ID_VALID,
96a78157ddSMilanka Ringwald     HID_REPORT_ID_INVALID
97a78157ddSMilanka Ringwald } hid_report_id_status_t;
98fcaf12e7SMilanka Ringwald 
99d40c9ac6SMatthias Ringwald /**
100d40c9ac6SMatthias Ringwald  * @brief Create HID Device SDP service record.
101d40c9ac6SMatthias Ringwald  * @param service Empty buffer in which a new service record will be stored.
102d40c9ac6SMatthias Ringwald  * @param have_remote_audio_control
103d40c9ac6SMatthias Ringwald  * @param service
104d40c9ac6SMatthias Ringwald  * @param service_record_handle
105d40c9ac6SMatthias Ringwald  * @param hid_device_subclass
106d40c9ac6SMatthias Ringwald  * @param hid_country_code
107d40c9ac6SMatthias Ringwald  * @param hid_virtual_cable
108d40c9ac6SMatthias Ringwald  * @param hid_reconnect_initiate
109d40c9ac6SMatthias Ringwald  * @param hid_boot_device
110d40c9ac6SMatthias Ringwald  * @param hid_descriptor
111d40c9ac6SMatthias Ringwald  * @param hid_descriptor_size size of hid_descriptor
112d40c9ac6SMatthias Ringwald  * @param device_name
113d40c9ac6SMatthias Ringwald  */
114d40c9ac6SMatthias Ringwald void hid_create_sdp_record(
115d40c9ac6SMatthias Ringwald     uint8_t *       service,
116d40c9ac6SMatthias Ringwald     uint32_t        service_record_handle,
117d40c9ac6SMatthias Ringwald     uint16_t        hid_device_subclass,
118d40c9ac6SMatthias Ringwald     uint8_t         hid_country_code,
119d40c9ac6SMatthias Ringwald     uint8_t         hid_virtual_cable,
120d40c9ac6SMatthias Ringwald     uint8_t         hid_reconnect_initiate,
121d40c9ac6SMatthias Ringwald     uint8_t         hid_boot_device,
122d40c9ac6SMatthias Ringwald     const uint8_t * hid_descriptor,
123d40c9ac6SMatthias Ringwald     uint16_t 		hid_descriptor_size,
124d40c9ac6SMatthias Ringwald     const char *    device_name);
125d40c9ac6SMatthias Ringwald 
126fe9125ceSMilanka Ringwald 
1278eb8d463SMatthias Ringwald /**
1288eb8d463SMatthias Ringwald  * @brief Set up HID Device
129ba9a58feSMilanka Ringwald  * @param boot_protocol_mode_supported
130*662cddc2SMilanka Ringwald  * @param hid_descriptor_len
131*662cddc2SMilanka Ringwald  * @param hid_descriptor
1328eb8d463SMatthias Ringwald  */
133*662cddc2SMilanka Ringwald void hid_device_init(uint8_t boot_protocol_mode_supported, uint16_t hid_descriptor_len, const uint8_t * hid_descriptor);
1348eb8d463SMatthias Ringwald 
1358eb8d463SMatthias Ringwald /**
1368eb8d463SMatthias Ringwald  * @brief Register callback for the HID Device client.
1378eb8d463SMatthias Ringwald  * @param callback
1388eb8d463SMatthias Ringwald  */
1398eb8d463SMatthias Ringwald void hid_device_register_packet_handler(btstack_packet_handler_t callback);
1408eb8d463SMatthias Ringwald 
141fcaf12e7SMilanka Ringwald /**
142ba9a58feSMilanka Ringwald  * @brief Register get report callback for the HID Device client.
143fcaf12e7SMilanka Ringwald  * @param callback
144fcaf12e7SMilanka Ringwald  */
145481c7cdcSMilanka Ringwald void hid_device_register_report_request_callback(hid_handshake_param_type_t (*callback) (uint16_t hid_cid, hid_report_type_t report_type, uint16_t report_id, uint8_t report_max_size, int * out_report_size, uint8_t * out_report));
14657c643eeSMatthias Ringwald 
147ba9a58feSMilanka Ringwald /**
148ba9a58feSMilanka Ringwald  * @brief Register set report callback for the HID Device client.
149ba9a58feSMilanka Ringwald  * @param callback
150ba9a58feSMilanka Ringwald  */
151ba9a58feSMilanka Ringwald void hid_device_register_set_report_callback(hid_handshake_param_type_t (*callback) (uint16_t hid_cid, hid_report_type_t report_type, int report_size, uint8_t * report));
152ba9a58feSMilanka Ringwald 
1537d26fe66SMilanka Ringwald /**
1547d26fe66SMilanka Ringwald  * @brief Register callback to receive report data for the HID Device client.
1557d26fe66SMilanka Ringwald  * @param callback
1567d26fe66SMilanka Ringwald  */
1577d26fe66SMilanka Ringwald void hid_device_register_report_data_callback(void hid_report_data_callback(uint16_t cid, hid_report_type_t report_type, uint16_t report_id, int report_size, uint8_t * report));
158ba9a58feSMilanka Ringwald 
15957c643eeSMatthias Ringwald /*
16057c643eeSMatthias Ringwald  * @brief Create HID connection to HID Host
16157c643eeSMatthias Ringwald  * @param addr
16257c643eeSMatthias Ringwald  * @param hid_cid to use for other commands
16357c643eeSMatthias Ringwald  * @result status
16457c643eeSMatthias Ringwald  */
16557c643eeSMatthias Ringwald uint8_t hid_device_connect(bd_addr_t addr, uint16_t * hid_cid);
16657c643eeSMatthias Ringwald 
1676510739bSMilanka Ringwald /*
1686510739bSMilanka Ringwald  * @brief Disconnect from HID Host
1696510739bSMilanka Ringwald  * @param hid_cid
1706510739bSMilanka Ringwald  */
1716510739bSMilanka Ringwald void hid_device_disconnect(uint16_t hid_cid);
1726510739bSMilanka Ringwald 
1738eb8d463SMatthias Ringwald /**
1748eb8d463SMatthias Ringwald  * @brief Request can send now event to send HID Report
1758eb8d463SMatthias Ringwald  * Generates an HID_SUBEVENT_CAN_SEND_NOW subevent
1768eb8d463SMatthias Ringwald  * @param hid_cid
1778eb8d463SMatthias Ringwald  */
1788eb8d463SMatthias Ringwald void hid_device_request_can_send_now_event(uint16_t hid_cid);
1798eb8d463SMatthias Ringwald 
1808eb8d463SMatthias Ringwald /**
1818eb8d463SMatthias Ringwald  * @brief Send HID messageon interrupt channel
1828eb8d463SMatthias Ringwald  * @param hid_cid
1838eb8d463SMatthias Ringwald  */
1848eb8d463SMatthias Ringwald void hid_device_send_interrupt_message(uint16_t hid_cid, const uint8_t * message, uint16_t message_len);
1858eb8d463SMatthias Ringwald 
1868eb8d463SMatthias Ringwald /**
1878eb8d463SMatthias Ringwald  * @brief Send HID messageon control channel
1888eb8d463SMatthias Ringwald  * @param hid_cid
1898eb8d463SMatthias Ringwald  */
190fcaf12e7SMilanka Ringwald void hid_device_send_control_message(uint16_t hid_cid, const uint8_t * message, uint16_t message_len);
191fcaf12e7SMilanka Ringwald 
1927d26fe66SMilanka Ringwald /**
1937d26fe66SMilanka Ringwald  * @brief Retutn 1 if boot protocol mode active
1947d26fe66SMilanka Ringwald  * @param hid_cid
1957d26fe66SMilanka Ringwald  */
1967d26fe66SMilanka Ringwald int hid_device_in_boot_protocol_mode(uint16_t hid_cid);
1978eb8d463SMatthias Ringwald 
198*662cddc2SMilanka Ringwald 
199*662cddc2SMilanka Ringwald int hid_report_size_valid(uint16_t cid, int report_id, hid_report_type_t report_type, int report_size);
200fe9125ceSMilanka Ringwald /* API_END */
2018eb8d463SMatthias Ringwald 
20257c18996SMilanka Ringwald /* Only needed for PTS Testing */
20357c18996SMilanka Ringwald void hid_device_disconnect_interrupt_channel(uint16_t hid_cid);
20457c18996SMilanka Ringwald void hid_device_disconnect_control_channel(uint16_t hid_cid);
20557c18996SMilanka Ringwald 
206fe9125ceSMilanka Ringwald #if defined __cplusplus
207fe9125ceSMilanka Ringwald }
208fe9125ceSMilanka Ringwald #endif
209fe9125ceSMilanka Ringwald 
210fe9125ceSMilanka Ringwald #endif
211