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 #include "btstack_hid_parser.h" 53 54 #define HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS 0x80 55 56 // storage for 'generic' HID Device with single Input, Output, and Feature Report 57 static hids_device_report_t hid_reports_generic_storage[3]; 58 59 typedef struct{ 60 uint16_t con_handle; 61 62 uint8_t hid_country_code; 63 const uint8_t * hid_descriptor; 64 uint16_t hid_descriptor_size; 65 66 uint16_t hid_report_map_handle; 67 uint8_t hid_protocol_mode; 68 uint16_t hid_protocol_mode_value_handle; 69 70 uint16_t hid_boot_mouse_input_value_handle; 71 uint16_t hid_boot_mouse_input_client_configuration_handle; 72 uint16_t hid_boot_mouse_input_client_configuration_value; 73 74 uint16_t hid_boot_keyboard_input_value_handle; 75 uint16_t hid_boot_keyboard_input_client_configuration_handle; 76 uint16_t hid_boot_keyboard_input_client_configuration_value; 77 78 hids_device_report_t * hid_reports; 79 80 uint8_t hid_input_reports_num; 81 uint8_t hid_output_reports_num; 82 uint8_t hid_feature_reports_num; 83 84 uint16_t hid_control_point_value_handle; 85 uint8_t hid_control_point_suspend; 86 87 btstack_context_callback_registration_t can_send_now_callback; 88 } hids_device_t; 89 90 static hids_device_t hids_device; 91 92 static btstack_packet_handler_t packet_handler; 93 static att_service_handler_t hid_service; 94 static void (*hids_device_get_report_callback)(hci_con_handle_t hid_cid, hid_report_type_t report_type, uint16_t report_id, uint16_t max_report_size, uint8_t * out_report); 95 96 // TODO: store hids device connection into list 97 static hids_device_t * hids_device_get_instance_for_con_handle(uint16_t con_handle){ 98 UNUSED(con_handle); 99 return &hids_device; 100 } 101 102 static hids_device_t * hids_device_create_instance(void){ 103 memset(&hids_device, 0, sizeof(hids_device_t)); 104 return &hids_device; 105 } 106 107 static hids_device_report_t * hids_device_get_report_for_value_handle(hids_device_t * device, uint16_t value_handle){ 108 uint8_t pos; 109 uint8_t total_reports = device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num; 110 for (pos = 0 ; pos < total_reports ; pos++){ 111 if (device->hid_reports[pos].value_handle == value_handle){ 112 return &device->hid_reports[pos]; 113 } 114 } 115 return NULL; 116 } 117 118 static hids_device_report_t * hids_device_get_report_for_client_configuration_handle(hids_device_t * device, uint16_t client_configuration_handle){ 119 uint8_t pos; 120 uint8_t total_reports = device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num; 121 for (pos = 0 ; pos < total_reports ; pos++){ 122 if (device->hid_reports[pos].client_configuration_handle == client_configuration_handle){ 123 return &device->hid_reports[pos]; 124 } 125 } 126 return NULL; 127 } 128 129 static hids_device_report_t * 130 hids_device_get_report_for_id_and_type(hids_device_t *device, uint16_t report_id, hid_report_type_t type) { 131 uint8_t pos; 132 uint8_t total_reports = device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num; 133 for (pos = 0 ; pos < total_reports ; pos++){ 134 if ((device->hid_reports[pos].type == type) && (device->hid_reports[pos].id == report_id)){ 135 return &device->hid_reports[pos]; 136 } 137 } 138 return NULL; 139 } 140 141 static void hids_device_emit_event_with_uint8(uint8_t event, hci_con_handle_t con_handle, uint8_t value){ 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[6]; 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 buffer[5] = value; 155 (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer)); 156 } 157 158 static void hids_device_emit_event_with_uint8_uint8_t(uint8_t event, hci_con_handle_t con_handle, uint8_t value_1, uint8_t value_2){ 159 hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 160 if (!instance){ 161 log_error("no instance for handle 0x%02x", con_handle); 162 return; 163 } 164 165 if (!packet_handler) return; 166 uint8_t buffer[7]; 167 buffer[0] = HCI_EVENT_HIDS_META; 168 buffer[1] = 4; 169 buffer[2] = event; 170 little_endian_store_16(buffer, 3, (uint16_t) con_handle); 171 buffer[5] = value_1; 172 buffer[6] = value_2; 173 (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer)); 174 } 175 176 static void hids_device_emit_event(uint8_t event, hci_con_handle_t con_handle){ 177 hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 178 if (!instance){ 179 log_error("no instance for handle 0x%02x", con_handle); 180 return; 181 } 182 183 if (!packet_handler) return; 184 uint8_t buffer[5]; 185 buffer[0] = HCI_EVENT_HIDS_META; 186 buffer[1] = 4; 187 buffer[2] = event; 188 little_endian_store_16(buffer, 3, (uint16_t) con_handle); 189 (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer)); 190 } 191 192 static void hids_device_can_send_now(void * context){ 193 hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 194 // notify client 195 hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 196 if (!instance){ 197 log_error("no instance for handle 0x%02x", con_handle); 198 return; 199 } 200 201 if (!packet_handler) return; 202 uint8_t buffer[5]; 203 buffer[0] = HCI_EVENT_HIDS_META; 204 buffer[1] = 3; 205 buffer[2] = HIDS_SUBEVENT_CAN_SEND_NOW; 206 little_endian_store_16(buffer, 3, (uint16_t) con_handle); 207 (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer)); 208 } 209 210 // ATT Client Read Callback for Dynamic Data 211 // - if buffer == NULL, don't copy data, just return size of value 212 // - if buffer != NULL, copy data and return number bytes copied 213 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){ 214 hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 215 if (!instance){ 216 log_error("no instance for handle 0x%02x", con_handle); 217 return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS; 218 } 219 220 if (att_handle == instance->hid_protocol_mode_value_handle){ 221 log_info("Read protocol mode"); 222 return att_read_callback_handle_byte(instance->hid_protocol_mode, offset, buffer, buffer_size); 223 } 224 225 if (att_handle == instance->hid_report_map_handle){ 226 log_info("Read report map"); 227 return att_read_callback_handle_blob(instance->hid_descriptor, instance->hid_descriptor_size, offset, buffer, buffer_size); 228 } 229 230 if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){ 231 return att_read_callback_handle_little_endian_16(instance->hid_boot_mouse_input_client_configuration_value, offset, buffer, buffer_size); 232 } 233 234 if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){ 235 return att_read_callback_handle_little_endian_16(instance->hid_boot_keyboard_input_client_configuration_value, offset, buffer, buffer_size); 236 } 237 238 if (att_handle == instance->hid_control_point_value_handle){ 239 if (buffer && (buffer_size >= 1u)){ 240 buffer[0] = instance->hid_control_point_suspend; 241 } 242 return 1; 243 } 244 245 uint8_t boot_report_size = 0; 246 if (att_handle == instance->hid_boot_mouse_input_value_handle){ 247 boot_report_size = 3; 248 } 249 if (att_handle == instance->hid_boot_keyboard_input_value_handle){ 250 boot_report_size = 8; 251 } 252 if (boot_report_size != 0){ 253 // no callback, no report 254 if (hids_device_get_report_callback == NULL){ 255 return 0; 256 } 257 // answer length request by ATT Server 258 if (buffer == NULL){ 259 return boot_report_size; 260 } else { 261 // Report ID 0, Type Input 262 (*hids_device_get_report_callback)(con_handle, HID_REPORT_TYPE_INPUT, 0, boot_report_size, buffer); 263 return boot_report_size; 264 } 265 } 266 267 hids_device_report_t * report; 268 report = hids_device_get_report_for_value_handle(instance, att_handle); 269 if (report != NULL){ 270 // no callback, no report 271 if (hids_device_get_report_callback == NULL){ 272 return 0; 273 } 274 // answer length request by ATT Server 275 if (buffer == NULL){ 276 return report->size; 277 } else { 278 uint16_t max_size = btstack_min(report->size, buffer_size); 279 (*hids_device_get_report_callback)(con_handle, report->type, report->id, max_size, buffer); 280 return report->size; 281 } 282 } 283 284 report = hids_device_get_report_for_client_configuration_handle(instance, att_handle); 285 if (report != NULL){ 286 return att_read_callback_handle_little_endian_16(report->client_configuration_value, offset, buffer, buffer_size); 287 } 288 return 0; 289 } 290 291 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){ 292 UNUSED(buffer_size); 293 UNUSED(offset); 294 295 if (transaction_mode != ATT_TRANSACTION_MODE_NONE){ 296 return 0; 297 } 298 299 hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 300 if (!instance){ 301 log_error("no instance for handle 0x%02x", con_handle); 302 return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS; 303 } 304 305 if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){ 306 uint16_t new_value = little_endian_read_16(buffer, 0); 307 instance->hid_boot_mouse_input_client_configuration_value = new_value; 308 hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_MOUSE_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value); 309 return 0; 310 } 311 if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){ 312 uint16_t new_value = little_endian_read_16(buffer, 0); 313 instance->hid_boot_keyboard_input_client_configuration_value = new_value; 314 hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value); 315 return 0; 316 } 317 318 if (att_handle == instance->hid_protocol_mode_value_handle){ 319 instance->hid_protocol_mode = buffer[0]; 320 log_info("Set protocol mode: %u", instance->hid_protocol_mode); 321 hids_device_emit_event_with_uint8(HIDS_SUBEVENT_PROTOCOL_MODE, con_handle, instance->hid_protocol_mode); 322 return 0; 323 } 324 325 if (att_handle == instance->hid_control_point_value_handle){ 326 if (buffer_size < 1u){ 327 return ATT_ERROR_INVALID_OFFSET; 328 } 329 instance->hid_control_point_suspend = buffer[0]; 330 instance->con_handle = con_handle; 331 log_info("Set suspend tp: %u", instance->hid_control_point_suspend ); 332 if (instance->hid_control_point_suspend == 0u){ 333 hids_device_emit_event(HIDS_SUBEVENT_SUSPEND, con_handle); 334 } else if (instance->hid_control_point_suspend == 1u){ 335 hids_device_emit_event(HIDS_SUBEVENT_EXIT_SUSPEND, con_handle); 336 } 337 return 0; 338 } 339 } 340 341 hids_device_report_t * report; 342 report = hids_device_get_report_for_value_handle(instance, att_handle); 343 if (report != NULL){ 344 // assemble event in buffer 345 uint8_t event[257]; 346 uint16_t pos = 0; 347 event[pos++] = HCI_EVENT_HIDS_META; 348 // skip length 349 pos++; 350 event[pos++] = HIDS_SUBEVENT_SET_REPORT; 351 little_endian_store_16(event, pos, con_handle); 352 pos += 2; 353 event[pos++] = report->id; 354 event[pos++] = (uint8_t) report->type; 355 uint8_t length_to_copy = btstack_min(buffer_size, 250); 356 event[pos++] = length_to_copy; 357 memcpy(&event[pos], buffer, length_to_copy); 358 pos += length_to_copy; 359 // set event length 360 event[1] = pos - 2; 361 (*packet_handler)(HCI_EVENT_PACKET, 0, event, pos); 362 return 0; 363 } 364 365 report = hids_device_get_report_for_client_configuration_handle(instance, att_handle); 366 if (report != NULL){ 367 uint16_t new_value = little_endian_read_16(buffer, 0); 368 report->client_configuration_value = new_value; 369 log_info("Enable Report (type %u) notifications: %x", (uint8_t) report->type, new_value); 370 371 switch (report->type){ 372 case HID_REPORT_TYPE_INPUT: 373 hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_INPUT_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value); 374 break; 375 case HID_REPORT_TYPE_OUTPUT: 376 hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_OUTPUT_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value); 377 break; 378 case HID_REPORT_TYPE_FEATURE: 379 hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_FEATURE_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value); 380 break; 381 default: 382 btstack_unreachable(); 383 break; 384 } 385 } 386 return 0; 387 } 388 389 void hids_device_init_with_storage(uint8_t hid_country_code, const uint8_t * hid_descriptor, uint16_t hid_descriptor_size, 390 uint16_t num_reports, hids_device_report_t * report_storage){ 391 392 hids_device_t * instance = hids_device_create_instance(); 393 394 btstack_assert(num_reports > 0); 395 btstack_assert(report_storage != NULL); 396 397 instance->hid_country_code = hid_country_code; 398 instance->hid_descriptor = hid_descriptor; 399 instance->hid_descriptor_size = hid_descriptor_size; 400 401 // default 402 instance->hid_protocol_mode = 1; 403 404 // get service handle range 405 uint16_t start_handle = 0; 406 uint16_t end_handle = 0xffff; 407 int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE, &start_handle, &end_handle); 408 btstack_assert(service_found != 0); 409 UNUSED(service_found); 410 411 // get report map handle 412 instance->hid_report_map_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP); 413 414 // get report map handle 415 instance->hid_protocol_mode_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_PROTOCOL_MODE); 416 417 // get value and client configuration handles for boot mouse input, boot keyboard input and report input 418 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); 419 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); 420 421 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); 422 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); 423 424 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); 425 426 log_info("hid_report_map_handle 0x%02x", instance->hid_report_map_handle); 427 log_info("hid_protocol_mode_value_handle 0x%02x", instance->hid_protocol_mode_value_handle); 428 log_info("hid_boot_mouse_input_value_handle 0x%02x", instance->hid_boot_mouse_input_value_handle); 429 log_info("hid_boot_mouse_input_client_configuration_handle 0x%02x", instance->hid_boot_mouse_input_client_configuration_handle); 430 log_info("hid_boot_keyboard_input_value_handle 0x%02x", instance->hid_boot_keyboard_input_value_handle); 431 log_info("hid_boot_keyboard_input_client_configuration_handle 0x%02x", instance->hid_boot_keyboard_input_client_configuration_handle); 432 log_info("hid_control_point_value_handle 0x%02x", instance->hid_control_point_value_handle); 433 434 instance->hid_reports = report_storage; 435 436 uint16_t hid_reports_num = num_reports; 437 uint16_t assigned_reports_num = 0; 438 uint16_t start_chr_handle = start_handle; 439 440 while ( (start_chr_handle < end_handle) && (assigned_reports_num < hid_reports_num)) { 441 // mandatory 442 uint16_t chr_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_chr_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT); 443 if (chr_value_handle == 0){ 444 break; 445 } 446 447 // optional 448 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); 449 450 // mandatory 451 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); 452 if (report_reference_handle == 0){ 453 break; 454 } 455 456 // get report id and type from report reference 457 uint16_t report_reference_value_len; 458 const uint8_t * report_reference_value = gatt_server_get_const_value_for_handle(report_reference_handle, &report_reference_value_len); 459 if (report_reference_value == NULL){ 460 break; 461 } 462 if (report_reference_value_len != 2){ 463 break; 464 } 465 uint8_t report_id = report_reference_value[0]; 466 hid_report_type_t report_type = (hid_report_type_t) report_reference_value[1]; 467 468 // store report info 469 hids_device_report_t * report = &report_storage[assigned_reports_num]; 470 report->value_handle = chr_value_handle; 471 report->client_configuration_handle = chr_client_configuration_handle; 472 report->client_configuration_value = 0; 473 report->id = report_id; 474 report->type = report_type; 475 report->size = btstack_hid_get_report_size_for_id(report_id, report_type, hid_descriptor_size, hid_descriptor); 476 477 switch (report->type){ 478 case HID_REPORT_TYPE_INPUT: 479 instance->hid_input_reports_num++; 480 break; 481 case HID_REPORT_TYPE_OUTPUT: 482 instance->hid_output_reports_num++; 483 break; 484 case HID_REPORT_TYPE_FEATURE: 485 instance->hid_feature_reports_num++; 486 break; 487 default: 488 btstack_unreachable(); 489 return; 490 } 491 log_info("hid_report_value_handle 0x%02x, id %u, type %u", report->value_handle, report->id, (uint8_t)report->type); 492 if (report->client_configuration_handle != 0){ 493 log_info("hid_report_client_configuration_handle 0x%02x", report->client_configuration_handle); 494 } 495 496 assigned_reports_num++; 497 start_chr_handle = report_reference_handle + 1; 498 } 499 500 // register service with ATT Server 501 hid_service.start_handle = start_handle; 502 hid_service.end_handle = end_handle; 503 hid_service.read_callback = &att_read_callback; 504 hid_service.write_callback = &att_write_callback; 505 att_server_register_service_handler(&hid_service); 506 } 507 508 /** 509 * @brief Set up HIDS Device 510 */ 511 void hids_device_init(uint8_t country_code, const uint8_t * descriptor, uint16_t descriptor_size){ 512 uint16_t hid_reports_num = sizeof(hid_reports_generic_storage) / sizeof(hids_device_report_t); 513 hids_device_init_with_storage(country_code, descriptor, descriptor_size, hid_reports_num, hid_reports_generic_storage); 514 } 515 516 /** 517 * @brief Register callback for the HIDS Device client. 518 * @param callback 519 */ 520 void hids_device_register_packet_handler(btstack_packet_handler_t callback){ 521 packet_handler = callback; 522 } 523 524 void hids_device_register_get_report_callback(void (*callback)(hci_con_handle_t con_handle, hid_report_type_t report_type, uint16_t report_id, uint16_t max_report_size, uint8_t * out_report)){ 525 hids_device_get_report_callback = callback; 526 } 527 528 /** 529 * @brief Request can send now event to send HID Report 530 * Generates an HIDS_SUBEVENT_CAN_SEND_NOW subevent 531 * @param hid_cid 532 */ 533 void hids_device_request_can_send_now_event(hci_con_handle_t con_handle){ 534 hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 535 if (!instance){ 536 log_error("no instance for handle 0x%02x", con_handle); 537 return; 538 } 539 540 instance->can_send_now_callback.callback = &hids_device_can_send_now; 541 instance->can_send_now_callback.context = (void*) (uintptr_t) con_handle; 542 att_server_register_can_send_now_callback(&instance->can_send_now_callback, con_handle); 543 } 544 545 uint8_t hids_device_send_input_report_for_id(hci_con_handle_t con_handle, uint16_t report_id, const uint8_t * report, uint16_t report_len){ 546 hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 547 if (!instance){ 548 log_error("no instance for handle 0x%02x", con_handle); 549 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 550 } 551 552 hids_device_report_t * report_storage = hids_device_get_report_for_id_and_type(instance, report_id, 553 HID_REPORT_TYPE_INPUT); 554 if (report_storage == NULL){ 555 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 556 } 557 558 return att_server_notify(con_handle, report_storage->value_handle, report, report_len); 559 } 560 561 uint8_t hids_device_send_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){ 562 hids_device_t * device = hids_device_get_instance_for_con_handle(con_handle); 563 if (!device){ 564 log_error("no instance for handle 0x%02x", con_handle); 565 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 566 } 567 568 uint8_t pos; 569 uint8_t total_reports = device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num; 570 for (pos = 0 ; pos < total_reports ; pos++){ 571 hids_device_report_t * report_storage = &device->hid_reports[pos]; 572 if (report_storage->type == HID_REPORT_TYPE_INPUT){ 573 return att_server_notify(con_handle, report_storage->value_handle, report, report_len); 574 } 575 } 576 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 577 } 578 579 /** 580 * @brief Send HID Boot Mouse Input Report 581 */ 582 uint8_t hids_device_send_boot_mouse_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){ 583 hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 584 if (!instance){ 585 log_error("no instance for handle 0x%02x", con_handle); 586 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 587 } 588 return att_server_notify(con_handle, instance->hid_boot_mouse_input_value_handle, report, report_len); 589 } 590 591 /** 592 * @brief Send HID Boot Mouse Input Report 593 */ 594 uint8_t hids_device_send_boot_keyboard_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){ 595 hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 596 if (!instance){ 597 log_error("no instance for handle 0x%02x", con_handle); 598 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 599 } 600 return att_server_notify(con_handle, instance->hid_boot_keyboard_input_value_handle, report, report_len); 601 } 602