mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-03-04 01:13:40 +00:00
gatt_client: extract gatt_client_handle_att_response
This commit is contained in:
parent
0fde3c5edb
commit
cf8e571806
@ -1380,47 +1380,10 @@ static void gatt_client_event_packet_handler(uint8_t packet_type, uint16_t chann
|
||||
gatt_client_run();
|
||||
}
|
||||
|
||||
static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
|
||||
gatt_client_t * gatt_client;
|
||||
if (size < 1u) return;
|
||||
|
||||
if (packet_type == HCI_EVENT_PACKET) {
|
||||
switch (packet[0]){
|
||||
case L2CAP_EVENT_CAN_SEND_NOW:
|
||||
gatt_client_run();
|
||||
break;
|
||||
// att_server has negotiated the mtu for this connection, cache if context exists
|
||||
case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
|
||||
if (size < 6u) break;
|
||||
gatt_client = gatt_client_get_context_for_handle(handle);
|
||||
if (gatt_client == NULL) break;
|
||||
gatt_client->mtu = little_endian_read_16(packet, 4);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (packet_type != ATT_DATA_PACKET) return;
|
||||
|
||||
// special cases: notifications & indications motivate creating context
|
||||
switch (packet[0]){
|
||||
case ATT_HANDLE_VALUE_NOTIFICATION:
|
||||
case ATT_HANDLE_VALUE_INDICATION:
|
||||
gatt_client_provide_context_for_handle(handle, &gatt_client);
|
||||
break;
|
||||
default:
|
||||
gatt_client = gatt_client_get_context_for_handle(handle);
|
||||
break;
|
||||
}
|
||||
|
||||
if (gatt_client == NULL) return;
|
||||
|
||||
static void gatt_client_handle_att_response(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size) {
|
||||
uint8_t error_code;
|
||||
switch (packet[0]) {
|
||||
case ATT_EXCHANGE_MTU_RESPONSE:
|
||||
{
|
||||
case ATT_EXCHANGE_MTU_RESPONSE: {
|
||||
if (size < 3u) break;
|
||||
uint16_t remote_rx_mtu = little_endian_read_16(packet, 1);
|
||||
uint16_t local_rx_mtu = l2cap_max_le_mtu();
|
||||
@ -1430,8 +1393,8 @@ static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle,
|
||||
gatt_client->mtu = mtu;
|
||||
gatt_client->mtu_state = MTU_EXCHANGED;
|
||||
|
||||
// set per connection mtu state
|
||||
hci_connection_t * hci_connection = hci_connection_for_handle(handle);
|
||||
// set per connection mtu state - for fixed channel
|
||||
hci_connection_t *hci_connection = hci_connection_for_handle(gatt_client->con_handle);
|
||||
hci_connection->att_connection.mtu = gatt_client->mtu;
|
||||
hci_connection->att_connection.mtu_exchanged = true;
|
||||
|
||||
@ -1463,16 +1426,17 @@ static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle,
|
||||
switch (gatt_client->gatt_client_state) {
|
||||
case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT:
|
||||
report_gatt_characteristics(gatt_client, packet, size);
|
||||
trigger_next_characteristic_query(gatt_client, get_last_result_handle_from_characteristics_list(packet, size));
|
||||
trigger_next_characteristic_query(gatt_client,
|
||||
get_last_result_handle_from_characteristics_list(packet, size));
|
||||
// GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR
|
||||
break;
|
||||
case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT:
|
||||
report_gatt_characteristics(gatt_client, packet, size);
|
||||
trigger_next_characteristic_query(gatt_client, get_last_result_handle_from_characteristics_list(packet, size));
|
||||
trigger_next_characteristic_query(gatt_client,
|
||||
get_last_result_handle_from_characteristics_list(packet, size));
|
||||
// GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR
|
||||
break;
|
||||
case P_W4_INCLUDED_SERVICE_QUERY_RESULT:
|
||||
{
|
||||
case P_W4_INCLUDED_SERVICE_QUERY_RESULT: {
|
||||
if (size < 2u) break;
|
||||
uint16_t uuid16 = 0;
|
||||
uint16_t pair_size = packet[1];
|
||||
@ -1499,7 +1463,9 @@ static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle,
|
||||
report_gatt_included_service_uuid16(gatt_client, include_handle, uuid16);
|
||||
}
|
||||
|
||||
trigger_next_included_service_query(gatt_client, get_last_result_handle_from_included_services_list(packet, size));
|
||||
trigger_next_included_service_query(gatt_client,
|
||||
get_last_result_handle_from_included_services_list(packet,
|
||||
size));
|
||||
// GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
|
||||
break;
|
||||
}
|
||||
@ -1517,7 +1483,8 @@ static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle,
|
||||
uint16_t offset;
|
||||
for (offset = 2; offset < size; offset += pair_size) {
|
||||
uint16_t value_handle = little_endian_read_16(packet, offset);
|
||||
report_gatt_characteristic_value(gatt_client, value_handle, &packet[offset + 2u], pair_size - 2u);
|
||||
report_gatt_characteristic_value(gatt_client, value_handle, &packet[offset + 2u],
|
||||
pair_size - 2u);
|
||||
last_result_handle = value_handle;
|
||||
}
|
||||
}
|
||||
@ -1548,21 +1515,25 @@ static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle,
|
||||
|
||||
case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:
|
||||
gatt_client_handle_transaction_complete(gatt_client);
|
||||
report_gatt_characteristic_descriptor(gatt_client, gatt_client->attribute_handle, &packet[1], size - 1u, 0u);
|
||||
report_gatt_characteristic_descriptor(gatt_client, gatt_client->attribute_handle, &packet[1],
|
||||
size - 1u, 0u);
|
||||
emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
|
||||
break;
|
||||
|
||||
// Use ATT_READ_REQUEST for first blob of Read Long Characteristic
|
||||
case P_W4_READ_BLOB_RESULT:
|
||||
report_gatt_long_characteristic_value_blob(gatt_client, gatt_client->attribute_handle, &packet[1], size - 1u, gatt_client->attribute_offset);
|
||||
report_gatt_long_characteristic_value_blob(gatt_client, gatt_client->attribute_handle, &packet[1],
|
||||
size - 1u, gatt_client->attribute_offset);
|
||||
trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_QUERY, size - 1u);
|
||||
// GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
|
||||
break;
|
||||
|
||||
// Use ATT_READ_REQUEST for first blob of Read Long Characteristic Descriptor
|
||||
case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
|
||||
report_gatt_long_characteristic_descriptor(gatt_client, gatt_client->attribute_handle, &packet[1], size-1u, gatt_client->attribute_offset);
|
||||
trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY, size-1u);
|
||||
report_gatt_long_characteristic_descriptor(gatt_client, gatt_client->attribute_handle, &packet[1],
|
||||
size - 1u, gatt_client->attribute_offset);
|
||||
trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY,
|
||||
size - 1u);
|
||||
// GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
|
||||
break;
|
||||
|
||||
@ -1571,8 +1542,7 @@ static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle,
|
||||
}
|
||||
break;
|
||||
|
||||
case ATT_FIND_BY_TYPE_VALUE_RESPONSE:
|
||||
{
|
||||
case ATT_FIND_BY_TYPE_VALUE_RESPONSE: {
|
||||
uint8_t pair_size = 4;
|
||||
int i;
|
||||
uint16_t start_group_handle;
|
||||
@ -1580,14 +1550,14 @@ static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle,
|
||||
for (i = 1u; (i + pair_size) <= size; i += pair_size) {
|
||||
start_group_handle = little_endian_read_16(packet, i);
|
||||
end_group_handle = little_endian_read_16(packet, i + 2);
|
||||
emit_gatt_service_query_result_event(gatt_client, start_group_handle, end_group_handle, gatt_client->uuid128);
|
||||
emit_gatt_service_query_result_event(gatt_client, start_group_handle, end_group_handle,
|
||||
gatt_client->uuid128);
|
||||
}
|
||||
trigger_next_service_by_uuid_query(gatt_client, end_group_handle);
|
||||
// GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
|
||||
break;
|
||||
}
|
||||
case ATT_FIND_INFORMATION_REPLY:
|
||||
{
|
||||
case ATT_FIND_INFORMATION_REPLY: {
|
||||
if (size < 2u) break;
|
||||
|
||||
uint8_t pair_size = 4;
|
||||
@ -1654,7 +1624,8 @@ static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle,
|
||||
uint16_t received_blob_length = size - 1u;
|
||||
switch (gatt_client->gatt_client_state) {
|
||||
case P_W4_READ_BLOB_RESULT:
|
||||
report_gatt_long_characteristic_value_blob(gatt_client, gatt_client->attribute_handle, &packet[1], received_blob_length, gatt_client->attribute_offset);
|
||||
report_gatt_long_characteristic_value_blob(gatt_client, gatt_client->attribute_handle, &packet[1],
|
||||
received_blob_length, gatt_client->attribute_offset);
|
||||
trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_QUERY, received_blob_length);
|
||||
// GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
|
||||
break;
|
||||
@ -1662,7 +1633,8 @@ static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle,
|
||||
report_gatt_long_characteristic_descriptor(gatt_client, gatt_client->attribute_handle,
|
||||
&packet[1], received_blob_length,
|
||||
gatt_client->attribute_offset);
|
||||
trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY, received_blob_length);
|
||||
trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY,
|
||||
received_blob_length);
|
||||
// GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
|
||||
break;
|
||||
default:
|
||||
@ -1689,14 +1661,16 @@ static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle,
|
||||
}
|
||||
case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: {
|
||||
gatt_client->attribute_offset = little_endian_read_16(packet, 3);
|
||||
trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR, P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR);
|
||||
trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR,
|
||||
P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR);
|
||||
// GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
|
||||
break;
|
||||
}
|
||||
case P_W4_PREPARE_RELIABLE_WRITE_RESULT: {
|
||||
if (is_value_valid(gatt_client, packet, size)) {
|
||||
gatt_client->attribute_offset = little_endian_read_16(packet, 3);
|
||||
trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_RELIABLE_WRITE, P_W2_EXECUTE_PREPARED_WRITE);
|
||||
trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_RELIABLE_WRITE,
|
||||
P_W2_EXECUTE_PREPARED_WRITE);
|
||||
// GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
|
||||
break;
|
||||
}
|
||||
@ -1884,7 +1858,47 @@ static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle,
|
||||
log_info("ATT Handler, unhandled response type 0x%02x", packet[0]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size) {
|
||||
gatt_client_t *gatt_client;
|
||||
if (size < 1u) return;
|
||||
|
||||
if (packet_type == HCI_EVENT_PACKET) {
|
||||
switch (packet[0]) {
|
||||
case L2CAP_EVENT_CAN_SEND_NOW:
|
||||
gatt_client_run();
|
||||
break;
|
||||
// att_server has negotiated the mtu for this connection, cache if context exists
|
||||
case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
|
||||
if (size < 6u) break;
|
||||
gatt_client = gatt_client_get_context_for_handle(handle);
|
||||
if (gatt_client == NULL) break;
|
||||
gatt_client->mtu = little_endian_read_16(packet, 4);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (packet_type != ATT_DATA_PACKET) return;
|
||||
|
||||
// special cases: notifications & indications motivate creating context
|
||||
switch (packet[0]) {
|
||||
case ATT_HANDLE_VALUE_NOTIFICATION:
|
||||
case ATT_HANDLE_VALUE_INDICATION:
|
||||
gatt_client_provide_context_for_handle(handle, &gatt_client);
|
||||
break;
|
||||
default:
|
||||
gatt_client = gatt_client_get_context_for_handle(handle);
|
||||
break;
|
||||
}
|
||||
|
||||
if (gatt_client != NULL) {
|
||||
gatt_client_handle_att_response(gatt_client, packet, size);
|
||||
gatt_client_run();
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef ENABLE_LE_SIGNED_WRITE
|
||||
|
Loading…
x
Reference in New Issue
Block a user