bluez/client/agent.c

161 lines
3.9 KiB
C

/*
*
* 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"
#define AGENT_PATH "/org/bluez/agent"
#define AGENT_INTERFACE "org.bluez.Agent1"
static gboolean agent_registered = FALSE;
static DBusMessage *release_agent(DBusConnection *conn,
DBusMessage *msg, void *user_data)
{
agent_registered = FALSE;
begin_message();
printf("Agent released\n");
end_message();
g_dbus_unregister_interface(conn, AGENT_PATH, AGENT_INTERFACE);
return dbus_message_new_method_return(msg);
}
static const GDBusMethodTable methods[] = {
{ GDBUS_METHOD("Release", NULL, NULL, release_agent) },
{ }
};
static void register_agent_setup(DBusMessageIter *iter, void *user_data)
{
const char *path = AGENT_PATH;
const char *capability = "";
dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &capability);
}
static void register_agent_reply(DBusMessage *message, void *user_data)
{
DBusConnection *conn = user_data;
DBusError error;
begin_message();
dbus_error_init(&error);
if (dbus_set_error_from_message(&error, message) == FALSE) {
agent_registered = TRUE;
printf("Agent registered\n");
} else {
printf("Failed to register agent: %s\n", error.name);
dbus_error_free(&error);
if (g_dbus_unregister_interface(conn, AGENT_PATH,
AGENT_INTERFACE) == FALSE)
printf("Failed to unregister agent object\n");
}
end_message();
}
void agent_register(DBusConnection *conn, GDBusProxy *manager)
{
if (agent_registered == TRUE) {
printf("Agent is already registered\n");
return;
}
if (g_dbus_register_interface(conn, AGENT_PATH,
AGENT_INTERFACE, methods,
NULL, NULL, NULL, NULL) == FALSE) {
printf("Failed to register agent object\n");
return;
}
if (g_dbus_proxy_method_call(manager, "RegisterAgent",
register_agent_setup,
register_agent_reply,
conn, NULL) == FALSE) {
printf("Failed to call register agent method\n");
return;
}
}
static void unregister_agent_setup(DBusMessageIter *iter, void *user_data)
{
const char *path = AGENT_PATH;
dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
}
static void unregister_agent_reply(DBusMessage *message, void *user_data)
{
DBusConnection *conn = user_data;
DBusError error;
begin_message();
dbus_error_init(&error);
if (dbus_set_error_from_message(&error, message) == FALSE) {
agent_registered = FALSE;
printf("Agent unregistered\n");
if (g_dbus_unregister_interface(conn, AGENT_PATH,
AGENT_INTERFACE) == FALSE)
printf("Failed to unregister agent object\n");
} else {
printf("Failed to unregister agent: %s\n", error.name);
dbus_error_free(&error);
}
end_message();
}
void agent_unregister(DBusConnection *conn, GDBusProxy *manager)
{
if (agent_registered == FALSE) {
printf("No agent is registered\n");
return;
}
if (g_dbus_proxy_method_call(manager, "UnregisterAgent",
unregister_agent_setup,
unregister_agent_reply,
conn, NULL) == FALSE) {
printf("Failed to call unregister agent method\n");
return;
}
}