• 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 /** Callback invoked when the subrate change event for a given connection
190  * is received */
191 typedef void (*subrate_change_callback)(int conn_id, uint16_t subrate_factor,
192                                         uint16_t latency, uint16_t cont_num,
193                                         uint16_t timeout, uint8_t status);
194 
195 typedef struct {
196   register_client_callback register_client_cb;
197   connect_callback open_cb;
198   disconnect_callback close_cb;
199   search_complete_callback search_complete_cb;
200   register_for_notification_callback register_for_notification_cb;
201   notify_callback notify_cb;
202   read_characteristic_callback read_characteristic_cb;
203   write_characteristic_callback write_characteristic_cb;
204   read_descriptor_callback read_descriptor_cb;
205   write_descriptor_callback write_descriptor_cb;
206   execute_write_callback execute_write_cb;
207   read_remote_rssi_callback read_remote_rssi_cb;
208   configure_mtu_callback configure_mtu_cb;
209   congestion_callback congestion_cb;
210   get_gatt_db_callback get_gatt_db_cb;
211   services_removed_callback services_removed_cb;
212   services_added_callback services_added_cb;
213   phy_updated_callback phy_updated_cb;
214   conn_updated_callback conn_updated_cb;
215   service_changed_callback service_changed_cb;
216   subrate_change_callback subrate_chg_cb;
217 } btgatt_client_callbacks_t;
218 
219 /** Represents the standard BT-GATT client interface. */
220 
221 typedef struct {
222   /** Registers a GATT client application with the stack */
223   bt_status_t (*register_client)(const bluetooth::Uuid& uuid,
224                                  bool eatt_support);
225 
226   /** Unregister a client application from the stack */
227   bt_status_t (*unregister_client)(int client_if);
228 
229   /** Create a connection to a remote LE or dual-mode device */
230   bt_status_t (*connect)(int client_if, const RawAddress& bd_addr,
231                          uint8_t addr_type, bool is_direct, int transport,
232                          bool opportunistic, int initiating_phys);
233 
234   /** Disconnect a remote device or cancel a pending connection */
235   bt_status_t (*disconnect)(int client_if, const RawAddress& bd_addr,
236                             int conn_id);
237 
238   /** Clear the attribute cache for a given device */
239   bt_status_t (*refresh)(int client_if, const RawAddress& bd_addr);
240 
241   /**
242    * Enumerate all GATT services on a connected device.
243    * Optionally, the results can be filtered for a given UUID.
244    */
245   bt_status_t (*search_service)(int conn_id,
246                                 const bluetooth::Uuid* filter_uuid);
247 
248   /**
249    * Sead "Find service by UUID" request. Used only for PTS tests.
250    */
251   void (*btif_gattc_discover_service_by_uuid)(int conn_id,
252                                               const bluetooth::Uuid& uuid);
253 
254   /** Read a characteristic on a remote device */
255   bt_status_t (*read_characteristic)(int conn_id, uint16_t handle,
256                                      int auth_req);
257 
258   /** Read a characteristic on a remote device */
259   bt_status_t (*read_using_characteristic_uuid)(int conn_id,
260                                                 const bluetooth::Uuid& uuid,
261                                                 uint16_t s_handle,
262                                                 uint16_t e_handle,
263                                                 int auth_req);
264 
265   /** Write a remote characteristic */
266   bt_status_t (*write_characteristic)(int conn_id, uint16_t handle,
267                                       int write_type, int auth_req,
268                                       const uint8_t* value, size_t length);
269 
270   /** Read the descriptor for a given characteristic */
271   bt_status_t (*read_descriptor)(int conn_id, uint16_t handle, int auth_req);
272 
273   /** Write a remote descriptor for a given characteristic */
274   bt_status_t (*write_descriptor)(int conn_id, uint16_t handle, int auth_req,
275                                   const uint8_t* value, size_t length);
276 
277   /** Execute a prepared write operation */
278   bt_status_t (*execute_write)(int conn_id, int execute);
279 
280   /**
281    * Register to receive notifications or indications for a given
282    * characteristic
283    */
284   bt_status_t (*register_for_notification)(int client_if,
285                                            const RawAddress& bd_addr,
286                                            uint16_t handle);
287 
288   /** Deregister a previous request for notifications/indications */
289   bt_status_t (*deregister_for_notification)(int client_if,
290                                              const RawAddress& bd_addr,
291                                              uint16_t handle);
292 
293   /** Request RSSI for a given remote device */
294   bt_status_t (*read_remote_rssi)(int client_if, const RawAddress& bd_addr);
295 
296   /** Determine the type of the remote device (LE, BR/EDR, Dual-mode) */
297   int (*get_device_type)(const RawAddress& bd_addr);
298 
299   /** Configure the MTU for a given connection */
300   bt_status_t (*configure_mtu)(int conn_id, int mtu);
301 
302   /** Request a connection parameter update */
303   bt_status_t (*conn_parameter_update)(const RawAddress& bd_addr,
304                                        int min_interval, int max_interval,
305                                        int latency, int timeout,
306                                        uint16_t min_ce_len,
307                                        uint16_t max_ce_len);
308 
309   bt_status_t (*set_preferred_phy)(const RawAddress& bd_addr, uint8_t tx_phy,
310                                    uint8_t rx_phy, uint16_t phy_options);
311 
312   bt_status_t (*read_phy)(
313       const RawAddress& bd_addr,
314       base::Callback<void(uint8_t tx_phy, uint8_t rx_phy, uint8_t status)> cb);
315 
316   /** Test mode interface */
317   bt_status_t (*test_command)(int command, const btgatt_test_params_t& params);
318 
319   /** Get gatt db content */
320   bt_status_t (*get_gatt_db)(int conn_id);
321 
322   /** Request a BLE subrate request procedure */
323   bt_status_t (*subrate_request)(const RawAddress& bd_addr, int subrate_min,
324                                  int subrate_max, int max_latency, int cont_num,
325                                  int timeout);
326 
327 } btgatt_client_interface_t;
328 
329 __END_DECLS
330 
331 #endif /* ANDROID_INCLUDE_BT_GATT_CLIENT_H */
332