xref: /btstack/src/ble/att_db.h (revision 046b44372d25c7bd6fe78e5cf6e5176a1979f437)
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 
47 #if defined __cplusplus
48 extern "C" {
49 #endif
50 
51 // custom BTstack ATT Response Pending for att_read_callback
52 #define ATT_READ_RESPONSE_PENDING                 0xffff
53 
54 // internally used to signal write response pending
55 #define ATT_INTERNAL_WRITE_RESPONSE_PENDING       0xfffe
56 
57 typedef struct att_connection {
58     hci_con_handle_t con_handle;
59     uint16_t mtu;       // initialized to ATT_DEFAULT_MTU (23), negotiated during MTU exchange
60     uint16_t max_mtu;   // local maximal L2CAP_MTU, set to l2cap_max_le_mtu()
61     uint8_t  encryption_key_size;
62     uint8_t  authenticated;
63     uint8_t  authorized;
64     uint8_t  secure_connection;
65 } att_connection_t;
66 
67 // ATT Client Read Callback for Dynamic Data
68 // - if buffer == NULL, don't copy data, just return size of value
69 // - if buffer != NULL, copy data and return number bytes copied
70 // If ENABLE_ATT_DELAYED_READ_RESPONSE is defined, you may return ATT_READ_RESPONSE_PENDING if data isn't available yet
71 // @param con_handle of hci le connection
72 // @param attribute_handle to be read
73 // @param offset defines start of attribute value
74 // @param buffer
75 // @param buffer_size
76 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);
77 
78 // ATT Client Write Callback for Dynamic Data
79 // @param con_handle of hci le connection
80 // @param attribute_handle to be written
81 // @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
82 // @param offset into the value - used for queued writes and long attributes
83 // @param buffer
84 // @param buffer_size
85 // @param signature used for signed write commmands
86 // @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
87 //
88 // Each Prepared Write Request triggers a callback with transaction mode ATT_TRANSACTION_MODE_ACTIVE.
89 // 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.
90 // 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.
91 // Otherwise, all callbacks will be called with ATT_TRANSACTION_MODE_CANCEL.
92 //
93 // If the additional validation step is not needed, just return 0 for all callbacks with transaction mode ATT_TRANSACTION_MODE_VALIDATE.
94 //
95 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);
96 
97 // Read & Write Callbacks for handle range
98 typedef struct att_service_handler {
99     btstack_linked_item_t * item;
100     uint16_t start_handle;
101     uint16_t end_handle;
102     att_read_callback_t read_callback;
103     att_write_callback_t write_callback;
104     btstack_packet_handler_t packet_handler;
105 } att_service_handler_t;
106 
107 // MARK: ATT Operations
108 
109 /*
110  * @brief setup ATT database
111  */
112 void att_set_db(uint8_t const * db);
113 
114 /*
115  * @brief set callback for read of dynamic attributes
116  * @param callback
117  */
118 void att_set_read_callback(att_read_callback_t callback);
119 
120 /*
121  * @brief set callback for write of dynamic attributes
122  * @param callback
123  */
124 void att_set_write_callback(att_write_callback_t callback);
125 
126 /*
127  * @brief debug helper, dump ATT database to stdout using log_info
128  */
129 void att_dump_attributes(void);
130 
131 /*
132  * @brief process ATT request against database and put response into response buffer
133  * @param att_connection used for mtu and security properties
134  * @param request_buffer, request_len: ATT request from clinet
135  * @param response_buffer for result
136  * @returns len of data in response buffer. 0 = no response,
137  *          ATT_READ_RESPONSE_PENDING if it was returned at least once for dynamic data (requires ENABLE_ATT_DELAYED_READ_RESPONSE)
138  */
139 uint16_t att_handle_request(att_connection_t * att_connection,
140                             uint8_t * request_buffer,
141                             uint16_t request_len,
142                             uint8_t * response_buffer);
143 
144 /*
145  * @brief setup value notification in response buffer for a given handle and value
146  * @param att_connection
147  * @param attribute_handle
148  * @param value
149  * @param value_len
150  * @param response_buffer for notification
151  */
152 uint16_t att_prepare_handle_value_notification(att_connection_t * att_connection,
153                                                uint16_t attribute_handle,
154                                                const uint8_t *value,
155                                                uint16_t value_len,
156                                                uint8_t * response_buffer);
157 
158 /*
159  * @brief setup value indication in response buffer for a given handle and value
160  * @param att_connection
161  * @param attribute_handle
162  * @param value
163  * @param value_len
164  * @param response_buffer for indication
165  */
166 uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection,
167                                              uint16_t attribute_handle,
168                                              const uint8_t *value,
169                                              uint16_t value_len,
170                                              uint8_t * response_buffer);
171 
172 /*
173  * @brief transcation queue of prepared writes, e.g., after disconnect
174  */
175 void att_clear_transaction_queue(att_connection_t * att_connection);
176 
177 // att_read_callback helpers for a various data types
178 
179 /*
180  * @brief Handle read of blob like data for att_read_callback
181  * @param blob of data
182  * @param blob_size of blob
183  * @param offset from att_read_callback
184  * @param buffer from att_read_callback
185  * @param buffer_size from att_read_callback
186  * @returns value size for buffer == 0 and num bytes copied otherwise
187  */
188 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);
189 
190 /*
191  * @brief Handle read of little endian unsigned 32 bit value for att_read_callback
192  * @param value
193  * @param offset from att_read_callback
194  * @param buffer from att_read_callback
195  * @param buffer_size from att_read_callback
196  * @returns value size for buffer == 0 and num bytes copied otherwise
197  */
198 uint16_t att_read_callback_handle_little_endian_32(uint32_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
199 
200 /*
201  * @brief Handle read of little endian unsigned 16 bit value for att_read_callback
202  * @param value
203  * @param offset from att_read_callback
204  * @param buffer from att_read_callback
205  * @param buffer_size from att_read_callback
206  * @returns value size for buffer == 0 and num bytes copied otherwise
207  */
208 uint16_t att_read_callback_handle_little_endian_16(uint16_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
209 
210 /*
211  * @brief Handle read of single byte for att_read_callback
212  * @param blob of data
213  * @param blob_size of blob
214  * @param offset from att_read_callback
215  * @param buffer from att_read_callback
216  * @param buffer_size from att_read_callback
217  * @returns value size for buffer == 0 and num bytes copied otherwise
218  */
219 uint16_t att_read_callback_handle_byte(uint8_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
220 
221 
222 // experimental client API
223 uint16_t att_uuid_for_handle(uint16_t attribute_handle);
224 
225 
226 // experimental GATT Server API
227 
228 // returns 1 if service found. only primary service.
229 int gatt_server_get_get_handle_range_for_service_with_uuid16(uint16_t uuid16, uint16_t * start_handle, uint16_t * end_handle);
230 
231 // returns 0 if not found
232 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16);
233 
234 // returns 0 if not found
235 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);
236 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16);
237 uint16_t gatt_server_get_server_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16);
238 
239 
240 // returns 1 if service found. only primary service.
241 int gatt_server_get_get_handle_range_for_service_with_uuid128(const uint8_t * uuid128, uint16_t * start_handle, uint16_t * end_handle);
242 
243 // returns 0 if not found
244 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128);
245 
246 // returns 0 if not found
247 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128);
248 
249 // non-user functionality for att_server
250 
251 /*
252  * @brief Check if writes to handle should be persistent
253  * @param handle
254  * @returns 1 if persistent
255  */
256 int att_is_persistent_ccc(uint16_t handle);
257 
258 
259 #if defined __cplusplus
260 }
261 #endif
262 
263 #endif // ATT_H
264