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