1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2014 Instituto Nokia de Tecnologia - INdT
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <glib.h>
#include <stdbool.h>
#include "log.h"
#include "lib/uuid.h"
#include "attrib/att.h"
#include "src/shared/util.h"
#include "gatt-dbus.h"
#include "gatt.h"
/* Common GATT UUIDs */
static const bt_uuid_t primary_uuid = { .type = BT_UUID16,
.value.u16 = GATT_PRIM_SVC_UUID };
static const bt_uuid_t chr_uuid = { .type = BT_UUID16,
.value.u16 = GATT_CHARAC_UUID };
struct btd_attribute {
uint16_t handle;
bt_uuid_t type;
btd_attr_read_t read_cb;
btd_attr_write_t write_cb;
uint16_t value_len;
uint8_t value[0];
};
static GList *local_attribute_db;
static uint16_t next_handle = 0x0001;
static inline void put_uuid_le(const bt_uuid_t *src, void *dst)
{
if (src->type == BT_UUID16)
put_le16(src->value.u16, dst);
else if (src->type == BT_UUID32)
put_le32(src->value.u32, dst);
else
/* Convert from 128-bit BE to LE */
bswap_128(&src->value.u128, dst);
}
/*
* Helper function to create new attributes containing constant/static values.
* eg: declaration of services/characteristics, and characteristics with
* fixed values.
*/
static struct btd_attribute *new_const_attribute(const bt_uuid_t *type,
const uint8_t *value,
uint16_t len)
{
struct btd_attribute *attr;
attr = malloc0(sizeof(struct btd_attribute) + len);
if (!attr)
return NULL;
attr->type = *type;
memcpy(&attr->value, value, len);
attr->value_len = len;
return attr;
}
static struct btd_attribute *new_attribute(const bt_uuid_t *type,
btd_attr_read_t read_cb,
btd_attr_write_t write_cb)
{
struct btd_attribute *attr;
attr = new0(struct btd_attribute, 1);
if (!attr)
return NULL;
attr->type = *type;
attr->read_cb = read_cb;
attr->write_cb = write_cb;
return attr;
}
static bool is_service(const struct btd_attribute *attr)
{
if (attr->type.type != BT_UUID16)
return false;
if (attr->type.value.u16 == GATT_PRIM_SVC_UUID ||
attr->type.value.u16 == GATT_SND_SVC_UUID)
return true;
return false;
}
static int local_database_add(uint16_t handle, struct btd_attribute *attr)
{
attr->handle = handle;
local_attribute_db = g_list_append(local_attribute_db, attr);
return 0;
}
struct btd_attribute *btd_gatt_add_service(const bt_uuid_t *uuid)
{
struct btd_attribute *attr;
uint16_t len = bt_uuid_len(uuid);
uint8_t value[len];
/*
* Service DECLARATION
*
* TYPE ATTRIBUTE VALUE
* +-------+---------------------------------+
* |0x2800 | 0xYYYY... |
* | (1) | (2) |
* +------+----------------------------------+
* (1) - 2 octets: Primary/Secondary Service UUID
* (2) - 2 or 16 octets: Service UUID
*/
/* Set attribute value */
put_uuid_le(uuid, value);
attr = new_const_attribute(&primary_uuid, value, len);
if (!attr)
return NULL;
if (local_database_add(next_handle, attr) < 0) {
free(attr);
return NULL;
}
/* TODO: missing overflow checking */
next_handle = next_handle + 1;
return attr;
}
void btd_gatt_remove_service(struct btd_attribute *service)
{
GList *list = g_list_find(local_attribute_db, service);
bool first_node;
if (!list)
return;
first_node = local_attribute_db == list;
/* Remove service declaration attribute */
free(list->data);
list = g_list_delete_link(list, list);
/* Remove all characteristics until next service declaration */
while (list && !is_service(list->data)) {
free(list->data);
list = g_list_delete_link(list, list);
}
/*
* When removing the first node, local attribute database head
* needs to be updated. Node removed from middle doesn't change
* the list head address.
*/
if (first_node)
local_attribute_db = list;
}
struct btd_attribute *btd_gatt_add_char(const bt_uuid_t *uuid,
uint8_t properties,
btd_attr_read_t read_cb,
btd_attr_write_t write_cb)
{
struct btd_attribute *char_decl, *char_value = NULL;
/* Attribute value length */
uint16_t len = 1 + 2 + bt_uuid_len(uuid);
uint8_t value[len];
/*
* Characteristic DECLARATION
*
* TYPE ATTRIBUTE VALUE
* +-------+---------------------------------+
* |0x2803 | 0xXX 0xYYYY 0xZZZZ... |
* | (1) | (2) (3) (4) |
* +------+----------------------------------+
* (1) - 2 octets: Characteristic declaration UUID
* (2) - 1 octet : Properties
* (3) - 2 octets: Handle of the characteristic Value
* (4) - 2 or 16 octets: Characteristic UUID
*/
value[0] = properties;
/*
* Since we don't know yet the characteristic value attribute
* handle, we skip and set it later.
*/
put_uuid_le(uuid, &value[3]);
char_decl = new_const_attribute(&chr_uuid, value, len);
if (!char_decl)
goto fail;
char_value = new_attribute(uuid, read_cb, write_cb);
if (!char_value)
goto fail;
if (local_database_add(next_handle, char_decl) < 0)
goto fail;
next_handle = next_handle + 1;
/*
* Characteristic VALUE
*
* TYPE ATTRIBUTE VALUE
* +----------+---------------------------------+
* |0xZZZZ... | 0x... |
* | (1) | (2) |
* +----------+---------------------------------+
* (1) - 2 or 16 octets: Characteristic UUID
* (2) - N octets: Value is read dynamically from the service
* implementation (external entity).
*/
if (local_database_add(next_handle, char_value) < 0)
/* TODO: remove declaration */
goto fail;
next_handle = next_handle + 1;
/*
* Update characteristic value handle in characteristic declaration
* attribute. For local attributes, we can assume that the handle
* representing the characteristic value will get the next available
* handle. However, for remote attribute this assumption is not valid.
*/
put_le16(char_value->handle, &char_decl->value[1]);
return char_value;
fail:
free(char_decl);
free(char_value);
return NULL;
}
struct btd_attribute *btd_gatt_add_char_desc(const bt_uuid_t *uuid,
btd_attr_read_t read_cb,
btd_attr_write_t write_cb)
{
struct btd_attribute *attr;
/*
* From Core SPEC 4.1 page 2184:
* "Characteristic descriptor declaration permissions are defined by a
* higher layer profile or are implementation specific. A client shall
* not assume all characteristic descriptor declarations are readable."
*
* The read/write callbacks presence will define the descriptor
* permissions managed directly by the core. The upper layer can define
* additional permissions constraints.
*/
attr = new_attribute(uuid, read_cb, write_cb);
if (!attr)
return NULL;
if (local_database_add(next_handle, attr) < 0) {
free(attr);
return NULL;
}
next_handle = next_handle + 1;
return attr;
}
void gatt_init(void)
{
DBG("Starting GATT server");
gatt_dbus_manager_register();
}
void gatt_cleanup(void)
{
DBG("Stopping GATT server");
gatt_dbus_manager_unregister();
}
|