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