From 9056d20c9364cb4829aabeb517b98440aa0d5240 Mon Sep 17 00:00:00 2001 From: Ariel Gertzenstein Date: Fri, 4 Apr 2014 23:57:10 -0700 Subject: [PATCH] DO NOT MERGE Rewrite PAN_Write in terms of PAN_WriteBuf. The original code looked copy/pasted. The new implementation still results in the same number of buffer copies but is much easier to read and reduces redundancy. Change-Id: I3ace395d2b124074e9687d13ae9a1b0b93a25a31 --- stack/pan/pan_api.c | 117 ++++++++++++++-------------------------------------- 1 file changed, 32 insertions(+), 85 deletions(-) diff --git a/stack/pan/pan_api.c b/stack/pan/pan_api.c index e6275ea02..d08e527d0 100644 --- a/stack/pan/pan_api.c +++ b/stack/pan/pan_api.c @@ -492,9 +492,7 @@ tPAN_RESULT PAN_Disconnect (UINT16 handle) ** Description This sends data over the PAN connections. If this is called ** on GN or NAP side and the packet is multicast or broadcast ** it will be sent on all the links. Otherwise the correct link -** is found based on the destination address and forwarded on it -** If the return value is not PAN_SUCCESS the application should -** take care of releasing the message buffer +** is found based on the destination address and forwarded on it. ** ** Parameters: handle - handle for the connection ** dst - MAC or BD Addr of the destination device @@ -509,89 +507,39 @@ tPAN_RESULT PAN_Disconnect (UINT16 handle) ** there is an error in sending data ** *******************************************************************************/ -tPAN_RESULT PAN_Write (UINT16 handle, BD_ADDR dst, BD_ADDR src, UINT16 protocol, UINT8 *p_data, UINT16 len, BOOLEAN ext) +tPAN_RESULT PAN_Write(UINT16 handle, BD_ADDR dst, BD_ADDR src, UINT16 protocol, UINT8 *p_data, UINT16 len, BOOLEAN ext) { - tPAN_CONN *pcb; - UINT16 i; - tBNEP_RESULT result; + BT_HDR *buffer; - if (pan_cb.role == PAN_ROLE_INACTIVE || (!(pan_cb.num_conns))) - { - PAN_TRACE_ERROR0 ("PAN is not active Data write failed"); + if (pan_cb.role == PAN_ROLE_INACTIVE || !pan_cb.num_conns) { + PAN_TRACE_ERROR1("%s PAN is not active, data write failed.", __func__); return PAN_FAILURE; } - /* Check if it is broadcast or multicast packet */ - if (dst[0] & 0x01) - { - for (i=0; icon_state != PAN_STATE_CONNECTED) - { - PAN_TRACE_ERROR0 ("PAN Data write when conn is not active"); - return PAN_FAILURE; + buffer = (BT_HDR *)GKI_getpoolbuf(PAN_POOL_ID); + if (!buffer) { + PAN_TRACE_ERROR1("%s unable to acquire buffer from pool.", __func__); + return PAN_NO_RESOURCES; } - result = BNEP_Write (pcb->handle, dst, p_data, len, protocol, src, ext); - if (result == BNEP_IGNORE_CMD) - { - PAN_TRACE_DEBUG0 ("PAN ignored data write to PANU"); - return result; - } - else if (result != BNEP_SUCCESS) - { - PAN_TRACE_ERROR0 ("PAN failed to send data to the PANU"); - return result; - } + buffer->len = len; + buffer->offset = PAN_MINIMUM_OFFSET; + memcpy((UINT8 *)buffer + sizeof(BT_HDR) + buffer->offset, p_data, buffer->len); - PAN_TRACE_DEBUG0 ("PAN successfully sent data to the PANU"); - return PAN_SUCCESS; + return PAN_WriteBuf(handle, dst, src, protocol, buffer, ext); } @@ -624,19 +572,6 @@ tPAN_RESULT PAN_WriteBuf (UINT16 handle, BD_ADDR dst, BD_ADDR src, UINT16 protoc UINT16 i; tBNEP_RESULT result; - /* Check if it is broadcast or multicast packet */ - if (dst[0] & 0x01) - { - UINT8 *p_data; - UINT16 len; - - p_data = (UINT8 *)(p_buf + 1) + p_buf->offset; - len = p_buf->len; - PAN_Write (handle, dst, src, protocol, p_data, len, ext); - GKI_freebuf (p_buf); - return PAN_SUCCESS; - } - if (pan_cb.role == PAN_ROLE_INACTIVE || (!(pan_cb.num_conns))) { PAN_TRACE_ERROR0 ("PAN is not active Data write failed"); @@ -644,6 +579,18 @@ tPAN_RESULT PAN_WriteBuf (UINT16 handle, BD_ADDR dst, BD_ADDR src, UINT16 protoc return PAN_FAILURE; } + /* Check if it is broadcast or multicast packet */ + if (dst[0] & 0x01) + { + UINT8 *data = (UINT8 *)p_buf + sizeof(BT_HDR) + p_buf->offset; + for (i = 0; i < MAX_PAN_CONNS; ++i) { + if (pan_cb.pcb[i].con_state == PAN_STATE_CONNECTED) + BNEP_Write(pan_cb.pcb[i].handle, dst, data, p_buf->len, protocol, src, ext); + } + GKI_freebuf(p_buf); + return PAN_SUCCESS; + } + /* Check if the data write is on PANU side */ if (pan_cb.active_role == PAN_ROLE_CLIENT) { -- 2.11.0