xref: /btstack/src/ble/gatt-service/hids_client.h (revision 1624214b8ffb7f8a044e7257f6894798c40254f1)
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 10
58 
59 typedef enum {
60     HIDS_CLIENT_STATE_IDLE,
61     HIDS_CLIENT_STATE_W2_QUERY_SERVICE,
62     HIDS_CLIENT_STATE_W4_SERVICE_RESULT,
63     HIDS_CLIENT_STATE_W2_QUERY_CHARACTERISTIC,
64     HIDS_CLIENT_STATE_W4_CHARACTERISTIC_RESULT,
65     HIDS_CLIENT_STATE_W2_ENABLE_KEYBOARD,
66     HIDS_CLIENT_STATE_W4_KEYBOARD_ENABLED,
67     HIDS_CLIENT_STATE_W2_ENABLE_MOUSE,
68     HIDS_CLIENT_STATE_W4_MOUSE_ENABLED,
69     HIDS_CLIENT_STATE_W2_SET_PROTOCOL_MODE,
70     HIDS_CLIENT_STATE_W4_SET_PROTOCOL_MODE,
71     HIDS_CLIENT_STATE_CONNECTED,
72     HIDS_CLIENT_W2_SEND_REPORT
73 } hid_service_client_state_t;
74 
75 
76 typedef struct {
77     uint16_t value_handle;
78     uint8_t  report_id;
79     hid_report_type_t report_type;
80 } hids_client_report_t;
81 
82 typedef struct {
83     uint16_t start_handle;
84     uint16_t end_handle;
85 } hid_service_t;
86 
87 typedef struct {
88     btstack_linked_item_t item;
89 
90     hci_con_handle_t  con_handle;
91     uint16_t          cid;
92     hid_protocol_mode_t protocol_mode;
93     hid_service_client_state_t state;
94     btstack_packet_handler_t   client_handler;
95 
96     uint8_t num_instances;
97     hid_service_t services[MAX_NUM_HID_SERVICES];
98 
99     // used for discovering characteristics
100     uint8_t service_index;
101     hid_protocol_mode_t required_protocol_mode;
102 
103     uint16_t protocol_mode_value_handle;
104 
105     uint16_t boot_keyboard_input_value_handle;
106     uint16_t boot_keyboard_input_end_handle;
107     uint16_t boot_keyboard_input_properties;
108     gatt_client_notification_t boot_keyboard_notifications;
109 
110     uint16_t boot_mouse_input_value_handle;
111     uint16_t boot_mouse_input_end_handle;
112     uint16_t boot_mouse_input_properties;
113     gatt_client_notification_t boot_mouse_notifications;
114 
115     // send report
116     hids_client_report_t reports[HIDS_CLIENT_NUM_REPORTS];
117     uint8_t num_reports;
118 
119     uint8_t   active_report_index;
120     uint16_t  report_len;
121     const uint8_t * report;
122 } hids_client_t;
123 
124 /* API_START */
125 
126 /**
127  * @brief Initialize Battery Service.
128  */
129 void hids_client_init(void);
130 
131 /* @brief Connect to HID Services of remote device.
132  *
133  * @param con_handle
134  * @param packet_handler
135  * @param protocol_mode see hid_protocol_mode_t in hid.h
136  * @param hids_cid
137  * @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
138  */
139 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);
140 
141 /**
142  * @brief Send HID output report.
143  * @param hids_cid
144  * @param report_id
145  * @param report
146  * @param report_len
147  * @result status ERROR_CODE_SUCCESS on success, otherwise ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, ERROR_CODE_COMMAND_DISALLOWED
148  */
149 uint8_t hids_client_send_report(uint16_t hids_cid, uint8_t report_id, const uint8_t * report, uint8_t report_len);
150 
151 /**
152  * @brief Disconnect from Battery Service.
153  * @param hids_cid
154  * @return status
155  */
156 uint8_t hids_client_disconnect(uint16_t hids_cid);
157 
158 /**
159  * @brief De-initialize Battery Service.
160  */
161 void hids_client_deinit(void);
162 
163 /* API_END */
164 
165 #if defined __cplusplus
166 }
167 #endif
168 
169 #endif
170