OSDN Git Service

fixed rfc resouce leaking and multi-session issue
authorMatthew Xie <mattx@google.com>
Tue, 15 Jan 2013 23:54:03 +0000 (15:54 -0800)
committerMatthew Xie <mattx@google.com>
Wed, 16 Jan 2013 04:53:23 +0000 (20:53 -0800)
also added more debug messages
bug 7486080

Change-Id: Ic21e9d2df5f651b51ea2126a3cac25811808dcfb

bta/include/bta_jv_api.h
bta/jv/bta_jv_act.c
bta/jv/bta_jv_api.c
bta/jv/bta_jv_int.h
btif/src/btif_sock_rfc.c
stack/include/port_api.h
stack/rfcomm/port_api.c
stack/rfcomm/port_rfc.c
stack/rfcomm/port_utils.c
stack/rfcomm/rfc_utils.c

index 7533a61..8336c87 100644 (file)
@@ -1025,7 +1025,7 @@ BTA_API extern tBTA_JV_STATUS BTA_JvRfcommConnect(tBTA_SEC sec_mask,
 **                  BTA_JV_FAILURE, otherwise.
 **
 *******************************************************************************/
-BTA_API extern tBTA_JV_STATUS BTA_JvRfcommClose(UINT32 handle);
+BTA_API extern tBTA_JV_STATUS BTA_JvRfcommClose(UINT32 handle, void* user_data);
 
 /*******************************************************************************
 **
@@ -1057,7 +1057,7 @@ BTA_API extern tBTA_JV_STATUS BTA_JvRfcommStartServer(tBTA_SEC sec_mask,
 **                  BTA_JV_FAILURE, otherwise.
 **
 *******************************************************************************/
-BTA_API extern tBTA_JV_STATUS BTA_JvRfcommStopServer(UINT32 handle);
+BTA_API extern tBTA_JV_STATUS BTA_JvRfcommStopServer(UINT32 handle, void* user_data);
 
 /*******************************************************************************
 **
index 51382af..160bafe 100644 (file)
@@ -59,7 +59,7 @@ static inline void logu(const char* title, const uint8_t * p_uuid)
 }
 
 
-static tBTA_JV_PCB * bta_jv_add_rfc_port(tBTA_JV_RFC_CB *p_cb);
+static tBTA_JV_PCB * bta_jv_add_rfc_port(tBTA_JV_RFC_CB *p_cb, tBTA_JV_PCB *p_pcb_open);
 
 /*******************************************************************************
 **
@@ -118,6 +118,32 @@ UINT8 bta_jv_alloc_sec_id(void)
     return ret;
 
 }
+static int get_sec_id_used(void)
+{
+    int i;
+    int used = 0;
+    for(i=0; i<BTA_JV_NUM_SERVICE_ID; i++)
+    {
+        if(bta_jv_cb.sec_id[i])
+            used++;
+    }
+    if(used == BTA_JV_NUM_SERVICE_ID)
+        APPL_TRACE_ERROR1("get_sec_id_used, sec id exceeds the limit:%d", BTA_JV_NUM_SERVICE_ID);
+    return used;
+}
+static int get_rfc_cb_used(void)
+{
+    int i;
+    int used = 0;
+    for(i=0; i<BTA_JV_MAX_RFC_CONN; i++)
+    {
+        if(bta_jv_cb.rfc_cb[i].handle )
+            used++;
+    }
+    if(used == BTA_JV_MAX_RFC_CONN)
+        APPL_TRACE_ERROR1("get_sec_id_used, rfc ctrl block exceeds the limit:%d", BTA_JV_MAX_RFC_CONN);
+    return used;
+}
 
 /*******************************************************************************
 **
@@ -160,6 +186,7 @@ tBTA_JV_RFC_CB * bta_jv_alloc_rfc_cb(UINT16 port_handle, tBTA_JV_PCB **pp_pcb)
             p_cb = &bta_jv_cb.rfc_cb[i];
             p_cb->handle            = i + 1;
             p_cb->max_sess          = 1;
+            p_cb->curr_sess         = 1;
             p_cb->rfc_hdl[0]        = port_handle;
             APPL_TRACE_DEBUG2( "bta_jv_alloc_rfc_cb port_handle:%d handle:%d",
                 port_handle, p_cb->handle);
@@ -170,6 +197,11 @@ tBTA_JV_RFC_CB * bta_jv_alloc_rfc_cb(UINT16 port_handle, tBTA_JV_PCB **pp_pcb)
             break;
         }
     }
+    if(p_cb == NULL)
+    {
+        APPL_TRACE_ERROR2( "bta_jv_alloc_rfc_cb: port_handle:%d, ctrl block exceeds limit:%d",
+                    port_handle, BTA_JV_MAX_RFC_CONN);
+    }
     return p_cb;
 }
 
@@ -217,114 +249,84 @@ tBTA_JV_RFC_CB * bta_jv_rfc_port_to_cb(UINT16 port_handle)
     }
     return p_cb;
 }
-
-/*******************************************************************************
-**
-** Function     bta_jv_free_rfc_pcb
-**
-** Description  free the given port control block
-**
-** Returns
-**
-*******************************************************************************/
-tBTA_JV_STATUS bta_jv_free_rfc_pcb(tBTA_JV_PCB *p_pcb)
+static tBTA_JV_STATUS bta_jv_free_rfc_cb(tBTA_JV_RFC_CB *p_cb, tBTA_JV_PCB *p_pcb)
 {
     tBTA_JV_STATUS status = BTA_JV_SUCCESS;
-    BOOLEAN         remove = FALSE;
-    BOOLEAN         is_server = TRUE;
-    UINT16          port_handle;
-
-    APPL_TRACE_DEBUG2( "bta_jv_free_rfc_pcb handle:%d s:%d", p_pcb->port_handle, p_pcb->state);
+    UINT8           i;
+    if(!p_cb || !p_pcb)
+    {
+        APPL_TRACE_ERROR0("bta_jv_free_sr_rfc_cb, p_cb or p_pcb cannot be null");
+        return BTA_JV_FAILURE;
+    }
+    APPL_TRACE_DEBUG5("bta_jv_free_sr_rfc_cb: max_sess:%d, curr_sess:%d, p_pcb:%p, user:%d, state:%d"
+                     ,p_cb->max_sess, p_cb->curr_sess, p_pcb, (int)p_pcb->user_data, p_pcb->state);
 
-    if (p_pcb->port_handle)
+    if(p_cb->curr_sess <= 0)
+        return BTA_JV_SUCCESS;
+    int state = p_pcb->state;
+    int close_pending = 0;
+    if(p_pcb->state == BTA_JV_ST_SR_LISTEN)
+    {
+        APPL_TRACE_DEBUG2("bta_jv_free_sr_rfc_cb: state: BTA_JV_ST_SR_LISTEN, scn:%d, ueser_data:%d"
+                        , p_cb->scn, (int)p_pcb->user_data);
+        p_pcb->state = BTA_JV_ST_SR_CLOSING;
+        if(RFCOMM_RemoveServer(p_pcb->port_handle) != PORT_SUCCESS)
+                status = BTA_JV_FAILURE;
+    }
+    else if(p_pcb->state == BTA_JV_ST_SR_OPEN)
+    {
+        APPL_TRACE_DEBUG2("bta_jv_free_sr_rfc_cb: state: BTA_JV_ST_SR_OPEN, scn:%d, ueser_data:%d"
+                        , p_cb->scn, (int)p_pcb->user_data);
+        p_pcb->state = BTA_JV_ST_SR_CLOSING;
+        if(RFCOMM_RemoveConnection(p_pcb->port_handle) != PORT_SUCCESS)
+                status = BTA_JV_FAILURE;
+        //else close_pending = 1;
+    }
+    else if(p_pcb->state == BTA_JV_ST_CL_OPEN)
+    {
+        APPL_TRACE_DEBUG2("bta_jv_free_sr_rfc_cb: state: BTA_JV_ST_CL_OPEN, scn:%d, ueser_data:%d"
+                         ,p_cb->scn, (int)p_pcb->user_data);
+        p_pcb->state = BTA_JV_ST_CL_CLOSING;
+        if(RFCOMM_RemoveConnection(p_pcb->port_handle) != PORT_SUCCESS)
+                status = BTA_JV_FAILURE;
+        //else close_pending = 1;
+    }
+    else if(p_pcb->state == BTA_JV_ST_CL_CLOSING || p_pcb->state == BTA_JV_ST_SR_CLOSING)
+    {
+       APPL_TRACE_WARNING4("bta_jv_free_sr_rfc_cb: return on closing, port state:%d, scn:%d, p_pcb:%p, ueser_data:%d"
+                        , p_pcb->state, p_cb->scn, p_pcb, (int)p_pcb->user_data);
+       status = BTA_JV_FAILURE;
+       return status;
+    }
+    else
+    {
+       APPL_TRACE_WARNING4("bta_jv_free_sr_rfc_cb: failed, ignore port state:%d, scn:%d, p_pcb:%p, ueser_data:%d"
+                        , p_pcb->state, p_cb->scn, p_pcb, (int)p_pcb->user_data);
+       status = BTA_JV_FAILURE;
+    }
+    if(!close_pending)
     {
-        if(BTA_JV_ST_NONE != p_pcb->state)
-        {
-            remove = TRUE;
-            if(p_pcb->state <= BTA_JV_ST_CL_MAX)
-                is_server = FALSE;
-            port_handle = p_pcb->port_handle;
-        }
         p_pcb->port_handle = 0;
         p_pcb->state = BTA_JV_ST_NONE;
 
         //Initialize congestion flags
         p_pcb->cong = FALSE;
-
-        if(remove)
-        {
-            if(is_server)
-            {
-                if(RFCOMM_RemoveServer(port_handle) != PORT_SUCCESS)
-                    status = BTA_JV_FAILURE;
-            }
-            else
-            {
-                if(RFCOMM_RemoveConnection(port_handle) != PORT_SUCCESS)
-                    status = BTA_JV_FAILURE;
-            }
-        }
-    }
-    return status;
-}
-
-/*******************************************************************************
-**
-** Function     bta_jv_free_rfc_cb
-**
-** Description  free the given RFCOMM control block
-**
-** Returns
-**
-*******************************************************************************/
-tBTA_JV_STATUS bta_jv_free_rfc_cb(tBTA_JV_RFC_CB *p_cb)
-{
-    tBTA_JV_STATUS status = BTA_JV_SUCCESS;
-    UINT8           i;
-    APPL_TRACE_DEBUG1( "bta_jv_free_rfc_cb max_sess:%d", p_cb->max_sess);
-    for (i=0; i<p_cb->max_sess; i++)
-    {
-        APPL_TRACE_DEBUG2( "[%d]: port=%d", i, p_cb->rfc_hdl[i]);
-        if (p_cb->rfc_hdl[i])
-        {
-            bta_jv_free_rfc_pcb (&bta_jv_cb.port_cb[p_cb->rfc_hdl[i] - 1]);
-            p_cb->rfc_hdl[i] = 0;
-        }
-    }
-
-    p_cb->scn = 0;
-    bta_jv_free_sec_id(&p_cb->sec_id);
-    p_cb->p_cback = NULL;
-    p_cb->handle = 0;
-    return status;
-}
-static tBTA_JV_STATUS bta_jv_free_rfc_listen_cb(tBTA_JV_RFC_CB *p_cb)
-{
-    tBTA_JV_STATUS status = BTA_JV_SUCCESS;
-    UINT8           i;
-    APPL_TRACE_DEBUG1("max_sess:%d", p_cb->max_sess);
-    for (i=0; i<p_cb->max_sess; i++)
-    {
-        APPL_TRACE_DEBUG2( "bta_jv_free_rfc_listen_cb: [%d]: port=%d", i, p_cb->rfc_hdl[i]);
-        if (p_cb->rfc_hdl[i])
+        p_pcb->user_data = 0;
+        int si = BTA_JV_RFC_HDL_TO_SIDX(p_pcb->handle);
+        if(0 <= si && si < BTA_JV_MAX_RFC_SR_SESSION)
+            p_cb->rfc_hdl[si] = 0;
+        p_cb->curr_sess--;
+        if(p_cb->curr_sess == 0)
         {
-            tBTA_JV_PCB *p_pcb = &bta_jv_cb.port_cb[p_cb->rfc_hdl[i] - 1];
-            if(p_pcb->state == BTA_JV_ST_SR_LISTEN)
-            {
-                APPL_TRACE_DEBUG2("free listen pcb: scn:%d, ueser_data:%d", p_cb->scn, (int)p_pcb->user_data);
-                p_pcb->user_data = 0;
-                bta_jv_free_rfc_pcb (p_pcb);
-                p_cb->max_sess = 1;
-                p_cb->rfc_hdl[i] = 0;
-                break;
-            }
+            p_cb->scn = 0;
+            bta_jv_free_sec_id(&p_cb->sec_id);
+            p_cb->p_cback = NULL;
+            p_cb->handle = 0;
+            p_cb->curr_sess = -1;
         }
     }
-
-    bta_jv_free_sec_id(&p_cb->sec_id);
     return status;
 }
-
 /*******************************************************************************
 **
 ** Function     bta_jv_free_l2c_cb
@@ -523,6 +525,8 @@ void bta_jv_enable(tBTA_JV_MSG *p_data)
 *******************************************************************************/
 void bta_jv_disable (tBTA_JV_MSG *p_data)
 {
+    APPL_TRACE_ERROR0("bta_jv_disable not used");
+#if 0
     int i;
 
     bta_jv_cb.p_dm_cback = NULL;
@@ -569,6 +573,7 @@ void bta_jv_disable (tBTA_JV_MSG *p_data)
             bta_jv_cb.sec_id[i] = 0;
         }
     }
+#endif
 }
 
 /*******************************************************************************
@@ -1213,7 +1218,6 @@ void bta_jv_delete_attribute(tBTA_JV_MSG *p_data)
 void bta_jv_delete_record(tBTA_JV_MSG *p_data)
 {
     tBTA_JV_API_ADD_ATTRIBUTE *dr = &(p_data->add_attr);
-
     if(dr->handle)
     {
         /* this is a record created by btif layer*/
@@ -1723,8 +1727,9 @@ static int bta_jv_port_data_co_cback(UINT16 port_handle, UINT8 *buf, UINT16 len,
 {
     tBTA_JV_RFC_CB  *p_cb = bta_jv_rfc_port_to_cb(port_handle);
     tBTA_JV_PCB     *p_pcb = bta_jv_rfc_port_to_pcb(port_handle);
-
-    if (p_cb != NULL)
+    APPL_TRACE_DEBUG3("bta_jv_port_data_co_cback, p_cb:%p, p_pcb:%p, len:%d",
+                        p_cb, p_pcb, len);
+    if (p_pcb != NULL)
     {
         switch(type)
         {
@@ -1761,7 +1766,7 @@ static void bta_jv_port_mgmt_cl_cback(UINT32 code, UINT16 port_handle)
     UINT16 lcid;
     tBTA_JV_RFCOMM_CBACK *p_cback;  /* the callback function */
 
-    APPL_TRACE_DEBUG1( "bta_jv_port_mgmt_cl_cback:%d", port_handle);
+    APPL_TRACE_DEBUG2( "bta_jv_port_mgmt_cl_cback:code:%d, port_handle%d", code, port_handle);
     if(NULL == p_cb || NULL == p_cb->p_cback)
         return;
 
@@ -1788,12 +1793,11 @@ static void bta_jv_port_mgmt_cl_cback(UINT32 code, UINT16 port_handle)
         {
             evt_data.rfc_close.async = FALSE;
         }
-        p_pcb->state = BTA_JV_ST_NONE;
-        p_pcb->cong = FALSE;
+        //p_pcb->state = BTA_JV_ST_NONE;
+        //p_pcb->cong = FALSE;
         p_cback = p_cb->p_cback;
-        bta_jv_free_rfc_cb(p_cb);
-
         p_cback(BTA_JV_RFCOMM_CLOSE_EVT, &evt_data, p_pcb->user_data);
+        //bta_jv_free_rfc_cb(p_cb, p_pcb);
     }
 
 }
@@ -1853,8 +1857,7 @@ void bta_jv_rfcomm_connect(tBTA_JV_MSG *p_data)
     tBTA_JV_RFC_CB  *p_cb = NULL;
     tBTA_JV_PCB     *p_pcb;
     tBTA_JV_API_RFCOMM_CONNECT *cc = &(p_data->rfcomm_connect);
-    tBTA_JV_RFCOMM_CL_INIT      evt_data;
-    memset(&evt_data, 0, sizeof(evt_data));
+    tBTA_JV_RFCOMM_CL_INIT      evt_data = {0};
 
     /* TODO DM role manager
     L2CA_SetDesireRole(cc->role);
@@ -1919,7 +1922,24 @@ void bta_jv_rfcomm_connect(tBTA_JV_MSG *p_data)
             RFCOMM_RemoveConnection(handle);
     }
  }
-
+static int find_rfc_pcb(void* user_data, tBTA_JV_RFC_CB **cb, tBTA_JV_PCB **pcb)
+{
+    *cb = NULL;
+    *pcb = NULL;
+    int i;
+    for(i = 0; i < MAX_RFC_PORTS; i++)
+    {
+         UINT32 rfc_handle = bta_jv_cb.port_cb[i].handle & BTA_JV_RFC_HDL_MASK;
+        if(rfc_handle && bta_jv_cb.port_cb[i].user_data == user_data)
+        {
+            *pcb = &bta_jv_cb.port_cb[i];
+            *cb = &bta_jv_cb.rfc_cb[rfc_handle - 1];
+            return 1;
+        }
+    }
+    APPL_TRACE_DEBUG1("find_rfc_pcb: cannot find rfc_cb from user data:%d", (UINT32)user_data);
+    return 0;
+}
 /*******************************************************************************
 **
 ** Function     bta_jv_rfcomm_close
@@ -1931,51 +1951,22 @@ void bta_jv_rfcomm_connect(tBTA_JV_MSG *p_data)
 *******************************************************************************/
 void bta_jv_rfcomm_close(tBTA_JV_MSG *p_data)
 {
-    tBTA_JV_RFCOMM_CLOSE     evt_data;
     tBTA_JV_API_RFCOMM_CLOSE *cc = &(p_data->rfcomm_close);
+    tBTA_JV_RFC_CB           *p_cb = NULL;
+    tBTA_JV_PCB              *p_pcb = NULL;
+    APPL_TRACE_DEBUG1("bta_jv_rfcomm_close, rfc handle:%d", cc->handle);
     if(!cc->handle)
     {
         APPL_TRACE_ERROR0("bta_jv_rfcomm_close, rfc handle is null");
         return;
     }
 
-    tBTA_JV_RFC_CB           *p_cb = cc->p_cb;
-    tBTA_JV_PCB              *p_pcb = cc->p_pcb;
-    tBTA_JV_RFCOMM_CBACK     *p_cback = p_cb->p_cback;
-    evt_data.handle = p_cb->handle;
-    evt_data.status = BTA_JV_FAILURE;
-
-    void* user_data = p_pcb->user_data;
-    p_pcb->cong = FALSE;
-    if(p_pcb->state <= BTA_JV_ST_CL_MAX)
-    {
-        if(p_pcb->state == BTA_JV_ST_CL_OPEN)
-        {
-            if(PORT_SUCCESS == RFCOMM_RemoveConnection(p_pcb->port_handle))
-            {
-                p_pcb->state = BTA_JV_ST_CL_CLOSING;
-                return;
-            }
-        }
-        evt_data.status = bta_jv_free_rfc_cb(p_cb);
-    }
-    else if(BTA_JV_ST_SR_OPEN == p_pcb->state)
-    {
-        /* server is connected */
-
-        if(PORT_SUCCESS == RFCOMM_RemoveConnection(p_pcb->port_handle))
-        {
-            p_pcb->state = BTA_JV_ST_SR_CLOSING;
-            return;
-        }
-    }
-
-    evt_data.async = FALSE;
-
-    if (p_cback)
-        p_cback(BTA_JV_RFCOMM_CLOSE_EVT, (tBTA_JV *)&evt_data, user_data);
-    else
-        APPL_TRACE_ERROR0("bta_jv_rfcomm_close, p_cback is null");
+    void* user_data = cc->user_data;
+    if(!find_rfc_pcb(user_data, &p_cb, &p_pcb))
+        return;
+    bta_jv_free_rfc_cb(p_cb, p_pcb);
+    APPL_TRACE_DEBUG2("bta_jv_rfcomm_close: sec id in use:%d, rfc_cb in use:%d",
+                get_sec_id_used(), get_rfc_cb_used());
 }
 
 /*******************************************************************************
@@ -2030,12 +2021,16 @@ static void bta_jv_port_mgmt_sr_cback(UINT32 code, UINT16 port_handle)
     UINT16 lcid;
     UINT8  num;
     UINT32  si;
-
+    APPL_TRACE_DEBUG2("bta_jv_port_mgmt_sr_cback, code:%d, port_handle:%d", code, port_handle);
     if(NULL == p_cb || NULL == p_cb->p_cback)
+    {
+        APPL_TRACE_ERROR2("bta_jv_port_mgmt_sr_cback, p_cb:%p, p_cb->p_cback%p",
+                p_cb, p_cb ? p_cb->p_cback : NULL);
         return;
+    }
     void *user_data = p_pcb->user_data;
-    APPL_TRACE_DEBUG4( "bta_jv_port_mgmt_sr_cback code=%d port_handle:%d handle:%d/0x%x",
-        code, port_handle, p_cb->handle, p_pcb->handle);
+    APPL_TRACE_DEBUG5( "bta_jv_port_mgmt_sr_cback code=%d port_handle:%d handle:%d, p_pcb:%p, user:%d",
+        code, port_handle, p_cb->handle, p_pcb, p_pcb->user_data);
 
     PORT_CheckConnection(port_handle, rem_bda, &lcid);
     int failed = TRUE;
@@ -2044,13 +2039,12 @@ static void bta_jv_port_mgmt_sr_cback(UINT32 code, UINT16 port_handle)
         evt_data.rfc_srv_open.handle = p_pcb->handle;
         evt_data.rfc_srv_open.status = BTA_JV_SUCCESS;
         bdcpy(evt_data.rfc_srv_open.rem_bda, rem_bda);
-        p_pcb->state = BTA_JV_ST_SR_OPEN;
-        tBTA_JV_PCB *p_pcb_new_listen  = bta_jv_add_rfc_port(p_cb);
+        tBTA_JV_PCB *p_pcb_new_listen  = bta_jv_add_rfc_port(p_cb, p_pcb);
         if(p_pcb_new_listen)
         {
             evt_data.rfc_srv_open.new_listen_handle = p_pcb_new_listen->handle;
             p_pcb_new_listen->user_data = p_cb->p_cback(BTA_JV_RFCOMM_SRV_OPEN_EVT, &evt_data, user_data);
-            APPL_TRACE_DEBUG1("PORT_SUCCESS: max_sess:%d", p_cb->max_sess);
+            APPL_TRACE_DEBUG2("PORT_SUCCESS: curr_sess:%d, max_sess:%d", p_cb->curr_sess, p_cb->max_sess);
             failed = FALSE;
         }
         else APPL_TRACE_ERROR0("bta_jv_add_rfc_port failed to create new listen port");
@@ -2064,23 +2058,19 @@ static void bta_jv_port_mgmt_sr_cback(UINT32 code, UINT16 port_handle)
         p_pcb->cong = FALSE;
 
         tBTA_JV_RFCOMM_CBACK    *p_cback = p_cb->p_cback;
+        APPL_TRACE_DEBUG2("PORT_CLOSED before BTA_JV_RFCOMM_CLOSE_EVT: curr_sess:%d, max_sess:%d",
+                            p_cb->curr_sess, p_cb->max_sess);
         if(BTA_JV_ST_SR_CLOSING == p_pcb->state)
         {
             evt_data.rfc_close.async = FALSE;
             evt_data.rfc_close.status = BTA_JV_SUCCESS;
         }
-        p_pcb->cong = FALSE;
-        p_cback = p_cb->p_cback;
-        APPL_TRACE_DEBUG1( "removing rfc handle:0x%x", p_pcb->handle);
-        si = BTA_JV_RFC_HDL_TO_SIDX(p_pcb->handle);
-        p_cb->rfc_hdl[si] = 0;
-        p_pcb->state = BTA_JV_ST_NONE;
-        p_pcb->handle = 0;
-        RFCOMM_RemoveServer(port_handle);
-        evt_data.rfc_close.port_status = code;
+        //p_pcb->state = BTA_JV_ST_NONE;
         p_cback(BTA_JV_RFCOMM_CLOSE_EVT, &evt_data, user_data);
-        APPL_TRACE_DEBUG1("PORT_CLOSED after BTA_JV_RFCOMM_CLOSE_EVT: max_sess:%d",
-                          p_cb->max_sess);
+        //bta_jv_free_rfc_cb(p_cb, p_pcb);
+
+        APPL_TRACE_DEBUG2("PORT_CLOSED after BTA_JV_RFCOMM_CLOSE_EVT: curr_sess:%d, max_sess:%d",
+                p_cb->curr_sess, p_cb->max_sess);
      }
 }
 
@@ -2131,14 +2121,13 @@ static void bta_jv_port_event_sr_cback(UINT32 code, UINT16 port_handle)
 ** Returns   return a pointer to tBTA_JV_PCB just added
 **
 *******************************************************************************/
-static tBTA_JV_PCB * bta_jv_add_rfc_port(tBTA_JV_RFC_CB *p_cb)
+static tBTA_JV_PCB * bta_jv_add_rfc_port(tBTA_JV_RFC_CB *p_cb, tBTA_JV_PCB *p_pcb_open)
 {
     UINT8   used = 0, i, listen=0;
     UINT32  si = 0;
-    tBTA_JV_PCB *p_pcb = NULL;
     tPORT_STATE port_state;
     UINT32 event_mask = (PORT_EV_RXCHAR | PORT_EV_FC | PORT_EV_FCS);
-
+    tBTA_JV_PCB *p_pcb = NULL;
     if (p_cb->max_sess > 1)
     {
         for (i=0; i<p_cb->max_sess; i++)
@@ -2147,27 +2136,46 @@ static tBTA_JV_PCB * bta_jv_add_rfc_port(tBTA_JV_RFC_CB *p_cb)
             {
                 p_pcb = &bta_jv_cb.port_cb[p_cb->rfc_hdl[i] - 1];
                 if (p_pcb->state == BTA_JV_ST_SR_LISTEN)
+                {
                     listen++;
+                    if(p_pcb_open == p_pcb)
+                    {
+                        APPL_TRACE_DEBUG1("bta_jv_add_rfc_port, port_handle:%d, change the listen port to open state",
+                                              p_pcb->port_handle);
+                        p_pcb->state = BTA_JV_ST_SR_OPEN;
+
+                    }
+                    else
+                    {
+                        APPL_TRACE_ERROR3("bta_jv_add_rfc_port, open pcb not matching listen one,"
+                            "listen count:%d, listen pcb handle:%d, open pcb:%d",
+                               listen, p_pcb->port_handle, p_pcb_open->handle);
+                        return NULL;
+                    }
+                }
                 used++;
             }
-            else if (si==0)
+            else if (si == 0)
             {
                 si = (UINT32)(i + 1);
             }
         }
 
-        APPL_TRACE_DEBUG4("bta_jv_add_rfc_port max_sess=%d used:%d listen:%d si:%d",
-                    p_cb->max_sess, used, listen, si);
-
-        if (used <p_cb->max_sess && listen==0 && si)
+        APPL_TRACE_DEBUG5("bta_jv_add_rfc_port max_sess=%d used:%d curr_sess:%d, listen:%d si:%d",
+                    p_cb->max_sess, used, p_cb->curr_sess, listen, si);
+        if (used <p_cb->max_sess && listen == 1 && si)
         {
             si--;
             if (RFCOMM_CreateConnection(p_cb->sec_id, p_cb->scn, TRUE,
                 BTA_JV_DEF_RFC_MTU, (UINT8 *) bd_addr_any, &(p_cb->rfc_hdl[si]), bta_jv_port_mgmt_sr_cback) == PORT_SUCCESS)
             {
+                p_cb->curr_sess++;
                 p_pcb = &bta_jv_cb.port_cb[p_cb->rfc_hdl[si] - 1];
                 p_pcb->state = BTA_JV_ST_SR_LISTEN;
                 p_pcb->port_handle = p_cb->rfc_hdl[si];
+                p_pcb->user_data = p_pcb_open->user_data;
+
+                PORT_ClearKeepHandleFlag(p_pcb->port_handle);
                 PORT_SetEventCallback(p_pcb->port_handle, bta_jv_port_event_sr_cback);
                 PORT_SetDataCOCallback (p_pcb->port_handle, bta_jv_port_data_co_cback);
                 PORT_SetEventMask(p_pcb->port_handle, event_mask);
@@ -2179,10 +2187,14 @@ static tBTA_JV_PCB * bta_jv_add_rfc_port(tBTA_JV_RFC_CB *p_cb)
 FALSE-POSITIVE: port_state is initialized at PORT_GetState() */
                 PORT_SetState(p_pcb->port_handle, &port_state);
                 p_pcb->handle = BTA_JV_RFC_H_S_TO_HDL(p_cb->handle, si);
-                APPL_TRACE_DEBUG1( "new rfc handle:0x%x", p_pcb->handle);
+                APPL_TRACE_DEBUG2("bta_jv_add_rfc_port: p_pcb->handle:0x%x, curr_sess:%d",
+                                    p_pcb->handle, p_cb->curr_sess);
             }
         }
+        else APPL_TRACE_ERROR0("bta_jv_add_rfc_port, cannot create new rfc listen port");
     }
+    APPL_TRACE_DEBUG2("bta_jv_add_rfc_port: sec id in use:%d, rfc_cb in use:%d",
+                get_sec_id_used(), get_rfc_cb_used());
     return p_pcb;
 }
 
@@ -2205,12 +2217,14 @@ void bta_jv_rfcomm_start_server(tBTA_JV_MSG *p_data)
     tBTA_JV_RFC_CB  *p_cb = NULL;
     tBTA_JV_PCB     *p_pcb;
     tBTA_JV_API_RFCOMM_SERVER *rs = &(p_data->rfcomm_server);
-    tBTA_JV_RFCOMM_START        evt_data;
-    memset(&evt_data, 0, sizeof(evt_data));
+    tBTA_JV_RFCOMM_START        evt_data = {0};
     /* TODO DM role manager
     L2CA_SetDesireRole(rs->role);
     */
     evt_data.status = BTA_JV_FAILURE;
+    APPL_TRACE_DEBUG2("bta_jv_rfcomm_start_server: sec id in use:%d, rfc_cb in use:%d",
+                get_sec_id_used(), get_rfc_cb_used());
+
     do
     {
         sec_id = bta_jv_alloc_sec_id();
@@ -2230,6 +2244,7 @@ void bta_jv_rfcomm_start_server(tBTA_JV_MSG *p_data)
             break;
         }
 
+
         p_cb = bta_jv_alloc_rfc_cb(handle, &p_pcb);
         if(!p_cb)
         {
@@ -2248,6 +2263,7 @@ void bta_jv_rfcomm_start_server(tBTA_JV_MSG *p_data)
         evt_data.sec_id = sec_id;
         evt_data.use_co = TRUE; //FALSE;
 
+        PORT_ClearKeepHandleFlag(handle);
         PORT_SetEventCallback(handle, bta_jv_port_event_sr_cback);
         PORT_SetEventMask(handle, event_mask);
         PORT_GetState(handle, &port_state);
@@ -2284,36 +2300,24 @@ FALSE-POSITIVE: port_state is initialized at PORT_GetState() */
 
 void bta_jv_rfcomm_stop_server(tBTA_JV_MSG *p_data)
 {
-    tBTA_JV_RFCOMM_CLOSE  evt_data;
-    int i;
     tBTA_JV_API_RFCOMM_SERVER *ls = &(p_data->rfcomm_server);
+    tBTA_JV_RFC_CB           *p_cb = NULL;
+    tBTA_JV_PCB              *p_pcb = NULL;
+    APPL_TRACE_ERROR0("bta_jv_rfcomm_stop_server");
     if(!ls->rfc_handle)
     {
         APPL_TRACE_ERROR0("bta_jv_rfcomm_stop_server, rfc handle is null");
         return;
     }
-    HDL2CB(ls->rfc_handle);
-    evt_data.status = BTA_JV_FAILURE;
-    if(p_cb && p_pcb)
-    {
-        tBTA_JV_RFCOMM_CBACK     *p_cback = p_cb->p_cback;
-        evt_data.handle = p_cb->handle;
-        void* user_data = p_pcb->user_data;
-        evt_data.status = bta_jv_free_rfc_listen_cb(p_cb);
-        evt_data.async = FALSE;
-
-        /* occasionally when shutting down stack the callback is already
-           freed, hence make sure we check for this condition (pending investigatation
-           of rootcause) */
-        APPL_TRACE_DEBUG2("send BTA_JV_RFCOMM_CLOSE_EVT, p_cback:%p, user_data:%d", p_cback, (int)user_data);
-        if(p_cback)
-             p_cback(BTA_JV_RFCOMM_CLOSE_EVT, (tBTA_JV *)&evt_data, user_data);
-        else
-        {
-            APPL_TRACE_DEBUG0("bta_jv_rfcomm_stop_server, p_cback is null");
-        }
-    }
- }
+    void* user_data = ls->user_data;
+    if(!find_rfc_pcb(user_data, &p_cb, &p_pcb))
+        return;
+    APPL_TRACE_DEBUG2("bta_jv_rfcomm_stop_server: p_pcb:%p, p_pcb->port_handle:%d",
+                        p_pcb, p_pcb->port_handle);
+    bta_jv_free_rfc_cb(p_cb, p_pcb);
+    APPL_TRACE_DEBUG2("bta_jv_rfcomm_stop_server: sec id in use:%d, rfc_cb in use:%d", 
+                get_sec_id_used(), get_rfc_cb_used());
+}
 
 /*******************************************************************************
 **
index 8953886..c1dbe16 100644 (file)
@@ -1358,7 +1358,7 @@ tBTA_JV_STATUS BTA_JvRfcommConnect(tBTA_SEC sec_mask,
 **                  BTA_JV_FAILURE, otherwise.
 **
 *******************************************************************************/
-tBTA_JV_STATUS BTA_JvRfcommClose(UINT32 handle)
+tBTA_JV_STATUS BTA_JvRfcommClose(UINT32 handle, void *user_data)
 {
     tBTA_JV_STATUS status = BTA_JV_FAILURE;
     tBTA_JV_API_RFCOMM_CLOSE *p_msg;
@@ -1374,6 +1374,7 @@ tBTA_JV_STATUS BTA_JvRfcommClose(UINT32 handle)
         p_msg->handle = handle;
         p_msg->p_cb = &bta_jv_cb.rfc_cb[hi];
         p_msg->p_pcb = &bta_jv_cb.port_cb[p_msg->p_cb->rfc_hdl[si] - 1];
+        p_msg->user_data = user_data;
         bta_sys_sendmsg(p_msg);
         status = BTA_JV_SUCCESS;
     }
@@ -1439,7 +1440,7 @@ tBTA_JV_STATUS BTA_JvRfcommStartServer(tBTA_SEC sec_mask,
 **                  BTA_JV_FAILURE, otherwise.
 **
 *******************************************************************************/
-tBTA_JV_STATUS BTA_JvRfcommStopServer(UINT32 handle)
+tBTA_JV_STATUS BTA_JvRfcommStopServer(UINT32 handle, void * user_data)
 {
     tBTA_JV_STATUS status = BTA_JV_FAILURE;
     tBTA_JV_API_RFCOMM_SERVER *p_msg;
@@ -1448,6 +1449,7 @@ tBTA_JV_STATUS BTA_JvRfcommStopServer(UINT32 handle)
     {
         p_msg->hdr.event = BTA_JV_API_RFCOMM_STOP_SERVER_EVT;
         p_msg->rfc_handle = handle;
+        p_msg->user_data = user_data; //caller's private data
         bta_sys_sendmsg(p_msg);
         status = BTA_JV_SUCCESS;
     }
index 5a0aaed..493a96d 100644 (file)
@@ -196,6 +196,7 @@ typedef struct
     UINT8               handle;     /* index: the handle reported to java app */
     UINT8               scn;        /* the scn of the server */
     UINT8               max_sess;   /* max sessions */
+    int                 curr_sess;   /* current sessions count*/
 } tBTA_JV_RFC_CB;
 
 /* data type for BTA_JV_API_L2CAP_CONNECT_EVT */
@@ -307,6 +308,7 @@ typedef struct
     UINT16          handle;
     tBTA_JV_RFC_CB  *p_cb;
     tBTA_JV_PCB     *p_pcb;
+    void        *user_data;
 } tBTA_JV_API_RFCOMM_CLOSE;
 
 /* data type for BTA_JV_API_CREATE_RECORD_EVT */
index 0e52c1b..9d7f9bd 100644 (file)
@@ -291,12 +291,11 @@ static inline rfc_slot_t* create_srv_accept_rfc_slot(rfc_slot_t* srv_rs, const b
     accept_rs->role = srv_rs->role;
     accept_rs->rfc_handle = open_handle;
     accept_rs->rfc_port_handle = BTA_JvRfcommGetPortHdl(open_handle);
-    asrt(accept_rs->rfc_handle == srv_rs->rfc_handle);
-    asrt(accept_rs->rfc_port_handle == srv_rs->rfc_port_handle);
-    //now update listen handle of server slot
+     //now update listen handle of server slot
     srv_rs->rfc_handle = new_listen_handle;
-    srv_rs->rfc_port_handle =  BTA_JvRfcommGetPortHdl(new_listen_handle);
-    //now swap the slot id
+    srv_rs->rfc_port_handle = BTA_JvRfcommGetPortHdl(new_listen_handle);
+    asrt(accept_rs->rfc_port_handle != srv_rs->rfc_port_handle);
+  //now swap the slot id
     uint32_t new_listen_id = accept_rs->id;
     accept_rs->id = srv_rs->id;
     srv_rs->id = new_listen_id;
@@ -471,7 +470,7 @@ static inline void free_rfc_slot_scn(rfc_slot_t* rs)
     {
         if(rs->f.server && !rs->f.closing && rs->rfc_handle)
         {
-            BTA_JvRfcommStopServer(rs->rfc_handle);
+            BTA_JvRfcommStopServer(rs->rfc_handle, (void*)rs->id);
             rs->rfc_handle = 0;
         }
         if(rs->f.server)
@@ -501,7 +500,7 @@ static void cleanup_rfc_slot(rfc_slot_t* rs)
     if(rs->rfc_handle && !rs->f.closing && !rs->f.server)
     {
         APPL_TRACE_DEBUG1("closing rfcomm connection, rfc_handle:%d", rs->rfc_handle);
-        BTA_JvRfcommClose(rs->rfc_handle);
+        BTA_JvRfcommClose(rs->rfc_handle, (void*)rs->id);
         rs->rfc_handle = 0;
     }
     free_rfc_slot_scn(rs);
@@ -644,7 +643,6 @@ static void on_rfc_close(tBTA_JV_RFCOMM_CLOSE * p_close, uint32_t id)
                          rs->id, rs->fd, rs->scn, rs->f.server);
         free_rfc_slot_scn(rs);
         //rfc_handle already closed when receiving rfcomm close event from stack.
-        rs->rfc_handle = 0;
         rs->f.connected = FALSE;
         cleanup_rfc_slot(rs);
     }
@@ -881,7 +879,13 @@ void btsock_rfc_signaled(int fd, int flags, uint32_t user_id)
             if(!rs->f.server)
             {
                 if(rs->f.connected)
-                    BTA_JvRfcommWrite(rs->rfc_handle, (UINT32)rs->id);
+                {
+                    int size = 0;
+                    //make sure there's data pending in case the peer closed the socket
+                    if(!(flags & SOCK_THREAD_FD_EXCEPTION) ||
+                                (ioctl(rs->fd, FIONREAD, &size) == 0 && size))
+                        BTA_JvRfcommWrite(rs->rfc_handle, (UINT32)rs->id);
+                }
                 else
                 {
                     APPL_TRACE_ERROR2("SOCK_THREAD_FD_RD signaled when rfc is not connected, \
@@ -903,12 +907,17 @@ void btsock_rfc_signaled(int fd, int flags, uint32_t user_id)
         }
         if(need_close || (flags & SOCK_THREAD_FD_EXCEPTION))
         {
-            APPL_TRACE_DEBUG1("SOCK_THREAD_FD_EXCEPTION, flags:%x", flags);
-            rs->f.closing = TRUE;
-            if(rs->f.server)
-                BTA_JvRfcommStopServer(rs->rfc_handle);
+            int size = 0;
+            if(need_close || ioctl(rs->fd, FIONREAD, &size) != 0 || size == 0 )
+            {
+                //cleanup when no data pending
+                APPL_TRACE_DEBUG3("SOCK_THREAD_FD_EXCEPTION, cleanup, flags:%x, need_close:%d, pending size:%d",
+                                flags, need_close, size);
+                cleanup_rfc_slot(rs);
+            }
             else
-                BTA_JvRfcommClose(rs->rfc_handle);
+                APPL_TRACE_DEBUG3("SOCK_THREAD_FD_EXCEPTION, cleanup pending, flags:%x, need_close:%d, pending size:%d",
+                                flags, need_close, size);
         }
     }
     unlock_slot(&slot_lock);
@@ -967,6 +976,7 @@ int bta_co_rfc_data_outgoing_size(void *user_data, int *size)
             cleanup_rfc_slot(rs);
         }
     }
+    else APPL_TRACE_ERROR1("bta_co_rfc_data_outgoing_size, invalid slot id:%d", id);
     unlock_slot(&slot_lock);
     return ret;
 }
@@ -988,6 +998,7 @@ int bta_co_rfc_data_outgoing(void *user_data, UINT8* buf, UINT16 size)
             cleanup_rfc_slot(rs);
         }
     }
+    else APPL_TRACE_ERROR1("bta_co_rfc_data_outgoing, invalid slot id:%d", id);
     unlock_slot(&slot_lock);
     return ret;
 }
index 2f64932..050ee52 100644 (file)
@@ -260,6 +260,16 @@ RFC_API extern int RFCOMM_RemoveServer (UINT16 handle);
 RFC_API extern int PORT_SetEventCallback (UINT16 port_handle,
                                           tPORT_CALLBACK *p_port_cb);
 
+/*******************************************************************************
+**
+** Function         PORT_ClearKeepHandleFlag
+**
+** Description      This function is called to clear the keep handle flag
+**                  which will cause not to keep the port handle open when closed
+** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
+**
+*******************************************************************************/
+int PORT_ClearKeepHandleFlag (UINT16 port_handle);
 
 /*******************************************************************************
 **
index 617d31b..325d8fa 100644 (file)
@@ -84,8 +84,6 @@ int RFCOMM_CreateConnection (UINT16 uuid, UINT8 scn, BOOLEAN is_server,
     tRFC_MCB   *p_mcb = port_find_mcb (bd_addr);
     UINT16     rfcomm_mtu;
 
-    RFCOMM_TRACE_API3 ("RFCOMM_CreateConnection() called SCN: %d is_server:%d mtu:%d",
-                       scn, is_server, mtu);
     RFCOMM_TRACE_API6 ("RFCOMM_CreateConnection()  BDA: %02x-%02x-%02x-%02x-%02x-%02x",
                        bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
 
@@ -104,6 +102,8 @@ int RFCOMM_CreateConnection (UINT16 uuid, UINT8 scn, BOOLEAN is_server,
         dlci = (scn << 1) + 1;
     else
         dlci = (scn << 1);
+    RFCOMM_TRACE_API5("RFCOMM_CreateConnection(): scn:%d, dlci:%d, is_server:%d mtu:%d, p_mcb:%p",
+                       scn, dlci, is_server, mtu, p_mcb);
 
     /* For the server side always allocate a new port.  On the client side */
     /* do not allow the same (dlci, bd_addr) to be opened twice by application */
@@ -123,6 +123,8 @@ int RFCOMM_CreateConnection (UINT16 uuid, UINT8 scn, BOOLEAN is_server,
         RFCOMM_TRACE_WARNING0 ("RFCOMM_CreateConnection - no resources");
         return (PORT_NO_RESOURCES);
     }
+   RFCOMM_TRACE_API6("RFCOMM_CreateConnection(): scn:%d, dlci:%d, is_server:%d mtu:%d, p_mcb:%p, p_port:%p",
+                       scn, dlci, is_server, mtu, p_mcb, p_port);
 
     p_port->default_signal_state = (PORT_DTRDSR_ON | PORT_CTSRTS_ON | PORT_DCD_ON);
 
@@ -309,7 +311,30 @@ int PORT_SetEventCallback (UINT16 port_handle, tPORT_CALLBACK *p_port_cb)
 
     return (PORT_SUCCESS);
 }
+/*******************************************************************************
+**
+** Function         PORT_ClearKeepHandleFlag
+**
+** Description      This function is called to clear the keep handle flag
+**                  which will cause not to keep the port handle open when closed
+** Parameters:      handle     - Handle returned in the RFCOMM_CreateConnection
+**
+*******************************************************************************/
+
+int PORT_ClearKeepHandleFlag (UINT16 port_handle)
+{
+    tPORT  *p_port;
 
+    /* Check if handle is valid to avoid crashing */
+    if ((port_handle == 0) || (port_handle > MAX_RFC_PORTS))
+    {
+        return (PORT_BAD_HANDLE);
+    }
+
+    p_port = &rfc_cb.port.port[port_handle - 1];
+    p_port->keep_port_handle = 0;
+    return (PORT_SUCCESS);
+}
 
 /*******************************************************************************
 **
index 8980330..3f697e1 100644 (file)
@@ -55,7 +55,7 @@ int port_open_continue (tPORT *p_port)
 {
     tRFC_MCB *p_mcb;
 
-    RFCOMM_TRACE_EVENT0 ("port_open_continue");
+    RFCOMM_TRACE_EVENT1 ("port_open_continue, p_port:%p", p_port);
 
     /* Check if multiplexer channel has already been established */
     if ((p_mcb = rfc_alloc_multiplexer_channel (p_port->bd_addr, TRUE)) == NULL)
@@ -271,6 +271,7 @@ void PORT_StartInd (tRFC_MCB *p_mcb)
         if ((p_port->rfc.p_mcb == NULL)
          || (p_port->rfc.p_mcb == p_mcb))
         {
+            RFCOMM_TRACE_DEBUG1("PORT_StartInd, RFCOMM_StartRsp RFCOMM_SUCCESS: p_mcb:%p", p_mcb);
             RFCOMM_StartRsp (p_mcb, RFCOMM_SUCCESS);
             return;
         }
@@ -445,7 +446,10 @@ void PORT_DlcEstablishInd (tRFC_MCB *p_mcb, UINT8 dlci, UINT16 mtu)
 {
     tPORT *p_port = port_find_mcb_dlci_port (p_mcb, dlci);
 
-    RFCOMM_TRACE_EVENT2 ("PORT_DlcEstablishInd dlci:%d mtu:%d", dlci, mtu);
+    RFCOMM_TRACE_DEBUG4 ("PORT_DlcEstablishInd p_mcb:%p, dlci:%d mtu:%di, p_port:%p", p_mcb, dlci, mtu, p_port);
+    RFCOMM_TRACE_DEBUG6 ("PORT_DlcEstablishInd p_mcb addr:%02x:%02x:%02x:%02x:%02x:%02x",
+                         p_mcb->bd_addr[0], p_mcb->bd_addr[1], p_mcb->bd_addr[2],
+                         p_mcb->bd_addr[3], p_mcb->bd_addr[4], p_mcb->bd_addr[5]);
 
     if (!p_port)
     {
@@ -826,7 +830,8 @@ void PORT_DataInd (tRFC_MCB *p_mcb, UINT8 dlci, BT_HDR *p_buf)
     UINT8  *p;
     int    i;
 
-    RFCOMM_TRACE_EVENT1 ("PORT_DataInd with data length %d", p_buf->len);
+    RFCOMM_TRACE_EVENT4("PORT_DataInd with data length %d, p_mcb:%p,p_port:%p,dlci:%d",
+                        p_buf->len, p_mcb, p_port, dlci);
     if (!p_port)
     {
         GKI_freebuf (p_buf);
@@ -843,7 +848,7 @@ void PORT_DataInd (tRFC_MCB *p_mcb, UINT8 dlci, BT_HDR *p_buf)
         //GKI_freebuf (p_buf);
         return;
     }
-
+    else RFCOMM_TRACE_ERROR1("PORT_DataInd, p_port:%p, p_data_co_callback is null", p_port);
     /* If client registered callback we can just deliver receive data */
     if (p_port->p_data_callback)
     {
index fe2eb60..0f7c03b 100644 (file)
@@ -85,7 +85,9 @@ tPORT *port_allocate_port (UINT8 dlci, BD_ADDR bd_addr)
             port_set_defaults (p_port);
 
             rfc_cb.rfc.last_port = yy;
-            RFCOMM_TRACE_DEBUG2("rfc_cb.port.port[%d] allocated, last_port:%d", yy, rfc_cb.rfc.last_port);
+            RFCOMM_TRACE_DEBUG3("rfc_cb.port.port[%d]:%p allocated, last_port:%d", yy, p_port, rfc_cb.rfc.last_port);
+            RFCOMM_TRACE_DEBUG6("port_allocate_port: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]);
             return (p_port);
         }
     }
@@ -236,7 +238,7 @@ void port_release_port (tPORT *p_port)
             rfc_check_mcb_active (p_port->rfc.p_mcb);
         }
         rfc_port_timer_stop (p_port);
-
+        RFCOMM_TRACE_DEBUG1 ("port_release_port:p_port->keep_port_handle:%d", p_port->keep_port_handle);
         if( p_port->keep_port_handle )
         {
             RFCOMM_TRACE_DEBUG1 ("port_release_port:Initialize handle:%d", p_port->inx);
@@ -287,9 +289,15 @@ tRFC_MCB *port_find_mcb (BD_ADDR bd_addr)
          && !memcmp (rfc_cb.port.rfc_mcb[i].bd_addr, bd_addr, BD_ADDR_LEN))
         {
             /* Multiplexer channel found do not change anything */
+            RFCOMM_TRACE_DEBUG6("port_find_mcb: found  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]);
+            RFCOMM_TRACE_DEBUG3("port_find_mcb: rfc_cb.port.rfc_mcb:index:%d, %p, lcid:%d",
+                                i, &rfc_cb.port.rfc_mcb[i], rfc_cb.port.rfc_mcb[i].lcid);
             return (&rfc_cb.port.rfc_mcb[i]);
         }
     }
+    RFCOMM_TRACE_DEBUG6("port_find_mcb: not found, 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]);
     return (NULL);
 }
 
@@ -318,7 +326,10 @@ tPORT *port_find_mcb_dlci_port (tRFC_MCB *p_mcb, UINT8 dlci)
 
     inx = p_mcb->port_inx[dlci];
     if (inx == 0)
+    {
+        RFCOMM_TRACE_DEBUG2("port_find_mcb_dlci_port: p_mcb:%p, port_inx[dlci:%d] is 0", p_mcb, dlci);
         return (NULL);
+    }
     else
         return (&rfc_cb.port.port[inx - 1]);
 }
@@ -341,6 +352,7 @@ tPORT *port_find_dlci_port (UINT8 dlci)
     for (i = 0; i < MAX_RFC_PORTS; i++)
     {
         p_port = &rfc_cb.port.port[i];
+
         if (p_port->in_use && (p_port->rfc.p_mcb == NULL))
         {
             if (p_port->dlci == dlci)
index 49405ac..2b850fa 100644 (file)
@@ -133,9 +133,19 @@ tRFC_MCB *rfc_alloc_multiplexer_channel (BD_ADDR bd_addr, BOOLEAN is_initiator)
 {
     int i, j;
     tRFC_MCB *p_mcb = NULL;
+    RFCOMM_TRACE_DEBUG6("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]);
+    RFCOMM_TRACE_DEBUG1("rfc_alloc_multiplexer_channel:is_initiator:%d", is_initiator);
 
     for (i = 0; i < MAX_BD_CONNECTIONS; i++)
     {
+        RFCOMM_TRACE_DEBUG2("rfc_alloc_multiplexer_channel rfc_cb.port.rfc_mcb[%d].state:%d",
+                            i, rfc_cb.port.rfc_mcb[i].state);
+        RFCOMM_TRACE_DEBUG6("(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]);
+
         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)))
         {
@@ -143,6 +153,8 @@ tRFC_MCB *rfc_alloc_multiplexer_channel (BD_ADDR bd_addr, BOOLEAN is_initiator)
             /* If there was an inactivity timer running stop it now */
             if (rfc_cb.port.rfc_mcb[i].state == RFC_MX_STATE_CONNECTED)
                 rfc_timer_stop (&rfc_cb.port.rfc_mcb[i]);
+            RFCOMM_TRACE_DEBUG3("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]);
             return (&rfc_cb.port.rfc_mcb[i]);
         }
     }
@@ -159,6 +171,8 @@ tRFC_MCB *rfc_alloc_multiplexer_channel (BD_ADDR bd_addr, BOOLEAN is_initiator)
             /* New multiplexer control block */
             memset (p_mcb, 0, sizeof (tRFC_MCB));
             memcpy (p_mcb->bd_addr, bd_addr, BD_ADDR_LEN);
+            RFCOMM_TRACE_DEBUG3("rfc_alloc_multiplexer_channel:is_initiator:%d, create new p_mcb:%p, index:%d",
+                                is_initiator, &rfc_cb.port.rfc_mcb[j], j);
 
             GKI_init_q(&p_mcb->cmd_q);