xref: /btstack/src/ble/att_db.h (revision 8ac574d6faacb291043ffa9b8cfaefc28781cf8c)
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"
44*8ac574d6SMatthias Ringwald #include "btstack_linked_list.h"
45591423b2SMatthias Ringwald 
46591423b2SMatthias Ringwald #if defined __cplusplus
47591423b2SMatthias Ringwald extern "C" {
48591423b2SMatthias Ringwald #endif
49591423b2SMatthias Ringwald 
50591423b2SMatthias Ringwald // custom BTstack error codes
51591423b2SMatthias Ringwald #define ATT_ERROR_HCI_DISCONNECT_RECEIVED          0x1f
52591423b2SMatthias Ringwald 
53*8ac574d6SMatthias Ringwald // custom BTstack ATT error codes
54591423b2SMatthias Ringwald #define ATT_ERROR_DATA_MISMATCH                    0x7e
55591423b2SMatthias Ringwald #define ATT_ERROR_TIMEOUT                          0x7F
56591423b2SMatthias Ringwald 
57591423b2SMatthias Ringwald typedef struct att_connection {
58711e6c80SMatthias Ringwald     hci_con_handle_t con_handle;
59591423b2SMatthias Ringwald     uint16_t mtu;       // initialized to ATT_DEFAULT_MTU (23), negotiated during MTU exchange
60591423b2SMatthias Ringwald     uint16_t max_mtu;   // local maximal L2CAP_MTU, set to l2cap_max_le_mtu()
61591423b2SMatthias Ringwald     uint8_t  encryption_key_size;
62591423b2SMatthias Ringwald     uint8_t  authenticated;
63591423b2SMatthias Ringwald     uint8_t  authorized;
64591423b2SMatthias Ringwald } att_connection_t;
65591423b2SMatthias Ringwald 
66591423b2SMatthias Ringwald // ATT Client Read Callback for Dynamic Data
67591423b2SMatthias Ringwald // - if buffer == NULL, don't copy data, just return size of value
68591423b2SMatthias Ringwald // - if buffer != NULL, copy data and return number bytes copied
69591423b2SMatthias Ringwald // @param con_handle of hci le connection
70591423b2SMatthias Ringwald // @param attribute_handle to be read
71591423b2SMatthias Ringwald // @param offset defines start of attribute value
72591423b2SMatthias Ringwald // @param buffer
73591423b2SMatthias Ringwald // @param buffer_size
74711e6c80SMatthias 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);
75591423b2SMatthias Ringwald 
76591423b2SMatthias Ringwald // ATT Client Write Callback for Dynamic Data
77591423b2SMatthias Ringwald // @param con_handle of hci le connection
78591423b2SMatthias Ringwald // @param attribute_handle to be written
79591423b2SMatthias Ringwald // @param transaction - ATT_TRANSACTION_MODE_NONE for regular writes, ATT_TRANSACTION_MODE_ACTIVE for prepared writes and ATT_TRANSACTION_MODE_EXECUTE
80591423b2SMatthias Ringwald // @param offset into the value - used for queued writes and long attributes
81591423b2SMatthias Ringwald // @param buffer
82591423b2SMatthias Ringwald // @param buffer_size
83591423b2SMatthias Ringwald // @param signature used for signed write commmands
84591423b2SMatthias 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
85711e6c80SMatthias 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);
86591423b2SMatthias Ringwald 
87*8ac574d6SMatthias Ringwald 
88*8ac574d6SMatthias Ringwald // Read & Write Callbacks for handle range
89*8ac574d6SMatthias Ringwald typedef struct att_service_handler {
90*8ac574d6SMatthias Ringwald   btstack_linked_item_t * item;
91*8ac574d6SMatthias Ringwald   uint16_t start_handle;
92*8ac574d6SMatthias Ringwald   uint16_t end_handle;
93*8ac574d6SMatthias Ringwald   att_read_callback_t read_callback;
94*8ac574d6SMatthias Ringwald   att_write_callback_t write_callback;
95*8ac574d6SMatthias Ringwald } att_service_handler_t;
96*8ac574d6SMatthias Ringwald 
97591423b2SMatthias Ringwald // MARK: ATT Operations
98591423b2SMatthias Ringwald 
99591423b2SMatthias Ringwald /*
100591423b2SMatthias Ringwald  * @brief setup ATT database
101591423b2SMatthias Ringwald  */
102591423b2SMatthias Ringwald void att_set_db(uint8_t const * db);
103591423b2SMatthias Ringwald 
104591423b2SMatthias Ringwald /*
105591423b2SMatthias Ringwald  * @brief set callback for read of dynamic attributes
106591423b2SMatthias Ringwald  * @param callback
107591423b2SMatthias Ringwald  */
108591423b2SMatthias Ringwald void att_set_read_callback(att_read_callback_t callback);
109591423b2SMatthias Ringwald 
110591423b2SMatthias Ringwald /*
111591423b2SMatthias Ringwald  * @brief set callback for write of dynamic attributes
112591423b2SMatthias Ringwald  * @param callback
113591423b2SMatthias Ringwald  */
114591423b2SMatthias Ringwald void att_set_write_callback(att_write_callback_t callback);
115591423b2SMatthias Ringwald 
116591423b2SMatthias Ringwald /*
117591423b2SMatthias Ringwald  * @brief debug helper, dump ATT database to stdout using log_info
118591423b2SMatthias Ringwald  */
119591423b2SMatthias Ringwald void att_dump_attributes(void);
120591423b2SMatthias Ringwald 
121591423b2SMatthias Ringwald /*
122591423b2SMatthias Ringwald  * @brief process ATT request against database and put response into response buffer
123591423b2SMatthias Ringwald  * @param att_connection used for mtu and security properties
124591423b2SMatthias Ringwald  * @param request_buffer, request_len: ATT request from clinet
125591423b2SMatthias Ringwald  * @param response_buffer for result
126591423b2SMatthias Ringwald  * @returns len of data in response buffer. 0 = no response
127591423b2SMatthias Ringwald  */
128591423b2SMatthias Ringwald uint16_t att_handle_request(att_connection_t * att_connection,
129591423b2SMatthias Ringwald                             uint8_t * request_buffer,
130591423b2SMatthias Ringwald                             uint16_t request_len,
131591423b2SMatthias Ringwald                             uint8_t * response_buffer);
132591423b2SMatthias Ringwald 
133591423b2SMatthias Ringwald /*
134591423b2SMatthias Ringwald  * @brief setup value notification in response buffer for a given handle and value
135591423b2SMatthias Ringwald  * @param att_connection
136fc64f94aSMatthias Ringwald  * @param attribute_handle
137fc64f94aSMatthias Ringwald  * @param value
138fc64f94aSMatthias Ringwald  * @param value_len
139591423b2SMatthias Ringwald  * @param response_buffer for notification
140591423b2SMatthias Ringwald  */
141591423b2SMatthias Ringwald uint16_t att_prepare_handle_value_notification(att_connection_t * att_connection,
142fc64f94aSMatthias Ringwald                                                uint16_t attribute_handle,
143591423b2SMatthias Ringwald                                                uint8_t *value,
144591423b2SMatthias Ringwald                                                uint16_t value_len,
145591423b2SMatthias Ringwald                                                uint8_t * response_buffer);
146591423b2SMatthias Ringwald 
147591423b2SMatthias Ringwald /*
148591423b2SMatthias Ringwald  * @brief setup value indication in response buffer for a given handle and value
149591423b2SMatthias Ringwald  * @param att_connection
150fc64f94aSMatthias Ringwald  * @param attribute_handle
151fc64f94aSMatthias Ringwald  * @param value
152fc64f94aSMatthias Ringwald  * @param value_len
153591423b2SMatthias Ringwald  * @param response_buffer for indication
154591423b2SMatthias Ringwald  */
155591423b2SMatthias Ringwald uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection,
156fc64f94aSMatthias Ringwald                                              uint16_t attribute_handle,
157591423b2SMatthias Ringwald                                              uint8_t *value,
158591423b2SMatthias Ringwald                                              uint16_t value_len,
159591423b2SMatthias Ringwald                                              uint8_t * response_buffer);
160591423b2SMatthias Ringwald 
161591423b2SMatthias Ringwald /*
162591423b2SMatthias Ringwald  * @brief transcation queue of prepared writes, e.g., after disconnect
163591423b2SMatthias Ringwald  */
164591423b2SMatthias Ringwald void att_clear_transaction_queue(att_connection_t * att_connection);
165591423b2SMatthias Ringwald 
166*8ac574d6SMatthias Ringwald /**
167*8ac574d6SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
168*8ac574d6SMatthias Ringwald  * @param att_service_handler_t
169*8ac574d6SMatthias Ringwald  */
170*8ac574d6SMatthias Ringwald void att_register_service_handler(att_service_handler_t * handler);
171*8ac574d6SMatthias Ringwald 
172*8ac574d6SMatthias Ringwald 
173591423b2SMatthias Ringwald  // experimental client API
174fc64f94aSMatthias Ringwald uint16_t att_uuid_for_handle(uint16_t attribute_handle);
175591423b2SMatthias Ringwald 
176591423b2SMatthias Ringwald #if defined __cplusplus
177591423b2SMatthias Ringwald }
178591423b2SMatthias Ringwald #endif
179591423b2SMatthias Ringwald 
180591423b2SMatthias Ringwald #endif // __ATT_H
181