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 <stdint.h>
21 #include <vector>
22 #include "bt_common_types.h"
23 #include "bt_gatt_types.h"
24 
25 #include <bluetooth/uuid.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);
134 
135 /** GATT execute prepared write callback */
136 typedef void (*execute_write_callback)(int conn_id, int status);
137 
138 /** Callback invoked in response to read_descriptor */
139 typedef void (*read_descriptor_callback)(int conn_id, int status,
140                                          const btgatt_read_params_t& p_data);
141 
142 /** Callback invoked in response to write_descriptor */
143 typedef void (*write_descriptor_callback)(int conn_id, int status,
144                                           uint16_t handle);
145 
146 /** Callback triggered in response to read_remote_rssi */
147 typedef void (*read_remote_rssi_callback)(int client_if, const RawAddress& bda,
148                                           int rssi, int status);
149 
150 /** Callback invoked when the MTU for a given connection changes */
151 typedef void (*configure_mtu_callback)(int conn_id, int status, int mtu);
152 
153 /**
154  * Callback notifying an application that a remote device connection is
155  * currently congested and cannot receive any more data. An application should
156  * avoid sending more data until a further callback is received indicating the
157  * congestion status has been cleared.
158  */
159 typedef void (*congestion_callback)(int conn_id, bool congested);
160 
161 /** GATT get database callback */
162 typedef void (*get_gatt_db_callback)(int conn_id, const btgatt_db_element_t* db,
163                                      int count);
164 
165 /** GATT services between start_handle and end_handle were removed */
166 typedef void (*services_removed_callback)(int conn_id, uint16_t start_handle,
167                                           uint16_t end_handle);
168 
169 /** GATT services were added */
170 typedef void (*services_added_callback)(int conn_id,
171                                         const btgatt_db_element_t& added,
172                                         int added_count);
173 
174 /** Callback invoked when the PHY for a given connection changes */
175 typedef void (*phy_updated_callback)(int conn_id, uint8_t tx_phy,
176                                      uint8_t rx_phy, uint8_t status);
177 
178 /** Callback invoked when the connection parameters for a given connection
179  * changes */
180 typedef void (*conn_updated_callback)(int conn_id, uint16_t interval,
181                                       uint16_t latency, uint16_t timeout,
182                                       uint8_t status);
183 
184 typedef struct {
185   register_client_callback register_client_cb;
186   connect_callback open_cb;
187   disconnect_callback close_cb;
188   search_complete_callback search_complete_cb;
189   register_for_notification_callback register_for_notification_cb;
190   notify_callback notify_cb;
191   read_characteristic_callback read_characteristic_cb;
192   write_characteristic_callback write_characteristic_cb;
193   read_descriptor_callback read_descriptor_cb;
194   write_descriptor_callback write_descriptor_cb;
195   execute_write_callback execute_write_cb;
196   read_remote_rssi_callback read_remote_rssi_cb;
197   configure_mtu_callback configure_mtu_cb;
198   congestion_callback congestion_cb;
199   get_gatt_db_callback get_gatt_db_cb;
200   services_removed_callback services_removed_cb;
201   services_added_callback services_added_cb;
202   phy_updated_callback phy_updated_cb;
203   conn_updated_callback conn_updated_cb;
204 } btgatt_client_callbacks_t;
205 
206 /** Represents the standard BT-GATT client interface. */
207 
208 typedef struct {
209   /** Registers a GATT client application with the stack */
210   bt_status_t (*register_client)(const bluetooth::Uuid& uuid);
211 
212   /** Unregister a client application from the stack */
213   bt_status_t (*unregister_client)(int client_if);
214 
215   /** Create a connection to a remote LE or dual-mode device */
216   bt_status_t (*connect)(int client_if, const RawAddress& bd_addr,
217                          bool is_direct, int transport, bool opportunistic,
218                          int initiating_phys);
219 
220   /** Disconnect a remote device or cancel a pending connection */
221   bt_status_t (*disconnect)(int client_if, const RawAddress& bd_addr,
222                             int conn_id);
223 
224   /** Clear the attribute cache for a given device */
225   bt_status_t (*refresh)(int client_if, const RawAddress& bd_addr);
226 
227   /**
228    * Enumerate all GATT services on a connected device.
229    * Optionally, the results can be filtered for a given UUID.
230    */
231   bt_status_t (*search_service)(int conn_id,
232                                 const bluetooth::Uuid* filter_uuid);
233 
234   /**
235    * Sead "Find service by UUID" request. Used only for PTS tests.
236    */
237   void (*btif_gattc_discover_service_by_uuid)(int conn_id,
238                                               const bluetooth::Uuid& uuid);
239 
240   /** Read a characteristic on a remote device */
241   bt_status_t (*read_characteristic)(int conn_id, uint16_t handle,
242                                      int auth_req);
243 
244   /** Read a characteristic on a remote device */
245   bt_status_t (*read_using_characteristic_uuid)(int conn_id,
246                                                 const bluetooth::Uuid& uuid,
247                                                 uint16_t s_handle,
248                                                 uint16_t e_handle,
249                                                 int auth_req);
250 
251   /** Write a remote characteristic */
252   bt_status_t (*write_characteristic)(int conn_id, uint16_t handle,
253                                       int write_type, int auth_req,
254                                       std::vector<uint8_t> value);
255 
256   /** Read the descriptor for a given characteristic */
257   bt_status_t (*read_descriptor)(int conn_id, uint16_t handle, int auth_req);
258 
259   /** Write a remote descriptor for a given characteristic */
260   bt_status_t (*write_descriptor)(int conn_id, uint16_t handle, int auth_req,
261                                   std::vector<uint8_t> value);
262 
263   /** Execute a prepared write operation */
264   bt_status_t (*execute_write)(int conn_id, int execute);
265 
266   /**
267    * Register to receive notifications or indications for a given
268    * characteristic
269    */
270   bt_status_t (*register_for_notification)(int client_if,
271                                            const RawAddress& bd_addr,
272                                            uint16_t handle);
273 
274   /** Deregister a previous request for notifications/indications */
275   bt_status_t (*deregister_for_notification)(int client_if,
276                                              const RawAddress& bd_addr,
277                                              uint16_t handle);
278 
279   /** Request RSSI for a given remote device */
280   bt_status_t (*read_remote_rssi)(int client_if, const RawAddress& bd_addr);
281 
282   /** Determine the type of the remote device (LE, BR/EDR, Dual-mode) */
283   int (*get_device_type)(const RawAddress& bd_addr);
284 
285   /** Configure the MTU for a given connection */
286   bt_status_t (*configure_mtu)(int conn_id, int mtu);
287 
288   /** Request a connection parameter update */
289   bt_status_t (*conn_parameter_update)(const RawAddress& bd_addr,
290                                        int min_interval, int max_interval,
291                                        int latency, int timeout,
292                                        uint16_t min_ce_len,
293                                        uint16_t max_ce_len);
294 
295   bt_status_t (*set_preferred_phy)(const RawAddress& bd_addr, uint8_t tx_phy,
296                                    uint8_t rx_phy, uint16_t phy_options);
297 
298   bt_status_t (*read_phy)(
299       const RawAddress& bd_addr,
300       base::Callback<void(uint8_t tx_phy, uint8_t rx_phy, uint8_t status)> cb);
301 
302   /** Test mode interface */
303   bt_status_t (*test_command)(int command, const btgatt_test_params_t& params);
304 
305   /** Get gatt db content */
306   bt_status_t (*get_gatt_db)(int conn_id);
307 
308 } btgatt_client_interface_t;
309 
310 __END_DECLS
311 
312 #endif /* ANDROID_INCLUDE_BT_GATT_CLIENT_H */
313