xref: /btstack/src/ble/gatt-service/hids_device.c (revision 35b80bb0da380f95b4c8d2d44665c4aa74682e3e)
1a4bfc4feSMatthias Ringwald /*
2a4bfc4feSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3a4bfc4feSMatthias Ringwald  *
4a4bfc4feSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5a4bfc4feSMatthias Ringwald  * modification, are permitted provided that the following conditions
6a4bfc4feSMatthias Ringwald  * are met:
7a4bfc4feSMatthias Ringwald  *
8a4bfc4feSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9a4bfc4feSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10a4bfc4feSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11a4bfc4feSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12a4bfc4feSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13a4bfc4feSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14a4bfc4feSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15a4bfc4feSMatthias Ringwald  *    from this software without specific prior written permission.
16a4bfc4feSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17a4bfc4feSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18a4bfc4feSMatthias Ringwald  *    monetary gain.
19a4bfc4feSMatthias Ringwald  *
20a4bfc4feSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21a4bfc4feSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22a4bfc4feSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23a4bfc4feSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24a4bfc4feSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25a4bfc4feSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26a4bfc4feSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27a4bfc4feSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28a4bfc4feSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29a4bfc4feSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30a4bfc4feSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31a4bfc4feSMatthias Ringwald  * SUCH DAMAGE.
32a4bfc4feSMatthias Ringwald  *
33a4bfc4feSMatthias Ringwald  * Please inquire about commercial licensing options at
34a4bfc4feSMatthias Ringwald  * [email protected]
35a4bfc4feSMatthias Ringwald  *
36a4bfc4feSMatthias Ringwald  */
37a4bfc4feSMatthias Ringwald 
38523edd21SMatthias Ringwald #define __BTSTACK_FILE__ "hids_device.c"
39523edd21SMatthias Ringwald 
40a4bfc4feSMatthias Ringwald /**
41a4bfc4feSMatthias Ringwald  * Implementation of the GATT HIDS Device
42a4bfc4feSMatthias Ringwald  * To use with your application, add '#import <hids.gatt>' to your .gatt file
43a4bfc4feSMatthias Ringwald  */
44a4bfc4feSMatthias Ringwald 
45a4bfc4feSMatthias Ringwald #include "hids_device.h"
46a4bfc4feSMatthias Ringwald 
47*35b80bb0SMatthias Ringwald #include "ble/att_db.h"
48*35b80bb0SMatthias Ringwald #include "ble/att_server.h"
49a4bfc4feSMatthias Ringwald #include "bluetooth_gatt.h"
50a4bfc4feSMatthias Ringwald #include "btstack_util.h"
51523edd21SMatthias Ringwald #include "btstack_debug.h"
52a4bfc4feSMatthias Ringwald 
53a4bfc4feSMatthias Ringwald static btstack_packet_handler_t packet_handler;
54a4bfc4feSMatthias Ringwald static btstack_context_callback_registration_t  battery_callback;
55a4bfc4feSMatthias Ringwald 
56a4bfc4feSMatthias Ringwald static uint8_t         hid_country_code;
57a4bfc4feSMatthias Ringwald static const uint8_t * hid_descriptor;
58a4bfc4feSMatthias Ringwald static uint16_t        hid_descriptor_size;
59a4bfc4feSMatthias Ringwald 
60a4bfc4feSMatthias Ringwald static uint16_t        hid_report_map_handle;
61a4bfc4feSMatthias Ringwald static uint16_t        hid_protocol_mode;
62a4bfc4feSMatthias Ringwald static uint16_t        hid_protocol_mode_value_handle;
63a4bfc4feSMatthias Ringwald 
64a4bfc4feSMatthias Ringwald static uint16_t        hid_boot_mouse_input_value_handle;
65a4bfc4feSMatthias Ringwald static uint16_t        hid_boot_mouse_input_client_configuration_handle;
66a4bfc4feSMatthias Ringwald static uint16_t        hid_boot_mouse_input_client_configuration_value;
67a4bfc4feSMatthias Ringwald 
68a4bfc4feSMatthias Ringwald static uint16_t        hid_boot_keyboard_input_value_handle;
69a4bfc4feSMatthias Ringwald static uint16_t        hid_boot_keyboard_input_client_configuration_handle;
70a4bfc4feSMatthias Ringwald static uint16_t        hid_boot_keyboard_input_client_configuration_value;
71a4bfc4feSMatthias Ringwald 
72a4bfc4feSMatthias Ringwald static uint16_t        hid_report_input_value_handle;
73a4bfc4feSMatthias Ringwald static uint16_t        hid_report_input_client_configuration_handle;
74a4bfc4feSMatthias Ringwald static uint16_t        hid_report_input_client_configuration_value;
75a4bfc4feSMatthias Ringwald 
76a4bfc4feSMatthias Ringwald static att_service_handler_t hid_service;
77a4bfc4feSMatthias Ringwald 
78a4bfc4feSMatthias Ringwald static void hids_device_emit_event_with_uint8(uint8_t event, hci_con_handle_t con_handle, uint8_t value){
79a4bfc4feSMatthias Ringwald     if (!packet_handler) return;
80a4bfc4feSMatthias Ringwald     uint8_t buffer[6];
81a4bfc4feSMatthias Ringwald     buffer[0] = HCI_EVENT_HIDS_META;
82a4bfc4feSMatthias Ringwald     buffer[1] = 4;
83a4bfc4feSMatthias Ringwald     buffer[2] = event;
84a4bfc4feSMatthias Ringwald     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
85a4bfc4feSMatthias Ringwald     buffer[5] = value;
86a4bfc4feSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
87a4bfc4feSMatthias Ringwald }
88a4bfc4feSMatthias Ringwald 
89a4bfc4feSMatthias Ringwald static void hids_device_can_send_now(void * context){
90a4bfc4feSMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
91a4bfc4feSMatthias Ringwald     // notify client
92a4bfc4feSMatthias Ringwald     if (!packet_handler) return;
93a4bfc4feSMatthias Ringwald     uint8_t buffer[5];
94a4bfc4feSMatthias Ringwald     buffer[0] = HCI_EVENT_HIDS_META;
95a4bfc4feSMatthias Ringwald     buffer[1] = 3;
96a4bfc4feSMatthias Ringwald     buffer[2] = HIDS_SUBEVENT_CAN_SEND_NOW;
97a4bfc4feSMatthias Ringwald     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
98a4bfc4feSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
99a4bfc4feSMatthias Ringwald }
100a4bfc4feSMatthias Ringwald 
101a4bfc4feSMatthias Ringwald // ATT Client Read Callback for Dynamic Data
102a4bfc4feSMatthias Ringwald // - if buffer == NULL, don't copy data, just return size of value
103a4bfc4feSMatthias Ringwald // - if buffer != NULL, copy data and return number bytes copied
104a4bfc4feSMatthias Ringwald static uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
105a4bfc4feSMatthias Ringwald     UNUSED(connection_handle);
106a4bfc4feSMatthias Ringwald 
107a4bfc4feSMatthias Ringwald     if (att_handle == hid_protocol_mode_value_handle){
108523edd21SMatthias Ringwald         log_info("Read protocol mode");
109a4bfc4feSMatthias Ringwald         return att_read_callback_handle_byte(hid_protocol_mode, offset, buffer, buffer_size);
110a4bfc4feSMatthias Ringwald     }
111a4bfc4feSMatthias Ringwald     if (att_handle == hid_report_map_handle){
112523edd21SMatthias Ringwald         log_info("Read report map");
113a4bfc4feSMatthias Ringwald         return att_read_callback_handle_blob(hid_descriptor, hid_descriptor_size, offset, buffer, buffer_size);
114a4bfc4feSMatthias Ringwald     }
115a4bfc4feSMatthias Ringwald     // if (att_handle == hid_boot_mouse_input_value_handle){
116a4bfc4feSMatthias Ringwald     // }
117a4bfc4feSMatthias Ringwald     if (att_handle == hid_boot_mouse_input_client_configuration_handle){
118a4bfc4feSMatthias Ringwald         return att_read_callback_handle_little_endian_16(hid_boot_keyboard_input_client_configuration_value, offset, buffer, buffer_size);
119a4bfc4feSMatthias Ringwald     }
120a4bfc4feSMatthias Ringwald     // if (att_handle == hid_boot_keyboard_input_value_handle){
121a4bfc4feSMatthias Ringwald     // }
122a4bfc4feSMatthias Ringwald     if (att_handle == hid_boot_keyboard_input_client_configuration_handle){
123a4bfc4feSMatthias Ringwald         return att_read_callback_handle_little_endian_16(hid_boot_keyboard_input_client_configuration_value, offset, buffer, buffer_size);
124a4bfc4feSMatthias Ringwald     }
125a4bfc4feSMatthias Ringwald     // if (att_handle == hid_report_input_value_handle){
126a4bfc4feSMatthias Ringwald     // }
127a4bfc4feSMatthias Ringwald     if (att_handle == hid_report_input_client_configuration_handle){
128a4bfc4feSMatthias Ringwald         return att_read_callback_handle_little_endian_16(hid_report_input_client_configuration_value, offset, buffer, buffer_size);
129a4bfc4feSMatthias Ringwald     }
130a4bfc4feSMatthias Ringwald     return 0;
131a4bfc4feSMatthias Ringwald }
132a4bfc4feSMatthias Ringwald 
133a4bfc4feSMatthias Ringwald static int att_write_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){
134a4bfc4feSMatthias Ringwald     UNUSED(transaction_mode);
135a4bfc4feSMatthias Ringwald     UNUSED(buffer_size);
136a4bfc4feSMatthias Ringwald     UNUSED(offset);
137a4bfc4feSMatthias Ringwald 
138a4bfc4feSMatthias Ringwald     if (att_handle == hid_boot_mouse_input_client_configuration_handle){
139a4bfc4feSMatthias Ringwald         hid_boot_mouse_input_client_configuration_value = little_endian_read_16(buffer, 0);
140a4bfc4feSMatthias Ringwald         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_MOUSE_INPUT_REPORT_ENABLE, con_handle, hid_protocol_mode);
141a4bfc4feSMatthias Ringwald     }
142a4bfc4feSMatthias Ringwald     if (att_handle == hid_boot_keyboard_input_client_configuration_handle){
143a4bfc4feSMatthias Ringwald         hid_boot_keyboard_input_client_configuration_value = little_endian_read_16(buffer, 0);
144a4bfc4feSMatthias Ringwald         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE, con_handle, hid_protocol_mode);
145a4bfc4feSMatthias Ringwald     }
146a4bfc4feSMatthias Ringwald     if (att_handle == hid_report_input_client_configuration_handle){
147a4bfc4feSMatthias Ringwald         hid_report_input_client_configuration_value = little_endian_read_16(buffer, 0);
148523edd21SMatthias Ringwald         log_info("Enable Report Input notifications: %x", hid_report_input_client_configuration_value);
149a4bfc4feSMatthias Ringwald         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_INPUT_REPORT_ENABLE, con_handle, hid_protocol_mode);
150a4bfc4feSMatthias Ringwald     }
151a4bfc4feSMatthias Ringwald     if (att_handle == hid_protocol_mode_value_handle){
152a4bfc4feSMatthias Ringwald         hid_protocol_mode = buffer[0];
153523edd21SMatthias Ringwald         log_info("Set protocol mode: %u", hid_protocol_mode);
154a4bfc4feSMatthias Ringwald         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_PROTOCOL_MODE, con_handle, hid_protocol_mode);
155a4bfc4feSMatthias Ringwald     }
156a4bfc4feSMatthias Ringwald     return 0;
157a4bfc4feSMatthias Ringwald }
158a4bfc4feSMatthias Ringwald 
159a4bfc4feSMatthias Ringwald /**
160a4bfc4feSMatthias Ringwald  * @brief Set up HIDS Device
161a4bfc4feSMatthias Ringwald  */
162a4bfc4feSMatthias Ringwald void hids_device_init(uint8_t country_code, const uint8_t * descriptor, uint16_t descriptor_size){
163a4bfc4feSMatthias Ringwald 
164a4bfc4feSMatthias Ringwald     hid_country_code    = country_code;
165a4bfc4feSMatthias Ringwald     hid_descriptor      = descriptor;
166a4bfc4feSMatthias Ringwald     hid_descriptor_size = descriptor_size;
167a4bfc4feSMatthias Ringwald 
168a4bfc4feSMatthias Ringwald     // default
169a4bfc4feSMatthias Ringwald     hid_protocol_mode   = 1;
170a4bfc4feSMatthias Ringwald 
171a4bfc4feSMatthias Ringwald     // get service handle range
172a4bfc4feSMatthias Ringwald     uint16_t start_handle = 0;
173a4bfc4feSMatthias Ringwald     uint16_t end_handle   = 0xfff;
174a4bfc4feSMatthias Ringwald     int service_found = gatt_server_get_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE, &start_handle, &end_handle);
175a4bfc4feSMatthias Ringwald     if (!service_found) return;
176a4bfc4feSMatthias Ringwald 
177a4bfc4feSMatthias Ringwald     // get report map handle
178a4bfc4feSMatthias Ringwald     hid_report_map_handle                               = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP);
179a4bfc4feSMatthias Ringwald 
180a4bfc4feSMatthias Ringwald     // get report map handle
181a4bfc4feSMatthias Ringwald     hid_protocol_mode_value_handle                      = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_PROTOCOL_MODE);
182a4bfc4feSMatthias Ringwald 
183a4bfc4feSMatthias Ringwald     // get value and client configuration handles for boot mouse input, boot keyboard input and report input
184a4bfc4feSMatthias Ringwald     hid_boot_mouse_input_value_handle                   = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BOOT_MOUSE_INPUT_REPORT);
185a4bfc4feSMatthias Ringwald     hid_boot_mouse_input_client_configuration_handle    = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BOOT_MOUSE_INPUT_REPORT);
186a4bfc4feSMatthias Ringwald 
187a4bfc4feSMatthias Ringwald     hid_boot_keyboard_input_value_handle                = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_INPUT_REPORT);
188a4bfc4feSMatthias Ringwald     hid_boot_keyboard_input_client_configuration_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_INPUT_REPORT);
189a4bfc4feSMatthias Ringwald 
190a4bfc4feSMatthias Ringwald     hid_report_input_value_handle                       = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT);
191a4bfc4feSMatthias Ringwald     hid_report_input_client_configuration_handle        = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT);
192a4bfc4feSMatthias Ringwald 
193a4bfc4feSMatthias Ringwald     // register service with ATT DB
194a4bfc4feSMatthias Ringwald     hid_service.start_handle   = start_handle;
195a4bfc4feSMatthias Ringwald     hid_service.end_handle     = end_handle;
196a4bfc4feSMatthias Ringwald     hid_service.read_callback  = &att_read_callback;
197a4bfc4feSMatthias Ringwald     hid_service.write_callback = &att_write_callback;
198a4bfc4feSMatthias Ringwald     att_register_service_handler(&hid_service);
199a4bfc4feSMatthias Ringwald }
200a4bfc4feSMatthias Ringwald 
201a4bfc4feSMatthias Ringwald /**
202a4bfc4feSMatthias Ringwald  * @brief Register callback for the HIDS Device client.
203a4bfc4feSMatthias Ringwald  * @param callback
204a4bfc4feSMatthias Ringwald  */
205a4bfc4feSMatthias Ringwald void hids_device_register_packet_handler(btstack_packet_handler_t callback){
206a4bfc4feSMatthias Ringwald     packet_handler = callback;
207a4bfc4feSMatthias Ringwald }
208a4bfc4feSMatthias Ringwald 
209a4bfc4feSMatthias Ringwald /**
210a4bfc4feSMatthias Ringwald  * @brief Request can send now event to send HID Report
211a4bfc4feSMatthias Ringwald  * Generates an HIDS_SUBEVENT_CAN_SEND_NOW subevent
212a4bfc4feSMatthias Ringwald  * @param hid_cid
213a4bfc4feSMatthias Ringwald  */
214a4bfc4feSMatthias Ringwald void hids_device_request_can_send_now_event(hci_con_handle_t con_handle){
215a4bfc4feSMatthias Ringwald     battery_callback.callback = &hids_device_can_send_now;
216a4bfc4feSMatthias Ringwald     battery_callback.context  = (void*) (uintptr_t) con_handle;
217a4bfc4feSMatthias Ringwald     att_server_register_can_send_now_callback(&battery_callback, con_handle);
218a4bfc4feSMatthias Ringwald }
219a4bfc4feSMatthias Ringwald 
220a4bfc4feSMatthias Ringwald /**
221a4bfc4feSMatthias Ringwald  * @brief Send HID Report: Input
222a4bfc4feSMatthias Ringwald  */
223a4bfc4feSMatthias Ringwald void hids_device_send_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
224a4bfc4feSMatthias Ringwald     att_server_notify(con_handle, hid_report_input_value_handle, (uint8_t*) report, report_len);
225a4bfc4feSMatthias Ringwald }
226a4bfc4feSMatthias Ringwald 
227a4bfc4feSMatthias Ringwald /**
228a4bfc4feSMatthias Ringwald  * @brief Send HID Boot Mouse Input Report
229a4bfc4feSMatthias Ringwald  */
230a4bfc4feSMatthias Ringwald void hids_device_send_boot_mouse_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
231a4bfc4feSMatthias Ringwald     att_server_notify(con_handle, hid_boot_mouse_input_value_handle, (uint8_t*) report, report_len);
232a4bfc4feSMatthias Ringwald }
233a4bfc4feSMatthias Ringwald 
234a4bfc4feSMatthias Ringwald /**
235a4bfc4feSMatthias Ringwald  * @brief Send HID Boot Mouse Input Report
236a4bfc4feSMatthias Ringwald  */
237a4bfc4feSMatthias Ringwald void hids_device_send_boot_keyboard_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
238a4bfc4feSMatthias Ringwald     att_server_notify(con_handle, hid_boot_keyboard_input_value_handle, (uint8_t*) report, report_len);
239a4bfc4feSMatthias Ringwald }
240