xref: /btstack/src/ble/gatt-service/hids_device.c (revision c824d78c0a34df89b57d535abafcc7dacf30bb06)
1 /*
2  * Copyright (C) 2014 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 BLUEKITCHEN
24  * GMBH 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 #define BTSTACK_FILE__ "hids_device.c"
39 
40 /**
41  * Implementation of the GATT HIDS Device
42  * To use with your application, add '#import <hids.gatt>' to your .gatt file
43  */
44 
45 #include "hids_device.h"
46 
47 #include "ble/att_db.h"
48 #include "ble/att_server.h"
49 #include "bluetooth_gatt.h"
50 #include "btstack_util.h"
51 #include "btstack_debug.h"
52 
53 #define HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS    0x80
54 
55 typedef struct{
56     uint16_t        con_handle;
57 
58     uint8_t         hid_country_code;
59     const uint8_t * hid_descriptor;
60     uint16_t        hid_descriptor_size;
61 
62     uint16_t        hid_report_map_handle;
63     uint8_t         hid_protocol_mode;
64     uint16_t        hid_protocol_mode_value_handle;
65 
66     uint16_t        hid_boot_mouse_input_value_handle;
67     uint16_t        hid_boot_mouse_input_client_configuration_handle;
68     uint16_t        hid_boot_mouse_input_client_configuration_value;
69 
70     uint16_t        hid_boot_keyboard_input_value_handle;
71     uint16_t        hid_boot_keyboard_input_client_configuration_handle;
72     uint16_t        hid_boot_keyboard_input_client_configuration_value;
73 
74     uint16_t        hid_report_input_value_handle;
75     uint16_t        hid_report_input_client_configuration_handle;
76     uint16_t        hid_report_input_client_configuration_value;
77 
78     uint16_t        hid_report_output_value_handle;
79     uint16_t        hid_report_output_client_configuration_handle;
80     uint16_t        hid_report_output_client_configuration_value;
81 
82     uint16_t        hid_report_feature_value_handle;
83     uint16_t        hid_report_feature_client_configuration_handle;
84     uint16_t        hid_report_feature_client_configuration_value;
85 
86     uint16_t        hid_control_point_value_handle;
87     uint8_t         hid_control_point_suspend;
88 
89     btstack_context_callback_registration_t  battery_callback;
90 } hids_device_t;
91 
92 static hids_device_t hids_device;
93 
94 static btstack_packet_handler_t packet_handler;
95 static att_service_handler_t hid_service;
96 
97 // TODO: store hids device connection into list
98 static hids_device_t * hids_device_get_instance_for_con_handle(uint16_t con_handle){
99     UNUSED(con_handle);
100     return &hids_device;
101 }
102 
103 static hids_device_t * hids_device_create_instance(void){
104     return &hids_device;
105 }
106 
107 
108 static void hids_device_emit_event_with_uint8(uint8_t event, hci_con_handle_t con_handle, uint8_t value){
109     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
110     if (!instance){
111         log_error("no instance for handle 0x%02x", con_handle);
112         return;
113     }
114 
115     if (!packet_handler) return;
116     uint8_t buffer[6];
117     buffer[0] = HCI_EVENT_HIDS_META;
118     buffer[1] = 4;
119     buffer[2] = event;
120     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
121     buffer[5] = value;
122     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
123 }
124 
125 static void hids_device_emit_event(uint8_t event, hci_con_handle_t con_handle){
126     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
127     if (!instance){
128         log_error("no instance for handle 0x%02x", con_handle);
129         return;
130     }
131 
132     if (!packet_handler) return;
133     uint8_t buffer[5];
134     buffer[0] = HCI_EVENT_HIDS_META;
135     buffer[1] = 4;
136     buffer[2] = event;
137     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
138     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
139 }
140 
141 static void hids_device_can_send_now(void * context){
142     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
143     // notify client
144     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
145     if (!instance){
146         log_error("no instance for handle 0x%02x", con_handle);
147         return;
148     }
149 
150     if (!packet_handler) return;
151     uint8_t buffer[5];
152     buffer[0] = HCI_EVENT_HIDS_META;
153     buffer[1] = 3;
154     buffer[2] = HIDS_SUBEVENT_CAN_SEND_NOW;
155     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
156     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
157 }
158 
159 // ATT Client Read Callback for Dynamic Data
160 // - if buffer == NULL, don't copy data, just return size of value
161 // - if buffer != NULL, copy data and return number bytes copied
162 static uint16_t att_read_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
163     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
164     if (!instance){
165         log_error("no instance for handle 0x%02x", con_handle);
166         return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS;
167     }
168 
169     if (att_handle == instance->hid_protocol_mode_value_handle){
170         log_info("Read protocol mode");
171         return att_read_callback_handle_byte(instance->hid_protocol_mode, offset, buffer, buffer_size);
172     }
173 
174     if (att_handle == instance->hid_report_map_handle){
175         log_info("Read report map");
176         return att_read_callback_handle_blob(instance->hid_descriptor, instance->hid_descriptor_size, offset, buffer, buffer_size);
177     }
178 
179     if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){
180         return att_read_callback_handle_little_endian_16(instance->hid_boot_mouse_input_client_configuration_value, offset, buffer, buffer_size);
181     }
182 
183     if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){
184         return att_read_callback_handle_little_endian_16(instance->hid_boot_keyboard_input_client_configuration_value, offset, buffer, buffer_size);
185     }
186 
187     if (att_handle == instance->hid_report_input_client_configuration_handle){
188         return att_read_callback_handle_little_endian_16(instance->hid_report_input_client_configuration_value, offset, buffer, buffer_size);
189     }
190 
191     if (att_handle == instance->hid_report_output_client_configuration_handle){
192         return att_read_callback_handle_little_endian_16(instance->hid_report_output_client_configuration_value, offset, buffer, buffer_size);
193     }
194 
195     if (att_handle == instance->hid_report_feature_client_configuration_handle){
196         return att_read_callback_handle_little_endian_16(instance->hid_report_feature_client_configuration_value, offset, buffer, buffer_size);
197     }
198 
199     if (att_handle == instance->hid_control_point_value_handle){
200         if (buffer && (buffer_size >= 1u)){
201             buffer[0] = instance->hid_control_point_suspend;
202         }
203         return 1;
204     }
205     return 0;
206 }
207 
208 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){
209     UNUSED(transaction_mode);
210     UNUSED(buffer_size);
211     UNUSED(offset);
212 
213     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
214     if (!instance){
215         log_error("no instance for handle 0x%02x", con_handle);
216         return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS;
217     }
218 
219     if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){
220         uint16_t new_value = little_endian_read_16(buffer, 0);
221         instance->hid_boot_mouse_input_client_configuration_value = new_value;
222         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_MOUSE_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
223     }
224     if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){
225         uint16_t new_value = little_endian_read_16(buffer, 0);
226         instance->hid_boot_keyboard_input_client_configuration_value = new_value;
227         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
228     }
229     if (att_handle == instance->hid_report_input_client_configuration_handle){
230         uint16_t new_value = little_endian_read_16(buffer, 0);
231         instance->hid_report_input_client_configuration_value = new_value;
232         log_info("Enable Report Input notifications: %x", new_value);
233         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
234     }
235     if (att_handle == instance->hid_report_output_client_configuration_handle){
236         uint16_t new_value = little_endian_read_16(buffer, 0);
237         instance->hid_report_output_client_configuration_value = new_value;
238         log_info("Enable Report Output notifications: %x", new_value);
239         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_OUTPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
240     }
241     if (att_handle == instance->hid_report_feature_client_configuration_handle){
242         uint16_t new_value = little_endian_read_16(buffer, 0);
243         instance->hid_report_feature_client_configuration_value = new_value;
244         log_info("Enable Report Feature notifications: %x", new_value);
245         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_FEATURE_REPORT_ENABLE, con_handle, (uint8_t) new_value);
246     }
247 
248     if (att_handle == instance->hid_protocol_mode_value_handle){
249         instance->hid_protocol_mode = buffer[0];
250         log_info("Set protocol mode: %u", instance->hid_protocol_mode);
251         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_PROTOCOL_MODE, con_handle, instance->hid_protocol_mode);
252     }
253 
254     if (att_handle == instance->hid_control_point_value_handle){
255         if (buffer_size < 1u){
256             return ATT_ERROR_INVALID_OFFSET;
257         }
258         instance->hid_control_point_suspend = buffer[0];
259         instance->con_handle = con_handle;
260         log_info("Set suspend tp: %u", instance->hid_control_point_suspend );
261         if (instance->hid_control_point_suspend == 0u){
262             hids_device_emit_event(HIDS_SUBEVENT_SUSPEND, con_handle);
263         } else if (instance->hid_control_point_suspend == 1u){
264             hids_device_emit_event(HIDS_SUBEVENT_EXIT_SUSPEND, con_handle);
265         }
266     }
267     return 0;
268 }
269 
270 /**
271  * @brief Set up HIDS Device
272  */
273 void hids_device_init(uint8_t country_code, const uint8_t * descriptor, uint16_t descriptor_size){
274     hids_device_t * instance = hids_device_create_instance();
275     if (!instance){
276         log_error("hids_device_init: instance could not be created, not enough memory");
277         return;
278     }
279 
280     instance->hid_country_code    = country_code;
281     instance->hid_descriptor      = descriptor;
282     instance->hid_descriptor_size = descriptor_size;
283 
284     // default
285     instance->hid_protocol_mode   = 1;
286 
287     // get service handle range
288     uint16_t start_handle = 0;
289     uint16_t end_handle   = 0xffff;
290     int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE, &start_handle, &end_handle);
291 	btstack_assert(service_found != 0);
292 	UNUSED(service_found);
293 
294     // get report map handle
295     instance->hid_report_map_handle                               = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP);
296 
297     // get report map handle
298     instance->hid_protocol_mode_value_handle                      = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_PROTOCOL_MODE);
299 
300     // get value and client configuration handles for boot mouse input, boot keyboard input and report input
301     instance->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);
302     instance->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);
303 
304     instance->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);
305     instance->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);
306 
307     instance->hid_report_input_value_handle                       = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT);
308     instance->hid_report_input_client_configuration_handle        = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT);
309 
310     instance->hid_report_output_value_handle                      = gatt_server_get_value_handle_for_characteristic_with_uuid16(instance->hid_report_input_client_configuration_handle+1u, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT);
311     instance->hid_report_output_client_configuration_handle       = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(instance->hid_report_input_client_configuration_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT);
312 
313     instance->hid_report_feature_value_handle                     = gatt_server_get_value_handle_for_characteristic_with_uuid16(instance->hid_report_output_client_configuration_handle+1u, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT);
314     instance->hid_report_feature_client_configuration_handle      = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(instance->hid_report_output_client_configuration_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT);
315 
316     instance->hid_control_point_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_HID_CONTROL_POINT);
317 
318     log_info("hid_report_map_handle                               0x%02x", instance->hid_report_map_handle);
319     log_info("hid_protocol_mode_value_handle                      0x%02x", instance->hid_protocol_mode_value_handle);
320     log_info("hid_boot_mouse_input_value_handle                   0x%02x", instance->hid_boot_mouse_input_value_handle);
321     log_info("hid_boot_mouse_input_client_configuration_handle    0x%02x", instance->hid_boot_mouse_input_client_configuration_handle);
322     log_info("hid_boot_keyboard_input_value_handle                0x%02x", instance->hid_boot_keyboard_input_value_handle);
323     log_info("hid_boot_keyboard_input_client_configuration_handle 0x%02x", instance->hid_boot_keyboard_input_client_configuration_handle);
324     log_info("hid_report_input_value_handle                       0x%02x", instance->hid_report_input_value_handle);
325     log_info("hid_report_input_client_configuration_handle        0x%02x", instance->hid_report_input_client_configuration_handle);
326     log_info("hid_report_output_value_handle                      0x%02x", instance->hid_report_output_value_handle);
327     log_info("hid_report_output_client_configuration_handle       0x%02x", instance->hid_report_output_client_configuration_handle);
328     log_info("hid_report_feature_value_handle                     0x%02x", instance->hid_report_feature_value_handle);
329     log_info("hid_report_feature_client_configuration_handle      0x%02x", instance->hid_report_feature_client_configuration_handle);
330     log_info("hid_control_point_value_handle                      0x%02x", instance->hid_control_point_value_handle);
331 
332     // register service with ATT Server
333     hid_service.start_handle   = start_handle;
334     hid_service.end_handle     = end_handle;
335     hid_service.read_callback  = &att_read_callback;
336     hid_service.write_callback = &att_write_callback;
337     att_server_register_service_handler(&hid_service);
338 }
339 
340 /**
341  * @brief Register callback for the HIDS Device client.
342  * @param callback
343  */
344 void hids_device_register_packet_handler(btstack_packet_handler_t callback){
345     packet_handler = callback;
346 }
347 
348 /**
349  * @brief Request can send now event to send HID Report
350  * Generates an HIDS_SUBEVENT_CAN_SEND_NOW subevent
351  * @param hid_cid
352  */
353 void hids_device_request_can_send_now_event(hci_con_handle_t con_handle){
354     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
355     if (!instance){
356         log_error("no instance for handle 0x%02x", con_handle);
357         return;
358     }
359 
360     instance->battery_callback.callback = &hids_device_can_send_now;
361     instance->battery_callback.context  = (void*) (uintptr_t) con_handle;
362     att_server_register_can_send_now_callback(&instance->battery_callback, con_handle);
363 }
364 
365 /**
366  * @brief Send HID Report: Input
367  */
368 void hids_device_send_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
369     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
370     if (!instance){
371         log_error("no instance for handle 0x%02x", con_handle);
372         return;
373     }
374     att_server_notify(con_handle, instance->hid_report_input_value_handle, report, report_len);
375 }
376 
377 /**
378  * @brief Send HID Report: Output
379  */
380 void hids_device_send_output_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
381     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
382     if (!instance){
383         log_error("no instance for handle 0x%02x", con_handle);
384         return;
385     }
386     att_server_notify(con_handle, instance->hid_report_output_value_handle, report, report_len);
387 }
388 
389 /**
390  * @brief Send HID Report: Feature
391  */
392 void hids_device_send_feature_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
393     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
394     if (!instance){
395         log_error("no instance for handle 0x%02x", con_handle);
396         return;
397     }
398     att_server_notify(con_handle, instance->hid_report_feature_value_handle, report, report_len);
399 }
400 
401 /**
402  * @brief Send HID Boot Mouse Input Report
403  */
404 void hids_device_send_boot_mouse_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
405     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
406     if (!instance){
407         log_error("no instance for handle 0x%02x", con_handle);
408         return;
409     }
410     att_server_notify(con_handle, instance->hid_boot_mouse_input_value_handle, report, report_len);
411 }
412 
413 /**
414  * @brief Send HID Boot Mouse Input Report
415  */
416 void hids_device_send_boot_keyboard_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
417     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
418     if (!instance){
419         log_error("no instance for handle 0x%02x", con_handle);
420         return;
421     }
422     att_server_notify(con_handle, instance->hid_boot_keyboard_input_value_handle, report, report_len);
423 }
424