xref: /btstack/src/ble/gatt-service/battery_service_client.c (revision 98f6d969a938c70e3dde105515d11fbd80f9f5db)
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->num_instances < MAX_NUM_BATTERY_SERVICES){
318                 gatt_event_service_query_result_get_service(packet, &service);
319                 client->services[client->num_instances].start_handle = service.start_group_handle;
320                 client->services[client->num_instances].end_handle = service.end_group_handle;
321 
322 #ifdef ENABLE_TESTING_SUPPORT
323                 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);
324 #endif
325                 client->num_instances++;
326             } else {
327                 log_info("Found more then %d, Battery Service instances. Increase MAX_NUM_BATTERY_SERVICES to store all.", MAX_NUM_BATTERY_SERVICES);
328             }
329             break;
330 
331         case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
332             client = battery_service_get_client_for_con_handle(gatt_event_characteristic_query_result_get_handle(packet));
333             btstack_assert(client != NULL);
334 
335             gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic);
336             if (client->service_index < client->num_instances){
337                 btstack_assert(characteristic.uuid16 == ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
338 
339                 client->services[client->service_index].value_handle = characteristic.value_handle;
340                 client->services[client->service_index].properties = characteristic.properties;
341 
342 #ifdef ENABLE_TESTING_SUPPORT
343                 printf("Battery Level Characteristic:\n    Attribute Handle 0x%04X, Properties 0x%02X, Handle 0x%04X, UUID 0x%04X, service %d\n",
344                     // hid_characteristic_name(characteristic.uuid16),
345                     characteristic.start_handle,
346                     characteristic.properties,
347                     characteristic.value_handle, characteristic.uuid16,
348                     client->service_index);
349 #endif
350             }
351             break;
352 
353         case GATT_EVENT_NOTIFICATION:
354             // ignore if wrong (event type 1, length 1, handle 2, value handle 2, value length 2, value 1)
355             if (size != 9) break;
356 
357             client = battery_service_get_client_for_con_handle(gatt_event_notification_get_handle(packet));
358             btstack_assert(client != NULL);
359 
360             if (gatt_event_notification_get_value_length(packet) != 1) break;
361 
362             battery_service_emit_battery_level(client,
363                 gatt_event_notification_get_value_handle(packet),
364                 ATT_ERROR_SUCCESS,
365                 gatt_event_notification_get_value(packet)[0]);
366             break;
367 
368         case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT:
369             client = battery_service_get_client_for_con_handle(gatt_event_characteristic_value_query_result_get_handle(packet));
370             btstack_assert(client != NULL);
371 
372             switch (client->state){
373 #ifdef ENABLE_TESTING_SUPPORT
374                 case BATTERY_SERVICE_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT:
375                     printf("    Received CCC value: ");
376                     printf_hexdump(gatt_event_characteristic_value_query_result_get_value(packet),  gatt_event_characteristic_value_query_result_get_value_length(packet));
377                     break;
378 #endif
379                 case BATTERY_SERVICE_CLIENT_STATE_CONNECTED:
380                     // ignore if wrong (event type 1, length 1, handle 2, value handle 2, value length 2, value 1)
381                     if (size != 9) break;
382                     if (gatt_event_characteristic_value_query_result_get_value_length(packet) != 1) break;
383 
384                     battery_service_emit_battery_level(client,
385                         gatt_event_characteristic_value_query_result_get_value_handle(packet),
386                         ATT_ERROR_SUCCESS,
387                         gatt_event_characteristic_value_query_result_get_value(packet)[0]);
388                     break;
389 
390                 default:
391                     break;
392             }
393 
394             // call run for client function to trigger next poll
395             break;
396 
397 #ifdef ENABLE_TESTING_SUPPORT
398         case GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:{
399             gatt_client_characteristic_descriptor_t characteristic_descriptor;
400 
401             client = battery_service_get_client_for_con_handle(gatt_event_all_characteristic_descriptors_query_result_get_handle(packet));
402             btstack_assert(client != NULL);
403             gatt_event_all_characteristic_descriptors_query_result_get_characteristic_descriptor(packet, &characteristic_descriptor);
404 
405             if (characteristic_descriptor.uuid16 == ORG_BLUETOOTH_DESCRIPTOR_GATT_CLIENT_CHARACTERISTIC_CONFIGURATION){
406                 client->services[client->service_index].ccc_handle = characteristic_descriptor.handle;
407 
408                 printf("    Battery Level Client Characteristic Configuration Descriptor[%d]:  Handle 0x%04X, UUID 0x%04X\n",
409                     client->service_index,
410                     characteristic_descriptor.handle,
411                     characteristic_descriptor.uuid16);
412             }
413             break;
414         }
415 #endif
416 
417         case GATT_EVENT_QUERY_COMPLETE:
418             client = battery_service_get_client_for_con_handle(gatt_event_query_complete_get_handle(packet));
419             btstack_assert(client != NULL);
420 
421             status = gatt_event_query_complete_get_att_status(packet);
422             switch (client->state){
423                 case BATTERY_SERVICE_CLIENT_STATE_W4_SERVICE_RESULT:
424                     if (status != ATT_ERROR_SUCCESS){
425                         battery_service_emit_connection_established(client, status);
426                         battery_service_finalize_client(client);
427                         return;
428                     }
429 
430                     if (client->num_instances == 0){
431                         battery_service_emit_connection_established(client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
432                         battery_service_finalize_client(client);
433                         return;
434                     }
435 
436                     if (client->num_instances > MAX_NUM_BATTERY_SERVICES) {
437                         log_info("%d battery services found, only first %d can be stored, increase MAX_NUM_BATTERY_SERVICES", client->num_instances, MAX_NUM_BATTERY_SERVICES);
438                         client->num_instances = MAX_NUM_BATTERY_SERVICES;
439                     }
440 
441                     client->service_index = 0;
442                     client->state = BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTICS;
443                     break;
444 
445                 case BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_RESULT:
446                     if (status != ATT_ERROR_SUCCESS){
447                         battery_service_emit_connection_established(client, status);
448                         battery_service_finalize_client(client);
449                         return;
450                     }
451 
452                     // check if there is another service to query
453                     if ((client->service_index + 1) < client->num_instances){
454                         client->service_index++;
455                         client->state = BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTICS;
456                         break;
457                     }
458 
459                     battery_service_client_validate_service(client);
460 
461                     if (client->num_instances == 0){
462                         battery_service_emit_connection_established(client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
463                         battery_service_finalize_client(client);
464                         return;
465                     }
466 
467                     // we are done with quering all services
468                     client->service_index = 0;
469 
470 #ifdef ENABLE_TESTING_SUPPORT
471                     client->state = BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTIC_DESCRIPTORS;
472 #else
473                     // wait for notification registration
474                     // to send connection established event
475                     client->state = BATTERY_SERVICE_CLIENT_STATE_W2_REGISTER_NOTIFICATION;
476 #endif
477                     break;
478 
479 #ifdef ENABLE_TESTING_SUPPORT
480                 case BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_DESCRIPTORS_RESULT:
481                     if ((client->service_index + 1) < client->num_instances){
482                         client->service_index++;
483                         client->state = BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTIC_DESCRIPTORS;
484                         break;
485                     }
486                     client->service_index = 0;
487                     client->state = BATTERY_SERVICE_CLIENT_STATE_W2_REGISTER_NOTIFICATION;
488                     break;
489 
490                 case BATTERY_SERVICE_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT:
491                     client->state = BATTERY_SERVICE_CLIENT_STATE_CONNECTED;
492                     battery_service_emit_connection_established(client, ERROR_CODE_SUCCESS);
493 
494                     if (client->poll_interval_ms > 0){
495                         client->need_poll_bitmap = client->poll_bitmap;
496                         battery_service_poll_timer_start(client);
497                     }
498                     break;
499 #endif
500                 case BATTERY_SERVICE_CLIENT_STATE_W4_NOTIFICATION_REGISTERED:
501                     if ((client->service_index + 1) < client->num_instances){
502                         client->service_index++;
503                         client->state = BATTERY_SERVICE_CLIENT_STATE_W2_REGISTER_NOTIFICATION;
504                         break;
505                     }
506 #ifdef ENABLE_TESTING_SUPPORT
507                     client->service_index = 0;
508 
509                     for (client->service_index = 0; client->service_index < client->num_instances; client->service_index++){
510                         if (client->services[client->service_index].ccc_handle != 0) {
511                             client->state = BATTERY_SERVICE_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION;
512                             break;
513                         }
514                     }
515                     if (client->service_index < client->num_instances){
516                         client->state = BATTERY_SERVICE_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION;
517                         break;
518                     }
519 #endif
520                     client->state = BATTERY_SERVICE_CLIENT_STATE_CONNECTED;
521                     battery_service_emit_connection_established(client, ERROR_CODE_SUCCESS);
522 
523                     if (client->poll_interval_ms > 0){
524                         client->need_poll_bitmap = client->poll_bitmap;
525                         battery_service_poll_timer_start(client);
526                     }
527                     break;
528 
529                 case BATTERY_SERVICE_CLIENT_STATE_CONNECTED:
530                     if (client->polled_service_index != BATTERY_SERVICE_INVALID_INDEX){
531                         if (status != ATT_ERROR_SUCCESS){
532                             battery_service_emit_battery_level(client, client->services[client->polled_service_index].value_handle, status, 0);
533                         }
534                         client->polled_service_index = BATTERY_SERVICE_INVALID_INDEX;
535                     }
536                     break;
537 
538                 default:
539                     break;
540 
541             }
542             break;
543 
544 
545         default:
546             break;
547     }
548 
549     if (client != NULL){
550         battery_service_run_for_client(client);
551     }
552 }
553 
554 
555 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){
556     btstack_assert(packet_handler != NULL);
557 
558     battery_service_client_t * client = battery_service_get_client_for_con_handle(con_handle);
559     if (client != NULL){
560         return ERROR_CODE_COMMAND_DISALLOWED;
561     }
562 
563     uint16_t cid = battery_service_get_next_cid();
564     if (battery_service_cid != NULL) {
565         *battery_service_cid = cid;
566     }
567 
568     client = battery_service_create_client(con_handle, cid);
569     if (client == NULL) {
570         return BTSTACK_MEMORY_ALLOC_FAILED;
571     }
572 
573     client->client_handler = packet_handler;
574     client->poll_interval_ms = poll_interval_ms;
575     client->state = BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_SERVICE;
576     battery_service_run_for_client(client);
577     return ERROR_CODE_SUCCESS;
578 }
579 
580 uint8_t battery_service_client_disconnect(uint16_t battery_service_cid){
581     battery_service_client_t * client = battery_service_get_client_for_cid(battery_service_cid);
582     if (client == NULL){
583         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
584     }
585     // finalize connections
586     battery_service_finalize_client(client);
587     return ERROR_CODE_SUCCESS;
588 }
589 
590 uint8_t battery_service_client_read_battery_level(uint16_t battery_service_cid, uint8_t service_index){
591     battery_service_client_t * client = battery_service_get_client_for_cid(battery_service_cid);
592     if (client == NULL) {
593         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
594     }
595     if (client->state != BATTERY_SERVICE_CLIENT_STATE_CONNECTED) {
596         return GATT_CLIENT_IN_WRONG_STATE;
597     }
598     if (service_index >= client->num_instances) {
599         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
600     }
601 
602     client->need_poll_bitmap |= (1u << service_index);
603     battery_service_run_for_client(client);
604     return ERROR_CODE_SUCCESS;
605 }
606 
607 void battery_service_client_init(void){}
608 
609 void battery_service_client_deinit(void){
610     battery_service_cid_counter = 0;
611     clients = NULL;
612 }
613 
614