sync.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * OBEX Client
  5. *
  6. * Copyright (C) 2007-2010 Intel Corporation
  7. * Copyright (C) 2007-2010 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #include <errno.h>
  15. #include <string.h>
  16. #include <glib.h>
  17. #include "gdbus/gdbus.h"
  18. #include "obexd/src/log.h"
  19. #include "transfer.h"
  20. #include "session.h"
  21. #include "driver.h"
  22. #include "sync.h"
  23. #define OBEX_SYNC_UUID "IRMC-SYNC"
  24. #define OBEX_SYNC_UUID_LEN 9
  25. #define SYNC_INTERFACE "org.bluez.obex.Synchronization1"
  26. #define ERROR_INF SYNC_INTERFACE ".Error"
  27. #define SYNC_UUID "00001104-0000-1000-8000-00805f9b34fb"
  28. struct sync_data {
  29. struct obc_session *session;
  30. char *phonebook_path;
  31. DBusMessage *msg;
  32. };
  33. static DBusConnection *conn = NULL;
  34. static DBusMessage *sync_setlocation(DBusConnection *connection,
  35. DBusMessage *message, void *user_data)
  36. {
  37. struct sync_data *sync = user_data;
  38. const char *location;
  39. char *path = NULL, *tmp;
  40. if (dbus_message_get_args(message, NULL,
  41. DBUS_TYPE_STRING, &location,
  42. DBUS_TYPE_INVALID) == FALSE)
  43. return g_dbus_create_error(message,
  44. ERROR_INF ".InvalidArguments", NULL);
  45. if (!g_ascii_strcasecmp(location, "int") ||
  46. !g_ascii_strcasecmp(location, "internal"))
  47. path = g_strdup("telecom/pb.vcf");
  48. else if (!g_ascii_strncasecmp(location, "sim", 3)) {
  49. tmp = g_ascii_strup(location, 4);
  50. path = g_build_filename(tmp, "telecom/pb.vcf", NULL);
  51. g_free(tmp);
  52. } else
  53. return g_dbus_create_error(message,
  54. ERROR_INF ".InvalidArguments", "InvalidPhonebook");
  55. g_free(sync->phonebook_path);
  56. sync->phonebook_path = path;
  57. return dbus_message_new_method_return(message);
  58. }
  59. static DBusMessage *sync_getphonebook(DBusConnection *connection,
  60. DBusMessage *message, void *user_data)
  61. {
  62. struct sync_data *sync = user_data;
  63. struct obc_transfer *transfer;
  64. const char *target_file;
  65. GError *err = NULL;
  66. DBusMessage *reply;
  67. if (dbus_message_get_args(message, NULL,
  68. DBUS_TYPE_STRING, &target_file,
  69. DBUS_TYPE_INVALID) == FALSE)
  70. return g_dbus_create_error(message,
  71. ERROR_INF ".InvalidArguments",
  72. "Invalid arguments in method call");
  73. if (sync->msg)
  74. return g_dbus_create_error(message,
  75. ERROR_INF ".InProgress", "Transfer in progress");
  76. /* set default phonebook_path to memory internal phonebook */
  77. if (!sync->phonebook_path)
  78. sync->phonebook_path = g_strdup("telecom/pb.vcf");
  79. transfer = obc_transfer_get("phonebook", sync->phonebook_path,
  80. target_file, &err);
  81. if (transfer == NULL)
  82. goto fail;
  83. if (!obc_session_queue(sync->session, transfer, NULL, NULL, &err))
  84. goto fail;
  85. return obc_transfer_create_dbus_reply(transfer, message);
  86. fail:
  87. reply = g_dbus_create_error(message, ERROR_INF ".Failed", "%s",
  88. err->message);
  89. g_error_free(err);
  90. return reply;
  91. }
  92. static DBusMessage *sync_putphonebook(DBusConnection *connection,
  93. DBusMessage *message, void *user_data)
  94. {
  95. struct sync_data *sync = user_data;
  96. struct obc_transfer *transfer;
  97. const char *source_file;
  98. GError *err = NULL;
  99. DBusMessage *reply;
  100. if (dbus_message_get_args(message, NULL,
  101. DBUS_TYPE_STRING, &source_file,
  102. DBUS_TYPE_INVALID) == FALSE)
  103. return g_dbus_create_error(message,
  104. ERROR_INF ".InvalidArguments",
  105. "Invalid arguments in method call");
  106. /* set default phonebook_path to memory internal phonebook */
  107. if (!sync->phonebook_path)
  108. sync->phonebook_path = g_strdup("telecom/pb.vcf");
  109. transfer = obc_transfer_put(NULL, sync->phonebook_path, source_file,
  110. NULL, 0, &err);
  111. if (transfer == NULL)
  112. goto fail;
  113. if (!obc_session_queue(sync->session, transfer, NULL, NULL, &err))
  114. goto fail;
  115. return obc_transfer_create_dbus_reply(transfer, message);
  116. fail:
  117. reply = g_dbus_create_error(message, ERROR_INF ".Failed", "%s",
  118. err->message);
  119. g_error_free(err);
  120. return reply;
  121. }
  122. static const GDBusMethodTable sync_methods[] = {
  123. { GDBUS_METHOD("SetLocation",
  124. GDBUS_ARGS({ "location", "s" }), NULL,
  125. sync_setlocation) },
  126. { GDBUS_METHOD("GetPhonebook",
  127. GDBUS_ARGS({ "targetfile", "s" }),
  128. GDBUS_ARGS({ "transfer", "o" },
  129. { "properties", "a{sv}" }),
  130. sync_getphonebook) },
  131. { GDBUS_METHOD("PutPhonebook",
  132. GDBUS_ARGS({ "sourcefile", "s" }),
  133. GDBUS_ARGS({ "transfer", "o" },
  134. { "properties", "a{sv}" }),
  135. sync_putphonebook) },
  136. { }
  137. };
  138. static void sync_free(void *data)
  139. {
  140. struct sync_data *sync = data;
  141. obc_session_unref(sync->session);
  142. g_free(sync->phonebook_path);
  143. g_free(sync);
  144. }
  145. static int sync_probe(struct obc_session *session)
  146. {
  147. struct sync_data *sync;
  148. const char *path;
  149. path = obc_session_get_path(session);
  150. DBG("%s", path);
  151. sync = g_try_new0(struct sync_data, 1);
  152. if (!sync)
  153. return -ENOMEM;
  154. sync->session = obc_session_ref(session);
  155. if (!g_dbus_register_interface(conn, path, SYNC_INTERFACE, sync_methods,
  156. NULL, NULL, sync, sync_free)) {
  157. sync_free(sync);
  158. return -ENOMEM;
  159. }
  160. return 0;
  161. }
  162. static void sync_remove(struct obc_session *session)
  163. {
  164. const char *path = obc_session_get_path(session);
  165. DBG("%s", path);
  166. g_dbus_unregister_interface(conn, path, SYNC_INTERFACE);
  167. }
  168. static struct obc_driver sync = {
  169. .service = "SYNC",
  170. .uuid = SYNC_UUID,
  171. .target = OBEX_SYNC_UUID,
  172. .target_len = OBEX_SYNC_UUID_LEN,
  173. .probe = sync_probe,
  174. .remove = sync_remove
  175. };
  176. int sync_init(void)
  177. {
  178. int err;
  179. DBG("");
  180. conn = dbus_bus_get(DBUS_BUS_SESSION, NULL);
  181. if (!conn)
  182. return -EIO;
  183. err = obc_driver_register(&sync);
  184. if (err < 0) {
  185. dbus_connection_unref(conn);
  186. conn = NULL;
  187. return err;
  188. }
  189. return 0;
  190. }
  191. void sync_exit(void)
  192. {
  193. DBG("");
  194. dbus_connection_unref(conn);
  195. conn = NULL;
  196. obc_driver_unregister(&sync);
  197. }