xref: /btstack/src/ble/att_db.h (revision 29502c9b6278f5ef85634162beef9b6a43c78688)
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     att_read_callback_t read_callback;
170     att_write_callback_t write_callback;
171     btstack_packet_handler_t packet_handler;
172     uint16_t start_handle;
173     uint16_t end_handle;
174     uint8_t flags;
175 } att_service_handler_t;
176 
177 // MARK: ATT Operations
178 
179 /**
180  * @brief setup ATT database
181  * @param db
182  */
183 void att_set_db(uint8_t const * db);
184 
185 /*
186  * @brief set callback for read of dynamic attributes
187  * @param callback
188  */
189 void att_set_read_callback(att_read_callback_t callback);
190 
191 /**
192  * @brief set callback for write of dynamic attributes
193  * @param callback
194  */
195 void att_set_write_callback(att_write_callback_t callback);
196 
197 /**
198  * @brief debug helper, dump ATT database to stdout using log_info
199  */
200 void att_dump_attributes(void);
201 
202 /**
203  * @brief process ATT request against database and put response into response buffer
204  * @param att_connection used for mtu and security properties
205  * @param request_buffer, request_len: ATT request from clinet
206  * @param response_buffer for result
207  * @return len of data in response buffer. 0 = no response,
208  *          ATT_READ_RESPONSE_PENDING if it was returned at least once for dynamic data (requires ENABLE_ATT_DELAYED_READ_RESPONSE)
209  */
210 uint16_t att_handle_request(att_connection_t * att_connection,
211                             uint8_t * request_buffer,
212                             uint16_t request_len,
213                             uint8_t * response_buffer);
214 
215 /**
216  * @brief setup value notification in response buffer for a given handle and value
217  * @param att_connection
218  * @param attribute_handle
219  * @param value
220  * @param value_len
221  * @param response_buffer for notification
222  */
223 uint16_t att_prepare_handle_value_notification(att_connection_t * att_connection,
224                                                uint16_t attribute_handle,
225                                                const uint8_t *value,
226                                                uint16_t value_len,
227                                                uint8_t * response_buffer);
228 
229 /**
230  * @brief setup value notification in response buffer for multiple handles and values
231  * @param att_connection
232  * @param attribute_handle
233  * @param value
234  * @param value_len
235  * @param response_buffer for notification
236  */
237 uint16_t att_prepare_handle_value_multiple_notification(att_connection_t * att_connection,
238                                                         uint8_t num_attributes,
239                                                         const uint16_t * attribute_handles,
240                                                         const uint8_t ** values_data,
241                                                         const uint16_t * values_len,
242                                                         uint8_t * response_buffer);
243 
244 /**
245  * @brief setup value indication in response buffer for a given handle and value
246  * @param att_connection
247  * @param attribute_handle
248  * @param value
249  * @param value_len
250  * @param response_buffer for indication
251  */
252 uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection,
253                                              uint16_t attribute_handle,
254                                              const uint8_t *value,
255                                              uint16_t value_len,
256                                              uint8_t * response_buffer);
257 
258 /**
259  * @brief transcation queue of prepared writes, e.g., after disconnect
260  * @return att_connection
261  */
262 void att_clear_transaction_queue(att_connection_t * att_connection);
263 
264 // att_read_callback helpers for a various data types
265 
266 /**
267  * @brief Handle read of blob like data for att_read_callback
268  * @param blob of data
269  * @param blob_size of blob
270  * @param offset from att_read_callback
271  * @param buffer from att_read_callback
272  * @param buffer_size from att_read_callback
273  * @return value size for buffer == 0 and num bytes copied otherwise
274  */
275 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);
276 
277 /**
278  * @brief Handle read of little endian unsigned 32 bit value for att_read_callback
279  * @param value
280  * @param offset from att_read_callback
281  * @param buffer from att_read_callback
282  * @param buffer_size from att_read_callback
283  * @return value size for buffer == 0 and num bytes copied otherwise
284  */
285 uint16_t att_read_callback_handle_little_endian_32(uint32_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
286 
287 /**
288  * @brief Handle read of little endian unsigned 16 bit value for att_read_callback
289  * @param value
290  * @param offset from att_read_callback
291  * @param buffer from att_read_callback
292  * @param buffer_size from att_read_callback
293  * @return value size for buffer == 0 and num bytes copied otherwise
294  */
295 uint16_t att_read_callback_handle_little_endian_16(uint16_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
296 
297 /**
298  * @brief Handle read of single byte for att_read_callback
299  * @param blob of data
300  * @param blob_size of blob
301  * @param offset from att_read_callback
302  * @param buffer from att_read_callback
303  * @param buffer_size from att_read_callback
304  * @return value size for buffer == 0 and num bytes copied otherwise
305  */
306 uint16_t att_read_callback_handle_byte(uint8_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
307 
308 
309 // experimental client API
310 /**
311  * @brief Get UUID for handle
312  * @param attribute_handle
313  * @return 0 if not found
314  */
315 uint16_t att_uuid_for_handle(uint16_t attribute_handle);
316 
317 /**
318  * @brief Get const value for handle
319  * @param attribute_handle
320  * @param out_value_len  output variable that hold value len
321  * @return value
322  */
323 
324 const uint8_t * gatt_server_get_const_value_for_handle(uint16_t attribute_handle, uint16_t * out_value_len);
325 
326 // experimental GATT Server API
327 
328 /**
329  * @brief Get handle range for primary service.
330  * @param uuid16
331  * @param start_handle
332  * @param end_handle
333  * @return false if not found
334  */
335 bool gatt_server_get_handle_range_for_service_with_uuid16(uint16_t uuid16, uint16_t * start_handle, uint16_t * end_handle);
336 
337 /**
338  * @brief Get handle range for included service.
339  * @param start_handle
340  * @param end_handle
341  * @param uuid16
342  * @param out_included_service_handle
343  * @param out_included_service_start_handle
344  * @param out_included_service_end_handle
345  * @return false if not found
346  */
347 bool gatt_server_get_included_service_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16,
348     uint16_t * out_included_service_handle, uint16_t * out_included_service_start_handle, uint16_t * out_included_service_end_handle);
349 
350 /**
351  * @brief Get value handle for characteristic.
352  * @param start_handle
353  * @param end_handle
354  * @param uuid16
355  * @return 0 if not found
356  */
357 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16);
358 
359 /**
360  * @brief Get descriptor handle for characteristic.
361  * @param start_handle
362  * @param end_handle
363  * @param characteristic_uuid16
364  * @param descriptor_uuid16
365  * @return 0 if not found
366  */
367 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);
368 
369 /**
370  * @brief Get client configuration handle for characteristic.
371  * @param start_handle
372  * @param end_handle
373  * @param characteristic_uuid16
374  * @return 0 if not found
375  */
376 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16);
377 
378 /**
379  * @brief Get server configuration handle for characteristic.
380  * @param start_handle
381  * @param end_handle
382  * @param characteristic_uuid16
383  * @param descriptor_uuid16
384  * @return 0 if not found
385  */
386 uint16_t gatt_server_get_server_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16);
387 
388 
389 /**
390  * @brief Get handle range for primary service.
391  * @param uuid128
392  * @param start_handle
393  * @param end_handle
394  * @return false if not found
395  */
396 bool gatt_server_get_handle_range_for_service_with_uuid128(const uint8_t * uuid128, uint16_t * start_handle, uint16_t * end_handle);
397 
398 /**
399  * @brief Get value handle.
400  * @param start_handle
401  * @param end_handle
402  * @param uuid128
403  * @return 0 if not found
404  */
405 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128);
406 
407 /**
408  * @brief Get client configuration handle.
409  * @param start_handle
410  * @param end_handle
411  * @param uuid128
412  * @return 0 if not found
413  */
414 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128);
415 
416 /* API_END */
417 
418 // non-user functionality for att_server
419 
420 /**
421  * @brief Check if writes to handle should be persistent
422  * @param handle
423  * @return 1 if persistent
424  */
425 bool att_is_persistent_ccc(uint16_t handle);
426 
427 
428 
429 // auto-pts testing, returns response size
430 #ifdef ENABLE_BTP
431 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);
432 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);
433 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);
434 #endif
435 
436 #if defined __cplusplus
437 }
438 #endif
439 
440 #endif // ATT_H
441