Tizen 2.0 Release
[profile/ivi/bluez.git] / attrib / gatttool.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2010  Nokia Corporation
6  *  Copyright (C) 2010  Marcel Holtmann <marcel@holtmann.org>
7  *
8  *
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.
13  *
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.
18  *
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
22  *
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <errno.h>
30 #include <glib.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 #include <bluetooth/bluetooth.h>
35 #include <bluetooth/hci.h>
36 #include <bluetooth/hci_lib.h>
37 #include <bluetooth/uuid.h>
38
39 #include "att.h"
40 #include "btio.h"
41 #include "gattrib.h"
42 #include "gatt.h"
43 #include "gatttool.h"
44
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;
68
69 struct characteristic_data {
70         GAttrib *attrib;
71         uint16_t start;
72         uint16_t end;
73 };
74
75 static void events_handler(const uint8_t *pdu, uint16_t len, gpointer user_data)
76 {
77         GAttrib *attrib = user_data;
78         uint8_t opdu[ATT_MAX_MTU];
79         uint16_t handle, i, olen = 0;
80
81         handle = att_get_u16(&pdu[1]);
82
83         switch (pdu[0]) {
84         case ATT_OP_HANDLE_NOTIFY:
85                 g_print("Notification handle = 0x%04x value: ", handle);
86                 break;
87         case ATT_OP_HANDLE_IND:
88                 g_print("Indication   handle = 0x%04x value: ", handle);
89                 break;
90         default:
91                 g_print("Invalid opcode\n");
92                 return;
93         }
94
95         for (i = 3; i < len; i++)
96                 g_print("%02x ", pdu[i]);
97
98         g_print("\n");
99
100         if (pdu[0] == ATT_OP_HANDLE_NOTIFY)
101                 return;
102
103         olen = enc_confirmation(opdu, sizeof(opdu));
104
105         if (olen > 0)
106                 g_attrib_send(attrib, 0, opdu[0], opdu, olen, NULL, NULL, NULL);
107 }
108
109 static gboolean listen_start(gpointer user_data)
110 {
111         GAttrib *attrib = user_data;
112
113         g_attrib_register(attrib, ATT_OP_HANDLE_NOTIFY, events_handler,
114                                                         attrib, NULL);
115         g_attrib_register(attrib, ATT_OP_HANDLE_IND, events_handler,
116                                                         attrib, NULL);
117
118         return FALSE;
119 }
120
121 static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
122 {
123         GAttrib *attrib;
124
125         if (err) {
126                 g_printerr("%s\n", err->message);
127                 got_error = TRUE;
128                 g_main_loop_quit(event_loop);
129         }
130
131         attrib = g_attrib_new(io);
132
133         if (opt_listen)
134                 g_idle_add(listen_start, attrib);
135
136         operation(attrib);
137 }
138
139 static void primary_all_cb(GSList *services, guint8 status, gpointer user_data)
140 {
141         GSList *l;
142
143         if (status) {
144                 g_printerr("Discover all primary services failed: %s\n",
145                                                         att_ecode2str(status));
146                 goto done;
147         }
148
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);
153         }
154
155 done:
156         g_main_loop_quit(event_loop);
157 }
158
159 static void primary_by_uuid_cb(GSList *ranges, guint8 status,
160                                                         gpointer user_data)
161 {
162         GSList *l;
163
164         if (status != 0) {
165                 g_printerr("Discover primary services by UUID failed: %s\n",
166                                                         att_ecode2str(status));
167                 goto done;
168         }
169
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);
174         }
175
176 done:
177         g_main_loop_quit(event_loop);
178 }
179
180 static gboolean primary(gpointer user_data)
181 {
182         GAttrib *attrib = user_data;
183
184         if (opt_uuid)
185                 gatt_discover_primary(attrib, opt_uuid, primary_by_uuid_cb,
186                                                                         NULL);
187         else
188                 gatt_discover_primary(attrib, NULL, primary_all_cb, NULL);
189
190         return FALSE;
191 }
192
193 static void char_discovered_cb(GSList *characteristics, guint8 status,
194                                                         gpointer user_data)
195 {
196         GSList *l;
197
198         if (status) {
199                 g_printerr("Discover all characteristics failed: %s\n",
200                                                         att_ecode2str(status));
201                 goto done;
202         }
203
204         for (l = characteristics; l; l = l->next) {
205                 struct gatt_char *chars = l->data;
206
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);
210         }
211
212 done:
213         g_main_loop_quit(event_loop);
214 }
215
216 static gboolean characteristics(gpointer user_data)
217 {
218         GAttrib *attrib = user_data;
219
220         gatt_discover_char(attrib, opt_start, opt_end, opt_uuid,
221                                                 char_discovered_cb, NULL);
222
223         return FALSE;
224 }
225
226 static void char_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
227                                                         gpointer user_data)
228 {
229         uint8_t value[ATT_MAX_MTU];
230         int i, vlen;
231
232         if (status != 0) {
233                 g_printerr("Characteristic value/descriptor read failed: %s\n",
234                                                         att_ecode2str(status));
235                 goto done;
236         }
237         if (!dec_read_resp(pdu, plen, value, &vlen)) {
238                 g_printerr("Protocol error\n");
239                 goto done;
240         }
241         g_print("Characteristic value/descriptor: ");
242         for (i = 0; i < vlen; i++)
243                 g_print("%02x ", value[i]);
244         g_print("\n");
245
246 done:
247         if (opt_listen == FALSE)
248                 g_main_loop_quit(event_loop);
249 }
250
251 static void char_read_by_uuid_cb(guint8 status, const guint8 *pdu,
252                                         guint16 plen, gpointer user_data)
253 {
254         struct characteristic_data *char_data = user_data;
255         struct att_data_list *list;
256         int i;
257
258         if (status == ATT_ECODE_ATTR_NOT_FOUND &&
259                                         char_data->start != opt_start)
260                 goto done;
261
262         if (status != 0) {
263                 g_printerr("Read characteristics by UUID failed: %s\n",
264                                                         att_ecode2str(status));
265                 goto done;
266         }
267
268         list = dec_read_by_type_resp(pdu, plen);
269         if (list == NULL)
270                 goto done;
271
272         for (i = 0; i < list->num; i++) {
273                 uint8_t *value = list->data[i];
274                 int j;
275
276                 char_data->start = att_get_u16(value) + 1;
277
278                 g_print("handle: 0x%04x \t value: ", att_get_u16(value));
279                 value += 2;
280                 for (j = 0; j < list->len - 2; j++, value++)
281                         g_print("%02x ", *value);
282                 g_print("\n");
283         }
284
285         att_data_list_free(list);
286
287 done:
288         g_free(char_data);
289         g_main_loop_quit(event_loop);
290 }
291
292 static gboolean characteristics_read(gpointer user_data)
293 {
294         GAttrib *attrib = user_data;
295
296         if (opt_uuid != NULL) {
297                 struct characteristic_data *char_data;
298
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;
303
304                 gatt_read_char_by_uuid(attrib, opt_start, opt_end, opt_uuid,
305                                                 char_read_by_uuid_cb, char_data);
306
307                 return FALSE;
308         }
309
310         if (opt_handle <= 0) {
311                 g_printerr("A valid handle is required\n");
312                 g_main_loop_quit(event_loop);
313                 return FALSE;
314         }
315
316         gatt_read_char(attrib, opt_handle, opt_offset, char_read_cb, attrib);
317
318         return FALSE;
319 }
320
321 static void mainloop_quit(gpointer user_data)
322 {
323         uint8_t *value = user_data;
324
325         g_free(value);
326         g_main_loop_quit(event_loop);
327 }
328
329 static gboolean characteristics_write(gpointer user_data)
330 {
331         GAttrib *attrib = user_data;
332         uint8_t *value;
333         size_t len;
334
335         if (opt_handle <= 0) {
336                 g_printerr("A valid handle is required\n");
337                 goto error;
338         }
339
340         if (opt_value == NULL || opt_value[0] == '\0') {
341                 g_printerr("A value is required\n");
342                 goto error;
343         }
344
345         len = gatt_attr_data_from_string(opt_value, &value);
346         if (len == 0) {
347                 g_printerr("Invalid value\n");
348                 goto error;
349         }
350
351         gatt_write_cmd(attrib, opt_handle, value, len, mainloop_quit, value);
352
353         return FALSE;
354
355 error:
356         g_main_loop_quit(event_loop);
357         return FALSE;
358 }
359
360 static void char_write_req_cb(guint8 status, const guint8 *pdu, guint16 plen,
361                                                         gpointer user_data)
362 {
363         if (status != 0) {
364                 g_printerr("Characteristic Write Request failed: "
365                                                 "%s\n", att_ecode2str(status));
366                 goto done;
367         }
368
369         if (!dec_write_resp(pdu, plen)) {
370                 g_printerr("Protocol error\n");
371                 goto done;
372         }
373
374         g_print("Characteristic value was written successfully\n");
375
376 done:
377         if (opt_listen == FALSE)
378                 g_main_loop_quit(event_loop);
379 }
380
381 static gboolean characteristics_write_req(gpointer user_data)
382 {
383         GAttrib *attrib = user_data;
384         uint8_t *value;
385         size_t len;
386
387         if (opt_handle <= 0) {
388                 g_printerr("A valid handle is required\n");
389                 goto error;
390         }
391
392         if (opt_value == NULL || opt_value[0] == '\0') {
393                 g_printerr("A value is required\n");
394                 goto error;
395         }
396
397         len = gatt_attr_data_from_string(opt_value, &value);
398         if (len == 0) {
399                 g_printerr("Invalid value\n");
400                 goto error;
401         }
402
403         gatt_write_char(attrib, opt_handle, value, len, char_write_req_cb,
404                                                                         NULL);
405
406         return FALSE;
407
408 error:
409         g_main_loop_quit(event_loop);
410         return FALSE;
411 }
412
413 static void char_desc_cb(guint8 status, const guint8 *pdu, guint16 plen,
414                                                         gpointer user_data)
415 {
416         struct att_data_list *list;
417         guint8 format;
418         int i;
419
420         if (status != 0) {
421                 g_printerr("Discover all characteristic descriptors failed: "
422                                                 "%s\n", att_ecode2str(status));
423                 goto done;
424         }
425
426         list = dec_find_info_resp(pdu, plen, &format);
427         if (list == NULL)
428                 goto done;
429
430         for (i = 0; i < list->num; i++) {
431                 char uuidstr[MAX_LEN_UUID_STR];
432                 uint16_t handle;
433                 uint8_t *value;
434                 bt_uuid_t uuid;
435
436                 value = list->data[i];
437                 handle = att_get_u16(value);
438
439                 if (format == 0x01)
440                         uuid = att_get_uuid16(&value[2]);
441                 else
442                         uuid = att_get_uuid128(&value[2]);
443
444                 bt_uuid_to_string(&uuid, uuidstr, MAX_LEN_UUID_STR);
445                 g_print("handle = 0x%04x, uuid = %s\n", handle, uuidstr);
446         }
447
448         att_data_list_free(list);
449
450 done:
451         if (opt_listen == FALSE)
452                 g_main_loop_quit(event_loop);
453 }
454
455 static gboolean characteristics_desc(gpointer user_data)
456 {
457         GAttrib *attrib = user_data;
458
459         gatt_find_info(attrib, opt_start, opt_end, char_desc_cb, NULL);
460
461         return FALSE;
462 }
463
464 static gboolean parse_uuid(const char *key, const char *value,
465                                 gpointer user_data, GError **error)
466 {
467         if (!value)
468                 return FALSE;
469
470         opt_uuid = g_try_malloc(sizeof(bt_uuid_t));
471         if (opt_uuid == NULL)
472                 return FALSE;
473
474         if (bt_string_to_uuid(opt_uuid, value) < 0)
475                 return FALSE;
476
477         return TRUE;
478 }
479
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"},
487         { NULL },
488 };
489
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)",
495                 "0x0001" },
496         { "offset", 'o', 0, G_OPTION_ARG_INT, &opt_offset,
497                 "Offset to long read characteristic by handle", "N"},
498         {NULL},
499 };
500
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)",
510                 NULL },
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 },
519         { NULL },
520 };
521
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]"},
535         { NULL },
536 };
537
538 int main(int argc, char *argv[])
539 {
540         GOptionContext *context;
541         GOptionGroup *gatt_group, *params_group, *char_rw_group;
542         GError *gerr = NULL;
543         GIOChannel *chan;
544
545         opt_dst_type = g_strdup("public");
546         opt_sec_level = g_strdup("low");
547
548         context = g_option_context_new(NULL);
549         g_option_context_add_main_entries(context, options, NULL);
550
551         /* GATT commands */
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);
556
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",
561                         NULL, NULL);
562         g_option_context_add_group(context, params_group);
563         g_option_group_add_entries(params_group, primary_char_options);
564
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 "
569                 "arguments",
570                 NULL, NULL);
571         g_option_context_add_group(context, char_rw_group);
572         g_option_group_add_entries(char_rw_group, char_rw_options);
573
574         if (g_option_context_parse(context, &argc, &argv, &gerr) == FALSE) {
575                 g_printerr("%s\n", gerr->message);
576                 g_error_free(gerr);
577         }
578
579         if (opt_interactive) {
580                 interactive(opt_src, opt_dst, opt_dst_type, opt_psm);
581                 goto done;
582         }
583
584         if (opt_primary)
585                 operation = primary;
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;
596         else {
597                 gchar *help = g_option_context_get_help(context, TRUE, NULL);
598                 g_print("%s\n", help);
599                 g_free(help);
600                 got_error = TRUE;
601                 goto done;
602         }
603
604         chan = gatt_connect(opt_src, opt_dst, opt_dst_type, opt_sec_level,
605                                         opt_psm, opt_mtu, connect_cb);
606         if (chan == NULL) {
607                 got_error = TRUE;
608                 goto done;
609         }
610
611         event_loop = g_main_loop_new(NULL, FALSE);
612
613         g_main_loop_run(event_loop);
614
615         g_main_loop_unref(event_loop);
616
617 done:
618         g_option_context_free(context);
619         g_free(opt_src);
620         g_free(opt_dst);
621         g_free(opt_uuid);
622         g_free(opt_sec_level);
623
624         if (got_error)
625                 exit(EXIT_FAILURE);
626         else
627                 exit(EXIT_SUCCESS);
628 }