3
* BlueZ - Bluetooth protocol stack for Linux
5
* Copyright (C) 2010 Nokia Corporation
6
* Copyright (C) 2010 Marcel Holtmann <marcel@holtmann.org>
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 2 of the License, or
12
* (at your option) any later version.
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34
#include <bluetooth/bluetooth.h>
35
#include <bluetooth/hci.h>
36
#include <bluetooth/hci_lib.h>
37
#include <bluetooth/sdp.h>
38
#include <bluetooth/sdp_lib.h>
43
#include "glib-helper.h"
46
/* Minimum MTU for L2CAP connections over BR/EDR */
47
#define ATT_MIN_MTU_L2CAP 48
49
static gchar *opt_src = NULL;
50
static gchar *opt_dst = NULL;
51
static gchar *opt_value = NULL;
52
static gchar *opt_sec_level = "low";
53
static uuid_t *opt_uuid = NULL;
54
static int opt_start = 0x0001;
55
static int opt_end = 0xffff;
56
static int opt_handle = -1;
57
static int opt_mtu = 0;
58
static int opt_psm = 0x1f;
59
static gboolean opt_primary = FALSE;
60
static gboolean opt_characteristics = FALSE;
61
static gboolean opt_char_read = FALSE;
62
static gboolean opt_listen = FALSE;
63
static gboolean opt_char_desc = FALSE;
64
static gboolean opt_le = FALSE;
65
static gboolean opt_char_write = FALSE;
66
static GMainLoop *event_loop;
67
static gboolean got_error = FALSE;
69
struct characteristic_data {
75
static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
78
g_printerr("%s\n", err->message);
80
g_main_loop_quit(event_loop);
84
static GIOChannel *do_connect(gboolean le)
89
BtIOSecLevel sec_level;
91
/* This check is required because currently setsockopt() returns no
92
* errors for MTU values smaller than the allowed minimum. */
93
if (opt_mtu != 0 && opt_mtu < ATT_MIN_MTU_L2CAP) {
94
g_printerr("MTU cannot be smaller than %d\n",
100
if (opt_dst == NULL) {
101
g_printerr("Remote Bluetooth address required\n");
104
str2ba(opt_dst, &dba);
107
if (opt_src != NULL) {
108
if (!strncmp(opt_src, "hci", 3))
109
hci_devba(atoi(opt_src + 3), &sba);
111
str2ba(opt_src, &sba);
113
bacpy(&sba, BDADDR_ANY);
115
if (strcmp(opt_sec_level, "medium") == 0)
116
sec_level = BT_IO_SEC_MEDIUM;
117
else if (strcmp(opt_sec_level, "high") == 0)
118
sec_level = BT_IO_SEC_HIGH;
120
sec_level = BT_IO_SEC_LOW;
123
chan = bt_io_connect(BT_IO_L2CAP, connect_cb, NULL, NULL, &err,
124
BT_IO_OPT_SOURCE_BDADDR, &sba,
125
BT_IO_OPT_DEST_BDADDR, &dba,
126
BT_IO_OPT_CID, GATT_CID,
127
BT_IO_OPT_OMTU, opt_mtu,
128
BT_IO_OPT_SEC_LEVEL, sec_level,
131
chan = bt_io_connect(BT_IO_L2CAP, connect_cb, NULL, NULL, &err,
132
BT_IO_OPT_SOURCE_BDADDR, &sba,
133
BT_IO_OPT_DEST_BDADDR, &dba,
134
BT_IO_OPT_PSM, opt_psm,
135
BT_IO_OPT_OMTU, opt_mtu,
136
BT_IO_OPT_SEC_LEVEL, sec_level,
140
g_printerr("%s\n", err->message);
148
static void primary_all_cb(guint8 status, const guint8 *pdu, guint16 plen,
151
GAttrib *attrib = user_data;
152
struct att_data_list *list;
156
if (status == ATT_ECODE_ATTR_NOT_FOUND)
160
g_printerr("Discover all primary services failed: %s\n",
161
att_ecode2str(status));
165
list = dec_read_by_grp_resp(pdu, plen);
169
for (i = 0, end = 0; i < list->num; i++) {
170
char uuidstr[MAX_LEN_UUID_STR];
171
uint8_t *value = list->data[i];
176
/* Each element contains: attribute handle, end group handle
177
* and attribute value */
178
length = list->len - 2 * sizeof(uint16_t);
179
start = att_get_u16(value);
180
end = att_get_u16(&value[2]);
182
g_print("attr handle = 0x%04x, end grp handle = 0x%04x, ",
185
sdp_uuid16_create(&uuid, att_get_u16(&value[4]));
187
sdp_uuid128_create(&uuid, value + 4);
189
sdp_uuid2strn(&uuid, uuidstr, MAX_LEN_UUID_STR);
190
g_print("attr value (UUID) = %s\n", uuidstr);
193
att_data_list_free(list);
195
/* Don't go beyond the maximum handle value */
200
* Discover all primary services sub-procedure shall send another
201
* Read by Group Type Request until Error Response is received and
202
* the Error Code is set to Attribute Not Found.
205
gatt_discover_primary(attrib, end + 1, opt_end, NULL, primary_all_cb,
210
if (opt_listen == FALSE)
211
g_main_loop_quit(event_loop);
214
static void primary_by_uuid_cb(guint8 status, const guint8 *pdu, guint16 plen,
220
g_printerr("Discover primary services by UUID failed: %s\n",
221
att_ecode2str(status));
225
ranges = dec_find_by_type_resp(pdu, plen);
226
if (ranges == NULL) {
227
g_printerr("Protocol error!\n");
231
for (l = ranges; l; l = l->next) {
232
struct att_range *range = l->data;
233
g_print("Starting handle: %04x Ending handle: %04x\n",
234
range->start, range->end);
237
g_slist_foreach(ranges, (GFunc) g_free, NULL);
238
g_slist_free(ranges);
241
g_main_loop_quit(event_loop);
244
static void events_handler(const uint8_t *pdu, uint16_t len, gpointer user_data)
246
GAttrib *attrib = user_data;
247
uint8_t opdu[ATT_MAX_MTU];
248
uint16_t handle, i, olen = 0;
250
handle = att_get_u16(&pdu[1]);
253
case ATT_OP_HANDLE_NOTIFY:
254
g_print("Notification handle = 0x%04x value: ", handle);
256
case ATT_OP_HANDLE_IND:
257
g_print("Indication handle = 0x%04x value: ", handle);
260
g_print("Invalid opcode\n");
264
for (i = 3; i < len; i++)
265
g_print("%02x ", pdu[i]);
269
if (pdu[0] == ATT_OP_HANDLE_NOTIFY)
272
olen = enc_confirmation(opdu, sizeof(opdu));
275
g_attrib_send(attrib, opdu[0], opdu, olen, NULL, NULL, NULL);
278
static gboolean listen_start(gpointer user_data)
280
GAttrib *attrib = user_data;
282
g_attrib_register(attrib, ATT_OP_HANDLE_NOTIFY, events_handler,
284
g_attrib_register(attrib, ATT_OP_HANDLE_IND, events_handler,
290
static gboolean primary(gpointer user_data)
292
GAttrib *attrib = user_data;
295
gatt_discover_primary(attrib, opt_start, opt_end, opt_uuid,
296
primary_by_uuid_cb, attrib);
298
gatt_discover_primary(attrib, opt_start, opt_end, NULL,
299
primary_all_cb, attrib);
304
static void char_discovered_cb(guint8 status, const guint8 *pdu, guint16 plen,
307
struct characteristic_data *char_data = user_data;
308
struct att_data_list *list;
309
uint16_t last = char_data->start;
312
if (status == ATT_ECODE_ATTR_NOT_FOUND)
316
g_printerr("Discover all characteristics failed: %s\n",
317
att_ecode2str(status));
321
list = dec_read_by_type_resp(pdu, plen);
325
for (i = 0; i < list->num; i++) {
326
uint8_t *value = list->data[i];
327
char uuidstr[MAX_LEN_UUID_STR];
330
last = att_get_u16(value);
332
g_print("handle = 0x%04x, char properties = 0x%02x, "
333
"char value handle = 0x%04x, ", last, value[2],
334
att_get_u16(&value[3]));
337
sdp_uuid16_create(&uuid, att_get_u16(&value[5]));
339
sdp_uuid128_create(&uuid, value + 5);
341
sdp_uuid2strn(&uuid, uuidstr, MAX_LEN_UUID_STR);
342
g_print("uuid = %s\n", uuidstr);
345
att_data_list_free(list);
347
/* Fetch remaining characteristics for the CURRENT primary service */
348
gatt_discover_char(char_data->attrib, last + 1, char_data->end,
349
char_discovered_cb, char_data);
355
if (opt_listen == FALSE)
356
g_main_loop_quit(event_loop);
359
static gboolean characteristics(gpointer user_data)
361
GAttrib *attrib = user_data;
362
struct characteristic_data *char_data;
364
char_data = g_new(struct characteristic_data, 1);
365
char_data->attrib = attrib;
366
char_data->start = opt_start;
367
char_data->end = opt_end;
369
gatt_discover_char(attrib, opt_start, opt_end, char_discovered_cb,
375
static void char_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
378
uint8_t value[ATT_MAX_MTU];
382
g_printerr("Characteristic value/descriptor read failed: %s\n",
383
att_ecode2str(status));
386
if (!dec_read_resp(pdu, plen, value, &vlen)) {
387
g_printerr("Protocol error\n");
390
g_print("Characteristic value/descriptor: ");
391
for (i = 0; i < vlen; i++)
392
g_print("%02x ", value[i]);
396
if (opt_listen == FALSE)
397
g_main_loop_quit(event_loop);
400
static void char_read_by_uuid_cb(guint8 status, const guint8 *pdu,
401
guint16 plen, gpointer user_data)
403
struct characteristic_data *char_data = user_data;
404
struct att_data_list *list;
407
if (status == ATT_ECODE_ATTR_NOT_FOUND &&
408
char_data->start != opt_start)
412
g_printerr("Read characteristics by UUID failed: %s\n",
413
att_ecode2str(status));
417
list = dec_read_by_type_resp(pdu, plen);
421
for (i = 0; i < list->num; i++) {
422
uint8_t *value = list->data[i];
425
char_data->start = att_get_u16(value) + 1;
427
g_print("handle: 0x%04x \t value: ", att_get_u16(value));
429
for (j = 0; j < list->len - 2; j++, value++)
430
g_print("%02x ", *value);
434
att_data_list_free(list);
436
gatt_read_char_by_uuid(char_data->attrib, char_data->start,
437
char_data->end, opt_uuid,
438
char_read_by_uuid_cb,
444
g_main_loop_quit(event_loop);
447
static gboolean characteristics_read(gpointer user_data)
449
GAttrib *attrib = user_data;
451
if (opt_uuid != NULL) {
452
struct characteristic_data *char_data;
454
char_data = g_new(struct characteristic_data, 1);
455
char_data->attrib = attrib;
456
char_data->start = opt_start;
457
char_data->end = opt_end;
459
gatt_read_char_by_uuid(attrib, opt_start, opt_end, opt_uuid,
460
char_read_by_uuid_cb, char_data);
465
if (opt_handle <= 0) {
466
g_printerr("A valid handle is required\n");
467
g_main_loop_quit(event_loop);
471
gatt_read_char(attrib, opt_handle, char_read_cb, attrib);
476
static size_t attr_data_from_string(const char *str, uint8_t **data)
481
size = strlen(str) / 2;
482
*data = g_try_malloc0(size);
487
for (i = 0; i < size; i++) {
488
memcpy(tmp, str + (i * 2), 2);
489
(*data)[i] = (uint8_t) strtol(tmp, NULL, 16);
495
static void mainloop_quit(gpointer user_data)
497
uint8_t *value = user_data;
500
g_main_loop_quit(event_loop);
503
static gboolean characteristics_write(gpointer user_data)
505
GAttrib *attrib = user_data;
509
if (opt_handle <= 0) {
510
g_printerr("A valid handle is required\n");
514
if (opt_value == NULL || opt_value[0] == '\0') {
515
g_printerr("A value is required\n");
519
len = attr_data_from_string(opt_value, &value);
521
g_printerr("Invalid value\n");
525
gatt_write_cmd(attrib, opt_handle, value, len, mainloop_quit, value);
530
g_main_loop_quit(event_loop);
534
static void char_desc_cb(guint8 status, const guint8 *pdu, guint16 plen,
537
struct att_data_list *list;
542
g_printerr("Discover all characteristic descriptors failed: "
543
"%s\n", att_ecode2str(status));
547
list = dec_find_info_resp(pdu, plen, &format);
551
for (i = 0; i < list->num; i++) {
552
char uuidstr[MAX_LEN_UUID_STR];
557
value = list->data[i];
558
handle = att_get_u16(value);
561
sdp_uuid16_create(&uuid, att_get_u16(&value[2]));
563
sdp_uuid128_create(&uuid, &value[2]);
565
sdp_uuid2strn(&uuid, uuidstr, MAX_LEN_UUID_STR);
566
g_print("handle = 0x%04x, uuid = %s\n", handle, uuidstr);
569
att_data_list_free(list);
572
if (opt_listen == FALSE)
573
g_main_loop_quit(event_loop);
576
static gboolean characteristics_desc(gpointer user_data)
578
GAttrib *attrib = user_data;
580
gatt_find_info(attrib, opt_start, opt_end, char_desc_cb, NULL);
585
static gboolean parse_uuid(const char *key, const char *value,
586
gpointer user_data, GError **error)
591
opt_uuid = g_try_malloc(sizeof(uuid_t));
592
if (opt_uuid == NULL)
595
if (bt_string2uuid(opt_uuid, value) < 0)
601
static GOptionEntry primary_char_options[] = {
602
{ "start", 's' , 0, G_OPTION_ARG_INT, &opt_start,
603
"Starting handle(optional)", "0x0001" },
604
{ "end", 'e' , 0, G_OPTION_ARG_INT, &opt_end,
605
"Ending handle(optional)", "0xffff" },
606
{ "uuid", 'u', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
607
parse_uuid, "UUID16 or UUID128(optional)", "0x1801"},
611
static GOptionEntry char_rw_options[] = {
612
{ "handle", 'a' , 0, G_OPTION_ARG_INT, &opt_handle,
613
"Read/Write characteristic by handle(required)", "0x0001" },
614
{ "value", 'n' , 0, G_OPTION_ARG_STRING, &opt_value,
615
"Write characteristic value (required for write operation)",
620
static GOptionEntry gatt_options[] = {
621
{ "primary", 0, 0, G_OPTION_ARG_NONE, &opt_primary,
622
"Primary Service Discovery", NULL },
623
{ "characteristics", 0, 0, G_OPTION_ARG_NONE, &opt_characteristics,
624
"Characteristics Discovery", NULL },
625
{ "char-read", 0, 0, G_OPTION_ARG_NONE, &opt_char_read,
626
"Characteristics Value/Descriptor Read", NULL },
627
{ "char-write", 0, 0, G_OPTION_ARG_NONE, &opt_char_write,
628
"Characteristics Value Write", NULL },
629
{ "char-desc", 0, 0, G_OPTION_ARG_NONE, &opt_char_desc,
630
"Characteristics Descriptor Discovery", NULL },
631
{ "listen", 0, 0, G_OPTION_ARG_NONE, &opt_listen,
632
"Listen for notifications and indications", NULL },
633
{ "le", 0, 0, G_OPTION_ARG_NONE, &opt_le,
634
"Use Bluetooth Low Energy transport", NULL },
638
static GOptionEntry options[] = {
639
{ "adapter", 'i', 0, G_OPTION_ARG_STRING, &opt_src,
640
"Specify local adapter interface", "hciX" },
641
{ "device", 'b', 0, G_OPTION_ARG_STRING, &opt_dst,
642
"Specify remote Bluetooth address", "MAC" },
643
{ "mtu", 'm', 0, G_OPTION_ARG_INT, &opt_mtu,
644
"Specify the MTU size", "MTU" },
645
{ "psm", 'p', 0, G_OPTION_ARG_INT, &opt_psm,
646
"Specify the PSM for GATT/ATT over BR/EDR", "PSM" },
647
{ "sec-level", 'l', 0, G_OPTION_ARG_STRING, &opt_sec_level,
648
"Set security level. Default: low", "[low | medium | high]"},
652
int main(int argc, char *argv[])
654
GOptionContext *context;
655
GOptionGroup *gatt_group, *params_group, *char_rw_group;
659
GSourceFunc callback;
661
context = g_option_context_new(NULL);
662
g_option_context_add_main_entries(context, options, NULL);
665
gatt_group = g_option_group_new("gatt", "GATT commands",
666
"Show all GATT commands", NULL, NULL);
667
g_option_context_add_group(context, gatt_group);
668
g_option_group_add_entries(gatt_group, gatt_options);
670
/* Primary Services and Characteristics arguments */
671
params_group = g_option_group_new("params",
672
"Primary Services/Characteristics arguments",
673
"Show all Primary Services/Characteristics arguments",
675
g_option_context_add_group(context, params_group);
676
g_option_group_add_entries(params_group, primary_char_options);
678
/* Characteristics value/descriptor read/write arguments */
679
char_rw_group = g_option_group_new("char-read-write",
680
"Characteristics Value/Descriptor Read/Write arguments",
681
"Show all Characteristics Value/Descriptor Read/Write "
684
g_option_context_add_group(context, char_rw_group);
685
g_option_group_add_entries(char_rw_group, char_rw_options);
687
if (g_option_context_parse(context, &argc, &argv, &gerr) == FALSE) {
688
g_printerr("%s\n", gerr->message);
694
else if (opt_characteristics)
695
callback = characteristics;
696
else if (opt_char_read)
697
callback = characteristics_read;
698
else if (opt_char_write)
699
callback = characteristics_write;
700
else if (opt_char_desc)
701
callback = characteristics_desc;
703
gchar *help = g_option_context_get_help(context, TRUE, NULL);
704
g_print("%s\n", help);
710
chan = do_connect(opt_le);
716
attrib = g_attrib_new(chan);
718
event_loop = g_main_loop_new(NULL, FALSE);
721
g_idle_add(listen_start, attrib);
723
g_idle_add(callback, attrib);
725
g_main_loop_run(event_loop);
727
g_attrib_unregister_all(attrib);
729
g_main_loop_unref(event_loop);
731
g_io_channel_unref(chan);
732
g_attrib_unref(attrib);
735
g_option_context_free(context);