--- /dev/null
+/*
+ * Copyright (c) 2016 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 <ContextManager.h>
+#include "CustomBase.h"
+
+ctx::CustomBase::CustomBase(std::string subject, std::string name, ctx::Json tmpl, std::string owner) :
+ __subject(subject),
+ __name(name),
+ __tmpl(tmpl),
+ __owner(owner)
+{
+}
+
+ctx::CustomBase::~CustomBase()
+{
+}
+
+bool ctx::CustomBase::isSupported()
+{
+ return true;
+}
+
+void ctx::CustomBase::submitTriggerItem()
+{
+ context_manager::registerTriggerItem(__subject.c_str(), OPS_SUBSCRIBE | OPS_READ,
+ __tmpl.str(), NULL, __owner.c_str());
+}
+
+void ctx::CustomBase::unsubmitTriggerItem()
+{
+ context_manager::unregisterTriggerItem(__subject.c_str());
+}
+
+int ctx::CustomBase::subscribe(const char *subject, ctx::Json option, ctx::Json *requestResult)
+{
+ return ERR_NONE;
+
+}
+
+int ctx::CustomBase::unsubscribe(const char *subject, ctx::Json option)
+{
+ return ERR_NONE;
+}
+
+int ctx::CustomBase::read(const char *subject, ctx::Json option, ctx::Json *requestResult)
+{
+ ctx::Json data = __latest.str();
+ ctx::context_manager::replyToRead(__subject.c_str(), NULL, ERR_NONE, data);
+ return ERR_NONE;
+}
+
+int ctx::CustomBase::write(const char *subject, ctx::Json data, ctx::Json *requestResult)
+{
+ return ERR_NONE;
+}
+
+void ctx::CustomBase::handleUpdate(ctx::Json data)
+{
+ // Store latest state
+ __latest = data.str();
+ ctx::context_manager::publish(__subject.c_str(), NULL, ERR_NONE, data);
+}
+
+const char* ctx::CustomBase::getSubject()
+{
+ return __subject.c_str();
+}
+
+std::string ctx::CustomBase::getOwner()
+{
+ return __owner;
+}
+
+ctx::Json ctx::CustomBase::getTemplate()
+{
+ return __tmpl;
+}
#include <ContextProviderBase.h>
#include <db_mgr.h>
#include <CustomContextProvider.h>
-#include "custom_base.h"
+#include "CustomBase.h"
-std::map<std::string, ctx::custom_base*> custom_map;
+static std::map<std::string, ctx::CustomBase*> __customMap;
-static bool is_valid_fact(std::string subject, ctx::Json& fact);
-static bool check_value_int(ctx::Json& tmpl, std::string key, int value);
-static bool check_value_string(ctx::Json& tmpl, std::string key, std::string value);
+static bool __isValidFact(std::string subject, ctx::Json& fact);
+static bool __checkValueInt(ctx::Json& tmpl, std::string key, int value);
+static bool __checkValueString(ctx::Json& tmpl, std::string key, std::string value);
void registerProvider(const char *subject, const char *privilege)
{
ctx::custom_context_provider::destroy,
const_cast<char*>(subject), privilege);
ctx::context_manager::registerProvider(subject, providerInfo);
- custom_map[subject]->submit_trigger_item();
+ __customMap[subject]->submitTriggerItem();
}
void unregisterProvider(const char* subject)
{
- custom_map[subject]->unsubmit_trigger_item();
+ __customMap[subject]->unsubmitTriggerItem();
ctx::context_manager::unregisterProvider(subject);
}
EXTAPI ctx::ContextProviderBase* ctx::custom_context_provider::create(void *data)
{
// Already created in addItem() function. Return corresponding custom provider
- return custom_map[static_cast<const char*>(data)];
+ return __customMap[static_cast<const char*>(data)];
}
EXTAPI void ctx::custom_context_provider::destroy(void *data)
{
- std::map<std::string, ctx::custom_base*>::iterator it = custom_map.find(static_cast<char*>(data));
- if (it != custom_map.end()) {
+ std::map<std::string, ctx::CustomBase*>::iterator it = __customMap.find(static_cast<char*>(data));
+ if (it != __customMap.end()) {
delete it->second;
- custom_map.erase(it);
+ __customMap.erase(it);
}
}
IF_FAIL_RETURN_TAG(ret, false, _E, "Create template table failed");
// Register custom items
- std::string q_select = "SELECT * FROM context_trigger_custom_template";
- ret = db_manager::execute_sync(q_select.c_str(), &record);
+ std::string qSelect = "SELECT * FROM context_trigger_custom_template";
+ ret = db_manager::execute_sync(qSelect.c_str(), &record);
IF_FAIL_RETURN_TAG(ret, false, _E, "Failed to query custom templates");
IF_FAIL_RETURN(record.size() > 0, true);
int error;
- std::vector<Json>::iterator vec_end = record.end();
- for (std::vector<Json>::iterator vec_pos = record.begin(); vec_pos != vec_end; ++vec_pos) {
- ctx::Json elem = *vec_pos;
+ std::vector<Json>::iterator vedEnd = record.end();
+ for (auto vecPos = record.begin(); vecPos != vedEnd; ++vecPos) {
+ ctx::Json elem = *vecPos;
std::string subject;
std::string name;
std::string attributes;
return true;
}
-EXTAPI int ctx::custom_context_provider::addItem(std::string subject, std::string name, ctx::Json tmpl, const char* owner, bool is_init)
+EXTAPI int ctx::custom_context_provider::addItem(std::string subject, std::string name, ctx::Json tmpl, const char* owner, bool isInit)
{
- std::map<std::string, ctx::custom_base*>::iterator it;
- it = custom_map.find(subject);
+ std::map<std::string, ctx::CustomBase*>::iterator it;
+ it = __customMap.find(subject);
- if (it != custom_map.end()) {
- if ((it->second)->get_template() != tmpl) { // Same item name, but different template
+ if (it != __customMap.end()) {
+ if ((it->second)->getTemplate() != tmpl) { // Same item name, but different template
return ERR_DATA_EXIST;
}
// Same item name with same template
}
// Create custom base
- ctx::custom_base* custom = new(std::nothrow) custom_base(subject, name, tmpl, owner);
+ ctx::CustomBase* custom = new(std::nothrow) CustomBase(subject, name, tmpl, owner);
IF_FAIL_RETURN_TAG(custom, ERR_OUT_OF_MEMORY, _E, "Memory allocation failed");
- custom_map[subject] = custom;
+ __customMap[subject] = custom;
- registerProvider(custom->get_subject(), NULL);
+ registerProvider(custom->getSubject(), NULL);
// Add item to custom template db
- if (!is_init) {
+ if (!isInit) {
std::string q = "INSERT OR IGNORE INTO context_trigger_custom_template (subject, name, attributes, owner) VALUES ('"
+ subject + "', '" + name + "', '" + tmpl.str() + "', '" + owner + "'); ";
std::vector<Json> record;
EXTAPI int ctx::custom_context_provider::removeItem(std::string subject)
{
- std::map<std::string, ctx::custom_base*>::iterator it;
- it = custom_map.find(subject);
- IF_FAIL_RETURN_TAG(it != custom_map.end(), ERR_NOT_SUPPORTED, _E, "%s not supported", subject.c_str());
+ std::map<std::string, ctx::CustomBase*>::iterator it;
+ it = __customMap.find(subject);
+ IF_FAIL_RETURN_TAG(it != __customMap.end(), ERR_NOT_SUPPORTED, _E, "%s not supported", subject.c_str());
unregisterProvider(subject.c_str());
EXTAPI int ctx::custom_context_provider::publishData(std::string subject, ctx::Json fact)
{
- std::map<std::string, ctx::custom_base*>::iterator it;
- it = custom_map.find(subject);
- IF_FAIL_RETURN_TAG(it != custom_map.end(), ERR_NOT_SUPPORTED, _E, "%s not supported", subject.c_str());
+ std::map<std::string, ctx::CustomBase*>::iterator it;
+ it = __customMap.find(subject);
+ IF_FAIL_RETURN_TAG(it != __customMap.end(), ERR_NOT_SUPPORTED, _E, "%s not supported", subject.c_str());
- bool ret = is_valid_fact(subject, fact);
+ bool ret = __isValidFact(subject, fact);
IF_FAIL_RETURN_TAG(ret, ERR_INVALID_DATA, _E, "Invalid fact(%s)", subject.c_str());
- custom_map[subject]->handle_update(fact);
+ __customMap[subject]->handleUpdate(fact);
return ERR_NONE;
}
-bool is_valid_fact(std::string subject, ctx::Json& fact)
+bool __isValidFact(std::string subject, ctx::Json& fact)
{
- ctx::Json tmpl = custom_map[subject]->get_template();
+ ctx::Json tmpl = __customMap[subject]->getTemplate();
IF_FAIL_RETURN_TAG(tmpl != EMPTY_JSON_OBJECT, false, _E, "Failed to get template");
bool ret;
ret = fact.get(NULL, key.c_str(), &val);
IF_FAIL_RETURN_TAG(ret, false, _E, "Custom fact: invalid data type");
- ret = check_value_int(tmpl, key, val);
+ ret = __checkValueInt(tmpl, key, val);
IF_FAIL_RETURN_TAG(ret, false, _E, "Custom fact: invalid value");
} else if (type == "string") {
std::string val_str;
ret = fact.get(NULL, key.c_str(), &val_str);
IF_FAIL_RETURN_TAG(ret, false, _E, "Custom fact: invalid data type");
- ret = check_value_string(tmpl, key, val_str);
+ ret = __checkValueString(tmpl, key, val_str);
IF_FAIL_RETURN_TAG(ret, false, _E, "Custom fact: invalid value");
} else {
_E("Custom fact: invalid data type");
return true;
}
-bool check_value_int(ctx::Json& tmpl, std::string key, int value)
+bool __checkValueInt(ctx::Json& tmpl, std::string key, int value)
{
int min, max;
return true;
}
-bool check_value_string(ctx::Json& tmpl, std::string key, std::string value)
+bool __checkValueString(ctx::Json& tmpl, std::string key, std::string value)
{
// case1: any value is accepted
if (tmpl.getSize(key.c_str(), "values") <= 0)
return true;
// case2: check acceptable value
- std::string t_val;
- for (int i = 0; tmpl.getAt(key.c_str(), "values", i, &t_val); i++) {
- if (t_val == value)
+ std::string tmplValue;
+ for (int i = 0; tmpl.getAt(key.c_str(), "values", i, &tmplValue); i++) {
+ if (tmplValue == value)
return true;
}
+++ /dev/null
-/*
- * Copyright (c) 2016 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 <ContextManager.h>
-#include "custom_base.h"
-
-ctx::custom_base::custom_base(std::string subject, std::string name, ctx::Json tmpl, std::string owner) :
- _subject(subject),
- _name(name),
- _tmpl(tmpl),
- _owner(owner)
-{
-}
-
-ctx::custom_base::~custom_base()
-{
-}
-
-bool ctx::custom_base::is_supported()
-{
- return true;
-}
-
-void ctx::custom_base::submit_trigger_item()
-{
- context_manager::registerTriggerItem(_subject.c_str(), OPS_SUBSCRIBE | OPS_READ,
- _tmpl.str(), NULL, _owner.c_str());
-}
-
-void ctx::custom_base::unsubmit_trigger_item()
-{
- context_manager::unregisterTriggerItem(_subject.c_str());
-}
-
-int ctx::custom_base::subscribe(const char *subject, ctx::Json option, ctx::Json *requestResult)
-{
- return ERR_NONE;
-
-}
-
-int ctx::custom_base::unsubscribe(const char *subject, ctx::Json option)
-{
- return ERR_NONE;
-}
-
-int ctx::custom_base::read(const char *subject, ctx::Json option, ctx::Json *requestResult)
-{
- ctx::Json data = latest.str();
- ctx::context_manager::replyToRead(_subject.c_str(), NULL, ERR_NONE, data);
- return ERR_NONE;
-}
-
-int ctx::custom_base::write(const char *subject, ctx::Json data, ctx::Json *requestResult)
-{
- return ERR_NONE;
-}
-
-void ctx::custom_base::handle_update(ctx::Json data)
-{
- // Store latest state
- latest = data.str();
- ctx::context_manager::publish(_subject.c_str(), NULL, ERR_NONE, data);
-}
-
-const char* ctx::custom_base::get_subject()
-{
- return _subject.c_str();
-}
-
-std::string ctx::custom_base::get_owner()
-{
- return _owner;
-}
-
-ctx::Json ctx::custom_base::get_template()
-{
- return _tmpl;
-}