~bluetooth/bluez/vivid-phone-overlay

« back to all changes in this revision

Viewing changes to attrib/interactive.c

  • Committer: Simon Fels
  • Date: 2015-09-11 09:01:46 UTC
  • Revision ID: morphis@gravedo.de-20150911090146-4c0ln9s7ec3xf0nx
Import package bluez_4.101-0ubuntu25.1~overlay4 from stable phone overlay PPA

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) 2011  Nokia Corporation
 
6
 *
 
7
 *
 
8
 *  This program is free software; you can redistribute it and/or modify
 
9
 *  it under the terms of the GNU General Public License as published by
 
10
 *  the Free Software Foundation; either version 2 of the License, or
 
11
 *  (at your option) any later version.
 
12
 *
 
13
 *  This program is distributed in the hope that it will be useful,
 
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 *  GNU General Public License for more details.
 
17
 *
 
18
 *  You should have received a copy of the GNU General Public License
 
19
 *  along with this program; if not, write to the Free Software
 
20
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
21
 *
 
22
 */
 
23
#include <string.h>
 
24
#include <stdlib.h>
 
25
#include <errno.h>
 
26
#include <stdio.h>
 
27
#include <glib.h>
 
28
 
 
29
#include <bluetooth/uuid.h>
 
30
 
 
31
#include <readline/readline.h>
 
32
#include <readline/history.h>
 
33
 
 
34
#include "att.h"
 
35
#include "btio.h"
 
36
#include "gattrib.h"
 
37
#include "gatt.h"
 
38
#include "gatttool.h"
 
39
 
 
40
static GIOChannel *iochannel = NULL;
 
41
static GAttrib *attrib = NULL;
 
42
static GMainLoop *event_loop;
 
43
static GString *prompt;
 
44
 
 
45
static gchar *opt_src = NULL;
 
46
static gchar *opt_dst = NULL;
 
47
static gchar *opt_dst_type = NULL;
 
48
static gchar *opt_sec_level = NULL;
 
49
static int opt_psm = 0;
 
50
static int opt_mtu = 0;
 
51
 
 
52
struct characteristic_data {
 
53
        uint16_t orig_start;
 
54
        uint16_t start;
 
55
        uint16_t end;
 
56
        bt_uuid_t uuid;
 
57
};
 
58
 
 
59
static void cmd_help(int argcp, char **argvp);
 
60
 
 
61
enum state {
 
62
        STATE_DISCONNECTED,
 
63
        STATE_CONNECTING,
 
64
        STATE_CONNECTED
 
65
} conn_state;
 
66
 
 
67
static char *get_prompt(void)
 
68
{
 
69
        if (conn_state == STATE_CONNECTING) {
 
70
                g_string_assign(prompt, "Connecting... ");
 
71
                return prompt->str;
 
72
        }
 
73
 
 
74
        if (conn_state == STATE_CONNECTED)
 
75
                g_string_assign(prompt, "[CON]");
 
76
        else
 
77
                g_string_assign(prompt, "[   ]");
 
78
 
 
79
        if (opt_dst)
 
80
                g_string_append_printf(prompt, "[%17s]", opt_dst);
 
81
        else
 
82
                g_string_append_printf(prompt, "[%17s]", "");
 
83
 
 
84
        if (opt_psm)
 
85
                g_string_append(prompt, "[BR]");
 
86
        else
 
87
                g_string_append(prompt, "[LE]");
 
88
 
 
89
        g_string_append(prompt, "> ");
 
90
 
 
91
        return prompt->str;
 
92
}
 
93
 
 
94
 
 
95
static void set_state(enum state st)
 
96
{
 
97
        conn_state = st;
 
98
        rl_set_prompt(get_prompt());
 
99
        rl_redisplay();
 
100
}
 
101
 
 
102
static void events_handler(const uint8_t *pdu, uint16_t len, gpointer user_data)
 
103
{
 
104
        uint8_t opdu[ATT_MAX_MTU];
 
105
        uint16_t handle, i, olen;
 
106
 
 
107
        handle = att_get_u16(&pdu[1]);
 
108
 
 
109
        printf("\n");
 
110
        switch (pdu[0]) {
 
111
        case ATT_OP_HANDLE_NOTIFY:
 
112
                printf("Notification handle = 0x%04x value: ", handle);
 
113
                break;
 
114
        case ATT_OP_HANDLE_IND:
 
115
                printf("Indication   handle = 0x%04x value: ", handle);
 
116
                break;
 
117
        default:
 
118
                printf("Invalid opcode\n");
 
119
                return;
 
120
        }
 
121
 
 
122
        for (i = 3; i < len; i++)
 
123
                printf("%02x ", pdu[i]);
 
124
 
 
125
        printf("\n");
 
126
        rl_forced_update_display();
 
127
 
 
128
        if (pdu[0] == ATT_OP_HANDLE_NOTIFY)
 
129
                return;
 
130
 
 
131
        olen = enc_confirmation(opdu, sizeof(opdu));
 
132
 
 
133
        if (olen > 0)
 
134
                g_attrib_send(attrib, 0, opdu[0], opdu, olen, NULL, NULL, NULL);
 
135
}
 
136
 
 
137
static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
 
138
{
 
139
        if (err) {
 
140
                printf("connect error: %s\n", err->message);
 
141
                set_state(STATE_DISCONNECTED);
 
142
                return;
 
143
        }
 
144
 
 
145
        attrib = g_attrib_new(iochannel);
 
146
        g_attrib_register(attrib, ATT_OP_HANDLE_NOTIFY, events_handler,
 
147
                                                        attrib, NULL);
 
148
        g_attrib_register(attrib, ATT_OP_HANDLE_IND, events_handler,
 
149
                                                        attrib, NULL);
 
150
        set_state(STATE_CONNECTED);
 
151
}
 
152
 
 
153
static void disconnect_io()
 
154
{
 
155
        if (conn_state == STATE_DISCONNECTED)
 
156
                return;
 
157
 
 
158
        g_attrib_unref(attrib);
 
159
        attrib = NULL;
 
160
        opt_mtu = 0;
 
161
 
 
162
        g_io_channel_shutdown(iochannel, FALSE, NULL);
 
163
        g_io_channel_unref(iochannel);
 
164
        iochannel = NULL;
 
165
 
 
166
        set_state(STATE_DISCONNECTED);
 
167
}
 
168
 
 
169
static void primary_all_cb(GSList *services, guint8 status, gpointer user_data)
 
170
{
 
171
        GSList *l;
 
172
 
 
173
        if (status) {
 
174
                printf("Discover all primary services failed: %s\n",
 
175
                                                        att_ecode2str(status));
 
176
                return;
 
177
        }
 
178
 
 
179
        printf("\n");
 
180
        for (l = services; l; l = l->next) {
 
181
                struct gatt_primary *prim = l->data;
 
182
                printf("attr handle: 0x%04x, end grp handle: 0x%04x "
 
183
                        "uuid: %s\n", prim->range.start, prim->range.end, prim->uuid);
 
184
        }
 
185
 
 
186
        rl_forced_update_display();
 
187
}
 
188
 
 
189
static void primary_by_uuid_cb(GSList *ranges, guint8 status,
 
190
                                                        gpointer user_data)
 
191
{
 
192
        GSList *l;
 
193
 
 
194
        if (status) {
 
195
                printf("Discover primary services by UUID failed: %s\n",
 
196
                                                        att_ecode2str(status));
 
197
                return;
 
198
        }
 
199
 
 
200
        printf("\n");
 
201
        for (l = ranges; l; l = l->next) {
 
202
                struct att_range *range = l->data;
 
203
                g_print("Starting handle: 0x%04x Ending handle: 0x%04x\n",
 
204
                                                range->start, range->end);
 
205
        }
 
206
 
 
207
        rl_forced_update_display();
 
208
}
 
209
 
 
210
static void char_cb(GSList *characteristics, guint8 status, gpointer user_data)
 
211
{
 
212
        GSList *l;
 
213
 
 
214
        if (status) {
 
215
                printf("Discover all characteristics failed: %s\n",
 
216
                                                        att_ecode2str(status));
 
217
                return;
 
218
        }
 
219
 
 
220
        printf("\n");
 
221
        for (l = characteristics; l; l = l->next) {
 
222
                struct gatt_char *chars = l->data;
 
223
 
 
224
                printf("handle: 0x%04x, char properties: 0x%02x, char value "
 
225
                                "handle: 0x%04x, uuid: %s\n", chars->handle,
 
226
                                chars->properties, chars->value_handle,
 
227
                                chars->uuid);
 
228
        }
 
229
 
 
230
        rl_forced_update_display();
 
231
}
 
232
 
 
233
static void char_desc_cb(guint8 status, const guint8 *pdu, guint16 plen,
 
234
                                                        gpointer user_data)
 
235
{
 
236
        struct att_data_list *list;
 
237
        guint8 format;
 
238
        int i;
 
239
 
 
240
        if (status != 0) {
 
241
                printf("Discover all characteristic descriptors failed: "
 
242
                                                "%s\n", att_ecode2str(status));
 
243
                return;
 
244
        }
 
245
 
 
246
        list = dec_find_info_resp(pdu, plen, &format);
 
247
        if (list == NULL)
 
248
                return;
 
249
 
 
250
        printf("\n");
 
251
        for (i = 0; i < list->num; i++) {
 
252
                char uuidstr[MAX_LEN_UUID_STR];
 
253
                uint16_t handle;
 
254
                uint8_t *value;
 
255
                bt_uuid_t uuid;
 
256
 
 
257
                value = list->data[i];
 
258
                handle = att_get_u16(value);
 
259
 
 
260
                if (format == 0x01)
 
261
                        uuid = att_get_uuid16(&value[2]);
 
262
                else
 
263
                        uuid = att_get_uuid128(&value[2]);
 
264
 
 
265
                bt_uuid_to_string(&uuid, uuidstr, MAX_LEN_UUID_STR);
 
266
                printf("handle: 0x%04x, uuid: %s\n", handle, uuidstr);
 
267
        }
 
268
 
 
269
        att_data_list_free(list);
 
270
 
 
271
        rl_forced_update_display();
 
272
}
 
273
 
 
274
static void char_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
 
275
                                                        gpointer user_data)
 
276
{
 
277
        uint8_t value[ATT_MAX_MTU];
 
278
        int i, vlen;
 
279
 
 
280
        if (status != 0) {
 
281
                printf("Characteristic value/descriptor read failed: %s\n",
 
282
                                                        att_ecode2str(status));
 
283
                return;
 
284
        }
 
285
 
 
286
        if (!dec_read_resp(pdu, plen, value, &vlen)) {
 
287
                printf("Protocol error\n");
 
288
                return;
 
289
        }
 
290
 
 
291
        printf("\nCharacteristic value/descriptor: ");
 
292
        for (i = 0; i < vlen; i++)
 
293
                printf("%02x ", value[i]);
 
294
        printf("\n");
 
295
 
 
296
        rl_forced_update_display();
 
297
}
 
298
 
 
299
static void char_read_by_uuid_cb(guint8 status, const guint8 *pdu,
 
300
                                        guint16 plen, gpointer user_data)
 
301
{
 
302
        struct characteristic_data *char_data = user_data;
 
303
        struct att_data_list *list;
 
304
        int i;
 
305
 
 
306
        if (status == ATT_ECODE_ATTR_NOT_FOUND &&
 
307
                                char_data->start != char_data->orig_start)
 
308
                goto done;
 
309
 
 
310
        if (status != 0) {
 
311
                printf("Read characteristics by UUID failed: %s\n",
 
312
                                                        att_ecode2str(status));
 
313
                goto done;
 
314
        }
 
315
 
 
316
        list = dec_read_by_type_resp(pdu, plen);
 
317
        if (list == NULL)
 
318
                goto done;
 
319
 
 
320
        for (i = 0; i < list->num; i++) {
 
321
                uint8_t *value = list->data[i];
 
322
                int j;
 
323
 
 
324
                char_data->start = att_get_u16(value) + 1;
 
325
 
 
326
                printf("\nhandle: 0x%04x \t value: ", att_get_u16(value));
 
327
                value += 2;
 
328
                for (j = 0; j < list->len - 2; j++, value++)
 
329
                        printf("%02x ", *value);
 
330
                printf("\n");
 
331
        }
 
332
 
 
333
        att_data_list_free(list);
 
334
 
 
335
        rl_forced_update_display();
 
336
 
 
337
done:
 
338
        g_free(char_data);
 
339
}
 
340
 
 
341
static void cmd_exit(int argcp, char **argvp)
 
342
{
 
343
        rl_callback_handler_remove();
 
344
        g_main_loop_quit(event_loop);
 
345
}
 
346
 
 
347
static gboolean channel_watcher(GIOChannel *chan, GIOCondition cond,
 
348
                                gpointer user_data)
 
349
{
 
350
        disconnect_io();
 
351
 
 
352
        return FALSE;
 
353
}
 
354
 
 
355
static void cmd_connect(int argcp, char **argvp)
 
356
{
 
357
        if (conn_state != STATE_DISCONNECTED)
 
358
                return;
 
359
 
 
360
        if (argcp > 1) {
 
361
                g_free(opt_dst);
 
362
                opt_dst = g_strdup(argvp[1]);
 
363
 
 
364
                g_free(opt_dst_type);
 
365
                if (argcp > 2)
 
366
                        opt_dst_type = g_strdup(argvp[2]);
 
367
                else
 
368
                        opt_dst_type = g_strdup("public");
 
369
        }
 
370
 
 
371
        if (opt_dst == NULL) {
 
372
                printf("Remote Bluetooth address required\n");
 
373
                return;
 
374
        }
 
375
 
 
376
        set_state(STATE_CONNECTING);
 
377
        iochannel = gatt_connect(opt_src, opt_dst, opt_dst_type, opt_sec_level,
 
378
                                                opt_psm, opt_mtu, connect_cb);
 
379
        if (iochannel == NULL)
 
380
                set_state(STATE_DISCONNECTED);
 
381
        else
 
382
                g_io_add_watch(iochannel, G_IO_HUP, channel_watcher, NULL);
 
383
}
 
384
 
 
385
static void cmd_disconnect(int argcp, char **argvp)
 
386
{
 
387
        disconnect_io();
 
388
}
 
389
 
 
390
static void cmd_primary(int argcp, char **argvp)
 
391
{
 
392
        bt_uuid_t uuid;
 
393
 
 
394
        if (conn_state != STATE_CONNECTED) {
 
395
                printf("Command failed: disconnected\n");
 
396
                return;
 
397
        }
 
398
 
 
399
        if (argcp == 1) {
 
400
                gatt_discover_primary(attrib, NULL, primary_all_cb, NULL);
 
401
                return;
 
402
        }
 
403
 
 
404
        if (bt_string_to_uuid(&uuid, argvp[1]) < 0) {
 
405
                printf("Invalid UUID\n");
 
406
                return;
 
407
        }
 
408
 
 
409
        gatt_discover_primary(attrib, &uuid, primary_by_uuid_cb, NULL);
 
410
}
 
411
 
 
412
static int strtohandle(const char *src)
 
413
{
 
414
        char *e;
 
415
        int dst;
 
416
 
 
417
        errno = 0;
 
418
        dst = strtoll(src, &e, 16);
 
419
        if (errno != 0 || *e != '\0')
 
420
                return -EINVAL;
 
421
 
 
422
        return dst;
 
423
}
 
424
 
 
425
static void cmd_char(int argcp, char **argvp)
 
426
{
 
427
        int start = 0x0001;
 
428
        int end = 0xffff;
 
429
 
 
430
        if (conn_state != STATE_CONNECTED) {
 
431
                printf("Command failed: disconnected\n");
 
432
                return;
 
433
        }
 
434
 
 
435
        if (argcp > 1) {
 
436
                start = strtohandle(argvp[1]);
 
437
                if (start < 0) {
 
438
                        printf("Invalid start handle: %s\n", argvp[1]);
 
439
                        return;
 
440
                }
 
441
        }
 
442
 
 
443
        if (argcp > 2) {
 
444
                end = strtohandle(argvp[2]);
 
445
                if (end < 0) {
 
446
                        printf("Invalid end handle: %s\n", argvp[2]);
 
447
                        return;
 
448
                }
 
449
        }
 
450
 
 
451
        if (argcp > 3) {
 
452
                bt_uuid_t uuid;
 
453
 
 
454
                if (bt_string_to_uuid(&uuid, argvp[3]) < 0) {
 
455
                        printf("Invalid UUID\n");
 
456
                        return;
 
457
                }
 
458
 
 
459
                gatt_discover_char(attrib, start, end, &uuid, char_cb, NULL);
 
460
                return;
 
461
        }
 
462
 
 
463
        gatt_discover_char(attrib, start, end, NULL, char_cb, NULL);
 
464
}
 
465
 
 
466
static void cmd_char_desc(int argcp, char **argvp)
 
467
{
 
468
        int start = 0x0001;
 
469
        int end = 0xffff;
 
470
 
 
471
        if (conn_state != STATE_CONNECTED) {
 
472
                printf("Command failed: disconnected\n");
 
473
                return;
 
474
        }
 
475
 
 
476
        if (argcp > 1) {
 
477
                start = strtohandle(argvp[1]);
 
478
                if (start < 0) {
 
479
                        printf("Invalid start handle: %s\n", argvp[1]);
 
480
                        return;
 
481
                }
 
482
        }
 
483
 
 
484
        if (argcp > 2) {
 
485
                end = strtohandle(argvp[2]);
 
486
                if (end < 0) {
 
487
                        printf("Invalid end handle: %s\n", argvp[2]);
 
488
                        return;
 
489
                }
 
490
        }
 
491
 
 
492
        gatt_find_info(attrib, start, end, char_desc_cb, NULL);
 
493
}
 
494
 
 
495
static void cmd_read_hnd(int argcp, char **argvp)
 
496
{
 
497
        int handle;
 
498
        int offset = 0;
 
499
 
 
500
        if (conn_state != STATE_CONNECTED) {
 
501
                printf("Command failed: disconnected\n");
 
502
                return;
 
503
        }
 
504
 
 
505
        if (argcp < 2) {
 
506
                printf("Missing argument: handle\n");
 
507
                return;
 
508
        }
 
509
 
 
510
        handle = strtohandle(argvp[1]);
 
511
        if (handle < 0) {
 
512
                printf("Invalid handle: %s\n", argvp[1]);
 
513
                return;
 
514
        }
 
515
 
 
516
        if (argcp > 2) {
 
517
                char *e;
 
518
 
 
519
                errno = 0;
 
520
                offset = strtol(argvp[2], &e, 0);
 
521
                if (errno != 0 || *e != '\0') {
 
522
                        printf("Invalid offset: %s\n", argvp[2]);
 
523
                        return;
 
524
                }
 
525
        }
 
526
 
 
527
        gatt_read_char(attrib, handle, offset, char_read_cb, attrib);
 
528
}
 
529
 
 
530
static void cmd_read_uuid(int argcp, char **argvp)
 
531
{
 
532
        struct characteristic_data *char_data;
 
533
        int start = 0x0001;
 
534
        int end = 0xffff;
 
535
        bt_uuid_t uuid;
 
536
 
 
537
        if (conn_state != STATE_CONNECTED) {
 
538
                printf("Command failed: disconnected\n");
 
539
                return;
 
540
        }
 
541
 
 
542
        if (argcp < 2) {
 
543
                printf("Missing argument: UUID\n");
 
544
                return;
 
545
        }
 
546
 
 
547
        if (bt_string_to_uuid(&uuid, argvp[1]) < 0) {
 
548
                printf("Invalid UUID\n");
 
549
                return;
 
550
        }
 
551
 
 
552
        if (argcp > 2) {
 
553
                start = strtohandle(argvp[2]);
 
554
                if (start < 0) {
 
555
                        printf("Invalid start handle: %s\n", argvp[1]);
 
556
                        return;
 
557
                }
 
558
        }
 
559
 
 
560
        if (argcp > 3) {
 
561
                end = strtohandle(argvp[3]);
 
562
                if (end < 0) {
 
563
                        printf("Invalid end handle: %s\n", argvp[2]);
 
564
                        return;
 
565
                }
 
566
        }
 
567
 
 
568
        char_data = g_new(struct characteristic_data, 1);
 
569
        char_data->orig_start = start;
 
570
        char_data->start = start;
 
571
        char_data->end = end;
 
572
        char_data->uuid = uuid;
 
573
 
 
574
        gatt_read_char_by_uuid(attrib, start, end, &char_data->uuid,
 
575
                                        char_read_by_uuid_cb, char_data);
 
576
}
 
577
 
 
578
static void char_write_req_cb(guint8 status, const guint8 *pdu, guint16 plen,
 
579
                                                        gpointer user_data)
 
580
{
 
581
        if (status != 0) {
 
582
                printf("Characteristic Write Request failed: "
 
583
                                                "%s\n", att_ecode2str(status));
 
584
                return;
 
585
        }
 
586
 
 
587
        if (!dec_write_resp(pdu, plen)) {
 
588
                printf("Protocol error\n");
 
589
                return;
 
590
        }
 
591
 
 
592
        printf("Characteristic value was written successfully\n");
 
593
}
 
594
 
 
595
static void cmd_char_write(int argcp, char **argvp)
 
596
{
 
597
        uint8_t *value;
 
598
        size_t plen;
 
599
        int handle;
 
600
 
 
601
        if (conn_state != STATE_CONNECTED) {
 
602
                printf("Command failed: disconnected\n");
 
603
                return;
 
604
        }
 
605
 
 
606
        if (argcp < 3) {
 
607
                printf("Usage: %s <handle> <new value>\n", argvp[0]);
 
608
                return;
 
609
        }
 
610
 
 
611
        handle = strtohandle(argvp[1]);
 
612
        if (handle <= 0) {
 
613
                printf("A valid handle is required\n");
 
614
                return;
 
615
        }
 
616
 
 
617
        plen = gatt_attr_data_from_string(argvp[2], &value);
 
618
        if (plen == 0) {
 
619
                g_printerr("Invalid value\n");
 
620
                return;
 
621
        }
 
622
 
 
623
        if (g_strcmp0("char-write-req", argvp[0]) == 0)
 
624
                gatt_write_char(attrib, handle, value, plen,
 
625
                                        char_write_req_cb, NULL);
 
626
        else
 
627
                gatt_write_char(attrib, handle, value, plen, NULL, NULL);
 
628
 
 
629
        g_free(value);
 
630
}
 
631
 
 
632
static void cmd_sec_level(int argcp, char **argvp)
 
633
{
 
634
        GError *gerr = NULL;
 
635
        BtIOSecLevel sec_level;
 
636
 
 
637
        if (argcp < 2) {
 
638
                printf("sec-level: %s\n", opt_sec_level);
 
639
                return;
 
640
        }
 
641
 
 
642
        if (strcasecmp(argvp[1], "medium") == 0)
 
643
                sec_level = BT_IO_SEC_MEDIUM;
 
644
        else if (strcasecmp(argvp[1], "high") == 0)
 
645
                sec_level = BT_IO_SEC_HIGH;
 
646
        else if (strcasecmp(argvp[1], "low") == 0)
 
647
                sec_level = BT_IO_SEC_LOW;
 
648
        else {
 
649
                printf("Allowed values: low | medium | high\n");
 
650
                return;
 
651
        }
 
652
 
 
653
        g_free(opt_sec_level);
 
654
        opt_sec_level = g_strdup(argvp[1]);
 
655
 
 
656
        if (conn_state != STATE_CONNECTED)
 
657
                return;
 
658
 
 
659
        if (opt_psm) {
 
660
                printf("It must be reconnected to this change take effect\n");
 
661
                return;
 
662
        }
 
663
 
 
664
        bt_io_set(iochannel, BT_IO_L2CAP, &gerr,
 
665
                        BT_IO_OPT_SEC_LEVEL, sec_level,
 
666
                        BT_IO_OPT_INVALID);
 
667
 
 
668
        if (gerr) {
 
669
                printf("Error: %s\n", gerr->message);
 
670
                g_error_free(gerr);
 
671
        }
 
672
}
 
673
 
 
674
static void exchange_mtu_cb(guint8 status, const guint8 *pdu, guint16 plen,
 
675
                                                        gpointer user_data)
 
676
{
 
677
        uint16_t mtu;
 
678
 
 
679
        if (status != 0) {
 
680
                printf("Exchange MTU Request failed: %s\n",
 
681
                                                        att_ecode2str(status));
 
682
                return;
 
683
        }
 
684
 
 
685
        if (!dec_mtu_resp(pdu, plen, &mtu)) {
 
686
                printf("Protocol error\n");
 
687
                return;
 
688
        }
 
689
 
 
690
        mtu = MIN(mtu, opt_mtu);
 
691
        /* Set new value for MTU in client */
 
692
        if (g_attrib_set_mtu(attrib, mtu))
 
693
                printf("MTU was exchanged successfully: %d\n", mtu);
 
694
        else
 
695
                printf("Error exchanging MTU\n");
 
696
}
 
697
 
 
698
static void cmd_mtu(int argcp, char **argvp)
 
699
{
 
700
        if (conn_state != STATE_CONNECTED) {
 
701
                printf("Command failed: not connected.\n");
 
702
                return;
 
703
        }
 
704
 
 
705
        if (opt_psm) {
 
706
                printf("Command failed: operation is only available for LE"
 
707
                                                        " transport.\n");
 
708
                return;
 
709
        }
 
710
 
 
711
        if (argcp < 2) {
 
712
                printf("Usage: mtu <value>\n");
 
713
                return;
 
714
        }
 
715
 
 
716
        if (opt_mtu) {
 
717
                printf("Command failed: MTU exchange can only occur once per"
 
718
                                                        " connection.\n");
 
719
                return;
 
720
        }
 
721
 
 
722
        errno = 0;
 
723
        opt_mtu = strtoll(argvp[1], NULL, 0);
 
724
        if (errno != 0 || opt_mtu < ATT_DEFAULT_LE_MTU) {
 
725
                printf("Invalid value. Minimum MTU size is %d\n",
 
726
                                                        ATT_DEFAULT_LE_MTU);
 
727
                return;
 
728
        }
 
729
 
 
730
        gatt_exchange_mtu(attrib, opt_mtu, exchange_mtu_cb, NULL);
 
731
}
 
732
 
 
733
static struct {
 
734
        const char *cmd;
 
735
        void (*func)(int argcp, char **argvp);
 
736
        const char *params;
 
737
        const char *desc;
 
738
} commands[] = {
 
739
        { "help",               cmd_help,       "",
 
740
                "Show this help"},
 
741
        { "exit",               cmd_exit,       "",
 
742
                "Exit interactive mode" },
 
743
        { "quit",               cmd_exit,       "",
 
744
                "Exit interactive mode" },
 
745
        { "connect",            cmd_connect,    "[address [address type]]",
 
746
                "Connect to a remote device" },
 
747
        { "disconnect",         cmd_disconnect, "",
 
748
                "Disconnect from a remote device" },
 
749
        { "primary",            cmd_primary,    "[UUID]",
 
750
                "Primary Service Discovery" },
 
751
        { "characteristics",    cmd_char,       "[start hnd [end hnd [UUID]]]",
 
752
                "Characteristics Discovery" },
 
753
        { "char-desc",          cmd_char_desc,  "[start hnd] [end hnd]",
 
754
                "Characteristics Descriptor Discovery" },
 
755
        { "char-read-hnd",      cmd_read_hnd,   "<handle> [offset]",
 
756
                "Characteristics Value/Descriptor Read by handle" },
 
757
        { "char-read-uuid",     cmd_read_uuid,  "<UUID> [start hnd] [end hnd]",
 
758
                "Characteristics Value/Descriptor Read by UUID" },
 
759
        { "char-write-req",     cmd_char_write, "<handle> <new value>",
 
760
                "Characteristic Value Write (Write Request)" },
 
761
        { "char-write-cmd",     cmd_char_write, "<handle> <new value>",
 
762
                "Characteristic Value Write (No response)" },
 
763
        { "sec-level",          cmd_sec_level,  "[low | medium | high]",
 
764
                "Set security level. Default: low" },
 
765
        { "mtu",                cmd_mtu,        "<value>",
 
766
                "Exchange MTU for GATT/ATT" },
 
767
        { NULL, NULL, NULL}
 
768
};
 
769
 
 
770
static void cmd_help(int argcp, char **argvp)
 
771
{
 
772
        int i;
 
773
 
 
774
        for (i = 0; commands[i].cmd; i++)
 
775
                printf("%-15s %-30s %s\n", commands[i].cmd,
 
776
                                commands[i].params, commands[i].desc);
 
777
}
 
778
 
 
779
static void parse_line(char *line_read)
 
780
{
 
781
        gchar **argvp;
 
782
        int argcp;
 
783
        int i;
 
784
 
 
785
        if (line_read == NULL) {
 
786
                printf("\n");
 
787
                cmd_exit(0, NULL);
 
788
                return;
 
789
        }
 
790
 
 
791
        line_read = g_strstrip(line_read);
 
792
 
 
793
        if (*line_read == '\0')
 
794
                return;
 
795
 
 
796
        add_history(line_read);
 
797
 
 
798
        g_shell_parse_argv(line_read, &argcp, &argvp, NULL);
 
799
 
 
800
        for (i = 0; commands[i].cmd; i++)
 
801
                if (strcasecmp(commands[i].cmd, argvp[0]) == 0)
 
802
                        break;
 
803
 
 
804
        if (commands[i].cmd)
 
805
                commands[i].func(argcp, argvp);
 
806
        else
 
807
                printf("%s: command not found\n", argvp[0]);
 
808
 
 
809
        g_strfreev(argvp);
 
810
}
 
811
 
 
812
static gboolean prompt_read(GIOChannel *chan, GIOCondition cond,
 
813
                                                        gpointer user_data)
 
814
{
 
815
        if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
 
816
                g_io_channel_unref(chan);
 
817
                return FALSE;
 
818
        }
 
819
 
 
820
        rl_callback_read_char();
 
821
 
 
822
        return TRUE;
 
823
}
 
824
 
 
825
static char *completion_generator(const char *text, int state)
 
826
{
 
827
        static int index = 0, len = 0;
 
828
        const char *cmd = NULL;
 
829
 
 
830
        if (state == 0) {
 
831
                index = 0;
 
832
                len = strlen(text);
 
833
        }
 
834
 
 
835
        while ((cmd = commands[index].cmd) != NULL) {
 
836
                index++;
 
837
                if (strncmp(cmd, text, len) == 0)
 
838
                        return strdup(cmd);
 
839
        }
 
840
 
 
841
        return NULL;
 
842
}
 
843
 
 
844
static char **commands_completion(const char *text, int start, int end)
 
845
{
 
846
        if (start == 0)
 
847
                return rl_completion_matches(text, &completion_generator);
 
848
        else
 
849
                return NULL;
 
850
}
 
851
 
 
852
int interactive(const gchar *src, const gchar *dst,
 
853
                const gchar *dst_type, int psm)
 
854
{
 
855
        GIOChannel *pchan;
 
856
        gint events;
 
857
 
 
858
        opt_sec_level = g_strdup("low");
 
859
 
 
860
        opt_src = g_strdup(src);
 
861
        opt_dst = g_strdup(dst);
 
862
        opt_dst_type = g_strdup(dst_type);
 
863
        opt_psm = psm;
 
864
 
 
865
        prompt = g_string_new(NULL);
 
866
 
 
867
        event_loop = g_main_loop_new(NULL, FALSE);
 
868
 
 
869
        pchan = g_io_channel_unix_new(fileno(stdin));
 
870
        g_io_channel_set_close_on_unref(pchan, TRUE);
 
871
        events = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
 
872
        g_io_add_watch(pchan, events, prompt_read, NULL);
 
873
 
 
874
        rl_attempted_completion_function = commands_completion;
 
875
        rl_callback_handler_install(get_prompt(), parse_line);
 
876
 
 
877
        g_main_loop_run(event_loop);
 
878
 
 
879
        rl_callback_handler_remove();
 
880
        cmd_disconnect(0, NULL);
 
881
        g_io_channel_unref(pchan);
 
882
        g_main_loop_unref(event_loop);
 
883
        g_string_free(prompt, TRUE);
 
884
 
 
885
        g_free(opt_src);
 
886
        g_free(opt_dst);
 
887
        g_free(opt_sec_level);
 
888
 
 
889
        return 0;
 
890
}