trigger: define API handle structures and static util functions 93/142193/3
authorMu-Woong Lee <muwoong.lee@samsung.com>
Thu, 3 Aug 2017 04:12:19 +0000 (13:12 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Thu, 3 Aug 2017 07:31:42 +0000 (16:31 +0900)
Change-Id: I6c3b6ebdfff1e2cf3024b145601e8455783944b4
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/trigger/context_trigger.cpp

index a6ad4da..512510a 100644 (file)
  * limitations under the License.
  */
 
+#include <string>
+#include <vector>
+#include <set>
+#include <app_control_internal.h>
+#include <aul.h>
+#include <pkgmgr-info.h>
+
+#include <ContextTypes.h>
+#include <SharedUtil.h>
+#include <ScopeMutex.h>
+#include <job_scheduler_internal.h>
 #include <context_trigger.h>
 #include <context_trigger_internal.h>
 
+#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<std::string> 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<std::string> 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;