xref: /btstack/src/ble/att_db.h (revision 591423b2a599e503fdfb75c3f1c4bbd1c0d3e506)
1*591423b2SMatthias Ringwald /*
2*591423b2SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3*591423b2SMatthias Ringwald  *
4*591423b2SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*591423b2SMatthias Ringwald  * modification, are permitted provided that the following conditions
6*591423b2SMatthias Ringwald  * are met:
7*591423b2SMatthias Ringwald  *
8*591423b2SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*591423b2SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*591423b2SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*591423b2SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*591423b2SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*591423b2SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*591423b2SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*591423b2SMatthias Ringwald  *    from this software without specific prior written permission.
16*591423b2SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*591423b2SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*591423b2SMatthias Ringwald  *    monetary gain.
19*591423b2SMatthias Ringwald  *
20*591423b2SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*591423b2SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*591423b2SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*591423b2SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24*591423b2SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*591423b2SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*591423b2SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*591423b2SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*591423b2SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*591423b2SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*591423b2SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*591423b2SMatthias Ringwald  * SUCH DAMAGE.
32*591423b2SMatthias Ringwald  *
33*591423b2SMatthias Ringwald  * Please inquire about commercial licensing options at
34*591423b2SMatthias Ringwald  * [email protected]
35*591423b2SMatthias Ringwald  *
36*591423b2SMatthias Ringwald  */
37*591423b2SMatthias Ringwald 
38*591423b2SMatthias Ringwald 
39*591423b2SMatthias Ringwald #ifndef __ATT_H
40*591423b2SMatthias Ringwald #define __ATT_H
41*591423b2SMatthias Ringwald 
42*591423b2SMatthias Ringwald #include <stdint.h>
43*591423b2SMatthias Ringwald 
44*591423b2SMatthias Ringwald #if defined __cplusplus
45*591423b2SMatthias Ringwald extern "C" {
46*591423b2SMatthias Ringwald #endif
47*591423b2SMatthias Ringwald 
48*591423b2SMatthias Ringwald // custom BTstack error codes
49*591423b2SMatthias Ringwald #define ATT_ERROR_HCI_DISCONNECT_RECEIVED          0x1f
50*591423b2SMatthias Ringwald 
51*591423b2SMatthias Ringwald // custom BTstack ATT error coders
52*591423b2SMatthias Ringwald #define ATT_ERROR_DATA_MISMATCH                    0x7e
53*591423b2SMatthias Ringwald #define ATT_ERROR_TIMEOUT                          0x7F
54*591423b2SMatthias Ringwald 
55*591423b2SMatthias Ringwald typedef struct att_connection {
56*591423b2SMatthias Ringwald     uint16_t con_handle;
57*591423b2SMatthias Ringwald     uint16_t mtu;       // initialized to ATT_DEFAULT_MTU (23), negotiated during MTU exchange
58*591423b2SMatthias Ringwald     uint16_t max_mtu;   // local maximal L2CAP_MTU, set to l2cap_max_le_mtu()
59*591423b2SMatthias Ringwald     uint8_t  encryption_key_size;
60*591423b2SMatthias Ringwald     uint8_t  authenticated;
61*591423b2SMatthias Ringwald     uint8_t  authorized;
62*591423b2SMatthias Ringwald } att_connection_t;
63*591423b2SMatthias Ringwald 
64*591423b2SMatthias Ringwald // ATT Client Read Callback for Dynamic Data
65*591423b2SMatthias Ringwald // - if buffer == NULL, don't copy data, just return size of value
66*591423b2SMatthias Ringwald // - if buffer != NULL, copy data and return number bytes copied
67*591423b2SMatthias Ringwald // @param con_handle of hci le connection
68*591423b2SMatthias Ringwald // @param attribute_handle to be read
69*591423b2SMatthias Ringwald // @param offset defines start of attribute value
70*591423b2SMatthias Ringwald // @param buffer
71*591423b2SMatthias Ringwald // @param buffer_size
72*591423b2SMatthias Ringwald typedef uint16_t (*att_read_callback_t)(uint16_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
73*591423b2SMatthias Ringwald 
74*591423b2SMatthias Ringwald // ATT Client Write Callback for Dynamic Data
75*591423b2SMatthias Ringwald // @param con_handle of hci le connection
76*591423b2SMatthias Ringwald // @param attribute_handle to be written
77*591423b2SMatthias Ringwald // @param transaction - ATT_TRANSACTION_MODE_NONE for regular writes, ATT_TRANSACTION_MODE_ACTIVE for prepared writes and ATT_TRANSACTION_MODE_EXECUTE
78*591423b2SMatthias Ringwald // @param offset into the value - used for queued writes and long attributes
79*591423b2SMatthias Ringwald // @param buffer
80*591423b2SMatthias Ringwald // @param buffer_size
81*591423b2SMatthias Ringwald // @param signature used for signed write commmands
82*591423b2SMatthias 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
83*591423b2SMatthias Ringwald typedef int (*att_write_callback_t)(uint16_t con_handle, uint16_t attribute_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size);
84*591423b2SMatthias Ringwald 
85*591423b2SMatthias Ringwald // MARK: ATT Operations
86*591423b2SMatthias Ringwald 
87*591423b2SMatthias Ringwald /*
88*591423b2SMatthias Ringwald  * @brief setup ATT database
89*591423b2SMatthias Ringwald  */
90*591423b2SMatthias Ringwald void att_set_db(uint8_t const * db);
91*591423b2SMatthias Ringwald 
92*591423b2SMatthias Ringwald /*
93*591423b2SMatthias Ringwald  * @brief set callback for read of dynamic attributes
94*591423b2SMatthias Ringwald  * @param callback
95*591423b2SMatthias Ringwald  */
96*591423b2SMatthias Ringwald void att_set_read_callback(att_read_callback_t callback);
97*591423b2SMatthias Ringwald 
98*591423b2SMatthias Ringwald /*
99*591423b2SMatthias Ringwald  * @brief set callback for write of dynamic attributes
100*591423b2SMatthias Ringwald  * @param callback
101*591423b2SMatthias Ringwald  */
102*591423b2SMatthias Ringwald void att_set_write_callback(att_write_callback_t callback);
103*591423b2SMatthias Ringwald 
104*591423b2SMatthias Ringwald /*
105*591423b2SMatthias Ringwald  * @brief debug helper, dump ATT database to stdout using log_info
106*591423b2SMatthias Ringwald  */
107*591423b2SMatthias Ringwald void att_dump_attributes(void);
108*591423b2SMatthias Ringwald 
109*591423b2SMatthias Ringwald /*
110*591423b2SMatthias Ringwald  * @brief process ATT request against database and put response into response buffer
111*591423b2SMatthias Ringwald  * @param att_connection used for mtu and security properties
112*591423b2SMatthias Ringwald  * @param request_buffer, request_len: ATT request from clinet
113*591423b2SMatthias Ringwald  * @param response_buffer for result
114*591423b2SMatthias Ringwald  * @returns len of data in response buffer. 0 = no response
115*591423b2SMatthias Ringwald  */
116*591423b2SMatthias Ringwald uint16_t att_handle_request(att_connection_t * att_connection,
117*591423b2SMatthias Ringwald                             uint8_t * request_buffer,
118*591423b2SMatthias Ringwald                             uint16_t request_len,
119*591423b2SMatthias Ringwald                             uint8_t * response_buffer);
120*591423b2SMatthias Ringwald 
121*591423b2SMatthias Ringwald /*
122*591423b2SMatthias Ringwald  * @brief setup value notification in response buffer for a given handle and value
123*591423b2SMatthias Ringwald  * @param att_connection
124*591423b2SMatthias Ringwald  * @param value, value_len: new attribute value
125*591423b2SMatthias Ringwald  * @param response_buffer for notification
126*591423b2SMatthias Ringwald  */
127*591423b2SMatthias Ringwald uint16_t att_prepare_handle_value_notification(att_connection_t * att_connection,
128*591423b2SMatthias Ringwald                                                uint16_t handle,
129*591423b2SMatthias Ringwald                                                uint8_t *value,
130*591423b2SMatthias Ringwald                                                uint16_t value_len,
131*591423b2SMatthias Ringwald                                                uint8_t * response_buffer);
132*591423b2SMatthias Ringwald 
133*591423b2SMatthias Ringwald /*
134*591423b2SMatthias Ringwald  * @brief setup value indication in response buffer for a given handle and value
135*591423b2SMatthias Ringwald  * @param att_connection
136*591423b2SMatthias Ringwald  * @param value, value_len: new attribute value
137*591423b2SMatthias Ringwald  * @param response_buffer for indication
138*591423b2SMatthias Ringwald  */
139*591423b2SMatthias Ringwald uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection,
140*591423b2SMatthias Ringwald                                              uint16_t handle,
141*591423b2SMatthias Ringwald                                              uint8_t *value,
142*591423b2SMatthias Ringwald                                              uint16_t value_len,
143*591423b2SMatthias Ringwald                                              uint8_t * response_buffer);
144*591423b2SMatthias Ringwald 
145*591423b2SMatthias Ringwald /*
146*591423b2SMatthias Ringwald  * @brief transcation queue of prepared writes, e.g., after disconnect
147*591423b2SMatthias Ringwald  */
148*591423b2SMatthias Ringwald void att_clear_transaction_queue(att_connection_t * att_connection);
149*591423b2SMatthias Ringwald 
150*591423b2SMatthias Ringwald // experimental client API
151*591423b2SMatthias Ringwald uint16_t att_uuid_for_handle(uint16_t handle);
152*591423b2SMatthias Ringwald 
153*591423b2SMatthias Ringwald #if defined __cplusplus
154*591423b2SMatthias Ringwald }
155*591423b2SMatthias Ringwald #endif
156*591423b2SMatthias Ringwald 
157*591423b2SMatthias Ringwald #endif // __ATT_H
158