esp_gap_bt_api.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef __ESP_GAP_BT_API_H__
  14. #define __ESP_GAP_BT_API_H__
  15. #include <stdint.h>
  16. #include "esp_err.h"
  17. #include "esp_bt_defs.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /// Discoverability and Connectability mode
  22. typedef enum {
  23. ESP_BT_SCAN_MODE_NONE = 0, /*!< Neither discoverable nor connectable */
  24. ESP_BT_SCAN_MODE_CONNECTABLE, /*!< Connectable but not discoverable */
  25. ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE /*!< both discoverable and connectable */
  26. } esp_bt_scan_mode_t;
  27. /// Bluetooth Device Property type
  28. typedef enum {
  29. ESP_BT_GAP_DEV_PROP_BDNAME = 1, /*!< Bluetooth device name, value type is int8_t [] */
  30. ESP_BT_GAP_DEV_PROP_COD, /*!< Class of Device, value type is uint32_t */
  31. ESP_BT_GAP_DEV_PROP_RSSI, /*!< Received Signal strength Indication, value type is int8_t, ranging from -128 to 127 */
  32. ESP_BT_GAP_DEV_PROP_EIR, /*!< Extended Inquiry Response, value type is uint8_t [] */
  33. } esp_bt_gap_dev_prop_type_t;
  34. /// Maximum bytes of Bluetooth device name
  35. #define ESP_BT_GAP_MAX_BDNAME_LEN (248)
  36. /// Maximum size of EIR Significant part
  37. #define ESP_BT_GAP_EIR_DATA_LEN (240)
  38. /// Bluetooth Device Property Descriptor
  39. typedef struct {
  40. esp_bt_gap_dev_prop_type_t type; /*!< device property type */
  41. int len; /*!< device property value length */
  42. void *val; /*!< devlice prpoerty value */
  43. } esp_bt_gap_dev_prop_t;
  44. /// Extended Inquiry Response data type
  45. typedef enum {
  46. ESP_BT_EIR_TYPE_FLAGS = 0x01, /*!< Flag with information such as BR/EDR and LE support */
  47. ESP_BT_EIR_TYPE_INCMPL_16BITS_UUID = 0x02, /*!< Incomplete list of 16-bit service UUIDs */
  48. ESP_BT_EIR_TYPE_CMPL_16BITS_UUID = 0x03, /*!< Complete list of 16-bit service UUIDs */
  49. ESP_BT_EIR_TYPE_INCMPL_32BITS_UUID = 0x04, /*!< Incomplete list of 32-bit service UUIDs */
  50. ESP_BT_EIR_TYPE_CMPL_32BITS_UUID = 0x05, /*!< Complete list of 32-bit service UUIDs */
  51. ESP_BT_EIR_TYPE_INCMPL_128BITS_UUID = 0x06, /*!< Incomplete list of 128-bit service UUIDs */
  52. ESP_BT_EIR_TYPE_CMPL_128BITS_UUID = 0x07, /*!< Complete list of 128-bit service UUIDs */
  53. ESP_BT_EIR_TYPE_SHORT_LOCAL_NAME = 0x08, /*!< Shortened Local Name */
  54. ESP_BT_EIR_TYPE_CMPL_LOCAL_NAME = 0x09, /*!< Complete Local Name */
  55. ESP_BT_EIR_TYPE_TX_POWER_LEVEL = 0x0a, /*!< Tx power level, value is 1 octet ranging from -127 to 127, unit is dBm*/
  56. ESP_BT_EIR_TYPE_MANU_SPECIFIC = 0xff, /*!< Manufacturer specific data */
  57. } esp_bt_eir_type_t;
  58. /// Major service class field of Class of Device, mutiple bits can be set
  59. typedef enum {
  60. ESP_BT_COD_SRVC_NONE = 0, /*!< None indicates an invalid value */
  61. ESP_BT_COD_SRVC_LMTD_DISCOVER = 0x1, /*!< Limited Discoverable Mode */
  62. ESP_BT_COD_SRVC_POSITIONING = 0x8, /*!< Positioning (Location identification) */
  63. ESP_BT_COD_SRVC_NETWORKING = 0x10, /*!< Networking, e.g. LAN, Ad hoc */
  64. ESP_BT_COD_SRVC_RENDERING = 0x20, /*!< Rendering, e.g. Printing, Speakers */
  65. ESP_BT_COD_SRVC_CAPTURING = 0x40, /*!< Capturing, e.g. Scanner, Microphone */
  66. ESP_BT_COD_SRVC_OBJ_TRANSFER = 0x80, /*!< Object Transfer, e.g. v-Inbox, v-Folder */
  67. ESP_BT_COD_SRVC_AUDIO = 0x100, /*!< Audio, e.g. Speaker, Microphone, Headerset service */
  68. ESP_BT_COD_SRVC_TELEPHONY = 0x200, /*!< Telephony, e.g. Cordless telephony, Modem, Headset service */
  69. ESP_BT_COD_SRVC_INFORMATION = 0x400, /*!< Information, e.g., WEB-server, WAP-server */
  70. } esp_bt_cod_srvc_t;
  71. /// Bits of major service class field
  72. #define ESP_BT_COD_SRVC_BIT_MASK (0xffe000) /*!< Major service bit mask */
  73. #define ESP_BT_COD_SRVC_BIT_OFFSET (13) /*!< Major service bit offset */
  74. /// Major device class field of Class of Device
  75. typedef enum {
  76. ESP_BT_COD_MAJOR_DEV_MISC = 0, /*!< Miscellaneous */
  77. ESP_BT_COD_MAJOR_DEV_COMPUTER = 1, /*!< Computer */
  78. ESP_BT_COD_MAJOR_DEV_PHONE = 2, /*!< Phone(cellular, cordless, pay phone, modem */
  79. ESP_BT_COD_MAJOR_DEV_LAN_NAP = 3, /*!< LAN, Network Access Point */
  80. ESP_BT_COD_MAJOR_DEV_AV = 4, /*!< Audio/Video(headset, speaker, stereo, video display, VCR */
  81. ESP_BT_COD_MAJOR_DEV_PERIPHERAL = 5, /*!< Peripheral(mouse, joystick, keyboard) */
  82. ESP_BT_COD_MAJOR_DEV_IMAGING = 6, /*!< Imaging(printer, scanner, camera, display */
  83. ESP_BT_COD_MAJOR_DEV_WEARABLE = 7, /*!< Wearable */
  84. ESP_BT_COD_MAJOR_DEV_TOY = 8, /*!< Toy */
  85. ESP_BT_COD_MAJOR_DEV_HEALTH = 9, /*!< Health */
  86. ESP_BT_COD_MAJOR_DEV_UNCATEGORIZED = 31, /*!< Uncategorized: device not specified */
  87. } esp_bt_cod_major_dev_t;
  88. /// Bits of major device class field
  89. #define ESP_BT_COD_MAJOR_DEV_BIT_MASK (0x1f00) /*!< Major device bit mask */
  90. #define ESP_BT_COD_MAJOR_DEV_BIT_OFFSET (8) /*!< Major device bit offset */
  91. /// Bits of minor device class field
  92. #define ESP_BT_COD_MINOR_DEV_BIT_MASK (0xfc) /*!< Minor device bit mask */
  93. #define ESP_BT_COD_MINOR_DEV_BIT_OFFSET (2) /*!< Minor device bit offset */
  94. /// Bits of format type
  95. #define ESP_BT_COD_FORMAT_TYPE_BIT_MASK (0x03) /*!< Format type bit mask */
  96. #define ESP_BT_COD_FORMAT_TYPE_BIT_OFFSET (0) /*!< Format type bit offset */
  97. /// Class of device format type 1
  98. #define ESP_BT_COD_FORMAT_TYPE_1 (0x00)
  99. /** Bluetooth Device Discovery state */
  100. typedef enum {
  101. ESP_BT_GAP_DISCOVERY_STOPPED, /*!< device discovery stopped */
  102. ESP_BT_GAP_DISCOVERY_STARTED, /*!< device discovery started */
  103. } esp_bt_gap_discovery_state_t;
  104. /// BT GAP callback events
  105. typedef enum {
  106. ESP_BT_GAP_DISC_RES_EVT = 0, /*!< device discovery result event */
  107. ESP_BT_GAP_DISC_STATE_CHANGED_EVT, /*!< discovery state changed event */
  108. ESP_BT_GAP_RMT_SRVCS_EVT, /*!< get remote services event */
  109. ESP_BT_GAP_RMT_SRVC_REC_EVT, /*!< get remote service record event */
  110. } esp_bt_gap_cb_event_t;
  111. /** Inquiry Mode */
  112. typedef enum {
  113. ESP_BT_INQ_MODE_GENERAL_INQUIRY, /*!< General inquiry mode */
  114. ESP_BT_INQ_MODE_LIMITED_INQIURY, /*!< Limited inquiry mode */
  115. } esp_bt_inq_mode_t;
  116. /** Minimum and Maximum inquiry length*/
  117. #define ESP_BT_GAP_MIN_INQ_LEN (0x01) /*!< Minimum inquiry duration, unit is 1.28s */
  118. #define ESP_BT_GAP_MAX_INQ_LEN (0x30) /*!< Maximum inquiry duration, unit is 1.28s */
  119. /// A2DP state callback parameters
  120. typedef union {
  121. /**
  122. * @brief ESP_BT_GAP_DISC_RES_EVT
  123. */
  124. struct disc_res_param {
  125. esp_bd_addr_t bda; /*!< remote bluetooth device address*/
  126. int num_prop; /*!< number of properties got */
  127. esp_bt_gap_dev_prop_t *prop; /*!< properties discovered from the new device */
  128. } disc_res; /*!< discovery result paramter struct */
  129. /**
  130. * @brief ESP_BT_GAP_DISC_STATE_CHANGED_EVT
  131. */
  132. struct disc_state_changed_param {
  133. esp_bt_gap_discovery_state_t state; /*!< discovery state */
  134. } disc_st_chg; /*!< discovery state changed parameter struct */
  135. /**
  136. * @brief ESP_BT_GAP_RMT_SRVCS_EVT
  137. */
  138. struct rmt_srvcs_param {
  139. esp_bd_addr_t bda; /*!< remote bluetooth device address*/
  140. esp_bt_status_t stat; /*!< service search status */
  141. int num_uuids; /*!< number of UUID in uuid_list */
  142. esp_bt_uuid_t *uuid_list; /*!< list of service UUIDs of remote device */
  143. } rmt_srvcs; /*!< services of remote device parameter struct */
  144. /**
  145. * @brief ESP_BT_GAP_RMT_SRVC_REC_EVT
  146. */
  147. struct rmt_srvc_rec_param {
  148. esp_bd_addr_t bda; /*!< remote bluetooth device address*/
  149. esp_bt_status_t stat; /*!< service search status */
  150. } rmt_srvc_rec; /*!< specific service record from remote device parameter struct */
  151. } esp_bt_gap_cb_param_t;
  152. /**
  153. * @brief bluetooth GAP callback function type
  154. * @param event : Event type
  155. * @param param : Pointer to callback parameter
  156. */
  157. typedef void (* esp_bt_gap_cb_t)(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param);
  158. /**
  159. * @brief get major service field of COD
  160. * @param[in] cod: Class of Device
  161. * @return major service bits
  162. */
  163. inline uint32_t esp_bt_gap_get_cod_srvc(uint32_t cod)
  164. {
  165. return (cod & ESP_BT_COD_SRVC_BIT_MASK) >> ESP_BT_COD_SRVC_BIT_OFFSET;
  166. }
  167. /**
  168. * @brief get major device field of COD
  169. * @param[in] cod: Class of Device
  170. * @return major device bits
  171. */
  172. inline uint32_t esp_bt_gap_get_cod_major_dev(uint32_t cod)
  173. {
  174. return (cod & ESP_BT_COD_MAJOR_DEV_BIT_MASK) >> ESP_BT_COD_MAJOR_DEV_BIT_OFFSET;
  175. }
  176. /**
  177. * @brief get minor service field of COD
  178. * @param[in] cod: Class of Device
  179. * @return minor service bits
  180. */
  181. inline uint32_t esp_bt_gap_get_cod_minor_dev(uint32_t cod)
  182. {
  183. return (cod & ESP_BT_COD_MINOR_DEV_BIT_MASK) >> ESP_BT_COD_MINOR_DEV_BIT_OFFSET;
  184. }
  185. /**
  186. * @brief get format type of COD
  187. * @param[in] cod: Class of Device
  188. * @return format type
  189. */
  190. inline uint32_t esp_bt_gap_get_cod_format_type(uint32_t cod)
  191. {
  192. return (cod & ESP_BT_COD_FORMAT_TYPE_BIT_MASK);
  193. }
  194. /**
  195. * @brief decide the integrity of COD
  196. * @param[in] cod: Class of Device
  197. * @return
  198. * - true if cod is valid
  199. * - false otherise
  200. */
  201. inline bool esp_bt_gap_is_valid_cod(uint32_t cod)
  202. {
  203. if (esp_bt_gap_get_cod_format_type(cod) == ESP_BT_COD_FORMAT_TYPE_1 &&
  204. esp_bt_gap_get_cod_srvc(cod) != ESP_BT_COD_SRVC_NONE) {
  205. return true;
  206. }
  207. return false;
  208. }
  209. /**
  210. * @brief register callback function. This function should be called after esp_bluedroid_enable() completes successfully
  211. *
  212. * @return
  213. * - ESP_OK : Succeed
  214. * - ESP_FAIL: others
  215. */
  216. esp_err_t esp_bt_gap_register_callback(esp_bt_gap_cb_t callback);
  217. /**
  218. * @brief Set discoverability and connectability mode for legacy bluetooth. This function should
  219. * be called after esp_bluedroid_enable() completes successfully
  220. *
  221. * @param[in] mode : one of the enums of bt_scan_mode_t
  222. *
  223. * @return
  224. * - ESP_OK : Succeed
  225. * - ESP_ERR_INVALID_ARG: if argument invalid
  226. * - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
  227. * - ESP_FAIL: others
  228. */
  229. esp_err_t esp_bt_gap_set_scan_mode(esp_bt_scan_mode_t mode);
  230. /**
  231. * @brief Start device discovery. This function should be called after esp_bluedroid_enable() completes successfully.
  232. * esp_bt_gap_cb_t will is called with ESP_BT_GAP_DISC_STATE_CHANGED_EVT if discovery is started or halted.
  233. * esp_bt_gap_cb_t will is called with ESP_BT_GAP_DISC_RES_EVT if discovery result is got.
  234. *
  235. * @param[in] mode - inquiry mode
  236. * @param[in] inq_len - inquiry duration in 1.28 sec units, ranging from 0x01 to 0x30
  237. * @param[in] num_rsps - number of inquiry responses that can be received, value 0 indicates an unlimited number of responses
  238. *
  239. * @return
  240. * - ESP_OK : Succeed
  241. * - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
  242. * - ESP_ERR_INVALID_ARG: if invalid parameters are provided
  243. * - ESP_FAIL: others
  244. */
  245. esp_err_t esp_bt_gap_start_discovery(esp_bt_inq_mode_t mode, uint8_t inq_len, uint8_t num_rsps);
  246. /**
  247. * @brief Cancel device discovery. This function should be called after esp_bluedroid_enable() completes successfully
  248. * esp_bt_gap_cb_t will is called with ESP_BT_GAP_DISC_STATE_CHANGED_EVT if discovery is stopped.
  249. *
  250. * @return
  251. * - ESP_OK : Succeed
  252. * - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
  253. * - ESP_FAIL: others
  254. */
  255. esp_err_t esp_bt_gap_cancel_discovery(void);
  256. /**
  257. * @brief Start SDP to get remote services. This function should be called after esp_bluedroid_enable() completes successfully.
  258. * esp_bt_gap_cb_t will is called with ESP_BT_GAP_RMT_SRVCS_EVT after service discovery ends
  259. *
  260. * @return
  261. * - ESP_OK : Succeed
  262. * - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
  263. * - ESP_FAIL: others
  264. */
  265. esp_err_t esp_bt_gap_get_remote_services(esp_bd_addr_t remote_bda);
  266. /**
  267. * @brief Start SDP to look up the service matching uuid on the remote device. This function should be called after
  268. * esp_bluedroid_enable() completes successfully
  269. *
  270. * esp_bt_gap_cb_t will is called with ESP_BT_GAP_RMT_SRVC_REC_EVT after service discovery ends
  271. * @return
  272. * - ESP_OK : Succeed
  273. * - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
  274. * - ESP_FAIL: others
  275. */
  276. esp_err_t esp_bt_gap_get_remote_service_record(esp_bd_addr_t remote_bda, esp_bt_uuid_t *uuid);
  277. /**
  278. * @brief This function is called to get EIR data for a specific type.
  279. *
  280. * @param[in] eir - pointer of raw eir data to be resolved
  281. * @param[in] type - specific EIR data type
  282. * @param[out] length - return the length of EIR data excluding fields of length and data type
  283. *
  284. * @return pointer of starting position of eir data excluding eir data type, NULL if not found
  285. *
  286. */
  287. uint8_t *esp_bt_gap_resolve_eir_data(uint8_t *eir, esp_bt_eir_type_t type, uint8_t *length);
  288. #ifdef __cplusplus
  289. }
  290. #endif
  291. #endif /* __ESP_GAP_BT_API_H__ */