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