From cf7827865f90b6a08f8ab3e160c20e1887a36a4f Mon Sep 17 00:00:00 2001 From: "ole.reinhardt@googlemail.com" Date: Wed, 12 Nov 2014 00:12:23 +0000 Subject: [PATCH] Defined HCI_INCOMING_PRE_BUFFER_SIZE (14 Bytes = 2 * sizeof(bd_addr_t) + sizeof(uint16_t)) This avoids extra copying of the ethernet frame into a static buffer bnep.c implements two solutions with and without HCI_INCOMING_PRE_BUFFER_SIZE defined --- example/libusb/btstack-config.h | 2 +- src/bnep.c | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/example/libusb/btstack-config.h b/example/libusb/btstack-config.h index 2667a9186..0448f2826 100644 --- a/example/libusb/btstack-config.h +++ b/example/libusb/btstack-config.h @@ -16,7 +16,7 @@ #define SDP_DES_DUMP #define ENABLE_LOG_INFO #define ENABLE_LOG_ERROR -//#define HCI_ACL_PAYLOAD_SIZE 1021 +#define HCI_INCOMING_PRE_BUFFER_SIZE 14 // sizeof benep heade, avoid memcpy #define HCI_ACL_PAYLOAD_SIZE (1691 + 4) #define HAVE_HCI_DUMP #define SDP_DES_DUMP diff --git a/src/bnep.c b/src/bnep.c index ed20d7b32..d704ef7b0 100644 --- a/src/bnep.c +++ b/src/bnep.c @@ -794,8 +794,25 @@ static int bnep_handle_multi_addr_response(bnep_channel_t *channel, uint8_t *pac static int bnep_handle_ethernet_packet(bnep_channel_t *channel, bd_addr_t addr_dest, bd_addr_t addr_source, uint16_t network_protocol_type, uint8_t *payload, uint16_t size) { - uint8_t ethernet_packet[BNEP_MTU_MIN]; uint16_t pos = 0; + +#if (HCI_INCOMING_PRE_BUFFER_SIZE) && (HCI_INCOMING_PRE_BUFFER_SIZE >= 14 - 8) // 2 * sizeof(bd_addr_t) + sizeof(uint16_t) - L2CAP Header (4) - ACL Header (4) + /* In-place modify the package and add the ethernet header in front of the payload. + * WARNING: This modifies the data in front of the payload and may overwrite 14 bytes there! + */ + uint8_t *ethernet_packet = payload - 2 * sizeof(bd_addr_t) - sizeof(uint16_t); + /* Restore the ethernet packet header */ + BD_ADDR_COPY(ethernet_packet + pos, addr_dest); + pos += sizeof(bd_addr_t); + BD_ADDR_COPY(ethernet_packet + pos, addr_source); + pos += sizeof(bd_addr_t); + bt_store_16(ethernet_packet, pos, network_protocol_type); + /* Payload is just in place... */ +#else + /* Copy ethernet frame to statically allocated buffer. This solution is more + * save, but needs an extra copy and more stack! + */ + uint8_t ethernet_packet[BNEP_MTU_MIN]; /* Restore the ethernet packet header */ BD_ADDR_COPY(ethernet_packet + pos, addr_dest); @@ -805,11 +822,12 @@ static int bnep_handle_ethernet_packet(bnep_channel_t *channel, bd_addr_t addr_d bt_store_16(ethernet_packet, pos, network_protocol_type); pos += 2; memcpy(ethernet_packet + pos, payload, size); - +#endif + /* Notify application layer and deliver the ethernet packet */ (*app_packet_handler)(channel->connection, BNEP_DATA_PACKET, channel->uuid_source, ethernet_packet, size + sizeof(uint16_t) + 2 * sizeof(bd_addr_t)); - + return size; }