Add the initial implementation of Tizen 4.0 context-service 38/113138/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 6 Feb 2017 07:55:19 +0000 (16:55 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 6 Feb 2017 07:55:19 +0000 (16:55 +0900)
Change-Id: I911a10d551f35f0c916e976a2ad5f63c36534500
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
12 files changed:
CMakeLists.txt [new file with mode: 0644]
packaging/context-service.manifest [new file with mode: 0644]
packaging/context-service.service [new file with mode: 0644]
packaging/context-service.spec [new file with mode: 0644]
packaging/org.tizen.context.conf [new file with mode: 0644]
src/AlarmInitializer.cpp [new file with mode: 0644]
src/AlarmInitializer.h [new file with mode: 0644]
src/DBusConnector.cpp [new file with mode: 0644]
src/DBusConnector.h [new file with mode: 0644]
src/Main.cpp [new file with mode: 0644]
src/ServiceLoader.cpp [new file with mode: 0644]
src/ServiceLoader.h [new file with mode: 0644]

diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644 (file)
index 0000000..74abc03
--- /dev/null
@@ -0,0 +1,30 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(context-service)
+INCLUDE(GNUInstallDirs)
+
+SET(target "contextd")
+
+SET(DEPS "glib-2.0 gio-2.0 dlog capi-base-common alarm-service")
+SET(DEPS "${DEPS} context-common-server")
+SET(DEPS "${DEPS} sensor-recorder-server")
+SET(DEPS "${DEPS} context-store-server")
+
+FILE(GLOB_RECURSE SRCS src/*.cpp)
+MESSAGE("Sources: ${SRCS}")
+
+ADD_DEFINITIONS(-O2 -Wall -fPIC -fPIE -fdata-sections -ffunction-sections -fvisibility=hidden)
+ADD_DEFINITIONS(-DLOG_TAG="CONTEXT")
+ADD_DEFINITIONS(-D_LIBDIR_="${LIBDIR}")
+SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIC -pie -Wl,--as-needed -Wl,--gc-section -Wl,--print-gc-section")
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(DEPS_PKG REQUIRED ${DEPS})
+
+FOREACH(flag ${DEPS_PKG_CFLAGS})
+       SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
+ENDFOREACH(flag)
+
+ADD_EXECUTABLE(${target} ${SRCS})
+TARGET_LINK_LIBRARIES(${target} ${DEPS_PKG_LDFLAGS})
+
+INSTALL(TARGETS ${target} DESTINATION ${CMAKE_INSTALL_BINDIR})
diff --git a/packaging/context-service.manifest b/packaging/context-service.manifest
new file mode 100644 (file)
index 0000000..97e8c31
--- /dev/null
@@ -0,0 +1,5 @@
+<manifest>
+       <request>
+               <domain name="_"/>
+       </request>
+</manifest>
diff --git a/packaging/context-service.service b/packaging/context-service.service
new file mode 100644 (file)
index 0000000..ba392a5
--- /dev/null
@@ -0,0 +1,12 @@
+[Unit]
+Description=Contextual Service Framework Daemon
+
+[Service]
+Type=dbus
+BusName=org.tizen.context
+ExecStart=/usr/bin/contextd
+Restart=always
+RestartSec=1
+
+[Install]
+WantedBy=default.target
diff --git a/packaging/context-service.spec b/packaging/context-service.spec
new file mode 100644 (file)
index 0000000..57494ee
--- /dev/null
@@ -0,0 +1,54 @@
+Name:       context-service
+Summary:    Tizen Contextual Service Framework
+Version:    1.0.0
+Release:    1
+Group:      Service/Context
+License:    Apache-2.0
+Source0:    %{name}-%{version}.tar.gz
+Source1:       context-service.service
+Source2:       org.tizen.context.conf
+
+BuildRequires: cmake
+BuildRequires: pkgconfig(glib-2.0)
+BuildRequires: pkgconfig(gio-2.0)
+BuildRequires: pkgconfig(dlog)
+BuildRequires: pkgconfig(capi-base-common)
+BuildRequires: pkgconfig(alarm-service)
+
+BuildRequires: pkgconfig(context-common-server)
+BuildRequires: pkgconfig(sensor-recorder-server)
+BuildRequires: pkgconfig(context-store-server)
+
+%description
+This is a systemd service that contains all features provided by the Tizen Contextual Service Framework.
+
+%prep
+%setup -q
+
+%build
+export CXXFLAGS+=" -Wextra -Wcast-align -Wshadow -Wwrite-strings -Wswitch-default -Wno-unused-parameter"
+export CXXFLAGS+=" -Wno-empty-body -fomit-frame-pointer -fno-optimize-sibling-calls"
+export CXXFLAGS+=" -fno-strict-aliasing -fno-unroll-loops -fsigned-char -fstrict-overflow"
+export CXXFLAGS+=" -Wnon-virtual-dtor -std=c++0x"
+
+cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} -DLIBDIR=%{_libdir}
+make %{?jobs:-j%jobs}
+
+%install
+rm -rf %{buildroot}
+%make_install
+
+mkdir -p %{buildroot}%{_unitdir}/default.target.wants
+install -m 0644 %{SOURCE1} %{buildroot}%{_unitdir}
+ln -s ../context-service.service %{buildroot}%{_unitdir}/default.target.wants/context-service.service
+
+mkdir -p %{buildroot}%{_sysconfdir}/dbus-1/system.d
+install -m 0644 %{SOURCE2} %{buildroot}%{_sysconfdir}/dbus-1/system.d/
+
+%files
+%manifest packaging/%{name}.manifest
+%config %{_sysconfdir}/dbus-1/system.d/*
+%{_bindir}/*
+%{_unitdir}/context-service.service
+%{_unitdir}/*/context-service.service
+%license LICENSE
diff --git a/packaging/org.tizen.context.conf b/packaging/org.tizen.context.conf
new file mode 100644 (file)
index 0000000..b43e39b
--- /dev/null
@@ -0,0 +1,15 @@
+<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
+"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
+<busconfig>
+       <policy smack="System">
+               <allow own="org.tizen.context"/>
+               <allow send_destination="org.tizen.context"/>
+       </policy>
+       <policy smack="User">
+               <deny own="org.tizen.context"/>
+               <allow send_destination="org.tizen.context"/>
+       </policy>
+       <policy context="default">
+               <allow send_destination="org.tizen.context"/>
+       </policy>
+</busconfig>
diff --git a/src/AlarmInitializer.cpp b/src/AlarmInitializer.cpp
new file mode 100644 (file)
index 0000000..04aa0c6
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <alarm.h>
+#include <ContextTypes.h>
+#include "AlarmInitializer.h"
+
+#define IDENTIFIER             "contextd"
+
+using namespace ctx;
+
+static bool __initialized = false;
+
+AlarmInitializer::AlarmInitializer()
+{
+       _I("Init Alarm");
+       int result = alarmmgr_init(IDENTIFIER);
+       if (result != ALARMMGR_RESULT_SUCCESS) {
+               _E("Alarm manager initialization failed");
+               return;
+       }
+       alarmmgr_remove_all();
+       __initialized = true;
+}
+
+AlarmInitializer::~AlarmInitializer()
+{
+       if (!__initialized)
+               return;
+
+       _I("Release Alarm");
+       alarmmgr_remove_all();
+       alarmmgr_fini();
+       __initialized = false;
+}
diff --git a/src/AlarmInitializer.h b/src/AlarmInitializer.h
new file mode 100644 (file)
index 0000000..40ae599
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __CONTEXT_ALARM_INITIALIZER_H__
+#define __CONTEXT_ALARM_INITIALIZER_H__
+
+namespace ctx {
+
+       class AlarmInitializer {
+       public:
+               AlarmInitializer();
+               ~AlarmInitializer();
+       };
+
+}
+
+#endif
diff --git a/src/DBusConnector.cpp b/src/DBusConnector.cpp
new file mode 100644 (file)
index 0000000..257311e
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "DBusConnector.h"
+
+using namespace ctx;
+
+DBusConnector::DBusConnector(bus_acquired_cb_t acqCb, bus_lost_cb_t lostCb) :
+       __owner(-1),
+       __connection(NULL),
+       __busAcquiredCb(acqCb),
+       __busLostCb(lostCb)
+{
+       __init();
+}
+
+DBusConnector::~DBusConnector()
+{
+       __release();
+}
+
+void DBusConnector::__onBusAcquired(GDBusConnection *conn, const gchar *name, gpointer userData)
+{
+       DBusConnector* connector = static_cast<DBusConnector*>(userData);
+       connector->__connection = conn;
+}
+
+void DBusConnector::__onNameAcquired(GDBusConnection *conn, const gchar *name, gpointer userData)
+{
+       _I(GREEN("Acquired"));
+       DBusConnector* connector = static_cast<DBusConnector*>(userData);
+       connector->__busAcquiredCb(conn);
+}
+
+void DBusConnector::__onNameLost(GDBusConnection *conn, const gchar *name, gpointer userData)
+{
+       _E(RED("Lost"));
+       DBusConnector* connector = static_cast<DBusConnector*>(userData);
+       connector->__busLostCb(conn);
+}
+
+void DBusConnector::__init()
+{
+       __owner = g_bus_own_name(G_BUS_TYPE_SYSTEM, CTX_DBUS_DEST, G_BUS_NAME_OWNER_FLAGS_NONE,
+                       __onBusAcquired, __onNameAcquired, __onNameLost, this, NULL);
+}
+
+void DBusConnector::__release()
+{
+       if (!__connection)
+               return;
+
+       g_dbus_connection_close_sync(__connection, NULL, NULL);
+       g_object_unref(__connection);
+}
diff --git a/src/DBusConnector.h b/src/DBusConnector.h
new file mode 100644 (file)
index 0000000..2311219
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __CONTEXT_DBUS_CONNECTOR_H__
+#define __CONTEXT_DBUS_CONNECTOR_H__
+
+#include <ContextTypes.h>
+
+namespace ctx {
+
+       typedef void (*bus_acquired_cb_t)(GDBusConnection *conn);
+       typedef void (*bus_lost_cb_t)(GDBusConnection *conn);
+
+       class DBusConnector {
+       public:
+               DBusConnector(bus_acquired_cb_t acqCb, bus_lost_cb_t lostCb);
+               ~DBusConnector();
+
+       private:
+               static void __onBusAcquired(GDBusConnection *conn, const gchar *name, gpointer userData);
+               static void __onNameAcquired(GDBusConnection *conn, const gchar *name, gpointer userData);
+               static void __onNameLost(GDBusConnection *conn, const gchar *name, gpointer userData);
+
+               void __init();
+               void __release();
+
+               guint __owner;
+               GDBusConnection *__connection;
+               bus_acquired_cb_t __busAcquiredCb;
+               bus_lost_cb_t __busLostCb;
+
+       };
+
+}
+
+#endif
diff --git a/src/Main.cpp b/src/Main.cpp
new file mode 100644 (file)
index 0000000..10b5451
--- /dev/null
@@ -0,0 +1,119 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <cstdlib>
+#include <cstring>
+#include <ContextTypes.h>
+#include <Timer.h>
+#include "DBusConnector.h"
+#include "ServiceLoader.h"
+#include "AlarmInitializer.h"
+
+using namespace ctx;
+
+namespace {
+       class Server {
+       public:
+               Server();
+               ~Server();
+               bool start();
+               void stop();
+       private:
+               GMainLoop* __mainloop;
+       };
+}
+
+::Server::Server() :
+       __mainloop(NULL)
+{
+       __mainloop = g_main_loop_new(NULL, FALSE);
+       IF_FAIL_VOID_TAG(__mainloop, _E, "Memory allocation failed");
+}
+
+::Server::~Server()
+{
+       if (__mainloop)
+               g_main_loop_unref(__mainloop);
+}
+
+bool ::Server::start()
+{
+       _I(CYAN("Starting..."));
+
+       IF_FAIL_RETURN(__mainloop, false);
+       g_main_loop_run(__mainloop);
+       return true;
+}
+
+void ::Server::stop()
+{
+       _I(PURPLE("Terminating..."));
+
+       if (g_main_loop_is_running(__mainloop))
+               g_main_loop_quit(__mainloop);
+}
+
+static ::Server __server;
+static ServiceLoader __loader;
+static AlarmInitializer __alarmInit;
+
+static void __signalHandler(int signum)
+{
+       _I(YELLOW("SIGNAL-%d: '%s'"), signum, strsignal(signum));
+       _I("Unloading services");
+       __loader.unload();
+       __server.stop();
+}
+
+static void __busAcquired(GDBusConnection* conn)
+{
+       Timer::setDBusConnection(conn);
+
+       _I("Loading services");
+       if (__loader.load(conn)) {
+               _I("Service loading successful");
+               return;
+       }
+
+       _E(RED("No service loaded."));
+       // FIXME: __server.stop();
+}
+
+static void __busLost(GDBusConnection* conn)
+{
+       _I("Unloading services");
+       __loader.unload();
+       __server.stop();
+}
+
+int main(int argc, char* argv[])
+{
+       static struct sigaction signalAction;
+       signalAction.sa_handler = __signalHandler;
+       sigemptyset(&signalAction.sa_mask);
+
+       sigaction(SIGINT, &signalAction, NULL);
+       sigaction(SIGHUP, &signalAction, NULL);
+       sigaction(SIGTERM, &signalAction, NULL);
+       sigaction(SIGQUIT, &signalAction, NULL);
+       sigaction(SIGABRT, &signalAction, NULL);
+
+       DBusConnector dbusConnector(__busAcquired, __busLost);
+
+       __server.start();
+
+       return EXIT_SUCCESS;
+}
diff --git a/src/ServiceLoader.cpp b/src/ServiceLoader.cpp
new file mode 100644 (file)
index 0000000..d461a5d
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//#include <AppHistoryService.h>
+#include <SensorRecorderService.h>
+#include <ContextStoreService.h>
+//#include <JobSchedulerService.h>
+
+#include "ServiceLoader.h"
+
+using namespace ctx;
+
+ServiceLoader::ServiceLoader()
+{
+}
+
+ServiceLoader::~ServiceLoader()
+{
+       unload();
+}
+
+bool ServiceLoader::load(GDBusConnection* conn)
+{
+       //__load<AppHistoryService>(conn);
+       __load<SensorRecorderService>(conn);
+       __load<ContextStoreService>(conn);
+       //__load<JobSchedulerService>(conn);
+
+       return !__services.empty();
+}
+
+void ServiceLoader::unload()
+{
+       for (auto& svc : __services) {
+               delete svc;
+       }
+       __services.clear();
+}
diff --git a/src/ServiceLoader.h b/src/ServiceLoader.h
new file mode 100644 (file)
index 0000000..4a8e59b
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __CONTEXT_SERVICE_LOADER_H__
+#define __CONTEXT_SERVICE_LOADER_H__
+
+#include <vector>
+#include <ContextTypes.h>
+#include <DBusService.h>
+
+namespace ctx {
+
+       class ServiceLoader {
+       public:
+               ServiceLoader();
+               ~ServiceLoader();
+
+               bool load(GDBusConnection* conn);
+               void unload();
+
+       private:
+               std::vector<DBusService*> __services;
+
+               template<typename ServiceType> void __load(GDBusConnection* conn)
+               {
+                       DBusService *svc = NULL;
+                       try {
+                               svc = new ServiceType(conn);
+                       } catch (std::exception& e) {
+                               _W("Service creation failed (%s)", e.what());
+                               return;
+                       }
+                       if (!svc->start()) {
+                               delete svc;
+                               return;
+                       }
+                       __services.push_back(svc);
+               }
+       };
+
+}
+
+#endif