xref: /btstack/src/ble/att_db_util.c (revision 6973c35a87cc7f398c429d8942b18d0a401e28a3)
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 	att_db_size = 0;
79 	att_db_next_handle = 1;
80 	att_db_util_set_end_tag();
81 }
82 
83 /**
84  * asserts that the requested amount of bytes can be stored in the att_db
85  * @returns TRUE if space is available
86  */
87 static int att_db_util_assert_space(uint16_t size){
88 	size += 2; // for end tag
89 	if (att_db_size + size <= att_db_max_size) return 1;
90 #ifdef HAVE_MALLOC
91 	int new_size = att_db_size + att_db_size / 2;
92 	uint8_t * new_db = (uint8_t*) realloc(att_db, new_size);
93 	if (!new_db) {
94 		log_error("att_db: realloc failed");
95 		return 0;
96 	}
97 	att_db = new_db;
98 	att_db_max_size = new_size;
99     att_set_db(att_db); // Update att_db with the new db
100 	return 1;
101 #else
102 	log_error("att_db: out of memory");
103 	return 0;
104 #endif
105 }
106 
107 // attribute size in bytes (16), flags(16), handle (16), uuid (16/128), value(...)
108 
109 // db endds with 0x00 0x00
110 
111 static void att_db_util_add_attribute_uuid16(uint16_t uuid16, uint16_t flags, uint8_t * data, uint16_t data_len){
112 	int size = 2 + 2 + 2 + 2 + data_len;
113 	if (!att_db_util_assert_space(size)) return;
114 	little_endian_store_16(att_db, att_db_size, size);
115 	att_db_size += 2;
116 	little_endian_store_16(att_db, att_db_size, flags);
117 	att_db_size += 2;
118 	little_endian_store_16(att_db, att_db_size, att_db_next_handle);
119 	att_db_size += 2;
120 	att_db_next_handle++;
121 	little_endian_store_16(att_db, att_db_size, uuid16);
122 	att_db_size += 2;
123 	memcpy(&att_db[att_db_size], data, data_len);
124 	att_db_size += data_len;
125 	att_db_util_set_end_tag();
126 }
127 
128 static void att_db_util_add_attribute_uuid128(uint8_t * uuid128, uint16_t flags, uint8_t * data, uint16_t data_len){
129 	int size = 2 + 2 + 2 + 16 + data_len;
130 	if (!att_db_util_assert_space(size)) return;
131 	flags |= ATT_PROPERTY_UUID128;
132 	little_endian_store_16(att_db, att_db_size, size);
133 	att_db_size += 2;
134 	little_endian_store_16(att_db, att_db_size, flags);
135 	att_db_size += 2;
136 	little_endian_store_16(att_db, att_db_size, att_db_next_handle);
137 	att_db_size += 2;
138 	att_db_next_handle++;
139 	reverse_128(uuid128, &att_db[att_db_size]);
140 	att_db_size += 16;
141 	memcpy(&att_db[att_db_size], data, data_len);
142 	att_db_size += data_len;
143 	att_db_util_set_end_tag();
144 }
145 
146 uint16_t att_db_util_add_service_uuid16(uint16_t uuid16){
147 	uint8_t buffer[2];
148 	little_endian_store_16(buffer, 0, uuid16);
149 	uint16_t service_handle = att_db_next_handle;
150 	att_db_util_add_attribute_uuid16(GATT_PRIMARY_SERVICE_UUID, ATT_PROPERTY_READ, buffer, 2);
151 	return service_handle;
152 }
153 
154 uint16_t att_db_util_add_service_uuid128(uint8_t * uuid128){
155 	uint8_t buffer[16];
156 	reverse_128(uuid128, buffer);
157 	uint16_t service_handle = att_db_next_handle;
158 	att_db_util_add_attribute_uuid16(GATT_PRIMARY_SERVICE_UUID, ATT_PROPERTY_READ, buffer, 16);
159 	return service_handle;
160 }
161 
162 static void att_db_util_add_client_characteristic_configuration(uint16_t properties){
163 	uint8_t buffer[2];
164 	// keep authentication flags
165 	uint16_t flags = (properties & 0x1ff00) | ATT_DB_PERSISTENT_WRITE_CCC | ATT_DB_FLAGS_READ_WITHOUT_AUTHENTICATION | ATT_PROPERTY_READ | ATT_PROPERTY_WRITE | ATT_PROPERTY_DYNAMIC;
166 	little_endian_store_16(buffer, 0, 0);
167 	att_db_util_add_attribute_uuid16(GATT_CLIENT_CHARACTERISTICS_CONFIGURATION, flags, buffer, 2);
168 }
169 
170 uint16_t att_db_util_add_characteristic_uuid16(uint16_t uuid16, uint16_t properties, uint8_t * data, uint16_t data_len){
171 	uint8_t buffer[5];
172 	buffer[0] = properties;
173 	little_endian_store_16(buffer, 1, att_db_next_handle + 1);
174 	little_endian_store_16(buffer, 3, uuid16);
175 	att_db_util_add_attribute_uuid16(GATT_CHARACTERISTICS_UUID, ATT_PROPERTY_READ, buffer, sizeof(buffer));
176 	// drop Broadcast, Notify, Indicate - not used for flags
177 	uint16_t value_flags = properties & 0x1ffce;
178 	uint16_t value_handle = att_db_next_handle;
179 	att_db_util_add_attribute_uuid16(uuid16, value_flags, data, data_len);
180 	if (properties & (ATT_PROPERTY_NOTIFY | ATT_PROPERTY_INDICATE)){
181 		att_db_util_add_client_characteristic_configuration(properties);
182 	}
183 	return value_handle;
184 }
185 
186 uint16_t att_db_util_add_characteristic_uuid128(uint8_t * uuid128, uint16_t properties, uint8_t * data, uint16_t data_len){
187 	uint8_t buffer[19];
188 	buffer[0] = properties;
189 	little_endian_store_16(buffer, 1, att_db_next_handle + 1);
190 	reverse_128(uuid128, &buffer[3]);
191 	att_db_util_add_attribute_uuid16(GATT_CHARACTERISTICS_UUID, ATT_PROPERTY_READ, buffer, sizeof(buffer));
192 	// drop Broadcast, Notify, Indicate - not used for flags
193 	uint16_t value_flags = properties & 0x1ffce;
194 	uint16_t value_handle = att_db_next_handle;
195 	att_db_util_add_attribute_uuid128(uuid128, value_flags, data, data_len);
196 	if (properties & (ATT_PROPERTY_NOTIFY | ATT_PROPERTY_INDICATE)){
197 		att_db_util_add_client_characteristic_configuration(properties);
198 	}
199 	return value_handle;
200 }
201 
202 uint16_t att_db_util_add_descriptor_uuid16(uint16_t uuid16, uint16_t properties, uint8_t * data, uint16_t data_len){
203     uint16_t descriptor_handler = att_db_next_handle;
204     att_db_util_add_attribute_uuid16(uuid16, properties, data, data_len);
205     return descriptor_handler;
206 }
207 
208 uint16_t att_db_util_add_descriptor_uuid128(uint8_t * uuid128, uint16_t properties, uint8_t * data, uint16_t data_len){
209     uint16_t descriptor_handler = att_db_next_handle;
210     att_db_util_add_attribute_uuid128(uuid128, properties, data, data_len);
211     return descriptor_handler;
212  }
213 
214 uint8_t * att_db_util_get_address(void){
215 	return att_db;
216 }
217 
218 uint16_t att_db_util_get_size(void){
219 	return att_db_size + 2;	// end tag
220 }
221