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