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