2017-12-22 07:29:53

by ERAMOTO Masaya

[permalink] [raw]
Subject: [PATCH BlueZ 0/8] Add generators for bluetooth-player/obexctl

This patch set adds generators for bluetooth-player and obexctl.
bluetooth-player and obexctl also use GList instead of GSList.

ERAMOTO Masaya (8):
gdbus: Make proxy_lookup() global
client: Use g_dbus_proxy_lookup()
tools/bluetooth-player: Use g_dbus_proxy_lookup() instead of find_*
tools/obexctl: Use g_dbus_proxy_lookup() instead of find_*
gdbus: Introduce g_dbus_proxy_path_lookup()
client: Use g_dbus_proxy_path_lookup()
tools/bluetooth-player: Add generator for player/item
tools/obexctl: Add generator for session/transfer

client/gatt.c | 56 ++---------
client/main.c | 15 +--
gdbus/client.c | 32 +++++-
gdbus/gdbus.h | 4 +
tools/bluetooth-player.c | 132 ++++++++++++-------------
tools/obexctl.c | 253 ++++++++++++++++++-----------------------------
6 files changed, 206 insertions(+), 286 deletions(-)

--
2.14.1



2017-12-27 18:55:09

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: Re: [PATCH BlueZ 5/8] gdbus: Introduce g_dbus_proxy_path_lookup()

Hi Eramoto,

On Wed, Dec 27, 2017 at 4:51 PM, Luiz Augusto von Dentz
<[email protected]> wrote:
> Hi Eramoto,
>
> On Fri, Dec 22, 2017 at 5:35 AM, ERAMOTO Masaya
> <[email protected]> wrote:
>> g_dbus_proxy_path_lookup() returns the path of the proxy that matches the
>> passed text. It also returns the index of the proxy coming next to the
>> matched proxy in the passed list.
>> ---
>> gdbus/client.c | 19 +++++++++++++++++++
>> gdbus/gdbus.h | 1 +
>> 2 files changed, 20 insertions(+)
>>
>> diff --git a/gdbus/client.c b/gdbus/client.c
>> index 6ec611455..bb13709be 100644
>> --- a/gdbus/client.c
>> +++ b/gdbus/client.c
>> @@ -26,6 +26,7 @@
>> #endif
>>
>> #include <stdio.h>
>> +#include <string.h>
>> #include <glib.h>
>> #include <dbus/dbus.h>
>>
>> @@ -371,6 +372,24 @@ GDBusProxy *g_dbus_proxy_lookup(GList *list, const char *path,
>> return NULL;
>> }
>>
>> +char *g_dbus_proxy_path_lookup(GList *source, int *index, const char *text)
>> +{
>> + GList *l;
>> +
>> + for (l = g_list_nth(source, *index); l; l = g_list_next(l)) {
>> + GDBusProxy *proxy = l->data;
>> + const char *path;
>> +
>> + (*index)++;
>> +
>> + path = g_dbus_proxy_get_path(proxy);
>> + if (!strncasecmp(path, text, strlen(text)))
>
> We better move out the strlen(text) portion outside of the for loop.
>
>> + return strdup(path);
>> + }
>> +
>> + return NULL;
>> +}
>> +
>> static gboolean properties_changed(DBusConnection *conn, DBusMessage *msg,
>> void *user_data)
>> {
>> diff --git a/gdbus/gdbus.h b/gdbus/gdbus.h
>> index 930aa5f77..99f21b3ce 100644
>> --- a/gdbus/gdbus.h
>> +++ b/gdbus/gdbus.h
>> @@ -341,6 +341,7 @@ gboolean g_dbus_proxy_get_property(GDBusProxy *proxy, const char *name,
>>
>> GDBusProxy *g_dbus_proxy_lookup(GList *list, const char *path,
>> const char *interface);
>> +char *g_dbus_proxy_path_lookup(GList *source, int *index, const char *text);

I second thought here, why don't we add the index to
g_dbus_proxy_lookup and make it check for NULL interface?

>> gboolean g_dbus_proxy_refresh_property(GDBusProxy *proxy, const char *name);
>>
>> --
>> 2.14.1
>>
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in
>> the body of a message to [email protected]
>> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
>
>
> --
> Luiz Augusto von Dentz



--
Luiz Augusto von Dentz

2017-12-27 18:51:32

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: Re: [PATCH BlueZ 5/8] gdbus: Introduce g_dbus_proxy_path_lookup()

Hi Eramoto,

On Fri, Dec 22, 2017 at 5:35 AM, ERAMOTO Masaya
<[email protected]> wrote:
> g_dbus_proxy_path_lookup() returns the path of the proxy that matches the
> passed text. It also returns the index of the proxy coming next to the
> matched proxy in the passed list.
> ---
> gdbus/client.c | 19 +++++++++++++++++++
> gdbus/gdbus.h | 1 +
> 2 files changed, 20 insertions(+)
>
> diff --git a/gdbus/client.c b/gdbus/client.c
> index 6ec611455..bb13709be 100644
> --- a/gdbus/client.c
> +++ b/gdbus/client.c
> @@ -26,6 +26,7 @@
> #endif
>
> #include <stdio.h>
> +#include <string.h>
> #include <glib.h>
> #include <dbus/dbus.h>
>
> @@ -371,6 +372,24 @@ GDBusProxy *g_dbus_proxy_lookup(GList *list, const char *path,
> return NULL;
> }
>
> +char *g_dbus_proxy_path_lookup(GList *source, int *index, const char *text)
> +{
> + GList *l;
> +
> + for (l = g_list_nth(source, *index); l; l = g_list_next(l)) {
> + GDBusProxy *proxy = l->data;
> + const char *path;
> +
> + (*index)++;
> +
> + path = g_dbus_proxy_get_path(proxy);
> + if (!strncasecmp(path, text, strlen(text)))

We better move out the strlen(text) portion outside of the for loop.

> + return strdup(path);
> + }
> +
> + return NULL;
> +}
> +
> static gboolean properties_changed(DBusConnection *conn, DBusMessage *msg,
> void *user_data)
> {
> diff --git a/gdbus/gdbus.h b/gdbus/gdbus.h
> index 930aa5f77..99f21b3ce 100644
> --- a/gdbus/gdbus.h
> +++ b/gdbus/gdbus.h
> @@ -341,6 +341,7 @@ gboolean g_dbus_proxy_get_property(GDBusProxy *proxy, const char *name,
>
> GDBusProxy *g_dbus_proxy_lookup(GList *list, const char *path,
> const char *interface);
> +char *g_dbus_proxy_path_lookup(GList *source, int *index, const char *text);
>
> gboolean g_dbus_proxy_refresh_property(GDBusProxy *proxy, const char *name);
>
> --
> 2.14.1
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html



--
Luiz Augusto von Dentz

2017-12-22 07:36:05

by ERAMOTO Masaya

[permalink] [raw]
Subject: [PATCH BlueZ 8/8] tools/obexctl: Add generator for session/transfer

---
tools/obexctl.c | 42 +++++++++++++++++++++++++++++++++++-------
1 file changed, 35 insertions(+), 7 deletions(-)

diff --git a/tools/obexctl.c b/tools/obexctl.c
index 38f0fd506..3f9e5ddc0 100644
--- a/tools/obexctl.c
+++ b/tools/obexctl.c
@@ -83,6 +83,27 @@ static void disconnect_handler(DBusConnection *connection, void *user_data)
bt_shell_set_prompt(PROMPT_OFF);
}

+static char *generic_generator(const char *text, int state, GList *source)
+{
+ static int index = 0;
+
+ if (!state) {
+ index = 0;
+ }
+
+ return g_dbus_proxy_path_lookup(source, &index, text);
+}
+
+static char *session_generator(const char *text, int state)
+{
+ return generic_generator(text, state, sessions);
+}
+
+static char *transfer_generator(const char *text, int state)
+{
+ return generic_generator(text, state, transfers);
+}
+
static void connect_reply(DBusMessage *message, void *user_data)
{
DBusError error;
@@ -1785,14 +1806,21 @@ static const struct bt_shell_menu main_menu = {
.name = "main",
.entries = {
{ "connect", "<dev> [uuid]", cmd_connect, "Connect session" },
- { "disconnect", "[session]", cmd_disconnect, "Disconnect session" },
+ { "disconnect", "[session]", cmd_disconnect, "Disconnect session",
+ session_generator },
{ "list", NULL, cmd_list, "List available sessions" },
- { "show", "[session]", cmd_show, "Session information" },
- { "select", "<session>", cmd_select, "Select default session" },
- { "info", "<object>", cmd_info, "Object information" },
- { "cancel", "<transfer>", cmd_cancel, "Cancel transfer" },
- { "suspend", "<transfer>", cmd_suspend, "Suspend transfer" },
- { "resume", "<transfer>", cmd_resume, "Resume transfer" },
+ { "show", "[session]", cmd_show, "Session information",
+ session_generator },
+ { "select", "<session>", cmd_select, "Select default session",
+ session_generator },
+ { "info", "<object>", cmd_info, "Object information",
+ transfer_generator },
+ { "cancel", "<transfer>", cmd_cancel, "Cancel transfer",
+ transfer_generator },
+ { "suspend", "<transfer>", cmd_suspend, "Suspend transfer",
+ transfer_generator },
+ { "resume", "<transfer>", cmd_resume, "Resume transfer",
+ transfer_generator },
{ "send", "<file>", cmd_send, "Send file" },
{ "pull", "<file>", cmd_pull,
"Pull Vobject & stores in file" },
--
2.14.1


2017-12-22 07:36:00

by ERAMOTO Masaya

[permalink] [raw]
Subject: [PATCH BlueZ 7/8] tools/bluetooth-player: Add generator for player/item

---
tools/bluetooth-player.c | 36 +++++++++++++++++++++++++++++++-----
1 file changed, 31 insertions(+), 5 deletions(-)

diff --git a/tools/bluetooth-player.c b/tools/bluetooth-player.c
index 4fbae0c0a..3dc7dc985 100644
--- a/tools/bluetooth-player.c
+++ b/tools/bluetooth-player.c
@@ -75,6 +75,27 @@ static bool check_default_player(void)
return TRUE;
}

+static char *generic_generator(const char *text, int state, GList *source)
+{
+ static int index = 0;
+
+ if (!state) {
+ index = 0;
+ }
+
+ return g_dbus_proxy_path_lookup(source, &index, text);
+}
+
+static char *player_generator(const char *text, int state)
+{
+ return generic_generator(text, state, players);
+}
+
+static char *item_generator(const char *text, int state)
+{
+ return generic_generator(text, state, items);
+}
+
static void play_reply(DBusMessage *message, void *user_data)
{
DBusError error;
@@ -909,9 +930,12 @@ static const struct bt_shell_menu main_menu = {
.name = "main",
.entries = {
{ "list", NULL, cmd_list, "List available players" },
- { "show", "[player]", cmd_show, "Player information" },
- { "select", "<player>", cmd_select, "Select default player" },
- { "play", "[item]", cmd_play, "Start playback" },
+ { "show", "[player]", cmd_show, "Player information",
+ player_generator},
+ { "select", "<player>", cmd_select, "Select default player",
+ player_generator},
+ { "play", "[item]", cmd_play, "Start playback",
+ item_generator},
{ "pause", NULL, cmd_pause, "Pause playback" },
{ "stop", NULL, cmd_stop, "Stop playback" },
{ "next", NULL, cmd_next, "Jump to next item" },
@@ -933,8 +957,10 @@ static const struct bt_shell_menu main_menu = {
"List items of current folder" },
{ "search", "<string>", cmd_search,
"Search items containing string" },
- { "queue", "<item>", cmd_queue, "Add item to playlist queue" },
- { "show-item", "<item>", cmd_show_item, "Show item information" },
+ { "queue", "<item>", cmd_queue, "Add item to playlist queue",
+ item_generator},
+ { "show-item", "<item>", cmd_show_item, "Show item information",
+ item_generator},
{} },
};

--
2.14.1


2017-12-22 07:35:55

by ERAMOTO Masaya

[permalink] [raw]
Subject: [PATCH BlueZ 6/8] client: Use g_dbus_proxy_path_lookup()

---
client/gatt.c | 19 ++-----------------
1 file changed, 2 insertions(+), 17 deletions(-)

diff --git a/client/gatt.c b/client/gatt.c
index cdf14363c..c0f789c34 100644
--- a/client/gatt.c
+++ b/client/gatt.c
@@ -444,28 +444,13 @@ GDBusProxy *gatt_select_attribute(GDBusProxy *parent, const char *arg)

static char *attribute_generator(const char *text, int state, GList *source)
{
- static int index, len;
- GList *list;
+ static int index;

if (!state) {
index = 0;
- len = strlen(text);
}

- for (list = g_list_nth(source, index); list;
- list = g_list_next(list)) {
- GDBusProxy *proxy = list->data;
- const char *path;
-
- index++;
-
- path = g_dbus_proxy_get_path(proxy);
-
- if (!strncmp(path, text, len))
- return strdup(path);
- }
-
- return NULL;
+ return g_dbus_proxy_path_lookup(source, &index, text);
}

char *gatt_attribute_generator(const char *text, int state)
--
2.14.1


2017-12-22 07:35:49

by ERAMOTO Masaya

[permalink] [raw]
Subject: [PATCH BlueZ 5/8] gdbus: Introduce g_dbus_proxy_path_lookup()

g_dbus_proxy_path_lookup() returns the path of the proxy that matches the
passed text. It also returns the index of the proxy coming next to the
matched proxy in the passed list.
---
gdbus/client.c | 19 +++++++++++++++++++
gdbus/gdbus.h | 1 +
2 files changed, 20 insertions(+)

diff --git a/gdbus/client.c b/gdbus/client.c
index 6ec611455..bb13709be 100644
--- a/gdbus/client.c
+++ b/gdbus/client.c
@@ -26,6 +26,7 @@
#endif

#include <stdio.h>
+#include <string.h>
#include <glib.h>
#include <dbus/dbus.h>

@@ -371,6 +372,24 @@ GDBusProxy *g_dbus_proxy_lookup(GList *list, const char *path,
return NULL;
}

+char *g_dbus_proxy_path_lookup(GList *source, int *index, const char *text)
+{
+ GList *l;
+
+ for (l = g_list_nth(source, *index); l; l = g_list_next(l)) {
+ GDBusProxy *proxy = l->data;
+ const char *path;
+
+ (*index)++;
+
+ path = g_dbus_proxy_get_path(proxy);
+ if (!strncasecmp(path, text, strlen(text)))
+ return strdup(path);
+ }
+
+ return NULL;
+}
+
static gboolean properties_changed(DBusConnection *conn, DBusMessage *msg,
void *user_data)
{
diff --git a/gdbus/gdbus.h b/gdbus/gdbus.h
index 930aa5f77..99f21b3ce 100644
--- a/gdbus/gdbus.h
+++ b/gdbus/gdbus.h
@@ -341,6 +341,7 @@ gboolean g_dbus_proxy_get_property(GDBusProxy *proxy, const char *name,

GDBusProxy *g_dbus_proxy_lookup(GList *list, const char *path,
const char *interface);
+char *g_dbus_proxy_path_lookup(GList *source, int *index, const char *text);

gboolean g_dbus_proxy_refresh_property(GDBusProxy *proxy, const char *name);

--
2.14.1


2017-12-22 07:35:44

by ERAMOTO Masaya

[permalink] [raw]
Subject: [PATCH BlueZ 4/8] tools/obexctl: Use g_dbus_proxy_lookup() instead of find_*

Uses g_dbus_proxy_lookup() instead of find_{session,transfer,message,opp,
map,ftp,pbap}
---
tools/obexctl.c | 211 +++++++++++++++++---------------------------------------
1 file changed, 63 insertions(+), 148 deletions(-)

diff --git a/tools/obexctl.c b/tools/obexctl.c
index c4c7686c1..38f0fd506 100644
--- a/tools/obexctl.c
+++ b/tools/obexctl.c
@@ -59,13 +59,13 @@

static DBusConnection *dbus_conn;
static GDBusProxy *default_session;
-static GSList *sessions = NULL;
-static GSList *opps = NULL;
-static GSList *ftps = NULL;
-static GSList *pbaps = NULL;
-static GSList *maps = NULL;
-static GSList *msgs = NULL;
-static GSList *transfers = NULL;
+static GList *sessions = NULL;
+static GList *opps = NULL;
+static GList *ftps = NULL;
+static GList *pbaps = NULL;
+static GList *maps = NULL;
+static GList *msgs = NULL;
+static GList *transfers = NULL;
static GDBusProxy *client = NULL;

struct transfer_data {
@@ -219,26 +219,13 @@ static void disconnect_setup(DBusMessageIter *iter, void *user_data)
dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
}

-static GDBusProxy *find_session(const char *path)
-{
- GSList *l;
-
- for (l = sessions; l; l = g_slist_next(l)) {
- GDBusProxy *proxy = l->data;
-
- if (strcmp(path, g_dbus_proxy_get_path(proxy)) == 0)
- return proxy;
- }
-
- return NULL;
-}
-
static void cmd_disconnect(int argc, char *argv[])
{
GDBusProxy *proxy;

if (argc > 1)
- proxy = find_session(argv[1]);
+ proxy = g_dbus_proxy_lookup(sessions, argv[1],
+ OBEX_SESSION_INTERFACE);
else
proxy = default_session;

@@ -285,9 +272,9 @@ static void print_proxy(GDBusProxy *proxy, const char *title,

static void cmd_list(int argc, char *arg[])
{
- GSList *l;
+ GList *l;

- for (l = sessions; l; l = g_slist_next(l)) {
+ for (l = sessions; l; l = g_list_next(l)) {
GDBusProxy *proxy = l->data;
print_proxy(proxy, "Session", NULL);
}
@@ -393,7 +380,8 @@ static void cmd_show(int argc, char *argv[])

proxy = default_session;
} else {
- proxy = find_session(argv[1]);
+ proxy = g_dbus_proxy_lookup(sessions, argv[1],
+ OBEX_SESSION_INTERFACE);
if (!proxy) {
bt_shell_printf("Session %s not available\n", argv[1]);
return;
@@ -430,7 +418,7 @@ static void cmd_select(int argc, char *argv[])
{
GDBusProxy *proxy;

- proxy = find_session(argv[1]);
+ proxy = g_dbus_proxy_lookup(sessions, argv[1], OBEX_SESSION_INTERFACE);
if (proxy == NULL) {
bt_shell_printf("Session %s not available\n", argv[1]);
return;
@@ -444,34 +432,6 @@ static void cmd_select(int argc, char *argv[])
print_proxy(proxy, "Session", NULL);
}

-static GDBusProxy *find_transfer(const char *path)
-{
- GSList *l;
-
- for (l = transfers; l; l = g_slist_next(l)) {
- GDBusProxy *proxy = l->data;
-
- if (strcmp(path, g_dbus_proxy_get_path(proxy)) == 0)
- return proxy;
- }
-
- return NULL;
-}
-
-static GDBusProxy *find_message(const char *path)
-{
- GSList *l;
-
- for (l = msgs; l; l = g_slist_next(l)) {
- GDBusProxy *proxy = l->data;
-
- if (strcmp(path, g_dbus_proxy_get_path(proxy)) == 0)
- return proxy;
- }
-
- return NULL;
-}
-
static void transfer_info(GDBusProxy *proxy, int argc, char *argv[])
{
bt_shell_printf("Transfer %s\n", g_dbus_proxy_get_path(proxy));
@@ -512,13 +472,14 @@ static void cmd_info(int argc, char *argv[])
{
GDBusProxy *proxy;

- proxy = find_transfer(argv[1]);
+ proxy = g_dbus_proxy_lookup(transfers, argv[1],
+ OBEX_TRANSFER_INTERFACE);
if (proxy) {
transfer_info(proxy, argc, argv);
return;
}

- proxy = find_message(argv[1]);
+ proxy = g_dbus_proxy_lookup(msgs, argv[1], OBEX_MSG_INTERFACE);
if (proxy) {
message_info(proxy, argc, argv);
return;
@@ -546,7 +507,8 @@ static void cmd_cancel(int argc, char *argv[])
{
GDBusProxy *proxy;

- proxy = find_transfer(argv[1]);
+ proxy = g_dbus_proxy_lookup(transfers, argv[1],
+ OBEX_TRANSFER_INTERFACE);
if (!proxy) {
bt_shell_printf("Transfer %s not available\n", argv[1]);
return;
@@ -581,7 +543,8 @@ static void cmd_suspend(int argc, char *argv[])
{
GDBusProxy *proxy;

- proxy = find_transfer(argv[1]);
+ proxy = g_dbus_proxy_lookup(transfers, argv[1],
+ OBEX_TRANSFER_INTERFACE);
if (!proxy) {
bt_shell_printf("Transfer %s not available\n", argv[1]);
return;
@@ -616,7 +579,8 @@ static void cmd_resume(int argc, char *argv[])
{
GDBusProxy *proxy;

- proxy = find_transfer(argv[1]);
+ proxy = g_dbus_proxy_lookup(transfers, argv[1],
+ OBEX_TRANSFER_INTERFACE);
if (!proxy) {
bt_shell_printf("Transfer %s not available\n", argv[1]);
return;
@@ -632,34 +596,6 @@ static void cmd_resume(int argc, char *argv[])
g_dbus_proxy_get_path(proxy));
}

-static GDBusProxy *find_opp(const char *path)
-{
- GSList *l;
-
- for (l = opps; l; l = g_slist_next(l)) {
- GDBusProxy *proxy = l->data;
-
- if (strcmp(path, g_dbus_proxy_get_path(proxy)) == 0)
- return proxy;
- }
-
- return NULL;
-}
-
-static GDBusProxy *find_map(const char *path)
-{
- GSList *l;
-
- for (l = maps; l; l = g_slist_next(l)) {
- GDBusProxy *proxy = l->data;
-
- if (strcmp(path, g_dbus_proxy_get_path(proxy)) == 0)
- return proxy;
- }
-
- return NULL;
-}
-
static void print_dict_iter(DBusMessageIter *iter)
{
DBusMessageIter dict;
@@ -806,18 +742,19 @@ static void map_send(GDBusProxy *proxy, int argc, char *argv[])

static void cmd_send(int argc, char *argv[])
{
+ const char *path = g_dbus_proxy_get_path(default_session);
GDBusProxy *proxy;

if (!check_default_session())
return;

- proxy = find_opp(g_dbus_proxy_get_path(default_session));
+ proxy = g_dbus_proxy_lookup(opps, path, OBEX_OPP_INTERFACE);
if (proxy) {
opp_send(proxy, argc, argv);
return;
}

- proxy = find_map(g_dbus_proxy_get_path(default_session));
+ proxy = g_dbus_proxy_lookup(maps, path, OBEX_MAP_INTERFACE);
if (proxy) {
map_send(proxy, argc, argv);
return;
@@ -828,12 +765,13 @@ static void cmd_send(int argc, char *argv[])

static void cmd_pull(int argc, char *argv[])
{
+ const char *path = g_dbus_proxy_get_path(default_session);
GDBusProxy *proxy;

if (!check_default_session())
return;

- proxy = find_opp(g_dbus_proxy_get_path(default_session));
+ proxy = g_dbus_proxy_lookup(opps, path, OBEX_OPP_INTERFACE);
if (proxy) {
opp_pull(proxy, argc, argv);
return;
@@ -913,34 +851,6 @@ static void setfolder_setup(DBusMessageIter *iter, void *user_data)
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &folder);
}

-static GDBusProxy *find_ftp(const char *path)
-{
- GSList *l;
-
- for (l = ftps; l; l = g_slist_next(l)) {
- GDBusProxy *proxy = l->data;
-
- if (strcmp(path, g_dbus_proxy_get_path(proxy)) == 0)
- return proxy;
- }
-
- return NULL;
-}
-
-static GDBusProxy *find_pbap(const char *path)
-{
- GSList *l;
-
- for (l = pbaps; l; l = g_slist_next(l)) {
- GDBusProxy *proxy = l->data;
-
- if (strcmp(path, g_dbus_proxy_get_path(proxy)) == 0)
- return proxy;
- }
-
- return NULL;
-}
-
static void ftp_cd(GDBusProxy *proxy, int argc, char *argv[])
{
if (g_dbus_proxy_method_call(proxy, "ChangeFolder", change_folder_setup,
@@ -979,24 +889,25 @@ static void map_cd(GDBusProxy *proxy, int argc, char *argv[])

static void cmd_cd(int argc, char *argv[])
{
+ const char *path = g_dbus_proxy_get_path(default_session);
GDBusProxy *proxy;

if (!check_default_session())
return;

- proxy = find_ftp(g_dbus_proxy_get_path(default_session));
+ proxy = g_dbus_proxy_lookup(ftps, path, OBEX_FTP_INTERFACE);
if (proxy) {
ftp_cd(proxy, argc, argv);
return;
}

- proxy = find_pbap(g_dbus_proxy_get_path(default_session));
+ proxy = g_dbus_proxy_lookup(pbaps, path, OBEX_PBAP_INTERFACE);
if (proxy) {
pbap_cd(proxy, argc, argv);
return;
}

- proxy = find_map(g_dbus_proxy_get_path(default_session));
+ proxy = g_dbus_proxy_lookup(maps, path, OBEX_MAP_INTERFACE);
if (proxy) {
map_cd(proxy, argc, argv);
return;
@@ -1326,24 +1237,25 @@ static void map_ls(GDBusProxy *proxy, int argc, char *argv[])

static void cmd_ls(int argc, char *argv[])
{
+ const char *path = g_dbus_proxy_get_path(default_session);
GDBusProxy *proxy;

if (!check_default_session())
return;

- proxy = find_ftp(g_dbus_proxy_get_path(default_session));
+ proxy = g_dbus_proxy_lookup(ftps, path, OBEX_FTP_INTERFACE);
if (proxy) {
ftp_ls(proxy, argc, argv);
return;
}

- proxy = find_pbap(g_dbus_proxy_get_path(default_session));
+ proxy = g_dbus_proxy_lookup(pbaps, path, OBEX_PBAP_INTERFACE);
if (proxy) {
pbap_ls(proxy, argc, argv);
return;
}

- proxy = find_map(g_dbus_proxy_get_path(default_session));
+ proxy = g_dbus_proxy_lookup(maps, path, OBEX_MAP_INTERFACE);
if (proxy) {
map_ls(proxy, argc, argv);
return;
@@ -1652,7 +1564,7 @@ static void map_cp(GDBusProxy *proxy, int argc, char *argv[])
{
GDBusProxy *obj;

- obj = find_message(argv[1]);
+ obj = g_dbus_proxy_lookup(msgs, argv[1], OBEX_MSG_INTERFACE);
if (obj == NULL) {
bt_shell_printf("Invalid message argument\n");
return;
@@ -1669,25 +1581,25 @@ static void map_cp(GDBusProxy *proxy, int argc, char *argv[])

static void cmd_cp(int argc, char *argv[])
{
-
+ const char *path = g_dbus_proxy_get_path(default_session);
GDBusProxy *proxy;

if (!check_default_session())
return;

- proxy = find_ftp(g_dbus_proxy_get_path(default_session));
+ proxy = g_dbus_proxy_lookup(ftps, path, OBEX_FTP_INTERFACE);
if (proxy) {
ftp_cp(proxy, argc, argv);
return;
}

- proxy = find_pbap(g_dbus_proxy_get_path(default_session));
+ proxy = g_dbus_proxy_lookup(pbaps, path, OBEX_PBAP_INTERFACE);
if (proxy) {
pbap_cp(proxy, argc, argv);
return;
}

- proxy = find_map(g_dbus_proxy_get_path(default_session));
+ proxy = g_dbus_proxy_lookup(maps, path, OBEX_MAP_INTERFACE);
if (proxy) {
map_cp(proxy, argc, argv);
return;
@@ -1713,13 +1625,14 @@ static void move_file_reply(DBusMessage *message, void *user_data)

static void cmd_mv(int argc, char *argv[])
{
+ const char *path = g_dbus_proxy_get_path(default_session);
GDBusProxy *proxy;
struct cp_args *args;

if (!check_default_session())
return;

- proxy = find_ftp(g_dbus_proxy_get_path(default_session));
+ proxy = g_dbus_proxy_lookup(ftps, path, OBEX_FTP_INTERFACE);
if (proxy == NULL) {
bt_shell_printf("Command not supported\n");
return;
@@ -1783,7 +1696,7 @@ static void map_rm(GDBusProxy *proxy, int argc, char *argv[])
GDBusProxy *msg;
dbus_bool_t value = TRUE;

- msg = find_message(argv[1]);
+ msg = g_dbus_proxy_lookup(msgs, argv[1], OBEX_MSG_INTERFACE);
if (msg == NULL) {
bt_shell_printf("Invalid message argument\n");
return;
@@ -1801,18 +1714,19 @@ static void map_rm(GDBusProxy *proxy, int argc, char *argv[])

static void cmd_rm(int argc, char *argv[])
{
+ const char *path = g_dbus_proxy_get_path(default_session);
GDBusProxy *proxy;

if (!check_default_session())
return;

- proxy = find_ftp(g_dbus_proxy_get_path(default_session));
+ proxy = g_dbus_proxy_lookup(ftps, path, OBEX_FTP_INTERFACE);
if (proxy) {
ftp_rm(proxy, argc, argv);
return;
}

- proxy = find_map(g_dbus_proxy_get_path(default_session));
+ proxy = g_dbus_proxy_lookup(maps, path, OBEX_MAP_INTERFACE);
if (proxy) {
map_rm(proxy, argc, argv);
return;
@@ -1845,12 +1759,13 @@ static void create_folder_setup(DBusMessageIter *iter, void *user_data)

static void cmd_mkdir(int argc, char *argv[])
{
+ const char *path = g_dbus_proxy_get_path(default_session);
GDBusProxy *proxy;

if (!check_default_session())
return;

- proxy = find_ftp(g_dbus_proxy_get_path(default_session));
+ proxy = g_dbus_proxy_lookup(ftps, path, OBEX_FTP_INTERFACE);
if (proxy == NULL) {
bt_shell_printf("Command not supported\n");
return;
@@ -1902,7 +1817,7 @@ static void client_added(GDBusProxy *proxy)

static void session_added(GDBusProxy *proxy)
{
- sessions = g_slist_append(sessions, proxy);
+ sessions = g_list_append(sessions, proxy);

if (default_session == NULL)
set_default_session(proxy);
@@ -1969,7 +1884,7 @@ static void transfer_added(GDBusProxy *proxy)
struct transfer_data *data;
DBusMessageIter iter;

- transfers = g_slist_append(transfers, proxy);
+ transfers = g_list_append(transfers, proxy);

print_proxy(proxy, "Transfer", COLORED_NEW);

@@ -1987,35 +1902,35 @@ static void transfer_added(GDBusProxy *proxy)

static void opp_added(GDBusProxy *proxy)
{
- opps = g_slist_append(opps, proxy);
+ opps = g_list_append(opps, proxy);

print_proxy(proxy, "ObjectPush", COLORED_NEW);
}

static void ftp_added(GDBusProxy *proxy)
{
- ftps = g_slist_append(ftps, proxy);
+ ftps = g_list_append(ftps, proxy);

print_proxy(proxy, "FileTransfer", COLORED_NEW);
}

static void pbap_added(GDBusProxy *proxy)
{
- pbaps = g_slist_append(pbaps, proxy);
+ pbaps = g_list_append(pbaps, proxy);

print_proxy(proxy, "PhonebookAccess", COLORED_NEW);
}

static void map_added(GDBusProxy *proxy)
{
- maps = g_slist_append(maps, proxy);
+ maps = g_list_append(maps, proxy);

print_proxy(proxy, "MessageAccess", COLORED_NEW);
}

static void msg_added(GDBusProxy *proxy)
{
- msgs = g_slist_append(msgs, proxy);
+ msgs = g_list_append(msgs, proxy);

print_proxy(proxy, "Message", COLORED_NEW);
}
@@ -2059,49 +1974,49 @@ static void session_removed(GDBusProxy *proxy)
if (default_session == proxy)
set_default_session(NULL);

- sessions = g_slist_remove(sessions, proxy);
+ sessions = g_list_remove(sessions, proxy);
}

static void transfer_removed(GDBusProxy *proxy)
{
print_proxy(proxy, "Transfer", COLORED_DEL);

- transfers = g_slist_remove(transfers, proxy);
+ transfers = g_list_remove(transfers, proxy);
}

static void opp_removed(GDBusProxy *proxy)
{
print_proxy(proxy, "ObjectPush", COLORED_DEL);

- opps = g_slist_remove(opps, proxy);
+ opps = g_list_remove(opps, proxy);
}

static void ftp_removed(GDBusProxy *proxy)
{
print_proxy(proxy, "FileTransfer", COLORED_DEL);

- ftps = g_slist_remove(ftps, proxy);
+ ftps = g_list_remove(ftps, proxy);
}

static void pbap_removed(GDBusProxy *proxy)
{
print_proxy(proxy, "PhonebookAccess", COLORED_DEL);

- pbaps = g_slist_remove(pbaps, proxy);
+ pbaps = g_list_remove(pbaps, proxy);
}

static void map_removed(GDBusProxy *proxy)
{
print_proxy(proxy, "MessageAccess", COLORED_DEL);

- maps = g_slist_remove(maps, proxy);
+ maps = g_list_remove(maps, proxy);
}

static void msg_removed(GDBusProxy *proxy)
{
print_proxy(proxy, "Message", COLORED_DEL);

- msgs = g_slist_remove(msgs, proxy);
+ msgs = g_list_remove(msgs, proxy);
}

static void proxy_removed(GDBusProxy *proxy, void *user_data)
--
2.14.1


2017-12-22 07:35:39

by ERAMOTO Masaya

[permalink] [raw]
Subject: [PATCH BlueZ 3/8] tools/bluetooth-player: Use g_dbus_proxy_lookup() instead of find_*

Uses g_dbus_proxy_lookup() instead of find_{player,folder,item}
---
tools/bluetooth-player.c | 96 +++++++++++++++++-------------------------------
1 file changed, 33 insertions(+), 63 deletions(-)

diff --git a/tools/bluetooth-player.c b/tools/bluetooth-player.c
index b22de2775..4fbae0c0a 100644
--- a/tools/bluetooth-player.c
+++ b/tools/bluetooth-player.c
@@ -51,9 +51,9 @@

static DBusConnection *dbus_conn;
static GDBusProxy *default_player;
-static GSList *players = NULL;
-static GSList *folders = NULL;
-static GSList *items = NULL;
+static GList *players = NULL;
+static GList *folders = NULL;
+static GList *items = NULL;

static void connect_handler(DBusConnection *connection, void *user_data)
{
@@ -90,25 +90,12 @@ static void play_reply(DBusMessage *message, void *user_data)
bt_shell_printf("Play successful\n");
}

-static GDBusProxy *find_item(const char *path)
-{
- GSList *l;
-
- for (l = items; l; l = g_slist_next(l)) {
- GDBusProxy *proxy = l->data;
-
- if (strcmp(path, g_dbus_proxy_get_path(proxy)) == 0)
- return proxy;
- }
-
- return NULL;
-}
-
static void cmd_play_item(int argc, char *argv[])
{
GDBusProxy *proxy;

- proxy = find_item(argv[1]);
+ proxy = g_dbus_proxy_lookup(items, argv[1],
+ BLUEZ_MEDIA_ITEM_INTERFACE);
if (proxy == NULL) {
bt_shell_printf("Item %s not available\n", argv[1]);
return;
@@ -460,28 +447,14 @@ static void print_player(GDBusProxy *proxy, const char *description)

static void cmd_list(int argc, char *arg[])
{
- GSList *l;
+ GList *l;

- for (l = players; l; l = g_slist_next(l)) {
+ for (l = players; l; l = g_list_next(l)) {
GDBusProxy *proxy = l->data;
print_player(proxy, NULL);
}
}

-static GDBusProxy *find_player(const char *path)
-{
- GSList *l;
-
- for (l = players; l; l = g_slist_next(l)) {
- GDBusProxy *proxy = l->data;
-
- if (strcmp(path, g_dbus_proxy_get_path(proxy)) == 0)
- return proxy;
- }
-
- return NULL;
-}
-
static void print_iter(const char *label, const char *name,
DBusMessageIter *iter)
{
@@ -557,25 +530,12 @@ static void print_property(GDBusProxy *proxy, const char *name)
print_iter("\t", name, &iter);
}

-static GDBusProxy *find_folder(const char *path)
-{
- GSList *l;
-
- for (l = folders; l; l = g_slist_next(l)) {
- GDBusProxy *proxy = l->data;
-
- if (strcmp(path, g_dbus_proxy_get_path(proxy)) == 0)
- return proxy;
- }
-
- return NULL;
-}
-
static void cmd_show_item(int argc, char *argv[])
{
GDBusProxy *proxy;

- proxy = find_item(argv[1]);
+ proxy = g_dbus_proxy_lookup(items, argv[1],
+ BLUEZ_MEDIA_ITEM_INTERFACE);
if (!proxy) {
bt_shell_printf("Item %s not available\n", argv[1]);
return;
@@ -605,7 +565,8 @@ static void cmd_show(int argc, char *argv[])

proxy = default_player;
} else {
- proxy = find_player(argv[1]);
+ proxy = g_dbus_proxy_lookup(players, argv[1],
+ BLUEZ_MEDIA_PLAYER_INTERFACE);
if (!proxy) {
bt_shell_printf("Player %s not available\n", argv[1]);
return;
@@ -623,7 +584,8 @@ static void cmd_show(int argc, char *argv[])
print_property(proxy, "Position");
print_property(proxy, "Track");

- folder = find_folder(g_dbus_proxy_get_path(proxy));
+ folder = g_dbus_proxy_lookup(folders, g_dbus_proxy_get_path(proxy),
+ BLUEZ_MEDIA_FOLDER_INTERFACE);
if (folder == NULL)
return;

@@ -637,7 +599,7 @@ static void cmd_show(int argc, char *argv[])

dbus_message_iter_get_basic(&iter, &path);

- item = find_item(path);
+ item = g_dbus_proxy_lookup(items, path, BLUEZ_MEDIA_ITEM_INTERFACE);
if (item == NULL)
return;

@@ -650,7 +612,8 @@ static void cmd_select(int argc, char *argv[])
{
GDBusProxy *proxy;

- proxy = find_player(argv[1]);
+ proxy = g_dbus_proxy_lookup(players, argv[1],
+ BLUEZ_MEDIA_PLAYER_INTERFACE);
if (proxy == NULL) {
bt_shell_printf("Player %s not available\n", argv[1]);
return;
@@ -697,7 +660,9 @@ static void cmd_change_folder(int argc, char *argv[])
if (check_default_player() == FALSE)
return;

- proxy = find_folder(g_dbus_proxy_get_path(default_player));
+ proxy = g_dbus_proxy_lookup(folders,
+ g_dbus_proxy_get_path(default_player),
+ BLUEZ_MEDIA_FOLDER_INTERFACE);
if (proxy == NULL) {
bt_shell_printf("Operation not supported\n");
return;
@@ -799,7 +764,9 @@ static void cmd_list_items(int argc, char *argv[])
if (check_default_player() == FALSE)
return;

- proxy = find_folder(g_dbus_proxy_get_path(default_player));
+ proxy = g_dbus_proxy_lookup(folders,
+ g_dbus_proxy_get_path(default_player),
+ BLUEZ_MEDIA_FOLDER_INTERFACE);
if (proxy == NULL) {
bt_shell_printf("Operation not supported\n");
return;
@@ -882,7 +849,9 @@ static void cmd_search(int argc, char *argv[])
if (check_default_player() == FALSE)
return;

- proxy = find_folder(g_dbus_proxy_get_path(default_player));
+ proxy = g_dbus_proxy_lookup(folders,
+ g_dbus_proxy_get_path(default_player),
+ BLUEZ_MEDIA_FOLDER_INTERFACE);
if (proxy == NULL) {
bt_shell_printf("Operation not supported\n");
return;
@@ -919,7 +888,8 @@ static void cmd_queue(int argc, char *argv[])
{
GDBusProxy *proxy;

- proxy = find_item(argv[1]);
+ proxy = g_dbus_proxy_lookup(items, argv[1],
+ BLUEZ_MEDIA_ITEM_INTERFACE);
if (proxy == NULL) {
bt_shell_printf("Item %s not available\n", argv[1]);
return;
@@ -970,7 +940,7 @@ static const struct bt_shell_menu main_menu = {

static void player_added(GDBusProxy *proxy)
{
- players = g_slist_append(players, proxy);
+ players = g_list_append(players, proxy);

if (default_player == NULL)
default_player = proxy;
@@ -992,7 +962,7 @@ static void print_folder(GDBusProxy *proxy, const char *description)

static void folder_added(GDBusProxy *proxy)
{
- folders = g_slist_append(folders, proxy);
+ folders = g_list_append(folders, proxy);

print_folder(proxy, COLORED_NEW);
}
@@ -1017,7 +987,7 @@ static void print_item(GDBusProxy *proxy, const char *description)

static void item_added(GDBusProxy *proxy)
{
- items = g_slist_append(items, proxy);
+ items = g_list_append(items, proxy);

print_item(proxy, COLORED_NEW);
}
@@ -1043,19 +1013,19 @@ static void player_removed(GDBusProxy *proxy)
if (default_player == proxy)
default_player = NULL;

- players = g_slist_remove(players, proxy);
+ players = g_list_remove(players, proxy);
}

static void folder_removed(GDBusProxy *proxy)
{
- folders = g_slist_remove(folders, proxy);
+ folders = g_list_remove(folders, proxy);

print_folder(proxy, COLORED_DEL);
}

static void item_removed(GDBusProxy *proxy)
{
- items = g_slist_remove(items, proxy);
+ items = g_list_remove(items, proxy);

print_item(proxy, COLORED_DEL);
}
--
2.14.1


2017-12-22 07:35:35

by ERAMOTO Masaya

[permalink] [raw]
Subject: [PATCH BlueZ 2/8] client: Use g_dbus_proxy_lookup()

---
client/gatt.c | 37 ++++++++-----------------------------
client/main.c | 15 +++------------
2 files changed, 11 insertions(+), 41 deletions(-)

diff --git a/client/gatt.c b/client/gatt.c
index 224a78a13..cdf14363c 100644
--- a/client/gatt.c
+++ b/client/gatt.c
@@ -206,25 +206,16 @@ static void print_characteristic(GDBusProxy *proxy, const char *description)

static gboolean chrc_is_child(GDBusProxy *characteristic)
{
- GList *l;
DBusMessageIter iter;
- const char *service, *path;
+ const char *service;

if (!g_dbus_proxy_get_property(characteristic, "Service", &iter))
return FALSE;

dbus_message_iter_get_basic(&iter, &service);

- for (l = services; l; l = g_list_next(l)) {
- GDBusProxy *proxy = l->data;
-
- path = g_dbus_proxy_get_path(proxy);
-
- if (!strcmp(path, service))
- return TRUE;
- }
-
- return FALSE;
+ return g_dbus_proxy_lookup(services, service,
+ "org.bluez.GattService1") != NULL;
}

void gatt_add_characteristic(GDBusProxy *proxy)
@@ -378,33 +369,21 @@ void gatt_list_attributes(const char *path)
list_attributes(path, services);
}

-static GDBusProxy *select_proxy(const char *path, GList *source)
-{
- GList *l;
-
- for (l = source; l; l = g_list_next(l)) {
- GDBusProxy *proxy = l->data;
-
- if (strcmp(path, g_dbus_proxy_get_path(proxy)) == 0)
- return proxy;
- }
-
- return NULL;
-}
-
static GDBusProxy *select_attribute(const char *path)
{
GDBusProxy *proxy;

- proxy = select_proxy(path, services);
+ proxy = g_dbus_proxy_lookup(services, path, "org.bluez.GattService1");
if (proxy)
return proxy;

- proxy = select_proxy(path, characteristics);
+ proxy = g_dbus_proxy_lookup(characteristics, path,
+ "org.bluez.GattCharacteristic1");
if (proxy)
return proxy;

- return select_proxy(path, descriptors);
+ return g_dbus_proxy_lookup(descriptors, path,
+ "org.bluez.GattDescriptor1");
}

static GDBusProxy *select_proxy_by_uuid(GDBusProxy *parent, const char *uuid,
diff --git a/client/main.c b/client/main.c
index 26d99d6ed..3d8f1f5f4 100644
--- a/client/main.c
+++ b/client/main.c
@@ -392,9 +392,8 @@ static gboolean device_is_child(GDBusProxy *device, GDBusProxy *master)

static gboolean service_is_child(GDBusProxy *service)
{
- GList *l;
DBusMessageIter iter;
- const char *device, *path;
+ const char *device;

if (g_dbus_proxy_get_property(service, "Device", &iter) == FALSE)
return FALSE;
@@ -404,16 +403,8 @@ static gboolean service_is_child(GDBusProxy *service)
if (!default_ctrl)
return FALSE;

- for (l = default_ctrl->devices; l; l = g_list_next(l)) {
- struct GDBusProxy *proxy = l->data;
-
- path = g_dbus_proxy_get_path(proxy);
-
- if (!strcmp(path, device))
- return TRUE;
- }
-
- return FALSE;
+ return g_dbus_proxy_lookup(default_ctrl->devices, device,
+ "org.bluez.Device1") != NULL;
}

static struct adapter *find_parent(GDBusProxy *device)
--
2.14.1


2017-12-22 07:35:28

by ERAMOTO Masaya

[permalink] [raw]
Subject: [PATCH BlueZ 1/8] gdbus: Make proxy_lookup() global

Also g_dbus_proxy_lookup() is more robust even if proxy is NULL.
---
gdbus/client.c | 13 ++++++++-----
gdbus/gdbus.h | 3 +++
2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/gdbus/client.c b/gdbus/client.c
index ab4059697..6ec611455 100644
--- a/gdbus/client.c
+++ b/gdbus/client.c
@@ -352,7 +352,7 @@ static void get_all_properties(GDBusProxy *proxy)
dbus_message_unref(msg);
}

-static GDBusProxy *proxy_lookup(GList *list, const char *path,
+GDBusProxy *g_dbus_proxy_lookup(GList *list, const char *path,
const char *interface)
{
GList *l;
@@ -360,8 +360,11 @@ static GDBusProxy *proxy_lookup(GList *list, const char *path,
for (l = g_list_first(list); l; l = g_list_next(l)) {
GDBusProxy *proxy = l->data;

- if (g_str_equal(proxy->interface, interface) == TRUE &&
- g_str_equal(proxy->obj_path, path) == TRUE)
+ const char *proxy_iface = g_dbus_proxy_get_interface(proxy);
+ const char *proxy_path = g_dbus_proxy_get_path(proxy);
+
+ if (g_str_equal(proxy_iface, interface) == TRUE &&
+ g_str_equal(proxy_path, path) == TRUE)
return proxy;
}

@@ -519,7 +522,7 @@ GDBusProxy *g_dbus_proxy_new(GDBusClient *client, const char *path,
if (client == NULL)
return NULL;

- proxy = proxy_lookup(client->proxy_list, path, interface);
+ proxy = g_dbus_proxy_lookup(client->proxy_list, path, interface);
if (proxy)
return g_dbus_proxy_ref(proxy);

@@ -992,7 +995,7 @@ static void parse_properties(GDBusClient *client, const char *path,
if (g_str_equal(interface, DBUS_INTERFACE_PROPERTIES) == TRUE)
return;

- proxy = proxy_lookup(client->proxy_list, path, interface);
+ proxy = g_dbus_proxy_lookup(client->proxy_list, path, interface);
if (proxy && !proxy->pending) {
update_properties(proxy, iter, FALSE);
return;
diff --git a/gdbus/gdbus.h b/gdbus/gdbus.h
index e37385fa1..930aa5f77 100644
--- a/gdbus/gdbus.h
+++ b/gdbus/gdbus.h
@@ -339,6 +339,9 @@ const char *g_dbus_proxy_get_interface(GDBusProxy *proxy);
gboolean g_dbus_proxy_get_property(GDBusProxy *proxy, const char *name,
DBusMessageIter *iter);

+GDBusProxy *g_dbus_proxy_lookup(GList *list, const char *path,
+ const char *interface);
+
gboolean g_dbus_proxy_refresh_property(GDBusProxy *proxy, const char *name);

typedef void (* GDBusResultFunction) (const DBusError *error, void *user_data);
--
2.14.1