2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <DBusTypes.h>
23 #include <ContextProvider.h>
24 #include <CustomRegister.h>
26 #include "access_control/Privilege.h"
27 #include "trigger/TemplateManager.h"
30 #include "ProviderHandler.h"
31 #include "ProviderLoader.h"
32 #include "ContextManager.h"
34 using namespace ctx::trigger;
37 ContextManager::ContextManager()
39 ContextProvider::__setContextManager(this);
40 CustomRegister::__setCustomRegister(this);
41 ProviderLoader::init();
44 ContextManager::~ContextManager()
49 bool ContextManager::init()
54 void ContextManager::release()
56 ProviderHandler::purge();
59 void ContextManager::assignRequest(RequestInfo* request)
61 std::string subject = request->getSubject();
62 if (subject.empty()) {
63 _W("Empty subject name");
64 request->reply(ERR_NOT_SUPPORTED);
69 ProviderHandler *handle = ProviderHandler::getInstance(subject, true);
70 if (!handle || !handle->isSupported()) {
71 request->reply(ERR_NOT_SUPPORTED);
76 if (request->getType() != REQ_SUPPORT && !handle->isAllowed(request->getCredentials())) {
77 _W("Permission denied");
78 request->reply(ERR_PERMISSION_DENIED);
83 switch (request->getType()) {
85 handle->subscribe(request);
88 handle->unsubscribe(request);
92 handle->read(request);
95 handle->write(request);
98 request->reply(ERR_NONE);
102 _E("Invalid type of request");
107 bool ContextManager::isSupported(const char *subject)
109 ProviderHandler *handle = ProviderHandler::getInstance(subject, true);
114 return handle->isSupported();
117 bool ContextManager::isAllowed(const Credentials *creds, const char *subject)
119 IF_FAIL_RETURN(creds, true); /* In case internal requests */
121 ProviderHandler *handle = ProviderHandler::getInstance(subject, true);
126 return handle->isAllowed(creds);
129 void ContextManager::__publish(const char* subject, CtxJson1 &option, int error, CtxJson1 &dataUpdated)
131 _I("Publishing '%s'", subject);
132 _J("Option", option);
134 ProviderHandler *handle = ProviderHandler::getInstance(subject, false);
135 IF_FAIL_VOID_TAG(handle, _W, "No corresponding provider");
137 handle->publish(option, error, dataUpdated);
140 void ContextManager::__replyToRead(const char* subject, CtxJson1 &option, int error, CtxJson1 &dataRead)
142 _I("Sending data of '%s'", subject);
143 _J("Option", option);
144 _J("Data", dataRead);
146 ProviderHandler *handle = ProviderHandler::getInstance(subject, false);
147 IF_FAIL_VOID_TAG(handle, _W, "No corresponding provider");
149 handle->replyToRead(option, error, dataRead);
152 struct PublishedData {
159 PublishedData(int t, ContextManager *m, const char* s, CtxJson1& o, int e, CtxJson1& d)
160 : type(t), mgr(m), subject(s), error(e)
167 gboolean ContextManager::__threadSwitcher(gpointer data)
169 PublishedData *tuple = static_cast<PublishedData*>(data);
171 switch (tuple->type) {
173 tuple->mgr->__publish(tuple->subject.c_str(), tuple->option, tuple->error, tuple->data);
176 tuple->mgr->__replyToRead(tuple->subject.c_str(), tuple->option, tuple->error, tuple->data);
186 bool ContextManager::publish(const char* subject, CtxJson1& option, int error, CtxJson1& dataUpdated)
188 IF_FAIL_RETURN_TAG(subject, false, _E, "Invalid parameter");
190 PublishedData *tuple = new(std::nothrow) PublishedData(REQ_SUBSCRIBE, this, subject, option, error, dataUpdated);
191 IF_FAIL_RETURN_TAG(tuple, false, _E, "Memory allocation failed");
193 g_idle_add(__threadSwitcher, tuple);
198 bool ContextManager::replyToRead(const char* subject, CtxJson1& option, int error, CtxJson1& dataRead)
200 IF_FAIL_RETURN_TAG(subject, false, _E, "Invalid parameter");
202 PublishedData *tuple = new(std::nothrow) PublishedData(REQ_READ, this, subject, option, error, dataRead);
203 IF_FAIL_RETURN_TAG(tuple, false, _E, "Memory allocation failed");
205 g_idle_add(__threadSwitcher, tuple);
210 bool ContextManager::popTriggerTemplate(std::string &subject, int &operation, CtxJson1 &attribute, CtxJson1 &option)
212 return ProviderLoader::popTriggerTemplate(subject, operation, attribute, option);
215 /* Only for explicit request of custom provider */
216 bool ContextManager::registerCustomProvider(const char* subject, int operation, ctx::CtxJson1 &attribute, ctx::CtxJson1 &option, const char* owner)
218 IF_FAIL_RETURN_TAG(ProviderHandler::getInstance(subject, true), false, _E, "Register provider failed");
220 TemplateManager* tmplMgr = TemplateManager::getInstance();
221 IF_FAIL_RETURN_TAG(tmplMgr, false, _E, "Memory allocation failed");
222 tmplMgr->registerTemplate(subject, operation, attribute, option, owner);
227 bool ContextManager::unregisterCustomProvider(const char* subject)
229 TemplateManager* tmplMgr = TemplateManager::getInstance();
230 IF_FAIL_RETURN_TAG(tmplMgr, false, _E, "Memory allocation failed");
231 tmplMgr->unregisterTemplate(subject);
233 int error = ProviderHandler::unregisterCustomProvider(subject);
234 IF_FAIL_RETURN_TAG(error == ERR_NONE, false, _E, "Unregister provider failed");