wm_ble_gatt.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /**
  2. * @file wm_ble_gatt.h
  3. *
  4. * @brief Bluetooth API
  5. *
  6. * @author WinnerMicro
  7. *
  8. * Copyright (c) 2020 Winner Microelectronics Co., Ltd.
  9. */
  10. #ifndef WM_BLE_GATT_H
  11. #define WM_BLE_GATT_H
  12. #include "wm_bt_def.h"
  13. /**
  14. * @defgroup BT_APIs Bluetooth APIs
  15. * @brief Bluetooth related APIs
  16. */
  17. /**
  18. * @addtogroup BT_APIs
  19. * @{
  20. */
  21. /**
  22. * @defgroup BLE_GATT_Client_APIs BLE GATT Client APIs
  23. * @brief BLE GATT Client APIs
  24. */
  25. /**
  26. * @addtogroup BLE_GATT_Client_APIs
  27. * @{
  28. */
  29. /**
  30. * @brief initialize the btif_gatt_client callback function
  31. *
  32. * @param[in] *p_callback pointer on callback function
  33. *
  34. * @retval @ref tls_bt_status_t
  35. *
  36. * @note None
  37. */
  38. tls_bt_status_t tls_ble_client_app_init(tls_ble_callback_t callback);
  39. /**
  40. * @brief free the tls_ble_callback_t pointer
  41. *
  42. * @param None
  43. *
  44. * @retval @ref tls_bt_status_t
  45. *
  46. * @note None
  47. */
  48. tls_bt_status_t tls_ble_client_app_deinit(void);
  49. /**
  50. * @brief this function is called to register client application
  51. *
  52. * @param[in] *uuid pointer on uuid
  53. *
  54. * @retval @ref tls_bt_status_t
  55. *
  56. * @note None
  57. */
  58. tls_bt_status_t tls_ble_client_app_register(tls_bt_uuid_t *uuid);
  59. /**
  60. * @brief this function is called to unregister client application
  61. *
  62. * @param[in] client_if gatt client access interface
  63. *
  64. * @retval @ref tls_bt_status_t
  65. *
  66. * @note None
  67. */
  68. tls_bt_status_t tls_ble_client_app_unregister(uint8_t client_if);
  69. /**
  70. * @brief this function is called to open an BLE connection to a remote
  71. * device or add a background auto connection
  72. *
  73. * @param[in] client_if gatt client access interface
  74. * @param[in] *bd_addr remote device bluetooth device address
  75. * @param[in] is_direct direct connection or background auto connection
  76. * @param[in] transport specific BLE/BR-EDR/mixed
  77. *
  78. * @retval @ref tls_bt_status_t
  79. *
  80. * @note None
  81. */
  82. tls_bt_status_t tls_ble_client_connect(uint8_t client_if, const tls_bt_addr_t *bd_addr, uint8_t is_direct, int transport);
  83. /**
  84. * @brief this function is called to disconnect with gatt server connection
  85. *
  86. * @param[in] client_if gatt client access interface
  87. * @param[in] *bd_addr remote device bluetooth device address
  88. * @param[in] conn_id connection ID to be closed
  89. *
  90. * @retval @ref tls_bt_status_t
  91. *
  92. * @note None
  93. */
  94. tls_bt_status_t tls_ble_client_disconnect(uint8_t client_if, const tls_bt_addr_t *bd_addr, int conn_id);
  95. /**
  96. * @brief start or stop advertisements to listen for incoming connections
  97. *
  98. * @param[in] client_if gatt client access interface
  99. * @param[in] start start: 1; stop 0
  100. *
  101. * @retval @ref tls_bt_status_t
  102. *
  103. * @note None
  104. */
  105. tls_bt_status_t tls_ble_client_listen(uint8_t client_if, uint8_t start);
  106. /**
  107. * @brief clear the attribute cache for a given device
  108. *
  109. * @param[in] client_if gatt client access interface
  110. * @param[in] *bd_addr remote device address
  111. *
  112. * @retval @ref tls_bt_status_t
  113. *
  114. * @note None
  115. */
  116. tls_bt_status_t tls_ble_client_refresh(uint8_t client_if, const tls_bt_addr_t *bd_addr);
  117. /**
  118. * @brief enumerate all GATT services on a connected device
  119. *
  120. * @param[in] conn_id connection indicator return value when connected
  121. * @param[in] *filter_uuid filter this uuid
  122. *
  123. * @retval @ref tls_bt_status_t
  124. *
  125. * @note Optionally, the results can be filtered for a given UUID
  126. */
  127. tls_bt_status_t tls_ble_client_search_service(uint16_t conn_id, tls_bt_uuid_t *filter_uuid);
  128. /**
  129. * @brief write a remote characteristic
  130. *
  131. * @param[in] conn_id connection indicator return value when connected
  132. * @param[in] handle the character attribute handle
  133. * @param[in] write_type the type of attribute write operation
  134. * @param[in] len length of the value to be written
  135. * @param[in] auth_req authentication request
  136. * @param[in] *p_value the value to be written
  137. *
  138. * @retval @ref tls_bt_status_t
  139. *
  140. * @note None
  141. */
  142. tls_bt_status_t tls_ble_client_write_characteristic(uint16_t conn_id, uint16_t handle, int write_type, int len, int auth_req, char *p_value);
  143. /**
  144. * @brief read a characteristic on a remote device
  145. *
  146. * @param[in] conn_id connection indicator return value when connected
  147. * @param[in] handle the character attribute handle
  148. * @param[in] auth_req authentication request
  149. *
  150. * @retval @ref tls_bt_status_t
  151. *
  152. * @note None
  153. */
  154. tls_bt_status_t tls_ble_client_read_characteristic(uint16_t conn_id, uint16_t handle, int auth_req);
  155. /**
  156. * @brief read the descriptor for a given characteristic
  157. *
  158. * @param[in] conn_id connection indicator return value when connected
  159. * @param[in] handle the character attribute handle
  160. * @param[in] auth_req authentication request
  161. *
  162. * @retval @ref tls_bt_status_t
  163. *
  164. * @note None
  165. */
  166. tls_bt_status_t tls_ble_client_read_descriptor(uint16_t conn_id, uint16_t handle, int auth_req);
  167. /**
  168. * @brief write a remote descriptor for a given characteristic
  169. *
  170. * @param[in] conn_id connection indicator return value when connected
  171. * @param[in] handle the character attribute handle
  172. * @param[in] write_type the type of attribute write operation
  173. * @param[in] len length of the value to be written
  174. * @param[in] auth_req authentication request
  175. * @param[in] *p_value the value to be written
  176. *
  177. * @retval @ref tls_bt_status_t
  178. *
  179. * @note None
  180. */
  181. tls_bt_status_t tls_ble_client_write_descriptor(uint16_t conn_id, uint16_t handle, int write_type, int len, int auth_req, char *p_value);
  182. /**
  183. * @brief execute a prepared write operation
  184. *
  185. * @param[in] conn_id connection indicator return value when connected
  186. * @param[in] execute execute or cancel
  187. *
  188. * @retval @ref tls_bt_status_t
  189. *
  190. * @note None
  191. */
  192. tls_bt_status_t tls_ble_client_execute_write(uint16_t conn_id, int execute);
  193. /**
  194. * @brief Register to receive notifications or indications for a given
  195. * characteristic
  196. *
  197. * @param[in] client_if gatt client access interface
  198. * @param[in] *bd_addr the target server address
  199. * @param[in] handle the attribute handle of characteristic
  200. * @param[in] conn_id the connection id
  201. *
  202. * @retval @ref tls_bt_status_t
  203. *
  204. * @note None
  205. */
  206. tls_bt_status_t tls_ble_client_register_for_notification(int client_if, const tls_bt_addr_t *bd_addr, uint16_t handle, uint16_t conn_id);
  207. /**
  208. * @brief deregister a previous request for notifications/indications
  209. *
  210. * @param[in] client_if gatt client access interface
  211. * @param[in] *bd_addr the target server address
  212. * @param[in] handle the attribute handle of characteristic
  213. *
  214. * @retval @ref tls_bt_status_t
  215. *
  216. * @note None
  217. */
  218. tls_bt_status_t tls_ble_client_deregister_for_notification(int client_if, const tls_bt_addr_t *bd_addr, uint16_t handle,uint16_t conn_id);
  219. /**
  220. * @brief configure the MTU for a given connection
  221. *
  222. * @param[in] conn_id connection indicator return value when connected
  223. * @param[in] mtu the max transmit unit of this connection
  224. *
  225. * @retval @ref tls_bt_status_t
  226. *
  227. * @note None
  228. */
  229. tls_bt_status_t tls_ble_client_configure_mtu(uint16_t conn_id, uint16_t mtu);
  230. /**
  231. * @brief get gatt db content
  232. *
  233. * @param[in] conn_id connection indicator return value when connected
  234. *
  235. * @retval @ref tls_bt_status_t
  236. *
  237. * @note None
  238. */
  239. tls_bt_status_t tls_ble_client_get_gatt_db(uint16_t conn_id);
  240. /**
  241. * @}
  242. */
  243. /**
  244. * @defgroup BLE_GATT_Server_APIs BLE GATT Server APIs
  245. * @brief BLE GATT Server APIs
  246. */
  247. /**
  248. * @addtogroup BLE_GATT_Server_APIs
  249. * @{
  250. */
  251. /**
  252. * @brief initialize the btif_gatt_server callback function
  253. *
  254. * @param[in] *p_callback pointer on callback function
  255. *
  256. * @retval @ref tls_bt_status_t
  257. *
  258. * @note None
  259. */
  260. tls_bt_status_t tls_ble_server_app_init(tls_ble_callback_t callback);
  261. /*******************************************************************************
  262. **
  263. ** Function tls_ble_server_app_deinit
  264. **
  265. ** Description free the tls_ble_callback_t pointer
  266. **
  267. ** Parameters None
  268. **
  269. ** Returns TLS_BT_STATUS_SUCCESS
  270. ** TLS_BT_STATUS_DONE
  271. **
  272. *******************************************************************************/
  273. tls_bt_status_t tls_ble_server_app_deinit();
  274. /**
  275. * @brief this function is called to register server application
  276. *
  277. * @param[in] *uuid pointer on uuid
  278. *
  279. * @retval @ref tls_bt_status_t
  280. *
  281. * @note None
  282. */
  283. tls_bt_status_t tls_ble_server_app_register(tls_bt_uuid_t *uuid);
  284. /**
  285. * @brief this function is called to unregister server application
  286. *
  287. * @param[in] server_if assigned after app registering
  288. *
  289. * @retval @ref tls_bt_status_t
  290. *
  291. * @note None
  292. */
  293. tls_bt_status_t tls_ble_server_app_unregister(uint8_t server_if);
  294. /**
  295. * @brief create a new service
  296. *
  297. * @param[in] server_if the gatt server access interface created by app register
  298. * @param[in] inst_id instance identifier of this service
  299. * @param[in] primay is primary or not service
  300. * @param[in] *uuid the id property of this service
  301. * @param[in] num_handles number of handle requested for this service
  302. *
  303. * @retval @ref tls_bt_status_t
  304. *
  305. * @note None
  306. */
  307. tls_bt_status_t tls_ble_server_add_service(uint8_t server_if, int inst_id, int primay, tls_bt_uuid_t *uuid, int num_handles);
  308. /**
  309. * @brief add a characteristic to a service
  310. *
  311. * @param[in] server_if the gatt server access interface created by app register
  312. * @param[in] service_handle the handle of this service assigned when creating a service
  313. * @param[in] *uuid the id property of this characteristic
  314. * @param[in] properties access properties
  315. * @param[in] permission access permission
  316. *
  317. * @retval @ref tls_bt_status_t
  318. *
  319. * @note None
  320. */
  321. tls_bt_status_t tls_ble_server_add_characteristic(uint8_t server_if, uint16_t service_handle, tls_bt_uuid_t *uuid, int properties, int permission);
  322. /**
  323. * @brief add a descriptor to a given service
  324. *
  325. * @param[in] server_if the gatt server access interface created by app register
  326. * @param[in] service_handle the handle of this service assigned when creating a service
  327. * @param[in] *uuid the id property of this characteristic
  328. * @param[in] permission access permission
  329. *
  330. * @retval @ref tls_bt_status_t
  331. *
  332. * @note None
  333. */
  334. tls_bt_status_t tls_ble_server_add_descriptor(uint8_t server_if, uint16_t service_handle, tls_bt_uuid_t *uuid, int permissions);
  335. /**
  336. * @brief starts a local service
  337. *
  338. * @param[in] server_if the gatt server access interface created by app register
  339. * @param[in] service_handle the handle of this service assigned when creating a service
  340. * @param[in] transport tranport type, BLE/BR-EDR/MIXED
  341. *
  342. * @retval @ref tls_bt_status_t
  343. *
  344. * @note None
  345. */
  346. tls_bt_status_t tls_ble_server_start_service(uint8_t server_if, uint16_t service_handle, int transport);
  347. /**
  348. * @brief stop a local service
  349. *
  350. * @param[in] server_if the gatt server access interface created by app register
  351. * @param[in] service_handle the handle of this service assigned when creating a service
  352. *
  353. * @retval @ref tls_bt_status_t
  354. *
  355. * @note None
  356. */
  357. tls_bt_status_t tls_ble_server_stop_service(uint8_t server_if, uint16_t service_handle);
  358. /**
  359. * @brief delete a local service
  360. *
  361. * @param[in] server_if the gatt server access interface created by app register
  362. * @param[in] service_handle the handle of this service assigned when creating a service
  363. *
  364. * @retval @ref tls_bt_status_t
  365. *
  366. * @note None
  367. */
  368. tls_bt_status_t tls_ble_server_delete_service(uint8_t server_if, uint16_t service_handle);
  369. /**
  370. * @brief create a connection to a remote peripheral
  371. *
  372. * @param[in] server_if the gatt server access interface created by app register
  373. * @param[in] *bd_addr the remote device address
  374. * @param[in] is_direct true direct connection; false: background auto connection
  375. * @param[in] transport tranport type, BLE/BR-EDR/MIXED
  376. *
  377. * @retval @ref tls_bt_status_t
  378. *
  379. * @note None
  380. */
  381. tls_bt_status_t tls_ble_server_connect(uint8_t server_if, const tls_bt_addr_t *bd_addr, uint8_t is_direct, int transport);
  382. /**
  383. * @brief disconnect an established connection or cancel a pending one
  384. *
  385. * @param[in] server_if the gatt server access interface created by app register
  386. * @param[in] *bd_addr the remote device address
  387. * @param[in] conn_id connection id create when connection established
  388. *
  389. * @retval @ref tls_bt_status_t
  390. *
  391. * @note None
  392. */
  393. tls_bt_status_t tls_ble_server_disconnect(uint8_t server_if, const tls_bt_addr_t *bd_addr, uint16_t conn_id);
  394. /**
  395. * @brief send value indication to a remote device
  396. *
  397. * @param[in] server_if the gatt server access interface created by app register
  398. * @param[in] attribute_handle the handle of characteristic
  399. * @param[in] conn_id connection id create when connection established
  400. * @param[in] len the length of value to be sent
  401. * @param[in] confirm need the remote device acked after receive the message , normally
  402. * Whether a confirmation is required. FALSE sends a GATT notification,
  403. * TRUE sends a GATT indication
  404. * @param[in] *p_value the value to be written
  405. *
  406. * @retval @ref tls_bt_status_t
  407. *
  408. * @note None
  409. */
  410. tls_bt_status_t tls_ble_server_send_indication(uint8_t server_if, uint16_t attribute_handle, uint16_t conn_id, int len, int confirm, char *p_value);
  411. /**
  412. * @brief send a response to a read/write operation
  413. *
  414. * @param[in] conn_id connection id create when connection established
  415. * @param[in] trans_id the transation identifier
  416. * @param[in] status TODO:
  417. * @param[in] offset the offset the fragmented value
  418. * @param[in] attr_handle the attribute handle
  419. * @param[in] auth_req access properties
  420. * @param[in] *p_value the value to be written
  421. * @param[in] len the length of value to be written
  422. *
  423. * @retval @ref tls_bt_status_t
  424. *
  425. * @note None
  426. */
  427. tls_bt_status_t tls_ble_server_send_response(uint16_t conn_id, uint32_t trans_id, uint8_t status, int offset, uint16_t attr_handle, int auth_req, uint8_t *p_value, int len);
  428. /**
  429. * @}
  430. */
  431. /**
  432. * @}
  433. */
  434. #endif /* WM_BLE_GATT_H */