aboutsummaryrefslogtreecommitdiffstats
path: root/attrib/gatttool.c
blob: c763c10df51561d670291c2407d1b4c73538f418 (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
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2007-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 <errno.h>
#include <glib.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/un.h>
#include <sys/socket.h>

#include <bluetooth/bluetooth.h>
#include <bluetooth/l2cap.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/sdp.h>
#include <bluetooth/sdp_lib.h>

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

#define GATT_UNIX_PATH "/var/run/gatt"
#define GATT_PSM 27

static gchar *opt_src = NULL;
static gchar *opt_dst = NULL;
static int opt_start = 0x0001;
static int opt_end = 0xffff;
static gboolean opt_unix = FALSE;
static gboolean opt_primary = FALSE;
static gboolean opt_characteristics = FALSE;
static GMainLoop *event_loop;

static int l2cap_connect(void)
{
	struct sockaddr_l2 addr;
	bdaddr_t sba, dba;
	int err, sk;

	/* Remote device */
	if (opt_dst == NULL) {
		g_printerr("Remote Bluetooth address required\n");
		return -EINVAL;
	}

	str2ba(opt_dst, &dba);

	/* Local adapter */
	if (opt_src != NULL) {
		if (!strncmp(opt_src, "hci", 3))
			hci_devba(atoi(opt_src + 3), &sba);
		else
			str2ba(opt_src, &sba);
		g_free(opt_src);
	} else
		bacpy(&sba, BDADDR_ANY);

	sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
	if (sk < 0) {
		err = errno;
		g_printerr("L2CAP socket create failed: %s(%d)\n", strerror(err), err);
		return -err;
	}

	memset(&addr, 0, sizeof(addr));
	addr.l2_family = AF_BLUETOOTH;
	bacpy(&addr.l2_bdaddr, &sba);

	if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
		err = errno;
		g_printerr("L2CAP socket bind failed: %s(%d)\n", strerror(err), err);
		close(sk);
		return -err;
	}

	memset(&addr, 0, sizeof(addr));
	addr.l2_family = AF_BLUETOOTH;
	bacpy(&addr.l2_bdaddr, &dba);
	addr.l2_psm = htobs(GATT_PSM);

	err = connect(sk, (struct sockaddr *) &addr, sizeof(addr));
	if (err < 0) {
		err = errno;
		g_printerr("L2CAP socket connect failed: %s(%d)\n", strerror(err), err);
		close(sk);
		return -err;
	}

	return sk;
}

static int unix_connect(const char *address)
{
	struct sockaddr_un addr;
	int sk, err;

	memset(&addr, 0, sizeof(addr));
	addr.sun_family = PF_UNIX;
	strncpy(addr.sun_path, address, sizeof(addr.sun_path) - 1);

	sk = socket(AF_UNIX, SOCK_STREAM, 0);
	if (sk < 0) {
		err = errno;
		g_printerr("Unix socket(%s) create failed: %s(%d)\n", address,
				strerror(err), err);
		return -err;
	}

	if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
		err = errno;
		g_printerr("Unix socket(%s) connect failed: %s(%d)\n", address,
				strerror(err), err);
		close(sk);
		return -err;
	}

	return sk;
}

static void primary_cb(guint8 status, const guint8 *pdu, guint16 plen,
		gpointer user_data)
{
	GAttrib *attrib = user_data;
	unsigned int i;
	uint8_t length;
	uint16_t end, start;
	guint atid;

	if (status == ATT_ECODE_ATTR_NOT_FOUND)
		goto done;

	if (status != 0) {
		g_printerr("Discover all primary services failed: %s\n",
						att_ecode2str(status));
		goto done;
	}

	if (pdu[0] != ATT_OP_READ_BY_GROUP_RESP) {
		g_printerr("Protocol error\n");
		goto done;
	}

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

		p16 = (void *) &pdu[i];
		start = btohs(*p16);
		p16++;
		end = btohs(*p16);
		p16++;
		if (length == 6) {
			uint16_t u16 = btohs(*p16);

			g_print("Service => start: 0x%04x, end: 0x%04x, "
					"uuid: 0x%04x\n", start, end, u16);
		} else if (length == 20) {
			/* FIXME: endianness */
		} else {
			g_printerr("ATT: Invalid Length field\n");
			goto done;
		}
	}

	if (end == 0) {
		g_printerr("ATT: Invalid PDU format\n");
		goto done;
	}

	/*
	 * 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.
	 */
	atid = gatt_discover_primary(attrib,
				end + 1, 0xffff, primary_cb, attrib);
	if (atid == 0)
		g_printerr("Discovery primary failed\n");

done:
	g_main_loop_quit(event_loop);
}

static gboolean primary(gpointer user_data)
{
	int sk;
	guint atid;
	GIOChannel *chan;
	GAttrib *attrib;

	if (opt_unix)
		sk = unix_connect(GATT_UNIX_PATH);
	else
		sk = l2cap_connect();
	if (sk < 0) {
		g_main_loop_quit(event_loop);
		return FALSE;
	}

	chan = g_io_channel_unix_new(sk);
	g_io_channel_set_flags(chan, G_IO_FLAG_NONBLOCK, NULL);
	attrib = g_attrib_new(chan);

	atid = gatt_discover_primary(attrib, 0x0001, 0xffff, primary_cb, attrib);
	if (atid == 0)
		g_attrib_unref(attrib);

	return FALSE;
}

static GOptionEntry primary_options[] = {
	{ "start", 's' , 0, G_OPTION_ARG_INT, &opt_start,
		"Starting handle(optional)", "0x0000" },
	{ "end", 'e' , 0, G_OPTION_ARG_INT, &opt_end,
		"Ending handle(optional)", "0xffff" },
	{ NULL },
};

static GOptionEntry gatt_options[] = {
	{ "primary", 0, 0, G_OPTION_ARG_NONE, &opt_primary,
		"Primary Service Discovery", NULL },
	{ "characteristics", 0, 0, G_OPTION_ARG_NONE, &opt_characteristics,
		"Characteristics Discovery", NULL },
	{ NULL },
};

static GOptionEntry options[] = {
	{ "unix", 'u', 0, G_OPTION_ARG_NONE, &opt_unix,
		"Connect to server using Unix socket" , NULL },
	{ "adapter", 'i', 0, G_OPTION_ARG_STRING, &opt_src,
		"Specify local adapter interface", "hciX" },
	{ "device", 'b', 0, G_OPTION_ARG_STRING, &opt_dst,
		"Specify remote Bluetooth address", "MAC" },
	{ NULL },
};

int main(int argc, char *argv[])
{
	GOptionContext *context;
	GOptionGroup *gatt_group, *primary_group;
	GError *gerr = NULL;

	context = g_option_context_new(NULL);
	g_option_context_add_main_entries(context, options, NULL);

	/* GATT commands */
	gatt_group = g_option_group_new("gatt", "GATT commands",
				"Show all GATT commands", NULL, NULL);
	g_option_context_add_group(context, gatt_group);
	g_option_group_add_entries(gatt_group, gatt_options);

	/* Primary Services arguments */
	primary_group = g_option_group_new("primary",
			"Discover primary services arguments",
			"Show all Primary arguments", NULL, NULL);
	g_option_context_add_group(context, primary_group);
	g_option_group_add_entries(primary_group, primary_options);

	if (g_option_context_parse(context, &argc, &argv, &gerr) == FALSE) {
		g_printerr("%s\n", gerr->message);
		g_error_free(gerr);
	}

	event_loop = g_main_loop_new(NULL, FALSE);
	if (opt_primary)
		g_idle_add(primary, NULL);
	g_main_loop_run(event_loop);

	g_option_context_free(context);

	return 0;
}