xref: /btstack/src/ble/gatt-service/hids_device.c (revision f4f6c196472256f1e4016b750d62cbee58c36894)
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 static hids_device_report_t hid_reports_storage[3];
56 
57 typedef struct{
58     uint16_t        con_handle;
59 
60     uint8_t         hid_country_code;
61     const uint8_t * hid_descriptor;
62     uint16_t        hid_descriptor_size;
63 
64     uint16_t        hid_report_map_handle;
65     uint8_t         hid_protocol_mode;
66     uint16_t        hid_protocol_mode_value_handle;
67 
68     uint16_t        hid_boot_mouse_input_value_handle;
69     uint16_t        hid_boot_mouse_input_client_configuration_handle;
70     uint16_t        hid_boot_mouse_input_client_configuration_value;
71 
72     uint16_t        hid_boot_keyboard_input_value_handle;
73     uint16_t        hid_boot_keyboard_input_client_configuration_handle;
74     uint16_t        hid_boot_keyboard_input_client_configuration_value;
75 
76     hids_device_report_t * hid_reports;
77 
78     uint8_t         hid_input_reports_num;
79     uint8_t         hid_output_reports_num;
80     uint8_t         hid_feature_reports_num;
81 
82     uint16_t        hid_control_point_value_handle;
83     uint8_t         hid_control_point_suspend;
84 
85     btstack_context_callback_registration_t  battery_callback;
86 } hids_device_t;
87 
88 static hids_device_t hids_device;
89 
90 static btstack_packet_handler_t packet_handler;
91 static att_service_handler_t hid_service;
92 
93 // TODO: store hids device connection into list
94 static hids_device_t * hids_device_get_instance_for_con_handle(uint16_t con_handle){
95     UNUSED(con_handle);
96     return &hids_device;
97 }
98 
99 static hids_device_t * hids_device_create_instance(void){
100     memset(&hids_device, 0, sizeof(hids_device_t));
101     return &hids_device;
102 }
103 
104 static hids_device_report_t * hids_device_get_report_for_client_configuration_handle(hids_device_t * device, uint16_t client_configuration_handle){
105     uint8_t pos = 0;
106     while (pos < (device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num)){
107         if (device->hid_reports[pos].client_configuration_handle == client_configuration_handle){
108             return &device->hid_reports[pos];
109         }
110     }
111     return NULL;
112 }
113 
114 static hids_device_report_t * hids_device_get_report_for_id(hids_device_t * device, uint16_t report_id){
115     uint8_t pos = 0;
116     while (pos < (device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num)){
117         if (device->hid_reports[pos].id == report_id){
118             return &device->hid_reports[pos];
119         }
120     }
121     return NULL;
122 }
123 
124 static void hids_device_emit_event_with_uint8(uint8_t event, hci_con_handle_t con_handle, uint8_t value){
125     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
126     if (!instance){
127         log_error("no instance for handle 0x%02x", con_handle);
128         return;
129     }
130 
131     if (!packet_handler) return;
132     uint8_t buffer[6];
133     buffer[0] = HCI_EVENT_HIDS_META;
134     buffer[1] = 4;
135     buffer[2] = event;
136     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
137     buffer[5] = value;
138     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
139 }
140 
141 static void hids_device_emit_event(uint8_t event, hci_con_handle_t con_handle){
142     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
143     if (!instance){
144         log_error("no instance for handle 0x%02x", con_handle);
145         return;
146     }
147 
148     if (!packet_handler) return;
149     uint8_t buffer[5];
150     buffer[0] = HCI_EVENT_HIDS_META;
151     buffer[1] = 4;
152     buffer[2] = event;
153     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
154     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
155 }
156 
157 static void hids_device_can_send_now(void * context){
158     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
159     // notify client
160     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
161     if (!instance){
162         log_error("no instance for handle 0x%02x", con_handle);
163         return;
164     }
165 
166     if (!packet_handler) return;
167     uint8_t buffer[5];
168     buffer[0] = HCI_EVENT_HIDS_META;
169     buffer[1] = 3;
170     buffer[2] = HIDS_SUBEVENT_CAN_SEND_NOW;
171     little_endian_store_16(buffer, 3, (uint16_t) con_handle);
172     (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
173 }
174 
175 // ATT Client Read Callback for Dynamic Data
176 // - if buffer == NULL, don't copy data, just return size of value
177 // - if buffer != NULL, copy data and return number bytes copied
178 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){
179     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
180     if (!instance){
181         log_error("no instance for handle 0x%02x", con_handle);
182         return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS;
183     }
184 
185     if (att_handle == instance->hid_protocol_mode_value_handle){
186         log_info("Read protocol mode");
187         return att_read_callback_handle_byte(instance->hid_protocol_mode, offset, buffer, buffer_size);
188     }
189 
190     if (att_handle == instance->hid_report_map_handle){
191         log_info("Read report map");
192         return att_read_callback_handle_blob(instance->hid_descriptor, instance->hid_descriptor_size, offset, buffer, buffer_size);
193     }
194 
195     if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){
196         return att_read_callback_handle_little_endian_16(instance->hid_boot_mouse_input_client_configuration_value, offset, buffer, buffer_size);
197     }
198 
199     if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){
200         return att_read_callback_handle_little_endian_16(instance->hid_boot_keyboard_input_client_configuration_value, offset, buffer, buffer_size);
201     }
202 
203     if (att_handle == instance->hid_control_point_value_handle){
204         if (buffer && (buffer_size >= 1u)){
205             buffer[0] = instance->hid_control_point_suspend;
206         }
207         return 1;
208     }
209 
210     hids_device_report_t * report = hids_device_get_report_for_client_configuration_handle(instance, att_handle);
211     if (report != NULL){
212         return att_read_callback_handle_little_endian_16(report->client_configuration_value, offset, buffer, buffer_size);
213     }
214     return 0;
215 }
216 
217 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){
218     UNUSED(transaction_mode);
219     UNUSED(buffer_size);
220     UNUSED(offset);
221 
222     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
223     if (!instance){
224         log_error("no instance for handle 0x%02x", con_handle);
225         return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS;
226     }
227 
228     if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){
229         uint16_t new_value = little_endian_read_16(buffer, 0);
230         instance->hid_boot_mouse_input_client_configuration_value = new_value;
231         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_MOUSE_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
232     }
233     if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){
234         uint16_t new_value = little_endian_read_16(buffer, 0);
235         instance->hid_boot_keyboard_input_client_configuration_value = new_value;
236         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
237     }
238 
239     if (att_handle == instance->hid_protocol_mode_value_handle){
240         instance->hid_protocol_mode = buffer[0];
241         log_info("Set protocol mode: %u", instance->hid_protocol_mode);
242         hids_device_emit_event_with_uint8(HIDS_SUBEVENT_PROTOCOL_MODE, con_handle, instance->hid_protocol_mode);
243     }
244 
245     if (att_handle == instance->hid_control_point_value_handle){
246         if (buffer_size < 1u){
247             return ATT_ERROR_INVALID_OFFSET;
248         }
249         instance->hid_control_point_suspend = buffer[0];
250         instance->con_handle = con_handle;
251         log_info("Set suspend tp: %u", instance->hid_control_point_suspend );
252         if (instance->hid_control_point_suspend == 0u){
253             hids_device_emit_event(HIDS_SUBEVENT_SUSPEND, con_handle);
254         } else if (instance->hid_control_point_suspend == 1u){
255             hids_device_emit_event(HIDS_SUBEVENT_EXIT_SUSPEND, con_handle);
256         }
257     }
258 
259     hids_device_report_t * report = hids_device_get_report_for_client_configuration_handle(instance, att_handle);
260     if (report != NULL){
261         uint16_t new_value = little_endian_read_16(buffer, 0);
262         report->client_configuration_value = new_value;
263         log_info("Enable Report (type %u) notifications: %x", (uint8_t) report->type, new_value);
264 
265         switch (report->type){
266             case HID_REPORT_TYPE_INPUT:
267                 hids_device_emit_event_with_uint8(HIDS_SUBEVENT_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
268                 break;
269             case HID_REPORT_TYPE_OUTPUT:
270                 hids_device_emit_event_with_uint8(HIDS_SUBEVENT_OUTPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
271                 break;
272             case HID_REPORT_TYPE_FEATURE:
273                 hids_device_emit_event_with_uint8(HIDS_SUBEVENT_FEATURE_REPORT_ENABLE, con_handle, (uint8_t) new_value);
274                 break;
275             default:
276                 btstack_unreachable();
277                 break;
278         }
279     }
280     return 0;
281 }
282 
283 /**
284  * @brief Set up HIDS Device
285  */
286 void hids_device_init(uint8_t country_code, const uint8_t * descriptor, uint16_t descriptor_size){
287     hids_device_t * instance = hids_device_create_instance();
288 
289     instance->hid_country_code    = country_code;
290     instance->hid_descriptor      = descriptor;
291     instance->hid_descriptor_size = descriptor_size;
292 
293     // default
294     instance->hid_protocol_mode   = 1;
295 
296     // get service handle range
297     uint16_t start_handle = 0;
298     uint16_t end_handle   = 0xffff;
299     int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE, &start_handle, &end_handle);
300 	btstack_assert(service_found != 0);
301 	UNUSED(service_found);
302 
303     // get report map handle
304     instance->hid_report_map_handle                               = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP);
305 
306     // get report map handle
307     instance->hid_protocol_mode_value_handle                      = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_PROTOCOL_MODE);
308 
309     // get value and client configuration handles for boot mouse input, boot keyboard input and report input
310     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);
311     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);
312 
313     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);
314     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);
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_control_point_value_handle                      0x%02x", instance->hid_control_point_value_handle);
325 
326     instance->hid_reports = hid_reports_storage;
327 
328     uint16_t hid_reports_num = sizeof(hid_reports_storage)/sizeof(hids_device_report_t);
329     uint16_t assigned_reports_num = 0;
330     uint16_t start_chr_handle = start_handle;
331 
332     while ( (start_chr_handle < end_handle) && (assigned_reports_num < hid_reports_num)) {
333         uint16_t chr_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_chr_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT);
334         if (chr_value_handle == 0){
335             break;
336         }
337 
338         uint16_t chr_client_configuration_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_chr_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT);
339         uint16_t report_reference_handle = gatt_server_get_descriptor_handle_for_characteristic_with_uuid16(start_chr_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT, ORG_BLUETOOTH_DESCRIPTOR_REPORT_REFERENCE);
340         uint16_t report_reference_value_len;
341         const uint8_t * report_reference_value = gatt_server_get_const_value_for_handle(report_reference_handle, &report_reference_value_len);
342 
343         if (report_reference_value == NULL){
344             break;
345         }
346         if (report_reference_value_len != 2){
347             break;
348         }
349 
350         hids_device_report_t * report = &hid_reports_storage[assigned_reports_num];
351         report->value_handle = chr_value_handle;
352         report->client_configuration_handle = chr_client_configuration_handle;
353         report->client_configuration_value = 0;
354 
355         report->id = report_reference_value[0];
356         report->type = (hid_report_type_t)report_reference_value[1];
357 
358         switch (report->type){
359             case HID_REPORT_TYPE_INPUT:
360                 instance->hid_input_reports_num++;
361                 break;
362             case HID_REPORT_TYPE_OUTPUT:
363                 instance->hid_output_reports_num++;
364                 break;
365             case HID_REPORT_TYPE_FEATURE:
366                 instance->hid_feature_reports_num++;
367                 break;
368             default:
369                 btstack_unreachable();
370                 return;
371         }
372         log_info("hid_report_value_handle                       0x%02x, id %u, type %u", report->value_handle, report->id, (uint8_t)report->type);
373         log_info("hid_report_client_configuration_handle        0x%02x", report->client_configuration_handle);
374 
375         assigned_reports_num++;
376         start_chr_handle = chr_client_configuration_handle + 1;
377     }
378 
379     // register service with ATT Server
380     hid_service.start_handle   = start_handle;
381     hid_service.end_handle     = end_handle;
382     hid_service.read_callback  = &att_read_callback;
383     hid_service.write_callback = &att_write_callback;
384     att_server_register_service_handler(&hid_service);
385 }
386 
387 /**
388  * @brief Register callback for the HIDS Device client.
389  * @param callback
390  */
391 void hids_device_register_packet_handler(btstack_packet_handler_t callback){
392     packet_handler = callback;
393 }
394 
395 /**
396  * @brief Request can send now event to send HID Report
397  * Generates an HIDS_SUBEVENT_CAN_SEND_NOW subevent
398  * @param hid_cid
399  */
400 void hids_device_request_can_send_now_event(hci_con_handle_t con_handle){
401     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
402     if (!instance){
403         log_error("no instance for handle 0x%02x", con_handle);
404         return;
405     }
406 
407     instance->battery_callback.callback = &hids_device_can_send_now;
408     instance->battery_callback.context  = (void*) (uintptr_t) con_handle;
409     att_server_register_can_send_now_callback(&instance->battery_callback, con_handle);
410 }
411 
412 /**
413  * @brief Send HID Report: Input
414  */
415 
416 uint8_t hids_device_send_report_with_id(hci_con_handle_t con_handle, uint16_t report_id, 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 ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
421     }
422 
423     hids_device_report_t * report_storage = hids_device_get_report_for_id(instance, report_id);
424     if (report_storage == NULL){
425         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
426     }
427 
428     return att_server_notify(con_handle, report_storage->value_handle, report, report_len);
429 }
430 
431 static uint8_t hids_device_send_report_with_type(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len, hid_report_type_t report_type){
432     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
433     if (!instance){
434         log_error("no instance for handle 0x%02x", con_handle);
435         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
436     }
437 
438     uint8_t pos = 0;
439     while (pos < (instance->hid_input_reports_num + instance->hid_output_reports_num + instance->hid_feature_reports_num)){
440         hids_device_report_t * report_storage = &instance->hid_reports[pos];
441 
442         if (report_storage->type == report_type){
443             return att_server_notify(con_handle, report_storage->value_handle, report, report_len);
444         }
445     }
446     return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
447 }
448 
449 void hids_device_send_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
450     (void)hids_device_send_report_with_type(con_handle, report, report_len, HID_REPORT_TYPE_INPUT);
451 }
452 
453 /**
454  * @brief Send HID Report: Output
455  */
456 void hids_device_send_output_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
457     (void)hids_device_send_report_with_type(con_handle, report, report_len, HID_REPORT_TYPE_OUTPUT);
458 }
459 
460 /**
461  * @brief Send HID Report: Feature
462  */
463 void hids_device_send_feature_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
464     (void)hids_device_send_report_with_type(con_handle, report, report_len, HID_REPORT_TYPE_FEATURE);
465 }
466 
467 /**
468  * @brief Send HID Boot Mouse Input Report
469  */
470 void hids_device_send_boot_mouse_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
471     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
472     if (!instance){
473         log_error("no instance for handle 0x%02x", con_handle);
474         return;
475     }
476     att_server_notify(con_handle, instance->hid_boot_mouse_input_value_handle, report, report_len);
477 }
478 
479 /**
480  * @brief Send HID Boot Mouse Input Report
481  */
482 void hids_device_send_boot_keyboard_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
483     hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
484     if (!instance){
485         log_error("no instance for handle 0x%02x", con_handle);
486         return;
487     }
488     att_server_notify(con_handle, instance->hid_boot_keyboard_input_value_handle, report, report_len);
489 }
490