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 MATTHIAS 24 * RINGWALD 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 #ifndef ATT_DB_H 40 #define ATT_DB_H 41 42 #include <stdint.h> 43 #include "bluetooth.h" 44 #include "btstack_linked_list.h" 45 #include "btstack_defines.h" 46 #include "btstack_bool.h" 47 48 #if defined __cplusplus 49 extern "C" { 50 #endif 51 52 // MARK: Attribute PDU Opcodes 53 #define ATT_ERROR_RESPONSE 0x01 54 55 #define ATT_EXCHANGE_MTU_REQUEST 0x02 56 #define ATT_EXCHANGE_MTU_RESPONSE 0x03 57 58 #define ATT_FIND_INFORMATION_REQUEST 0x04 59 #define ATT_FIND_INFORMATION_REPLY 0x05 60 #define ATT_FIND_BY_TYPE_VALUE_REQUEST 0x06 61 #define ATT_FIND_BY_TYPE_VALUE_RESPONSE 0x07 62 63 #define ATT_READ_BY_TYPE_REQUEST 0x08 64 #define ATT_READ_BY_TYPE_RESPONSE 0x09 65 #define ATT_READ_REQUEST 0x0a 66 #define ATT_READ_RESPONSE 0x0b 67 #define ATT_READ_BLOB_REQUEST 0x0c 68 #define ATT_READ_BLOB_RESPONSE 0x0d 69 #define ATT_READ_MULTIPLE_REQUEST 0x0e 70 #define ATT_READ_MULTIPLE_RESPONSE 0x0f 71 #define ATT_READ_BY_GROUP_TYPE_REQUEST 0x10 72 #define ATT_READ_BY_GROUP_TYPE_RESPONSE 0x11 73 74 #define ATT_WRITE_REQUEST 0x12 75 #define ATT_WRITE_RESPONSE 0x13 76 77 #define ATT_PREPARE_WRITE_REQUEST 0x16 78 #define ATT_PREPARE_WRITE_RESPONSE 0x17 79 #define ATT_EXECUTE_WRITE_REQUEST 0x18 80 #define ATT_EXECUTE_WRITE_RESPONSE 0x19 81 82 #define ATT_HANDLE_VALUE_NOTIFICATION 0x1b 83 #define ATT_HANDLE_VALUE_INDICATION 0x1d 84 #define ATT_HANDLE_VALUE_CONFIRMATION 0x1e 85 86 #define ATT_READ_MULTIPLE_VARIABLE_REQ 0x20 87 #define ATT_READ_MULTIPLE_VARIABLE_RSP 0x21 88 #define ATT_MULTIPLE_HANDLE_VALUE_NTF 0x23 89 90 #define ATT_WRITE_COMMAND 0x52 91 #define ATT_SIGNED_WRITE_COMMAND 0xD2 92 93 // map ATT ERROR CODES on to att_read_callback length 94 #define ATT_READ_ERROR_CODE_OFFSET 0xfe00 95 96 // custom BTstack ATT Response Pending for att_read_callback 97 #define ATT_READ_RESPONSE_PENDING 0xffff 98 99 // internally used to signal write response pending 100 #define ATT_INTERNAL_WRITE_RESPONSE_PENDING 0xfffe 101 102 // internal additions 103 // 128 bit UUID used 104 #define ATT_PROPERTY_UUID128 0x200 105 // Read/Write Permission bits 106 #define ATT_PROPERTY_READ_PERMISSION_BIT_0 0x0400 107 #define ATT_PROPERTY_READ_PERMISSION_BIT_1 0x0800 108 #define ATT_PROPERTY_WRITE_PERMISSION_BIT_0 0x0001 109 #define ATT_PROPERTY_WRITE_PERMISSION_BIT_1 0x0010 110 #define ATT_PROPERTY_READ_PERMISSION_SC 0x0020 111 #define ATT_PROPERTY_WRITE_PERMISSION_SC 0x0080 112 113 114 typedef struct att_connection { 115 hci_con_handle_t con_handle; 116 uint16_t mtu; // initialized to ATT_DEFAULT_MTU (23), negotiated during MTU exchange 117 uint16_t max_mtu; // local maximal L2CAP_MTU, set to l2cap_max_le_mtu() 118 bool mtu_exchanged; 119 uint8_t encryption_key_size; 120 uint8_t authenticated; 121 uint8_t authorized; 122 uint8_t secure_connection; 123 } att_connection_t; 124 125 // ATT Client Read Callback for Dynamic Data 126 // - if buffer == NULL, don't copy data, just return size of value 127 // - if buffer != NULL, copy data and return number bytes copied 128 // If ENABLE_ATT_DELAYED_READ_RESPONSE is defined, you may return ATT_READ_RESPONSE_PENDING if data isn't available yet 129 // @param con_handle of hci le connection 130 // @param attribute_handle to be read 131 // @param offset defines start of attribute value 132 // @param buffer 133 // @param buffer_size 134 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); 135 136 // ATT Client Write Callback for Dynamic Data 137 // @param con_handle of hci le connection 138 // @param attribute_handle to be written 139 // @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 140 // @param offset into the value - used for queued writes and long attributes 141 // @param buffer 142 // @param buffer_size 143 // @param signature used for signed write commmands 144 // @returns 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 145 // 146 // Each Prepared Write Request triggers a callback with transaction mode ATT_TRANSACTION_MODE_ACTIVE. 147 // 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. 148 // 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. 149 // Otherwise, all callbacks will be called with ATT_TRANSACTION_MODE_CANCEL. 150 // 151 // If the additional validation step is not needed, just return 0 for all callbacks with transaction mode ATT_TRANSACTION_MODE_VALIDATE. 152 // 153 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); 154 155 // Read & Write Callbacks for handle range 156 typedef struct att_service_handler { 157 btstack_linked_item_t * item; 158 uint16_t start_handle; 159 uint16_t end_handle; 160 att_read_callback_t read_callback; 161 att_write_callback_t write_callback; 162 btstack_packet_handler_t packet_handler; 163 } att_service_handler_t; 164 165 // MARK: ATT Operations 166 167 /* 168 * @brief setup ATT database 169 */ 170 void att_set_db(uint8_t const * db); 171 172 /* 173 * @brief set callback for read of dynamic attributes 174 * @param callback 175 */ 176 void att_set_read_callback(att_read_callback_t callback); 177 178 /* 179 * @brief set callback for write of dynamic attributes 180 * @param callback 181 */ 182 void att_set_write_callback(att_write_callback_t callback); 183 184 /* 185 * @brief debug helper, dump ATT database to stdout using log_info 186 */ 187 void att_dump_attributes(void); 188 189 /* 190 * @brief process ATT request against database and put response into response buffer 191 * @param att_connection used for mtu and security properties 192 * @param request_buffer, request_len: ATT request from clinet 193 * @param response_buffer for result 194 * @returns len of data in response buffer. 0 = no response, 195 * ATT_READ_RESPONSE_PENDING if it was returned at least once for dynamic data (requires ENABLE_ATT_DELAYED_READ_RESPONSE) 196 */ 197 uint16_t att_handle_request(att_connection_t * att_connection, 198 uint8_t * request_buffer, 199 uint16_t request_len, 200 uint8_t * response_buffer); 201 202 /* 203 * @brief setup value notification in response buffer for a given handle and value 204 * @param att_connection 205 * @param attribute_handle 206 * @param value 207 * @param value_len 208 * @param response_buffer for notification 209 */ 210 uint16_t att_prepare_handle_value_notification(att_connection_t * att_connection, 211 uint16_t attribute_handle, 212 const uint8_t *value, 213 uint16_t value_len, 214 uint8_t * response_buffer); 215 216 /* 217 * @brief setup value indication in response buffer for a given handle and value 218 * @param att_connection 219 * @param attribute_handle 220 * @param value 221 * @param value_len 222 * @param response_buffer for indication 223 */ 224 uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection, 225 uint16_t attribute_handle, 226 const uint8_t *value, 227 uint16_t value_len, 228 uint8_t * response_buffer); 229 230 /* 231 * @brief transcation queue of prepared writes, e.g., after disconnect 232 */ 233 void att_clear_transaction_queue(att_connection_t * att_connection); 234 235 // att_read_callback helpers for a various data types 236 237 /* 238 * @brief Handle read of blob like data for att_read_callback 239 * @param blob of data 240 * @param blob_size of blob 241 * @param offset from att_read_callback 242 * @param buffer from att_read_callback 243 * @param buffer_size from att_read_callback 244 * @returns value size for buffer == 0 and num bytes copied otherwise 245 */ 246 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); 247 248 /* 249 * @brief Handle read of little endian unsigned 32 bit value for att_read_callback 250 * @param value 251 * @param offset from att_read_callback 252 * @param buffer from att_read_callback 253 * @param buffer_size from att_read_callback 254 * @returns value size for buffer == 0 and num bytes copied otherwise 255 */ 256 uint16_t att_read_callback_handle_little_endian_32(uint32_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size); 257 258 /* 259 * @brief Handle read of little endian unsigned 16 bit value for att_read_callback 260 * @param value 261 * @param offset from att_read_callback 262 * @param buffer from att_read_callback 263 * @param buffer_size from att_read_callback 264 * @returns value size for buffer == 0 and num bytes copied otherwise 265 */ 266 uint16_t att_read_callback_handle_little_endian_16(uint16_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size); 267 268 /* 269 * @brief Handle read of single byte for att_read_callback 270 * @param blob of data 271 * @param blob_size of blob 272 * @param offset from att_read_callback 273 * @param buffer from att_read_callback 274 * @param buffer_size from att_read_callback 275 * @returns value size for buffer == 0 and num bytes copied otherwise 276 */ 277 uint16_t att_read_callback_handle_byte(uint8_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size); 278 279 280 // experimental client API 281 uint16_t att_uuid_for_handle(uint16_t attribute_handle); 282 283 284 // experimental GATT Server API 285 286 // returns true if service found. only primary service. 287 bool gatt_server_get_get_handle_range_for_service_with_uuid16(uint16_t uuid16, uint16_t * start_handle, uint16_t * end_handle); 288 289 // returns 0 if not found 290 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16); 291 292 // returns 0 if not found 293 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); 294 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16); 295 uint16_t gatt_server_get_server_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16); 296 297 298 // returns 1 if service found. only primary service. 299 int gatt_server_get_get_handle_range_for_service_with_uuid128(const uint8_t * uuid128, uint16_t * start_handle, uint16_t * end_handle); 300 301 // returns 0 if not found 302 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128); 303 304 // returns 0 if not found 305 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128); 306 307 // non-user functionality for att_server 308 309 /* 310 * @brief Check if writes to handle should be persistent 311 * @param handle 312 * @returns 1 if persistent 313 */ 314 bool att_is_persistent_ccc(uint16_t handle); 315 316 // auto-pts testing, returns response size 317 #ifdef ENABLE_BTP 318 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); 319 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); 320 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); 321 #endif 322 323 #if defined __cplusplus 324 } 325 #endif 326 327 #endif // ATT_H 328