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