~ubuntu-branches/ubuntu/wily/bluez/wily

« back to all changes in this revision

Viewing changes to attrib/gatttool.c

ImportĀ upstreamĀ versionĀ 4.81

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/sdp.h>
 
38
#include <bluetooth/sdp_lib.h>
 
39
 
 
40
#include "att.h"
 
41
#include "btio.h"
 
42
#include "gattrib.h"
 
43
#include "glib-helper.h"
 
44
#include "gatt.h"
 
45
 
 
46
/* Minimum MTU for L2CAP connections over BR/EDR */
 
47
#define ATT_MIN_MTU_L2CAP 48
 
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;
 
68
 
 
69
struct characteristic_data {
 
70
        GAttrib *attrib;
 
71
        uint16_t start;
 
72
        uint16_t end;
 
73
};
 
74
 
 
75
static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
 
76
{
 
77
        if (err) {
 
78
                g_printerr("%s\n", err->message);
 
79
                got_error = TRUE;
 
80
                g_main_loop_quit(event_loop);
 
81
        }
 
82
}
 
83
 
 
84
static GIOChannel *do_connect(gboolean le)
 
85
{
 
86
        GIOChannel *chan;
 
87
        bdaddr_t sba, dba;
 
88
        GError *err = NULL;
 
89
        BtIOSecLevel sec_level;
 
90
 
 
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",
 
95
                                                        ATT_MIN_MTU_L2CAP);
 
96
                return NULL;
 
97
        }
 
98
 
 
99
        /* Remote device */
 
100
        if (opt_dst == NULL) {
 
101
                g_printerr("Remote Bluetooth address required\n");
 
102
                return NULL;
 
103
        }
 
104
        str2ba(opt_dst, &dba);
 
105
 
 
106
        /* Local adapter */
 
107
        if (opt_src != NULL) {
 
108
                if (!strncmp(opt_src, "hci", 3))
 
109
                        hci_devba(atoi(opt_src + 3), &sba);
 
110
                else
 
111
                        str2ba(opt_src, &sba);
 
112
        } else
 
113
                bacpy(&sba, BDADDR_ANY);
 
114
 
 
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;
 
119
        else
 
120
                sec_level = BT_IO_SEC_LOW;
 
121
 
 
122
        if (le)
 
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,
 
129
                                BT_IO_OPT_INVALID);
 
130
        else
 
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,
 
137
                                BT_IO_OPT_INVALID);
 
138
 
 
139
        if (err) {
 
140
                g_printerr("%s\n", err->message);
 
141
                g_error_free(err);
 
142
                return NULL;
 
143
        }
 
144
 
 
145
        return chan;
 
146
}
 
147
 
 
148
static void primary_all_cb(guint8 status, const guint8 *pdu, guint16 plen,
 
149
                                                        gpointer user_data)
 
150
{
 
151
        GAttrib *attrib = user_data;
 
152
        struct att_data_list *list;
 
153
        unsigned int i;
 
154
        uint16_t end;
 
155
 
 
156
        if (status == ATT_ECODE_ATTR_NOT_FOUND)
 
157
                goto done;
 
158
 
 
159
        if (status != 0) {
 
160
                g_printerr("Discover all primary services failed: %s\n",
 
161
                                                        att_ecode2str(status));
 
162
                goto done;
 
163
        }
 
164
 
 
165
        list = dec_read_by_grp_resp(pdu, plen);
 
166
        if (list == NULL)
 
167
                goto done;
 
168
 
 
169
        for (i = 0, end = 0; i < list->num; i++) {
 
170
                char uuidstr[MAX_LEN_UUID_STR];
 
171
                uint8_t *value = list->data[i];
 
172
                uint8_t length;
 
173
                uint16_t start;
 
174
                uuid_t uuid;
 
175
 
 
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]);
 
181
 
 
182
                g_print("attr handle = 0x%04x, end grp handle = 0x%04x, ",
 
183
                                                                start, end);
 
184
                if (length == 2)
 
185
                        sdp_uuid16_create(&uuid, att_get_u16(&value[4]));
 
186
                else
 
187
                        sdp_uuid128_create(&uuid, value + 4);
 
188
 
 
189
                sdp_uuid2strn(&uuid, uuidstr, MAX_LEN_UUID_STR);
 
190
                g_print("attr value (UUID) = %s\n", uuidstr);
 
191
        }
 
192
 
 
193
        att_data_list_free(list);
 
194
 
 
195
        /* Don't go beyond the maximum handle value */
 
196
        if (end == 0xffff)
 
197
                goto done;
 
198
 
 
199
        /*
 
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.
 
203
         */
 
204
 
 
205
        gatt_discover_primary(attrib, end + 1, opt_end, NULL, primary_all_cb,
 
206
                                                                attrib);
 
207
        return;
 
208
 
 
209
done:
 
210
        if (opt_listen == FALSE)
 
211
                g_main_loop_quit(event_loop);
 
212
}
 
213
 
 
214
static void primary_by_uuid_cb(guint8 status, const guint8 *pdu, guint16 plen,
 
215
                                                        gpointer user_data)
 
216
{
 
217
        GSList *ranges, *l;
 
218
 
 
219
        if (status != 0) {
 
220
                g_printerr("Discover primary services by UUID failed: %s\n",
 
221
                                                        att_ecode2str(status));
 
222
                goto done;
 
223
        }
 
224
 
 
225
        ranges = dec_find_by_type_resp(pdu, plen);
 
226
        if (ranges == NULL) {
 
227
                g_printerr("Protocol error!\n");
 
228
                goto done;
 
229
        }
 
230
 
 
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);
 
235
        }
 
236
 
 
237
        g_slist_foreach(ranges, (GFunc) g_free, NULL);
 
238
        g_slist_free(ranges);
 
239
 
 
240
done:
 
241
        g_main_loop_quit(event_loop);
 
242
}
 
243
 
 
244
static void events_handler(const uint8_t *pdu, uint16_t len, gpointer user_data)
 
245
{
 
246
        GAttrib *attrib = user_data;
 
247
        uint8_t opdu[ATT_MAX_MTU];
 
248
        uint16_t handle, i, olen = 0;
 
249
 
 
250
        handle = att_get_u16(&pdu[1]);
 
251
 
 
252
        switch (pdu[0]) {
 
253
        case ATT_OP_HANDLE_NOTIFY:
 
254
                g_print("Notification handle = 0x%04x value: ", handle);
 
255
                break;
 
256
        case ATT_OP_HANDLE_IND:
 
257
                g_print("Indication   handle = 0x%04x value: ", handle);
 
258
                break;
 
259
        default:
 
260
                g_print("Invalid opcode\n");
 
261
                return;
 
262
        }
 
263
 
 
264
        for (i = 3; i < len; i++)
 
265
                g_print("%02x ", pdu[i]);
 
266
 
 
267
        g_print("\n");
 
268
 
 
269
        if (pdu[0] == ATT_OP_HANDLE_NOTIFY)
 
270
                return;
 
271
 
 
272
        olen = enc_confirmation(opdu, sizeof(opdu));
 
273
 
 
274
        if (olen > 0)
 
275
                g_attrib_send(attrib, opdu[0], opdu, olen, NULL, NULL, NULL);
 
276
}
 
277
 
 
278
static gboolean listen_start(gpointer user_data)
 
279
{
 
280
        GAttrib *attrib = user_data;
 
281
 
 
282
        g_attrib_register(attrib, ATT_OP_HANDLE_NOTIFY, events_handler,
 
283
                                                        attrib, NULL);
 
284
        g_attrib_register(attrib, ATT_OP_HANDLE_IND, events_handler,
 
285
                                                        attrib, NULL);
 
286
 
 
287
        return FALSE;
 
288
}
 
289
 
 
290
static gboolean primary(gpointer user_data)
 
291
{
 
292
        GAttrib *attrib = user_data;
 
293
 
 
294
        if (opt_uuid)
 
295
                gatt_discover_primary(attrib, opt_start, opt_end, opt_uuid,
 
296
                                                primary_by_uuid_cb, attrib);
 
297
        else
 
298
                gatt_discover_primary(attrib, opt_start, opt_end, NULL,
 
299
                                                primary_all_cb, attrib);
 
300
 
 
301
        return FALSE;
 
302
}
 
303
 
 
304
static void char_discovered_cb(guint8 status, const guint8 *pdu, guint16 plen,
 
305
                                                        gpointer user_data)
 
306
{
 
307
        struct characteristic_data *char_data = user_data;
 
308
        struct att_data_list *list;
 
309
        uint16_t last = char_data->start;
 
310
        int i;
 
311
 
 
312
        if (status == ATT_ECODE_ATTR_NOT_FOUND)
 
313
                goto done;
 
314
 
 
315
        if (status != 0) {
 
316
                g_printerr("Discover all characteristics failed: %s\n",
 
317
                                                        att_ecode2str(status));
 
318
                goto done;
 
319
        }
 
320
 
 
321
        list = dec_read_by_type_resp(pdu, plen);
 
322
        if (list == NULL)
 
323
                return;
 
324
 
 
325
        for (i = 0; i < list->num; i++) {
 
326
                uint8_t *value = list->data[i];
 
327
                char uuidstr[MAX_LEN_UUID_STR];
 
328
                uuid_t uuid;
 
329
 
 
330
                last = att_get_u16(value);
 
331
 
 
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]));
 
335
 
 
336
                if (list->len == 7)
 
337
                        sdp_uuid16_create(&uuid, att_get_u16(&value[5]));
 
338
                else
 
339
                        sdp_uuid128_create(&uuid, value + 5);
 
340
 
 
341
                sdp_uuid2strn(&uuid, uuidstr, MAX_LEN_UUID_STR);
 
342
                g_print("uuid = %s\n", uuidstr);
 
343
        }
 
344
 
 
345
        att_data_list_free(list);
 
346
 
 
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);
 
350
 
 
351
        return;
 
352
 
 
353
done:
 
354
        g_free(char_data);
 
355
        if (opt_listen == FALSE)
 
356
                g_main_loop_quit(event_loop);
 
357
}
 
358
 
 
359
static gboolean characteristics(gpointer user_data)
 
360
{
 
361
        GAttrib *attrib = user_data;
 
362
        struct characteristic_data *char_data;
 
363
 
 
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;
 
368
 
 
369
        gatt_discover_char(attrib, opt_start, opt_end, char_discovered_cb,
 
370
                                                                char_data);
 
371
 
 
372
        return FALSE;
 
373
}
 
374
 
 
375
static void char_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
 
376
                                                        gpointer user_data)
 
377
{
 
378
        uint8_t value[ATT_MAX_MTU];
 
379
        int i, vlen;
 
380
 
 
381
        if (status != 0) {
 
382
                g_printerr("Characteristic value/descriptor read failed: %s\n",
 
383
                                                        att_ecode2str(status));
 
384
                goto done;
 
385
        }
 
386
        if (!dec_read_resp(pdu, plen, value, &vlen)) {
 
387
                g_printerr("Protocol error\n");
 
388
                goto done;
 
389
        }
 
390
        g_print("Characteristic value/descriptor: ");
 
391
        for (i = 0; i < vlen; i++)
 
392
                g_print("%02x ", value[i]);
 
393
        g_print("\n");
 
394
 
 
395
done:
 
396
        if (opt_listen == FALSE)
 
397
                g_main_loop_quit(event_loop);
 
398
}
 
399
 
 
400
static void char_read_by_uuid_cb(guint8 status, const guint8 *pdu,
 
401
                                        guint16 plen, gpointer user_data)
 
402
{
 
403
        struct characteristic_data *char_data = user_data;
 
404
        struct att_data_list *list;
 
405
        int i;
 
406
 
 
407
        if (status == ATT_ECODE_ATTR_NOT_FOUND &&
 
408
                                        char_data->start != opt_start)
 
409
                goto done;
 
410
 
 
411
        if (status != 0) {
 
412
                g_printerr("Read characteristics by UUID failed: %s\n",
 
413
                                                        att_ecode2str(status));
 
414
                goto done;
 
415
        }
 
416
 
 
417
        list = dec_read_by_type_resp(pdu, plen);
 
418
        if (list == NULL)
 
419
                goto done;
 
420
 
 
421
        for (i = 0; i < list->num; i++) {
 
422
                uint8_t *value = list->data[i];
 
423
                int j;
 
424
 
 
425
                char_data->start = att_get_u16(value) + 1;
 
426
 
 
427
                g_print("handle: 0x%04x \t value: ", att_get_u16(value));
 
428
                value += 2;
 
429
                for (j = 0; j < list->len - 2; j++, value++)
 
430
                        g_print("%02x ", *value);
 
431
                g_print("\n");
 
432
        }
 
433
 
 
434
        att_data_list_free(list);
 
435
 
 
436
        gatt_read_char_by_uuid(char_data->attrib, char_data->start,
 
437
                                        char_data->end, opt_uuid,
 
438
                                        char_read_by_uuid_cb,
 
439
                                        char_data);
 
440
 
 
441
        return;
 
442
done:
 
443
        g_free(char_data);
 
444
        g_main_loop_quit(event_loop);
 
445
}
 
446
 
 
447
static gboolean characteristics_read(gpointer user_data)
 
448
{
 
449
        GAttrib *attrib = user_data;
 
450
 
 
451
        if (opt_uuid != NULL) {
 
452
                struct characteristic_data *char_data;
 
453
 
 
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;
 
458
 
 
459
                gatt_read_char_by_uuid(attrib, opt_start, opt_end, opt_uuid,
 
460
                                                char_read_by_uuid_cb, char_data);
 
461
 
 
462
                return FALSE;
 
463
        }
 
464
 
 
465
        if (opt_handle <= 0) {
 
466
                g_printerr("A valid handle is required\n");
 
467
                g_main_loop_quit(event_loop);
 
468
                return FALSE;
 
469
        }
 
470
 
 
471
        gatt_read_char(attrib, opt_handle, char_read_cb, attrib);
 
472
 
 
473
        return FALSE;
 
474
}
 
475
 
 
476
static size_t attr_data_from_string(const char *str, uint8_t **data)
 
477
{
 
478
        char tmp[3];
 
479
        size_t size, i;
 
480
 
 
481
        size = strlen(str) / 2;
 
482
        *data = g_try_malloc0(size);
 
483
        if (*data == NULL)
 
484
                return 0;
 
485
 
 
486
        tmp[2] = '\0';
 
487
        for (i = 0; i < size; i++) {
 
488
                memcpy(tmp, str + (i * 2), 2);
 
489
                (*data)[i] = (uint8_t) strtol(tmp, NULL, 16);
 
490
        }
 
491
 
 
492
        return size;
 
493
}
 
494
 
 
495
static void mainloop_quit(gpointer user_data)
 
496
{
 
497
        uint8_t *value = user_data;
 
498
 
 
499
        g_free(value);
 
500
        g_main_loop_quit(event_loop);
 
501
}
 
502
 
 
503
static gboolean characteristics_write(gpointer user_data)
 
504
{
 
505
        GAttrib *attrib = user_data;
 
506
        uint8_t *value;
 
507
        size_t len;
 
508
 
 
509
        if (opt_handle <= 0) {
 
510
                g_printerr("A valid handle is required\n");
 
511
                goto error;
 
512
        }
 
513
 
 
514
        if (opt_value == NULL || opt_value[0] == '\0') {
 
515
                g_printerr("A value is required\n");
 
516
                goto error;
 
517
        }
 
518
 
 
519
        len = attr_data_from_string(opt_value, &value);
 
520
        if (len == 0) {
 
521
                g_printerr("Invalid value\n");
 
522
                goto error;
 
523
        }
 
524
 
 
525
        gatt_write_cmd(attrib, opt_handle, value, len, mainloop_quit, value);
 
526
 
 
527
        return FALSE;
 
528
 
 
529
error:
 
530
        g_main_loop_quit(event_loop);
 
531
        return FALSE;
 
532
}
 
533
 
 
534
static void char_desc_cb(guint8 status, const guint8 *pdu, guint16 plen,
 
535
                                                        gpointer user_data)
 
536
{
 
537
        struct att_data_list *list;
 
538
        guint8 format;
 
539
        int i;
 
540
 
 
541
        if (status != 0) {
 
542
                g_printerr("Discover all characteristic descriptors failed: "
 
543
                                                "%s\n", att_ecode2str(status));
 
544
                goto done;
 
545
        }
 
546
 
 
547
        list = dec_find_info_resp(pdu, plen, &format);
 
548
        if (list == NULL)
 
549
                goto done;
 
550
 
 
551
        for (i = 0; i < list->num; i++) {
 
552
                char uuidstr[MAX_LEN_UUID_STR];
 
553
                uint16_t handle;
 
554
                uint8_t *value;
 
555
                uuid_t uuid;
 
556
 
 
557
                value = list->data[i];
 
558
                handle = att_get_u16(value);
 
559
 
 
560
                if (format == 0x01)
 
561
                        sdp_uuid16_create(&uuid, att_get_u16(&value[2]));
 
562
                else
 
563
                        sdp_uuid128_create(&uuid, &value[2]);
 
564
 
 
565
                sdp_uuid2strn(&uuid, uuidstr, MAX_LEN_UUID_STR);
 
566
                g_print("handle = 0x%04x, uuid = %s\n", handle, uuidstr);
 
567
        }
 
568
 
 
569
        att_data_list_free(list);
 
570
 
 
571
done:
 
572
        if (opt_listen == FALSE)
 
573
                g_main_loop_quit(event_loop);
 
574
}
 
575
 
 
576
static gboolean characteristics_desc(gpointer user_data)
 
577
{
 
578
        GAttrib *attrib = user_data;
 
579
 
 
580
        gatt_find_info(attrib, opt_start, opt_end, char_desc_cb, NULL);
 
581
 
 
582
        return FALSE;
 
583
}
 
584
 
 
585
static gboolean parse_uuid(const char *key, const char *value,
 
586
                                gpointer user_data, GError **error)
 
587
{
 
588
        if (!value)
 
589
                return FALSE;
 
590
 
 
591
        opt_uuid = g_try_malloc(sizeof(uuid_t));
 
592
        if (opt_uuid == NULL)
 
593
                return FALSE;
 
594
 
 
595
        if (bt_string2uuid(opt_uuid, value) < 0)
 
596
                return FALSE;
 
597
 
 
598
        return TRUE;
 
599
}
 
600
 
 
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"},
 
608
        { NULL },
 
609
};
 
610
 
 
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)",
 
616
                "0x0001" },
 
617
        {NULL},
 
618
};
 
619
 
 
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 },
 
635
        { NULL },
 
636
};
 
637
 
 
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]"},
 
649
        { NULL },
 
650
};
 
651
 
 
652
int main(int argc, char *argv[])
 
653
{
 
654
        GOptionContext *context;
 
655
        GOptionGroup *gatt_group, *params_group, *char_rw_group;
 
656
        GError *gerr = NULL;
 
657
        GAttrib *attrib;
 
658
        GIOChannel *chan;
 
659
        GSourceFunc callback;
 
660
 
 
661
        context = g_option_context_new(NULL);
 
662
        g_option_context_add_main_entries(context, options, NULL);
 
663
 
 
664
        /* GATT commands */
 
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);
 
669
 
 
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",
 
674
                        NULL, NULL);
 
675
        g_option_context_add_group(context, params_group);
 
676
        g_option_group_add_entries(params_group, primary_char_options);
 
677
 
 
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 "
 
682
                "arguments",
 
683
                NULL, NULL);
 
684
        g_option_context_add_group(context, char_rw_group);
 
685
        g_option_group_add_entries(char_rw_group, char_rw_options);
 
686
 
 
687
        if (g_option_context_parse(context, &argc, &argv, &gerr) == FALSE) {
 
688
                g_printerr("%s\n", gerr->message);
 
689
                g_error_free(gerr);
 
690
        }
 
691
 
 
692
        if (opt_primary)
 
693
                callback = primary;
 
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;
 
702
        else {
 
703
                gchar *help = g_option_context_get_help(context, TRUE, NULL);
 
704
                g_print("%s\n", help);
 
705
                g_free(help);
 
706
                got_error = TRUE;
 
707
                goto done;
 
708
        }
 
709
 
 
710
        chan = do_connect(opt_le);
 
711
        if (chan == NULL) {
 
712
                got_error = TRUE;
 
713
                goto done;
 
714
        }
 
715
 
 
716
        attrib = g_attrib_new(chan);
 
717
 
 
718
        event_loop = g_main_loop_new(NULL, FALSE);
 
719
 
 
720
        if (opt_listen)
 
721
                g_idle_add(listen_start, attrib);
 
722
 
 
723
        g_idle_add(callback, attrib);
 
724
 
 
725
        g_main_loop_run(event_loop);
 
726
 
 
727
        g_attrib_unregister_all(attrib);
 
728
 
 
729
        g_main_loop_unref(event_loop);
 
730
 
 
731
        g_io_channel_unref(chan);
 
732
        g_attrib_unref(attrib);
 
733
 
 
734
done:
 
735
        g_option_context_free(context);
 
736
        g_free(opt_src);
 
737
        g_free(opt_dst);
 
738
        g_free(opt_uuid);
 
739
 
 
740
        if (got_error)
 
741
                exit(EXIT_FAILURE);
 
742
        else
 
743
                exit(EXIT_SUCCESS);
 
744
}