xref: /btstack/src/ble/att_db_util.c (revision c37cd8f3d1350b92a2f66c31b2a5fcd75f8c91a4)
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 "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 	return 1;
100 #else
101 	log_error("att_db: out of memory");
102 	return 0;
103 #endif
104 }
105 
106 // attribute size in bytes (16), flags(16), handle (16), uuid (16/128), value(...)
107 
108 // db endds with 0x00 0x00
109 
110 static void att_db_util_add_attribute_uuid16(uint16_t uuid16, uint16_t flags, uint8_t * data, uint16_t data_len){
111 	int size = 2 + 2 + 2 + 2 + data_len;
112 	if (!att_db_util_assert_space(size)) return;
113 	little_endian_store_16(att_db, att_db_size, size);
114 	att_db_size += 2;
115 	little_endian_store_16(att_db, att_db_size, flags);
116 	att_db_size += 2;
117 	little_endian_store_16(att_db, att_db_size, att_db_next_handle);
118 	att_db_size += 2;
119 	att_db_next_handle++;
120 	little_endian_store_16(att_db, att_db_size, uuid16);
121 	att_db_size += 2;
122 	memcpy(&att_db[att_db_size], data, data_len);
123 	att_db_size += data_len;
124 	att_db_util_set_end_tag();
125 }
126 
127 static void att_db_util_add_attribute_uuid128(uint8_t * uuid128, uint16_t flags, uint8_t * data, uint16_t data_len){
128 	int size = 2 + 2 + 2 + 16 + data_len;
129 	if (!att_db_util_assert_space(size)) return;
130 	flags |= ATT_PROPERTY_UUID128;
131 	little_endian_store_16(att_db, att_db_size, size);
132 	att_db_size += 2;
133 	little_endian_store_16(att_db, att_db_size, flags);
134 	att_db_size += 2;
135 	little_endian_store_16(att_db, att_db_size, att_db_next_handle);
136 	att_db_size += 2;
137 	att_db_next_handle++;
138 	reverse_128(uuid128, &att_db[att_db_size]);
139 	att_db_size += 16;
140 	memcpy(&att_db[att_db_size], data, data_len);
141 	att_db_size += data_len;
142 	att_db_util_set_end_tag();
143 }
144 
145 void att_db_util_add_service_uuid16(uint16_t uuid16){
146 	uint8_t buffer[2];
147 	little_endian_store_16(buffer, 0, uuid16);
148 	att_db_util_add_attribute_uuid16(GATT_PRIMARY_SERVICE_UUID, ATT_PROPERTY_READ, buffer, 2);
149 }
150 
151 void att_db_util_add_service_uuid128(uint8_t * uuid128){
152 	uint8_t buffer[16];
153 	reverse_128(uuid128, buffer);
154 	att_db_util_add_attribute_uuid16(GATT_PRIMARY_SERVICE_UUID, ATT_PROPERTY_READ, buffer, 16);
155 }
156 
157 static void att_db_util_add_client_characteristic_configuration(uint16_t properties){
158 	uint8_t buffer[2];
159 	// keep authentication flags
160 	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;
161 	little_endian_store_16(buffer, 0, 0);
162 	att_db_util_add_attribute_uuid16(GATT_CLIENT_CHARACTERISTICS_CONFIGURATION, flags, buffer, 2);
163 }
164 
165 uint16_t att_db_util_add_characteristic_uuid16(uint16_t uuid16, uint16_t properties, uint8_t * data, uint16_t data_len){
166 	uint8_t buffer[5];
167 	buffer[0] = properties;
168 	little_endian_store_16(buffer, 1, att_db_next_handle + 1);
169 	little_endian_store_16(buffer, 3, uuid16);
170 	att_db_util_add_attribute_uuid16(GATT_CHARACTERISTICS_UUID, ATT_PROPERTY_READ, buffer, sizeof(buffer));
171 	// drop Broadcast, Notify, Indicate - not used for flags
172 	uint16_t value_flags = properties & 0x1ffce;
173 	uint16_t value_handle = att_db_next_handle;
174 	att_db_util_add_attribute_uuid16(uuid16, value_flags, data, data_len);
175 	if (properties & (ATT_PROPERTY_NOTIFY | ATT_PROPERTY_INDICATE)){
176 		att_db_util_add_client_characteristic_configuration(properties);
177 	}
178 	return value_handle;
179 }
180 
181 uint16_t att_db_util_add_characteristic_uuid128(uint8_t * uuid128, uint16_t properties, uint8_t * data, uint16_t data_len){
182 	uint8_t buffer[19];
183 	buffer[0] = properties;
184 	little_endian_store_16(buffer, 1, att_db_next_handle + 1);
185 	reverse_128(uuid128, &buffer[3]);
186 	att_db_util_add_attribute_uuid16(GATT_CHARACTERISTICS_UUID, ATT_PROPERTY_READ, buffer, sizeof(buffer));
187 	// drop Broadcast, Notify, Indicate - not used for flags
188 	uint16_t value_flags = properties & 0x1ffce;
189 	uint16_t value_handle = att_db_next_handle;
190 	att_db_util_add_attribute_uuid128(uuid128, value_flags, data, data_len);
191 	if (properties & (ATT_PROPERTY_NOTIFY | ATT_PROPERTY_INDICATE)){
192 		att_db_util_add_client_characteristic_configuration(properties);
193 	}
194 	return value_handle;
195 }
196 
197 uint8_t * att_db_util_get_address(void){
198 	return att_db;
199 }
200 
201 uint16_t att_db_util_get_size(void){
202 	return att_db_size + 2;	// end tag
203 }
204