]> granicus.if.org Git - esp-idf/blobdiff - components/bt/bluedroid/stack/rfcomm/rfc_utils.c
Merge branch 'bugfix/spiram_malloc_reserve_internal_fragments' into 'master'
[esp-idf] / components / bt / bluedroid / stack / rfcomm / rfc_utils.c
old mode 100755 (executable)
new mode 100644 (file)
index 4850fc9..8b1e043
  *
  *****************************************************************************/
 
-#include "bt_target.h"
-#include "gki.h"
+#include "common/bt_target.h"
 
-#include "btm_api.h"
+#include "stack/btm_api.h"
 #include "btm_int.h"
-#include "rfcdefs.h"
-#include "port_api.h"
-#include "port_ext.h"
+#include "stack/rfcdefs.h"
+#include "stack/port_api.h"
+#include "stack/port_ext.h"
 #include "port_int.h"
 #include "rfc_int.h"
-#include "btu.h"
-#include "bt_defs.h"
+#include "stack/btu.h"
+#include "common/bt_defs.h"
+
+#include "osi/allocator.h"
+#include "osi/mutex.h"
 
 #include <string.h>
 
+#if (defined RFCOMM_INCLUDED && RFCOMM_INCLUDED == TRUE)
 /*******************************************************************************
 **
 ** Function         rfc_calc_fcs
@@ -44,8 +47,7 @@
 ** Description      Reversed CRC Table , 8-bit, poly=0x07
 **                  (GSM 07.10 TS 101 369 V6.3.0)
 *******************************************************************************/
-static const UINT8 rfc_crctable[] =
-{
+static const UINT8 rfc_crctable[] = {
     0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75,  0x0E, 0x9F, 0xED, 0x7C, 0x09, 0x98, 0xEA, 0x7B,
     0x1C, 0x8D, 0xFF, 0x6E, 0x1B, 0x8A, 0xF8, 0x69,  0x12, 0x83, 0xF1, 0x60, 0x15, 0x84, 0xF6, 0x67,
     0x38, 0xA9, 0xDB, 0x4A, 0x3F, 0xAE, 0xDC, 0x4D,  0x36, 0xA7, 0xD5, 0x44, 0x31, 0xA0, 0xD2, 0x43,
@@ -83,8 +85,7 @@ UINT8 rfc_calc_fcs (UINT16 len, UINT8 *p)
 {
     UINT8  fcs = 0xFF;
 
-    while (len--)
-    {
+    while (len--) {
         fcs = rfc_crctable[fcs ^ *p++];
     }
 
@@ -109,8 +110,7 @@ BOOLEAN rfc_check_fcs (UINT16 len, UINT8 *p, UINT8 received_fcs)
 {
     UINT8  fcs = 0xFF;
 
-    while (len--)
-    {
+    while (len--) {
         fcs = rfc_crctable[fcs ^ *p++];
     }
 
@@ -135,47 +135,47 @@ tRFC_MCB *rfc_alloc_multiplexer_channel (BD_ADDR bd_addr, BOOLEAN is_initiator)
     int i, j;
     tRFC_MCB *p_mcb = NULL;
     RFCOMM_TRACE_DEBUG("rfc_alloc_multiplexer_channel: bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
-                                bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
+                       bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
     RFCOMM_TRACE_DEBUG("rfc_alloc_multiplexer_channel:is_initiator:%d", is_initiator);
 
-    for (i = 0; i < MAX_BD_CONNECTIONS; i++)
-    {
+    for (i = 0; i < MAX_BD_CONNECTIONS; i++) {
         RFCOMM_TRACE_DEBUG("rfc_alloc_multiplexer_channel rfc_cb.port.rfc_mcb[%d].state:%d",
-                            i, rfc_cb.port.rfc_mcb[i].state);
+                           i, rfc_cb.port.rfc_mcb[i].state);
         RFCOMM_TRACE_DEBUG("(rfc_cb.port.rfc_mcb[i].bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
-                                rfc_cb.port.rfc_mcb[i].bd_addr[0], rfc_cb.port.rfc_mcb[i].bd_addr[1],
-                                rfc_cb.port.rfc_mcb[i].bd_addr[2], rfc_cb.port.rfc_mcb[i].bd_addr[3],
-                                rfc_cb.port.rfc_mcb[i].bd_addr[4], rfc_cb.port.rfc_mcb[i].bd_addr[5]);
+                           rfc_cb.port.rfc_mcb[i].bd_addr[0], rfc_cb.port.rfc_mcb[i].bd_addr[1],
+                           rfc_cb.port.rfc_mcb[i].bd_addr[2], rfc_cb.port.rfc_mcb[i].bd_addr[3],
+                           rfc_cb.port.rfc_mcb[i].bd_addr[4], rfc_cb.port.rfc_mcb[i].bd_addr[5]);
 
         if ((rfc_cb.port.rfc_mcb[i].state != RFC_MX_STATE_IDLE)
-         && (!memcmp (rfc_cb.port.rfc_mcb[i].bd_addr, bd_addr, BD_ADDR_LEN)))
-        {
+                && (!memcmp (rfc_cb.port.rfc_mcb[i].bd_addr, bd_addr, BD_ADDR_LEN))) {
             /* Multiplexer channel found do not change anything */
             /* If there was an inactivity timer running stop it now */
-            if (rfc_cb.port.rfc_mcb[i].state == RFC_MX_STATE_CONNECTED)
+            if (rfc_cb.port.rfc_mcb[i].state == RFC_MX_STATE_CONNECTED) {
                 rfc_timer_stop (&rfc_cb.port.rfc_mcb[i]);
+            }
             RFCOMM_TRACE_DEBUG("rfc_alloc_multiplexer_channel:is_initiator:%d, found, state:%d, p_mcb:%p",
-                                is_initiator, rfc_cb.port.rfc_mcb[i].state, &rfc_cb.port.rfc_mcb[i]);
+                               is_initiator, rfc_cb.port.rfc_mcb[i].state, &rfc_cb.port.rfc_mcb[i]);
             return (&rfc_cb.port.rfc_mcb[i]);
         }
     }
 
     /* connection with bd_addr does not exist */
-    for (i = 0, j = rfc_cb.rfc.last_mux + 1; i < MAX_BD_CONNECTIONS; i++, j++)
-    {
-        if (j >= MAX_BD_CONNECTIONS)
+    for (i = 0, j = rfc_cb.rfc.last_mux + 1; i < MAX_BD_CONNECTIONS; i++, j++) {
+        if (j >= MAX_BD_CONNECTIONS) {
             j = 0;
+        }
 
         p_mcb = &rfc_cb.port.rfc_mcb[j];
-        if (rfc_cb.port.rfc_mcb[j].state == RFC_MX_STATE_IDLE)
-        {
+        if (rfc_cb.port.rfc_mcb[j].state == RFC_MX_STATE_IDLE) {
             /* New multiplexer control block */
+            fixed_queue_free(p_mcb->cmd_q, NULL);
+            rfc_timer_free(p_mcb);
             memset (p_mcb, 0, sizeof (tRFC_MCB));
             memcpy (p_mcb->bd_addr, bd_addr, BD_ADDR_LEN);
             RFCOMM_TRACE_DEBUG("rfc_alloc_multiplexer_channel:is_initiator:%d, create new p_mcb:%p, index:%d",
-                                is_initiator, &rfc_cb.port.rfc_mcb[j], j);
+                               is_initiator, &rfc_cb.port.rfc_mcb[j], j);
 
-            GKI_init_q(&p_mcb->cmd_q);
+            p_mcb->cmd_q = fixed_queue_new(QUEUE_SIZE_MAX);
 
             p_mcb->is_initiator = is_initiator;
 
@@ -188,7 +188,9 @@ tRFC_MCB *rfc_alloc_multiplexer_channel (BD_ADDR bd_addr, BOOLEAN is_initiator)
     return (NULL);
 }
 
-
+void osi_free_fun(void *p){
+    osi_free(p);
+}
 /*******************************************************************************
 **
 ** Function         rfc_release_multiplexer_channel
@@ -199,12 +201,10 @@ tRFC_MCB *rfc_alloc_multiplexer_channel (BD_ADDR bd_addr, BOOLEAN is_initiator)
 *******************************************************************************/
 void rfc_release_multiplexer_channel (tRFC_MCB *p_mcb)
 {
-    void    *p_buf;
 
-    rfc_timer_stop (p_mcb);
+    rfc_timer_free (p_mcb);
 
-    while ((p_buf = GKI_dequeue(&p_mcb->cmd_q)) != NULL)
-        GKI_freebuf(p_buf);
+    fixed_queue_free(p_mcb->cmd_q, osi_free_fun);
 
     memset (p_mcb, 0, sizeof (tRFC_MCB));
     p_mcb->state = RFC_MX_STATE_IDLE;
@@ -229,7 +229,6 @@ void rfc_timer_start (tRFC_MCB *p_mcb, UINT16 timeout)
     btu_start_timer (p_tle, BTU_TTYPE_RFCOMM_MFC, timeout);
 }
 
-
 /*******************************************************************************
 **
 ** Function         rfc_timer_stop
@@ -244,6 +243,20 @@ void rfc_timer_stop (tRFC_MCB *p_mcb)
     btu_stop_timer (&p_mcb->tle);
 }
 
+/*******************************************************************************
+**
+** Function         rfc_timer_free
+**
+** Description      Stop and free RFC Timer
+**
+*******************************************************************************/
+void rfc_timer_free (tRFC_MCB *p_mcb)
+{
+    RFCOMM_TRACE_EVENT ("rfc_timer_free");
+
+    btu_free_timer (&p_mcb->tle);
+    memset(&p_mcb->tle, 0, sizeof(TIMER_LIST_ENT));
+}
 
 /*******************************************************************************
 **
@@ -263,7 +276,6 @@ void rfc_port_timer_start (tPORT *p_port, UINT16 timeout)
     btu_start_timer (p_tle, BTU_TTYPE_RFCOMM_PORT, timeout);
 }
 
-
 /*******************************************************************************
 **
 ** Function         rfc_port_timer_stop
@@ -278,6 +290,20 @@ void rfc_port_timer_stop (tPORT *p_port)
     btu_stop_timer (&p_port->rfc.tle);
 }
 
+/*******************************************************************************
+**
+** Function         rfc_port_timer_free
+**
+** Description      Stop and free RFC Timer
+**
+*******************************************************************************/
+void rfc_port_timer_free (tPORT *p_port)
+{
+    RFCOMM_TRACE_EVENT ("rfc_port_timer_stop");
+
+    btu_free_timer (&p_port->rfc.tle);
+    memset(&p_port->rfc.tle, 0, sizeof(TIMER_LIST_ENT));
+}
 
 /*******************************************************************************
 **
@@ -293,23 +319,20 @@ void rfc_check_mcb_active (tRFC_MCB *p_mcb)
 {
     UINT16 i;
 
-    for (i = 0; i < RFCOMM_MAX_DLCI; i++)
-    {
-        if (p_mcb->port_inx[i] != 0)
-        {
+    for (i = 0; i < RFCOMM_MAX_DLCI; i++) {
+        if (p_mcb->port_inx[i] != 0) {
             p_mcb->is_disc_initiator = FALSE;
             return;
         }
     }
     /* The last port was DISCed.  On the client side start disconnecting Mx */
     /* On the server side start inactivity timer */
-    if (p_mcb->is_disc_initiator)
-    {
+    if (p_mcb->is_disc_initiator) {
         p_mcb->is_disc_initiator = FALSE;
         rfc_mx_sm_execute (p_mcb, RFC_MX_EVENT_CLOSE_REQ, NULL);
-    }
-    else
+    } else {
         rfc_timer_start (p_mcb, RFC_MCB_RELEASE_INACT_TIMER);
+    }
 }
 
 
@@ -324,8 +347,7 @@ void rfc_check_mcb_active (tRFC_MCB *p_mcb)
 *******************************************************************************/
 void rfcomm_process_timeout (TIMER_LIST_ENT  *p_tle)
 {
-    switch (p_tle->event)
-    {
+    switch (p_tle->event) {
     case BTU_TTYPE_RFCOMM_MFC:
         rfc_mx_sm_execute ((tRFC_MCB *)p_tle->param, RFC_EVENT_TIMEOUT, NULL);
         break;
@@ -358,9 +380,10 @@ void rfc_sec_check_complete (BD_ADDR bd_addr, tBT_TRANSPORT transport, void *p_r
 
     /* Verify that PORT is still waiting for Security to complete */
     if (!p_port->in_use
-     || ((p_port->rfc.state != RFC_STATE_ORIG_WAIT_SEC_CHECK)
-      && (p_port->rfc.state != RFC_STATE_TERM_WAIT_SEC_CHECK)))
+            || ((p_port->rfc.state != RFC_STATE_ORIG_WAIT_SEC_CHECK)
+                && (p_port->rfc.state != RFC_STATE_TERM_WAIT_SEC_CHECK))) {
         return;
+    }
 
     rfc_port_sm_execute ((tPORT *)p_ref_data, RFC_EVENT_SEC_COMPLETE, &res);
 }
@@ -388,8 +411,7 @@ void rfc_port_closed (tPORT *p_port)
     p_port->rfc.state = RFC_STATE_CLOSED;
 
     /* If multiplexer channel was up mark it as down */
-    if (p_mcb)
-    {
+    if (p_mcb) {
         p_mcb->port_inx[p_port->dlci] = 0;
 
         /* If there are no more ports opened on this MCB release it */
@@ -413,14 +435,14 @@ void rfc_port_closed (tPORT *p_port)
 *******************************************************************************/
 void rfc_inc_credit (tPORT *p_port, UINT8 credit)
 {
-    if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT)
-    {
+    if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) {
         p_port->credit_tx += credit;
 
         RFCOMM_TRACE_EVENT ("rfc_inc_credit:%d", p_port->credit_tx);
 
-        if (p_port->tx.peer_fc == TRUE)
+        if (p_port->tx.peer_fc == TRUE) {
             PORT_FlowInd(p_port->rfc.p_mcb, p_port->dlci, TRUE);
+        }
     }
 }
 
@@ -437,13 +459,14 @@ void rfc_inc_credit (tPORT *p_port, UINT8 credit)
 *******************************************************************************/
 void rfc_dec_credit (tPORT *p_port)
 {
-    if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT)
-    {
-        if (p_port->credit_tx > 0)
+    if (p_port->rfc.p_mcb->flow == PORT_FC_CREDIT) {
+        if (p_port->credit_tx > 0) {
             p_port->credit_tx--;
+        }
 
-        if (p_port->credit_tx == 0)
+        if (p_port->credit_tx == 0) {
             p_port->tx.peer_fc = TRUE;
+        }
     }
 }
 
@@ -463,16 +486,18 @@ void rfc_check_send_cmd(tRFC_MCB *p_mcb, BT_HDR *p_buf)
     BT_HDR  *p;
 
     /* if passed a buffer queue it */
-    if (p_buf != NULL)
-    {
-        GKI_enqueue(&p_mcb->cmd_q, p_buf);
+    if (p_buf != NULL) {
+        if (p_mcb->cmd_q == NULL) {
+            RFCOMM_TRACE_ERROR("%s: empty queue: p_mcb = %p p_mcb->lcid = %u cached p_mcb = %p",
+                               __func__, p_mcb, p_mcb->lcid,
+                               rfc_find_lcid_mcb(p_mcb->lcid));
+        }
+        fixed_queue_enqueue(p_mcb->cmd_q, p_buf);
     }
 
     /* handle queue if L2CAP not congested */
-    while (p_mcb->l2cap_congested == FALSE)
-    {
-        if ((p = (BT_HDR *) GKI_dequeue(&p_mcb->cmd_q)) == NULL)
-        {
+    while (p_mcb->l2cap_congested == FALSE) {
+        if ((p = (BT_HDR *)fixed_queue_try_dequeue(p_mcb->cmd_q)) == NULL) {
             break;
         }
 
@@ -482,3 +507,4 @@ void rfc_check_send_cmd(tRFC_MCB *p_mcb, BT_HDR *p_buf)
 }
 
 
+#endif ///(defined RFCOMM_INCLUDED && RFCOMM_INCLUDED == TRUE)