aboutsummaryrefslogtreecommitdiffstats
path: root/attrib/client.c
blob: d984122d953e809cf49bfbabbdd244f954bc5b42 (plain)
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
 *
 *
 *  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 <bluetooth/bluetooth.h>
#include <bluetooth/sdp.h>
#include <bluetooth/sdp_lib.h>

#include "log.h"
#include "gdbus.h"
#include "btio.h"
#include "storage.h"

#include "att.h"
#include "gattrib.h"
#include "gatt.h"
#include "client.h"

#define CHAR_INTERFACE "org.bluez.Characteristic"

struct gatt_service {
	bdaddr_t sba;
	bdaddr_t dba;
	char *path;
	GIOChannel *io;
	GSList *primary;
	/* FIXME: remove this when we have a command queue */
	GSList *cur_prim;
	GAttrib *attrib;
	int psm;
	guint atid;
};

struct characteristic {
	char *path;
	struct attribute *decl;		/* Characteristics declaration */
	struct attribute *value;	/* Characteristic value */
	GSList *desc;			/* Characteristic descriptors */
};

struct primary {
	uuid_t *uuid;
	uint16_t start;
	uint16_t end;
	GSList *chars;
};

static GSList *services = NULL;

static DBusConnection *connection;

static void characteristic_free(void *user_data)
{
	struct characteristic *chr = user_data;

	g_free(chr->path);
	g_free(chr->decl);
	g_free(chr->value);
	g_slist_foreach(chr->desc, (GFunc) g_free, NULL);
	g_slist_free(chr->desc);
	g_free(chr);
}

static void primary_free(void *user_data)
{
	struct primary *prim = user_data;

	g_slist_foreach(prim->chars, (GFunc) characteristic_free, NULL);
	g_free(prim->uuid);
	g_free(prim);
}

static void gatt_service_free(void *user_data)
{
	struct gatt_service *gatt = user_data;

	g_slist_foreach(gatt->primary, (GFunc) primary_free, NULL);
	g_attrib_unref(gatt->attrib);
	g_free(gatt->path);
	g_free(gatt);
}

static int gatt_path_cmp(const struct gatt_service *gatt, const char *path)
{
	return strcmp(gatt->path, path);
}

static DBusMessage *get_characteristics(DBusConnection *conn,
						DBusMessage *msg, void *data)
{
	return dbus_message_new_method_return(msg);
}

static DBusMessage *register_watcher(DBusConnection *conn,
						DBusMessage *msg, void *data)
{
	return dbus_message_new_method_return(msg);
}

static DBusMessage *unregister_watcher(DBusConnection *conn,
						DBusMessage *msg, void *data)
{
	return dbus_message_new_method_return(msg);
}

static GDBusMethodTable char_methods[] = {
	{ "GetCharacteristics",	"",	"a{oa{sv}}", get_characteristics},
	{ "RegisterCharacteristicsWatcher",	"o", "",
						register_watcher	},
	{ "UnregisterCharacteristicsWatcher",	"o", "",
						unregister_watcher	},
	{ }
};

static void register_characteristics(struct gatt_service *gatt)
{
	GSList *lp, *lc;

	for (lp = gatt->primary; lp; lp = lp->next) {
		struct primary *prim = lp->data;
		for (lc = prim->chars; lc; lc = lc->next) {
			struct characteristic *chr = lc->data;
			g_dbus_register_interface(connection, chr->path,
						CHAR_INTERFACE, char_methods,
						NULL, NULL, chr, NULL);
			DBG("Registered: %s", chr->path);
		}
	}
}

static void char_discovered_cb(guint8 status, const guint8 *pdu, guint16 plen,
							gpointer user_data)
{
	struct gatt_service *gatt = user_data;
	struct att_data_list *list;
	struct primary *prim;
	uint16_t last;
	int i;

	if (status == ATT_ECODE_ATTR_NOT_FOUND) {
		if (gatt->cur_prim == NULL || gatt->cur_prim->next == NULL) {
			register_characteristics(gatt);
			return;
		}

		gatt->cur_prim = gatt->cur_prim->next;

		/* Fetch characteristics of the NEXT primary service */
		prim = gatt->cur_prim->data;
		gatt_discover_char(gatt->attrib, prim->start, prim->end,
						char_discovered_cb, gatt);
		return;
	}

	if (status != 0) {
		DBG("Discover all characteristics failed: %s",
						att_ecode2str(status));

		goto fail;
	}

	DBG("Read by Type Response received");

	list = dec_read_by_type_resp(pdu, plen);
	if (list == NULL)
		return;

	prim = gatt->cur_prim->data;

	for (i = 0, last = 0; i < list->num; i++) {
		struct characteristic *chr;
		struct attribute *decl;
		uint16_t *u16, length;

		u16 = (uint16_t *) list->data[i];

		chr = g_new0(struct characteristic, 1);

		/* Each element contains: handle and attribute value */
		length = list->len - sizeof(*u16);
		decl = g_malloc0(sizeof(struct attribute) + length);
		decl->handle = btohs(*u16);
		sdp_uuid16_create(&decl->uuid, GATT_CHARAC_UUID);
		decl->len = length;
		u16++;
		memcpy(decl->data, u16, length);

		chr->path = g_strdup_printf("%s/service%04x/characteristic%04x",
					gatt->path, prim->start, decl->handle);

		chr->decl = decl;
		prim->chars = g_slist_append(prim->chars, chr);

		last = decl->handle;
	}

	att_data_list_free(list);

	/* Fetch remaining characteristics for the CURRENT primary service */
	gatt_discover_char(gatt->attrib, last + 1, prim->end,
					char_discovered_cb, gatt);

	return;

fail:
	gatt_service_free(gatt);
}

static char *primary_list_to_string(GSList *primary_list)
{
	GString *services;
	GSList *l;

	services = g_string_new(NULL);

	for (l = primary_list; l; l = l->next) {
		struct primary *primary = l->data;
		uuid_t *uuid128;
		char service[64];
		char uuidstr[MAX_LEN_UUID_STR];

		memset(service, 0, sizeof(service));

		uuid128 = sdp_uuid_to_uuid128(primary->uuid);
		sdp_uuid2strn(uuid128, uuidstr, MAX_LEN_UUID_STR);

		bt_free(uuid128);

		snprintf(service, sizeof(service), "%d#%d#%s ", primary->start,
							primary->end, uuidstr);

		services = g_string_append(services, service);
	}

	return g_string_free(services, FALSE);
}

static void store_primary_services(struct gatt_service *gatt)
{
       char *services;

       services = primary_list_to_string(gatt->primary);

       write_device_services(&gatt->sba, &gatt->dba, services);

       g_free(services);
}

static void primary_cb(guint8 status, const guint8 *pdu, guint16 plen,
							gpointer user_data)
{
	struct gatt_service *gatt = user_data;
	unsigned int i;
	uint8_t length;
	uint16_t end, start;

	if (status == ATT_ECODE_ATTR_NOT_FOUND) {
		struct primary *prim;

		if (gatt->primary == NULL)
			return;

		store_primary_services(gatt);

		/* Start Characteristic Discovery */
		gatt->cur_prim = gatt->primary;
		prim = gatt->cur_prim->data;

		gatt_discover_char(gatt->attrib, prim->start, prim->end,
						char_discovered_cb, gatt);
		return;
	}

	if (status != 0) {
		error("Discover all primary services failed: %s",
						att_ecode2str(status));
		goto fail;
	}

	if (pdu[0] != ATT_OP_READ_BY_GROUP_RESP) {
		error("Protocol error");
		goto fail;
	}

	DBG("Read by Group Type Response received");

	length = pdu[1];
	for (i = 2, end = 0; i < plen; i += length) {
		struct primary *prim;
		uuid_t *uuid;
		uint16_t *p16;

		p16 = (void *) &pdu[i];
		start = btohs(*p16);
		p16++;
		end = btohs(*p16);
		p16++;
		uuid = bt_malloc(sizeof(uuid_t));
		if (length == 6) {
			uint16_t u16 = btohs(*p16);
			uuid = sdp_uuid16_create(uuid, u16);

		} else if (length == 20) {
			/* FIXME: endianness */
			uuid = sdp_uuid128_create(uuid, p16);
		} else {
			DBG("ATT: Invalid Length field");
			goto fail;
		}

		prim = g_new0(struct primary, 1);
		prim->start = start;
		prim->end = end;
		prim->uuid = uuid;

		gatt->primary = g_slist_append(gatt->primary, prim);
	}

	if (end == 0) {
		DBG("ATT: Invalid PDU format");
		goto fail;
	}

	/*
	 * Discover all primary services sub-procedure shall send another
	 * Read by Group Type Request until Error Response is received and
	 * the Error Code is set to Attribute Not Found.
	 */
	gatt->atid = gatt_discover_primary(gatt->attrib,
				end + 1, 0xffff, primary_cb, gatt);
	if (gatt->atid == 0)
		goto fail;

	return;
fail:
	gatt_service_free(gatt);
}

static void connect_cb(GIOChannel *chan, GError *gerr, gpointer user_data)
{
	struct gatt_service *gatt = user_data;
	GAttrib *attrib;
	guint atid;

	if (gerr) {
		error("%s", gerr->message);
		goto fail;
	}

	attrib = g_attrib_new(chan);

	atid = gatt_discover_primary(attrib, 0x0001, 0xffff, primary_cb, gatt);
	if (atid == 0) {
		g_attrib_unref(attrib);
		goto fail;
	}

	gatt->attrib = attrib;
	gatt->atid = atid;

	services = g_slist_append(services, gatt);

	return;
fail:
	g_io_channel_unref(gatt->io);
	gatt_service_free(gatt);
}

int attrib_client_register(bdaddr_t *sba, bdaddr_t *dba, const char *path,
									int psm)
{
	struct gatt_service *gatt;
	GError *gerr = NULL;
	GIOChannel *io;

	/*
	 * Registering fake services/characteristics. The following
	 * paths/interfaces shall be registered after discover primary
	 * services only.
	 */

	gatt = g_new0(struct gatt_service, 1);
	gatt->path = g_strdup(path);
	bacpy(&gatt->sba, sba);
	bacpy(&gatt->dba, dba);
	gatt->psm = psm;

	if (psm < 0) {
		/*
		 * FIXME: when PSM is not given means that L2CAP fixed
		 * channel shall be used. For this case, ATT CID(0x0004).
		 */

		DBG("GATT over LE");

		return 0;
	}

	io = bt_io_connect(BT_IO_L2CAP, connect_cb, gatt, NULL, &gerr,
					BT_IO_OPT_SOURCE_BDADDR, sba,
					BT_IO_OPT_DEST_BDADDR, dba,
					BT_IO_OPT_PSM, psm,
					BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
					BT_IO_OPT_INVALID);

	if (!io) {
		error("%s", gerr->message);
		g_error_free(gerr);
		gatt_service_free(gatt);
		return -1;
	}

	gatt->io = io;

	return 0;
}

void attrib_client_unregister(const char *path)
{
	struct gatt_service *gatt;
	GSList *l;

	l = g_slist_find_custom(services, path, (GCompareFunc) gatt_path_cmp);
	if (!l)
		return;

	gatt = l->data;
	services = g_slist_remove(services, gatt);
	gatt_service_free(gatt);
}

int attrib_client_init(DBusConnection *conn)
{

	connection = dbus_connection_ref(conn);

	/*
	 * FIXME: if the adapter supports BLE start scanning. Temporary
	 * solution, this approach doesn't allow to control scanning based
	 * on the discoverable property.
	 */

	return 0;
}

void attrib_client_exit(void)
{
	dbus_connection_unref(connection);
}