xref: /btstack/src/ble/att_db.h (revision a06bcae0f7f63d9d69b2f846d04300ea7058ea66)
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_H
40 #define __ATT_H
41 
42 #include <stdint.h>
43 #include "bluetooth.h"
44 #include "btstack_linked_list.h"
45 
46 #if defined __cplusplus
47 extern "C" {
48 #endif
49 
50 // custom BTstack error codes
51 #define ATT_ERROR_HCI_DISCONNECT_RECEIVED          0x1f
52 
53 // custom BTstack ATT error codes
54 #define ATT_ERROR_DATA_MISMATCH                    0x7e
55 #define ATT_ERROR_TIMEOUT                          0x7F
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 } att_connection_t;
65 
66 // ATT Client Read Callback for Dynamic Data
67 // - if buffer == NULL, don't copy data, just return size of value
68 // - if buffer != NULL, copy data and return number bytes copied
69 // @param con_handle of hci le connection
70 // @param attribute_handle to be read
71 // @param offset defines start of attribute value
72 // @param buffer
73 // @param buffer_size
74 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);
75 
76 // ATT Client Write Callback for Dynamic Data
77 // @param con_handle of hci le connection
78 // @param attribute_handle to be written
79 // @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
80 // @param offset into the value - used for queued writes and long attributes
81 // @param buffer
82 // @param buffer_size
83 // @param signature used for signed write commmands
84 // @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
85 //
86 // Each Prepared Write Request triggers a callback with transaction mode ATT_TRANSACTION_MODE_ACTIVE.
87 // 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.
88 // 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.
89 // Otherwise, all callbacks will be called with ATT_TRANSACTION_MODE_CANCEL.
90 //
91 // If the additional validation step is not needed, just return 0 for all callbacks with transaction mode ATT_TRANSACTION_MODE_VALIDATE.
92 //
93 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);
94 
95 // Read & Write Callbacks for handle range
96 typedef struct att_service_handler {
97   btstack_linked_item_t * item;
98   uint16_t start_handle;
99   uint16_t end_handle;
100   att_read_callback_t read_callback;
101   att_write_callback_t write_callback;
102 } att_service_handler_t;
103 
104 // MARK: ATT Operations
105 
106 /*
107  * @brief setup ATT database
108  */
109 void att_set_db(uint8_t const * db);
110 
111 /*
112  * @brief set callback for read of dynamic attributes
113  * @param callback
114  */
115 void att_set_read_callback(att_read_callback_t callback);
116 
117 /*
118  * @brief set callback for write of dynamic attributes
119  * @param callback
120  */
121 void att_set_write_callback(att_write_callback_t callback);
122 
123 /*
124  * @brief debug helper, dump ATT database to stdout using log_info
125  */
126 void att_dump_attributes(void);
127 
128 /*
129  * @brief process ATT request against database and put response into response buffer
130  * @param att_connection used for mtu and security properties
131  * @param request_buffer, request_len: ATT request from clinet
132  * @param response_buffer for result
133  * @returns len of data in response buffer. 0 = no response
134  */
135 uint16_t att_handle_request(att_connection_t * att_connection,
136                             uint8_t * request_buffer,
137                             uint16_t request_len,
138                             uint8_t * response_buffer);
139 
140 /*
141  * @brief setup value notification in response buffer for a given handle and value
142  * @param att_connection
143  * @param attribute_handle
144  * @param value
145  * @param value_len
146  * @param response_buffer for notification
147  */
148 uint16_t att_prepare_handle_value_notification(att_connection_t * att_connection,
149                                                uint16_t attribute_handle,
150                                                uint8_t *value,
151                                                uint16_t value_len,
152                                                uint8_t * response_buffer);
153 
154 /*
155  * @brief setup value indication in response buffer for a given handle and value
156  * @param att_connection
157  * @param attribute_handle
158  * @param value
159  * @param value_len
160  * @param response_buffer for indication
161  */
162 uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection,
163                                              uint16_t attribute_handle,
164                                              uint8_t *value,
165                                              uint16_t value_len,
166                                              uint8_t * response_buffer);
167 
168 /*
169  * @brief transcation queue of prepared writes, e.g., after disconnect
170  */
171 void att_clear_transaction_queue(att_connection_t * att_connection);
172 
173 /**
174  * @brief register read/write callbacks for specific handle range
175  * @param att_service_handler_t
176  */
177 void att_register_service_handler(att_service_handler_t * handler);
178 
179 
180  // experimental client API
181 uint16_t att_uuid_for_handle(uint16_t attribute_handle);
182 
183 
184 // experimental GATT Server API
185 
186 // returns 1 if service found. only primary service.
187 int gatt_server_get_get_handle_range_for_service_with_uuid16(uint16_t uuid16, uint16_t * start_handle, uint16_t * end_handle);
188 
189 // returns 0 if not found
190 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16);
191 
192 // returns 0 if not found
193 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16);
194 
195 #if defined __cplusplus
196 }
197 #endif
198 
199 #endif // __ATT_H
200