bt_gatt_client.h
Go to the documentation of this file.
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 
17 
18 #ifndef ANDROID_INCLUDE_BT_GATT_CLIENT_H
19 #define ANDROID_INCLUDE_BT_GATT_CLIENT_H
20 
21 #include <stdint.h>
22 #include <vector>
23 #include "bt_gatt_types.h"
24 #include "bt_common_types.h"
25 
26 using std::vector;
27 
28 __BEGIN_DECLS
29 
30 /**
31  * Buffer sizes for maximum attribute length and maximum read/write
32  * operation buffer size.
33  */
34 #define BTGATT_MAX_ATTR_LEN 600
35 
36 /** Buffer type for unformatted reads/writes */
37 typedef struct
38 {
39  uint8_t value[BTGATT_MAX_ATTR_LEN];
40  uint16_t len;
42 
43 /** Parameters for GATT read operations */
44 typedef struct
45 {
46  uint16_t handle;
48  uint16_t value_type;
49  uint8_t status;
51 
52 /** Parameters for GATT write operations */
53 typedef struct
54 {
58  uint8_t status;
60 
61 /** Attribute change notification parameters */
62 typedef struct
63 {
64  uint8_t value[BTGATT_MAX_ATTR_LEN];
65  bt_bdaddr_t bda;
66  uint16_t handle;
67  uint16_t len;
68  uint8_t is_notify;
70 
71 typedef struct
72 {
73  uint8_t client_if;
74  uint8_t action;
75  uint8_t filt_index;
76  uint16_t feat_seln;
77  uint16_t list_logic_type;
78  uint8_t filt_logic_type;
79  uint8_t rssi_high_thres;
80  uint8_t rssi_low_thres;
81  uint8_t dely_mode;
82  uint16_t found_timeout;
83  uint16_t lost_timeout;
87 
88 typedef struct
89 {
90  bt_bdaddr_t *bda1;
92  uint16_t u1;
93  uint16_t u2;
94  uint16_t u3;
95  uint16_t u4;
96  uint16_t u5;
98 
99 /* BT GATT client error codes */
100 typedef enum
101 {
102  BT_GATTC_COMMAND_SUCCESS = 0, /* 0 Command succeeded */
103  BT_GATTC_COMMAND_STARTED, /* 1 Command started OK. */
104  BT_GATTC_COMMAND_BUSY, /* 2 Device busy with another command */
105  BT_GATTC_COMMAND_STORED, /* 3 request is stored in control block */
106  BT_GATTC_NO_RESOURCES, /* 4 No resources to issue command */
107  BT_GATTC_MODE_UNSUPPORTED, /* 5 Request for 1 or more unsupported modes */
108  BT_GATTC_ILLEGAL_VALUE, /* 6 Illegal command /parameter value */
109  BT_GATTC_INCORRECT_STATE, /* 7 Device in wrong state for request */
110  BT_GATTC_UNKNOWN_ADDR, /* 8 Unknown remote BD address */
111  BT_GATTC_DEVICE_TIMEOUT, /* 9 Device timeout */
112  BT_GATTC_INVALID_CONTROLLER_OUTPUT,/* 10 An incorrect value was received from HCI */
113  BT_GATTC_SECURITY_ERROR, /* 11 Authorization or security failure or not authorized */
114  BT_GATTC_DELAYED_ENCRYPTION_CHECK, /*12 Delayed encryption check */
115  BT_GATTC_ERR_PROCESSING /* 12 Generic error */
117 
118 /** BT-GATT Client callback structure. */
119 
120 /** Callback invoked in response to register_client */
121 typedef void (*register_client_callback)(int status, int client_if,
122  bt_uuid_t *app_uuid);
123 
124 /** Callback for scan results */
125 typedef void (*scan_result_callback)(bt_bdaddr_t* bda, int rssi, vector<uint8_t> adv_data);
126 
127 /** GATT open callback invoked in response to open */
128 typedef void (*connect_callback)(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
129 
130 /** Callback invoked in response to close */
131 typedef void (*disconnect_callback)(int conn_id, int status,
132  int client_if, bt_bdaddr_t* bda);
133 
134 /**
135  * Invoked in response to search_service when the GATT service search
136  * has been completed.
137  */
138 typedef void (*search_complete_callback)(int conn_id, int status);
139 
140 /** Callback invoked in response to [de]register_for_notification */
141 typedef void (*register_for_notification_callback)(int conn_id,
142  int registered, int status, uint16_t handle);
143 
144 /**
145  * Remote device notification callback, invoked when a remote device sends
146  * a notification or indication that a client has registered for.
147  */
148 typedef void (*notify_callback)(int conn_id, btgatt_notify_params_t *p_data);
149 
150 /** Reports result of a GATT read operation */
151 typedef void (*read_characteristic_callback)(int conn_id, int status,
152  btgatt_read_params_t *p_data);
153 
154 /** GATT write characteristic operation callback */
155 typedef void (*write_characteristic_callback)(int conn_id, int status, uint16_t handle);
156 
157 /** GATT execute prepared write callback */
158 typedef void (*execute_write_callback)(int conn_id, int status);
159 
160 /** Callback invoked in response to read_descriptor */
161 typedef void (*read_descriptor_callback)(int conn_id, int status,
162  btgatt_read_params_t *p_data);
163 
164 /** Callback invoked in response to write_descriptor */
165 typedef void (*write_descriptor_callback)(int conn_id, int status, uint16_t handle);
166 
167 /** Callback triggered in response to read_remote_rssi */
168 typedef void (*read_remote_rssi_callback)(int client_if, bt_bdaddr_t* bda,
169  int rssi, int status);
170 
171 /**
172  * Callback indicating the status of a listen() operation
173  */
174 typedef void (*listen_callback)(int status, int server_if);
175 
176 /** Callback invoked when the MTU for a given connection changes */
177 typedef void (*configure_mtu_callback)(int conn_id, int status, int mtu);
178 
179 /** Callback invoked when a scan filter configuration command has completed */
180 typedef void (*scan_filter_cfg_callback)(int action, int client_if, int status, int filt_type,
181  int avbl_space);
182 
183 /** Callback invoked when scan param has been added, cleared, or deleted */
184 typedef void (*scan_filter_param_callback)(int action, int client_if, int status,
185  int avbl_space);
186 
187 /** Callback invoked when a scan filter configuration command has completed */
188 typedef void (*scan_filter_status_callback)(int enable, int client_if, int status);
189 
190 /** Callback invoked when multi-adv enable operation has completed */
191 typedef void (*multi_adv_enable_callback)(int client_if, int status);
192 
193 /** Callback invoked when multi-adv param update operation has completed */
194 typedef void (*multi_adv_update_callback)(int client_if, int status);
195 
196 /** Callback invoked when multi-adv instance data set operation has completed */
197 typedef void (*multi_adv_data_callback)(int client_if, int status);
198 
199 /** Callback invoked when multi-adv disable operation has completed */
200 typedef void (*multi_adv_disable_callback)(int client_if, int status);
201 
202 /**
203  * Callback notifying an application that a remote device connection is currently congested
204  * and cannot receive any more data. An application should avoid sending more data until
205  * a further callback is received indicating the congestion status has been cleared.
206  */
207 typedef void (*congestion_callback)(int conn_id, bool congested);
208 /** Callback invoked when batchscan storage config operation has completed */
209 typedef void (*batchscan_cfg_storage_callback)(int client_if, int status);
210 
211 /** Callback invoked when batchscan enable / disable operation has completed */
212 typedef void (*batchscan_enable_disable_callback)(int action, int client_if, int status);
213 
214 /** Callback invoked when batchscan reports are obtained */
215 typedef void (*batchscan_reports_callback)(int client_if, int status, int report_format,
216  int num_records, vector<uint8_t> data);
217 
218 /** Callback invoked when batchscan storage threshold limit is crossed */
219 typedef void (*batchscan_threshold_callback)(int client_if);
220 
221 /** Track ADV VSE callback invoked when tracked device is found or lost */
222 typedef void (*track_adv_event_callback)(btgatt_track_adv_info_t *p_track_adv_info);
223 
224 /** Callback invoked when scan parameter setup has completed */
225 typedef void (*scan_parameter_setup_completed_callback)(int client_if,
226  btgattc_error_t status);
227 
228 /** GATT get database callback */
229 typedef void (*get_gatt_db_callback)(int conn_id, btgatt_db_element_t *db, int count);
230 
231 /** GATT services between start_handle and end_handle were removed */
232 typedef void (*services_removed_callback)(int conn_id, uint16_t start_handle, uint16_t end_handle);
233 
234 /** GATT services were added */
235 typedef void (*services_added_callback)(int conn_id, btgatt_db_element_t *added, int added_count);
236 
237 typedef struct {
271 
272 /** Represents the standard BT-GATT client interface. */
273 
274 typedef struct {
275  /** Registers a GATT client application with the stack */
276  bt_status_t (*register_client)( bt_uuid_t *uuid );
277 
278  /** Unregister a client application from the stack */
279  bt_status_t (*unregister_client)(int client_if );
280 
281  /** Start or stop LE device scanning */
282  bt_status_t (*scan)( bool start );
283 
284  /** Create a connection to a remote LE or dual-mode device */
285  bt_status_t (*connect)( int client_if, const bt_bdaddr_t *bd_addr,
286  bool is_direct, int transport );
287 
288  /** Disconnect a remote device or cancel a pending connection */
289  bt_status_t (*disconnect)( int client_if, const bt_bdaddr_t *bd_addr,
290  int conn_id);
291 
292  /** Start or stop advertisements to listen for incoming connections */
293  bt_status_t (*listen)(int client_if, bool start);
294 
295  /** Clear the attribute cache for a given device */
296  bt_status_t (*refresh)( int client_if, const bt_bdaddr_t *bd_addr );
297 
298  /**
299  * Enumerate all GATT services on a connected device.
300  * Optionally, the results can be filtered for a given UUID.
301  */
302  bt_status_t (*search_service)(int conn_id, bt_uuid_t *filter_uuid );
303 
304  /** Read a characteristic on a remote device */
305  bt_status_t (*read_characteristic)( int conn_id, uint16_t handle,
306  int auth_req );
307 
308  /** Write a remote characteristic */
309  bt_status_t (*write_characteristic)(int conn_id, uint16_t handle,
310  int write_type, int auth_req,
311  vector<uint8_t> value);
312 
313  /** Read the descriptor for a given characteristic */
314  bt_status_t (*read_descriptor)(int conn_id, uint16_t handle, int auth_req);
315 
316  /** Write a remote descriptor for a given characteristic */
317  bt_status_t (*write_descriptor)( int conn_id, uint16_t handle,
318  int write_type, int auth_req,
319  vector<uint8_t> value);
320 
321  /** Execute a prepared write operation */
322  bt_status_t (*execute_write)(int conn_id, int execute);
323 
324  /**
325  * Register to receive notifications or indications for a given
326  * characteristic
327  */
328  bt_status_t (*register_for_notification)( int client_if,
329  const bt_bdaddr_t *bd_addr, uint16_t handle);
330 
331  /** Deregister a previous request for notifications/indications */
332  bt_status_t (*deregister_for_notification)( int client_if,
333  const bt_bdaddr_t *bd_addr, uint16_t handle);
334 
335  /** Request RSSI for a given remote device */
336  bt_status_t (*read_remote_rssi)( int client_if, const bt_bdaddr_t *bd_addr);
337 
338  /** Setup scan filter params */
339  bt_status_t (*scan_filter_param_setup)(btgatt_filt_param_setup_t filt_param);
340 
341 
342  /** Configure a scan filter condition */
343  bt_status_t (*scan_filter_add_remove)(int client_if, int action, int filt_type,
344  int filt_index, int company_id,
345  int company_id_mask, const bt_uuid_t *p_uuid,
346  const bt_uuid_t *p_uuid_mask, const bt_bdaddr_t *bd_addr,
347  char addr_type, vector<uint8_t> data,
348  vector<uint8_t> p_mask);
349 
350  /** Clear all scan filter conditions for specific filter index*/
351  bt_status_t (*scan_filter_clear)(int client_if, int filt_index);
352 
353  /** Enable / disable scan filter feature*/
354  bt_status_t (*scan_filter_enable)(int client_if, bool enable);
355 
356  /** Determine the type of the remote device (LE, BR/EDR, Dual-mode) */
357  int (*get_device_type)( const bt_bdaddr_t *bd_addr );
358 
359  /** Set the advertising data or scan response data */
360  bt_status_t (*set_adv_data)(int client_if, bool set_scan_rsp, bool include_name,
361  bool include_txpower, int min_interval, int max_interval, int appearance,
362  vector<uint8_t> manufacturer_data,
363  vector<uint8_t> service_data,
364  vector<uint8_t> service_uuid);
365 
366  /** Configure the MTU for a given connection */
367  bt_status_t (*configure_mtu)(int conn_id, int mtu);
368 
369  /** Request a connection parameter update */
370  bt_status_t (*conn_parameter_update)(const bt_bdaddr_t *bd_addr, int min_interval,
371  int max_interval, int latency, int timeout);
372 
373  /** Sets the LE scan interval and window in units of N*0.625 msec */
374  bt_status_t (*set_scan_parameters)(int client_if, int scan_interval, int scan_window);
375 
376  /* Setup the parameters as per spec, user manual specified values and enable multi ADV */
377  bt_status_t (*multi_adv_enable)(int client_if, int min_interval,int max_interval,int adv_type,
378  int chnl_map, int tx_power, int timeout_s);
379 
380  /* Update the parameters as per spec, user manual specified values and restart multi ADV */
381  bt_status_t (*multi_adv_update)(int client_if, int min_interval,int max_interval,int adv_type,
382  int chnl_map, int tx_power, int timeout_s);
383 
384  /* Setup the data for the specified instance */
385  bt_status_t (*multi_adv_set_inst_data)(int client_if, bool set_scan_rsp, bool include_name,
386  bool incl_txpower, int appearance, vector<uint8_t> manufacturer_data,
387  vector<uint8_t> service_data, vector<uint8_t> service_uuid);
388 
389  /* Disable the multi adv instance */
390  bt_status_t (*multi_adv_disable)(int client_if);
391 
392  /* Configure the batchscan storage */
393  bt_status_t (*batchscan_cfg_storage)(int client_if, int batch_scan_full_max,
394  int batch_scan_trunc_max, int batch_scan_notify_threshold);
395 
396  /* Enable batchscan */
397  bt_status_t (*batchscan_enb_batch_scan)(int client_if, int scan_mode,
398  int scan_interval, int scan_window, int addr_type, int discard_rule);
399 
400  /* Disable batchscan */
401  bt_status_t (*batchscan_dis_batch_scan)(int client_if);
402 
403  /* Read out batchscan reports */
404  bt_status_t (*batchscan_read_reports)(int client_if, int scan_mode);
405 
406  /** Test mode interface */
407  bt_status_t (*test_command)( int command, btgatt_test_params_t* params);
408 
409  /** Get gatt db content */
410  bt_status_t (*get_gatt_db)( int conn_id);
411 
413 
414 __END_DECLS
415 
416 #endif /* ANDROID_INCLUDE_BT_GATT_CLIENT_H */
void(* read_descriptor_callback)(int conn_id, int status, btgatt_read_params_t *p_data)
write_characteristic_callback write_characteristic_cb
void(* services_removed_callback)(int conn_id, uint16_t start_handle, uint16_t end_handle)
register_client_callback register_client_cb
void(* register_for_notification_callback)(int conn_id, int registered, int status, uint16_t handle)
btgatt_srvc_id_t srvc_id
services_removed_callback services_removed_cb
void(* congestion_callback)(int conn_id, bool congested)
btgatt_gatt_id_t char_id
multi_adv_disable_callback multi_adv_disable_cb
execute_write_callback execute_write_cb
disconnect_callback close_cb
void(* scan_filter_param_callback)(int action, int client_if, int status, int avbl_space)
batchscan_cfg_storage_callback batchscan_cfg_storage_cb
scan_filter_status_callback scan_filter_status_cb
void(* execute_write_callback)(int conn_id, int status)
read_characteristic_callback read_characteristic_cb
#define BTGATT_MAX_ATTR_LEN
bt_status_t
Definition: bluetooth.h:80
write_descriptor_callback write_descriptor_cb
void(* connect_callback)(int conn_id, int status, int client_if, bt_bdaddr_t *bda)
multi_adv_enable_callback multi_adv_enable_cb
void(* search_complete_callback)(int conn_id, int status)
search_complete_callback search_complete_cb
void(* notify_callback)(int conn_id, btgatt_notify_params_t *p_data)
void(* batchscan_reports_callback)(int client_if, int status, int report_format, int num_records, vector< uint8_t > data)
bt_bdaddr_t * bda1
batchscan_threshold_callback batchscan_threshold_cb
void(* register_client_callback)(int status, int client_if, bt_uuid_t *app_uuid)
void(* listen_callback)(int status, int server_if)
void(* write_characteristic_callback)(int conn_id, int status, uint16_t handle)
multi_adv_data_callback multi_adv_data_cb
batchscan_reports_callback batchscan_reports_cb
btgatt_gatt_id_t descr_id
void(* multi_adv_disable_callback)(int client_if, int status)
congestion_callback congestion_cb
void(* multi_adv_data_callback)(int client_if, int status)
void(* read_remote_rssi_callback)(int client_if, bt_bdaddr_t *bda, int rssi, int status)
void(* multi_adv_enable_callback)(int client_if, int status)
scan_filter_param_callback scan_filter_param_cb
track_adv_event_callback track_adv_event_cb
void(* batchscan_cfg_storage_callback)(int client_if, int status)
services_added_callback services_added_cb
void(* batchscan_threshold_callback)(int client_if)
void(* scan_filter_status_callback)(int enable, int client_if, int status)
btgattc_error_t
void(* write_descriptor_callback)(int conn_id, int status, uint16_t handle)
void(* services_added_callback)(int conn_id, btgatt_db_element_t *added, int added_count)
void(* get_gatt_db_callback)(int conn_id, btgatt_db_element_t *db, int count)
void(* scan_parameter_setup_completed_callback)(int client_if, btgattc_error_t status)
get_gatt_db_callback get_gatt_db_cb
void(* track_adv_event_callback)(btgatt_track_adv_info_t *p_track_adv_info)
scan_filter_cfg_callback scan_filter_cfg_cb
read_remote_rssi_callback read_remote_rssi_cb
void(* read_characteristic_callback)(int conn_id, int status, btgatt_read_params_t *p_data)
void(* batchscan_enable_disable_callback)(int action, int client_if, int status)
multi_adv_update_callback multi_adv_update_cb
void(* scan_result_callback)(bt_bdaddr_t *bda, int rssi, vector< uint8_t > adv_data)
void(* configure_mtu_callback)(int conn_id, int status, int mtu)
btgatt_unformatted_value_t value
void(* multi_adv_update_callback)(int client_if, int status)
void(* scan_filter_cfg_callback)(int action, int client_if, int status, int filt_type, int avbl_space)
configure_mtu_callback configure_mtu_cb
scan_result_callback scan_result_cb
register_for_notification_callback register_for_notification_cb
connect_callback open_cb
batchscan_enable_disable_callback batchscan_enb_disable_cb
scan_parameter_setup_completed_callback scan_parameter_setup_completed_cb
void(* disconnect_callback)(int conn_id, int status, int client_if, bt_bdaddr_t *bda)
read_descriptor_callback read_descriptor_cb