diff --git a/ble/gatt_client.c b/ble/gatt_client.c index 35ba1d76e..12e930c92 100644 --- a/ble/gatt_client.c +++ b/ble/gatt_client.c @@ -652,7 +652,7 @@ static void report_gatt_included_service(gatt_client_t * peripheral, uint8_t *uu } // @returns packet pointer -// @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes +// @note assume that value is part of an l2cap buffer - overwrite HCI + L2CAP packet headers static const int characteristic_value_event_header_size = 8; static uint8_t * setup_characteristic_value_packet(uint8_t type, uint16_t con_handle, uint16_t attribute_handle, uint8_t * value, uint16_t length){ // before the value inside the ATT PDU @@ -669,6 +669,7 @@ static uint8_t * setup_characteristic_value_packet(uint8_t type, uint16_t con_ha // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes static const int long_characteristic_value_event_header_size = 10; static uint8_t * setup_long_characteristic_value_packet(uint8_t type, uint16_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * value, uint16_t length){ +#if defined(HCI_INCOMING_PRE_BUFFER_SIZE) && (HCI_INCOMING_PRE_BUFFER_SIZE >= 10 - 8) // L2CAP Header (4) - ACL Header (4) // before the value inside the ATT PDU uint8_t * packet = value - long_characteristic_value_event_header_size; packet[0] = type; @@ -678,6 +679,10 @@ static uint8_t * setup_long_characteristic_value_packet(uint8_t type, uint16_t c bt_store_16(packet, 6, offset); bt_store_16(packet, 8, length); return packet; +#else + log_error("HCI_INCOMING_PRE_BUFFER_SIZE >= 2 required for long characteristic reads"); + return NULL; +#endif } @@ -702,6 +707,7 @@ static void report_gatt_characteristic_value(gatt_client_t * peripheral, uint16_ // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes static void report_gatt_long_characteristic_value_blob(gatt_client_t * peripheral, uint16_t attribute_handle, uint8_t * blob, uint16_t blob_length, int value_offset){ uint8_t * packet = setup_long_characteristic_value_packet(GATT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT, peripheral->handle, attribute_handle, value_offset, blob, blob_length); + if (!packet) return; emit_event_new(peripheral->subclient_id, packet, blob_length + long_characteristic_value_event_header_size); } @@ -712,6 +718,7 @@ static void report_gatt_characteristic_descriptor(gatt_client_t * peripheral, ui static void report_gatt_long_characteristic_descriptor(gatt_client_t * peripheral, uint16_t descriptor_handle, uint8_t *blob, uint16_t blob_length, uint16_t value_offset){ uint8_t * packet = setup_long_characteristic_value_packet(GATT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT, peripheral->handle, descriptor_handle, value_offset, blob, blob_length); + if (!packet) return; emit_event_new(peripheral->subclient_id, packet, blob_length + long_characteristic_value_event_header_size); } diff --git a/test/btstack-config.h b/test/btstack-config.h index 88454e68f..a69d51cc4 100644 --- a/test/btstack-config.h +++ b/test/btstack-config.h @@ -16,6 +16,7 @@ #define USE_POSIX_RUN_LOOP #define HCI_ACL_PAYLOAD_SIZE 52 +#define HCI_INCOMING_PRE_BUFFER_SIZE 4 #define MAX_SPP_CONNECTIONS 1 diff --git a/test/gatt_client/mock.c b/test/gatt_client/mock.c index e18476e83..bacc5bbe9 100644 --- a/test/gatt_client/mock.c +++ b/test/gatt_client/mock.c @@ -16,7 +16,7 @@ static void (*registered_l2cap_packet_handler) (void * connection, uint8_t packe static linked_list_t connections; static const uint16_t max_mtu = 23; -static uint8_t l2cap_stack_buffer[max_mtu]; +static uint8_t l2cap_stack_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 8 + max_mtu]; // pre buffer + HCI Header + L2CAP header uint16_t gatt_client_handle = 0x40; uint16_t get_gatt_client_handle(void){