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