xref: /btstack/src/ble/att_db.h (revision 82c9843e67930118ee91effaf3bfc78a59bb5b9a)
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_H
40 #define __ATT_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 // custom BTstack error codes
52 #define ATT_ERROR_HCI_DISCONNECT_RECEIVED         0x1f
53 
54 // custom BTstack ATT error codes
55 #define ATT_ERROR_DATA_MISMATCH                   0x7e
56 #define ATT_ERROR_TIMEOUT                         0x7F
57 
58 // custom BTstack ATT Response Pending
59 #define ATT_READ_RESPONSE_PENDING                 0xffff
60 
61 typedef struct att_connection {
62     hci_con_handle_t con_handle;
63     uint16_t mtu;       // initialized to ATT_DEFAULT_MTU (23), negotiated during MTU exchange
64     uint16_t max_mtu;   // local maximal L2CAP_MTU, set to l2cap_max_le_mtu()
65     uint8_t  encryption_key_size;
66     uint8_t  authenticated;
67     uint8_t  authorized;
68 } att_connection_t;
69 
70 // ATT Client Read Callback for Dynamic Data
71 // - if buffer == NULL, don't copy data, just return size of value
72 // - if buffer != NULL, copy data and return number bytes copied
73 // If ENABLE_ATT_DELAYED_READ_RESPONSE is defined, you may return ATT_READ_RESPONSE_PENDING if data isn't available yet
74 // @param con_handle of hci le connection
75 // @param attribute_handle to be read
76 // @param offset defines start of attribute value
77 // @param buffer
78 // @param buffer_size
79 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);
80 
81 // ATT Client Write Callback for Dynamic Data
82 // @param con_handle of hci le connection
83 // @param attribute_handle to be written
84 // @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
85 // @param offset into the value - used for queued writes and long attributes
86 // @param buffer
87 // @param buffer_size
88 // @param signature used for signed write commmands
89 // @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
90 //
91 // Each Prepared Write Request triggers a callback with transaction mode ATT_TRANSACTION_MODE_ACTIVE.
92 // 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.
93 // 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.
94 // Otherwise, all callbacks will be called with ATT_TRANSACTION_MODE_CANCEL.
95 //
96 // If the additional validation step is not needed, just return 0 for all callbacks with transaction mode ATT_TRANSACTION_MODE_VALIDATE.
97 //
98 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);
99 
100 // Read & Write Callbacks for handle range
101 typedef struct att_service_handler {
102     btstack_linked_item_t * item;
103     uint16_t start_handle;
104     uint16_t end_handle;
105     att_read_callback_t read_callback;
106     att_write_callback_t write_callback;
107     btstack_packet_handler_t packet_handler;
108 } att_service_handler_t;
109 
110 // MARK: ATT Operations
111 
112 /*
113  * @brief setup ATT database
114  */
115 void att_set_db(uint8_t const * db);
116 
117 /*
118  * @brief set callback for read of dynamic attributes
119  * @param callback
120  */
121 void att_set_read_callback(att_read_callback_t callback);
122 
123 /*
124  * @brief set callback for write of dynamic attributes
125  * @param callback
126  */
127 void att_set_write_callback(att_write_callback_t callback);
128 
129 /*
130  * @brief debug helper, dump ATT database to stdout using log_info
131  */
132 void att_dump_attributes(void);
133 
134 /*
135  * @brief process ATT request against database and put response into response buffer
136  * @param att_connection used for mtu and security properties
137  * @param request_buffer, request_len: ATT request from clinet
138  * @param response_buffer for result
139  * @returns len of data in response buffer. 0 = no response,
140  *          ATT_READ_RESPONSE_PENDING if it was returned at least once for dynamic data (requires ENABLE_ATT_DELAYED_READ_RESPONSE)
141  */
142 uint16_t att_handle_request(att_connection_t * att_connection,
143                             uint8_t * request_buffer,
144                             uint16_t request_len,
145                             uint8_t * response_buffer);
146 
147 /*
148  * @brief setup value notification in response buffer for a given handle and value
149  * @param att_connection
150  * @param attribute_handle
151  * @param value
152  * @param value_len
153  * @param response_buffer for notification
154  */
155 uint16_t att_prepare_handle_value_notification(att_connection_t * att_connection,
156                                                uint16_t attribute_handle,
157                                                uint8_t *value,
158                                                uint16_t value_len,
159                                                uint8_t * response_buffer);
160 
161 /*
162  * @brief setup value indication in response buffer for a given handle and value
163  * @param att_connection
164  * @param attribute_handle
165  * @param value
166  * @param value_len
167  * @param response_buffer for indication
168  */
169 uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection,
170                                              uint16_t attribute_handle,
171                                              uint8_t *value,
172                                              uint16_t value_len,
173                                              uint8_t * response_buffer);
174 
175 /*
176  * @brief transcation queue of prepared writes, e.g., after disconnect
177  */
178 void att_clear_transaction_queue(att_connection_t * att_connection);
179 
180 // att_read_callback helpers for a various data types
181 
182 /*
183  * @brief Handle read of blob like data for att_read_callback
184  * @param blob of data
185  * @param blob_size of blob
186  * @param offset from att_read_callback
187  * @param buffer from att_read_callback
188  * @param buffer_size from att_read_callback
189  * @returns value size for buffer == 0 and num bytes copied otherwise
190  */
191 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);
192 
193 /*
194  * @brief Handle read of little endian unsigned 32 bit value for att_read_callback
195  * @param value
196  * @param offset from att_read_callback
197  * @param buffer from att_read_callback
198  * @param buffer_size from att_read_callback
199  * @returns value size for buffer == 0 and num bytes copied otherwise
200  */
201 uint16_t att_read_callback_handle_little_endian_32(uint32_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
202 
203 /*
204  * @brief Handle read of little endian unsigned 16 bit value for att_read_callback
205  * @param value
206  * @param offset from att_read_callback
207  * @param buffer from att_read_callback
208  * @param buffer_size from att_read_callback
209  * @returns value size for buffer == 0 and num bytes copied otherwise
210  */
211 uint16_t att_read_callback_handle_little_endian_16(uint16_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
212 
213 /*
214  * @brief Handle read of single byte for att_read_callback
215  * @param blob of data
216  * @param blob_size of blob
217  * @param offset from att_read_callback
218  * @param buffer from att_read_callback
219  * @param buffer_size from att_read_callback
220  * @returns value size for buffer == 0 and num bytes copied otherwise
221  */
222 uint16_t att_read_callback_handle_byte(uint8_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
223 
224 
225 // experimental client API
226 uint16_t att_uuid_for_handle(uint16_t attribute_handle);
227 
228 
229 // experimental GATT Server API
230 
231 // returns 1 if service found. only primary service.
232 int gatt_server_get_get_handle_range_for_service_with_uuid16(uint16_t uuid16, uint16_t * start_handle, uint16_t * end_handle);
233 
234 // returns 0 if not found
235 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16);
236 
237 // returns 0 if not found
238 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16);
239 
240 
241 // returns 1 if service found. only primary service.
242 int gatt_server_get_get_handle_range_for_service_with_uuid128(const uint8_t * uuid128, uint16_t * start_handle, uint16_t * end_handle);
243 
244 // returns 0 if not found
245 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128);
246 
247 // returns 0 if not found
248 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128);
249 
250 // non-user functionality for att_server
251 
252 /*
253  * @brief Check if writes to handle should be persistent
254  * @param handle
255  * @returns 1 if persistent
256  */
257 int att_is_persistent_ccc(uint16_t handle);
258 
259 
260 #if defined __cplusplus
261 }
262 #endif
263 
264 #endif // __ATT_H
265