1 /******************************************************************************
2  *
3  *  Copyright (C) 1998-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  *  This file contains the HeaLth device profile main functions and state
22  *  machine.
23  *
24  ******************************************************************************/
25 #include <string.h>
26 
27 #include "bt_target.h"
28 #if defined(HL_INCLUDED) && (HL_INCLUDED == TRUE)
29 
30 
31 
32 #include "bta_hl_api.h"
33 #include "bta_hl_int.h"
34 #include "bt_common.h"
35 #include "utl.h"
36 #include "l2c_api.h"
37 #include "mca_defs.h"
38 
39 
40 #if (BTA_HL_DEBUG == TRUE) && (BT_USE_TRACES == TRUE)
41 static char *bta_hl_cch_state_code(tBTA_HL_CCH_STATE state_code);
42 static char *bta_hl_dch_state_code(tBTA_HL_DCH_STATE state_code);
43 #endif
44 
45 extern UINT16 L2CA_AllocateRandomPsm(void);
46 extern UINT16 L2CA_AllocatePsm(void);
47 /*****************************************************************************
48 ** DCH State Table
49 *****************************************************************************/
50 /*****************************************************************************
51 ** Constants and types
52 *****************************************************************************/
53 /* state machine action enumeration list for DCH */
54 /* The order of this enumeration must be the same as bta_hl_dch_act_tbl[] */
55 enum
56 {
57     BTA_HL_DCH_MCA_CREATE,
58     BTA_HL_DCH_MCA_CREATE_CFM,
59     BTA_HL_DCH_MCA_CREATE_IND,
60     BTA_HL_DCH_MCA_OPEN_CFM,
61     BTA_HL_DCH_MCA_OPEN_IND,
62     BTA_HL_DCH_MCA_CLOSE,
63     BTA_HL_DCH_MCA_CLOSE_CFM,
64     BTA_HL_DCH_MCA_CLOSE_IND,
65     BTA_HL_DCH_CLOSE_CMPL,
66     BTA_HL_DCH_MCA_RCV_DATA,
67 
68     BTA_HL_DCH_SDP_INIT,
69     BTA_HL_DCH_MCA_RECONNECT,
70     BTA_HL_DCH_MCA_RECONNECT_IND,
71     BTA_HL_DCH_MCA_RECONNECT_CFM,
72     BTA_HL_DCH_CLOSE_ECHO_TEST,
73     BTA_HL_DCH_CREATE_RSP,
74     BTA_HL_DCH_MCA_ABORT,
75     BTA_HL_DCH_MCA_ABORT_IND,
76     BTA_HL_DCH_MCA_ABORT_CFM,
77     BTA_HL_DCH_MCA_CONG_CHANGE,
78 
79     BTA_HL_DCH_SDP_FAIL,
80     BTA_HL_DCH_SEND_DATA,
81     BTA_HL_DCH_CI_GET_TX_DATA,
82     BTA_HL_DCH_CI_PUT_RX_DATA,
83     BTA_HL_DCH_CI_GET_ECHO_DATA,
84     BTA_HL_DCH_ECHO_TEST,
85     BTA_HL_DCH_CI_PUT_ECHO_DATA,
86     BTA_HL_DCH_IGNORE
87 };
88 
89 typedef void (*tBTA_HL_DCH_ACTION)(UINT8 app_idx, UINT8 mcl_idx, UINT8 mdl_idx, tBTA_HL_DATA *p_data);
90 
91 static const tBTA_HL_DCH_ACTION bta_hl_dch_action[] =
92 {
93     bta_hl_dch_mca_create,
94     bta_hl_dch_mca_create_cfm,
95     bta_hl_dch_mca_create_ind,
96     bta_hl_dch_mca_open_cfm,
97     bta_hl_dch_mca_open_ind,
98     bta_hl_dch_mca_close,
99     bta_hl_dch_mca_close_cfm,
100     bta_hl_dch_mca_close_ind,
101     bta_hl_dch_close_cmpl,
102     bta_hl_dch_mca_rcv_data,
103 
104     bta_hl_dch_sdp_init,
105     bta_hl_dch_mca_reconnect,
106     bta_hl_dch_mca_reconnect_ind,
107     bta_hl_dch_mca_reconnect_cfm,
108     bta_hl_dch_close_echo_test,
109     bta_hl_dch_create_rsp,
110     bta_hl_dch_mca_abort,
111     bta_hl_dch_mca_abort_ind,
112     bta_hl_dch_mca_abort_cfm,
113     bta_hl_dch_mca_cong_change,
114 
115     bta_hl_dch_sdp_fail,
116     bta_hl_dch_send_data,
117     bta_hl_dch_ci_get_tx_data,
118     bta_hl_dch_ci_put_rx_data,
119     bta_hl_dch_ci_get_echo_data,
120     bta_hl_dch_echo_test,
121     bta_hl_dch_ci_put_echo_data,
122 };
123 
124 
125 /* state table information */
126 #define BTA_HL_DCH_ACTIONS             1       /* number of actions */
127 #define BTA_HL_DCH_ACTION_COL          0       /* position of action */
128 #define BTA_HL_DCH_NEXT_STATE          1       /* position of next state */
129 #define BTA_HL_DCH_NUM_COLS            2       /* number of columns in state tables */
130 
131 /* state table for idle state */
132 static const UINT8 bta_hl_dch_st_idle[][BTA_HL_DCH_NUM_COLS] =
133 {
134 /* Event                                Action 1                    Next state */
135 /* BTA_HL_DCH_SDP_INIT_EVT   */     {BTA_HL_DCH_SDP_INIT,           BTA_HL_DCH_OPENING_ST},
136 /* BTA_HL_DCH_OPEN_EVT       */     {BTA_HL_DCH_MCA_CREATE,         BTA_HL_DCH_OPENING_ST},
137 /* BTA_HL_MCA_CREATE_IND_EVT */     {BTA_HL_DCH_MCA_CREATE_IND,     BTA_HL_DCH_OPENING_ST},
138 /* BTA_HL_MCA_CREATE_CFM_EVT */     {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
139 /* BTA_HL_MCA_OPEN_IND_EVT   */     {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
140 
141 /* BTA_HL_MCA_OPEN_CFM_EVT   */     {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
142 /* BTA_HL_DCH_CLOSE_EVT      */     {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
143 /* BTA_HL_MCA_CLOSE_IND_EVT  */     {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
144 /* BTA_HL_MCA_CLOSE_CFM_EVT  */     {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
145 /* BTA_HL_API_SEND_DATA_EVT  */     {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
146 
147 /* BTA_HL_MCA_RCV_DATA_EVT   */     {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
148 /* BTA_HL_DCH_CLOSE_CMPL_EVT */     {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
149 /* BTA_HL_DCH_RECONNECT_EVT  */     {BTA_HL_DCH_MCA_RECONNECT,      BTA_HL_DCH_OPENING_ST},
150 /* BTA_HL_DCH_SDP_FAIL_EVT   */     {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
151 /* BTA_HL_MCA_RECONNECT_IND_EVT*/   {BTA_HL_DCH_MCA_RECONNECT_IND,  BTA_HL_DCH_OPENING_ST},
152 
153 /* BTA_HL_MCA_RECONNECT_CFM_EVT*/   {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
154 /* BTA_HL_DCH_CLOSE_ECHO_TEST_EVT*/ {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
155 /* BTA_HL_API_DCH_CREATE_RSP_EVT */ {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
156 /* BTA_HL_DCH_ABORT_EVT */          {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
157 /* BTA_HL_MCA_ABORT_IND_EVT */      {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
158 
159 /* BTA_HL_MCA_ABORT_CFM_EVT */      {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
160 /* BTA_HL_MCA_CONG_CHG_EVT */       {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
161 /* BTA_HL_CI_GET_TX_DATA_EVT  */    {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
162 /* BTA_HL_CI_PUT_RX_DATA_EVT  */    {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
163 /* BTA_HL_CI_GET_ECHO_DATA_EVT  */  {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
164 /* BTA_HL_DCH_ECHO_TEST_EVT  */     {BTA_HL_DCH_ECHO_TEST,          BTA_HL_DCH_OPENING_ST},
165 /* BTA_HL_CI_PUT_ECHO_DATA_EVT  */  {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST}
166 };
167 
168 /* state table for opening state */
169 static const UINT8 bta_hl_dch_st_opening[][BTA_HL_DCH_NUM_COLS] =
170 {
171 /* Event                                Action 1                    Next state */
172 /* BTA_HL_DCH_SDP_INIT_EVT   */   {BTA_HL_DCH_SDP_INIT,             BTA_HL_DCH_OPENING_ST},
173 /* BTA_HL_DCH_OPEN_EVT       */   {BTA_HL_DCH_MCA_CREATE,           BTA_HL_DCH_OPENING_ST},
174 /* BTA_HL_MCA_CREATE_IND_EVT */   {BTA_HL_DCH_IGNORE,               BTA_HL_DCH_OPENING_ST},
175 /* BTA_HL_MCA_CREATE_CFM_EVT */   {BTA_HL_DCH_MCA_CREATE_CFM,       BTA_HL_DCH_OPENING_ST},
176 /* BTA_HL_MCA_OPEN_IND_EVT   */   {BTA_HL_DCH_MCA_OPEN_IND,         BTA_HL_DCH_OPEN_ST},
177 /* BTA_HL_MCA_OPEN_CFM_EVT   */   {BTA_HL_DCH_MCA_OPEN_CFM,         BTA_HL_DCH_OPEN_ST},
178 /* BTA_HL_DCH_CLOSE_EVT      */   {BTA_HL_DCH_IGNORE,               BTA_HL_DCH_OPENING_ST},
179 /* BTA_HL_MCA_CLOSE_IND_EVT  */   {BTA_HL_DCH_MCA_CLOSE_IND,        BTA_HL_DCH_CLOSING_ST},
180 /* BTA_HL_MCA_CLOSE_CFM_EVT  */   {BTA_HL_DCH_MCA_CLOSE_CFM,        BTA_HL_DCH_CLOSING_ST},
181 /* BTA_HL_API_SEND_DATA_EVT  */   {BTA_HL_DCH_IGNORE,               BTA_HL_DCH_OPEN_ST},
182 
183 /* BTA_HL_MCA_RCV_DATA_EVT   */   {BTA_HL_DCH_IGNORE,               BTA_HL_DCH_OPEN_ST},
184 /* BTA_HL_DCH_CLOSE_CMPL_EVT */   {BTA_HL_DCH_CLOSE_CMPL,           BTA_HL_DCH_IDLE_ST},
185 /* BTA_HL_DCH_RECONNECT_EVT  */   {BTA_HL_DCH_MCA_RECONNECT,        BTA_HL_DCH_OPENING_ST},
186 /* BTA_HL_DCH_SDP_FAIL_EVT   */   {BTA_HL_DCH_SDP_FAIL,             BTA_HL_DCH_CLOSING_ST},
187 /* BTA_HL_MCA_RECONNECT_IND_EVT*/ {BTA_HL_DCH_IGNORE,               BTA_HL_DCH_IDLE_ST},
188 /* BTA_HL_MCA_RECONNECT_CFM_EVT*/ {BTA_HL_DCH_MCA_RECONNECT_CFM,    BTA_HL_DCH_OPENING_ST},
189 /* BTA_HL_DCH_CLOSE_ECHO_TEST_EVT*/ {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_OPENING_ST},
190 /* BTA_HL_API_DCH_CREATE_RSP_EVT */ {BTA_HL_DCH_CREATE_RSP,         BTA_HL_DCH_OPENING_ST},
191 /* BTA_HL_DCH_ABORT_EVT */          {BTA_HL_DCH_MCA_ABORT,          BTA_HL_DCH_OPENING_ST},
192 /* BTA_HL_MCA_ABORT_IND_EVT */      {BTA_HL_DCH_MCA_ABORT_IND,      BTA_HL_DCH_OPENING_ST},
193 
194 /* BTA_HL_MCA_ABORT_CFM_EVT */      {BTA_HL_DCH_MCA_ABORT_CFM,      BTA_HL_DCH_OPENING_ST},
195 /* BTA_HL_MCA_CONG_CHG_EVT */       {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_OPENING_ST},
196 /* BTA_HL_CI_GET_TX_DATA_EVT  */    {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_OPENING_ST},
197 /* BTA_HL_CI_PUT_RX_DATA_EVT  */    {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_OPENING_ST},
198 /* BTA_HL_CI_GET_ECHO_DATA_EVT  */  {BTA_HL_DCH_CI_GET_ECHO_DATA,   BTA_HL_DCH_OPENING_ST},
199 /* BTA_HL_DCH_ECHO_TEST_EVT  */     {BTA_HL_DCH_ECHO_TEST,          BTA_HL_DCH_OPENING_ST},
200 /* BTA_HL_CI_PUT_ECHO_DATA_EVT  */  {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_OPENING_ST}
201 };
202 
203 /* state table for open state */
204 static const UINT8 bta_hl_dch_st_open[][BTA_HL_DCH_NUM_COLS] =
205 {
206 /* Event                                Action 1                  Next state */
207 /* BTA_HL_DCH_SDP_INIT_EVT   */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
208 /* BTA_HL_DCH_OPEN_EVT       */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
209 /* BTA_HL_MCA_CREATE_IND_EVT */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
210 /* BTA_HL_MCA_CREATE_CFM_EVT */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
211 /* BTA_HL_MCA_OPEN_IND_EVT   */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
212 /* BTA_HL_MCA_OPEN_CFM_EVT   */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
213 /* BTA_HL_DCH_CLOSE_EVT      */     {BTA_HL_DCH_MCA_CLOSE,        BTA_HL_DCH_CLOSING_ST},
214 /* BTA_HL_MCA_CLOSE_IND_EVT  */     {BTA_HL_DCH_MCA_CLOSE_IND,    BTA_HL_DCH_CLOSING_ST},
215 /* BTA_HL_MCA_CLOSE_CFM_EVT  */     {BTA_HL_DCH_MCA_CLOSE_CFM,    BTA_HL_DCH_CLOSING_ST},
216 /* BTA_HL_API_SEND_DATA_EVT  */     {BTA_HL_DCH_SEND_DATA,        BTA_HL_DCH_OPEN_ST},
217 
218 /* BTA_HL_MCA_RCV_DATA_EVT   */     {BTA_HL_DCH_MCA_RCV_DATA,     BTA_HL_DCH_OPEN_ST},
219 /* BTA_HL_DCH_CLOSE_CMPL_EVT */     {BTA_HL_DCH_CLOSE_CMPL,       BTA_HL_DCH_IDLE_ST},
220 /* BTA_HL_DCH_RECONNECT_EVT  */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
221 /* BTA_HL_DCH_SDP_FAIL_EVT   */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
222 /* BTA_HL_MCA_RECONNECT_IND_EVT*/   {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
223 /* BTA_HL_MCA_RECONNECT_CFM_EVT*/   {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
224 /* BTA_HL_DCH_CLOSE_ECHO_TEST_EVT*/ {BTA_HL_DCH_CLOSE_ECHO_TEST,  BTA_HL_DCH_CLOSING_ST},
225 /* BTA_HL_API_DCH_CREATE_RSP_EVT */ {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
226 /* BTA_HL_DCH_ABORT_EVT */          {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
227 /* BTA_HL_MCA_ABORT_IND_EVT */      {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
228 
229 /* BTA_HL_DCH_ABORT_CFM_EVT */      {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
230 /* BTA_HL_MCA_CONG_CHG_EVT */       {BTA_HL_DCH_MCA_CONG_CHANGE,  BTA_HL_DCH_OPEN_ST},
231 /* BTA_HL_CI_GET_TX_DATA_EVT  */    {BTA_HL_DCH_CI_GET_TX_DATA,   BTA_HL_DCH_OPEN_ST},
232 /* BTA_HL_CI_PUT_RX_DATA_EVT  */    {BTA_HL_DCH_CI_PUT_RX_DATA,   BTA_HL_DCH_OPEN_ST},
233 /* BTA_HL_CI_GET_ECHO_DATA_EVT  */  {BTA_HL_DCH_CI_GET_ECHO_DATA, BTA_HL_DCH_OPEN_ST},
234 /* BTA_HL_DCH_ECHO_TEST_EVT  */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
235 /* BTA_HL_CI_PUT_ECHO_DATA_EVT  */  {BTA_HL_DCH_CI_PUT_ECHO_DATA, BTA_HL_DCH_OPEN_ST}
236 };
237 
238 
239 /* state table for closing state */
240 static const UINT8 bta_hl_dch_st_closing[][BTA_HL_DCH_NUM_COLS] =
241 {
242 /* Event                                Action 1                  Next state */
243 /* BTA_HL_DCH_SDP_INIT_EVT   */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
244 /* BTA_HL_DCH_OPEN_EVT       */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
245 /* BTA_HL_MCA_CREATE_IND_EVT */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
246 /* BTA_HL_MCA_CREATE_CFM_EVT */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
247 /* BTA_HL_MCA_OPEN_IND_EVT   */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
248 /* BTA_HL_MCA_OPEN_CFM_EVT   */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
249 /* BTA_HL_DCH_CLOSE_EVT      */     {BTA_HL_DCH_MCA_CLOSE,        BTA_HL_DCH_CLOSING_ST},
250 /* BTA_HL_MCA_CLOSE_IND_EVT  */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
251 /* BTA_HL_MCA_CLOSE_CFM_EVT  */     {BTA_HL_DCH_MCA_CLOSE_CFM,    BTA_HL_DCH_CLOSING_ST},
252 /* BTA_HL_API_SEND_DATA_EVT  */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
253 
254 /* BTA_HL_MCA_RCV_DATA_EVT   */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
255 /* BTA_HL_DCH_CLOSE_CMPL_EVT */     {BTA_HL_DCH_CLOSE_CMPL,       BTA_HL_DCH_IDLE_ST},
256 /* BTA_HL_DCH_RECONNECT_EVT  */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
257 /* BTA_HL_DCH_SDP_FAIL_EVT   */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
258 /* BTA_HL_MCA_RECONNECT_IND_EVT*/   {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
259 /* BTA_HL_MCA_RECONNECT_CFM_EVT*/   {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
260 /* BTA_HL_DCH_CLOSE_ECHO_TEST_EVT*/ {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
261 /* BTA_HL_API_DCH_CREATE_RSP_EVT */ {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
262 /* BTA_HL_DCH_ABORT_EVT */          {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
263 /* BTA_HL_MCA_ABORT_IND_EVT */      {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
264 
265 /* BTA_HL_DCH_ABORT_CFM_EVT */      {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
266 /* BTA_HL_MCA_CONG_CHG_EVT */       {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
267 /* BTA_HL_CI_GET_TX_DATA_EVT  */    {BTA_HL_DCH_CI_GET_TX_DATA,   BTA_HL_DCH_CLOSING_ST},
268 /* BTA_HL_CI_PUT_RX_DATA_EVT  */    {BTA_HL_DCH_CI_PUT_RX_DATA,   BTA_HL_DCH_CLOSING_ST},
269 /* BTA_HL_CI_GET_ECHO_DATA_EVT  */  {BTA_HL_DCH_CI_GET_ECHO_DATA, BTA_HL_DCH_CLOSING_ST},
270 /* BTA_HL_DCH_ECHO_TEST_EVT  */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
271 /* BTA_HL_CI_PUT_ECHO_DATA_EVT  */  {BTA_HL_DCH_CI_PUT_ECHO_DATA, BTA_HL_DCH_CLOSING_ST}
272 };
273 
274 /* type for state table */
275 typedef const UINT8 (*tBTA_HL_DCH_ST_TBL)[BTA_HL_DCH_NUM_COLS];
276 
277 /* state table */
278 const tBTA_HL_DCH_ST_TBL bta_hl_dch_st_tbl[] =
279 {
280     bta_hl_dch_st_idle,
281     bta_hl_dch_st_opening,
282     bta_hl_dch_st_open,
283     bta_hl_dch_st_closing
284 };
285 
286 /*****************************************************************************
287 ** CCH State Table
288 *****************************************************************************/
289 /*****************************************************************************
290 ** Constants and types
291 *****************************************************************************/
292 /* state machine action enumeration list for CCH */
293 enum
294 {
295     BTA_HL_CCH_SDP_INIT,
296     BTA_HL_CCH_MCA_OPEN,
297     BTA_HL_CCH_MCA_CLOSE,
298     BTA_HL_CCH_CLOSE_CMPL,
299     BTA_HL_CCH_MCA_CONNECT,
300     BTA_HL_CCH_MCA_DISCONNECT,
301     BTA_HL_CCH_MCA_RSP_TOUT,
302     BTA_HL_CCH_MCA_DISC_OPEN,
303     BTA_HL_CCH_IGNORE
304 };
305 
306 /* type for action functions */
307 typedef void (*tBTA_HL_CCH_ACTION)(UINT8 app_idx, UINT8 mcl_idx, tBTA_HL_DATA *p_data);
308 
309 /* action function list for MAS */
310 const tBTA_HL_CCH_ACTION bta_hl_cch_action[] =
311 {
312     bta_hl_cch_sdp_init,
313     bta_hl_cch_mca_open,
314     bta_hl_cch_mca_close,
315     bta_hl_cch_close_cmpl,
316     bta_hl_cch_mca_connect,
317     bta_hl_cch_mca_disconnect,
318     bta_hl_cch_mca_rsp_tout,
319     bta_hl_cch_mca_disc_open
320 };
321 
322 
323 /* state table information */
324 #define BTA_HL_CCH_ACTIONS             1       /* number of actions */
325 #define BTA_HL_CCH_NEXT_STATE          1       /* position of next state */
326 #define BTA_HL_CCH_NUM_COLS            2       /* number of columns in state tables */
327 
328 
329 /* state table for MAS idle state */
330 static const UINT8 bta_hl_cch_st_idle[][BTA_HL_CCH_NUM_COLS] =
331 {
332 /* Event                          Action 1                  Next state */
333 /* BTA_HL_CCH_OPEN_EVT           */ {BTA_HL_CCH_SDP_INIT,       BTA_HL_CCH_OPENING_ST},
334 /* BTA_HL_CCH_SDP_OK_EVT         */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_IDLE_ST},
335 /* BTA_HL_CCH_SDP_FAIL_EVT       */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_IDLE_ST},
336 /* BTA_HL_MCA_CONNECT_IND_EVT    */ {BTA_HL_CCH_MCA_CONNECT,    BTA_HL_CCH_OPEN_ST},
337 /* BTA_HL_MCA_DISCONNECT_IND_EVT */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_IDLE_ST},
338 /* BTA_HL_CCH_CLOSE_EVT          */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_IDLE_ST},
339 /* BTA_HL_CCH_CLOSE_CMPL_EVT     */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_IDLE_ST},
340 /* BTA_HL_CCH_CLOSE_CMPL_EVT     */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_IDLE_ST}
341 };
342 
343 /* state table for obex/rfcomm connection state */
344 static const UINT8 bta_hl_cch_st_opening[][BTA_HL_CCH_NUM_COLS] =
345 {
346 /* Event                          Action 1               Next state */
347 /* BTA_HL_CCH_OPEN_EVT           */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_OPENING_ST},
348 /* BTA_HL_CCH_SDP_OK_EVT         */ {BTA_HL_CCH_MCA_OPEN,       BTA_HL_CCH_OPENING_ST},
349 /* BTA_HL_CCH_SDP_FAIL_EVT       */ {BTA_HL_CCH_CLOSE_CMPL,     BTA_HL_CCH_IDLE_ST},
350 /* BTA_HL_MCA_CONNECT_IND_EVT    */ {BTA_HL_CCH_MCA_CONNECT,    BTA_HL_CCH_OPEN_ST},
351 /* BTA_HL_MCA_DISCONNECT_IND_EVT */ {BTA_HL_CCH_MCA_DISCONNECT, BTA_HL_CCH_CLOSING_ST},
352 /* BTA_HL_CCH_CLOSE_EVT          */ {BTA_HL_CCH_MCA_CLOSE,      BTA_HL_CCH_CLOSING_ST},
353 /* BTA_HL_CCH_CLOSE_CMPL_EVT     */ {BTA_HL_CCH_CLOSE_CMPL,     BTA_HL_CCH_IDLE_ST},
354 /* BTA_HL_MCA_RSP_TOUT_IND_EVT   */ {BTA_HL_CCH_MCA_RSP_TOUT,   BTA_HL_CCH_CLOSING_ST}
355 };
356 
357 /* state table for open state */
358 static const UINT8 bta_hl_cch_st_open[][BTA_HL_CCH_NUM_COLS] =
359 {
360 /* Event                          Action 1                  Next state */
361 /* BTA_HL_CCH_OPEN_EVT           */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_OPEN_ST},
362 /* BTA_HL_CCH_SDP_OK_EVT         */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_OPEN_ST},
363 /* BTA_HL_CCH_SDP_FAIL_EVT       */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_OPEN_ST},
364 /* BTA_HL_MCA_CONNECT_IND_EVT    */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_OPEN_ST},
365 /* BTA_HL_MCA_DISCONNECT_IND_EVT */ {BTA_HL_CCH_MCA_DISCONNECT, BTA_HL_CCH_CLOSING_ST},
366 /* BTA_HL_CCH_CLOSE_EVT          */ {BTA_HL_CCH_MCA_CLOSE,      BTA_HL_CCH_CLOSING_ST},
367 /* BTA_HL_CCH_CLOSE_CMPL_EVT     */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_OPEN_ST},
368 /* BTA_HL_MCA_RSP_TOUT_IND_EVT   */ {BTA_HL_CCH_MCA_RSP_TOUT,   BTA_HL_CCH_CLOSING_ST}
369 };
370 
371 /* state table for closing state */
372 static const UINT8 bta_hl_cch_st_closing[][BTA_HL_CCH_NUM_COLS] =
373 {
374 /* Event                          Action 1                  Next state */
375 /* BTA_HL_CCH_OPEN_EVT           */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_CLOSING_ST},
376 /* BTA_HL_CCH_SDP_OK_EVT         */ {BTA_HL_CCH_CLOSE_CMPL,     BTA_HL_CCH_IDLE_ST},
377 /* BTA_HL_CCH_SDP_FAIL_EVT       */ {BTA_HL_CCH_CLOSE_CMPL,     BTA_HL_CCH_IDLE_ST},
378 /* BTA_HL_MCA_CONNECT_IND_EVT    */ {BTA_HL_CCH_MCA_DISC_OPEN,  BTA_HL_CCH_CLOSING_ST},
379 /* BTA_HL_MCA_DISCONNECT_IND_EVT */ {BTA_HL_CCH_MCA_DISCONNECT, BTA_HL_CCH_CLOSING_ST},
380 /* BTA_HL_CCH_CLOSE_EVT          */ {BTA_HL_CCH_MCA_CLOSE,      BTA_HL_CCH_CLOSING_ST},
381 /* BTA_HL_CCH_CLOSE_CMPL_EVT     */ {BTA_HL_CCH_CLOSE_CMPL,     BTA_HL_CCH_IDLE_ST},
382 /* BTA_HL_MCA_RSP_TOUT_IND_EVT   */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_CLOSING_ST}
383 };
384 
385 /* type for state table CCH */
386 typedef const UINT8 (*tBTA_HL_CCH_ST_TBL)[BTA_HL_CCH_NUM_COLS];
387 
388 /* MAS state table */
389 const tBTA_HL_CCH_ST_TBL bta_hl_cch_st_tbl[] =
390 {
391     bta_hl_cch_st_idle,
392     bta_hl_cch_st_opening,
393     bta_hl_cch_st_open,
394     bta_hl_cch_st_closing
395 };
396 
397 
398 /*****************************************************************************
399 ** Global data
400 *****************************************************************************/
401 
402 /* HL control block */
403 #if BTA_DYNAMIC_MEMORY == FALSE
404 tBTA_HL_CB  bta_hl_cb;
405 #endif
406 
407 
408 /*******************************************************************************
409 **
410 ** Function         bta_hl_cch_sm_execute
411 **
412 ** Description      State machine event handling function for CCH
413 **
414 ** Returns          void
415 **
416 *******************************************************************************/
bta_hl_cch_sm_execute(UINT8 app_idx,UINT8 mcl_idx,UINT16 event,tBTA_HL_DATA * p_data)417 void bta_hl_cch_sm_execute(UINT8 app_idx, UINT8 mcl_idx,
418                            UINT16 event, tBTA_HL_DATA *p_data)
419 {
420     tBTA_HL_CCH_ST_TBL  state_table;
421     UINT8               action;
422     int                 i;
423     tBTA_HL_MCL_CB      *p_cb  = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
424 
425 #if (BTA_HL_DEBUG == TRUE) && (BT_USE_TRACES == TRUE)
426     tBTA_HL_CCH_STATE in_state = p_cb->cch_state;
427     UINT16             cur_evt = event;
428     APPL_TRACE_DEBUG("HDP CCH Event Handler: State 0x%02x [%s], Event [%s]", in_state,
429                       bta_hl_cch_state_code(in_state),
430                       bta_hl_evt_code(cur_evt));
431 #endif
432 
433     /* look up the state table for the current state */
434     state_table = bta_hl_cch_st_tbl[p_cb->cch_state];
435 
436     event &= 0x00FF;
437 
438     /* set next state */
439     p_cb->cch_state = state_table[event][BTA_HL_CCH_NEXT_STATE];
440 
441     for (i = 0; i < BTA_HL_CCH_ACTIONS; i++)
442     {
443         if ((action = state_table[event][i]) != BTA_HL_CCH_IGNORE)
444         {
445             (*bta_hl_cch_action[action])(app_idx, mcl_idx, p_data);
446         }
447         else
448         {
449             /* discard HDP data */
450             bta_hl_discard_data(p_data->hdr.event, p_data);
451             break;
452         }
453     }
454 #if (BTA_HL_DEBUG == TRUE) && (BT_USE_TRACES == TRUE)
455     if (in_state != p_cb->cch_state)
456     {
457         APPL_TRACE_DEBUG("HL CCH State Change: [%s] -> [%s] after [%s]",
458                           bta_hl_cch_state_code(in_state),
459                           bta_hl_cch_state_code(p_cb->cch_state),
460                           bta_hl_evt_code(cur_evt));
461     }
462 #endif
463 
464 }
465 
466 /*******************************************************************************
467 **
468 ** Function         bta_hl_dch_sm_execute
469 **
470 ** Description      State machine event handling function for DCH
471 **
472 ** Returns          void
473 **
474 *******************************************************************************/
bta_hl_dch_sm_execute(UINT8 app_idx,UINT8 mcl_idx,UINT8 mdl_idx,UINT16 event,tBTA_HL_DATA * p_data)475 void bta_hl_dch_sm_execute(UINT8 app_idx, UINT8 mcl_idx, UINT8 mdl_idx,
476                            UINT16 event, tBTA_HL_DATA *p_data)
477 {
478     tBTA_HL_DCH_ST_TBL  state_table;
479     UINT8               action;
480     int                 i;
481     tBTA_HL_MDL_CB      *p_cb  = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
482 
483 #if (BTA_HL_DEBUG == TRUE) && (BT_USE_TRACES == TRUE)
484     tBTA_HL_DCH_STATE in_state = p_cb->dch_state;
485     UINT16             cur_evt = event;
486     APPL_TRACE_DEBUG("HDP DCH Event Handler: State 0x%02x [%s], Event [%s]", in_state,
487                       bta_hl_dch_state_code(in_state),
488                       bta_hl_evt_code(cur_evt));
489 #endif
490 
491     /* look up the state table for the current state */
492     state_table = bta_hl_dch_st_tbl[p_cb->dch_state];
493     event -= BTA_HL_DCH_EVT_MIN;
494 
495     /* set next state */
496     p_cb->dch_state = state_table[event][BTA_HL_DCH_NEXT_STATE];
497 
498     for (i = 0; i < BTA_HL_DCH_ACTIONS; i++)
499     {
500         if ((action = state_table[event][i]) != BTA_HL_DCH_IGNORE)
501         {
502             (*bta_hl_dch_action[action])(app_idx, mcl_idx, mdl_idx, p_data);
503         }
504         else
505         {
506             /* discard mas data */
507             bta_hl_discard_data(p_data->hdr.event, p_data);
508             break;
509         }
510     }
511 
512 
513 #if (BTA_HL_DEBUG == TRUE) && (BT_USE_TRACES == TRUE)
514     if (in_state != p_cb->dch_state)
515     {
516         APPL_TRACE_DEBUG("HL DCH State Change: [%s] -> [%s] after [%s]",
517                           bta_hl_dch_state_code(in_state),
518                           bta_hl_dch_state_code(p_cb->dch_state),
519                           bta_hl_evt_code(cur_evt));
520     }
521 #endif
522 }
523 /*******************************************************************************
524 **
525 ** Function         bta_hl_api_enable
526 **
527 ** Description      Process the API enable request to enable the HL subsystem
528 **
529 ** Returns          void
530 **
531 *******************************************************************************/
bta_hl_api_enable(tBTA_HL_CB * p_cb,tBTA_HL_DATA * p_data)532 static void bta_hl_api_enable(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
533 {
534     tBTA_HL_CTRL    evt_data;
535 
536     /* If already enabled then reject this request */
537     if (p_cb->enable)
538     {
539         APPL_TRACE_ERROR("HL is already enabled");
540         evt_data.enable_cfm.status = BTA_HL_STATUS_FAIL;
541         if (p_data->api_enable.p_cback)
542             p_data->api_enable.p_cback(BTA_HL_CTRL_ENABLE_CFM_EVT, (tBTA_HL_CTRL *) &evt_data);
543         return;
544     }
545 
546     /* Done with checking. now perform the enable oepration*/
547     /* initialize control block */
548     memset(p_cb, 0, sizeof(tBTA_HL_CB));
549     p_cb->enable = TRUE;
550     p_cb->p_ctrl_cback = p_data->api_enable.p_cback;
551     evt_data.enable_cfm.status = BTA_HL_STATUS_OK;
552     if (p_data->api_enable.p_cback)
553         p_data->api_enable.p_cback(BTA_HL_CTRL_ENABLE_CFM_EVT, (tBTA_HL_CTRL *) &evt_data);
554 
555 }
556 /*******************************************************************************
557 **
558 ** Function         bta_hl_api_disable
559 **
560 ** Description      Process the API disable request to disable the HL subsystem
561 **
562 ** Returns          void
563 **
564 *******************************************************************************/
bta_hl_api_disable(tBTA_HL_CB * p_cb,tBTA_HL_DATA * p_data)565 static void bta_hl_api_disable(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
566 {
567     tBTA_HL_CTRL    evt_data;
568     tBTA_HL_STATUS  status = BTA_HL_STATUS_OK;
569 
570     if (p_cb->enable)
571     {
572         p_cb->disabling = TRUE;
573         bta_hl_check_disable(p_data);
574     }
575     else
576     {
577         status = BTA_HL_STATUS_FAIL;
578         evt_data.disable_cfm.status = status;
579         if (p_cb->p_ctrl_cback) p_cb->p_ctrl_cback(BTA_HL_CTRL_DISABLE_CFM_EVT, (tBTA_HL_CTRL *) &evt_data);
580     }
581 
582 
583 #if BTA_HL_DEBUG == TRUE
584     if (status != BTA_HL_STATUS_OK)
585     {
586         APPL_TRACE_DEBUG("bta_hl_api_disable status =%s", bta_hl_status_code(status));
587     }
588 #endif
589 
590 }
591 
592 /*******************************************************************************
593 **
594 ** Function         bta_hl_api_update
595 **
596 ** Description      Process the API registration request to register an HDP applciation
597 **
598 ** Returns          void
599 **
600 *******************************************************************************/
bta_hl_api_update(tBTA_HL_CB * p_cb,tBTA_HL_DATA * p_data)601 static void bta_hl_api_update(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
602 {
603     tBTA_HL         evt_data;
604     tBTA_HL_APP_CB  *p_acb = BTA_HL_GET_APP_CB_PTR(0);
605     tBTA_HL_STATUS  status = BTA_HL_STATUS_FAIL;
606 
607 
608     APPL_TRACE_DEBUG("bta_hl_api_update");
609     if (p_cb->enable)
610     {
611 
612         status = bta_hl_app_update(p_data->api_update.app_id, p_data->api_update.is_register);
613         if (!p_data->api_update.is_register)
614         {
615             APPL_TRACE_DEBUG("Deregister");
616             memset(&evt_data, 0, sizeof(tBTA_HL));
617             evt_data.dereg_cfm.status = status;
618             evt_data.dereg_cfm.app_id = p_data->api_update.app_id;
619             if (status == BTA_HL_STATUS_OK)
620                 evt_data.dereg_cfm.app_handle = p_acb->app_handle;
621             if (p_acb->p_cback)
622             {
623                 p_acb->p_cback(BTA_HL_DEREGISTER_CFM_EVT, (tBTA_HL *) &evt_data);
624             }
625             return ;
626         }
627 
628     }
629 
630     if (status != BTA_HL_STATUS_OK)
631     {
632         if ((status != BTA_HL_STATUS_DUPLICATE_APP_ID) &&
633             (status != BTA_HL_STATUS_NO_RESOURCE))
634         {
635             if (p_acb)
636                 memset(p_acb, 0, sizeof(tBTA_HL_APP_CB));
637         }
638     }
639 #if BTA_HL_DEBUG == TRUE
640     if (status != BTA_HL_STATUS_OK)
641     {
642         APPL_TRACE_DEBUG("bta_hl_api_register status =%s", bta_hl_status_code(status));
643     }
644 #endif
645 
646     memset(&evt_data, 0, sizeof(tBTA_HL));
647     evt_data.reg_cfm.status = status;
648     evt_data.reg_cfm.app_id = p_data->api_update.app_id;
649     if (status == BTA_HL_STATUS_OK)
650         evt_data.reg_cfm.app_handle = p_acb->app_handle;
651     if (p_data->api_reg.p_cback)
652     {
653         p_data->api_reg.p_cback(BTA_HL_REGISTER_CFM_EVT, (tBTA_HL *) &evt_data);
654     }
655 
656     if (status == BTA_HL_STATUS_OK)
657     {
658         evt_data.sdp_info_ind.app_handle = p_acb->app_handle;
659         evt_data.sdp_info_ind.ctrl_psm = p_acb->ctrl_psm;
660         evt_data.sdp_info_ind.data_psm = p_acb->data_psm;
661         evt_data.sdp_info_ind.data_x_spec = BTA_HL_SDP_IEEE_11073_20601;
662         evt_data.sdp_info_ind.mcap_sup_procs = BTA_HL_MCAP_SUP_PROC_MASK ;
663 
664         if (p_data->api_reg.p_cback)
665         {
666             p_data->api_reg.p_cback(BTA_HL_SDP_INFO_IND_EVT, (tBTA_HL *) &evt_data);
667         }
668     }
669 }
670 
671 /*******************************************************************************
672 **
673 ** Function         bta_hl_api_register
674 **
675 ** Description      Process the API registration request to register an HDP applciation
676 **
677 ** Returns          void
678 **
679 *******************************************************************************/
bta_hl_api_register(tBTA_HL_CB * p_cb,tBTA_HL_DATA * p_data)680 static void bta_hl_api_register(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
681 {
682     tBTA_HL         evt_data;
683     UINT8           app_idx;
684     tBTA_HL_APP_CB  *p_acb = NULL;
685     tBTA_HL_STATUS  status = BTA_HL_STATUS_FAIL;
686 
687 
688     if (p_cb->enable)
689     {
690         if (!bta_hl_is_a_duplicate_id(p_data->api_reg.app_id))
691         {
692             if (bta_hl_find_avail_app_idx(&app_idx))
693             {
694                 p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
695                 p_acb->in_use = TRUE;
696                 p_acb->app_id = p_data->api_reg.app_id;
697                 p_acb->p_cback = p_data->api_reg.p_cback;
698                 p_acb->sec_mask = p_data->api_reg.sec_mask;
699                 p_acb->dev_type = p_data->api_reg.dev_type;
700                 strlcpy(p_acb->srv_name, p_data->api_reg.srv_name, BTA_SERVICE_NAME_LEN);
701                 strlcpy(p_acb->srv_desp, p_data->api_reg.srv_desp, BTA_SERVICE_DESP_LEN);
702                 strlcpy(p_acb->provider_name, p_data->api_reg.provider_name, BTA_PROVIDER_NAME_LEN);
703                 bta_hl_cb.p_alloc_psm = L2CA_AllocatePSM;
704                 p_acb->ctrl_psm = bta_hl_cb.p_alloc_psm();
705                 p_acb->data_psm = bta_hl_cb.p_alloc_psm();
706                 p_acb->p_mcap_cback = bta_hl_mcap_ctrl_cback;
707                 status = bta_hl_app_registration(app_idx);
708             }
709             else
710             {
711                 status = BTA_HL_STATUS_NO_RESOURCE;
712             }
713         }
714         else
715         {
716             status = BTA_HL_STATUS_DUPLICATE_APP_ID;
717         }
718     }
719 
720     if (status != BTA_HL_STATUS_OK)
721     {
722         if ((status != BTA_HL_STATUS_DUPLICATE_APP_ID) &&
723             (status != BTA_HL_STATUS_NO_RESOURCE))
724         {
725             if (p_acb)
726                 memset(p_acb, 0, sizeof(tBTA_HL_APP_CB));
727         }
728     }
729 #if BTA_HL_DEBUG == TRUE
730     if (status != BTA_HL_STATUS_OK)
731     {
732         APPL_TRACE_DEBUG("bta_hl_api_register status =%s", bta_hl_status_code(status));
733     }
734 #endif
735 
736     memset(&evt_data, 0, sizeof(tBTA_HL));
737     evt_data.reg_cfm.status = status;
738     evt_data.reg_cfm.app_id = p_data->api_reg.app_id;
739     if (status == BTA_HL_STATUS_OK)
740         evt_data.reg_cfm.app_handle = p_acb->app_handle;
741     if (p_data->api_reg.p_cback)
742     {
743         p_data->api_reg.p_cback(BTA_HL_REGISTER_CFM_EVT, (tBTA_HL *) &evt_data);
744     }
745 
746     if (status == BTA_HL_STATUS_OK)
747     {
748         evt_data.sdp_info_ind.app_handle = p_acb->app_handle;
749         evt_data.sdp_info_ind.ctrl_psm = p_acb->ctrl_psm;
750         evt_data.sdp_info_ind.data_psm = p_acb->data_psm;
751         evt_data.sdp_info_ind.data_x_spec = BTA_HL_SDP_IEEE_11073_20601;
752         evt_data.sdp_info_ind.mcap_sup_procs = BTA_HL_MCAP_SUP_PROC_MASK ;
753 
754         if (p_data->api_reg.p_cback)
755         {
756             p_data->api_reg.p_cback(BTA_HL_SDP_INFO_IND_EVT, (tBTA_HL *) &evt_data);
757         }
758     }
759 }
760 
761 /*******************************************************************************
762 **
763 ** Function         bta_hl_api_deregister
764 **
765 ** Description      Process the API de-registration request
766 **
767 ** Returns          void
768 **
769 *******************************************************************************/
bta_hl_api_deregister(tBTA_HL_CB * p_cb,tBTA_HL_DATA * p_data)770 static void bta_hl_api_deregister(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
771 {
772     UINT8           app_idx;
773     tBTA_HL_APP_CB  *p_acb;
774     UNUSED(p_cb);
775 
776     if (bta_hl_find_app_idx_using_handle(p_data->api_dereg.app_handle, &app_idx))
777     {
778         p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
779         p_acb->deregistering= TRUE;
780         bta_hl_check_deregistration(app_idx,p_data);
781     }
782     else
783     {
784         APPL_TRACE_ERROR("Invalid app_handle=%d", p_data->api_dereg.app_handle);
785     }
786 }
787 
788 /*******************************************************************************
789 **
790 ** Function         bta_hl_api_cch_open
791 **
792 ** Description      Process the API CCH open request
793 **
794 ** Returns          void
795 **
796 *******************************************************************************/
bta_hl_api_cch_open(tBTA_HL_CB * p_cb,tBTA_HL_DATA * p_data)797 static void bta_hl_api_cch_open(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
798 {
799     tBTA_HL         evt_data;
800     tBTA_HL_STATUS  status = BTA_HL_STATUS_OK;
801     UINT8           app_idx, mcl_idx;
802     tBTA_HL_APP_CB  *p_acb;
803     tBTA_HL_MCL_CB  *p_mcb;
804     UNUSED(p_cb);
805 
806     if (bta_hl_find_app_idx_using_handle(p_data->api_cch_open.app_handle, &app_idx))
807     {
808 
809         if (!bta_hl_find_mcl_idx(app_idx, p_data->api_cch_open.bd_addr, &mcl_idx))
810         {
811             if (bta_hl_find_avail_mcl_idx(app_idx, &mcl_idx))
812             {
813                 p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
814                 p_mcb->in_use = TRUE;
815                 p_mcb->req_ctrl_psm = p_data->api_cch_open.ctrl_psm;
816                 p_mcb->sec_mask = p_data->api_cch_open.sec_mask;
817                 bdcpy(p_mcb->bd_addr, p_data->api_cch_open.bd_addr);
818                 p_mcb->cch_oper = BTA_HL_CCH_OP_LOCAL_OPEN;
819             }
820             else
821             {
822                 status = BTA_HL_STATUS_NO_RESOURCE;
823             }
824         }
825         else
826         {
827             /* Only one MCL per BD_ADDR */
828             status = BTA_HL_STATUS_DUPLICATE_CCH_OPEN;
829             APPL_TRACE_DEBUG("bta_hl_api_cch_open: CCH already open: status =%d",status)
830             p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
831             p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
832             if (p_acb->p_cback)
833             {
834                 bta_hl_build_cch_open_cfm(&evt_data, p_data->api_cch_open.app_id,
835                                           p_data->api_cch_open.app_handle,
836                                           p_mcb->mcl_handle,
837                                           p_data->api_cch_open.bd_addr,
838                                           status);
839                 p_acb->p_cback(BTA_HL_CCH_OPEN_CFM_EVT,(tBTA_HL *) &evt_data );
840             }
841             else
842             {
843                 APPL_TRACE_ERROR("bta_hl_api_cch_open Null Callback");
844             }
845             return;
846         }
847     }
848     else
849     {
850         status = BTA_HL_STATUS_INVALID_APP_HANDLE;
851     }
852 #if BTA_HL_DEBUG == TRUE
853     if (status != BTA_HL_STATUS_OK)
854     {
855         APPL_TRACE_DEBUG("bta_hl_api_cch_open status =%s", bta_hl_status_code(status));
856     }
857 #endif
858     switch (status)
859     {
860         case BTA_HL_STATUS_OK:
861             bta_hl_cch_sm_execute(app_idx, mcl_idx, BTA_HL_CCH_OPEN_EVT, p_data);
862             break;
863         case BTA_HL_STATUS_NO_RESOURCE:
864         case BTA_HL_STATUS_FAIL:
865 
866             p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
867             if (p_acb->p_cback)
868             {
869                 bta_hl_build_cch_open_cfm(&evt_data, p_data->api_cch_open.app_id,
870                                           p_data->api_cch_open.app_handle,
871                                           0,
872                                           p_data->api_cch_open.bd_addr,
873                                           status);
874                 p_acb->p_cback(BTA_HL_CCH_OPEN_CFM_EVT,(tBTA_HL *) &evt_data );
875             }
876             else
877             {
878                 APPL_TRACE_ERROR("bta_hl_api_cch_open Null Callback");
879             }
880             break;
881         default:
882             APPL_TRACE_ERROR("status code=%d", status);
883             break;
884     }
885 }
886 
887 /*******************************************************************************
888 **
889 ** Function         bta_hl_api_cch_close
890 **
891 ** Description      Process the API CCH close request
892 **
893 ** Returns          void
894 **
895 *******************************************************************************/
bta_hl_api_cch_close(tBTA_HL_CB * p_cb,tBTA_HL_DATA * p_data)896 static void bta_hl_api_cch_close(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
897 {
898     tBTA_HL         evt_data;
899     tBTA_HL_STATUS  status = BTA_HL_STATUS_OK;
900     UINT8           app_idx, mcl_idx;
901     tBTA_HL_APP_CB  *p_acb;
902     tBTA_HL_MCL_CB  *p_mcb;
903     UNUSED(p_cb);
904 
905     if (bta_hl_find_mcl_idx_using_handle(p_data->api_cch_close.mcl_handle, &app_idx,  &mcl_idx))
906     {
907         p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
908         p_mcb->cch_oper = BTA_HL_CCH_OP_LOCAL_CLOSE;
909     }
910     else
911     {
912         status = BTA_HL_STATUS_INVALID_MCL_HANDLE;
913     }
914 #if BTA_HL_DEBUG == TRUE
915     if (status != BTA_HL_STATUS_OK)
916     {
917         APPL_TRACE_DEBUG("bta_hl_api_cch_close status =%s", bta_hl_status_code(status));
918     }
919 #endif
920     switch (status)
921     {
922         case BTA_HL_STATUS_OK:
923             bta_hl_check_cch_close(app_idx, mcl_idx, p_data, TRUE);
924             break;
925 
926         case BTA_HL_STATUS_INVALID_MCL_HANDLE:
927             p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
928             if (p_acb->p_cback)
929             {
930                 bta_hl_build_cch_close_cfm(&evt_data,
931                                        p_acb->app_handle,
932                                        p_data->api_cch_close.mcl_handle,
933                                        status);
934                 p_acb->p_cback(BTA_HL_CCH_CLOSE_CFM_EVT,(tBTA_HL *) &evt_data );
935             }
936             else
937             {
938                 APPL_TRACE_ERROR("bta_hl_api_cch_close Null Callback");
939             }
940             break;
941         default:
942             APPL_TRACE_ERROR("status code=%d", status);
943             break;
944     }
945 
946 }
947 
948 /*******************************************************************************
949 **
950 ** Function         bta_hl_api_dch_open
951 **
952 ** Description      Process the API DCH open request
953 **
954 ** Returns          void
955 **
956 *******************************************************************************/
bta_hl_api_dch_open(tBTA_HL_CB * p_cb,tBTA_HL_DATA * p_data)957 static void bta_hl_api_dch_open(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
958 {
959     tBTA_HL                     evt_data;
960     tBTA_HL_STATUS              status = BTA_HL_STATUS_OK;
961     UINT8                       app_idx, mcl_idx, mdl_idx;
962     tBTA_HL_APP_CB              *p_acb;
963     tBTA_HL_MCL_CB              *p_mcb = NULL;
964     tBTA_HL_MDL_CB              *p_dcb;
965     tBTA_HL_MDEP_CFG            *p_mdep_cfg;
966     UINT8                       mdep_cfg_idx;
967     UNUSED(p_cb);
968 
969     if (bta_hl_find_mcl_idx_using_handle(p_data->api_dch_open.mcl_handle, &app_idx, &mcl_idx))
970     {
971         p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
972         p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
973 
974         APPL_TRACE_DEBUG("bta_hl_api_dch_open: app_ix=%d, mcl_idx=%d, cch_state=%d, mcl_handle=%d",app_idx,mcl_idx,p_mcb->cch_state,p_data->api_dch_open.mcl_handle);
975         if (p_mcb->cch_state == BTA_HL_CCH_OPEN_ST)
976         {
977             if (bta_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx))
978             {
979                 p_dcb = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
980 
981                 if (bta_hl_find_mdep_cfg_idx(app_idx, p_data->api_dch_open.local_mdep_id, &mdep_cfg_idx))
982                 {
983                     if ( mdep_cfg_idx &&
984                          (p_acb->sup_feature.mdep[mdep_cfg_idx].mdep_cfg.mdep_role == BTA_HL_MDEP_ROLE_SINK))
985                     {
986                         p_data->api_dch_open.local_cfg = BTA_HL_DCH_CFG_NO_PREF;
987                     }
988 
989                     if ((status = bta_hl_chk_local_cfg(app_idx,mcl_idx,mdep_cfg_idx,p_data->api_dch_open.local_cfg))
990                         == BTA_HL_STATUS_OK)
991                     {
992 
993                         if (p_data->api_dch_open.local_mdep_id != BTA_HL_ECHO_TEST_MDEP_ID)
994                         {
995                             if (bta_hl_set_ctrl_psm_for_dch(app_idx, mcl_idx, mdl_idx, p_data->api_dch_open.ctrl_psm))
996                             {
997                                 p_mdep_cfg = BTA_HL_GET_MDEP_CFG_PTR(app_idx, mdep_cfg_idx);
998                                 p_dcb->in_use                   = TRUE;
999                                 p_dcb->dch_oper                 = BTA_HL_DCH_OP_LOCAL_OPEN;
1000                                 p_dcb->sec_mask                 = p_data->api_dch_open.sec_mask;
1001                                 p_dcb->local_mdep_id            = p_data->api_dch_open.local_mdep_id;
1002                                 p_dcb->peer_mdep_id             = p_data->api_dch_open.peer_mdep_id;
1003 
1004                                 if (p_mdep_cfg->mdep_role == BTA_HL_MDEP_ROLE_SINK)
1005                                 {
1006                                     p_dcb->peer_mdep_role = BTA_HL_MDEP_ROLE_SOURCE;
1007                                 }
1008                                 else
1009                                 {
1010                                     p_dcb->peer_mdep_role = BTA_HL_MDEP_ROLE_SINK;
1011                                 }
1012 
1013                                 p_dcb->local_mdep_cfg_idx   = mdep_cfg_idx;
1014                                 p_dcb->local_cfg            = p_data->api_dch_open.local_cfg;
1015 
1016                                 bta_hl_find_rxtx_apdu_size(app_idx, mdep_cfg_idx,
1017                                                            &p_dcb->max_rx_apdu_size,
1018                                                            &p_dcb->max_tx_apdu_size);
1019                                 p_dcb->mdl_id               = bta_hl_allocate_mdl_id(app_idx,mcl_idx,mdl_idx);
1020                                 p_dcb->mdl_cfg_idx_included = FALSE;
1021                             }
1022                             else
1023                             {
1024                                 status =  BTA_HL_STATUS_INVALID_CTRL_PSM;
1025                             }
1026 
1027                         }
1028                         else
1029                         {
1030                             status =  BTA_HL_STATUS_INVALID_LOCAL_MDEP_ID;
1031                         }
1032                     }
1033                 }
1034                 else
1035                 {
1036                     status =  BTA_HL_STATUS_INVALID_LOCAL_MDEP_ID;
1037                 }
1038             }
1039             else
1040             {
1041                 status = BTA_HL_STATUS_NO_RESOURCE;
1042             }
1043         }
1044         else
1045         {
1046             status =  BTA_HL_STATUS_NO_CCH;
1047         }
1048     }
1049     else
1050     {
1051         status = BTA_HL_STATUS_INVALID_MCL_HANDLE;
1052     }
1053 
1054 #if BTA_HL_DEBUG == TRUE
1055     if (status != BTA_HL_STATUS_OK)
1056     {
1057         APPL_TRACE_DEBUG("bta_hl_api_dch_open status =%s", bta_hl_status_code(status));
1058     }
1059 #endif
1060     switch (status)
1061     {
1062         case BTA_HL_STATUS_OK:
1063             if (p_mcb->sdp.num_recs)
1064             {
1065                 bta_hl_dch_sm_execute(app_idx, mcl_idx, mdl_idx, BTA_HL_DCH_OPEN_EVT, p_data);
1066             }
1067             else
1068             {
1069                 bta_hl_dch_sm_execute(app_idx, mcl_idx, mdl_idx, BTA_HL_DCH_SDP_INIT_EVT, p_data);
1070             }
1071             break;
1072         case BTA_HL_STATUS_INVALID_DCH_CFG:
1073         case BTA_HL_STATUS_NO_FIRST_RELIABLE:
1074         case BTA_HL_STATUS_NO_CCH:
1075         case BTA_HL_STATUS_NO_RESOURCE:
1076         case BTA_HL_STATUS_FAIL:
1077         case BTA_HL_STATUS_INVALID_LOCAL_MDEP_ID:
1078         case BTA_HL_STATUS_INVALID_CTRL_PSM:
1079             p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
1080             if (p_acb->p_cback)
1081             {
1082                 bta_hl_build_dch_open_cfm(&evt_data,
1083                                           p_acb->app_handle,
1084                                           p_data->api_dch_open.mcl_handle,
1085                                           BTA_HL_INVALID_MDL_HANDLE,
1086                                           0,0,0,0,0, status);
1087                 p_acb->p_cback(BTA_HL_DCH_OPEN_CFM_EVT,(tBTA_HL *) &evt_data );
1088             }
1089             else
1090             {
1091                 APPL_TRACE_ERROR("bta_hl_api_dch_open Null Callback");
1092             }
1093 
1094             break;
1095         default:
1096             APPL_TRACE_ERROR("Status code=%d", status);
1097             break;
1098 
1099     }
1100 
1101 }
1102 /*******************************************************************************
1103 **
1104 ** Function         bta_hl_api_dch_close
1105 **
1106 ** Description      Process the API DCH close request
1107 **
1108 ** Returns          void
1109 **
1110 *******************************************************************************/
bta_hl_api_dch_close(tBTA_HL_CB * p_cb,tBTA_HL_DATA * p_data)1111 static void bta_hl_api_dch_close(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
1112 {
1113     tBTA_HL         evt_data;
1114     tBTA_HL_STATUS  status = BTA_HL_STATUS_OK;
1115     UINT8           app_idx, mcl_idx, mdl_idx;
1116     tBTA_HL_APP_CB  *p_acb;
1117     tBTA_HL_MCL_CB  *p_mcb;
1118     tBTA_HL_MDL_CB  *p_dcb;
1119     UNUSED(p_cb);
1120 
1121     if (bta_hl_find_mdl_idx_using_handle(p_data->api_dch_close.mdl_handle, &app_idx, &mcl_idx, &mdl_idx ))
1122     {
1123         p_dcb = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
1124         if (p_dcb->dch_state != BTA_HL_DCH_OPEN_ST)
1125         {
1126             status =  BTA_HL_STATUS_FAIL;
1127         }
1128     }
1129     else
1130     {
1131         status = BTA_HL_STATUS_INVALID_MDL_HANDLE;
1132     }
1133 
1134 #if BTA_HL_DEBUG == TRUE
1135     if (status != BTA_HL_STATUS_OK)
1136     {
1137         APPL_TRACE_DEBUG("bta_hl_api_dch_close status =%s", bta_hl_status_code(status));
1138     }
1139 #endif
1140 
1141     switch (status)
1142     {
1143         case BTA_HL_STATUS_OK:
1144             bta_hl_dch_sm_execute(app_idx, mcl_idx, mdl_idx, BTA_HL_DCH_CLOSE_EVT, p_data);
1145             break;
1146         case BTA_HL_STATUS_FAIL:
1147             p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
1148             if (p_acb->p_cback)
1149             {
1150                 p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
1151                 bta_hl_build_dch_close_cfm(&evt_data,
1152                                            p_acb->app_handle,
1153                                            p_mcb->mcl_handle,
1154                                            p_data->api_dch_close.mdl_handle,
1155                                            status);
1156 
1157                 p_acb->p_cback(BTA_HL_DCH_CLOSE_CFM_EVT,(tBTA_HL *) &evt_data );
1158             }
1159             else
1160             {
1161                 APPL_TRACE_ERROR("bta_hl_api_dch_close Null Callback");
1162             }
1163             break;
1164         default:
1165             APPL_TRACE_ERROR("Status code=%d", status);
1166             break;
1167     }
1168 }
1169 
1170 
1171 /*******************************************************************************
1172 **
1173 ** Function         bta_hl_api_dch_reconnect
1174 **
1175 ** Description      Process the API DCH reconnect request
1176 **
1177 ** Returns          void
1178 **
1179 *******************************************************************************/
bta_hl_api_dch_reconnect(tBTA_HL_CB * p_cb,tBTA_HL_DATA * p_data)1180 static void bta_hl_api_dch_reconnect(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
1181 {
1182     tBTA_HL         evt_data;
1183     tBTA_HL_STATUS  status = BTA_HL_STATUS_OK;
1184     UINT8           app_idx, mcl_idx, mdl_idx;
1185     tBTA_HL_APP_CB  *p_acb;
1186     tBTA_HL_MCL_CB  *p_mcb = NULL;
1187     tBTA_HL_MDL_CB  *p_dcb;
1188     UINT8           mdep_cfg_idx;
1189     UINT8           mdl_cfg_idx;
1190     tBTA_HL_MDEP_CFG            *p_mdep_cfg;
1191     UNUSED(p_cb);
1192 
1193     if (bta_hl_find_mcl_idx_using_handle(p_data->api_dch_reconnect.mcl_handle, &app_idx, &mcl_idx))
1194     {
1195         p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
1196         p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
1197         if (p_mcb->cch_state == BTA_HL_CCH_OPEN_ST)
1198         {
1199             if (bta_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx))
1200             {
1201                 p_dcb = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
1202                 if (bta_hl_validate_reconnect_params(app_idx, mcl_idx,  &(p_data->api_dch_reconnect),
1203                                                      &mdep_cfg_idx, &mdl_cfg_idx ))
1204                 {
1205                     if (!bta_hl_is_the_first_reliable_existed(app_idx, mcl_idx) &&
1206                         (p_acb->mdl_cfg[mdl_cfg_idx].dch_mode != BTA_HL_DCH_MODE_RELIABLE))
1207                     {
1208                         status =  BTA_HL_STATUS_NO_FIRST_RELIABLE;
1209                     }
1210                     else
1211                     {
1212                         if (bta_hl_set_ctrl_psm_for_dch(app_idx, mcl_idx, mdl_idx, p_data->api_dch_open.ctrl_psm))
1213                         {
1214                             p_dcb->in_use                   = TRUE;
1215                             p_dcb->dch_oper                 = BTA_HL_DCH_OP_LOCAL_RECONNECT;
1216                             p_dcb->sec_mask                 = (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT);
1217                             p_dcb->local_mdep_id            = p_acb->mdl_cfg[mdl_cfg_idx].local_mdep_id;
1218                             p_dcb->local_mdep_cfg_idx       = mdep_cfg_idx;
1219                             p_dcb->local_cfg                = BTA_HL_DCH_CFG_UNKNOWN;
1220                             p_dcb->mdl_id                   = p_data->api_dch_reconnect.mdl_id;
1221                             p_dcb->mdl_cfg_idx_included     = TRUE;
1222                             p_dcb->mdl_cfg_idx              = mdl_cfg_idx;
1223                             p_dcb->dch_mode                 = p_acb->mdl_cfg[mdl_cfg_idx].dch_mode;
1224 
1225                            p_mdep_cfg = BTA_HL_GET_MDEP_CFG_PTR(app_idx, mdep_cfg_idx);
1226 
1227                             if (p_mdep_cfg->mdep_role == BTA_HL_MDEP_ROLE_SINK)
1228                             {
1229                                 p_dcb->peer_mdep_role = BTA_HL_MDEP_ROLE_SOURCE;
1230                                   APPL_TRACE_DEBUG("peer mdep role = SOURCE ");
1231 							}
1232                             else
1233                             {
1234                                 p_dcb->peer_mdep_role = BTA_HL_MDEP_ROLE_SINK;
1235                                 APPL_TRACE_DEBUG("peer mdep role = SINK ");
1236                             }
1237 
1238                             bta_hl_find_rxtx_apdu_size(app_idx, mdep_cfg_idx,
1239                                                        &p_dcb->max_rx_apdu_size,
1240                                                        &p_dcb->max_tx_apdu_size);
1241                         }
1242                         else
1243                         {
1244                             status =  BTA_HL_STATUS_INVALID_CTRL_PSM;
1245                         }
1246                     }
1247                 }
1248                 else
1249                 {
1250                     status =  BTA_HL_STATUS_INVALID_RECONNECT_CFG;
1251                 }
1252             }
1253             else
1254             {
1255                 status = BTA_HL_STATUS_NO_RESOURCE;
1256             }
1257         }
1258         else
1259         {
1260             status =  BTA_HL_STATUS_NO_CCH;
1261         }
1262     }
1263     else
1264     {
1265         status = BTA_HL_STATUS_INVALID_MCL_HANDLE;
1266     }
1267 
1268 #if BTA_HL_DEBUG == TRUE
1269     if (status != BTA_HL_STATUS_OK)
1270     {
1271         APPL_TRACE_DEBUG("bta_hl_api_dch_reconnect status=%s", bta_hl_status_code(status));
1272     }
1273 #endif
1274 
1275     switch (status)
1276     {
1277         case BTA_HL_STATUS_OK:
1278             if (p_mcb->sdp.num_recs)
1279             {
1280                 bta_hl_dch_sm_execute(app_idx, mcl_idx, mdl_idx, BTA_HL_DCH_RECONNECT_EVT, p_data);
1281             }
1282             else
1283             {
1284                 bta_hl_dch_sm_execute(app_idx, mcl_idx, mdl_idx, BTA_HL_DCH_SDP_INIT_EVT, p_data);
1285             }
1286             break;
1287         case BTA_HL_STATUS_INVALID_RECONNECT_CFG:
1288         case BTA_HL_STATUS_NO_FIRST_RELIABLE:
1289         case BTA_HL_STATUS_NO_CCH:
1290         case BTA_HL_STATUS_NO_RESOURCE:
1291             p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
1292             if (p_acb->p_cback)
1293             {
1294                 bta_hl_build_dch_open_cfm(&evt_data,
1295                                           p_acb->app_handle,
1296                                           p_data->api_dch_reconnect.mcl_handle,
1297                                           BTA_HL_INVALID_MDL_HANDLE,
1298                                           0,p_data->api_dch_reconnect.mdl_id,0,0,0, status);
1299                 p_acb->p_cback(BTA_HL_DCH_RECONNECT_CFM_EVT,(tBTA_HL *) &evt_data );
1300             }
1301             else
1302             {
1303                 APPL_TRACE_ERROR("bta_hl_api_dch_reconnect Null Callback");
1304             }
1305             break;
1306         default:
1307             APPL_TRACE_ERROR("Status code=%d", status);
1308             break;
1309     }
1310 }
1311 
1312 /*******************************************************************************
1313 **
1314 ** Function         bta_hl_api_dch_echo_test
1315 **
1316 ** Description      Process the API Echo test request
1317 **
1318 ** Returns          void
1319 **
1320 *******************************************************************************/
bta_hl_api_dch_echo_test(tBTA_HL_CB * p_cb,tBTA_HL_DATA * p_data)1321 static void bta_hl_api_dch_echo_test(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
1322 {
1323     tBTA_HL             evt_data;
1324     tBTA_HL_STATUS      status = BTA_HL_STATUS_OK;
1325     UINT8               app_idx, mcl_idx, mdl_idx;
1326     tBTA_HL_APP_CB      *p_acb;
1327     tBTA_HL_MCL_CB      *p_mcb = NULL;
1328     tBTA_HL_MDL_CB      *p_dcb;
1329     tBTA_HL_ECHO_CFG    *p_echo_cfg;
1330     UNUSED(p_cb);
1331 
1332     if (bta_hl_find_mcl_idx_using_handle(p_data->api_dch_echo_test.mcl_handle, &app_idx,  &mcl_idx))
1333     {
1334         p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
1335         p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
1336         if (p_mcb->cch_state == BTA_HL_CCH_OPEN_ST)
1337         {
1338             if (!p_mcb->echo_test )
1339             {
1340                 if (bta_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx))
1341                 {
1342                     p_dcb = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
1343 
1344                     if ((p_data->api_dch_echo_test.local_cfg == BTA_HL_DCH_CFG_RELIABLE) ||
1345                         (p_data->api_dch_echo_test.local_cfg == BTA_HL_DCH_CFG_STREAMING))
1346                     {
1347                         BOOLEAN fcs_use = (BOOLEAN) (p_dcb->chnl_cfg.fcs & BTA_HL_MCA_FCS_USE_MASK);
1348                         if ((p_dcb->p_echo_tx_pkt = bta_hl_get_buf(p_data->api_dch_echo_test.pkt_size, fcs_use)) != NULL )
1349                         {
1350                             if (bta_hl_set_ctrl_psm_for_dch(app_idx, mcl_idx, mdl_idx, p_data->api_dch_open.ctrl_psm))
1351                             {
1352                                 p_dcb->in_use                   = TRUE;
1353                                 p_dcb->dch_oper                 = BTA_HL_DCH_OP_LOCAL_OPEN;
1354                                 p_dcb->sec_mask                 = (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT);
1355                                 p_dcb->local_mdep_cfg_idx       = BTA_HL_ECHO_TEST_MDEP_CFG_IDX;
1356                                 p_dcb->local_cfg                = p_data->api_dch_echo_test.local_cfg;
1357                                 p_dcb->local_mdep_id            = BTA_HL_ECHO_TEST_MDEP_ID;
1358                                 p_dcb->peer_mdep_id             = BTA_HL_ECHO_TEST_MDEP_ID;
1359                                 p_dcb->mdl_id                   = bta_hl_allocate_mdl_id(app_idx,mcl_idx,mdl_idx);
1360                                 p_dcb->mdl_cfg_idx_included     = FALSE;
1361                                 p_echo_cfg                      = BTA_HL_GET_ECHO_CFG_PTR(app_idx);
1362                                 p_dcb->max_rx_apdu_size         = p_echo_cfg->max_rx_apdu_size;
1363                                 p_dcb->max_tx_apdu_size         = p_echo_cfg->max_tx_apdu_size;
1364                                 p_mcb->echo_test                = TRUE;
1365                                 p_mcb->echo_mdl_idx             = mdl_idx;
1366                             }
1367                             else
1368                             {
1369                                 status =  BTA_HL_STATUS_INVALID_CTRL_PSM;
1370                             }
1371                         }
1372                         else
1373                         {
1374                             status = BTA_HL_STATUS_NO_RESOURCE;
1375                         }
1376                     }
1377                     else
1378                     {
1379                         status = BTA_HL_STATUS_INVALID_DCH_CFG;
1380                     }
1381                 }
1382                 else
1383                 {
1384                     status = BTA_HL_STATUS_NO_RESOURCE;
1385                 }
1386             }
1387             else
1388             {
1389                 status = BTA_HL_STATUS_ECHO_TEST_BUSY;
1390             }
1391         }
1392         else
1393         {
1394             status =  BTA_HL_STATUS_NO_CCH;
1395         }
1396     }
1397     else
1398     {
1399         status = BTA_HL_STATUS_NO_MCL;
1400     }
1401 
1402 #if BTA_HL_DEBUG == TRUE
1403     if (status != BTA_HL_STATUS_OK)
1404     {
1405         APPL_TRACE_DEBUG("bta_hl_api_dch_echo_test status=%s", bta_hl_status_code(status));
1406     }
1407 #endif
1408 
1409     switch (status)
1410     {
1411         case BTA_HL_STATUS_OK:
1412             if (p_mcb->sdp.num_recs)
1413             {
1414                 bta_hl_dch_sm_execute(app_idx, mcl_idx, mdl_idx, BTA_HL_DCH_ECHO_TEST_EVT, p_data);
1415             }
1416             else
1417             {
1418                 bta_hl_dch_sm_execute(app_idx, mcl_idx, mdl_idx, BTA_HL_DCH_SDP_INIT_EVT, p_data);
1419             }
1420             break;
1421         case BTA_HL_STATUS_ECHO_TEST_BUSY:
1422         case BTA_HL_STATUS_NO_RESOURCE:
1423         case BTA_HL_STATUS_INVALID_DCH_CFG:
1424             p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
1425             if (p_acb->p_cback)
1426             {
1427                 bta_hl_build_echo_test_cfm(&evt_data,
1428                                            p_acb->app_handle,
1429                                            p_mcb->mcl_handle,
1430                                            status);
1431                 p_acb->p_cback(BTA_HL_DCH_ECHO_TEST_CFM_EVT,(tBTA_HL *) &evt_data );
1432             }
1433             else
1434             {
1435                 APPL_TRACE_ERROR("bta_hl_api_dch_echo_test Null Callback");
1436             }
1437             break;
1438 
1439         default:
1440             APPL_TRACE_ERROR("Status code=%s", status);
1441             break;
1442     }
1443 }
1444 
1445 
1446 /*******************************************************************************
1447 **
1448 ** Function         bta_hl_api_sdp_query
1449 **
1450 ** Description      Process the API SDP query request
1451 **
1452 ** Returns          void
1453 **
1454 *******************************************************************************/
bta_hl_api_sdp_query(tBTA_HL_CB * p_cb,tBTA_HL_DATA * p_data)1455 static void bta_hl_api_sdp_query(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
1456 {
1457     tBTA_HL         evt_data;
1458     tBTA_HL_STATUS  status = BTA_HL_STATUS_OK;
1459     UINT8           app_idx, mcl_idx;
1460     tBTA_HL_APP_CB  *p_acb;
1461     tBTA_HL_MCL_CB  *p_mcb;
1462     UNUSED(p_cb);
1463 
1464     if (bta_hl_find_app_idx_using_handle(p_data->api_sdp_query.app_handle, &app_idx))
1465     {
1466         if (!bta_hl_find_mcl_idx(app_idx, p_data->api_sdp_query.bd_addr, &mcl_idx))
1467         {
1468             if (bta_hl_find_avail_mcl_idx(app_idx, &mcl_idx))
1469             {
1470                 p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
1471                 p_mcb->in_use = TRUE;
1472                 bdcpy(p_mcb->bd_addr, p_data->api_sdp_query.bd_addr);
1473                 APPL_TRACE_DEBUG("bta_hl_api_sdp_query p_mcb->app_id %d app_idx %d mcl_idx %d", p_mcb->app_id, app_idx, mcl_idx);
1474                 p_mcb->app_id = p_data->api_sdp_query.app_id;
1475                 p_mcb->sdp_oper  = BTA_HL_SDP_OP_SDP_QUERY_NEW ;
1476             }
1477             else
1478             {
1479                 status = BTA_HL_STATUS_NO_RESOURCE;
1480             }
1481         }
1482         else
1483         {
1484             p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
1485             p_mcb->app_id = p_data->api_sdp_query.app_id;
1486             if (p_mcb->sdp_oper != BTA_HL_SDP_OP_NONE)
1487             {
1488                 status = BTA_HL_STATUS_SDP_NO_RESOURCE;
1489             }
1490             else
1491             {
1492                 p_mcb->sdp_oper  = BTA_HL_SDP_OP_SDP_QUERY_CURRENT;
1493             }
1494         }
1495     }
1496     else
1497     {
1498         status = BTA_HL_STATUS_INVALID_APP_HANDLE;
1499     }
1500 
1501     if (status == BTA_HL_STATUS_OK)
1502     {
1503         status = bta_hl_init_sdp( p_mcb->sdp_oper, app_idx, mcl_idx, 0xFF);
1504         if ( (status != BTA_HL_STATUS_OK) &&
1505              (p_mcb->sdp_oper == BTA_HL_SDP_OP_SDP_QUERY_NEW) )
1506         {
1507             memset(p_mcb, 0 ,sizeof(tBTA_HL_MCL_CB));
1508         }
1509     }
1510 #if BTA_HL_DEBUG == TRUE
1511     if (status != BTA_HL_STATUS_OK)
1512     {
1513         APPL_TRACE_DEBUG("bta_hl_api_sdp_query status=%s", bta_hl_status_code(status));
1514     }
1515 #endif
1516     switch (status)
1517     {
1518         case BTA_HL_STATUS_NO_RESOURCE:
1519         case BTA_HL_STATUS_FAIL:
1520         case BTA_HL_STATUS_SDP_NO_RESOURCE:
1521             p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
1522             if (p_acb->p_cback)
1523             {
1524                 bta_hl_build_sdp_query_cfm(&evt_data,
1525                                            p_data->api_sdp_query.app_id,
1526                                            p_data->api_sdp_query.app_handle,
1527                                            p_data->api_sdp_query.bd_addr,
1528                                            NULL,
1529                                            status);
1530                 p_acb->p_cback(BTA_HL_SDP_QUERY_CFM_EVT,(tBTA_HL *) &evt_data );
1531             }
1532             else
1533             {
1534                 APPL_TRACE_ERROR("bta_hl_api_sdp_query Null Callback");
1535             }
1536             break;
1537         case BTA_HL_STATUS_OK:
1538             break;
1539         default:
1540             APPL_TRACE_ERROR("Status code=%d", status);
1541             break;
1542     }
1543 }
1544 
1545 
1546 
1547 
1548 /*******************************************************************************
1549 **
1550 ** Function         bta_hl_sdp_query_results
1551 **
1552 ** Description      Process the SDP query results
1553 **
1554 ** Returns          void
1555 **
1556 *******************************************************************************/
bta_hl_sdp_query_results(tBTA_HL_CB * p_cb,tBTA_HL_DATA * p_data)1557 static void bta_hl_sdp_query_results(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
1558 {
1559     tBTA_HL             evt_data;
1560     tBTA_HL_STATUS      status = BTA_HL_STATUS_OK;
1561     UINT8               app_idx = p_data->cch_sdp.app_idx;
1562     UINT8               mcl_idx = p_data->cch_sdp.mcl_idx;
1563     tBTA_HL_APP_CB      *p_acb = BTA_HL_GET_APP_CB_PTR( app_idx);
1564     tBTA_HL_MCL_CB      *p_mcb =  BTA_HL_GET_MCL_CB_PTR( app_idx,  mcl_idx);
1565     tBTA_HL_SDP         *p_sdp=NULL;
1566     UINT16              event;
1567     BOOLEAN             release_sdp_buf=FALSE;
1568     UNUSED(p_cb);
1569 
1570     event = p_data->hdr.event;
1571 
1572     if (event == BTA_HL_SDP_QUERY_OK_EVT) {
1573         p_sdp = (tBTA_HL_SDP *)osi_malloc(sizeof(tBTA_HL_SDP));
1574         memcpy(p_sdp, &p_mcb->sdp, sizeof(tBTA_HL_SDP));
1575         release_sdp_buf = TRUE;
1576     } else {
1577         status = BTA_HL_STATUS_SDP_FAIL;
1578     }
1579 
1580 #if BTA_HL_DEBUG == TRUE
1581     if (status != BTA_HL_STATUS_OK)
1582     {
1583         APPL_TRACE_DEBUG("bta_hl_sdp_query_results status=%s", bta_hl_status_code(status));
1584     }
1585 #endif
1586 
1587     APPL_TRACE_DEBUG("bta_hl_sdp_query_results p_mcb->app_id %d app_idx %d mcl_idx %d", p_mcb->app_id, app_idx, mcl_idx);
1588     bta_hl_build_sdp_query_cfm(&evt_data,p_mcb->app_id, p_acb->app_handle,
1589                                p_mcb->bd_addr,p_sdp,status);
1590     p_acb->p_cback(BTA_HL_SDP_QUERY_CFM_EVT,(tBTA_HL *) &evt_data );
1591 
1592     if (release_sdp_buf)
1593         osi_free_and_reset((void **)&p_sdp);
1594 
1595     if (p_data->cch_sdp.release_mcl_cb) {
1596         memset(p_mcb, 0, sizeof(tBTA_HL_MCL_CB));
1597     } else {
1598         if (p_mcb->close_pending)
1599             bta_hl_check_cch_close(app_idx, mcl_idx, p_data, TRUE);
1600 
1601         if (!p_mcb->ctrl_psm) {
1602             /* Control channel acceptor: do not store the SDP records */
1603             memset(&p_mcb->sdp, 0, sizeof(tBTA_HL_SDP));
1604         }
1605     }
1606 }
1607 
1608 
1609 /*******************************************************************************
1610 **
1611 ** Function         bta_hl_api_delete_mdl
1612 **
1613 ** Description      Process the API DELETE MDL request
1614 **
1615 ** Returns          void
1616 **
1617 *******************************************************************************/
bta_hl_api_delete_mdl(tBTA_HL_CB * p_cb,tBTA_HL_DATA * p_data)1618 static void bta_hl_api_delete_mdl(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
1619 {
1620     tBTA_HL         evt_data;
1621     tBTA_HL_STATUS  status = BTA_HL_STATUS_OK;
1622     UINT8           app_idx, mcl_idx;
1623     tBTA_HL_APP_CB  *p_acb;
1624     tBTA_HL_MCL_CB  *p_mcb;
1625     UNUSED(p_cb);
1626 
1627     if (bta_hl_find_mcl_idx_using_handle(p_data->api_delete_mdl.mcl_handle, &app_idx, &mcl_idx ))
1628     {
1629         if (bta_hl_is_mdl_value_valid(p_data->api_delete_mdl.mdl_id))
1630         {
1631             p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
1632             if (bta_hl_is_mdl_exsit_in_mcl(app_idx,
1633                                            p_mcb->bd_addr,
1634                                            p_data->api_delete_mdl.mdl_id))
1635             {
1636 
1637 
1638                 p_mcb->delete_mdl.mcl_handle =  p_data->api_delete_mdl.mcl_handle;
1639                 p_mcb->delete_mdl.mdl_id = p_data->api_delete_mdl.mdl_id;
1640                 p_mcb->delete_mdl.delete_req_pending = TRUE;
1641 
1642                 if (MCA_Delete((tMCA_CL) p_mcb->mcl_handle,
1643                                p_data->api_delete_mdl.mdl_id)!= MCA_SUCCESS)
1644                 {
1645                     status = BTA_HL_STATUS_FAIL;
1646                     memset(&p_mcb->delete_mdl, 0, sizeof(tBTA_HL_DELETE_MDL));
1647                 }
1648             }
1649             else
1650             {
1651                 status = BTA_HL_STATUS_NO_MDL_ID_FOUND;
1652             }
1653         }
1654         else
1655         {
1656             status = BTA_HL_STATUS_INVALID_MDL_ID;
1657         }
1658     }
1659     else
1660     {
1661         status = BTA_HL_STATUS_INVALID_MCL_HANDLE;
1662     }
1663 
1664 #if BTA_HL_DEBUG == TRUE
1665     if (status != BTA_HL_STATUS_OK)
1666     {
1667         APPL_TRACE_DEBUG("bta_hl_api_delete_mdl status=%s", bta_hl_status_code(status));
1668     }
1669 #endif
1670     switch (status)
1671     {
1672         case BTA_HL_STATUS_OK:
1673             break;
1674         case BTA_HL_STATUS_FAIL:
1675         case BTA_HL_STATUS_NO_MDL_ID_FOUND:
1676         case BTA_HL_STATUS_INVALID_MDL_ID:
1677             p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
1678             if (p_acb->p_cback)
1679             {
1680                 bta_hl_build_delete_mdl_cfm(&evt_data,
1681                                             p_acb->app_handle,
1682                                             p_data->api_delete_mdl.mcl_handle,
1683                                             p_data->api_delete_mdl.mdl_id,
1684                                             status);
1685                 p_acb->p_cback(BTA_HL_DELETE_MDL_CFM_EVT,(tBTA_HL *) &evt_data );
1686             }
1687             else
1688             {
1689                 APPL_TRACE_ERROR("bta_hl_api_delete_mdl Null Callback");
1690             }
1691             break;
1692         default:
1693             APPL_TRACE_ERROR("status code =%d", status);
1694             break;
1695     }
1696 }
1697 
1698 /*******************************************************************************
1699 **
1700 ** Function         bta_hl_mca_delete_mdl_cfm
1701 **
1702 ** Description      Process the DELETE MDL confirmation event
1703 **
1704 ** Returns          void
1705 **
1706 *******************************************************************************/
bta_hl_mca_delete_mdl_cfm(tBTA_HL_CB * p_cb,tBTA_HL_DATA * p_data)1707 static void bta_hl_mca_delete_mdl_cfm(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
1708 {
1709     tBTA_HL         evt_data;
1710     tBTA_HL_STATUS  status = BTA_HL_STATUS_OK;
1711     UINT8           app_idx, mcl_idx;
1712     tMCA_RSP_EVT    *p_delete_cfm = &p_data->mca_evt.mca_data.delete_cfm;
1713     tBTA_HL_MCL_CB  *p_mcb;
1714     BOOLEAN         send_cfm_evt = TRUE;
1715     tBTA_HL_APP_CB  *p_acb;
1716     UNUSED(p_cb);
1717 
1718     if (bta_hl_find_mcl_idx_using_handle(p_data->mca_evt.mcl_handle, &app_idx, &mcl_idx))
1719     {
1720         p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
1721         if ( p_mcb->delete_mdl.delete_req_pending)
1722         {
1723             if (p_delete_cfm->rsp_code == MCA_RSP_SUCCESS   )
1724             {
1725 
1726                 if (!bta_hl_delete_mdl_cfg(app_idx,
1727                                            p_mcb->bd_addr ,
1728                                            p_delete_cfm->mdl_id))
1729                 {
1730                     status = BTA_HL_STATUS_FAIL;
1731                 }
1732             }
1733             else
1734             {
1735                 status = BTA_HL_STATUS_FAIL;
1736             }
1737 
1738             memset(&p_mcb->delete_mdl, 0, sizeof(tBTA_HL_DELETE_MDL));
1739         }
1740         else
1741         {
1742             send_cfm_evt = FALSE;
1743         }
1744     }
1745     else
1746     {
1747         send_cfm_evt = FALSE;
1748     }
1749 
1750 #if BTA_HL_DEBUG == TRUE
1751     if (status != BTA_HL_STATUS_OK)
1752     {
1753         APPL_TRACE_DEBUG("bta_hl_api_delete_mdl status=%s", bta_hl_status_code(status));
1754     }
1755 #endif
1756 
1757     if (send_cfm_evt)
1758     {
1759         p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
1760         if (p_acb->p_cback)
1761         {
1762             bta_hl_build_delete_mdl_cfm(&evt_data,
1763                                         p_acb->app_handle,
1764                                         p_mcb->mcl_handle,
1765                                         p_delete_cfm->mdl_id,
1766                                         status);
1767 
1768             p_acb->p_cback(BTA_HL_DELETE_MDL_CFM_EVT,(tBTA_HL *) &evt_data );
1769         }
1770         else
1771         {
1772             APPL_TRACE_ERROR("bta_hl_mca_delete_mdl_cfm Null Callback");
1773 
1774         }
1775     }
1776 }
1777 
1778 /*******************************************************************************
1779 **
1780 ** Function         bta_hl_mca_delete_mdl_ind
1781 **
1782 ** Description      Process the DELETE MDL indication event
1783 **
1784 ** Returns          void
1785 **
1786 *******************************************************************************/
bta_hl_mca_delete_mdl_ind(tBTA_HL_CB * p_cb,tBTA_HL_DATA * p_data)1787 static void bta_hl_mca_delete_mdl_ind(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
1788 {
1789     tBTA_HL         evt_data;
1790     UINT8           app_idx, mcl_idx, mdl_idx;
1791     tMCA_EVT_HDR    *p_delete_ind = &p_data->mca_evt.mca_data.delete_ind;
1792     tBTA_HL_MCL_CB  *p_mcb;
1793     tBTA_HL_MDL_CB  *p_dcb;
1794     BOOLEAN         send_ind_evt = FALSE;
1795     tBTA_HL_APP_CB  *p_acb;
1796     UNUSED(p_cb);
1797 
1798     if (bta_hl_find_mcl_idx_using_handle(p_data->mca_evt.mcl_handle, &app_idx, &mcl_idx) )
1799     {
1800         p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
1801 
1802         if (bta_hl_find_mdl_idx(app_idx, mcl_idx, p_delete_ind->mdl_id, &mdl_idx ))
1803         {
1804             p_dcb = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
1805             p_dcb->dch_oper = BTA_HL_DCH_OP_REMOTE_DELETE;
1806         }
1807         if (bta_hl_delete_mdl_cfg(app_idx,
1808                                   p_mcb->bd_addr ,
1809                                   p_delete_ind->mdl_id))
1810         {
1811             send_ind_evt = TRUE;
1812         }
1813     }
1814 
1815 #if BTA_HL_DEBUG == TRUE
1816     if (!send_ind_evt)
1817     {
1818         APPL_TRACE_DEBUG("bta_hl_mca_delete_mdl_ind is_send_ind_evt =%d", send_ind_evt);
1819     }
1820 #endif
1821 
1822     if (send_ind_evt)
1823     {
1824         p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
1825         if (p_acb->p_cback)
1826         {
1827             evt_data.delete_mdl_ind.mcl_handle = p_mcb->mcl_handle;
1828             evt_data.delete_mdl_ind.app_handle = p_acb->app_handle;
1829             evt_data.delete_mdl_ind.mdl_id = p_delete_ind->mdl_id;
1830             p_acb->p_cback(BTA_HL_DELETE_MDL_IND_EVT,(tBTA_HL *) &evt_data );
1831         }
1832         else
1833         {
1834             APPL_TRACE_ERROR("bta_hl_mca_delete_mdl_ind Null Callback");
1835         }
1836     }
1837 }
1838 
1839 
1840 
1841 /*******************************************************************************
1842 **
1843 ** Function         bta_hl_api_dch_abort
1844 **
1845 ** Description      Process the API DCH abort request
1846 **
1847 ** Returns          void
1848 **
1849 *******************************************************************************/
bta_hl_api_dch_abort(tBTA_HL_CB * p_cb,tBTA_HL_DATA * p_data)1850 static void bta_hl_api_dch_abort(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
1851 {
1852     tBTA_HL_STATUS  status = BTA_HL_STATUS_OK;
1853     UINT8           app_idx, mcl_idx, mdl_idx;
1854     tBTA_HL_APP_CB  *p_acb;
1855     tBTA_HL_MCL_CB  *p_mcb;
1856     tBTA_HL_MDL_CB  *p_dcb;
1857     tBTA_HL         evt_data;
1858     UNUSED(p_cb);
1859 
1860     if (bta_hl_find_mcl_idx_using_handle(p_data->api_dch_abort.mcl_handle, &app_idx, &mcl_idx ))
1861     {
1862 
1863         if (!bta_hl_find_dch_setup_mdl_idx(app_idx, mcl_idx, &mdl_idx ))
1864         {
1865             status = BTA_HL_STATUS_NO_MDL_ID_FOUND;
1866         }
1867         else
1868         {
1869             p_dcb = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
1870             if (p_dcb->abort_oper)
1871             {
1872                 /* abort already in progress*/
1873                 status = BTA_HL_STATUS_FAIL;
1874             }
1875             else
1876             {
1877                 p_dcb->abort_oper = BTA_HL_ABORT_LOCAL_MASK;
1878             }
1879         }
1880     }
1881     else
1882     {
1883         status = BTA_HL_STATUS_INVALID_MCL_HANDLE;
1884     }
1885 
1886 
1887 #if BTA_HL_DEBUG == TRUE
1888     if (status != BTA_HL_STATUS_OK)
1889     {
1890         APPL_TRACE_DEBUG("bta_hl_api_dch_abort status=%s", bta_hl_status_code(status));
1891     }
1892 #endif
1893     switch (status)
1894     {
1895         case BTA_HL_STATUS_OK:
1896 
1897             bta_hl_dch_sm_execute(app_idx, mcl_idx, mdl_idx, BTA_HL_DCH_ABORT_EVT, p_data);
1898             break;
1899         case BTA_HL_STATUS_NO_MDL_ID_FOUND:
1900         case BTA_HL_STATUS_FAIL:
1901 
1902             p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
1903             if (p_acb->p_cback)
1904             {
1905                 p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
1906                 bta_hl_build_abort_cfm(&evt_data,
1907 
1908 
1909 
1910                                        p_acb->app_handle,
1911                                        p_mcb->mcl_handle,
1912                                        BTA_HL_STATUS_FAIL);
1913                 p_acb->p_cback(BTA_HL_DCH_ABORT_CFM_EVT,(tBTA_HL *) &evt_data);
1914             }
1915             else
1916             {
1917                 APPL_TRACE_ERROR("bta_hl_api_dch_abort Null Callback");
1918             }
1919             break;
1920         default:
1921             APPL_TRACE_ERROR("Status code=%d", status);
1922             break;
1923     }
1924 }
1925 
1926 /*******************************************************************************
1927 **
1928 ** Function         bta_hl_hdl_event
1929 **
1930 ** Description      HL main event handling function.
1931 **
1932 ** Returns          void
1933 **
1934 *******************************************************************************/
bta_hl_hdl_event(BT_HDR * p_msg)1935 BOOLEAN bta_hl_hdl_event(BT_HDR *p_msg)
1936 {
1937     UINT8 app_idx, mcl_idx, mdl_idx;
1938     BOOLEAN success = TRUE;
1939 
1940 #if BTA_HL_DEBUG == TRUE
1941     APPL_TRACE_DEBUG("BTA HL Event Handler: Event [%s]",
1942                       bta_hl_evt_code(p_msg->event));
1943 #endif
1944 
1945     switch (p_msg->event)
1946     {
1947         case BTA_HL_API_ENABLE_EVT:
1948             bta_hl_api_enable(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
1949             break;
1950         case BTA_HL_API_DISABLE_EVT:
1951             bta_hl_api_disable(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
1952             break;
1953         case BTA_HL_API_UPDATE_EVT:
1954             bta_hl_api_update(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
1955             break;
1956         case BTA_HL_API_REGISTER_EVT:
1957             bta_hl_api_register(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
1958             break;
1959         case BTA_HL_API_DEREGISTER_EVT:
1960             bta_hl_api_deregister(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
1961             break;
1962         case BTA_HL_API_CCH_OPEN_EVT:
1963             bta_hl_api_cch_open(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
1964             break;
1965         case BTA_HL_API_CCH_CLOSE_EVT:
1966             bta_hl_api_cch_close(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
1967             break;
1968         case BTA_HL_API_DCH_OPEN_EVT:
1969             bta_hl_api_dch_open(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
1970             break;
1971         case BTA_HL_API_DCH_CLOSE_EVT:
1972             bta_hl_api_dch_close(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
1973             break;
1974         case BTA_HL_API_DELETE_MDL_EVT:
1975             bta_hl_api_delete_mdl(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
1976             break;
1977         case BTA_HL_API_DCH_RECONNECT_EVT:
1978             bta_hl_api_dch_reconnect(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
1979             break;
1980 
1981         case BTA_HL_API_DCH_ECHO_TEST_EVT:
1982             bta_hl_api_dch_echo_test(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
1983             break;
1984 
1985         case BTA_HL_API_SDP_QUERY_EVT:
1986             bta_hl_api_sdp_query(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
1987             break;
1988 
1989         case BTA_HL_MCA_DELETE_CFM_EVT:
1990             bta_hl_mca_delete_mdl_cfm(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
1991             break;
1992 
1993         case BTA_HL_MCA_DELETE_IND_EVT:
1994             bta_hl_mca_delete_mdl_ind(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
1995             break;
1996 
1997         case BTA_HL_SDP_QUERY_OK_EVT:
1998         case BTA_HL_SDP_QUERY_FAIL_EVT:
1999             bta_hl_sdp_query_results(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
2000             break;
2001         case BTA_HL_API_DCH_ABORT_EVT:
2002             bta_hl_api_dch_abort(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
2003             break;
2004 
2005 
2006         default:
2007             if (p_msg->event < BTA_HL_DCH_EVT_MIN)
2008             {
2009                 if (bta_hl_find_cch_cb_indexes((tBTA_HL_DATA *) p_msg, &app_idx, &mcl_idx))
2010                 {
2011                     bta_hl_cch_sm_execute( app_idx,
2012                                            mcl_idx,
2013                                            p_msg->event, (tBTA_HL_DATA *) p_msg);
2014                 }
2015                 else
2016                 {
2017 #if BTA_HL_DEBUG == TRUE
2018                     APPL_TRACE_ERROR("unable to find control block indexes for CCH: [event=%s]",
2019                                       bta_hl_evt_code(p_msg->event));
2020 #else
2021                     APPL_TRACE_ERROR("unable to find control block indexes for CCH: [event=%d]", p_msg->event);
2022 #endif
2023                     success = FALSE;
2024                 }
2025             }
2026             else
2027             {
2028                 if (bta_hl_find_dch_cb_indexes((tBTA_HL_DATA *) p_msg, &app_idx, &mcl_idx, &mdl_idx))
2029                 {
2030                     bta_hl_dch_sm_execute( app_idx,
2031                                            mcl_idx,
2032                                            mdl_idx,
2033                                            p_msg->event, (tBTA_HL_DATA *) p_msg);
2034                 }
2035                 else
2036                 {
2037 
2038 #if BTA_HL_DEBUG == TRUE
2039                     APPL_TRACE_ERROR("unable to find control block indexes for DCH : [event=%s]",
2040                                       bta_hl_evt_code(p_msg->event));
2041 #else
2042                     APPL_TRACE_ERROR("unable to find control block indexes for DCH: [event=%d]", p_msg->event);
2043 #endif
2044                     success = FALSE;
2045                 }
2046             }
2047 
2048             break;
2049     }
2050 
2051     return(success);
2052 }
2053 
2054 
2055 /*****************************************************************************
2056 **  Debug Functions
2057 *****************************************************************************/
2058 #if (BTA_HL_DEBUG == TRUE) && (BT_USE_TRACES == TRUE)
2059 
2060 /*******************************************************************************
2061 **
2062 ** Function         bta_hl_cch_state_code
2063 **
2064 ** Description      Map CCH state code to the corresponding state string
2065 **
2066 ** Returns          string pointer for the associated state name
2067 **
2068 *******************************************************************************/
bta_hl_cch_state_code(tBTA_HL_CCH_STATE state_code)2069 static char *bta_hl_cch_state_code(tBTA_HL_CCH_STATE state_code)
2070 {
2071     switch (state_code)
2072     {
2073         case BTA_HL_CCH_IDLE_ST:
2074             return "BTA_HL_CCH_IDLE_ST";
2075         case BTA_HL_CCH_OPENING_ST:
2076             return "BTA_HL_CCH_OPENING_ST";
2077         case BTA_HL_CCH_OPEN_ST:
2078             return "BTA_HL_CCH_OPEN_ST";
2079         case BTA_HL_CCH_CLOSING_ST:
2080             return "BTA_HL_CCH_CLOSING_ST";
2081         default:
2082             return "Unknown CCH state code";
2083     }
2084 }
2085 
2086 /*******************************************************************************
2087 **
2088 ** Function         bta_hl_dch_state_code
2089 **
2090 ** Description      Map DCH state code to the corresponding state string
2091 **
2092 ** Returns          string pointer for the associated state name
2093 **
2094 *******************************************************************************/
bta_hl_dch_state_code(tBTA_HL_DCH_STATE state_code)2095 static char *bta_hl_dch_state_code(tBTA_HL_DCH_STATE state_code)
2096 {
2097     switch (state_code)
2098     {
2099         case BTA_HL_DCH_IDLE_ST:
2100             return "BTA_HL_DCH_IDLE_ST";
2101         case BTA_HL_DCH_OPENING_ST:
2102             return "BTA_HL_DCH_OPENING_ST";
2103         case BTA_HL_DCH_OPEN_ST:
2104             return "BTA_HL_DCH_OPEN_ST";
2105         case BTA_HL_DCH_CLOSING_ST:
2106             return "BTA_HL_DCH_CLOSING_ST";
2107         default:
2108             return "Unknown DCH state code";
2109     }
2110 }
2111 #endif  /* Debug Functions */
2112 #endif /* HL_INCLUDED */
2113