xref: /btstack/src/ble/att_db_util.h (revision 39127754112d635f8d55bf39817a539acc1c6e74)
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 BLUEKITCHEN
24  * GMBH 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  * @title Runtine ATT Database Setup
40  *
41  * Helper to construct ATT DB at runtime (BTstack GATT Compiler is not used).
42  *
43  */
44 
45 #ifndef ATT_DB_UTIL_H
46 #define ATT_DB_UTIL_H
47 
48 #include "btstack_config.h"
49 #include "btstack_crypto.h"
50 
51 #include <stdint.h>
52 
53 #if defined __cplusplus
54 extern "C" {
55 #endif
56 
57 /* API_START */
58 
59 /**
60  * @brief Init ATT DB storage
61  */
62 void att_db_util_init(void);
63 
64 /**
65  * @brief Set handle value for next table entry
66  * @param handle
67  * @note new handle must be >= previous to avoid reusing assigned handles
68  */
69 void att_db_util_set_next_handle(uint16_t handle);
70 
71 /**
72  * @brief Add primary service for 16-bit UUID
73  * @param uuid16
74  * @return attribute handle for the new service definition
75  */
76 uint16_t att_db_util_add_service_uuid16(uint16_t uuid16);
77 
78 /**
79  * @brief Add primary service for 128-bit UUID
80  * @param uuid128
81  * @return attribute handle for the new service definition
82  */
83 uint16_t att_db_util_add_service_uuid128(const uint8_t * uuid128);
84 
85 /**
86  * @brief Add secondary service for 16-bit UUID
87  * @param uuid16
88  * @return attribute handle for the new service definition
89  */
90 uint16_t att_db_util_add_secondary_service_uuid16(uint16_t uuid16);
91 
92 /**
93  * @brief Add secondary service for 128-bit UUID
94  * @param uuid128
95  * @return attribute handle for the new service definition
96  */
97 uint16_t att_db_util_add_secondary_service_uuid128(const uint8_t * uuid128);
98 
99 /**
100  * @brief Add included service with 16-bit UUID
101  * @param start_group_handle
102  * @param end_group_handle
103  * @param uuid16
104  * @return attribute handle for the new service definition
105  */
106 uint16_t att_db_util_add_included_service_uuid16(uint16_t start_group_handle, uint16_t  end_group_handle, uint16_t uuid16);
107 
108 /**
109  * @brief Add Characteristic with 16-bit UUID, properties, and data
110  * @param uuid16
111  * @param properties        - see ATT_PROPERTY_* in src/bluetooth.h
112  * @param read_permissions  - see ATT_SECURITY_* in src/bluetooth.h
113  * @param write_permissions - see ATT_SECURITY_* in src/bluetooth.h
114  * @param data returned in read operations if ATT_PROPERTY_DYNAMIC is not specified
115  * @param data_len
116  * @return attribute handle of the new characteristic value declaration
117  * @note If properties contains ATT_PROPERTY_NOTIFY or ATT_PROPERTY_INDICATE flags, a Client Configuration Characteristic Descriptor (CCCD)
118  *       is created as well. The attribute value handle of the CCCD is the attribute value handle plus 1
119  */
120 uint16_t att_db_util_add_characteristic_uuid16(uint16_t uuid16, uint16_t properties, uint8_t read_permission, uint8_t write_permission, uint8_t * data, uint16_t data_len);
121 
122 /**
123  * @brief Add Characteristic with 128-bit UUID, properties, and data
124  * @param uuid128
125  * @param properties        - see ATT_PROPERTY_* in src/bluetooth.h
126  * @param read_permissions  - see ATT_SECURITY_* in src/bluetooth.h
127  * @param write_permissions - see ATT_SECURITY_* in src/bluetooth.h
128  * @param data returned in read operations if ATT_PROPERTY_DYNAMIC is not specified
129  * @param data_len
130  * @return attribute handle of the new characteristic value declaration
131  * @note If properties contains ATT_PROPERTY_NOTIFY or ATT_PROPERTY_INDICATE flags, a Client Configuration Characteristic Descriptor (CCCD)
132  *       is created as well. The attribute value handle of the CCCD is the attribute value handle plus 1
133  */
134 uint16_t att_db_util_add_characteristic_uuid128(const uint8_t * uuid128, uint16_t properties, uint8_t read_permission, uint8_t write_permission, uint8_t * data, uint16_t data_len);
135 
136 /**
137 * @brief Add descriptor with 16-bit UUID, properties, and data
138 * @param uuid16
139 * @param properties        - see ATT_PROPERTY_* in src/bluetooth.h
140 * @param read_permissions  - see ATT_SECURITY_* in src/bluetooth.h
141 * @param write_permissions - see ATT_SECURITY_* in src/bluetooth.h
142 * @param data returned in read operations if ATT_PROPERTY_DYNAMIC is not specified
143 * @param data_len
144 * @return attribute handle of the new characteristic descriptor declaration
145 */
146 uint16_t att_db_util_add_descriptor_uuid16(uint16_t uuid16, uint16_t properties, uint8_t read_permission, uint8_t write_permission, uint8_t * data, uint16_t data_len);
147 
148 /**
149 * @brief Add descriptor with 128-bit UUID, properties, and data
150 * @param uuid128
151 * @param properties        - see ATT_PROPERTY_* in src/bluetooth.h
152 * @param read_permissions  - see ATT_SECURITY_* in src/bluetooth.h
153 * @param write_permissions - see ATT_SECURITY_* in src/bluetooth.h
154 * @param data returned in read operations if ATT_PROPERTY_DYNAMIC is not specified
155 * @param data_len
156 * @return attribute handle of the new characteristic descriptor declaration
157 */
158 uint16_t att_db_util_add_descriptor_uuid128(const uint8_t * uuid128, uint16_t properties, uint8_t read_permission, uint8_t write_permission, uint8_t * data, uint16_t data_len);
159 
160 /**
161  * @brief Get address of constructed ATT DB
162  */
163 uint8_t * att_db_util_get_address(void);
164 
165 /**
166  * @brief Get size of constructed ATT DB
167  */
168 uint16_t att_db_util_get_size(void);
169 
170 /**
171  * @brief Get number of bytes that are included in GATT Database Hash
172  */
173 uint16_t att_db_util_hash_len(void);
174 
175 /**
176  * @brief init generator for GATT Database Hash
177  */
178 void att_db_util_hash_init(void);
179 
180 /**
181  * @brief get next byte from generator for GATT Database Hash
182  */
183 uint8_t att_db_util_hash_get_next(void);
184 
185 /**
186  * @brief Calculate GATT Database Hash using crypto engine
187  * @param request
188  * @param db_hash
189  * @param callback
190  * @param callback_arg
191  */
192 void att_db_util_hash_calc(btstack_crypto_aes128_cmac_t * request, uint8_t * db_hash, void (* callback)(void * arg), void * callback_arg);
193 
194 /* API_END */
195 
196 #if defined __cplusplus
197 }
198 #endif
199 #endif
200