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 "bt_trace.h"
26 #include "bta/dm/bta_dm_int.h"
27 #include "stack/include/bt_types.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 /*******************************************************************************
38  *
39  * Function         bta_dm_sm_search_disable
40  *
41  * Description     unregister BTA SEARCH DM
42  *
43  *
44  * Returns          void
45  *
46  ******************************************************************************/
bta_dm_search_sm_disable()47 void bta_dm_search_sm_disable() { bta_sys_deregister(BTA_ID_DM_SEARCH); }
48 
bta_dm_search_set_state(uint8_t state)49 void bta_dm_search_set_state(uint8_t state) { bta_dm_search_cb.state = state; }
bta_dm_search_get_state()50 uint8_t bta_dm_search_get_state() { return bta_dm_search_cb.state; }
51 
52 /*******************************************************************************
53  *
54  * Function         bta_dm_search_sm_execute
55  *
56  * Description      State machine event handling function for DM
57  *
58  *
59  * Returns          void
60  *
61  ******************************************************************************/
bta_dm_search_sm_execute(BT_HDR_RIGID * p_msg)62 bool bta_dm_search_sm_execute(BT_HDR_RIGID* p_msg) {
63   APPL_TRACE_EVENT("bta_dm_search_sm_execute state:%d, event:0x%x",
64                    bta_dm_search_cb.state, p_msg->event);
65 
66   tBTA_DM_MSG* message = (tBTA_DM_MSG*)p_msg;
67   switch (bta_dm_search_cb.state) {
68     case BTA_DM_SEARCH_IDLE:
69       switch (p_msg->event) {
70         case BTA_DM_API_SEARCH_EVT:
71           bta_dm_search_set_state(BTA_DM_SEARCH_ACTIVE);
72           bta_dm_search_start(message);
73           break;
74         case BTA_DM_API_DISCOVER_EVT:
75           bta_dm_search_set_state(BTA_DM_DISCOVER_ACTIVE);
76           bta_dm_discover(message);
77           break;
78         case BTA_DM_SDP_RESULT_EVT:
79           bta_dm_free_sdp_db();
80           break;
81         case BTA_DM_DISC_CLOSE_TOUT_EVT:
82           bta_dm_close_gatt_conn(message);
83           break;
84         case BTA_DM_API_QUEUE_SEARCH_EVT:
85           bta_dm_queue_search(message);
86           break;
87         case BTA_DM_API_QUEUE_DISCOVER_EVT:
88           bta_dm_queue_disc(message);
89           break;
90       }
91       break;
92     case BTA_DM_SEARCH_ACTIVE:
93       switch (p_msg->event) {
94         case BTA_DM_REMT_NAME_EVT:
95           bta_dm_rmt_name(message);
96           break;
97         case BTA_DM_SDP_RESULT_EVT:
98           bta_dm_sdp_result(message);
99           break;
100         case BTA_DM_SEARCH_CMPL_EVT:
101           bta_dm_search_cmpl();
102           break;
103         case BTA_DM_DISCOVERY_RESULT_EVT:
104           bta_dm_search_result(message);
105           break;
106         case BTA_DM_DISC_CLOSE_TOUT_EVT:
107           bta_dm_close_gatt_conn(message);
108           break;
109         case BTA_DM_API_DISCOVER_EVT:
110         case BTA_DM_API_QUEUE_DISCOVER_EVT:
111           bta_dm_queue_disc(message);
112           break;
113       }
114       break;
115     case BTA_DM_SEARCH_CANCELLING:
116       switch (p_msg->event) {
117         case BTA_DM_API_SEARCH_EVT:
118         case BTA_DM_API_QUEUE_SEARCH_EVT:
119           bta_dm_queue_search(message);
120           break;
121         case BTA_DM_API_DISCOVER_EVT:
122         case BTA_DM_API_QUEUE_DISCOVER_EVT:
123           bta_dm_queue_disc(message);
124           break;
125         case BTA_DM_SDP_RESULT_EVT:
126         case BTA_DM_REMT_NAME_EVT:
127         case BTA_DM_SEARCH_CMPL_EVT:
128         case BTA_DM_DISCOVERY_RESULT_EVT:
129           bta_dm_search_set_state(BTA_DM_SEARCH_IDLE);
130           bta_dm_free_sdp_db();
131           bta_dm_search_cancel_notify();
132           bta_dm_search_cancel_cmpl();
133           break;
134       }
135       break;
136     case BTA_DM_DISCOVER_ACTIVE:
137       switch (p_msg->event) {
138         case BTA_DM_REMT_NAME_EVT:
139           bta_dm_disc_rmt_name(message);
140           break;
141         case BTA_DM_SDP_RESULT_EVT:
142           bta_dm_sdp_result(message);
143           break;
144         case BTA_DM_SEARCH_CMPL_EVT:
145           bta_dm_search_cmpl();
146           break;
147         case BTA_DM_DISCOVERY_RESULT_EVT:
148           bta_dm_disc_result(message);
149           break;
150         case BTA_DM_API_SEARCH_EVT:
151         case BTA_DM_API_QUEUE_SEARCH_EVT:
152           bta_dm_queue_search(message);
153           break;
154         case BTA_DM_API_DISCOVER_EVT:
155         case BTA_DM_API_QUEUE_DISCOVER_EVT:
156           bta_dm_queue_disc(message);
157           break;
158       }
159       break;
160   }
161   return true;
162 }
163