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 "bt_gatt_types.h"
23#include "bt_common_types.h"
24
25__BEGIN_DECLS
26
27/**
28 * Buffer sizes for maximum attribute length and maximum read/write
29 * operation buffer size.
30 */
31#define BTGATT_MAX_ATTR_LEN 600
32
33/** Buffer type for unformatted reads/writes */
34typedef struct
35{
36    uint8_t             value[BTGATT_MAX_ATTR_LEN];
37    uint16_t            len;
38} btgatt_unformatted_value_t;
39
40/** Parameters for GATT read operations */
41typedef struct
42{
43    btgatt_srvc_id_t    srvc_id;
44    btgatt_gatt_id_t    char_id;
45    btgatt_gatt_id_t    descr_id;
46    btgatt_unformatted_value_t value;
47    uint16_t            value_type;
48    uint8_t             status;
49} btgatt_read_params_t;
50
51/** Parameters for GATT write operations */
52typedef struct
53{
54    btgatt_srvc_id_t    srvc_id;
55    btgatt_gatt_id_t    char_id;
56    btgatt_gatt_id_t    descr_id;
57    uint8_t             status;
58} btgatt_write_params_t;
59
60/** Attribute change notification parameters */
61typedef struct
62{
63    uint8_t             value[BTGATT_MAX_ATTR_LEN];
64    bt_bdaddr_t         bda;
65    btgatt_srvc_id_t    srvc_id;
66    btgatt_gatt_id_t    char_id;
67    uint16_t            len;
68    uint8_t             is_notify;
69} btgatt_notify_params_t;
70
71typedef 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;
84    uint8_t  found_timeout_cnt;
85    uint16_t  num_of_tracking_entries;
86} btgatt_filt_param_setup_t;
87
88typedef struct
89{
90    bt_bdaddr_t        *bda1;
91    bt_uuid_t          *uuid1;
92    uint16_t            u1;
93    uint16_t            u2;
94    uint16_t            u3;
95    uint16_t            u4;
96    uint16_t            u5;
97} btgatt_test_params_t;
98
99/* BT GATT client error codes */
100typedef 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                     */
116} btgattc_error_t;
117
118/** BT-GATT Client callback structure. */
119
120/** Callback invoked in response to register_client */
121typedef void (*register_client_callback)(int status, int client_if,
122                bt_uuid_t *app_uuid);
123
124/** Callback for scan results */
125typedef void (*scan_result_callback)(bt_bdaddr_t* bda, int rssi, uint8_t* adv_data);
126
127/** GATT open callback invoked in response to open */
128typedef void (*connect_callback)(int conn_id, int status, int client_if, bt_bdaddr_t* bda);
129
130/** Callback invoked in response to close */
131typedef 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 */
138typedef void (*search_complete_callback)(int conn_id, int status);
139
140/** Reports GATT services on a remote device */
141typedef void (*search_result_callback)( int conn_id, btgatt_srvc_id_t *srvc_id);
142
143/** GATT characteristic enumeration result callback */
144typedef void (*get_characteristic_callback)(int conn_id, int status,
145                btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
146                int char_prop);
147
148/** GATT descriptor enumeration result callback */
149typedef void (*get_descriptor_callback)(int conn_id, int status,
150                btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
151                btgatt_gatt_id_t *descr_id);
152
153/** GATT included service enumeration result callback */
154typedef void (*get_included_service_callback)(int conn_id, int status,
155                btgatt_srvc_id_t *srvc_id, btgatt_srvc_id_t *incl_srvc_id);
156
157/** Callback invoked in response to [de]register_for_notification */
158typedef void (*register_for_notification_callback)(int conn_id,
159                int registered, int status, btgatt_srvc_id_t *srvc_id,
160                btgatt_gatt_id_t *char_id);
161
162/**
163 * Remote device notification callback, invoked when a remote device sends
164 * a notification or indication that a client has registered for.
165 */
166typedef void (*notify_callback)(int conn_id, btgatt_notify_params_t *p_data);
167
168/** Reports result of a GATT read operation */
169typedef void (*read_characteristic_callback)(int conn_id, int status,
170                btgatt_read_params_t *p_data);
171
172/** GATT write characteristic operation callback */
173typedef void (*write_characteristic_callback)(int conn_id, int status,
174                btgatt_write_params_t *p_data);
175
176/** GATT execute prepared write callback */
177typedef void (*execute_write_callback)(int conn_id, int status);
178
179/** Callback invoked in response to read_descriptor */
180typedef void (*read_descriptor_callback)(int conn_id, int status,
181                btgatt_read_params_t *p_data);
182
183/** Callback invoked in response to write_descriptor */
184typedef void (*write_descriptor_callback)(int conn_id, int status,
185                btgatt_write_params_t *p_data);
186
187/** Callback triggered in response to read_remote_rssi */
188typedef void (*read_remote_rssi_callback)(int client_if, bt_bdaddr_t* bda,
189                                          int rssi, int status);
190
191/**
192 * Callback indicating the status of a listen() operation
193 */
194typedef void (*listen_callback)(int status, int server_if);
195
196/** Callback invoked when the MTU for a given connection changes */
197typedef void (*configure_mtu_callback)(int conn_id, int status, int mtu);
198
199/** Callback invoked when a scan filter configuration command has completed */
200typedef void (*scan_filter_cfg_callback)(int action, int client_if, int status, int filt_type,
201                                         int avbl_space);
202
203/** Callback invoked when scan param has been added, cleared, or deleted */
204typedef void (*scan_filter_param_callback)(int action, int client_if, int status,
205                                         int avbl_space);
206
207/** Callback invoked when a scan filter configuration command has completed */
208typedef void (*scan_filter_status_callback)(int enable, int client_if, int status);
209
210/** Callback invoked when multi-adv enable operation has completed */
211typedef void (*multi_adv_enable_callback)(int client_if, int status);
212
213/** Callback invoked when multi-adv param update operation has completed */
214typedef void (*multi_adv_update_callback)(int client_if, int status);
215
216/** Callback invoked when multi-adv instance data set operation has completed */
217typedef void (*multi_adv_data_callback)(int client_if, int status);
218
219/** Callback invoked when multi-adv disable operation has completed */
220typedef void (*multi_adv_disable_callback)(int client_if, int status);
221
222/**
223 * Callback notifying an application that a remote device connection is currently congested
224 * and cannot receive any more data. An application should avoid sending more data until
225 * a further callback is received indicating the congestion status has been cleared.
226 */
227typedef void (*congestion_callback)(int conn_id, bool congested);
228/** Callback invoked when batchscan storage config operation has completed */
229typedef void (*batchscan_cfg_storage_callback)(int client_if, int status);
230
231/** Callback invoked when batchscan enable / disable operation has completed */
232typedef void (*batchscan_enable_disable_callback)(int action, int client_if, int status);
233
234/** Callback invoked when batchscan reports are obtained */
235typedef void (*batchscan_reports_callback)(int client_if, int status, int report_format,
236                                           int num_records, int data_len, uint8_t* rep_data);
237
238/** Callback invoked when batchscan storage threshold limit is crossed */
239typedef void (*batchscan_threshold_callback)(int client_if);
240
241/** Track ADV VSE callback invoked when tracked device is found or lost */
242typedef void (*track_adv_event_callback)(btgatt_track_adv_info_t *p_track_adv_info);
243
244/** Callback invoked when scan parameter setup has completed */
245typedef void (*scan_parameter_setup_completed_callback)(int client_if,
246                                                        btgattc_error_t status);
247
248typedef struct {
249    register_client_callback            register_client_cb;
250    scan_result_callback                scan_result_cb;
251    connect_callback                    open_cb;
252    disconnect_callback                 close_cb;
253    search_complete_callback            search_complete_cb;
254    search_result_callback              search_result_cb;
255    get_characteristic_callback         get_characteristic_cb;
256    get_descriptor_callback             get_descriptor_cb;
257    get_included_service_callback       get_included_service_cb;
258    register_for_notification_callback  register_for_notification_cb;
259    notify_callback                     notify_cb;
260    read_characteristic_callback        read_characteristic_cb;
261    write_characteristic_callback       write_characteristic_cb;
262    read_descriptor_callback            read_descriptor_cb;
263    write_descriptor_callback           write_descriptor_cb;
264    execute_write_callback              execute_write_cb;
265    read_remote_rssi_callback           read_remote_rssi_cb;
266    listen_callback                     listen_cb;
267    configure_mtu_callback              configure_mtu_cb;
268    scan_filter_cfg_callback            scan_filter_cfg_cb;
269    scan_filter_param_callback          scan_filter_param_cb;
270    scan_filter_status_callback         scan_filter_status_cb;
271    multi_adv_enable_callback           multi_adv_enable_cb;
272    multi_adv_update_callback           multi_adv_update_cb;
273    multi_adv_data_callback             multi_adv_data_cb;
274    multi_adv_disable_callback          multi_adv_disable_cb;
275    congestion_callback                 congestion_cb;
276    batchscan_cfg_storage_callback      batchscan_cfg_storage_cb;
277    batchscan_enable_disable_callback   batchscan_enb_disable_cb;
278    batchscan_reports_callback          batchscan_reports_cb;
279    batchscan_threshold_callback        batchscan_threshold_cb;
280    track_adv_event_callback            track_adv_event_cb;
281    scan_parameter_setup_completed_callback scan_parameter_setup_completed_cb;
282} btgatt_client_callbacks_t;
283
284/** Represents the standard BT-GATT client interface. */
285
286typedef struct {
287    /** Registers a GATT client application with the stack */
288    bt_status_t (*register_client)( bt_uuid_t *uuid );
289
290    /** Unregister a client application from the stack */
291    bt_status_t (*unregister_client)(int client_if );
292
293    /** Start or stop LE device scanning */
294    bt_status_t (*scan)( bool start );
295
296    /** Create a connection to a remote LE or dual-mode device */
297    bt_status_t (*connect)( int client_if, const bt_bdaddr_t *bd_addr,
298                         bool is_direct, int transport );
299
300    /** Disconnect a remote device or cancel a pending connection */
301    bt_status_t (*disconnect)( int client_if, const bt_bdaddr_t *bd_addr,
302                    int conn_id);
303
304    /** Start or stop advertisements to listen for incoming connections */
305    bt_status_t (*listen)(int client_if, bool start);
306
307    /** Clear the attribute cache for a given device */
308    bt_status_t (*refresh)( int client_if, const bt_bdaddr_t *bd_addr );
309
310    /**
311     * Enumerate all GATT services on a connected device.
312     * Optionally, the results can be filtered for a given UUID.
313     */
314    bt_status_t (*search_service)(int conn_id, bt_uuid_t *filter_uuid );
315
316    /**
317     * Enumerate included services for a given service.
318     * Set start_incl_srvc_id to NULL to get the first included service.
319     */
320    bt_status_t (*get_included_service)( int conn_id, btgatt_srvc_id_t *srvc_id,
321                                         btgatt_srvc_id_t *start_incl_srvc_id);
322
323    /**
324     * Enumerate characteristics for a given service.
325     * Set start_char_id to NULL to get the first characteristic.
326     */
327    bt_status_t (*get_characteristic)( int conn_id,
328                    btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *start_char_id);
329
330    /**
331     * Enumerate descriptors for a given characteristic.
332     * Set start_descr_id to NULL to get the first descriptor.
333     */
334    bt_status_t (*get_descriptor)( int conn_id,
335                    btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
336                    btgatt_gatt_id_t *start_descr_id);
337
338    /** Read a characteristic on a remote device */
339    bt_status_t (*read_characteristic)( int conn_id,
340                    btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
341                    int auth_req );
342
343    /** Write a remote characteristic */
344    bt_status_t (*write_characteristic)(int conn_id,
345                    btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
346                    int write_type, int len, int auth_req,
347                    char* p_value);
348
349    /** Read the descriptor for a given characteristic */
350    bt_status_t (*read_descriptor)(int conn_id,
351                    btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
352                    btgatt_gatt_id_t *descr_id, int auth_req);
353
354    /** Write a remote descriptor for a given characteristic */
355    bt_status_t (*write_descriptor)( int conn_id,
356                    btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id,
357                    btgatt_gatt_id_t *descr_id, int write_type, int len,
358                    int auth_req, char* p_value);
359
360    /** Execute a prepared write operation */
361    bt_status_t (*execute_write)(int conn_id, int execute);
362
363    /**
364     * Register to receive notifications or indications for a given
365     * characteristic
366     */
367    bt_status_t (*register_for_notification)( int client_if,
368                    const bt_bdaddr_t *bd_addr, btgatt_srvc_id_t *srvc_id,
369                    btgatt_gatt_id_t *char_id);
370
371    /** Deregister a previous request for notifications/indications */
372    bt_status_t (*deregister_for_notification)( int client_if,
373                    const bt_bdaddr_t *bd_addr, btgatt_srvc_id_t *srvc_id,
374                    btgatt_gatt_id_t *char_id);
375
376    /** Request RSSI for a given remote device */
377    bt_status_t (*read_remote_rssi)( int client_if, const bt_bdaddr_t *bd_addr);
378
379    /** Setup scan filter params */
380    bt_status_t (*scan_filter_param_setup)(btgatt_filt_param_setup_t filt_param);
381
382
383    /** Configure a scan filter condition  */
384    bt_status_t (*scan_filter_add_remove)(int client_if, int action, int filt_type,
385                                   int filt_index, int company_id,
386                                   int company_id_mask, const bt_uuid_t *p_uuid,
387                                   const bt_uuid_t *p_uuid_mask, const bt_bdaddr_t *bd_addr,
388                                   char addr_type, int data_len, char* p_data, int mask_len,
389                                   char* p_mask);
390
391    /** Clear all scan filter conditions for specific filter index*/
392    bt_status_t (*scan_filter_clear)(int client_if, int filt_index);
393
394    /** Enable / disable scan filter feature*/
395    bt_status_t (*scan_filter_enable)(int client_if, bool enable);
396
397    /** Determine the type of the remote device (LE, BR/EDR, Dual-mode) */
398    int (*get_device_type)( const bt_bdaddr_t *bd_addr );
399
400    /** Set the advertising data or scan response data */
401    bt_status_t (*set_adv_data)(int client_if, bool set_scan_rsp, bool include_name,
402                    bool include_txpower, int min_interval, int max_interval, int appearance,
403                    uint16_t manufacturer_len, char* manufacturer_data,
404                    uint16_t service_data_len, char* service_data,
405                    uint16_t service_uuid_len, char* service_uuid);
406
407    /** Configure the MTU for a given connection */
408    bt_status_t (*configure_mtu)(int conn_id, int mtu);
409
410    /** Request a connection parameter update */
411    bt_status_t (*conn_parameter_update)(const bt_bdaddr_t *bd_addr, int min_interval,
412                    int max_interval, int latency, int timeout);
413
414    /** Sets the LE scan interval and window in units of N*0.625 msec */
415    bt_status_t (*set_scan_parameters)(int client_if, int scan_interval, int scan_window);
416
417    /* Setup the parameters as per spec, user manual specified values and enable multi ADV */
418    bt_status_t (*multi_adv_enable)(int client_if, int min_interval,int max_interval,int adv_type,
419                 int chnl_map, int tx_power, int timeout_s);
420
421    /* Update the parameters as per spec, user manual specified values and restart multi ADV */
422    bt_status_t (*multi_adv_update)(int client_if, int min_interval,int max_interval,int adv_type,
423                 int chnl_map, int tx_power, int timeout_s);
424
425    /* Setup the data for the specified instance */
426    bt_status_t (*multi_adv_set_inst_data)(int client_if, bool set_scan_rsp, bool include_name,
427                    bool incl_txpower, int appearance, int manufacturer_len,
428                    char* manufacturer_data, int service_data_len,
429                    char* service_data, int service_uuid_len, char* service_uuid);
430
431    /* Disable the multi adv instance */
432    bt_status_t (*multi_adv_disable)(int client_if);
433
434    /* Configure the batchscan storage */
435    bt_status_t (*batchscan_cfg_storage)(int client_if, int batch_scan_full_max,
436        int batch_scan_trunc_max, int batch_scan_notify_threshold);
437
438    /* Enable batchscan */
439    bt_status_t (*batchscan_enb_batch_scan)(int client_if, int scan_mode,
440        int scan_interval, int scan_window, int addr_type, int discard_rule);
441
442    /* Disable batchscan */
443    bt_status_t (*batchscan_dis_batch_scan)(int client_if);
444
445    /* Read out batchscan reports */
446    bt_status_t (*batchscan_read_reports)(int client_if, int scan_mode);
447
448    /** Test mode interface */
449    bt_status_t (*test_command)( int command, btgatt_test_params_t* params);
450
451} btgatt_client_interface_t;
452
453__END_DECLS
454
455#endif /* ANDROID_INCLUDE_BT_GATT_CLIENT_H */
456