xref: /btstack/src/ble/att_db_util.c (revision ff3cc4a5378c2f681cc9b75cf54d154a12a3051e)
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 #include <stdlib.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 	(void)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 	int size = 2 + 2 + 2 + 16 + data_len;
132 	if (!att_db_util_assert_space(size)) return;
133 	flags |= ATT_PROPERTY_UUID128;
134 	little_endian_store_16(att_db, att_db_size, size);
135 	att_db_size += 2;
136 	little_endian_store_16(att_db, att_db_size, flags);
137 	att_db_size += 2;
138 	little_endian_store_16(att_db, att_db_size, att_db_next_handle);
139 	att_db_size += 2;
140 	att_db_next_handle++;
141 	reverse_128(uuid128, &att_db[att_db_size]);
142 	att_db_size += 16;
143 	(void)memcpy(&att_db[att_db_size], data, data_len);
144 	att_db_size += data_len;
145 	att_db_util_set_end_tag();
146 }
147 
148 uint16_t att_db_util_add_service_uuid16(uint16_t uuid16){
149 	uint8_t buffer[2];
150 	little_endian_store_16(buffer, 0, uuid16);
151 	uint16_t service_handle = att_db_next_handle;
152 	att_db_util_add_attribute_uuid16(GATT_PRIMARY_SERVICE_UUID, ATT_PROPERTY_READ, buffer, 2);
153 	return service_handle;
154 }
155 
156 uint16_t att_db_util_add_service_uuid128(const uint8_t * uuid128){
157 	uint8_t buffer[16];
158 	reverse_128(uuid128, buffer);
159 	uint16_t service_handle = att_db_next_handle;
160 	att_db_util_add_attribute_uuid16(GATT_PRIMARY_SERVICE_UUID, ATT_PROPERTY_READ, buffer, 16);
161 	return service_handle;
162 }
163 
164 static void att_db_util_add_client_characteristic_configuration(uint16_t flags){
165 	uint8_t buffer[2];
166 	// drop permission for read (0xc00), keep write permissions (0x0091)
167 	flags = (flags & 0x1f391) | ATT_PROPERTY_READ | ATT_PROPERTY_WRITE | ATT_PROPERTY_DYNAMIC;
168 	little_endian_store_16(buffer, 0, 0);
169 	att_db_util_add_attribute_uuid16(GATT_CLIENT_CHARACTERISTICS_CONFIGURATION, flags, buffer, 2);
170 }
171 
172 static uint16_t att_db_util_encode_permissions(uint16_t properties, uint8_t read_permission, uint8_t write_permission){
173     // drop Broadcast (0x01), Notify (0x10), Indicate (0x20), Extended Attributes (0x80) - not used for flags
174     uint16_t flags = properties & 0xfff4e;
175     // if encryption requested, set encryption key size to 16
176     if ((read_permission > ATT_SECURITY_NONE) || (write_permission > ATT_SECURITY_NONE)){
177     	flags |= 0xf000;
178     }
179     // map SC requirement
180     if (read_permission == ATT_SECURITY_AUTHENTICATED_SC){
181         read_permission =  ATT_SECURITY_AUTHENTICATED;
182         flags |= ATT_PROPERTY_READ_PERMISSION_SC;
183     }
184     if (write_permission == ATT_SECURITY_AUTHENTICATED_SC){
185         write_permission =  ATT_SECURITY_AUTHENTICATED;
186         flags |= ATT_PROPERTY_WRITE_PERMISSION_SC;
187     }
188     // encode read/write security levels
189     if (read_permission & 1){
190     	flags |= ATT_PROPERTY_READ_PERMISSION_BIT_0;
191     }
192     if (read_permission & 2){
193     	flags |= ATT_PROPERTY_READ_PERMISSION_BIT_1;
194     }
195     if (write_permission & 1){
196     	flags |= ATT_PROPERTY_WRITE_PERMISSION_BIT_0;
197     }
198     if (write_permission & 2){
199     	flags |= ATT_PROPERTY_WRITE_PERMISSION_BIT_1;
200     }
201 	return flags;
202 }
203 
204 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){
205 	uint8_t buffer[5];
206 	buffer[0] = properties;
207 	little_endian_store_16(buffer, 1, att_db_next_handle + 1);
208 	little_endian_store_16(buffer, 3, uuid16);
209 	att_db_util_add_attribute_uuid16(GATT_CHARACTERISTICS_UUID, ATT_PROPERTY_READ, buffer, sizeof(buffer));
210 	uint16_t flags = att_db_util_encode_permissions(properties, read_permission, write_permission);
211 	uint16_t value_handle = att_db_next_handle;
212 	att_db_util_add_attribute_uuid16(uuid16, flags, data, data_len);
213 	if (properties & (ATT_PROPERTY_NOTIFY | ATT_PROPERTY_INDICATE)){
214 		att_db_util_add_client_characteristic_configuration(flags);
215 	}
216 	return value_handle;
217 }
218 
219 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){
220 	uint8_t buffer[19];
221 	buffer[0] = properties;
222 	little_endian_store_16(buffer, 1, att_db_next_handle + 1);
223 	reverse_128(uuid128, &buffer[3]);
224 	att_db_util_add_attribute_uuid16(GATT_CHARACTERISTICS_UUID, ATT_PROPERTY_READ, buffer, sizeof(buffer));
225 	uint16_t flags = att_db_util_encode_permissions(properties, read_permission, write_permission);
226 	uint16_t value_handle = att_db_next_handle;
227 	att_db_util_add_attribute_uuid128(uuid128, flags, data, data_len);
228 	if (properties & (ATT_PROPERTY_NOTIFY | ATT_PROPERTY_INDICATE)){
229 		att_db_util_add_client_characteristic_configuration(flags);
230 	}
231 	return value_handle;
232 }
233 
234 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){
235     uint16_t descriptor_handler = att_db_next_handle;
236 	uint16_t flags = att_db_util_encode_permissions(properties, read_permission, write_permission);
237     att_db_util_add_attribute_uuid16(uuid16, flags, data, data_len);
238     return descriptor_handler;
239 }
240 
241 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){
242     uint16_t descriptor_handler = att_db_next_handle;
243 	uint16_t flags = att_db_util_encode_permissions(properties, read_permission, write_permission);
244     att_db_util_add_attribute_uuid128(uuid128, flags, data, data_len);
245     return descriptor_handler;
246  }
247 
248 uint8_t * att_db_util_get_address(void){
249 	return att_db;
250 }
251 
252 uint16_t att_db_util_get_size(void){
253 	return att_db_size + 2;	// end tag
254 }
255