2017-08-25 08:28:19

by ERAMOTO Masaya

[permalink] [raw]
Subject: [PATCH BlueZ 1/2] client: Use the common argument generator

---
client/main.c | 27 +++++++++------------------
1 file changed, 9 insertions(+), 18 deletions(-)

diff --git a/client/main.c b/client/main.c
index 75696c2..34fcbd7 100644
--- a/client/main.c
+++ b/client/main.c
@@ -2234,7 +2234,8 @@ static char *attribute_generator(const char *text, int state)
return gatt_attribute_generator(text, state);
}

-static char *capability_generator(const char *text, int state)
+static char *argument_generator(const char *text, int state,
+ const char * const *args_list)
{
static int index, len;
const char *arg;
@@ -2244,7 +2245,7 @@ static char *capability_generator(const char *text, int state)
len = strlen(text);
}

- while ((arg = agent_arguments[index])) {
+ while ((arg = args_list[index])) {
index++;

if (!strncmp(arg, text, len))
@@ -2254,6 +2255,11 @@ static char *capability_generator(const char *text, int state)
return NULL;
}

+static char *capability_generator(const char *text, int state)
+{
+ return argument_generator(text, state, agent_arguments);
+}
+
static gboolean parse_argument_advertise(const char *arg, dbus_bool_t *value,
const char **type)
{
@@ -2308,22 +2314,7 @@ static void cmd_advertise(const char *arg)

static char *ad_generator(const char *text, int state)
{
- static int index, len;
- const char *arg;
-
- if (!state) {
- index = 0;
- len = strlen(text);
- }
-
- while ((arg = ad_arguments[index])) {
- index++;
-
- if (!strncmp(arg, text, len))
- return strdup(arg);
- }
-
- return NULL;
+ return argument_generator(text, state, ad_arguments);
}

static void cmd_set_advertise_uuids(const char *arg)
--
2.7.4



2017-08-28 11:27:05

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: Re: [PATCH BlueZ 1/2] client: Use the common argument generator

Hi Eramoto,

On Fri, Aug 25, 2017 at 11:28 AM, ERAMOTO Masaya
<[email protected]> wrote:
> ---
> client/main.c | 27 +++++++++------------------
> 1 file changed, 9 insertions(+), 18 deletions(-)
>
> diff --git a/client/main.c b/client/main.c
> index 75696c2..34fcbd7 100644
> --- a/client/main.c
> +++ b/client/main.c
> @@ -2234,7 +2234,8 @@ static char *attribute_generator(const char *text, int state)
> return gatt_attribute_generator(text, state);
> }
>
> -static char *capability_generator(const char *text, int state)
> +static char *argument_generator(const char *text, int state,
> + const char * const *args_list)
> {
> static int index, len;
> const char *arg;
> @@ -2244,7 +2245,7 @@ static char *capability_generator(const char *text, int state)
> len = strlen(text);
> }
>
> - while ((arg = agent_arguments[index])) {
> + while ((arg = args_list[index])) {
> index++;
>
> if (!strncmp(arg, text, len))
> @@ -2254,6 +2255,11 @@ static char *capability_generator(const char *text, int state)
> return NULL;
> }
>
> +static char *capability_generator(const char *text, int state)
> +{
> + return argument_generator(text, state, agent_arguments);
> +}
> +
> static gboolean parse_argument_advertise(const char *arg, dbus_bool_t *value,
> const char **type)
> {
> @@ -2308,22 +2314,7 @@ static void cmd_advertise(const char *arg)
>
> static char *ad_generator(const char *text, int state)
> {
> - static int index, len;
> - const char *arg;
> -
> - if (!state) {
> - index = 0;
> - len = strlen(text);
> - }
> -
> - while ((arg = ad_arguments[index])) {
> - index++;
> -
> - if (!strncmp(arg, text, len))
> - return strdup(arg);
> - }
> -
> - return NULL;
> + return argument_generator(text, state, ad_arguments);
> }
>
> static void cmd_set_advertise_uuids(const char *arg)
> --
> 2.7.4

Applied, thanks.

--
Luiz Augusto von Dentz

2017-08-25 08:35:47

by ERAMOTO Masaya

[permalink] [raw]
Subject: [PATCH BlueZ 2/2] client: Add autocompletion for on/off

For some commands bluetoothctl gives the hint of argument, which is "on"
and "off", it is convenient even if the argument of command was overlooked
in help message.
---
client/main.c | 32 +++++++++++++++++++++++++-------
1 file changed, 25 insertions(+), 7 deletions(-)

diff --git a/client/main.c b/client/main.c
index 34fcbd7..825647d 100644
--- a/client/main.c
+++ b/client/main.c
@@ -73,6 +73,12 @@ static GList *ctrl_list;

static guint input = 0;

+static const char * const mode_arguments[] = {
+ "on",
+ "off",
+ NULL
+};
+
static const char * const agent_arguments[] = {
"on",
"off",
@@ -2255,6 +2261,11 @@ static char *argument_generator(const char *text, int state,
return NULL;
}

+static char *mode_generator(const char *text, int state)
+{
+ return argument_generator(text, state, mode_arguments);
+}
+
static char *capability_generator(const char *text, int state)
{
return argument_generator(text, state, agent_arguments);
@@ -2421,11 +2432,14 @@ static const struct {
"Set controller alias" },
{ "reset-alias", NULL, cmd_reset_alias,
"Reset controller alias" },
- { "power", "<on/off>", cmd_power, "Set controller power" },
+ { "power", "<on/off>", cmd_power, "Set controller power",
+ mode_generator },
{ "pairable", "<on/off>", cmd_pairable,
- "Set controller pairable mode" },
+ "Set controller pairable mode",
+ mode_generator },
{ "discoverable", "<on/off>", cmd_discoverable,
- "Set controller discoverable mode" },
+ "Set controller discoverable mode",
+ mode_generator },
{ "agent", "<on/off/capability>", cmd_agent,
"Enable/disable agent with given capability",
capability_generator},
@@ -2444,7 +2458,8 @@ static const struct {
"Set advertise manufacturer data" },
{ "set-advertise-tx-power", "<on/off>",
cmd_set_advertise_tx_power,
- "Enable/disable TX power to be advertised" },
+ "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,
@@ -2459,10 +2474,12 @@ static const struct {
{ "set-scan-filter-transport", "[transport]",
cmd_set_scan_filter_transport, "Set scan filter transport" },
{ "set-scan-filter-reset-data", "[on/off]",
- cmd_set_scan_filter_reset_data, "Set scan filter reset data" },
+ cmd_set_scan_filter_reset_data, "Set scan filter reset data",
+ mode_generator },
{ "set-scan-filter-clear", "", cmd_set_scan_filter_clear,
"Clears discovery filter." },
- { "scan", "<on/off>", cmd_scan, "Scan for devices" },
+ { "scan", "<on/off>", cmd_scan, "Scan for devices",
+ mode_generator },
{ "info", "[dev]", cmd_info, "Device information",
dev_generator },
{ "pair", "[dev]", cmd_pair, "Pair with device",
@@ -2499,7 +2516,8 @@ static const struct {
"Acquire Notify file descriptor" },
{ "release-notify", NULL, cmd_release_notify,
"Release Notify file descriptor" },
- { "notify", "<on/off>", cmd_notify, "Notify attribute value" },
+ { "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,
--
2.7.4