Store the dbus connection while launching the service
[platform/core/context/context-service.git] / src / agent / legacy / trigger / Trigger.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <Types.h>
18 #include <TriggerTypes.h>
19 #include "Trigger.h"
20 #include "ContextMonitor.h"
21 #include "TemplateManager.h"
22 #include "RuleManager.h"
23
24 using namespace ctx;
25 using namespace ctx::trigger;
26
27 Trigger::Trigger() :
28         __ruleMgr(NULL)
29 {
30 }
31
32 Trigger::~Trigger()
33 {
34 }
35
36 bool Trigger::init(ContextManager* ctxMgr)
37 {
38         // Do the necessary initialization process.
39         // This function is called from the main thread during the service launching process.
40         _D("Context Trigger Init");
41         __processInitialize(ctxMgr);
42
43         return true;
44 }
45
46 void Trigger::release()
47 {
48         // Release the occupied resources.
49         // This function is called from the main thread during the service termination process.
50
51         _D("Template Manager Destroy");
52         TemplateManager::destroy();
53
54         _D("Rule Manager Release");
55         delete __ruleMgr;
56         __ruleMgr = NULL;
57
58         _D("Context Monitor Destroy");
59         ContextMonitor::destroy();
60 }
61
62 bool Trigger::assignRequest(RequestInfo* request)
63 {
64         std::string subject = request->getSubject();
65         if (subject != SUBJ_TRIGGER_ADD && subject != SUBJ_TRIGGER_REMOVE &&
66                         subject != SUBJ_TRIGGER_ENABLE && subject != SUBJ_TRIGGER_DISABLE       &&
67                         subject != SUBJ_TRIGGER_GET && subject != SUBJ_TRIGGER_GET_RULE_IDS &&
68                         subject != SUBJ_TRIGGER_GET_TEMPLATE) {
69                 return false;
70         }
71
72         __processRequest(request);
73         return true;
74 }
75
76 void Trigger::__processRequest(RequestInfo* request)
77 {
78         // Process the request, and reply to the client if necessary.
79         const char* reqSubj = request->getSubject();
80         _D("Request is %s", reqSubj);
81         std::string subject(reqSubj);
82
83         if (subject == SUBJ_TRIGGER_ADD) {
84                 __addRule(request);
85         } else if (subject == SUBJ_TRIGGER_REMOVE) {
86                 __removeRule(request);
87         } else if (subject == SUBJ_TRIGGER_ENABLE) {
88                 __enableRule(request);
89         } else if (subject == SUBJ_TRIGGER_DISABLE) {
90                 __disableRule(request);
91         } else if (subject == SUBJ_TRIGGER_GET) {
92                 __getRuleById(request);
93         } else if (subject == SUBJ_TRIGGER_GET_RULE_IDS) {
94                 __getRuleIds(request);
95         } else if (subject == SUBJ_TRIGGER_GET_TEMPLATE) {
96                 __getTemplate(request);
97         } else {
98                 _E("Invalid request");
99         }
100
101         delete request;
102 }
103
104 void Trigger::__processInitialize(ContextManager* mgr)
105 {
106         // Context Monitor
107         ContextMonitor::setContextManager(mgr);
108
109         // Rule Manager
110         __ruleMgr = new(std::nothrow) RuleManager();
111         IF_FAIL_VOID_TAG(__ruleMgr, _E, "Memory allocation failed");
112
113         // Template Manager
114         TemplateManager::setManager(mgr, __ruleMgr);
115         TemplateManager* tmplMgr = TemplateManager::getInstance();
116         IF_FAIL_VOID_TAG(tmplMgr, _E, "Memory allocation failed");
117
118         // Initialization
119         if (!tmplMgr->init()) {
120                 _E("Template manager initialization failed");
121                 raise(SIGTERM);
122         }
123
124         if (!__ruleMgr->init()) {
125                 _E("Context trigger initialization failed");
126                 raise(SIGTERM);
127         }
128 }
129
130 void Trigger::__addRule(RequestInfo* request)
131 {
132         CtxJson1 ruleId;
133
134         const char* client = request->getClient();
135         if (client == NULL) {
136                 request->reply(ERR_OPERATION_FAILED);
137                 return;
138         }
139
140         const char* pkgId = request->getPackageId();
141
142         int error = __ruleMgr->addRule(client, pkgId, request->getDescription(), &ruleId);
143         _I("'%s' adds a rule (Error: %#x)", request->getClient(), error);
144
145         request->reply(error, ruleId);
146 }
147
148 void Trigger::__removeRule(RequestInfo* request)
149 {
150         int id;
151         int error;
152
153         const char* pkgId = request->getPackageId();
154
155         CtxJson1 ruleId = request->getDescription();
156         ruleId.get(NULL, TRIG_KEY_RULE_ID, &id);
157
158         error = __ruleMgr->checkRule((pkgId)? pkgId : "", id);
159         if (error != ERR_NONE) {
160                 request->reply(error);
161                 return;
162         }
163
164         bool ret = __ruleMgr->isRuleEnabled(id);
165         if (ret) {
166                 request->reply(ERR_RULE_ENABLED);
167                 return;
168         }
169
170         error = __ruleMgr->removeRule(id);
171         _I("'%s' removes rule%d (Error: %#x)", request->getClient(), id, error);
172         request->reply(error);
173 }
174
175 void Trigger::__enableRule(RequestInfo* request)
176 {
177         int id;
178         int error;
179
180         const char* pkgId = request->getPackageId();
181
182         CtxJson1 ruleId = request->getDescription();
183         ruleId.get(NULL, TRIG_KEY_RULE_ID, &id);
184
185         error = __ruleMgr->checkRule((pkgId)? pkgId : "", id);
186         if (error != ERR_NONE) {
187                 request->reply(error);
188                 return;
189         }
190
191         bool ret = __ruleMgr->isRuleEnabled(id);
192         if (ret) {
193                 request->reply(ERR_RULE_ENABLED);
194                 return;
195         }
196
197         error = __ruleMgr->enableRule(id);
198         _I("'%s' enables rule%d (Error: %#x)", request->getClient(), id, error);
199         request->reply(error);
200 }
201
202 void Trigger::__disableRule(RequestInfo* request)
203 {
204         int id;
205         int error;
206
207         const char* pkgId = request->getPackageId();
208
209         CtxJson1 ruleId = request->getDescription();
210         ruleId.get(NULL, TRIG_KEY_RULE_ID, &id);
211
212         error = __ruleMgr->checkRule((pkgId)? pkgId : "", id);
213         if (error != ERR_NONE) {
214                 request->reply(error);
215                 return;
216         }
217
218         bool ret = __ruleMgr->isRuleEnabled(id);
219         if (!ret) {
220                 request->reply(ERR_RULE_NOT_ENABLED);
221                 return;
222         }
223
224         error = __ruleMgr->disableRule(id);
225         _I("'%s' disables rule%d (Error: %#x)", request->getClient(), id, error);
226         request->reply(error);
227 }
228
229 void Trigger::__getRuleById(RequestInfo* request)
230 {
231         int error;
232
233         CtxJson1 option = request->getDescription();
234         int id;
235         option.get(NULL, TRIG_KEY_RULE_ID, &id);
236
237         const char* pkgId = request->getPackageId();
238
239         CtxJson1 readData;
240         error = __ruleMgr->getRuleById((pkgId)? pkgId : "", id, &readData);
241
242         CtxJson1 dummy;
243         request->reply(error, dummy, readData);
244 }
245
246 void Trigger::__getRuleIds(RequestInfo* request)
247 {
248         int error;
249
250         const char* pkgId = request->getPackageId();
251
252         CtxJson1 readData;
253         error = __ruleMgr->getRuleIds((pkgId)? pkgId : "", &readData);
254
255         CtxJson1 dummy;
256         request->reply(error, dummy, readData);
257 }
258
259 void Trigger::__getTemplate(RequestInfo* request)
260 {
261         int error;
262
263         CtxJson1 option = request->getDescription();
264         std::string name;
265         option.get(NULL, TRIG_TMPL_KEY_SUBJECT, &name);
266
267         TemplateManager* tmplMgr = TemplateManager::getInstance();
268         if (!tmplMgr) {
269                 _E("Memory allocation failed");
270                 request->reply(ERR_OUT_OF_MEMORY);
271                 return;
272         }
273
274         CtxJson1 tmpl;
275         error = tmplMgr->getTemplate(name, &tmpl);
276
277         CtxJson1 dummy;
278         request->reply(error, dummy, tmpl);
279 }