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