From accf24e2a7d0dcb104d9cb29636a85fba9392d3c Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Wed, 5 Apr 2017 16:32:38 +0900 Subject: [PATCH 01/16] Fix the build script typos Change-Id: I2007e1abec9fef0a0873edcdab4ababce5848dd8 Signed-off-by: Mu-Woong Lee --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ff2c8a0..dd4a656 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ PROJECT(context-service) INCLUDE(GNUInstallDirs) ADD_DEFINITIONS(-O2 -Wall -fPIC -fPIE -fdata-sections -ffunction-sections -fvisibility=hidden) -SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIC -pie -Wl,--as-needed -Wl,--gc-section -Wl,--print-gc-section") +SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIC -pie -Wl,--as-needed -Wl,--gc-sections -Wl,--print-gc-sections") ADD_SUBDIRECTORY(src/server) ADD_SUBDIRECTORY(src/agent) -- 2.7.4 From 5c3c145b2861dc536200c40a08d142abec7ab811 Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Thu, 30 Mar 2017 16:05:37 +0900 Subject: [PATCH 02/16] Integrate the legacy context-service into the agent service (2/2) This patch integrates the legacy contextd logic into contextd-agent. Change-Id: I5a8c8372dcab37b6ee1f96dbdf6b28e12436f8fb Signed-off-by: Mu-Woong Lee --- packaging/context-service.spec | 13 +++++++++++++ src/agent/AgentMain.cpp | 3 ++- src/agent/CMakeLists.txt | 18 +++++++++++++++++- src/agent/legacy/Server.cpp | 2 +- src/agent/legacy/Server.h | 2 ++ 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/packaging/context-service.spec b/packaging/context-service.spec index 14c6608..42a8c4f 100644 --- a/packaging/context-service.spec +++ b/packaging/context-service.spec @@ -25,6 +25,19 @@ BuildRequires: pkgconfig(context-sensor-recorder-server) BuildRequires: pkgconfig(context-store-server) BuildRequires: pkgconfig(context-job-scheduler-server) +# Legacy support. These dependencies will be removed. +BuildRequires: pkgconfig(gmodule-2.0) +BuildRequires: pkgconfig(vconf) +BuildRequires: pkgconfig(capi-system-device) +BuildRequires: pkgconfig(capi-appfw-app-manager) +BuildRequires: pkgconfig(capi-appfw-package-manager) +BuildRequires: pkgconfig(notification) +BuildRequires: pkgconfig(cynara-creds-gdbus) +BuildRequires: pkgconfig(cynara-client) +BuildRequires: pkgconfig(cynara-session) +BuildRequires: pkgconfig(context-common-legacy) +# --- + %description This is a systemd service that contains all features provided by the Tizen Contextual Service Framework. diff --git a/src/agent/AgentMain.cpp b/src/agent/AgentMain.cpp index fc1dc8d..b1bfe89 100644 --- a/src/agent/AgentMain.cpp +++ b/src/agent/AgentMain.cpp @@ -15,8 +15,9 @@ */ #include +#include "legacy/Server.h" int main(int argc, char* argv[]) { - return EXIT_SUCCESS; + return mainLegacy(); } diff --git a/src/agent/CMakeLists.txt b/src/agent/CMakeLists.txt index a97f2b1..c476073 100644 --- a/src/agent/CMakeLists.txt +++ b/src/agent/CMakeLists.txt @@ -4,7 +4,22 @@ SET(target "contextd-agent") SET(DEPS glib-2.0 gio-2.0 dlog capi-base-common) -FILE(GLOB SRCS *.cpp) +# Legacy support +SET(DEPS ${DEPS} + gmodule-2.0 + vconf + cynara-creds-gdbus + cynara-client + cynara-session + capi-system-device + capi-appfw-app-manager + capi-appfw-package-manager + notification + context-common-legacy +) +# --- + +FILE(GLOB_RECURSE SRCS *.cpp) MESSAGE("Sources: ${SRCS}") INCLUDE(FindPkgConfig) @@ -14,6 +29,7 @@ FOREACH(flag ${DEPS_AGENT_PKG_CFLAGS}) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}") ENDFOREACH(flag) +ADD_DEFINITIONS(-D_LIBDIR_="${CMAKE_INSTALL_LIBDIR}") ADD_EXECUTABLE(${target} ${SRCS}) SET_TARGET_PROPERTIES(${target} PROPERTIES COMPILE_DEFINITIONS "LOG_TAG=\"CONTEXT-AGENT\"") TARGET_LINK_LIBRARIES(${target} ${DEPS_AGENT_PKG_LDFLAGS}) diff --git a/src/agent/legacy/Server.cpp b/src/agent/legacy/Server.cpp index 197b0d6..b6e1c50 100644 --- a/src/agent/legacy/Server.cpp +++ b/src/agent/legacy/Server.cpp @@ -142,7 +142,7 @@ static void __signalHandler(int signo) QUIT(mainloop); } -int main(int argc, char* argv[]) +int mainLegacy() { static struct sigaction signalAction; signalAction.sa_handler = __signalHandler; diff --git a/src/agent/legacy/Server.h b/src/agent/legacy/Server.h index cd93bbf..621a6ea 100644 --- a/src/agent/legacy/Server.h +++ b/src/agent/legacy/Server.h @@ -17,6 +17,8 @@ #ifndef _CONTEXT_SERVER_H_ #define _CONTEXT_SERVER_H_ +int mainLegacy(); + namespace ctx { class RequestInfo; -- 2.7.4 From 52144b9c7c5baad56f3ee29f141862bab0ab85ca Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Tue, 11 Apr 2017 11:01:15 +0900 Subject: [PATCH 03/16] Fix warnings detected from static analysis Change-Id: Icf15e17a3fc8f08d55734ce6cfb51200f3ada511 Signed-off-by: Mu-Woong Lee --- src/agent/legacy/ProviderHandler.cpp | 1 + src/agent/legacy/access_control/Privilege.cpp | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/agent/legacy/ProviderHandler.cpp b/src/agent/legacy/ProviderHandler.cpp index 2ce7f69..62e1cf1 100644 --- a/src/agent/legacy/ProviderHandler.cpp +++ b/src/agent/legacy/ProviderHandler.cpp @@ -29,6 +29,7 @@ std::map ProviderHandler::__instanceMap; ProviderHandler::ProviderHandler(const std::string &subject) : __subject(subject), + __provider(NULL), __deleteScheduled(false) { _D("Subject: %s", __subject.c_str()); diff --git a/src/agent/legacy/access_control/Privilege.cpp b/src/agent/legacy/access_control/Privilege.cpp index da2bdae..e10aa24 100644 --- a/src/agent/legacy/access_control/Privilege.cpp +++ b/src/agent/legacy/access_control/Privilege.cpp @@ -27,7 +27,8 @@ class PermissionChecker { private: cynara *__cynara; - PermissionChecker() + PermissionChecker() : + __cynara(NULL) { cynara_configuration *conf; -- 2.7.4 From 7054a84da686b2abcd3b15667ee8fa37044c2a55 Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Wed, 12 Apr 2017 10:56:33 +0900 Subject: [PATCH 04/16] Disable data monitoring/logging features in the legacy context-service Change-Id: Ib6f577c83e574f370cca7b8c3a401f29833485e4 Signed-off-by: Mu-Woong Lee --- src/agent/legacy/policy/PolicyManager.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/agent/legacy/policy/PolicyManager.cpp b/src/agent/legacy/policy/PolicyManager.cpp index 3f97344..782c81c 100644 --- a/src/agent/legacy/policy/PolicyManager.cpp +++ b/src/agent/legacy/policy/PolicyManager.cpp @@ -29,16 +29,16 @@ PolicyManager::PolicyManager(ContextManager *contextMgr) : { __subscribe(SUBJ_CUSTOM); - __subscribe(SUBJ_APP_LOGGER); - __subscribe(SUBJ_BATTERY_LOGGER); - __subscribe(SUBJ_MEDIA_LOGGER); - __subscribe(SUBJ_PLACE_DETECTION); - __subscribe(SUBJ_STATE_WIFI); +// __subscribe(SUBJ_APP_LOGGER); +// __subscribe(SUBJ_BATTERY_LOGGER); +// __subscribe(SUBJ_MEDIA_LOGGER); +// __subscribe(SUBJ_PLACE_DETECTION); +// __subscribe(SUBJ_STATE_WIFI); - __subscribe(SUBJ_SENSOR_PEDOMETER); - __subscribe(SUBJ_SENSOR_PRESSURE); - __subscribe(SUBJ_SENSOR_SLEEP_MONITOR); - __subscribe(SUBJ_SENSOR_HEART_RATE); +// __subscribe(SUBJ_SENSOR_PEDOMETER); +// __subscribe(SUBJ_SENSOR_PRESSURE); +// __subscribe(SUBJ_SENSOR_SLEEP_MONITOR); +// __subscribe(SUBJ_SENSOR_HEART_RATE); } PolicyManager::~PolicyManager() -- 2.7.4 From 4d9cf579b629f37b50e371981a0ab2ba4a19656d Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Fri, 14 Apr 2017 18:18:35 +0900 Subject: [PATCH 05/16] Add compile options to suppress verbose messages and enable colored diagnostics Change-Id: Icd64cc069336202e1b910c687b14ecd0b792076c Signed-off-by: Mu-Woong Lee --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index dd4a656..c9d1849 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,9 @@ PROJECT(context-service) INCLUDE(GNUInstallDirs) ADD_DEFINITIONS(-O2 -Wall -fPIC -fPIE -fdata-sections -ffunction-sections -fvisibility=hidden) +ADD_DEFINITIONS(-fdiagnostics-color) SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIC -pie -Wl,--as-needed -Wl,--gc-sections -Wl,--print-gc-sections") +SET(CMAKE_VERBOSE_MAKEFILE OFF) ADD_SUBDIRECTORY(src/server) ADD_SUBDIRECTORY(src/agent) -- 2.7.4 From c256f494fbdc9fc8fff16ac883c4f3a8272bf7ae Mon Sep 17 00:00:00 2001 From: Somin Kim Date: Thu, 20 Apr 2017 15:42:48 +0900 Subject: [PATCH 06/16] Fix svace issue Change-Id: Icda8de107e6c5f80fa266930b4966895fc6b0694 Signed-off-by: Somin Kim --- src/agent/legacy/access_control/Privilege.cpp | 2 +- src/server/ServerMain.cpp | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/agent/legacy/access_control/Privilege.cpp b/src/agent/legacy/access_control/Privilege.cpp index e10aa24..b9b6c37 100644 --- a/src/agent/legacy/access_control/Privilege.cpp +++ b/src/agent/legacy/access_control/Privilege.cpp @@ -30,7 +30,7 @@ private: PermissionChecker() : __cynara(NULL) { - cynara_configuration *conf; + cynara_configuration *conf = NULL; int err = cynara_configuration_create(&conf); IF_FAIL_VOID_TAG(err == CYNARA_API_SUCCESS, _E, "Cynara configuration creation failed"); diff --git a/src/server/ServerMain.cpp b/src/server/ServerMain.cpp index 25134e9..a1d002e 100644 --- a/src/server/ServerMain.cpp +++ b/src/server/ServerMain.cpp @@ -17,7 +17,6 @@ #include #include #include -#include #include #include #include @@ -145,7 +144,6 @@ static void __onNewFailed() failCount += 1; if (failCount >= NEW_FAIL_LIMIT) throw std::bad_alloc(); - usleep(100000); } int main(int argc, char* argv[]) -- 2.7.4 From c02bbf63169a98592f80fc518f0ad3e15dd21c61 Mon Sep 17 00:00:00 2001 From: Somin Kim Date: Thu, 20 Apr 2017 15:48:25 +0900 Subject: [PATCH 07/16] Fix typo in agent conf file Change-Id: I65c8a6d60201fd62956c69490f52fe62844eed51 Signed-off-by: Somin Kim --- packaging/org.tizen.contextagent.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/org.tizen.contextagent.conf b/packaging/org.tizen.contextagent.conf index f5f075e..9dfee23 100644 --- a/packaging/org.tizen.contextagent.conf +++ b/packaging/org.tizen.contextagent.conf @@ -10,6 +10,6 @@ -- 2.7.4 From c8468b4d3336a1c0d675bd067fe00e8879227f4a Mon Sep 17 00:00:00 2001 From: Somin Kim Date: Thu, 20 Apr 2017 18:13:11 +0900 Subject: [PATCH 08/16] Version 1.0.1 Change-Id: Iaf2a5d1769ba2fd096b418632aec68cd199055fd Signed-off-by: Somin Kim --- packaging/context-service.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/context-service.spec b/packaging/context-service.spec index 42a8c4f..e53cb3b 100644 --- a/packaging/context-service.spec +++ b/packaging/context-service.spec @@ -1,6 +1,6 @@ Name: context-service Summary: Tizen Contextual Service Framework -Version: 1.0.0 +Version: 1.0.1 Release: 1 Group: Service/Context License: Apache-2.0 -- 2.7.4 From 70f414cd05e8e140958e8bc89a400f96cd815d16 Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Tue, 25 Apr 2017 19:38:45 +0900 Subject: [PATCH 09/16] Change the creation order of the services Change-Id: I856a3667f6f9976e82b9b38500cff9d3c5e58fbd Signed-off-by: Mu-Woong Lee --- src/server/ServiceLoader.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/server/ServiceLoader.cpp b/src/server/ServiceLoader.cpp index 03acfbc..19ca7a2 100644 --- a/src/server/ServiceLoader.cpp +++ b/src/server/ServiceLoader.cpp @@ -42,10 +42,10 @@ ServiceLoader::~ServiceLoader() bool ServiceLoader::load(GDBusConnection* conn) { - __create(conn); - __create(conn); __create(conn); __create(conn); + __create(conn); + __create(conn); return (!__systemServices.empty() || !__userServices.empty()); } -- 2.7.4 From 2a02f721ea2e7ebb5e0443ec4f380ebc5a33e905 Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Tue, 2 May 2017 11:26:58 +0900 Subject: [PATCH 10/16] Add missing dbus privilege checking rules Change-Id: I8a01a928bf0d2db1ce3242e2a59b174855a5eae3 Signed-off-by: Mu-Woong Lee --- packaging/org.tizen.contextagent.conf | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packaging/org.tizen.contextagent.conf b/packaging/org.tizen.contextagent.conf index 9dfee23..f6f0870 100644 --- a/packaging/org.tizen.contextagent.conf +++ b/packaging/org.tizen.contextagent.conf @@ -11,5 +11,17 @@ + + + -- 2.7.4 From 0dab404224a893459a379aa41cfacd28d87519dc Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Fri, 2 Jun 2017 08:37:38 +0900 Subject: [PATCH 11/16] Remove deprecated D-bus smack policies Change-Id: Id6eb2ff037f8abe737ba0af7a42d4abb22550b31 Signed-off-by: Hyotaek Shim --- packaging/org.tizen.contextagent.conf | 3 --- 1 file changed, 3 deletions(-) diff --git a/packaging/org.tizen.contextagent.conf b/packaging/org.tizen.contextagent.conf index f6f0870..8ea6b67 100644 --- a/packaging/org.tizen.contextagent.conf +++ b/packaging/org.tizen.contextagent.conf @@ -4,9 +4,6 @@ - - - Date: Fri, 2 Jun 2017 13:25:21 +0900 Subject: [PATCH 12/16] Remove a misused 'deny own' rule from contextd-agent dbus conf Change-Id: Ia3f9178113fc1fb455eec936069a42fe8124d96f Signed-off-by: Mu-Woong Lee --- packaging/org.tizen.contextagent.conf | 1 - 1 file changed, 1 deletion(-) diff --git a/packaging/org.tizen.contextagent.conf b/packaging/org.tizen.contextagent.conf index 8ea6b67..dfb4f9f 100644 --- a/packaging/org.tizen.contextagent.conf +++ b/packaging/org.tizen.contextagent.conf @@ -5,7 +5,6 @@ - Date: Thu, 8 Jun 2017 21:28:32 +0900 Subject: [PATCH 13/16] Rename the class Json to avoid symbol conflicts with Jsoncpp Change-Id: I7826f4420ee0fa4879ce2d9e25d1a61c5279bada Signed-off-by: Mu-Woong Lee --- src/agent/legacy/ClientRequest.cpp | 6 +- src/agent/legacy/ClientRequest.h | 6 +- src/agent/legacy/ContextManager.cpp | 20 ++--- src/agent/legacy/ContextManager.h | 12 +-- src/agent/legacy/ProviderHandler.cpp | 16 ++-- src/agent/legacy/ProviderHandler.h | 8 +- src/agent/legacy/ProviderLoader.cpp | 2 +- src/agent/legacy/ProviderLoader.h | 2 +- src/agent/legacy/Request.cpp | 2 +- src/agent/legacy/Request.h | 12 +-- src/agent/legacy/policy/PolicyRequest.cpp | 6 +- src/agent/legacy/policy/PolicyRequest.h | 6 +- src/agent/legacy/trigger/ActionManager.cpp | 16 ++-- src/agent/legacy/trigger/ActionManager.h | 4 +- src/agent/legacy/trigger/ContextMonitor.cpp | 26 +++--- src/agent/legacy/trigger/ContextMonitor.h | 28 +++---- src/agent/legacy/trigger/FactRequest.cpp | 6 +- src/agent/legacy/trigger/FactRequest.h | 6 +- src/agent/legacy/trigger/IContextListener.h | 6 +- src/agent/legacy/trigger/Rule.cpp | 20 ++--- src/agent/legacy/trigger/Rule.h | 24 +++--- src/agent/legacy/trigger/RuleEvaluator.cpp | 60 +++++++------- src/agent/legacy/trigger/RuleEvaluator.h | 30 +++---- src/agent/legacy/trigger/RuleManager.cpp | 116 +++++++++++++-------------- src/agent/legacy/trigger/RuleManager.h | 14 ++-- src/agent/legacy/trigger/TemplateManager.cpp | 22 ++--- src/agent/legacy/trigger/TemplateManager.h | 8 +- src/agent/legacy/trigger/Trigger.cpp | 24 +++--- 28 files changed, 254 insertions(+), 254 deletions(-) diff --git a/src/agent/legacy/ClientRequest.cpp b/src/agent/legacy/ClientRequest.cpp index e625bf0..0ae8573 100644 --- a/src/agent/legacy/ClientRequest.cpp +++ b/src/agent/legacy/ClientRequest.cpp @@ -72,7 +72,7 @@ bool ctx::ClientRequest::reply(int error) return true; } -bool ctx::ClientRequest::reply(int error, ctx::Json& requestResult) +bool ctx::ClientRequest::reply(int error, ctx::CtxJson1& requestResult) { IF_FAIL_RETURN(__invocation, true); IF_FAIL_RETURN(error != ERR_NONE || __type != REQ_READ_SYNC, true); @@ -89,7 +89,7 @@ bool ctx::ClientRequest::reply(int error, ctx::Json& requestResult) return true; } -bool ctx::ClientRequest::reply(int error, ctx::Json& requestResult, ctx::Json& dataRead) +bool ctx::ClientRequest::reply(int error, ctx::CtxJson1& requestResult, ctx::CtxJson1& dataRead) { if (__invocation == NULL) { return publish(error, dataRead); @@ -109,7 +109,7 @@ bool ctx::ClientRequest::reply(int error, ctx::Json& requestResult, ctx::Json& d return true; } -bool ctx::ClientRequest::publish(int error, ctx::Json& data) +bool ctx::ClientRequest::publish(int error, ctx::CtxJson1& data) { DBusServer::publish(__dbusSender, __reqId, __subject, error, data.str()); return true; diff --git a/src/agent/legacy/ClientRequest.h b/src/agent/legacy/ClientRequest.h index 69d18e1..e6e653b 100644 --- a/src/agent/legacy/ClientRequest.h +++ b/src/agent/legacy/ClientRequest.h @@ -32,9 +32,9 @@ namespace ctx { const char* getPackageId(); const char* getClient(); bool reply(int error); - bool reply(int error, ctx::Json &requestResult); - bool reply(int error, ctx::Json &requestResult, ctx::Json &dataRead); - bool publish(int error, ctx::Json &data); + bool reply(int error, ctx::CtxJson1 &requestResult); + bool reply(int error, ctx::CtxJson1 &requestResult, ctx::CtxJson1 &dataRead); + bool publish(int error, ctx::CtxJson1 &data); private: Credentials *__credentials; diff --git a/src/agent/legacy/ContextManager.cpp b/src/agent/legacy/ContextManager.cpp index 7d07966..9f52415 100644 --- a/src/agent/legacy/ContextManager.cpp +++ b/src/agent/legacy/ContextManager.cpp @@ -19,7 +19,7 @@ #include #include -#include +#include #include #include @@ -126,7 +126,7 @@ bool ContextManager::isAllowed(const Credentials *creds, const char *subject) return handle->isAllowed(creds); } -void ContextManager::__publish(const char* subject, Json &option, int error, Json &dataUpdated) +void ContextManager::__publish(const char* subject, CtxJson1 &option, int error, CtxJson1 &dataUpdated) { _I("Publishing '%s'", subject); _J("Option", option); @@ -137,7 +137,7 @@ void ContextManager::__publish(const char* subject, Json &option, int error, Jso handle->publish(option, error, dataUpdated); } -void ContextManager::__replyToRead(const char* subject, Json &option, int error, Json &dataRead) +void ContextManager::__replyToRead(const char* subject, CtxJson1 &option, int error, CtxJson1 &dataRead) { _I("Sending data of '%s'", subject); _J("Option", option); @@ -154,9 +154,9 @@ struct PublishedData { ContextManager *mgr; std::string subject; int error; - Json option; - Json data; - PublishedData(int t, ContextManager *m, const char* s, Json& o, int e, Json& d) + CtxJson1 option; + CtxJson1 data; + PublishedData(int t, ContextManager *m, const char* s, CtxJson1& o, int e, CtxJson1& d) : type(t), mgr(m), subject(s), error(e) { option = o.str(); @@ -183,7 +183,7 @@ gboolean ContextManager::__threadSwitcher(gpointer data) return FALSE; } -bool ContextManager::publish(const char* subject, Json& option, int error, Json& dataUpdated) +bool ContextManager::publish(const char* subject, CtxJson1& option, int error, CtxJson1& dataUpdated) { IF_FAIL_RETURN_TAG(subject, false, _E, "Invalid parameter"); @@ -195,7 +195,7 @@ bool ContextManager::publish(const char* subject, Json& option, int error, Json& return true; } -bool ContextManager::replyToRead(const char* subject, Json& option, int error, Json& dataRead) +bool ContextManager::replyToRead(const char* subject, CtxJson1& option, int error, CtxJson1& dataRead) { IF_FAIL_RETURN_TAG(subject, false, _E, "Invalid parameter"); @@ -207,13 +207,13 @@ bool ContextManager::replyToRead(const char* subject, Json& option, int error, J return true; } -bool ContextManager::popTriggerTemplate(std::string &subject, int &operation, Json &attribute, Json &option) +bool ContextManager::popTriggerTemplate(std::string &subject, int &operation, CtxJson1 &attribute, CtxJson1 &option) { return ProviderLoader::popTriggerTemplate(subject, operation, attribute, option); } /* Only for explicit request of custom provider */ -bool ContextManager::registerCustomProvider(const char* subject, int operation, ctx::Json &attribute, ctx::Json &option, const char* owner) +bool ContextManager::registerCustomProvider(const char* subject, int operation, ctx::CtxJson1 &attribute, ctx::CtxJson1 &option, const char* owner) { IF_FAIL_RETURN_TAG(ProviderHandler::getInstance(subject, true), false, _E, "Register provider failed"); diff --git a/src/agent/legacy/ContextManager.h b/src/agent/legacy/ContextManager.h index 9be24c1..ab98910 100644 --- a/src/agent/legacy/ContextManager.h +++ b/src/agent/legacy/ContextManager.h @@ -39,20 +39,20 @@ namespace ctx { bool isAllowed(const Credentials *creds, const char *subject); /* From the interface class */ - bool publish(const char *subject, ctx::Json &option, int error, ctx::Json &dataUpdated); - bool replyToRead(const char *subject, ctx::Json &option, int error, ctx::Json &dataRead); - bool registerCustomProvider(const char* subject, int operation, ctx::Json &attribute, ctx::Json &option, const char* owner); + bool publish(const char *subject, ctx::CtxJson1 &option, int error, ctx::CtxJson1 &dataUpdated); + bool replyToRead(const char *subject, ctx::CtxJson1 &option, int error, ctx::CtxJson1 &dataRead); + bool registerCustomProvider(const char* subject, int operation, ctx::CtxJson1 &attribute, ctx::CtxJson1 &option, const char* owner); bool unregisterCustomProvider(const char* subject); - bool popTriggerTemplate(std::string &subject, int &operation, Json &attribute, Json &option); + bool popTriggerTemplate(std::string &subject, int &operation, CtxJson1 &attribute, CtxJson1 &option); private: ContextManager(); static gboolean __threadSwitcher(gpointer data); - void __publish(const char *subject, ctx::Json &option, int error, ctx::Json &dataUpdated); - void __replyToRead(const char *subject, ctx::Json &option, int error, ctx::Json &dataRead); + void __publish(const char *subject, ctx::CtxJson1 &option, int error, ctx::CtxJson1 &dataUpdated); + void __replyToRead(const char *subject, ctx::CtxJson1 &option, int error, ctx::CtxJson1 &dataRead); friend class Server; diff --git a/src/agent/legacy/ProviderHandler.cpp b/src/agent/legacy/ProviderHandler.cpp index 62e1cf1..d4bbc4f 100644 --- a/src/agent/legacy/ProviderHandler.cpp +++ b/src/agent/legacy/ProviderHandler.cpp @@ -127,7 +127,7 @@ void ProviderHandler::subscribe(RequestInfo *request) { _I(CYAN("'%s' subscribes '%s' (RID-%d)"), request->getClient(), __subject.c_str(), request->getId()); - Json requestResult; + CtxJson1 requestResult; int error = __provider->subscribe(request->getDescription().str(), &requestResult); if (!request->reply(error, requestResult) || error != ERR_NONE) { @@ -182,7 +182,7 @@ void ProviderHandler::read(RequestInfo *request) { _I(CYAN("'%s' reads '%s' (RID-%d)"), request->getClient(), __subject.c_str(), request->getId()); - Json requestResult; + CtxJson1 requestResult; int error = __provider->read(request->getDescription().str(), &requestResult); if (!request->reply(error, requestResult) || error != ERR_NONE) { @@ -199,7 +199,7 @@ void ProviderHandler::write(RequestInfo *request) { _I(CYAN("'%s' writes '%s' (RID-%d)"), request->getClient(), __subject.c_str(), request->getId()); - Json requestResult; + CtxJson1 requestResult; request->getDescription().set(NULL, KEY_CLIENT_PKG_ID, request->getPackageId()? request->getPackageId() : "SYSTEM"); int error = __provider->write(request->getDescription(), &requestResult); @@ -210,7 +210,7 @@ void ProviderHandler::write(RequestInfo *request) __scheduleToDelete(); } -bool ProviderHandler::publish(Json &option, int error, Json &dataUpdated) +bool ProviderHandler::publish(CtxJson1 &option, int error, CtxJson1 &dataUpdated) { auto end = __subscribeRequests.end(); auto target = __findRequest(__subscribeRequests.begin(), end, option); @@ -225,13 +225,13 @@ bool ProviderHandler::publish(Json &option, int error, Json &dataUpdated) return true; } -bool ProviderHandler::replyToRead(Json &option, int error, Json &dataRead) +bool ProviderHandler::replyToRead(CtxJson1 &option, int error, CtxJson1 &dataRead) { auto end = __readRequests.end(); auto target = __findRequest(__readRequests.begin(), end, option); auto prev = target; - Json dummy; + CtxJson1 dummy; while (target != end) { (*target)->reply(error, dummy, dataRead); @@ -283,7 +283,7 @@ gboolean ProviderHandler::__deletor(gpointer data) } ProviderHandler::RequestList::iterator -ProviderHandler::__findRequest(RequestList &reqList, Json &option) +ProviderHandler::__findRequest(RequestList &reqList, CtxJson1 &option) { return __findRequest(reqList.begin(), reqList.end(), option); } @@ -300,7 +300,7 @@ ProviderHandler::__findRequest(RequestList &reqList, std::string client, int req } ProviderHandler::RequestList::iterator -ProviderHandler::__findRequest(RequestList::iterator begin, RequestList::iterator end, Json &option) +ProviderHandler::__findRequest(RequestList::iterator begin, RequestList::iterator end, CtxJson1 &option) { for (auto it = begin; it != end; ++it) { if (option == (*it)->getDescription()) { diff --git a/src/agent/legacy/ProviderHandler.h b/src/agent/legacy/ProviderHandler.h index 58225f7..ae2228e 100644 --- a/src/agent/legacy/ProviderHandler.h +++ b/src/agent/legacy/ProviderHandler.h @@ -41,8 +41,8 @@ namespace ctx { void read(RequestInfo *request); void write(RequestInfo *request); - bool publish(ctx::Json &option, int error, ctx::Json &dataUpdated); - bool replyToRead(ctx::Json &option, int error, ctx::Json &dataRead); + bool publish(ctx::CtxJson1 &option, int error, ctx::CtxJson1 &dataUpdated); + bool replyToRead(ctx::CtxJson1 &option, int error, ctx::CtxJson1 &dataRead); static ProviderHandler* getInstance(std::string subject, bool force); static void purge(); @@ -65,9 +65,9 @@ namespace ctx { bool __idle(); void __scheduleToDelete(); - RequestList::iterator __findRequest(RequestList &reqList, Json &option); + RequestList::iterator __findRequest(RequestList &reqList, CtxJson1 &option); RequestList::iterator __findRequest(RequestList &reqList, std::string client, int reqId); - RequestList::iterator __findRequest(RequestList::iterator begin, RequestList::iterator end, Json &option); + RequestList::iterator __findRequest(RequestList::iterator begin, RequestList::iterator end, CtxJson1 &option); static gboolean __deletor(gpointer data); diff --git a/src/agent/legacy/ProviderLoader.cpp b/src/agent/legacy/ProviderLoader.cpp index 8a7d6a6..7ef4627 100644 --- a/src/agent/legacy/ProviderLoader.cpp +++ b/src/agent/legacy/ProviderLoader.cpp @@ -102,7 +102,7 @@ bool ProviderLoader::init() return true; } -bool ProviderLoader::popTriggerTemplate(std::string &subject, int &operation, Json &attribute, Json &option) +bool ProviderLoader::popTriggerTemplate(std::string &subject, int &operation, CtxJson1 &attribute, CtxJson1 &option) { static int i = 0; static int size = ARRAY_SIZE(triggerTemplateList); diff --git a/src/agent/legacy/ProviderLoader.h b/src/agent/legacy/ProviderLoader.h index ab5a427..aae6c5d 100644 --- a/src/agent/legacy/ProviderLoader.h +++ b/src/agent/legacy/ProviderLoader.h @@ -48,7 +48,7 @@ namespace ctx { ContextProvider* load(const char *subject); static bool init(); - static bool popTriggerTemplate(std::string &subject, int &operation, Json &attribute, Json &option); + static bool popTriggerTemplate(std::string &subject, int &operation, CtxJson1 &attribute, CtxJson1 &option); private: ContextProvider* __load(const char *soPath, const char *subject); diff --git a/src/agent/legacy/Request.cpp b/src/agent/legacy/Request.cpp index d900fcc..efaa13f 100644 --- a/src/agent/legacy/Request.cpp +++ b/src/agent/legacy/Request.cpp @@ -60,7 +60,7 @@ const char* ctx::RequestInfo::getSubject() return __subject.c_str(); } -ctx::Json& ctx::RequestInfo::getDescription() +ctx::CtxJson1& ctx::RequestInfo::getDescription() { return __description; } diff --git a/src/agent/legacy/Request.h b/src/agent/legacy/Request.h index 9750b2f..b3425a1 100644 --- a/src/agent/legacy/Request.h +++ b/src/agent/legacy/Request.h @@ -18,7 +18,7 @@ #define _CONTEXT_REQUEST_INFO_H_ #include -#include +#include namespace ctx { @@ -33,22 +33,22 @@ namespace ctx { int getType(); int getId(); const char* getSubject(); - ctx::Json& getDescription(); + ctx::CtxJson1& getDescription(); virtual const Credentials* getCredentials(); virtual const char* getPackageId(); /* TODO: remove this getClient() */ virtual const char* getClient(); virtual bool reply(int error) = 0; - virtual bool reply(int error, ctx::Json &requestResult) = 0; - virtual bool reply(int error, ctx::Json &requestResult, ctx::Json &dataRead) = 0; - virtual bool publish(int error, ctx::Json &data) = 0; + virtual bool reply(int error, ctx::CtxJson1 &requestResult) = 0; + virtual bool reply(int error, ctx::CtxJson1 &requestResult, ctx::CtxJson1 &dataRead) = 0; + virtual bool publish(int error, ctx::CtxJson1 &data) = 0; protected: int __type; int __reqId; std::string __subject; - ctx::Json __description; + ctx::CtxJson1 __description; }; } /* namespace ctx */ diff --git a/src/agent/legacy/policy/PolicyRequest.cpp b/src/agent/legacy/policy/PolicyRequest.cpp index 63cf8ae..cd69534 100644 --- a/src/agent/legacy/policy/PolicyRequest.cpp +++ b/src/agent/legacy/policy/PolicyRequest.cpp @@ -37,17 +37,17 @@ bool PolicyRequest::reply(int error) return true; } -bool PolicyRequest::reply(int error, ctx::Json &requestResult) +bool PolicyRequest::reply(int error, ctx::CtxJson1 &requestResult) { return true; } -bool PolicyRequest::reply(int error, ctx::Json &requestResult, ctx::Json &dataRead) +bool PolicyRequest::reply(int error, ctx::CtxJson1 &requestResult, ctx::CtxJson1 &dataRead) { return true; } -bool PolicyRequest::publish(int error, ctx::Json &data) +bool PolicyRequest::publish(int error, ctx::CtxJson1 &data) { return true; } diff --git a/src/agent/legacy/policy/PolicyRequest.h b/src/agent/legacy/policy/PolicyRequest.h index 2ed28c7..ce68fb8 100644 --- a/src/agent/legacy/policy/PolicyRequest.h +++ b/src/agent/legacy/policy/PolicyRequest.h @@ -29,9 +29,9 @@ namespace ctx { const char* getClient(); bool reply(int error); - bool reply(int error, ctx::Json &requestResult); - bool reply(int error, ctx::Json &requestResult, ctx::Json &dataRead); - bool publish(int error, ctx::Json &data); + bool reply(int error, ctx::CtxJson1 &requestResult); + bool reply(int error, ctx::CtxJson1 &requestResult, ctx::CtxJson1 &dataRead); + bool publish(int error, ctx::CtxJson1 &data); }; } /* namespace ctx */ diff --git a/src/agent/legacy/trigger/ActionManager.cpp b/src/agent/legacy/trigger/ActionManager.cpp index b45671f..332c87e 100644 --- a/src/agent/legacy/trigger/ActionManager.cpp +++ b/src/agent/legacy/trigger/ActionManager.cpp @@ -15,7 +15,7 @@ */ #include -#include +#include #include #include #include @@ -29,11 +29,11 @@ using namespace ctx; -static void __triggerActionAppControl(Json& action); -static void __triggerActionNotification(Json& action, std::string pkgId); -static void __triggerActionDbusCall(Json& action); +static void __triggerActionAppControl(CtxJson1& action); +static void __triggerActionNotification(CtxJson1& action, std::string pkgId); +static void __triggerActionDbusCall(CtxJson1& action); -void trigger::action_manager::triggerAction(Json& action, std::string pkgId) +void trigger::action_manager::triggerAction(CtxJson1& action, std::string pkgId) { std::list types; action.getKeys(&types); @@ -50,7 +50,7 @@ void trigger::action_manager::triggerAction(Json& action, std::string pkgId) } } -void __triggerActionAppControl(Json& action) +void __triggerActionAppControl(CtxJson1& action) { int error; std::string appctlStr; @@ -85,7 +85,7 @@ void __triggerActionAppControl(Json& action) } } -void __triggerActionNotification(Json& action, std::string pkgId) +void __triggerActionNotification(CtxJson1& action, std::string pkgId) { int error; notification_h notification = notification_create(NOTIFICATION_TYPE_NOTI); @@ -187,7 +187,7 @@ void __triggerActionNotification(Json& action, std::string pkgId) } } -void __triggerActionDbusCall(Json& action) +void __triggerActionDbusCall(CtxJson1& action) { std::string busName, object, iface, method; GVariant *param = NULL; diff --git a/src/agent/legacy/trigger/ActionManager.h b/src/agent/legacy/trigger/ActionManager.h index 273f450..5d8c026 100644 --- a/src/agent/legacy/trigger/ActionManager.h +++ b/src/agent/legacy/trigger/ActionManager.h @@ -19,13 +19,13 @@ namespace ctx { /* Forward Declaration */ - class Json; + class CtxJson1; namespace trigger { namespace action_manager { - void triggerAction(Json& action, std::string pkgId); + void triggerAction(CtxJson1& action, std::string pkgId); } /* namespace action_manager */ diff --git a/src/agent/legacy/trigger/ContextMonitor.cpp b/src/agent/legacy/trigger/ContextMonitor.cpp index 5dab7f5..86d8c99 100644 --- a/src/agent/legacy/trigger/ContextMonitor.cpp +++ b/src/agent/legacy/trigger/ContextMonitor.cpp @@ -75,7 +75,7 @@ void ContextMonitor::destroy() } } -int ContextMonitor::subscribe(int ruleId, std::string subject, Json option, IContextListener* listener) +int ContextMonitor::subscribe(int ruleId, std::string subject, CtxJson1 option, IContextListener* listener) { int reqId = __subscribe(subject.c_str(), &option, listener); IF_FAIL_RETURN_TAG(reqId > 0, reqId, _E, "Subscribe event failed"); @@ -84,7 +84,7 @@ int ContextMonitor::subscribe(int ruleId, std::string subject, Json option, ICon return ERR_NONE; } -int ContextMonitor::__subscribe(const char* subject, Json* option, IContextListener* listener) +int ContextMonitor::__subscribe(const char* subject, CtxJson1* option, IContextListener* listener) { IF_FAIL_RETURN(subject, ERR_INVALID_PARAMETER); @@ -113,7 +113,7 @@ int ContextMonitor::__subscribe(const char* subject, Json* option, IContextListe return rid; } -int ContextMonitor::unsubscribe(int ruleId, std::string subject, Json option, IContextListener* listener) +int ContextMonitor::unsubscribe(int ruleId, std::string subject, CtxJson1 option, IContextListener* listener) { int rid = __findSub(REQ_SUBSCRIBE, subject.c_str(), &option); if (rid < 0) { @@ -138,7 +138,7 @@ void ContextMonitor::__unsubscribe(const char *subject, int subscriptionId) __removeSub(REQ_SUBSCRIBE, subscriptionId); } -int ContextMonitor::read(std::string subject, Json option, IContextListener* listener) +int ContextMonitor::read(std::string subject, CtxJson1 option, IContextListener* listener) { int reqId = __read(subject.c_str(), &option, listener); IF_FAIL_RETURN_TAG(reqId > 0, ERR_OPERATION_FAILED, _E, "Read condition failed"); @@ -147,7 +147,7 @@ int ContextMonitor::read(std::string subject, Json option, IContextListener* lis return ERR_NONE; } -int ContextMonitor::__read(const char* subject, Json* option, IContextListener* listener) +int ContextMonitor::__read(const char* subject, CtxJson1* option, IContextListener* listener) { IF_FAIL_RETURN(subject, ERR_INVALID_PARAMETER); @@ -187,12 +187,12 @@ bool ContextMonitor::isAllowed(const char *client, const char *subject) return true; } -int ContextMonitor::__findSub(RequestType type, const char* subject, Json* option) +int ContextMonitor::__findSub(RequestType type, const char* subject, CtxJson1* option) { // @return request id std::map* map = (type == REQ_SUBSCRIBE)? &__subscrMap : &__readMap; - Json jOpt; + CtxJson1 jOpt; if (option) { jOpt = *option; } @@ -206,7 +206,7 @@ int ContextMonitor::__findSub(RequestType type, const char* subject, Json* optio return -1; } -bool ContextMonitor::__addSub(RequestType type, int sid, const char* subject, Json* option, IContextListener* listener) +bool ContextMonitor::__addSub(RequestType type, int sid, const char* subject, CtxJson1* option, IContextListener* listener) { std::map* map = (type == REQ_SUBSCRIBE)? &__subscrMap : &__readMap; @@ -218,11 +218,11 @@ bool ContextMonitor::__addSub(RequestType type, int sid, const char* subject, Js return true; } -void ContextMonitor::__removeSub(RequestType type, const char* subject, Json* option) +void ContextMonitor::__removeSub(RequestType type, const char* subject, CtxJson1* option) { std::map* map = (type == REQ_SUBSCRIBE)? &__subscrMap : &__readMap; - Json jOpt; + CtxJson1 jOpt; if (option) { jOpt = *option; } @@ -281,7 +281,7 @@ int ContextMonitor::__removeListener(RequestType type, int sid, IContextListener return info->listenerList.size(); } -void ContextMonitor::replyResult(int reqId, int error, Json* requestResult) +void ContextMonitor::replyResult(int reqId, int error, CtxJson1* requestResult) { _D("Request result received: %d", reqId); @@ -289,7 +289,7 @@ void ContextMonitor::replyResult(int reqId, int error, Json* requestResult) __lastErr = error; } -void ContextMonitor::replyResult(int reqId, int error, const char* subject, Json* option, Json* fact) +void ContextMonitor::replyResult(int reqId, int error, const char* subject, CtxJson1* option, CtxJson1* fact) { _D(YELLOW("Condition received: subject(%s), option(%s), fact(%s)"), subject, option->str().c_str(), fact->str().c_str()); @@ -304,7 +304,7 @@ void ContextMonitor::replyResult(int reqId, int error, const char* subject, Json __removeSub(REQ_READ, reqId); } -void ContextMonitor::publishFact(int reqId, int error, const char* subject, Json* option, Json* fact) +void ContextMonitor::publishFact(int reqId, int error, const char* subject, CtxJson1* option, CtxJson1* fact) { _D(YELLOW("Event received: subject(%s), option(%s), fact(%s)"), subject, option->str().c_str(), fact->str().c_str()); diff --git a/src/agent/legacy/trigger/ContextMonitor.h b/src/agent/legacy/trigger/ContextMonitor.h index 473cac2..6f65cdf 100644 --- a/src/agent/legacy/trigger/ContextMonitor.h +++ b/src/agent/legacy/trigger/ContextMonitor.h @@ -19,7 +19,7 @@ #include #include -#include +#include #include namespace ctx { @@ -37,15 +37,15 @@ namespace trigger { static void setContextManager(ContextManager* ctxMgr); static void destroy(); - int subscribe(int ruleId, std::string subject, Json option, IContextListener* listener); - int unsubscribe(int ruleId, std::string subject, Json option, IContextListener* listener); - int read(std::string subject, Json option, IContextListener* listener); + int subscribe(int ruleId, std::string subject, CtxJson1 option, IContextListener* listener); + int unsubscribe(int ruleId, std::string subject, CtxJson1 option, IContextListener* listener); + int read(std::string subject, CtxJson1 option, IContextListener* listener); bool isSupported(std::string subject); bool isAllowed(const char *client, const char *subject); - void replyResult(int reqId, int error, Json *requestResult = NULL); - void replyResult(int reqId, int error, const char *subject, Json *option, Json *fact); - void publishFact(int reqId, int error, const char *subject, Json *option, Json *fact); + void replyResult(int reqId, int error, CtxJson1 *requestResult = NULL); + void replyResult(int reqId, int error, const char *subject, CtxJson1 *option, CtxJson1 *fact); + void publishFact(int reqId, int error, const char *subject, CtxJson1 *option, CtxJson1 *fact); private: ContextMonitor(); @@ -55,17 +55,17 @@ namespace trigger { static ContextMonitor *__instance; static ContextManager *__contextMgr; - int __subscribe(const char* subject, Json* option, IContextListener* listener); + int __subscribe(const char* subject, CtxJson1* option, IContextListener* listener); void __unsubscribe(const char *subject, int subscriptionId); - int __read(const char *subject, Json *option, IContextListener* listener); + int __read(const char *subject, CtxJson1 *option, IContextListener* listener); struct SubscrInfo { int sid; std::string subject; - Json option; + CtxJson1 option; std::list listenerList; - SubscrInfo(int id, const char *subj, Json *opt) : + SubscrInfo(int id, const char *subj, CtxJson1 *opt) : sid(id), subject(subj) { @@ -77,9 +77,9 @@ namespace trigger { std::map __subscrMap; std::map __readMap; - int __findSub(RequestType type, const char *subject, Json *option); - bool __addSub(RequestType type, int sid, const char *subject, Json *option, IContextListener* listener); - void __removeSub(RequestType type, const char *subject, Json *option); + int __findSub(RequestType type, const char *subject, CtxJson1 *option); + bool __addSub(RequestType type, int sid, const char *subject, CtxJson1 *option, IContextListener* listener); + void __removeSub(RequestType type, const char *subject, CtxJson1 *option); void __removeSub(RequestType type, int sid); int __addListener(RequestType type, int sid, IContextListener* listener); int __removeListener(RequestType type, int sid, IContextListener* listener); diff --git a/src/agent/legacy/trigger/FactRequest.cpp b/src/agent/legacy/trigger/FactRequest.cpp index fc09d9d..da9212c 100644 --- a/src/agent/legacy/trigger/FactRequest.cpp +++ b/src/agent/legacy/trigger/FactRequest.cpp @@ -45,7 +45,7 @@ bool FactRequest::reply(int error) return true; } -bool FactRequest::reply(int error, Json& requestResult) +bool FactRequest::reply(int error, CtxJson1& requestResult) { IF_FAIL_RETURN(!__replied && __ctxMonitor, true); __ctxMonitor->replyResult(__reqId, error, &requestResult); @@ -53,14 +53,14 @@ bool FactRequest::reply(int error, Json& requestResult) return true; } -bool FactRequest::reply(int error, Json& requestResult, Json& dataRead) +bool FactRequest::reply(int error, CtxJson1& requestResult, CtxJson1& dataRead) { IF_FAIL_RETURN(!__replied && __ctxMonitor, true); __ctxMonitor->replyResult(__reqId, error, __subject.c_str(), &getDescription(), &dataRead); return (__replied = true); } -bool FactRequest::publish(int error, Json& data) +bool FactRequest::publish(int error, CtxJson1& data) { IF_FAIL_RETURN(__ctxMonitor, true); __ctxMonitor->publishFact(__reqId, error, __subject.c_str(), &getDescription(), &data); diff --git a/src/agent/legacy/trigger/FactRequest.h b/src/agent/legacy/trigger/FactRequest.h index 8c54fe8..2c18b4a 100644 --- a/src/agent/legacy/trigger/FactRequest.h +++ b/src/agent/legacy/trigger/FactRequest.h @@ -31,9 +31,9 @@ namespace trigger { const char* getClient(); bool reply(int error); - bool reply(int error, ctx::Json& requestResult); - bool reply(int error, ctx::Json& requestResult, ctx::Json& dataRead); - bool publish(int error, ctx::Json& data); + bool reply(int error, ctx::CtxJson1& requestResult); + bool reply(int error, ctx::CtxJson1& requestResult, ctx::CtxJson1& dataRead); + bool publish(int error, ctx::CtxJson1& data); private: ContextMonitor *__ctxMonitor; diff --git a/src/agent/legacy/trigger/IContextListener.h b/src/agent/legacy/trigger/IContextListener.h index 826ab41..1311254 100644 --- a/src/agent/legacy/trigger/IContextListener.h +++ b/src/agent/legacy/trigger/IContextListener.h @@ -19,7 +19,7 @@ namespace ctx { /* Forward Declaration */ - class Json; + class CtxJson1; namespace trigger { @@ -27,9 +27,9 @@ namespace trigger { public: virtual ~IContextListener() {} - virtual void onEventReceived(std::string name, Json option, Json data) = 0; + virtual void onEventReceived(std::string name, CtxJson1 option, CtxJson1 data) = 0; - virtual void onConditionReceived(std::string name, Json option, Json data) = 0; + virtual void onConditionReceived(std::string name, CtxJson1 option, CtxJson1 data) = 0; }; } /* namespace trigger */ diff --git a/src/agent/legacy/trigger/Rule.cpp b/src/agent/legacy/trigger/Rule.cpp index 33b2ef8..d6bff32 100644 --- a/src/agent/legacy/trigger/Rule.cpp +++ b/src/agent/legacy/trigger/Rule.cpp @@ -27,7 +27,7 @@ using namespace ctx::trigger; RuleManager *Rule::__ruleMgr = NULL; -Rule::Rule(int i, Json& d, const char* p, RuleManager* rm) : +Rule::Rule(int i, CtxJson1& d, const char* p, RuleManager* rm) : __result(EMPTY_JSON_OBJECT), id(i), pkgId(p) @@ -41,25 +41,25 @@ Rule::Rule(int i, Json& d, const char* p, RuleManager* rm) : __statement = d.str(); // Event - Json e; + CtxJson1 e; d.get(NULL, TRIG_RULE_KEY_EVENT, &e); __event = new(std::nothrow) ContextItem(e); // Condition int condNum = d.getSize(NULL, TRIG_RULE_KEY_CONDITION); for (int j = 0; j < condNum; j++) { - Json c; + CtxJson1 c; d.getAt(NULL, TRIG_RULE_KEY_CONDITION, j, &c); __condition.push_back(new(std::nothrow) ContextItem(c)); } // Extra - Json extra; + CtxJson1 extra; d.get(NULL, _TRIG_RULE_KEY_EXTRA, &extra); __extra = extra.str(); // Action - Json a; + CtxJson1 a; d.get(NULL, TRIG_RULE_KEY_ACTION, &a); __action = a.str(); } @@ -95,7 +95,7 @@ int Rule::stop(void) return error; } -bool Rule::__setConditionOptionBasedOnEvent(Json& option) +bool Rule::__setConditionOptionBasedOnEvent(CtxJson1& option) { // Set condition option if it references event data std::list optionKeys; @@ -128,7 +128,7 @@ bool Rule::__setConditionOptionBasedOnEvent(Json& option) return true; } -void Rule::onEventReceived(std::string name, Json option, Json data) +void Rule::onEventReceived(std::string name, CtxJson1 option, CtxJson1 data) { if (__result != EMPTY_JSON_OBJECT) { __clearResult(); @@ -157,7 +157,7 @@ void Rule::onEventReceived(std::string name, Json option, Json data) // Request read conditions for (auto it = __condition.begin(); it != __condition.end(); ++it) { - Json condOption = (*it)->option.str(); + CtxJson1 condOption = (*it)->option.str(); if (!__setConditionOptionBasedOnEvent(condOption)) { // condOption should be copy of original option. __clearResult(); return; @@ -170,12 +170,12 @@ void Rule::onEventReceived(std::string name, Json option, Json data) // TODO timer set } -void Rule::onConditionReceived(std::string name, Json option, Json data) +void Rule::onConditionReceived(std::string name, CtxJson1 option, CtxJson1 data) { _D("Rule%d received condition data", id); // Set condition data - Json item; + CtxJson1 item; item.set(NULL, FACT_KEY_NAME, name); item.set(NULL, FACT_KEY_OPTION, option); item.set(NULL, FACT_KEY_DATA, data); diff --git a/src/agent/legacy/trigger/Rule.h b/src/agent/legacy/trigger/Rule.h index 1192b6e..cce117a 100644 --- a/src/agent/legacy/trigger/Rule.h +++ b/src/agent/legacy/trigger/Rule.h @@ -19,7 +19,7 @@ #include #include -#include +#include #include "IContextListener.h" namespace ctx { @@ -32,29 +32,29 @@ namespace trigger { private: struct ContextItem { std::string name; - ctx::Json option; - ContextItem(ctx::Json item) { + ctx::CtxJson1 option; + ContextItem(ctx::CtxJson1 item) { std::list keyList; item.getKeys(&keyList); name = *keyList.begin(); - ctx::Json o; + ctx::CtxJson1 o; if (item.get(name.c_str(), TRIG_RULE_KEY_OPTION, &o)) option = o.str(); } }; - ctx::Json __statement; + ctx::CtxJson1 __statement; ContextItem* __event; std::list __condition; - ctx::Json __extra; - ctx::Json __action; - ctx::Json __result; + ctx::CtxJson1 __extra; + ctx::CtxJson1 __action; + ctx::CtxJson1 __result; static RuleManager* __ruleMgr; void __clearResult(void); - bool __setConditionOptionBasedOnEvent(ctx::Json& option); + bool __setConditionOptionBasedOnEvent(ctx::CtxJson1& option); void __onContextDataPrepared(void); static gboolean __handleUninstalledRule(gpointer data); @@ -63,14 +63,14 @@ namespace trigger { int id; std::string pkgId; - Rule(int i, ctx::Json& d, const char* p, RuleManager* rm); + Rule(int i, ctx::CtxJson1& d, const char* p, RuleManager* rm); ~Rule(); int start(void); int stop(void); - void onEventReceived(std::string name, ctx::Json option, ctx::Json data); - void onConditionReceived(std::string name, ctx::Json option, ctx::Json data); + void onEventReceived(std::string name, ctx::CtxJson1 option, ctx::CtxJson1 data); + void onConditionReceived(std::string name, ctx::CtxJson1 option, ctx::CtxJson1 data); }; diff --git a/src/agent/legacy/trigger/RuleEvaluator.cpp b/src/agent/legacy/trigger/RuleEvaluator.cpp index b0e5833..6dc66b4 100644 --- a/src/agent/legacy/trigger/RuleEvaluator.cpp +++ b/src/agent/legacy/trigger/RuleEvaluator.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ -#include +#include #include #include #include "RuleEvaluator.h" @@ -35,7 +35,7 @@ RuleEvaluator::RuleEvaluator() } template -bool RuleEvaluator::__evaluateSingleData(T factVal, Json& comparison, std::string op) +bool RuleEvaluator::__evaluateSingleData(T factVal, CtxJson1& comparison, std::string op) { T ruleVal; comparison.get(NULL, TRIG_RULE_KEY_VALUE, &ruleVal); @@ -58,7 +58,7 @@ bool RuleEvaluator::__evaluateSingleData(T factVal, Json& comparison, std::strin } template -bool RuleEvaluator::__evaluateDualData(T factVal, Json& comparison, std::string op) +bool RuleEvaluator::__evaluateDualData(T factVal, CtxJson1& comparison, std::string op) { T ruleVal1, ruleVal2; comparison.getAt(NULL, TRIG_RULE_KEY_VALUE, 0, &ruleVal1); @@ -74,7 +74,7 @@ bool RuleEvaluator::__evaluateDualData(T factVal, Json& comparison, std::string } template -bool RuleEvaluator::__evaluateMultipleData(T factVal, Json& comparison, std::string op) +bool RuleEvaluator::__evaluateMultipleData(T factVal, CtxJson1& comparison, std::string op) { T ruleVal; for (int i = 0; comparison.getAt(NULL, TRIG_RULE_KEY_VALUE, i, &ruleVal); i++) { @@ -95,7 +95,7 @@ bool RuleEvaluator::__evaluateMultipleData(T factVal, Json& comparison, std::str } template -bool RuleEvaluator::__evaluateData(T factVal, Json& comparison) +bool RuleEvaluator::__evaluateData(T factVal, CtxJson1& comparison) { std::string op; comparison.get(NULL, TRIG_RULE_KEY_OPERATOR, &op); @@ -113,7 +113,7 @@ bool RuleEvaluator::__evaluateData(T factVal, Json& comparison) return false; } -void RuleEvaluator::__replaceSingleDataReferences(Json& eventFactData, Json& ruleComp, std::string& dataKey) +void RuleEvaluator::__replaceSingleDataReferences(CtxJson1& eventFactData, CtxJson1& ruleComp, std::string& dataKey) { std::string refVal; std::string eventRefStr; @@ -137,7 +137,7 @@ void RuleEvaluator::__replaceSingleDataReferences(Json& eventFactData, Json& rul } } -void RuleEvaluator::__replaceMultipleDataReferences(Json& eventFactData, Json& ruleComp, std::string& dataKey) +void RuleEvaluator::__replaceMultipleDataReferences(CtxJson1& eventFactData, CtxJson1& ruleComp, std::string& dataKey) { std::string refVal; std::string eventRefStr; @@ -159,7 +159,7 @@ void RuleEvaluator::__replaceMultipleDataReferences(Json& eventFactData, Json& r } } -void RuleEvaluator::__replaceDataReferences(Json eventFactData, Json& ruleComp) +void RuleEvaluator::__replaceDataReferences(CtxJson1 eventFactData, CtxJson1& ruleComp) { // Replace referencing data to actual value std::list compKeys; @@ -183,12 +183,12 @@ void RuleEvaluator::__replaceDataReferences(Json eventFactData, Json& ruleComp) } } -bool RuleEvaluator::__evaluateItem(Json& factItem, Json& ruleItem, std::string logicalOp) +bool RuleEvaluator::__evaluateItem(CtxJson1& factItem, CtxJson1& ruleItem, std::string logicalOp) { std::string name; factItem.get(NULL, FACT_KEY_NAME, &name); - Json comparison; + CtxJson1 comparison; ruleItem.get(name.c_str(), TRIG_RULE_KEY_COMPARISON, &comparison); std::list compKeys; @@ -202,7 +202,7 @@ bool RuleEvaluator::__evaluateItem(Json& factItem, Json& ruleItem, std::string l for (auto it = compKeys.begin(); it != compKeys.end(); ++it) { std::string dataKey = *it; - Json comp; + CtxJson1 comp; comparison.get(NULL, dataKey.c_str(), &comp); std::string factValStr; @@ -227,10 +227,10 @@ bool RuleEvaluator::__evaluateItem(Json& factItem, Json& ruleItem, std::string l return isConjunction; } -bool RuleEvaluator::__evaluateRuleEvent(Json& fact, Json& rule) +bool RuleEvaluator::__evaluateRuleEvent(CtxJson1& fact, CtxJson1& rule) { - Json factItem; - Json ruleItem; + CtxJson1 factItem; + CtxJson1 ruleItem; fact.get(NULL, FACT_KEY_EVENT, &factItem); rule.get(NULL, TRIG_RULE_KEY_EVENT, &ruleItem); @@ -240,13 +240,13 @@ bool RuleEvaluator::__evaluateRuleEvent(Json& fact, Json& rule) return __evaluateItem(factItem, ruleItem, eventOp); } -Json RuleEvaluator::__getConditionFact(Json& fact, Json& ruleCond) +CtxJson1 RuleEvaluator::__getConditionFact(CtxJson1& fact, CtxJson1& ruleCond) { std::list condKey; ruleCond.getKeys(&condKey); std::string ruleCondName = *(condKey.begin()); - Json factCond; + CtxJson1 factCond; for (int i = 0; fact.getAt(NULL, FACT_KEY_CONDITION, i, &factCond); i++) { // Check if fact item name is matched with condition std::string factCondName; @@ -256,8 +256,8 @@ Json RuleEvaluator::__getConditionFact(Json& fact, Json& ruleCond) } // Check if fact item option is mathced with condition - Json ruleCondOption; - Json factCondOption; + CtxJson1 ruleCondOption; + CtxJson1 factCondOption; ruleCond.get(ruleCondName.c_str(), TRIG_RULE_KEY_OPTION, &ruleCondOption); factCond.get(NULL, FACT_KEY_OPTION, &factCondOption); if (factCondOption == ruleCondOption) { @@ -269,15 +269,15 @@ Json RuleEvaluator::__getConditionFact(Json& fact, Json& ruleCond) return EMPTY_JSON_OBJECT; } -bool RuleEvaluator::__evaluateRuleCondition(Json& fact, Json& rule) +bool RuleEvaluator::__evaluateRuleCondition(CtxJson1& fact, CtxJson1& rule) { std::string ruleOp; rule.get(_TRIG_RULE_KEY_EXTRA, _TRIG_RULE_KEY_RULE_LOGICAL_OP, &ruleOp); bool isConjunction = (TRIG_RULE_LOGICAL_CONJUNCTION == ruleOp); - Json ruleCond; + CtxJson1 ruleCond; for (int i = 0; rule.getAt(NULL, TRIG_RULE_KEY_CONDITION, i, &ruleCond); i++) { - Json factCond = __getConditionFact(fact, ruleCond); + CtxJson1 factCond = __getConditionFact(fact, ruleCond); bool result; if (factCond == EMPTY_JSON_OBJECT) { @@ -298,7 +298,7 @@ bool RuleEvaluator::__evaluateRuleCondition(Json& fact, Json& rule) return isConjunction; } -bool RuleEvaluator::__replaceOptionReferences(Json eventFactData, Json& ruleOption) +bool RuleEvaluator::__replaceOptionReferences(CtxJson1 eventFactData, CtxJson1& ruleOption) { // Replace referencing option to actual value std::string refVal; @@ -332,27 +332,27 @@ bool RuleEvaluator::__replaceOptionReferences(Json eventFactData, Json& ruleOpti return true; } -bool RuleEvaluator::__replaceEventReferences(Json& fact, Json& rule) +bool RuleEvaluator::__replaceEventReferences(CtxJson1& fact, CtxJson1& rule) { // Replace referencing data/option to actual value - Json eventFactData; + CtxJson1 eventFactData; if (!fact.get(FACT_KEY_EVENT, FACT_KEY_DATA, &eventFactData)) { _E("No event data found, error"); return false; } - Json ruleCond; + CtxJson1 ruleCond; for (int i = 0; rule.getAt(NULL, TRIG_RULE_KEY_CONDITION, i, &ruleCond); i++) { std::list condKey; ruleCond.getKeys(&condKey); std::string ruleCondName = *(condKey.begin()); - Json ruleComp; + CtxJson1 ruleComp; for (int j = 0; ruleCond.getAt(ruleCondName.c_str(), TRIG_RULE_KEY_COMPARISON, j, &ruleComp); j++) { __replaceDataReferences(eventFactData, ruleComp); } - Json ruleOption; + CtxJson1 ruleOption; if (ruleCond.get(ruleCondName.c_str(), TRIG_RULE_KEY_OPTION, &ruleOption)) { __replaceOptionReferences(eventFactData, ruleOption); } @@ -361,16 +361,16 @@ bool RuleEvaluator::__replaceEventReferences(Json& fact, Json& rule) return true; } -bool RuleEvaluator::evaluateRule(Json rule, Json fact) +bool RuleEvaluator::evaluateRule(CtxJson1 rule, CtxJson1 fact) { _D("Rule is %s ", rule.str().c_str()); _D("fact is %s ", fact.str().c_str()); RuleEvaluator eval; bool ret; - Json tempJson; + CtxJson1 tempJson; if (fact.get(NULL, FACT_KEY_CONDITION, &tempJson)) { - Json ruleCopy(rule.str()); + CtxJson1 ruleCopy(rule.str()); if (!eval.__replaceEventReferences(fact, ruleCopy)) { _W("Replace failed"); } diff --git a/src/agent/legacy/trigger/RuleEvaluator.h b/src/agent/legacy/trigger/RuleEvaluator.h index 3e79682..e9c0370 100644 --- a/src/agent/legacy/trigger/RuleEvaluator.h +++ b/src/agent/legacy/trigger/RuleEvaluator.h @@ -19,7 +19,7 @@ namespace ctx { - class Json; + class CtxJson1; namespace trigger { @@ -27,24 +27,24 @@ namespace trigger { private: RuleEvaluator(); - bool __evaluateRuleEvent(ctx::Json& fact, ctx::Json& rule); - bool __evaluateRuleCondition(ctx::Json& fact, ctx::Json& rule); - bool __evaluateItem(ctx::Json& factItem, ctx::Json& ruleItem, std::string logicalOp); - template bool __evaluateData(T factVal, Json& comparison); - template bool __evaluateSingleData(T factVal, Json& comparison, std::string op); - template bool __evaluateDualData(T factVal, Json& comparison, std::string op); - template bool __evaluateMultipleData(T factVal, Json& comparison, std::string op); + bool __evaluateRuleEvent(ctx::CtxJson1& fact, ctx::CtxJson1& rule); + bool __evaluateRuleCondition(ctx::CtxJson1& fact, ctx::CtxJson1& rule); + bool __evaluateItem(ctx::CtxJson1& factItem, ctx::CtxJson1& ruleItem, std::string logicalOp); + template bool __evaluateData(T factVal, CtxJson1& comparison); + template bool __evaluateSingleData(T factVal, CtxJson1& comparison, std::string op); + template bool __evaluateDualData(T factVal, CtxJson1& comparison, std::string op); + template bool __evaluateMultipleData(T factVal, CtxJson1& comparison, std::string op); - ctx::Json __getConditionFact(ctx::Json& fact, ctx::Json& ruleCond); + ctx::CtxJson1 __getConditionFact(ctx::CtxJson1& fact, ctx::CtxJson1& ruleCond); - bool __replaceEventReferences(ctx::Json& fact, ctx::Json& rule); - void __replaceDataReferences(ctx::Json eventFactData, ctx::Json& ruleComp); - void __replaceSingleDataReferences(ctx::Json& eventFactData, ctx::Json& ruleComp, std::string& dataKey); - void __replaceMultipleDataReferences(ctx::Json& eventFactData, ctx::Json& ruleComp, std::string& dataKey); - bool __replaceOptionReferences(ctx::Json eventFactData, ctx::Json& ruleOption); + bool __replaceEventReferences(ctx::CtxJson1& fact, ctx::CtxJson1& rule); + void __replaceDataReferences(ctx::CtxJson1 eventFactData, ctx::CtxJson1& ruleComp); + void __replaceSingleDataReferences(ctx::CtxJson1& eventFactData, ctx::CtxJson1& ruleComp, std::string& dataKey); + void __replaceMultipleDataReferences(ctx::CtxJson1& eventFactData, ctx::CtxJson1& ruleComp, std::string& dataKey); + bool __replaceOptionReferences(ctx::CtxJson1 eventFactData, ctx::CtxJson1& ruleOption); public: - static bool evaluateRule(ctx::Json rule, ctx::Json data); + static bool evaluateRule(ctx::CtxJson1 rule, ctx::CtxJson1 data); }; } /* namespace trigger */ diff --git a/src/agent/legacy/trigger/RuleManager.cpp b/src/agent/legacy/trigger/RuleManager.cpp index 11a9561..5386cef 100644 --- a/src/agent/legacy/trigger/RuleManager.cpp +++ b/src/agent/legacy/trigger/RuleManager.cpp @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include "RuleManager.h" #include "ContextMonitor.h" #include "Rule.h" @@ -102,13 +102,13 @@ int RuleManager::__getUninstalledApp(void) // Return number of uninstalled apps std::string q1 = "SELECT DISTINCT packageId FROM ContextTriggerRule"; - std::vector record; + std::vector record; bool ret = __dbManager.executeSync(q1.c_str(), &record); IF_FAIL_RETURN_TAG(ret, -1, _E, "Query package ids of registered rules failed"); - std::vector::iterator vecEnd = record.end(); - for (std::vector::iterator vecPos = record.begin(); vecPos != vecEnd; ++vecPos) { - Json elem = *vecPos; + std::vector::iterator vecEnd = record.end(); + for (std::vector::iterator vecPos = record.begin(); vecPos != vecEnd; ++vecPos) { + CtxJson1 elem = *vecPos; std::string pkgId; elem.get(NULL, "packageId", &pkgId); @@ -166,13 +166,13 @@ int RuleManager::__clearRuleOfUninstalledPackage(bool isInit) q1 += pkgList; q1 += ")"; - std::vector record; + std::vector record; ret = __dbManager.executeSync(q1.c_str(), &record); IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Failed to query enabled rules of uninstalled packages"); - std::vector::iterator vecEnd = record.end(); - for (std::vector::iterator vecPos = record.begin(); vecPos != vecEnd; ++vecPos) { - Json elem = *vecPos; + std::vector::iterator vecEnd = record.end(); + for (std::vector::iterator vecPos = record.begin(); vecPos != vecEnd; ++vecPos) { + CtxJson1 elem = *vecPos; int ruleId; elem.get(NULL, "rowId", &ruleId); error = disableRule(ruleId); @@ -183,7 +183,7 @@ int RuleManager::__clearRuleOfUninstalledPackage(bool isInit) // Delete rules of uninstalled packages from DB std::string q2 = "DELETE FROM ContextTriggerRule WHERE " + pkgList; - std::vector dummy; + std::vector dummy; ret = __dbManager.executeSync(q2.c_str(), &dummy); IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Failed to remove rules from db"); _D("Uninstalled packages' rules are deleted from db"); @@ -196,15 +196,15 @@ int RuleManager::__clearRuleOfUninstalledPackage(bool isInit) int RuleManager::pauseRuleWithItem(std::string& subject) { std::string q = "SELECT rowId FROM ContextTriggerRule WHERE (status=2) AND (details LIKE '%\"ITEM_NAME\":\"" + subject + "\"%');"; - std::vector record; + std::vector record; bool ret = __dbManager.executeSync(q.c_str(), &record); IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Failed to query rowIds to be paused"); IF_FAIL_RETURN(record.size() > 0, ERR_NONE); _D("Pause rules related to %s", subject.c_str()); - std::vector::iterator vecEnd = record.end(); - for (std::vector::iterator vecPos = record.begin(); vecPos != vecEnd; ++vecPos) { - Json elem = *vecPos; + std::vector::iterator vecEnd = record.end(); + for (std::vector::iterator vecPos = record.begin(); vecPos != vecEnd; ++vecPos) { + CtxJson1 elem = *vecPos; int rowId; elem.get(NULL, "rowId", &rowId); @@ -218,16 +218,16 @@ int RuleManager::pauseRuleWithItem(std::string& subject) int RuleManager::resumeRuleWithItem(std::string& subject) { std::string q = "SELECT rowId FROM ContextTriggerRule WHERE (status=1) AND (details LIKE '%\"ITEM_NAME\":\"" + subject + "\"%');"; - std::vector record; + std::vector record; bool ret = __dbManager.executeSync(q.c_str(), &record); IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Query paused rule ids failed"); IF_FAIL_RETURN(record.size() > 0, ERR_NONE); _D("Resume rules related to %s", subject.c_str()); std::string qRowId; - std::vector::iterator vecEnd = record.end(); - for (std::vector::iterator vecPos = record.begin(); vecPos != vecEnd; ++vecPos) { - Json elem = *vecPos; + std::vector::iterator vecEnd = record.end(); + for (std::vector::iterator vecPos = record.begin(); vecPos != vecEnd; ++vecPos) { + CtxJson1 elem = *vecPos; int rowId; elem.get(NULL, "rowId", &rowId); @@ -243,7 +243,7 @@ bool RuleManager::__reenableRule(void) int error; std::string q = "SELECT rowId FROM ContextTriggerRule WHERE status = 2"; - std::vector record; + std::vector record; bool ret = __dbManager.executeSync(q.c_str(), &record); IF_FAIL_RETURN_TAG(ret, false, _E, "Query rowIds of enabled rules failed"); IF_FAIL_RETURN_TAG(record.size() > 0, true, _D, "No rule to re-enable"); @@ -252,9 +252,9 @@ bool RuleManager::__reenableRule(void) std::string qRowId; qRowId.clear(); - std::vector::iterator vecEnd = record.end(); - for (std::vector::iterator vecPos = record.begin(); vecPos != vecEnd; ++vecPos) { - Json elem = *vecPos; + std::vector::iterator vecEnd = record.end(); + for (std::vector::iterator vecPos = record.begin(); vecPos != vecEnd; ++vecPos) { + CtxJson1 elem = *vecPos; int rowId; elem.get(NULL, "rowId", &rowId); @@ -270,17 +270,17 @@ bool RuleManager::__reenableRule(void) // For rules which is failed to re-enable std::string qUpdate = "UPDATE ContextTriggerRule SET status = 1 WHERE " + qRowId; - std::vector record2; + std::vector record2; ret = __dbManager.executeSync(qUpdate.c_str(), &record2); IF_FAIL_RETURN_TAG(ret, false, _E, "Failed to update rules as paused"); return true; } -bool RuleManager::__ruleEquals(Json& lRule, Json& rRule) +bool RuleManager::__ruleEquals(CtxJson1& lRule, CtxJson1& rRule) { // Compare event - Json lEvent, rEvent; + CtxJson1 lEvent, rEvent; lRule.get(NULL, TRIG_RULE_KEY_EVENT, &lEvent); rRule.get(NULL, TRIG_RULE_KEY_EVENT, &rEvent); @@ -306,14 +306,14 @@ bool RuleManager::__ruleEquals(Json& lRule, Json& rRule) for (int i = 0; i < lCondCnt; i++) { bool found = false; - Json lCond; + CtxJson1 lCond; lRule.getAt(NULL, TRIG_RULE_KEY_CONDITION, i, &lCond); std::string lCOp; lRule.getAt(_TRIG_RULE_KEY_EXTRA, _TRIG_RULE_KEY_CONDITION_LOGICAL_OP, i, &lCOp); for (int j = 0; j < lCondCnt; j++) { - Json rCond; + CtxJson1 rCond; rRule.getAt(NULL, TRIG_RULE_KEY_CONDITION, j, &rCond); std::string rCOp; @@ -329,7 +329,7 @@ bool RuleManager::__ruleEquals(Json& lRule, Json& rRule) } // Compare action - Json lAction, rAction; + CtxJson1 lAction, rAction; lRule.get(NULL, TRIG_RULE_KEY_ACTION, &lAction); rRule.get(NULL, TRIG_RULE_KEY_ACTION, &rAction); if (lAction != rAction) @@ -338,26 +338,26 @@ bool RuleManager::__ruleEquals(Json& lRule, Json& rRule) return true; } -int64_t RuleManager::__getDuplicatedRuleId(std::string pkgId, Json& rule) +int64_t RuleManager::__getDuplicatedRuleId(std::string pkgId, CtxJson1& rule) { std::string q = "SELECT rowId, description, details FROM ContextTriggerRule WHERE packageId = '"; q += pkgId; q += "'"; - std::vector record; + std::vector record; bool ret = __dbManager.executeSync(q.c_str(), &record); IF_FAIL_RETURN_TAG(ret, false, _E, "Query rowId, details by package id failed"); std::string rDesc; rule.get(NULL, TRIG_RULE_KEY_DESCRIPTION, &rDesc); - Json rDetails = rule.str(); + CtxJson1 rDetails = rule.str(); rDetails.remove(NULL, TRIG_RULE_KEY_DESCRIPTION); - std::vector::iterator vecEnd = record.end(); - for (std::vector::iterator vecPos = record.begin(); vecPos != vecEnd; ++vecPos) { - Json elem = *vecPos; + std::vector::iterator vecEnd = record.end(); + for (std::vector::iterator vecPos = record.begin(); vecPos != vecEnd; ++vecPos) { + CtxJson1 elem = *vecPos; std::string dStr; - Json details; + CtxJson1 details; elem.get(NULL, "details", &dStr); details = dStr; @@ -373,7 +373,7 @@ int64_t RuleManager::__getDuplicatedRuleId(std::string pkgId, Json& rule) // Only description is changed std::string qUpdate = "UPDATE ContextTriggerRule SET description='" + rDesc + "' WHERE rowId = " + __intToString(rowId); - std::vector dummy; + std::vector dummy; ret = __dbManager.executeSync(qUpdate.c_str(), &dummy); if (ret) { _D("Rule%lld description is updated", rowId); @@ -389,13 +389,13 @@ int64_t RuleManager::__getDuplicatedRuleId(std::string pkgId, Json& rule) return -1; } -int RuleManager::__verifyRule(Json& rule, const char* creator) +int RuleManager::__verifyRule(CtxJson1& rule, const char* creator) { ContextMonitor* ctxMonitor = ContextMonitor::getInstance(); IF_FAIL_RETURN_TAG(ctxMonitor, ERR_OUT_OF_MEMORY, _E, "Memory allocation failed"); // Event - Json event; + CtxJson1 event; rule.get(NULL, TRIG_RULE_KEY_EVENT, &event); std::list eventKey; @@ -411,7 +411,7 @@ int RuleManager::__verifyRule(Json& rule, const char* creator) } // Conditions - Json cond; + CtxJson1 cond; for (int i = 0; rule.getAt(NULL, TRIG_RULE_KEY_CONDITION, i, &cond); i++) { std::list condKey; cond.getKeys(&condKey); @@ -429,7 +429,7 @@ int RuleManager::__verifyRule(Json& rule, const char* creator) return ERR_NONE; } -int RuleManager::addRule(std::string creator, const char* pkgId, Json rule, Json* ruleId) +int RuleManager::addRule(std::string creator, const char* pkgId, CtxJson1 rule, CtxJson1* ruleId) { bool ret; int64_t rid; @@ -447,11 +447,11 @@ int RuleManager::addRule(std::string creator, const char* pkgId, Json rule, Json } // Insert rule to rule table, get rule id - Json record; + CtxJson1 record; std::string description; rule.get(NULL, TRIG_RULE_KEY_DESCRIPTION, &description); - Json details = rule.str(); + CtxJson1 details = rule.str(); details.remove(NULL, TRIG_RULE_KEY_DESCRIPTION); record.set(NULL, "details", details.str()); @@ -480,7 +480,7 @@ int RuleManager::removeRule(int ruleId) std::string query = "DELETE FROM 'ContextTriggerRule' where rowId = "; query += __intToString(ruleId); - std::vector record; + std::vector record; ret = __dbManager.executeSync(query.c_str(), &record); IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Remove rule from db failed"); @@ -493,16 +493,16 @@ int RuleManager::enableRule(int ruleId) { int error; std::string query; - std::vector record; - std::vector dummy; + std::vector record; + std::vector dummy; std::string pkgId; - Json jRule; + CtxJson1 jRule; std::string tmp; std::string idStr = __intToString(ruleId); Rule* rule; - // Get rule Json by rule id; + // Get rule CtxJson1 by rule id; query = "SELECT details, packageId FROM ContextTriggerRule WHERE rowId = "; query += idStr; error = (__dbManager.executeSync(query.c_str(), &record))? ERR_NONE : ERR_OPERATION_FAILED; @@ -562,7 +562,7 @@ int RuleManager::disableRule(int ruleId) // Update db to set 'disabled' // TODO skip while clear uninstalled rule std::string query = "UPDATE ContextTriggerRule SET status = 0 WHERE rowId = "; query += __intToString(ruleId); - std::vector record; + std::vector record; ret = __dbManager.executeSync(query.c_str(), &record); IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Update db failed"); @@ -587,7 +587,7 @@ int RuleManager::pauseRule(int ruleId) std::string query = "UPDATE ContextTriggerRule SET status = 1 WHERE rowId = "; query += __intToString(ruleId); - std::vector record; + std::vector record; ret = __dbManager.executeSync(query.c_str(), &record); IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Update db failed"); @@ -605,7 +605,7 @@ int RuleManager::checkRule(std::string pkgId, int ruleId) std::string q = "SELECT packageId FROM ContextTriggerRule WHERE rowId ="; q += __intToString(ruleId); - std::vector record; + std::vector record; bool ret = __dbManager.executeSync(q.c_str(), &record); IF_FAIL_RETURN_TAG(ret, false, _E, "Query package id by rule id failed"); @@ -628,7 +628,7 @@ bool RuleManager::isRuleEnabled(int ruleId) std::string q = "SELECT status FROM ContextTriggerRule WHERE rowId ="; q += __intToString(ruleId); - std::vector record; + std::vector record; bool ret = __dbManager.executeSync(q.c_str(), &record); IF_FAIL_RETURN_TAG(ret, false, _E, "Query enabled by rule id failed"); @@ -638,7 +638,7 @@ bool RuleManager::isRuleEnabled(int ruleId) return (status != 0); } -int RuleManager::getRuleById(std::string pkgId, int ruleId, Json* requestResult) +int RuleManager::getRuleById(std::string pkgId, int ruleId, CtxJson1* requestResult) { std::string q = "SELECT description FROM ContextTriggerRule WHERE (packageId = '"; q += pkgId; @@ -646,7 +646,7 @@ int RuleManager::getRuleById(std::string pkgId, int ruleId, Json* requestResult) q += __intToString(ruleId); q += ")"; - std::vector record; + std::vector record; bool ret = __dbManager.executeSync(q.c_str(), &record); IF_FAIL_RETURN_TAG(ret, false, _E, "Query rule by rule id failed"); @@ -665,7 +665,7 @@ int RuleManager::getRuleById(std::string pkgId, int ruleId, Json* requestResult) return ERR_NONE; } -int RuleManager::getRuleIds(std::string pkgId, Json* requestResult) +int RuleManager::getRuleIds(std::string pkgId, CtxJson1* requestResult) { (*requestResult) = RULE_IDS_JSON; @@ -673,13 +673,13 @@ int RuleManager::getRuleIds(std::string pkgId, Json* requestResult) q += pkgId; q += "')"; - std::vector record; + std::vector record; bool ret = __dbManager.executeSync(q.c_str(), &record); IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Query rules failed"); - std::vector::iterator vecEnd = record.end(); - for (std::vector::iterator vecPos = record.begin(); vecPos != vecEnd; ++vecPos) { - Json elem = *vecPos; + std::vector::iterator vecEnd = record.end(); + for (std::vector::iterator vecPos = record.begin(); vecPos != vecEnd; ++vecPos) { + CtxJson1 elem = *vecPos; std::string id; int status; diff --git a/src/agent/legacy/trigger/RuleManager.h b/src/agent/legacy/trigger/RuleManager.h index cf47eee..2187aa2 100644 --- a/src/agent/legacy/trigger/RuleManager.h +++ b/src/agent/legacy/trigger/RuleManager.h @@ -23,7 +23,7 @@ namespace ctx { - class Json; + class CtxJson1; namespace trigger { @@ -36,12 +36,12 @@ namespace trigger { ~RuleManager(); bool init(); - int addRule(std::string creator, const char* pkgId, Json rule, Json* ruleId); + int addRule(std::string creator, const char* pkgId, CtxJson1 rule, CtxJson1* ruleId); int removeRule(int ruleId); int enableRule(int ruleId); int disableRule(int ruleId); - int getRuleById(std::string pkgId, int ruleId, Json* requestResult); - int getRuleIds(std::string pkgId, Json* requestResult); + int getRuleById(std::string pkgId, int ruleId, CtxJson1* requestResult); + int getRuleIds(std::string pkgId, CtxJson1* requestResult); int checkRule(std::string pkgId, int ruleId); bool isRuleEnabled(int ruleId); int pauseRuleWithItem(std::string& subject); @@ -53,9 +53,9 @@ namespace trigger { private: bool __reenableRule(void); - int __verifyRule(Json& rule, const char* creator); - int64_t __getDuplicatedRuleId(std::string pkgId, Json& rule); - bool __ruleEquals(Json& lRule, Json& rRule); + int __verifyRule(CtxJson1& rule, const char* creator); + int64_t __getDuplicatedRuleId(std::string pkgId, CtxJson1& rule); + bool __ruleEquals(CtxJson1& lRule, CtxJson1& rRule); int __getUninstalledApp(void); int __clearRuleOfUninstalledPackage(bool isInit = false); void __applyTemplates(void); diff --git a/src/agent/legacy/trigger/TemplateManager.cpp b/src/agent/legacy/trigger/TemplateManager.cpp index 65ddc39..6a952a1 100644 --- a/src/agent/legacy/trigger/TemplateManager.cpp +++ b/src/agent/legacy/trigger/TemplateManager.cpp @@ -79,7 +79,7 @@ bool TemplateManager::init() + "(name TEXT DEFAULT '' NOT NULL PRIMARY KEY, operation INTEGER DEFAULT 3 NOT NULL, " + "attributes TEXT DEFAULT '' NOT NULL, options TEXT DEFAULT '' NOT NULL, owner TEXT DEFAULT '' NOT NULL)"; - std::vector record; + std::vector record; bool ret = __dbManager.executeSync(q.c_str(), &record); IF_FAIL_RETURN_TAG(ret, false, _E, "Create template table failed"); @@ -93,8 +93,8 @@ void TemplateManager::applyTemplates() { std::string subject; int operation; - Json attributes; - Json options; + CtxJson1 attributes; + CtxJson1 options; std::string owner; std::string query; query.clear(); @@ -104,7 +104,7 @@ void TemplateManager::applyTemplates() } } -void TemplateManager::registerTemplate(std::string subject, int operation, Json attributes, Json options, std::string owner) +void TemplateManager::registerTemplate(std::string subject, int operation, CtxJson1 attributes, CtxJson1 options, std::string owner) { _D("[Add template] Subject: %s, Ops: %d, Owner: %s", subject.c_str(), operation, owner.c_str()); _J("Attr", attributes); @@ -118,7 +118,7 @@ void TemplateManager::registerTemplate(std::string subject, int operation, Json + subject + "', " + __intToString(operation) + ", '" + attributes.str() + "', '" + options.str() + "', '" + owner + "'); "; - std::vector record; + std::vector record; bool ret = __dbManager.executeSync(query.c_str(), &record); IF_FAIL_VOID_TAG(ret, _E, "Update template db failed"); @@ -132,7 +132,7 @@ void TemplateManager::unregisterTemplate(std::string subject) _D("[Remove template] Subject: %s", subject.c_str()); std::string query = "DELETE FROM ContextTriggerTemplate WHERE name = '" + subject + "'; "; - std::vector record; + std::vector record; bool ret = __dbManager.executeSync(query.c_str(), &record); IF_FAIL_VOID_TAG(ret, _E, "Update template db failed"); @@ -140,7 +140,7 @@ void TemplateManager::unregisterTemplate(std::string subject) } -std::string TemplateManager::__addTemplate(std::string &subject, int &operation, Json &attributes, Json &options, std::string &owner) +std::string TemplateManager::__addTemplate(std::string &subject, int &operation, CtxJson1 &attributes, CtxJson1 &options, std::string &owner) { _D("[Add template] Subject: %s, Ops: %d, Owner: %s", subject.c_str(), operation, owner.c_str()); _J("Attr", attributes); @@ -165,7 +165,7 @@ std::string TemplateManager::__removeTemplate(std::string &subject) return query; } -int TemplateManager::getTemplate(std::string &subject, Json* tmpl) +int TemplateManager::getTemplate(std::string &subject, CtxJson1* tmpl) { if (!__contextMgr->isSupported(subject.c_str())) return ERR_NOT_SUPPORTED; @@ -173,7 +173,7 @@ int TemplateManager::getTemplate(std::string &subject, Json* tmpl) // Update latest template information std::string q = "SELECT * FROM ContextTriggerTemplate WHERE name = '" + subject + "'"; - std::vector record; + std::vector record; bool ret = __dbManager.executeSync(q.c_str(), &record); IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Query template failed"); IF_FAIL_RETURN_TAG(record.size() > 0, ERR_NOT_SUPPORTED, _E, "Template(%s) not found", subject.c_str()); @@ -186,8 +186,8 @@ int TemplateManager::getTemplate(std::string &subject, Json* tmpl) tmpl->get(NULL, TRIG_TMPL_KEY_OPTION, &optStr); tmpl->get(NULL, TRIG_TMPL_KEY_ATTRIBUTE, &attrStr); - Json opt = optStr; - Json attr = attrStr; + CtxJson1 opt = optStr; + CtxJson1 attr = attrStr; tmpl->set(NULL, TRIG_TMPL_KEY_OPTION, opt); tmpl->set(NULL, TRIG_TMPL_KEY_ATTRIBUTE, attr); diff --git a/src/agent/legacy/trigger/TemplateManager.h b/src/agent/legacy/trigger/TemplateManager.h index c91ef15..69c0e06 100644 --- a/src/agent/legacy/trigger/TemplateManager.h +++ b/src/agent/legacy/trigger/TemplateManager.h @@ -17,7 +17,7 @@ #ifndef _CONTEXT_TRIGGER_TEMPLATE_MANAGER_H_ #define _CONTEXT_TRIGGER_TEMPLATE_MANAGER_H_ -#include +#include #include namespace ctx { @@ -36,8 +36,8 @@ namespace trigger { bool init(); void applyTemplates(); - int getTemplate(std::string &subject, Json* tmpl); - void registerTemplate(std::string subject, int operation, Json attributes, Json options, std::string owner); + int getTemplate(std::string &subject, CtxJson1* tmpl); + void registerTemplate(std::string subject, int operation, CtxJson1 attributes, CtxJson1 options, std::string owner); void unregisterTemplate(std::string subject); private: @@ -51,7 +51,7 @@ namespace trigger { DatabaseManager __dbManager; - std::string __addTemplate(std::string &subject, int &operation, Json &attributes, Json &options, std::string &owner); + std::string __addTemplate(std::string &subject, int &operation, CtxJson1 &attributes, CtxJson1 &options, std::string &owner); std::string __removeTemplate(std::string &subject); }; /* class TemplateManager */ diff --git a/src/agent/legacy/trigger/Trigger.cpp b/src/agent/legacy/trigger/Trigger.cpp index bb3c7aa..762bdea 100644 --- a/src/agent/legacy/trigger/Trigger.cpp +++ b/src/agent/legacy/trigger/Trigger.cpp @@ -129,7 +129,7 @@ void Trigger::__processInitialize(ContextManager* mgr) void Trigger::__addRule(RequestInfo* request) { - Json ruleId; + CtxJson1 ruleId; const char* client = request->getClient(); if (client == NULL) { @@ -152,7 +152,7 @@ void Trigger::__removeRule(RequestInfo* request) const char* pkgId = request->getPackageId(); - Json ruleId = request->getDescription(); + CtxJson1 ruleId = request->getDescription(); ruleId.get(NULL, TRIG_KEY_RULE_ID, &id); error = __ruleMgr->checkRule((pkgId)? pkgId : "", id); @@ -179,7 +179,7 @@ void Trigger::__enableRule(RequestInfo* request) const char* pkgId = request->getPackageId(); - Json ruleId = request->getDescription(); + CtxJson1 ruleId = request->getDescription(); ruleId.get(NULL, TRIG_KEY_RULE_ID, &id); error = __ruleMgr->checkRule((pkgId)? pkgId : "", id); @@ -206,7 +206,7 @@ void Trigger::__disableRule(RequestInfo* request) const char* pkgId = request->getPackageId(); - Json ruleId = request->getDescription(); + CtxJson1 ruleId = request->getDescription(); ruleId.get(NULL, TRIG_KEY_RULE_ID, &id); error = __ruleMgr->checkRule((pkgId)? pkgId : "", id); @@ -230,16 +230,16 @@ void Trigger::__getRuleById(RequestInfo* request) { int error; - Json option = request->getDescription(); + CtxJson1 option = request->getDescription(); int id; option.get(NULL, TRIG_KEY_RULE_ID, &id); const char* pkgId = request->getPackageId(); - Json readData; + CtxJson1 readData; error = __ruleMgr->getRuleById((pkgId)? pkgId : "", id, &readData); - Json dummy; + CtxJson1 dummy; request->reply(error, dummy, readData); } @@ -249,10 +249,10 @@ void Trigger::__getRuleIds(RequestInfo* request) const char* pkgId = request->getPackageId(); - Json readData; + CtxJson1 readData; error = __ruleMgr->getRuleIds((pkgId)? pkgId : "", &readData); - Json dummy; + CtxJson1 dummy; request->reply(error, dummy, readData); } @@ -260,7 +260,7 @@ void Trigger::__getTemplate(RequestInfo* request) { int error; - Json option = request->getDescription(); + CtxJson1 option = request->getDescription(); std::string name; option.get(NULL, TRIG_TMPL_KEY_SUBJECT, &name); @@ -271,9 +271,9 @@ void Trigger::__getTemplate(RequestInfo* request) return; } - Json tmpl; + CtxJson1 tmpl; error = tmplMgr->getTemplate(name, &tmpl); - Json dummy; + CtxJson1 dummy; request->reply(error, dummy, tmpl); } -- 2.7.4 From 63233db6bd6343139a86e21d88bb10a32773262d Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Fri, 9 Jun 2017 18:40:02 +0900 Subject: [PATCH 14/16] Dependency cleanup: add service & client manipulation classes These classes were originally implemented in context-common. Change-Id: Id26d5dd7321de8062ae7126607b66bf0a5119666 Signed-off-by: Mu-Woong Lee --- packaging/context-service.spec | 8 +- src/server/CMakeLists.txt | 22 ++- src/server/Credential.cpp | 158 +++++++++++++++++++++ src/server/Credential.h | 52 +++++++ src/server/MethodCall.cpp | 104 ++++++++++++++ src/server/MethodCall.h | 60 ++++++++ src/server/ServiceClient.cpp | 110 +++++++++++++++ src/server/ServiceClient.h | 67 +++++++++ src/server/ServiceLoader.cpp | 37 ++--- src/server/ServiceLoader.h | 21 ++- src/server/ServiceRunner.cpp | 311 +++++++++++++++++++++++++++++++++++++++++ src/server/ServiceRunner.h | 104 ++++++++++++++ 12 files changed, 1019 insertions(+), 35 deletions(-) create mode 100644 src/server/Credential.cpp create mode 100644 src/server/Credential.h create mode 100644 src/server/MethodCall.cpp create mode 100644 src/server/MethodCall.h create mode 100644 src/server/ServiceClient.cpp create mode 100644 src/server/ServiceClient.h create mode 100644 src/server/ServiceRunner.cpp create mode 100644 src/server/ServiceRunner.h diff --git a/packaging/context-service.spec b/packaging/context-service.spec index e53cb3b..ec68da9 100644 --- a/packaging/context-service.spec +++ b/packaging/context-service.spec @@ -1,6 +1,6 @@ Name: context-service Summary: Tizen Contextual Service Framework -Version: 1.0.1 +Version: 1.0.2 Release: 1 Group: Service/Context License: Apache-2.0 @@ -18,6 +18,9 @@ BuildRequires: pkgconfig(gio-2.0) BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(capi-base-common) BuildRequires: pkgconfig(alarm-service) +BuildRequires: pkgconfig(cynara-creds-gdbus) +BuildRequires: pkgconfig(cynara-client) +BuildRequires: pkgconfig(cynara-session) BuildRequires: pkgconfig(context-common-server) BuildRequires: pkgconfig(context-app-history-server) @@ -32,9 +35,6 @@ BuildRequires: pkgconfig(capi-system-device) BuildRequires: pkgconfig(capi-appfw-app-manager) BuildRequires: pkgconfig(capi-appfw-package-manager) BuildRequires: pkgconfig(notification) -BuildRequires: pkgconfig(cynara-creds-gdbus) -BuildRequires: pkgconfig(cynara-client) -BuildRequires: pkgconfig(cynara-session) BuildRequires: pkgconfig(context-common-legacy) # --- diff --git a/src/server/CMakeLists.txt b/src/server/CMakeLists.txt index 170e43e..4b474cc 100644 --- a/src/server/CMakeLists.txt +++ b/src/server/CMakeLists.txt @@ -2,12 +2,22 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) SET(target "contextd") -SET(DEPS libsystemd-login glib-2.0 gio-2.0 dlog capi-base-common alarm-service) -SET(DEPS ${DEPS} context-common-server) -SET(DEPS ${DEPS} context-app-history-server) -SET(DEPS ${DEPS} context-sensor-recorder-server) -SET(DEPS ${DEPS} context-store-server) -SET(DEPS ${DEPS} context-job-scheduler-server) +SET(DEPS + libsystemd-login + glib-2.0 + gio-2.0 + dlog + capi-base-common + alarm-service + cynara-creds-gdbus + cynara-client + cynara-session + context-common-server + context-app-history-server + context-sensor-recorder-server + context-store-server + context-job-scheduler-server +) SET(INCDIR "${CMAKE_INSTALL_INCLUDEDIR}/context-service") diff --git a/src/server/Credential.cpp b/src/server/Credential.cpp new file mode 100644 index 0000000..5bf2e14 --- /dev/null +++ b/src/server/Credential.cpp @@ -0,0 +1,158 @@ +/* + * 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 +#include +#include +#include +#include +#include "Credential.h" + +#define CACHE_SIZE 100 + +using namespace ctx; + +namespace { + + GMutex __cynaraMutex; + + class PrivilegeChecker { + private: + cynara* __cynara; + + public: + PrivilegeChecker() : + __cynara(NULL) + { + ScopeMutex sm(&__cynaraMutex); + + int err; + cynara_configuration* conf = NULL; + + err = cynara_configuration_create(&conf); + IF_FAIL_VOID_TAG(err == CYNARA_API_SUCCESS, _E, "Cynara configuration creation failed"); + + err = cynara_configuration_set_cache_size(conf, CACHE_SIZE); + if (err != CYNARA_API_SUCCESS) { + _E("Cynara cache size set failed"); + cynara_configuration_destroy(conf); + return; + } + + err = cynara_initialize(&__cynara, conf); + cynara_configuration_destroy(conf); + if (err != CYNARA_API_SUCCESS) { + _E("Cynara initialization failed"); + __cynara = NULL; + return; + } + + _I("Cynara initialized"); + } + + ~PrivilegeChecker() + { + ScopeMutex sm(&__cynaraMutex); + + if (__cynara) + cynara_finish(__cynara); + + _I("Cynara deinitialized"); + } + + bool hasPrivilege(const char* client, const char* session, const char* user, const char* privil) + { + ScopeMutex sm(&__cynaraMutex); + + IF_FAIL_RETURN_TAG(__cynara, false, _E, "Cynara not initialized"); + int ret = cynara_check(__cynara, client, session, user, privil); + return (ret == CYNARA_API_ACCESS_ALLOWED); + } + }; +} + +Credential::Credential(GDBusConnection* conn, const std::string& busName) : + __pid(0), + __uid(ROOT_UID), + __session(NULL), + __user(NULL), + __valid(true) +{ + ScopeMutex sm(&__cynaraMutex); + + if (cynara_creds_gdbus_get_pid(conn, busName.c_str(), &__pid) == CYNARA_API_SUCCESS) + __session = cynara_session_from_pid(__pid); + + char* cid = NULL; + cynara_creds_gdbus_get_client(conn, busName.c_str(), CLIENT_METHOD_DEFAULT, &cid); + cynara_creds_gdbus_get_user(conn, busName.c_str(), USER_METHOD_DEFAULT, &__user); + + _SD("%d, %s, %s, %s", __pid, cid, __session, __user); + + if (!cid || !__session || !__user) { + _E("Peer credentialing failed"); + __valid = false; + } + + if (cid) { + __clientId = cid; + g_free(cid); + } + + if (__user) + __uid = static_cast(std::atoll(__user)); +} + +Credential::~Credential() +{ + g_free(__session); + g_free(__user); +} + +bool Credential::valid() const +{ + return __valid; +} + +bool Credential::hasPrivilege(const char* privil) const +{ + IF_FAIL_RETURN(privil, true); + IF_FAIL_RETURN_TAG(valid(), false, _W, "Unidentified peer"); + + static PrivilegeChecker privilegeChecker; + + _D("Checking '%s' for '%s'", privil, __clientId.c_str()); + if (!privilegeChecker.hasPrivilege(__clientId.c_str(), __session, __user, privil)) { + _W("Privilege denied"); + return false; + } + return true; +} + +uid_t Credential::getUid() const +{ + return __uid; +} + +const std::string& Credential::getClientId() const +{ + return __clientId; +} + +bool Credential::isSystem() const +{ + return util::isSystemUid(__uid); +} diff --git a/src/server/Credential.h b/src/server/Credential.h new file mode 100644 index 0000000..2c00f27 --- /dev/null +++ b/src/server/Credential.h @@ -0,0 +1,52 @@ +/* + * 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_CREDENTIAL_H__ +#define __CONTEXT_CREDENTIAL_H__ + +#include +#include + +#define ROOT_UID 0 + +namespace ctx { + + class Credential { + public: + Credential(GDBusConnection* conn, const std::string& busName); + ~Credential(); + + bool valid() const; + bool hasPrivilege(const char* privil) const; + + uid_t getUid() const; + const std::string& getClientId() const; + bool isSystem() const; + + static bool isSystemUid(uid_t uid); + + private: + pid_t __pid; + uid_t __uid; + std::string __clientId; /* Tizen-Default: Smack label */ + char* __session; + char* __user; /* Tizen-Default: UID */ + bool __valid; + }; + +} + +#endif diff --git a/src/server/MethodCall.cpp b/src/server/MethodCall.cpp new file mode 100644 index 0000000..6d50861 --- /dev/null +++ b/src/server/MethodCall.cpp @@ -0,0 +1,104 @@ +/* + * 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 +#include "MethodCall.h" + +using namespace ctx; + +MethodCall::MethodCall(IClient* caller, const std::string& methodName, GVariant* param, GDBusMethodInvocation* invocation) : + __caller(caller), + __methodName(methodName), + __param(param), + __invocation(invocation) +{ + g_variant_ref(__param); +} + +MethodCall::~MethodCall() +{ + g_variant_unref(__param); + + if (!__invocation) + return; + + _E("Method call '%s' from '%s' is not handled yet", __methodName.c_str(), __caller->getBusName().c_str()); + + g_dbus_method_invocation_return_error_literal(__invocation, CTX_ERROR_DOMAIN, E_FAILED, ""); +} + +const std::string& MethodCall::getMethodName() +{ + return __methodName; +} + +GVariant* MethodCall::getParam() +{ + return __param; +} + +bool MethodCall::reply(GVariant* param) +{ + IF_FAIL_RETURN_TAG(__invocation, false, _E, "Replied already"); + + g_dbus_method_invocation_return_value(__invocation, param); + __invocation = NULL; + + return true; +} + +bool MethodCall::reply(int error) +{ + IF_FAIL_RETURN_TAG(__invocation, false, _D, "Replied already"); + + if (error == E_NONE) + return reply(static_cast(NULL)); + + g_dbus_method_invocation_return_error_literal(__invocation, CTX_ERROR_DOMAIN, error, ""); + __invocation = NULL; + + return true; +} + +void MethodCall::publish(const std::string& signalName, GVariant* param) +{ + getCaller().publish(signalName, param); +} + +bool MethodCall::hasPrivilege(const char* privil) +{ + return getCaller().hasPrivilege(privil); +} + +uid_t MethodCall::getUid() +{ + return getCaller().getUid(); +} + +const std::string& MethodCall::getCallerId() +{ + return getCaller().getId(); +} + +bool MethodCall::isSystem() +{ + return getCaller().isSystem(); +} + +IClient& MethodCall::getCaller() +{ + return *__caller; +} diff --git a/src/server/MethodCall.h b/src/server/MethodCall.h new file mode 100644 index 0000000..86a34f2 --- /dev/null +++ b/src/server/MethodCall.h @@ -0,0 +1,60 @@ +/* + * 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_METHOD_CALL_H__ +#define __CONTEXT_METHOD_CALL_H__ + +#include +#include +#include +#include + +namespace ctx { + + class MethodCall : public IMethodCall { + public: + MethodCall(IClient* caller, const std::string& methodName, GVariant* param, GDBusMethodInvocation* invocation); + ~MethodCall(); + + const std::string& getMethodName(); + + GVariant* getParam(); + + bool reply(GVariant* param); + bool reply(int error); + + void publish(const std::string& signalName, GVariant* param); + + bool hasPrivilege(const char* privil); + + uid_t getUid(); + + const std::string& getCallerId(); + + bool isSystem(); + + IClient& getCaller(); + + private: + IClient* __caller; + std::string __methodName; + GVariant* __param; + GDBusMethodInvocation* __invocation; + }; + +} + +#endif /* __CONTEXT_METHOD_CALL_H__ */ diff --git a/src/server/ServiceClient.cpp b/src/server/ServiceClient.cpp new file mode 100644 index 0000000..28ebcf9 --- /dev/null +++ b/src/server/ServiceClient.cpp @@ -0,0 +1,110 @@ +/* + * 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 "Credential.h" +#include "ServiceRunner.h" +#include "ServiceClient.h" + +using namespace ctx; + +ServiceClient::ServiceClient(ServiceRunner* runner, IMethodCallHandler* handler, const std::string& busName) : + __serviceRunner(runner), + __methodCallHandler(handler), + __busName(busName), + __credential(NULL) +{ +} + +ServiceClient::~ServiceClient() +{ + delete __credential; + delete __methodCallHandler; +} + +const std::string& ServiceClient::getBusName() +{ + return __busName; +} + +const std::string& ServiceClient::getId() +{ + return __credential->getClientId(); +} + +uid_t ServiceClient::getUid() +{ + return __credential->getUid(); +} + +bool ServiceClient::isSystem() +{ + return __credential->isSystem(); +} + +bool ServiceClient::isVerified() +{ + IF_FAIL_RETURN(__getCredential(), false); + return __credential->valid(); +} + +bool ServiceClient::hasPrivilege(const char* privil) +{ + return __credential->hasPrivilege(privil); +} + +bool ServiceClient::hasPrivileges(const std::vector& privil) +{ + for (auto& item : privil) { + if (!hasPrivilege(item.c_str())) + return false; + } + return true; +} + +void ServiceClient::publish(const std::string& signalName, GVariant* param) +{ + __serviceRunner->publish(__busName, signalName, param); +} + +IService* ServiceClient::getHostService() +{ + return __serviceRunner->getService(); +} + +ServiceRunner* ServiceClient::getHostServiceRunner() +{ + return __serviceRunner; +} + +void ServiceClient::onMethodCalled(IMethodCall* methodCall) +{ + __methodCallHandler->onMethodCalled(methodCall); +} + +void ServiceClient::onDisconnected() +{ + __methodCallHandler->onDisconnected(); +} + +bool ServiceClient::__getCredential() +{ + if (__credential) + return true; + + __credential = new Credential(__serviceRunner->getConnection(), __busName); + + return true; +} diff --git a/src/server/ServiceClient.h b/src/server/ServiceClient.h new file mode 100644 index 0000000..820cfcb --- /dev/null +++ b/src/server/ServiceClient.h @@ -0,0 +1,67 @@ +/* + * 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_CLIENT_H__ +#define __CONTEXT_SERVICE_CLIENT_H__ + +#include +#include +#include +#include +#include +#include + +namespace ctx { + + class ServiceRunner; + class Credential; + + class ServiceClient : public IClient { + public: + ServiceClient(ServiceRunner* runner, IMethodCallHandler* callHandler, const std::string& busName); + ~ServiceClient(); + + const std::string& getBusName(); + const std::string& getId(); + + uid_t getUid(); + bool isSystem(); + + bool isVerified(); + + bool hasPrivilege(const char* privil); + bool hasPrivileges(const std::vector& privil); + + void publish(const std::string& signalName, GVariant* param); + + IService* getHostService(); + ServiceRunner* getHostServiceRunner(); + + void onMethodCalled(IMethodCall* methodCall); + void onDisconnected(); + + private: + bool __getCredential(); + + ServiceRunner* __serviceRunner; + IMethodCallHandler* __methodCallHandler; + std::string __busName; + Credential* __credential; + }; + +} + +#endif /* __CONTEXT_SERVICE_CLIENT_H__ */ diff --git a/src/server/ServiceLoader.cpp b/src/server/ServiceLoader.cpp index 19ca7a2..90f8ef1 100644 --- a/src/server/ServiceLoader.cpp +++ b/src/server/ServiceLoader.cpp @@ -14,6 +14,7 @@ * limitations under the License. */ +#include #include #include #include @@ -32,11 +33,11 @@ ServiceLoader::ServiceLoader() : ServiceLoader::~ServiceLoader() { - for (auto& svc : __userServices) { - delete svc; + for (auto& runner : __userServices) { + delete runner; } - for (auto& svc : __systemServices) { - delete svc; + for (auto& runner : __systemServices) { + delete runner; } } @@ -52,15 +53,15 @@ bool ServiceLoader::load(GDBusConnection* conn) void ServiceLoader::startSystem() { - for (auto& svc : __systemServices) { - svc->start(); + for (auto& runner : __systemServices) { + runner->start(); } } void ServiceLoader::stopSystem() { - for (auto& svc : __systemServices) { - svc->stop(); + for (auto& runner : __systemServices) { + runner->stop(); } } @@ -69,14 +70,14 @@ void ServiceLoader::startUser(uid_t uid) IF_FAIL_VOID(__activeUser != uid); _I("Starting services for %u", static_cast(uid)); - ServiceBase::setActiveUser(uid); + util::setActiveUid(uid); - for (auto& svc : __userServices) { - svc->start(); + for (auto& runner : __userServices) { + runner->start(); } - for (auto& svc : __systemServices) { - svc->notifyUserNew(); + for (auto& runner : __systemServices) { + runner->notifyUserNew(); } __activeUser = uid; @@ -87,14 +88,14 @@ void ServiceLoader::stopUser() IF_FAIL_VOID(__activeUser != ROOT_UID); _I("Stopping services for %u", static_cast(__activeUser)); - for (auto& svc : __userServices) { - svc->stop(); + for (auto& runner : __userServices) { + runner->stop(); } - ServiceBase::setActiveUser(ROOT_UID); + util::setActiveUid(ROOT_UID); - for (auto& svc : __systemServices) { - svc->notifyUserRemoved(); + for (auto& runner : __systemServices) { + runner->notifyUserRemoved(); } __activeUser = ROOT_UID; diff --git a/src/server/ServiceLoader.h b/src/server/ServiceLoader.h index 7492f14..ef85039 100644 --- a/src/server/ServiceLoader.h +++ b/src/server/ServiceLoader.h @@ -19,7 +19,9 @@ #include #include -#include +#include +#include "ServiceRunner.h" +#include "ServiceClient.h" namespace ctx { @@ -37,22 +39,27 @@ namespace ctx { private: uid_t __activeUser; - std::vector __userServices; - std::vector __systemServices; + std::vector __userServices; + std::vector __systemServices; template void __create(GDBusConnection* conn) { - ServiceBase *svc = NULL; + IService* svc = NULL; + try { - svc = new ServiceType(conn); + svc = new ServiceType(); } catch (const std::runtime_error& e) { _I(YELLOW("%s"), e.what()); return; } + + ServiceRunner* runner = new ServiceRunner(conn, svc); + svc->setServiceRunner(runner); + if (svc->isUserService()) { - __userServices.push_back(svc); + __userServices.push_back(runner); } else { - __systemServices.push_back(svc); + __systemServices.push_back(runner); } } }; diff --git a/src/server/ServiceRunner.cpp b/src/server/ServiceRunner.cpp new file mode 100644 index 0000000..da72446 --- /dev/null +++ b/src/server/ServiceRunner.cpp @@ -0,0 +1,311 @@ +/* + * 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 +#include "MethodCall.h" +#include "ServiceClient.h" +#include "ServiceRunner.h" + +using namespace ctx; + +ServiceRunner::ServiceRunner(GDBusConnection* conn, IService* service) : + __service(service), + __started(false), + __threadRunning(false), + __mainContext(NULL), + __mainLoop(NULL), + __gthread(NULL), + __connection(conn), + __objPath(CTX_DBUS_PATH), + __interface(CTX_DBUS_IFACE), + __nodeInfo(NULL), + __registrationId(0) +{ + __objPath += __service->getServiceName(); + __interface += __service->getServiceName(); +} + +ServiceRunner::~ServiceRunner() +{ +} + +GMainContext* ServiceRunner::getMainContext() +{ + return __mainContext; +} + +GDBusConnection* ServiceRunner::getConnection() +{ + return __connection; +} + +IService* ServiceRunner::getService() +{ + return __service; +} + +bool ServiceRunner::start() +{ + IF_FAIL_RETURN(!__started, true); + + _I("Preparing '%s'", __service->getServiceName()); + + __gthread = g_thread_new(__service->getServiceName(), __threadFunc, this); + IF_FAIL_RETURN_TAG(__gthread, false, _E, "Thread creation failed"); + + __started = true; + return true; +} + +void ServiceRunner::stop() +{ + IF_FAIL_VOID(__started); + __started = false; + + IF_FAIL_VOID(__threadRunning.load()); + + GSource *gSrc = g_idle_source_new(); + if (gSrc) { + // Tries to stop the main loop within its thread. + // In this way, already scheduled idle tasks are not discarded. + g_source_set_callback(gSrc, __stopMainLoop, this, NULL); + g_source_attach(gSrc, __mainContext); + g_source_unref(gSrc); + } else { + __stopMainLoop(this); + } + + _I("Joining the thread of '%s'", __service->getServiceName()); + g_thread_join(__gthread); + __gthread = NULL; +} + +gboolean ServiceRunner::__stopMainLoop(gpointer data) +{ + ServiceRunner* runner = static_cast(data); + _I(PURPLE("Stopping '%s'"), runner->__service->getServiceName()); + g_main_loop_quit(runner->__mainLoop); + return G_SOURCE_REMOVE; +} + +void ServiceRunner::publish(const std::string& busName, const std::string& signalName, GVariant* param) +{ + GError* gerr = NULL; + g_dbus_connection_emit_signal(__connection, + busName.c_str(), __objPath.c_str(), __interface.c_str(), + signalName.c_str(), param, &gerr); + HANDLE_GERROR(gerr); +} + +void ServiceRunner::notifyUserNew() +{ + IF_FAIL_VOID(__started); + IF_FAIL_VOID(__threadRunning.load()); + + GSource* gSrc = g_idle_source_new(); + IF_FAIL_VOID_TAG(gSrc, _E, E_STR_ALLOC); + + g_source_set_callback(gSrc, __onUserActivated, __service, NULL); + g_source_attach(gSrc, __mainContext); + g_source_unref(gSrc); +} + +void ServiceRunner::notifyUserRemoved() +{ + IF_FAIL_VOID(__started); + IF_FAIL_VOID(__threadRunning.load()); + + GSource* gSrc = g_idle_source_new(); + IF_FAIL_VOID_TAG(gSrc, _E, E_STR_ALLOC); + + g_source_set_callback(gSrc, __onUserDeactivated, __service, NULL); + g_source_attach(gSrc, __mainContext); + g_source_unref(gSrc); +} + +gboolean ServiceRunner::__onUserActivated(gpointer data) +{ + IService* svc = static_cast(data); + svc->onUserActivated(); + return G_SOURCE_REMOVE; +} + +gboolean ServiceRunner::__onUserDeactivated(gpointer data) +{ + IService* svc = static_cast(data); + svc->onUserDeactivated(); + return G_SOURCE_REMOVE; +} + +gpointer ServiceRunner::__threadFunc(gpointer data) +{ + ServiceRunner* runner = static_cast(data); + runner->__run(); + return NULL; +} + +void ServiceRunner::__run() +{ + if (!__init()) { + _E("Starting '%s' failed", __service->getServiceName()); + __release(); + return; + } + + __threadRunning.store(true); + + _I(CYAN("Starting '%s'"), __service->getServiceName()); + g_main_loop_run(__mainLoop); + + __threadRunning.store(false); + + __release(); +} + +bool ServiceRunner::__init() +{ + GError* gerr = NULL; + GDBusInterfaceVTable vtable; + + vtable.method_call = __onMethodCalled; + vtable.get_property = NULL; + vtable.set_property = NULL; + + __mainContext = g_main_context_new(); + IF_FAIL_RETURN_TAG(__mainContext, false, _E, "MainContext creation failed"); + + g_main_context_push_thread_default(__mainContext); + + __mainLoop = g_main_loop_new(__mainContext, FALSE); + IF_FAIL_RETURN_TAG(__mainLoop, false, _E, "MainLoop creation failed"); + + std::string introspection("" + __service->getMethodSpecs() + ""; + + __nodeInfo = g_dbus_node_info_new_for_xml(introspection.c_str(), NULL); + IF_FAIL_RETURN_TAG(__nodeInfo, false, _E, "NodeInfo creation failed"); + + __registrationId = g_dbus_connection_register_object(__connection, + __objPath.c_str(), __nodeInfo->interfaces[0], &vtable, this, NULL, &gerr); + HANDLE_GERROR(gerr); + IF_FAIL_RETURN_TAG(__registrationId > 0, false, _E, "Object registration failed"); + + return __service->prepare(); +} + +void ServiceRunner::__release() +{ + _D("Releasing '%s'", __service->getServiceName()); + + for (auto iter = __clients.begin(); iter != __clients.end(); ++iter) { + iter->second.client->onDisconnected(); + delete iter->second.client; + } + + __clients.clear(); + + __service->cleanup(); + + if (__registrationId > 0) + g_dbus_connection_unregister_object(__connection, __registrationId); + + if (__nodeInfo) + g_dbus_node_info_unref(__nodeInfo); + + if (__mainLoop) + g_main_loop_unref(__mainLoop); + + if (__mainContext) + g_main_context_unref(__mainContext); +} + +void ServiceRunner::__onMethodCalled(GDBusConnection* conn, const gchar* sender, + const gchar* path, const gchar* iface, const gchar* name, + GVariant* param, GDBusMethodInvocation* invocation, gpointer userData) +{ + ServiceRunner* runner = static_cast(userData); + runner->__onMethodCalled(sender, name, param, invocation); +} + +void ServiceRunner::__onMethodCalled(const std::string& sender, + const std::string& name, GVariant* param, GDBusMethodInvocation* invocation) +{ + _I("'%s' called '%s.%s'", sender.c_str(), __service->getServiceName(), name.c_str()); + + ServiceClient* client = __getClient(sender); + IF_FAIL_VOID(client); + + client->onMethodCalled(new MethodCall(client, name, param, invocation)); +} + +ServiceClient* ServiceRunner::__getClient(const std::string& busName) +{ + auto iter = __clients.find(busName); + + if (iter != __clients.end()) + return iter->second.client; + + IMethodCallHandler* callHandler = __service->createMethodCallHandler(); + IF_FAIL_RETURN(callHandler, NULL); + + ServiceClient* client = new ServiceClient(this, callHandler, busName); + if (!client->isVerified()) { + delete client; + return NULL; + } + + callHandler->setCaller(client); + + __ClientInfo info = {client, __watch(busName, client)}; + __clients[busName] = info; + + return client; +} + +void ServiceRunner::__onNameOwnerChanged(GDBusConnection* conn, const gchar* sender, + const gchar* path, const gchar* iface, const gchar* name, + GVariant* param, gpointer userData) +{ + ServiceClient* client = static_cast(userData); + client->getHostServiceRunner()->__removeClient(client->getBusName()); +} + +void ServiceRunner::__removeClient(const std::string& busName) +{ + _I("'%s' lost '%s'", __service->getServiceName(), busName.c_str()); + + auto iter = __clients.find(busName); + IF_FAIL_VOID(iter != __clients.end()); + + __ClientInfo info = iter->second; + __clients.erase(iter); + + __unwatch(info.watchId); + info.client->onDisconnected(); + delete info.client; +} + +unsigned int ServiceRunner::__watch(const std::string& busName, ServiceClient* client) +{ + return g_dbus_connection_signal_subscribe(__connection, + "org.freedesktop.DBus", "org.freedesktop.DBus", "NameOwnerChanged", "/org/freedesktop/DBus", + busName.c_str(), G_DBUS_SIGNAL_FLAGS_NONE, __onNameOwnerChanged, client, NULL); +} + +void ServiceRunner::__unwatch(unsigned int watchId) +{ + g_dbus_connection_signal_unsubscribe(__connection, watchId); +} diff --git a/src/server/ServiceRunner.h b/src/server/ServiceRunner.h new file mode 100644 index 0000000..967c13a --- /dev/null +++ b/src/server/ServiceRunner.h @@ -0,0 +1,104 @@ +/* + * 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_RUNNER_H__ +#define __CONTEXT_SERVICE_RUNNER_H__ + +#include +#include +#include +#include +#include +#include + +namespace ctx { + + class ServiceClient; + + class ServiceRunner : public IServiceRunner { + public: + ServiceRunner(GDBusConnection* conn, IService* service); + ~ServiceRunner(); + + void setService(IService* service); + + bool start(); + void stop(); + + void notifyUserNew(); + void notifyUserRemoved(); + + void publish(const std::string& busName, const std::string& signalName, GVariant* param); + + GMainContext* getMainContext(); + GDBusConnection* getConnection(); + IService* getService(); + + private: + static gpointer __threadFunc(gpointer data); + + static void __onMethodCalled(GDBusConnection* conn, const gchar* sender, + const gchar* path, const gchar* iface, const gchar* name, + GVariant* param, GDBusMethodInvocation* invocation, gpointer userData); + + static void __onNameOwnerChanged(GDBusConnection* conn, const gchar* sender, + const gchar* path, const gchar* iface, const gchar* name, + GVariant* param, gpointer userData); + + static gboolean __onUserActivated(gpointer data); + + static gboolean __onUserDeactivated(gpointer data); + + static gboolean __stopMainLoop(gpointer data); + + void __onMethodCalled(const std::string& sender, + const std::string& name, GVariant* param, GDBusMethodInvocation* invocation); + + void __run(); + bool __init(); + void __release(); + + ServiceClient* __getClient(const std::string& busName); + void __removeClient(const std::string& busName); + unsigned int __watch(const std::string& busName, ServiceClient* client); + void __unwatch(unsigned int watchId); + + IService* __service; + bool __started; + std::atomic_bool __threadRunning; + + GMainContext* __mainContext; + GMainLoop* __mainLoop; + GThread* __gthread; + + GDBusConnection* __connection; + std::string __objPath; + std::string __interface; + + GDBusNodeInfo* __nodeInfo; + guint __registrationId; + + struct __ClientInfo { + ServiceClient* client; + unsigned int watchId; + }; + + std::map __clients; + }; + +} + +#endif /* __CONTEXT_SERVICE_RUNNER_H__ */ -- 2.7.4 From f4e22d7ca2cb978f30e7926f615a987d1debc902 Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Mon, 12 Jun 2017 16:12:45 +0900 Subject: [PATCH 15/16] Remove unused function IService::setService() Change-Id: I56dd57f5a6b1be7fd2e92e21fccfcbe495df11d5 Signed-off-by: Mu-Woong Lee --- src/server/ServiceRunner.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/server/ServiceRunner.h b/src/server/ServiceRunner.h index 967c13a..b344337 100644 --- a/src/server/ServiceRunner.h +++ b/src/server/ServiceRunner.h @@ -33,8 +33,6 @@ namespace ctx { ServiceRunner(GDBusConnection* conn, IService* service); ~ServiceRunner(); - void setService(IService* service); - bool start(); void stop(); -- 2.7.4 From 4f469a3fc76add2772875e2e2c73938c8ba22085 Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Fri, 16 Jun 2017 16:04:21 +0900 Subject: [PATCH 16/16] Enable more compile warnings: -Wall & -Wformat=2 Change-Id: I1cc553717234ab3bb1b0a33f3520dbf8c9ba8019 Signed-off-by: Mu-Woong Lee --- packaging/context-service.spec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packaging/context-service.spec b/packaging/context-service.spec index ec68da9..b124152 100644 --- a/packaging/context-service.spec +++ b/packaging/context-service.spec @@ -53,8 +53,8 @@ This is a systemd service that runs in the user session to assist the main servi %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+=" -Wall -Wextra -Wcast-align -Wshadow -Wwrite-strings -Wswitch-default -Wno-unused-parameter" +export CXXFLAGS+=" -Wformat=2 -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" -- 2.7.4