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