1591423b2SMatthias Ringwald /* 2591423b2SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3591423b2SMatthias Ringwald * 4591423b2SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5591423b2SMatthias Ringwald * modification, are permitted provided that the following conditions 6591423b2SMatthias Ringwald * are met: 7591423b2SMatthias Ringwald * 8591423b2SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9591423b2SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10591423b2SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11591423b2SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12591423b2SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13591423b2SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14591423b2SMatthias Ringwald * contributors may be used to endorse or promote products derived 15591423b2SMatthias Ringwald * from this software without specific prior written permission. 16591423b2SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17591423b2SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18591423b2SMatthias Ringwald * monetary gain. 19591423b2SMatthias Ringwald * 20591423b2SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21591423b2SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22591423b2SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23591423b2SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24591423b2SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25591423b2SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26591423b2SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27591423b2SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28591423b2SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29591423b2SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30591423b2SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31591423b2SMatthias Ringwald * SUCH DAMAGE. 32591423b2SMatthias Ringwald * 33591423b2SMatthias Ringwald * Please inquire about commercial licensing options at 34591423b2SMatthias Ringwald * [email protected] 35591423b2SMatthias Ringwald * 36591423b2SMatthias Ringwald */ 37591423b2SMatthias Ringwald 38591423b2SMatthias Ringwald 39*80e33422SMatthias Ringwald #ifndef ATT_H 40*80e33422SMatthias Ringwald #define ATT_H 41591423b2SMatthias Ringwald 42591423b2SMatthias Ringwald #include <stdint.h> 43711e6c80SMatthias Ringwald #include "bluetooth.h" 448ac574d6SMatthias Ringwald #include "btstack_linked_list.h" 455d07396bSMatthias Ringwald #include "btstack_defines.h" 46591423b2SMatthias Ringwald 47591423b2SMatthias Ringwald #if defined __cplusplus 48591423b2SMatthias Ringwald extern "C" { 49591423b2SMatthias Ringwald #endif 50591423b2SMatthias Ringwald 51591423b2SMatthias Ringwald // custom BTstack error codes 52591423b2SMatthias Ringwald #define ATT_ERROR_HCI_DISCONNECT_RECEIVED 0x1f 53591423b2SMatthias Ringwald 548ac574d6SMatthias Ringwald // custom BTstack ATT error codes 55591423b2SMatthias Ringwald #define ATT_ERROR_DATA_MISMATCH 0x7e 56591423b2SMatthias Ringwald #define ATT_ERROR_TIMEOUT 0x7F 57beb20288SMatthias Ringwald #define ATT_ERROR_WRITE_RESPONSE_PENDING 0x100 58591423b2SMatthias Ringwald 59beb20288SMatthias Ringwald // custom BTstack ATT Response Pending for att_read_callback 60e404a688SMatthias Ringwald #define ATT_READ_RESPONSE_PENDING 0xffff 61e404a688SMatthias Ringwald 6256c3a347SMatthias Ringwald // internally used to signal write response pending 6356c3a347SMatthias Ringwald #define ATT_INTERNAL_WRITE_RESPONSE_PENDING 0xfffe 6456c3a347SMatthias Ringwald 65591423b2SMatthias Ringwald typedef struct att_connection { 66711e6c80SMatthias Ringwald hci_con_handle_t con_handle; 67591423b2SMatthias Ringwald uint16_t mtu; // initialized to ATT_DEFAULT_MTU (23), negotiated during MTU exchange 68591423b2SMatthias Ringwald uint16_t max_mtu; // local maximal L2CAP_MTU, set to l2cap_max_le_mtu() 69591423b2SMatthias Ringwald uint8_t encryption_key_size; 70591423b2SMatthias Ringwald uint8_t authenticated; 71591423b2SMatthias Ringwald uint8_t authorized; 7213eb7322SMatthias Ringwald uint8_t secure_connection; 73591423b2SMatthias Ringwald } att_connection_t; 74591423b2SMatthias Ringwald 75591423b2SMatthias Ringwald // ATT Client Read Callback for Dynamic Data 76591423b2SMatthias Ringwald // - if buffer == NULL, don't copy data, just return size of value 77591423b2SMatthias Ringwald // - if buffer != NULL, copy data and return number bytes copied 78e404a688SMatthias Ringwald // If ENABLE_ATT_DELAYED_READ_RESPONSE is defined, you may return ATT_READ_RESPONSE_PENDING if data isn't available yet 79591423b2SMatthias Ringwald // @param con_handle of hci le connection 80591423b2SMatthias Ringwald // @param attribute_handle to be read 81591423b2SMatthias Ringwald // @param offset defines start of attribute value 82591423b2SMatthias Ringwald // @param buffer 83591423b2SMatthias Ringwald // @param buffer_size 84711e6c80SMatthias Ringwald 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); 85591423b2SMatthias Ringwald 86591423b2SMatthias Ringwald // ATT Client Write Callback for Dynamic Data 87591423b2SMatthias Ringwald // @param con_handle of hci le connection 88591423b2SMatthias Ringwald // @param attribute_handle to be written 899b31df63SMatthias Ringwald // @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 90591423b2SMatthias Ringwald // @param offset into the value - used for queued writes and long attributes 91591423b2SMatthias Ringwald // @param buffer 92591423b2SMatthias Ringwald // @param buffer_size 93591423b2SMatthias Ringwald // @param signature used for signed write commmands 94591423b2SMatthias Ringwald // @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 959b31df63SMatthias Ringwald // 969b31df63SMatthias Ringwald // Each Prepared Write Request triggers a callback with transaction mode ATT_TRANSACTION_MODE_ACTIVE. 979b31df63SMatthias Ringwald // 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. 989b31df63SMatthias Ringwald // 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. 999b31df63SMatthias Ringwald // Otherwise, all callbacks will be called with ATT_TRANSACTION_MODE_CANCEL. 1009b31df63SMatthias Ringwald // 1019b31df63SMatthias Ringwald // If the additional validation step is not needed, just return 0 for all callbacks with transaction mode ATT_TRANSACTION_MODE_VALIDATE. 1029b31df63SMatthias Ringwald // 103711e6c80SMatthias Ringwald 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); 104591423b2SMatthias Ringwald 1058ac574d6SMatthias Ringwald // Read & Write Callbacks for handle range 1068ac574d6SMatthias Ringwald typedef struct att_service_handler { 1078ac574d6SMatthias Ringwald btstack_linked_item_t * item; 1088ac574d6SMatthias Ringwald uint16_t start_handle; 1098ac574d6SMatthias Ringwald uint16_t end_handle; 1108ac574d6SMatthias Ringwald att_read_callback_t read_callback; 1118ac574d6SMatthias Ringwald att_write_callback_t write_callback; 1125d07396bSMatthias Ringwald btstack_packet_handler_t packet_handler; 1138ac574d6SMatthias Ringwald } att_service_handler_t; 1148ac574d6SMatthias Ringwald 115591423b2SMatthias Ringwald // MARK: ATT Operations 116591423b2SMatthias Ringwald 117591423b2SMatthias Ringwald /* 118591423b2SMatthias Ringwald * @brief setup ATT database 119591423b2SMatthias Ringwald */ 120591423b2SMatthias Ringwald void att_set_db(uint8_t const * db); 121591423b2SMatthias Ringwald 122591423b2SMatthias Ringwald /* 123591423b2SMatthias Ringwald * @brief set callback for read of dynamic attributes 124591423b2SMatthias Ringwald * @param callback 125591423b2SMatthias Ringwald */ 126591423b2SMatthias Ringwald void att_set_read_callback(att_read_callback_t callback); 127591423b2SMatthias Ringwald 128591423b2SMatthias Ringwald /* 129591423b2SMatthias Ringwald * @brief set callback for write of dynamic attributes 130591423b2SMatthias Ringwald * @param callback 131591423b2SMatthias Ringwald */ 132591423b2SMatthias Ringwald void att_set_write_callback(att_write_callback_t callback); 133591423b2SMatthias Ringwald 134591423b2SMatthias Ringwald /* 135591423b2SMatthias Ringwald * @brief debug helper, dump ATT database to stdout using log_info 136591423b2SMatthias Ringwald */ 137591423b2SMatthias Ringwald void att_dump_attributes(void); 138591423b2SMatthias Ringwald 139591423b2SMatthias Ringwald /* 140591423b2SMatthias Ringwald * @brief process ATT request against database and put response into response buffer 141591423b2SMatthias Ringwald * @param att_connection used for mtu and security properties 142591423b2SMatthias Ringwald * @param request_buffer, request_len: ATT request from clinet 143591423b2SMatthias Ringwald * @param response_buffer for result 144e404a688SMatthias Ringwald * @returns len of data in response buffer. 0 = no response, 145e404a688SMatthias Ringwald * ATT_READ_RESPONSE_PENDING if it was returned at least once for dynamic data (requires ENABLE_ATT_DELAYED_READ_RESPONSE) 146591423b2SMatthias Ringwald */ 147591423b2SMatthias Ringwald uint16_t att_handle_request(att_connection_t * att_connection, 148591423b2SMatthias Ringwald uint8_t * request_buffer, 149591423b2SMatthias Ringwald uint16_t request_len, 150591423b2SMatthias Ringwald uint8_t * response_buffer); 151591423b2SMatthias Ringwald 152591423b2SMatthias Ringwald /* 153591423b2SMatthias Ringwald * @brief setup value notification in response buffer for a given handle and value 154591423b2SMatthias Ringwald * @param att_connection 155fc64f94aSMatthias Ringwald * @param attribute_handle 156fc64f94aSMatthias Ringwald * @param value 157fc64f94aSMatthias Ringwald * @param value_len 158591423b2SMatthias Ringwald * @param response_buffer for notification 159591423b2SMatthias Ringwald */ 160591423b2SMatthias Ringwald uint16_t att_prepare_handle_value_notification(att_connection_t * att_connection, 161fc64f94aSMatthias Ringwald uint16_t attribute_handle, 1623bb3035fSMilanka Ringwald const uint8_t *value, 163591423b2SMatthias Ringwald uint16_t value_len, 164591423b2SMatthias Ringwald uint8_t * response_buffer); 165591423b2SMatthias Ringwald 166591423b2SMatthias Ringwald /* 167591423b2SMatthias Ringwald * @brief setup value indication in response buffer for a given handle and value 168591423b2SMatthias Ringwald * @param att_connection 169fc64f94aSMatthias Ringwald * @param attribute_handle 170fc64f94aSMatthias Ringwald * @param value 171fc64f94aSMatthias Ringwald * @param value_len 172591423b2SMatthias Ringwald * @param response_buffer for indication 173591423b2SMatthias Ringwald */ 174591423b2SMatthias Ringwald uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection, 175fc64f94aSMatthias Ringwald uint16_t attribute_handle, 1763bb3035fSMilanka Ringwald const uint8_t *value, 177591423b2SMatthias Ringwald uint16_t value_len, 178591423b2SMatthias Ringwald uint8_t * response_buffer); 179591423b2SMatthias Ringwald 180591423b2SMatthias Ringwald /* 181591423b2SMatthias Ringwald * @brief transcation queue of prepared writes, e.g., after disconnect 182591423b2SMatthias Ringwald */ 183591423b2SMatthias Ringwald void att_clear_transaction_queue(att_connection_t * att_connection); 184591423b2SMatthias Ringwald 185bfd413f0SMatthias Ringwald // att_read_callback helpers for a various data types 186bfd413f0SMatthias Ringwald 187bfd413f0SMatthias Ringwald /* 188bfd413f0SMatthias Ringwald * @brief Handle read of blob like data for att_read_callback 189bfd413f0SMatthias Ringwald * @param blob of data 190bfd413f0SMatthias Ringwald * @param blob_size of blob 191bfd413f0SMatthias Ringwald * @param offset from att_read_callback 192bfd413f0SMatthias Ringwald * @param buffer from att_read_callback 193bfd413f0SMatthias Ringwald * @param buffer_size from att_read_callback 194bfd413f0SMatthias Ringwald * @returns value size for buffer == 0 and num bytes copied otherwise 195bfd413f0SMatthias Ringwald */ 196bfd413f0SMatthias Ringwald 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); 197bfd413f0SMatthias Ringwald 198bfd413f0SMatthias Ringwald /* 199bfd413f0SMatthias Ringwald * @brief Handle read of little endian unsigned 32 bit value for att_read_callback 200bfd413f0SMatthias Ringwald * @param value 201bfd413f0SMatthias Ringwald * @param offset from att_read_callback 202bfd413f0SMatthias Ringwald * @param buffer from att_read_callback 203bfd413f0SMatthias Ringwald * @param buffer_size from att_read_callback 204bfd413f0SMatthias Ringwald * @returns value size for buffer == 0 and num bytes copied otherwise 205bfd413f0SMatthias Ringwald */ 206bfd413f0SMatthias Ringwald uint16_t att_read_callback_handle_little_endian_32(uint32_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size); 207bfd413f0SMatthias Ringwald 208bfd413f0SMatthias Ringwald /* 209bfd413f0SMatthias Ringwald * @brief Handle read of little endian unsigned 16 bit value for att_read_callback 210bfd413f0SMatthias Ringwald * @param value 211bfd413f0SMatthias Ringwald * @param offset from att_read_callback 212bfd413f0SMatthias Ringwald * @param buffer from att_read_callback 213bfd413f0SMatthias Ringwald * @param buffer_size from att_read_callback 214bfd413f0SMatthias Ringwald * @returns value size for buffer == 0 and num bytes copied otherwise 215bfd413f0SMatthias Ringwald */ 216bfd413f0SMatthias Ringwald uint16_t att_read_callback_handle_little_endian_16(uint16_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size); 217bfd413f0SMatthias Ringwald 218bfd413f0SMatthias Ringwald /* 219bfd413f0SMatthias Ringwald * @brief Handle read of single byte for att_read_callback 220bfd413f0SMatthias Ringwald * @param blob of data 221bfd413f0SMatthias Ringwald * @param blob_size of blob 222bfd413f0SMatthias Ringwald * @param offset from att_read_callback 223bfd413f0SMatthias Ringwald * @param buffer from att_read_callback 224bfd413f0SMatthias Ringwald * @param buffer_size from att_read_callback 225bfd413f0SMatthias Ringwald * @returns value size for buffer == 0 and num bytes copied otherwise 226bfd413f0SMatthias Ringwald */ 227bfd413f0SMatthias Ringwald uint16_t att_read_callback_handle_byte(uint8_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size); 228bfd413f0SMatthias Ringwald 229bfd413f0SMatthias Ringwald 230591423b2SMatthias Ringwald // experimental client API 231fc64f94aSMatthias Ringwald uint16_t att_uuid_for_handle(uint16_t attribute_handle); 232591423b2SMatthias Ringwald 233660ce368SMatthias Ringwald 234660ce368SMatthias Ringwald // experimental GATT Server API 235660ce368SMatthias Ringwald 236660ce368SMatthias Ringwald // returns 1 if service found. only primary service. 237660ce368SMatthias Ringwald int gatt_server_get_get_handle_range_for_service_with_uuid16(uint16_t uuid16, uint16_t * start_handle, uint16_t * end_handle); 238660ce368SMatthias Ringwald 239660ce368SMatthias Ringwald // returns 0 if not found 240660ce368SMatthias Ringwald uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16); 241660ce368SMatthias Ringwald 242660ce368SMatthias Ringwald // returns 0 if not found 243c7774db7SMilanka Ringwald 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); 244c7774db7SMilanka Ringwald uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16); 245c7774db7SMilanka Ringwald uint16_t gatt_server_get_server_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16); 246660ce368SMatthias Ringwald 24715f22304SMatthias Ringwald 24815f22304SMatthias Ringwald // returns 1 if service found. only primary service. 24915f22304SMatthias Ringwald int gatt_server_get_get_handle_range_for_service_with_uuid128(const uint8_t * uuid128, uint16_t * start_handle, uint16_t * end_handle); 25015f22304SMatthias Ringwald 25115f22304SMatthias Ringwald // returns 0 if not found 25215f22304SMatthias Ringwald uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128); 25315f22304SMatthias Ringwald 25415f22304SMatthias Ringwald // returns 0 if not found 25515f22304SMatthias Ringwald uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128); 25615f22304SMatthias Ringwald 2578b1d64c6SMatthias Ringwald // non-user functionality for att_server 2588b1d64c6SMatthias Ringwald 2598b1d64c6SMatthias Ringwald /* 2608b1d64c6SMatthias Ringwald * @brief Check if writes to handle should be persistent 2618b1d64c6SMatthias Ringwald * @param handle 2628b1d64c6SMatthias Ringwald * @returns 1 if persistent 2638b1d64c6SMatthias Ringwald */ 2648b1d64c6SMatthias Ringwald int att_is_persistent_ccc(uint16_t handle); 2658b1d64c6SMatthias Ringwald 2668b1d64c6SMatthias Ringwald 267591423b2SMatthias Ringwald #if defined __cplusplus 268591423b2SMatthias Ringwald } 269591423b2SMatthias Ringwald #endif 270591423b2SMatthias Ringwald 271*80e33422SMatthias Ringwald #endif // ATT_H 272