1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
75
76
77
78
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
102
103
104
105
106
107
108
109
110
120
121
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
191
192
193
194
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
276
277
278
279
287
288
289
290
291
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
314
315
329
330
331
332
333
334
339
340
345
346
347
348
349
350
351
356
357
358
363
364
365
366
371
372
373
374
375
376
377
378
381
382
383
384
385
386
391
392
393
394
395
396
397
398
399
400
401
402
407
408
417
418
419
420
421
422
431
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
545
547
548
549
552
553
554
555
556
557
558
559
560
561
562
563
578
579
580
581
582
583
584
587
588
589
592
593
594
597
598
599
600
601
602
603
604
605
614
615
616
617
620
623
626
627
628
629
630
631
632
636
637
641
/* ... */
#define BTSTACK_FILE__ "battery_service_client.c"
#include "btstack_config.h"
#ifdef ENABLE_TESTING_SUPPORT
#include <stdio.h>
#include <unistd.h>
/* ... */#endif
#include <stdint.h>
#include <string.h>
#include "ble/gatt-service/battery_service_client.h"
#include "btstack_memory.h"
#include "ble/core.h"
#include "ble/gatt_client.h"
#include "bluetooth_gatt.h"
#include "btstack_debug.h"
#include "btstack_event.h"
#include "btstack_run_loop.h"
#include "gap.h"
11 includes
#define BATTERY_SERVICE_INVALID_INDEX 0xFF
static btstack_linked_list_t clients;
static uint16_t battery_service_cid_counter = 0;
static btstack_packet_callback_registration_t hci_event_callback_registration;
static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
static void battery_service_poll_timer_start(battery_service_client_t * client);
static uint16_t battery_service_get_next_cid(void){
battery_service_cid_counter = btstack_next_cid_ignoring_zero(battery_service_cid_counter);
return battery_service_cid_counter;
}{ ... }
static battery_service_client_t * battery_service_create_client(hci_con_handle_t con_handle, uint16_t cid){
battery_service_client_t * client = btstack_memory_battery_service_client_get();
if (!client){
log_error("Not enough memory to create client");
return NULL;
}if (!client) { ... }
client->cid = cid;
client->con_handle = con_handle;
client->poll_interval_ms = 0;
client->num_instances = 0;
client->service_index = 0;
client->poll_bitmap = 0;
client->need_poll_bitmap = 0;
client->polled_service_index = BATTERY_SERVICE_INVALID_INDEX;
client->state = BATTERY_SERVICE_CLIENT_STATE_IDLE;
btstack_linked_list_add(&clients, (btstack_linked_item_t *) client);
return client;
}{ ... }
static void battery_service_finalize_client(battery_service_client_t * client){
uint8_t i;
for (i = 0; i < client->num_instances; i++){
gatt_client_stop_listening_for_characteristic_value_updates(&client->services[i].notification_listener);
}for (i = 0; i < client->num_instances; i++) { ... }
btstack_run_loop_remove_timer(&client->poll_timer);
btstack_linked_list_remove(&clients, (btstack_linked_item_t *) client);
btstack_memory_battery_service_client_free(client);
}{ ... }
static battery_service_client_t * battery_service_get_client_for_con_handle(hci_con_handle_t con_handle){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &clients);
while (btstack_linked_list_iterator_has_next(&it)){
battery_service_client_t * client = (battery_service_client_t *)btstack_linked_list_iterator_next(&it);
if (client->con_handle != con_handle) continue;
return client;
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
return NULL;
}{ ... }
static battery_service_client_t * battery_service_get_client_for_cid(uint16_t battery_service_cid){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &clients);
while (btstack_linked_list_iterator_has_next(&it)){
battery_service_client_t * client = (battery_service_client_t *)btstack_linked_list_iterator_next(&it);
if (client->cid != battery_service_cid) continue;
return client;
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
return NULL;
}{ ... }
static void battery_service_emit_connection_established(battery_service_client_t * client, uint8_t status){
uint8_t event[8];
int pos = 0;
event[pos++] = HCI_EVENT_GATTSERVICE_META;
event[pos++] = sizeof(event) - 2;
event[pos++] = GATTSERVICE_SUBEVENT_BATTERY_SERVICE_CONNECTED;
little_endian_store_16(event, pos, client->cid);
pos += 2;
event[pos++] = status;
event[pos++] = client->num_instances;
event[pos++] = client->poll_bitmap;
(*client->client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
}{ ... }
static void battery_service_emit_battery_level(battery_service_client_t * client, uint16_t value_handle, uint8_t att_status, uint8_t battery_level){
uint8_t event[8];
int pos = 0;
event[pos++] = HCI_EVENT_GATTSERVICE_META;
event[pos++] = sizeof(event) - 2;
event[pos++] = GATTSERVICE_SUBEVENT_BATTERY_SERVICE_LEVEL;
little_endian_store_16(event, pos, client->cid);
pos += 2;
uint8_t i;
for (i = 0; i < client->num_instances; i++){
if (value_handle == client->services[i].value_handle){
event[pos++] = i;
event[pos++] = att_status;
event[pos++] = battery_level;
(*client->client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
break;
}if (value_handle == client->services[i].value_handle) { ... }
}for (i = 0; i < client->num_instances; i++) { ... }
}{ ... }
static bool battery_service_registered_notification(battery_service_client_t * client, uint16_t service_index){
gatt_client_characteristic_t characteristic;
characteristic.value_handle = client->services[service_index].value_handle;
characteristic.properties = client->services[service_index].properties;
characteristic.end_handle = client->services[service_index].end_handle;
uint8_t status = gatt_client_write_client_characteristic_configuration(
&handle_gatt_client_event,
client->con_handle,
&characteristic,
GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION);
if (status == ERROR_CODE_SUCCESS){
gatt_client_listen_for_characteristic_value_updates(
&client->services[service_index].notification_listener,
&handle_gatt_client_event,
client->con_handle, &characteristic);
}if (status == ERROR_CODE_SUCCESS) { ... } else {
client->poll_bitmap |= 1u << client->service_index;
}else { ... }
return status;
}{ ... }
static void battery_service_start_polling_if_needed(battery_service_client_t * client){
if (client->poll_interval_ms > 0){
client->need_poll_bitmap = client->poll_bitmap;
battery_service_poll_timer_start(client);
}if (client->poll_interval_ms > 0) { ... }
}{ ... }
static void battery_service_run_for_client(battery_service_client_t * client){
uint8_t status;
uint8_t i;
gatt_client_characteristic_t characteristic;
switch (client->state){
case BATTERY_SERVICE_CLIENT_STATE_CONNECTED:
for (i = 0; i < client->num_instances; i++){
if ( ((client->need_poll_bitmap >> i) & 0x01) == 0x01 ){
client->need_poll_bitmap &= ~(1u << i);
client->polled_service_index = i;
characteristic.value_handle = client->services[i].value_handle;
characteristic.properties = client->services[i].properties;
characteristic.end_handle = client->services[i].end_handle;
gatt_client_read_value_of_characteristic(&handle_gatt_client_event, client->con_handle, &characteristic);
break;
}if (((client->need_poll_bitmap >> i) & 0x01) == 0x01) { ... }
}for (i = 0; i < client->num_instances; i++) { ... }
break;
case BATTERY_SERVICE_CLIENT_STATE_CONNECTED:
case BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_SERVICE:
client->state = BATTERY_SERVICE_CLIENT_STATE_W4_SERVICE_RESULT;
status = gatt_client_discover_primary_services_by_uuid16(&handle_gatt_client_event, client->con_handle, ORG_BLUETOOTH_SERVICE_BATTERY_SERVICE);
break;
case BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_SERVICE:
case BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTICS:
client->state = BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_RESULT;
gatt_client_discover_characteristics_for_handle_range_by_uuid16(
&handle_gatt_client_event,
client->con_handle,
client->services[client->service_index].start_handle,
client->services[client->service_index].end_handle,
ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
break;
#ifdef ENABLE_TESTING_SUPPORTcase BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTICS:
case BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTIC_DESCRIPTORS:
client->state = BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_DESCRIPTORS_RESULT;
characteristic.value_handle = client->services[client->service_index].value_handle;
characteristic.properties = client->services[client->service_index].properties;
characteristic.end_handle = client->services[client->service_index].end_handle;
(void) gatt_client_discover_characteristic_descriptors(&handle_gatt_client_event, client->con_handle, &characteristic);
break;
case BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTIC_DESCRIPTORS:
case BATTERY_SERVICE_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION:
printf("Read client characteristic value [Service %d, handle 0x%04X]:\n",
client->service_index,
client->services[client->service_index].ccc_handle);
client->state = BATTERY_SERVICE_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT;
(void) gatt_client_read_characteristic_descriptor_using_descriptor_handle(
&handle_gatt_client_event,
client->con_handle,
client->services[client->service_index].ccc_handle);
break;/* ... */
#endif
case BATTERY_SERVICE_CLIENT_STATE_W2_REGISTER_NOTIFICATION:
client->state = BATTERY_SERVICE_CLIENT_STATE_W4_NOTIFICATION_REGISTERED;
for (; client->service_index < client->num_instances; client->service_index++){
status = battery_service_registered_notification(client, client->service_index);
if (status == ERROR_CODE_SUCCESS) return;
}for (; client->service_index < client->num_instances; client->service_index++) { ... }
#ifdef ENABLE_TESTING_SUPPORT
for (client->service_index = 0; client->service_index < client->num_instances; client->service_index++){
bool need_polling = (client->poll_bitmap & (1 << client->service_index)) != 0;
if ( (client->services[client->service_index].ccc_handle != 0) && !need_polling ){
client->state = BATTERY_SERVICE_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION;
break;
}if ((client->services[client->service_index].ccc_handle != 0) && !need_polling) { ... }
}for (client->service_index = 0; client->service_index < client->num_instances; client->service_index++) { ... }
/* ... */#endif
client->state = BATTERY_SERVICE_CLIENT_STATE_CONNECTED;
battery_service_emit_connection_established(client, ERROR_CODE_SUCCESS);
battery_service_start_polling_if_needed(client);
break;case BATTERY_SERVICE_CLIENT_STATE_W2_REGISTER_NOTIFICATION:
default:
break;default
}switch (client->state) { ... }
}{ ... }
static void battery_service_poll_timer_timeout_handler(btstack_timer_source_t * timer){
uint16_t battery_service_cid = (uint16_t)(uintptr_t) btstack_run_loop_get_timer_context(timer);
battery_service_client_t * client = battery_service_get_client_for_cid(battery_service_cid);
btstack_assert(client != NULL);
client->need_poll_bitmap = client->poll_bitmap;
battery_service_poll_timer_start(client);
battery_service_run_for_client(client);
}{ ... }
static void battery_service_poll_timer_start(battery_service_client_t * client){
btstack_run_loop_set_timer_handler(&client->poll_timer, battery_service_poll_timer_timeout_handler);
btstack_run_loop_set_timer_context(&client->poll_timer, (void *)(uintptr_t)client->cid);
btstack_run_loop_set_timer(&client->poll_timer, client->poll_interval_ms);
btstack_run_loop_add_timer(&client->poll_timer);
}{ ... }
static void battery_service_client_validate_service(battery_service_client_t * client){
uint8_t src_index = 0;
uint8_t dest_index = 0;
for (src_index = 0; src_index < client->num_instances; src_index++){
if (client->services[src_index].value_handle != 0){
if (src_index != dest_index) {
client->services[dest_index] = client->services[src_index];
}if (src_index != dest_index) { ... }
dest_index++;
}if (client->services[src_index].value_handle != 0) { ... }
}for (src_index = 0; src_index < client->num_instances; src_index++) { ... }
client->num_instances = dest_index;
}{ ... }
static bool battery_service_client_handle_query_complete(battery_service_client_t * client, uint8_t status){
switch (client->state){
case BATTERY_SERVICE_CLIENT_STATE_W4_SERVICE_RESULT:
if (status != ATT_ERROR_SUCCESS){
battery_service_emit_connection_established(client, status);
battery_service_finalize_client(client);
return false;
}if (status != ATT_ERROR_SUCCESS) { ... }
if (client->num_instances == 0){
battery_service_emit_connection_established(client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
battery_service_finalize_client(client);
return false;
}if (client->num_instances == 0) { ... }
client->service_index = 0;
client->state = BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTICS;
break;
case BATTERY_SERVICE_CLIENT_STATE_W4_SERVICE_RESULT:
case BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_RESULT:
if (status != ATT_ERROR_SUCCESS){
battery_service_emit_connection_established(client, status);
battery_service_finalize_client(client);
return false;
}if (status != ATT_ERROR_SUCCESS) { ... }
if ((client->service_index + 1) < client->num_instances){
client->service_index++;
client->state = BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTICS;
break;
}if ((client->service_index + 1) < client->num_instances) { ... }
battery_service_client_validate_service(client);
if (client->num_instances == 0){
battery_service_emit_connection_established(client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
battery_service_finalize_client(client);
return false;
}if (client->num_instances == 0) { ... }
client->service_index = 0;
#ifdef ENABLE_TESTING_SUPPORT
client->state = BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTIC_DESCRIPTORS;
#else
client->state = BATTERY_SERVICE_CLIENT_STATE_W2_REGISTER_NOTIFICATION;/* ... */
#endif
break;
#ifdef ENABLE_TESTING_SUPPORTcase BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_RESULT:
case BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_DESCRIPTORS_RESULT:
if ((client->service_index + 1) < client->num_instances){
client->service_index++;
client->state = BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_CHARACTERISTIC_DESCRIPTORS;
break;
}if ((client->service_index + 1) < client->num_instances) { ... }
client->service_index = 0;
client->state = BATTERY_SERVICE_CLIENT_STATE_W2_REGISTER_NOTIFICATION;
break;
case BATTERY_SERVICE_CLIENT_STATE_W4_CHARACTERISTIC_DESCRIPTORS_RESULT:
case BATTERY_SERVICE_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT:
client->state = BATTERY_SERVICE_CLIENT_STATE_CONNECTED;
battery_service_emit_connection_established(client, ERROR_CODE_SUCCESS);
battery_service_start_polling_if_needed(client);
break;/* ... */
#endif
case BATTERY_SERVICE_CLIENT_STATE_W4_NOTIFICATION_REGISTERED:
if ((client->service_index + 1) < client->num_instances){
client->service_index++;
client->state = BATTERY_SERVICE_CLIENT_STATE_W2_REGISTER_NOTIFICATION;
break;
}if ((client->service_index + 1) < client->num_instances) { ... }
#ifdef ENABLE_TESTING_SUPPORT
for (client->service_index = 0; client->service_index < client->num_instances; client->service_index++){
bool need_polling = (client->poll_bitmap & (1 << client->service_index)) != 0;
printf("read CCC 1 0x%02x, polling %d \n", client->services[client->service_index].ccc_handle, (int) need_polling);
if ( (client->services[client->service_index].ccc_handle != 0) && !need_polling ) {
client->state = BATTERY_SERVICE_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION;
break;
}if ((client->services[client->service_index].ccc_handle != 0) && !need_polling) { ... }
}for (client->service_index = 0; client->service_index < client->num_instances; client->service_index++) { ... }
/* ... */#endif
client->state = BATTERY_SERVICE_CLIENT_STATE_CONNECTED;
battery_service_emit_connection_established(client, ERROR_CODE_SUCCESS);
battery_service_start_polling_if_needed(client);
break;
case BATTERY_SERVICE_CLIENT_STATE_W4_NOTIFICATION_REGISTERED:
case BATTERY_SERVICE_CLIENT_STATE_CONNECTED:
if (client->polled_service_index != BATTERY_SERVICE_INVALID_INDEX){
if (status != ATT_ERROR_SUCCESS){
battery_service_emit_battery_level(client, client->services[client->polled_service_index].value_handle, status, 0);
}if (status != ATT_ERROR_SUCCESS) { ... }
client->polled_service_index = BATTERY_SERVICE_INVALID_INDEX;
}if (client->polled_service_index != BATTERY_SERVICE_INVALID_INDEX) { ... }
break;
case BATTERY_SERVICE_CLIENT_STATE_CONNECTED:
default:
break;
default
}switch (client->state) { ... }
return true;
}{ ... }
static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
UNUSED(packet_type);
UNUSED(channel);
UNUSED(size);
battery_service_client_t * client = NULL;
gatt_client_service_t service;
gatt_client_characteristic_t characteristic;
bool call_run = true;
switch(hci_event_packet_get_type(packet)){
case GATT_EVENT_SERVICE_QUERY_RESULT:
client = battery_service_get_client_for_con_handle(gatt_event_service_query_result_get_handle(packet));
btstack_assert(client != NULL);
if (client->num_instances < MAX_NUM_BATTERY_SERVICES){
gatt_event_service_query_result_get_service(packet, &service);
client->services[client->num_instances].start_handle = service.start_group_handle;
client->services[client->num_instances].end_handle = service.end_group_handle;
#ifdef ENABLE_TESTING_SUPPORT
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);
#endif
client->num_instances++;
}if (client->num_instances < MAX_NUM_BATTERY_SERVICES) { ... } else {
log_info("Found more then %d, Battery Service instances. Increase MAX_NUM_BATTERY_SERVICES to store all.", MAX_NUM_BATTERY_SERVICES);
}else { ... }
break;
case GATT_EVENT_SERVICE_QUERY_RESULT:
case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
client = battery_service_get_client_for_con_handle(gatt_event_characteristic_query_result_get_handle(packet));
btstack_assert(client != NULL);
gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic);
btstack_assert(client->service_index < client->num_instances);
btstack_assert(characteristic.uuid16 == ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
client->services[client->service_index].value_handle = characteristic.value_handle;
client->services[client->service_index].properties = characteristic.properties;
#ifdef ENABLE_TESTING_SUPPORT
printf("Battery Level Characteristic:\n Attribute Handle 0x%04X, Properties 0x%02X, Handle 0x%04X, UUID 0x%04X, service %d\n",
characteristic.start_handle,
characteristic.properties,
characteristic.value_handle, characteristic.uuid16,
client->service_index);/* ... */
#endif
break;
case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
case GATT_EVENT_NOTIFICATION:
if (gatt_event_notification_get_value_length(packet) != 1) break;
client = battery_service_get_client_for_con_handle(gatt_event_notification_get_handle(packet));
btstack_assert(client != NULL);
battery_service_emit_battery_level(client,
gatt_event_notification_get_value_handle(packet),
ATT_ERROR_SUCCESS,
gatt_event_notification_get_value(packet)[0]);
break;
case GATT_EVENT_NOTIFICATION:
case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT:
client = battery_service_get_client_for_con_handle(gatt_event_characteristic_value_query_result_get_handle(packet));
btstack_assert(client != NULL);
#ifdef ENABLE_TESTING_SUPPORT
if (client->state == BATTERY_SERVICE_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT){
printf(" Received CCC value: ");
printf_hexdump(gatt_event_characteristic_value_query_result_get_value(packet), gatt_event_characteristic_value_query_result_get_value_length(packet));
break;
}if (client->state == BATTERY_SERVICE_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT) { ... }
/* ... */#endif
if (gatt_event_characteristic_value_query_result_get_value_length(packet) != 1) break;
battery_service_emit_battery_level(client,
gatt_event_characteristic_value_query_result_get_value_handle(packet),
ATT_ERROR_SUCCESS,
gatt_event_characteristic_value_query_result_get_value(packet)[0]);
break;
#ifdef ENABLE_TESTING_SUPPORTcase GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT:
case GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:{
gatt_client_characteristic_descriptor_t characteristic_descriptor;
client = battery_service_get_client_for_con_handle(gatt_event_all_characteristic_descriptors_query_result_get_handle(packet));
btstack_assert(client != NULL);
gatt_event_all_characteristic_descriptors_query_result_get_characteristic_descriptor(packet, &characteristic_descriptor);
if (characteristic_descriptor.uuid16 == ORG_BLUETOOTH_DESCRIPTOR_GATT_CLIENT_CHARACTERISTIC_CONFIGURATION){
client->services[client->service_index].ccc_handle = characteristic_descriptor.handle;
printf(" Battery Level Client Characteristic Configuration Descriptor[%d]: Handle 0x%04X, UUID 0x%04X\n",
client->service_index,
characteristic_descriptor.handle,
characteristic_descriptor.uuid16);
}if (characteristic_descriptor.uuid16 == ORG_BLUETOOTH_DESCRIPTOR_GATT_CLIENT_CHARACTERISTIC_CONFIGURATION) { ... }
break;
...}/* ... */
#endif
case GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
case GATT_EVENT_QUERY_COMPLETE:
client = battery_service_get_client_for_con_handle(gatt_event_query_complete_get_handle(packet));
btstack_assert(client != NULL);
call_run = battery_service_client_handle_query_complete(client, gatt_event_query_complete_get_att_status(packet));
break;
case GATT_EVENT_QUERY_COMPLETE:
default:
break;default
}switch (hci_event_packet_get_type(packet)) { ... }
if (call_run && (client != NULL)){
battery_service_run_for_client(client);
}if (call_run && (client != NULL)) { ... }
}{ ... }
static void handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
UNUSED(packet_type);
UNUSED(channel);
UNUSED(size);
hci_con_handle_t con_handle;
battery_service_client_t * client;
switch (hci_event_packet_get_type(packet)) {
case HCI_EVENT_DISCONNECTION_COMPLETE:
con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
client = battery_service_get_client_for_con_handle(con_handle);
if (client != NULL){
uint16_t cid = client->cid;
battery_service_finalize_client(client);
UNUSED(cid);
}if (client != NULL) { ... }
break;case HCI_EVENT_DISCONNECTION_COMPLETE:
default:
break;default
}switch (hci_event_packet_get_type(packet)) { ... }
}{ ... }
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){
btstack_assert(packet_handler != NULL);
battery_service_client_t * client = battery_service_get_client_for_con_handle(con_handle);
if (client != NULL){
return ERROR_CODE_COMMAND_DISALLOWED;
}if (client != NULL) { ... }
uint16_t cid = battery_service_get_next_cid();
if (battery_service_cid != NULL) {
*battery_service_cid = cid;
}if (battery_service_cid != NULL) { ... }
client = battery_service_create_client(con_handle, cid);
if (client == NULL) {
return BTSTACK_MEMORY_ALLOC_FAILED;
}if (client == NULL) { ... }
client->client_handler = packet_handler;
client->poll_interval_ms = poll_interval_ms;
client->state = BATTERY_SERVICE_CLIENT_STATE_W2_QUERY_SERVICE;
battery_service_run_for_client(client);
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t battery_service_client_disconnect(uint16_t battery_service_cid){
battery_service_client_t * client = battery_service_get_client_for_cid(battery_service_cid);
if (client == NULL){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}if (client == NULL) { ... }
battery_service_finalize_client(client);
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t battery_service_client_read_battery_level(uint16_t battery_service_cid, uint8_t service_index){
battery_service_client_t * client = battery_service_get_client_for_cid(battery_service_cid);
if (client == NULL) {
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}if (client == NULL) { ... }
if (client->state != BATTERY_SERVICE_CLIENT_STATE_CONNECTED) {
return GATT_CLIENT_IN_WRONG_STATE;
}if (client->state != BATTERY_SERVICE_CLIENT_STATE_CONNECTED) { ... }
if (service_index >= client->num_instances) {
return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
}if (service_index >= client->num_instances) { ... }
client->need_poll_bitmap |= (1u << service_index);
battery_service_run_for_client(client);
return ERROR_CODE_SUCCESS;
}{ ... }
void battery_service_client_init(void){
hci_event_callback_registration.callback = &handle_hci_event;
hci_add_event_handler(&hci_event_callback_registration);
}{ ... }
void battery_service_client_deinit(void){
battery_service_cid_counter = 0;
clients = NULL;
}{ ... }