xref: /btstack/src/ble/att_db.h (revision 0561b2d8d5dba972c7daa57d5e677f7a1327edfd)
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     uint8_t  encryption_key_size;
113     uint8_t  authenticated;
114     uint8_t  authorized;
115     uint8_t  secure_connection;
116 } att_connection_t;
117 
118 // ATT Client Read Callback for Dynamic Data
119 // - if buffer == NULL, don't copy data, just return size of value
120 // - if buffer != NULL, copy data and return number bytes copied
121 // If ENABLE_ATT_DELAYED_READ_RESPONSE is defined, you may return ATT_READ_RESPONSE_PENDING if data isn't available yet
122 // @param con_handle of hci le connection
123 // @param attribute_handle to be read
124 // @param offset defines start of attribute value
125 // @param buffer
126 // @param buffer_size
127 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);
128 
129 // ATT Client Write Callback for Dynamic Data
130 // @param con_handle of hci le connection
131 // @param attribute_handle to be written
132 // @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
133 // @param offset into the value - used for queued writes and long attributes
134 // @param buffer
135 // @param buffer_size
136 // @param signature used for signed write commmands
137 // @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
138 //
139 // Each Prepared Write Request triggers a callback with transaction mode ATT_TRANSACTION_MODE_ACTIVE.
140 // 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.
141 // 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.
142 // Otherwise, all callbacks will be called with ATT_TRANSACTION_MODE_CANCEL.
143 //
144 // If the additional validation step is not needed, just return 0 for all callbacks with transaction mode ATT_TRANSACTION_MODE_VALIDATE.
145 //
146 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);
147 
148 // Read & Write Callbacks for handle range
149 typedef struct att_service_handler {
150     btstack_linked_item_t * item;
151     uint16_t start_handle;
152     uint16_t end_handle;
153     att_read_callback_t read_callback;
154     att_write_callback_t write_callback;
155     btstack_packet_handler_t packet_handler;
156 } att_service_handler_t;
157 
158 // MARK: ATT Operations
159 
160 /*
161  * @brief setup ATT database
162  */
163 void att_set_db(uint8_t const * db);
164 
165 /*
166  * @brief set callback for read of dynamic attributes
167  * @param callback
168  */
169 void att_set_read_callback(att_read_callback_t callback);
170 
171 /*
172  * @brief set callback for write of dynamic attributes
173  * @param callback
174  */
175 void att_set_write_callback(att_write_callback_t callback);
176 
177 /*
178  * @brief debug helper, dump ATT database to stdout using log_info
179  */
180 void att_dump_attributes(void);
181 
182 /*
183  * @brief process ATT request against database and put response into response buffer
184  * @param att_connection used for mtu and security properties
185  * @param request_buffer, request_len: ATT request from clinet
186  * @param response_buffer for result
187  * @returns len of data in response buffer. 0 = no response,
188  *          ATT_READ_RESPONSE_PENDING if it was returned at least once for dynamic data (requires ENABLE_ATT_DELAYED_READ_RESPONSE)
189  */
190 uint16_t att_handle_request(att_connection_t * att_connection,
191                             uint8_t * request_buffer,
192                             uint16_t request_len,
193                             uint8_t * response_buffer);
194 
195 /*
196  * @brief setup value notification in response buffer for a given handle and value
197  * @param att_connection
198  * @param attribute_handle
199  * @param value
200  * @param value_len
201  * @param response_buffer for notification
202  */
203 uint16_t att_prepare_handle_value_notification(att_connection_t * att_connection,
204                                                uint16_t attribute_handle,
205                                                const uint8_t *value,
206                                                uint16_t value_len,
207                                                uint8_t * response_buffer);
208 
209 /*
210  * @brief setup value indication in response buffer for a given handle and value
211  * @param att_connection
212  * @param attribute_handle
213  * @param value
214  * @param value_len
215  * @param response_buffer for indication
216  */
217 uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection,
218                                              uint16_t attribute_handle,
219                                              const uint8_t *value,
220                                              uint16_t value_len,
221                                              uint8_t * response_buffer);
222 
223 /*
224  * @brief transcation queue of prepared writes, e.g., after disconnect
225  */
226 void att_clear_transaction_queue(att_connection_t * att_connection);
227 
228 // att_read_callback helpers for a various data types
229 
230 /*
231  * @brief Handle read of blob like data for att_read_callback
232  * @param blob of data
233  * @param blob_size of blob
234  * @param offset from att_read_callback
235  * @param buffer from att_read_callback
236  * @param buffer_size from att_read_callback
237  * @returns value size for buffer == 0 and num bytes copied otherwise
238  */
239 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);
240 
241 /*
242  * @brief Handle read of little endian unsigned 32 bit value for att_read_callback
243  * @param value
244  * @param offset from att_read_callback
245  * @param buffer from att_read_callback
246  * @param buffer_size from att_read_callback
247  * @returns value size for buffer == 0 and num bytes copied otherwise
248  */
249 uint16_t att_read_callback_handle_little_endian_32(uint32_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
250 
251 /*
252  * @brief Handle read of little endian unsigned 16 bit value for att_read_callback
253  * @param value
254  * @param offset from att_read_callback
255  * @param buffer from att_read_callback
256  * @param buffer_size from att_read_callback
257  * @returns value size for buffer == 0 and num bytes copied otherwise
258  */
259 uint16_t att_read_callback_handle_little_endian_16(uint16_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
260 
261 /*
262  * @brief Handle read of single byte for att_read_callback
263  * @param blob of data
264  * @param blob_size of blob
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_byte(uint8_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
271 
272 
273 // experimental client API
274 uint16_t att_uuid_for_handle(uint16_t attribute_handle);
275 
276 
277 // experimental GATT Server API
278 
279 // returns true if service found. only primary service.
280 bool gatt_server_get_get_handle_range_for_service_with_uuid16(uint16_t uuid16, uint16_t * start_handle, uint16_t * end_handle);
281 
282 // returns 0 if not found
283 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16);
284 
285 // returns 0 if not found
286 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);
287 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16);
288 uint16_t gatt_server_get_server_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16);
289 
290 
291 // returns 1 if service found. only primary service.
292 int gatt_server_get_get_handle_range_for_service_with_uuid128(const uint8_t * uuid128, uint16_t * start_handle, uint16_t * end_handle);
293 
294 // returns 0 if not found
295 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128);
296 
297 // returns 0 if not found
298 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128);
299 
300 // non-user functionality for att_server
301 
302 /*
303  * @brief Check if writes to handle should be persistent
304  * @param handle
305  * @returns 1 if persistent
306  */
307 bool att_is_persistent_ccc(uint16_t handle);
308 
309 
310 #if defined __cplusplus
311 }
312 #endif
313 
314 #endif // ATT_H
315