blob: f9bffe66ba635b3596e16b1f230d63f87ba9c660 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07003 * Copyright 2003-2012 Broadcom Corporation
The Android Open Source Project5738f832012-12-12 16:00:35 -08004 *
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 is the main implementation file for the BTA device manager.
22 *
23 ******************************************************************************/
24
25#include "bta_api.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080026#include "bta_dm_int.h"
Myles Watsoncd1fd072016-11-09 13:17:43 -080027#include "bta_sys.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080028
29/*****************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -080030 * Constants and types
31 ****************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080032
Myles Watsoncd1fd072016-11-09 13:17:43 -080033tBTA_DM_CB bta_dm_cb;
The Android Open Source Project5738f832012-12-12 16:00:35 -080034tBTA_DM_SEARCH_CB bta_dm_search_cb;
Myles Watsoncd1fd072016-11-09 13:17:43 -080035tBTA_DM_DI_CB bta_dm_di_cb;
The Android Open Source Project5738f832012-12-12 16:00:35 -080036
Myles Watsoncd1fd072016-11-09 13:17:43 -080037#define BTA_DM_NUM_ACTIONS (BTA_DM_MAX_EVT & 0x00ff)
The Android Open Source Project5738f832012-12-12 16:00:35 -080038
39/* type for action functions */
Myles Watsoncd1fd072016-11-09 13:17:43 -080040typedef void (*tBTA_DM_ACTION)(tBTA_DM_MSG* p_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -080041
The Android Open Source Project5738f832012-12-12 16:00:35 -080042/* state machine action enumeration list */
Myles Watsoncd1fd072016-11-09 13:17:43 -080043enum {
44 BTA_DM_API_SEARCH, /* 0 bta_dm_search_start */
45 BTA_DM_API_SEARCH_CANCEL, /* 1 bta_dm_search_cancel */
46 BTA_DM_API_DISCOVER, /* 2 bta_dm_discover */
47 BTA_DM_INQUIRY_CMPL, /* 3 bta_dm_inq_cmpl */
48 BTA_DM_REMT_NAME, /* 4 bta_dm_rmt_name */
49 BTA_DM_SDP_RESULT, /* 5 bta_dm_sdp_result */
50 BTA_DM_SEARCH_CMPL, /* 6 bta_dm_search_cmpl*/
51 BTA_DM_FREE_SDP_DB, /* 7 bta_dm_free_sdp_db */
52 BTA_DM_DISC_RESULT, /* 8 bta_dm_disc_result */
53 BTA_DM_SEARCH_RESULT, /* 9 bta_dm_search_result */
54 BTA_DM_QUEUE_SEARCH, /* 10 bta_dm_queue_search */
55 BTA_DM_QUEUE_DISC, /* 11 bta_dm_queue_disc */
56 BTA_DM_SEARCH_CLEAR_QUEUE, /* 12 bta_dm_search_clear_queue */
57 BTA_DM_SEARCH_CANCEL_CMPL, /* 13 bta_dm_search_cancel_cmpl */
58 BTA_DM_SEARCH_CANCEL_NOTIFY, /* 14 bta_dm_search_cancel_notify */
59 BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL, /* 15 bta_dm_search_cancel_transac_cmpl */
60 BTA_DM_DISC_RMT_NAME, /* 16 bta_dm_disc_rmt_name */
61 BTA_DM_API_DI_DISCOVER, /* 17 bta_dm_di_disc */
Myles Watson90088882016-11-15 16:33:22 -080062 BTA_DM_CLOSE_GATT_CONN, /* 18 bta_dm_close_gatt_conn */
63 BTA_DM_SEARCH_NUM_ACTIONS /* 19 */
The Android Open Source Project5738f832012-12-12 16:00:35 -080064};
65
The Android Open Source Project5738f832012-12-12 16:00:35 -080066/* action function list */
Myles Watsoncd1fd072016-11-09 13:17:43 -080067const tBTA_DM_ACTION bta_dm_search_action[] = {
The Android Open Source Project5738f832012-12-12 16:00:35 -080068
Myles Watsoncd1fd072016-11-09 13:17:43 -080069 bta_dm_search_start, /* 0 BTA_DM_API_SEARCH */
70 bta_dm_search_cancel, /* 1 BTA_DM_API_SEARCH_CANCEL */
71 bta_dm_discover, /* 2 BTA_DM_API_DISCOVER */
72 bta_dm_inq_cmpl, /* 3 BTA_DM_INQUIRY_CMPL */
73 bta_dm_rmt_name, /* 4 BTA_DM_REMT_NAME */
74 bta_dm_sdp_result, /* 5 BTA_DM_SDP_RESULT */
75 bta_dm_search_cmpl, /* 6 BTA_DM_SEARCH_CMPL */
76 bta_dm_free_sdp_db, /* 7 BTA_DM_FREE_SDP_DB */
77 bta_dm_disc_result, /* 8 BTA_DM_DISC_RESULT */
78 bta_dm_search_result, /* 9 BTA_DM_SEARCH_RESULT */
79 bta_dm_queue_search, /* 10 BTA_DM_QUEUE_SEARCH */
80 bta_dm_queue_disc, /* 11 BTA_DM_QUEUE_DISC */
81 bta_dm_search_clear_queue, /* 12 BTA_DM_SEARCH_CLEAR_QUEUE */
82 bta_dm_search_cancel_cmpl, /* 13 BTA_DM_SEARCH_CANCEL_CMPL */
83 bta_dm_search_cancel_notify, /* 14 BTA_DM_SEARCH_CANCEL_NOTIFY */
84 bta_dm_search_cancel_transac_cmpl, /* 15 BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL
85 */
86 bta_dm_disc_rmt_name, /* 16 BTA_DM_DISC_RMT_NAME */
Myles Watson84baa7f2016-11-14 12:05:37 -080087 bta_dm_di_disc, /* 17 BTA_DM_API_DI_DISCOVER */
Myles Watson90088882016-11-15 16:33:22 -080088 bta_dm_close_gatt_conn};
The Android Open Source Project5738f832012-12-12 16:00:35 -080089
Myles Watsoncd1fd072016-11-09 13:17:43 -080090#define BTA_DM_SEARCH_IGNORE BTA_DM_SEARCH_NUM_ACTIONS
The Android Open Source Project5738f832012-12-12 16:00:35 -080091/* state table information */
Myles Watsoncd1fd072016-11-09 13:17:43 -080092#define BTA_DM_SEARCH_ACTIONS 2 /* number of actions */
93#define BTA_DM_SEARCH_NEXT_STATE 2 /* position of next state */
94#define BTA_DM_SEARCH_NUM_COLS 3 /* number of columns in state tables */
The Android Open Source Project5738f832012-12-12 16:00:35 -080095
96/* state table for listen state */
Myles Watsoncd1fd072016-11-09 13:17:43 -080097const uint8_t bta_dm_search_idle_st_table[][BTA_DM_SEARCH_NUM_COLS] = {
The Android Open Source Project5738f832012-12-12 16:00:35 -080098
Myles Watsoncd1fd072016-11-09 13:17:43 -080099 /* Event Action 1
100 Action 2 Next State */
101 /* API_SEARCH */ {BTA_DM_API_SEARCH, BTA_DM_SEARCH_IGNORE,
102 BTA_DM_SEARCH_ACTIVE},
103 /* API_SEARCH_CANCEL */ {BTA_DM_SEARCH_CANCEL_NOTIFY, BTA_DM_SEARCH_IGNORE,
104 BTA_DM_SEARCH_IDLE},
105 /* API_SEARCH_DISC */ {BTA_DM_API_DISCOVER, BTA_DM_SEARCH_IGNORE,
106 BTA_DM_DISCOVER_ACTIVE},
107 /* INQUIRY_CMPL */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE,
108 BTA_DM_SEARCH_IDLE},
109 /* REMT_NAME_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE,
110 BTA_DM_SEARCH_IDLE},
111 /* SDP_RESULT_EVT */ {BTA_DM_FREE_SDP_DB, BTA_DM_SEARCH_IGNORE,
112 BTA_DM_SEARCH_IDLE},
113 /* SEARCH_CMPL_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE,
114 BTA_DM_SEARCH_IDLE},
115 /* DISCV_RES_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE,
116 BTA_DM_SEARCH_IDLE},
117 /* API_DI_DISCOVER_EVT */ {BTA_DM_API_DI_DISCOVER, BTA_DM_SEARCH_IGNORE,
Myles Watson84baa7f2016-11-14 12:05:37 -0800118 BTA_DM_SEARCH_ACTIVE},
119 /* DISC_CLOSE_TOUT_EVT */
Myles Watson90088882016-11-15 16:33:22 -0800120 {BTA_DM_CLOSE_GATT_CONN, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}};
Myles Watsoncd1fd072016-11-09 13:17:43 -0800121const uint8_t bta_dm_search_search_active_st_table[][BTA_DM_SEARCH_NUM_COLS] = {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800122
Myles Watsoncd1fd072016-11-09 13:17:43 -0800123 /* Event Action 1
124 Action 2 Next State */
125 /* API_SEARCH */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE,
126 BTA_DM_SEARCH_ACTIVE},
127 /* API_SEARCH_CANCEL */ {BTA_DM_API_SEARCH_CANCEL, BTA_DM_SEARCH_IGNORE,
128 BTA_DM_SEARCH_CANCELLING},
129 /* API_SEARCH_DISC */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE,
130 BTA_DM_SEARCH_ACTIVE},
131 /* INQUIRY_CMPL */ {BTA_DM_INQUIRY_CMPL, BTA_DM_SEARCH_IGNORE,
132 BTA_DM_SEARCH_ACTIVE},
133 /* REMT_NAME_EVT */ {BTA_DM_REMT_NAME, BTA_DM_SEARCH_IGNORE,
134 BTA_DM_SEARCH_ACTIVE},
135 /* SDP_RESULT_EVT */ {BTA_DM_SDP_RESULT, BTA_DM_SEARCH_IGNORE,
136 BTA_DM_SEARCH_ACTIVE},
137 /* SEARCH_CMPL_EVT */ {BTA_DM_SEARCH_CMPL, BTA_DM_SEARCH_IGNORE,
138 BTA_DM_SEARCH_IDLE},
139 /* DISCV_RES_EVT */ {BTA_DM_SEARCH_RESULT, BTA_DM_SEARCH_IGNORE,
140 BTA_DM_SEARCH_ACTIVE},
141 /* API_DI_DISCOVER_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE,
Myles Watson84baa7f2016-11-14 12:05:37 -0800142 BTA_DM_SEARCH_ACTIVE},
143 /* DISC_CLOSE_TOUT_EVT */
Myles Watsoncd1fd072016-11-09 13:17:43 -0800144 {BTA_DM_CLOSE_GATT_CONN, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800145
146};
147
Myles Watsoncd1fd072016-11-09 13:17:43 -0800148const uint8_t
149 bta_dm_search_search_cancelling_st_table[][BTA_DM_SEARCH_NUM_COLS] = {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800150
Myles Watsoncd1fd072016-11-09 13:17:43 -0800151 /* Event Action 1
152 Action 2 Next State */
153 /* API_SEARCH */ {BTA_DM_QUEUE_SEARCH, BTA_DM_SEARCH_IGNORE,
154 BTA_DM_SEARCH_CANCELLING},
155 /* API_SEARCH_CANCEL */ {BTA_DM_SEARCH_CLEAR_QUEUE,
156 BTA_DM_SEARCH_CANCEL_NOTIFY,
157 BTA_DM_SEARCH_CANCELLING},
158 /* API_SEARCH_DISC */ {BTA_DM_QUEUE_DISC, BTA_DM_SEARCH_IGNORE,
159 BTA_DM_SEARCH_CANCELLING},
160 /* INQUIRY_CMPL */ {BTA_DM_SEARCH_CANCEL_CMPL, BTA_DM_SEARCH_IGNORE,
161 BTA_DM_SEARCH_IDLE},
162 /* REMT_NAME_EVT */ {BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL,
163 BTA_DM_SEARCH_CANCEL_CMPL, BTA_DM_SEARCH_IDLE},
164 /* SDP_RESULT_EVT */ {BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL,
165 BTA_DM_SEARCH_CANCEL_CMPL, BTA_DM_SEARCH_IDLE},
166 /* SEARCH_CMPL_EVT */ {BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL,
167 BTA_DM_SEARCH_CANCEL_CMPL, BTA_DM_SEARCH_IDLE},
168 /* DISCV_RES_EVT */ {BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL,
169 BTA_DM_SEARCH_CANCEL_CMPL, BTA_DM_SEARCH_IDLE},
170 /* API_DI_DISCOVER_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE,
Myles Watson84baa7f2016-11-14 12:05:37 -0800171 BTA_DM_SEARCH_CANCELLING},
172 /* DISC_CLOSE_TOUT_EVT */
Myles Watson90088882016-11-15 16:33:22 -0800173 {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_CANCELLING}};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800174
Myles Watsoncd1fd072016-11-09 13:17:43 -0800175const uint8_t bta_dm_search_disc_active_st_table[][BTA_DM_SEARCH_NUM_COLS] = {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800176
Myles Watsoncd1fd072016-11-09 13:17:43 -0800177 /* Event Action 1
178 Action 2 Next State */
179 /* API_SEARCH */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE,
180 BTA_DM_DISCOVER_ACTIVE},
181 /* API_SEARCH_CANCEL */ {BTA_DM_SEARCH_CANCEL_NOTIFY, BTA_DM_SEARCH_IGNORE,
182 BTA_DM_SEARCH_CANCELLING},
183 /* API_SEARCH_DISC */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE,
184 BTA_DM_DISCOVER_ACTIVE},
185 /* INQUIRY_CMPL */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE,
186 BTA_DM_DISCOVER_ACTIVE},
187 /* REMT_NAME_EVT */ {BTA_DM_DISC_RMT_NAME, BTA_DM_SEARCH_IGNORE,
188 BTA_DM_DISCOVER_ACTIVE},
189 /* SDP_RESULT_EVT */ {BTA_DM_SDP_RESULT, BTA_DM_SEARCH_IGNORE,
190 BTA_DM_DISCOVER_ACTIVE},
191 /* SEARCH_CMPL_EVT */ {BTA_DM_SEARCH_CMPL, BTA_DM_SEARCH_IGNORE,
192 BTA_DM_SEARCH_IDLE},
193 /* DISCV_RES_EVT */ {BTA_DM_DISC_RESULT, BTA_DM_SEARCH_IGNORE,
194 BTA_DM_DISCOVER_ACTIVE},
195 /* API_DI_DISCOVER_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE,
Myles Watson84baa7f2016-11-14 12:05:37 -0800196 BTA_DM_DISCOVER_ACTIVE},
197 /* DISC_CLOSE_TOUT_EVT */
Myles Watson90088882016-11-15 16:33:22 -0800198 {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE}};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800199
Marie Janssene9e58ce2016-06-17 14:12:17 -0700200typedef const uint8_t (*tBTA_DM_ST_TBL)[BTA_DM_SEARCH_NUM_COLS];
The Android Open Source Project5738f832012-12-12 16:00:35 -0800201
202/* state table */
203const tBTA_DM_ST_TBL bta_dm_search_st_tbl[] = {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800204 bta_dm_search_idle_st_table, bta_dm_search_search_active_st_table,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800205 bta_dm_search_search_cancelling_st_table,
Myles Watsoncd1fd072016-11-09 13:17:43 -0800206 bta_dm_search_disc_active_st_table};
The Android Open Source Project5738f832012-12-12 16:00:35 -0800207
208/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800209 *
Myles Watson8af480e2016-11-09 10:40:23 -0800210 * Function bta_dm_sm_search_disable
211 *
212 * Description unregister BTA SEARCH DM
213 *
214 *
215 * Returns void
216 *
217 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800218void bta_dm_search_sm_disable() { bta_sys_deregister(BTA_ID_DM_SEARCH); }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800219
220/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800221 *
222 * Function bta_dm_search_sm_execute
223 *
224 * Description State machine event handling function for DM
225 *
226 *
227 * Returns void
228 *
229 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800230bool bta_dm_search_sm_execute(BT_HDR* p_msg) {
231 tBTA_DM_ST_TBL state_table;
232 uint8_t action;
233 int i;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800234
Myles Watsoncd1fd072016-11-09 13:17:43 -0800235 APPL_TRACE_EVENT("bta_dm_search_sm_execute state:%d, event:0x%x",
236 bta_dm_search_cb.state, p_msg->event);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800237
Myles Watsoncd1fd072016-11-09 13:17:43 -0800238 /* look up the state table for the current state */
239 state_table = bta_dm_search_st_tbl[bta_dm_search_cb.state];
The Android Open Source Project5738f832012-12-12 16:00:35 -0800240
Myles Watsoncd1fd072016-11-09 13:17:43 -0800241 bta_dm_search_cb.state =
242 state_table[p_msg->event & 0x00ff][BTA_DM_SEARCH_NEXT_STATE];
The Android Open Source Project5738f832012-12-12 16:00:35 -0800243
Myles Watsoncd1fd072016-11-09 13:17:43 -0800244 /* execute action functions */
245 for (i = 0; i < BTA_DM_SEARCH_ACTIONS; i++) {
Marie Janssenf33b6f42016-11-22 15:01:42 -0800246 action = state_table[p_msg->event & 0x00ff][i];
247 if (action != BTA_DM_SEARCH_IGNORE) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800248 (*bta_dm_search_action[action])((tBTA_DM_MSG*)p_msg);
249 } else {
250 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800251 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800252 }
253 return true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800254}