--- /dev/null
+// Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by a apache 2.0 license that can be
+// found in the LICENSE file.
+
+#include "src/dbus_service.h"
+
+#include <glib.h>
+#include <gio/gio.h>
+
+#include <functional>
+#include <vector>
+
+#include "src/utils/logging.h"
+
+namespace {
+
+const char kDBusInstropectionXml[] =
+ "<node>"
+ " <interface name='org.tizen.capmgr'>"
+ " <method name='DiscoverUnownedDevice'>"
+ " <arg type='b' name='result' direction='out'/>"
+ " </method>"
+ " <method name='ExchangeCapabilities'>"
+ " <arg type='b' name='result' direction='out'/>"
+ " </method>"
+ " <method name='SendRemoteAppControl'>"
+ " <arg type='b' name='result' direction='out'/>"
+ " </method>"
+ " </interface>"
+ "</node>";
+const char kDBusServiceName[] = "org.tizen.capmgr";
+const char kDBusObjectPath[] = "/org/tizen/capmgr";
+
+bool HandleDiscoverUnownedDevice(GVariant* params,
+ GDBusMethodInvocation* invocation) {
+ // do something
+ return true;
+}
+
+bool HandleExchangeCapabilities(GVariant* params,
+ GDBusMethodInvocation* invocation) {
+ // do something
+ return true;
+}
+
+bool HandleSendRemoteAppControl(GVariant* params,
+ GDBusMethodInvocation* invocation) {
+ // do something
+ return true;
+}
+
+typedef std::function<bool(GVariant*, GDBusMethodInvocation*)> MethodHandler;
+const std::vector<std::pair<const gchar*, MethodHandler>> kHandlerList = {
+ {"DiscoverUnownedDevice", HandleDiscoverUnownedDevice},
+ {"ExchangeCapabilities", HandleExchangeCapabilities},
+ {"SendRemoteAppControl", HandleSendRemoteAppControl},
+};
+
+} // namespace
+
+namespace capmgr {
+
+DBusService::DBusService() {
+ node_info_ = g_dbus_node_info_new_for_xml(kDBusInstropectionXml, nullptr);
+ if (node_info_) {
+ owner_id_ = g_bus_own_name(G_BUS_TYPE_SYSTEM, kDBusServiceName,
+ G_BUS_NAME_OWNER_FLAGS_NONE,
+ [](GDBusConnection* connection, const gchar* name,
+ gpointer user_data) {
+ reinterpret_cast<DBusService*>(user_data)->OnBusAcquired(
+ connection, name, user_data);
+ },
+ nullptr, nullptr, this, nullptr);
+ } else {
+ LOG(ERROR) << "Failed to create DBus node info";
+ }
+}
+
+DBusService::~DBusService() {
+ if (owner_id_ > 0)
+ g_bus_unown_name(owner_id_);
+ if (node_info_)
+ g_dbus_node_info_unref(node_info_);
+}
+
+void DBusService::HandleMethodCall(GDBusConnection* /* connection */,
+ const gchar* /* sender */, const gchar* /* object_path */,
+ const gchar* /* interface_name */, const gchar* method_name,
+ GVariant* parameters, GDBusMethodInvocation* invocation,
+ gpointer /* user_data */) {
+ bool r = false;
+
+ LOG(INFO) << "Incomming method call: " << method_name;
+ for (auto& entry : kHandlerList) {
+ if (g_strcmp0(entry.first, method_name) != 0)
+ continue;
+ r = entry.second(parameters, invocation);
+ }
+
+ g_dbus_method_invocation_return_value(invocation, g_variant_new("(b)", r));
+}
+
+void DBusService::OnBusAcquired(
+ GDBusConnection* connection, const gchar* /* name */,
+ gpointer /* user_data */) {
+ GError* err = nullptr;
+ GDBusInterfaceVTable vtable = {
+ [](GDBusConnection* connection, const gchar* sender,
+ const gchar* object_path, const gchar* interface_name,
+ const gchar* method_name, GVariant* parameters,
+ GDBusMethodInvocation* invocation, gpointer user_data) {
+ reinterpret_cast<DBusService*>(user_data)->HandleMethodCall(
+ connection, sender, object_path, interface_name, method_name,
+ parameters, invocation, user_data);
+ },
+ nullptr, nullptr, {0, }
+ };
+
+ guint reg_id = g_dbus_connection_register_object(connection, kDBusObjectPath,
+ node_info_->interfaces[0], &vtable, this, nullptr, &err);
+ if (reg_id == 0) {
+ LOG(ERROR) << "Register failed";
+ if (err) {
+ LOG(ERROR) << "Error message: " << err->message;
+ g_error_free(err);
+ }
+ } else {
+ LOG(INFO) << "DBus service registered";
+ }
+}
+
+} // namespace capmgr