mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-04-17 02:42:33 +00:00
gatt_client: extract functions to deserialize gatt client structs and provide in gatt_client
This commit is contained in:
parent
d25c33e5a2
commit
6ba2ad2202
@ -140,35 +140,13 @@ static void error_code(int status){
|
||||
}
|
||||
}
|
||||
|
||||
static void extract_service(gatt_client_service_t * service, uint8_t * packet){
|
||||
service->start_group_handle = little_endian_read_16(packet, 4);
|
||||
service->end_group_handle = little_endian_read_16(packet, 6);
|
||||
service->uuid16 = 0;
|
||||
reverse_128(&packet[8], service->uuid128);
|
||||
if (uuid_has_bluetooth_prefix(service->uuid128)){
|
||||
service->uuid16 = big_endian_read_32(service->uuid128, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void extract_characteristic(gatt_client_characteristic_t * characteristic, uint8_t * packet){
|
||||
characteristic->start_handle = little_endian_read_16(packet, 4);
|
||||
characteristic->value_handle = little_endian_read_16(packet, 6);
|
||||
characteristic->end_handle = little_endian_read_16(packet, 8);
|
||||
characteristic->properties = little_endian_read_16(packet, 10);
|
||||
characteristic->uuid16 = 0;
|
||||
reverse_128(&packet[12], characteristic->uuid128);
|
||||
if (uuid_has_bluetooth_prefix(characteristic->uuid128)){
|
||||
characteristic->uuid16 = big_endian_read_32(characteristic->uuid128, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
||||
|
||||
switch(state){
|
||||
case TC_W4_SERVICE_RESULT:
|
||||
switch(packet[0]){
|
||||
case GATT_EVENT_SERVICE_QUERY_RESULT:
|
||||
extract_service(&battery_service, packet);
|
||||
gatt_client_deserialize_service(packet, 4, &battery_service);
|
||||
printf("Battery service found:\n");
|
||||
dump_service(&battery_service);
|
||||
break;
|
||||
@ -192,7 +170,7 @@ static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint
|
||||
switch(packet[0]){
|
||||
case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
|
||||
printf("Battery level characteristic found:\n");
|
||||
extract_characteristic(&config_characteristic, packet);
|
||||
gatt_client_deserialize_characteristic(packet, 4, &config_characteristic);
|
||||
dump_characteristic(&config_characteristic);
|
||||
break;
|
||||
case GATT_EVENT_QUERY_COMPLETE:
|
||||
|
@ -237,39 +237,17 @@ static void handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *pac
|
||||
/* LISTING_START(GATTBrowserQueryHandler): Handling of the GATT client queries */
|
||||
static int search_services = 1;
|
||||
|
||||
static void extract_service(gatt_client_service_t * service, uint8_t * packet){
|
||||
service->start_group_handle = little_endian_read_16(packet, 4);
|
||||
service->end_group_handle = little_endian_read_16(packet, 6);
|
||||
service->uuid16 = 0;
|
||||
reverse_128(&packet[8], service->uuid128);
|
||||
if (uuid_has_bluetooth_prefix(service->uuid128)){
|
||||
service->uuid16 = big_endian_read_32(service->uuid128, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void extract_characteristic(gatt_client_characteristic_t * characteristic, uint8_t * packet){
|
||||
characteristic->start_handle = little_endian_read_16(packet, 4);
|
||||
characteristic->value_handle = little_endian_read_16(packet, 6);
|
||||
characteristic->end_handle = little_endian_read_16(packet, 8);
|
||||
characteristic->properties = little_endian_read_16(packet, 10);
|
||||
characteristic->uuid16 = 0;
|
||||
reverse_128(&packet[12], characteristic->uuid128);
|
||||
if (uuid_has_bluetooth_prefix(characteristic->uuid128)){
|
||||
characteristic->uuid16 = big_endian_read_32(characteristic->uuid128, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
||||
gatt_client_service_t service;
|
||||
gatt_client_characteristic_t characteristic;
|
||||
switch(packet[0]){
|
||||
case GATT_EVENT_SERVICE_QUERY_RESULT:\
|
||||
extract_service(&service, packet);
|
||||
gatt_client_deserialize_service(packet, 4, &service);
|
||||
dump_service(&service);
|
||||
services[service_count++] = service;
|
||||
break;
|
||||
case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
|
||||
extract_characteristic(&characteristic, packet);
|
||||
gatt_client_deserialize_characteristic(packet, 4, &characteristic);
|
||||
dump_characteristic(&characteristic);
|
||||
break;
|
||||
case GATT_EVENT_QUERY_COMPLETE:
|
||||
|
@ -853,27 +853,12 @@ btstack_linked_list_gatt_client_helper_t * daemon_setup_gatt_client_request(conn
|
||||
|
||||
// (de)serialize structs from/to HCI commands/events
|
||||
|
||||
void daemon_gatt_deserialize_service(uint8_t *packet, int offset, gatt_client_service_t *service){
|
||||
service->start_group_handle = little_endian_read_16(packet, offset);
|
||||
service->end_group_handle = little_endian_read_16(packet, offset + 2);
|
||||
reverse_128(&packet[offset + 4], service->uuid128);
|
||||
}
|
||||
|
||||
void daemon_gatt_serialize_service(gatt_client_service_t * service, uint8_t * event, int offset){
|
||||
little_endian_store_16(event, offset, service->start_group_handle);
|
||||
little_endian_store_16(event, offset+2, service->end_group_handle);
|
||||
reverse_128(service->uuid128, &event[offset + 4]);
|
||||
}
|
||||
|
||||
void daemon_gatt_deserialize_characteristic(uint8_t * packet, int offset, gatt_client_characteristic_t * characteristic){
|
||||
characteristic->start_handle = little_endian_read_16(packet, offset);
|
||||
characteristic->value_handle = little_endian_read_16(packet, offset + 2);
|
||||
characteristic->end_handle = little_endian_read_16(packet, offset + 4);
|
||||
characteristic->properties = little_endian_read_16(packet, offset + 6);
|
||||
characteristic->uuid16 = little_endian_read_16(packet, offset + 8);
|
||||
reverse_128(&packet[offset+10], characteristic->uuid128);
|
||||
}
|
||||
|
||||
void daemon_gatt_serialize_characteristic(gatt_client_characteristic_t * characteristic, uint8_t * event, int offset){
|
||||
little_endian_store_16(event, offset, characteristic->start_handle);
|
||||
little_endian_store_16(event, offset+2, characteristic->value_handle);
|
||||
@ -882,11 +867,6 @@ void daemon_gatt_serialize_characteristic(gatt_client_characteristic_t * charact
|
||||
reverse_128(characteristic->uuid128, &event[offset+8]);
|
||||
}
|
||||
|
||||
void daemon_gatt_deserialize_characteristic_descriptor(uint8_t * packet, int offset, gatt_client_characteristic_descriptor_t * descriptor){
|
||||
descriptor->handle = little_endian_read_16(packet, offset);
|
||||
reverse_128(&packet[offset+2], descriptor->uuid128);
|
||||
}
|
||||
|
||||
void daemon_gatt_serialize_characteristic_descriptor(gatt_client_characteristic_descriptor_t * characteristic_descriptor, uint8_t * event, int offset){
|
||||
little_endian_store_16(event, offset, characteristic_descriptor->handle);
|
||||
reverse_128(characteristic_descriptor->uuid128, &event[offset+2]);
|
||||
@ -1190,47 +1170,47 @@ static int btstack_command_handler(connection_t *connection, uint8_t *packet, ui
|
||||
case GATT_FIND_INCLUDED_SERVICES_FOR_SERVICE:
|
||||
gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
|
||||
if (!gatt_helper) break;
|
||||
daemon_gatt_deserialize_service(packet, 5, &service);
|
||||
gatt_client_deserialize_service(packet, 5, &service);
|
||||
gatt_client_find_included_services_for_service(&handle_gatt_client_event, gatt_helper->con_handle, &service);
|
||||
break;
|
||||
|
||||
case GATT_DISCOVER_CHARACTERISTICS_FOR_SERVICE:
|
||||
gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
|
||||
if (!gatt_helper) break;
|
||||
daemon_gatt_deserialize_service(packet, 5, &service);
|
||||
gatt_client_deserialize_service(packet, 5, &service);
|
||||
gatt_client_discover_characteristics_for_service(&handle_gatt_client_event, gatt_helper->con_handle, &service);
|
||||
break;
|
||||
case GATT_DISCOVER_CHARACTERISTICS_FOR_SERVICE_BY_UUID128:
|
||||
gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
|
||||
if (!gatt_helper) break;
|
||||
daemon_gatt_deserialize_service(packet, 5, &service);
|
||||
gatt_client_deserialize_service(packet, 5, &service);
|
||||
reverse_128(&packet[5 + SERVICE_LENGTH], uuid128);
|
||||
gatt_client_discover_characteristics_for_service_by_uuid128(&handle_gatt_client_event, gatt_helper->con_handle, &service, uuid128);
|
||||
break;
|
||||
case GATT_DISCOVER_CHARACTERISTIC_DESCRIPTORS:
|
||||
gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
|
||||
if (!gatt_helper) break;
|
||||
daemon_gatt_deserialize_characteristic(packet, 5, &characteristic);
|
||||
gatt_client_deserialize_characteristic(packet, 5, &characteristic);
|
||||
gatt_client_discover_characteristic_descriptors(&handle_gatt_client_event, gatt_helper->con_handle, &characteristic);
|
||||
break;
|
||||
|
||||
case GATT_READ_VALUE_OF_CHARACTERISTIC:
|
||||
gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
|
||||
if (!gatt_helper) break;
|
||||
daemon_gatt_deserialize_characteristic(packet, 5, &characteristic);
|
||||
gatt_client_deserialize_characteristic(packet, 5, &characteristic);
|
||||
gatt_client_read_value_of_characteristic(&handle_gatt_client_event, gatt_helper->con_handle, &characteristic);
|
||||
break;
|
||||
case GATT_READ_LONG_VALUE_OF_CHARACTERISTIC:
|
||||
gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
|
||||
if (!gatt_helper) break;
|
||||
daemon_gatt_deserialize_characteristic(packet, 5, &characteristic);
|
||||
gatt_client_deserialize_characteristic(packet, 5, &characteristic);
|
||||
gatt_client_read_long_value_of_characteristic(&handle_gatt_client_event, gatt_helper->con_handle, &characteristic);
|
||||
break;
|
||||
|
||||
case GATT_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE:
|
||||
gatt_helper = daemon_setup_gatt_client_request(connection, packet, 0); // note: don't track active connection
|
||||
if (!gatt_helper) break;
|
||||
daemon_gatt_deserialize_characteristic(packet, 5, &characteristic);
|
||||
gatt_client_deserialize_characteristic(packet, 5, &characteristic);
|
||||
data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_LENGTH);
|
||||
data = gatt_helper->characteristic_buffer;
|
||||
memcpy(data, &packet[7 + CHARACTERISTIC_LENGTH], data_length);
|
||||
@ -1239,7 +1219,7 @@ static int btstack_command_handler(connection_t *connection, uint8_t *packet, ui
|
||||
case GATT_WRITE_VALUE_OF_CHARACTERISTIC:
|
||||
gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
|
||||
if (!gatt_helper) break;
|
||||
daemon_gatt_deserialize_characteristic(packet, 5, &characteristic);
|
||||
gatt_client_deserialize_characteristic(packet, 5, &characteristic);
|
||||
data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_LENGTH);
|
||||
data = gatt_helper->characteristic_buffer;
|
||||
memcpy(data, &packet[7 + CHARACTERISTIC_LENGTH], data_length);
|
||||
@ -1248,7 +1228,7 @@ static int btstack_command_handler(connection_t *connection, uint8_t *packet, ui
|
||||
case GATT_WRITE_LONG_VALUE_OF_CHARACTERISTIC:
|
||||
gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
|
||||
if (!gatt_helper) break;
|
||||
daemon_gatt_deserialize_characteristic(packet, 5, &characteristic);
|
||||
gatt_client_deserialize_characteristic(packet, 5, &characteristic);
|
||||
data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_LENGTH);
|
||||
data = gatt_helper->characteristic_buffer;
|
||||
memcpy(data, &packet[7 + CHARACTERISTIC_LENGTH], data_length);
|
||||
@ -1257,7 +1237,7 @@ static int btstack_command_handler(connection_t *connection, uint8_t *packet, ui
|
||||
case GATT_RELIABLE_WRITE_LONG_VALUE_OF_CHARACTERISTIC:
|
||||
gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
|
||||
if (!gatt_helper) break;
|
||||
daemon_gatt_deserialize_characteristic(packet, 5, &characteristic);
|
||||
gatt_client_deserialize_characteristic(packet, 5, &characteristic);
|
||||
data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_LENGTH);
|
||||
data = gatt_helper->characteristic_buffer;
|
||||
memcpy(data, &packet[7 + CHARACTERISTIC_LENGTH], data_length);
|
||||
@ -1267,20 +1247,20 @@ static int btstack_command_handler(connection_t *connection, uint8_t *packet, ui
|
||||
gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
|
||||
if (!gatt_helper) break;
|
||||
handle = little_endian_read_16(packet, 3);
|
||||
daemon_gatt_deserialize_characteristic_descriptor(packet, 5, &descriptor);
|
||||
gatt_client_deserialize_characteristic_descriptor(packet, 5, &descriptor);
|
||||
gatt_client_read_characteristic_descriptor(&handle_gatt_client_event, gatt_helper->con_handle, &descriptor);
|
||||
break;
|
||||
case GATT_READ_LONG_CHARACTERISTIC_DESCRIPTOR:
|
||||
gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
|
||||
if (!gatt_helper) break;
|
||||
daemon_gatt_deserialize_characteristic_descriptor(packet, 5, &descriptor);
|
||||
gatt_client_deserialize_characteristic_descriptor(packet, 5, &descriptor);
|
||||
gatt_client_read_long_characteristic_descriptor(&handle_gatt_client_event, gatt_helper->con_handle, &descriptor);
|
||||
break;
|
||||
|
||||
case GATT_WRITE_CHARACTERISTIC_DESCRIPTOR:
|
||||
gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
|
||||
if (!gatt_helper) break;
|
||||
daemon_gatt_deserialize_characteristic_descriptor(packet, 5, &descriptor);
|
||||
gatt_client_deserialize_characteristic_descriptor(packet, 5, &descriptor);
|
||||
data = gatt_helper->characteristic_buffer;
|
||||
data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_DESCRIPTOR_LENGTH);
|
||||
gatt_client_write_characteristic_descriptor(&handle_gatt_client_event, gatt_helper->con_handle, &descriptor, data_length, data);
|
||||
@ -1288,7 +1268,7 @@ static int btstack_command_handler(connection_t *connection, uint8_t *packet, ui
|
||||
case GATT_WRITE_LONG_CHARACTERISTIC_DESCRIPTOR:
|
||||
gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
|
||||
if (!gatt_helper) break;
|
||||
daemon_gatt_deserialize_characteristic_descriptor(packet, 5, &descriptor);
|
||||
gatt_client_deserialize_characteristic_descriptor(packet, 5, &descriptor);
|
||||
data = gatt_helper->characteristic_buffer;
|
||||
data_length = little_endian_read_16(packet, 5 + CHARACTERISTIC_DESCRIPTOR_LENGTH);
|
||||
gatt_client_write_long_characteristic_descriptor(&handle_gatt_client_event, gatt_helper->con_handle, &descriptor, data_length, data);
|
||||
@ -1298,7 +1278,7 @@ static int btstack_command_handler(connection_t *connection, uint8_t *packet, ui
|
||||
gatt_helper = daemon_setup_gatt_client_request(connection, packet, 1);
|
||||
if (!gatt_helper) break;
|
||||
data = gatt_helper->characteristic_buffer;
|
||||
daemon_gatt_deserialize_characteristic(packet, 5, &characteristic);
|
||||
gatt_client_deserialize_characteristic(packet, 5, &characteristic);
|
||||
gatt_client_write_client_characteristic_configuration(&handle_gatt_client_event, gatt_helper->con_handle, &characteristic, configuration);
|
||||
break;
|
||||
case GATT_GET_MTU:
|
||||
|
@ -1833,3 +1833,28 @@ void gatt_client_pts_suppress_mtu_exchange(void){
|
||||
pts_suppress_mtu_exchange = 1;
|
||||
}
|
||||
|
||||
void gatt_client_deserialize_service(uint8_t *packet, int offset, gatt_client_service_t *service){
|
||||
service->start_group_handle = little_endian_read_16(packet, offset);
|
||||
service->end_group_handle = little_endian_read_16(packet, offset + 2);
|
||||
reverse_128(&packet[offset + 4], service->uuid128);
|
||||
if (uuid_has_bluetooth_prefix(service->uuid128)){
|
||||
service->uuid16 = big_endian_read_32(service->uuid128, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void gatt_client_deserialize_characteristic(uint8_t * packet, int offset, gatt_client_characteristic_t * characteristic){
|
||||
characteristic->start_handle = little_endian_read_16(packet, offset);
|
||||
characteristic->value_handle = little_endian_read_16(packet, offset + 2);
|
||||
characteristic->end_handle = little_endian_read_16(packet, offset + 4);
|
||||
characteristic->properties = little_endian_read_16(packet, offset + 6);
|
||||
characteristic->uuid16 = little_endian_read_16(packet, offset + 8);
|
||||
reverse_128(&packet[offset+10], characteristic->uuid128);
|
||||
if (uuid_has_bluetooth_prefix(characteristic->uuid128)){
|
||||
characteristic->uuid16 = big_endian_read_32(characteristic->uuid128, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void gatt_client_deserialize_characteristic_descriptor(uint8_t * packet, int offset, gatt_client_characteristic_descriptor_t * descriptor){
|
||||
descriptor->handle = little_endian_read_16(packet, offset);
|
||||
reverse_128(&packet[offset+2], descriptor->uuid128);
|
||||
}
|
||||
|
@ -355,9 +355,14 @@ uint8_t gatt_client_execute_write(btstack_packet_handler_t callback, uint16_t co
|
||||
*/
|
||||
uint8_t gatt_client_cancel_write(btstack_packet_handler_t callback, uint16_t con_handle);
|
||||
|
||||
|
||||
/* API_END */
|
||||
|
||||
// used by generated btstack_event.c
|
||||
|
||||
void gatt_client_deserialize_service(uint8_t *packet, int offset, gatt_client_service_t *service);
|
||||
void gatt_client_deserialize_characteristic(uint8_t * packet, int offset, gatt_client_characteristic_t * characteristic);
|
||||
void gatt_client_deserialize_characteristic_descriptor(uint8_t * packet, int offset, gatt_client_characteristic_descriptor_t * descriptor);
|
||||
|
||||
// only used for testing
|
||||
void gatt_client_pts_suppress_mtu_exchange(void);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user