xref: /btstack/src/ble/att_db.h (revision 8257e5f9e1a9f15ac2fe1e69adc5d2eeeadf3fff)
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 
45 #if defined __cplusplus
46 extern "C" {
47 #endif
48 
49 // custom BTstack error codes
50 #define ATT_ERROR_HCI_DISCONNECT_RECEIVED          0x1f
51 
52 // custom BTstack ATT error coders
53 #define ATT_ERROR_DATA_MISMATCH                    0x7e
54 #define ATT_ERROR_TIMEOUT                          0x7F
55 
56 typedef struct att_connection {
57     hci_con_handle_t con_handle;
58     uint16_t mtu;       // initialized to ATT_DEFAULT_MTU (23), negotiated during MTU exchange
59     uint16_t max_mtu;   // local maximal L2CAP_MTU, set to l2cap_max_le_mtu()
60     uint8_t  encryption_key_size;
61     uint8_t  authenticated;
62     uint8_t  authorized;
63 } att_connection_t;
64 
65 // ATT Client Read Callback for Dynamic Data
66 // - if buffer == NULL, don't copy data, just return size of value
67 // - if buffer != NULL, copy data and return number bytes copied
68 // @param con_handle of hci le connection
69 // @param attribute_handle to be read
70 // @param offset defines start of attribute value
71 // @param buffer
72 // @param buffer_size
73 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);
74 
75 // ATT Client Write Callback for Dynamic Data
76 // @param con_handle of hci le connection
77 // @param attribute_handle to be written
78 // @param transaction - ATT_TRANSACTION_MODE_NONE for regular writes, ATT_TRANSACTION_MODE_ACTIVE for prepared writes and ATT_TRANSACTION_MODE_EXECUTE
79 // @param offset into the value - used for queued writes and long attributes
80 // @param buffer
81 // @param buffer_size
82 // @param signature used for signed write commmands
83 // @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
84 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);
85 
86 // MARK: ATT Operations
87 
88 /*
89  * @brief setup ATT database
90  */
91 void att_set_db(uint8_t const * db);
92 
93 /*
94  * @brief set callback for read of dynamic attributes
95  * @param callback
96  */
97 void att_set_read_callback(att_read_callback_t callback);
98 
99 /*
100  * @brief set callback for write of dynamic attributes
101  * @param callback
102  */
103 void att_set_write_callback(att_write_callback_t callback);
104 
105 /*
106  * @brief debug helper, dump ATT database to stdout using log_info
107  */
108 void att_dump_attributes(void);
109 
110 /*
111  * @brief process ATT request against database and put response into response buffer
112  * @param att_connection used for mtu and security properties
113  * @param request_buffer, request_len: ATT request from clinet
114  * @param response_buffer for result
115  * @returns len of data in response buffer. 0 = no response
116  */
117 uint16_t att_handle_request(att_connection_t * att_connection,
118                             uint8_t * request_buffer,
119                             uint16_t request_len,
120                             uint8_t * response_buffer);
121 
122 /*
123  * @brief setup value notification in response buffer for a given handle and value
124  * @param att_connection
125  * @param attribute_handle
126  * @param value
127  * @param value_len
128  * @param response_buffer for notification
129  */
130 uint16_t att_prepare_handle_value_notification(att_connection_t * att_connection,
131                                                uint16_t attribute_handle,
132                                                uint8_t *value,
133                                                uint16_t value_len,
134                                                uint8_t * response_buffer);
135 
136 /*
137  * @brief setup value indication in response buffer for a given handle and value
138  * @param att_connection
139  * @param attribute_handle
140  * @param value
141  * @param value_len
142  * @param response_buffer for indication
143  */
144 uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection,
145                                              uint16_t attribute_handle,
146                                              uint8_t *value,
147                                              uint16_t value_len,
148                                              uint8_t * response_buffer);
149 
150 /*
151  * @brief transcation queue of prepared writes, e.g., after disconnect
152  */
153 void att_clear_transaction_queue(att_connection_t * att_connection);
154 
155 // experimental client API
156 uint16_t att_uuid_for_handle(uint16_t attribute_handle);
157 
158 #if defined __cplusplus
159 }
160 #endif
161 
162 #endif // __ATT_H
163