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 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_client.c" 39 40 #include "btstack_config.h" 41 42 #ifdef ENABLE_TESTING_SUPPORT 43 #include <stdio.h> 44 #endif 45 46 #include <stdint.h> 47 #include <string.h> 48 49 #include "ble/gatt-service/hids_client.h" 50 51 #include "btstack_memory.h" 52 #include "ble/core.h" 53 #include "ble/gatt_client.h" 54 #include "bluetooth_gatt.h" 55 #include "btstack_debug.h" 56 #include "btstack_event.h" 57 #include "btstack_run_loop.h" 58 #include "gap.h" 59 60 #define HID_REPORT_MODE_REPORT_ID 3 61 #define HID_REPORT_MODE_REPORT_MAP_ID 4 62 #define HID_REPORT_MODE_HID_INFORMATION_ID 5 63 #define HID_REPORT_MODE_HID_CONTROL_POINT_ID 6 64 65 static btstack_linked_list_t clients; 66 static uint16_t hids_cid_counter = 0; 67 68 static uint8_t * hids_client_descriptor_storage; 69 static uint16_t hids_client_descriptor_storage_len; 70 71 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 72 static void hids_client_handle_can_write_without_reponse(void * context); 73 74 #ifdef ENABLE_TESTING_SUPPORT 75 static char * hid_characteristic_name(uint16_t uuid){ 76 switch (uuid){ 77 case ORG_BLUETOOTH_CHARACTERISTIC_PROTOCOL_MODE: 78 return "PROTOCOL_MODE"; 79 80 case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_INPUT_REPORT: 81 return "BOOT_KEYBOARD_INPUT_REPORT"; 82 83 case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_MOUSE_INPUT_REPORT: 84 return "BOOT_MOUSE_INPUT_REPORT"; 85 86 case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_OUTPUT_REPORT: 87 return "BOOT_KEYBOARD_OUTPUT_REPORT"; 88 89 case ORG_BLUETOOTH_CHARACTERISTIC_REPORT: 90 return "REPORT"; 91 92 case ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP: 93 return "REPORT_MAP"; 94 95 case ORG_BLUETOOTH_CHARACTERISTIC_HID_INFORMATION: 96 return "HID_INFORMATION"; 97 98 case ORG_BLUETOOTH_CHARACTERISTIC_HID_CONTROL_POINT: 99 return "HID_CONTROL_POINT"; 100 default: 101 return "UKNOWN"; 102 } 103 } 104 #endif 105 106 static hids_client_t * hids_get_client_for_con_handle(hci_con_handle_t con_handle){ 107 btstack_linked_list_iterator_t it; 108 btstack_linked_list_iterator_init(&it, &clients); 109 while (btstack_linked_list_iterator_has_next(&it)){ 110 hids_client_t * client = (hids_client_t *)btstack_linked_list_iterator_next(&it); 111 if (client->con_handle != con_handle) continue; 112 return client; 113 } 114 return NULL; 115 } 116 117 static hids_client_t * hids_get_client_for_cid(uint16_t hids_cid){ 118 btstack_linked_list_iterator_t it; 119 btstack_linked_list_iterator_init(&it, &clients); 120 while (btstack_linked_list_iterator_has_next(&it)){ 121 hids_client_t * client = (hids_client_t *)btstack_linked_list_iterator_next(&it); 122 if (client->cid != hids_cid) continue; 123 return client; 124 } 125 return NULL; 126 } 127 128 129 // START Descriptor Storage Util 130 131 static uint16_t hids_client_descriptors_len(hids_client_t * client){ 132 uint16_t descriptors_len = 0; 133 uint8_t service_index; 134 for (service_index = 0; service_index < client->num_instances; service_index++){ 135 descriptors_len += client->services[service_index].hid_descriptor_len; 136 } 137 return descriptors_len; 138 } 139 140 static uint16_t hids_client_descriptor_storage_get_available_space(void){ 141 // assumes all descriptors are back to back 142 uint16_t free_space = hids_client_descriptor_storage_len; 143 btstack_linked_list_iterator_t it; 144 btstack_linked_list_iterator_init(&it, &clients); 145 while (btstack_linked_list_iterator_has_next(&it)){ 146 hids_client_t * client = (hids_client_t *)btstack_linked_list_iterator_next(&it); 147 free_space -= hids_client_descriptors_len(client); 148 } 149 return free_space; 150 } 151 152 static void hids_client_descriptor_storage_init(hids_client_t * client, uint8_t service_index){ 153 // reserve remaining space for this connection 154 uint16_t available_space = hids_client_descriptor_storage_get_available_space(); 155 client->services[service_index].hid_descriptor_len = 0; 156 client->services[service_index].hid_descriptor_max_len = available_space; 157 client->services[service_index].hid_descriptor_offset = hids_client_descriptor_storage_len - available_space; 158 } 159 160 static bool hids_client_descriptor_storage_store(hids_client_t * client, uint8_t service_index, uint8_t byte){ 161 // store single hid descriptor byte 162 if (client->services[service_index].hid_descriptor_len >= client->services[service_index].hid_descriptor_max_len) return false; 163 164 hids_client_descriptor_storage[client->services[service_index].hid_descriptor_offset + client->services[service_index].hid_descriptor_len] = byte; 165 client->services[service_index].hid_descriptor_len++; 166 return true; 167 } 168 169 static void hids_client_descriptor_storage_delete(hids_client_t * client){ 170 uint8_t service_index; 171 172 // calculate descriptors len 173 uint16_t descriptors_len = hids_client_descriptors_len(client); 174 175 if (descriptors_len > 0){ 176 // move higher descriptors down 177 uint16_t next_offset = client->services[0].hid_descriptor_offset + descriptors_len; 178 memmove(&hids_client_descriptor_storage[client->services[0].hid_descriptor_offset], 179 &hids_client_descriptor_storage[next_offset], 180 hids_client_descriptor_storage_len - next_offset); 181 182 // fix descriptor offset of higher descriptors 183 btstack_linked_list_iterator_t it; 184 btstack_linked_list_iterator_init(&it, &clients); 185 while (btstack_linked_list_iterator_has_next(&it)){ 186 hids_client_t * conn = (hids_client_t *)btstack_linked_list_iterator_next(&it); 187 if (conn == client) continue; 188 for (service_index = 0; service_index < client->num_instances; service_index++){ 189 if (conn->services[service_index].hid_descriptor_offset >= next_offset){ 190 conn->services[service_index].hid_descriptor_offset -= descriptors_len; 191 } 192 } 193 } 194 } 195 196 // clear descriptors 197 for (service_index = 0; service_index < client->num_instances; service_index++){ 198 client->services[service_index].hid_descriptor_len = 0; 199 client->services[service_index].hid_descriptor_offset = 0; 200 } 201 } 202 203 const uint8_t * hids_client_descriptor_storage_get_descriptor_data(uint16_t hids_cid, uint8_t service_index){ 204 hids_client_t * client = hids_get_client_for_cid(hids_cid); 205 if (client == NULL){ 206 return NULL; 207 } 208 if (service_index >= client->num_instances){ 209 return NULL; 210 } 211 return &hids_client_descriptor_storage[client->services[service_index].hid_descriptor_offset]; 212 } 213 214 uint16_t hids_client_descriptor_storage_get_descriptor_len(uint16_t hids_cid, uint8_t service_index){ 215 hids_client_t * client = hids_get_client_for_cid(hids_cid); 216 if (client == NULL){ 217 return 0; 218 } 219 if (service_index >= client->num_instances){ 220 return 0; 221 } 222 return client->services[service_index].hid_descriptor_len; 223 } 224 225 // END Descriptor Storage Util 226 227 static uint16_t hids_get_next_cid(void){ 228 if (hids_cid_counter == 0xffff) { 229 hids_cid_counter = 1; 230 } else { 231 hids_cid_counter++; 232 } 233 return hids_cid_counter; 234 } 235 236 static uint8_t find_report_index_for_value_handle(hids_client_t * client, uint16_t value_handle){ 237 uint8_t i; 238 for (i = 0; i < client->num_reports; i++){ 239 if (client->reports[i].value_handle == value_handle){ 240 return i; 241 } 242 } 243 return HIDS_CLIENT_INVALID_REPORT_INDEX; 244 } 245 246 static uint8_t find_external_report_index_for_value_handle(hids_client_t * client, uint16_t value_handle){ 247 uint8_t i; 248 for (i = 0; i < client->num_external_reports; i++){ 249 if (client->external_reports[i].value_handle == value_handle){ 250 return i; 251 } 252 } 253 return HIDS_CLIENT_INVALID_REPORT_INDEX; 254 } 255 256 static bool external_report_index_for_uuid_exists(hids_client_t * client, uint16_t uuid16){ 257 uint8_t i; 258 for (i = 0; i < client->num_external_reports; i++){ 259 if (client->external_reports[i].external_report_reference_uuid == uuid16){ 260 return true; 261 } 262 } 263 return false; 264 } 265 266 static uint8_t find_report_index_for_report_id_and_report_type(hids_client_t * client, uint8_t report_id, hid_report_type_t report_type){ 267 uint8_t i; 268 269 for (i = 0; i < client->num_reports; i++){ 270 hids_client_report_t report = client->reports[i]; 271 hid_protocol_mode_t protocol_mode = client->services[report.service_index].protocol_mode; 272 273 if (protocol_mode == HID_PROTOCOL_MODE_BOOT){ 274 if (!client->reports[i].boot_report){ 275 continue; 276 } 277 } else if (protocol_mode == HID_PROTOCOL_MODE_REPORT){ 278 if (client->reports[i].boot_report){ 279 continue; 280 } 281 } 282 283 if (report.report_id == report_id && report.report_type == report_type){ 284 return i; 285 } 286 } 287 return HIDS_CLIENT_INVALID_REPORT_INDEX; 288 } 289 290 static uint8_t hids_client_add_characteristic(hids_client_t * client, gatt_client_characteristic_t * characteristic, uint8_t report_id, hid_report_type_t report_type, bool boot_report){ 291 292 uint8_t report_index = find_external_report_index_for_value_handle(client, characteristic->value_handle); 293 if (report_index != HIDS_CLIENT_INVALID_REPORT_INDEX){ 294 return report_index; 295 } 296 report_index = client->num_reports; 297 298 if (report_index < HIDS_CLIENT_NUM_REPORTS) { 299 client->reports[report_index].value_handle = characteristic->value_handle; 300 client->reports[report_index].end_handle = characteristic->end_handle; 301 client->reports[report_index].properties = characteristic->properties; 302 303 client->reports[report_index].service_index = client->service_index; 304 client->reports[report_index].report_id = report_id; 305 client->reports[report_index].report_type = report_type; 306 client->reports[report_index].boot_report = boot_report; 307 308 log_info("add index %d, id %d, type %d, value handle 0x%02x, properties 0x%02x", report_index, report_id, report_type, characteristic->value_handle, characteristic->properties); 309 client->num_reports++; 310 return report_index; 311 } else { 312 log_info("not enough storage, increase HIDS_CLIENT_NUM_REPORTS"); 313 return HIDS_CLIENT_INVALID_REPORT_INDEX; 314 } 315 } 316 317 static uint8_t hids_client_add_external_report(hids_client_t * client, gatt_client_characteristic_descriptor_t * characteristic_descriptor){ 318 uint8_t report_index = client->num_external_reports; 319 320 if (report_index < HIDS_CLIENT_NUM_REPORTS) { 321 client->external_reports[report_index].value_handle = characteristic_descriptor->handle; 322 client->external_reports[report_index].service_index = client->service_index; 323 324 client->num_external_reports++; 325 log_info("add external index %d [%d], value handle 0x%02x", report_index, client->num_external_reports, characteristic_descriptor->handle); 326 return report_index; 327 } else { 328 log_info("not enough storage, increase HIDS_CLIENT_NUM_REPORTS"); 329 return HIDS_CLIENT_INVALID_REPORT_INDEX; 330 } 331 } 332 333 static uint8_t hids_client_get_next_active_report_map_index(hids_client_t * client){ 334 uint8_t i; 335 for (i = client->service_index; i < client->num_instances; i++){ 336 if (client->services[i].report_map_value_handle != 0){ 337 return i; 338 } 339 } 340 client->service_index = HIDS_CLIENT_INVALID_REPORT_INDEX; 341 return HIDS_CLIENT_INVALID_REPORT_INDEX; 342 } 343 344 static bool hids_client_report_query_next_report_map(hids_client_t * client){ 345 client->service_index++; 346 if (hids_client_get_next_active_report_map_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){ 347 client->state = HIDS_CLIENT_STATE_W2_READ_REPORT_MAP_HID_DESCRIPTOR; 348 return true; 349 } 350 return false; 351 } 352 353 static bool hids_client_report_map_query_init(hids_client_t * client){ 354 client->service_index = 0; 355 356 if (hids_client_get_next_active_report_map_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){ 357 client->state = HIDS_CLIENT_STATE_W2_READ_REPORT_MAP_HID_DESCRIPTOR; 358 return true; 359 } 360 return false; 361 } 362 363 static bool hids_client_report_query_next_report_map_uuid(hids_client_t * client){ 364 client->report_index++; 365 if (client->report_index < client->num_external_reports){ 366 client->state = HIDS_CLIENT_STATE_W2_REPORT_MAP_READ_EXTERNAL_REPORT_REFERENCE_UUID; 367 return true; 368 } 369 return false; 370 } 371 372 static bool hids_client_report_map_uuid_query_init(hids_client_t * client){ 373 client->report_index = 0; 374 if (client->num_external_reports > 0){ 375 client->state = HIDS_CLIENT_STATE_W2_REPORT_MAP_READ_EXTERNAL_REPORT_REFERENCE_UUID; 376 return true; 377 } 378 return false; 379 } 380 381 static uint8_t hids_client_get_next_report_index(hids_client_t * client){ 382 uint8_t i; 383 uint8_t index = HIDS_CLIENT_INVALID_REPORT_INDEX; 384 385 for (i = client->report_index; i < client->num_reports && (index == HIDS_CLIENT_INVALID_REPORT_INDEX); i++){ 386 hids_client_report_t report = client->reports[i]; 387 if (!report.boot_report){ 388 if (report.report_type == HID_REPORT_TYPE_RESERVED && report.report_id == HID_REPORT_MODE_REPORT_ID){ 389 index = i; 390 client->service_index = report.service_index; 391 break; 392 } 393 } 394 } 395 client->report_index = index; 396 return index; 397 } 398 399 static bool hids_client_report_query_next_report(hids_client_t * client){ 400 client->report_index++; 401 if (hids_client_get_next_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){ 402 client->state = HIDS_CLIENT_STATE_W2_FIND_REPORT; 403 return true; 404 } 405 return false; 406 } 407 408 static bool hids_client_report_query_init(hids_client_t * client){ 409 client->report_index = 0; 410 411 if (hids_client_get_next_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){ 412 client->state = HIDS_CLIENT_STATE_W2_FIND_REPORT; 413 return true; 414 } 415 return false; 416 } 417 418 static uint8_t hids_client_get_next_notification_report_index(hids_client_t * client){ 419 uint8_t i; 420 uint8_t index = HIDS_CLIENT_INVALID_REPORT_INDEX; 421 422 for (i = client->report_index; i < client->num_reports && (index == HIDS_CLIENT_INVALID_REPORT_INDEX); i++){ 423 hids_client_report_t report = client->reports[i]; 424 hid_protocol_mode_t protocol_mode = client->services[report.service_index].protocol_mode; 425 426 if (protocol_mode == HID_PROTOCOL_MODE_BOOT){ 427 if (!client->reports[i].boot_report){ 428 continue; 429 } 430 } else if (protocol_mode == HID_PROTOCOL_MODE_REPORT){ 431 if (client->reports[i].boot_report){ 432 continue; 433 } 434 } 435 if (report.report_type == HID_REPORT_TYPE_INPUT){ 436 index = i; 437 } 438 } 439 client->report_index = index; 440 return index; 441 } 442 443 static bool hids_client_report_next_notification_report_index(hids_client_t * client){ 444 client->report_index++; 445 if (hids_client_get_next_notification_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){ 446 client->state = HIDS_CLIENT_STATE_W2_ENABLE_INPUT_REPORTS; 447 return true; 448 } 449 return false; 450 } 451 452 static bool hids_client_report_notifications_init(hids_client_t * client){ 453 #ifdef ENABLE_TESTING_SUPPORT 454 printf("\nRegister for Notifications: \n"); 455 #endif 456 client->report_index = 0; 457 458 if (hids_client_get_next_notification_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){ 459 client->state = HIDS_CLIENT_STATE_W2_ENABLE_INPUT_REPORTS; 460 return true; 461 } 462 return false; 463 } 464 465 static bool hids_client_report_next_notifications_configuration_report_index(hids_client_t * client){ 466 client->report_index++; 467 if (hids_client_get_next_notification_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){ 468 client->state = HIDS_CLIENT_STATE_W2_CONFIGURE_NOTIFICATIONS; 469 return true; 470 } 471 return false; 472 } 473 474 static bool hids_client_notifications_configuration_init(hids_client_t * client){ 475 #ifdef ENABLE_TESTING_SUPPORT 476 printf("\nConfigure for Notifications: \n"); 477 #endif 478 client->report_index = 0; 479 480 if (hids_client_get_next_notification_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){ 481 client->state = HIDS_CLIENT_STATE_W2_CONFIGURE_NOTIFICATIONS; 482 return true; 483 } 484 return false; 485 } 486 487 static hids_client_t * hids_create_client(hci_con_handle_t con_handle, uint16_t cid){ 488 hids_client_t * client = btstack_memory_hids_client_get(); 489 if (!client){ 490 log_error("Not enough memory to create client"); 491 return NULL; 492 } 493 client->state = HIDS_CLIENT_STATE_IDLE; 494 client->cid = cid; 495 client->con_handle = con_handle; 496 497 btstack_linked_list_add(&clients, (btstack_linked_item_t *) client); 498 return client; 499 } 500 501 static void hids_finalize_client(hids_client_t * client){ 502 // stop listening 503 uint8_t i; 504 for (i = 0; i < client->num_reports; i++){ 505 gatt_client_stop_listening_for_characteristic_value_updates(&client->reports[i].notification_listener); 506 } 507 508 hids_client_descriptor_storage_delete(client); 509 btstack_linked_list_remove(&clients, (btstack_linked_item_t *) client); 510 btstack_memory_hids_client_free(client); 511 } 512 513 514 static void hids_emit_connection_established(hids_client_t * client, uint8_t status){ 515 uint8_t event[8]; 516 int pos = 0; 517 event[pos++] = HCI_EVENT_GATTSERVICE_META; 518 event[pos++] = sizeof(event) - 2; 519 event[pos++] = GATTSERVICE_SUBEVENT_HID_SERVICE_CONNECTED; 520 little_endian_store_16(event, pos, client->cid); 521 pos += 2; 522 event[pos++] = status; 523 event[pos++] = client->services[0].protocol_mode; 524 event[pos++] = client->num_instances; 525 (*client->client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 526 } 527 528 static void hids_emit_notifications_configuration(hids_client_t * client){ 529 uint8_t event[6]; 530 int pos = 0; 531 event[pos++] = HCI_EVENT_GATTSERVICE_META; 532 event[pos++] = sizeof(event) - 2; 533 event[pos++] = GATTSERVICE_SUBEVENT_HID_SERVICE_REPORTS_NOTIFICATION; 534 little_endian_store_16(event, pos, client->cid); 535 pos += 2; 536 event[pos++] = client->value; 537 (*client->client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 538 } 539 540 static uint16_t hids_client_setup_report_event(hids_client_t * client, uint8_t report_index, uint8_t *buffer, uint16_t report_len){ 541 uint16_t pos = 0; 542 buffer[pos++] = HCI_EVENT_GATTSERVICE_META; 543 pos++; // skip len 544 buffer[pos++] = GATTSERVICE_SUBEVENT_HID_REPORT; 545 little_endian_store_16(buffer, pos, client->cid); 546 pos += 2; 547 buffer[pos++] = client->reports[report_index].service_index; 548 buffer[pos++] = client->reports[report_index].report_id; 549 little_endian_store_16(buffer, pos, report_len); 550 pos += 2; 551 buffer[1] = pos + report_len - 2; 552 return pos; 553 } 554 555 static void hids_client_setup_report_event_with_report_id(hids_client_t * client, uint8_t report_index, uint8_t *buffer, uint16_t report_len){ 556 uint16_t pos = hids_client_setup_report_event(client, report_index, buffer, report_len + 1); 557 buffer[pos] = client->reports[report_index].report_id; 558 } 559 560 static void hids_client_emit_hid_information_event(hids_client_t * client, const uint8_t *value, uint16_t value_len){ 561 if (value_len != 4) return; 562 563 uint8_t event[11]; 564 int pos = 0; 565 event[pos++] = HCI_EVENT_GATTSERVICE_META; 566 event[pos++] = sizeof(event) - 2; 567 event[pos++] = GATTSERVICE_SUBEVENT_HID_INFORMATION; 568 little_endian_store_16(event, pos, client->cid); 569 pos += 2; 570 event[pos++] = client->service_index; 571 572 memcpy(event+pos, value, 3); 573 pos += 3; 574 event[pos++] = (value[3] & 0x02) >> 1; 575 event[pos++] = value[3] & 0x01; 576 577 (*client->client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 578 } 579 580 static void hids_client_emit_protocol_mode_event(hids_client_t * client, const uint8_t *value, uint16_t value_len){ 581 if (value_len != 1) return; 582 583 uint8_t event[11]; 584 int pos = 0; 585 event[pos++] = HCI_EVENT_GATTSERVICE_META; 586 event[pos++] = sizeof(event) - 2; 587 event[pos++] = GATTSERVICE_SUBEVENT_HID_PROTOCOL_MODE; 588 little_endian_store_16(event, pos, client->cid); 589 pos += 2; 590 event[pos++] = client->service_index; 591 event[pos++] = value[0]; 592 (*client->client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 593 } 594 595 static void handle_notification_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { 596 UNUSED(packet_type); 597 UNUSED(channel); 598 599 if (hci_event_packet_get_type(packet) != GATT_EVENT_NOTIFICATION) return; 600 601 hids_client_t * client = hids_get_client_for_con_handle(gatt_event_notification_get_handle(packet)); 602 if (client == NULL) return; 603 604 uint8_t report_index = find_report_index_for_value_handle(client, gatt_event_notification_get_value_handle(packet)); 605 if (report_index == HIDS_CLIENT_INVALID_REPORT_INDEX){ 606 return; 607 } 608 609 uint8_t * in_place_event = &packet[-2]; 610 hids_client_setup_report_event_with_report_id(client, report_index, in_place_event, 611 gatt_event_notification_get_value_length(packet)); 612 (*client->client_handler)(HCI_EVENT_GATTSERVICE_META, client->cid, in_place_event, size + 2); 613 } 614 615 static void handle_report_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { 616 UNUSED(packet_type); 617 UNUSED(channel); 618 619 if (hci_event_packet_get_type(packet) != GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT) return; 620 621 hids_client_t * client = hids_get_client_for_con_handle(gatt_event_characteristic_value_query_result_get_handle(packet)); 622 if (client == NULL) return; 623 624 if (client->state != HIDS_CLIENT_W4_GET_REPORT_RESULT){ 625 return; 626 } 627 client->state = HIDS_CLIENT_STATE_CONNECTED; 628 629 uint8_t report_index = find_report_index_for_value_handle(client, gatt_event_characteristic_value_query_result_get_value_handle(packet)); 630 if (report_index == HIDS_CLIENT_INVALID_REPORT_INDEX){ 631 return; 632 } 633 634 uint8_t * in_place_event = &packet[-2]; 635 hids_client_setup_report_event(client, report_index, in_place_event, gatt_event_characteristic_value_query_result_get_value_length(packet)); 636 (*client->client_handler)(HCI_EVENT_GATTSERVICE_META, client->cid, in_place_event, size + 2); 637 } 638 639 static void hids_run_for_client(hids_client_t * client){ 640 uint8_t att_status; 641 gatt_client_service_t service; 642 gatt_client_characteristic_t characteristic; 643 644 switch (client->state){ 645 case HIDS_CLIENT_STATE_W2_QUERY_SERVICE: 646 #ifdef ENABLE_TESTING_SUPPORT 647 printf("\n\nQuery Services:\n"); 648 #endif 649 client->state = HIDS_CLIENT_STATE_W4_SERVICE_RESULT; 650 651 // result in GATT_EVENT_SERVICE_QUERY_RESULT 652 att_status = gatt_client_discover_primary_services_by_uuid16(handle_gatt_client_event, client->con_handle, ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE); 653 UNUSED(att_status); 654 break; 655 656 case HIDS_CLIENT_STATE_W2_QUERY_CHARACTERISTIC: 657 #ifdef ENABLE_TESTING_SUPPORT 658 printf("\n\nQuery Characteristics of service %d:\n", client->service_index); 659 #endif 660 client->state = HIDS_CLIENT_STATE_W4_CHARACTERISTIC_RESULT; 661 662 service.start_group_handle = client->services[client->service_index].start_handle; 663 service.end_group_handle = client->services[client->service_index].end_handle; 664 665 // result in GATT_EVENT_CHARACTERISTIC_QUERY_RESULT 666 att_status = gatt_client_discover_characteristics_for_service(&handle_gatt_client_event, client->con_handle, &service); 667 668 UNUSED(att_status); 669 break; 670 671 case HIDS_CLIENT_STATE_W2_READ_REPORT_MAP_HID_DESCRIPTOR: 672 #ifdef ENABLE_TESTING_SUPPORT 673 printf("\n\nRead REPORT_MAP (Handle 0x%04X) HID Descriptor of service %d:\n", client->services[client->service_index].report_map_value_handle, client->service_index); 674 #endif 675 client->state = HIDS_CLIENT_STATE_W4_REPORT_MAP_HID_DESCRIPTOR; 676 677 // result in GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT 678 att_status = gatt_client_read_long_value_of_characteristic_using_value_handle(&handle_gatt_client_event, client->con_handle, client->services[client->service_index].report_map_value_handle); 679 UNUSED(att_status); 680 break; 681 682 case HIDS_CLIENT_STATE_W2_REPORT_MAP_DISCOVER_CHARACTERISTIC_DESCRIPTORS: 683 #ifdef ENABLE_TESTING_SUPPORT 684 printf("\nDiscover REPORT_MAP (Handle 0x%04X) Characteristic Descriptors of service %d:\n", client->services[client->service_index].report_map_value_handle, client->service_index); 685 #endif 686 client->state = HIDS_CLIENT_STATE_W4_REPORT_MAP_CHARACTERISTIC_DESCRIPTORS_RESULT; 687 688 characteristic.value_handle = client->services[client->service_index].report_map_value_handle; 689 characteristic.end_handle = client->services[client->service_index].report_map_end_handle; 690 691 // result in GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT 692 att_status = gatt_client_discover_characteristic_descriptors(&handle_gatt_client_event, client->con_handle, &characteristic); 693 UNUSED(att_status); 694 break; 695 696 case HIDS_CLIENT_STATE_W2_REPORT_MAP_READ_EXTERNAL_REPORT_REFERENCE_UUID: 697 #ifdef ENABLE_TESTING_SUPPORT 698 printf("\nRead external chr UUID (Handle 0x%04X) Characteristic Descriptors, service index %d:\n", client->external_reports[client->report_index].value_handle, client->service_index); 699 #endif 700 client->state = HIDS_CLIENT_STATE_W4_REPORT_MAP_EXTERNAL_REPORT_REFERENCE_UUID; 701 702 // result in GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT 703 att_status = gatt_client_read_characteristic_descriptor_using_descriptor_handle(&handle_gatt_client_event, client->con_handle, client->external_reports[client->report_index].value_handle); 704 UNUSED(att_status); 705 break; 706 707 case HIDS_CLIENT_STATE_W2_DISCOVER_EXTERNAL_REPORT_CHARACTERISTIC: 708 #ifdef ENABLE_TESTING_SUPPORT 709 printf("\nDiscover External Report Characteristic:\n"); 710 #endif 711 client->state = HIDS_CLIENT_STATE_W4_EXTERNAL_REPORT_CHARACTERISTIC_RESULT; 712 713 service.start_group_handle = 0x0001; 714 service.end_group_handle = 0xffff; 715 716 // Result in GATT_EVENT_CHARACTERISTIC_QUERY_RESULT 717 att_status = gatt_client_discover_characteristics_for_service(&handle_gatt_client_event, client->con_handle, &service); 718 UNUSED(att_status); 719 break; 720 721 case HIDS_CLIENT_STATE_W2_FIND_REPORT: 722 #ifdef ENABLE_TESTING_SUPPORT 723 printf("\nQuery Report Characteristic Descriptors [%d, %d, 0x%04X]:\n", 724 client->report_index, 725 client->reports[client->report_index].service_index, 726 client->reports[client->report_index].value_handle); 727 #endif 728 client->state = HIDS_CLIENT_STATE_W4_REPORT_FOUND; 729 client->handle = 0; 730 731 characteristic.value_handle = client->reports[client->report_index].value_handle; 732 characteristic.end_handle = client->reports[client->report_index].end_handle; 733 characteristic.properties = client->reports[client->report_index].properties; 734 735 // result in GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT 736 att_status = gatt_client_discover_characteristic_descriptors(&handle_gatt_client_event, client->con_handle, &characteristic); 737 UNUSED(att_status); 738 break; 739 740 case HIDS_CLIENT_STATE_W2_READ_REPORT_ID_AND_TYPE: 741 client->state = HIDS_CLIENT_STATE_W4_REPORT_ID_AND_TYPE; 742 743 // result in GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT 744 att_status = gatt_client_read_characteristic_descriptor_using_descriptor_handle(&handle_gatt_client_event, client->con_handle, client->handle); 745 client->handle = 0; 746 UNUSED(att_status); 747 break; 748 749 case HIDS_CLIENT_STATE_W2_CONFIGURE_NOTIFICATIONS: 750 #ifdef ENABLE_TESTING_SUPPORT 751 if (client->value > 0){ 752 printf(" Notification configuration enable "); 753 } else { 754 printf(" Notification configuration disable "); 755 } 756 printf("[%d, %d, 0x%04X]:\n", 757 client->report_index, 758 client->reports[client->report_index].service_index, client->reports[client->report_index].value_handle); 759 #endif 760 761 client->state = HIDS_CLIENT_STATE_W4_NOTIFICATIONS_CONFIGURED; 762 763 characteristic.value_handle = client->reports[client->report_index].value_handle; 764 characteristic.end_handle = client->reports[client->report_index].end_handle; 765 characteristic.properties = client->reports[client->report_index].properties; 766 767 // end of write marked in GATT_EVENT_QUERY_COMPLETE 768 769 att_status = gatt_client_write_client_characteristic_configuration(&handle_gatt_client_event, client->con_handle, &characteristic, client->value); 770 771 if (att_status == ERROR_CODE_SUCCESS){ 772 switch(client->value){ 773 case GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION: 774 gatt_client_listen_for_characteristic_value_updates( 775 &client->reports[client->report_index].notification_listener, 776 &handle_notification_event, client->con_handle, &characteristic); 777 break; 778 default: 779 gatt_client_stop_listening_for_characteristic_value_updates(&client->reports[client->report_index].notification_listener); 780 break; 781 } 782 } else { 783 if (hids_client_report_next_notifications_configuration_report_index(client)){ 784 hids_run_for_client(client); 785 break; 786 } 787 client->state = HIDS_CLIENT_STATE_CONNECTED; 788 } 789 break; 790 791 case HIDS_CLIENT_STATE_W2_ENABLE_INPUT_REPORTS: 792 #ifdef ENABLE_TESTING_SUPPORT 793 printf(" Notification enable [%d, %d, 0x%04X]:\n", 794 client->report_index, 795 client->reports[client->report_index].service_index, client->reports[client->report_index].value_handle); 796 #endif 797 client->state = HIDS_CLIENT_STATE_W4_INPUT_REPORTS_ENABLED; 798 799 characteristic.value_handle = client->reports[client->report_index].value_handle; 800 characteristic.end_handle = client->reports[client->report_index].end_handle; 801 characteristic.properties = client->reports[client->report_index].properties; 802 803 // end of write marked in GATT_EVENT_QUERY_COMPLETE 804 att_status = gatt_client_write_client_characteristic_configuration(&handle_gatt_client_event, client->con_handle, &characteristic, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION); 805 806 if (att_status == ERROR_CODE_SUCCESS){ 807 gatt_client_listen_for_characteristic_value_updates( 808 &client->reports[client->report_index].notification_listener, 809 &handle_notification_event, client->con_handle, &characteristic); 810 } else { 811 if (hids_client_report_next_notification_report_index(client)){ 812 hids_run_for_client(client); 813 break; 814 } 815 client->state = HIDS_CLIENT_STATE_CONNECTED; 816 hids_emit_connection_established(client, ERROR_CODE_SUCCESS); 817 } 818 break; 819 820 821 case HIDS_CLIENT_W2_SEND_WRITE_REPORT: 822 #ifdef ENABLE_TESTING_SUPPORT 823 printf(" Write report [%d, %d, 0x%04X]:\n", 824 client->report_index, 825 client->reports[client->report_index].service_index, client->reports[client->report_index].value_handle); 826 #endif 827 828 client->state = HIDS_CLIENT_W4_WRITE_REPORT_DONE; 829 830 // see GATT_EVENT_QUERY_COMPLETE for end of write 831 att_status = gatt_client_write_value_of_characteristic( 832 &handle_report_event, client->con_handle, 833 client->reports[client->report_index].value_handle, 834 client->report_len, (uint8_t *)client->report); 835 UNUSED(att_status); 836 break; 837 838 case HIDS_CLIENT_W2_SEND_GET_REPORT: 839 #ifdef ENABLE_TESTING_SUPPORT 840 printf(" Get report [index %d, ID %d, Service %d, handle 0x%04X]:\n", 841 client->report_index, 842 client->reports[client->report_index].report_id, 843 client->reports[client->report_index].service_index, client->reports[client->report_index].value_handle); 844 #endif 845 846 client->state = HIDS_CLIENT_W4_GET_REPORT_RESULT; 847 // result in GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT 848 att_status = gatt_client_read_value_of_characteristic_using_value_handle( 849 &handle_report_event, 850 client->con_handle, 851 client->reports[client->report_index].value_handle); 852 UNUSED(att_status); 853 break; 854 855 #ifdef ENABLE_TESTING_SUPPORT 856 case HIDS_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION: 857 client->state = HIDS_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT; 858 859 // result in GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT 860 att_status = gatt_client_read_value_of_characteristic_using_value_handle( 861 &handle_gatt_client_event, 862 client->con_handle, 863 client->reports[client->report_index].ccc_handle); 864 865 break; 866 #endif 867 case HIDS_CLIENT_W2_READ_VALUE_OF_CHARACTERISTIC: 868 client->state = HIDS_CLIENT_W4_VALUE_OF_CHARACTERISTIC_RESULT; 869 870 // result in GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT 871 att_status = gatt_client_read_value_of_characteristic_using_value_handle( 872 &handle_gatt_client_event, 873 client->con_handle, 874 client->handle); 875 break; 876 877 case HIDS_CLIENT_STATE_W2_SET_PROTOCOL_MODE_WITHOUT_RESPONSE: 878 case HIDS_CLIENT_W2_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE: 879 client->write_without_response_request.callback = &hids_client_handle_can_write_without_reponse; 880 client->write_without_response_request.context = client; 881 (void) gatt_client_request_to_write_without_response(&client->write_without_response_request, client->con_handle); 882 break; 883 884 default: 885 break; 886 } 887 } 888 889 static void hids_client_handle_can_write_without_reponse(void * context) { 890 hids_client_t *client = (hids_client_t *) context; 891 uint8_t att_status; 892 switch (client->state){ 893 case HIDS_CLIENT_STATE_W2_SET_PROTOCOL_MODE_WITHOUT_RESPONSE: 894 att_status = gatt_client_write_value_of_characteristic_without_response( 895 client->con_handle, 896 client->services[client->service_index].protocol_mode_value_handle, 1, (uint8_t *)&client->required_protocol_mode); 897 898 #ifdef ENABLE_TESTING_SUPPORT 899 printf("\n\nSet report mode %d of service %d, status 0x%02x", client->required_protocol_mode, client->service_index, att_status); 900 #endif 901 902 if (att_status == ATT_ERROR_SUCCESS){ 903 client->services[client->service_index].protocol_mode = client->required_protocol_mode; 904 if ((client->service_index + 1) < client->num_instances){ 905 client->service_index++; 906 hids_run_for_client(client); 907 break; 908 } 909 } 910 911 // read UUIDS for external characteristics 912 if (hids_client_report_map_uuid_query_init(client)){ 913 hids_run_for_client(client); 914 break; 915 } 916 917 // discover characteristic descriptor for all Report characteristics, 918 // then read value of characteristic descriptor to get Report ID 919 if (hids_client_report_query_init(client)){ 920 hids_run_for_client(client); 921 break; 922 } 923 924 client->state = HIDS_CLIENT_STATE_CONNECTED; 925 hids_emit_connection_established(client, ERROR_CODE_SUCCESS); 926 break; 927 928 case HIDS_CLIENT_W2_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE: 929 #ifdef ENABLE_TESTING_SUPPORT 930 printf(" Write characteristic [service %d, handle 0x%04X]:\n", client->service_index, client->handle); 931 #endif 932 client->state = HIDS_CLIENT_STATE_CONNECTED; 933 (void) gatt_client_write_value_of_characteristic_without_response(client->con_handle, client->handle, 1, (uint8_t *) &client->value); 934 break; 935 936 default: 937 break; 938 } 939 } 940 941 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 942 UNUSED(packet_type); 943 UNUSED(channel); 944 UNUSED(size); 945 946 hids_client_t * client = NULL; 947 uint8_t att_status; 948 gatt_client_service_t service; 949 gatt_client_characteristic_t characteristic; 950 gatt_client_characteristic_descriptor_t characteristic_descriptor; 951 952 // hids_client_report_t * boot_keyboard_report; 953 // hids_client_report_t * boot_mouse_report; 954 const uint8_t * characteristic_descriptor_value; 955 uint8_t i; 956 uint8_t report_index; 957 958 const uint8_t * value; 959 uint16_t value_len; 960 961 switch(hci_event_packet_get_type(packet)){ 962 case GATT_EVENT_SERVICE_QUERY_RESULT: 963 client = hids_get_client_for_con_handle(gatt_event_service_query_result_get_handle(packet)); 964 if (client == NULL) break; 965 966 if (client->num_instances < MAX_NUM_HID_SERVICES){ 967 uint8_t index = client->num_instances; 968 gatt_event_service_query_result_get_service(packet, &service); 969 client->services[index].start_handle = service.start_group_handle; 970 client->services[index].end_handle = service.end_group_handle; 971 client->num_instances++; 972 973 #ifdef ENABLE_TESTING_SUPPORT 974 printf("HID Service: start handle 0x%04X, end handle 0x%04X\n", client->services[index].start_handle, client->services[index].end_handle); 975 #endif 976 hids_client_descriptor_storage_init(client, index); 977 } else { 978 log_info("%d hid services found, only first %d can be stored, increase MAX_NUM_HID_SERVICES", client->num_instances + 1, MAX_NUM_HID_SERVICES); 979 } 980 break; 981 982 case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT: 983 client = hids_get_client_for_con_handle(gatt_event_characteristic_query_result_get_handle(packet)); 984 if (client == NULL) break; 985 986 gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic); 987 988 report_index = HIDS_CLIENT_INVALID_REPORT_INDEX; 989 if (client->state == HIDS_CLIENT_STATE_W4_EXTERNAL_REPORT_CHARACTERISTIC_RESULT){ 990 if (!external_report_index_for_uuid_exists(client, characteristic.uuid16)){ 991 break; 992 } 993 } 994 995 switch (characteristic.uuid16){ 996 case ORG_BLUETOOTH_CHARACTERISTIC_PROTOCOL_MODE: 997 client->services[client->service_index].protocol_mode_value_handle = characteristic.value_handle; 998 break; 999 1000 case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_INPUT_REPORT: 1001 report_index = hids_client_add_characteristic(client, &characteristic, HID_BOOT_MODE_KEYBOARD_ID, HID_REPORT_TYPE_INPUT, true); 1002 break; 1003 1004 case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_MOUSE_INPUT_REPORT: 1005 report_index = hids_client_add_characteristic(client, &characteristic, HID_BOOT_MODE_MOUSE_ID, HID_REPORT_TYPE_INPUT, true); 1006 break; 1007 1008 case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_OUTPUT_REPORT: 1009 report_index = hids_client_add_characteristic(client, &characteristic, HID_BOOT_MODE_KEYBOARD_ID, HID_REPORT_TYPE_OUTPUT, true); 1010 break; 1011 1012 case ORG_BLUETOOTH_CHARACTERISTIC_REPORT: 1013 report_index = hids_client_add_characteristic(client, &characteristic, HID_REPORT_MODE_REPORT_ID, HID_REPORT_TYPE_RESERVED, false); 1014 break; 1015 1016 case ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP: 1017 client->services[client->service_index].report_map_value_handle = characteristic.value_handle; 1018 client->services[client->service_index].report_map_end_handle = characteristic.end_handle; 1019 break; 1020 1021 case ORG_BLUETOOTH_CHARACTERISTIC_HID_INFORMATION: 1022 client->services[client->service_index].hid_information_value_handle = characteristic.value_handle; 1023 break; 1024 1025 case ORG_BLUETOOTH_CHARACTERISTIC_HID_CONTROL_POINT: 1026 client->services[client->service_index].control_point_value_handle = characteristic.value_handle; 1027 break; 1028 1029 default: 1030 #ifdef ENABLE_TESTING_SUPPORT 1031 printf(" TODO: Found external characteristic 0x%04X\n", characteristic.uuid16); 1032 #endif 1033 return; 1034 } 1035 1036 #ifdef ENABLE_TESTING_SUPPORT 1037 printf("HID Characteristic %s: \n Attribute Handle 0x%04X, Properties 0x%02X, Handle 0x%04X, UUID 0x%04X, service %d", 1038 hid_characteristic_name(characteristic.uuid16), 1039 characteristic.start_handle, 1040 characteristic.properties, 1041 characteristic.value_handle, characteristic.uuid16, 1042 client->service_index); 1043 1044 if (report_index != HIDS_CLIENT_INVALID_REPORT_INDEX){ 1045 printf(", report index 0x%02X", report_index); 1046 } 1047 printf("\n"); 1048 #endif 1049 break; 1050 1051 case GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT: 1052 // Map Report characteristic value == HID Descriptor 1053 client = hids_get_client_for_con_handle(gatt_event_long_characteristic_value_query_result_get_handle(packet)); 1054 if (client == NULL) break; 1055 1056 value = gatt_event_long_characteristic_value_query_result_get_value(packet); 1057 value_len = gatt_event_long_characteristic_value_query_result_get_value_length(packet); 1058 1059 #ifdef ENABLE_TESTING_SUPPORT 1060 // printf("Report Map HID Desc [%d] for service %d\n", descriptor_len, client->service_index); 1061 printf_hexdump(value, value_len); 1062 #endif 1063 for (i = 0; i < value_len; i++){ 1064 bool stored = hids_client_descriptor_storage_store(client, client->service_index, value[i]); 1065 if (!stored){ 1066 client->services[client->service_index].hid_descriptor_status = ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 1067 break; 1068 } 1069 } 1070 break; 1071 1072 case GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT: 1073 client = hids_get_client_for_con_handle(gatt_event_all_characteristic_descriptors_query_result_get_handle(packet)); 1074 if (client == NULL) break; 1075 1076 gatt_event_all_characteristic_descriptors_query_result_get_characteristic_descriptor(packet, &characteristic_descriptor); 1077 1078 switch (client->state) { 1079 case HIDS_CLIENT_STATE_W4_REPORT_MAP_CHARACTERISTIC_DESCRIPTORS_RESULT: 1080 // setup for descriptor value query 1081 if (characteristic_descriptor.uuid16 == ORG_BLUETOOTH_DESCRIPTOR_EXTERNAL_REPORT_REFERENCE){ 1082 report_index = hids_client_add_external_report(client, &characteristic_descriptor); 1083 1084 #ifdef ENABLE_TESTING_SUPPORT 1085 if (report_index != HIDS_CLIENT_INVALID_REPORT_INDEX){ 1086 printf(" External Report Reference Characteristic Descriptor: Handle 0x%04X, UUID 0x%04X, service %d, report index 0x%02X\n", 1087 characteristic_descriptor.handle, 1088 characteristic_descriptor.uuid16, 1089 client->service_index, report_index); 1090 } 1091 #endif 1092 } 1093 break; 1094 case HIDS_CLIENT_STATE_W4_REPORT_FOUND: 1095 // setup for descriptor value query 1096 if (characteristic_descriptor.uuid16 == ORG_BLUETOOTH_DESCRIPTOR_REPORT_REFERENCE){ 1097 client->handle = characteristic_descriptor.handle; 1098 #ifdef ENABLE_TESTING_SUPPORT 1099 printf(" Report Characteristic Report Reference Characteristic Descriptor: Handle 0x%04X, UUID 0x%04X\n", 1100 characteristic_descriptor.handle, 1101 characteristic_descriptor.uuid16); 1102 #endif 1103 } 1104 1105 #ifdef ENABLE_TESTING_SUPPORT 1106 if (characteristic_descriptor.uuid16 == ORG_BLUETOOTH_DESCRIPTOR_GATT_CLIENT_CHARACTERISTIC_CONFIGURATION){ 1107 client->reports[client->report_index].ccc_handle = characteristic_descriptor.handle; 1108 printf(" Report Client Characteristic Configuration Descriptor: Handle 0x%04X, UUID 0x%04X\n", 1109 characteristic_descriptor.handle, 1110 characteristic_descriptor.uuid16); 1111 } 1112 #endif 1113 break; 1114 1115 default: 1116 break; 1117 } 1118 break; 1119 1120 case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT: 1121 client = hids_get_client_for_con_handle(gatt_event_characteristic_value_query_result_get_handle(packet)); 1122 if (client == NULL) break; 1123 1124 value = gatt_event_characteristic_value_query_result_get_value(packet); 1125 value_len = gatt_event_characteristic_value_query_result_get_value_length(packet); 1126 1127 1128 switch (client->state){ 1129 #ifdef ENABLE_TESTING_SUPPORT 1130 case HIDS_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT: 1131 printf(" Received CCC value: "); 1132 printf_hexdump(value, value_len); 1133 break; 1134 #endif 1135 case HIDS_CLIENT_W4_VALUE_OF_CHARACTERISTIC_RESULT:{ 1136 uint16_t value_handle = gatt_event_characteristic_value_query_result_get_value_handle(packet); 1137 if (value_handle == client->services[client->service_index].hid_information_value_handle){ 1138 hids_client_emit_hid_information_event(client, value, value_len); 1139 break; 1140 } 1141 if (value_handle == client->services[client->service_index].protocol_mode_value_handle){ 1142 hids_client_emit_protocol_mode_event(client, value, value_len); 1143 break; 1144 } 1145 break; 1146 } 1147 default: 1148 break; 1149 } 1150 1151 break; 1152 1153 case GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT: 1154 client = hids_get_client_for_con_handle(gatt_event_characteristic_descriptor_query_result_get_handle(packet)); 1155 if (client == NULL) break; 1156 1157 if (gatt_event_characteristic_descriptor_query_result_get_descriptor_length(packet) != 2){ 1158 break; 1159 } 1160 1161 characteristic_descriptor_value = gatt_event_characteristic_descriptor_query_result_get_descriptor(packet); 1162 switch (client->state) { 1163 case HIDS_CLIENT_STATE_W4_REPORT_MAP_EXTERNAL_REPORT_REFERENCE_UUID: 1164 // get external report characteristic uuid 1165 report_index = find_external_report_index_for_value_handle(client, gatt_event_characteristic_descriptor_query_result_get_descriptor_handle(packet)); 1166 if (report_index != HIDS_CLIENT_INVALID_REPORT_INDEX){ 1167 client->external_reports[report_index].external_report_reference_uuid = little_endian_read_16(characteristic_descriptor_value, 0); 1168 #ifdef ENABLE_TESTING_SUPPORT 1169 printf(" Update external_report_reference_uuid of report index 0x%02X, service index 0x%02X, UUID 0x%02X\n", 1170 report_index, client->service_index, client->external_reports[report_index].external_report_reference_uuid); 1171 #endif 1172 } 1173 break; 1174 1175 case HIDS_CLIENT_STATE_W4_REPORT_ID_AND_TYPE: 1176 1177 if (client->report_index != HIDS_CLIENT_INVALID_REPORT_INDEX){ 1178 client->reports[client->report_index].report_id = characteristic_descriptor_value[0]; 1179 client->reports[client->report_index].report_type = (hid_report_type_t)characteristic_descriptor_value[1]; 1180 #ifdef ENABLE_TESTING_SUPPORT 1181 printf(" Update report ID and type [%d, %d] of report index 0x%02X, service index 0x%02X\n", 1182 client->reports[client->report_index].report_id, 1183 client->reports[client->report_index].report_type, 1184 client->report_index, client->service_index); 1185 #endif 1186 } 1187 break; 1188 1189 default: 1190 break; 1191 } 1192 break; 1193 1194 case GATT_EVENT_QUERY_COMPLETE: 1195 client = hids_get_client_for_con_handle(gatt_event_query_complete_get_handle(packet)); 1196 if (client == NULL) break; 1197 1198 att_status = gatt_event_query_complete_get_att_status(packet); 1199 1200 switch (client->state){ 1201 case HIDS_CLIENT_STATE_W4_SERVICE_RESULT: 1202 if (att_status != ATT_ERROR_SUCCESS){ 1203 hids_emit_connection_established(client, att_status); 1204 hids_finalize_client(client); 1205 return; 1206 } 1207 1208 if (client->num_instances == 0){ 1209 hids_emit_connection_established(client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE); 1210 hids_finalize_client(client); 1211 return; 1212 } 1213 1214 client->service_index = 0; 1215 client->state = HIDS_CLIENT_STATE_W2_QUERY_CHARACTERISTIC; 1216 break; 1217 1218 case HIDS_CLIENT_STATE_W4_CHARACTERISTIC_RESULT: 1219 if (att_status != ATT_ERROR_SUCCESS){ 1220 hids_emit_connection_established(client, att_status); 1221 hids_finalize_client(client); 1222 return; 1223 } 1224 1225 if ((client->service_index + 1) < client->num_instances){ 1226 // discover characteristics of next service 1227 client->service_index++; 1228 client->state = HIDS_CLIENT_STATE_W2_QUERY_CHARACTERISTIC; 1229 break; 1230 } 1231 1232 btstack_assert(client->required_protocol_mode != HID_PROTOCOL_MODE_REPORT_WITH_FALLBACK_TO_BOOT); 1233 1234 switch (client->required_protocol_mode){ 1235 case HID_PROTOCOL_MODE_REPORT: 1236 for (i = 0; i < client->num_instances; i++){ 1237 client->services[i].protocol_mode = HID_PROTOCOL_MODE_REPORT; 1238 } 1239 // 1. we need to get HID Descriptor and 1240 // 2. get external Report characteristics if referenced from Report Map 1241 if (hids_client_report_map_query_init(client)){ 1242 break; 1243 } 1244 hids_emit_connection_established(client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE); 1245 hids_finalize_client(client); 1246 return; 1247 1248 default: 1249 // set boot mode 1250 client->service_index = 0; 1251 client->state = HIDS_CLIENT_STATE_W2_SET_PROTOCOL_MODE_WITHOUT_RESPONSE; 1252 break; 1253 } 1254 break; 1255 1256 1257 // HID descriptor found 1258 case HIDS_CLIENT_STATE_W4_REPORT_MAP_HID_DESCRIPTOR: 1259 if (att_status != ATT_ERROR_SUCCESS){ 1260 hids_emit_connection_established(client, att_status); 1261 hids_finalize_client(client); 1262 return; 1263 } 1264 client->state = HIDS_CLIENT_STATE_W2_REPORT_MAP_DISCOVER_CHARACTERISTIC_DESCRIPTORS; 1265 break; 1266 1267 // found all descriptors, check if there is one with EXTERNAL_REPORT_REFERENCE 1268 case HIDS_CLIENT_STATE_W4_REPORT_MAP_CHARACTERISTIC_DESCRIPTORS_RESULT: 1269 // go for next report map 1270 if (hids_client_report_query_next_report_map(client)){ 1271 break; 1272 } 1273 1274 // read UUIDS for external characteristics 1275 if (hids_client_report_map_uuid_query_init(client)){ 1276 break; 1277 } 1278 1279 // discover characteristic descriptor for all Report characteristics, 1280 // then read value of characteristic descriptor to get Report ID 1281 if (hids_client_report_query_init(client)){ 1282 break; 1283 } 1284 1285 hids_emit_connection_established(client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE); 1286 hids_finalize_client(client); 1287 return; 1288 1289 case HIDS_CLIENT_STATE_W4_REPORT_MAP_EXTERNAL_REPORT_REFERENCE_UUID: 1290 // go for next map report 1291 if (hids_client_report_query_next_report_map_uuid(client)){ 1292 break; 1293 } 1294 1295 // update external characteristics with correct value handle and end handle 1296 client->state = HIDS_CLIENT_STATE_W2_DISCOVER_EXTERNAL_REPORT_CHARACTERISTIC; 1297 break; 1298 1299 case HIDS_CLIENT_STATE_W4_EXTERNAL_REPORT_CHARACTERISTIC_RESULT: 1300 // discover characteristic descriptor for all Report characteristics, 1301 // then read value of characteristic descriptor to get Report ID 1302 if (hids_client_report_query_init(client)){ 1303 break; 1304 } 1305 1306 hids_emit_connection_established(client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE); 1307 hids_finalize_client(client); 1308 return; 1309 1310 case HIDS_CLIENT_STATE_W4_REPORT_FOUND: 1311 if (client->handle != 0){ 1312 client->state = HIDS_CLIENT_STATE_W2_READ_REPORT_ID_AND_TYPE; 1313 break; 1314 } 1315 // go for next report 1316 if (hids_client_report_query_next_report(client)){ 1317 break; 1318 } 1319 client->state = HIDS_CLIENT_STATE_CONNECTED; 1320 hids_emit_connection_established(client, ERROR_CODE_SUCCESS); 1321 break; 1322 1323 case HIDS_CLIENT_STATE_W4_REPORT_ID_AND_TYPE: 1324 // go for next report 1325 if (hids_client_report_query_next_report(client)){ 1326 break; 1327 } 1328 if (hids_client_report_notifications_init(client)){ 1329 break; 1330 } 1331 client->state = HIDS_CLIENT_STATE_CONNECTED; 1332 hids_emit_connection_established(client, ERROR_CODE_SUCCESS); 1333 break; 1334 1335 case HIDS_CLIENT_STATE_W4_INPUT_REPORTS_ENABLED: 1336 if (hids_client_report_next_notification_report_index(client)){ 1337 break; 1338 } 1339 client->state = HIDS_CLIENT_STATE_CONNECTED; 1340 hids_emit_connection_established(client, ERROR_CODE_SUCCESS); 1341 break; 1342 1343 case HIDS_CLIENT_STATE_W4_NOTIFICATIONS_CONFIGURED: 1344 if (hids_client_report_next_notifications_configuration_report_index(client)){ 1345 break; 1346 } 1347 hids_emit_notifications_configuration(client); 1348 client->state = HIDS_CLIENT_STATE_CONNECTED; 1349 break; 1350 1351 #ifdef ENABLE_TESTING_SUPPORT 1352 case HIDS_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT: 1353 client->state = HIDS_CLIENT_W2_SEND_GET_REPORT; 1354 break; 1355 #endif 1356 1357 case HIDS_CLIENT_W4_VALUE_OF_CHARACTERISTIC_RESULT: 1358 case HIDS_CLIENT_W4_WRITE_REPORT_DONE: 1359 client->state = HIDS_CLIENT_STATE_CONNECTED; 1360 break; 1361 1362 1363 default: 1364 break; 1365 } 1366 break; 1367 1368 default: 1369 break; 1370 } 1371 1372 if (client != NULL){ 1373 hids_run_for_client(client); 1374 } 1375 } 1376 1377 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){ 1378 btstack_assert(packet_handler != NULL); 1379 1380 hids_client_t * client = hids_get_client_for_con_handle(con_handle); 1381 if (client != NULL){ 1382 return ERROR_CODE_COMMAND_DISALLOWED; 1383 } 1384 1385 uint16_t cid = hids_get_next_cid(); 1386 if (hids_cid != NULL) { 1387 *hids_cid = cid; 1388 } 1389 1390 client = hids_create_client(con_handle, cid); 1391 if (client == NULL) { 1392 return BTSTACK_MEMORY_ALLOC_FAILED; 1393 } 1394 1395 client->required_protocol_mode = protocol_mode; 1396 client->client_handler = packet_handler; 1397 client->state = HIDS_CLIENT_STATE_W2_QUERY_SERVICE; 1398 1399 hids_run_for_client(client); 1400 return ERROR_CODE_SUCCESS; 1401 } 1402 1403 uint8_t hids_client_disconnect(uint16_t hids_cid){ 1404 hids_client_t * client = hids_get_client_for_cid(hids_cid); 1405 if (client == NULL){ 1406 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1407 } 1408 // finalize connection 1409 hids_finalize_client(client); 1410 return ERROR_CODE_SUCCESS; 1411 } 1412 1413 uint8_t hids_client_send_write_report(uint16_t hids_cid, uint8_t report_id, hid_report_type_t report_type, const uint8_t * report, uint8_t report_len){ 1414 hids_client_t * client = hids_get_client_for_cid(hids_cid); 1415 if (client == NULL){ 1416 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1417 } 1418 1419 if (client->state != HIDS_CLIENT_STATE_CONNECTED) { 1420 return ERROR_CODE_COMMAND_DISALLOWED; 1421 } 1422 1423 uint8_t report_index = find_report_index_for_report_id_and_report_type(client, report_id, report_type); 1424 1425 if (report_index == HIDS_CLIENT_INVALID_REPORT_INDEX){ 1426 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1427 } 1428 1429 uint16_t mtu; 1430 uint8_t status = gatt_client_get_mtu(client->con_handle, &mtu); 1431 1432 if (status != ERROR_CODE_SUCCESS){ 1433 return status; 1434 } 1435 1436 if (mtu - 2 < report_len){ 1437 return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE; 1438 } 1439 1440 client->state = HIDS_CLIENT_W2_SEND_WRITE_REPORT; 1441 client->report_index = report_index; 1442 client->report = report; 1443 client->report_len = report_len; 1444 1445 hids_run_for_client(client); 1446 return ERROR_CODE_SUCCESS; 1447 } 1448 1449 uint8_t hids_client_send_get_report(uint16_t hids_cid, uint8_t report_id, hid_report_type_t report_type){ 1450 hids_client_t * client = hids_get_client_for_cid(hids_cid); 1451 if (client == NULL){ 1452 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1453 } 1454 1455 if (client->state != HIDS_CLIENT_STATE_CONNECTED) { 1456 return ERROR_CODE_COMMAND_DISALLOWED; 1457 } 1458 1459 uint8_t report_index = find_report_index_for_report_id_and_report_type(client, report_id, report_type); 1460 if (report_index == HIDS_CLIENT_INVALID_REPORT_INDEX){ 1461 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1462 } 1463 1464 client->report_index = report_index; 1465 1466 #ifdef ENABLE_TESTING_SUPPORT 1467 client->state = HIDS_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION; 1468 #else 1469 client->state = HIDS_CLIENT_W2_SEND_GET_REPORT; 1470 #endif 1471 hids_run_for_client(client); 1472 return ERROR_CODE_SUCCESS; 1473 } 1474 1475 1476 uint8_t hids_client_get_hid_information(uint16_t hids_cid, uint8_t service_index){ 1477 hids_client_t * client = hids_get_client_for_cid(hids_cid); 1478 if (client == NULL){ 1479 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1480 } 1481 1482 if (client->state != HIDS_CLIENT_STATE_CONNECTED) { 1483 return ERROR_CODE_COMMAND_DISALLOWED; 1484 } 1485 1486 if (service_index >= client->num_instances){ 1487 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1488 } 1489 1490 client->service_index = service_index; 1491 client->handle = client->services[client->service_index].hid_information_value_handle; 1492 1493 client->state = HIDS_CLIENT_W2_READ_VALUE_OF_CHARACTERISTIC; 1494 hids_run_for_client(client); 1495 return ERROR_CODE_SUCCESS; 1496 } 1497 1498 uint8_t hids_client_get_protocol_mode(uint16_t hids_cid, uint8_t service_index){ 1499 hids_client_t * client = hids_get_client_for_cid(hids_cid); 1500 if (client == NULL){ 1501 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1502 } 1503 1504 if (client->state != HIDS_CLIENT_STATE_CONNECTED) { 1505 return ERROR_CODE_COMMAND_DISALLOWED; 1506 } 1507 1508 if (service_index >= client->num_instances){ 1509 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1510 } 1511 1512 client->service_index = service_index; 1513 client->handle = client->services[client->service_index].protocol_mode_value_handle; 1514 1515 client->state = HIDS_CLIENT_W2_READ_VALUE_OF_CHARACTERISTIC; 1516 hids_run_for_client(client); 1517 return ERROR_CODE_SUCCESS; 1518 } 1519 1520 uint8_t hids_client_send_set_protocol_mode(uint16_t hids_cid, uint8_t service_index, hid_protocol_mode_t protocol_mode){ 1521 hids_client_t * client = hids_get_client_for_cid(hids_cid); 1522 if (client == NULL){ 1523 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1524 } 1525 1526 if (client->state != HIDS_CLIENT_STATE_CONNECTED) { 1527 return ERROR_CODE_COMMAND_DISALLOWED; 1528 } 1529 1530 if (service_index >= client->num_instances){ 1531 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1532 } 1533 1534 client->service_index = service_index; 1535 client->handle = client->services[client->service_index].protocol_mode_value_handle; 1536 client->value = (uint8_t)protocol_mode; 1537 1538 client->state = HIDS_CLIENT_W2_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE; 1539 hids_run_for_client(client); 1540 return ERROR_CODE_SUCCESS; 1541 } 1542 1543 1544 static uint8_t hids_client_send_control_point_cmd(uint16_t hids_cid, uint8_t service_index, uint8_t value){ 1545 hids_client_t * client = hids_get_client_for_cid(hids_cid); 1546 if (client == NULL){ 1547 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1548 } 1549 1550 if (client->state != HIDS_CLIENT_STATE_CONNECTED) { 1551 return ERROR_CODE_COMMAND_DISALLOWED; 1552 } 1553 1554 if (service_index >= client->num_instances){ 1555 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1556 } 1557 1558 client->service_index = service_index; 1559 client->handle = client->services[client->service_index].control_point_value_handle; 1560 client->value = value; 1561 1562 client->state = HIDS_CLIENT_W2_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE; 1563 hids_run_for_client(client); 1564 return ERROR_CODE_SUCCESS; 1565 } 1566 1567 uint8_t hids_client_send_suspend(uint16_t hids_cid, uint8_t service_index){ 1568 return hids_client_send_control_point_cmd(hids_cid, service_index, 0); 1569 } 1570 1571 uint8_t hids_client_send_exit_suspend(uint16_t hids_cid, uint8_t service_index){ 1572 return hids_client_send_control_point_cmd(hids_cid, service_index, 1); 1573 } 1574 1575 uint8_t hids_client_enable_notifications(uint16_t hids_cid){ 1576 hids_client_t * client = hids_get_client_for_cid(hids_cid); 1577 if (client == NULL){ 1578 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1579 } 1580 1581 if (client->state != HIDS_CLIENT_STATE_CONNECTED) { 1582 return ERROR_CODE_COMMAND_DISALLOWED; 1583 } 1584 client->value = GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION; 1585 if (hids_client_notifications_configuration_init(client)){ 1586 hids_run_for_client(client); 1587 return ERROR_CODE_SUCCESS; 1588 } 1589 hids_emit_notifications_configuration(client); 1590 return ERROR_CODE_SUCCESS; 1591 } 1592 1593 uint8_t hids_client_disable_notifications(uint16_t hids_cid){ 1594 hids_client_t * client = hids_get_client_for_cid(hids_cid); 1595 if (client == NULL){ 1596 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1597 } 1598 1599 if (client->state != HIDS_CLIENT_STATE_CONNECTED) { 1600 return ERROR_CODE_COMMAND_DISALLOWED; 1601 } 1602 1603 client->value = GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NONE; 1604 if (hids_client_notifications_configuration_init(client)){ 1605 hids_run_for_client(client); 1606 return ERROR_CODE_SUCCESS; 1607 } 1608 hids_emit_notifications_configuration(client); 1609 return ERROR_CODE_SUCCESS; 1610 } 1611 1612 void hids_client_init(uint8_t * hid_descriptor_storage, uint16_t hid_descriptor_storage_len){ 1613 hids_client_descriptor_storage = hid_descriptor_storage; 1614 hids_client_descriptor_storage_len = hid_descriptor_storage_len; 1615 } 1616 1617 void hids_client_deinit(void){} 1618