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