Add basic dbus services
authorSangyoon Jang <jeremy.jang@samsung.com>
Fri, 23 Mar 2018 06:52:05 +0000 (15:52 +0900)
committer장상윤/Tizen Platform Lab(SR)/Engineer/삼성전자 <jeremy.jang@samsung.com>
Mon, 26 Mar 2018 09:01:20 +0000 (18:01 +0900)
Change-Id: I3100158e182474074e6dc1d79fd1794e2d5621bf

CMakeLists.txt
capability-manager.service [new file with mode: 0644]
org.tizen.capmgr.conf [new file with mode: 0644]
packaging/capmgr.spec
src/dbus_service.cc [new file with mode: 0644]
src/dbus_service.h [new file with mode: 0644]
src/main.cc

index aaf921a03cd304695ac7745f2227e849b44a35e8..2cbeb4c4b81de2c78b474268a7d990709e868df7 100644 (file)
@@ -28,7 +28,7 @@ SET(CAPMGR "capmgr")
 AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/src SRC)
 AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/src/utils SRC_UTILS)
 
-SET(SERVER_CHECK_MODULES dlog glib-2.0 iotivity jsoncpp pkgmgr pkgmgr-info)
+SET(SERVER_CHECK_MODULES dlog gio-2.0 glib-2.0 iotivity jsoncpp pkgmgr pkgmgr-info)
 
 PKG_CHECK_MODULES(DEPS REQUIRED ${SERVER_CHECK_MODULES})
 
@@ -42,3 +42,5 @@ TARGET_LINK_LIBRARIES(${CAPMGR} ${DEPS_LDFLAGS})
 
 INSTALL(TARGETS ${CAPMGR} DESTINATION bin)
 INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/data/oic_svr_db_capmgr.dat DESTINATION ${SVR_DB_DIR})
+INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/org.tizen.capmgr.conf DESTINATION ${SYSCONF_INSTALL_DIR}/dbus-1/system.d/)
+INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/capability-manager.service DESTINATION ${UNITDIR})
diff --git a/capability-manager.service b/capability-manager.service
new file mode 100644 (file)
index 0000000..e1972a8
--- /dev/null
@@ -0,0 +1,8 @@
+[Unit]
+Description=Capability Manager
+
+[Service]
+User=app_fw
+Group=app_fw
+SmackProcessLabel=System
+ExecStart=/usr/bin/capmgr
diff --git a/org.tizen.capmgr.conf b/org.tizen.capmgr.conf
new file mode 100644 (file)
index 0000000..77fe974
--- /dev/null
@@ -0,0 +1,15 @@
+<?xml version="1.0"?>
+<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
+    "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
+
+<busconfig>
+  <policy user="root">
+    <allow own="org.tizen.capmgr"/>
+  </policy>
+  <policy user="app_fw">
+    <allow own="org.tizen.capmgr"/>
+  </policy>
+  <policy context="default">
+    <allow send_destination="org.tizen.capmgr" send_interface="org.tizen.capmgr"/>
+  </policy>
+</busconfig>
index 7b2c387eaf0b42324197960aeb432c9968a648b5..e997e94bceafd0e55187460eaa5a029a64fb3e43 100644 (file)
@@ -12,7 +12,7 @@ Source1001: %{name}.manifest
 BuildRequires:  boost-devel
 BuildRequires:  cmake
 BuildRequires:  pkgconfig(dlog)
-#BuildRequires:  pkgconfig(gio-2.0)
+BuildRequires:  pkgconfig(gio-2.0)
 BuildRequires:  pkgconfig(glib-2.0)
 BuildRequires:  pkgconfig(iotivity)
 BuildRequires:  pkgconfig(jsoncpp)
@@ -29,7 +29,8 @@ cp %{SOURCE1001} .
 
 %build
 %cmake . -DSVR_DB_DIR=%{svr_db_dir} \
-         -DSVR_DB_PATH=%{svr_db_path}
+         -DSVR_DB_PATH=%{svr_db_path} \
+         -DUNITDIR=%{_unitdir}
 
 %__make %{?_smp_mflags}
 
@@ -37,6 +38,8 @@ cp %{SOURCE1001} .
 %make_install
 
 mkdir -p %{buildroot}%{svr_db_dir}
+mkdir -p %{buildroot}%{_unitdir}/multi-user.target.wants
+ln -sf ../capability-manager.service %{buildroot}%{_unitdir}/multi-user.target.wants/capability-manager.service
 
 %post
 /sbin/ldconfig
@@ -47,3 +50,6 @@ mkdir -p %{buildroot}%{svr_db_dir}
 %defattr(-,root,root,-)
 %{_bindir}/*
 %{svr_db_path}
+%{_sysconfdir}/dbus-1/system.d/org.tizen.capmgr.conf
+%{_unitdir}/capability-manager.service
+%{_unitdir}/multi-user.target.wants/capability-manager.service
diff --git a/src/dbus_service.cc b/src/dbus_service.cc
new file mode 100644 (file)
index 0000000..7ed6f4d
--- /dev/null
@@ -0,0 +1,132 @@
+// 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
diff --git a/src/dbus_service.h b/src/dbus_service.h
new file mode 100644 (file)
index 0000000..867964d
--- /dev/null
@@ -0,0 +1,34 @@
+// 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.
+
+#ifndef DBUS_SERVICE_H_
+#define DBUS_SERVICE_H_
+
+#include <glib.h>
+#include <gio/gio.h>
+
+namespace capmgr {
+
+class DBusService {
+ public:
+  DBusService();
+  ~DBusService();
+
+ private:
+  void 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);
+  void OnBusAcquired(GDBusConnection* connection, const gchar* name,
+      gpointer user_data);
+
+  GDBusNodeInfo* node_info_;
+  guint owner_id_;
+  guint sid_;
+};
+
+}  // namespace capmgr
+
+#endif  // DBUS_SERVICE_H_
index 499a0cb14165ed02194baf87e421b9f65b20df9c..5a45f35e3066dc4ec07456e059c4f6bfeec1ab6a 100644 (file)
@@ -6,6 +6,7 @@
 #include <signal.h>
 #include <sys/signalfd.h>
 
+#include "src/dbus_service.h"
 // iotivity headers should be included before boost headers...
 // this makes compilation warning. (redefinition)
 #include "src/iotivity.h"
@@ -100,6 +101,8 @@ int main(int argc, char* argv[]) {
   if (source == 0)
     LOG(ERROR) << "Failed to register signal handler";
 
+  capmgr::DBusService ds;
+
   g_main_loop_run(mainloop);
   g_source_remove(source);
   g_main_loop_unref(mainloop);