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 #include <stdlib.h> 39 #include <string.h> 40 41 #include "att_db_util.h" 42 #include "ble/att_db.h" 43 #include "ble/core.h" 44 #include "btstack_util.h" 45 #include "btstack_debug.h" 46 #include "bluetooth.h" 47 48 // ATT DB Storage 49 #ifndef HAVE_MALLOC 50 #ifdef MAX_ATT_DB_SIZE 51 static uint8_t att_db_storage[MAX_ATT_DB_SIZE]; 52 #else 53 #error Neither HAVE_MALLOC] nor MAX_ATT_DB_SIZE is defined. 54 #endif 55 #endif 56 57 static uint8_t * att_db; 58 static uint16_t att_db_size; 59 static uint16_t att_db_max_size; 60 static uint16_t att_db_next_handle; 61 62 static void att_db_util_set_end_tag(void){ 63 // end tag 64 att_db[att_db_size] = 0; 65 att_db[att_db_size+1] = 0; 66 } 67 68 void att_db_util_init(void){ 69 #ifdef HAVE_MALLOC 70 att_db = (uint8_t*) malloc(128); 71 att_db_max_size = 128; 72 #else 73 att_db = att_db_storage; 74 att_db_max_size = sizeof(att_db_storage); 75 #endif 76 att_db_size = 0; 77 att_db_next_handle = 1; 78 att_db_util_set_end_tag(); 79 } 80 81 /** 82 * asserts that the requested amount of bytes can be stored in the att_db 83 * @returns TRUE if space is available 84 */ 85 static int att_db_util_assert_space(uint16_t size){ 86 size += 2; // for end tag 87 if (att_db_size + size <= att_db_max_size) return 1; 88 #ifdef HAVE_MALLOC 89 int new_size = att_db_size + att_db_size / 2; 90 uint8_t * new_db = (uint8_t*) realloc(att_db, new_size); 91 if (!new_db) { 92 log_error("att_db: realloc failed"); 93 return 0; 94 } 95 att_db = new_db; 96 att_db_max_size = new_size; 97 return 1; 98 #else 99 log_error("att_db: out of memory"); 100 return 0; 101 #endif 102 } 103 104 // attribute size in bytes (16), flags(16), handle (16), uuid (16/128), value(...) 105 106 // db endds with 0x00 0x00 107 108 static void att_db_util_add_attribute_uuid16(uint16_t uuid16, uint16_t flags, uint8_t * data, uint16_t data_len){ 109 int size = 2 + 2 + 2 + 2 + data_len; 110 if (!att_db_util_assert_space(size)) return; 111 little_endian_store_16(att_db, att_db_size, size); 112 att_db_size += 2; 113 little_endian_store_16(att_db, att_db_size, flags); 114 att_db_size += 2; 115 little_endian_store_16(att_db, att_db_size, att_db_next_handle); 116 att_db_size += 2; 117 att_db_next_handle++; 118 little_endian_store_16(att_db, att_db_size, uuid16); 119 att_db_size += 2; 120 memcpy(&att_db[att_db_size], data, data_len); 121 att_db_size += data_len; 122 att_db_util_set_end_tag(); 123 } 124 125 static void att_db_util_add_attribute_uuid128(uint8_t * uuid128, uint16_t flags, uint8_t * data, uint16_t data_len){ 126 int size = 2 + 2 + 2 + 16 + data_len; 127 if (!att_db_util_assert_space(size)) return; 128 flags |= ATT_PROPERTY_UUID128; 129 little_endian_store_16(att_db, att_db_size, size); 130 att_db_size += 2; 131 little_endian_store_16(att_db, att_db_size, flags); 132 att_db_size += 2; 133 little_endian_store_16(att_db, att_db_size, att_db_next_handle); 134 att_db_size += 2; 135 att_db_next_handle++; 136 reverse_128(uuid128, &att_db[att_db_size]); 137 att_db_size += 16; 138 memcpy(&att_db[att_db_size], data, data_len); 139 att_db_size += data_len; 140 att_db_util_set_end_tag(); 141 } 142 143 void att_db_util_add_service_uuid16(uint16_t uuid16){ 144 uint8_t buffer[2]; 145 little_endian_store_16(buffer, 0, uuid16); 146 att_db_util_add_attribute_uuid16(GATT_PRIMARY_SERVICE_UUID, ATT_PROPERTY_READ, buffer, 2); 147 } 148 149 void att_db_util_add_service_uuid128(uint8_t * uuid128){ 150 uint8_t buffer[16]; 151 reverse_128(uuid128, buffer); 152 att_db_util_add_attribute_uuid16(GATT_PRIMARY_SERVICE_UUID, ATT_PROPERTY_READ, buffer, 16); 153 } 154 155 uint16_t att_db_util_add_characteristic_uuid16(uint16_t uuid16, uint16_t properties, uint8_t * data, uint16_t data_len){ 156 uint8_t buffer[5]; 157 buffer[0] = properties; 158 little_endian_store_16(buffer, 1, att_db_next_handle + 1); 159 little_endian_store_16(buffer, 3, uuid16); 160 att_db_util_add_attribute_uuid16(GATT_CHARACTERISTICS_UUID, ATT_PROPERTY_READ, buffer, sizeof(buffer)); 161 uint16_t value_handle = att_db_next_handle; 162 att_db_util_add_attribute_uuid16(uuid16, properties, data, data_len); 163 if (properties & (ATT_PROPERTY_NOTIFY | ATT_PROPERTY_INDICATE)){ 164 uint16_t flags = ATT_PROPERTY_READ | ATT_PROPERTY_WRITE | ATT_PROPERTY_DYNAMIC; 165 little_endian_store_16(buffer, 0, 0); 166 att_db_util_add_attribute_uuid16(GATT_CLIENT_CHARACTERISTICS_CONFIGURATION, flags, buffer, 2); 167 } 168 return value_handle; 169 } 170 171 uint16_t att_db_util_add_characteristic_uuid128(uint8_t * uuid128, uint16_t properties, uint8_t * data, uint16_t data_len){ 172 uint8_t buffer[19]; 173 buffer[0] = properties; 174 little_endian_store_16(buffer, 1, att_db_next_handle + 1); 175 reverse_128(uuid128, &buffer[3]); 176 att_db_util_add_attribute_uuid16(GATT_CHARACTERISTICS_UUID, ATT_PROPERTY_READ, buffer, sizeof(buffer)); 177 uint16_t value_handle = att_db_next_handle; 178 att_db_util_add_attribute_uuid128(uuid128, properties, data, data_len); 179 if (properties & (ATT_PROPERTY_NOTIFY | ATT_PROPERTY_INDICATE)){ 180 uint16_t flags = ATT_PROPERTY_READ | ATT_PROPERTY_WRITE | ATT_PROPERTY_DYNAMIC; 181 little_endian_store_16(buffer, 0, 0); 182 att_db_util_add_attribute_uuid16(GATT_CLIENT_CHARACTERISTICS_CONFIGURATION, flags, buffer, 2); 183 } 184 return value_handle; 185 } 186 187 uint8_t * att_db_util_get_address(void){ 188 return att_db; 189 } 190 191 uint16_t att_db_util_get_size(void){ 192 return att_db_size + 2; // end tag 193 } 194