xref: /btstack/src/ble/att_db.h (revision fc64f94a8301d3d0e398cde6b9b488722d71c2fc)
1591423b2SMatthias Ringwald /*
2591423b2SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3591423b2SMatthias Ringwald  *
4591423b2SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5591423b2SMatthias Ringwald  * modification, are permitted provided that the following conditions
6591423b2SMatthias Ringwald  * are met:
7591423b2SMatthias Ringwald  *
8591423b2SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9591423b2SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10591423b2SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11591423b2SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12591423b2SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13591423b2SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14591423b2SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15591423b2SMatthias Ringwald  *    from this software without specific prior written permission.
16591423b2SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17591423b2SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18591423b2SMatthias Ringwald  *    monetary gain.
19591423b2SMatthias Ringwald  *
20591423b2SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21591423b2SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22591423b2SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23591423b2SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24591423b2SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25591423b2SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26591423b2SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27591423b2SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28591423b2SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29591423b2SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30591423b2SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31591423b2SMatthias Ringwald  * SUCH DAMAGE.
32591423b2SMatthias Ringwald  *
33591423b2SMatthias Ringwald  * Please inquire about commercial licensing options at
34591423b2SMatthias Ringwald  * [email protected]
35591423b2SMatthias Ringwald  *
36591423b2SMatthias Ringwald  */
37591423b2SMatthias Ringwald 
38591423b2SMatthias Ringwald 
39591423b2SMatthias Ringwald #ifndef __ATT_H
40591423b2SMatthias Ringwald #define __ATT_H
41591423b2SMatthias Ringwald 
42591423b2SMatthias Ringwald #include <stdint.h>
43711e6c80SMatthias Ringwald #include "bluetooth.h"
44591423b2SMatthias Ringwald 
45591423b2SMatthias Ringwald #if defined __cplusplus
46591423b2SMatthias Ringwald extern "C" {
47591423b2SMatthias Ringwald #endif
48591423b2SMatthias Ringwald 
49591423b2SMatthias Ringwald // custom BTstack error codes
50591423b2SMatthias Ringwald #define ATT_ERROR_HCI_DISCONNECT_RECEIVED          0x1f
51591423b2SMatthias Ringwald 
52591423b2SMatthias Ringwald // custom BTstack ATT error coders
53591423b2SMatthias Ringwald #define ATT_ERROR_DATA_MISMATCH                    0x7e
54591423b2SMatthias Ringwald #define ATT_ERROR_TIMEOUT                          0x7F
55591423b2SMatthias Ringwald 
56591423b2SMatthias Ringwald typedef struct att_connection {
57711e6c80SMatthias Ringwald     hci_con_handle_t con_handle;
58591423b2SMatthias Ringwald     uint16_t mtu;       // initialized to ATT_DEFAULT_MTU (23), negotiated during MTU exchange
59591423b2SMatthias Ringwald     uint16_t max_mtu;   // local maximal L2CAP_MTU, set to l2cap_max_le_mtu()
60591423b2SMatthias Ringwald     uint8_t  encryption_key_size;
61591423b2SMatthias Ringwald     uint8_t  authenticated;
62591423b2SMatthias Ringwald     uint8_t  authorized;
63591423b2SMatthias Ringwald } att_connection_t;
64591423b2SMatthias Ringwald 
65591423b2SMatthias Ringwald // ATT Client Read Callback for Dynamic Data
66591423b2SMatthias Ringwald // - if buffer == NULL, don't copy data, just return size of value
67591423b2SMatthias Ringwald // - if buffer != NULL, copy data and return number bytes copied
68591423b2SMatthias Ringwald // @param con_handle of hci le connection
69591423b2SMatthias Ringwald // @param attribute_handle to be read
70591423b2SMatthias Ringwald // @param offset defines start of attribute value
71591423b2SMatthias Ringwald // @param buffer
72591423b2SMatthias Ringwald // @param buffer_size
73711e6c80SMatthias Ringwald 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);
74591423b2SMatthias Ringwald 
75591423b2SMatthias Ringwald // ATT Client Write Callback for Dynamic Data
76591423b2SMatthias Ringwald // @param con_handle of hci le connection
77591423b2SMatthias Ringwald // @param attribute_handle to be written
78591423b2SMatthias Ringwald // @param transaction - ATT_TRANSACTION_MODE_NONE for regular writes, ATT_TRANSACTION_MODE_ACTIVE for prepared writes and ATT_TRANSACTION_MODE_EXECUTE
79591423b2SMatthias Ringwald // @param offset into the value - used for queued writes and long attributes
80591423b2SMatthias Ringwald // @param buffer
81591423b2SMatthias Ringwald // @param buffer_size
82591423b2SMatthias Ringwald // @param signature used for signed write commmands
83591423b2SMatthias Ringwald // @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
84711e6c80SMatthias Ringwald 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);
85591423b2SMatthias Ringwald 
86591423b2SMatthias Ringwald // MARK: ATT Operations
87591423b2SMatthias Ringwald 
88591423b2SMatthias Ringwald /*
89591423b2SMatthias Ringwald  * @brief setup ATT database
90591423b2SMatthias Ringwald  */
91591423b2SMatthias Ringwald void att_set_db(uint8_t const * db);
92591423b2SMatthias Ringwald 
93591423b2SMatthias Ringwald /*
94591423b2SMatthias Ringwald  * @brief set callback for read of dynamic attributes
95591423b2SMatthias Ringwald  * @param callback
96591423b2SMatthias Ringwald  */
97591423b2SMatthias Ringwald void att_set_read_callback(att_read_callback_t callback);
98591423b2SMatthias Ringwald 
99591423b2SMatthias Ringwald /*
100591423b2SMatthias Ringwald  * @brief set callback for write of dynamic attributes
101591423b2SMatthias Ringwald  * @param callback
102591423b2SMatthias Ringwald  */
103591423b2SMatthias Ringwald void att_set_write_callback(att_write_callback_t callback);
104591423b2SMatthias Ringwald 
105591423b2SMatthias Ringwald /*
106591423b2SMatthias Ringwald  * @brief debug helper, dump ATT database to stdout using log_info
107591423b2SMatthias Ringwald  */
108591423b2SMatthias Ringwald void att_dump_attributes(void);
109591423b2SMatthias Ringwald 
110591423b2SMatthias Ringwald /*
111591423b2SMatthias Ringwald  * @brief process ATT request against database and put response into response buffer
112591423b2SMatthias Ringwald  * @param att_connection used for mtu and security properties
113591423b2SMatthias Ringwald  * @param request_buffer, request_len: ATT request from clinet
114591423b2SMatthias Ringwald  * @param response_buffer for result
115591423b2SMatthias Ringwald  * @returns len of data in response buffer. 0 = no response
116591423b2SMatthias Ringwald  */
117591423b2SMatthias Ringwald uint16_t att_handle_request(att_connection_t * att_connection,
118591423b2SMatthias Ringwald                             uint8_t * request_buffer,
119591423b2SMatthias Ringwald                             uint16_t request_len,
120591423b2SMatthias Ringwald                             uint8_t * response_buffer);
121591423b2SMatthias Ringwald 
122591423b2SMatthias Ringwald /*
123591423b2SMatthias Ringwald  * @brief setup value notification in response buffer for a given handle and value
124591423b2SMatthias Ringwald  * @param att_connection
125*fc64f94aSMatthias Ringwald  * @param attribute_handle
126*fc64f94aSMatthias Ringwald  * @param value
127*fc64f94aSMatthias Ringwald  * @param value_len
128591423b2SMatthias Ringwald  * @param response_buffer for notification
129591423b2SMatthias Ringwald  */
130591423b2SMatthias Ringwald uint16_t att_prepare_handle_value_notification(att_connection_t * att_connection,
131*fc64f94aSMatthias Ringwald                                                uint16_t attribute_handle,
132591423b2SMatthias Ringwald                                                uint8_t *value,
133591423b2SMatthias Ringwald                                                uint16_t value_len,
134591423b2SMatthias Ringwald                                                uint8_t * response_buffer);
135591423b2SMatthias Ringwald 
136591423b2SMatthias Ringwald /*
137591423b2SMatthias Ringwald  * @brief setup value indication in response buffer for a given handle and value
138591423b2SMatthias Ringwald  * @param att_connection
139*fc64f94aSMatthias Ringwald  * @param attribute_handle
140*fc64f94aSMatthias Ringwald  * @param value
141*fc64f94aSMatthias Ringwald  * @param value_len
142591423b2SMatthias Ringwald  * @param response_buffer for indication
143591423b2SMatthias Ringwald  */
144591423b2SMatthias Ringwald uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection,
145*fc64f94aSMatthias Ringwald                                              uint16_t attribute_handle,
146591423b2SMatthias Ringwald                                              uint8_t *value,
147591423b2SMatthias Ringwald                                              uint16_t value_len,
148591423b2SMatthias Ringwald                                              uint8_t * response_buffer);
149591423b2SMatthias Ringwald 
150591423b2SMatthias Ringwald /*
151591423b2SMatthias Ringwald  * @brief transcation queue of prepared writes, e.g., after disconnect
152591423b2SMatthias Ringwald  */
153591423b2SMatthias Ringwald void att_clear_transaction_queue(att_connection_t * att_connection);
154591423b2SMatthias Ringwald 
155591423b2SMatthias Ringwald // experimental client API
156*fc64f94aSMatthias Ringwald uint16_t att_uuid_for_handle(uint16_t attribute_handle);
157591423b2SMatthias Ringwald 
158591423b2SMatthias Ringwald #if defined __cplusplus
159591423b2SMatthias Ringwald }
160591423b2SMatthias Ringwald #endif
161591423b2SMatthias Ringwald 
162591423b2SMatthias Ringwald #endif // __ATT_H
163