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 Add primary service for 16-bit UUID 66 * @param uuid16 67 * @return attribute handle for the new service definition 68 */ 69 uint16_t att_db_util_add_service_uuid16(uint16_t uuid16); 70 71 /** 72 * @brief Add primary service for 128-bit UUID 73 * @param uuid128 74 * @return attribute handle for the new service definition 75 */ 76 uint16_t att_db_util_add_service_uuid128(const uint8_t * uuid128); 77 78 /** 79 * @brief Add secondary service for 16-bit UUID 80 * @param uuid16 81 * @return attribute handle for the new service definition 82 */ 83 uint16_t att_db_util_add_secondary_service_uuid16(uint16_t uuid16); 84 85 /** 86 * @brief Add secondary service for 128-bit UUID 87 * @param uuid128 88 * @return attribute handle for the new service definition 89 */ 90 uint16_t att_db_util_add_secondary_service_uuid128(const uint8_t * uuid128); 91 92 /** 93 * @brief Add included service with 16-bit UUID 94 * @param start_group_handle 95 * @param end_group_handle 96 * @param uuid16 97 * @return attribute handle for the new service definition 98 */ 99 uint16_t att_db_util_add_included_service_uuid16(uint16_t start_group_handle, uint16_t end_group_handle, uint16_t uuid16); 100 101 /** 102 * @brief Add Characteristic with 16-bit UUID, properties, and data 103 * @param uuid16 104 * @param properties - see ATT_PROPERTY_* in src/bluetooth.h 105 * @param read_permissions - see ATT_SECURITY_* in src/bluetooth.h 106 * @param write_permissions - see ATT_SECURITY_* in src/bluetooth.h 107 * @param data returned in read operations if ATT_PROPERTY_DYNAMIC is not specified 108 * @param data_len 109 * @return attribute handle of the new characteristic value declaration 110 * @note If properties contains ATT_PROPERTY_NOTIFY or ATT_PROPERTY_INDICATE flags, a Client Configuration Characteristic Descriptor (CCCD) 111 * is created as well. The attribute value handle of the CCCD is the attribute value handle plus 1 112 */ 113 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); 114 115 /** 116 * @brief Add Characteristic with 128-bit UUID, properties, and data 117 * @param uuid128 118 * @param properties - see ATT_PROPERTY_* in src/bluetooth.h 119 * @param read_permissions - see ATT_SECURITY_* in src/bluetooth.h 120 * @param write_permissions - see ATT_SECURITY_* in src/bluetooth.h 121 * @param data returned in read operations if ATT_PROPERTY_DYNAMIC is not specified 122 * @param data_len 123 * @return attribute handle of the new characteristic value declaration 124 * @note If properties contains ATT_PROPERTY_NOTIFY or ATT_PROPERTY_INDICATE flags, a Client Configuration Characteristic Descriptor (CCCD) 125 * is created as well. The attribute value handle of the CCCD is the attribute value handle plus 1 126 */ 127 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); 128 129 /** 130 * @brief Add descriptor with 16-bit UUID, properties, and data 131 * @param uuid16 132 * @param properties - see ATT_PROPERTY_* in src/bluetooth.h 133 * @param read_permissions - see ATT_SECURITY_* in src/bluetooth.h 134 * @param write_permissions - see ATT_SECURITY_* in src/bluetooth.h 135 * @param data returned in read operations if ATT_PROPERTY_DYNAMIC is not specified 136 * @param data_len 137 * @return attribute handle of the new characteristic descriptor declaration 138 */ 139 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); 140 141 /** 142 * @brief Add descriptor with 128-bit UUID, properties, and data 143 * @param uuid128 144 * @param properties - see ATT_PROPERTY_* in src/bluetooth.h 145 * @param read_permissions - see ATT_SECURITY_* in src/bluetooth.h 146 * @param write_permissions - see ATT_SECURITY_* in src/bluetooth.h 147 * @param data returned in read operations if ATT_PROPERTY_DYNAMIC is not specified 148 * @param data_len 149 * @return attribute handle of the new characteristic descriptor declaration 150 */ 151 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); 152 153 /** 154 * @brief Get address of constructed ATT DB 155 */ 156 uint8_t * att_db_util_get_address(void); 157 158 /** 159 * @brief Get size of constructed ATT DB 160 */ 161 uint16_t att_db_util_get_size(void); 162 163 /** 164 * @brief Get number of bytes that are included in GATT Database Hash 165 */ 166 uint16_t att_db_util_hash_len(void); 167 168 /** 169 * @brief init generator for GATT Database Hash 170 */ 171 void att_db_util_hash_init(void); 172 173 /** 174 * @brief get next byte from generator for GATT Database Hash 175 */ 176 uint8_t att_db_util_hash_get_next(void); 177 178 /** 179 * @brief Calculate GATT Database Hash using crypto engine 180 * @param request 181 * @param db_hash 182 * @param callback 183 * @param callback_arg 184 */ 185 void att_db_util_hash_calc(btstack_crypto_aes128_cmac_t * request, uint8_t * db_hash, void (* callback)(void * arg), void * callback_arg); 186 187 /* API_END */ 188 189 #if defined __cplusplus 190 } 191 #endif 192 #endif 193