xref: /btstack/src/ble/att_db.h (revision 5ce1359ee5f0fe594f97c68ec2795ef20f59817f)
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  * @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              0x01
58 
59 #define ATT_EXCHANGE_MTU_REQUEST        0x02
60 #define ATT_EXCHANGE_MTU_RESPONSE       0x03
61 
62 #define ATT_FIND_INFORMATION_REQUEST    0x04
63 #define ATT_FIND_INFORMATION_REPLY      0x05
64 #define ATT_FIND_BY_TYPE_VALUE_REQUEST  0x06
65 #define ATT_FIND_BY_TYPE_VALUE_RESPONSE 0x07
66 
67 #define ATT_READ_BY_TYPE_REQUEST        0x08
68 #define ATT_READ_BY_TYPE_RESPONSE       0x09
69 #define ATT_READ_REQUEST                0x0a
70 #define ATT_READ_RESPONSE               0x0b
71 #define ATT_READ_BLOB_REQUEST           0x0c
72 #define ATT_READ_BLOB_RESPONSE          0x0d
73 #define ATT_READ_MULTIPLE_REQUEST       0x0e
74 #define ATT_READ_MULTIPLE_RESPONSE      0x0f
75 #define ATT_READ_BY_GROUP_TYPE_REQUEST  0x10
76 #define ATT_READ_BY_GROUP_TYPE_RESPONSE 0x11
77 
78 #define ATT_WRITE_REQUEST               0x12
79 #define ATT_WRITE_RESPONSE              0x13
80 
81 #define ATT_PREPARE_WRITE_REQUEST       0x16
82 #define ATT_PREPARE_WRITE_RESPONSE      0x17
83 #define ATT_EXECUTE_WRITE_REQUEST       0x18
84 #define ATT_EXECUTE_WRITE_RESPONSE      0x19
85 
86 #define ATT_HANDLE_VALUE_NOTIFICATION   0x1b
87 #define ATT_HANDLE_VALUE_INDICATION     0x1d
88 #define ATT_HANDLE_VALUE_CONFIRMATION   0x1e
89 
90 #define ATT_READ_MULTIPLE_VARIABLE_REQ  0x20
91 #define ATT_READ_MULTIPLE_VARIABLE_RSP  0x21
92 #define ATT_MULTIPLE_HANDLE_VALUE_NTF   0x23
93 
94 #define ATT_WRITE_COMMAND                0x52
95 #define ATT_SIGNED_WRITE_COMMAND         0xD2
96 
97 // map ATT ERROR CODES on to att_read_callback length
98 #define ATT_READ_ERROR_CODE_OFFSET       0xfe00
99 
100 // custom BTstack ATT Response Pending for att_read_callback
101 #define ATT_READ_RESPONSE_PENDING                 0xffff
102 
103 // internally used to signal write response pending
104 #define ATT_INTERNAL_WRITE_RESPONSE_PENDING       0xfffe
105 
106 // internal additions
107 // 128 bit UUID used
108 #define ATT_PROPERTY_UUID128             0x200
109 // Read/Write Permission bits
110 #define ATT_PROPERTY_READ_PERMISSION_BIT_0  0x0400
111 #define ATT_PROPERTY_READ_PERMISSION_BIT_1  0x0800
112 #define ATT_PROPERTY_WRITE_PERMISSION_BIT_0 0x0001
113 #define ATT_PROPERTY_WRITE_PERMISSION_BIT_1 0x0010
114 #define ATT_PROPERTY_READ_PERMISSION_SC     0x0020
115 #define ATT_PROPERTY_WRITE_PERMISSION_SC    0x0080
116 
117 
118 typedef struct att_connection {
119     hci_con_handle_t con_handle;
120     uint16_t mtu;       // initialized to ATT_DEFAULT_MTU (23), negotiated during MTU exchange
121     uint16_t max_mtu;   // local maximal L2CAP_MTU, set to l2cap_max_le_mtu()
122     bool     mtu_exchanged;
123     uint8_t  encryption_key_size;
124     uint8_t  authenticated;
125     uint8_t  authorized;
126     uint8_t  secure_connection;
127 } att_connection_t;
128 
129 // ATT Client Read Callback for Dynamic Data
130 // - if buffer == NULL, don't copy data, just return size of value
131 // - if buffer != NULL, copy data and return number bytes copied
132 // If ENABLE_ATT_DELAYED_READ_RESPONSE is defined, you may return ATT_READ_RESPONSE_PENDING if data isn't available yet
133 // @param con_handle of hci le connection
134 // @param attribute_handle to be read
135 // @param offset defines start of attribute value
136 // @param buffer
137 // @param buffer_size
138 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);
139 
140 // ATT Client Write Callback for Dynamic Data
141 // @param con_handle of hci le connection
142 // @param attribute_handle to be written
143 // @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
144 // @param offset into the value - used for queued writes and long attributes
145 // @param buffer
146 // @param buffer_size
147 // @param signature used for signed write commmands
148 // @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
149 //
150 // Each Prepared Write Request triggers a callback with transaction mode ATT_TRANSACTION_MODE_ACTIVE.
151 // 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.
152 // 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.
153 // Otherwise, all callbacks will be called with ATT_TRANSACTION_MODE_CANCEL.
154 //
155 // If the additional validation step is not needed, just return 0 for all callbacks with transaction mode ATT_TRANSACTION_MODE_VALIDATE.
156 //
157 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);
158 
159 // Read & Write Callbacks for handle range
160 typedef struct att_service_handler {
161     btstack_linked_item_t * item;
162     uint16_t start_handle;
163     uint16_t end_handle;
164     att_read_callback_t read_callback;
165     att_write_callback_t write_callback;
166     btstack_packet_handler_t packet_handler;
167 } att_service_handler_t;
168 
169 // MARK: ATT Operations
170 
171 /*
172  * @brief setup ATT database
173  */
174 void att_set_db(uint8_t const * db);
175 
176 /*
177  * @brief set callback for read of dynamic attributes
178  * @param callback
179  */
180 void att_set_read_callback(att_read_callback_t callback);
181 
182 /*
183  * @brief set callback for write of dynamic attributes
184  * @param callback
185  */
186 void att_set_write_callback(att_write_callback_t callback);
187 
188 /*
189  * @brief debug helper, dump ATT database to stdout using log_info
190  */
191 void att_dump_attributes(void);
192 
193 /*
194  * @brief process ATT request against database and put response into response buffer
195  * @param att_connection used for mtu and security properties
196  * @param request_buffer, request_len: ATT request from clinet
197  * @param response_buffer for result
198  * @returns len of data in response buffer. 0 = no response,
199  *          ATT_READ_RESPONSE_PENDING if it was returned at least once for dynamic data (requires ENABLE_ATT_DELAYED_READ_RESPONSE)
200  */
201 uint16_t att_handle_request(att_connection_t * att_connection,
202                             uint8_t * request_buffer,
203                             uint16_t request_len,
204                             uint8_t * response_buffer);
205 
206 /*
207  * @brief setup value notification in response buffer for a given handle and value
208  * @param att_connection
209  * @param attribute_handle
210  * @param value
211  * @param value_len
212  * @param response_buffer for notification
213  */
214 uint16_t att_prepare_handle_value_notification(att_connection_t * att_connection,
215                                                uint16_t attribute_handle,
216                                                const uint8_t *value,
217                                                uint16_t value_len,
218                                                uint8_t * response_buffer);
219 
220 /*
221  * @brief setup value indication in response buffer for a given handle and value
222  * @param att_connection
223  * @param attribute_handle
224  * @param value
225  * @param value_len
226  * @param response_buffer for indication
227  */
228 uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection,
229                                              uint16_t attribute_handle,
230                                              const uint8_t *value,
231                                              uint16_t value_len,
232                                              uint8_t * response_buffer);
233 
234 /*
235  * @brief transcation queue of prepared writes, e.g., after disconnect
236  */
237 void att_clear_transaction_queue(att_connection_t * att_connection);
238 
239 // att_read_callback helpers for a various data types
240 
241 /*
242  * @brief Handle read of blob like data for att_read_callback
243  * @param blob of data
244  * @param blob_size of blob
245  * @param offset from att_read_callback
246  * @param buffer from att_read_callback
247  * @param buffer_size from att_read_callback
248  * @returns value size for buffer == 0 and num bytes copied otherwise
249  */
250 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);
251 
252 /*
253  * @brief Handle read of little endian unsigned 32 bit value for att_read_callback
254  * @param value
255  * @param offset from att_read_callback
256  * @param buffer from att_read_callback
257  * @param buffer_size from att_read_callback
258  * @returns value size for buffer == 0 and num bytes copied otherwise
259  */
260 uint16_t att_read_callback_handle_little_endian_32(uint32_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
261 
262 /*
263  * @brief Handle read of little endian unsigned 16 bit value for att_read_callback
264  * @param value
265  * @param offset from att_read_callback
266  * @param buffer from att_read_callback
267  * @param buffer_size from att_read_callback
268  * @returns value size for buffer == 0 and num bytes copied otherwise
269  */
270 uint16_t att_read_callback_handle_little_endian_16(uint16_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
271 
272 /*
273  * @brief Handle read of single byte for att_read_callback
274  * @param blob of data
275  * @param blob_size of blob
276  * @param offset from att_read_callback
277  * @param buffer from att_read_callback
278  * @param buffer_size from att_read_callback
279  * @returns value size for buffer == 0 and num bytes copied otherwise
280  */
281 uint16_t att_read_callback_handle_byte(uint8_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
282 
283 
284 // experimental client API
285 uint16_t att_uuid_for_handle(uint16_t attribute_handle);
286 
287 
288 // experimental GATT Server API
289 
290 // returns true if service found. only primary service.
291 bool gatt_server_get_get_handle_range_for_service_with_uuid16(uint16_t uuid16, uint16_t * start_handle, uint16_t * end_handle);
292 
293 // returns 0 if not found
294 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16);
295 
296 // returns 0 if not found
297 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);
298 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16);
299 uint16_t gatt_server_get_server_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16);
300 
301 
302 // returns 1 if service found. only primary service.
303 int gatt_server_get_get_handle_range_for_service_with_uuid128(const uint8_t * uuid128, uint16_t * start_handle, uint16_t * end_handle);
304 
305 // returns 0 if not found
306 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128);
307 
308 // returns 0 if not found
309 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128);
310 
311 // non-user functionality for att_server
312 
313 /*
314  * @brief Check if writes to handle should be persistent
315  * @param handle
316  * @returns 1 if persistent
317  */
318 bool att_is_persistent_ccc(uint16_t handle);
319 
320 // auto-pts testing, returns response size
321 #ifdef ENABLE_BTP
322 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);
323 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);
324 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);
325 #endif
326 
327 #if defined __cplusplus
328 }
329 #endif
330 
331 #endif // ATT_H
332