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