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