1 /******************************************************************************
2  *
3  *  Copyright 2003-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 is the main implementation file for the BTA device manager.
22  *
23  ******************************************************************************/
24 
25 #include "bta_api.h"
26 #include "bta_dm_int.h"
27 #include "bta_sys.h"
28 
29 /*****************************************************************************
30  * Constants and types
31  ****************************************************************************/
32 
33 tBTA_DM_CB bta_dm_cb;
34 tBTA_DM_SEARCH_CB bta_dm_search_cb;
35 tBTA_DM_DI_CB bta_dm_di_cb;
36 
37 #define BTA_DM_NUM_ACTIONS (BTA_DM_MAX_EVT & 0x00ff)
38 
39 /* type for action functions */
40 typedef void (*tBTA_DM_ACTION)(tBTA_DM_MSG* p_data);
41 
42 /* state machine action enumeration list */
43 enum {
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 */
62   BTA_DM_CLOSE_GATT_CONN,            /* 18 bta_dm_close_gatt_conn */
63   BTA_DM_SEARCH_NUM_ACTIONS          /* 19 */
64 };
65 
66 /* action function list */
67 const tBTA_DM_ACTION bta_dm_search_action[] = {
68 
69     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 */
87     bta_dm_di_disc,                    /* 17 BTA_DM_API_DI_DISCOVER */
88     bta_dm_close_gatt_conn};
89 
90 #define BTA_DM_SEARCH_IGNORE BTA_DM_SEARCH_NUM_ACTIONS
91 /* state table information */
92 #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 */
95 
96 /* state table for listen state */
97 const uint8_t bta_dm_search_idle_st_table[][BTA_DM_SEARCH_NUM_COLS] = {
98 
99     /* 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,
118                                BTA_DM_SEARCH_ACTIVE},
119     /* DISC_CLOSE_TOUT_EVT */
120     {BTA_DM_CLOSE_GATT_CONN, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}};
121 const uint8_t bta_dm_search_search_active_st_table[][BTA_DM_SEARCH_NUM_COLS] = {
122 
123     /* 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,
142                                BTA_DM_SEARCH_ACTIVE},
143     /* DISC_CLOSE_TOUT_EVT */
144     {BTA_DM_CLOSE_GATT_CONN, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE}
145 
146 };
147 
148 const uint8_t
149     bta_dm_search_search_cancelling_st_table[][BTA_DM_SEARCH_NUM_COLS] = {
150 
151         /* 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,
171                                    BTA_DM_SEARCH_CANCELLING},
172         /* DISC_CLOSE_TOUT_EVT */
173         {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_CANCELLING}};
174 
175 const uint8_t bta_dm_search_disc_active_st_table[][BTA_DM_SEARCH_NUM_COLS] = {
176 
177     /* 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,
196                                BTA_DM_DISCOVER_ACTIVE},
197     /* DISC_CLOSE_TOUT_EVT */
198     {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE}};
199 
200 typedef const uint8_t (*tBTA_DM_ST_TBL)[BTA_DM_SEARCH_NUM_COLS];
201 
202 /* state table */
203 const tBTA_DM_ST_TBL bta_dm_search_st_tbl[] = {
204     bta_dm_search_idle_st_table, bta_dm_search_search_active_st_table,
205     bta_dm_search_search_cancelling_st_table,
206     bta_dm_search_disc_active_st_table};
207 
208 /*******************************************************************************
209  *
210  * Function         bta_dm_sm_search_disable
211  *
212  * Description     unregister BTA SEARCH DM
213  *
214  *
215  * Returns          void
216  *
217  ******************************************************************************/
bta_dm_search_sm_disable()218 void bta_dm_search_sm_disable() { bta_sys_deregister(BTA_ID_DM_SEARCH); }
219 
220 /*******************************************************************************
221  *
222  * Function         bta_dm_search_sm_execute
223  *
224  * Description      State machine event handling function for DM
225  *
226  *
227  * Returns          void
228  *
229  ******************************************************************************/
bta_dm_search_sm_execute(BT_HDR * p_msg)230 bool bta_dm_search_sm_execute(BT_HDR* p_msg) {
231   tBTA_DM_ST_TBL state_table;
232   uint8_t action;
233   int i;
234 
235   APPL_TRACE_EVENT("bta_dm_search_sm_execute state:%d, event:0x%x",
236                    bta_dm_search_cb.state, p_msg->event);
237 
238   /* look up the state table for the current state */
239   state_table = bta_dm_search_st_tbl[bta_dm_search_cb.state];
240 
241   bta_dm_search_cb.state =
242       state_table[p_msg->event & 0x00ff][BTA_DM_SEARCH_NEXT_STATE];
243 
244   /* execute action functions */
245   for (i = 0; i < BTA_DM_SEARCH_ACTIONS; i++) {
246     action = state_table[p_msg->event & 0x00ff][i];
247     if (action != BTA_DM_SEARCH_IGNORE) {
248       (*bta_dm_search_action[action])((tBTA_DM_MSG*)p_msg);
249     } else {
250       break;
251     }
252   }
253   return true;
254 }
255