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__ "battery_service_client.c" 39 40 #include "btstack_config.h" 41 42 #ifdef ENABLE_TESTING_SUPPORT 43 #include <stdio.h> 44 #include <unistd.h> 45 #endif 46 47 #include <stdint.h> 48 #include <string.h> 49 50 51 #include "ble/gatt-service/battery_service_client.h" 52 53 #include "btstack_memory.h" 54 #include "ble/core.h" 55 #include "ble/gatt_client.h" 56 #include "bluetooth_gatt.h" 57 #include "btstack_debug.h" 58 #include "btstack_event.h" 59 #include "btstack_run_loop.h" 60 #include "gap.h" 61 62 #define BATTERY_SERVICE_INVALID_INDEX 0xFF 63 64 static btstack_context_callback_registration_t battery_service_handle_can_send_now; 65 66 static btstack_linked_list_t clients; 67 static uint16_t battery_service_cid_counter = 0; 68 static btstack_packet_callback_registration_t hci_event_callback_registration; 69 70 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 71 static void battery_service_poll_timer_start(battery_service_client_t * client); 72 73 static uint16_t battery_service_get_next_cid(void){ 74 battery_service_cid_counter = btstack_next_cid_ignoring_zero(battery_service_cid_counter); 75 return battery_service_cid_counter; 76 } 77 78 79 static uint8_t battery_service_client_request_send_gatt_query(battery_service_client_t * client){ 80 battery_service_handle_can_send_now.context = (void *)(uintptr_t)client->cid; 81 return gatt_client_request_to_send_gatt_query(&battery_service_handle_can_send_now, client->con_handle); 82 } 83 84 static battery_service_client_t * battery_service_create_client(hci_con_handle_t con_handle, uint16_t cid){ 85 battery_service_client_t * client = btstack_memory_battery_service_client_get(); 86 if (!client){ 87 log_error("Not enough memory to create client"); 88 return NULL; 89 } 90 client->cid = cid; 91 client->con_handle = con_handle; 92 client->poll_interval_ms = 0; 93 client->num_instances = 0; 94 client->service_index = 0; 95 client->poll_bitmap = 0; 96 client->need_poll_bitmap = 0; 97 client->polled_service_index = BATTERY_SERVICE_INVALID_INDEX; 98 client->state = BATTERY_SERVICE_CLIENT_STATE_IDLE; 99 100 btstack_linked_list_add(&clients, (btstack_linked_item_t *) client); 101 return client; 102 } 103 104 static void battery_service_finalize_client(battery_service_client_t * client){ 105 // stop listening 106 uint8_t i; 107 for (i = 0; i < client->num_instances; i++){ 108 gatt_client_stop_listening_for_characteristic_value_updates(&client->services[i].notification_listener); 109 } 110 111 // remove timer 112 btstack_run_loop_remove_timer(&client->poll_timer); 113 114 btstack_linked_list_remove(&clients, (btstack_linked_item_t *) client); 115 btstack_memory_battery_service_client_free(client); 116 } 117 118 static battery_service_client_t * battery_service_get_client_for_con_handle(hci_con_handle_t con_handle){ 119 btstack_linked_list_iterator_t it; 120 btstack_linked_list_iterator_init(&it, &clients); 121 while (btstack_linked_list_iterator_has_next(&it)){ 122 battery_service_client_t * client = (battery_service_client_t *)btstack_linked_list_iterator_next(&it); 123 if (client->con_handle != con_handle) continue; 124 return client; 125 } 126 return NULL; 127 } 128 129 static battery_service_client_t * battery_service_get_client_for_cid(uint16_t battery_service_cid){ 130 btstack_linked_list_iterator_t it; 131 btstack_linked_list_iterator_init(&it, &clients); 132 while (btstack_linked_list_iterator_has_next(&it)){ 133 battery_service_client_t * client = (battery_service_client_t *)btstack_linked_list_iterator_next(&it); 134 if (client->cid != battery_service_cid) continue; 135 return client; 136 } 137 return NULL; 138 } 139 140 static void battery_service_emit_connection_established(battery_service_client_t * client, uint8_t status){ 141 uint8_t event[8]; 142 int pos = 0; 143 event[pos++] = HCI_EVENT_GATTSERVICE_META; 144 event[pos++] = sizeof(event) - 2; 145 event[pos++] = GATTSERVICE_SUBEVENT_BATTERY_SERVICE_CONNECTED; 146 little_endian_store_16(event, pos, client->cid); 147 pos += 2; 148 event[pos++] = status; 149 event[pos++] = client->num_instances; 150 event[pos++] = client->poll_bitmap; 151 152 (*client->client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 153 } 154 155 static void battery_service_emit_battery_level(battery_service_client_t * client, uint16_t value_handle, uint8_t att_status, uint8_t battery_level){ 156 uint8_t event[8]; 157 int pos = 0; 158 event[pos++] = HCI_EVENT_GATTSERVICE_META; 159 event[pos++] = sizeof(event) - 2; 160 event[pos++] = GATTSERVICE_SUBEVENT_BATTERY_SERVICE_LEVEL; 161 little_endian_store_16(event, pos, client->cid); 162 pos += 2; 163 164 uint8_t i; 165 for (i = 0; i < client->num_instances; i++){ 166 if (value_handle == client->services[i].value_handle){ 167 event[pos++] = i; 168 event[pos++] = att_status; 169 event[pos++] = battery_level; 170 (*client->client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 171 break; 172 } 173 } 174 } 175 176 static bool battery_service_registered_notification(battery_service_client_t * client, uint16_t service_index){ 177 gatt_client_characteristic_t characteristic; 178 // if there are services without notification, register pool timer, 179 // otherwise register for notifications 180 characteristic.value_handle = client->services[service_index].value_handle; 181 characteristic.properties = client->services[service_index].properties; 182 characteristic.end_handle = client->services[service_index].end_handle; 183 184 uint8_t status = gatt_client_write_client_characteristic_configuration( 185 &handle_gatt_client_event, 186 client->con_handle, 187 &characteristic, 188 GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION); 189 190 // notification supported, register for value updates 191 if (status == ERROR_CODE_SUCCESS){ 192 gatt_client_listen_for_characteristic_value_updates( 193 &client->services[service_index].notification_listener, 194 &handle_gatt_client_event, 195 client->con_handle, &characteristic); 196 } else { 197 client->poll_bitmap |= 1u << client->service_index; 198 } 199 return status; 200 } 201 202 static bool battery_service_is_polling_needed(battery_service_client_t * client){ 203 return (client->poll_bitmap > 0u) && (client->poll_interval_ms > 0u); 204 } 205 206 static void battery_service_start_polling(battery_service_client_t * client){ 207 client->need_poll_bitmap = client->poll_bitmap; 208 battery_service_poll_timer_start(client); 209 } 210 211 static void battery_service_send_next_query(void * context){ 212 uint16_t cid = (uint16_t)(uintptr_t)context; 213 battery_service_client_t * client = battery_service_get_client_for_cid(cid); 214 215 if (client == NULL){ 216 return; 217 } 218 219 uint8_t status; 220 uint8_t i; 221 gatt_client_characteristic_t characteristic; 222 223 switch (client->state){ 224 case BATTERY_SERVICE_CLIENT_STATE_CONNECTED: 225 for (i = 0; i < client->num_instances; i++){ 226 if ( ((client->need_poll_bitmap >> i) & 0x01) == 0x01 ){ 227 client->state = BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_VALUE_READ; 228 // clear bit of polled service 229 client->need_poll_bitmap &= ~(1u << i); 230 client->polled_service_index = i; 231 232 #ifdef ENABLE_TESTING_SUPPORT 233 printf("Poll value [%d]\n", i); 234 #endif 235 // poll value of characteristic 236 characteristic.value_handle = client->services[i].value_handle; 237 characteristic.properties = client->services[i].properties; 238 characteristic.end_handle = client->services[i].end_handle; 239 gatt_client_read_value_of_characteristic(&handle_gatt_client_event, client->con_handle, &characteristic); 240 break; 241 } 242 } 243 break; 244 245 case BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_SERVICE: 246 client->state = BATTERY_SERVICE_CLIENT_STATE_W4_SERVICE_RESULT; 247 status = gatt_client_discover_primary_services_by_uuid16(&handle_gatt_client_event, client->con_handle, ORG_BLUETOOTH_SERVICE_BATTERY_SERVICE); 248 // TODO handle status 249 break; 250 251 case BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTICS: 252 client->state = BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_RESULT; 253 254 gatt_client_discover_characteristics_for_handle_range_by_uuid16( 255 &handle_gatt_client_event, 256 client->con_handle, 257 client->services[client->service_index].start_handle, 258 client->services[client->service_index].end_handle, 259 ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 260 261 break; 262 263 #ifdef ENABLE_TESTING_SUPPORT 264 case BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTIC_DESCRIPTORS: 265 client->state = BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_DESCRIPTORS_RESULT; 266 // if there are services without notification, register pool timer, 267 // othervise register for notifications 268 characteristic.value_handle = client->services[client->service_index].value_handle; 269 characteristic.properties = client->services[client->service_index].properties; 270 characteristic.end_handle = client->services[client->service_index].end_handle; 271 272 (void) gatt_client_discover_characteristic_descriptors(&handle_gatt_client_event, client->con_handle, &characteristic); 273 break; 274 275 case BATTERY_SERVICE_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION: 276 printf("Read client characteristic value [Service %d, handle 0x%04X]:\n", 277 client->service_index, 278 client->services[client->service_index].ccc_handle); 279 280 client->state = BATTERY_SERVICE_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT; 281 282 // result in GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT 283 (void) gatt_client_read_characteristic_descriptor_using_descriptor_handle( 284 &handle_gatt_client_event, 285 client->con_handle, 286 client->services[client->service_index].ccc_handle); 287 break; 288 #endif 289 290 case BATTERY_SERVICE_CLIENT_STATE_W2_REGISTER_NOTIFICATION: 291 client->state = BATTERY_SERVICE_CLIENT_STATE_W4_NOTIFICATION_REGISTERED; 292 293 for (; client->service_index < client->num_instances; client->service_index++){ 294 status = battery_service_registered_notification(client, client->service_index); 295 if (status == ERROR_CODE_SUCCESS) return; 296 } 297 298 299 #ifdef ENABLE_TESTING_SUPPORT 300 for (client->service_index = 0; client->service_index < client->num_instances; client->service_index++){ 301 bool need_polling = (client->poll_bitmap & (1 << client->service_index)) != 0; 302 if ( (client->services[client->service_index].ccc_handle != 0) && !need_polling ){ 303 client->state = BATTERY_SERVICE_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION; 304 break; 305 } 306 } 307 #endif 308 client->state = BATTERY_SERVICE_CLIENT_STATE_CONNECTED; 309 battery_service_emit_connection_established(client, ERROR_CODE_SUCCESS); 310 if (battery_service_is_polling_needed(client)){ 311 battery_service_start_polling(client); 312 } 313 break; 314 default: 315 break; 316 } 317 } 318 319 320 static void battery_service_poll_timer_timeout_handler(btstack_timer_source_t * timer){ 321 uint16_t battery_service_cid = (uint16_t)(uintptr_t) btstack_run_loop_get_timer_context(timer); 322 323 battery_service_client_t * client = battery_service_get_client_for_cid(battery_service_cid); 324 btstack_assert(client != NULL); 325 326 client->need_poll_bitmap = client->poll_bitmap; 327 battery_service_client_request_send_gatt_query(client); 328 } 329 330 static void battery_service_poll_timer_start(battery_service_client_t * client){ 331 btstack_run_loop_set_timer_handler(&client->poll_timer, battery_service_poll_timer_timeout_handler); 332 btstack_run_loop_set_timer_context(&client->poll_timer, (void *)(uintptr_t)client->cid); 333 334 btstack_run_loop_set_timer(&client->poll_timer, client->poll_interval_ms); 335 btstack_run_loop_remove_timer(&client->poll_timer); 336 btstack_run_loop_add_timer(&client->poll_timer); 337 } 338 339 static void battery_service_client_validate_service(battery_service_client_t * client){ 340 // remove all services without characteristic (array in-place) 341 uint8_t src_index = 0; // next entry to check 342 uint8_t dest_index = 0; // to store entry 343 for (src_index = 0; src_index < client->num_instances; src_index++){ 344 if (client->services[src_index].value_handle != 0){ 345 if (src_index != dest_index) { 346 client->services[dest_index] = client->services[src_index]; 347 } 348 dest_index++; 349 } 350 } 351 client->num_instances = dest_index; 352 } 353 354 // @return true if client valid / run function should be called 355 static bool battery_service_client_handle_query_complete_for_connection_setup(battery_service_client_t * client, uint8_t status){ 356 switch (client->state){ 357 case BATTERY_SERVICE_CLIENT_STATE_W4_SERVICE_RESULT: 358 if (status != ATT_ERROR_SUCCESS){ 359 battery_service_emit_connection_established(client, status); 360 battery_service_finalize_client(client); 361 return false; 362 } 363 364 if (client->num_instances == 0){ 365 battery_service_emit_connection_established(client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE); 366 battery_service_finalize_client(client); 367 return false; 368 } 369 370 client->service_index = 0; 371 client->state = BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTICS; 372 break; 373 374 case BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_RESULT: 375 if (status != ATT_ERROR_SUCCESS){ 376 battery_service_emit_connection_established(client, status); 377 battery_service_finalize_client(client); 378 return false; 379 } 380 381 // check if there is another service to query 382 if ((client->service_index + 1) < client->num_instances){ 383 client->service_index++; 384 client->state = BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTICS; 385 break; 386 } 387 388 battery_service_client_validate_service(client); 389 390 if (client->num_instances == 0){ 391 battery_service_emit_connection_established(client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE); 392 battery_service_finalize_client(client); 393 return false; 394 } 395 396 // we are done with querying all services 397 client->service_index = 0; 398 399 #ifdef ENABLE_TESTING_SUPPORT 400 client->state = BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTIC_DESCRIPTORS; 401 #else 402 // wait for notification registration 403 // to send connection established event 404 client->state = BATTERY_SERVICE_CLIENT_STATE_W2_REGISTER_NOTIFICATION; 405 #endif 406 break; 407 408 #ifdef ENABLE_TESTING_SUPPORT 409 case BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_DESCRIPTORS_RESULT: 410 if ((client->service_index + 1) < client->num_instances){ 411 client->service_index++; 412 client->state = BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTIC_DESCRIPTORS; 413 break; 414 } 415 client->service_index = 0; 416 client->state = BATTERY_SERVICE_CLIENT_STATE_W2_REGISTER_NOTIFICATION; 417 break; 418 419 case BATTERY_SERVICE_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT: 420 client->state = BATTERY_SERVICE_CLIENT_STATE_CONNECTED; 421 battery_service_emit_connection_established(client, ERROR_CODE_SUCCESS); 422 if (battery_service_is_polling_needed(client)){ 423 battery_service_start_polling(client); 424 return false; 425 } 426 break; 427 #endif 428 case BATTERY_SERVICE_CLIENT_STATE_W4_NOTIFICATION_REGISTERED: 429 if ((client->service_index + 1) < client->num_instances){ 430 client->service_index++; 431 client->state = BATTERY_SERVICE_CLIENT_STATE_W2_REGISTER_NOTIFICATION; 432 break; 433 } 434 #ifdef ENABLE_TESTING_SUPPORT 435 for (client->service_index = 0; client->service_index < client->num_instances; client->service_index++){ 436 bool need_polling = (client->poll_bitmap & (1 << client->service_index)) != 0; 437 printf("read CCC 1 0x%02x, polling %d \n", client->services[client->service_index].ccc_handle, (int) need_polling); 438 if ( (client->services[client->service_index].ccc_handle != 0) && !need_polling ) { 439 client->state = BATTERY_SERVICE_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION; 440 break; 441 } 442 } 443 #endif 444 client->state = BATTERY_SERVICE_CLIENT_STATE_CONNECTED; 445 battery_service_emit_connection_established(client, ERROR_CODE_SUCCESS); 446 if (battery_service_is_polling_needed(client)){ 447 battery_service_start_polling(client); 448 return false; 449 } 450 break; 451 452 default: 453 return false; 454 455 } 456 return true; 457 } 458 459 static bool battery_service_client_handle_query_complete(battery_service_client_t * client, uint8_t status){ 460 switch (client->state){ 461 462 case BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_VALUE_READ: 463 if (client->polled_service_index != BATTERY_SERVICE_INVALID_INDEX){ 464 if (status != ATT_ERROR_SUCCESS){ 465 battery_service_emit_battery_level(client, client->services[client->polled_service_index].value_handle, status, 0); 466 } 467 client->polled_service_index = BATTERY_SERVICE_INVALID_INDEX; 468 } 469 client->state = BATTERY_SERVICE_CLIENT_STATE_CONNECTED; 470 return (client->need_poll_bitmap != 0u); 471 472 default: 473 break; 474 475 } 476 return false; 477 } 478 479 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 480 UNUSED(packet_type); 481 UNUSED(channel); 482 UNUSED(size); 483 484 battery_service_client_t * client = NULL; 485 gatt_client_service_t service; 486 gatt_client_characteristic_t characteristic; 487 bool send_next_gatt_query = false; 488 489 switch(hci_event_packet_get_type(packet)){ 490 case GATT_EVENT_SERVICE_QUERY_RESULT: 491 client = battery_service_get_client_for_con_handle(gatt_event_service_query_result_get_handle(packet)); 492 btstack_assert(client != NULL); 493 494 if (client->num_instances < MAX_NUM_BATTERY_SERVICES){ 495 gatt_event_service_query_result_get_service(packet, &service); 496 client->services[client->num_instances].start_handle = service.start_group_handle; 497 client->services[client->num_instances].end_handle = service.end_group_handle; 498 499 #ifdef ENABLE_TESTING_SUPPORT 500 printf("Battery Service: start handle 0x%04X, end handle 0x%04X\n", client->services[client->num_instances].start_handle, client->services[client->num_instances].end_handle); 501 #endif 502 client->num_instances++; 503 } else { 504 log_info("Found more then %d, Battery Service instances. Increase MAX_NUM_BATTERY_SERVICES to store all.", MAX_NUM_BATTERY_SERVICES); 505 } 506 // for sending next query w4 GATT_EVENT_QUERY_COMPLETE 507 break; 508 509 case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT: 510 client = battery_service_get_client_for_con_handle(gatt_event_characteristic_query_result_get_handle(packet)); 511 btstack_assert(client != NULL); 512 513 gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic); 514 btstack_assert(client->service_index < client->num_instances); 515 btstack_assert(characteristic.uuid16 == ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 516 517 client->services[client->service_index].value_handle = characteristic.value_handle; 518 client->services[client->service_index].properties = characteristic.properties; 519 520 #ifdef ENABLE_TESTING_SUPPORT 521 printf("Battery Level Characteristic:\n Attribute Handle 0x%04X, Properties 0x%02X, Handle 0x%04X, UUID 0x%04X, service %d\n", 522 // hid_characteristic_name(characteristic.uuid16), 523 characteristic.start_handle, 524 characteristic.properties, 525 characteristic.value_handle, characteristic.uuid16, 526 client->service_index); 527 #endif 528 // for sending next query w4 GATT_EVENT_QUERY_COMPLETE 529 break; 530 531 case GATT_EVENT_NOTIFICATION: 532 if (gatt_event_notification_get_value_length(packet) != 1) break; 533 534 client = battery_service_get_client_for_con_handle(gatt_event_notification_get_handle(packet)); 535 btstack_assert(client != NULL); 536 537 battery_service_emit_battery_level(client, 538 gatt_event_notification_get_value_handle(packet), 539 ATT_ERROR_SUCCESS, 540 gatt_event_notification_get_value(packet)[0]); 541 542 break; 543 544 case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT: 545 client = battery_service_get_client_for_con_handle(gatt_event_characteristic_value_query_result_get_handle(packet)); 546 btstack_assert(client != NULL); 547 548 #ifdef ENABLE_TESTING_SUPPORT 549 if (client->state == BATTERY_SERVICE_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT){ 550 printf(" Received CCC value: "); 551 printf_hexdump(gatt_event_characteristic_value_query_result_get_value(packet), gatt_event_characteristic_value_query_result_get_value_length(packet)); 552 break; 553 } 554 #endif 555 if (gatt_event_characteristic_value_query_result_get_value_length(packet) != 1) break; 556 557 battery_service_emit_battery_level(client, 558 gatt_event_characteristic_value_query_result_get_value_handle(packet), 559 ATT_ERROR_SUCCESS, 560 gatt_event_characteristic_value_query_result_get_value(packet)[0]); 561 // reset need_poll_bitmap in GATT_EVENT_QUERY_COMPLETE 562 break; 563 564 #ifdef ENABLE_TESTING_SUPPORT 565 case GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:{ 566 gatt_client_characteristic_descriptor_t characteristic_descriptor; 567 568 client = battery_service_get_client_for_con_handle(gatt_event_all_characteristic_descriptors_query_result_get_handle(packet)); 569 btstack_assert(client != NULL); 570 gatt_event_all_characteristic_descriptors_query_result_get_characteristic_descriptor(packet, &characteristic_descriptor); 571 572 if (characteristic_descriptor.uuid16 == ORG_BLUETOOTH_DESCRIPTOR_GATT_CLIENT_CHARACTERISTIC_CONFIGURATION){ 573 client->services[client->service_index].ccc_handle = characteristic_descriptor.handle; 574 575 printf(" Battery Level Client Characteristic Configuration Descriptor[%d]: Handle 0x%04X, UUID 0x%04X\n", 576 client->service_index, 577 characteristic_descriptor.handle, 578 characteristic_descriptor.uuid16); 579 } 580 break; 581 // for sending next query w4 GATT_EVENT_QUERY_COMPLETE 582 } 583 #endif 584 585 case GATT_EVENT_QUERY_COMPLETE: 586 client = battery_service_get_client_for_con_handle(gatt_event_query_complete_get_handle(packet)); 587 btstack_assert(client != NULL); 588 589 // 1. handle service establishment/notification subscription query results (client->state < BATTERY_SERVICE_CLIENT_STATE_CONNECTED) 590 if (client->state < BATTERY_SERVICE_CLIENT_STATE_CONNECTED){ 591 send_next_gatt_query = battery_service_client_handle_query_complete_for_connection_setup(client, gatt_event_query_complete_get_att_status(packet)); 592 break; 593 } 594 595 // 2. handle battery value query result when devices is connected 596 send_next_gatt_query = battery_service_client_handle_query_complete(client, gatt_event_query_complete_get_att_status(packet)); 597 if (!send_next_gatt_query){ 598 // if there are no further queries, and we're connected, trigger next polling read 599 if (battery_service_is_polling_needed(client)){ 600 battery_service_poll_timer_start(client); 601 } 602 } 603 break; 604 605 default: 606 break; 607 } 608 609 if (send_next_gatt_query){ 610 battery_service_client_request_send_gatt_query(client); 611 return; 612 } 613 } 614 615 616 static void handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 617 UNUSED(packet_type); // ok: only hci events 618 UNUSED(channel); // ok: there is no channel 619 UNUSED(size); // ok: fixed format events read from HCI buffer 620 621 hci_con_handle_t con_handle; 622 battery_service_client_t * client; 623 624 switch (hci_event_packet_get_type(packet)) { 625 case HCI_EVENT_DISCONNECTION_COMPLETE: 626 con_handle = hci_event_disconnection_complete_get_connection_handle(packet); 627 client = battery_service_get_client_for_con_handle(con_handle); 628 if (client != NULL){ 629 // finalize 630 uint16_t cid = client->cid; 631 battery_service_finalize_client(client); 632 // TODO: emit disconnected event 633 UNUSED(cid); 634 } 635 break; 636 default: 637 break; 638 } 639 } 640 641 uint8_t battery_service_client_connect(hci_con_handle_t con_handle, btstack_packet_handler_t packet_handler, uint32_t poll_interval_ms, uint16_t * battery_service_cid){ 642 btstack_assert(packet_handler != NULL); 643 644 battery_service_client_t * client = battery_service_get_client_for_con_handle(con_handle); 645 if (client != NULL){ 646 return ERROR_CODE_COMMAND_DISALLOWED; 647 } 648 649 uint16_t cid = battery_service_get_next_cid(); 650 if (battery_service_cid != NULL) { 651 *battery_service_cid = cid; 652 } 653 654 client = battery_service_create_client(con_handle, cid); 655 if (client == NULL) { 656 return BTSTACK_MEMORY_ALLOC_FAILED; 657 } 658 659 client->client_handler = packet_handler; 660 client->poll_interval_ms = poll_interval_ms; 661 client->state = BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_SERVICE; 662 663 uint8_t status = battery_service_client_request_send_gatt_query(client); 664 if (status != ERROR_CODE_SUCCESS){ 665 client->state = BATTERY_SERVICE_CLIENT_STATE_IDLE; 666 } 667 return status; 668 } 669 670 uint8_t battery_service_client_disconnect(uint16_t battery_service_cid){ 671 battery_service_client_t * client = battery_service_get_client_for_cid(battery_service_cid); 672 if (client == NULL){ 673 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 674 } 675 // finalize connections 676 battery_service_finalize_client(client); 677 return ERROR_CODE_SUCCESS; 678 } 679 680 681 uint8_t battery_service_client_read_battery_level(uint16_t battery_service_cid, uint8_t service_index){ 682 battery_service_client_t * client = battery_service_get_client_for_cid(battery_service_cid); 683 if (client == NULL) { 684 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 685 } 686 if (client->state < BATTERY_SERVICE_CLIENT_STATE_CONNECTED) { 687 return GATT_CLIENT_IN_WRONG_STATE; 688 } 689 if (service_index >= client->num_instances) { 690 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 691 } 692 693 client->need_poll_bitmap |= (1u << service_index); 694 695 uint8_t status = battery_service_client_request_send_gatt_query(client); 696 if (status != ERROR_CODE_SUCCESS){ 697 client->need_poll_bitmap &= ~(1u << client->polled_service_index); 698 } 699 return status; 700 } 701 702 void battery_service_client_init(void){ 703 hci_event_callback_registration.callback = &handle_hci_event; 704 hci_add_event_handler(&hci_event_callback_registration); 705 battery_service_handle_can_send_now.callback = &battery_service_send_next_query; 706 } 707 708 void battery_service_client_deinit(void){ 709 battery_service_cid_counter = 0; 710 clients = NULL; 711 } 712 713