2017-12-28 05:36:51

by ERAMOTO Masaya

[permalink] [raw]
Subject: [PATCH BlueZ v2 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.

Changes in v2:
- Add index to g_dbus_proxy_lookup().
- Make g_dbus_proxy_lookup() check if the passed interface is NULL.
- Rework callers of g_dbus_proxy_lookup() due to add the index argument.
- Move strlen() outside loop.
- Implement g_dbus_proxy_path_lookup() like g_dbus_proxy_lookup()


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 | 57 ++---------
client/main.c | 15 +--
gdbus/client.c | 44 ++++++--
gdbus/gdbus.h | 4 +
tools/bluetooth-player.c | 134 ++++++++++++-------------
tools/obexctl.c | 254 ++++++++++++++++++-----------------------------
6 files changed, 221 insertions(+), 287 deletions(-)

--
2.14.1



2017-12-28 05:44:23

by ERAMOTO Masaya

[permalink] [raw]
Subject: [PATCH BlueZ v2 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 3236616d0..05bbd3e84 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;
@@ -1786,14 +1807,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-28 05:44:20

by ERAMOTO Masaya

[permalink] [raw]
Subject: [PATCH BlueZ v2 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 0ab8cab33..709ef5633 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;
@@ -911,9 +932,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" },
@@ -935,8 +959,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-28 05:44:18

by ERAMOTO Masaya

[permalink] [raw]
Subject: [PATCH BlueZ v2 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 3d0222e63..4d51e053c 100644
--- a/client/gatt.c
+++ b/client/gatt.c
@@ -445,28 +445,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-28 05:44:15

by ERAMOTO Masaya

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

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

diff --git a/gdbus/client.c b/gdbus/client.c
index 3768d2f59..66cd43786 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>

@@ -377,6 +378,26 @@ GDBusProxy *g_dbus_proxy_lookup(GList *list, int *index, const char *path,
return NULL;
}

+char *g_dbus_proxy_path_lookup(GList *list, int *index, const char *path)
+{
+ int len = strlen(path);
+ GList *l;
+
+ for (l = g_list_nth(list, index ? *index : 0); l; l = g_list_next(l)) {
+ GDBusProxy *proxy = l->data;
+
+ const char *proxy_path = g_dbus_proxy_get_path(proxy);
+
+ if (index)
+ (*index)++;
+
+ if (!strncasecmp(proxy_path, path, len))
+ return strdup(proxy_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 85cb9682e..4880c84e7 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, int *index, const char *path,
const char *interface);
+char *g_dbus_proxy_path_lookup(GList *list, int *index, const char *path);

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

--
2.14.1


2017-12-28 05:44:10

by ERAMOTO Masaya

[permalink] [raw]
Subject: [PATCH BlueZ v2 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 | 212 +++++++++++++++++---------------------------------------
1 file changed, 64 insertions(+), 148 deletions(-)

diff --git a/tools/obexctl.c b/tools/obexctl.c
index c4c7686c1..3236616d0 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, NULL, 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, NULL, argv[1],
+ OBEX_SESSION_INTERFACE);
if (!proxy) {
bt_shell_printf("Session %s not available\n", argv[1]);
return;
@@ -430,7 +418,8 @@ static void cmd_select(int argc, char *argv[])
{
GDBusProxy *proxy;

- proxy = find_session(argv[1]);
+ proxy = g_dbus_proxy_lookup(sessions, NULL, argv[1],
+ OBEX_SESSION_INTERFACE);
if (proxy == NULL) {
bt_shell_printf("Session %s not available\n", argv[1]);
return;
@@ -444,34 +433,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 +473,14 @@ static void cmd_info(int argc, char *argv[])
{
GDBusProxy *proxy;

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

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

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

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

- proxy = find_transfer(argv[1]);
+ proxy = g_dbus_proxy_lookup(transfers, NULL, argv[1],
+ OBEX_TRANSFER_INTERFACE);
if (!proxy) {
bt_shell_printf("Transfer %s not available\n", argv[1]);
return;
@@ -632,34 +597,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 +743,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, NULL, 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, NULL, path, OBEX_MAP_INTERFACE);
if (proxy) {
map_send(proxy, argc, argv);
return;
@@ -828,12 +766,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, NULL, path, OBEX_OPP_INTERFACE);
if (proxy) {
opp_pull(proxy, argc, argv);
return;
@@ -913,34 +852,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 +890,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, NULL, 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, NULL, 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, NULL, path, OBEX_MAP_INTERFACE);
if (proxy) {
map_cd(proxy, argc, argv);
return;
@@ -1326,24 +1238,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, NULL, 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, NULL, 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, NULL, path, OBEX_MAP_INTERFACE);
if (proxy) {
map_ls(proxy, argc, argv);
return;
@@ -1652,7 +1565,7 @@ static void map_cp(GDBusProxy *proxy, int argc, char *argv[])
{
GDBusProxy *obj;

- obj = find_message(argv[1]);
+ obj = g_dbus_proxy_lookup(msgs, NULL, argv[1], OBEX_MSG_INTERFACE);
if (obj == NULL) {
bt_shell_printf("Invalid message argument\n");
return;
@@ -1669,25 +1582,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, NULL, 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, NULL, 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, NULL, path, OBEX_MAP_INTERFACE);
if (proxy) {
map_cp(proxy, argc, argv);
return;
@@ -1713,13 +1626,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, NULL, path, OBEX_FTP_INTERFACE);
if (proxy == NULL) {
bt_shell_printf("Command not supported\n");
return;
@@ -1783,7 +1697,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, NULL, argv[1], OBEX_MSG_INTERFACE);
if (msg == NULL) {
bt_shell_printf("Invalid message argument\n");
return;
@@ -1801,18 +1715,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, NULL, 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, NULL, path, OBEX_MAP_INTERFACE);
if (proxy) {
map_rm(proxy, argc, argv);
return;
@@ -1845,12 +1760,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, NULL, path, OBEX_FTP_INTERFACE);
if (proxy == NULL) {
bt_shell_printf("Command not supported\n");
return;
@@ -1902,7 +1818,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 +1885,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 +1903,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 +1975,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-28 05:44:07

by ERAMOTO Masaya

[permalink] [raw]
Subject: [PATCH BlueZ v2 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 | 98 +++++++++++++++++-------------------------------
1 file changed, 35 insertions(+), 63 deletions(-)

diff --git a/tools/bluetooth-player.c b/tools/bluetooth-player.c
index b22de2775..0ab8cab33 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, NULL, 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, NULL, 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, NULL, argv[1],
+ BLUEZ_MEDIA_PLAYER_INTERFACE);
if (!proxy) {
bt_shell_printf("Player %s not available\n", argv[1]);
return;
@@ -623,7 +584,9 @@ 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, NULL,
+ g_dbus_proxy_get_path(proxy),
+ BLUEZ_MEDIA_FOLDER_INTERFACE);
if (folder == NULL)
return;

@@ -637,7 +600,8 @@ 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, NULL, path,
+ BLUEZ_MEDIA_ITEM_INTERFACE);
if (item == NULL)
return;

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

- proxy = find_player(argv[1]);
+ proxy = g_dbus_proxy_lookup(players, NULL, argv[1],
+ BLUEZ_MEDIA_PLAYER_INTERFACE);
if (proxy == NULL) {
bt_shell_printf("Player %s not available\n", argv[1]);
return;
@@ -697,7 +662,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, NULL,
+ g_dbus_proxy_get_path(default_player),
+ BLUEZ_MEDIA_FOLDER_INTERFACE);
if (proxy == NULL) {
bt_shell_printf("Operation not supported\n");
return;
@@ -799,7 +766,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, NULL,
+ g_dbus_proxy_get_path(default_player),
+ BLUEZ_MEDIA_FOLDER_INTERFACE);
if (proxy == NULL) {
bt_shell_printf("Operation not supported\n");
return;
@@ -882,7 +851,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, NULL,
+ g_dbus_proxy_get_path(default_player),
+ BLUEZ_MEDIA_FOLDER_INTERFACE);
if (proxy == NULL) {
bt_shell_printf("Operation not supported\n");
return;
@@ -919,7 +890,8 @@ static void cmd_queue(int argc, char *argv[])
{
GDBusProxy *proxy;

- proxy = find_item(argv[1]);
+ proxy = g_dbus_proxy_lookup(items, NULL, argv[1],
+ BLUEZ_MEDIA_ITEM_INTERFACE);
if (proxy == NULL) {
bt_shell_printf("Item %s not available\n", argv[1]);
return;
@@ -970,7 +942,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 +964,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 +989,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 +1015,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-28 05:44:03

by ERAMOTO Masaya

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

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

diff --git a/client/gatt.c b/client/gatt.c
index 224a78a13..3d0222e63 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, NULL, service,
+ "org.bluez.GattService1") != NULL;
}

void gatt_add_characteristic(GDBusProxy *proxy)
@@ -378,33 +369,22 @@ 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, NULL, path,
+ "org.bluez.GattService1");
if (proxy)
return proxy;

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

- return select_proxy(path, descriptors);
+ return g_dbus_proxy_lookup(descriptors, NULL, 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..d3aee45e1 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, NULL, device,
+ "org.bluez.Device1") != NULL;
}

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


2017-12-28 05:43:59

by ERAMOTO Masaya

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

Also adds the following feature to g_dbus_proxy_lookup().
- It is more robust even if a proxy is NULL.
- It checks if the passed interface is NULL.
- It looks up from the position of the list specified by the index.
---
gdbus/client.c | 23 +++++++++++++++++------
gdbus/gdbus.h | 3 +++
2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/gdbus/client.c b/gdbus/client.c
index ab4059697..3768d2f59 100644
--- a/gdbus/client.c
+++ b/gdbus/client.c
@@ -352,16 +352,25 @@ 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, int *index, const char *path,
const char *interface)
{
GList *l;

- for (l = g_list_first(list); l; l = g_list_next(l)) {
+ if (!interface)
+ return NULL;
+
+ for (l = g_list_nth(list, index ? *index : 0); 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 (index)
+ (*index)++;
+
+ if (g_str_equal(proxy_iface, interface) == TRUE &&
+ g_str_equal(proxy_path, path) == TRUE)
return proxy;
}

@@ -519,7 +528,8 @@ 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, NULL,
+ path, interface);
if (proxy)
return g_dbus_proxy_ref(proxy);

@@ -992,7 +1002,8 @@ 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, NULL,
+ path, interface);
if (proxy && !proxy->pending) {
update_properties(proxy, iter, FALSE);
return;
diff --git a/gdbus/gdbus.h b/gdbus/gdbus.h
index e37385fa1..85cb9682e 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, int *index, 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


2018-01-02 16:29:44

by Luiz Augusto von Dentz

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

Hi Eramoto,

On Thu, Dec 28, 2017 at 3:36 AM, ERAMOTO Masaya
<[email protected]> wrote:
> This patch set adds generators for bluetooth-player and obexctl.
> bluetooth-player and obexctl also use GList instead of GSList.
>
> Changes in v2:
> - Add index to g_dbus_proxy_lookup().
> - Make g_dbus_proxy_lookup() check if the passed interface is NULL.
> - Rework callers of g_dbus_proxy_lookup() due to add the index argument.
> - Move strlen() outside loop.
> - Implement g_dbus_proxy_path_lookup() like g_dbus_proxy_lookup()
>
>
> 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 | 57 ++---------
> client/main.c | 15 +--
> gdbus/client.c | 44 ++++++--
> gdbus/gdbus.h | 4 +
> tools/bluetooth-player.c | 134 ++++++++++++-------------
> tools/obexctl.c | 254 ++++++++++++++++++-----------------------------
> 6 files changed, 221 insertions(+), 287 deletions(-)
>
> --
> 2.14.1

Applied, thanks.


--
Luiz Augusto von Dentz