1/******************************************************************************
2 *
3 *  Copyright (C) 2009-2013 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#include <string.h>
20#include "bt_target.h"
21#include "bt_utils.h"
22#include "gap_int.h"
23
24/*******************************************************************************
25**
26** Function         gap_allocate_cb
27**
28** Description      Look through the GAP Control Blocks for a free one.
29**
30** Returns          Pointer to the control block or NULL if not found
31**
32*******************************************************************************/
33tGAP_INFO *gap_allocate_cb (void)
34{
35    tGAP_INFO     *p_cb = &gap_cb.blk[0];
36    UINT8        x;
37
38    for (x = 0; x < GAP_MAX_BLOCKS; x++, p_cb++)
39    {
40        if (!p_cb->in_use)
41        {
42            memset (p_cb, 0, sizeof (tGAP_INFO));
43
44            p_cb->in_use = TRUE;
45            p_cb->index = x;
46            p_cb->p_data = (void *)NULL;
47            return (p_cb);
48        }
49    }
50
51    /* If here, no free control blocks found */
52    return (NULL);
53}
54
55
56/*******************************************************************************
57**
58** Function         gap_free_cb
59**
60** Description      Release GAP control block.
61**
62** Returns          Pointer to the control block or NULL if not found
63**
64*******************************************************************************/
65void gap_free_cb (tGAP_INFO *p_cb)
66{
67    if (p_cb)
68    {
69        p_cb->gap_cback = NULL;
70        p_cb->in_use = FALSE;
71    }
72}
73
74
75/*******************************************************************************
76**
77** Function         gap_is_service_busy
78**
79** Description      Look through the GAP Control Blocks that are in use
80**                  and check to see if the event waiting for is the command
81**                  requested.
82**
83** Returns          TRUE if already in use
84**                  FALSE if not busy
85**
86*******************************************************************************/
87BOOLEAN gap_is_service_busy (UINT16 request)
88{
89    tGAP_INFO   *p_cb = &gap_cb.blk[0];
90    UINT8        x;
91
92    for (x = 0; x < GAP_MAX_BLOCKS; x++, p_cb++)
93    {
94        if (p_cb->in_use && p_cb->event == request)
95            return (TRUE);
96    }
97
98    /* If here, service is not busy */
99    return (FALSE);
100}
101
102
103/*******************************************************************************
104**
105** Function         gap_convert_btm_status
106**
107** Description      Converts a BTM error status into a GAP error status
108**
109**
110** Returns          GAP_UNKNOWN_BTM_STATUS is returned if not recognized
111**
112*******************************************************************************/
113UINT16 gap_convert_btm_status (tBTM_STATUS btm_status)
114{
115    switch (btm_status)
116    {
117    case BTM_SUCCESS:
118        return (BT_PASS);
119
120    case BTM_CMD_STARTED:
121        return (GAP_CMD_INITIATED);
122
123    case BTM_BUSY:
124        return (GAP_ERR_BUSY);
125
126    case BTM_MODE_UNSUPPORTED:
127    case BTM_ILLEGAL_VALUE:
128        return (GAP_ERR_ILL_PARM);
129
130    case BTM_WRONG_MODE:
131        return (GAP_DEVICE_NOT_UP);
132
133    case BTM_UNKNOWN_ADDR:
134        return (GAP_BAD_BD_ADDR);
135
136    case BTM_DEVICE_TIMEOUT:
137        return (GAP_ERR_TIMEOUT);
138
139    default:
140        return (GAP_ERR_PROCESSING);
141    }
142}
143