OSDN Git Service

DO NOT MERGE Fix unexpected behavior in SDP
[android-x86/system-bt.git] / stack / gatt / gatt_utils.c
index f993fa1..c2db770 100644 (file)
@@ -79,6 +79,7 @@ const char * const op_code_name[] =
 static const UINT8  base_uuid[LEN_UUID_128] = {0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80,
     0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
 
+extern fixed_queue_t *btu_general_alarm_queue;
 
 /*******************************************************************************
 **
@@ -98,7 +99,7 @@ void gatt_free_pending_ind(tGATT_TCB *p_tcb)
 
     /* release all queued indications */
     while (!fixed_queue_is_empty(p_tcb->pending_ind_q))
-        osi_freebuf(fixed_queue_try_dequeue(p_tcb->pending_ind_q));
+        osi_free(fixed_queue_try_dequeue(p_tcb->pending_ind_q));
     fixed_queue_free(p_tcb->pending_ind_q, NULL);
     p_tcb->pending_ind_q = NULL;
 }
@@ -121,7 +122,7 @@ void gatt_free_pending_enc_queue(tGATT_TCB *p_tcb)
 
     /* release all queued indications */
     while (!fixed_queue_is_empty(p_tcb->pending_enc_clcb))
-        osi_freebuf(fixed_queue_try_dequeue(p_tcb->pending_enc_clcb));
+        osi_free(fixed_queue_try_dequeue(p_tcb->pending_enc_clcb));
     fixed_queue_free(p_tcb->pending_enc_clcb, NULL);
     p_tcb->pending_enc_clcb = NULL;
 }
@@ -149,7 +150,7 @@ void gatt_delete_dev_from_srv_chg_clt_list(BD_ADDR bd_addr)
             memcpy(req.srv_chg.bda, bd_addr, BD_ADDR_LEN);
             (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_REMOVE_CLIENT,&req, NULL);
         }
-        osi_freebuf(fixed_queue_try_remove_from_queue(gatt_cb.srv_chg_clt_q,
+        osi_free(fixed_queue_try_remove_from_queue(gatt_cb.srv_chg_clt_q,
                                                       p_buf));
     }
 }
@@ -232,19 +233,17 @@ tGATTS_PENDING_NEW_SRV_START *gatt_sr_is_new_srv_chg(tBT_UUID *p_app_uuid128, tB
 *******************************************************************************/
 tGATT_VALUE *gatt_add_pending_ind(tGATT_TCB  *p_tcb, tGATT_VALUE *p_ind)
 {
+    tGATT_VALUE *p_buf = (tGATT_VALUE *)osi_malloc(sizeof(tGATT_VALUE));
+
     GATT_TRACE_DEBUG("%s", __func__);
+    GATT_TRACE_DEBUG("enqueue a pending indication");
+
+    memcpy(p_buf, p_ind, sizeof(tGATT_VALUE));
+    fixed_queue_enqueue(p_tcb->pending_ind_q, p_buf);
 
-    tGATT_VALUE *p_buf = (tGATT_VALUE *)osi_getbuf((UINT16)sizeof(tGATT_VALUE));
-    if (p_buf != NULL)
-    {
-        GATT_TRACE_DEBUG("enqueue a pending indication");
-        memcpy(p_buf, p_ind, sizeof(tGATT_VALUE));
-        fixed_queue_enqueue(p_tcb->pending_ind_q, p_buf);
-    }
     return p_buf;
 }
 
-
 /*******************************************************************************
 **
 ** Function     gatt_add_pending_new_srv_start
@@ -256,16 +255,15 @@ tGATT_VALUE *gatt_add_pending_ind(tGATT_TCB  *p_tcb, tGATT_VALUE *p_ind)
 *******************************************************************************/
 tGATTS_PENDING_NEW_SRV_START *gatt_add_pending_new_srv_start(tGATTS_HNDL_RANGE *p_new_srv_start)
 {
+    tGATTS_PENDING_NEW_SRV_START *p_buf =
+        (tGATTS_PENDING_NEW_SRV_START *)osi_malloc(sizeof(tGATTS_PENDING_NEW_SRV_START));
+
     GATT_TRACE_DEBUG("%s", __func__);
+    GATT_TRACE_DEBUG("enqueue a new pending new srv start");
+
+    p_buf->p_new_srv_start = p_new_srv_start;
+    fixed_queue_enqueue(gatt_cb.pending_new_srv_start_q, p_buf);
 
-    tGATTS_PENDING_NEW_SRV_START *p_buf =
-        (tGATTS_PENDING_NEW_SRV_START *)osi_getbuf((UINT16)sizeof(tGATTS_PENDING_NEW_SRV_START));
-    if (p_buf != NULL)
-    {
-        GATT_TRACE_DEBUG("enqueue a new pending new srv start");
-        p_buf->p_new_srv_start = p_new_srv_start;
-        fixed_queue_enqueue(gatt_cb.pending_new_srv_start_q, p_buf);
-    }
     return p_buf;
 }
 
@@ -281,20 +279,17 @@ tGATTS_PENDING_NEW_SRV_START *gatt_add_pending_new_srv_start(tGATTS_HNDL_RANGE *
 *******************************************************************************/
 tGATTS_SRV_CHG *gatt_add_srv_chg_clt(tGATTS_SRV_CHG *p_srv_chg)
 {
-    GATT_TRACE_DEBUG ("%s", __func__);
+    tGATTS_SRV_CHG *p_buf = (tGATTS_SRV_CHG *)osi_malloc(sizeof(tGATTS_SRV_CHG));
 
-    tGATTS_SRV_CHG *p_buf = (tGATTS_SRV_CHG *)osi_getbuf((UINT16)sizeof(tGATTS_SRV_CHG));
-    if (p_buf != NULL)
-    {
-        GATT_TRACE_DEBUG ("enqueue a srv chg client");
-        memcpy(p_buf, p_srv_chg, sizeof(tGATTS_SRV_CHG));
-        fixed_queue_enqueue(gatt_cb.srv_chg_clt_q, p_buf);
-    }
+    GATT_TRACE_DEBUG("%s", __func__);
+    GATT_TRACE_DEBUG("enqueue a srv chg client");
+
+    memcpy(p_buf, p_srv_chg, sizeof(tGATTS_SRV_CHG));
+    fixed_queue_enqueue(gatt_cb.srv_chg_clt_q, p_buf);
 
     return p_buf;
 }
 
-
 /*******************************************************************************
 **
 ** Function     gatt_alloc_hdl_buffer
@@ -396,7 +391,7 @@ void gatt_free_hdl_buffer(tGATT_HDL_LIST_ELEM *p)
     if (p)
     {
         while (!fixed_queue_is_empty(p->svc_db.svc_buffer))
-            osi_freebuf(fixed_queue_try_dequeue(p->svc_db.svc_buffer));
+            osi_free(fixed_queue_try_dequeue(p->svc_db.svc_buffer));
         fixed_queue_free(p->svc_db.svc_buffer, NULL);
         memset(p, 0, sizeof(tGATT_HDL_LIST_ELEM));
     }
@@ -421,7 +416,7 @@ void gatt_free_srvc_db_buffer_app_id(tBT_UUID *p_app_id)
         if (memcmp(p_app_id, &p_elem->asgn_range.app_uuid128, sizeof(tBT_UUID)) == 0)
         {
             while (!fixed_queue_is_empty(p_elem->svc_db.svc_buffer))
-                osi_freebuf(fixed_queue_try_dequeue(p_elem->svc_db.svc_buffer));
+                osi_free(fixed_queue_try_dequeue(p_elem->svc_db.svc_buffer));
             fixed_queue_free(p_elem->svc_db.svc_buffer, NULL);
             p_elem->svc_db.svc_buffer = NULL;
 
@@ -975,7 +970,8 @@ tGATT_TCB * gatt_allocate_tcb_by_bdaddr(BD_ADDR bda, tBT_TRANSPORT transport)
             memset(p_tcb, 0, sizeof(tGATT_TCB));
             p_tcb->pending_enc_clcb = fixed_queue_new(SIZE_MAX);
             p_tcb->pending_ind_q = fixed_queue_new(SIZE_MAX);
-            p_tcb->sr_cmd.multi_rsp_q = fixed_queue_new(SIZE_MAX);
+            p_tcb->conf_timer = alarm_new("gatt.conf_timer");
+            p_tcb->ind_ack_timer = alarm_new("gatt.ind_ack_timer");
             p_tcb->in_use = TRUE;
             p_tcb->tcb_idx = i;
             p_tcb->transport = transport;
@@ -1198,37 +1194,44 @@ BOOLEAN gatt_parse_uuid_from_cmd(tBT_UUID *p_uuid_rec, UINT16 uuid_size, UINT8 *
 **
 ** Description      Start a wait_for_response timer.
 **
-** Returns          TRUE if command sent, otherwise FALSE.
+** Returns          void
 **
 *******************************************************************************/
 void gatt_start_rsp_timer(UINT16 clcb_idx)
 {
     tGATT_CLCB *p_clcb = &gatt_cb.clcb[clcb_idx];
-    UINT32 timeout = GATT_WAIT_FOR_RSP_TOUT;
-    p_clcb->rsp_timer_ent.param  = (timer_param_t)p_clcb;
+    period_ms_t timeout_ms = GATT_WAIT_FOR_RSP_TIMEOUT_MS;
+
     if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY &&
-        p_clcb->op_subtype == GATT_DISC_SRVC_ALL)
-    {
-        timeout = GATT_WAIT_FOR_DISC_RSP_TOUT;
+        p_clcb->op_subtype == GATT_DISC_SRVC_ALL) {
+        timeout_ms = GATT_WAIT_FOR_DISC_RSP_TIMEOUT_MS;
     }
-    btu_start_timer (&p_clcb->rsp_timer_ent, BTU_TTYPE_ATT_WAIT_FOR_RSP,
-                     timeout);
+
+    // TODO: The tGATT_CLCB memory and state management needs cleanup,
+    // and then the timers can be allocated elsewhere.
+    if (p_clcb->gatt_rsp_timer_ent == NULL) {
+        p_clcb->gatt_rsp_timer_ent = alarm_new("gatt.gatt_rsp_timer_ent");
+    }
+    alarm_set_on_queue(p_clcb->gatt_rsp_timer_ent, timeout_ms,
+                       gatt_rsp_timeout, p_clcb, btu_general_alarm_queue);
 }
+
 /*******************************************************************************
 **
 ** Function         gatt_start_conf_timer
 **
 ** Description      Start a wait_for_confirmation timer.
 **
-** Returns          TRUE if command sent, otherwise FALSE.
+** Returns          void
 **
 *******************************************************************************/
-void gatt_start_conf_timer(tGATT_TCB    *p_tcb)
+void gatt_start_conf_timer(tGATT_TCB *p_tcb)
 {
-    p_tcb->conf_timer_ent.param  = (timer_param_t)p_tcb;
-    btu_start_timer (&p_tcb->conf_timer_ent, BTU_TTYPE_ATT_WAIT_FOR_RSP,
-                     GATT_WAIT_FOR_RSP_TOUT);
+    alarm_set_on_queue(p_tcb->conf_timer, GATT_WAIT_FOR_RSP_TIMEOUT_MS,
+                       gatt_indication_confirmation_timeout, p_tcb,
+                       btu_general_alarm_queue);
 }
+
 /*******************************************************************************
 **
 ** Function         gatt_start_ind_ack_timer
@@ -1240,12 +1243,11 @@ void gatt_start_conf_timer(tGATT_TCB    *p_tcb)
 *******************************************************************************/
 void gatt_start_ind_ack_timer(tGATT_TCB *p_tcb)
 {
-    p_tcb->ind_ack_timer_ent.param  = (timer_param_t)p_tcb;
     /* start notification cache timer */
-    btu_start_timer (&p_tcb->ind_ack_timer_ent, BTU_TTYPE_ATT_WAIT_FOR_IND_ACK,
-                     GATT_WAIT_FOR_RSP_TOUT);
-
+    alarm_set_on_queue(p_tcb->ind_ack_timer, GATT_WAIT_FOR_RSP_TIMEOUT_MS,
+                       gatt_ind_ack_timeout, p_tcb, btu_general_alarm_queue);
 }
+
 /*******************************************************************************
 **
 ** Function         gatt_rsp_timeout
@@ -1255,12 +1257,13 @@ void gatt_start_ind_ack_timer(tGATT_TCB *p_tcb)
 ** Returns          void
 **
 *******************************************************************************/
-void gatt_rsp_timeout(timer_entry_t *p_te)
+void gatt_rsp_timeout(void *data)
 {
-    tGATT_CLCB *p_clcb = (tGATT_CLCB *)p_te->param;
+    tGATT_CLCB *p_clcb = (tGATT_CLCB *)data;
+
     if (p_clcb == NULL || p_clcb->p_tcb == NULL)
     {
-        GATT_TRACE_WARNING("gatt_rsp_timeout clcb is already deleted");
+        GATT_TRACE_WARNING("%s clcb is already deleted", __func__);
         return;
     }
     if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY &&
@@ -1268,10 +1271,11 @@ void gatt_rsp_timeout(timer_entry_t *p_te)
         p_clcb->retry_count < GATT_REQ_RETRY_LIMIT)
     {
         UINT8 rsp_code;
-        GATT_TRACE_WARNING("gatt_rsp_timeout retry discovery primary service");
+        GATT_TRACE_WARNING("%s retry discovery primary service", __func__);
         if (p_clcb != gatt_cmd_dequeue(p_clcb->p_tcb, &rsp_code))
         {
-            GATT_TRACE_ERROR("gatt_rsp_timeout command queue out of sync, disconnect");
+            GATT_TRACE_ERROR("%s command queue out of sync, disconnect",
+                             __func__);
         }
         else
         {
@@ -1281,12 +1285,29 @@ void gatt_rsp_timeout(timer_entry_t *p_te)
         }
     }
 
-    GATT_TRACE_WARNING("gatt_rsp_timeout disconnecting...");
+    GATT_TRACE_WARNING("%s disconnecting...", __func__);
     gatt_disconnect (p_clcb->p_tcb);
 }
 
 /*******************************************************************************
 **
+** Function         gatt_indication_confirmation_timeout
+**
+** Description      Called when the indication confirmation timer expires
+**
+** Returns          void
+**
+*******************************************************************************/
+void gatt_indication_confirmation_timeout(void *data)
+{
+    tGATT_TCB *p_tcb = (tGATT_TCB *)data;
+
+    GATT_TRACE_WARNING("%s disconnecting...", __func__);
+    gatt_disconnect(p_tcb);
+}
+
+/*******************************************************************************
+**
 ** Function         gatt_ind_ack_timeout
 **
 ** Description      Called when GATT wait for ATT handle confirmation timeout
@@ -1294,16 +1315,16 @@ void gatt_rsp_timeout(timer_entry_t *p_te)
 ** Returns          void
 **
 *******************************************************************************/
-void gatt_ind_ack_timeout(timer_entry_t *p_te)
+void gatt_ind_ack_timeout(void *data)
 {
-    tGATT_TCB * p_tcb = (tGATT_TCB *)p_te->param;
+    tGATT_TCB *p_tcb = (tGATT_TCB *)data;
 
-    GATT_TRACE_WARNING("gatt_ind_ack_timeout send ack now");
+    GATT_TRACE_WARNING("%s send ack now", __func__);
 
     if (p_tcb != NULL)
         p_tcb->ind_count = 0;
 
-    attp_send_cl_msg(((tGATT_TCB *)p_te->param), 0, GATT_HANDLE_VALUE_CONF, NULL);
+    attp_send_cl_msg(p_tcb, 0, GATT_HANDLE_VALUE_CONF, NULL);
 }
 /*******************************************************************************
 **
@@ -1687,6 +1708,7 @@ void gatt_clcb_dealloc (tGATT_CLCB *p_clcb)
 {
     if (p_clcb && p_clcb->in_use)
     {
+        alarm_free(p_clcb->gatt_rsp_timer_ent);
         memset(p_clcb, 0, sizeof(tGATT_CLCB));
     }
 }
@@ -2198,14 +2220,11 @@ void gatt_end_operation(tGATT_CLCB *p_clcb, tGATT_STATUS status, void *p_data)
         }
     }
 
-    if (p_clcb->p_attr_buf)
-    {
-        osi_freebuf(p_clcb->p_attr_buf);
-    }
+    osi_free_and_reset((void **)&p_clcb->p_attr_buf);
 
     operation =  p_clcb->operation;
     conn_id = p_clcb->conn_id;
-    btu_stop_timer(&p_clcb->rsp_timer_ent);
+    alarm_cancel(p_clcb->gatt_rsp_timer_ent);
 
     gatt_clcb_dealloc(p_clcb);
 
@@ -2248,7 +2267,7 @@ void gatt_cleanup_upon_disc(BD_ADDR bda, UINT16 reason, tBT_TRANSPORT transport)
             p_clcb = &gatt_cb.clcb[i];
             if (p_clcb->in_use && p_clcb->p_tcb == p_tcb)
             {
-                btu_stop_timer(&p_clcb->rsp_timer_ent);
+                alarm_cancel(p_clcb->gatt_rsp_timer_ent);
                 GATT_TRACE_DEBUG ("found p_clcb conn_id=%d clcb_idx=%d", p_clcb->conn_id, p_clcb->clcb_idx);
                 if (p_clcb->operation != GATTC_OPTYPE_NONE)
                     gatt_end_operation(p_clcb, GATT_ERROR, NULL);
@@ -2258,11 +2277,14 @@ void gatt_cleanup_upon_disc(BD_ADDR bda, UINT16 reason, tBT_TRANSPORT transport)
             }
         }
 
-        btu_stop_timer (&p_tcb->ind_ack_timer_ent);
-        btu_stop_timer (&p_tcb->conf_timer_ent);
+        alarm_free(p_tcb->ind_ack_timer);
+        p_tcb->ind_ack_timer = NULL;
+        alarm_free(p_tcb->conf_timer);
+        p_tcb->conf_timer = NULL;
         gatt_free_pending_ind(p_tcb);
         gatt_free_pending_enc_queue(p_tcb);
         fixed_queue_free(p_tcb->sr_cmd.multi_rsp_q, NULL);
+        p_tcb->sr_cmd.multi_rsp_q = NULL;
 
         for (i = 0; i < GATT_MAX_APPS; i ++)
         {
@@ -2345,10 +2367,9 @@ void gatt_dbg_display_uuid(tBT_UUID bt_uuid)
                 bt_uuid.uu.uuid128[1], bt_uuid.uu.uuid128[0]);
     }
     else
-        BCM_STRNCPY_S(str_buf, sizeof(str_buf), "Unknown UUID 0", 15);
+        strlcpy(str_buf, "Unknown UUID 0", sizeof(str_buf));
 
     GATT_TRACE_DEBUG ("UUID=[%s]", str_buf);
-
 }
 
 
@@ -2771,16 +2792,15 @@ BOOLEAN gatt_update_auto_connect_dev (tGATT_IF gatt_if, BOOLEAN add, BD_ADDR bd_
 *******************************************************************************/
 tGATT_PENDING_ENC_CLCB* gatt_add_pending_enc_channel_clcb(tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb)
 {
-    GATT_TRACE_DEBUG ("gatt_add_pending_new_srv_start");
-
     tGATT_PENDING_ENC_CLCB *p_buf =
-        (tGATT_PENDING_ENC_CLCB *)osi_getbuf((UINT16)sizeof(tGATT_PENDING_ENC_CLCB));
-    if (p_buf != NULL)
-    {
-        GATT_TRACE_DEBUG("enqueue a new pending encryption channel clcb");
-        p_buf->p_clcb = p_clcb;
-        fixed_queue_enqueue(p_tcb->pending_enc_clcb, p_buf);
-    }
+        (tGATT_PENDING_ENC_CLCB *)osi_malloc(sizeof(tGATT_PENDING_ENC_CLCB));
+
+    GATT_TRACE_DEBUG ("%s", __func__);
+    GATT_TRACE_DEBUG("enqueue a new pending encryption channel clcb");
+
+    p_buf->p_clcb = p_clcb;
+    fixed_queue_enqueue(p_tcb->pending_enc_clcb, p_buf);
+
     return p_buf;
 }
 /*******************************************************************************