From: Somin Kim Date: Wed, 6 Apr 2016 04:21:03 +0000 (+0900) Subject: Applying Tizen C++ coding style to custom context provider X-Git-Tag: submit/tizen/20160408.073002^2~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5ba9bc46546ff288c2694075106535f116c3f889;p=platform%2Fcore%2Fcontext%2Fcontext-provider.git Applying Tizen C++ coding style to custom context provider Change-Id: I7cdbed7358584669caef5eb7ffc5a59c08b70734 Signed-off-by: Somin Kim --- diff --git a/src/custom/CustomBase.cpp b/src/custom/CustomBase.cpp new file mode 100644 index 0000000..529835b --- /dev/null +++ b/src/custom/CustomBase.cpp @@ -0,0 +1,91 @@ +/* + * 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 +#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; +} diff --git a/src/custom/CustomBase.h b/src/custom/CustomBase.h new file mode 100644 index 0000000..c95ba5c --- /dev/null +++ b/src/custom/CustomBase.h @@ -0,0 +1,55 @@ +/* + * 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. + */ + +#ifndef _CONTEXT_CUSTOM_BASE_H_ +#define _CONTEXT_CUSTOM_BASE_H_ + +#include +#include +#include + +namespace ctx { + + class CustomBase : public ContextProviderBase { + public: + CustomBase(std::string subject, std::string name, ctx::Json tmpl, std::string owner); + ~CustomBase(); + + int subscribe(const char *subject, ctx::Json option, ctx::Json *requestResult); + int unsubscribe(const char *subject, ctx::Json option); + int read(const char *subject, ctx::Json option, ctx::Json *requestResult); + int write(const char *subject, ctx::Json data, ctx::Json *requestResult); + + static bool isSupported(); + void submitTriggerItem(); + void unsubmitTriggerItem(); + + void handleUpdate(ctx::Json data); + + const char* getSubject(); + std::string getOwner(); + ctx::Json getTemplate(); + + private: + std::string __subject; + std::string __name; + ctx::Json __tmpl; + std::string __owner; + ctx::Json __latest; + }; +} + +#endif /* End of _CONTEXT_CUSTOM_BASE_H_ */ diff --git a/src/custom/CustomContextProvider.cpp b/src/custom/CustomContextProvider.cpp new file mode 100644 index 0000000..286f28e --- /dev/null +++ b/src/custom/CustomContextProvider.cpp @@ -0,0 +1,231 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include +#include "CustomBase.h" + +static std::map __customMap; + +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::ContextProviderInfo providerInfo(ctx::custom_context_provider::create, + ctx::custom_context_provider::destroy, + const_cast(subject), privilege); + ctx::context_manager::registerProvider(subject, providerInfo); + __customMap[subject]->submitTriggerItem(); +} + +void unregisterProvider(const char* subject) +{ + __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 __customMap[static_cast(data)]; +} + +EXTAPI void ctx::custom_context_provider::destroy(void *data) +{ + std::map::iterator it = __customMap.find(static_cast(data)); + if (it != __customMap.end()) { + delete it->second; + __customMap.erase(it); + } +} + +EXTAPI bool ctx::initCustomContextProvider() +{ + // Create custom template db + std::string q = std::string("CREATE TABLE IF NOT EXISTS context_trigger_custom_template ") + + "(subject TEXT DEFAULT '' NOT NULL PRIMARY KEY, name TEXT DEFAULT '' NOT NULL, operation INTEGER DEFAULT 3 NOT NULL, " + + "attributes TEXT DEFAULT '' NOT NULL, owner TEXT DEFAULT '' NOT NULL)"; + + std::vector record; + bool ret = db_manager::execute_sync(q.c_str(), &record); + IF_FAIL_RETURN_TAG(ret, false, _E, "Create template table failed"); + + // Register custom items + 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::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; + std::string owner; + elem.get(NULL, "subject", &subject); + elem.get(NULL, "name", &name); + elem.get(NULL, "attributes", &attributes); + elem.get(NULL, "owner", &owner); + + error = ctx::custom_context_provider::addItem(subject, name, ctx::Json(attributes), owner.c_str(), true); + if (error != ERR_NONE) { + _E("Failed to add custom item(%s): %#x", subject.c_str(), error); + } + } + + return true; +} + +EXTAPI int ctx::custom_context_provider::addItem(std::string subject, std::string name, ctx::Json tmpl, const char* owner, bool isInit) +{ + std::map::iterator it; + it = __customMap.find(subject); + + 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 + return ERR_NONE; + } + + // Create custom base + ctx::CustomBase* custom = new(std::nothrow) CustomBase(subject, name, tmpl, owner); + IF_FAIL_RETURN_TAG(custom, ERR_OUT_OF_MEMORY, _E, "Memory allocation failed"); + __customMap[subject] = custom; + + registerProvider(custom->getSubject(), NULL); + + // Add item to custom template db + 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 record; + bool ret = db_manager::execute_sync(q.c_str(), &record); + IF_FAIL_RETURN_TAG(ret, false, _E, "Failed to query custom templates"); + } + + return ERR_NONE; +} + +EXTAPI int ctx::custom_context_provider::removeItem(std::string subject) +{ + std::map::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()); + + // Remove item from custom template db + std::string q = "DELETE FROM context_trigger_custom_template WHERE subject = '" + subject + "'"; + std::vector record; + bool ret = db_manager::execute_sync(q.c_str(), &record); + IF_FAIL_RETURN_TAG(ret, false, _E, "Failed to query custom templates"); + + return ERR_NONE; +} + +EXTAPI int ctx::custom_context_provider::publishData(std::string subject, ctx::Json fact) +{ + std::map::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 = __isValidFact(subject, fact); + IF_FAIL_RETURN_TAG(ret, ERR_INVALID_DATA, _E, "Invalid fact(%s)", subject.c_str()); + + __customMap[subject]->handleUpdate(fact); + return ERR_NONE; +} + +bool __isValidFact(std::string subject, ctx::Json& fact) +{ + ctx::Json tmpl = __customMap[subject]->getTemplate(); + IF_FAIL_RETURN_TAG(tmpl != EMPTY_JSON_OBJECT, false, _E, "Failed to get template"); + + bool ret; + std::list keys; + fact.getKeys(&keys); + + for (std::list::iterator it = keys.begin(); it != keys.end(); it++) { + std::string key = *it; + + std::string type; + tmpl.get(key.c_str(), "type", &type); + if (type == "integer") { + int val; + ret = fact.get(NULL, key.c_str(), &val); + IF_FAIL_RETURN_TAG(ret, false, _E, "Custom fact: invalid data type"); + + 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 = __checkValueString(tmpl, key, val_str); + IF_FAIL_RETURN_TAG(ret, false, _E, "Custom fact: invalid value"); + } else { + _E("Custom fact: invalid data type"); + return false; + } + } + + return true; +} + +bool __checkValueInt(ctx::Json& tmpl, std::string key, int value) +{ + int min, max; + + if (tmpl.get(key.c_str(), "min", &min)) { + IF_FAIL_RETURN(value >= min, false); + } + + if (tmpl.get(key.c_str(), "max", &max)) { + IF_FAIL_RETURN(value <= max, false); + } + + return true; +} + +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 tmplValue; + for (int i = 0; tmpl.getAt(key.c_str(), "values", i, &tmplValue); i++) { + if (tmplValue == value) + return true; + } + + return false; +} + + diff --git a/src/custom/custom_base.cpp b/src/custom/custom_base.cpp deleted file mode 100644 index 4371202..0000000 --- a/src/custom/custom_base.cpp +++ /dev/null @@ -1,91 +0,0 @@ -/* - * 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 -#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; -} diff --git a/src/custom/custom_base.h b/src/custom/custom_base.h deleted file mode 100644 index 68e45cc..0000000 --- a/src/custom/custom_base.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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. - */ - -#ifndef _CUSTOM_BASE_H_ -#define _CUSTOM_BASE_H_ - -#include -#include -#include - -namespace ctx { - - class custom_base : public ContextProviderBase { - public: - custom_base(std::string subject, std::string name, ctx::Json tmpl, std::string owner); - ~custom_base(); - - int subscribe(const char *subject, ctx::Json option, ctx::Json *requestResult); - int unsubscribe(const char *subject, ctx::Json option); - int read(const char *subject, ctx::Json option, ctx::Json *requestResult); - int write(const char *subject, ctx::Json data, ctx::Json *requestResult); - - static bool is_supported(); - void submit_trigger_item(); - void unsubmit_trigger_item(); - - void handle_update(ctx::Json data); - - const char* get_subject(); - std::string get_owner(); - ctx::Json get_template(); - - private: - std::string _subject; - std::string _name; - ctx::Json _tmpl; - std::string _owner; - ctx::Json latest; - }; -} - -#endif // _CUSTOM_BASE_H_ diff --git a/src/custom/custom_context_provider.cpp b/src/custom/custom_context_provider.cpp deleted file mode 100644 index 5bec97e..0000000 --- a/src/custom/custom_context_provider.cpp +++ /dev/null @@ -1,231 +0,0 @@ -/* - * 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 -#include -#include -#include -#include -#include -#include -#include "custom_base.h" - -std::map custom_map; - -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); - -void registerProvider(const char *subject, const char *privilege) -{ - ctx::ContextProviderInfo providerInfo(ctx::custom_context_provider::create, - ctx::custom_context_provider::destroy, - const_cast(subject), privilege); - ctx::context_manager::registerProvider(subject, providerInfo); - custom_map[subject]->submit_trigger_item(); -} - -void unregisterProvider(const char* subject) -{ - custom_map[subject]->unsubmit_trigger_item(); - 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(data)]; -} - -EXTAPI void ctx::custom_context_provider::destroy(void *data) -{ - std::map::iterator it = custom_map.find(static_cast(data)); - if (it != custom_map.end()) { - delete it->second; - custom_map.erase(it); - } -} - -EXTAPI bool ctx::initCustomContextProvider() -{ - // Create custom template db - std::string q = std::string("CREATE TABLE IF NOT EXISTS context_trigger_custom_template ") - + "(subject TEXT DEFAULT '' NOT NULL PRIMARY KEY, name TEXT DEFAULT '' NOT NULL, operation INTEGER DEFAULT 3 NOT NULL, " - + "attributes TEXT DEFAULT '' NOT NULL, owner TEXT DEFAULT '' NOT NULL)"; - - std::vector record; - bool ret = db_manager::execute_sync(q.c_str(), &record); - 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); - IF_FAIL_RETURN_TAG(ret, false, _E, "Failed to query custom templates"); - IF_FAIL_RETURN(record.size() > 0, true); - - int error; - std::vector::iterator vec_end = record.end(); - for (std::vector::iterator vec_pos = record.begin(); vec_pos != vec_end; ++vec_pos) { - ctx::Json elem = *vec_pos; - std::string subject; - std::string name; - std::string attributes; - std::string owner; - elem.get(NULL, "subject", &subject); - elem.get(NULL, "name", &name); - elem.get(NULL, "attributes", &attributes); - elem.get(NULL, "owner", &owner); - - error = ctx::custom_context_provider::addItem(subject, name, ctx::Json(attributes), owner.c_str(), true); - if (error != ERR_NONE) { - _E("Failed to add custom item(%s): %#x", subject.c_str(), error); - } - } - - return true; -} - -EXTAPI int ctx::custom_context_provider::addItem(std::string subject, std::string name, ctx::Json tmpl, const char* owner, bool is_init) -{ - std::map::iterator it; - it = custom_map.find(subject); - - if (it != custom_map.end()) { - if ((it->second)->get_template() != tmpl) { // Same item name, but different template - return ERR_DATA_EXIST; - } - // Same item name with same template - return ERR_NONE; - } - - // Create custom base - ctx::custom_base* custom = new(std::nothrow) custom_base(subject, name, tmpl, owner); - IF_FAIL_RETURN_TAG(custom, ERR_OUT_OF_MEMORY, _E, "Memory allocation failed"); - custom_map[subject] = custom; - - registerProvider(custom->get_subject(), NULL); - - // Add item to custom template db - if (!is_init) { - std::string q = "INSERT OR IGNORE INTO context_trigger_custom_template (subject, name, attributes, owner) VALUES ('" - + subject + "', '" + name + "', '" + tmpl.str() + "', '" + owner + "'); "; - std::vector record; - bool ret = db_manager::execute_sync(q.c_str(), &record); - IF_FAIL_RETURN_TAG(ret, false, _E, "Failed to query custom templates"); - } - - return ERR_NONE; -} - -EXTAPI int ctx::custom_context_provider::removeItem(std::string subject) -{ - std::map::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()); - - unregisterProvider(subject.c_str()); - - // Remove item from custom template db - std::string q = "DELETE FROM context_trigger_custom_template WHERE subject = '" + subject + "'"; - std::vector record; - bool ret = db_manager::execute_sync(q.c_str(), &record); - IF_FAIL_RETURN_TAG(ret, false, _E, "Failed to query custom templates"); - - return ERR_NONE; -} - -EXTAPI int ctx::custom_context_provider::publishData(std::string subject, ctx::Json fact) -{ - std::map::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()); - - bool ret = is_valid_fact(subject, fact); - IF_FAIL_RETURN_TAG(ret, ERR_INVALID_DATA, _E, "Invalid fact(%s)", subject.c_str()); - - custom_map[subject]->handle_update(fact); - return ERR_NONE; -} - -bool is_valid_fact(std::string subject, ctx::Json& fact) -{ - ctx::Json tmpl = custom_map[subject]->get_template(); - IF_FAIL_RETURN_TAG(tmpl != EMPTY_JSON_OBJECT, false, _E, "Failed to get template"); - - bool ret; - std::list keys; - fact.getKeys(&keys); - - for (std::list::iterator it = keys.begin(); it != keys.end(); it++) { - std::string key = *it; - - std::string type; - tmpl.get(key.c_str(), "type", &type); - if (type == "integer") { - int val; - 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); - 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); - IF_FAIL_RETURN_TAG(ret, false, _E, "Custom fact: invalid value"); - } else { - _E("Custom fact: invalid data type"); - return false; - } - } - - return true; -} - -bool check_value_int(ctx::Json& tmpl, std::string key, int value) -{ - int min, max; - - if (tmpl.get(key.c_str(), "min", &min)) { - IF_FAIL_RETURN(value >= min, false); - } - - if (tmpl.get(key.c_str(), "max", &max)) { - IF_FAIL_RETURN(value <= max, false); - } - - return true; -} - -bool check_value_string(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) - return true; - } - - return false; -} - -