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/uuid.h>
45 static gchar *opt_src = NULL;
46 static gchar *opt_dst = NULL;
47 static gchar *opt_dst_type = NULL;
48 static gchar *opt_value = NULL;
49 static gchar *opt_sec_level = NULL;
50 static bt_uuid_t *opt_uuid = NULL;
51 static int opt_start = 0x0001;
52 static int opt_end = 0xffff;
53 static int opt_handle = -1;
54 static int opt_mtu = 0;
55 static int opt_psm = 0;
56 static int opt_offset = 0;
57 static gboolean opt_primary = FALSE;
58 static gboolean opt_characteristics = FALSE;
59 static gboolean opt_char_read = FALSE;
60 static gboolean opt_listen = FALSE;
61 static gboolean opt_char_desc = FALSE;
62 static gboolean opt_char_write = FALSE;
63 static gboolean opt_char_write_req = FALSE;
64 static gboolean opt_interactive = FALSE;
65 static GMainLoop *event_loop;
66 static gboolean got_error = FALSE;
67 static GSourceFunc operation;
69 struct characteristic_data {
75 static void events_handler(const uint8_t *pdu, uint16_t len, gpointer user_data)
77 GAttrib *attrib = user_data;
78 uint8_t opdu[ATT_MAX_MTU];
79 uint16_t handle, i, olen = 0;
81 handle = att_get_u16(&pdu[1]);
84 case ATT_OP_HANDLE_NOTIFY:
85 g_print("Notification handle = 0x%04x value: ", handle);
87 case ATT_OP_HANDLE_IND:
88 g_print("Indication handle = 0x%04x value: ", handle);
91 g_print("Invalid opcode\n");
95 for (i = 3; i < len; i++)
96 g_print("%02x ", pdu[i]);
100 if (pdu[0] == ATT_OP_HANDLE_NOTIFY)
103 olen = enc_confirmation(opdu, sizeof(opdu));
106 g_attrib_send(attrib, 0, opdu[0], opdu, olen, NULL, NULL, NULL);
109 static gboolean listen_start(gpointer user_data)
111 GAttrib *attrib = user_data;
113 g_attrib_register(attrib, ATT_OP_HANDLE_NOTIFY, events_handler,
115 g_attrib_register(attrib, ATT_OP_HANDLE_IND, events_handler,
121 static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
126 g_printerr("%s\n", err->message);
128 g_main_loop_quit(event_loop);
131 attrib = g_attrib_new(io);
134 g_idle_add(listen_start, attrib);
139 static void primary_all_cb(GSList *services, guint8 status, gpointer user_data)
144 g_printerr("Discover all primary services failed: %s\n",
145 att_ecode2str(status));
149 for (l = services; l; l = l->next) {
150 struct gatt_primary *prim = l->data;
151 g_print("attr handle = 0x%04x, end grp handle = 0x%04x "
152 "uuid: %s\n", prim->range.start, prim->range.end, prim->uuid);
156 g_main_loop_quit(event_loop);
159 static void primary_by_uuid_cb(GSList *ranges, guint8 status,
165 g_printerr("Discover primary services by UUID failed: %s\n",
166 att_ecode2str(status));
170 for (l = ranges; l; l = l->next) {
171 struct att_range *range = l->data;
172 g_print("Starting handle: %04x Ending handle: %04x\n",
173 range->start, range->end);
177 g_main_loop_quit(event_loop);
180 static gboolean primary(gpointer user_data)
182 GAttrib *attrib = user_data;
185 gatt_discover_primary(attrib, opt_uuid, primary_by_uuid_cb,
188 gatt_discover_primary(attrib, NULL, primary_all_cb, NULL);
193 static void char_discovered_cb(GSList *characteristics, guint8 status,
199 g_printerr("Discover all characteristics failed: %s\n",
200 att_ecode2str(status));
204 for (l = characteristics; l; l = l->next) {
205 struct gatt_char *chars = l->data;
207 g_print("handle = 0x%04x, char properties = 0x%02x, char value "
208 "handle = 0x%04x, uuid = %s\n", chars->handle,
209 chars->properties, chars->value_handle, chars->uuid);
213 g_main_loop_quit(event_loop);
216 static gboolean characteristics(gpointer user_data)
218 GAttrib *attrib = user_data;
220 gatt_discover_char(attrib, opt_start, opt_end, opt_uuid,
221 char_discovered_cb, NULL);
226 static void char_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
229 uint8_t value[ATT_MAX_MTU];
233 g_printerr("Characteristic value/descriptor read failed: %s\n",
234 att_ecode2str(status));
237 if (!dec_read_resp(pdu, plen, value, &vlen)) {
238 g_printerr("Protocol error\n");
241 g_print("Characteristic value/descriptor: ");
242 for (i = 0; i < vlen; i++)
243 g_print("%02x ", value[i]);
247 if (opt_listen == FALSE)
248 g_main_loop_quit(event_loop);
251 static void char_read_by_uuid_cb(guint8 status, const guint8 *pdu,
252 guint16 plen, gpointer user_data)
254 struct characteristic_data *char_data = user_data;
255 struct att_data_list *list;
258 if (status == ATT_ECODE_ATTR_NOT_FOUND &&
259 char_data->start != opt_start)
263 g_printerr("Read characteristics by UUID failed: %s\n",
264 att_ecode2str(status));
268 list = dec_read_by_type_resp(pdu, plen);
272 for (i = 0; i < list->num; i++) {
273 uint8_t *value = list->data[i];
276 char_data->start = att_get_u16(value) + 1;
278 g_print("handle: 0x%04x \t value: ", att_get_u16(value));
280 for (j = 0; j < list->len - 2; j++, value++)
281 g_print("%02x ", *value);
285 att_data_list_free(list);
289 g_main_loop_quit(event_loop);
292 static gboolean characteristics_read(gpointer user_data)
294 GAttrib *attrib = user_data;
296 if (opt_uuid != NULL) {
297 struct characteristic_data *char_data;
299 char_data = g_new(struct characteristic_data, 1);
300 char_data->attrib = attrib;
301 char_data->start = opt_start;
302 char_data->end = opt_end;
304 gatt_read_char_by_uuid(attrib, opt_start, opt_end, opt_uuid,
305 char_read_by_uuid_cb, char_data);
310 if (opt_handle <= 0) {
311 g_printerr("A valid handle is required\n");
312 g_main_loop_quit(event_loop);
316 gatt_read_char(attrib, opt_handle, opt_offset, char_read_cb, attrib);
321 static void mainloop_quit(gpointer user_data)
323 uint8_t *value = user_data;
326 g_main_loop_quit(event_loop);
329 static gboolean characteristics_write(gpointer user_data)
331 GAttrib *attrib = user_data;
335 if (opt_handle <= 0) {
336 g_printerr("A valid handle is required\n");
340 if (opt_value == NULL || opt_value[0] == '\0') {
341 g_printerr("A value is required\n");
345 len = gatt_attr_data_from_string(opt_value, &value);
347 g_printerr("Invalid value\n");
351 gatt_write_cmd(attrib, opt_handle, value, len, mainloop_quit, value);
356 g_main_loop_quit(event_loop);
360 static void char_write_req_cb(guint8 status, const guint8 *pdu, guint16 plen,
364 g_printerr("Characteristic Write Request failed: "
365 "%s\n", att_ecode2str(status));
369 if (!dec_write_resp(pdu, plen)) {
370 g_printerr("Protocol error\n");
374 g_print("Characteristic value was written successfully\n");
377 if (opt_listen == FALSE)
378 g_main_loop_quit(event_loop);
381 static gboolean characteristics_write_req(gpointer user_data)
383 GAttrib *attrib = user_data;
387 if (opt_handle <= 0) {
388 g_printerr("A valid handle is required\n");
392 if (opt_value == NULL || opt_value[0] == '\0') {
393 g_printerr("A value is required\n");
397 len = gatt_attr_data_from_string(opt_value, &value);
399 g_printerr("Invalid value\n");
403 gatt_write_char(attrib, opt_handle, value, len, char_write_req_cb,
409 g_main_loop_quit(event_loop);
413 static void char_desc_cb(guint8 status, const guint8 *pdu, guint16 plen,
416 struct att_data_list *list;
421 g_printerr("Discover all characteristic descriptors failed: "
422 "%s\n", att_ecode2str(status));
426 list = dec_find_info_resp(pdu, plen, &format);
430 for (i = 0; i < list->num; i++) {
431 char uuidstr[MAX_LEN_UUID_STR];
436 value = list->data[i];
437 handle = att_get_u16(value);
440 uuid = att_get_uuid16(&value[2]);
442 uuid = att_get_uuid128(&value[2]);
444 bt_uuid_to_string(&uuid, uuidstr, MAX_LEN_UUID_STR);
445 g_print("handle = 0x%04x, uuid = %s\n", handle, uuidstr);
448 att_data_list_free(list);
451 if (opt_listen == FALSE)
452 g_main_loop_quit(event_loop);
455 static gboolean characteristics_desc(gpointer user_data)
457 GAttrib *attrib = user_data;
459 gatt_find_info(attrib, opt_start, opt_end, char_desc_cb, NULL);
464 static gboolean parse_uuid(const char *key, const char *value,
465 gpointer user_data, GError **error)
470 opt_uuid = g_try_malloc(sizeof(bt_uuid_t));
471 if (opt_uuid == NULL)
474 if (bt_string_to_uuid(opt_uuid, value) < 0)
480 static GOptionEntry primary_char_options[] = {
481 { "start", 's' , 0, G_OPTION_ARG_INT, &opt_start,
482 "Starting handle(optional)", "0x0001" },
483 { "end", 'e' , 0, G_OPTION_ARG_INT, &opt_end,
484 "Ending handle(optional)", "0xffff" },
485 { "uuid", 'u', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
486 parse_uuid, "UUID16 or UUID128(optional)", "0x1801"},
490 static GOptionEntry char_rw_options[] = {
491 { "handle", 'a' , 0, G_OPTION_ARG_INT, &opt_handle,
492 "Read/Write characteristic by handle(required)", "0x0001" },
493 { "value", 'n' , 0, G_OPTION_ARG_STRING, &opt_value,
494 "Write characteristic value (required for write operation)",
496 { "offset", 'o', 0, G_OPTION_ARG_INT, &opt_offset,
497 "Offset to long read characteristic by handle", "N"},
501 static GOptionEntry gatt_options[] = {
502 { "primary", 0, 0, G_OPTION_ARG_NONE, &opt_primary,
503 "Primary Service Discovery", NULL },
504 { "characteristics", 0, 0, G_OPTION_ARG_NONE, &opt_characteristics,
505 "Characteristics Discovery", NULL },
506 { "char-read", 0, 0, G_OPTION_ARG_NONE, &opt_char_read,
507 "Characteristics Value/Descriptor Read", NULL },
508 { "char-write", 0, 0, G_OPTION_ARG_NONE, &opt_char_write,
509 "Characteristics Value Write Without Response (Write Command)",
511 { "char-write-req", 0, 0, G_OPTION_ARG_NONE, &opt_char_write_req,
512 "Characteristics Value Write (Write Request)", NULL },
513 { "char-desc", 0, 0, G_OPTION_ARG_NONE, &opt_char_desc,
514 "Characteristics Descriptor Discovery", NULL },
515 { "listen", 0, 0, G_OPTION_ARG_NONE, &opt_listen,
516 "Listen for notifications and indications", NULL },
517 { "interactive", 'I', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
518 &opt_interactive, "Use interactive mode", NULL },
522 static GOptionEntry options[] = {
523 { "adapter", 'i', 0, G_OPTION_ARG_STRING, &opt_src,
524 "Specify local adapter interface", "hciX" },
525 { "device", 'b', 0, G_OPTION_ARG_STRING, &opt_dst,
526 "Specify remote Bluetooth address", "MAC" },
527 { "addr-type", 't', 0, G_OPTION_ARG_STRING, &opt_dst_type,
528 "Set LE address type. Default: public", "[public | random]"},
529 { "mtu", 'm', 0, G_OPTION_ARG_INT, &opt_mtu,
530 "Specify the MTU size", "MTU" },
531 { "psm", 'p', 0, G_OPTION_ARG_INT, &opt_psm,
532 "Specify the PSM for GATT/ATT over BR/EDR", "PSM" },
533 { "sec-level", 'l', 0, G_OPTION_ARG_STRING, &opt_sec_level,
534 "Set security level. Default: low", "[low | medium | high]"},
538 int main(int argc, char *argv[])
540 GOptionContext *context;
541 GOptionGroup *gatt_group, *params_group, *char_rw_group;
545 opt_dst_type = g_strdup("public");
546 opt_sec_level = g_strdup("low");
548 context = g_option_context_new(NULL);
549 g_option_context_add_main_entries(context, options, NULL);
552 gatt_group = g_option_group_new("gatt", "GATT commands",
553 "Show all GATT commands", NULL, NULL);
554 g_option_context_add_group(context, gatt_group);
555 g_option_group_add_entries(gatt_group, gatt_options);
557 /* Primary Services and Characteristics arguments */
558 params_group = g_option_group_new("params",
559 "Primary Services/Characteristics arguments",
560 "Show all Primary Services/Characteristics arguments",
562 g_option_context_add_group(context, params_group);
563 g_option_group_add_entries(params_group, primary_char_options);
565 /* Characteristics value/descriptor read/write arguments */
566 char_rw_group = g_option_group_new("char-read-write",
567 "Characteristics Value/Descriptor Read/Write arguments",
568 "Show all Characteristics Value/Descriptor Read/Write "
571 g_option_context_add_group(context, char_rw_group);
572 g_option_group_add_entries(char_rw_group, char_rw_options);
574 if (g_option_context_parse(context, &argc, &argv, &gerr) == FALSE) {
575 g_printerr("%s\n", gerr->message);
579 if (opt_interactive) {
580 interactive(opt_src, opt_dst, opt_dst_type, opt_psm);
586 else if (opt_characteristics)
587 operation = characteristics;
588 else if (opt_char_read)
589 operation = characteristics_read;
590 else if (opt_char_write)
591 operation = characteristics_write;
592 else if (opt_char_write_req)
593 operation = characteristics_write_req;
594 else if (opt_char_desc)
595 operation = characteristics_desc;
597 gchar *help = g_option_context_get_help(context, TRUE, NULL);
598 g_print("%s\n", help);
604 chan = gatt_connect(opt_src, opt_dst, opt_dst_type, opt_sec_level,
605 opt_psm, opt_mtu, connect_cb);
611 event_loop = g_main_loop_new(NULL, FALSE);
613 g_main_loop_run(event_loop);
615 g_main_loop_unref(event_loop);
618 g_option_context_free(context);
622 g_free(opt_sec_level);