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