1 /******************************************************************************
3 * Copyright 2016 The Android Open Source Project
4 * Copyright 2005-2012 Broadcom Corporation
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 ******************************************************************************/
20 /******************************************************************************
22 * This file contains the HID host main functions and state machine.
24 ******************************************************************************/
29 #include "bt_target.h" // Must be first to define build configuration
30 #if defined(BTA_HD_INCLUDED) && (BTA_HD_INCLUDED == TRUE)
32 #include "bta/hd/bta_hd_int.h"
34 /*****************************************************************************
36 ****************************************************************************/
38 /* state machine states */
41 BTA_HD_IDLE_ST, /* not connected, waiting for connection */
42 BTA_HD_CONN_ST, /* host connected */
43 BTA_HD_TRANSIENT_TO_INIT_ST, /* transient state: going back from CONN to INIT
46 typedef uint8_t tBTA_HD_STATE;
48 /*****************************************************************************
50 ****************************************************************************/
53 static tBTA_HD_STATE get_state() { return bta_hd_cb.state; }
55 static void set_state(tBTA_HD_STATE state) { bta_hd_cb.state = state; }
57 static void bta_hd_better_state_machine(uint16_t event, tBTA_HD_DATA* p_data) {
58 switch (get_state()) {
61 case BTA_HD_API_REGISTER_APP_EVT:
62 set_state(BTA_HD_IDLE_ST);
63 bta_hd_register_act(p_data);
65 case BTA_HD_API_ADD_DEVICE_EVT:
66 bta_hd_add_device_act(p_data);
68 case BTA_HD_API_REMOVE_DEVICE_EVT:
69 bta_hd_remove_device_act(p_data);
75 case BTA_HD_API_UNREGISTER_APP_EVT:
76 set_state(BTA_HD_INIT_ST);
77 bta_hd_unregister_act();
79 case BTA_HD_API_CONNECT_EVT:
80 bta_hd_connect_act(p_data);
82 case BTA_HD_API_DISCONNECT_EVT:
83 bta_hd_disconnect_act();
85 case BTA_HD_API_ADD_DEVICE_EVT:
86 bta_hd_add_device_act(p_data);
88 case BTA_HD_API_REMOVE_DEVICE_EVT:
89 bta_hd_remove_device_act(p_data);
91 case BTA_HD_API_SEND_REPORT_EVT:
92 bta_hd_send_report_act(p_data);
94 case BTA_HD_INT_OPEN_EVT:
95 set_state(BTA_HD_CONN_ST);
96 bta_hd_open_act(p_data);
98 case BTA_HD_INT_CLOSE_EVT:
99 bta_hd_close_act(p_data);
105 case BTA_HD_API_UNREGISTER_APP_EVT:
106 set_state(BTA_HD_TRANSIENT_TO_INIT_ST);
107 bta_hd_disconnect_act();
109 case BTA_HD_API_DISCONNECT_EVT:
110 bta_hd_disconnect_act();
112 case BTA_HD_API_ADD_DEVICE_EVT:
113 bta_hd_add_device_act(p_data);
115 case BTA_HD_API_REMOVE_DEVICE_EVT:
116 bta_hd_remove_device_act(p_data);
118 case BTA_HD_API_SEND_REPORT_EVT:
119 bta_hd_send_report_act(p_data);
121 case BTA_HD_API_REPORT_ERROR_EVT:
122 bta_hd_report_error_act(p_data);
124 case BTA_HD_API_VC_UNPLUG_EVT:
125 bta_hd_vc_unplug_act();
127 case BTA_HD_INT_CLOSE_EVT:
128 set_state(BTA_HD_IDLE_ST);
129 bta_hd_close_act(p_data);
131 case BTA_HD_INT_INTR_DATA_EVT:
132 bta_hd_intr_data_act(p_data);
134 case BTA_HD_INT_GET_REPORT_EVT:
135 bta_hd_get_report_act(p_data);
137 case BTA_HD_INT_SET_REPORT_EVT:
138 bta_hd_set_report_act(p_data);
140 case BTA_HD_INT_SET_PROTOCOL_EVT:
141 bta_hd_set_protocol_act(p_data);
143 case BTA_HD_INT_VC_UNPLUG_EVT:
144 set_state(BTA_HD_IDLE_ST);
145 bta_hd_vc_unplug_done_act(p_data);
147 case BTA_HD_INT_SUSPEND_EVT:
148 bta_hd_suspend_act(p_data);
150 case BTA_HD_INT_EXIT_SUSPEND_EVT:
151 bta_hd_exit_suspend_act(p_data);
155 case BTA_HD_TRANSIENT_TO_INIT_ST:
157 case BTA_HD_INT_CLOSE_EVT:
158 set_state(BTA_HD_INIT_ST);
159 bta_hd_unregister2_act(p_data);
161 case BTA_HD_INT_VC_UNPLUG_EVT:
162 set_state(BTA_HD_INIT_ST);
163 bta_hd_unregister2_act(p_data);
170 /*******************************************************************************
172 * Function bta_hd_hdl_event
174 * Description HID device main event handling function.
178 ******************************************************************************/
179 bool bta_hd_hdl_event(BT_HDR_RIGID* p_msg) {
180 APPL_TRACE_API("%s: p_msg->event=%d", __func__, p_msg->event);
182 switch (p_msg->event) {
183 case BTA_HD_API_ENABLE_EVT:
184 bta_hd_api_enable((tBTA_HD_DATA*)p_msg);
187 case BTA_HD_API_DISABLE_EVT:
188 if (bta_hd_cb.state == BTA_HD_CONN_ST) {
189 APPL_TRACE_WARNING("%s: host connected, disconnect before disabling",
192 // unregister (and disconnect)
193 bta_hd_cb.disable_w4_close = TRUE;
194 bta_hd_better_state_machine(BTA_HD_API_UNREGISTER_APP_EVT,
195 (tBTA_HD_DATA*)p_msg);
197 bta_hd_api_disable();
202 bta_hd_better_state_machine(p_msg->event, (tBTA_HD_DATA*)p_msg);
207 #endif /* BTA_HD_INCLUDED */