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