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 MATTHIAS 24 * RINGWALD 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_linked_list_t clients; 65 static uint16_t battery_service_cid_counter = 0; 66 67 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 68 static void battery_service_poll_timer_start(battery_service_client_t * client); 69 70 static uint16_t battery_service_get_next_cid(void){ 71 if (battery_service_cid_counter == 0xffff) { 72 battery_service_cid_counter = 1; 73 } else { 74 battery_service_cid_counter++; 75 } 76 return battery_service_cid_counter; 77 } 78 79 static battery_service_client_t * battery_service_create_client(hci_con_handle_t con_handle, uint16_t cid){ 80 battery_service_client_t * client = btstack_memory_battery_service_client_get(); 81 if (!client){ 82 log_error("Not enough memory to create client"); 83 return NULL; 84 } 85 client->cid = cid; 86 client->con_handle = con_handle; 87 client->poll_interval_ms = 0; 88 client->num_instances = 0; 89 client->service_index = 0; 90 client->poll_bitmap = 0; 91 client->need_poll_bitmap = 0; 92 client->polled_service_index = BATTERY_SERVICE_INVALID_INDEX; 93 client->state = BATTERY_SERVICE_CLIENT_STATE_IDLE; 94 95 btstack_linked_list_add(&clients, (btstack_linked_item_t *) client); 96 return client; 97 } 98 99 static void battery_service_finalize_client(battery_service_client_t * client){ 100 // stop listening 101 uint8_t i; 102 for (i = 0; i < client->num_instances; i++){ 103 gatt_client_stop_listening_for_characteristic_value_updates(&client->services[i].notification_listener); 104 } 105 106 // remove timer 107 btstack_run_loop_remove_timer(&client->poll_timer); 108 109 btstack_linked_list_remove(&clients, (btstack_linked_item_t *) client); 110 btstack_memory_battery_service_client_free(client); 111 } 112 113 static battery_service_client_t * battery_service_get_client_for_con_handle(hci_con_handle_t con_handle){ 114 btstack_linked_list_iterator_t it; 115 btstack_linked_list_iterator_init(&it, &clients); 116 while (btstack_linked_list_iterator_has_next(&it)){ 117 battery_service_client_t * client = (battery_service_client_t *)btstack_linked_list_iterator_next(&it); 118 if (client->con_handle != con_handle) continue; 119 return client; 120 } 121 return NULL; 122 } 123 124 static battery_service_client_t * battery_service_get_client_for_cid(uint16_t battery_service_cid){ 125 btstack_linked_list_iterator_t it; 126 btstack_linked_list_iterator_init(&it, &clients); 127 while (btstack_linked_list_iterator_has_next(&it)){ 128 battery_service_client_t * client = (battery_service_client_t *)btstack_linked_list_iterator_next(&it); 129 if (client->cid != battery_service_cid) continue; 130 return client; 131 } 132 return NULL; 133 } 134 135 static void battery_service_emit_connection_established(battery_service_client_t * client, uint8_t status){ 136 uint8_t event[8]; 137 int pos = 0; 138 event[pos++] = HCI_EVENT_GATTSERVICE_META; 139 event[pos++] = sizeof(event) - 2; 140 event[pos++] = GATTSERVICE_SUBEVENT_BATTERY_SERVICE_CONNECTED; 141 little_endian_store_16(event, pos, client->cid); 142 pos += 2; 143 event[pos++] = status; 144 event[pos++] = client->num_instances; 145 event[pos++] = client->poll_bitmap; 146 147 (*client->client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 148 } 149 150 static void battery_service_emit_battery_level(battery_service_client_t * client, uint16_t value_handle, uint8_t att_status, uint8_t battery_level){ 151 uint8_t event[8]; 152 int pos = 0; 153 event[pos++] = HCI_EVENT_GATTSERVICE_META; 154 event[pos++] = sizeof(event) - 2; 155 event[pos++] = GATTSERVICE_SUBEVENT_BATTERY_SERVICE_LEVEL; 156 little_endian_store_16(event, pos, client->cid); 157 pos += 2; 158 159 uint8_t i; 160 for (i = 0; i < client->num_instances; i++){ 161 if (value_handle == client->services[i].value_handle){ 162 event[pos++] = i; 163 event[pos++] = att_status; 164 event[pos++] = battery_level; 165 (*client->client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 166 break; 167 } 168 } 169 } 170 171 static void battery_service_run_for_client(battery_service_client_t * client){ 172 uint8_t status; 173 uint8_t i; 174 gatt_client_characteristic_t characteristic; 175 176 switch (client->state){ 177 case BATTERY_SERVICE_CLIENT_STATE_CONNECTED: 178 for (i = 0; i < client->num_instances; i++){ 179 if ( ((client->need_poll_bitmap >> i) & 0x01) == 0x01 ){ 180 // clear bit of polled service 181 client->need_poll_bitmap &= ~(1u << i); 182 client->polled_service_index = i; 183 184 // poll value of characteristic 185 characteristic.value_handle = client->services[i].value_handle; 186 characteristic.properties = client->services[i].properties; 187 characteristic.end_handle = client->services[i].end_handle; 188 gatt_client_read_value_of_characteristic(&handle_gatt_client_event, client->con_handle, &characteristic); 189 break; 190 } 191 } 192 break; 193 194 case BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_SERVICE: 195 client->state = BATTERY_SERVICE_CLIENT_STATE_W4_SERVICE_RESULT; 196 status = gatt_client_discover_primary_services_by_uuid16(&handle_gatt_client_event, client->con_handle, ORG_BLUETOOTH_SERVICE_BATTERY_SERVICE); 197 // TODO handle status 198 break; 199 200 case BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTICS: 201 client->state = BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_RESULT; 202 203 gatt_client_discover_characteristics_for_handle_range_by_uuid16( 204 &handle_gatt_client_event, 205 client->con_handle, 206 client->services[client->service_index].start_handle, 207 client->services[client->service_index].end_handle, 208 ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL); 209 210 break; 211 212 #ifdef ENABLE_TESTING_SUPPORT 213 case BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTIC_DESCRIPTORS: 214 client->state = BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_DESCRIPTORS_RESULT; 215 // if there are services without notification, register pool timer, 216 // othervise register for notifications 217 characteristic.value_handle = client->services[client->service_index].value_handle; 218 characteristic.properties = client->services[client->service_index].properties; 219 characteristic.end_handle = client->services[client->service_index].end_handle; 220 221 (void) gatt_client_discover_characteristic_descriptors(&handle_gatt_client_event, client->con_handle, &characteristic); 222 break; 223 224 case BATTERY_SERVICE_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION: 225 printf("Read client characteristic value [Service %d, handle 0x%04X]:\n", 226 client->service_index, 227 client->services[client->service_index].ccc_handle); 228 229 client->state = BATTERY_SERVICE_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT; 230 231 // result in GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT 232 (void) gatt_client_read_value_of_characteristic_using_value_handle( 233 &handle_gatt_client_event, 234 client->con_handle, 235 client->services[client->service_index].ccc_handle); 236 break; 237 #endif 238 239 case BATTERY_SERVICE_CLIENT_STATE_W2_REGISTER_NOTIFICATION: 240 client->state = BATTERY_SERVICE_CLIENT_STATE_W4_NOTIFICATION_REGISTERED; 241 // if there are services without notification, register pool timer, 242 // othervise register for notifications 243 characteristic.value_handle = client->services[client->service_index].value_handle; 244 characteristic.properties = client->services[client->service_index].properties; 245 characteristic.end_handle = client->services[client->service_index].end_handle; 246 247 status = gatt_client_write_client_characteristic_configuration( 248 &handle_gatt_client_event, 249 client->con_handle, 250 &characteristic, 251 GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION); 252 253 // notification supported, register for value updates 254 if (status == ERROR_CODE_SUCCESS){ 255 gatt_client_listen_for_characteristic_value_updates( 256 &client->services[client->service_index].notification_listener, 257 &handle_gatt_client_event, 258 client->con_handle, 259 &characteristic); 260 } else { 261 client->poll_bitmap |= 1u << client->service_index; 262 } 263 break; 264 default: 265 break; 266 } 267 } 268 269 static void battery_service_poll_timer_timeout_handler(btstack_timer_source_t * timer){ 270 uint16_t battery_service_cid = (uint16_t)(uintptr_t) btstack_run_loop_get_timer_context(timer); 271 272 battery_service_client_t * client = battery_service_get_client_for_cid(battery_service_cid); 273 btstack_assert(client != NULL); 274 275 client->need_poll_bitmap = client->poll_bitmap; 276 battery_service_poll_timer_start(client); 277 battery_service_run_for_client(client); 278 } 279 280 static void battery_service_poll_timer_start(battery_service_client_t * client){ 281 btstack_run_loop_set_timer_handler(&client->poll_timer, battery_service_poll_timer_timeout_handler); 282 btstack_run_loop_set_timer_context(&client->poll_timer, (void *)(uintptr_t)client->cid); 283 284 btstack_run_loop_set_timer(&client->poll_timer, client->poll_interval_ms); 285 btstack_run_loop_add_timer(&client->poll_timer); 286 } 287 288 static void battery_service_client_validate_service(battery_service_client_t * client){ 289 // remove all services without characteristic (array in-place) 290 uint8_t src_index = 0; // next entry to check 291 uint8_t dest_index = 0; // to store entry 292 for (src_index = 0; src_index < client->num_instances; src_index++){ 293 if (client->services[src_index].value_handle != 0){ 294 if (src_index != dest_index) { 295 client->services[dest_index] = client->services[src_index]; 296 } 297 dest_index++; 298 } 299 } 300 client->num_instances = dest_index; 301 } 302 303 static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 304 UNUSED(packet_type); 305 UNUSED(channel); 306 307 battery_service_client_t * client = NULL; 308 gatt_client_service_t service; 309 gatt_client_characteristic_t characteristic; 310 uint8_t status; 311 312 switch(hci_event_packet_get_type(packet)){ 313 case GATT_EVENT_SERVICE_QUERY_RESULT: 314 client = battery_service_get_client_for_con_handle(gatt_event_service_query_result_get_handle(packet)); 315 btstack_assert(client != NULL); 316 317 if (client->state != BATTERY_SERVICE_CLIENT_STATE_W4_SERVICE_RESULT) { 318 battery_service_emit_connection_established(client, GATT_CLIENT_IN_WRONG_STATE); 319 battery_service_finalize_client(client); 320 return; 321 } 322 323 if (client->num_instances < MAX_NUM_BATTERY_SERVICES){ 324 gatt_event_service_query_result_get_service(packet, &service); 325 client->services[client->num_instances].start_handle = service.start_group_handle; 326 client->services[client->num_instances].end_handle = service.end_group_handle; 327 328 #ifdef ENABLE_TESTING_SUPPORT 329 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); 330 #endif 331 client->num_instances++; 332 } else { 333 log_info("Found more then %d, Battery Service instances. Increase MAX_NUM_BATTERY_SERVICES to store all.", MAX_NUM_BATTERY_SERVICES); 334 } 335 break; 336 337 case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT: 338 client = battery_service_get_client_for_con_handle(gatt_event_characteristic_query_result_get_handle(packet)); 339 btstack_assert(client != NULL); 340 341 gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic); 342 if (client->service_index < client->num_instances){ 343 if (characteristic.uuid16 != ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL) { 344 break; 345 } 346 client->services[client->service_index].value_handle = characteristic.value_handle; 347 client->services[client->service_index].properties = characteristic.properties; 348 349 #ifdef ENABLE_TESTING_SUPPORT 350 printf("Battery Level Characteristic:\n Attribute Handle 0x%04X, Properties 0x%02X, Handle 0x%04X, UUID 0x%04X, service %d\n", 351 // hid_characteristic_name(characteristic.uuid16), 352 characteristic.start_handle, 353 characteristic.properties, 354 characteristic.value_handle, characteristic.uuid16, 355 client->service_index); 356 #endif 357 } 358 break; 359 360 case GATT_EVENT_NOTIFICATION: 361 // ignore if wrong (event type 1, length 1, handle 2, value handle 2, value length 2, value 1) 362 if (size != 9) break; 363 364 client = battery_service_get_client_for_con_handle(gatt_event_notification_get_handle(packet)); 365 btstack_assert(client != NULL); 366 367 if (gatt_event_notification_get_value_length(packet) != 1) break; 368 369 battery_service_emit_battery_level(client, 370 gatt_event_notification_get_value_handle(packet), 371 ATT_ERROR_SUCCESS, 372 gatt_event_notification_get_value(packet)[0]); 373 break; 374 375 case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT: 376 client = battery_service_get_client_for_con_handle(gatt_event_characteristic_value_query_result_get_handle(packet)); 377 btstack_assert(client != NULL); 378 379 switch (client->state){ 380 #ifdef ENABLE_TESTING_SUPPORT 381 case BATTERY_SERVICE_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT: 382 printf(" Received CCC value: "); 383 printf_hexdump(gatt_event_characteristic_value_query_result_get_value(packet), gatt_event_characteristic_value_query_result_get_value_length(packet)); 384 break; 385 #endif 386 case BATTERY_SERVICE_CLIENT_STATE_CONNECTED: 387 // ignore if wrong (event type 1, length 1, handle 2, value handle 2, value length 2, value 1) 388 if (size != 9) break; 389 if (gatt_event_characteristic_value_query_result_get_value_length(packet) != 1) break; 390 391 battery_service_emit_battery_level(client, 392 gatt_event_characteristic_value_query_result_get_value_handle(packet), 393 ATT_ERROR_SUCCESS, 394 gatt_event_characteristic_value_query_result_get_value(packet)[0]); 395 break; 396 397 default: 398 break; 399 } 400 401 // call run for client function to trigger next poll 402 break; 403 404 #ifdef ENABLE_TESTING_SUPPORT 405 case GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:{ 406 gatt_client_characteristic_descriptor_t characteristic_descriptor; 407 408 client = battery_service_get_client_for_con_handle(gatt_event_all_characteristic_descriptors_query_result_get_handle(packet)); 409 btstack_assert(client != NULL); 410 gatt_event_all_characteristic_descriptors_query_result_get_characteristic_descriptor(packet, &characteristic_descriptor); 411 412 if (characteristic_descriptor.uuid16 == ORG_BLUETOOTH_DESCRIPTOR_GATT_CLIENT_CHARACTERISTIC_CONFIGURATION){ 413 client->services[client->service_index].ccc_handle = characteristic_descriptor.handle; 414 415 printf(" Battery Level Client Characteristic Configuration Descriptor[%d]: Handle 0x%04X, UUID 0x%04X\n", 416 client->service_index, 417 characteristic_descriptor.handle, 418 characteristic_descriptor.uuid16); 419 } 420 break; 421 } 422 #endif 423 424 case GATT_EVENT_QUERY_COMPLETE: 425 client = battery_service_get_client_for_con_handle(gatt_event_query_complete_get_handle(packet)); 426 btstack_assert(client != NULL); 427 428 status = gatt_event_query_complete_get_att_status(packet); 429 switch (client->state){ 430 case BATTERY_SERVICE_CLIENT_STATE_W4_SERVICE_RESULT: 431 if (status != ATT_ERROR_SUCCESS){ 432 battery_service_emit_connection_established(client, status); 433 battery_service_finalize_client(client); 434 return; 435 } 436 437 if (client->num_instances == 0){ 438 battery_service_emit_connection_established(client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE); 439 battery_service_finalize_client(client); 440 return; 441 } 442 443 if (client->num_instances > MAX_NUM_BATTERY_SERVICES) { 444 log_info("%d battery services found, only first %d can be stored, increase MAX_NUM_BATTERY_SERVICES", client->num_instances, MAX_NUM_BATTERY_SERVICES); 445 client->num_instances = MAX_NUM_BATTERY_SERVICES; 446 } 447 448 client->service_index = 0; 449 client->state = BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTICS; 450 break; 451 452 case BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_RESULT: 453 if (status != ATT_ERROR_SUCCESS){ 454 battery_service_emit_connection_established(client, status); 455 battery_service_finalize_client(client); 456 return; 457 } 458 459 // check if there is another service to query 460 if ((client->service_index + 1) < client->num_instances){ 461 client->service_index++; 462 client->state = BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTICS; 463 break; 464 } 465 466 battery_service_client_validate_service(client); 467 468 if (client->num_instances == 0){ 469 battery_service_emit_connection_established(client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE); 470 battery_service_finalize_client(client); 471 return; 472 } 473 474 // we are done with quering all services 475 client->service_index = 0; 476 477 #ifdef ENABLE_TESTING_SUPPORT 478 client->state = BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTIC_DESCRIPTORS; 479 #else 480 // wait for notification registration 481 // to send connection established event 482 client->state = BATTERY_SERVICE_CLIENT_STATE_W2_REGISTER_NOTIFICATION; 483 #endif 484 break; 485 486 #ifdef ENABLE_TESTING_SUPPORT 487 case BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_DESCRIPTORS_RESULT: 488 if ((client->service_index + 1) < client->num_instances){ 489 client->service_index++; 490 client->state = BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTIC_DESCRIPTORS; 491 break; 492 } 493 client->service_index = 0; 494 client->state = BATTERY_SERVICE_CLIENT_STATE_W2_REGISTER_NOTIFICATION; 495 break; 496 497 case BATTERY_SERVICE_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT: 498 client->state = BATTERY_SERVICE_CLIENT_STATE_CONNECTED; 499 battery_service_emit_connection_established(client, ERROR_CODE_SUCCESS); 500 501 if (client->poll_interval_ms > 0){ 502 client->need_poll_bitmap = client->poll_bitmap; 503 battery_service_poll_timer_start(client); 504 } 505 break; 506 #endif 507 case BATTERY_SERVICE_CLIENT_STATE_W4_NOTIFICATION_REGISTERED: 508 if ((client->service_index + 1) < client->num_instances){ 509 client->service_index++; 510 client->state = BATTERY_SERVICE_CLIENT_STATE_W2_REGISTER_NOTIFICATION; 511 break; 512 } 513 #ifdef ENABLE_TESTING_SUPPORT 514 client->service_index = 0; 515 client->state = BATTERY_SERVICE_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION; 516 #else 517 client->state = BATTERY_SERVICE_CLIENT_STATE_CONNECTED; 518 battery_service_emit_connection_established(client, ERROR_CODE_SUCCESS); 519 520 if (client->poll_interval_ms > 0){ 521 client->need_poll_bitmap = client->poll_bitmap; 522 battery_service_poll_timer_start(client); 523 } 524 #endif 525 break; 526 527 case BATTERY_SERVICE_CLIENT_STATE_CONNECTED: 528 if (client->polled_service_index != BATTERY_SERVICE_INVALID_INDEX){ 529 if (status != ATT_ERROR_SUCCESS){ 530 battery_service_emit_battery_level(client, client->services[client->polled_service_index].value_handle, status, 0); 531 } 532 client->polled_service_index = BATTERY_SERVICE_INVALID_INDEX; 533 } 534 break; 535 536 default: 537 break; 538 539 } 540 break; 541 542 543 default: 544 break; 545 } 546 547 if (client != NULL){ 548 battery_service_run_for_client(client); 549 } 550 } 551 552 553 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){ 554 btstack_assert(packet_handler != NULL); 555 556 battery_service_client_t * client = battery_service_get_client_for_con_handle(con_handle); 557 if (client != NULL){ 558 return ERROR_CODE_COMMAND_DISALLOWED; 559 } 560 561 uint16_t cid = battery_service_get_next_cid(); 562 if (battery_service_cid != NULL) { 563 *battery_service_cid = cid; 564 } 565 566 client = battery_service_create_client(con_handle, cid); 567 if (client == NULL) { 568 return BTSTACK_MEMORY_ALLOC_FAILED; 569 } 570 571 client->client_handler = packet_handler; 572 client->poll_interval_ms = poll_interval_ms; 573 client->state = BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_SERVICE; 574 battery_service_run_for_client(client); 575 return ERROR_CODE_SUCCESS; 576 } 577 578 uint8_t battery_service_client_disconnect(uint16_t battery_service_cid){ 579 battery_service_client_t * client = battery_service_get_client_for_cid(battery_service_cid); 580 if (client == NULL){ 581 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 582 } 583 // finalize connections 584 battery_service_finalize_client(client); 585 return ERROR_CODE_SUCCESS; 586 } 587 588 uint8_t battery_service_client_read_battery_level(uint16_t battery_service_cid, uint8_t service_index){ 589 battery_service_client_t * client = battery_service_get_client_for_cid(battery_service_cid); 590 if (client == NULL) { 591 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 592 } 593 if (client->state != BATTERY_SERVICE_CLIENT_STATE_CONNECTED) { 594 return GATT_CLIENT_IN_WRONG_STATE; 595 } 596 if (service_index >= client->num_instances) { 597 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 598 } 599 600 client->need_poll_bitmap |= (1u << service_index); 601 battery_service_run_for_client(client); 602 return ERROR_CODE_SUCCESS; 603 } 604 605 void battery_service_client_init(void){} 606 607 void battery_service_client_deinit(void){ 608 battery_service_cid_counter = 0; 609 clients = NULL; 610 } 611 612