esp_gatts_api.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #ifndef __ESP_GATTS_API_H__
  2. #define __ESP_GATTS_API_H__
  3. #include "bt_types.h"
  4. #include "esp_bt_defs.h"
  5. #include "esp_gatt_defs.h"
  6. #include "bta_gatt_api.h"
  7. #include "esp_err.h"
  8. /* GATT Server Data Structure */
  9. /* Server callback function events */
  10. #define ESP_GATTS_REG_EVT 0
  11. #define ESP_GATTS_READ_EVT 1
  12. #define ESP_GATTS_WRITE_EVT 2
  13. #define ESP_GATTS_EXEC_WRITE_EVT 3
  14. #define ESP_GATTS_MTU_EVT 4
  15. #define ESP_GATTS_CONF_EVT 5
  16. #define ESP_GATTS_UNREG_EVT 6
  17. #define ESP_GATTS_CREATE_EVT 7
  18. #define ESP_GATTS_ADD_INCL_SRVC_EVT 8
  19. #define ESP_GATTS_ADD_CHAR_EVT 9
  20. #define ESP_GATTS_ADD_CHAR_DESCR_EVT 10
  21. #define ESP_GATTS_DELELTE_EVT 11
  22. #define ESP_GATTS_START_EVT 12
  23. #define ESP_GATTS_STOP_EVT 13
  24. #define ESP_GATTS_CONNECT_EVT 14
  25. #define ESP_GATTS_DISCONNECT_EVT 15
  26. #define ESP_GATTS_OPEN_EVT 16
  27. #define ESP_GATTS_CANCEL_OPEN_EVT 17
  28. #define ESP_GATTS_CLOSE_EVT 18
  29. #define ESP_GATTS_LISTEN_EVT 19
  30. #define ESP_GATTS_CONGEST_EVT 20
  31. /* following is extra event */
  32. #define ESP_GATTS_RESPONSE_EVT 21
  33. /* esp_ble_gatts_cb_param_t */
  34. typedef union {
  35. //ESP_GATTS_REG_EVT
  36. struct gatts_reg_evt_param {
  37. int status;
  38. uint16_t gatt_if;
  39. uint16_t app_id;
  40. } reg;
  41. // param for ESP_GATTS_READ_EVT
  42. struct gatts_read_evt_param {
  43. uint16_t conn_id;
  44. uint32_t trans_id;
  45. esp_bd_addr_t bda;
  46. uint16_t handle;
  47. uint16_t offset;
  48. bool is_long;
  49. } read;
  50. // param for ESP_GATTS_WRITE_EVT
  51. struct gatts_write_evt_param {
  52. uint16_t conn_id;
  53. uint32_t trans_id;
  54. esp_bd_addr_t bda;
  55. uint16_t handle;
  56. uint16_t offset;
  57. bool need_rsp;
  58. bool is_prep;
  59. uint16_t len;
  60. uint8_t value[ESP_GATT_MAX_ATTR_LEN];
  61. } write;
  62. // param for ESP_GATTS_EXEC_WRITE_EVT
  63. struct gatts_exec_write_evt_param {
  64. uint16_t conn_id;
  65. uint32_t trans_id;
  66. esp_bd_addr_t bda;
  67. #define ESP_GATT_PREP_WRITE_CANCEL 0x00
  68. #define ESP_GATT_PREP_WRITE_EXEC 0x01
  69. uint8_t exec_write_flag;
  70. } exec_write;
  71. // param for ESP_GATTS_MTU_EVT
  72. struct gatts_mtu_evt_param {
  73. uint16_t conn_id;
  74. uint16_t mtu;
  75. } mtu;
  76. // param for ESP_GATTS_CONF_EVT
  77. struct gatts_conf_evt_param {
  78. uint16_t conn_id;
  79. int status;
  80. } conf;
  81. // param for ESP_GATTS_DEREG_EVT, NONE
  82. // param for ESP_GATTS_CREATE_EVT
  83. struct gatts_create_evt_param {
  84. int status;
  85. uint16_t gatt_if;
  86. uint16_t service_handle; //handle
  87. esp_gatt_srvc_id_t service_id; //id
  88. } create;
  89. // param for ESP_GATTS_ADD_INCL_SRVC_EVT
  90. struct gatts_add_incl_srvc_evt_param {
  91. int status;
  92. uint16_t gatt_if;
  93. uint16_t attr_handle; //handle
  94. uint16_t service_handle; //handle
  95. } add_incl_srvc;
  96. // param for ESP_GATTS_ADD_CHAR_EVT
  97. struct gatts_add_char_evt_param {
  98. int status;
  99. uint16_t gatt_if;
  100. uint16_t attr_handle; //handle
  101. uint16_t service_handle; //handle
  102. esp_bt_uuid_t char_uuid;
  103. } add_char;
  104. // param for ESP_GATTS_ADD_CHAR_DESCR_EVT
  105. struct gatts_add_char_descr_evt_param {
  106. int status;
  107. uint16_t gatt_if;
  108. uint16_t attr_handle; //handle
  109. uint16_t service_handle; //handle
  110. esp_bt_uuid_t char_uuid;
  111. } add_char_descr;
  112. // param for ESP_GATTS_DELELTE_EVT
  113. struct gatts_delete_evt_param {
  114. int status;
  115. uint16_t gatt_if;
  116. uint16_t service_handle; //handle
  117. } del;
  118. // param for ESP_GATTS_START_EVT
  119. struct gatts_start_evt_param {
  120. int status;
  121. uint16_t gatt_if;
  122. uint16_t service_handle; //handle
  123. } start;
  124. // param for ESP_GATTS_STOP_EVT
  125. struct gatts_stop_evt_param {
  126. int status;
  127. uint16_t gatt_if;
  128. uint16_t service_handle; //handle
  129. } stop;
  130. // param for ESP_GATTS_CONNECT_EVT
  131. struct gatts_connect_evt_param {
  132. uint16_t conn_id;
  133. uint16_t gatt_if;
  134. esp_bd_addr_t remote_bda;
  135. bool is_connected;
  136. } connect;
  137. // param for ESP_GATTS_DISCONNECT_EVT
  138. struct gatts_disconnect_evt_param {
  139. uint16_t conn_id;
  140. uint16_t gatt_if;
  141. esp_bd_addr_t remote_bda;
  142. bool is_connected;
  143. } disconnect;
  144. // param for ESP_GATTS_OPEN_EVT none
  145. // param for ESP_GATTS_CANCEL_OPEN_EVT none
  146. // param for ESP_GATTS_CLOSE_EVT none
  147. // param for ESP_GATTS_LISTEN_EVT none
  148. // param for ESP_GATTS_CONGEST_EVT
  149. struct gatts_congest_evt_param {
  150. uint16_t conn_id;
  151. bool congested;
  152. } congest;
  153. // param for ESP_GATTS_RESPONSE_EVT
  154. struct gatts_rsp_evt_param {
  155. int status; //response status, 0 is success
  156. uint16_t handle; //attribute handle which send response
  157. } rsp;
  158. } esp_ble_gatts_cb_param_t;
  159. /*******************************************************************************
  160. **
  161. ** @function esp_ble_gatts_register_callback
  162. **
  163. ** @brief This function is called to register application callbacks
  164. ** with BTA GATTS module.
  165. **
  166. **
  167. ** @return ESP_OK - success, other - failed
  168. **
  169. *******************************************************************************/
  170. esp_err_t esp_ble_gatts_register_callback(esp_profile_cb_t callback);
  171. /*******************************************************************************
  172. **
  173. ** @function esp_ble_gatts_app_register
  174. **
  175. ** @brief This function is called to register application identity
  176. **
  177. **
  178. ** @return ESP_OK - success, other - failed
  179. **
  180. *******************************************************************************/
  181. esp_err_t esp_ble_gatts_app_register(uint16_t app_id);
  182. /*******************************************************************************
  183. **
  184. ** @function esp_ble_gatts_app_unregister
  185. **
  186. ** @brief un-register with GATT Server.
  187. **
  188. ** @param[in] gatt_if: gatt interface id.
  189. **
  190. ** @return ESP_OK - success, other - failed
  191. **
  192. *******************************************************************************/
  193. esp_err_t esp_ble_gatts_app_unregister(esp_gatt_if_t gatt_if);
  194. /*******************************************************************************
  195. **
  196. ** @function esp_ble_gatts_create_service
  197. **
  198. ** @brief Create a service. When service creation is done, a callback
  199. ** event BTA_GATTS_CREATE_SRVC_EVT is called to report status
  200. ** and service ID to the profile. The service ID obtained in
  201. ** the callback function needs to be used when adding included
  202. ** service and characteristics/descriptors into the service.
  203. **
  204. ** @param[in] gatt_if: gatt interface ID
  205. ** @param[in] service_id: service ID.
  206. ** @param[in] num_handle: numble of handle requessted for this service.
  207. **
  208. ** @return ESP_OK - success, other - failed
  209. **
  210. *******************************************************************************/
  211. esp_err_t esp_ble_gatts_create_service(esp_gatt_if_t gatt_if,
  212. esp_gatt_srvc_id_t *service_id, uint16_t num_handle);
  213. /*******************************************************************************
  214. **
  215. ** @function esp_ble_gatts_add_include_service
  216. **
  217. ** @brief This function is called to add an included service. After included
  218. ** service is included, a callback event BTA_GATTS_ADD_INCL_SRVC_EVT
  219. ** is reported the included service ID.
  220. **
  221. ** @param[in] service_handle: service handle to which this included service is to
  222. ** be added.
  223. ** @param[in] included_service_handle: the service ID to be included.
  224. **
  225. ** @return ESP_OK - success, other - failed
  226. **
  227. *******************************************************************************/
  228. esp_err_t esp_ble_gatts_add_include_service(uint16_t service_handle, uint16_t included_service_handle);
  229. /*******************************************************************************
  230. **
  231. ** @function esp_ble_gatts_add_char
  232. **
  233. ** @brief This function is called to add a characteristic into a service.
  234. **
  235. ** @param[in] service_handle: service handle to which this included service is to
  236. ** be added.
  237. ** @param[in] char_uuid : Characteristic UUID.
  238. ** @param[in] perm : Characteristic value declaration attribute permission.
  239. ** @param[in] property : Characteristic Properties
  240. **
  241. ** @return ESP_OK - success, other - failed
  242. **
  243. *******************************************************************************/
  244. esp_err_t esp_ble_gatts_add_char(uint16_t service_handle, esp_bt_uuid_t *char_uuid,
  245. esp_gatt_perm_t perm, esp_gatt_char_prop_t property);
  246. /*******************************************************************************
  247. **
  248. ** @function esp_ble_gatts_add_char_descr
  249. **
  250. ** @brief This function is called to add characteristic descriptor. When
  251. ** it's done, a callback event BTA_GATTS_ADD_DESCR_EVT is called
  252. ** to report the status and an ID number for this descriptor.
  253. **
  254. ** @param[in] service_handle: service handle to which this charatceristic descriptor is to
  255. ** be added.
  256. ** @param[in] perm: descriptor access permission.
  257. ** @param[in] descr_uuid: descriptor UUID.
  258. **
  259. ** @return ESP_OK - success, other - failed
  260. **
  261. *******************************************************************************/
  262. esp_err_t esp_ble_gatts_add_char_descr (uint16_t service_handle,
  263. esp_bt_uuid_t *descr_uuid,
  264. esp_gatt_perm_t perm);
  265. /*******************************************************************************
  266. **
  267. ** @function esp_ble_gatts_delete_service
  268. **
  269. ** @brief This function is called to delete a service. When this is done,
  270. ** a callback event BTA_GATTS_DELETE_EVT is report with the status.
  271. **
  272. ** @param[in] service_handled: service_handle to be deleted.
  273. **
  274. ** @return ESP_OK - success, other - failed
  275. **
  276. *******************************************************************************/
  277. esp_err_t esp_ble_gatts_delete_service(uint16_t service_handle);
  278. /*******************************************************************************
  279. **
  280. ** @function esp_ble_gatts_start_service
  281. **
  282. ** @brief This function is called to start a service.
  283. **
  284. ** @param[in] service_handle: the service handle to be started.
  285. ** @param[in] sup_transport: supported trasnport.
  286. **
  287. ** @return ESP_OK - success, other - failed
  288. **
  289. *******************************************************************************/
  290. esp_err_t esp_ble_gatts_start_service(uint16_t service_handle);
  291. /*******************************************************************************
  292. **
  293. ** @function esp_ble_gatts_stop_service
  294. **
  295. ** @brief This function is called to stop a service.
  296. **
  297. ** @param[in] service_handle - service to be topped.
  298. **
  299. ** @return ESP_OK - success, other - failed
  300. **
  301. *******************************************************************************/
  302. esp_err_t esp_ble_gatts_stop_service(uint16_t service_handle);
  303. /*******************************************************************************
  304. **
  305. ** @function esp_ble_gatts_send_indicate
  306. **
  307. ** @brief This function is called to read a characteristics descriptor.
  308. **
  309. ** @param[in] conn_id - connection id to indicate.
  310. ** @param[in] attribute_handle - attribute handle to indicate.
  311. ** @param[in] value_len - indicate value length.
  312. ** @param[in] value: value to indicate.
  313. ** @param[in] need_confirm - if this indication expects a confirmation or not.
  314. **
  315. ** @return ESP_OK - success, other - failed
  316. **
  317. *******************************************************************************/
  318. esp_err_t esp_ble_gatts_send_indicate(uint16_t conn_id, uint16_t attr_handle,
  319. uint16_t value_len, uint8_t *value, bool need_confirm);
  320. /*******************************************************************************
  321. **
  322. ** @function esp_ble_gatts_send_rsp
  323. **
  324. ** @brief This function is called to send a response to a request.
  325. **
  326. ** @param[in] conn_id - connection identifier.
  327. ** @param[in] trans_id - transfe id
  328. ** @param[in] status - response status
  329. ** @param[in] rsp - response data.
  330. **
  331. ** @return ESP_OK - success, other - failed
  332. **
  333. *******************************************************************************/
  334. esp_err_t esp_ble_gatts_send_response(uint16_t conn_id, uint32_t trans_id,
  335. esp_gatt_status_t status, esp_gatt_rsp_t *rsp);
  336. /*******************************************************************************
  337. **
  338. ** @function esp_ble_gatts_open
  339. **
  340. ** @brief Open a direct open connection or add a background auto connection
  341. ** bd address
  342. **
  343. ** @param[in] gatt_if: application ID.
  344. ** @param[in] remote_bda: remote device BD address.
  345. ** @param[in] is_direct: direct connection or background auto connection
  346. **
  347. ** @return ESP_OK - success, other - failed
  348. **
  349. *******************************************************************************/
  350. esp_err_t esp_ble_gatts_open(esp_gatt_if_t gatt_if, esp_bd_addr_t remote_bda, bool is_direct);
  351. /*******************************************************************************
  352. **
  353. ** @function esp_ble_gatts_close
  354. **
  355. ** @brief Close a connection a remote device.
  356. **
  357. ** @param[in] conn_id: connectino ID to be closed.
  358. **
  359. ** @return ESP_OK - success, other - failed
  360. **
  361. *******************************************************************************/
  362. esp_err_t esp_ble_gatts_close(uint16_t conn_id);
  363. #endif /* __ESP_GATTS_API_H__ */