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