1 /******************************************************************************
2 *
3 * Copyright 2002-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 #ifndef BTA_HH_API_H
19 #define BTA_HH_API_H
20
21 #include <base/strings/stringprintf.h>
22
23 #include <cstdint>
24 #include <string>
25
26 #include "bta/include/bta_api.h"
27 #include "stack/include/bt_hdr.h"
28 #include "stack/include/hiddefs.h"
29 #include "types/raw_address.h"
30
31 /*****************************************************************************
32 * Constants and Type Definitions
33 ****************************************************************************/
34 #ifndef BTA_HH_DEBUG
35 #define BTA_HH_DEBUG TRUE
36 #endif
37
38 #ifndef BTA_HH_SSR_MAX_LATENCY_DEF
39 #define BTA_HH_SSR_MAX_LATENCY_DEF 800 /* 500 ms*/
40 #endif
41
42 #ifndef BTA_HH_SSR_MIN_TOUT_DEF
43 #define BTA_HH_SSR_MIN_TOUT_DEF 2
44 #endif
45
46 #ifndef CASE_RETURN_TEXT
47 #define CASE_RETURN_TEXT(code) \
48 case code: \
49 return #code
50 #endif
51
52 /* BTA HID Host callback events */
53 #define BTA_HH_ENABLE_EVT 0 /* HH enabled */
54 #define BTA_HH_DISABLE_EVT 1 /* HH disabled */
55 #define BTA_HH_OPEN_EVT 2 /* connection opened */
56 #define BTA_HH_CLOSE_EVT 3 /* connection closed */
57 #define BTA_HH_GET_RPT_EVT 4 /* BTA_HhGetReport callback */
58 #define BTA_HH_SET_RPT_EVT 5 /* BTA_HhSetReport callback */
59 #define BTA_HH_GET_PROTO_EVT 6 /* BTA_GetProtoMode callback */
60 #define BTA_HH_SET_PROTO_EVT 7 /* BTA_HhSetProtoMode callback */
61 #define BTA_HH_GET_IDLE_EVT 8 /* BTA_HhGetIdle comes callback */
62 #define BTA_HH_SET_IDLE_EVT 9 /* BTA_HhSetIdle finish callback */
63 #define BTA_HH_GET_DSCP_EVT 10 /* Get report descriptor */
64 #define BTA_HH_ADD_DEV_EVT 11 /* Add Device callback */
65 #define BTA_HH_RMV_DEV_EVT 12 /* remove device finished */
66 #define BTA_HH_VC_UNPLUG_EVT 13 /* virtually unplugged */
67 #define BTA_HH_DATA_EVT 15
68 #define BTA_HH_API_ERR_EVT 16 /* API error is caught */
69 #define BTA_HH_UPDATE_SCPP_EVT 17 /* update scan paramter complete */
70
71 typedef uint16_t tBTA_HH_EVT;
72
73 /* application ID(none-zero) for each type of device */
74 #define BTA_HH_APP_ID_MI 1
75 #define BTA_HH_APP_ID_KB 2
76 #define BTA_HH_APP_ID_RMC 3
77 #define BTA_HH_APP_ID_3DSG 4
78 #define BTA_HH_APP_ID_JOY 5
79 #define BTA_HH_APP_ID_GPAD 6
80 #define BTA_HH_APP_ID_LE 0xff
81
82 /* defined the minimum offset */
83 #define BTA_HH_MIN_OFFSET (L2CAP_MIN_OFFSET + 1)
84
85 /* HID_HOST_MAX_DEVICES can not exceed 15 for th design of BTA HH */
86 #define BTA_HH_IDX_INVALID 0xff
87 #define BTA_HH_MAX_KNOWN HID_HOST_MAX_DEVICES
88
89 /* GATT_MAX_PHY_CHANNEL can not exceed 14 for the design of BTA HH */
90 #if GATT_MAX_PHY_CHANNEL > 14
91 #define BTA_HH_LE_MAX_KNOWN 14
92 #else
93 #define BTA_HH_LE_MAX_KNOWN GATT_MAX_PHY_CHANNEL
94 #endif
95
96 #define BTA_HH_MAX_DEVICE (HID_HOST_MAX_DEVICES + BTA_HH_LE_MAX_KNOWN)
97 /* invalid device handle */
98 #define BTA_HH_INVALID_HANDLE 0xff
99
100 /* type of protocol mode */
101 #define BTA_HH_PROTO_RPT_MODE (0x00)
102 #define BTA_HH_PROTO_BOOT_MODE (0x01)
103 #define BTA_HH_PROTO_UNKNOWN (0xff)
104 typedef uint8_t tBTA_HH_PROTO_MODE;
105
106 enum { BTA_HH_KEYBD_RPT_ID = 1, BTA_HH_MOUSE_RPT_ID };
107 typedef uint8_t tBTA_HH_BOOT_RPT_ID;
108
109 /* type of devices, bit mask */
110 #define BTA_HH_DEVT_UNKNOWN 0x00
111 #define BTA_HH_DEVT_JOS 0x01 /* joy stick */
112 #define BTA_HH_DEVT_GPD 0x02 /* game pad */
113 #define BTA_HH_DEVT_RMC 0x03 /* remote control */
114 #define BTA_HH_DEVT_SED 0x04 /* sensing device */
115 #define BTA_HH_DEVT_DGT 0x05 /* Digitizer tablet */
116 #define BTA_HH_DEVT_CDR 0x06 /* card reader */
117 #define BTA_HH_DEVT_KBD 0x10 /* keyboard */
118 #define BTA_HH_DEVT_MIC 0x20 /* pointing device */
119 #define BTA_HH_DEVT_COM 0x30 /* Combo keyboard/pointing */
120 #define BTA_HH_DEVT_OTHER 0x80
121 typedef uint8_t tBTA_HH_DEVT;
122
123 typedef enum : uint8_t {
124 BTA_HH_OK = 0,
125 BTA_HH_HS_HID_NOT_READY, /* handshake error : device not ready */
126 BTA_HH_HS_INVALID_RPT_ID, /* handshake error : invalid report ID */
127 BTA_HH_HS_TRANS_NOT_SPT, /* handshake error : transaction not spt */
128 BTA_HH_HS_INVALID_PARAM, /* handshake error : invalid paremter */
129 BTA_HH_HS_ERROR, /* handshake error : unspecified HS error */
130 BTA_HH_ERR, /* general BTA HH error */
131 BTA_HH_ERR_SDP, /* SDP error */
132 BTA_HH_ERR_PROTO, /* SET_Protocol error,
133 only used in BTA_HH_OPEN_EVT callback */
134
135 BTA_HH_ERR_DB_FULL, /* device database full error, used in
136 BTA_HH_OPEN_EVT/BTA_HH_ADD_DEV_EVT */
137 BTA_HH_ERR_TOD_UNSPT, /* type of device not supported */
138 BTA_HH_ERR_NO_RES, /* out of system resources */
139 BTA_HH_ERR_AUTH_FAILED, /* authentication fail */
140 BTA_HH_ERR_HDL,
141 BTA_HH_ERR_SEC
142 } tBTA_HH_STATUS;
143
to_bta_hh_status(uint32_t status)144 inline tBTA_HH_STATUS to_bta_hh_status(uint32_t status) {
145 return static_cast<tBTA_HH_STATUS>(status);
146 }
147
bta_hh_status_text(const tBTA_HH_STATUS & status)148 inline std::string bta_hh_status_text(const tBTA_HH_STATUS& status) {
149 switch (status) {
150 CASE_RETURN_TEXT(BTA_HH_OK);
151 CASE_RETURN_TEXT(BTA_HH_HS_HID_NOT_READY);
152 CASE_RETURN_TEXT(BTA_HH_HS_INVALID_RPT_ID);
153 CASE_RETURN_TEXT(BTA_HH_HS_TRANS_NOT_SPT);
154 CASE_RETURN_TEXT(BTA_HH_HS_INVALID_PARAM);
155 CASE_RETURN_TEXT(BTA_HH_HS_ERROR);
156 CASE_RETURN_TEXT(BTA_HH_ERR);
157 CASE_RETURN_TEXT(BTA_HH_ERR_SDP);
158 CASE_RETURN_TEXT(BTA_HH_ERR_PROTO);
159 CASE_RETURN_TEXT(BTA_HH_ERR_DB_FULL);
160 CASE_RETURN_TEXT(BTA_HH_ERR_TOD_UNSPT);
161 CASE_RETURN_TEXT(BTA_HH_ERR_NO_RES);
162 CASE_RETURN_TEXT(BTA_HH_ERR_AUTH_FAILED);
163 CASE_RETURN_TEXT(BTA_HH_ERR_HDL);
164 CASE_RETURN_TEXT(BTA_HH_ERR_SEC);
165 default:
166 return base::StringPrintf("UNKNOWN[%hhu]", status);
167 }
168 }
169
170 typedef uint16_t tBTA_HH_ATTR_MASK;
171
172 /* supported type of device and corresponding application ID */
173 typedef struct {
174 tBTA_HH_DEVT tod; /* type of device */
175 uint8_t app_id; /* corresponding application ID */
176 } tBTA_HH_SPT_TOD;
177
178 /* configuration struct */
179 typedef struct {
180 uint8_t max_devt_spt; /* max number of types of devices spt */
181 tBTA_HH_SPT_TOD* p_devt_list; /* supported types of device list */
182 uint16_t sdp_db_size;
183 } tBTA_HH_CFG;
184
185 enum {
186 BTA_HH_RPTT_RESRV, /* reserved */
187 BTA_HH_RPTT_INPUT, /* input report */
188 BTA_HH_RPTT_OUTPUT, /* output report */
189 BTA_HH_RPTT_FEATURE /* feature report */
190 };
191 typedef uint8_t tBTA_HH_RPT_TYPE;
192
193 /* HID_CONTROL operation code used in BTA_HhSendCtrl()
194 */
195 enum {
196 BTA_HH_CTRL_NOP = 0 + HID_PAR_CONTROL_NOP, /* mapping from BTE */
197 BTA_HH_CTRL_HARD_RESET, /* hard reset */
198 BTA_HH_CTRL_SOFT_RESET, /* soft reset */
199 BTA_HH_CTRL_SUSPEND, /* enter suspend */
200 BTA_HH_CTRL_EXIT_SUSPEND, /* exit suspend */
201 BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG /* virtual unplug */
202 };
203 typedef uint8_t tBTA_HH_TRANS_CTRL_TYPE;
204
205 typedef tHID_DEV_DSCP_INFO tBTA_HH_DEV_DESCR;
206
207 #define BTA_HH_SSR_PARAM_INVALID HID_SSR_PARAM_INVALID
208
209 /* id DI is not existing in remote device, vendor_id in tBTA_HH_DEV_DSCP_INFO
210 * will be set to 0xffff */
211 #define BTA_HH_VENDOR_ID_INVALID 0xffff
212
213 /* report descriptor information */
214 typedef struct {
215 uint16_t vendor_id; /* vendor ID */
216 uint16_t product_id; /* product ID */
217 uint16_t version; /* version */
218 uint16_t ssr_max_latency; /* SSR max latency, BTA_HH_SSR_PARAM_INVALID if
219 unknown */
220 uint16_t
221 ssr_min_tout; /* SSR min timeout, BTA_HH_SSR_PARAM_INVALID if unknown */
222 uint8_t ctry_code; /*Country Code.*/
223 #define BTA_HH_LE_REMOTE_WAKE 0x01
224 #define BTA_HH_LE_NORMAL_CONN 0x02
225
226 uint8_t flag;
227 tBTA_HH_DEV_DESCR descriptor;
228 uint8_t hid_handle;
229
ToString__anon297d73c80708230 std::string ToString() const {
231 return base::StringPrintf("%04x::%04x::%04x", vendor_id, product_id,
232 version);
233 }
234 } tBTA_HH_DEV_DSCP_INFO;
235
236 /* callback event data for BTA_HH_OPEN_EVT */
237 typedef struct {
238 RawAddress bda; /* HID device bd address */
239 tBTA_HH_STATUS status; /* operation status */
240 uint8_t handle; /* device handle */
241 bool le_hid; /* is LE devices? */
242 bool scps_supported; /* scan parameter service supported */
243
244 } tBTA_HH_CONN;
245
246 typedef tBTA_HH_CONN tBTA_HH_DEV_INFO;
247
248 /* callback event data */
249 typedef struct {
250 tBTA_HH_STATUS status; /* operation status */
251 uint8_t handle; /* device handle */
252 } tBTA_HH_CBDATA;
253
254 enum {
255 BTA_HH_MOD_CTRL_KEY,
256 BTA_HH_MOD_SHFT_KEY,
257 BTA_HH_MOD_ALT_KEY,
258 BTA_HH_MOD_GUI_KEY,
259 BTA_HH_MOD_MAX_KEY
260 };
261
262 /* parsed boot mode keyboard report */
263 typedef struct {
264 uint8_t this_char[6]; /* virtual key code */
265 bool mod_key[BTA_HH_MOD_MAX_KEY];
266 /* ctrl, shift, Alt, GUI */
267 /* modifier key: is Shift key pressed */
268 /* modifier key: is Ctrl key pressed */
269 /* modifier key: is Alt key pressed */
270 /* modifier key: GUI up/down */
271 bool caps_lock; /* is caps locked */
272 bool num_lock; /* is Num key pressed */
273 } tBTA_HH_KEYBD_RPT;
274
275 /* parsed boot mode mouse report */
276 typedef struct {
277 uint8_t mouse_button; /* mouse button is clicked */
278 int8_t delta_x; /* displacement x */
279 int8_t delta_y; /* displacement y */
280 } tBTA_HH_MICE_RPT;
281
282 /* parsed Boot report */
283 typedef struct {
284 tBTA_HH_BOOT_RPT_ID dev_type; /* type of device report */
285 union {
286 tBTA_HH_KEYBD_RPT keybd_rpt; /* keyboard report */
287 tBTA_HH_MICE_RPT mice_rpt; /* mouse report */
288 } data_rpt;
289 } tBTA_HH_BOOT_RPT;
290
291 /* handshake data */
292 typedef struct {
293 tBTA_HH_STATUS status; /* handshake status */
294 uint8_t handle; /* device handle */
295 union {
296 tBTA_HH_PROTO_MODE proto_mode; /* GET_PROTO_EVT :protocol mode */
297 BT_HDR* p_rpt_data; /* GET_RPT_EVT : report data */
298 uint8_t idle_rate; /* GET_IDLE_EVT : idle rate */
299 } rsp_data;
300
301 } tBTA_HH_HSDATA;
302
303 /* union of data associated with HD callback */
304 typedef union {
305 tBTA_HH_DEV_INFO dev_info; /* BTA_HH_ADD_DEV_EVT, BTA_HH_RMV_DEV_EVT */
306 tBTA_HH_CONN conn; /* BTA_HH_OPEN_EVT */
307 tBTA_HH_CBDATA dev_status; /* BTA_HH_CLOSE_EVT,
308 BTA_HH_SET_PROTO_EVT
309 BTA_HH_SET_RPT_EVT
310 BTA_HH_SET_IDLE_EVT
311 BTA_HH_UPDATE_SCPP_EVT */
312
313 tBTA_HH_STATUS status; /* BTA_HH_ENABLE_EVT */
314 tBTA_HH_DEV_DSCP_INFO dscp_info; /* BTA_HH_GET_DSCP_EVT */
315 tBTA_HH_HSDATA hs_data; /* GET_ transaction callback
316 BTA_HH_GET_RPT_EVT
317 BTA_HH_GET_PROTO_EVT
318 BTA_HH_GET_IDLE_EVT */
319 } tBTA_HH;
320
321 /* BTA HH callback function */
322 typedef void(tBTA_HH_CBACK)(tBTA_HH_EVT event, tBTA_HH* p_data);
323
324 /*****************************************************************************
325 * External Function Declarations
326 ****************************************************************************/
327
328 /*******************************************************************************
329 *
330 * Function BTA_HhRegister
331 *
332 * Description This function enable HID host and registers HID-Host with
333 * lower layers.
334 *
335 * Returns void
336 *
337 ******************************************************************************/
338 extern void BTA_HhEnable(tBTA_HH_CBACK* p_cback);
339
340 /*******************************************************************************
341 *
342 * Function BTA_HhDeregister
343 *
344 * Description This function is called when the host is about power down.
345 *
346 * Returns void
347 *
348 ******************************************************************************/
349 extern void BTA_HhDisable(void);
350
351 /*******************************************************************************
352 *
353 * Function BTA_HhOpen
354 *
355 * Description This function is called to start an inquiry and read SDP
356 * record of responding devices; connect to a device if only
357 * one active HID device is found.
358 *
359 * Returns void
360 *
361 ******************************************************************************/
362 extern void BTA_HhOpen(const RawAddress& dev_bda);
363
364 /*******************************************************************************
365 *
366 * Function BTA_HhClose
367 *
368 * Description This function disconnects the device.
369 *
370 * Returns void
371 *
372 ******************************************************************************/
373 extern void BTA_HhClose(uint8_t dev_handle);
374
375 /*******************************************************************************
376 *
377 * Function BTA_HhSetProtoMode
378 *
379 * Description This function set the protocol mode at specified HID handle
380 *
381 * Returns void
382 *
383 ******************************************************************************/
384 extern void BTA_HhSetProtoMode(uint8_t handle, tBTA_HH_PROTO_MODE t_type);
385
386 /*******************************************************************************
387 *
388 * Function BTA_HhGetProtoMode
389 *
390 * Description This function get the protocol mode of a specified HID
391 * device.
392 *
393 * Returns void
394 *
395 ******************************************************************************/
396 extern void BTA_HhGetProtoMode(uint8_t dev_handle);
397 /*******************************************************************************
398 *
399 * Function BTA_HhSetReport
400 *
401 * Description send SET_REPORT to device.
402 *
403 * Returns void
404 *
405 ******************************************************************************/
406 extern void BTA_HhSetReport(uint8_t dev_handle, tBTA_HH_RPT_TYPE r_type,
407 BT_HDR* p_data);
408
409 /*******************************************************************************
410 *
411 * Function BTA_HhGetReport
412 *
413 * Description Send a GET_REPORT to HID device.
414 *
415 * Returns void
416 *
417 ******************************************************************************/
418 extern void BTA_HhGetReport(uint8_t dev_handle, tBTA_HH_RPT_TYPE r_type,
419 uint8_t rpt_id, uint16_t buf_size);
420 /*******************************************************************************
421 *
422 * Function BTA_HhSetIdle
423 *
424 * Description send SET_IDLE to device.
425 *
426 * Returns void
427 *
428 ******************************************************************************/
429 extern void BTA_HhSetIdle(uint8_t dev_handle, uint16_t idle_rate);
430
431 /*******************************************************************************
432 *
433 * Function BTA_HhGetIdle
434 *
435 * Description Send a GET_IDLE to HID device.
436 *
437 * Returns void
438 *
439 ******************************************************************************/
440 extern void BTA_HhGetIdle(uint8_t dev_handle);
441
442 /*******************************************************************************
443 *
444 * Function BTA_HhSendCtrl
445 *
446 * Description Send HID_CONTROL request to a HID device.
447 *
448 * Returns void
449 *
450 ******************************************************************************/
451 extern void BTA_HhSendCtrl(uint8_t dev_handle, tBTA_HH_TRANS_CTRL_TYPE c_type);
452
453 /*******************************************************************************
454 *
455 * Function BTA_HhSetIdle
456 *
457 * Description send SET_IDLE to device.
458 *
459 * Returns void
460 *
461 ******************************************************************************/
462 extern void BTA_HhSetIdle(uint8_t dev_handle, uint16_t idle_rate);
463
464 /*******************************************************************************
465 *
466 * Function BTA_HhGetIdle
467 *
468 * Description Send a GET_IDLE from HID device.
469 *
470 * Returns void
471 *
472 ******************************************************************************/
473 extern void BTA_HhGetIdle(uint8_t dev_handle);
474
475 /*******************************************************************************
476 *
477 * Function BTA_HhSendData
478 *
479 * Description Send DATA transaction to a HID device.
480 *
481 * Returns void
482 *
483 ******************************************************************************/
484 extern void BTA_HhSendData(uint8_t dev_handle, const RawAddress& dev_bda,
485 BT_HDR* p_buf);
486
487 /*******************************************************************************
488 *
489 * Function BTA_HhGetDscpInfo
490 *
491 * Description Get report descriptor of the device
492 *
493 * Returns void
494 *
495 ******************************************************************************/
496 extern void BTA_HhGetDscpInfo(uint8_t dev_handle);
497
498 /*******************************************************************************
499 * Function BTA_HhAddDev
500 *
501 * Description Add a virtually cabled device into HID-Host device list
502 * to manage and assign a device handle for future API call,
503 * host applciation call this API at start-up to initialize its
504 * virtually cabled devices.
505 *
506 * Returns void
507 *
508 ******************************************************************************/
509 extern void BTA_HhAddDev(const RawAddress& bda, tBTA_HH_ATTR_MASK attr_mask,
510 uint8_t sub_class, uint8_t app_id,
511 tBTA_HH_DEV_DSCP_INFO dscp_info);
512 /*******************************************************************************
513 *
514 * Function BTA_HhRemoveDev
515 *
516 * Description Remove a device from the HID host devices list.
517 *
518 * Returns void
519 *
520 ******************************************************************************/
521 extern void BTA_HhRemoveDev(uint8_t dev_handle);
522
523 #endif /* BTA_HH_API_H */
524