3
* BlueZ - Bluetooth protocol stack for Linux
5
* Copyright (C) 2011 Nokia Corporation
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.
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.
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
29
#include <bluetooth/uuid.h>
31
#include <readline/readline.h>
32
#include <readline/history.h>
40
static GIOChannel *iochannel = NULL;
41
static GAttrib *attrib = NULL;
42
static GMainLoop *event_loop;
43
static GString *prompt;
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;
52
struct characteristic_data {
59
static void cmd_help(int argcp, char **argvp);
67
static char *get_prompt(void)
69
if (conn_state == STATE_CONNECTING) {
70
g_string_assign(prompt, "Connecting... ");
74
if (conn_state == STATE_CONNECTED)
75
g_string_assign(prompt, "[CON]");
77
g_string_assign(prompt, "[ ]");
80
g_string_append_printf(prompt, "[%17s]", opt_dst);
82
g_string_append_printf(prompt, "[%17s]", "");
85
g_string_append(prompt, "[BR]");
87
g_string_append(prompt, "[LE]");
89
g_string_append(prompt, "> ");
95
static void set_state(enum state st)
98
rl_set_prompt(get_prompt());
102
static void events_handler(const uint8_t *pdu, uint16_t len, gpointer user_data)
104
uint8_t opdu[ATT_MAX_MTU];
105
uint16_t handle, i, olen;
107
handle = att_get_u16(&pdu[1]);
111
case ATT_OP_HANDLE_NOTIFY:
112
printf("Notification handle = 0x%04x value: ", handle);
114
case ATT_OP_HANDLE_IND:
115
printf("Indication handle = 0x%04x value: ", handle);
118
printf("Invalid opcode\n");
122
for (i = 3; i < len; i++)
123
printf("%02x ", pdu[i]);
126
rl_forced_update_display();
128
if (pdu[0] == ATT_OP_HANDLE_NOTIFY)
131
olen = enc_confirmation(opdu, sizeof(opdu));
134
g_attrib_send(attrib, 0, opdu[0], opdu, olen, NULL, NULL, NULL);
137
static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
140
printf("connect error: %s\n", err->message);
141
set_state(STATE_DISCONNECTED);
145
attrib = g_attrib_new(iochannel);
146
g_attrib_register(attrib, ATT_OP_HANDLE_NOTIFY, events_handler,
148
g_attrib_register(attrib, ATT_OP_HANDLE_IND, events_handler,
150
set_state(STATE_CONNECTED);
153
static void disconnect_io()
155
if (conn_state == STATE_DISCONNECTED)
158
g_attrib_unref(attrib);
162
g_io_channel_shutdown(iochannel, FALSE, NULL);
163
g_io_channel_unref(iochannel);
166
set_state(STATE_DISCONNECTED);
169
static void primary_all_cb(GSList *services, guint8 status, gpointer user_data)
174
printf("Discover all primary services failed: %s\n",
175
att_ecode2str(status));
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);
186
rl_forced_update_display();
189
static void primary_by_uuid_cb(GSList *ranges, guint8 status,
195
printf("Discover primary services by UUID failed: %s\n",
196
att_ecode2str(status));
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);
207
rl_forced_update_display();
210
static void char_cb(GSList *characteristics, guint8 status, gpointer user_data)
215
printf("Discover all characteristics failed: %s\n",
216
att_ecode2str(status));
221
for (l = characteristics; l; l = l->next) {
222
struct gatt_char *chars = l->data;
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,
230
rl_forced_update_display();
233
static void char_desc_cb(guint8 status, const guint8 *pdu, guint16 plen,
236
struct att_data_list *list;
241
printf("Discover all characteristic descriptors failed: "
242
"%s\n", att_ecode2str(status));
246
list = dec_find_info_resp(pdu, plen, &format);
251
for (i = 0; i < list->num; i++) {
252
char uuidstr[MAX_LEN_UUID_STR];
257
value = list->data[i];
258
handle = att_get_u16(value);
261
uuid = att_get_uuid16(&value[2]);
263
uuid = att_get_uuid128(&value[2]);
265
bt_uuid_to_string(&uuid, uuidstr, MAX_LEN_UUID_STR);
266
printf("handle: 0x%04x, uuid: %s\n", handle, uuidstr);
269
att_data_list_free(list);
271
rl_forced_update_display();
274
static void char_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
277
uint8_t value[ATT_MAX_MTU];
281
printf("Characteristic value/descriptor read failed: %s\n",
282
att_ecode2str(status));
286
if (!dec_read_resp(pdu, plen, value, &vlen)) {
287
printf("Protocol error\n");
291
printf("\nCharacteristic value/descriptor: ");
292
for (i = 0; i < vlen; i++)
293
printf("%02x ", value[i]);
296
rl_forced_update_display();
299
static void char_read_by_uuid_cb(guint8 status, const guint8 *pdu,
300
guint16 plen, gpointer user_data)
302
struct characteristic_data *char_data = user_data;
303
struct att_data_list *list;
306
if (status == ATT_ECODE_ATTR_NOT_FOUND &&
307
char_data->start != char_data->orig_start)
311
printf("Read characteristics by UUID failed: %s\n",
312
att_ecode2str(status));
316
list = dec_read_by_type_resp(pdu, plen);
320
for (i = 0; i < list->num; i++) {
321
uint8_t *value = list->data[i];
324
char_data->start = att_get_u16(value) + 1;
326
printf("\nhandle: 0x%04x \t value: ", att_get_u16(value));
328
for (j = 0; j < list->len - 2; j++, value++)
329
printf("%02x ", *value);
333
att_data_list_free(list);
335
rl_forced_update_display();
341
static void cmd_exit(int argcp, char **argvp)
343
rl_callback_handler_remove();
344
g_main_loop_quit(event_loop);
347
static gboolean channel_watcher(GIOChannel *chan, GIOCondition cond,
355
static void cmd_connect(int argcp, char **argvp)
357
if (conn_state != STATE_DISCONNECTED)
362
opt_dst = g_strdup(argvp[1]);
364
g_free(opt_dst_type);
366
opt_dst_type = g_strdup(argvp[2]);
368
opt_dst_type = g_strdup("public");
371
if (opt_dst == NULL) {
372
printf("Remote Bluetooth address required\n");
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);
382
g_io_add_watch(iochannel, G_IO_HUP, channel_watcher, NULL);
385
static void cmd_disconnect(int argcp, char **argvp)
390
static void cmd_primary(int argcp, char **argvp)
394
if (conn_state != STATE_CONNECTED) {
395
printf("Command failed: disconnected\n");
400
gatt_discover_primary(attrib, NULL, primary_all_cb, NULL);
404
if (bt_string_to_uuid(&uuid, argvp[1]) < 0) {
405
printf("Invalid UUID\n");
409
gatt_discover_primary(attrib, &uuid, primary_by_uuid_cb, NULL);
412
static int strtohandle(const char *src)
418
dst = strtoll(src, &e, 16);
419
if (errno != 0 || *e != '\0')
425
static void cmd_char(int argcp, char **argvp)
430
if (conn_state != STATE_CONNECTED) {
431
printf("Command failed: disconnected\n");
436
start = strtohandle(argvp[1]);
438
printf("Invalid start handle: %s\n", argvp[1]);
444
end = strtohandle(argvp[2]);
446
printf("Invalid end handle: %s\n", argvp[2]);
454
if (bt_string_to_uuid(&uuid, argvp[3]) < 0) {
455
printf("Invalid UUID\n");
459
gatt_discover_char(attrib, start, end, &uuid, char_cb, NULL);
463
gatt_discover_char(attrib, start, end, NULL, char_cb, NULL);
466
static void cmd_char_desc(int argcp, char **argvp)
471
if (conn_state != STATE_CONNECTED) {
472
printf("Command failed: disconnected\n");
477
start = strtohandle(argvp[1]);
479
printf("Invalid start handle: %s\n", argvp[1]);
485
end = strtohandle(argvp[2]);
487
printf("Invalid end handle: %s\n", argvp[2]);
492
gatt_find_info(attrib, start, end, char_desc_cb, NULL);
495
static void cmd_read_hnd(int argcp, char **argvp)
500
if (conn_state != STATE_CONNECTED) {
501
printf("Command failed: disconnected\n");
506
printf("Missing argument: handle\n");
510
handle = strtohandle(argvp[1]);
512
printf("Invalid handle: %s\n", argvp[1]);
520
offset = strtol(argvp[2], &e, 0);
521
if (errno != 0 || *e != '\0') {
522
printf("Invalid offset: %s\n", argvp[2]);
527
gatt_read_char(attrib, handle, offset, char_read_cb, attrib);
530
static void cmd_read_uuid(int argcp, char **argvp)
532
struct characteristic_data *char_data;
537
if (conn_state != STATE_CONNECTED) {
538
printf("Command failed: disconnected\n");
543
printf("Missing argument: UUID\n");
547
if (bt_string_to_uuid(&uuid, argvp[1]) < 0) {
548
printf("Invalid UUID\n");
553
start = strtohandle(argvp[2]);
555
printf("Invalid start handle: %s\n", argvp[1]);
561
end = strtohandle(argvp[3]);
563
printf("Invalid end handle: %s\n", argvp[2]);
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;
574
gatt_read_char_by_uuid(attrib, start, end, &char_data->uuid,
575
char_read_by_uuid_cb, char_data);
578
static void char_write_req_cb(guint8 status, const guint8 *pdu, guint16 plen,
582
printf("Characteristic Write Request failed: "
583
"%s\n", att_ecode2str(status));
587
if (!dec_write_resp(pdu, plen)) {
588
printf("Protocol error\n");
592
printf("Characteristic value was written successfully\n");
595
static void cmd_char_write(int argcp, char **argvp)
601
if (conn_state != STATE_CONNECTED) {
602
printf("Command failed: disconnected\n");
607
printf("Usage: %s <handle> <new value>\n", argvp[0]);
611
handle = strtohandle(argvp[1]);
613
printf("A valid handle is required\n");
617
plen = gatt_attr_data_from_string(argvp[2], &value);
619
g_printerr("Invalid value\n");
623
if (g_strcmp0("char-write-req", argvp[0]) == 0)
624
gatt_write_char(attrib, handle, value, plen,
625
char_write_req_cb, NULL);
627
gatt_write_char(attrib, handle, value, plen, NULL, NULL);
632
static void cmd_sec_level(int argcp, char **argvp)
635
BtIOSecLevel sec_level;
638
printf("sec-level: %s\n", opt_sec_level);
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;
649
printf("Allowed values: low | medium | high\n");
653
g_free(opt_sec_level);
654
opt_sec_level = g_strdup(argvp[1]);
656
if (conn_state != STATE_CONNECTED)
660
printf("It must be reconnected to this change take effect\n");
664
bt_io_set(iochannel, BT_IO_L2CAP, &gerr,
665
BT_IO_OPT_SEC_LEVEL, sec_level,
669
printf("Error: %s\n", gerr->message);
674
static void exchange_mtu_cb(guint8 status, const guint8 *pdu, guint16 plen,
680
printf("Exchange MTU Request failed: %s\n",
681
att_ecode2str(status));
685
if (!dec_mtu_resp(pdu, plen, &mtu)) {
686
printf("Protocol error\n");
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);
695
printf("Error exchanging MTU\n");
698
static void cmd_mtu(int argcp, char **argvp)
700
if (conn_state != STATE_CONNECTED) {
701
printf("Command failed: not connected.\n");
706
printf("Command failed: operation is only available for LE"
712
printf("Usage: mtu <value>\n");
717
printf("Command failed: MTU exchange can only occur once per"
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",
730
gatt_exchange_mtu(attrib, opt_mtu, exchange_mtu_cb, NULL);
735
void (*func)(int argcp, char **argvp);
739
{ "help", cmd_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" },
770
static void cmd_help(int argcp, char **argvp)
774
for (i = 0; commands[i].cmd; i++)
775
printf("%-15s %-30s %s\n", commands[i].cmd,
776
commands[i].params, commands[i].desc);
779
static void parse_line(char *line_read)
785
if (line_read == NULL) {
791
line_read = g_strstrip(line_read);
793
if (*line_read == '\0')
796
add_history(line_read);
798
g_shell_parse_argv(line_read, &argcp, &argvp, NULL);
800
for (i = 0; commands[i].cmd; i++)
801
if (strcasecmp(commands[i].cmd, argvp[0]) == 0)
805
commands[i].func(argcp, argvp);
807
printf("%s: command not found\n", argvp[0]);
812
static gboolean prompt_read(GIOChannel *chan, GIOCondition cond,
815
if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
816
g_io_channel_unref(chan);
820
rl_callback_read_char();
825
static char *completion_generator(const char *text, int state)
827
static int index = 0, len = 0;
828
const char *cmd = NULL;
835
while ((cmd = commands[index].cmd) != NULL) {
837
if (strncmp(cmd, text, len) == 0)
844
static char **commands_completion(const char *text, int start, int end)
847
return rl_completion_matches(text, &completion_generator);
852
int interactive(const gchar *src, const gchar *dst,
853
const gchar *dst_type, int psm)
858
opt_sec_level = g_strdup("low");
860
opt_src = g_strdup(src);
861
opt_dst = g_strdup(dst);
862
opt_dst_type = g_strdup(dst_type);
865
prompt = g_string_new(NULL);
867
event_loop = g_main_loop_new(NULL, FALSE);
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);
874
rl_attempted_completion_function = commands_completion;
875
rl_callback_handler_install(get_prompt(), parse_line);
877
g_main_loop_run(event_loop);
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);
887
g_free(opt_sec_level);