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