esp_gatts_api.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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_GATTS_API_H__
  14. #define __ESP_GATTS_API_H__
  15. #include "esp_bt_defs.h"
  16. #include "esp_gatt_defs.h"
  17. #include "esp_err.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /// GATT Server callback function events
  22. typedef enum {
  23. ESP_GATTS_REG_EVT = 0, /*!< When register application id, the event comes */
  24. ESP_GATTS_READ_EVT = 1, /*!< When gatt client request read operation, the event comes */
  25. ESP_GATTS_WRITE_EVT = 2, /*!< When gatt client request write operation, the event comes */
  26. ESP_GATTS_EXEC_WRITE_EVT = 3, /*!< When gatt client request execute write, the event comes */
  27. ESP_GATTS_MTU_EVT = 4, /*!< When set mtu complete, the event comes */
  28. ESP_GATTS_CONF_EVT = 5, /*!< When receive confirm, the event comes */
  29. ESP_GATTS_UNREG_EVT = 6, /*!< When unregister application id, the event comes */
  30. ESP_GATTS_CREATE_EVT = 7, /*!< When create service complete, the event comes */
  31. ESP_GATTS_ADD_INCL_SRVC_EVT = 8, /*!< When add included service complete, the event comes */
  32. ESP_GATTS_ADD_CHAR_EVT = 9, /*!< When add characteristic complete, the event comes */
  33. ESP_GATTS_ADD_CHAR_DESCR_EVT = 10, /*!< When add descriptor complete, the event comes */
  34. ESP_GATTS_DELETE_EVT = 11, /*!< When delete service complete, the event comes */
  35. ESP_GATTS_START_EVT = 12, /*!< When start service complete, the event comes */
  36. ESP_GATTS_STOP_EVT = 13, /*!< When stop service complete, the event comes */
  37. ESP_GATTS_CONNECT_EVT = 14, /*!< When gatt client connect, the event comes */
  38. ESP_GATTS_DISCONNECT_EVT = 15, /*!< When gatt client disconnect, the event comes */
  39. ESP_GATTS_OPEN_EVT = 16, /*!< When connect to peer, the event comes */
  40. ESP_GATTS_CANCEL_OPEN_EVT = 17, /*!< When disconnect from peer, the event comes */
  41. ESP_GATTS_CLOSE_EVT = 18, /*!< When gatt server close, the event comes */
  42. ESP_GATTS_LISTEN_EVT = 19, /*!< When gatt listen to be connected the event comes */
  43. ESP_GATTS_CONGEST_EVT = 20, /*!< When congest happen, the event comes */
  44. /* following is extra event */
  45. ESP_GATTS_RESPONSE_EVT = 21, /*!< When gatt send response complete, the event comes */
  46. ESP_GATTS_CREAT_ATTR_TAB_EVT = 22,
  47. ESP_GATTS_SET_ATTR_VAL_EVT = 23,
  48. } esp_gatts_cb_event_t;
  49. /**
  50. * @brief Gatt server callback parameters union
  51. */
  52. typedef union {
  53. /**
  54. * @brief ESP_GATTS_REG_EVT
  55. */
  56. struct gatts_reg_evt_param {
  57. esp_gatt_status_t status; /*!< Operation status */
  58. uint16_t app_id; /*!< Application id which input in register API */
  59. } reg; /*!< Gatt server callback param of ESP_GATTS_REG_EVT */
  60. /**
  61. * @brief ESP_GATTS_READ_EVT
  62. */
  63. struct gatts_read_evt_param {
  64. uint16_t conn_id; /*!< Connection id */
  65. uint32_t trans_id; /*!< Transfer id */
  66. esp_bd_addr_t bda; /*!< The bluetooth device address which been read */
  67. uint16_t handle; /*!< The attribute handle */
  68. uint16_t offset; /*!< Offset of the value, if the value is too long */
  69. bool is_long; /*!< The value is too long or not */
  70. bool need_rsp; /*!< The read operation need to do response */
  71. } read; /*!< Gatt server callback param of ESP_GATTS_READ_EVT */
  72. /**
  73. * @brief ESP_GATTS_WRITE_EVT
  74. */
  75. struct gatts_write_evt_param {
  76. uint16_t conn_id; /*!< Connection id */
  77. uint32_t trans_id; /*!< Transfer id */
  78. esp_bd_addr_t bda; /*!< The bluetooth device address which been written */
  79. uint16_t handle; /*!< The attribute handle */
  80. uint16_t offset; /*!< Offset of the value, if the value is too long */
  81. bool need_rsp; /*!< The write operation need to do response */
  82. bool is_prep; /*!< This write operation is prepare write */
  83. uint16_t len; /*!< The write attribute value length */
  84. uint8_t *value; /*!< The write attribute value */
  85. } write; /*!< Gatt server callback param of ESP_GATTS_WRITE_EVT */
  86. /**
  87. * @brief ESP_GATTS_EXEC_WRITE_EVT
  88. */
  89. struct gatts_exec_write_evt_param {
  90. uint16_t conn_id; /*!< Connection id */
  91. uint32_t trans_id; /*!< Transfer id */
  92. esp_bd_addr_t bda; /*!< The bluetooth device address which been written */
  93. #define ESP_GATT_PREP_WRITE_CANCEL 0x00 /*!< Prepare write flag to indicate cancel prepare write */
  94. #define ESP_GATT_PREP_WRITE_EXEC 0x01 /*!< Prepare write flag to indicate execute prepare write */
  95. uint8_t exec_write_flag; /*!< Execute write flag */
  96. } exec_write; /*!< Gatt server callback param of ESP_GATTS_EXEC_WRITE_EVT */
  97. /**
  98. * @brief ESP_GATTS_MTU_EVT
  99. */
  100. struct gatts_mtu_evt_param {
  101. uint16_t conn_id; /*!< Connection id */
  102. uint16_t mtu; /*!< MTU size */
  103. } mtu; /*!< Gatt server callback param of ESP_GATTS_MTU_EVT */
  104. /**
  105. * @brief ESP_GATTS_CONF_EVT
  106. */
  107. struct gatts_conf_evt_param {
  108. esp_gatt_status_t status; /*!< Operation status */
  109. uint16_t conn_id; /*!< Connection id */
  110. } conf; /*!< Gatt server callback param of ESP_GATTS_CONF_EVT (confirm) */
  111. /**
  112. * @brief ESP_GATTS_UNREG_EVT
  113. */
  114. /**
  115. * @brief ESP_GATTS_CREATE_EVT
  116. */
  117. struct gatts_create_evt_param {
  118. esp_gatt_status_t status; /*!< Operation status */
  119. uint16_t service_handle; /*!< Service attribute handle */
  120. esp_gatt_srvc_id_t service_id; /*!< Service id, include service uuid and other information */
  121. } create; /*!< Gatt server callback param of ESP_GATTS_CREATE_EVT */
  122. /**
  123. * @brief ESP_GATTS_ADD_INCL_SRVC_EVT
  124. */
  125. struct gatts_add_incl_srvc_evt_param {
  126. esp_gatt_status_t status; /*!< Operation status */
  127. uint16_t attr_handle; /*!< Included service attribute handle */
  128. uint16_t service_handle; /*!< Service attribute handle */
  129. } add_incl_srvc; /*!< Gatt server callback param of ESP_GATTS_ADD_INCL_SRVC_EVT */
  130. /**
  131. * @brief ESP_GATTS_ADD_CHAR_EVT
  132. */
  133. struct gatts_add_char_evt_param {
  134. esp_gatt_status_t status; /*!< Operation status */
  135. uint16_t attr_handle; /*!< Characteristic attribute handle */
  136. uint16_t service_handle; /*!< Service attribute handle */
  137. esp_bt_uuid_t char_uuid; /*!< Characteristic uuid */
  138. } add_char; /*!< Gatt server callback param of ESP_GATTS_ADD_CHAR_EVT */
  139. /**
  140. * @brief ESP_GATTS_ADD_CHAR_DESCR_EVT
  141. */
  142. struct gatts_add_char_descr_evt_param {
  143. esp_gatt_status_t status; /*!< Operation status */
  144. uint16_t attr_handle; /*!< Descriptor attribute handle */
  145. uint16_t service_handle; /*!< Service attribute handle */
  146. esp_bt_uuid_t char_uuid; /*!< Characteristic uuid */
  147. } add_char_descr; /*!< Gatt server callback param of ESP_GATTS_ADD_CHAR_DESCR_EVT */
  148. /**
  149. * @brief ESP_GATTS_DELETE_EVT
  150. */
  151. struct gatts_delete_evt_param {
  152. esp_gatt_status_t status; /*!< Operation status */
  153. uint16_t service_handle; /*!< Service attribute handle */
  154. } del; /*!< Gatt server callback param of ESP_GATTS_DELETE_EVT */
  155. /**
  156. * @brief ESP_GATTS_START_EVT
  157. */
  158. struct gatts_start_evt_param {
  159. esp_gatt_status_t status; /*!< Operation status */
  160. uint16_t service_handle; /*!< Service attribute handle */
  161. } start; /*!< Gatt server callback param of ESP_GATTS_START_EVT */
  162. /**
  163. * @brief ESP_GATTS_STOP_EVT
  164. */
  165. struct gatts_stop_evt_param {
  166. esp_gatt_status_t status; /*!< Operation status */
  167. uint16_t service_handle; /*!< Service attribute handle */
  168. } stop; /*!< Gatt server callback param of ESP_GATTS_STOP_EVT */
  169. /**
  170. * @brief ESP_GATTS_CONNECT_EVT
  171. */
  172. struct gatts_connect_evt_param {
  173. uint16_t conn_id; /*!< Connection id */
  174. esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */
  175. bool is_connected; /*!< Indicate it is connected or not */
  176. } connect; /*!< Gatt server callback param of ESP_GATTS_CONNECT_EVT */
  177. /**
  178. * @brief ESP_GATTS_DISCONNECT_EVT
  179. */
  180. struct gatts_disconnect_evt_param {
  181. uint16_t conn_id; /*!< Connection id */
  182. esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */
  183. bool is_connected; /*!< Indicate it is connected or not */
  184. } disconnect; /*!< Gatt server callback param of ESP_GATTS_DISCONNECT_EVT */
  185. /**
  186. * @brief ESP_GATTS_OPEN_EVT
  187. */
  188. /**
  189. * @brief ESP_GATTS_CANCEL_OPEN_EVT
  190. */
  191. /**
  192. * @brief ESP_GATTS_CLOSE_EVT
  193. */
  194. /**
  195. * @brief ESP_GATTS_LISTEN_EVT
  196. */
  197. /**
  198. * @brief ESP_GATTS_CONGEST_EVT
  199. */
  200. struct gatts_congest_evt_param {
  201. uint16_t conn_id; /*!< Connection id */
  202. bool congested; /*!< Congested or not */
  203. } congest; /*!< Gatt server callback param of ESP_GATTS_CONGEST_EVT */
  204. /**
  205. * @brief ESP_GATTS_RESPONSE_EVT
  206. */
  207. struct gatts_rsp_evt_param {
  208. esp_gatt_status_t status; /*!< Operation status */
  209. uint16_t handle; /*!< Attribute handle which send response */
  210. } rsp; /*!< Gatt server callback param of ESP_GATTS_RESPONSE_EVT */
  211. /**
  212. * @brief ESP_GATTS_CREAT_ATTR_TAB_EVT
  213. */
  214. struct gatts_add_attr_tab_evt_param{
  215. esp_gatt_status_t status; /*!< Operation status */
  216. esp_bt_uuid_t svc_uuid; /*!< Service uuid type */
  217. uint16_t num_handle; /*!< The number of the attribute handle to be added to the gatts database */
  218. uint16_t *handles; /*!< The number to the handles */
  219. } add_attr_tab; /*!< Gatt server callback param of ESP_GATTS_CREAT_ATTR_TAB_EVT */
  220. /**
  221. * @brief ESP_GATTS_SET_ATTR_VAL_EVT
  222. */
  223. struct gatts_set_attr_val_evt_param{
  224. uint16_t srvc_handle; /*!< The service handle */
  225. uint16_t attr_handle; /*!< The attribute handle */
  226. esp_gatt_status_t status; /*!< Operation status*/
  227. } set_attr_val; /*!< Gatt server callback param of ESP_GATTS_SET_ATTR_VAL_EVT */
  228. } esp_ble_gatts_cb_param_t;
  229. /**
  230. * @brief GATT Server callback function type
  231. * @param event : Event type
  232. * @param gatts_if : GATT server access interface, normally
  233. * different gatts_if correspond to different profile
  234. * @param param : Point to callback parameter, currently is union type
  235. */
  236. typedef void (* esp_gatts_cb_t)(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
  237. /**
  238. * @brief This function is called to register application callbacks
  239. * with BTA GATTS module.
  240. *
  241. * @return
  242. * - ESP_OK : success
  243. * - other : failed
  244. *
  245. */
  246. esp_err_t esp_ble_gatts_register_callback(esp_gatts_cb_t callback);
  247. /**
  248. * @brief This function is called to register application identifier
  249. *
  250. * @return
  251. * - ESP_OK : success
  252. * - other : failed
  253. *
  254. */
  255. esp_err_t esp_ble_gatts_app_register(uint16_t app_id);
  256. /**
  257. * @brief unregister with GATT Server.
  258. *
  259. * @param[in] gatts_if: GATT server access interface
  260. * @return
  261. * - ESP_OK : success
  262. * - other : failed
  263. *
  264. */
  265. esp_err_t esp_ble_gatts_app_unregister(esp_gatt_if_t gatts_if);
  266. /**
  267. * @brief Create a service. When service creation is done, a callback
  268. * event BTA_GATTS_CREATE_SRVC_EVT is called to report status
  269. * and service ID to the profile. The service ID obtained in
  270. * the callback function needs to be used when adding included
  271. * service and characteristics/descriptors into the service.
  272. *
  273. * @param[in] gatts_if: GATT server access interface
  274. * @param[in] service_id: service ID.
  275. * @param[in] num_handle: number of handle requested for this service.
  276. *
  277. * @return
  278. * - ESP_OK : success
  279. * - other : failed
  280. *
  281. */
  282. esp_err_t esp_ble_gatts_create_service(esp_gatt_if_t gatts_if,
  283. esp_gatt_srvc_id_t *service_id, uint16_t num_handle);
  284. /**
  285. * @brief Create a service attribute tab.
  286. * @param[in] gatts_attr_db: the pointer to the service attr tab
  287. * @param[in] gatts_if: GATT server access interface
  288. * @param[in] max_nb_attr: the number of attribute to be added to the service database.
  289. * @param[in] srvc_inst_id: the instance id of the service
  290. *
  291. * @return
  292. * - ESP_OK : success
  293. * - other : failed
  294. *
  295. */
  296. esp_err_t esp_ble_gatts_create_attr_tab(const esp_gatts_attr_db_t *gatts_attr_db,
  297. esp_gatt_if_t gatts_if,
  298. uint8_t max_nb_attr,
  299. uint8_t srvc_inst_id);
  300. /**
  301. * @brief This function is called to add an included service. After included
  302. * service is included, a callback event BTA_GATTS_ADD_INCL_SRVC_EVT
  303. * is reported the included service ID.
  304. *
  305. * @param[in] service_handle: service handle to which this included service is to
  306. * be added.
  307. * @param[in] included_service_handle: the service ID to be included.
  308. *
  309. * @return
  310. * - ESP_OK : success
  311. * - other : failed
  312. *
  313. */
  314. esp_err_t esp_ble_gatts_add_included_service(uint16_t service_handle, uint16_t included_service_handle);
  315. /**
  316. * @brief This function is called to add a characteristic into a service.
  317. *
  318. * @param[in] service_handle: service handle to which this included service is to
  319. * be added.
  320. * @param[in] char_uuid : Characteristic UUID.
  321. * @param[in] perm : Characteristic value declaration attribute permission.
  322. * @param[in] property : Characteristic Properties
  323. * @param[in] char_val : Characteristic value
  324. * @param[in] control : attribute response control byte
  325. *
  326. * @return
  327. * - ESP_OK : success
  328. * - other : failed
  329. *
  330. */
  331. esp_err_t esp_ble_gatts_add_char(uint16_t service_handle, esp_bt_uuid_t *char_uuid,
  332. esp_gatt_perm_t perm, esp_gatt_char_prop_t property, esp_attr_value_t *char_val,
  333. esp_attr_control_t *control);
  334. /**
  335. * @brief This function is called to add characteristic descriptor. When
  336. * it's done, a callback event BTA_GATTS_ADD_DESCR_EVT is called
  337. * to report the status and an ID number for this descriptor.
  338. *
  339. * @param[in] service_handle: service handle to which this characteristic descriptor is to
  340. * be added.
  341. * @param[in] perm: descriptor access permission.
  342. * @param[in] descr_uuid: descriptor UUID.
  343. * @param[in] char_descr_val : Characteristic descriptor value
  344. * @param[in] control : attribute response control byte
  345. * @return
  346. * - ESP_OK : success
  347. * - other : failed
  348. *
  349. */
  350. esp_err_t esp_ble_gatts_add_char_descr (uint16_t service_handle,
  351. esp_bt_uuid_t *descr_uuid,
  352. esp_gatt_perm_t perm, esp_attr_value_t *char_descr_val,
  353. esp_attr_control_t *control);
  354. /**
  355. * @brief This function is called to delete a service. When this is done,
  356. * a callback event BTA_GATTS_DELETE_EVT is report with the status.
  357. *
  358. * @param[in] service_handle: service_handle to be deleted.
  359. *
  360. * @return
  361. * - ESP_OK : success
  362. * - other : failed
  363. *
  364. */
  365. esp_err_t esp_ble_gatts_delete_service(uint16_t service_handle);
  366. /**
  367. * @brief This function is called to start a service.
  368. *
  369. * @param[in] service_handle: the service handle to be started.
  370. *
  371. * @return
  372. * - ESP_OK : success
  373. * - other : failed
  374. *
  375. */
  376. esp_err_t esp_ble_gatts_start_service(uint16_t service_handle);
  377. /**
  378. * @brief This function is called to stop a service.
  379. *
  380. * @param[in] service_handle - service to be topped.
  381. *
  382. * @return
  383. * - ESP_OK : success
  384. * - other : failed
  385. *
  386. */
  387. esp_err_t esp_ble_gatts_stop_service(uint16_t service_handle);
  388. /**
  389. * @brief Send indicate or notify to GATT client.
  390. * Set param need_confirm as false will send notification, otherwise indication.
  391. *
  392. * @param[in] gatts_if: GATT server access interface
  393. * @param[in] conn_id - connection id to indicate.
  394. * @param[in] attr_handle - attribute handle to indicate.
  395. * @param[in] value_len - indicate value length.
  396. * @param[in] value: value to indicate.
  397. * @param[in] need_confirm - Whether a confirmation is required.
  398. * false sends a GATT notification, true sends a GATT indication.
  399. *
  400. * @return
  401. * - ESP_OK : success
  402. * - other : failed
  403. *
  404. */
  405. esp_err_t esp_ble_gatts_send_indicate(esp_gatt_if_t gatts_if, uint16_t conn_id, uint16_t attr_handle,
  406. uint16_t value_len, uint8_t *value, bool need_confirm);
  407. /**
  408. * @brief This function is called to send a response to a request.
  409. *
  410. * @param[in] gatts_if: GATT server access interface
  411. * @param[in] conn_id - connection identifier.
  412. * @param[in] trans_id - transfer id
  413. * @param[in] status - response status
  414. * @param[in] rsp - response data.
  415. *
  416. * @return
  417. * - ESP_OK : success
  418. * - other : failed
  419. *
  420. */
  421. esp_err_t esp_ble_gatts_send_response(esp_gatt_if_t gatts_if, uint16_t conn_id, uint32_t trans_id,
  422. esp_gatt_status_t status, esp_gatt_rsp_t *rsp);
  423. /**
  424. * @brief This function is called to set the attribute value by the application
  425. *
  426. * @param[in] attr_handle: the attribute handle which to be set
  427. * @param[in] length: the value length
  428. * @param[in] value: the pointer to the attribute value
  429. *
  430. * @return
  431. * - ESP_OK : success
  432. * - other : failed
  433. *
  434. */
  435. esp_err_t esp_ble_gatts_set_attr_value(uint16_t attr_handle, uint16_t length, const uint8_t *value);
  436. /**
  437. * @brief Retrieve attribute value
  438. *
  439. * @param[in] attr_handle: Attribute handle.
  440. * @param[out] length: pointer to the attribute value length
  441. * @param[out] value: Pointer to attribute value payload, the value cannot be modified by user
  442. *
  443. * @return
  444. * - ESP_OK : success
  445. * - other : failed
  446. *
  447. */
  448. esp_err_t esp_ble_gatts_get_attr_value(uint16_t attr_handle, uint16_t *length, const uint8_t **value);
  449. /**
  450. * @brief Open a direct open connection or add a background auto connection
  451. *
  452. * @param[in] gatts_if: GATT server access interface
  453. * @param[in] remote_bda: remote device bluetooth device address.
  454. * @param[in] is_direct: direct connection or background auto connection
  455. *
  456. * @return
  457. * - ESP_OK : success
  458. * - other : failed
  459. *
  460. */
  461. esp_err_t esp_ble_gatts_open(esp_gatt_if_t gatts_if, esp_bd_addr_t remote_bda, bool is_direct);
  462. /**
  463. * @brief Close a connection a remote device.
  464. *
  465. * @param[in] gatts_if: GATT server access interface
  466. * @param[in] conn_id: connection ID to be closed.
  467. *
  468. * @return
  469. * - ESP_OK : success
  470. * - other : failed
  471. *
  472. */
  473. esp_err_t esp_ble_gatts_close(esp_gatt_if_t gatts_if, uint16_t conn_id);
  474. #ifdef __cplusplus
  475. }
  476. #endif
  477. #endif /* __ESP_GATTS_API_H__ */