diff options
author | Marcel Holtmann <marcel@holtmann.org> | 2012-12-15 00:20:25 +0100 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2012-12-15 00:20:25 +0100 |
commit | d8d4b4f0c0ed5ddce7df3cf7ddad8322a9787c45 (patch) | |
tree | 162a8948c8107c84fccb207a48343b515d704305 | |
parent | 8590e5b610ae12584e13b3495ce3af895233aaff (diff) |
client: Add support for handling agent object registration
-rw-r--r-- | Makefile.tools | 4 | ||||
-rw-r--r-- | client/agent.c | 84 | ||||
-rw-r--r-- | client/agent.h | 25 | ||||
-rw-r--r-- | client/main.c | 47 |
4 files changed, 157 insertions, 3 deletions
diff --git a/Makefile.tools b/Makefile.tools index d9cb36c5b..6c381ca10 100644 --- a/Makefile.tools +++ b/Makefile.tools @@ -2,7 +2,9 @@ if CLIENT bin_PROGRAMS += client/bluetoothctl -client_bluetoothctl_SOURCES = $(gdbus_sources) client/main.c client/display.h +client_bluetoothctl_SOURCES = $(gdbus_sources) \ + client/main.c client/display.h \ + client/agent.h client/agent.c client_bluetoothctl_LDADD = @GLIB_LIBS@ @DBUS_LIBS@ -lreadline endif diff --git a/client/agent.c b/client/agent.c new file mode 100644 index 000000000..0a82e2191 --- /dev/null +++ b/client/agent.c @@ -0,0 +1,84 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2012 Intel Corporation. All rights reserved. + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdio.h> +#include <readline/readline.h> +#include <gdbus.h> + +#include "display.h" +#include "agent.h" + +static gboolean agent_registered = FALSE; + +static DBusMessage *release_agent(DBusConnection *conn, + DBusMessage *msg, void *user_data) +{ + agent_registered = FALSE; + + g_dbus_unregister_interface(conn, "/org/bluez/agent", + "org.bluez.Agent1"); + + return dbus_message_new_method_return(msg); +} + +static const GDBusMethodTable methods[] = { + { GDBUS_METHOD("Release", NULL, NULL, release_agent) }, + { } +}; + +void agent_register(DBusConnection *conn, GDBusProxy *manager) +{ + if (agent_registered == TRUE) { + printf("Agent is already registered\n"); + return; + } + + if (g_dbus_register_interface(conn, "/org/bluez/agent", + "org.bluez.Agent1", methods, + NULL, NULL, NULL, NULL) == FALSE) { + printf("Failed to register agent object\n"); + return; + } + + agent_registered = TRUE; +} + +void agent_unregister(DBusConnection *conn, GDBusProxy *manager) +{ + if (agent_registered == FALSE) { + printf("No agent is registered\n"); + return; + } + + if (g_dbus_unregister_interface(conn, "/org/bluez/agent", + "org.bluez.Agent1") == FALSE) { + printf("Failed to unregister agent object\n"); + return; + } + + agent_registered = FALSE; +} diff --git a/client/agent.h b/client/agent.h new file mode 100644 index 000000000..41e166d52 --- /dev/null +++ b/client/agent.h @@ -0,0 +1,25 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2012 Intel Corporation. All rights reserved. + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +void agent_register(DBusConnection *conn, GDBusProxy *manager); +void agent_unregister(DBusConnection *conn, GDBusProxy *manager); diff --git a/client/main.c b/client/main.c index 8e66ea8a1..39773454b 100644 --- a/client/main.c +++ b/client/main.c @@ -37,6 +37,7 @@ #include <glib.h> #include <gdbus.h> +#include "agent.h" #include "display.h" static GMainLoop *main_loop; @@ -44,6 +45,8 @@ static DBusConnection *dbus_conn; static GList *ctrl_list; static GDBusProxy *default_ctrl; +static GDBusProxy *agent_manager; +static gboolean auto_register_agent = FALSE; static void connect_handler(DBusConnection *connection, void *user_data) { @@ -137,6 +140,13 @@ static void proxy_added(GDBusProxy *proxy, void *user_data) begin_message(); print_adapter(proxy, "NEW"); end_message(); + } else if (!strcmp(interface, "org.bluez.AgentManager1")) { + if (!agent_manager) { + agent_manager = proxy; + + if (auto_register_agent == TRUE) + agent_register(dbus_conn, agent_manager); + } } } @@ -155,6 +165,9 @@ static void proxy_removed(GDBusProxy *proxy, void *user_data) if (default_ctrl == proxy) default_ctrl = NULL; + } else if (!strcmp(interface, "org.bluez.AgentManager1")) { + if (agent_manager == proxy) + agent_manager = NULL; } } @@ -395,6 +408,30 @@ static void cmd_discoverable(const char *arg) g_free(str); } +static void cmd_agent(const char *arg) +{ + dbus_bool_t enable; + + if (parse_argument_on_off(arg, &enable) == FALSE) + return; + + if (enable == TRUE) { + auto_register_agent = TRUE; + + if (agent_manager) + agent_register(dbus_conn, agent_manager); + else + printf("Agent registration enabled\n"); + } else { + auto_register_agent = FALSE; + + if (agent_manager) + agent_unregister(dbus_conn, agent_manager); + else + printf("Agent registration disabled\n"); + } +} + static void cmd_name(const char *arg) { char *name; @@ -474,8 +511,9 @@ static const struct { "Set controller pairable mode" }, { "discoverable", "<on/off>", cmd_discoverable, "Set controller discoverable mode" }, - { "quit", NULL, cmd_quit, "Quit program" }, - { "exit", NULL, cmd_quit }, + { "agent", "<on/off>", cmd_agent, "Enable/disable agent" }, + { "quit", NULL, cmd_quit, "Quit program" }, + { "exit", NULL, cmd_quit }, { "help" }, { } }; @@ -690,10 +728,13 @@ static guint setup_signalfd(void) } static gboolean option_version = FALSE; +static gboolean option_agent = FALSE; static GOptionEntry options[] = { { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version, "Show version information and exit" }, + { "agent", 'a', 0, G_OPTION_ARG_NONE, &option_agent, + "Automatically register agent handler" }, { NULL }, }; @@ -723,6 +764,8 @@ int main(int argc, char *argv[]) exit(0); } + auto_register_agent = option_agent; + main_loop = g_main_loop_new(NULL, FALSE); dbus_conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, NULL); |