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