xref: /btstack/src/ble/att_db.h (revision ced70f9bfeafe291ec597a3a9cc862e39e0da3ce)
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 notification in response buffer for multiple handles and values
230  * @param att_connection
231  * @param attribute_handle
232  * @param value
233  * @param value_len
234  * @param response_buffer for notification
235  */
236 uint16_t att_prepare_handle_value_multiple_notification(att_connection_t * att_connection,
237                                                         uint8_t num_attributes,
238                                                         const uint16_t * attribute_handles,
239                                                         const uint8_t ** values_data,
240                                                         const uint16_t * values_len,
241                                                         uint8_t * response_buffer);
242 
243 /**
244  * @brief setup value indication in response buffer for a given handle and value
245  * @param att_connection
246  * @param attribute_handle
247  * @param value
248  * @param value_len
249  * @param response_buffer for indication
250  */
251 uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection,
252                                              uint16_t attribute_handle,
253                                              const uint8_t *value,
254                                              uint16_t value_len,
255                                              uint8_t * response_buffer);
256 
257 /**
258  * @brief transcation queue of prepared writes, e.g., after disconnect
259  * @return att_connection
260  */
261 void att_clear_transaction_queue(att_connection_t * att_connection);
262 
263 // att_read_callback helpers for a various data types
264 
265 /**
266  * @brief Handle read of blob like data for att_read_callback
267  * @param blob of data
268  * @param blob_size of blob
269  * @param offset from att_read_callback
270  * @param buffer from att_read_callback
271  * @param buffer_size from att_read_callback
272  * @return value size for buffer == 0 and num bytes copied otherwise
273  */
274 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);
275 
276 /**
277  * @brief Handle read of little endian unsigned 32 bit value for att_read_callback
278  * @param value
279  * @param offset from att_read_callback
280  * @param buffer from att_read_callback
281  * @param buffer_size from att_read_callback
282  * @return value size for buffer == 0 and num bytes copied otherwise
283  */
284 uint16_t att_read_callback_handle_little_endian_32(uint32_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
285 
286 /**
287  * @brief Handle read of little endian unsigned 16 bit value for att_read_callback
288  * @param value
289  * @param offset from att_read_callback
290  * @param buffer from att_read_callback
291  * @param buffer_size from att_read_callback
292  * @return value size for buffer == 0 and num bytes copied otherwise
293  */
294 uint16_t att_read_callback_handle_little_endian_16(uint16_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
295 
296 /**
297  * @brief Handle read of single byte for att_read_callback
298  * @param blob of data
299  * @param blob_size of blob
300  * @param offset from att_read_callback
301  * @param buffer from att_read_callback
302  * @param buffer_size from att_read_callback
303  * @return value size for buffer == 0 and num bytes copied otherwise
304  */
305 uint16_t att_read_callback_handle_byte(uint8_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
306 
307 
308 // experimental client API
309 /**
310  * @brief Get UUID for handle
311  * @param attribute_handle
312  * @return 0 if not found
313  */
314 uint16_t att_uuid_for_handle(uint16_t attribute_handle);
315 
316 /**
317  * @brief Get const value for handle
318  * @param attribute_handle
319  * @param out_value_len  output variable that hold value len
320  * @return value
321  */
322 
323 const uint8_t * gatt_server_get_const_value_for_handle(uint16_t attribute_handle, uint16_t * out_value_len);
324 
325 // experimental GATT Server API
326 
327 /**
328  * @brief Get handle range for primary service.
329  * @param uuid16
330  * @param start_handle
331  * @param end_handle
332  * @return false if not found
333  */
334 bool gatt_server_get_handle_range_for_service_with_uuid16(uint16_t uuid16, uint16_t * start_handle, uint16_t * end_handle);
335 
336 /**
337  * @brief Get handle range for included service.
338  * @param start_handle
339  * @param end_handle
340  * @param uuid16
341  * @param out_included_service_handle
342  * @param out_included_service_start_handle
343  * @param out_included_service_end_handle
344  * @return false if not found
345  */
346 bool gatt_server_get_included_service_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16,
347     uint16_t * out_included_service_handle, uint16_t * out_included_service_start_handle, uint16_t * out_included_service_end_handle);
348 
349 /**
350  * @brief Get value handle for characteristic.
351  * @param start_handle
352  * @param end_handle
353  * @param uuid16
354  * @return 0 if not found
355  */
356 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16);
357 
358 /**
359  * @brief Get descriptor handle for characteristic.
360  * @param start_handle
361  * @param end_handle
362  * @param characteristic_uuid16
363  * @param descriptor_uuid16
364  * @return 0 if not found
365  */
366 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);
367 
368 /**
369  * @brief Get client configuration handle for characteristic.
370  * @param start_handle
371  * @param end_handle
372  * @param characteristic_uuid16
373  * @return 0 if not found
374  */
375 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16);
376 
377 /**
378  * @brief Get server configuration handle for characteristic.
379  * @param start_handle
380  * @param end_handle
381  * @param characteristic_uuid16
382  * @param descriptor_uuid16
383  * @return 0 if not found
384  */
385 uint16_t gatt_server_get_server_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16);
386 
387 
388 /**
389  * @brief Get handle range for primary service.
390  * @param uuid128
391  * @param start_handle
392  * @param end_handle
393  * @return false if not found
394  */
395 bool gatt_server_get_handle_range_for_service_with_uuid128(const uint8_t * uuid128, uint16_t * start_handle, uint16_t * end_handle);
396 
397 /**
398  * @brief Get value handle.
399  * @param start_handle
400  * @param end_handle
401  * @param uuid128
402  * @return 0 if not found
403  */
404 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128);
405 
406 /**
407  * @brief Get client configuration handle.
408  * @param start_handle
409  * @param end_handle
410  * @param uuid128
411  * @return 0 if not found
412  */
413 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128);
414 
415 /* API_END */
416 
417 // non-user functionality for att_server
418 
419 /**
420  * @brief Check if writes to handle should be persistent
421  * @param handle
422  * @return 1 if persistent
423  */
424 bool att_is_persistent_ccc(uint16_t handle);
425 
426 
427 
428 // auto-pts testing, returns response size
429 #ifdef ENABLE_BTP
430 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);
431 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);
432 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);
433 #endif
434 
435 #if defined __cplusplus
436 }
437 #endif
438 
439 #endif // ATT_H
440