xref: /btstack/src/ble/att_db.h (revision a64cbea79fa5fb9daa58e135a19c6cce10f3e642)
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 BLUEKITCHEN
24  * GMBH 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  * @title ATT Database Engine
40  *
41  */
42 
43 #ifndef ATT_DB_H
44 #define ATT_DB_H
45 
46 #include <stdint.h>
47 #include "bluetooth.h"
48 #include "btstack_linked_list.h"
49 #include "btstack_defines.h"
50 #include "btstack_bool.h"
51 
52 #if defined __cplusplus
53 extern "C" {
54 #endif
55 
56 // MARK: Attribute PDU Opcodes
57 #define ATT_ERROR_RESPONSE              0x01u
58 
59 #define ATT_EXCHANGE_MTU_REQUEST        0x02u
60 #define ATT_EXCHANGE_MTU_RESPONSE       0x03u
61 
62 #define ATT_FIND_INFORMATION_REQUEST    0x04u
63 #define ATT_FIND_INFORMATION_REPLY      0x05u
64 #define ATT_FIND_BY_TYPE_VALUE_REQUEST  0x06u
65 #define ATT_FIND_BY_TYPE_VALUE_RESPONSE 0x07u
66 
67 #define ATT_READ_BY_TYPE_REQUEST        0x08u
68 #define ATT_READ_BY_TYPE_RESPONSE       0x09u
69 #define ATT_READ_REQUEST                0x0au
70 #define ATT_READ_RESPONSE               0x0bu
71 #define ATT_READ_BLOB_REQUEST           0x0cu
72 #define ATT_READ_BLOB_RESPONSE          0x0du
73 #define ATT_READ_MULTIPLE_REQUEST       0x0eu
74 #define ATT_READ_MULTIPLE_RESPONSE      0x0fu
75 #define ATT_READ_BY_GROUP_TYPE_REQUEST  0x10u
76 #define ATT_READ_BY_GROUP_TYPE_RESPONSE 0x11u
77 
78 #define ATT_WRITE_REQUEST               0x12u
79 #define ATT_WRITE_RESPONSE              0x13u
80 
81 #define ATT_PREPARE_WRITE_REQUEST       0x16u
82 #define ATT_PREPARE_WRITE_RESPONSE      0x17u
83 #define ATT_EXECUTE_WRITE_REQUEST       0x18u
84 #define ATT_EXECUTE_WRITE_RESPONSE      0x19u
85 
86 #define ATT_HANDLE_VALUE_NOTIFICATION   0x1bu
87 #define ATT_HANDLE_VALUE_INDICATION     0x1du
88 #define ATT_HANDLE_VALUE_CONFIRMATION   0x1eu
89 
90 #define ATT_READ_MULTIPLE_VARIABLE_REQ  0x20u
91 #define ATT_READ_MULTIPLE_VARIABLE_RSP  0x21u
92 #define ATT_MULTIPLE_HANDLE_VALUE_NTF   0x23u
93 
94 #define ATT_WRITE_COMMAND                0x52u
95 #define ATT_SIGNED_WRITE_COMMAND         0xD2u
96 
97 
98 // internal additions
99 // 128 bit UUID used
100 #define ATT_PROPERTY_UUID128             0x200u
101 // Read/Write Permission bits
102 #define ATT_PROPERTY_READ_PERMISSION_BIT_0  0x0400u
103 #define ATT_PROPERTY_READ_PERMISSION_BIT_1  0x0800u
104 #define ATT_PROPERTY_WRITE_PERMISSION_BIT_0 0x0001u
105 #define ATT_PROPERTY_WRITE_PERMISSION_BIT_1 0x0010u
106 #define ATT_PROPERTY_READ_PERMISSION_SC     0x0020u
107 #define ATT_PROPERTY_WRITE_PERMISSION_SC    0x0080u
108 
109 
110 typedef struct att_connection {
111     hci_con_handle_t con_handle;
112     uint16_t mtu;       // initialized to ATT_DEFAULT_MTU (23), negotiated during MTU exchange
113     uint16_t max_mtu;   // local maximal L2CAP_MTU, set to l2cap_max_le_mtu()
114     bool     mtu_exchanged;
115     uint8_t  encryption_key_size;
116     uint8_t  authenticated;
117     uint8_t  authorized;
118     uint8_t  secure_connection;
119 } att_connection_t;
120 
121 /* API_START */
122 
123 // map ATT ERROR CODES on to att_read_callback length
124 #define ATT_READ_ERROR_CODE_OFFSET       0xfe00u
125 
126 // custom BTstack ATT Response Pending for att_read_callback
127 #define ATT_READ_RESPONSE_PENDING                 0xffffu
128 
129 // internally used to signal write response pending
130 #define ATT_INTERNAL_WRITE_RESPONSE_PENDING       0xfffeu
131 
132 /**
133  * @brief ATT Client Read Callback for Dynamic Data
134  * - if buffer == NULL, don't copy data, just return size of value
135  * - if buffer != NULL, copy data and return number bytes copied
136  * If ENABLE_ATT_DELAYED_READ_RESPONSE is defined, you may return ATT_READ_RESPONSE_PENDING if data isn't available yet
137  * @param con_handle of hci le connection
138  * @param attribute_handle to be read
139  * @param offset defines start of attribute value
140  * @param buffer
141  * @param buffer_size
142  * @return size of value if buffer is NULL, otherwise number of bytes copied
143  */
144 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);
145 
146 /**
147  * @brief ATT Client Write Callback for Dynamic Data
148  * Each Prepared Write Request triggers a callback with transaction mode ATT_TRANSACTION_MODE_ACTIVE.
149  * 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.
150  * 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.
151  * Otherwise, all callbacks will be called with ATT_TRANSACTION_MODE_CANCEL.
152  *
153  * If the additional validation step is not needed, just return 0 for all callbacks with transaction mode ATT_TRANSACTION_MODE_VALIDATE.
154  *
155  * @param con_handle of hci le connection
156  * @param attribute_handle to be written
157  * @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
158  * @param offset into the value - used for queued writes and long attributes
159  * @param buffer
160  * @param buffer_size
161  * @param signature used for signed write commmands
162  * @return 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
163  */
164 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);
165 
166 // Read & Write Callbacks for handle range
167 typedef struct att_service_handler {
168     btstack_linked_item_t * item;
169     uint16_t start_handle;
170     uint16_t end_handle;
171     att_read_callback_t read_callback;
172     att_write_callback_t write_callback;
173     btstack_packet_handler_t packet_handler;
174 } att_service_handler_t;
175 
176 // MARK: ATT Operations
177 
178 /**
179  * @brief setup ATT database
180  * @param db
181  */
182 void att_set_db(uint8_t const * db);
183 
184 /*
185  * @brief set callback for read of dynamic attributes
186  * @param callback
187  */
188 void att_set_read_callback(att_read_callback_t callback);
189 
190 /**
191  * @brief set callback for write of dynamic attributes
192  * @param callback
193  */
194 void att_set_write_callback(att_write_callback_t callback);
195 
196 /**
197  * @brief debug helper, dump ATT database to stdout using log_info
198  */
199 void att_dump_attributes(void);
200 
201 /**
202  * @brief process ATT request against database and put response into response buffer
203  * @param att_connection used for mtu and security properties
204  * @param request_buffer, request_len: ATT request from clinet
205  * @param response_buffer for result
206  * @return len of data in response buffer. 0 = no response,
207  *          ATT_READ_RESPONSE_PENDING if it was returned at least once for dynamic data (requires ENABLE_ATT_DELAYED_READ_RESPONSE)
208  */
209 uint16_t att_handle_request(att_connection_t * att_connection,
210                             uint8_t * request_buffer,
211                             uint16_t request_len,
212                             uint8_t * response_buffer);
213 
214 /**
215  * @brief setup value notification in response buffer for a given handle and value
216  * @param att_connection
217  * @param attribute_handle
218  * @param value
219  * @param value_len
220  * @param response_buffer for notification
221  */
222 uint16_t att_prepare_handle_value_notification(att_connection_t * att_connection,
223                                                uint16_t attribute_handle,
224                                                const uint8_t *value,
225                                                uint16_t value_len,
226                                                uint8_t * response_buffer);
227 
228 /**
229  * @brief setup value indication in response buffer for a given handle and value
230  * @param att_connection
231  * @param attribute_handle
232  * @param value
233  * @param value_len
234  * @param response_buffer for indication
235  */
236 uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection,
237                                              uint16_t attribute_handle,
238                                              const uint8_t *value,
239                                              uint16_t value_len,
240                                              uint8_t * response_buffer);
241 
242 /**
243  * @brief transcation queue of prepared writes, e.g., after disconnect
244  * @return att_connection
245  */
246 void att_clear_transaction_queue(att_connection_t * att_connection);
247 
248 // att_read_callback helpers for a various data types
249 
250 /**
251  * @brief Handle read of blob like data for att_read_callback
252  * @param blob of data
253  * @param blob_size of blob
254  * @param offset from att_read_callback
255  * @param buffer from att_read_callback
256  * @param buffer_size from att_read_callback
257  * @return value size for buffer == 0 and num bytes copied otherwise
258  */
259 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);
260 
261 /**
262  * @brief Handle read of little endian unsigned 32 bit value for att_read_callback
263  * @param value
264  * @param offset from att_read_callback
265  * @param buffer from att_read_callback
266  * @param buffer_size from att_read_callback
267  * @return value size for buffer == 0 and num bytes copied otherwise
268  */
269 uint16_t att_read_callback_handle_little_endian_32(uint32_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
270 
271 /**
272  * @brief Handle read of little endian unsigned 16 bit value for att_read_callback
273  * @param value
274  * @param offset from att_read_callback
275  * @param buffer from att_read_callback
276  * @param buffer_size from att_read_callback
277  * @return value size for buffer == 0 and num bytes copied otherwise
278  */
279 uint16_t att_read_callback_handle_little_endian_16(uint16_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
280 
281 /**
282  * @brief Handle read of single byte for att_read_callback
283  * @param blob of data
284  * @param blob_size of blob
285  * @param offset from att_read_callback
286  * @param buffer from att_read_callback
287  * @param buffer_size from att_read_callback
288  * @return value size for buffer == 0 and num bytes copied otherwise
289  */
290 uint16_t att_read_callback_handle_byte(uint8_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
291 
292 
293 // experimental client API
294 /**
295  * @brief Get UUID for handle
296  * @param attribute_handle
297  * @return 0 if not found
298  */
299 uint16_t att_uuid_for_handle(uint16_t attribute_handle);
300 
301 // experimental GATT Server API
302 
303 /**
304  * @brief Get handle range for primary service.
305  * @param uuid16
306  * @param start_handle
307  * @param end_handle
308  * @return false if not found
309  */
310 bool gatt_server_get_handle_range_for_service_with_uuid16(uint16_t uuid16, uint16_t * start_handle, uint16_t * end_handle);
311 
312 /**
313  * @brief Get handle range for included service.
314  * @param start_handle
315  * @param end_handle
316  * @param uuid16
317  * @param out_included_service_handle
318  * @param out_included_service_start_handle
319  * @param out_included_service_end_handle
320  * @return false if not found
321  */
322 bool gatt_server_get_included_service_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16,
323     uint16_t * out_included_service_handle, uint16_t * out_included_service_start_handle, uint16_t * out_included_service_end_handle);
324 
325 /**
326  * @brief Get value handle for characteristic.
327  * @param start_handle
328  * @param end_handle
329  * @param uuid16
330  * @return 0 if not found
331  */
332 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16);
333 
334 /**
335  * @brief Get descriptor handle for characteristic.
336  * @param start_handle
337  * @param end_handle
338  * @param characteristic_uuid16
339  * @param descriptor_uuid16
340  * @return 0 if not found
341  */
342 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);
343 
344 /**
345  * @brief Get client configuration handle for characteristic.
346  * @param start_handle
347  * @param end_handle
348  * @param characteristic_uuid16
349  * @return 0 if not found
350  */
351 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16);
352 
353 /**
354  * @brief Get server configuration handle for characteristic.
355  * @param start_handle
356  * @param end_handle
357  * @param characteristic_uuid16
358  * @param descriptor_uuid16
359  * @return 0 if not found
360  */
361 uint16_t gatt_server_get_server_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16);
362 
363 
364 /**
365  * @brief Get handle range for primary service.
366  * @param uuid128
367  * @param start_handle
368  * @param end_handle
369  * @return false if not found
370  */
371 bool gatt_server_get_handle_range_for_service_with_uuid128(const uint8_t * uuid128, uint16_t * start_handle, uint16_t * end_handle);
372 
373 /**
374  * @brief Get value handle.
375  * @param start_handle
376  * @param end_handle
377  * @param uuid128
378  * @return 0 if not found
379  */
380 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128);
381 
382 /**
383  * @brief Get client configuration handle.
384  * @param start_handle
385  * @param end_handle
386  * @param uuid128
387  * @return 0 if not found
388  */
389 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128);
390 
391 /* API_END */
392 
393 // non-user functionality for att_server
394 
395 /**
396  * @brief Check if writes to handle should be persistent
397  * @param handle
398  * @return 1 if persistent
399  */
400 bool att_is_persistent_ccc(uint16_t handle);
401 
402 
403 
404 // auto-pts testing, returns response size
405 #ifdef ENABLE_BTP
406 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);
407 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);
408 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);
409 #endif
410 
411 #if defined __cplusplus
412 }
413 #endif
414 
415 #endif // ATT_H
416