1 /*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef ANDROID_INCLUDE_BT_HH_H
18 #define ANDROID_INCLUDE_BT_HH_H
19
20 #include <base/strings/stringprintf.h>
21 #include <raw_address.h>
22 #include <stdint.h>
23
24 #include <string>
25
26 __BEGIN_DECLS
27
28 #define BTHH_MAX_DSC_LEN 884
29
30 /* HH connection states */
31 typedef enum {
32 BTHH_CONN_STATE_CONNECTED = 0,
33 BTHH_CONN_STATE_CONNECTING = 1,
34 BTHH_CONN_STATE_DISCONNECTED = 2,
35 BTHH_CONN_STATE_DISCONNECTING = 3,
36 BTHH_CONN_STATE_UNKNOWN = 0xff,
37 } bthh_connection_state_t;
38
39 __END_DECLS
40 #define CASE_RETURN_TEXT(code) \
41 case code: \
42 return #code
43
bthh_connection_state_text(const bthh_connection_state_t & state)44 inline std::string bthh_connection_state_text(
45 const bthh_connection_state_t& state) {
46 switch (state) {
47 CASE_RETURN_TEXT(BTHH_CONN_STATE_CONNECTED);
48 CASE_RETURN_TEXT(BTHH_CONN_STATE_CONNECTING);
49 CASE_RETURN_TEXT(BTHH_CONN_STATE_DISCONNECTED);
50 CASE_RETURN_TEXT(BTHH_CONN_STATE_DISCONNECTING);
51 CASE_RETURN_TEXT(BTHH_CONN_STATE_UNKNOWN);
52 default:
53 return base::StringPrintf("UNKNOWN[%d]", state);
54 }
55 }
56 #undef CASE_RETURN_TEXT
57 __BEGIN_DECLS
58
59 typedef enum {
60 BTHH_OK = 0,
61 BTHH_HS_HID_NOT_READY, /* handshake error : device not ready */
62 BTHH_HS_INVALID_RPT_ID, /* handshake error : invalid report ID */
63 BTHH_HS_TRANS_NOT_SPT, /* handshake error : transaction not spt */
64 BTHH_HS_INVALID_PARAM, /* handshake error : invalid paremter */
65 BTHH_HS_ERROR, /* handshake error : unspecified HS error */
66 BTHH_ERR, /* general BTA HH error */
67 BTHH_ERR_SDP, /* SDP error */
68 BTHH_ERR_PROTO, /* SET_Protocol error,
69 only used in BTA_HH_OPEN_EVT
70 callback */
71 BTHH_ERR_DB_FULL, /* device database full error, used */
72 BTHH_ERR_TOD_UNSPT, /* type of device not supported */
73 BTHH_ERR_NO_RES, /* out of system resources */
74 BTHH_ERR_AUTH_FAILED, /* authentication fail */
75 BTHH_ERR_HDL
76 } bthh_status_t;
77
78 /* Protocol modes */
79 typedef enum {
80 BTHH_REPORT_MODE = 0x00,
81 BTHH_BOOT_MODE = 0x01,
82 BTHH_UNSUPPORTED_MODE = 0xff
83 } bthh_protocol_mode_t;
84
85 /* Report types */
86 typedef enum {
87 BTHH_INPUT_REPORT = 1,
88 BTHH_OUTPUT_REPORT,
89 BTHH_FEATURE_REPORT
90 } bthh_report_type_t;
91
92 /* Info for which profiles to enable */
93 typedef struct {
94 bool hidp_enabled;
95 bool hogp_enabled;
96 } bthh_profile_enable_t;
97
98 typedef struct {
99 int attr_mask;
100 uint8_t sub_class;
101 uint8_t app_id;
102 int vendor_id;
103 int product_id;
104 int version;
105 uint8_t ctry_code;
106 int dl_len;
107 uint8_t dsc_list[BTHH_MAX_DSC_LEN];
108 } bthh_hid_info_t;
109
110 /** Callback for connection state change.
111 * state will have one of the values from bthh_connection_state_t
112 */
113 typedef void (*bthh_connection_state_callback)(RawAddress* bd_addr,
114 bthh_connection_state_t state);
115
116 /** Callback for vitual unplug api.
117 * the status of the vitual unplug
118 */
119 typedef void (*bthh_virtual_unplug_callback)(RawAddress* bd_addr,
120 bthh_status_t hh_status);
121
122 /** Callback for get hid info
123 * hid_info will contain attr_mask, sub_class, app_id, vendor_id, product_id,
124 * version, ctry_code, len
125 */
126 typedef void (*bthh_hid_info_callback)(RawAddress* bd_addr,
127 bthh_hid_info_t hid_info);
128
129 /** Callback for get protocol api.
130 * the protocol mode is one of the value from bthh_protocol_mode_t
131 */
132 typedef void (*bthh_protocol_mode_callback)(RawAddress* bd_addr,
133 bthh_status_t hh_status,
134 bthh_protocol_mode_t mode);
135
136 /** Callback for get/set_idle_time api.
137 */
138 typedef void (*bthh_idle_time_callback)(RawAddress* bd_addr,
139 bthh_status_t hh_status, int idle_rate);
140
141 /** Callback for get report api.
142 * if staus is ok rpt_data contains the report data
143 */
144 typedef void (*bthh_get_report_callback)(RawAddress* bd_addr,
145 bthh_status_t hh_status,
146 uint8_t* rpt_data, int rpt_size);
147
148 /** Callback for set_report/set_protocol api and if error
149 * occurs for get_report/get_protocol api.
150 */
151 typedef void (*bthh_handshake_callback)(RawAddress* bd_addr,
152 bthh_status_t hh_status);
153
154 /** BT-HH callback structure. */
155 typedef struct {
156 /** set to sizeof(BtHfCallbacks) */
157 size_t size;
158 bthh_connection_state_callback connection_state_cb;
159 bthh_hid_info_callback hid_info_cb;
160 bthh_protocol_mode_callback protocol_mode_cb;
161 bthh_idle_time_callback idle_time_cb;
162 bthh_get_report_callback get_report_cb;
163 bthh_virtual_unplug_callback virtual_unplug_cb;
164 bthh_handshake_callback handshake_cb;
165
166 } bthh_callbacks_t;
167
168 /** Represents the standard BT-HH interface. */
169 typedef struct {
170 /** set to sizeof(BtHhInterface) */
171 size_t size;
172
173 /**
174 * Register the BtHh callbacks
175 */
176 bt_status_t (*init)(bthh_callbacks_t* callbacks);
177
178 /** connect to hid device */
179 bt_status_t (*connect)(RawAddress* bd_addr);
180
181 /** dis-connect from hid device */
182 bt_status_t (*disconnect)(RawAddress* bd_addr);
183
184 /** Virtual UnPlug (VUP) the specified HID device */
185 bt_status_t (*virtual_unplug)(RawAddress* bd_addr);
186
187 /** Set the HID device descriptor for the specified HID device. */
188 bt_status_t (*set_info)(RawAddress* bd_addr, bthh_hid_info_t hid_info);
189
190 /** Get the HID proto mode. */
191 bt_status_t (*get_protocol)(RawAddress* bd_addr,
192 bthh_protocol_mode_t protocolMode);
193
194 /** Set the HID proto mode. */
195 bt_status_t (*set_protocol)(RawAddress* bd_addr,
196 bthh_protocol_mode_t protocolMode);
197
198 /** Get the HID Idle Time */
199 bt_status_t (*get_idle_time)(RawAddress* bd_addr);
200
201 /** Set the HID Idle Time */
202 bt_status_t (*set_idle_time)(RawAddress* bd_addr, uint8_t idleTime);
203
204 /** Send a GET_REPORT to HID device. */
205 bt_status_t (*get_report)(RawAddress* bd_addr, bthh_report_type_t reportType,
206 uint8_t reportId, int bufferSize);
207
208 /** Send a GET_REPORT_REPLY to HID driver. */
209 bt_status_t (*get_report_reply)(RawAddress* bd_addr, bthh_status_t status,
210 char* report, uint16_t size);
211
212 /** Send a SET_REPORT to HID device. */
213 bt_status_t (*set_report)(RawAddress* bd_addr, bthh_report_type_t reportType,
214 char* report);
215
216 /** Send data to HID device. */
217 bt_status_t (*send_data)(RawAddress* bd_addr, char* data);
218
219 /** Closes the interface. */
220 void (*cleanup)(void);
221
222 /** Configure which profiles can be enabled. Affected after re-init */
223 void (*configure_enabled_profiles)(bool enable_hidp, bool enable_hogp);
224
225 } bthh_interface_t;
226 __END_DECLS
227
228 #endif /* ANDROID_INCLUDE_BT_HH_H */
229