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 #define __BTSTACK_FILE__ "att_db_util.c" 39 40 #include <string.h> 41 42 #include "ble/att_db_util.h" 43 #include "ble/att_db.h" 44 #include "ble/core.h" 45 #include "btstack_util.h" 46 #include "btstack_debug.h" 47 #include "bluetooth.h" 48 49 // ATT DB Storage 50 #ifndef HAVE_MALLOC 51 #ifdef MAX_ATT_DB_SIZE 52 static uint8_t att_db_storage[MAX_ATT_DB_SIZE]; 53 #else 54 #error Neither HAVE_MALLOC nor MAX_ATT_DB_SIZE is defined. 55 #endif 56 #endif 57 58 static uint8_t * att_db; 59 static uint16_t att_db_size; 60 static uint16_t att_db_max_size; 61 static uint16_t att_db_next_handle; 62 63 static void att_db_util_set_end_tag(void){ 64 // end tag 65 att_db[att_db_size] = 0; 66 att_db[att_db_size+1] = 0; 67 } 68 69 void att_db_util_init(void){ 70 #ifdef HAVE_MALLOC 71 att_db = (uint8_t*) malloc(128); 72 att_db_max_size = 128; 73 #else 74 att_db = att_db_storage; 75 att_db_max_size = sizeof(att_db_storage); 76 #endif 77 // store att version 78 att_db[0] = ATT_DB_VERSION; 79 att_db_size = 1; 80 att_db_next_handle = 1; 81 att_db_util_set_end_tag(); 82 } 83 84 /** 85 * asserts that the requested amount of bytes can be stored in the att_db 86 * @returns TRUE if space is available 87 */ 88 static int att_db_util_assert_space(uint16_t size){ 89 size += 2; // for end tag 90 if (att_db_size + size <= att_db_max_size) return 1; 91 #ifdef HAVE_MALLOC 92 int new_size = att_db_size + att_db_size / 2; 93 uint8_t * new_db = (uint8_t*) realloc(att_db, new_size); 94 if (!new_db) { 95 log_error("att_db: realloc failed"); 96 return 0; 97 } 98 att_db = new_db; 99 att_db_max_size = new_size; 100 att_set_db(att_db); // Update att_db with the new db 101 return 1; 102 #else 103 log_error("att_db: out of memory"); 104 return 0; 105 #endif 106 } 107 108 // attribute size in bytes (16), flags(16), handle (16), uuid (16/128), value(...) 109 110 // db endds with 0x00 0x00 111 112 static void att_db_util_add_attribute_uuid16(uint16_t uuid16, uint16_t flags, uint8_t * data, uint16_t data_len){ 113 int size = 2 + 2 + 2 + 2 + data_len; 114 if (!att_db_util_assert_space(size)) return; 115 little_endian_store_16(att_db, att_db_size, size); 116 att_db_size += 2; 117 little_endian_store_16(att_db, att_db_size, flags); 118 att_db_size += 2; 119 little_endian_store_16(att_db, att_db_size, att_db_next_handle); 120 att_db_size += 2; 121 att_db_next_handle++; 122 little_endian_store_16(att_db, att_db_size, uuid16); 123 att_db_size += 2; 124 memcpy(&att_db[att_db_size], data, data_len); 125 att_db_size += data_len; 126 att_db_util_set_end_tag(); 127 } 128 129 static void att_db_util_add_attribute_uuid128(const uint8_t * uuid128, uint16_t flags, uint8_t * data, uint16_t data_len){ 130 int size = 2 + 2 + 2 + 16 + data_len; 131 if (!att_db_util_assert_space(size)) return; 132 flags |= ATT_PROPERTY_UUID128; 133 little_endian_store_16(att_db, att_db_size, size); 134 att_db_size += 2; 135 little_endian_store_16(att_db, att_db_size, flags); 136 att_db_size += 2; 137 little_endian_store_16(att_db, att_db_size, att_db_next_handle); 138 att_db_size += 2; 139 att_db_next_handle++; 140 reverse_128(uuid128, &att_db[att_db_size]); 141 att_db_size += 16; 142 memcpy(&att_db[att_db_size], data, data_len); 143 att_db_size += data_len; 144 att_db_util_set_end_tag(); 145 } 146 147 uint16_t att_db_util_add_service_uuid16(uint16_t uuid16){ 148 uint8_t buffer[2]; 149 little_endian_store_16(buffer, 0, uuid16); 150 uint16_t service_handle = att_db_next_handle; 151 att_db_util_add_attribute_uuid16(GATT_PRIMARY_SERVICE_UUID, ATT_PROPERTY_READ, buffer, 2); 152 return service_handle; 153 } 154 155 uint16_t att_db_util_add_service_uuid128(const uint8_t * uuid128){ 156 uint8_t buffer[16]; 157 reverse_128(uuid128, buffer); 158 uint16_t service_handle = att_db_next_handle; 159 att_db_util_add_attribute_uuid16(GATT_PRIMARY_SERVICE_UUID, ATT_PROPERTY_READ, buffer, 16); 160 return service_handle; 161 } 162 163 static void att_db_util_add_client_characteristic_configuration(uint16_t flags){ 164 uint8_t buffer[2]; 165 // drop permission for read (0xc00), keep write permissions (0x0011) 166 flags = (flags & 0x1f311) | ATT_PROPERTY_READ | ATT_PROPERTY_WRITE | ATT_PROPERTY_DYNAMIC; 167 little_endian_store_16(buffer, 0, 0); 168 att_db_util_add_attribute_uuid16(GATT_CLIENT_CHARACTERISTICS_CONFIGURATION, flags, buffer, 2); 169 } 170 171 static uint16_t att_db_util_encode_permissions(uint16_t properties, uint8_t read_permission, uint8_t write_permission){ 172 // drop Broadcast (0x01), Notify (0x10), Indicate (0x20) - not used for flags 173 uint16_t flags = properties & 0xfffce; 174 // if encryption requested, set encryption key size to 16 175 if ((read_permission > ATT_SECURITY_NONE) || (write_permission > ATT_SECURITY_NONE)){ 176 flags |= 0xf000; 177 } 178 // encode read/write security levels 179 if (read_permission & 1){ 180 flags |= ATT_PROPERTY_READ_PERMISSION_BIT_0; 181 } 182 if (read_permission & 2){ 183 flags |= ATT_PROPERTY_READ_PERMISSION_BIT_1; 184 } 185 if (write_permission & 1){ 186 flags |= ATT_PROPERTY_WRITE_PERMISSION_BIT_0; 187 } 188 if (write_permission & 2){ 189 flags |= ATT_PROPERTY_WRITE_PERMISSION_BIT_1; 190 } 191 return flags; 192 } 193 194 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){ 195 uint8_t buffer[5]; 196 buffer[0] = properties; 197 little_endian_store_16(buffer, 1, att_db_next_handle + 1); 198 little_endian_store_16(buffer, 3, uuid16); 199 att_db_util_add_attribute_uuid16(GATT_CHARACTERISTICS_UUID, ATT_PROPERTY_READ, buffer, sizeof(buffer)); 200 uint16_t flags = att_db_util_encode_permissions(properties, read_permission, write_permission); 201 uint16_t value_handle = att_db_next_handle; 202 att_db_util_add_attribute_uuid16(uuid16, flags, data, data_len); 203 if (properties & (ATT_PROPERTY_NOTIFY | ATT_PROPERTY_INDICATE)){ 204 att_db_util_add_client_characteristic_configuration(flags); 205 } 206 return value_handle; 207 } 208 209 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){ 210 uint8_t buffer[19]; 211 buffer[0] = properties; 212 little_endian_store_16(buffer, 1, att_db_next_handle + 1); 213 reverse_128(uuid128, &buffer[3]); 214 att_db_util_add_attribute_uuid16(GATT_CHARACTERISTICS_UUID, ATT_PROPERTY_READ, buffer, sizeof(buffer)); 215 uint16_t flags = att_db_util_encode_permissions(properties, read_permission, write_permission); 216 uint16_t value_handle = att_db_next_handle; 217 att_db_util_add_attribute_uuid128(uuid128, flags, data, data_len); 218 if (properties & (ATT_PROPERTY_NOTIFY | ATT_PROPERTY_INDICATE)){ 219 att_db_util_add_client_characteristic_configuration(flags); 220 } 221 return value_handle; 222 } 223 224 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){ 225 uint16_t descriptor_handler = att_db_next_handle; 226 uint16_t flags = att_db_util_encode_permissions(properties, read_permission, write_permission); 227 att_db_util_add_attribute_uuid16(uuid16, flags, data, data_len); 228 return descriptor_handler; 229 } 230 231 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){ 232 uint16_t descriptor_handler = att_db_next_handle; 233 uint16_t flags = att_db_util_encode_permissions(properties, read_permission, write_permission); 234 att_db_util_add_attribute_uuid128(uuid128, flags, data, data_len); 235 return descriptor_handler; 236 } 237 238 uint8_t * att_db_util_get_address(void){ 239 return att_db; 240 } 241 242 uint16_t att_db_util_get_size(void){ 243 return att_db_size + 2; // end tag 244 } 245