Home | History | Annotate | Download | only in src
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2009-2012 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 /*******************************************************************************
     20  *
     21  *  Filename:      btif_profile_queue.c
     22  *
     23  *  Description:   Bluetooth remote device connection queuing implementation.
     24  *
     25  ******************************************************************************/
     26 
     27 #include <hardware/bluetooth.h>
     28 
     29 #define LOG_TAG "BTIF_QUEUE"
     30 #include "btif_common.h"
     31 #include "btif_profile_queue.h"
     32 #include "gki.h"
     33 #include "list.h"
     34 
     35 /*******************************************************************************
     36 **  Local type definitions
     37 *******************************************************************************/
     38 
     39 typedef enum {
     40   BTIF_QUEUE_CONNECT_EVT,
     41   BTIF_QUEUE_ADVANCE_EVT,
     42 } btif_queue_event_t;
     43 
     44 typedef struct {
     45     bt_bdaddr_t bda;
     46     uint16_t uuid;
     47     bool busy;
     48     btif_connect_cb_t connect_cb;
     49 } connect_node_t;
     50 
     51 /*******************************************************************************
     52 **  Static variables
     53 *******************************************************************************/
     54 
     55 static list_t *connect_queue;
     56 
     57 /*******************************************************************************
     58 **  Queue helper functions
     59 *******************************************************************************/
     60 
     61 static void queue_int_add(connect_node_t *p_param) {
     62     connect_node_t *p_node = GKI_getbuf(sizeof(connect_node_t));
     63     ASSERTC(p_node != NULL, "Failed to allocate new list node", 0);
     64 
     65     memcpy(p_node, p_param, sizeof(connect_node_t));
     66 
     67     if (!connect_queue) {
     68         connect_queue = list_new(GKI_freebuf);
     69         ASSERTC(connect_queue != NULL, "Failed to allocate list", 0);
     70     }
     71 
     72     list_append(connect_queue, p_node);
     73 }
     74 
     75 static void queue_int_advance() {
     76     if (connect_queue && !list_is_empty(connect_queue))
     77         list_remove(connect_queue, list_front(connect_queue));
     78 }
     79 
     80 static bt_status_t queue_int_connect_next() {
     81     if (!connect_queue || list_is_empty(connect_queue))
     82         return BT_STATUS_FAIL;
     83 
     84     connect_node_t *p_head = list_front(connect_queue);
     85 
     86     // If the queue is currently busy, we return success anyway,
     87     // since the connection has been queued...
     88     if (p_head->busy)
     89         return BT_STATUS_SUCCESS;
     90 
     91     p_head->busy = true;
     92     return p_head->connect_cb(&p_head->bda, p_head->uuid);
     93 }
     94 
     95 static void queue_int_handle_evt(UINT16 event, char *p_param) {
     96     switch(event) {
     97         case BTIF_QUEUE_CONNECT_EVT:
     98             queue_int_add((connect_node_t *)p_param);
     99             break;
    100 
    101         case BTIF_QUEUE_ADVANCE_EVT:
    102             queue_int_advance();
    103             break;
    104     }
    105 
    106     queue_int_connect_next();
    107 }
    108 
    109 /*******************************************************************************
    110 **
    111 ** Function         btif_queue_connect
    112 **
    113 ** Description      Add a new connection to the queue and trigger the next
    114 **                  scheduled connection.
    115 **
    116 ** Returns          BT_STATUS_SUCCESS if successful
    117 **
    118 *******************************************************************************/
    119 bt_status_t btif_queue_connect(uint16_t uuid, const bt_bdaddr_t *bda, btif_connect_cb_t connect_cb) {
    120     connect_node_t node;
    121     memset(&node, 0, sizeof(connect_node_t));
    122     memcpy(&node.bda, bda, sizeof(bt_bdaddr_t));
    123     node.uuid = uuid;
    124     node.connect_cb = connect_cb;
    125 
    126     return btif_transfer_context(queue_int_handle_evt, BTIF_QUEUE_CONNECT_EVT,
    127                           (char *)&node, sizeof(connect_node_t), NULL);
    128 }
    129 
    130 /*******************************************************************************
    131 **
    132 ** Function         btif_queue_advance
    133 **
    134 ** Description      Clear the queue's busy status and advance to the next
    135 **                  scheduled connection.
    136 **
    137 ** Returns          void
    138 **
    139 *******************************************************************************/
    140 void btif_queue_advance() {
    141     btif_transfer_context(queue_int_handle_evt, BTIF_QUEUE_ADVANCE_EVT,
    142                           NULL, 0, NULL);
    143 }
    144 
    145 /*******************************************************************************
    146 **
    147 ** Function         btif_queue_release
    148 **
    149 ** Description      Free up all the queue nodes and set the queue head to NULL
    150 **
    151 ** Returns          void
    152 **
    153 *******************************************************************************/
    154 void btif_queue_release() {
    155     list_free(connect_queue);
    156     connect_queue = NULL;
    157 }
    158