Update btif_profile_queue to use the list data structure.

The profile queue maintains a list of pending connect operations
for each profile. If a connect is followed by a disconnect before
the queued connect is dispatched, the disconnect will have no
effect and the connect will proceed.

This code clearly needs to be re-thought; it may be a good idea to
abandon the connect queue entirely in the long-run.

Change-Id: Ic0e85654abcf7a47f65953edb301eb9524394950
diff --git a/btif/include/btif_profile_queue.h b/btif/include/btif_profile_queue.h
index 931f457..e05c97a 100644
--- a/btif/include/btif_profile_queue.h
+++ b/btif/include/btif_profile_queue.h
@@ -27,10 +27,9 @@
 #ifndef BTIF_PROFILE_QUEUE_H
 #define BTIF_PROFILE_QUEUE_H
 
-typedef bt_status_t (btif_connect_cb_t) (bt_bdaddr_t *bda);
+typedef bt_status_t (*btif_connect_cb_t)(bt_bdaddr_t *bda);
 
-bt_status_t btif_queue_connect(uint16_t uuid, const bt_bdaddr_t *bda,
-                        btif_connect_cb_t *connect_cb);
+bt_status_t btif_queue_connect(uint16_t uuid, const bt_bdaddr_t *bda, btif_connect_cb_t connect_cb);
 void btif_queue_advance();
 void btif_queue_release();
 
diff --git a/btif/src/btif_profile_queue.c b/btif/src/btif_profile_queue.c
index 4af3b53..b9bbe17 100644
--- a/btif/src/btif_profile_queue.c
+++ b/btif/src/btif_profile_queue.c
@@ -30,6 +30,7 @@
 #include "btif_common.h"
 #include "btif_profile_queue.h"
 #include "gki.h"
+#include "list.h"
 
 /*******************************************************************************
 **  Local type definitions
@@ -37,81 +38,64 @@
 
 typedef enum {
   BTIF_QUEUE_CONNECT_EVT,
-  BTIF_QUEUE_ADVANCE_EVT
+  BTIF_QUEUE_ADVANCE_EVT,
 } btif_queue_event_t;
 
-typedef struct connect_node_tag
-{
+typedef struct {
     bt_bdaddr_t bda;
     uint16_t uuid;
-    uint16_t busy;
-    void *p_cb;
-    struct connect_node_tag *p_next;
-} __attribute__((packed))connect_node_t;
-
+    bool busy;
+    btif_connect_cb_t connect_cb;
+} connect_node_t;
 
 /*******************************************************************************
 **  Static variables
 *******************************************************************************/
 
-static connect_node_t *connect_queue;
-
+static list_t *connect_queue;
 
 /*******************************************************************************
 **  Queue helper functions
 *******************************************************************************/
 
-static void queue_int_add(connect_node_t *p_param)
-{
-    connect_node_t *p_list = connect_queue;
+static void queue_int_add(connect_node_t *p_param) {
     connect_node_t *p_node = GKI_getbuf(sizeof(connect_node_t));
     ASSERTC(p_node != NULL, "Failed to allocate new list node", 0);
 
     memcpy(p_node, p_param, sizeof(connect_node_t));
 
-    if (connect_queue == NULL)
-    {
-        connect_queue = p_node;
-        return;
+    if (!connect_queue) {
+        connect_queue = list_new(GKI_freebuf);
+        ASSERTC(connect_queue != NULL, "Failed to allocate list", 0);
     }
 
-    while (p_list->p_next)
-        p_list = p_list->p_next;
-    p_list->p_next = p_node;
+    list_append(connect_queue, p_node);
 }
 
-static void queue_int_advance()
-{
-    connect_node_t *p_head = connect_queue;
-    if (connect_queue == NULL)
-        return;
-
-    connect_queue = connect_queue->p_next;
-    GKI_freebuf(p_head);
+static void queue_int_advance() {
+    if (connect_queue && !list_is_empty(connect_queue))
+        list_remove(connect_queue, list_front(connect_queue));
 }
 
-static bt_status_t queue_int_connect_next()
-{
-    connect_node_t* p_head = connect_queue;
-
-    if (p_head == NULL)
+static bt_status_t queue_int_connect_next() {
+    if (!connect_queue || list_is_empty(connect_queue))
         return BT_STATUS_FAIL;
 
-    /* If the queue is currently busy, we return  success anyway,
-     * since the connection has been queued... */
-    if (p_head->busy != FALSE)
+    connect_node_t *p_head = list_front(connect_queue);
+
+    // If the queue is currently busy, we return success anyway,
+    // since the connection has been queued...
+    if (p_head->busy)
         return BT_STATUS_SUCCESS;
 
-    p_head->busy = TRUE;
-    return (*(btif_connect_cb_t*)p_head->p_cb)(&p_head->bda);
+    p_head->busy = true;
+    return p_head->connect_cb(&p_head->bda);
 }
 
-static void queue_int_handle_evt(UINT16 event, char *p_param)
-{
-    switch(event)
-    {
+static void queue_int_handle_evt(UINT16 event, char *p_param) {
+    switch(event) {
         case BTIF_QUEUE_CONNECT_EVT:
-            queue_int_add((connect_node_t*)p_param);
+            queue_int_add((connect_node_t *)p_param);
             break;
 
         case BTIF_QUEUE_ADVANCE_EVT:
@@ -132,17 +116,15 @@
 ** Returns          BT_STATUS_SUCCESS if successful
 **
 *******************************************************************************/
-bt_status_t btif_queue_connect(uint16_t uuid, const bt_bdaddr_t *bda,
-                        btif_connect_cb_t *connect_cb)
-{
+bt_status_t btif_queue_connect(uint16_t uuid, const bt_bdaddr_t *bda, btif_connect_cb_t connect_cb) {
     connect_node_t node;
     memset(&node, 0, sizeof(connect_node_t));
-    memcpy(&(node.bda), bda, sizeof(bt_bdaddr_t));
+    memcpy(&node.bda, bda, sizeof(bt_bdaddr_t));
     node.uuid = uuid;
-    node.p_cb = connect_cb;
+    node.connect_cb = connect_cb;
 
     return btif_transfer_context(queue_int_handle_evt, BTIF_QUEUE_CONNECT_EVT,
-                          (char*)&node, sizeof(connect_node_t), NULL);
+                          (char *)&node, sizeof(connect_node_t), NULL);
 }
 
 /*******************************************************************************
@@ -155,13 +137,11 @@
 ** Returns          void
 **
 *******************************************************************************/
-void btif_queue_advance()
-{
+void btif_queue_advance() {
     btif_transfer_context(queue_int_handle_evt, BTIF_QUEUE_ADVANCE_EVT,
                           NULL, 0, NULL);
 }
 
-
 /*******************************************************************************
 **
 ** Function         btif_queue_release
@@ -171,17 +151,7 @@
 ** Returns          void
 **
 *******************************************************************************/
-void btif_queue_release()
-{
-    connect_node_t *current = connect_queue;
-
-    while (current != NULL)
-    {
-         connect_node_t *next = current->p_next;
-         GKI_freebuf(current);
-         current = next;
-    }
-
+void btif_queue_release() {
+    list_free(connect_queue);
     connect_queue = NULL;
 }
-