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