Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 823431c1 authored by Sharvil Nanavati's avatar Sharvil Nanavati
Browse files

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
parent 6c607bae
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -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();

+34 −64
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@
#include "btif_common.h"
#include "btif_profile_queue.h"
#include "gki.h"
#include "list.h"

/*******************************************************************************
**  Local type definitions
@@ -37,79 +38,62 @@

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);
            break;
@@ -132,14 +116,12 @@ static void queue_int_handle_evt(UINT16 event, char *p_param)
** 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);
@@ -155,13 +137,11 @@ bt_status_t btif_queue_connect(uint16_t uuid, const bt_bdaddr_t *bda,
** 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 @@ void btif_queue_advance()
** 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;
}