esp_gatts_api.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. } connect; /*!< Gatt server callback param of ESP_GATTS_CONNECT_EVT */
  176. /**
  177. * @brief ESP_GATTS_DISCONNECT_EVT
  178. */
  179. struct gatts_disconnect_evt_param {
  180. uint16_t conn_id; /*!< Connection id */
  181. esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */
  182. esp_gatt_conn_reason_t reason; /*!< Indicate the reason of disconnection */
  183. } disconnect; /*!< Gatt server callback param of ESP_GATTS_DISCONNECT_EVT */
  184. /**
  185. * @brief ESP_GATTS_OPEN_EVT
  186. */
  187. struct gatts_open_evt_param {
  188. esp_gatt_status_t status; /*!< Operation status */
  189. } open; /*!< Gatt server callback param of ESP_GATTS_OPEN_EVT */
  190. /**
  191. * @brief ESP_GATTS_CANCEL_OPEN_EVT
  192. */
  193. struct gatts_cancel_open_evt_param {
  194. esp_gatt_status_t status; /*!< Operation status */
  195. } cancel_open; /*!< Gatt server callback param of ESP_GATTS_CANCEL_OPEN_EVT */
  196. /**
  197. * @brief ESP_GATTS_CLOSE_EVT
  198. */
  199. struct gatts_close_evt_param {
  200. esp_gatt_status_t status; /*!< Operation status */
  201. uint16_t conn_id; /*!< Connection id */
  202. } close; /*!< Gatt server callback param of ESP_GATTS_CLOSE_EVT */
  203. /**
  204. * @brief ESP_GATTS_LISTEN_EVT
  205. */
  206. /**
  207. * @brief ESP_GATTS_CONGEST_EVT
  208. */
  209. struct gatts_congest_evt_param {
  210. uint16_t conn_id; /*!< Connection id */
  211. bool congested; /*!< Congested or not */
  212. } congest; /*!< Gatt server callback param of ESP_GATTS_CONGEST_EVT */
  213. /**
  214. * @brief ESP_GATTS_RESPONSE_EVT
  215. */
  216. struct gatts_rsp_evt_param {
  217. esp_gatt_status_t status; /*!< Operation status */
  218. uint16_t handle; /*!< Attribute handle which send response */
  219. } rsp; /*!< Gatt server callback param of ESP_GATTS_RESPONSE_EVT */
  220. /**
  221. * @brief ESP_GATTS_CREAT_ATTR_TAB_EVT
  222. */
  223. struct gatts_add_attr_tab_evt_param{
  224. esp_gatt_status_t status; /*!< Operation status */
  225. esp_bt_uuid_t svc_uuid; /*!< Service uuid type */
  226. uint16_t num_handle; /*!< The number of the attribute handle to be added to the gatts database */
  227. uint16_t *handles; /*!< The number to the handles */
  228. } add_attr_tab; /*!< Gatt server callback param of ESP_GATTS_CREAT_ATTR_TAB_EVT */
  229. /**
  230. * @brief ESP_GATTS_SET_ATTR_VAL_EVT
  231. */
  232. struct gatts_set_attr_val_evt_param{
  233. uint16_t srvc_handle; /*!< The service handle */
  234. uint16_t attr_handle; /*!< The attribute handle */
  235. esp_gatt_status_t status; /*!< Operation status*/
  236. } set_attr_val; /*!< Gatt server callback param of ESP_GATTS_SET_ATTR_VAL_EVT */
  237. } esp_ble_gatts_cb_param_t;
  238. /**
  239. * @brief GATT Server callback function type
  240. * @param event : Event type
  241. * @param gatts_if : GATT server access interface, normally
  242. * different gatts_if correspond to different profile
  243. * @param param : Point to callback parameter, currently is union type
  244. */
  245. 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);
  246. /**
  247. * @brief This function is called to register application callbacks
  248. * with BTA GATTS module.
  249. *
  250. * @return
  251. * - ESP_OK : success
  252. * - other : failed
  253. *
  254. */
  255. esp_err_t esp_ble_gatts_register_callback(esp_gatts_cb_t callback);
  256. /**
  257. * @brief This function is called to register application identifier
  258. *
  259. * @return
  260. * - ESP_OK : success
  261. * - other : failed
  262. *
  263. */
  264. esp_err_t esp_ble_gatts_app_register(uint16_t app_id);
  265. /**
  266. * @brief unregister with GATT Server.
  267. *
  268. * @param[in] gatts_if: GATT server access interface
  269. * @return
  270. * - ESP_OK : success
  271. * - other : failed
  272. *
  273. */
  274. esp_err_t esp_ble_gatts_app_unregister(esp_gatt_if_t gatts_if);
  275. /**
  276. * @brief Create a service. When service creation is done, a callback
  277. * event BTA_GATTS_CREATE_SRVC_EVT is called to report status
  278. * and service ID to the profile. The service ID obtained in
  279. * the callback function needs to be used when adding included
  280. * service and characteristics/descriptors into the service.
  281. *
  282. * @param[in] gatts_if: GATT server access interface
  283. * @param[in] service_id: service ID.
  284. * @param[in] num_handle: number of handle requested for this service.
  285. *
  286. * @return
  287. * - ESP_OK : success
  288. * - other : failed
  289. *
  290. */
  291. esp_err_t esp_ble_gatts_create_service(esp_gatt_if_t gatts_if,
  292. esp_gatt_srvc_id_t *service_id, uint16_t num_handle);
  293. /**
  294. * @brief Create a service attribute tab.
  295. * @param[in] gatts_attr_db: the pointer to the service attr tab
  296. * @param[in] gatts_if: GATT server access interface
  297. * @param[in] max_nb_attr: the number of attribute to be added to the service database.
  298. * @param[in] srvc_inst_id: the instance id of the service
  299. *
  300. * @return
  301. * - ESP_OK : success
  302. * - other : failed
  303. *
  304. */
  305. esp_err_t esp_ble_gatts_create_attr_tab(const esp_gatts_attr_db_t *gatts_attr_db,
  306. esp_gatt_if_t gatts_if,
  307. uint8_t max_nb_attr,
  308. uint8_t srvc_inst_id);
  309. /**
  310. * @brief This function is called to add an included service. After included
  311. * service is included, a callback event BTA_GATTS_ADD_INCL_SRVC_EVT
  312. * is reported the included service ID.
  313. *
  314. * @param[in] service_handle: service handle to which this included service is to
  315. * be added.
  316. * @param[in] included_service_handle: the service ID to be included.
  317. *
  318. * @return
  319. * - ESP_OK : success
  320. * - other : failed
  321. *
  322. */
  323. esp_err_t esp_ble_gatts_add_included_service(uint16_t service_handle, uint16_t included_service_handle);
  324. /**
  325. * @brief This function is called to add a characteristic into a service.
  326. *
  327. * @param[in] service_handle: service handle to which this included service is to
  328. * be added.
  329. * @param[in] char_uuid : Characteristic UUID.
  330. * @param[in] perm : Characteristic value declaration attribute permission.
  331. * @param[in] property : Characteristic Properties
  332. * @param[in] char_val : Characteristic value
  333. * @param[in] control : attribute response control byte
  334. *
  335. * @return
  336. * - ESP_OK : success
  337. * - other : failed
  338. *
  339. */
  340. esp_err_t esp_ble_gatts_add_char(uint16_t service_handle, esp_bt_uuid_t *char_uuid,
  341. esp_gatt_perm_t perm, esp_gatt_char_prop_t property, esp_attr_value_t *char_val,
  342. esp_attr_control_t *control);
  343. /**
  344. * @brief This function is called to add characteristic descriptor. When
  345. * it's done, a callback event BTA_GATTS_ADD_DESCR_EVT is called
  346. * to report the status and an ID number for this descriptor.
  347. *
  348. * @param[in] service_handle: service handle to which this characteristic descriptor is to
  349. * be added.
  350. * @param[in] perm: descriptor access permission.
  351. * @param[in] descr_uuid: descriptor UUID.
  352. * @param[in] char_descr_val : Characteristic descriptor value
  353. * @param[in] control : attribute response control byte
  354. * @return
  355. * - ESP_OK : success
  356. * - other : failed
  357. *
  358. */
  359. esp_err_t esp_ble_gatts_add_char_descr (uint16_t service_handle,
  360. esp_bt_uuid_t *descr_uuid,
  361. esp_gatt_perm_t perm, esp_attr_value_t *char_descr_val,
  362. esp_attr_control_t *control);
  363. /**
  364. * @brief This function is called to delete a service. When this is done,
  365. * a callback event BTA_GATTS_DELETE_EVT is report with the status.
  366. *
  367. * @param[in] service_handle: service_handle to be deleted.
  368. *
  369. * @return
  370. * - ESP_OK : success
  371. * - other : failed
  372. *
  373. */
  374. esp_err_t esp_ble_gatts_delete_service(uint16_t service_handle);
  375. /**
  376. * @brief This function is called to start a service.
  377. *
  378. * @param[in] service_handle: the service handle to be started.
  379. *
  380. * @return
  381. * - ESP_OK : success
  382. * - other : failed
  383. *
  384. */
  385. esp_err_t esp_ble_gatts_start_service(uint16_t service_handle);
  386. /**
  387. * @brief This function is called to stop a service.
  388. *
  389. * @param[in] service_handle - service to be topped.
  390. *
  391. * @return
  392. * - ESP_OK : success
  393. * - other : failed
  394. *
  395. */
  396. esp_err_t esp_ble_gatts_stop_service(uint16_t service_handle);
  397. /**
  398. * @brief Send indicate or notify to GATT client.
  399. * Set param need_confirm as false will send notification, otherwise indication.
  400. *
  401. * @param[in] gatts_if: GATT server access interface
  402. * @param[in] conn_id - connection id to indicate.
  403. * @param[in] attr_handle - attribute handle to indicate.
  404. * @param[in] value_len - indicate value length.
  405. * @param[in] value: value to indicate.
  406. * @param[in] need_confirm - Whether a confirmation is required.
  407. * false sends a GATT notification, true sends a GATT indication.
  408. *
  409. * @return
  410. * - ESP_OK : success
  411. * - other : failed
  412. *
  413. */
  414. esp_err_t esp_ble_gatts_send_indicate(esp_gatt_if_t gatts_if, uint16_t conn_id, uint16_t attr_handle,
  415. uint16_t value_len, uint8_t *value, bool need_confirm);
  416. /**
  417. * @brief This function is called to send a response to a request.
  418. *
  419. * @param[in] gatts_if: GATT server access interface
  420. * @param[in] conn_id - connection identifier.
  421. * @param[in] trans_id - transfer id
  422. * @param[in] status - response status
  423. * @param[in] rsp - response data.
  424. *
  425. * @return
  426. * - ESP_OK : success
  427. * - other : failed
  428. *
  429. */
  430. esp_err_t esp_ble_gatts_send_response(esp_gatt_if_t gatts_if, uint16_t conn_id, uint32_t trans_id,
  431. esp_gatt_status_t status, esp_gatt_rsp_t *rsp);
  432. /**
  433. * @brief This function is called to set the attribute value by the application
  434. *
  435. * @param[in] attr_handle: the attribute handle which to be set
  436. * @param[in] length: the value length
  437. * @param[in] value: the pointer to the attribute value
  438. *
  439. * @return
  440. * - ESP_OK : success
  441. * - other : failed
  442. *
  443. */
  444. esp_err_t esp_ble_gatts_set_attr_value(uint16_t attr_handle, uint16_t length, const uint8_t *value);
  445. /**
  446. * @brief Retrieve attribute value
  447. *
  448. * @param[in] attr_handle: Attribute handle.
  449. * @param[out] length: pointer to the attribute value length
  450. * @param[out] value: Pointer to attribute value payload, the value cannot be modified by user
  451. *
  452. * @return
  453. * - ESP_GATT_OK : success
  454. * - other : failed
  455. *
  456. */
  457. esp_gatt_status_t esp_ble_gatts_get_attr_value(uint16_t attr_handle, uint16_t *length, const uint8_t **value);
  458. /**
  459. * @brief Open a direct open connection or add a background auto connection
  460. *
  461. * @param[in] gatts_if: GATT server access interface
  462. * @param[in] remote_bda: remote device bluetooth device address.
  463. * @param[in] is_direct: direct connection or background auto connection
  464. *
  465. * @return
  466. * - ESP_OK : success
  467. * - other : failed
  468. *
  469. */
  470. esp_err_t esp_ble_gatts_open(esp_gatt_if_t gatts_if, esp_bd_addr_t remote_bda, bool is_direct);
  471. /**
  472. * @brief Close a connection a remote device.
  473. *
  474. * @param[in] gatts_if: GATT server access interface
  475. * @param[in] conn_id: connection ID to be closed.
  476. *
  477. * @return
  478. * - ESP_OK : success
  479. * - other : failed
  480. *
  481. */
  482. esp_err_t esp_ble_gatts_close(esp_gatt_if_t gatts_if, uint16_t conn_id);
  483. #ifdef __cplusplus
  484. }
  485. #endif
  486. #endif /* __ESP_GATTS_API_H__ */