client: Rename set-uuids to uuids
Luiz Augusto von Dentz
maintainerPATCH 1/8 client: Rename set-uuids to uuids
From: Luiz Augusto von Dentz <[email protected]>
Make the command return the current value if no parameters:
[bluetooth]# uuids 0x1820
[bluetooth]# uuids
UUID: Internet Protocol Support (0x1820
--- client/advertising.c | 39 ++++++++++++++++++++++++++++++++++++--- client/main.c | 6 +++--- 2 files changed, 39 insertions(+), 6 deletions(-)
client/advertising.c +36 -3
@@ -32,6 +32,7 @@#include <string.h> #include "gdbus/gdbus.h" +#include "src/shared/util.h" #include "src/shared/shell.h" #include "advertising.h"
@@ -434,15 +435,47 @@ void ad_unregister(DBusConnection *conn, GDBusProxy *manager) } } +static void print_uuid (const char *uuid) +{ +const char *text; + + text =bt_uuidstr_to_str (uuid); +if (text) { +char str[26 ]; +unsigned int n; + + str[sizeof (str) -1 ] ='\0' ; + + n =snprintf (str,sizeof (str),"%s" , text); +if (n >sizeof (str) -1 ) { + str[sizeof (str) -2 ] ='.' ; + str[sizeof (str) -3 ] ='.' ; +if (str[sizeof (str) -4 ] ==' ' ) + str[sizeof (str) -4 ] ='.' ; + + n =sizeof (str) -1 ; + } + +bt_shell_printf ("\tUUID: %s(%s)\n" , str, uuid); + }else +bt_shell_printf ("\tUUID: (%s)\n" , uuid); +} +void ad_advertise_uuids (DBusConnection *conn,int argc,char *argv[]) { +if (argc <2 || !strlen (argv[1 ])) { +char **uuid; + +for (uuid = ad.uuids; uuid && *uuid; uuid++) +print_uuid (*uuid); + +return ; + } +g_strfreev (ad.uuids); ad.uuids =NULL ; ad.uuids_len =0 ; -if (argc <2 || !strlen (argv[1 ])) -return ; - ad.uuids =g_strdupv (&argv[1 ]);if (!ad.uuids) {bt_shell_printf ("Failed to parse input\n" );
client/main.c +3 -3
@@ -2191,7 +2191,7 @@ static char *ad_generator(const char *text, int state)return argument_generator (text, state, ad_arguments); } -static void cmd_set_advertise_uuids (int argc,char *argv[]) +static void cmd_advertise_uuids (int argc,char *argv[]) {ad_advertise_uuids (dbus_conn, argc, argv); }
@@ -2287,8 +2287,8 @@ static const struct bt_shell_menu advertise_menu = { .name = "advertise", .desc = "Advertise Options Submenu", .entries = { - {"set-uuids" ,"[uuid1 uuid2 ...]" , - cmd_set_advertise_uuids, "Set advertise uuids" }, + {"uuids" ,"[uuid1 uuid2 ...]" , cmd_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 ...]",
Signature · 1 line
2.14.3
Luiz Augusto von Dentz
maintainerPATCH 1/1 shared/shell: Introduce bt_shell_{get,set}_env
From: Luiz Augusto von Dentz <[email protected]>
These function can be used to share environment variable accross different files.
--- src/shared/shell.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/shared/shell.h | 3 +++ 2 files changed, 66 insertions(+)
src/shared/shell.c +63 -0
@@ -56,6 +56,11 @@static GMainLoop *main_loop; +struct bt_shell_env { +char *name; +void *value; +}; + static struct { struct io *input;
@@ -66,6 +71,8 @@ static struct {const struct bt_shell_menu *menu;const struct bt_shell_menu *main;struct queue *submenus; + +struct queue *envs; } data;static void shell_print_menu (void );
@@ -759,6 +766,14 @@ static void rl_cleanup(void)rl_callback_handler_remove (); } +static void env_destroy (void *data) +{ +struct bt_shell_env *env = data; + +free (env->name); +free (env); +} +void bt_shell_run (void ) {struct io *signal;
@@ -775,6 +790,11 @@ void bt_shell_run(void)g_main_loop_unref (main_loop); main_loop =NULL ; +if (data.envs) { +queue_destroy (data.envs, env_destroy); + data.envs =NULL ; + } +rl_cleanup (); }
@@ -849,3 +869,46 @@ bool bt_shell_detach(void)return true; } + +static bool match_env (const void *data,const void *user_data) +{ +const struct bt_shell_env *env = data; +const char *name = user_data; + +return !strcmp (env->name, name); +} + +void bt_shell_set_env (const char *name,void *value) +{ +struct bt_shell_env *env; + +if (!data.envs) { + data.envs =queue_new (); + goto done; + } + + env =queue_remove_if (data.envs, match_env, (void *) name); +if (env) +env_destroy (env); + +done: + env = new0(struct bt_shell_env, 1); + env->name =strdup (name); + env->value = value; + +queue_push_tail (data.envs, env); +} + +void *bt_shell_get_env (const char *name) +{ +const struct bt_shell_env *env; + +if (!data.envs) +return NULL ; + + env =queue_find (data.envs, match_env, name); +if (!env) +return NULL ; + +return env->name; +}
src/shared/shell.h +3 -0
@@ -87,4 +87,7 @@ int bt_shell_release_prompt(const char *input);bool bt_shell_attach (int fd);bool bt_shell_detach (void ); +void bt_shell_set_env (const char *name,void *value); +void *bt_shell_get_env (const char *name); +void bt_shell_cleanup (void );
Signature · 1 line
2.14.3
Luiz Augusto von Dentz
maintainerPATCH 2/8 client: Rename set-service to service
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
Make the command return the current value if no parameters:
[bluetooth]# service 0x1820 00 00 00
[bluetooth]# service
UUID: Internet Protocol Support (0x1820) 00 00 00 ... --- client/advertising.c | 9 +++++---- client/main.c | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-)
client/advertising.c +5 -4
@@ -498,15 +498,16 @@ void ad_advertise_service(DBusConnection *conn, int argc, char *argv[])unsigned int i;struct ad_data *data; -ad_clear_service (); - -if (argc <2 ) +if (argc <2 || !strlen (argv[1 ])) { +print_uuid (ad.service.uuid); +bt_shell_hexdump (ad.service.data.data, ad.service.data.len);return ; + } ad.service.uuid =g_strdup (argv[1 ]); data = &ad.service.data; -for (i =1 ; i < (unsigned int ) argc; i++) { +for (i =2 ; i < (unsigned int ) argc; i++) {long int val;char *endptr =NULL ;
client/main.c +2 -2
@@ -2196,7 +2196,7 @@ static void cmd_advertise_uuids(int argc, char *argv[])ad_advertise_uuids (dbus_conn, argc, argv); } -static void cmd_set_advertise_service (int argc,char *argv[]) +static void cmd_advertise_service (int argc,char *argv[]) {ad_advertise_service (dbus_conn, argc, argv); }
@@ -2289,7 +2289,7 @@ static const struct bt_shell_menu advertise_menu = { .entries = { {"uuids" ,"[uuid1 uuid2 ...]" , cmd_advertise_uuids, "Set advertise uuids" }, - { "set-service", "[uuid] [data=xx xx ...]", cmd_set_advertise_service, + { "service", "[uuid] [data=xx xx ...]", cmd_advertise_service, "Set advertise service data" }, { "set-manufacturer", "[id] [data=xx xx ...]", cmd_set_advertise_manufacturer,
Signature · 1 line
2.14.3
Luiz Augusto von Dentz
maintainerPATCH 3/8 client: Rename set-manufacturer to manufacturer
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
Make the command return the current value if no parameters:
[bluetooth]# manufacturer 2 00
[bluetooth]# manufacturer
Manufacturer: 2
00
--- client/advertising.c | 12 ++++++++++-- client/main.c | 6 +++--- 2 files changed, 13 insertions(+), 5 deletions(-)
client/advertising.c +10 -2
@@ -543,10 +543,18 @@ void ad_advertise_manufacturer(DBusConnection *conn, int argc, char *argv[])long int val;struct ad_data *data; -ad_clear_manufacturer (); +if (argc <2 || !strlen (argv[1 ])) { +if (ad.manufacturer.data.len) { +bt_shell_printf ("Manufacturer: %u\n" , + ad.manufacturer.id); +bt_shell_hexdump (ad.manufacturer.data.data, + ad.manufacturer.data.len); + } -if (argc <2 )return ; + } + +ad_clear_manufacturer (); val =strtol (argv[1 ], &endptr,0 );if (!endptr || *endptr !='\0' || val >UINT16_MAX ) {
client/main.c +3 -3
@@ -2201,7 +2201,7 @@ static void cmd_advertise_service(int argc, char *argv[])ad_advertise_service (dbus_conn, argc, argv); } -static void cmd_set_advertise_manufacturer (int argc,char *argv[]) +static void cmd_advertise_manufacturer (int argc,char *argv[]) {ad_advertise_manufacturer (dbus_conn, argc, argv); }
@@ -2291,8 +2291,8 @@ static const struct bt_shell_menu advertise_menu = { "Set advertise uuids" }, {"service" ,"[uuid] [data=xx xx ...]" , cmd_advertise_service, "Set advertise service data" }, - { "set-manufacturer", "[id] [data=xx xx ...]", - cmd_set_advertise_manufacturer, + { "manufacturer", "[id] [data=xx xx ...]", + cmd_advertise_manufacturer, "Set advertise manufacturer data" }, { "set-tx-power", "<on/off>", cmd_set_advertise_tx_power, "Enable/disable TX power to be advertised",
Signature · 1 line
2.14.3
Luiz Augusto von Dentz
maintainerPATCH 4/8 client: Rename set-tx-power to tx-power
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
Make the command return the current value if no parameters:
[bluetooth]# tx-power on
[bluetooth]# tx-power
Tx Power: on
--- client/advertising.c | 11 ++++++++--- client/advertising.h | 2 +- client/main.c | 11 ++++++++--- 3 files changed, 17 insertions(+), 7 deletions(-)
client/advertising.c +8 -3
@@ -587,12 +587,17 @@ void ad_advertise_manufacturer(DBusConnection *conn, int argc, char *argv[]) "ManufacturerData"); } -void ad_advertise_tx_power (DBusConnection *conn,bool value) +void ad_advertise_tx_power (DBusConnection *conn,dbus_bool_t *value) { -if (ad.tx_power == value) +if (!value) { +bt_shell_printf ("Tx Power: %s\n" , ad.tx_power ?"on" :"off" ); +return ; + } + +if (ad.tx_power == *value)return ; - ad.tx_power = value; + ad.tx_power = *value;g_dbus_emit_property_changed (conn,AD_PATH ,AD_IFACE ,"Includes" ); }
client/advertising.h +1 -1
@@ -27,7 +27,7 @@ void ad_unregister(DBusConnection *conn, GDBusProxy *manager);void ad_advertise_uuids (DBusConnection *conn,int argc,char *argv[]);void ad_advertise_service (DBusConnection *conn,int argc,char *argv[]);void ad_advertise_manufacturer (DBusConnection *conn,int argc,char *argv[]); -void ad_advertise_tx_power (DBusConnection *conn,bool value); +void ad_advertise_tx_power (DBusConnection *conn,dbus_bool_t *value);void ad_advertise_name (DBusConnection *conn,bool value);void ad_advertise_appearance (DBusConnection *conn,bool value);void ad_advertise_local_name (DBusConnection *conn,const char *name);
client/main.c +8 -3
@@ -2206,14 +2206,19 @@ static void cmd_advertise_manufacturer(int argc, char *argv[])ad_advertise_manufacturer (dbus_conn, argc, argv); } -static void cmd_set_advertise_tx_power (int argc,char *argv[]) +static void cmd_advertise_tx_power (int argc,char *argv[]) {dbus_bool_t powered; +if (argc <2 ) { +ad_advertise_tx_power (dbus_conn,NULL ); +return ; + } +if (!parse_argument (argc, argv,NULL ,NULL , &powered,NULL ))return ; -ad_advertise_tx_power (dbus_conn, powered); +ad_advertise_tx_power (dbus_conn, &powered); }static void cmd_set_advertise_name (int argc,char *argv[])
@@ -2294,7 +2299,7 @@ static const struct bt_shell_menu advertise_menu = { {"manufacturer" ,"[id] [data=xx xx ...]" , cmd_advertise_manufacturer, "Set advertise manufacturer data" }, - { "set-tx-power", "<on/off>", cmd_set_advertise_tx_power, + { "tx-power", "[on/off]", cmd_advertise_tx_power,"Enable/disable TX power to be advertised" , mode_generator }, { "set-name", "<on/off/name>", cmd_set_advertise_name,
Signature · 1 line
2.14.3
Luiz Augusto von Dentz
maintainerPATCH 5/8 client: Rename set-name to name
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
Make the command return the current value if no parameters:
[bluetooth]# name blah
[bluetooth]# name
LocalName: blah
--- client/advertising.c | 6 ++++++ client/main.c | 9 +++++++-- 2 files changed, 13 insertions(+), 2 deletions(-)
client/advertising.c +6 -0
@@ -619,6 +619,12 @@ void ad_advertise_name(DBusConnection *conn, bool value)void ad_advertise_local_name (DBusConnection *conn,const char *name) { +if (!name) { +if (ad.local_name) +bt_shell_printf ("LocalName: %s\n" , ad.local_name); +return ; + } +if (ad.local_name && !strcmp (name, ad.local_name))return ;
client/main.c +7 -2
@@ -2221,8 +2221,13 @@ static void cmd_advertise_tx_power(int argc, char *argv[])ad_advertise_tx_power (dbus_conn, &powered); } -static void cmd_set_advertise_name (int argc,char *argv[]) +static void cmd_advertise_name (int argc,char *argv[]) { +if (argc <2 ) { +ad_advertise_local_name (dbus_conn,NULL ); +return ; + } +if (strcmp (argv[1 ],"on" ) ==0 ||strcmp (argv[1 ],"yes" ) ==0 ) {ad_advertise_name (dbus_conn, true);return ;
@@ -2302,7 +2307,7 @@ static const struct bt_shell_menu advertise_menu = { {"tx-power" ,"[on/off]" , cmd_advertise_tx_power,"Enable/disable TX power to be advertised" , mode_generator }, - { "set-name", "<on/off/name>", cmd_set_advertise_name, + { "name", "[on/off/name]", cmd_advertise_name, "Enable/disable local name to be advertised" }, { "set-appearance", "<value>", cmd_set_advertise_appearance, "Set custom appearance to be advertised" },
Signature · 1 line
2.14.3
Luiz Augusto von Dentz
maintainerPATCH 6/8 client: Rename set-appearance to appearance
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
Make the command return the current value if no parameters:
[bluetooth]# appearance 0x0001
[bluetooth]# appearance
Appearance: Unknown (0x0001)
--- client/advertising.c | 18 +++++++++++++++--- client/advertising.h | 2 +- client/main.c | 11 ++++++++--- 3 files changed, 24 insertions(+), 7 deletions(-)
client/advertising.c +15 -3
@@ -647,12 +647,24 @@ void ad_advertise_appearance(DBusConnection *conn, bool value)g_dbus_emit_property_changed (conn,AD_PATH ,AD_IFACE ,"Includes" ); } -void ad_advertise_local_appearance (DBusConnection *conn,uint16_t value) +void ad_advertise_local_appearance (DBusConnection *conn,long int *value) { -if (ad.local_appearance == value) +if (!value) { +if (ad.local_appearance !=UINT16_MAX ) +bt_shell_printf ("Appearance: %s (0x%04x)\n" , +bt_appear_to_str (ad.local_appearance), + ad.local_appearance); +else +bt_shell_printf ("Apperance: %s\n" , + ad.appearance ?"on" :"off" ); + +return ; + } + +if (ad.local_appearance == *value)return ; - ad.local_appearance = value; + ad.local_appearance = *value;g_dbus_emit_property_changed (conn,AD_PATH ,AD_IFACE ,"Appearance" ); }
client/advertising.h +1 -1
@@ -31,6 +31,6 @@ void ad_advertise_tx_power(DBusConnection *conn, dbus_bool_t *value);void ad_advertise_name (DBusConnection *conn,bool value);void ad_advertise_appearance (DBusConnection *conn,bool value);void ad_advertise_local_name (DBusConnection *conn,const char *name); -void ad_advertise_local_appearance (DBusConnection *conn,uint16_t value); +void ad_advertise_local_appearance (DBusConnection *conn,long int *value);void ad_advertise_duration (DBusConnection *conn,uint16_t value);void ad_advertise_timeout (DBusConnection *conn,uint16_t value);
client/main.c +8 -3
@@ -2241,11 +2241,16 @@ static void cmd_advertise_name(int argc, char *argv[])ad_advertise_local_name (dbus_conn, argv[1 ]); } -static void cmd_set_advertise_appearance (int argc,char *argv[]) +static void cmd_advertise_appearance (int argc,char *argv[]) {long int value;char *endptr =NULL ; +if (argc <2 ) { +ad_advertise_local_appearance (dbus_conn,NULL ); +return ; + } +if (strcmp (argv[1 ],"on" ) ==0 ||strcmp (argv[1 ],"yes" ) ==0 ) {ad_advertise_appearance (dbus_conn, true);return ;
@@ -2262,7 +2267,7 @@ static void cmd_set_advertise_appearance(int argc, char *argv[])return ; } -ad_advertise_local_appearance (dbus_conn, value); +ad_advertise_local_appearance (dbus_conn, &value); }static void cmd_set_advertise_duration (int argc,char *argv[])
@@ -2309,7 +2314,7 @@ static const struct bt_shell_menu advertise_menu = { mode_generator }, {"name" ,"[on/off/name]" , cmd_advertise_name, "Enable/disable local name to be advertised" }, - { "set-appearance", "<value>", cmd_set_advertise_appearance, + { "appearance", "[value]", cmd_advertise_appearance, "Set custom appearance to be advertised" }, { "set-duration", "<seconds>", cmd_set_advertise_duration, "Set advertise duration" },
Signature · 1 line
2.14.3
Luiz Augusto von Dentz
maintainerPATCH 7/8 client: Rename set-duration to duration
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
Make the command return the current value if no parameters:
[bluetooth]# duration 1
[bluetooth]# duration
Duration: 1 sec
--- client/advertising.c | 11 ++++++++--- client/advertising.h | 2 +- client/main.c | 13 +++++++++---- 3 files changed, 18 insertions(+), 8 deletions(-)
client/advertising.c +8 -3
@@ -669,12 +669,17 @@ void ad_advertise_local_appearance(DBusConnection *conn, long int *value)g_dbus_emit_property_changed (conn,AD_PATH ,AD_IFACE ,"Appearance" ); } -void ad_advertise_duration (DBusConnection *conn,uint16_t value) +void ad_advertise_duration (DBusConnection *conn,long int *value) { -if (ad.duration == value) +if (!value) { +bt_shell_printf ("Duration: %u sec\n" , ad.duration); +return ; + } + +if (ad.duration == *value)return ; - ad.duration = value; + ad.duration = *value;g_dbus_emit_property_changed (conn,AD_PATH ,AD_IFACE ,"Duration" ); }
client/advertising.h +1 -1
@@ -32,5 +32,5 @@ void ad_advertise_name(DBusConnection *conn, bool value);void ad_advertise_appearance (DBusConnection *conn,bool value);void ad_advertise_local_name (DBusConnection *conn,const char *name);void ad_advertise_local_appearance (DBusConnection *conn,long int *value); -void ad_advertise_duration (DBusConnection *conn,uint16_t value); +void ad_advertise_duration (DBusConnection *conn,long int *value);void ad_advertise_timeout (DBusConnection *conn,uint16_t value);
client/main.c +9 -4
@@ -2270,18 +2270,23 @@ static void cmd_advertise_appearance(int argc, char *argv[])ad_advertise_local_appearance (dbus_conn, &value); } -static void cmd_set_advertise_duration (int argc,char *argv[]) +static void cmd_advertise_duration (int argc,char *argv[]) {long int value;char *endptr =NULL ; +if (argc <2 ) { +ad_advertise_duration (dbus_conn,NULL ); +return ; + } + value =strtol (argv[1 ], &endptr,0 );if (!endptr || *endptr !='\0' || value >UINT16_MAX ) {bt_shell_printf ("Invalid argument\n" );return ; } -ad_advertise_duration (dbus_conn, value); +ad_advertise_duration (dbus_conn, &value); }static void cmd_set_advertise_timeout (int argc,char *argv[])
@@ -2316,8 +2321,8 @@ static const struct bt_shell_menu advertise_menu = { "Enable/disable local name to be advertised" }, {"appearance" ,"[value]" , cmd_advertise_appearance, "Set custom appearance to be advertised" }, - { "set-duration", "<seconds>", cmd_set_advertise_duration, - "Set advertise duration" }, + { "duration", "[seconds]", cmd_advertise_duration, + "Set/Get advertise duration" }, { "set-timeout", "<seconds>", cmd_set_advertise_timeout, "Set advertise timeout" }, { } },
Signature · 1 line
2.14.3
Luiz Augusto von Dentz
maintainerPATCH 8/8 client: Rename set-timeout to timeout
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
Make the command return the current value if no parameters:
[bluetooth]# timeout 1
[bluetooth]# timeout
Timeout: 1 sec
--- client/advertising.c | 11 ++++++++--- client/advertising.h | 2 +- client/main.c | 13 +++++++++---- 3 files changed, 18 insertions(+), 8 deletions(-)
client/advertising.c +8 -3
@@ -684,12 +684,17 @@ void ad_advertise_duration(DBusConnection *conn, long int *value)g_dbus_emit_property_changed (conn,AD_PATH ,AD_IFACE ,"Duration" ); } -void ad_advertise_timeout (DBusConnection *conn,uint16_t value) +void ad_advertise_timeout (DBusConnection *conn,long int *value) { -if (ad.timeout == value) +if (!value) { +bt_shell_printf ("Timeout: %u sec\n" , ad.timeout); +return ; + } + +if (ad.timeout == *value)return ; - ad.timeout = value; + ad.timeout = *value;g_dbus_emit_property_changed (conn,AD_PATH ,AD_IFACE ,"Timeout" ); }
client/advertising.h +1 -1
@@ -33,4 +33,4 @@ void ad_advertise_appearance(DBusConnection *conn, bool value);void ad_advertise_local_name (DBusConnection *conn,const char *name);void ad_advertise_local_appearance (DBusConnection *conn,long int *value);void ad_advertise_duration (DBusConnection *conn,long int *value); -void ad_advertise_timeout (DBusConnection *conn,uint16_t value); +void ad_advertise_timeout (DBusConnection *conn,long int *value);
client/main.c +9 -4
@@ -2289,18 +2289,23 @@ static void cmd_advertise_duration(int argc, char *argv[])ad_advertise_duration (dbus_conn, &value); } -static void cmd_set_advertise_timeout (int argc,char *argv[]) +static void cmd_advertise_timeout (int argc,char *argv[]) {long int value;char *endptr =NULL ; +if (argc <2 ) { +ad_advertise_timeout (dbus_conn,NULL ); +return ; + } + value =strtol (argv[1 ], &endptr,0 );if (!endptr || *endptr !='\0' || value >UINT16_MAX ) {bt_shell_printf ("Invalid argument\n" );return ; } -ad_advertise_timeout (dbus_conn, value); +ad_advertise_timeout (dbus_conn, &value); }static const struct bt_shell_menu advertise_menu = {
@@ -2323,8 +2328,8 @@ static const struct bt_shell_menu advertise_menu = { "Set custom appearance to be advertised" }, {"duration" ,"[seconds]" , cmd_advertise_duration, "Set/Get advertise duration" }, - { "set-timeout", "<seconds>", cmd_set_advertise_timeout, - "Set advertise timeout" }, + { "timeout", "[seconds]", cmd_advertise_timeout, + "Set/Get advertise timeout" }, { } }, };
Signature · 1 line
2.14.3
Re: [PATCH BlueZ] shared/shell: Introduce bt_shell_{get,set}_env
In reply to Luiz Augusto von DentzHi Luiz,
Quoted 98 lines · Luiz Augusto von Dentz
From: Luiz Augusto von Dentz <[email protected]>
These function can be used to share environment variable accross different files.
--- src/shared/shell.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/shared/shell.h | 3 +++ 2 files changed, 66 insertions(+)src/shared/shell.c +53
-0@@ -56,6 +56,11 @@static GMainLoop *main_loop; +struct bt_shell_env { +char *name; +void *value; +}; + static struct { struct io *input;@@ -66,6 +71,8 @@ static struct {const struct bt_shell_menu *menu;const struct bt_shell_menu *main;struct queue *submenus; + +struct queue *envs; } data;static void shell_print_menu (void );@@ -759,6 +766,14 @@ static void rl_cleanup(void)rl_callback_handler_remove (); } +static void env_destroy (void *data) +{ +struct bt_shell_env *env = data; + +free (env->name); +free (env); +} +void bt_shell_run (void ) {struct io *signal;@@ -775,6 +790,11 @@ void bt_shell_run(void)g_main_loop_unref (main_loop); main_loop =NULL ; +if (data.envs) { +queue_destroy (data.envs, env_destroy); + data.envs =NULL ; + } +rl_cleanup (); }@@ -849,3 +869,46 @@ bool bt_shell_detach(void)return true; } + +static bool match_env (const void *data,const void *user_data) +{ +const struct bt_shell_env *env = data; +const char *name = user_data; + +return !strcmp (env->name, name); +} + +void bt_shell_set_env (const char *name,void *value) +{ +struct bt_shell_env *env; + +if (!data.envs) { + data.envs =queue_new (); + goto done; + } + + env =queue_remove_if (data.envs, match_env, (void *) name); +if (env) +env_destroy (env); + +done: + env = new0(struct bt_shell_env, 1); + env->name =strdup (name); + env->value = value; + +queue_push_tail (data.envs, env); +} + +void *bt_shell_get_env (const char *name) +{ +const struct bt_shell_env *env;
src/shared/shell.c bool bt_shell_detach(void) · line 914
Quoted 10 lines
src/shared/shell.c +10
-0+ +if (!data.envs) +return NULL ; + + env =queue_find (data.envs, match_env, name); +if (!env) +return NULL ; + +return env->name; +}
I think that it is better to return the env or the env->value so that we can use the env->value in a caller, otherwise this function shall return boolean type because it checks if the passed name exist.
Regards,
Eramoto
Quoted 13 lines
src/shared/shell.h +3
-0@@ -87,4 +87,7 @@ int bt_shell_release_prompt(const char *input);bool bt_shell_attach (int fd);bool bt_shell_detach (void ); +void bt_shell_set_env (const char *name,void *value); +void *bt_shell_get_env (const char *name); +void bt_shell_cleanup (void );
Hi,
Quoted 100 lines · Luiz Augusto von Dentz
From: Luiz Augusto von Dentz <[email protected]>
Make the command return the current value if no parameters:
[bluetooth]# uuids
0x1820
[bluetooth]# uuids
UUID: Internet Protocol Support (0x1820--- client/advertising.c | 39 ++++++++++++++++++++++++++++++++++++--- client/main.c | 6 +++--- 2 files changed, 39 insertions(+), 6 deletions(-)client/advertising.c +36
-3@@ -32,6 +32,7 @@#include <string.h> #include "gdbus/gdbus.h" +#include "src/shared/util.h" #include "src/shared/shell.h" #include "advertising.h" @@ -434,15 +435,47 @@ void ad_unregister(DBusConnection *conn, GDBusProxy *manager) } } +static void print_uuid (const char *uuid) +{ +const char *text; + + text =bt_uuidstr_to_str (uuid); +if (text) { +char str[26 ]; +unsigned int n; + + str[sizeof (str) -1 ] ='\0' ; + + n =snprintf (str,sizeof (str),"%s" , text); +if (n >sizeof (str) -1 ) { + str[sizeof (str) -2 ] ='.' ; + str[sizeof (str) -3 ] ='.' ; +if (str[sizeof (str) -4 ] ==' ' ) + str[sizeof (str) -4 ] ='.' ; + + n =sizeof (str) -1 ; + } + +bt_shell_printf ("\tUUID: %s(%s)\n" , str, uuid); + }else +bt_shell_printf ("\tUUID: (%s)\n" , uuid); +} +void ad_advertise_uuids (DBusConnection *conn,int argc,char *argv[]) { +if (argc <2 || !strlen (argv[1 ])) { +char **uuid; + +for (uuid = ad.uuids; uuid && *uuid; uuid++) +print_uuid (*uuid); + +return ; + } +g_strfreev (ad.uuids); ad.uuids =NULL ; ad.uuids_len =0 ; -if (argc <2 || !strlen (argv[1 ])) -return ; - ad.uuids =g_strdupv (&argv[1 ]);if (!ad.uuids) {bt_shell_printf ("Failed to parse input\n" );client/main.c +3
-4@@ -2191,7 +2191,7 @@ static char *ad_generator(const char *text, int state)return argument_generator (text, state, ad_arguments); } -static void cmd_set_advertise_uuids (int argc,char *argv[]) +static void cmd_advertise_uuids (int argc,char *argv[]) {ad_advertise_uuids (dbus_conn, argc, argv); }@@ -2287,8 +2287,8 @@ static const struct bt_shell_menu advertise_menu = { .name = "advertise", .desc = "Advertise Options Submenu", .entries = { - {"set-uuids" ,"[uuid1 uuid2 ...]" , - cmd_set_advertise_uuids, "Set advertise uuids" }, + {"uuids" ,"[uuid1 uuid2 ...]" , cmd_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 ...]", --
Quoted 1 line
2.14.3
Applied.
Signature · 1 line
Luiz Augusto von Dentz