--- /dev/null
+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})
--- /dev/null
+<manifest>
+ <request>
+ <domain name="_"/>
+ </request>
+</manifest>
--- /dev/null
+[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
--- /dev/null
+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
--- /dev/null
+<!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>
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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);
+}
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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();
+}
--- /dev/null
+/*
+ * 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