2009-01-19 06:18:33 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* OBEX Client
|
|
|
|
*
|
2010-01-07 18:42:04 +08:00
|
|
|
* Copyright (C) 2007-2010 Intel Corporation
|
2010-01-02 09:09:14 +08:00
|
|
|
* Copyright (C) 2007-2010 Marcel Holtmann <marcel@holtmann.org>
|
2009-01-19 06:18:33 +08:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-12-07 18:46:04 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2011-08-12 16:45:14 +08:00
|
|
|
#include <errno.h>
|
2012-04-24 20:03:10 +08:00
|
|
|
#include <string.h>
|
2011-08-12 16:45:14 +08:00
|
|
|
|
2009-01-19 06:18:33 +08:00
|
|
|
#include <glib.h>
|
2012-12-05 19:22:52 +08:00
|
|
|
#include <gdbus/gdbus.h>
|
2009-01-19 06:18:33 +08:00
|
|
|
|
2011-08-12 16:45:14 +08:00
|
|
|
#include "log.h"
|
|
|
|
|
2012-05-03 16:28:27 +08:00
|
|
|
#include "transfer.h"
|
2009-01-19 06:18:33 +08:00
|
|
|
#include "session.h"
|
2011-08-12 16:45:14 +08:00
|
|
|
#include "driver.h"
|
2009-01-19 11:29:23 +08:00
|
|
|
#include "sync.h"
|
2009-01-19 06:18:33 +08:00
|
|
|
|
2011-09-01 17:47:52 +08:00
|
|
|
#define OBEX_SYNC_UUID "IRMC-SYNC"
|
|
|
|
#define OBEX_SYNC_UUID_LEN 9
|
|
|
|
|
2012-12-21 02:38:16 +08:00
|
|
|
#define SYNC_INTERFACE "org.bluez.obex.Synchronization1"
|
2009-01-19 06:18:33 +08:00
|
|
|
#define ERROR_INF SYNC_INTERFACE ".Error"
|
2011-08-12 16:45:14 +08:00
|
|
|
#define SYNC_UUID "00001104-0000-1000-8000-00805f9b34fb"
|
2009-01-19 06:18:33 +08:00
|
|
|
|
|
|
|
struct sync_data {
|
2011-08-12 16:45:10 +08:00
|
|
|
struct obc_session *session;
|
2009-01-19 06:18:33 +08:00
|
|
|
char *phonebook_path;
|
2011-07-18 18:35:12 +08:00
|
|
|
DBusMessage *msg;
|
2009-01-19 06:18:33 +08:00
|
|
|
};
|
|
|
|
|
2011-08-12 16:45:14 +08:00
|
|
|
static DBusConnection *conn = NULL;
|
|
|
|
|
2009-01-19 06:18:33 +08:00
|
|
|
static DBusMessage *sync_setlocation(DBusConnection *connection,
|
|
|
|
DBusMessage *message, void *user_data)
|
|
|
|
{
|
2011-07-18 18:35:12 +08:00
|
|
|
struct sync_data *sync = user_data;
|
2009-01-19 06:18:33 +08:00
|
|
|
const char *location;
|
|
|
|
char *path = NULL, *tmp;
|
|
|
|
|
|
|
|
if (dbus_message_get_args(message, NULL,
|
|
|
|
DBUS_TYPE_STRING, &location,
|
|
|
|
DBUS_TYPE_INVALID) == FALSE)
|
|
|
|
return g_dbus_create_error(message,
|
|
|
|
ERROR_INF ".InvalidArguments", NULL);
|
|
|
|
|
2012-12-23 00:30:05 +08:00
|
|
|
if (!g_ascii_strcasecmp(location, "int") ||
|
|
|
|
!g_ascii_strcasecmp(location, "internal"))
|
2009-01-19 06:18:33 +08:00
|
|
|
path = g_strdup("telecom/pb.vcf");
|
2012-12-23 00:30:05 +08:00
|
|
|
else if (!g_ascii_strncasecmp(location, "sim", 3)) {
|
2009-01-19 06:18:33 +08:00
|
|
|
tmp = g_ascii_strup(location, 4);
|
|
|
|
path = g_build_filename(tmp, "telecom/pb.vcf", NULL);
|
|
|
|
g_free(tmp);
|
|
|
|
} else
|
|
|
|
return g_dbus_create_error(message,
|
|
|
|
ERROR_INF ".InvalidArguments", "InvalidPhonebook");
|
|
|
|
|
2011-07-18 18:35:12 +08:00
|
|
|
g_free(sync->phonebook_path);
|
|
|
|
sync->phonebook_path = path;
|
2009-01-19 06:18:33 +08:00
|
|
|
|
|
|
|
return dbus_message_new_method_return(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DBusMessage *sync_getphonebook(DBusConnection *connection,
|
|
|
|
DBusMessage *message, void *user_data)
|
|
|
|
{
|
2011-07-18 18:35:12 +08:00
|
|
|
struct sync_data *sync = user_data;
|
2012-05-04 20:39:38 +08:00
|
|
|
struct obc_transfer *transfer;
|
2012-06-04 17:37:52 +08:00
|
|
|
const char *target_file;
|
2012-04-26 23:11:04 +08:00
|
|
|
GError *err = NULL;
|
2012-05-04 20:39:38 +08:00
|
|
|
DBusMessage *reply;
|
2009-01-21 12:22:29 +08:00
|
|
|
|
2012-06-04 17:37:52 +08:00
|
|
|
if (dbus_message_get_args(message, NULL,
|
|
|
|
DBUS_TYPE_STRING, &target_file,
|
|
|
|
DBUS_TYPE_INVALID) == FALSE)
|
|
|
|
return g_dbus_create_error(message,
|
|
|
|
ERROR_INF ".InvalidArguments",
|
|
|
|
"Invalid arguments in method call");
|
|
|
|
|
2011-07-18 18:35:12 +08:00
|
|
|
if (sync->msg)
|
2009-01-21 12:22:29 +08:00
|
|
|
return g_dbus_create_error(message,
|
|
|
|
ERROR_INF ".InProgress", "Transfer in progress");
|
|
|
|
|
|
|
|
/* set default phonebook_path to memory internal phonebook */
|
2011-07-18 18:35:12 +08:00
|
|
|
if (!sync->phonebook_path)
|
|
|
|
sync->phonebook_path = g_strdup("telecom/pb.vcf");
|
2009-01-21 12:22:29 +08:00
|
|
|
|
2012-06-04 17:37:52 +08:00
|
|
|
transfer = obc_transfer_get("phonebook", sync->phonebook_path,
|
|
|
|
target_file, &err);
|
2012-05-04 20:39:38 +08:00
|
|
|
if (transfer == NULL)
|
|
|
|
goto fail;
|
2009-01-21 12:22:29 +08:00
|
|
|
|
2012-06-04 17:37:52 +08:00
|
|
|
if (!obc_session_queue(sync->session, transfer, NULL, NULL, &err))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return obc_transfer_create_dbus_reply(transfer, message);
|
2009-01-21 12:22:29 +08:00
|
|
|
|
2012-05-04 20:39:38 +08:00
|
|
|
fail:
|
|
|
|
reply = g_dbus_create_error(message, ERROR_INF ".Failed", "%s",
|
|
|
|
err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
return reply;
|
2009-01-19 06:18:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static DBusMessage *sync_putphonebook(DBusConnection *connection,
|
|
|
|
DBusMessage *message, void *user_data)
|
|
|
|
{
|
2011-07-18 18:35:12 +08:00
|
|
|
struct sync_data *sync = user_data;
|
2012-05-04 20:39:38 +08:00
|
|
|
struct obc_transfer *transfer;
|
2012-06-04 17:37:52 +08:00
|
|
|
const char *source_file;
|
2012-04-26 23:11:04 +08:00
|
|
|
GError *err = NULL;
|
2012-05-04 20:39:38 +08:00
|
|
|
DBusMessage *reply;
|
2009-01-23 06:11:55 +08:00
|
|
|
|
|
|
|
if (dbus_message_get_args(message, NULL,
|
2012-06-04 17:37:52 +08:00
|
|
|
DBUS_TYPE_STRING, &source_file,
|
|
|
|
DBUS_TYPE_INVALID) == FALSE)
|
2009-01-23 06:11:55 +08:00
|
|
|
return g_dbus_create_error(message,
|
2012-06-04 17:37:52 +08:00
|
|
|
ERROR_INF ".InvalidArguments",
|
|
|
|
"Invalid arguments in method call");
|
2009-01-23 06:11:55 +08:00
|
|
|
|
|
|
|
/* set default phonebook_path to memory internal phonebook */
|
2011-07-18 18:35:12 +08:00
|
|
|
if (!sync->phonebook_path)
|
|
|
|
sync->phonebook_path = g_strdup("telecom/pb.vcf");
|
2009-01-23 06:11:55 +08:00
|
|
|
|
2012-06-04 17:37:52 +08:00
|
|
|
transfer = obc_transfer_put(NULL, sync->phonebook_path, source_file,
|
|
|
|
NULL, 0, &err);
|
2012-05-04 20:39:38 +08:00
|
|
|
if (transfer == NULL)
|
|
|
|
goto fail;
|
2009-01-23 06:11:55 +08:00
|
|
|
|
2012-06-04 17:37:52 +08:00
|
|
|
if (!obc_session_queue(sync->session, transfer, NULL, NULL, &err))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return obc_transfer_create_dbus_reply(transfer, message);
|
2012-05-04 20:39:38 +08:00
|
|
|
|
|
|
|
fail:
|
|
|
|
reply = g_dbus_create_error(message, ERROR_INF ".Failed", "%s",
|
|
|
|
err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
return reply;
|
2009-01-19 06:18:33 +08:00
|
|
|
}
|
|
|
|
|
2012-05-18 11:34:07 +08:00
|
|
|
static const GDBusMethodTable sync_methods[] = {
|
2012-05-19 02:32:04 +08:00
|
|
|
{ GDBUS_METHOD("SetLocation",
|
2012-05-19 02:32:01 +08:00
|
|
|
GDBUS_ARGS({ "location", "s" }), NULL,
|
|
|
|
sync_setlocation) },
|
2012-06-04 17:37:52 +08:00
|
|
|
{ GDBUS_METHOD("GetPhonebook",
|
|
|
|
GDBUS_ARGS({ "targetfile", "s" }),
|
|
|
|
GDBUS_ARGS({ "transfer", "o" },
|
|
|
|
{ "properties", "a{sv}" }),
|
2012-05-19 02:32:01 +08:00
|
|
|
sync_getphonebook) },
|
2012-06-04 17:37:52 +08:00
|
|
|
{ GDBUS_METHOD("PutPhonebook",
|
|
|
|
GDBUS_ARGS({ "sourcefile", "s" }),
|
|
|
|
GDBUS_ARGS({ "transfer", "o" },
|
|
|
|
{ "properties", "a{sv}" }),
|
2012-05-19 02:32:01 +08:00
|
|
|
sync_putphonebook) },
|
|
|
|
{ }
|
2009-01-19 06:18:33 +08:00
|
|
|
};
|
|
|
|
|
2011-07-18 18:35:12 +08:00
|
|
|
static void sync_free(void *data)
|
|
|
|
{
|
|
|
|
struct sync_data *sync = data;
|
|
|
|
|
2011-08-12 16:45:10 +08:00
|
|
|
obc_session_unref(sync->session);
|
2011-07-18 18:35:12 +08:00
|
|
|
g_free(sync->phonebook_path);
|
|
|
|
g_free(sync);
|
|
|
|
}
|
|
|
|
|
2011-08-12 16:45:14 +08:00
|
|
|
static int sync_probe(struct obc_session *session)
|
2009-01-19 06:18:33 +08:00
|
|
|
{
|
2011-07-18 18:35:12 +08:00
|
|
|
struct sync_data *sync;
|
2011-08-12 16:45:14 +08:00
|
|
|
const char *path;
|
|
|
|
|
|
|
|
path = obc_session_get_path(session);
|
|
|
|
|
|
|
|
DBG("%s", path);
|
2009-01-19 06:18:33 +08:00
|
|
|
|
2011-07-18 18:35:12 +08:00
|
|
|
sync = g_try_new0(struct sync_data, 1);
|
|
|
|
if (!sync)
|
2011-08-12 16:45:14 +08:00
|
|
|
return -ENOMEM;
|
2009-01-19 06:18:33 +08:00
|
|
|
|
2011-08-12 16:45:10 +08:00
|
|
|
sync->session = obc_session_ref(session);
|
2009-01-19 06:18:33 +08:00
|
|
|
|
2011-08-12 16:45:14 +08:00
|
|
|
if (!g_dbus_register_interface(conn, path, SYNC_INTERFACE, sync_methods,
|
|
|
|
NULL, NULL, sync, sync_free)) {
|
2011-07-18 18:35:12 +08:00
|
|
|
sync_free(sync);
|
2011-08-12 16:45:14 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sync_remove(struct obc_session *session)
|
|
|
|
{
|
|
|
|
const char *path = obc_session_get_path(session);
|
|
|
|
|
|
|
|
DBG("%s", path);
|
|
|
|
|
|
|
|
g_dbus_unregister_interface(conn, path, SYNC_INTERFACE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct obc_driver sync = {
|
|
|
|
.service = "SYNC",
|
|
|
|
.uuid = SYNC_UUID,
|
|
|
|
.target = OBEX_SYNC_UUID,
|
|
|
|
.target_len = OBEX_SYNC_UUID_LEN,
|
|
|
|
.probe = sync_probe,
|
|
|
|
.remove = sync_remove
|
|
|
|
};
|
|
|
|
|
|
|
|
int sync_init(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
DBG("");
|
|
|
|
|
|
|
|
conn = dbus_bus_get(DBUS_BUS_SESSION, NULL);
|
|
|
|
if (!conn)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
err = obc_driver_register(&sync);
|
|
|
|
if (err < 0) {
|
|
|
|
dbus_connection_unref(conn);
|
|
|
|
conn = NULL;
|
|
|
|
return err;
|
2011-07-18 18:35:12 +08:00
|
|
|
}
|
|
|
|
|
2011-08-12 16:45:14 +08:00
|
|
|
return 0;
|
2009-01-19 06:18:33 +08:00
|
|
|
}
|
|
|
|
|
2011-08-12 16:45:14 +08:00
|
|
|
void sync_exit(void)
|
2009-01-19 06:18:33 +08:00
|
|
|
{
|
2011-08-12 16:45:14 +08:00
|
|
|
DBG("");
|
|
|
|
|
|
|
|
dbus_connection_unref(conn);
|
|
|
|
conn = NULL;
|
|
|
|
|
|
|
|
obc_driver_unregister(&sync);
|
2009-01-19 06:18:33 +08:00
|
|
|
}
|