opp.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * OBEX Client
  5. *
  6. * Copyright (C) 2011 Intel Corporation
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <errno.h>
  14. #include "gdbus/gdbus.h"
  15. #include "obexd/src/log.h"
  16. #include "transfer.h"
  17. #include "session.h"
  18. #include "driver.h"
  19. #include "opp.h"
  20. #define OPP_UUID "00001105-0000-1000-8000-00805f9b34fb"
  21. #define OPP_INTERFACE "org.bluez.obex.ObjectPush1"
  22. #define ERROR_INTERFACE "org.bluez.obex.Error"
  23. struct opp_data {
  24. struct obc_session *session;
  25. };
  26. static DBusConnection *conn = NULL;
  27. static DBusMessage *opp_send_file(DBusConnection *connection,
  28. DBusMessage *message, void *user_data)
  29. {
  30. struct opp_data *opp = user_data;
  31. struct obc_transfer *transfer;
  32. DBusMessage *reply;
  33. char *filename;
  34. char *basename;
  35. GError *err = NULL;
  36. if (dbus_message_get_args(message, NULL,
  37. DBUS_TYPE_STRING, &filename,
  38. DBUS_TYPE_INVALID) == FALSE)
  39. return g_dbus_create_error(message,
  40. ERROR_INTERFACE ".InvalidArguments", NULL);
  41. basename = g_path_get_basename(filename);
  42. transfer = obc_transfer_put(NULL, basename, filename, NULL, 0, &err);
  43. g_free(basename);
  44. if (transfer == NULL)
  45. goto fail;
  46. if (!obc_session_queue(opp->session, transfer, NULL, NULL, &err))
  47. goto fail;
  48. return obc_transfer_create_dbus_reply(transfer, message);
  49. fail:
  50. reply = g_dbus_create_error(message,
  51. ERROR_INTERFACE ".Failed", "%s", err->message);
  52. g_error_free(err);
  53. return reply;
  54. }
  55. static DBusMessage *opp_pull_business_card(DBusConnection *connection,
  56. DBusMessage *message, void *user_data)
  57. {
  58. struct opp_data *opp = user_data;
  59. struct obc_transfer *pull;
  60. DBusMessage *reply;
  61. const char *filename = NULL;
  62. GError *err = NULL;
  63. if (dbus_message_get_args(message, NULL,
  64. DBUS_TYPE_STRING, &filename,
  65. DBUS_TYPE_INVALID) == FALSE)
  66. return g_dbus_create_error(message,
  67. ERROR_INTERFACE ".InvalidArguments", NULL);
  68. pull = obc_transfer_get("text/x-vcard", NULL, filename, &err);
  69. if (pull == NULL)
  70. goto fail;
  71. if (!obc_session_queue(opp->session, pull, NULL, NULL, &err))
  72. goto fail;
  73. return obc_transfer_create_dbus_reply(pull, message);
  74. fail:
  75. reply = g_dbus_create_error(message,
  76. ERROR_INTERFACE ".Failed", "%s", err->message);
  77. g_error_free(err);
  78. return reply;
  79. }
  80. static DBusMessage *opp_exchange_business_cards(DBusConnection *connection,
  81. DBusMessage *message, void *user_data)
  82. {
  83. return g_dbus_create_error(message, ERROR_INTERFACE ".Failed",
  84. "Not Implemented");
  85. }
  86. static const GDBusMethodTable opp_methods[] = {
  87. { GDBUS_METHOD("SendFile",
  88. GDBUS_ARGS({ "sourcefile", "s" }),
  89. GDBUS_ARGS({ "transfer", "o" }, { "properties", "a{sv}" }),
  90. opp_send_file) },
  91. { GDBUS_METHOD("PullBusinessCard",
  92. GDBUS_ARGS({ "targetfile", "s" }),
  93. GDBUS_ARGS({ "transfer", "o" }, { "properties", "a{sv}" }),
  94. opp_pull_business_card) },
  95. { GDBUS_METHOD("ExchangeBusinessCards",
  96. GDBUS_ARGS({ "clientfile", "s" }, { "targetfile", "s" }),
  97. GDBUS_ARGS({ "transfer", "o" }, { "properties", "a{sv}" }),
  98. opp_exchange_business_cards) },
  99. { }
  100. };
  101. static void opp_free(void *data)
  102. {
  103. struct opp_data *opp = data;
  104. obc_session_unref(opp->session);
  105. g_free(opp);
  106. }
  107. static int opp_probe(struct obc_session *session)
  108. {
  109. struct opp_data *opp;
  110. const char *path;
  111. path = obc_session_get_path(session);
  112. DBG("%s", path);
  113. opp = g_try_new0(struct opp_data, 1);
  114. if (!opp)
  115. return -ENOMEM;
  116. opp->session = obc_session_ref(session);
  117. if (!g_dbus_register_interface(conn, path, OPP_INTERFACE, opp_methods,
  118. NULL, NULL, opp, opp_free)) {
  119. opp_free(opp);
  120. return -ENOMEM;
  121. }
  122. return 0;
  123. }
  124. static void opp_remove(struct obc_session *session)
  125. {
  126. const char *path = obc_session_get_path(session);
  127. DBG("%s", path);
  128. g_dbus_unregister_interface(conn, path, OPP_INTERFACE);
  129. }
  130. static struct obc_driver opp = {
  131. .service = "OPP",
  132. .uuid = OPP_UUID,
  133. .probe = opp_probe,
  134. .remove = opp_remove
  135. };
  136. int opp_init(void)
  137. {
  138. int err;
  139. DBG("");
  140. conn = dbus_bus_get(DBUS_BUS_SESSION, NULL);
  141. if (!conn)
  142. return -EIO;
  143. err = obc_driver_register(&opp);
  144. if (err < 0) {
  145. dbus_connection_unref(conn);
  146. conn = NULL;
  147. return err;
  148. }
  149. return 0;
  150. }
  151. void opp_exit(void)
  152. {
  153. DBG("");
  154. dbus_connection_unref(conn);
  155. conn = NULL;
  156. obc_driver_unregister(&opp);
  157. }