xref: /btstack/src/ble/att_db_util.h (revision 87e52a097b589e14e7cbce41276b5cb7f0fc4822)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
373deb3ec6SMatthias Ringwald 
383deb3ec6SMatthias Ringwald /**
39fe5a6c4eSMilanka Ringwald  * @title Runtine ATT Database Setup
40fe5a6c4eSMilanka Ringwald  *
41fe5a6c4eSMilanka Ringwald  * Helper to construct ATT DB at runtime (BTstack GATT Compiler is not used).
42fe5a6c4eSMilanka Ringwald  *
433deb3ec6SMatthias Ringwald  */
443deb3ec6SMatthias Ringwald 
454495c8c0SMilanka Ringwald #ifndef ATT_DB_UTIL_H
464495c8c0SMilanka Ringwald #define ATT_DB_UTIL_H
473deb3ec6SMatthias Ringwald 
487907f069SMatthias Ringwald #include "btstack_config.h"
49a4419cc8SMatthias Ringwald #include "btstack_crypto.h"
503deb3ec6SMatthias Ringwald 
513deb3ec6SMatthias Ringwald #include <stdint.h>
523deb3ec6SMatthias Ringwald 
533deb3ec6SMatthias Ringwald #if defined __cplusplus
543deb3ec6SMatthias Ringwald extern "C" {
553deb3ec6SMatthias Ringwald #endif
563deb3ec6SMatthias Ringwald 
573deb3ec6SMatthias Ringwald /* API_START */
583deb3ec6SMatthias Ringwald 
593deb3ec6SMatthias Ringwald /**
603deb3ec6SMatthias Ringwald  * @brief Init ATT DB storage
613deb3ec6SMatthias Ringwald  */
623deb3ec6SMatthias Ringwald void att_db_util_init(void);
633deb3ec6SMatthias Ringwald 
643deb3ec6SMatthias Ringwald /**
65*87e52a09SAlex McHugh  * @brief Set handle value for next table entry
66*87e52a09SAlex McHugh  * @param handle
67*87e52a09SAlex McHugh  * @note new handle must be >= previous to avoid reusing assigned handles
68*87e52a09SAlex McHugh  */
69*87e52a09SAlex McHugh void att_db_util_set_next_handle(uint16_t handle);
70*87e52a09SAlex McHugh 
71*87e52a09SAlex McHugh /**
723deb3ec6SMatthias Ringwald  * @brief Add primary service for 16-bit UUID
73ff04bac7SMatthias Ringwald  * @param uuid16
746b65794dSMilanka Ringwald  * @return attribute handle for the new service definition
753deb3ec6SMatthias Ringwald  */
76ff04bac7SMatthias Ringwald uint16_t att_db_util_add_service_uuid16(uint16_t uuid16);
773deb3ec6SMatthias Ringwald 
783deb3ec6SMatthias Ringwald /**
793deb3ec6SMatthias Ringwald  * @brief Add primary service for 128-bit UUID
8091974b5bSMatthias Ringwald  * @param uuid128
816b65794dSMilanka Ringwald  * @return attribute handle for the new service definition
823deb3ec6SMatthias Ringwald  */
83ff04bac7SMatthias Ringwald uint16_t att_db_util_add_service_uuid128(const uint8_t * uuid128);
843deb3ec6SMatthias Ringwald 
853deb3ec6SMatthias Ringwald /**
86d68b9e47SMatthias Ringwald  * @brief Add secondary service for 16-bit UUID
87d68b9e47SMatthias Ringwald  * @param uuid16
886b65794dSMilanka Ringwald  * @return attribute handle for the new service definition
89d68b9e47SMatthias Ringwald  */
90d68b9e47SMatthias Ringwald uint16_t att_db_util_add_secondary_service_uuid16(uint16_t uuid16);
91d68b9e47SMatthias Ringwald 
92d68b9e47SMatthias Ringwald /**
93d68b9e47SMatthias Ringwald  * @brief Add secondary service for 128-bit UUID
9491974b5bSMatthias Ringwald  * @param uuid128
956b65794dSMilanka Ringwald  * @return attribute handle for the new service definition
96d68b9e47SMatthias Ringwald  */
97d68b9e47SMatthias Ringwald uint16_t att_db_util_add_secondary_service_uuid128(const uint8_t * uuid128);
98d68b9e47SMatthias Ringwald 
99d68b9e47SMatthias Ringwald /**
10091974b5bSMatthias Ringwald  * @brief Add included service with 16-bit UUID
10191974b5bSMatthias Ringwald  * @param start_group_handle
10291974b5bSMatthias Ringwald  * @param end_group_handle
10391974b5bSMatthias Ringwald  * @param uuid16
1046b65794dSMilanka Ringwald  * @return attribute handle for the new service definition
10591974b5bSMatthias Ringwald  */
10691974b5bSMatthias Ringwald uint16_t att_db_util_add_included_service_uuid16(uint16_t start_group_handle, uint16_t  end_group_handle, uint16_t uuid16);
10791974b5bSMatthias Ringwald 
10891974b5bSMatthias Ringwald /**
1093deb3ec6SMatthias Ringwald  * @brief Add Characteristic with 16-bit UUID, properties, and data
110ff04bac7SMatthias Ringwald  * @param uuid16
111ff04bac7SMatthias Ringwald  * @param properties        - see ATT_PROPERTY_* in src/bluetooth.h
112ff04bac7SMatthias Ringwald  * @param read_permissions  - see ATT_SECURITY_* in src/bluetooth.h
113ff04bac7SMatthias Ringwald  * @param write_permissions - see ATT_SECURITY_* in src/bluetooth.h
114ff04bac7SMatthias Ringwald  * @param data returned in read operations if ATT_PROPERTY_DYNAMIC is not specified
115ff04bac7SMatthias Ringwald  * @param data_len
1166b65794dSMilanka Ringwald  * @return attribute handle of the new characteristic value declaration
1179f28d85dSMatthias Ringwald  * @note If properties contains ATT_PROPERTY_NOTIFY or ATT_PROPERTY_INDICATE flags, a Client Configuration Characteristic Descriptor (CCCD)
1189f28d85dSMatthias Ringwald  *       is created as well. The attribute value handle of the CCCD is the attribute value handle plus 1
1193deb3ec6SMatthias Ringwald  */
120ff04bac7SMatthias Ringwald 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);
1213deb3ec6SMatthias Ringwald 
1223deb3ec6SMatthias Ringwald /**
1233deb3ec6SMatthias Ringwald  * @brief Add Characteristic with 128-bit UUID, properties, and data
124ff04bac7SMatthias Ringwald  * @param uuid128
125ff04bac7SMatthias Ringwald  * @param properties        - see ATT_PROPERTY_* in src/bluetooth.h
126ff04bac7SMatthias Ringwald  * @param read_permissions  - see ATT_SECURITY_* in src/bluetooth.h
127ff04bac7SMatthias Ringwald  * @param write_permissions - see ATT_SECURITY_* in src/bluetooth.h
128ff04bac7SMatthias Ringwald  * @param data returned in read operations if ATT_PROPERTY_DYNAMIC is not specified
129ff04bac7SMatthias Ringwald  * @param data_len
1306b65794dSMilanka Ringwald  * @return attribute handle of the new characteristic value declaration
1319f28d85dSMatthias Ringwald  * @note If properties contains ATT_PROPERTY_NOTIFY or ATT_PROPERTY_INDICATE flags, a Client Configuration Characteristic Descriptor (CCCD)
1329f28d85dSMatthias Ringwald  *       is created as well. The attribute value handle of the CCCD is the attribute value handle plus 1
1333deb3ec6SMatthias Ringwald  */
134ff04bac7SMatthias Ringwald 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);
1359f28d85dSMatthias Ringwald 
1369f28d85dSMatthias Ringwald /**
13793493c27SMatthias Ringwald * @brief Add descriptor with 16-bit UUID, properties, and data
138ff04bac7SMatthias Ringwald * @param uuid16
139ff04bac7SMatthias Ringwald * @param properties        - see ATT_PROPERTY_* in src/bluetooth.h
140ff04bac7SMatthias Ringwald * @param read_permissions  - see ATT_SECURITY_* in src/bluetooth.h
141ff04bac7SMatthias Ringwald * @param write_permissions - see ATT_SECURITY_* in src/bluetooth.h
142ff04bac7SMatthias Ringwald * @param data returned in read operations if ATT_PROPERTY_DYNAMIC is not specified
143ff04bac7SMatthias Ringwald * @param data_len
1446b65794dSMilanka Ringwald * @return attribute handle of the new characteristic descriptor declaration
1459f28d85dSMatthias Ringwald */
146ff04bac7SMatthias Ringwald 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);
147ff04bac7SMatthias Ringwald 
148ff04bac7SMatthias Ringwald /**
149ff04bac7SMatthias Ringwald * @brief Add descriptor with 128-bit UUID, properties, and data
15093493c27SMatthias Ringwald * @param uuid128
151ff04bac7SMatthias Ringwald * @param properties        - see ATT_PROPERTY_* in src/bluetooth.h
152ff04bac7SMatthias Ringwald * @param read_permissions  - see ATT_SECURITY_* in src/bluetooth.h
153ff04bac7SMatthias Ringwald * @param write_permissions - see ATT_SECURITY_* in src/bluetooth.h
154ff04bac7SMatthias Ringwald * @param data returned in read operations if ATT_PROPERTY_DYNAMIC is not specified
155ff04bac7SMatthias Ringwald * @param data_len
1566b65794dSMilanka Ringwald * @return attribute handle of the new characteristic descriptor declaration
157ff04bac7SMatthias Ringwald */
158ff04bac7SMatthias Ringwald 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);
1599f28d85dSMatthias Ringwald 
1609f28d85dSMatthias Ringwald /**
1613deb3ec6SMatthias Ringwald  * @brief Get address of constructed ATT DB
1623deb3ec6SMatthias Ringwald  */
1633deb3ec6SMatthias Ringwald uint8_t * att_db_util_get_address(void);
1643deb3ec6SMatthias Ringwald 
1653deb3ec6SMatthias Ringwald /**
1663deb3ec6SMatthias Ringwald  * @brief Get size of constructed ATT DB
1673deb3ec6SMatthias Ringwald  */
1683deb3ec6SMatthias Ringwald uint16_t att_db_util_get_size(void);
1693deb3ec6SMatthias Ringwald 
170bc6b65c7SMatthias Ringwald /**
171bc6b65c7SMatthias Ringwald  * @brief Get number of bytes that are included in GATT Database Hash
172bc6b65c7SMatthias Ringwald  */
173bc6b65c7SMatthias Ringwald uint16_t att_db_util_hash_len(void);
174bc6b65c7SMatthias Ringwald 
17573843592SMatthias Ringwald /**
17673843592SMatthias Ringwald  * @brief init generator for GATT Database Hash
17773843592SMatthias Ringwald  */
17873843592SMatthias Ringwald void att_db_util_hash_init(void);
17973843592SMatthias Ringwald 
18073843592SMatthias Ringwald /**
18173843592SMatthias Ringwald  * @brief get next byte from generator for GATT Database Hash
18273843592SMatthias Ringwald  */
18373843592SMatthias Ringwald uint8_t att_db_util_hash_get_next(void);
18473843592SMatthias Ringwald 
185a4419cc8SMatthias Ringwald /**
186a4419cc8SMatthias Ringwald  * @brief Calculate GATT Database Hash using crypto engine
187a4419cc8SMatthias Ringwald  * @param request
188a4419cc8SMatthias Ringwald  * @param db_hash
189a4419cc8SMatthias Ringwald  * @param callback
190a4419cc8SMatthias Ringwald  * @param callback_arg
191a4419cc8SMatthias Ringwald  */
192a4419cc8SMatthias Ringwald void att_db_util_hash_calc(btstack_crypto_aes128_cmac_t * request, uint8_t * db_hash, void (* callback)(void * arg), void * callback_arg);
193a4419cc8SMatthias Ringwald 
1943deb3ec6SMatthias Ringwald /* API_END */
1953deb3ec6SMatthias Ringwald 
1963deb3ec6SMatthias Ringwald #if defined __cplusplus
1973deb3ec6SMatthias Ringwald }
1983deb3ec6SMatthias Ringwald #endif
1993deb3ec6SMatthias Ringwald #endif
200