SET(DEPS
vconf
capi-system-info
+ capi-system-device
+ capi-system-runtime-info
capi-appfw-app-manager
+ capi-appfw-package-manager
appsvc
notification
capi-system-system-settings
BuildRequires: sed
BuildRequires: pkgconfig(vconf)
BuildRequires: pkgconfig(capi-system-info)
+BuildRequires: pkgconfig(capi-system-device)
+BuildRequires: pkgconfig(capi-system-runtime-info)
BuildRequires: pkgconfig(capi-appfw-app-manager)
+BuildRequires: pkgconfig(capi-appfw-package-manager)
BuildRequires: pkgconfig(appsvc)
BuildRequires: pkgconfig(notification)
BuildRequires: pkgconfig(capi-system-system-settings)
--- /dev/null
+/*
+ * Copyright (c) 2015 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 <glib.h>
+#include <list>
+
+#include <Types.h>
+#include <DBusTypes.h>
+#include <context_trigger_types_internal.h>
+#include <Json.h>
+#include <ContextProvider.h>
+#include "Server.h"
+#include "access_control/Privilege.h"
+#include "Request.h"
+#include "ProviderHandler.h"
+#include "ContextManager.h"
+#include "trigger/TemplateManager.h"
+
+/* Context Providers */
+#include <internal/DeviceContextProvider.h>
+#include <internal/StatisticsContextProvider.h>
+#include <internal/PlaceContextProvider.h>
+#include <internal/CustomContextProvider.h>
+
+using namespace ctx;
+
+struct TriggerItemFormat {
+ std::string subject;
+ int operation;
+ Json attributes;
+ Json options;
+ std::string owner;
+ bool unregister;
+ TriggerItemFormat(std::string subj, int ops, Json attr, Json opt, std::string own) :
+ subject(subj),
+ operation(ops),
+ attributes(attr),
+ options(opt),
+ owner(own)
+ {
+ unregister = false;
+ }
+
+ TriggerItemFormat(std::string subj) :
+ subject(subj)
+ {
+ unregister = true;
+ }
+};
+
+static std::list<TriggerItemFormat> __triggerItemList;
+
+ContextManager::ContextManager() :
+ __initialized(false)
+{
+ ContextProvider::__setContextManager(this);
+}
+
+ContextManager::~ContextManager()
+{
+ release();
+}
+
+bool ContextManager::init()
+{
+ bool ret;
+
+ ret = initDeviceContextProvider();
+ IF_FAIL_RETURN_TAG(ret, false, _E, "Initialization failed: DeviceContextProvider");
+
+ ret = initStatisticsContextProvider();
+ IF_FAIL_RETURN_TAG(ret, false, _E, "Initialization failed: StatisticsContextProvider");
+
+ ret = initPlaceContextProvider();
+ IF_FAIL_RETURN_TAG(ret, false, _E, "Initialization failed: PlaceContextProvider");
+
+// ret = initCustomContextProvider();
+// IF_FAIL_RETURN_TAG(ret, false, _E, "Initialization failed: CustomContextProvider");
+
+ __initialized = true;
+ return true;
+}
+
+void ContextManager::release()
+{
+ for (auto& it : __providerHandleMap) {
+ delete it.second;
+ }
+ __providerHandleMap.clear();
+}
+
+bool ContextManager::registerProvider(const char *subject, const char *privilege, ContextProvider *provider)
+{
+ if (__providerHandleMap.find(subject) != __providerHandleMap.end()) {
+ _E("The provider for the subject '%s' is already registered.", subject);
+ return false;
+ }
+
+ _SI("Subj: %s, Priv: %s", subject, privilege);
+
+ ProviderHandler *handle = new(std::nothrow) ProviderHandler(subject, privilege, provider);
+ IF_FAIL_RETURN_TAG(handle, false, _E, "Memory allocation failed");
+
+ __providerHandleMap[subject] = handle;
+ return true;
+}
+
+bool ContextManager::unregisterProvider(const char *subject)
+{
+ auto it = __providerHandleMap.find(subject);
+ if (it == __providerHandleMap.end()) {
+ _E("The provider for the subject '%s' is not found.", subject);
+ return false;
+ }
+
+ delete it->second;
+ __providerHandleMap.erase(it);
+
+ return true;
+}
+
+bool ContextManager::registerTriggerItem(const char *subject, int operation, Json attributes, Json options, const char* owner)
+{
+ IF_FAIL_RETURN_TAG(subject, false, _E, "Invalid parameter");
+
+ if (!__initialized) {
+ __triggerItemList.push_back(TriggerItemFormat(subject, operation, attributes, options, (owner)? owner : ""));
+ } else {
+ trigger::TemplateManager* tmplMgr = trigger::TemplateManager::getInstance();
+ IF_FAIL_RETURN_TAG(tmplMgr, false, _E, "Memory allocation failed");
+ tmplMgr->registerTemplate(subject, operation, attributes, options, owner);
+ }
+
+ return true;
+}
+
+bool ContextManager::unregisterTriggerItem(const char *subject)
+{
+ IF_FAIL_RETURN_TAG(subject, false, _E, "Invalid parameter");
+
+ if (!__initialized) {
+ __triggerItemList.push_back(TriggerItemFormat(subject));
+ } else {
+ trigger::TemplateManager* tmplMgr = trigger::TemplateManager::getInstance();
+ IF_FAIL_RETURN_TAG(tmplMgr, false, _E, "Memory allocation failed");
+ tmplMgr->unregisterTemplate(subject);
+ }
+
+ return true;
+}
+
+bool ContextManager::popTriggerItem(std::string &subject, int &operation, Json &attributes, Json &options, std::string& owner, bool& unregister)
+{
+ IF_FAIL_RETURN(!__triggerItemList.empty(), false);
+
+ TriggerItemFormat format = __triggerItemList.front();
+ __triggerItemList.pop_front();
+
+ subject = format.subject;
+ operation = format.operation;
+ attributes = format.attributes;
+ options = format.options;
+ owner = format.owner;
+ unregister = format.unregister;
+
+ return true;
+}
+
+void ContextManager::assignRequest(RequestInfo* request)
+{
+ if (__handleCustomRequest(request)) {
+ delete request;
+ return;
+ }
+
+ auto it = __providerHandleMap.find(request->getSubject());
+ if (it == __providerHandleMap.end()) {
+ _W("Unsupported subject");
+ request->reply(ERR_NOT_SUPPORTED);
+ delete request;
+ return;
+ }
+
+ if (!it->second->isAllowed(request->getCredentials())) {
+ _W("Permission denied");
+ request->reply(ERR_PERMISSION_DENIED);
+ delete request;
+ return;
+ }
+
+ switch (request->getType()) {
+ case REQ_SUBSCRIBE:
+ it->second->subscribe(request);
+ break;
+ case REQ_UNSUBSCRIBE:
+ it->second->unsubscribe(request);
+ break;
+ case REQ_READ:
+ case REQ_READ_SYNC:
+ it->second->read(request);
+ break;
+ case REQ_WRITE:
+ it->second->write(request);
+ break;
+ case REQ_SUPPORT:
+ request->reply(ERR_NONE);
+ delete request;
+ break;
+ default:
+ _E("Invalid type of request");
+ delete request;
+ }
+}
+
+bool ContextManager::isSupported(const char *subject)
+{
+ auto it = __providerHandleMap.find(subject);
+ return (it != __providerHandleMap.end());
+}
+
+bool ContextManager::isAllowed(const Credentials *creds, const char *subject)
+{
+ IF_FAIL_RETURN(creds, true); /* In case internal requests */
+ auto it = __providerHandleMap.find(subject);
+ IF_FAIL_RETURN(it != __providerHandleMap.end(), false);
+ return it->second->isAllowed(creds);
+}
+
+void ContextManager::__publish(const char* subject, Json &option, int error, Json &dataUpdated)
+{
+ _I("Publishing '%s'", subject);
+ _J("Option", option);
+
+ auto it = __providerHandleMap.find(subject);
+ IF_FAIL_VOID(it != __providerHandleMap.end());
+
+ it->second->publish(option, error, dataUpdated);
+}
+
+void ContextManager::__replyToRead(const char* subject, Json &option, int error, Json &dataRead)
+{
+ _I("Sending data of '%s'", subject);
+ _J("Option", option);
+ _J("Data", dataRead);
+
+ auto it = __providerHandleMap.find(subject);
+ IF_FAIL_VOID(it != __providerHandleMap.end());
+
+ it->second->replyToRead(option, error, dataRead);
+}
+
+struct PublishedData {
+ int type;
+ 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)
+ : type(t), mgr(m), subject(s), error(e)
+ {
+ option = o.str();
+ data = d.str();
+ }
+};
+
+gboolean ContextManager::__threadSwitcher(gpointer data)
+{
+ PublishedData *tuple = static_cast<PublishedData*>(data);
+
+ switch (tuple->type) {
+ case REQ_SUBSCRIBE:
+ tuple->mgr->__publish(tuple->subject.c_str(), tuple->option, tuple->error, tuple->data);
+ break;
+ case REQ_READ:
+ tuple->mgr->__replyToRead(tuple->subject.c_str(), tuple->option, tuple->error, tuple->data);
+ break;
+ default:
+ _W("Invalid type");
+ }
+
+ delete tuple;
+ return FALSE;
+}
+
+bool ContextManager::publish(const char* subject, Json& option, int error, Json& dataUpdated)
+{
+ IF_FAIL_RETURN_TAG(subject, false, _E, "Invalid parameter");
+
+ PublishedData *tuple = new(std::nothrow) PublishedData(REQ_SUBSCRIBE, this, subject, option, error, dataUpdated);
+ IF_FAIL_RETURN_TAG(tuple, false, _E, "Memory allocation failed");
+
+ g_idle_add(__threadSwitcher, tuple);
+
+ return true;
+}
+
+bool ContextManager::replyToRead(const char* subject, Json& option, int error, Json& dataRead)
+{
+ IF_FAIL_RETURN_TAG(subject, false, _E, "Invalid parameter");
+
+ PublishedData *tuple = new(std::nothrow) PublishedData(REQ_READ, this, subject, option, error, dataRead);
+ IF_FAIL_RETURN_TAG(tuple, false, _E, "Memory allocation failed");
+
+ g_idle_add(__threadSwitcher, tuple);
+
+ return true;
+}
+
+bool ContextManager::__handleCustomRequest(RequestInfo* request)
+{
+ std::string subject = request->getSubject();
+ IF_FAIL_RETURN( subject == CONTEXT_TRIGGER_SUBJECT_CUSTOM_ADD ||
+ subject == CONTEXT_TRIGGER_SUBJECT_CUSTOM_REMOVE ||
+ subject == CONTEXT_TRIGGER_SUBJECT_CUSTOM_PUBLISH, false);
+
+#if 0
+ const char* pkg_id = request->getPackageId();
+ if (pkg_id == NULL) {
+ request->reply(ERR_OPERATION_FAILED);
+ return true;
+ }
+
+ Json desc = request->getDescription();
+ std::string name;
+ desc.get(NULL, CT_CUSTOM_NAME, &name);
+ std::string subj = pkg_id + std::string("::") + name;
+
+ int error = ERR_NONE;
+ if (subject == CONTEXT_TRIGGER_SUBJECT_CUSTOM_ADD) {
+ Json tmpl;
+ desc.get(NULL, CT_CUSTOM_ATTRIBUTES, &tmpl);
+
+ error = custom_context_provider::addItem(subj, name, tmpl, pkg_id);
+ } else if (subject == CONTEXT_TRIGGER_SUBJECT_CUSTOM_REMOVE) {
+ error = custom_context_provider::removeItem(subj);
+ if (error == ERR_NONE) {
+ Json data;
+ data.set(NULL, CT_CUSTOM_SUBJECT, subj);
+ request->reply(error, data);
+ }
+ } else if (subject == CONTEXT_TRIGGER_SUBJECT_CUSTOM_PUBLISH) {
+ Json fact;
+ desc.get(NULL, CT_CUSTOM_FACT, &fact);
+
+ error = custom_context_provider::publishData(subj, fact);
+ }
+
+ request->reply(error);
+#endif
+ return true;
+}
--- /dev/null
+/*
+ * Copyright (c) 2015 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_CONTEXT_MANAGER_H_
+#define _CONTEXT_CONTEXT_MANAGER_H_
+
+#include <map>
+#include <IContextManager.h>
+
+namespace ctx {
+
+ /* Forward declaration */
+ class Credentials;
+ class RequestInfo;
+ class ProviderHandler;
+
+ class ContextManager : public IContextManager {
+ public:
+ ~ContextManager();
+
+ bool init();
+ void release();
+
+ void assignRequest(ctx::RequestInfo *request);
+ bool isSupported(const char *subject);
+ bool isAllowed(const Credentials *creds, const char *subject);
+ bool popTriggerItem(std::string &subject, int &operation, ctx::Json &attributes, ctx::Json &options, std::string &owner, bool& unregister);
+
+ /* From the interface class */
+ bool registerProvider(const char *subject, const char *privilege, ContextProvider *provider);
+ bool unregisterProvider(const char *subject);
+ bool registerTriggerItem(const char *subject, int operation, ctx::Json attributes, ctx::Json options, const char *owner = NULL);
+ bool unregisterTriggerItem(const char *subject);
+ 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);
+
+ 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);
+
+ /* For custom request */
+ bool __handleCustomRequest(ctx::RequestInfo* request);
+
+ bool __initialized;
+ std::map<std::string, ProviderHandler*> __providerHandleMap;
+
+ friend class Server;
+
+ }; /* class ContextManager */
+
+} /* namespace ctx */
+
+#endif /* _CONTEXT_CONTEXT_MANAGER_H_ */
+++ /dev/null
-/*
- * Copyright (c) 2015 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 <glib.h>
-#include <list>
-
-#include <Types.h>
-#include <DBusTypes.h>
-#include <context_trigger_types_internal.h>
-#include <Json.h>
-#include "Server.h"
-#include "access_control/Privilege.h"
-#include "Request.h"
-#include "ProviderHandler.h"
-#include "ContextManagerImpl.h"
-#include "trigger/TemplateManager.h"
-
-/* Context Providers */
-#include <internal/DeviceContextProvider.h>
-#include <internal/StatisticsContextProvider.h>
-#include <internal/PlaceContextProvider.h>
-#include <internal/CustomContextProvider.h>
-
-struct TriggerItemFormat {
- std::string subject;
- int operation;
- ctx::Json attributes;
- ctx::Json options;
- std::string owner;
- bool unregister;
- TriggerItemFormat(std::string subj, int ops, ctx::Json attr, ctx::Json opt, std::string own) :
- subject(subj),
- operation(ops),
- attributes(attr),
- options(opt),
- owner(own)
- {
- unregister = false;
- }
-
- TriggerItemFormat(std::string subj) :
- subject(subj)
- {
- unregister = true;
- }
-};
-
-static std::list<TriggerItemFormat> __triggerItemList;
-bool ctx::ContextManagerImpl::__initialized = false;
-
-ctx::ContextManagerImpl::ContextManagerImpl()
-{
-}
-
-ctx::ContextManagerImpl::~ContextManagerImpl()
-{
- release();
-}
-
-bool ctx::ContextManagerImpl::init()
-{
- bool ret;
-
- ret = initDeviceContextProvider();
- IF_FAIL_RETURN_TAG(ret, false, _E, "Initialization failed: DeviceContextProvider");
-
- ret = initStatisticsContextProvider();
- IF_FAIL_RETURN_TAG(ret, false, _E, "Initialization failed: StatisticsContextProvider");
-
- ret = initPlaceContextProvider();
- IF_FAIL_RETURN_TAG(ret, false, _E, "Initialization failed: PlaceContextProvider");
-
- ret = initCustomContextProvider();
- IF_FAIL_RETURN_TAG(ret, false, _E, "Initialization failed: CustomContextProvider");
-
- __initialized = true;
-
- return true;
-}
-
-void ctx::ContextManagerImpl::release()
-{
- for (auto it = __providerHandleMap.begin(); it != __providerHandleMap.end(); ++it) {
- delete it->second;
- }
- __providerHandleMap.clear();
-}
-
-bool ctx::ContextManagerImpl::registerProvider(const char *subject, ctx::ContextProviderInfo &providerInfo)
-{
- if (__providerHandleMap.find(subject) != __providerHandleMap.end()) {
- _E("The provider for the subject '%s' is already registered.", subject);
- return false;
- }
-
- _SI("Subj: %s, Priv: %s", subject, providerInfo.privilege);
- __providerHandleMap[subject] = NULL;
-
- auto it = __providerHandleMap.find(subject);
- ProviderHandler *handle = new(std::nothrow) ProviderHandler(it->first.c_str(), providerInfo);
-
- if (!handle) {
- _E("Memory allocation failed");
- __providerHandleMap.erase(it);
- return false;
- }
-
- it->second = handle;
- return true;
-}
-
-bool ctx::ContextManagerImpl::unregisterProvider(const char *subject)
-{
- auto it = __providerHandleMap.find(subject);
- if (it == __providerHandleMap.end()) {
- _E("The provider for the subject '%s' is not found.", subject);
- return false;
- }
-
- delete it->second;
- __providerHandleMap.erase(it);
-
- return true;
-}
-
-bool ctx::ContextManagerImpl::registerTriggerItem(const char *subject, int operation, ctx::Json attributes, ctx::Json options, const char* owner)
-{
- IF_FAIL_RETURN_TAG(subject, false, _E, "Invalid parameter");
-
- if (!__initialized) {
- __triggerItemList.push_back(TriggerItemFormat(subject, operation, attributes, options, (owner)? owner : ""));
- } else {
- ctx::trigger::TemplateManager* tmplMgr = ctx::trigger::TemplateManager::getInstance();
- IF_FAIL_RETURN_TAG(tmplMgr, false, _E, "Memory allocation failed");
- tmplMgr->registerTemplate(subject, operation, attributes, options, owner);
- }
-
- return true;
-}
-
-bool ctx::ContextManagerImpl::unregisterTriggerItem(const char *subject)
-{
- IF_FAIL_RETURN_TAG(subject, false, _E, "Invalid parameter");
-
- if (!__initialized) {
- __triggerItemList.push_back(TriggerItemFormat(subject));
- } else {
- ctx::trigger::TemplateManager* tmplMgr = ctx::trigger::TemplateManager::getInstance();
- IF_FAIL_RETURN_TAG(tmplMgr, false, _E, "Memory allocation failed");
- tmplMgr->unregisterTemplate(subject);
- }
-
- return true;
-}
-
-bool ctx::ContextManagerImpl::popTriggerItem(std::string &subject, int &operation, ctx::Json &attributes, ctx::Json &options, std::string& owner, bool& unregister)
-{
- IF_FAIL_RETURN(!__triggerItemList.empty(), false);
-
- TriggerItemFormat format = __triggerItemList.front();
- __triggerItemList.pop_front();
-
- subject = format.subject;
- operation = format.operation;
- attributes = format.attributes;
- options = format.options;
- owner = format.owner;
- unregister = format.unregister;
-
- return true;
-}
-
-void ctx::ContextManagerImpl::assignRequest(ctx::RequestInfo* request)
-{
- if (__handleCustomRequest(request)) {
- delete request;
- return;
- }
-
- auto it = __providerHandleMap.find(request->getSubject());
- if (it == __providerHandleMap.end()) {
- _W("Unsupported subject");
- request->reply(ERR_NOT_SUPPORTED);
- delete request;
- return;
- }
-
- if (!it->second->isAllowed(request->getCredentials())) {
- _W("Permission denied");
- request->reply(ERR_PERMISSION_DENIED);
- delete request;
- return;
- }
-
- switch (request->getType()) {
- case REQ_SUBSCRIBE:
- it->second->subscribe(request);
- break;
- case REQ_UNSUBSCRIBE:
- it->second->unsubscribe(request);
- break;
- case REQ_READ:
- case REQ_READ_SYNC:
- it->second->read(request);
- break;
- case REQ_WRITE:
- it->second->write(request);
- break;
- case REQ_SUPPORT:
- request->reply(ERR_NONE);
- delete request;
- break;
- default:
- _E("Invalid type of request");
- delete request;
- }
-}
-
-bool ctx::ContextManagerImpl::isSupported(const char *subject)
-{
- auto it = __providerHandleMap.find(subject);
- return (it != __providerHandleMap.end());
-}
-
-bool ctx::ContextManagerImpl::isAllowed(const ctx::Credentials *creds, const char *subject)
-{
- IF_FAIL_RETURN(creds, true); /* In case internal requests */
- auto it = __providerHandleMap.find(subject);
- IF_FAIL_RETURN(it != __providerHandleMap.end(), false);
- return it->second->isAllowed(creds);
-}
-
-void ctx::ContextManagerImpl::__publish(const char* subject, ctx::Json &option, int error, ctx::Json &dataUpdated)
-{
- _I("Publishing '%s'", subject);
- _J("Option", option);
-
- auto it = __providerHandleMap.find(subject);
- IF_FAIL_VOID(it != __providerHandleMap.end());
-
- it->second->publish(option, error, dataUpdated);
-}
-
-void ctx::ContextManagerImpl::__replyToRead(const char* subject, ctx::Json &option, int error, ctx::Json &dataRead)
-{
- _I("Sending data of '%s'", subject);
- _J("Option", option);
- _J("Data", dataRead);
-
- auto it = __providerHandleMap.find(subject);
- IF_FAIL_VOID(it != __providerHandleMap.end());
-
- it->second->replyToRead(option, error, dataRead);
-}
-
-struct PublishedData {
- int type;
- ctx::ContextManagerImpl *mgr;
- std::string subject;
- int error;
- ctx::Json option;
- ctx::Json data;
- PublishedData(int t, ctx::ContextManagerImpl *m, const char* s, ctx::Json& o, int e, ctx::Json& d)
- : type(t), mgr(m), subject(s), error(e)
- {
- option = o.str();
- data = d.str();
- }
-};
-
-gboolean ctx::ContextManagerImpl::__threadSwitcher(gpointer data)
-{
- PublishedData *tuple = static_cast<PublishedData*>(data);
-
- switch (tuple->type) {
- case REQ_SUBSCRIBE:
- tuple->mgr->__publish(tuple->subject.c_str(), tuple->option, tuple->error, tuple->data);
- break;
- case REQ_READ:
- tuple->mgr->__replyToRead(tuple->subject.c_str(), tuple->option, tuple->error, tuple->data);
- break;
- default:
- _W("Invalid type");
- }
-
- delete tuple;
- return FALSE;
-}
-
-bool ctx::ContextManagerImpl::publish(const char* subject, ctx::Json& option, int error, ctx::Json& dataUpdated)
-{
- IF_FAIL_RETURN_TAG(subject, false, _E, "Invalid parameter");
-
- PublishedData *tuple = new(std::nothrow) PublishedData(REQ_SUBSCRIBE, this, subject, option, error, dataUpdated);
- IF_FAIL_RETURN_TAG(tuple, false, _E, "Memory allocation failed");
-
- g_idle_add(__threadSwitcher, tuple);
-
- return true;
-}
-
-bool ctx::ContextManagerImpl::replyToRead(const char* subject, ctx::Json& option, int error, ctx::Json& dataRead)
-{
- IF_FAIL_RETURN_TAG(subject, false, _E, "Invalid parameter");
-
- PublishedData *tuple = new(std::nothrow) PublishedData(REQ_READ, this, subject, option, error, dataRead);
- IF_FAIL_RETURN_TAG(tuple, false, _E, "Memory allocation failed");
-
- g_idle_add(__threadSwitcher, tuple);
-
- return true;
-}
-
-bool ctx::ContextManagerImpl::__handleCustomRequest(ctx::RequestInfo* request)
-{
- std::string subject = request->getSubject();
- IF_FAIL_RETURN( subject == CONTEXT_TRIGGER_SUBJECT_CUSTOM_ADD ||
- subject == CONTEXT_TRIGGER_SUBJECT_CUSTOM_REMOVE ||
- subject == CONTEXT_TRIGGER_SUBJECT_CUSTOM_PUBLISH, false);
-
- const char* pkg_id = request->getPackageId();
- if (pkg_id == NULL) {
- request->reply(ERR_OPERATION_FAILED);
- return true;
- }
-
- ctx::Json desc = request->getDescription();
- std::string name;
- desc.get(NULL, CT_CUSTOM_NAME, &name);
- std::string subj = pkg_id + std::string("::") + name;
-
- int error = ERR_NONE;
- if (subject == CONTEXT_TRIGGER_SUBJECT_CUSTOM_ADD) {
- ctx::Json tmpl;
- desc.get(NULL, CT_CUSTOM_ATTRIBUTES, &tmpl);
-
- error = ctx::custom_context_provider::addItem(subj, name, tmpl, pkg_id);
- } else if (subject == CONTEXT_TRIGGER_SUBJECT_CUSTOM_REMOVE) {
- error = ctx::custom_context_provider::removeItem(subj);
- if (error == ERR_NONE) {
- ctx::Json data;
- data.set(NULL, CT_CUSTOM_SUBJECT, subj);
- request->reply(error, data);
- }
- } else if (subject == CONTEXT_TRIGGER_SUBJECT_CUSTOM_PUBLISH) {
- ctx::Json fact;
- desc.get(NULL, CT_CUSTOM_FACT, &fact);
-
- error = ctx::custom_context_provider::publishData(subj, fact);
- }
-
- request->reply(error);
- return true;
-}
+++ /dev/null
-/*
- * Copyright (c) 2015 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_MANAGER_IMPL_H_
-#define _CONTEXT_MANAGER_IMPL_H_
-
-#include <string>
-#include <map>
-#include <glib.h>
-#include <ContextManager.h>
-#include <IContextManager.h>
-
-namespace ctx {
-
- /* Forward declaration */
- class Credentials;
- class RequestInfo;
- class ProviderHandler;
-
- class ContextManagerImpl : public IContextManager {
- public:
- ContextManagerImpl();
- ~ContextManagerImpl();
-
- bool init();
- void release();
-
- void assignRequest(ctx::RequestInfo *request);
- bool isSupported(const char *subject);
- bool isAllowed(const Credentials *creds, const char *subject);
- bool popTriggerItem(std::string &subject, int &operation, ctx::Json &attributes, ctx::Json &options, std::string &owner, bool& unregister);
-
- /* From the interface class */
- bool registerProvider(const char *subject, ContextProviderInfo &providerInfo);
- bool unregisterProvider(const char *subject);
- bool registerTriggerItem(const char *subject, int operation, ctx::Json attributes, ctx::Json options, const char *owner = NULL);
- bool unregisterTriggerItem(const char *subject);
- 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);
-
- private:
- std::map<std::string, ProviderHandler*> __providerHandleMap;
- static bool __initialized;
-
- 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);
-
- /* For custom request */
- bool __handleCustomRequest(ctx::RequestInfo* request);
- }; /* class ContextManagerImpl */
-
-} /* namespace ctx */
-
-#endif /* End of __CONTEXT_MANAGER_IMPL_H__ */
#include <glib.h>
#include <Types.h>
-#include <Json.h>
#include "access_control/Privilege.h"
-#include "Server.h"
#include "Request.h"
#include "ProviderHandler.h"
-ctx::ProviderHandler::ProviderHandler(const char *subj, ctx::ContextProviderInfo &prvd) :
- __subject(subj),
- __providerInfo(prvd)
+using namespace ctx;
+
+ProviderHandler::ProviderHandler(const char *subject, const char *privilege, ContextProvider *provider) :
+ __subject(subject),
+ __privilege(privilege),
+ __provider(provider)
{
}
-ctx::ProviderHandler::~ProviderHandler()
+ProviderHandler::~ProviderHandler()
{
- for (auto it = __subscribeRequests.begin(); it != __subscribeRequests.end(); ++it) {
- delete *it;
+ for (RequestInfo*& info : __subscribeRequests) {
+ delete info;
}
__subscribeRequests.clear();
- for (auto it = __readRequests.begin(); it != __readRequests.end(); ++it) {
- delete *it;
+ for (RequestInfo*& info : __readRequests) {
+ delete info;
}
__readRequests.clear();
- __providerInfo.destroy(__providerInfo.data);
+ delete __provider;
}
-bool ctx::ProviderHandler::isAllowed(const ctx::Credentials *creds)
+bool ProviderHandler::isAllowed(const Credentials *creds)
{
IF_FAIL_RETURN(creds, true); /* In case of internal requests */
- return privilege_manager::isAllowed(creds, __providerInfo.privilege);
+ return privilege_manager::isAllowed(creds, __privilege);
}
-ctx::ContextProviderBase* ctx::ProviderHandler::__getProvider(ctx::RequestInfo *request)
+ContextProvider* ProviderHandler::__getProvider(RequestInfo *request)
{
- ContextProviderBase *provider = __providerInfo.create(__providerInfo.data);
- if (!provider) {
- _E("Memory allocation failed");
- delete request;
- return NULL;
- }
-
- return provider;
+ /* TODO: When implementing dynamic so loading... */
+ return __provider;
}
-void ctx::ProviderHandler::subscribe(ctx::RequestInfo *request)
+void ProviderHandler::subscribe(RequestInfo *request)
{
_I(CYAN("'%s' subscribes '%s' (RID-%d)"), request->getClient(), __subject, request->getId());
- ContextProviderBase *provider = __getProvider(request);
+ ContextProvider *provider = __getProvider(request);
IF_FAIL_VOID(provider);
- ctx::Json requestResult;
- int error = provider->subscribe(__subject, request->getDescription().str(), &requestResult);
+ Json requestResult;
+ int error = provider->subscribe(request->getDescription().str(), &requestResult);
if (!request->reply(error, requestResult) || error != ERR_NONE) {
delete request;
__subscribeRequests.push_back(request);
}
-void ctx::ProviderHandler::unsubscribe(ctx::RequestInfo *request)
+void ProviderHandler::unsubscribe(RequestInfo *request)
{
_I(CYAN("'%s' unsubscribes '%s' (RID-%d)"), request->getClient(), __subject, request->getId());
- // Search the subscribe request to be removed
+ /* Search the subscribe request to be removed */
auto target = __findRequest(__subscribeRequests, request->getClient(), request->getId());
if (target == __subscribeRequests.end()) {
_W("Unknown request");
return;
}
- // Keep the pointer to the request found
- RequestInfo *req_found = *target;
+ /* Keep the pointer to the request found */
+ RequestInfo *reqFound = *target;
- // Remove the request from the list
+ /* Remove the request from the list */
__subscribeRequests.erase(target);
- // Check if there exist the same requests
- if (__findRequest(__subscribeRequests, req_found->getDescription()) != __subscribeRequests.end()) {
- // Do not stop detecting the subject
- _D("A same request from '%s' exists", req_found->getClient());
+ /* Check if there exist the same requests */
+ if (__findRequest(__subscribeRequests, reqFound->getDescription()) != __subscribeRequests.end()) {
+ /* Do not stop detecting the subject */
+ _D("A same request from '%s' exists", reqFound->getClient());
request->reply(ERR_NONE);
delete request;
- delete req_found;
+ delete reqFound;
return;
}
- // Get the provider
- ContextProviderBase *provider = __getProvider(request);
+ /* Get the provider */
+ ContextProvider *provider = __getProvider(request);
IF_FAIL_VOID(provider);
- // Stop detecting the subject
- int error = provider->unsubscribe(__subject, req_found->getDescription());
+ /* Stop detecting the subject */
+ int error = provider->unsubscribe(reqFound->getDescription());
request->reply(error);
delete request;
- delete req_found;
+ delete reqFound;
}
-void ctx::ProviderHandler::read(ctx::RequestInfo *request)
+void ProviderHandler::read(RequestInfo *request)
{
_I(CYAN("'%s' reads '%s' (RID-%d)"), request->getClient(), __subject, request->getId());
- ContextProviderBase *provider = __getProvider(request);
+ ContextProvider *provider = __getProvider(request);
IF_FAIL_VOID(provider);
- ctx::Json requestResult;
- int error = provider->read(__subject, request->getDescription().str(), &requestResult);
+ Json requestResult;
+ int error = provider->read(request->getDescription().str(), &requestResult);
if (!request->reply(error, requestResult) || error != ERR_NONE) {
delete request;
__readRequests.push_back(request);
}
-void ctx::ProviderHandler::write(ctx::RequestInfo *request)
+void ProviderHandler::write(RequestInfo *request)
{
_I(CYAN("'%s' writes '%s' (RID-%d)"), request->getClient(), __subject, request->getId());
- ContextProviderBase *provider = __getProvider(request);
+ ContextProvider *provider = __getProvider(request);
IF_FAIL_VOID(provider);
- ctx::Json requestResult;
- int error = provider->write(__subject, request->getDescription(), &requestResult);
+ Json requestResult;
+ int error = provider->write(request->getDescription(), &requestResult);
request->reply(error, requestResult);
delete request;
}
-bool ctx::ProviderHandler::publish(ctx::Json &option, int error, ctx::Json &dataUpdated)
+bool ProviderHandler::publish(Json &option, int error, Json &dataUpdated)
{
auto end = __subscribeRequests.end();
auto target = __findRequest(__subscribeRequests.begin(), end, option);
return true;
}
-bool ctx::ProviderHandler::replyToRead(ctx::Json &option, int error, ctx::Json &dataRead)
+bool ProviderHandler::replyToRead(Json &option, int error, Json &dataRead)
{
auto end = __readRequests.end();
auto target = __findRequest(__readRequests.begin(), end, option);
auto prev = target;
- ctx::Json dummy;
+ Json dummy;
while (target != end) {
(*target)->reply(error, dummy, dataRead);
return true;
}
-ctx::ProviderHandler::RequestList::iterator
-ctx::ProviderHandler::__findRequest(RequestList &reqList, Json &option)
+ProviderHandler::RequestList::iterator
+ProviderHandler::__findRequest(RequestList &reqList, Json &option)
{
return __findRequest(reqList.begin(), reqList.end(), option);
}
-ctx::ProviderHandler::RequestList::iterator
-ctx::ProviderHandler::__findRequest(RequestList &reqList, std::string client, int reqId)
+ProviderHandler::RequestList::iterator
+ProviderHandler::__findRequest(RequestList &reqList, std::string client, int reqId)
{
for (auto it = reqList.begin(); it != reqList.end(); ++it) {
if (client == (*it)->getClient() && reqId == (*it)->getId()) {
return reqList.end();
}
-ctx::ProviderHandler::RequestList::iterator
-ctx::ProviderHandler::__findRequest(RequestList::iterator begin, RequestList::iterator end, Json &option)
+ProviderHandler::RequestList::iterator
+ProviderHandler::__findRequest(RequestList::iterator begin, RequestList::iterator end, Json &option)
{
for (auto it = begin; it != end; ++it) {
if (option == (*it)->getDescription()) {
#include <string>
#include <list>
-#include <ContextProviderBase.h>
+#include <ContextProvider.h>
namespace ctx {
- class Json;
class Credentials;
class RequestInfo;
public:
typedef std::list<RequestInfo*> RequestList;
- ProviderHandler(const char *subj, ContextProviderInfo &prvd);
+ ProviderHandler(const char *subject, const char *privilege, ContextProvider *provider);
~ProviderHandler();
bool isAllowed(const Credentials *creds);
private:
const char *__subject;
- ContextProviderInfo __providerInfo;
+ const char *__privilege;
+ ContextProvider *__provider;
RequestList __subscribeRequests;
RequestList __readRequests;
- ContextProviderBase* __getProvider(RequestInfo *request);
+ ContextProvider* __getProvider(RequestInfo *request);
RequestList::iterator __findRequest(RequestList &reqList, Json &option);
RequestList::iterator __findRequest(RequestList &reqList, std::string client, int reqId);
RequestList::iterator __findRequest(RequestList::iterator begin, RequestList::iterator end, Json &option);
#include <Types.h>
#include "DBusServer.h"
-#include "ContextManagerImpl.h"
+#include "ContextManager.h"
#include "trigger/Trigger.h"
#include "Server.h"
static GMainLoop *mainloop = NULL;
static bool started = false;
-static ctx::ContextManagerImpl *__contextMgr = NULL;
+static ctx::ContextManager *__contextMgr = NULL;
static ctx::DBusServer *__dbusHandle = NULL;
static ctx::trigger::Trigger *__contextTrigger = NULL;
bool result = false;
_I("Init Context Manager");
- __contextMgr = new(std::nothrow) ctx::ContextManagerImpl();
+ __contextMgr = new(std::nothrow) ctx::ContextManager();
IF_FAIL_CATCH_TAG(__contextMgr, _E, "Memory allocation failed");
- context_manager::setInstance(__contextMgr);
result = __contextMgr->init();
IF_FAIL_CATCH_TAG(result, _E, "Initialization Failed");
#include <Types.h>
#include "../access_control/Privilege.h"
-#include "../ContextManagerImpl.h"
+#include "../ContextManager.h"
#include "ContextMonitor.h"
#include "IContextListener.h"
#include "FactRequest.h"
static int __lastErr;
ContextMonitor *ContextMonitor::__instance = NULL;
-ContextManagerImpl *ContextMonitor::__contextMgr = NULL;
+ContextManager *ContextMonitor::__contextMgr = NULL;
static int __generateReqId()
{
{
}
-void ContextMonitor::setContextManager(ContextManagerImpl* ctx_mgr)
+void ContextMonitor::setContextManager(ContextManager* ctx_mgr)
{
__contextMgr = ctx_mgr;
}
namespace ctx {
- class ContextManagerImpl;
+ class ContextManager;
namespace trigger {
class ContextMonitor {
public:
static ContextMonitor* getInstance();
- static void setContextManager(ContextManagerImpl* ctx_mgr);
+ static void setContextManager(ContextManager* ctx_mgr);
static void destroy();
int subscribe(int ruleId, std::string subject, Json option, IContextListener* listener);
~ContextMonitor();
static ContextMonitor *__instance;
- static ContextManagerImpl *__contextMgr;
+ static ContextManager *__contextMgr;
int __subscribe(const char* subject, Json* option, IContextListener* listener);
void __unsubscribe(const char *subject, int subscriptionId);
#include <sstream>
#include <Types.h>
#include <context_trigger_types_internal.h>
-#include "../ContextManagerImpl.h"
+#include "../ContextManager.h"
#include "RuleManager.h"
#include "TemplateManager.h"
using namespace ctx::trigger;
TemplateManager *TemplateManager::__instance = NULL;
-ContextManagerImpl *TemplateManager::__contextMgr = NULL;
+ContextManager *TemplateManager::__contextMgr = NULL;
RuleManager *TemplateManager::__ruleMgr = NULL;
static std::string __intToString(int i)
{
}
-void TemplateManager::setManager(ContextManagerImpl* ctxMgr, RuleManager* ruleMgr)
+void TemplateManager::setManager(ContextManager* ctxMgr, RuleManager* ruleMgr)
{
__contextMgr = ctxMgr;
__ruleMgr = ruleMgr;
namespace ctx {
- class ContextManagerImpl;
+ class ContextManager;
namespace trigger {
class TemplateManager {
public:
static TemplateManager* getInstance();
- static void setManager(ContextManagerImpl* ctxMgr, RuleManager* ruleMgr);
+ static void setManager(ContextManager* ctxMgr, RuleManager* ruleMgr);
static void destroy();
bool init();
~TemplateManager();
static TemplateManager *__instance;
- static ContextManagerImpl *__contextMgr;
+ static ContextManager *__contextMgr;
static RuleManager *__ruleMgr;
DatabaseManager __dbManager;
{
}
-bool Trigger::init(ContextManagerImpl* ctxMgr)
+bool Trigger::init(ContextManager* ctxMgr)
{
// Do the necessary initialization process.
// This function is called from the main thread during the service launching process.
}
}
-void Trigger::__processInitialize(ContextManagerImpl* mgr)
+void Trigger::__processInitialize(ContextManager* mgr)
{
// Context Monitor
ContextMonitor::setContextManager(mgr);
namespace ctx {
class client_request;
- class ContextManagerImpl;
+ class ContextManager;
namespace trigger {
Trigger();
~Trigger();
- bool init(ContextManagerImpl* ctxMgr);
+ bool init(ContextManager* ctxMgr);
void release();
bool assignRequest(RequestInfo* request);
private:
void __processRequest(RequestInfo* request);
- void __processInitialize(ContextManagerImpl* mgr);
+ void __processInitialize(ContextManager* mgr);
void __addRule(RequestInfo* request);
void __removeRule(RequestInfo* request);