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