esp_bt_hh_api.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2005-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 _ESP_BT_HH_API_H_
  19. #define _ESP_BT_HH_API_H_
  20. #include "esp_err.h"
  21. #include "esp_bt_defs.h"
  22. /*****************************************************************************
  23. ** Constants and Type Definitions
  24. *****************************************************************************/
  25. #ifndef BTA_HH_DEBUG
  26. #define BTA_HH_DEBUG TRUE
  27. #endif
  28. #ifndef BTA_HH_SSR_MAX_LATENCY_DEF
  29. #define BTA_HH_SSR_MAX_LATENCY_DEF 800 /* 500 ms*/
  30. #endif
  31. #ifndef BTA_HH_SSR_MIN_TOUT_DEF
  32. #define BTA_HH_SSR_MIN_TOUT_DEF 2
  33. #endif
  34. /* defined the minimum offset */
  35. #define BTA_HH_MIN_OFFSET L2CAP_MIN_OFFSET+1
  36. /* HID_HOST_MAX_DEVICES can not exceed 15 for th design of BTA HH */
  37. #define BTA_HH_IDX_INVALID 0xff
  38. #define BTA_HH_MAX_KNOWN HID_HOST_MAX_DEVICES
  39. /* Security Service Levels [bit mask] (BTM_SetSecurityLevel)
  40. ** Encryption should not be used without authentication
  41. */
  42. #define BTM_SEC_NONE 0x0000 /* Nothing required */
  43. #define BTM_SEC_IN_AUTHORIZE 0x0001 /* Inbound call requires authorization */
  44. #define BTM_SEC_IN_AUTHENTICATE 0x0002 /* Inbound call requires authentication */
  45. #define BTM_SEC_IN_ENCRYPT 0x0004 /* Inbound call requires encryption */
  46. #define BTM_SEC_OUT_AUTHORIZE 0x0008 /* Outbound call requires authorization */
  47. #define BTM_SEC_OUT_AUTHENTICATE 0x0010 /* Outbound call requires authentication */
  48. #define BTM_SEC_OUT_ENCRYPT 0x0020 /* Outbound call requires encryption */
  49. #define BTM_SEC_MODE4_LEVEL4 0x0040 /* Secure Connections Only Mode */
  50. #define BTM_SEC_FORCE_MASTER 0x0100 /* Need to switch connection to be master */
  51. #define BTM_SEC_ATTEMPT_MASTER 0x0200 /* Try to switch connection to be master */
  52. #define BTM_SEC_FORCE_SLAVE 0x0400 /* Need to switch connection to be master */
  53. #define BTM_SEC_ATTEMPT_SLAVE 0x0800 /* Try to switch connection to be slave */
  54. #define BTM_SEC_IN_MITM 0x1000 /* inbound Do man in the middle protection */
  55. #define BTM_SEC_OUT_MITM 0x2000 /* outbound Do man in the middle protection */
  56. #define BTM_SEC_IN_MIN_16_DIGIT_PIN 0x4000 /* enforce a minimum of 16 digit for sec mode 2 */
  57. /* Security Setting Mask */
  58. #define BTA_SEC_NONE BTM_SEC_NONE /* No security. */
  59. #define BTA_SEC_AUTHORIZE (BTM_SEC_IN_AUTHORIZE ) /* Authorization required (only needed for out going connection )*/
  60. #define BTA_SEC_AUTHENTICATE (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_OUT_AUTHENTICATE) /* Authentication required. */
  61. #define BTA_SEC_ENCRYPT (BTM_SEC_IN_ENCRYPT | BTM_SEC_OUT_ENCRYPT) /* Encryption required. */
  62. #define BTA_SEC_MODE4_LEVEL4 (BTM_SEC_MODE4_LEVEL4) /* Mode 4 level 4 service, i.e. incoming/outgoing MITM and P-256 encryption */
  63. #define BTA_SEC_MITM (BTM_SEC_IN_MITM | BTM_SEC_OUT_MITM) /* Man-In-The_Middle protection */
  64. #define BTA_SEC_IN_16_DIGITS (BTM_SEC_IN_MIN_16_DIGIT_PIN) /* Min 16 digit for pin code */
  65. #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
  66. /* GATT_MAX_PHY_CHANNEL can not exceed 14 for the design of BTA HH */
  67. #define BTA_HH_LE_MAX_KNOWN GATT_MAX_PHY_CHANNEL
  68. #define BTA_HH_MAX_DEVICE (HID_HOST_MAX_DEVICES + GATT_MAX_PHY_CHANNEL)
  69. #else
  70. #define BTA_HH_MAX_DEVICE HID_HOST_MAX_DEVICES
  71. #endif
  72. /* invalid device handle */
  73. #define BTA_HH_INVALID_HANDLE 0xff
  74. #define BTA_HH_SSR_PARAM_INVALID HID_SSR_PARAM_INVALID
  75. /* id DI is not existing in remote device, vendor_id in tBTA_HH_DEV_DSCP_INFO will be set to 0xffff */
  76. #define BTA_HH_VENDOR_ID_INVALID 0xffff
  77. /* application ID(none-zero) for each type of device */
  78. #define BTA_HH_APP_ID_MI 1
  79. #define BTA_HH_APP_ID_KB 2
  80. #define BTA_HH_APP_ID_RMC 3
  81. #define BTA_HH_APP_ID_3DSG 4
  82. #define BTA_HH_APP_ID_JOY 5
  83. #define BTA_HH_APP_ID_GPAD 6
  84. #define BTA_HH_APP_ID_LE 0xff
  85. /* type of devices, bit mask */
  86. #define BTA_HH_DEVT_UNKNOWN 0x00
  87. #define BTA_HH_DEVT_JOS 0x01 /* joy stick */
  88. #define BTA_HH_DEVT_GPD 0x02 /* game pad */
  89. #define BTA_HH_DEVT_RMC 0x03 /* remote control */
  90. #define BTA_HH_DEVT_SED 0x04 /* sensing device */
  91. #define BTA_HH_DEVT_DGT 0x05 /* Digitizer tablet */
  92. #define BTA_HH_DEVT_CDR 0x06 /* card reader */
  93. #define BTA_HH_DEVT_KBD 0x10 /* keyboard */
  94. #define BTA_HH_DEVT_MIC 0x20 /* pointing device */
  95. #define BTA_HH_DEVT_COM 0x30 /* Combo keyboard/pointing */
  96. #define BTA_HH_DEVT_OTHER 0x80
  97. typedef uint8_t tBTA_HH_DEVT;
  98. /* BTA HID Host callback events */
  99. #define BTA_HH_ENABLE_EVT 0 /* HH enabled */
  100. #define BTA_HH_DISABLE_EVT 1 /* HH disabled */
  101. #define BTA_HH_OPEN_EVT 2 /* connection opened */
  102. #define BTA_HH_CLOSE_EVT 3 /* connection closed */
  103. #define BTA_HH_GET_RPT_EVT 4 /* BTA_HhGetReport callback */
  104. #define BTA_HH_SET_RPT_EVT 5 /* BTA_HhSetReport callback */
  105. #define BTA_HH_GET_PROTO_EVT 6 /* BTA_GetProtoMode callback */
  106. #define BTA_HH_SET_PROTO_EVT 7 /* BTA_HhSetProtoMode callback */
  107. #define BTA_HH_GET_IDLE_EVT 8 /* BTA_HhGetIdle comes callback */
  108. #define BTA_HH_SET_IDLE_EVT 9 /* BTA_HhSetIdle finish callback */
  109. #define BTA_HH_GET_DSCP_EVT 10 /* Get report descriptor */
  110. #define BTA_HH_ADD_DEV_EVT 11 /* Add Device callback */
  111. #define BTA_HH_RMV_DEV_EVT 12 /* remove device finished */
  112. #define BTA_HH_VC_UNPLUG_EVT 13 /* virtually unplugged */
  113. #define BTA_HH_DATA_EVT 15
  114. #define BTA_HH_API_ERR_EVT 16 /* API error is caught */
  115. #define BTA_HH_UPDATE_SCPP_EVT 17 /* update scan paramter complete */
  116. typedef uint16_t tBTA_HH_EVT;
  117. /* type of protocol mode */
  118. #define BTA_HH_PROTO_RPT_MODE (0x00)
  119. #define BTA_HH_PROTO_BOOT_MODE (0x01)
  120. #define BTA_HH_PROTO_UNKNOWN (0xff)
  121. typedef uint8_t tBTA_HH_PROTO_MODE;
  122. #define BTA_HH_VIRTUAL_CABLE HID_VIRTUAL_CABLE
  123. #define BTA_HH_NORMALLY_CONNECTABLE HID_NORMALLY_CONNECTABLE
  124. #define BTA_HH_RECONN_INIT HID_RECONN_INIT
  125. #define BTA_HH_SDP_DISABLE HID_SDP_DISABLE
  126. #define BTA_HH_BATTERY_POWER HID_BATTERY_POWER
  127. #define BTA_HH_REMOTE_WAKE HID_REMOTE_WAKE
  128. #define BTA_HH_SUP_TOUT_AVLBL HID_SUP_TOUT_AVLBL
  129. #define BTA_HH_SEC_REQUIRED HID_SEC_REQUIRED
  130. typedef uint16_t tBTA_HH_ATTR_MASK;
  131. enum {
  132. BTA_HH_OK,
  133. BTA_HH_HS_HID_NOT_READY, /* handshake error : device not ready */
  134. BTA_HH_HS_INVALID_RPT_ID, /* handshake error : invalid report ID */
  135. BTA_HH_HS_TRANS_NOT_SPT, /* handshake error : transaction not spt */
  136. BTA_HH_HS_INVALID_PARAM, /* handshake error : invalid paremter */
  137. BTA_HH_HS_ERROR, /* handshake error : unspecified HS error */
  138. BTA_HH_ERR, /* general BTA HH error */
  139. BTA_HH_ERR_SDP, /* SDP error */
  140. BTA_HH_ERR_PROTO, /* SET_Protocol error, only used in BTA_HH_OPEN_EVT callback */
  141. BTA_HH_ERR_DB_FULL, /* device database full error, used in BTA_HH_OPEN_EVT/BTA_HH_ADD_DEV_EVT */
  142. BTA_HH_ERR_TOD_UNSPT, /* type of device not supported */
  143. BTA_HH_ERR_NO_RES, /* out of system resources */
  144. BTA_HH_ERR_AUTH_FAILED, /* authentication fail */
  145. BTA_HH_ERR_HDL,
  146. BTA_HH_ERR_SEC
  147. };
  148. typedef uint8_t tBTA_HH_STATUS;
  149. enum {
  150. BTA_HH_RPTT_RESRV, /* reserved */
  151. BTA_HH_RPTT_INPUT, /* input report */
  152. BTA_HH_RPTT_OUTPUT, /* output report */
  153. BTA_HH_RPTT_FEATURE /* feature report */
  154. };
  155. typedef uint8_t tBTA_HH_RPT_TYPE;
  156. /* HID_CONTROL operation code used in BTA_HhSendCtrl()
  157. */
  158. enum {
  159. BTA_HH_CTRL_NOP = 0, /* mapping from BTE */
  160. BTA_HH_CTRL_HARD_RESET, /* hard reset */
  161. BTA_HH_CTRL_SOFT_RESET, /* soft reset */
  162. BTA_HH_CTRL_SUSPEND, /* enter suspend */
  163. BTA_HH_CTRL_EXIT_SUSPEND, /* exit suspend */
  164. BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG /* virtual unplug */
  165. };
  166. typedef uint8_t tBTA_HH_TRANS_CTRL_TYPE;
  167. typedef struct desc_info {
  168. uint16_t dl_len;
  169. uint8_t *dsc_list;
  170. } tBTA_HH_DEV_DESCR;
  171. /* Define the header of each buffer used in the Bluetooth stack. */
  172. typedef struct {
  173. uint16_t event;
  174. uint16_t len;
  175. uint16_t offset;
  176. uint16_t layer_specific;
  177. uint8_t data[];
  178. } BT_HDR;
  179. #define BT_HDR_SIZE (sizeof (BT_HDR))
  180. /* callback event data for BTA_HH_OPEN_EVT */
  181. typedef struct {
  182. esp_bd_addr_t bda; /* HID device bd address */
  183. tBTA_HH_STATUS status; /* operation status */
  184. uint8_t handle; /* device handle */
  185. } tBTA_HH_CONN;
  186. typedef tBTA_HH_CONN tBTA_HH_DEV_INFO;
  187. /* callback event data */
  188. typedef struct {
  189. tBTA_HH_STATUS status; /* operation status */
  190. uint8_t handle; /* device handle */
  191. } tBTA_HH_CBDATA;
  192. /* report descriptor information */
  193. typedef struct {
  194. uint16_t vendor_id; /* vendor ID */
  195. uint16_t product_id; /* product ID */
  196. uint16_t version; /* version */
  197. uint16_t ssr_max_latency;/* SSR max latency, BTA_HH_SSR_PARAM_INVALID if unknown */
  198. uint16_t ssr_min_tout; /* SSR min timeout, BTA_HH_SSR_PARAM_INVALID if unknown */
  199. uint8_t ctry_code; /*Country Code.*/
  200. tBTA_HH_DEV_DESCR descriptor;
  201. } tBTA_HH_DEV_DSCP_INFO;
  202. /* handshake data */
  203. typedef struct {
  204. tBTA_HH_STATUS status; /* handshake status */
  205. uint8_t handle; /* device handle */
  206. union {
  207. tBTA_HH_PROTO_MODE proto_mode; /* GET_PROTO_EVT :protocol mode */
  208. BT_HDR *p_rpt_data;/* GET_RPT_EVT : report data */
  209. uint8_t idle_rate; /* GET_IDLE_EVT : idle rate */
  210. } rsp_data;
  211. } tBTA_HH_HSDATA;
  212. /* union of data associated with HD callback */
  213. typedef union {
  214. tBTA_HH_DEV_INFO dev_info; /* BTA_HH_ADD_DEV_EVT, BTA_HH_RMV_DEV_EVT */
  215. tBTA_HH_CONN conn; /* BTA_HH_OPEN_EVT */
  216. tBTA_HH_CBDATA dev_status; /* BTA_HH_CLOSE_EVT,
  217. BTA_HH_SET_PROTO_EVT
  218. BTA_HH_SET_RPT_EVT
  219. BTA_HH_SET_IDLE_EVT
  220. BTA_HH_UPDATE_SCPP_EVT */
  221. tBTA_HH_STATUS status; /* BTA_HH_ENABLE_EVT */
  222. tBTA_HH_DEV_DSCP_INFO dscp_info; /* BTA_HH_GET_DSCP_EVT */
  223. tBTA_HH_HSDATA hs_data; /* GET_ transaction callback
  224. BTA_HH_GET_RPT_EVT
  225. BTA_HH_GET_PROTO_EVT
  226. BTA_HH_GET_IDLE_EVT */
  227. } tBTA_HH;
  228. /* BTA HH callback function */
  229. typedef void (tBTA_HH_CBACK) (tBTA_HH_EVT event, tBTA_HH *p_data);
  230. /*****************************************************************************
  231. ** External Function Declarations
  232. *****************************************************************************/
  233. #ifdef __cplusplus
  234. extern "C"
  235. {
  236. #endif
  237. /* This function enable HID host and registers HID-Host with lower layers.*/
  238. extern void BTA_HhEnable(uint16_t sec_mask, tBTA_HH_CBACK *p_cback);
  239. /* This function is called when the host is about power down. */
  240. extern void BTA_HhDisable(void);
  241. /* This function is called to start an inquiry and read SDP record of responding devices; connect to a device if only one active HID device is found. */
  242. extern void BTA_HhOpen (esp_bd_addr_t dev_bda, uint8_t bd_type, tBTA_HH_PROTO_MODE mode, uint16_t sec_mask);
  243. /* This function disconnects the device. */
  244. extern void BTA_HhClose(uint8_t dev_handle);
  245. /* This function set the protocol mode at specified HID handle */
  246. extern void BTA_HhSetProtoMode(uint8_t handle, tBTA_HH_PROTO_MODE t_type);
  247. /* This function get the protocol mode of a specified HID device. */
  248. extern void BTA_HhGetProtoMode(uint8_t dev_handle);
  249. /* send SET_REPORT to device. */
  250. extern void BTA_HhSetReport(uint8_t dev_handle, tBTA_HH_RPT_TYPE r_type, BT_HDR *p_data);
  251. /* Send a GET_REPORT to HID device. */
  252. extern void BTA_HhGetReport(uint8_t dev_handle, tBTA_HH_RPT_TYPE r_type, uint8_t rpt_id, uint16_t buf_size);
  253. /* send SET_IDLE to device. */
  254. extern void BTA_HhSetIdle(uint8_t dev_handle, uint16_t idle_rate);
  255. /* Send a GET_IDLE to HID device. */
  256. extern void BTA_HhGetIdle(uint8_t dev_handle);
  257. /* Send HID_CONTROL request to a HID device. */
  258. extern void BTA_HhSendCtrl(uint8_t dev_handle, tBTA_HH_TRANS_CTRL_TYPE c_type);
  259. /* Send DATA transaction to a HID device. */
  260. extern void BTA_HhSendData(uint8_t dev_handle, esp_bd_addr_t dev_bda, BT_HDR *p_buf);
  261. /* Get report descriptor of the device */
  262. extern void BTA_HhGetDscpInfo(uint8_t dev_handle);
  263. /* Add a virtually cabled device into HID-Host device list to manage and assign a device handle for future API call, host applciation call this API at start-up to initialize its virtually cabled devices. */
  264. extern void BTA_HhAddDev(esp_bd_addr_t bda, tBTA_HH_ATTR_MASK attr_mask, uint8_t sub_class, uint8_t app_id, tBTA_HH_DEV_DSCP_INFO dscp_info);
  265. /* Remove a device from the HID host devices list. */
  266. extern void BTA_HhRemoveDev(uint8_t dev_handle );
  267. enum {
  268. BTA_HH_MOD_CTRL_KEY,
  269. BTA_HH_MOD_SHFT_KEY,
  270. BTA_HH_MOD_ALT_KEY,
  271. BTA_HH_MOD_GUI_KEY,
  272. BTA_HH_MOD_MAX_KEY
  273. };
  274. /* parsed boot mode keyboard report */
  275. typedef struct {
  276. uint8_t this_char[6]; /* virtual key code */
  277. bool mod_key[BTA_HH_MOD_MAX_KEY];/* ctrl, shift, Alt, GUI */
  278. bool caps_lock; /* is caps locked */
  279. bool num_lock; /* is Num key pressed */
  280. } tBTA_HH_KEYBD_RPT;
  281. /* parsed boot mode mouse report */
  282. typedef struct {
  283. uint8_t mouse_button; /* mouse button is clicked */
  284. int8_t delta_x; /* displacement x */
  285. int8_t delta_y; /* displacement y */
  286. } tBTA_HH_MICE_RPT;
  287. enum {
  288. BTA_HH_KEYBD_RPT_ID = 1,
  289. BTA_HH_MOUSE_RPT_ID
  290. };
  291. typedef uint8_t tBTA_HH_BOOT_RPT_ID;
  292. /* parsed Boot report */
  293. typedef struct {
  294. tBTA_HH_BOOT_RPT_ID dev_type; /* type of device report */
  295. union {
  296. tBTA_HH_KEYBD_RPT keybd_rpt; /* keyboard report */
  297. tBTA_HH_MICE_RPT mice_rpt; /* mouse report */
  298. } data_rpt;
  299. } tBTA_HH_BOOT_RPT;
  300. /* This utility function parse a boot mode report. */
  301. extern void BTA_HhParseBootRpt(tBTA_HH_BOOT_RPT *p_data, uint8_t *p_report, uint16_t report_len);
  302. #ifdef __cplusplus
  303. }
  304. #endif
  305. #endif /* _ESP_BT_HH_API_H_ */