1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 /** 39 * @title ATT Database Engine 40 * 41 */ 42 43 #ifndef ATT_DB_H 44 #define ATT_DB_H 45 46 #include <stdint.h> 47 #include "bluetooth.h" 48 #include "btstack_linked_list.h" 49 #include "btstack_defines.h" 50 #include "btstack_bool.h" 51 52 #if defined __cplusplus 53 extern "C" { 54 #endif 55 56 // MARK: Attribute PDU Opcodes 57 #define ATT_ERROR_RESPONSE 0x01u 58 59 #define ATT_EXCHANGE_MTU_REQUEST 0x02u 60 #define ATT_EXCHANGE_MTU_RESPONSE 0x03u 61 62 #define ATT_FIND_INFORMATION_REQUEST 0x04u 63 #define ATT_FIND_INFORMATION_REPLY 0x05u 64 #define ATT_FIND_BY_TYPE_VALUE_REQUEST 0x06u 65 #define ATT_FIND_BY_TYPE_VALUE_RESPONSE 0x07u 66 67 #define ATT_READ_BY_TYPE_REQUEST 0x08u 68 #define ATT_READ_BY_TYPE_RESPONSE 0x09u 69 #define ATT_READ_REQUEST 0x0au 70 #define ATT_READ_RESPONSE 0x0bu 71 #define ATT_READ_BLOB_REQUEST 0x0cu 72 #define ATT_READ_BLOB_RESPONSE 0x0du 73 #define ATT_READ_MULTIPLE_REQUEST 0x0eu 74 #define ATT_READ_MULTIPLE_RESPONSE 0x0fu 75 #define ATT_READ_BY_GROUP_TYPE_REQUEST 0x10u 76 #define ATT_READ_BY_GROUP_TYPE_RESPONSE 0x11u 77 78 #define ATT_WRITE_REQUEST 0x12u 79 #define ATT_WRITE_RESPONSE 0x13u 80 81 #define ATT_PREPARE_WRITE_REQUEST 0x16u 82 #define ATT_PREPARE_WRITE_RESPONSE 0x17u 83 #define ATT_EXECUTE_WRITE_REQUEST 0x18u 84 #define ATT_EXECUTE_WRITE_RESPONSE 0x19u 85 86 #define ATT_HANDLE_VALUE_NOTIFICATION 0x1bu 87 #define ATT_HANDLE_VALUE_INDICATION 0x1du 88 #define ATT_HANDLE_VALUE_CONFIRMATION 0x1eu 89 90 #define ATT_READ_MULTIPLE_VARIABLE_REQ 0x20u 91 #define ATT_READ_MULTIPLE_VARIABLE_RSP 0x21u 92 #define ATT_MULTIPLE_HANDLE_VALUE_NTF 0x23u 93 94 #define ATT_WRITE_COMMAND 0x52u 95 #define ATT_SIGNED_WRITE_COMMAND 0xD2u 96 97 98 // internal additions 99 // 128 bit UUID used 100 #define ATT_PROPERTY_UUID128 0x200u 101 // Read/Write Permission bits 102 #define ATT_PROPERTY_READ_PERMISSION_BIT_0 0x0400u 103 #define ATT_PROPERTY_READ_PERMISSION_BIT_1 0x0800u 104 #define ATT_PROPERTY_WRITE_PERMISSION_BIT_0 0x0001u 105 #define ATT_PROPERTY_WRITE_PERMISSION_BIT_1 0x0010u 106 #define ATT_PROPERTY_READ_PERMISSION_SC 0x0020u 107 #define ATT_PROPERTY_WRITE_PERMISSION_SC 0x0080u 108 109 110 typedef struct att_connection { 111 hci_con_handle_t con_handle; 112 uint16_t mtu; // initialized to ATT_DEFAULT_MTU (23), negotiated during MTU exchange 113 uint16_t max_mtu; // local maximal L2CAP_MTU, set to l2cap_max_le_mtu() 114 bool mtu_exchanged; 115 uint8_t encryption_key_size; 116 uint8_t authenticated; 117 uint8_t authorized; 118 uint8_t secure_connection; 119 } att_connection_t; 120 121 /* API_START */ 122 123 // map ATT ERROR CODES on to att_read_callback length 124 #define ATT_READ_ERROR_CODE_OFFSET 0xfe00u 125 126 // custom BTstack ATT Response Pending for att_read_callback 127 #define ATT_READ_RESPONSE_PENDING 0xffffu 128 129 // internally used to signal write response pending 130 // To ask ATT Server to defer the write response, you can return ATT_ERROR_WRITE_RESPONSE_PENDING in your att_write_callback 131 #define ATT_INTERNAL_WRITE_RESPONSE_PENDING 0xfffeu 132 133 /** 134 * @brief ATT Client Read Callback for Dynamic Data 135 * - if buffer == NULL, don't copy data, just return size of value 136 * - if buffer != NULL, copy data and return number bytes copied 137 * 138 * If ENABLE_ATT_DELAYED_READ_RESPONSE is defined, you may return ATT_READ_RESPONSE_PENDING if data isn't available yet 139 * and call att_server_response_ready to re-trigger the callback. 140 * 141 * @param con_handle of hci le connection 142 * @param attribute_handle to be read 143 * @param offset defines start of attribute value 144 * @param buffer 145 * @param buffer_size 146 * @return size of value if buffer is NULL, otherwise number of bytes copied 147 */ 148 typedef uint16_t (*att_read_callback_t)(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size); 149 150 /** 151 * @brief ATT Client Write Callback for Dynamic Data 152 * Each Prepared Write Request triggers a callback with transaction mode ATT_TRANSACTION_MODE_ACTIVE. 153 * On Execute Write, the callback will be called with ATT_TRANSACTION_MODE_VALIDATE and allows to validate all queued writes and return an application error. 154 * If none of the registered callbacks return an error for ATT_TRANSACTION_MODE_VALIDATE and the callback will be called with ATT_TRANSACTION_MODE_EXECUTE. 155 * Otherwise, all callbacks will be called with ATT_TRANSACTION_MODE_CANCEL. 156 * 157 * If the additional validation step is not needed, just return 0 for all callbacks with transaction mode ATT_TRANSACTION_MODE_VALIDATE. 158 * 159 * If ENABLE_ATT_DELAYED_READ_RESPONSE is defined, you may return ATT_ERROR_WRITE_RESPONSE_PENDING if data isn't available yet 160 * and call att_server_response_ready to re-trigger the callback. 161 * 162 * @param con_handle of hci le connection 163 * @param attribute_handle to be written 164 * @param transaction - ATT_TRANSACTION_MODE_NONE for regular writes. For prepared writes: ATT_TRANSACTION_MODE_ACTIVE, ATT_TRANSACTION_MODE_VALIDATE, ATT_TRANSACTION_MODE_EXECUTE, ATT_TRANSACTION_MODE_CANCEL 165 * @param offset into the value - used for queued writes and long attributes 166 * @param buffer 167 * @param buffer_size 168 * @param signature used for signed write commmands 169 * @return 0 if write was ok, ATT_ERROR_PREPARE_QUEUE_FULL if no space in queue, ATT_ERROR_INVALID_OFFSET if offset is larger than max buffer 170 */ 171 typedef int (*att_write_callback_t)(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size); 172 173 // Read & Write Callbacks for handle range 174 typedef struct att_service_handler { 175 btstack_linked_item_t * item; 176 att_read_callback_t read_callback; 177 att_write_callback_t write_callback; 178 btstack_packet_handler_t packet_handler; 179 uint16_t start_handle; 180 uint16_t end_handle; 181 uint8_t flags; 182 } att_service_handler_t; 183 184 // MARK: ATT Operations 185 186 /** 187 * @brief setup ATT database 188 * @param db 189 */ 190 void att_set_db(uint8_t const * db); 191 192 /* 193 * @brief set callback for read of dynamic attributes 194 * @param callback 195 */ 196 void att_set_read_callback(att_read_callback_t callback); 197 198 /** 199 * @brief set callback for write of dynamic attributes 200 * @param callback 201 */ 202 void att_set_write_callback(att_write_callback_t callback); 203 204 /** 205 * @brief debug helper, dump ATT database to stdout using log_info 206 */ 207 void att_dump_attributes(void); 208 209 /** 210 * @brief process ATT request against database and put response into response buffer 211 * @param att_connection used for mtu and security properties 212 * @param request_buffer, request_len: ATT request from clinet 213 * @param response_buffer for result 214 * @return len of data in response buffer. 0 = no response, 215 * ATT_READ_RESPONSE_PENDING if it was returned at least once for dynamic data (requires ENABLE_ATT_DELAYED_READ_RESPONSE) 216 */ 217 uint16_t att_handle_request(att_connection_t * att_connection, 218 uint8_t * request_buffer, 219 uint16_t request_len, 220 uint8_t * response_buffer); 221 222 /** 223 * @brief setup value notification in response buffer for a given handle and value 224 * @param att_connection 225 * @param attribute_handle 226 * @param value 227 * @param value_len 228 * @param response_buffer for notification 229 */ 230 uint16_t att_prepare_handle_value_notification(att_connection_t * att_connection, 231 uint16_t attribute_handle, 232 const uint8_t *value, 233 uint16_t value_len, 234 uint8_t * response_buffer); 235 236 /** 237 * @brief setup value notification in response buffer for multiple handles and values 238 * @param att_connection 239 * @param attribute_handle 240 * @param value 241 * @param value_len 242 * @param response_buffer for notification 243 */ 244 uint16_t att_prepare_handle_value_multiple_notification(att_connection_t * att_connection, 245 uint8_t num_attributes, 246 const uint16_t * attribute_handles, 247 const uint8_t ** values_data, 248 const uint16_t * values_len, 249 uint8_t * response_buffer); 250 251 /** 252 * @brief setup value indication in response buffer for a given handle and value 253 * @param att_connection 254 * @param attribute_handle 255 * @param value 256 * @param value_len 257 * @param response_buffer for indication 258 */ 259 uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection, 260 uint16_t attribute_handle, 261 const uint8_t *value, 262 uint16_t value_len, 263 uint8_t * response_buffer); 264 265 /** 266 * @brief transcation queue of prepared writes, e.g., after disconnect 267 * @return att_connection 268 */ 269 void att_clear_transaction_queue(att_connection_t * att_connection); 270 271 // att_read_callback helpers for a various data types 272 273 /** 274 * @brief Handle read of blob like data for att_read_callback 275 * @param blob of data 276 * @param blob_size of blob 277 * @param offset from att_read_callback 278 * @param buffer from att_read_callback 279 * @param buffer_size from att_read_callback 280 * @return value size for buffer == 0 and num bytes copied otherwise 281 */ 282 uint16_t att_read_callback_handle_blob(const uint8_t * blob, uint16_t blob_size, uint16_t offset, uint8_t * buffer, uint16_t buffer_size); 283 284 /** 285 * @brief Handle read of little endian unsigned 32 bit value for att_read_callback 286 * @param value 287 * @param offset from att_read_callback 288 * @param buffer from att_read_callback 289 * @param buffer_size from att_read_callback 290 * @return value size for buffer == 0 and num bytes copied otherwise 291 */ 292 uint16_t att_read_callback_handle_little_endian_32(uint32_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size); 293 294 /** 295 * @brief Handle read of little endian unsigned 16 bit value for att_read_callback 296 * @param value 297 * @param offset from att_read_callback 298 * @param buffer from att_read_callback 299 * @param buffer_size from att_read_callback 300 * @return value size for buffer == 0 and num bytes copied otherwise 301 */ 302 uint16_t att_read_callback_handle_little_endian_16(uint16_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size); 303 304 /** 305 * @brief Handle read of single byte for att_read_callback 306 * @param blob of data 307 * @param blob_size of blob 308 * @param offset from att_read_callback 309 * @param buffer from att_read_callback 310 * @param buffer_size from att_read_callback 311 * @return value size for buffer == 0 and num bytes copied otherwise 312 */ 313 uint16_t att_read_callback_handle_byte(uint8_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size); 314 315 316 // experimental client API 317 /** 318 * @brief Get UUID for handle 319 * @param attribute_handle 320 * @return 0 if not found 321 */ 322 uint16_t att_uuid_for_handle(uint16_t attribute_handle); 323 324 /** 325 * @brief Get const value for handle 326 * @param attribute_handle 327 * @param out_value_len output variable that hold value len 328 * @return value 329 */ 330 331 const uint8_t * gatt_server_get_const_value_for_handle(uint16_t attribute_handle, uint16_t * out_value_len); 332 333 // experimental GATT Server API 334 335 /** 336 * @brief Get handle range for primary service. 337 * @param uuid16 338 * @param start_handle 339 * @param end_handle 340 * @return false if not found 341 */ 342 bool gatt_server_get_handle_range_for_service_with_uuid16(uint16_t uuid16, uint16_t * start_handle, uint16_t * end_handle); 343 344 /** 345 * @brief Get handle range for included service. 346 * @param start_handle 347 * @param end_handle 348 * @param uuid16 349 * @param out_included_service_handle 350 * @param out_included_service_start_handle 351 * @param out_included_service_end_handle 352 * @return false if not found 353 */ 354 bool gatt_server_get_included_service_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16, 355 uint16_t * out_included_service_handle, uint16_t * out_included_service_start_handle, uint16_t * out_included_service_end_handle); 356 357 /** 358 * @brief Get value handle for characteristic. 359 * @param start_handle 360 * @param end_handle 361 * @param uuid16 362 * @return 0 if not found 363 */ 364 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16); 365 366 /** 367 * @brief Get descriptor handle for characteristic. 368 * @param start_handle 369 * @param end_handle 370 * @param characteristic_uuid16 371 * @param descriptor_uuid16 372 * @return 0 if not found 373 */ 374 uint16_t gatt_server_get_descriptor_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16, uint16_t descriptor_uuid16); 375 376 /** 377 * @brief Get client configuration handle for characteristic. 378 * @param start_handle 379 * @param end_handle 380 * @param characteristic_uuid16 381 * @return 0 if not found 382 */ 383 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16); 384 385 /** 386 * @brief Get server configuration handle for characteristic. 387 * @param start_handle 388 * @param end_handle 389 * @param characteristic_uuid16 390 * @param descriptor_uuid16 391 * @return 0 if not found 392 */ 393 uint16_t gatt_server_get_server_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16); 394 395 396 /** 397 * @brief Get handle range for primary service. 398 * @param uuid128 399 * @param start_handle 400 * @param end_handle 401 * @return false if not found 402 */ 403 bool gatt_server_get_handle_range_for_service_with_uuid128(const uint8_t * uuid128, uint16_t * start_handle, uint16_t * end_handle); 404 405 /** 406 * @brief Get value handle. 407 * @param start_handle 408 * @param end_handle 409 * @param uuid128 410 * @return 0 if not found 411 */ 412 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128); 413 414 /** 415 * @brief Get client configuration handle. 416 * @param start_handle 417 * @param end_handle 418 * @param uuid128 419 * @return 0 if not found 420 */ 421 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128); 422 423 /* API_END */ 424 425 // non-user functionality for att_server 426 427 /** 428 * @brief Check if writes to handle should be persistent 429 * @param handle 430 * @return 1 if persistent 431 */ 432 bool att_is_persistent_ccc(uint16_t handle); 433 434 435 436 // auto-pts testing, returns response size 437 #ifdef ENABLE_BTP 438 uint16_t btp_att_get_attributes_by_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16, uint8_t * response_buffer, uint16_t response_buffer_size); 439 uint16_t btp_att_get_attributes_by_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128, uint8_t * response_buffer, uint16_t response_buffer_size); 440 uint16_t btp_att_get_attribute_value(att_connection_t * att_connection, uint16_t attribute_handle, uint8_t * response_buffer, uint16_t response_buffer_size); 441 #endif 442 443 #if defined __cplusplus 444 } 445 #endif 446 447 #endif // ATT_H 448