bt_gatt_client.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright (C) 2013 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef ANDROID_INCLUDE_BT_GATT_CLIENT_H
  17. #define ANDROID_INCLUDE_BT_GATT_CLIENT_H
  18. #include <stdint.h>
  19. #include <vector>
  20. #include "bt_common_types.h"
  21. #include "bt_gatt_types.h"
  22. #include <bluetooth/uuid.h>
  23. __BEGIN_DECLS
  24. /**
  25. * Buffer sizes for maximum attribute length and maximum read/write
  26. * operation buffer size.
  27. */
  28. #define BTGATT_MAX_ATTR_LEN 600
  29. /** Buffer type for unformatted reads/writes */
  30. typedef struct {
  31. uint8_t value[BTGATT_MAX_ATTR_LEN];
  32. uint16_t len;
  33. } btgatt_unformatted_value_t;
  34. /** Parameters for GATT read operations */
  35. typedef struct {
  36. uint16_t handle;
  37. btgatt_unformatted_value_t value;
  38. uint16_t value_type;
  39. uint8_t status;
  40. } btgatt_read_params_t;
  41. /** Parameters for GATT write operations */
  42. typedef struct {
  43. btgatt_srvc_id_t srvc_id;
  44. btgatt_gatt_id_t char_id;
  45. btgatt_gatt_id_t descr_id;
  46. uint8_t status;
  47. } btgatt_write_params_t;
  48. /** Attribute change notification parameters */
  49. typedef struct {
  50. uint8_t value[BTGATT_MAX_ATTR_LEN];
  51. RawAddress bda;
  52. uint16_t handle;
  53. uint16_t len;
  54. uint8_t is_notify;
  55. } btgatt_notify_params_t;
  56. typedef struct {
  57. RawAddress* bda1;
  58. bluetooth::Uuid* uuid1;
  59. uint16_t u1;
  60. uint16_t u2;
  61. uint16_t u3;
  62. uint16_t u4;
  63. uint16_t u5;
  64. } btgatt_test_params_t;
  65. /* BT GATT client error codes */
  66. typedef enum {
  67. BT_GATTC_COMMAND_SUCCESS = 0, /* 0 Command succeeded */
  68. BT_GATTC_COMMAND_STARTED, /* 1 Command started OK. */
  69. BT_GATTC_COMMAND_BUSY, /* 2 Device busy with another command */
  70. BT_GATTC_COMMAND_STORED, /* 3 request is stored in control block */
  71. BT_GATTC_NO_RESOURCES, /* 4 No resources to issue command */
  72. BT_GATTC_MODE_UNSUPPORTED, /* 5 Request for 1 or more unsupported modes */
  73. BT_GATTC_ILLEGAL_VALUE, /* 6 Illegal command /parameter value */
  74. BT_GATTC_INCORRECT_STATE, /* 7 Device in wrong state for request */
  75. BT_GATTC_UNKNOWN_ADDR, /* 8 Unknown remote BD address */
  76. BT_GATTC_DEVICE_TIMEOUT, /* 9 Device timeout */
  77. BT_GATTC_INVALID_CONTROLLER_OUTPUT, /* 10 An incorrect value was received
  78. from HCI */
  79. BT_GATTC_SECURITY_ERROR, /* 11 Authorization or security failure or not
  80. authorized */
  81. BT_GATTC_DELAYED_ENCRYPTION_CHECK, /*12 Delayed encryption check */
  82. BT_GATTC_ERR_PROCESSING /* 12 Generic error */
  83. } btgattc_error_t;
  84. /** BT-GATT Client callback structure. */
  85. /** Callback invoked in response to register_client */
  86. typedef void (*register_client_callback)(int status, int client_if,
  87. const bluetooth::Uuid& app_uuid);
  88. /** GATT open callback invoked in response to open */
  89. typedef void (*connect_callback)(int conn_id, int status, int client_if,
  90. const RawAddress& bda);
  91. /** Callback invoked in response to close */
  92. typedef void (*disconnect_callback)(int conn_id, int status, int client_if,
  93. const RawAddress& bda);
  94. /**
  95. * Invoked in response to search_service when the GATT service search
  96. * has been completed.
  97. */
  98. typedef void (*search_complete_callback)(int conn_id, int status);
  99. /** Callback invoked in response to [de]register_for_notification */
  100. typedef void (*register_for_notification_callback)(int conn_id, int registered,
  101. int status, uint16_t handle);
  102. /**
  103. * Remote device notification callback, invoked when a remote device sends
  104. * a notification or indication that a client has registered for.
  105. */
  106. typedef void (*notify_callback)(int conn_id,
  107. const btgatt_notify_params_t& p_data);
  108. /** Reports result of a GATT read operation */
  109. typedef void (*read_characteristic_callback)(int conn_id, int status,
  110. btgatt_read_params_t* p_data);
  111. /** GATT write characteristic operation callback */
  112. typedef void (*write_characteristic_callback)(int conn_id, int status,
  113. uint16_t handle);
  114. /** GATT execute prepared write callback */
  115. typedef void (*execute_write_callback)(int conn_id, int status);
  116. /** Callback invoked in response to read_descriptor */
  117. typedef void (*read_descriptor_callback)(int conn_id, int status,
  118. const btgatt_read_params_t& p_data);
  119. /** Callback invoked in response to write_descriptor */
  120. typedef void (*write_descriptor_callback)(int conn_id, int status,
  121. uint16_t handle);
  122. /** Callback triggered in response to read_remote_rssi */
  123. typedef void (*read_remote_rssi_callback)(int client_if, const RawAddress& bda,
  124. int rssi, int status);
  125. /** Callback invoked when the MTU for a given connection changes */
  126. typedef void (*configure_mtu_callback)(int conn_id, int status, int mtu);
  127. /**
  128. * Callback notifying an application that a remote device connection is
  129. * currently congested and cannot receive any more data. An application should
  130. * avoid sending more data until a further callback is received indicating the
  131. * congestion status has been cleared.
  132. */
  133. typedef void (*congestion_callback)(int conn_id, bool congested);
  134. /** GATT get database callback */
  135. typedef void (*get_gatt_db_callback)(int conn_id, const btgatt_db_element_t* db,
  136. int count);
  137. /** GATT services between start_handle and end_handle were removed */
  138. typedef void (*services_removed_callback)(int conn_id, uint16_t start_handle,
  139. uint16_t end_handle);
  140. /** GATT services were added */
  141. typedef void (*services_added_callback)(int conn_id,
  142. const btgatt_db_element_t& added,
  143. int added_count);
  144. /** Callback invoked when the PHY for a given connection changes */
  145. typedef void (*phy_updated_callback)(int conn_id, uint8_t tx_phy,
  146. uint8_t rx_phy, uint8_t status);
  147. /** Callback invoked when the connection parameters for a given connection
  148. * changes */
  149. typedef void (*conn_updated_callback)(int conn_id, uint16_t interval,
  150. uint16_t latency, uint16_t timeout,
  151. uint8_t status);
  152. typedef struct {
  153. register_client_callback register_client_cb;
  154. connect_callback open_cb;
  155. disconnect_callback close_cb;
  156. search_complete_callback search_complete_cb;
  157. register_for_notification_callback register_for_notification_cb;
  158. notify_callback notify_cb;
  159. read_characteristic_callback read_characteristic_cb;
  160. write_characteristic_callback write_characteristic_cb;
  161. read_descriptor_callback read_descriptor_cb;
  162. write_descriptor_callback write_descriptor_cb;
  163. execute_write_callback execute_write_cb;
  164. read_remote_rssi_callback read_remote_rssi_cb;
  165. configure_mtu_callback configure_mtu_cb;
  166. congestion_callback congestion_cb;
  167. get_gatt_db_callback get_gatt_db_cb;
  168. services_removed_callback services_removed_cb;
  169. services_added_callback services_added_cb;
  170. phy_updated_callback phy_updated_cb;
  171. conn_updated_callback conn_updated_cb;
  172. } btgatt_client_callbacks_t;
  173. /** Represents the standard BT-GATT client interface. */
  174. typedef struct {
  175. /** Registers a GATT client application with the stack */
  176. bt_status_t (*register_client)(const bluetooth::Uuid& uuid);
  177. /** Unregister a client application from the stack */
  178. bt_status_t (*unregister_client)(int client_if);
  179. /** Create a connection to a remote LE or dual-mode device */
  180. bt_status_t (*connect)(int client_if, const RawAddress& bd_addr,
  181. bool is_direct, int transport, bool opportunistic,
  182. int initiating_phys);
  183. /** Disconnect a remote device or cancel a pending connection */
  184. bt_status_t (*disconnect)(int client_if, const RawAddress& bd_addr,
  185. int conn_id);
  186. /** Clear the attribute cache for a given device */
  187. bt_status_t (*refresh)(int client_if, const RawAddress& bd_addr);
  188. /**
  189. * Enumerate all GATT services on a connected device.
  190. * Optionally, the results can be filtered for a given UUID.
  191. */
  192. bt_status_t (*search_service)(int conn_id,
  193. const bluetooth::Uuid* filter_uuid);
  194. /**
  195. * Sead "Find service by UUID" request. Used only for PTS tests.
  196. */
  197. void (*btif_gattc_discover_service_by_uuid)(int conn_id,
  198. const bluetooth::Uuid& uuid);
  199. /** Read a characteristic on a remote device */
  200. bt_status_t (*read_characteristic)(int conn_id, uint16_t handle,
  201. int auth_req);
  202. /** Read a characteristic on a remote device */
  203. bt_status_t (*read_using_characteristic_uuid)(int conn_id,
  204. const bluetooth::Uuid& uuid,
  205. uint16_t s_handle,
  206. uint16_t e_handle,
  207. int auth_req);
  208. /** Write a remote characteristic */
  209. bt_status_t (*write_characteristic)(int conn_id, uint16_t handle,
  210. int write_type, int auth_req,
  211. std::vector<uint8_t> value);
  212. /** Read the descriptor for a given characteristic */
  213. bt_status_t (*read_descriptor)(int conn_id, uint16_t handle, int auth_req);
  214. /** Write a remote descriptor for a given characteristic */
  215. bt_status_t (*write_descriptor)(int conn_id, uint16_t handle, int auth_req,
  216. std::vector<uint8_t> value);
  217. /** Execute a prepared write operation */
  218. bt_status_t (*execute_write)(int conn_id, int execute);
  219. /**
  220. * Register to receive notifications or indications for a given
  221. * characteristic
  222. */
  223. bt_status_t (*register_for_notification)(int client_if,
  224. const RawAddress& bd_addr,
  225. uint16_t handle);
  226. /** Deregister a previous request for notifications/indications */
  227. bt_status_t (*deregister_for_notification)(int client_if,
  228. const RawAddress& bd_addr,
  229. uint16_t handle);
  230. /** Request RSSI for a given remote device */
  231. bt_status_t (*read_remote_rssi)(int client_if, const RawAddress& bd_addr);
  232. /** Determine the type of the remote device (LE, BR/EDR, Dual-mode) */
  233. int (*get_device_type)(const RawAddress& bd_addr);
  234. /** Configure the MTU for a given connection */
  235. bt_status_t (*configure_mtu)(int conn_id, int mtu);
  236. /** Request a connection parameter update */
  237. bt_status_t (*conn_parameter_update)(const RawAddress& bd_addr,
  238. int min_interval, int max_interval,
  239. int latency, int timeout,
  240. uint16_t min_ce_len,
  241. uint16_t max_ce_len);
  242. bt_status_t (*set_preferred_phy)(const RawAddress& bd_addr, uint8_t tx_phy,
  243. uint8_t rx_phy, uint16_t phy_options);
  244. bt_status_t (*read_phy)(
  245. const RawAddress& bd_addr,
  246. base::Callback<void(uint8_t tx_phy, uint8_t rx_phy, uint8_t status)> cb);
  247. /** Test mode interface */
  248. bt_status_t (*test_command)(int command, const btgatt_test_params_t& params);
  249. /** Get gatt db content */
  250. bt_status_t (*get_gatt_db)(int conn_id);
  251. } btgatt_client_interface_t;
  252. __END_DECLS
  253. #endif /* ANDROID_INCLUDE_BT_GATT_CLIENT_H */