bt_gatt_client.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* SPDX-License-Identifier: Apache-2.0 */
  2. /*
  3. * Copyright (C) 2013 The Android Open Source Project
  4. *
  5. */
  6. #ifndef ANDROID_INCLUDE_BT_GATT_CLIENT_H
  7. #define ANDROID_INCLUDE_BT_GATT_CLIENT_H
  8. #include <stdint.h>
  9. #include "bt_gatt_types.h"
  10. __BEGIN_DECLS
  11. /**
  12. * Buffer sizes for maximum attribute length and maximum read/write
  13. * operation buffer size.
  14. */
  15. #define BTGATT_MAX_ATTR_LEN 600
  16. /** Buffer type for unformatted reads/writes */
  17. typedef struct
  18. {
  19. uint8_t value[BTGATT_MAX_ATTR_LEN];
  20. uint16_t len;
  21. } btgatt_unformatted_value_t;
  22. /** Parameters for GATT read operations */
  23. typedef struct
  24. {
  25. btgatt_srvc_id_t srvc_id;
  26. btgatt_gatt_id_t char_id;
  27. btgatt_gatt_id_t descr_id;
  28. btgatt_unformatted_value_t value;
  29. uint16_t value_type;
  30. uint8_t status;
  31. } btgatt_read_params_t;
  32. /** Parameters for GATT write operations */
  33. typedef struct
  34. {
  35. btgatt_srvc_id_t srvc_id;
  36. btgatt_gatt_id_t char_id;
  37. btgatt_gatt_id_t descr_id;
  38. uint8_t status;
  39. } btgatt_write_params_t;
  40. /** Attribute change notification parameters */
  41. typedef struct
  42. {
  43. uint8_t value[BTGATT_MAX_ATTR_LEN];
  44. bt_bdaddr_t bda;
  45. btgatt_srvc_id_t srvc_id;
  46. btgatt_gatt_id_t char_id;
  47. uint16_t len;
  48. uint8_t is_notify;
  49. } btgatt_notify_params_t;
  50. typedef struct
  51. {
  52. bt_bdaddr_t *bda1;
  53. bt_uuid_t *uuid1;
  54. uint16_t u1;
  55. uint16_t u2;
  56. uint16_t u3;
  57. uint16_t u4;
  58. uint16_t u5;
  59. } btgatt_test_params_t;
  60. /** BT-GATT Client callback structure. */
  61. /** Callback invoked in response to register_client */
  62. typedef void (*register_client_callback)(int status, int client_if,
  63. bt_uuid_t *app_uuid);
  64. /** Callback for scan results */
  65. typedef void (*scan_result_callback)(bt_bdaddr_t* bda, int rssi, uint8_t* adv_data);
  66. /** GATT open callback invoked in response to open */
  67. typedef void (*connect_callback)(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
  68. /** Callback invoked in response to close */
  69. typedef void (*disconnect_callback)(int conn_id, int status,
  70. int client_if, bt_bdaddr_t* bda);
  71. /**
  72. * Invoked in response to search_service when the GATT service search
  73. * has been completed.
  74. */
  75. typedef void (*search_complete_callback)(int conn_id, int status);
  76. /** Reports GATT services on a remote device */
  77. typedef void (*search_result_callback)( int conn_id, btgatt_srvc_id_t *srvc_id);
  78. /** GATT characteristic enumeration result callback */
  79. typedef void (*get_characteristic_callback)(int conn_id, int status,
  80. btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
  81. int char_prop);
  82. /** GATT descriptor enumeration result callback */
  83. typedef void (*get_descriptor_callback)(int conn_id, int status,
  84. btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
  85. btgatt_gatt_id_t *descr_id);
  86. /** GATT included service enumeration result callback */
  87. typedef void (*get_included_service_callback)(int conn_id, int status,
  88. btgatt_srvc_id_t *srvc_id, btgatt_srvc_id_t *incl_srvc_id);
  89. /** Callback invoked in response to [de]register_for_notification */
  90. typedef void (*register_for_notification_callback)(int conn_id,
  91. int registered, int status, btgatt_srvc_id_t *srvc_id,
  92. btgatt_gatt_id_t *char_id);
  93. /**
  94. * Remote device notification callback, invoked when a remote device sends
  95. * a notification or indication that a client has registered for.
  96. */
  97. typedef void (*notify_callback)(int conn_id, btgatt_notify_params_t *p_data);
  98. /** Reports result of a GATT read operation */
  99. typedef void (*read_characteristic_callback)(int conn_id, int status,
  100. btgatt_read_params_t *p_data);
  101. /** GATT write characteristic operation callback */
  102. typedef void (*write_characteristic_callback)(int conn_id, int status,
  103. btgatt_write_params_t *p_data);
  104. /** GATT execute prepared write callback */
  105. typedef void (*execute_write_callback)(int conn_id, int status);
  106. /** Callback invoked in response to read_descriptor */
  107. typedef void (*read_descriptor_callback)(int conn_id, int status,
  108. btgatt_read_params_t *p_data);
  109. /** Callback invoked in response to write_descriptor */
  110. typedef void (*write_descriptor_callback)(int conn_id, int status,
  111. btgatt_write_params_t *p_data);
  112. /** Callback triggered in response to read_remote_rssi */
  113. typedef void (*read_remote_rssi_callback)(int client_if, bt_bdaddr_t* bda,
  114. int rssi, int status);
  115. /**
  116. * Callback indicating the status of a listen() operation
  117. */
  118. typedef void (*listen_callback)(int status, int server_if);
  119. /** Callback invoked when the MTU for a given connection changes */
  120. typedef void (*configure_mtu_callback)(int conn_id, int status, int mtu);
  121. /** Callback invoked when a scan filter configuration command has completed */
  122. typedef void (*scan_filter_cfg_callback)(int action, int client_if, int status, int filt_type,
  123. int avbl_space);
  124. /** Callback invoked when scan param has been added, cleared, or deleted */
  125. typedef void (*scan_filter_param_callback)(int action, int client_if, int status,
  126. int avbl_space);
  127. /** Callback invoked when a scan filter configuration command has completed */
  128. typedef void (*scan_filter_status_callback)(int enable, int client_if, int status);
  129. /** Callback invoked when multi-adv enable operation has completed */
  130. typedef void (*multi_adv_enable_callback)(int client_if, int status);
  131. /** Callback invoked when multi-adv param update operation has completed */
  132. typedef void (*multi_adv_update_callback)(int client_if, int status);
  133. /** Callback invoked when multi-adv instance data set operation has completed */
  134. typedef void (*multi_adv_data_callback)(int client_if, int status);
  135. /** Callback invoked when multi-adv disable operation has completed */
  136. typedef void (*multi_adv_disable_callback)(int client_if, int status);
  137. /**
  138. * Callback notifying an application that a remote device connection is currently congested
  139. * and cannot receive any more data. An application should avoid sending more data until
  140. * a further callback is received indicating the congestion status has been cleared.
  141. */
  142. typedef void (*congestion_callback)(int conn_id, bool congested);
  143. /** Callback invoked when batchscan storage config operation has completed */
  144. typedef void (*batchscan_cfg_storage_callback)(int client_if, int status);
  145. /** Callback invoked when batchscan enable / disable operation has completed */
  146. typedef void (*batchscan_enable_disable_callback)(int action, int client_if, int status);
  147. /** Callback invoked when batchscan reports are obtained */
  148. typedef void (*batchscan_reports_callback)(int client_if, int status, int report_format,
  149. int num_records, int data_len, uint8_t* rep_data);
  150. /** Callback invoked when batchscan storage threshold limit is crossed */
  151. typedef void (*batchscan_threshold_callback)(int client_if);
  152. /** Track ADV VSE callback invoked when tracked device is found or lost */
  153. typedef void (*track_adv_event_callback)(int client_if, int filt_index, int addr_type,
  154. bt_bdaddr_t* bda, int adv_state);
  155. typedef struct {
  156. register_client_callback register_client_cb;
  157. scan_result_callback scan_result_cb;
  158. connect_callback open_cb;
  159. disconnect_callback close_cb;
  160. search_complete_callback search_complete_cb;
  161. search_result_callback search_result_cb;
  162. get_characteristic_callback get_characteristic_cb;
  163. get_descriptor_callback get_descriptor_cb;
  164. get_included_service_callback get_included_service_cb;
  165. register_for_notification_callback register_for_notification_cb;
  166. notify_callback notify_cb;
  167. read_characteristic_callback read_characteristic_cb;
  168. write_characteristic_callback write_characteristic_cb;
  169. read_descriptor_callback read_descriptor_cb;
  170. write_descriptor_callback write_descriptor_cb;
  171. execute_write_callback execute_write_cb;
  172. read_remote_rssi_callback read_remote_rssi_cb;
  173. listen_callback listen_cb;
  174. configure_mtu_callback configure_mtu_cb;
  175. scan_filter_cfg_callback scan_filter_cfg_cb;
  176. scan_filter_param_callback scan_filter_param_cb;
  177. scan_filter_status_callback scan_filter_status_cb;
  178. multi_adv_enable_callback multi_adv_enable_cb;
  179. multi_adv_update_callback multi_adv_update_cb;
  180. multi_adv_data_callback multi_adv_data_cb;
  181. multi_adv_disable_callback multi_adv_disable_cb;
  182. congestion_callback congestion_cb;
  183. batchscan_cfg_storage_callback batchscan_cfg_storage_cb;
  184. batchscan_enable_disable_callback batchscan_enb_disable_cb;
  185. batchscan_reports_callback batchscan_reports_cb;
  186. batchscan_threshold_callback batchscan_threshold_cb;
  187. track_adv_event_callback track_adv_event_cb;
  188. } btgatt_client_callbacks_t;
  189. /** Represents the standard BT-GATT client interface. */
  190. typedef struct {
  191. /** Registers a GATT client application with the stack */
  192. bt_status_t (*register_client)( bt_uuid_t *uuid );
  193. /** Unregister a client application from the stack */
  194. bt_status_t (*unregister_client)(int client_if );
  195. /** Start or stop LE device scanning */
  196. bt_status_t (*scan)( bool start );
  197. /** Create a connection to a remote LE or dual-mode device */
  198. bt_status_t (*connect)( int client_if, const bt_bdaddr_t *bd_addr,
  199. bool is_direct, int transport );
  200. /** Disconnect a remote device or cancel a pending connection */
  201. bt_status_t (*disconnect)( int client_if, const bt_bdaddr_t *bd_addr,
  202. int conn_id);
  203. /** Start or stop advertisements to listen for incoming connections */
  204. bt_status_t (*listen)(int client_if, bool start);
  205. /** Clear the attribute cache for a given device */
  206. bt_status_t (*refresh)( int client_if, const bt_bdaddr_t *bd_addr );
  207. /**
  208. * Enumerate all GATT services on a connected device.
  209. * Optionally, the results can be filtered for a given UUID.
  210. */
  211. bt_status_t (*search_service)(int conn_id, bt_uuid_t *filter_uuid );
  212. /**
  213. * Enumerate included services for a given service.
  214. * Set start_incl_srvc_id to NULL to get the first included service.
  215. */
  216. bt_status_t (*get_included_service)( int conn_id, btgatt_srvc_id_t *srvc_id,
  217. btgatt_srvc_id_t *start_incl_srvc_id);
  218. /**
  219. * Enumerate characteristics for a given service.
  220. * Set start_char_id to NULL to get the first characteristic.
  221. */
  222. bt_status_t (*get_characteristic)( int conn_id,
  223. btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *start_char_id);
  224. /**
  225. * Enumerate descriptors for a given characteristic.
  226. * Set start_descr_id to NULL to get the first descriptor.
  227. */
  228. bt_status_t (*get_descriptor)( int conn_id,
  229. btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
  230. btgatt_gatt_id_t *start_descr_id);
  231. /** Read a characteristic on a remote device */
  232. bt_status_t (*read_characteristic)( int conn_id,
  233. btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
  234. int auth_req );
  235. /** Write a remote characteristic */
  236. bt_status_t (*write_characteristic)(int conn_id,
  237. btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
  238. int write_type, int len, int auth_req,
  239. char* p_value);
  240. /** Read the descriptor for a given characteristic */
  241. bt_status_t (*read_descriptor)(int conn_id,
  242. btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
  243. btgatt_gatt_id_t *descr_id, int auth_req);
  244. /** Write a remote descriptor for a given characteristic */
  245. bt_status_t (*write_descriptor)( int conn_id,
  246. btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
  247. btgatt_gatt_id_t *descr_id, int write_type, int len,
  248. int auth_req, char* p_value);
  249. /** Execute a prepared write operation */
  250. bt_status_t (*execute_write)(int conn_id, int execute);
  251. /**
  252. * Register to receive notifications or indications for a given
  253. * characteristic
  254. */
  255. bt_status_t (*register_for_notification)( int client_if,
  256. const bt_bdaddr_t *bd_addr, btgatt_srvc_id_t *srvc_id,
  257. btgatt_gatt_id_t *char_id);
  258. /** Deregister a previous request for notifications/indications */
  259. bt_status_t (*deregister_for_notification)( int client_if,
  260. const bt_bdaddr_t *bd_addr, btgatt_srvc_id_t *srvc_id,
  261. btgatt_gatt_id_t *char_id);
  262. /** Request RSSI for a given remote device */
  263. bt_status_t (*read_remote_rssi)( int client_if, const bt_bdaddr_t *bd_addr);
  264. /** Setup scan filter params */
  265. bt_status_t (*scan_filter_param_setup)(int client_if, int action, int filt_index, int feat_seln,
  266. int list_logic_type, int filt_logic_type, int rssi_high_thres,
  267. int rssi_low_thres, int dely_mode, int found_timeout,
  268. int lost_timeout, int found_timeout_cnt);
  269. /** Configure a scan filter condition */
  270. bt_status_t (*scan_filter_add_remove)(int client_if, int action, int filt_type,
  271. int filt_index, int company_id,
  272. int company_id_mask, const bt_uuid_t *p_uuid,
  273. const bt_uuid_t *p_uuid_mask, const bt_bdaddr_t *bd_addr,
  274. char addr_type, int data_len, char* p_data, int mask_len,
  275. char* p_mask);
  276. /** Clear all scan filter conditions for specific filter index*/
  277. bt_status_t (*scan_filter_clear)(int client_if, int filt_index);
  278. /** Enable / disable scan filter feature*/
  279. bt_status_t (*scan_filter_enable)(int client_if, bool enable);
  280. /** Determine the type of the remote device (LE, BR/EDR, Dual-mode) */
  281. int (*get_device_type)( const bt_bdaddr_t *bd_addr );
  282. /** Set the advertising data or scan response data */
  283. bt_status_t (*set_adv_data)(int client_if, bool set_scan_rsp, bool include_name,
  284. bool include_txpower, int min_interval, int max_interval, int appearance,
  285. uint16_t manufacturer_len, char* manufacturer_data,
  286. uint16_t service_data_len, char* service_data,
  287. uint16_t service_uuid_len, char* service_uuid);
  288. /** Configure the MTU for a given connection */
  289. bt_status_t (*configure_mtu)(int conn_id, int mtu);
  290. /** Request a connection parameter update */
  291. bt_status_t (*conn_parameter_update)(const bt_bdaddr_t *bd_addr, int min_interval,
  292. int max_interval, int latency, int timeout);
  293. /** Sets the LE scan interval and window in units of N*0.625 msec */
  294. bt_status_t (*set_scan_parameters)(int scan_interval, int scan_window);
  295. /* Setup the parameters as per spec, user manual specified values and enable multi ADV */
  296. bt_status_t (*multi_adv_enable)(int client_if, int min_interval,int max_interval,int adv_type,
  297. int chnl_map, int tx_power, int timeout_s);
  298. /* Update the parameters as per spec, user manual specified values and restart multi ADV */
  299. bt_status_t (*multi_adv_update)(int client_if, int min_interval,int max_interval,int adv_type,
  300. int chnl_map, int tx_power, int timeout_s);
  301. /* Setup the data for the specified instance */
  302. bt_status_t (*multi_adv_set_inst_data)(int client_if, bool set_scan_rsp, bool include_name,
  303. bool incl_txpower, int appearance, int manufacturer_len,
  304. char* manufacturer_data, int service_data_len,
  305. char* service_data, int service_uuid_len, char* service_uuid);
  306. /* Disable the multi adv instance */
  307. bt_status_t (*multi_adv_disable)(int client_if);
  308. /* Configure the batchscan storage */
  309. bt_status_t (*batchscan_cfg_storage)(int client_if, int batch_scan_full_max,
  310. int batch_scan_trunc_max, int batch_scan_notify_threshold);
  311. /* Enable batchscan */
  312. bt_status_t (*batchscan_enb_batch_scan)(int client_if, int scan_mode,
  313. int scan_interval, int scan_window, int addr_type, int discard_rule);
  314. /* Disable batchscan */
  315. bt_status_t (*batchscan_dis_batch_scan)(int client_if);
  316. /* Read out batchscan reports */
  317. bt_status_t (*batchscan_read_reports)(int client_if, int scan_mode);
  318. /** Test mode interface */
  319. bt_status_t (*test_command)( int command, btgatt_test_params_t* params);
  320. } btgatt_client_interface_t;
  321. __END_DECLS
  322. #endif /* ANDROID_INCLUDE_BT_GATT_CLIENT_H */