shared/shell: Add initial implementation
Subsystems (2)
Luiz Augusto von Dentz
maintainerPATCH v3 1/6 shared/shell: Add initial implementation
From: Luiz Augusto von Dentz <[email protected]>
This add initial bt_shell helper which can be used to create shell-like command line tools.
---
v3: Add submenu changes
Makefile.tools | 3 +- src/shared/shell.c | 570 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/shared/shell.h | 67 +++++++ 3 files changed, 639 insertions(+), 1 deletion(-) create mode 100644 src/shared/shell.c create mode 100644 src/shared/shell.h
Makefile.tools +2 -1
@@ -8,7 +8,8 @@ client_bluetoothctl_SOURCES = client/main.c \ client/advertising.h \ client/advertising.c \ client/gatt.h client/gatt.c \ - monitor/uuid.h monitor/uuid.c + monitor/uuid.h monitor/uuid.c \ + src/shared/shell.h src/shared/shell.c client_bluetoothctl_LDADD = gdbus/libgdbus-internal.la src/libshared-glib.la \ @GLIB_LIBS@ @DBUS_LIBS@ -lreadline endif
src/shared/shell.c new file +570 -0
570 lines not on this page · raw message
src/shared/shell.h new file +67 -0
67 lines not on this page · raw message
Signature · 1 line
2.13.6
Luiz Augusto von Dentz
maintainerPATCH v3 2/6 client: Make use of bt_shell
From: Luiz Augusto von Dentz <[email protected]>
Use bt_shell instead of readline directly.
--- client/advertising.c | 42 +++---- client/agent.c | 72 +++++------ client/gatt.c | 149 ++++++++++++----------- client/main.c | 333 ++------------------------------------------------- 4 files changed, 146 insertions(+), 450 deletions(-)
client/advertising.c +21 -21
@@ -29,11 +29,11 @@#include <stdlib.h> #include <stdint.h> #include <stdbool.h> -#include <readline/readline.h> +#include <string.h> #include <wordexp.h> #include "gdbus/gdbus.h" -#include "display.h" +#include "src/shared/shell.h" #include "advertising.h" #define AD_PATH "/org/bluez/advertising"
@@ -82,7 +82,7 @@ static void ad_release(DBusConnection *conn)static DBusMessage *release_advertising (DBusConnection *conn,DBusMessage *msg,void *user_data) { -rl_printf ("Advertising released\n" ); +bt_shell_printf ("Advertising released\n" );ad_release (conn);
@@ -117,14 +117,14 @@ static void register_reply(DBusMessage *message, void *user_data)if (dbus_set_error_from_message (&error, message) == FALSE) { ad.registered = true; -rl_printf ("Advertising object registered\n" ); +bt_shell_printf ("Advertising object registered\n" ); }else { -rl_printf ("Failed to register advertisement: %s\n" , error.name); +bt_shell_printf ("Failed to register advertisement: %s\n" , error.name);dbus_error_free (&error);if (g_dbus_unregister_interface (conn,AD_PATH ,AD_IFACE ) == FALSE) -rl_printf ("Failed to unregister advertising object\n" ); +bt_shell_printf ("Failed to unregister advertising object\n" ); } }
@@ -368,7 +368,7 @@ static const GDBusPropertyTable ad_props[] = {void ad_register (DBusConnection *conn,GDBusProxy *manager,const char *type) {if (ad.registered) { -rl_printf ("Advertisement is already registered\n" ); +bt_shell_printf ("Advertisement is already registered\n" );return ; }
@@ -377,14 +377,14 @@ void ad_register(DBusConnection *conn, GDBusProxy *manager, const char *type)if (g_dbus_register_interface (conn,AD_PATH ,AD_IFACE , ad_methods,NULL , ad_props,NULL ,NULL ) == FALSE) { -rl_printf ("Failed to register advertising object\n" ); +bt_shell_printf ("Failed to register advertising object\n" );return ; }if (g_dbus_proxy_method_call (manager,"RegisterAdvertisement" , register_setup, register_reply, conn,NULL ) == FALSE) { -rl_printf ("Failed to register advertising\n" ); +bt_shell_printf ("Failed to register advertising\n" );return ; } }
@@ -405,12 +405,12 @@ static void unregister_reply(DBusMessage *message, void *user_data)if (dbus_set_error_from_message (&error, message) == FALSE) { ad.registered = false; -rl_printf ("Advertising object unregistered\n" ); +bt_shell_printf ("Advertising object unregistered\n" );if (g_dbus_unregister_interface (conn,AD_PATH ,AD_IFACE ) == FALSE) -rl_printf ("Failed to unregister advertising object\n" ); +bt_shell_printf ("Failed to unregister advertising object\n" ); }else { -rl_printf ("Failed to unregister advertisement: %s\n" , +bt_shell_printf ("Failed to unregister advertisement: %s\n" , error.name);dbus_error_free (&error); }
@@ -430,7 +430,7 @@ void ad_unregister(DBusConnection *conn, GDBusProxy *manager)if (g_dbus_proxy_method_call (manager,"UnregisterAdvertisement" , unregister_setup, unregister_reply, conn,NULL ) == FALSE) { -rl_printf ("Failed to unregister advertisement method\n" ); +bt_shell_printf ("Failed to unregister advertisement method\n" );return ; } }
@@ -446,7 +446,7 @@ void ad_advertise_uuids(DBusConnection *conn, const char *arg) ad.uuids =g_strsplit (arg," " ,-1 );if (!ad.uuids) { -rl_printf ("Failed to parse input\n" ); +bt_shell_printf ("Failed to parse input\n" );return ; }
@@ -468,7 +468,7 @@ void ad_advertise_service(DBusConnection *conn, const char *arg)struct ad_data *data;if (wordexp (arg, &w,WRDE_NOCMD )) { -rl_printf ("Invalid argument\n" ); +bt_shell_printf ("Invalid argument\n" );return ; }
@@ -485,14 +485,14 @@ void ad_advertise_service(DBusConnection *conn, const char *arg)char *endptr =NULL ;if (i >=G_N_ELEMENTS (data->data)) { -rl_printf ("Too much data\n" ); +bt_shell_printf ("Too much data\n" );ad_clear_service (); goto done; } val =strtol (w.we_wordv[i], &endptr,0 );if (!endptr || *endptr !='\0' || val >UINT8_MAX ) { -rl_printf ("Invalid value at index %d\n" , i); +bt_shell_printf ("Invalid value at index %d\n" , i);ad_clear_service (); goto done; }
@@ -521,7 +521,7 @@ void ad_advertise_manufacturer(DBusConnection *conn, const char *arg)struct ad_data *data;if (wordexp (arg, &w,WRDE_NOCMD )) { -rl_printf ("Invalid argument\n" ); +bt_shell_printf ("Invalid argument\n" );return ; }
@@ -532,7 +532,7 @@ void ad_advertise_manufacturer(DBusConnection *conn, const char *arg) val =strtol (w.we_wordv[0 ], &endptr,0 );if (!endptr || *endptr !='\0' || val >UINT16_MAX ) { -rl_printf ("Invalid manufacture id\n" ); +bt_shell_printf ("Invalid manufacture id\n" ); goto done; }
@@ -541,14 +541,14 @@ void ad_advertise_manufacturer(DBusConnection *conn, const char *arg)for (i =1 ; i < w.we_wordc; i++) {if (i >=G_N_ELEMENTS (data->data)) { -rl_printf ("Too much data\n" ); +bt_shell_printf ("Too much data\n" );ad_clear_manufacturer (); goto done; } val =strtol (w.we_wordv[i], &endptr,0 );if (!endptr || *endptr !='\0' || val >UINT8_MAX ) { -rl_printf ("Invalid value at index %d\n" , i); +bt_shell_printf ("Invalid value at index %d\n" , i);ad_clear_manufacturer (); goto done; }
client/agent.c +38 -34
236 lines not on this page · raw message
client/gatt.c +74 -75
558 lines not on this page · raw message
client/main.c +13 -320
436 lines not on this page · raw message
Signature · 1 line
2.13.6
Luiz Augusto von Dentz
maintainerPATCH v3 3/6 shared/shell: Add submenu support
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
This adds submenu support so application can have one extra level of menus to better organize the interface.
Submenus will be show in the output of help command, to select a submenu:
Quoted 1 line
menu <name>
To move back to main menu:
Quoted 1 line
back
--- client/main.c | 8 ++- src/shared/shell.c | 168 +++++++++++++++++++++++++++++++++++++++++++++-------- src/shared/shell.h | 11 +++- 3 files changed, 159 insertions(+), 28 deletions(-)
client/main.c +5 -3
@@ -2358,7 +2358,9 @@ static void cmd_set_advertise_timeout(const char *arg)ad_advertise_timeout (dbus_conn, value); } -static const struct bt_shell_menu_entry cmd_table[] = { +static const struct bt_shell_menu main_menu = { + .name ="main" , + .entries = { {"list" ,NULL , cmd_list,"List available controllers" }, {"show" ,"[ctrl]" , cmd_show,"Controller information" , ctrl_generator },
@@ -2482,7 +2484,7 @@ static const struct bt_shell_menu_entry cmd_table[] = { {"unregister-descriptor" ,"<UUID/object>" , cmd_unregister_descriptor, "Unregister application descriptor" }, - { } + { } }, }; static gboolean parse_agent(const char *key, const char *value,
@@ -2531,7 +2533,7 @@ int main(int argc, char *argv[])g_option_context_free (context);bt_shell_init (&argc, &argv); -bt_shell_set_menu (cmd_table); +bt_shell_set_menu (&main_menu);bt_shell_set_prompt (PROMPT_OFF ); dbus_conn =g_dbus_setup_bus (DBUS_BUS_SYSTEM ,NULL ,NULL );
src/shared/shell.c +144 -24
248 lines not on this page · raw message
src/shared/shell.h +10 -1
@@ -45,11 +45,20 @@ struct bt_shell_menu_entry {bt_shell_menu_disp_t disp; }; +struct bt_shell_menu { +const char *name; +const struct bt_shell_menu_entry entries[]; +}; +void bt_shell_init (int *argc,char ***argv);void bt_shell_run (void ); -bool bt_shell_set_menu (const struct bt_shell_menu_entry *menu); +bool bt_shell_set_menu (const struct bt_shell_menu *menu); + +bool bt_shell_add_submenu (const struct bt_shell_menu *menu); + +bool bt_shell_remove_submenu (const struct bt_shell_menu *menu);void bt_shell_set_prompt (const char *string);
Signature · 1 line
2.13.6
Luiz Augusto von Dentz
maintainerPATCH v3 4/6 client: Move advertise related commands to a submenu
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
All commands related to advertise settings are now under advertise submenu:
Quoted 1 line
menu advertise
Menu advertise:
Available commands:
-------------------
set-uuids [uuid1 uuid2 ...] Set advertise uuids set-service [uuid][data=[xx xx ...] Set advertise service data set-manufacturer [id][data=[xx xx ...] Set advertise manufacturer data set-tx-power <on/off> Enable/disable TX power to be advertised set-name <on/off/name> Enable/disable local name to be advertised set-appearance <value> Set custom appearance to be advertised set-duration <seconds> Set advertise duration set-timeout <seconds> Set advertise timeout back Return to main menu version Display version quit Quit program exit Quit program help --- client/main.c | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-)
client/main.c +25 -20
@@ -2358,6 +2358,30 @@ static void cmd_set_advertise_timeout(const char *arg)ad_advertise_timeout (dbus_conn, value); } +static const struct bt_shell_menu advertise_menu = { + .name ="advertise" , + .entries = { + {"set-uuids" ,"[uuid1 uuid2 ...]" , + cmd_set_advertise_uuids,"Set advertise uuids" }, + {"set-service" ,"[uuid][data=[xx xx ...]" , cmd_set_advertise_service, +"Set advertise service data" }, + {"set-manufacturer" ,"[id][data=[xx xx ...]" , + cmd_set_advertise_manufacturer, +"Set advertise manufacturer data" }, + {"set-tx-power" ,"<on/off>" , cmd_set_advertise_tx_power, +"Enable/disable TX power to be advertised" , + mode_generator }, + {"set-name" ,"<on/off/name>" , cmd_set_advertise_name, +"Enable/disable local name to be advertised" }, + {"set-appearance" ,"<value>" , cmd_set_advertise_appearance, +"Set custom appearance to be advertised" }, + {"set-duration" ,"<seconds>" , cmd_set_advertise_duration, +"Set advertise duration" }, + {"set-timeout" ,"<seconds>" , cmd_set_advertise_timeout, +"Set advertise timeout" }, + { } }, +}; +static const struct bt_shell_menu main_menu = { .name ="main" , .entries = {
@@ -2389,26 +2413,6 @@ static const struct bt_shell_menu main_menu = { {"advertise" ,"<on/off/type>" , cmd_advertise,"Enable/disable advertising with given type" , ad_generator}, - { "set-advertise-uuids", "[uuid1 uuid2 ...]", - cmd_set_advertise_uuids, "Set advertise uuids" }, - { "set-advertise-service", "[uuid][data=[xx xx ...]", - cmd_set_advertise_service, - "Set advertise service data" }, - { "set-advertise-manufacturer", "[id][data=[xx xx ...]", - cmd_set_advertise_manufacturer, - "Set advertise manufacturer data" }, - { "set-advertise-tx-power", "<on/off>", - cmd_set_advertise_tx_power, -"Enable/disable TX power to be advertised" , - mode_generator }, - { "set-advertise-name", "<on/off/name>", cmd_set_advertise_name, - "Enable/disable local name to be advertised" }, - { "set-advertise-appearance", "<value>", cmd_set_advertise_appearance, - "Set custom appearance to be advertised" }, - { "set-advertise-duration", "<seconds>", cmd_set_advertise_duration, - "Set advertise duration" }, - { "set-advertise-timeout", "<seconds>", cmd_set_advertise_timeout, - "Set advertise timeout" }, { "set-scan-filter-uuids", "[uuid1 uuid2 ...]", cmd_set_scan_filter_uuids, "Set scan filter uuids" }, { "set-scan-filter-rssi", "[rssi]", cmd_set_scan_filter_rssi,
@@ -2534,6 +2538,7 @@ int main(int argc, char *argv[])bt_shell_init (&argc, &argv);bt_shell_set_menu (&main_menu); +bt_shell_add_submenu (&advertise_menu);bt_shell_set_prompt (PROMPT_OFF ); dbus_conn =g_dbus_setup_bus (DBUS_BUS_SYSTEM ,NULL ,NULL );
Signature · 1 line
2.13.6
Luiz Augusto von Dentz
maintainerPATCH v3 5/6 client: Move scan related commands to a submenu
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
All commands related to scan settings are now under scan submenu:
[bluetooth]# menu scan
Menu scan:
Available commands:
-------------------
set-filter-uuids [uuid1 uuid2 ...] Set scan filter uuids set-filter-rssi [rssi] Set scan filter rssi, and clears pathloss set-filter-pathloss [pathloss] Set scan filter pathloss, and clears rssi set-filter-transport [transport] Set scan filter transport set-filter-duplicate-data [on/off] Set scan filter duplicate data set-filter-clear Clears discovery filter. back Return to main menu version Display version quit Quit program exit Quit program help Display help about this program --- client/main.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-)
client/main.c +21 -15
@@ -2382,6 +2382,26 @@ static const struct bt_shell_menu advertise_menu = { { } }, }; +static const struct bt_shell_menu scan_menu = { + .name ="scan" , + .entries = { + {"set-filter-uuids" ,"[uuid1 uuid2 ...]" , cmd_set_scan_filter_uuids, +"Set scan filter uuids" }, + {"set-filter-rssi" ,"[rssi]" , cmd_set_scan_filter_rssi, +"Set scan filter rssi, and clears pathloss" }, + {"set-filter-pathloss" ,"[pathloss]" , cmd_set_scan_filter_pathloss, +"Set scan filter pathloss, and clears rssi" }, + {"set-filter-transport" ,"[transport]" , cmd_set_scan_filter_transport, +"Set scan filter transport" }, + {"set-filter-duplicate-data" ,"[on/off]" , + cmd_set_scan_filter_duplicate_data, +"Set scan filter duplicate data" , + mode_generator }, + {"set-filter-clear" ,"" , cmd_set_scan_filter_clear, +"Clears discovery filter." }, + { } }, +}; +static const struct bt_shell_menu main_menu = { .name ="main" , .entries = {
@@ -2413,21 +2433,6 @@ static const struct bt_shell_menu main_menu = { {"advertise" ,"<on/off/type>" , cmd_advertise,"Enable/disable advertising with given type" , ad_generator}, - { "set-scan-filter-uuids", "[uuid1 uuid2 ...]", - cmd_set_scan_filter_uuids, "Set scan filter uuids" }, - { "set-scan-filter-rssi", "[rssi]", cmd_set_scan_filter_rssi, - "Set scan filter rssi, and clears pathloss" }, - { "set-scan-filter-pathloss", "[pathloss]", - cmd_set_scan_filter_pathloss, - "Set scan filter pathloss, and clears rssi" }, - { "set-scan-filter-transport", "[transport]", - cmd_set_scan_filter_transport, "Set scan filter transport" }, - { "set-scan-filter-duplicate-data", "[on/off]", - cmd_set_scan_filter_duplicate_data, -"Set scan filter duplicate data" , - mode_generator }, - { "set-scan-filter-clear", "", cmd_set_scan_filter_clear, - "Clears discovery filter." }, { "scan", "<on/off>", cmd_scan, "Scan for devices", mode_generator }, { "info", "[dev]", cmd_info, "Device information",
@@ -2539,6 +2544,7 @@ int main(int argc, char *argv[])bt_shell_init (&argc, &argv);bt_shell_set_menu (&main_menu);bt_shell_add_submenu (&advertise_menu); +bt_shell_add_submenu (&scan_menu);bt_shell_set_prompt (PROMPT_OFF ); dbus_conn =g_dbus_setup_bus (DBUS_BUS_SYSTEM ,NULL ,NULL );
Signature · 1 line
2.13.6
Luiz Augusto von Dentz
maintainerPATCH v3 6/6 client: Move gatt related commands to a submenu
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
All commands related to gatt settings are now under gatt submenu:
[bluetooth]# menu gatt
Menu gatt:
Available commands:
-------------------
list-attributes [dev] List attributes set-alias <alias> Set device alias select-attribute <attribute/UUID> Select attribute attribute-info [attribute/UUID] Select attribute read Read attribute value write <data=[xx xx ...]> Write attribute value acquire-write Acquire Write file descriptor release-write Release Write file descriptor acquire-notify Acquire Notify file descriptor release-notify Release Notify file descriptor notify <on/off> Notify attribute value register-application [UUID ...] Register profile to connect unregister-application Unregister profile register-service <UUID> Register application service. unregister-service <UUID/object> Unregister application service register-characteristic <UUID> <Flags=read,write,notify...> Register application characteristic unregister-characteristic <UUID/object> Unregister application characteristic register-descriptor <UUID> <Flags=read,write...> Register application descriptor unregister-descriptor <UUID/object> Unregister application descriptor back Return to main menu version Display version quit Quit program exit Quit program help Display help about this program --- client/main.c | 87 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 47 insertions(+), 40 deletions(-)
client/main.c +47 -40
@@ -2402,6 +2402,52 @@ static const struct bt_shell_menu scan_menu = { { } }, }; +static const struct bt_shell_menu gatt_menu = { + .name ="gatt" , + .entries = { + {"list-attributes" ,"[dev]" , cmd_list_attributes,"List attributes" , + dev_generator }, + {"set-alias" ,"<alias>" , cmd_set_alias,"Set device alias" }, + {"select-attribute" ,"<attribute/UUID>" , cmd_select_attribute, +"Select attribute" , attribute_generator }, + {"attribute-info" ,"[attribute/UUID]" , cmd_attribute_info, +"Select attribute" , attribute_generator }, + {"read" ,NULL , cmd_read,"Read attribute value" }, + {"write" ,"<data=[xx xx ...]>" , cmd_write, +"Write attribute value" }, + {"acquire-write" ,NULL , cmd_acquire_write, +"Acquire Write file descriptor" }, + {"release-write" ,NULL , cmd_release_write, +"Release Write file descriptor" }, + {"acquire-notify" ,NULL , cmd_acquire_notify, +"Acquire Notify file descriptor" }, + {"release-notify" ,NULL , cmd_release_notify, +"Release Notify file descriptor" }, + {"notify" ,"<on/off>" , cmd_notify,"Notify attribute value" , + mode_generator }, + {"register-application" ,"[UUID ...]" , cmd_register_app, +"Register profile to connect" }, + {"unregister-application" ,NULL , cmd_unregister_app, +"Unregister profile" }, + {"register-service" ,"<UUID>" , cmd_register_service, +"Register application service." }, + {"unregister-service" ,"<UUID/object>" , cmd_unregister_service, +"Unregister application service" }, + {"register-characteristic" ,"<UUID> <Flags=read,write,notify...>" , + cmd_register_characteristic, +"Register application characteristic" }, + {"unregister-characteristic" ,"<UUID/object>" , + cmd_unregister_characteristic, +"Unregister application characteristic" }, + {"register-descriptor" ,"<UUID> <Flags=read,write...>" , + cmd_register_descriptor, +"Register application descriptor" }, + {"unregister-descriptor" ,"<UUID/object>" , + cmd_unregister_descriptor, +"Unregister application descriptor" }, + { } }, +}; +static const struct bt_shell_menu main_menu = { .name ="main" , .entries = {
@@ -2453,46 +2499,6 @@ static const struct bt_shell_menu main_menu = { dev_generator }, {"disconnect" ,"[dev]" , cmd_disconn,"Disconnect device" , dev_generator }, - { "list-attributes", "[dev]", cmd_list_attributes, "List attributes", - dev_generator }, - { "set-alias", "<alias>", cmd_set_alias, "Set device alias" }, - { "select-attribute", "<attribute/UUID>", cmd_select_attribute, - "Select attribute", attribute_generator }, - { "attribute-info", "[attribute/UUID]", cmd_attribute_info, - "Select attribute", attribute_generator }, - { "read", NULL, cmd_read, "Read attribute value" }, - { "write", "<data=[xx xx ...]>", cmd_write, - "Write attribute value" }, - { "acquire-write", NULL, cmd_acquire_write, - "Acquire Write file descriptor" }, - { "release-write", NULL, cmd_release_write, - "Release Write file descriptor" }, - { "acquire-notify", NULL, cmd_acquire_notify, - "Acquire Notify file descriptor" }, - { "release-notify", NULL, cmd_release_notify, - "Release Notify file descriptor" }, - { "notify", "<on/off>", cmd_notify, "Notify attribute value", - mode_generator }, - { "register-application", "[UUID ...]", cmd_register_app, - "Register profile to connect" }, - { "unregister-application", NULL, cmd_unregister_app, - "Unregister profile" }, - { "register-service", "<UUID>", cmd_register_service, - "Register application service." }, - { "unregister-service", "<UUID/object>", cmd_unregister_service, - "Unregister application service" }, - { "register-characteristic", "<UUID> <Flags=read,write,notify...>", - cmd_register_characteristic, - "Register application characteristic" }, - { "unregister-characteristic", "<UUID/object>", - cmd_unregister_characteristic, - "Unregister application characteristic" }, - { "register-descriptor", "<UUID> <Flags=read,write...>", - cmd_register_descriptor, - "Register application descriptor" }, - { "unregister-descriptor", "<UUID/object>", - cmd_unregister_descriptor, - "Unregister application descriptor" }, { } }, };
@@ -2545,6 +2551,7 @@ int main(int argc, char *argv[])bt_shell_set_menu (&main_menu);bt_shell_add_submenu (&advertise_menu);bt_shell_add_submenu (&scan_menu); +bt_shell_add_submenu (&gatt_menu);bt_shell_set_prompt (PROMPT_OFF ); dbus_conn =g_dbus_setup_bus (DBUS_BUS_SYSTEM ,NULL ,NULL );
Signature · 1 line
2.13.6
Hi,
Quoted 678 lines · Luiz Augusto von Dentz
From: Luiz Augusto von Dentz <[email protected]>
This add initial
bt_shellhelper which can be used to create shell-like command line tools.
---
v3: Add submenu changesMakefile.tools | 3 +- src/shared/shell.c | 570 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/shared/shell.h | 67 +++++++ 3 files changed, 639 insertions(+), 1 deletion(-) create mode 100644 src/shared/shell.c create mode 100644 src/shared/shell.hMakefile.tools +2
-1@@ -8,7 +8,8 @@ client_bluetoothctl_SOURCES = client/main.c \ client/advertising.h \ client/advertising.c \ client/gatt.h client/gatt.c \ - monitor/uuid.h monitor/uuid.c + monitor/uuid.h monitor/uuid.c \ + src/shared/shell.h src/shared/shell.c client_bluetoothctl_LDADD = gdbus/libgdbus-internal.la src/libshared-glib.la \ @GLIB_LIBS@ @DBUS_LIBS@ -lreadline endifsrc/shared/shell.c new file +570
-0@@ -0,0 +1,570 @@ +/* +* +* BlueZ - Bluetooth protocol stack for Linux +* +* Copyright (C) 2017 Intel Corporation. All rights reserved. +* +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library 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 +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; 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 <stdio.h> +#include <errno.h> +#include <unistd.h> +#include <stdlib.h> +#include <stdbool.h> +#include <signal.h> +#include <sys/signalfd.h> + +#include <readline/readline.h> +#include <readline/history.h> +#include <glib.h> + +#include "src/shared/io.h" +#include "src/shared/util.h" +#include "src/shared/queue.h" +#include "src/shared/shell.h" + +#define CMD_LENGTH 48 +#define print_text(color, fmt, args...) \ + printf(color fmt COLOR_OFF "\n", ## args) +#define print_menu (cmd, args, desc) \ + printf(COLOR_HIGHLIGHT "%s %-*s " COLOR_OFF "%s\n", \ + cmd, (int)(CMD_LENGTH - strlen(cmd)), args, desc) + +static GMainLoop *main_loop; +static gboolean option_version = FALSE; + +static struct { +struct io *input; + +bool saved_prompt; +bt_shell_prompt_input_func saved_func; +void *saved_user_data; + +const struct bt_shell_menu_entry *menu; +/* TODO: Add submenus support */ +} data; + +static void shell_print_menu (void ); + +static void cmd_version (const char *arg) +{ +bt_shell_printf ("Version %s\n" ,VERSION ); +} + +static void cmd_quit (const char *arg) +{ +g_main_loop_quit (main_loop); +} + +static void cmd_help (const char *arg) +{ +shell_print_menu (); +} + +static const struct bt_shell_menu_entry default_menu[] = { + {"version" ,NULL , cmd_version,"Display version" }, + {"quit" ,NULL , cmd_quit,"Quit program" }, + {"exit" ,NULL , cmd_quit,"Quit program" }, + {"help" ,NULL , cmd_help, +"Display help about this program" }, + { } +}; + +static void shell_print_menu (void ) +{ +const struct bt_shell_menu_entry *entry; + +if (!data.menu) +return ; + +print_text (COLOR_HIGHLIGHT ,"Available commands:" ); +print_text (COLOR_HIGHLIGHT ,"-------------------" ); +for (entry = data.menu; entry->cmd; entry++) { +print_menu (entry->cmd, entry->arg ? :"" , entry->desc ? :"" ); + } + +for (entry = default_menu; entry->cmd; entry++) { +print_menu (entry->cmd, entry->arg ? :"" , entry->desc ? :"" ); + } +} + +static void shell_exec (const char *cmd,const char *arg) +{ +const struct bt_shell_menu_entry *entry; + +if (!data.menu || !cmd) +return ; + +for (entry = data.menu; entry->cmd; entry++) { +if (strcmp (cmd, entry->cmd)) +continue ; + +if (entry->func) { + entry->func (arg); +return ; + } + } + +for (entry = default_menu; entry->cmd; entry++) { +if (strcmp (cmd, entry->cmd)) +continue ; + +if (entry->func) { + entry->func (arg); +return ; + } + } + +print_text (COLOR_HIGHLIGHT ,"Invalid command" ); +} + +void bt_shell_printf (const char *fmt, ...) +{ +va_list args; +bool save_input; +char *saved_line; +int saved_point; + + save_input = !RL_ISSTATE (RL_STATE_DONE ); + +if (save_input) { + saved_point = rl_point; + saved_line =rl_copy_text (0 , rl_end); +if (!data.saved_prompt) { +rl_save_prompt (); +rl_replace_line ("" ,0 ); +rl_redisplay (); + } + } + +va_start (args, fmt); +vprintf (fmt, args); +va_end (args); + +if (save_input) { +if (!data.saved_prompt) +rl_restore_prompt (); +rl_replace_line (saved_line,0 ); + rl_point = saved_point; +rl_forced_update_display (); +free (saved_line); + } +} + +void bt_shell_hexdump (const unsigned char *buf,size_t len) +{ +static const char hexdigits[] ="0123456789abcdef" ; +char str[68 ]; +size_t i; + +if (!len) +return ; + + str[0 ] =' ' ; + +for (i =0 ; i < len; i++) { + str[((i %16 ) *3 ) +1 ] =' ' ; + str[((i %16 ) *3 ) +2 ] = hexdigits[buf[i] >>4 ]; + str[((i %16 ) *3 ) +3 ] = hexdigits[buf[i] &0xf ]; + str[(i %16 ) +51 ] =isprint (buf[i]) ? buf[i] :'.' ; + +if ((i +1 ) %16 ==0 ) { + str[49 ] =' ' ; + str[50 ] =' ' ; + str[67 ] ='\0' ; +bt_shell_printf ("%s\n" , str); + str[0 ] =' ' ; + } + } + +if (i %16 >0 ) { +size_t j; +for (j = (i %16 ); j <16 ; j++) { + str[(j *3 ) +1 ] =' ' ; + str[(j *3 ) +2 ] =' ' ; + str[(j *3 ) +3 ] =' ' ; + str[j +51 ] =' ' ; + } + str[49 ] =' ' ; + str[50 ] =' ' ; + str[67 ] ='\0' ; +bt_shell_printf ("%s\n" , str); + } +} + +void bt_shell_prompt_input (const char *label,const char *msg, +bt_shell_prompt_input_func func,void *user_data) +{ +/* Normal use should not prompt for user input to the value a second +* time before it releases the prompt, but we take a safe action. */ +if (data.saved_prompt) +return ; + +rl_save_prompt (); +rl_message (COLOR_RED "[%s]" COLOR_OFF " %s " , label, msg); + + data.saved_prompt = true; + data.saved_func = func; + data.saved_user_data = user_data; +} + +int bt_shell_release_prompt (const char *input) +{ +bt_shell_prompt_input_func func; +void *user_data; + +if (!data.saved_prompt) +return -1 ; + + data.saved_prompt = false; + +rl_restore_prompt (); + + func = data.saved_func; + user_data = data.saved_user_data; + + data.saved_func =NULL ; + data.saved_user_data =NULL ; + +func (input, user_data); + +return 0 ; +} + +static void rl_handler (char *input) +{ +char *cmd, *arg; + +if (!input) { +rl_insert_text ("quit" ); +rl_redisplay (); +rl_crlf (); +g_main_loop_quit (main_loop); +return ; + } + +if (!strlen (input)) + goto done; + +if (!bt_shell_release_prompt (input)) + goto done; + +if (history_search (input,-1 )) +add_history (input); + + cmd =strtok_r (input," " , &arg); +if (!cmd) + goto done; + +if (arg) { +int len =strlen (arg); +if (len >0 && arg[len -1 ] ==' ' ) + arg[len -1 ] ='\0' ; + } + +shell_exec (cmd, arg); +done: +free (input); +} + +static char *cmd_generator (const char *text,int state) +{ +static const struct bt_shell_menu_entry *entry; +static int index, len; +const char *cmd; + +if (!state) { + entry = default_menu; + index =0 ; + len =strlen (text); + } + +while ((cmd = entry[index].cmd)) { + index++; + +if (!strncmp (cmd, text, len)) +return strdup (cmd); + } + +if (state) +return NULL ; + + entry = data.menu; + index =0 ; + +return cmd_generator (text,1 ); +} + +static char **menu_completion (const struct bt_shell_menu_entry *entry, +const char *text,char *input_cmd) +{ +char **matches =NULL ; + +for (entry = data.menu; entry->cmd; entry++) { +if (strcmp (entry->cmd, input_cmd)) +continue ; + +if (!entry->gen) +continue ; + + rl_completion_display_matches_hook = entry->disp; + matches =rl_completion_matches (text, entry->gen); +break ; + } + +return matches; +} + +static char **shell_completion (const char *text,int start,int end) +{ +char **matches =NULL ; + +if (!data.menu) +return NULL ; + +if (start >0 ) { +char *input_cmd; + + input_cmd =strndup (rl_line_buffer, start -1 ); + matches =menu_completion (default_menu, text, input_cmd); +if (!matches) + matches =menu_completion (data.menu, text, + input_cmd); + +free (input_cmd); + }else { + rl_completion_display_matches_hook =NULL ; + matches =rl_completion_matches (text, cmd_generator); + } + +if (!matches) + rl_attempted_completion_over =1 ; + +return matches; +} + +static bool io_hup (struct io *io,void *user_data) +{ +g_main_loop_quit (main_loop); + +return false; +} + +static bool signal_read (struct io *io,void *user_data) +{ +static bool terminated = false; +struct signalfd_siginfo si; +ssize_t result; +int fd; + + fd =io_get_fd (io); + + result =read (fd, &si,sizeof (si)); +if (result !=sizeof (si)) +return false; + +switch (si.ssi_signo) { +case SIGINT : +if (data.input) { +rl_replace_line ("" ,0 ); +rl_crlf (); +rl_on_new_line (); +rl_redisplay (); +break ; + } + +/* +* If input was not yet setup up that means signal was received +* while daemon was not yet running. Since user is not able +* to terminate client by CTRL-D or typing exit treat this as +* exit and fall through. +*/ + +/* fall through */ +case SIGTERM : +if (!terminated) { +rl_replace_line ("" ,0 ); +rl_crlf (); +g_main_loop_quit (main_loop); + } + + terminated = true; +break ; + } + +return false; +} + +static struct io *setup_signalfd (void ) +{ +struct io *io; +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 ; + } + + io =io_new (fd); + +io_set_close_on_destroy (io, true); +io_set_read_handler (io, signal_read,NULL ,NULL ); +io_set_disconnect_handler (io, io_hup,NULL ,NULL ); + +return io; +} + +static GOptionEntry options[] = { + {"version" ,'v' ,0 ,G_OPTION_ARG_NONE , &option_version, +"Show version information and exit" }, + {NULL }, +}; + +static void rl_init (void ) +{ +setlinebuf (stdout); + rl_attempted_completion_function = shell_completion; + + rl_erase_empty_line =1 ; +rl_callback_handler_install (NULL , rl_handler); +} + +void bt_shell_init (int *argc,char ***argv) +{ +GOptionContext *context; +GError *error =NULL ; + + context =g_option_context_new (NULL ); +g_option_context_add_main_entries (context, options,NULL ); + +if (g_option_context_parse (context, argc, argv, &error) == FALSE) { +if (error !=NULL ) { +g_printerr ("%s\n" , error->message); +g_error_free (error); + }else +g_printerr ("An unknown error occurred\n" ); +exit (1 ); + } + +g_option_context_free (context); + +if (option_version == TRUE) { +g_print ("%s\n" ,VERSION ); +exit (EXIT_SUCCESS ); + } + + main_loop =g_main_loop_new (NULL , FALSE); + +rl_init (); +} + +static void rl_cleanup (void ) +{ +rl_message ("" ); +rl_callback_handler_remove (); +} + +void bt_shell_run (void ) +{ +struct io *signal; + + signal =setup_signalfd (); + +g_main_loop_run (main_loop); + +bt_shell_release_prompt ("" ); +bt_shell_detach (); + +io_destroy (signal); + +g_main_loop_unref (main_loop); + main_loop =NULL ; + +rl_cleanup (); +} + +bool bt_shell_set_menu (const struct bt_shell_menu_entry *menu) +{ +if (data.menu || !menu) +return false; + + data.menu = menu; + +return true; +} + +void bt_shell_set_prompt (const char *string) +{ +if (!main_loop) +return ; + +rl_set_prompt (string); +printf ("\r" ); +rl_on_new_line (); +rl_redisplay (); +} + +static bool input_read (struct io *io,void *user_data) +{ +rl_callback_read_char (); +return true; +} + +bool bt_shell_attach (int fd) +{ +struct io *io; + +/* TODO: Allow more than one input? */ +if (data.input) +return false; + + io =io_new (fd); + +io_set_read_handler (io, input_read,NULL ,NULL ); +io_set_disconnect_handler (io, io_hup,NULL ,NULL ); + + data.input = io; + +return true; +} + +bool bt_shell_detach (void ) +{ +if (!data.input) +return false; + +io_destroy (data.input); + data.input =NULL ; + +return true; +}src/shared/shell.h new file +67
-1@@ -0,0 +1,67 @@ +/* +* +* BlueZ - Bluetooth protocol stack for Linux +* +* Copyright (C) 2017 Intel Corporation. All rights reserved. +* +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library 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 +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, write to the Free Software +* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +* +*/ + +#define COLOR_OFF "\x1B[0m" +#define COLOR_RED "\x1B[0;91m" +#define COLOR_GREEN "\x1B[0;92m" +#define COLOR_YELLOW "\x1B[0;93m" +#define COLOR_BLUE "\x1B[0;94m" +#define COLOR_BOLDGRAY "\x1B[1;30m" +#define COLOR_BOLDWHITE "\x1B[1;37m" +#define COLOR_HIGHLIGHT "\x1B[1;39m" + +typedef void (*bt_shell_menu_cb_t )(const char *arg); +typedef char * (*bt_shell_menu_gen_t )(const char *text,int state); +typedef void (*bt_shell_menu_disp_t ) (char **matches,int num_matches, +int max_length); +typedef void (*bt_shell_prompt_input_func ) (const char *input,void *user_data); + +struct bt_shell_menu_entry { +const char *cmd; +const char *arg; +bt_shell_menu_cb_t func; +const char *desc; +bt_shell_menu_gen_t gen; +bt_shell_menu_disp_t disp; +}; + +void bt_shell_init (int *argc,char ***argv); + +void bt_shell_run (void ); + +bool bt_shell_set_menu (const struct bt_shell_menu_entry *menu); + +void bt_shell_set_prompt (const char *string); + +void bt_shell_printf (const char *fmt, + ...) __attribute__((format (printf,1 ,2 ))); +void bt_shell_hexdump (const unsigned char *buf,size_t len); + +void bt_shell_prompt_input (const char *label,const char *msg, +bt_shell_prompt_input_func func,void *user_data); +int bt_shell_release_prompt (const char *input); + +bool bt_shell_attach (int fd); +bool bt_shell_detach (void ); + +void bt_shell_cleanup (void ); --
Quoted 1 line
2.13.6
Applied.
Signature · 1 line
Luiz Augusto von Dentz