• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 2016 The Android Open Source Project
4  *  Copyright 2005-2012 Broadcom Corporation
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at:
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  ******************************************************************************/
19 
20 /******************************************************************************
21  *
22  *  This file contains the HID host main functions and state machine.
23  *
24  ******************************************************************************/
25 
26 #include <cstdint>
27 
28 // BTA_HD_INCLUDED
29 #include "bt_target.h"  // Must be first to define build configuration
30 #if defined(BTA_HD_INCLUDED) && (BTA_HD_INCLUDED == TRUE)
31 
32 #include "bta/hd/bta_hd_int.h"
33 #include "stack/include/bt_hdr.h"
34 
35 /*****************************************************************************
36  * Constants and types
37  ****************************************************************************/
38 
39 /* state machine states */
40 enum {
41   BTA_HD_INIT_ST,
42   BTA_HD_IDLE_ST,              /* not connected, waiting for connection */
43   BTA_HD_CONN_ST,              /* host connected */
44   BTA_HD_TRANSIENT_TO_INIT_ST, /* transient state: going back from CONN to INIT
45                                   */
46 };
47 typedef uint8_t tBTA_HD_STATE;
48 
49 /*****************************************************************************
50  * Global data
51  ****************************************************************************/
52 tBTA_HD_CB bta_hd_cb;
53 
get_state()54 static tBTA_HD_STATE get_state() { return bta_hd_cb.state; }
55 
set_state(tBTA_HD_STATE state)56 static void set_state(tBTA_HD_STATE state) { bta_hd_cb.state = state; }
57 
bta_hd_better_state_machine(uint16_t event,tBTA_HD_DATA * p_data)58 static void bta_hd_better_state_machine(uint16_t event, tBTA_HD_DATA* p_data) {
59   switch (get_state()) {
60     case BTA_HD_INIT_ST:
61       switch (event) {
62         case BTA_HD_API_REGISTER_APP_EVT:
63           set_state(BTA_HD_IDLE_ST);
64           bta_hd_register_act(p_data);
65           break;
66         case BTA_HD_API_ADD_DEVICE_EVT:
67           bta_hd_add_device_act(p_data);
68           break;
69         case BTA_HD_API_REMOVE_DEVICE_EVT:
70           bta_hd_remove_device_act(p_data);
71           break;
72       }
73       break;
74     case BTA_HD_IDLE_ST:
75       switch (event) {
76         case BTA_HD_API_UNREGISTER_APP_EVT:
77           set_state(BTA_HD_INIT_ST);
78           bta_hd_unregister_act();
79           break;
80         case BTA_HD_API_CONNECT_EVT:
81           bta_hd_connect_act(p_data);
82           break;
83         case BTA_HD_API_DISCONNECT_EVT:
84           bta_hd_disconnect_act();
85           break;
86         case BTA_HD_API_ADD_DEVICE_EVT:
87           bta_hd_add_device_act(p_data);
88           break;
89         case BTA_HD_API_REMOVE_DEVICE_EVT:
90           bta_hd_remove_device_act(p_data);
91           break;
92         case BTA_HD_API_SEND_REPORT_EVT:
93           bta_hd_send_report_act(p_data);
94           break;
95         case BTA_HD_INT_OPEN_EVT:
96           set_state(BTA_HD_CONN_ST);
97           bta_hd_open_act(p_data);
98           break;
99         case BTA_HD_INT_CLOSE_EVT:
100           bta_hd_close_act(p_data);
101           break;
102       }
103       break;
104     case BTA_HD_CONN_ST:
105       switch (event) {
106         case BTA_HD_API_UNREGISTER_APP_EVT:
107           set_state(BTA_HD_TRANSIENT_TO_INIT_ST);
108           bta_hd_disconnect_act();
109           break;
110         case BTA_HD_API_DISCONNECT_EVT:
111           bta_hd_disconnect_act();
112           break;
113         case BTA_HD_API_ADD_DEVICE_EVT:
114           bta_hd_add_device_act(p_data);
115           break;
116         case BTA_HD_API_REMOVE_DEVICE_EVT:
117           bta_hd_remove_device_act(p_data);
118           break;
119         case BTA_HD_API_SEND_REPORT_EVT:
120           bta_hd_send_report_act(p_data);
121           break;
122         case BTA_HD_API_REPORT_ERROR_EVT:
123           bta_hd_report_error_act(p_data);
124           break;
125         case BTA_HD_API_VC_UNPLUG_EVT:
126           bta_hd_vc_unplug_act();
127           break;
128         case BTA_HD_INT_CLOSE_EVT:
129           set_state(BTA_HD_IDLE_ST);
130           bta_hd_close_act(p_data);
131           break;
132         case BTA_HD_INT_INTR_DATA_EVT:
133           bta_hd_intr_data_act(p_data);
134           break;
135         case BTA_HD_INT_GET_REPORT_EVT:
136           bta_hd_get_report_act(p_data);
137           break;
138         case BTA_HD_INT_SET_REPORT_EVT:
139           bta_hd_set_report_act(p_data);
140           break;
141         case BTA_HD_INT_SET_PROTOCOL_EVT:
142           bta_hd_set_protocol_act(p_data);
143           break;
144         case BTA_HD_INT_VC_UNPLUG_EVT:
145           set_state(BTA_HD_IDLE_ST);
146           bta_hd_vc_unplug_done_act(p_data);
147           break;
148         case BTA_HD_INT_SUSPEND_EVT:
149           bta_hd_suspend_act(p_data);
150           break;
151         case BTA_HD_INT_EXIT_SUSPEND_EVT:
152           bta_hd_exit_suspend_act(p_data);
153           break;
154       }
155       break;
156     case BTA_HD_TRANSIENT_TO_INIT_ST:
157       switch (event) {
158         case BTA_HD_INT_CLOSE_EVT:
159           set_state(BTA_HD_INIT_ST);
160           bta_hd_unregister2_act(p_data);
161           break;
162         case BTA_HD_INT_VC_UNPLUG_EVT:
163           set_state(BTA_HD_INIT_ST);
164           bta_hd_unregister2_act(p_data);
165           break;
166       }
167       break;
168   }
169 }
170 
171 /*******************************************************************************
172  *
173  * Function         bta_hd_hdl_event
174  *
175  * Description      HID device main event handling function.
176  *
177  * Returns          void
178  *
179  ******************************************************************************/
bta_hd_hdl_event(BT_HDR_RIGID * p_msg)180 bool bta_hd_hdl_event(BT_HDR_RIGID* p_msg) {
181   APPL_TRACE_API("%s: p_msg->event=%d", __func__, p_msg->event);
182 
183   switch (p_msg->event) {
184     case BTA_HD_API_ENABLE_EVT:
185       bta_hd_api_enable((tBTA_HD_DATA*)p_msg);
186       break;
187 
188     case BTA_HD_API_DISABLE_EVT:
189       if (bta_hd_cb.state == BTA_HD_CONN_ST) {
190         APPL_TRACE_WARNING("%s: host connected, disconnect before disabling",
191                            __func__);
192 
193         // unregister (and disconnect)
194         bta_hd_cb.disable_w4_close = TRUE;
195         bta_hd_better_state_machine(BTA_HD_API_UNREGISTER_APP_EVT,
196                                     (tBTA_HD_DATA*)p_msg);
197       } else {
198         bta_hd_api_disable();
199       }
200       break;
201 
202     default:
203       bta_hd_better_state_machine(p_msg->event, (tBTA_HD_DATA*)p_msg);
204   }
205   return (TRUE);
206 }
207 
208 #endif /* BTA_HD_INCLUDED */
209