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