From: Mu-Woong Lee Date: Thu, 3 Aug 2017 04:12:19 +0000 (+0900) Subject: trigger: define API handle structures and static util functions X-Git-Tag: accepted/tizen/4.0/unified/20170927.045601^2~14 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=605d5964c59c67ec1b7c92ffa1ecaf66d81f84a0;p=platform%2Fcore%2Fapi%2Fcontext.git trigger: define API handle structures and static util functions Change-Id: I6c3b6ebdfff1e2cf3024b145601e8455783944b4 Signed-off-by: Mu-Woong Lee --- diff --git a/src/trigger/context_trigger.cpp b/src/trigger/context_trigger.cpp index a6ad4da..512510a 100644 --- a/src/trigger/context_trigger.cpp +++ b/src/trigger/context_trigger.cpp @@ -14,9 +14,184 @@ * limitations under the License. */ +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include #include #include +#include "PrivilegeChecker.h" +#include "ContextItem.h" +#include "CustomTemplate.h" + +#define _DEPRECATED_FUNC _W("DEPRECATION WARNING: %s is deprecated and will be removed from next release.", __FUNCTION__) +#define _DEPRECATED_EVENT(id) _W("DEPRECATION WARNING: This event (%d) is deprecated and will be removed from next release.", (id)) +#define _DEPRECATED_COND(id) _W("DEPRECATION WARNING: This condition (%d) is deprecated and will be removed from next release.", (id)) + +#define ASSERT_ALLOC(X) IF_FAIL_RETURN_TAG(X, E_NO_MEM, _E, E_STR_ALLOC) +#define ASSERT_NOT_NULL(X) IF_FAIL_RETURN_TAG(X, E_PARAM, _E, "Parameter null") +#define INIT_SCHED IF_FAIL_RETURN_TAG(__scheduler.get(), E_SUPPORT, _E, "Job scheduler is not supported") +#define SCHED __scheduler.get() + +#define PKG_ID_LENGTH 128 + +enum EntryCategory { + CATEGORY_EVENT = 1, + CATEGORY_CONDITION = 2 +}; + +enum class OpType { + UNDEFINED = 0, + EQ, // equals to + NE, // not equals to + GT, // greater than + GE, // greater than or equals to + LT, // less than + LE // less than or equals to +}; + +typedef struct _context_trigger_rule_s { + ctx_sched_job_h job; + bool readOnly; + bool hasEvent; + bool hasAction; + std::set conditions; + + _context_trigger_rule_s() : + job(NULL), readOnly(false), hasEvent(false), hasAction(false) + { + ctx_sched_job_create_on_demand(&job); + } + + _context_trigger_rule_s(ctx_sched_job_h j) : + job(j), readOnly(true), hasEvent(true), hasAction(true) + { + } + + ~_context_trigger_rule_s() + { + ctx_sched_job_destroy(job); + } +} _context_trigger_rule_h; + + +typedef struct _context_trigger_rule_entry_s { + int category; + int type; + std::string uri; + ctx_sched_job_context_h jobContext; + std::set disjunctionKeys; + + _context_trigger_rule_entry_s(int c, int t, const char* u) : + category(c), type(t), uri(u), jobContext(NULL) + { + if (category == CATEGORY_EVENT) + ctx_sched_job_trigger_create(uri.c_str(), &jobContext); + else + ctx_sched_job_requirement_create(uri.c_str(), false, &jobContext); + } + + ~_context_trigger_rule_entry_s() + { + ctx_sched_job_context_destroy(jobContext); + } +} _context_trigger_rule_entry_h; + + +namespace { + class Scheduler { + private: + ctx_sched_h __scheduler; + + public: + Scheduler() : __scheduler(NULL) {} + + ~Scheduler() + { + ctx_sched_destroy(__scheduler); + } + + ctx_sched_h get() + { + if (__scheduler == NULL) + ctx_sched_create(&__scheduler); + + return __scheduler; + } + }; +} + +static OpType __getOpType(const char* opStr) +{ + if (STR_EQ(opStr, CONTEXT_TRIGGER_EQUAL_TO)) + return OpType::EQ; + + if (STR_EQ(opStr, CONTEXT_TRIGGER_NOT_EQUAL_TO)) + return OpType::NE; + + if (STR_EQ(opStr, CONTEXT_TRIGGER_GREATER_THAN)) + return OpType::GT; + + if (STR_EQ(opStr, CONTEXT_TRIGGER_GREATER_THAN_OR_EQUAL_TO)) + return OpType::GE; + + if (STR_EQ(opStr, CONTEXT_TRIGGER_LESS_THAN)) + return OpType::LT; + + if (STR_EQ(opStr, CONTEXT_TRIGGER_LESS_THAN_OR_EQUAL_TO)) + return OpType::LE; + + return OpType::UNDEFINED; +} + +static std::string __get_custom_uri(std::string name, std::string provider) +{ + std::string uri("http://"); + uri += provider + "/context/custom/" + name; + return uri; +} + +static const char* __get_pkg_id() +{ + static char pkgId[PKG_ID_LENGTH] = {0}; + + if (strlen(pkgId) > 0) + return pkgId; + + aul_app_get_pkgid_bypid(getpid(), pkgId, PKG_ID_LENGTH); + + if (strlen(pkgId) > 0) + return pkgId; + + return NULL; +} + +static ContextItem* __get_context_item(context_trigger_rule_entry_h entry) +{ + ContextItem* contextItem = NULL; + + if (entry->category == CATEGORY_EVENT) + contextItem = new(std::nothrow) EventItem(entry->type); + else + contextItem = new(std::nothrow) ConditionItem(entry->type); + + if (!contextItem) + _E_ALLOC; + + return contextItem; +} + +static Scheduler __scheduler; + + EXPORT_API int context_trigger_add_rule(context_trigger_rule_h rule, int* rule_id) { return CONTEXT_TRIGGER_ERROR_NOT_SUPPORTED;