2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <types_internal.h>
18 #include <context_trigger_types_internal.h>
19 #include "fact_reader.h"
21 #include "rule_manager.h"
23 ctx::context_trigger::context_trigger()
28 ctx::context_trigger::~context_trigger()
32 bool ctx::context_trigger::init(ctx::context_manager_impl* mgr)
34 // Do the necessary initialization process.
35 // This function is called from the main thread during the service launching process.
36 _reader = new(std::nothrow) fact_reader(mgr, this);
37 IF_FAIL_RETURN_TAG(_reader, false, _E, "Memory allocation failed");
39 _D("Starting Context Trigger Thread");
40 IF_FAIL_RETURN(start(), false);
42 push_thread_event(ETYPE_INITIALIZE, NULL);
47 void ctx::context_trigger::release()
49 _D("Stopping Context Trigger Thread");
52 // Release the occupied resources.
53 // This function is called from the main thread during the service termination process.
58 void ctx::context_trigger::on_thread_event_popped(int type, void* data)
63 process_request(static_cast<request_info*>(data));
67 process_fact(static_cast<context_fact*>(data));
69 case ETYPE_INITIALIZE:
73 _W("Unknown event type");
77 delete_thread_event(type, data);
80 void ctx::context_trigger::delete_thread_event(int type, void* data)
87 std::string subject = static_cast<ctx::request_info*>(data)->get_subject();
88 if (subject != CONTEXT_TRIGGER_SUBJECT_ENABLE) {
89 delete (static_cast<request_info*>(data));
94 delete (static_cast<context_fact*>(data));
96 case ETYPE_INITIALIZE:
99 _W("Unknown event type");
104 bool ctx::context_trigger::assign_request(ctx::request_info* request)
106 std::string subject = request->get_subject();
107 if (subject != CONTEXT_TRIGGER_SUBJECT_ADD && subject != CONTEXT_TRIGGER_SUBJECT_REMOVE &&
108 subject != CONTEXT_TRIGGER_SUBJECT_ENABLE && subject != CONTEXT_TRIGGER_SUBJECT_DISABLE &&
109 subject != CONTEXT_TRIGGER_SUBJECT_GET && subject != CONTEXT_TRIGGER_SUBJECT_GET_RULE_IDS) {
113 push_thread_event(ETYPE_REQUEST, request);
117 void ctx::context_trigger::process_request(ctx::request_info* request)
119 // Process the request, and reply to the client if necessary.
120 const char* req_sbj = request->get_subject();
121 _D("Request is %s", req_sbj);
122 std::string subject(req_sbj);
124 if (subject == CONTEXT_TRIGGER_SUBJECT_ADD) {
126 } else if (subject == CONTEXT_TRIGGER_SUBJECT_REMOVE) {
127 remove_rule(request);
128 } else if (subject == CONTEXT_TRIGGER_SUBJECT_ENABLE) {
129 enable_rule(request);
130 } else if (subject == CONTEXT_TRIGGER_SUBJECT_DISABLE) {
131 disable_rule(request);
132 } else if (subject == CONTEXT_TRIGGER_SUBJECT_GET) {
133 get_rule_by_id(request);
134 } else if (subject == CONTEXT_TRIGGER_SUBJECT_GET_RULE_IDS) {
135 get_rule_ids(request);
137 _E("Invalid request");
141 void ctx::context_trigger::push_fact(int req_id, int error, const char* subject, ctx::json& option, ctx::json& data, const char* zone)
143 context_fact *fact = new(std::nothrow) context_fact(req_id, error, subject, option, data, zone);
144 IF_FAIL_VOID_TAG(fact, _E, "Memory allocation failed");
146 push_thread_event(ETYPE_FACT, fact);
149 void ctx::context_trigger::process_fact(ctx::context_fact* fact)
151 // Process the context fact.
152 rule_mgr->on_event_received(fact->get_subject(), fact->get_option(), fact->get_data(), fact->get_zone_name());
155 void ctx::context_trigger::process_initialize(void)
157 rule_mgr = new(std::nothrow) rule_manager();
158 IF_FAIL_VOID_TAG(rule_mgr, _E, "Memory allocation failed");
160 bool ret = rule_mgr->init(this, _reader);
162 _E("Context trigger initialization failed.");
167 void ctx::context_trigger::add_rule(ctx::request_info* request)
171 const char* app_id = request->get_app_id();
172 if (app_id == NULL) {
173 request->reply(ERR_OPERATION_FAILED);
177 const char* zone_name = request->get_zone_name();
178 if (zone_name == NULL) {
179 request->reply(ERR_OPERATION_FAILED);
183 int error = rule_mgr->add_rule(app_id, request->get_description(), zone_name, &rule_id);
184 _I("'%s' adds a rule (Error: %#x)", request->get_client(), error);
186 request->reply(error, rule_id);
189 void ctx::context_trigger::remove_rule(ctx::request_info* request)
194 const char* app_id = request->get_app_id();
195 if (app_id == NULL) {
196 request->reply(ERR_OPERATION_FAILED);
200 ctx::json rule_id = request->get_description();
201 rule_id.get(NULL, CT_RULE_ID, &id);
203 error = rule_mgr->check_rule(app_id, id);
204 if (error != ERR_NONE) {
205 request->reply(error);
209 bool ret = rule_mgr->is_rule_enabled(id);
211 request->reply(ERR_RULE_ENABLED);
215 error = rule_mgr->remove_rule(id);
216 _I("'%s' removes rule%d (Error: %#x)", request->get_client(), id, error);
217 request->reply(error);
220 void ctx::context_trigger::enable_rule(ctx::request_info* request)
225 const char* app_id = request->get_app_id();
226 if (app_id == NULL) {
227 request->reply(ERR_OPERATION_FAILED);
231 ctx::json rule_id = request->get_description();
232 rule_id.get(NULL, CT_RULE_ID, &id);
234 error = rule_mgr->check_rule(app_id, id);
235 if (error != ERR_NONE) {
236 request->reply(error);
240 bool ret = rule_mgr->is_rule_enabled(id);
242 request->reply(ERR_RULE_ENABLED);
246 error = rule_mgr->enable_rule(id);
247 _I("'%s' enables rule%d (Error: %#x)", request->get_client(), id, error);
248 request->reply(error);
251 void ctx::context_trigger::disable_rule(ctx::request_info* request)
256 const char* app_id = request->get_app_id();
257 if (app_id == NULL) {
258 request->reply(ERR_OPERATION_FAILED);
262 ctx::json rule_id = request->get_description();
263 rule_id.get(NULL, CT_RULE_ID, &id);
265 error = rule_mgr->check_rule(app_id, id);
266 if (error != ERR_NONE) {
267 request->reply(error);
271 bool ret = rule_mgr->is_rule_enabled(id);
273 request->reply(ERR_RULE_NOT_ENABLED);
277 error = rule_mgr->disable_rule(id);
278 _I("'%s' disables rule%d (Error: %#x)", request->get_client(), id, error);
279 request->reply(error);
282 void ctx::context_trigger::get_rule_by_id(ctx::request_info* request)
286 ctx::json option = request->get_description();
288 option.get(NULL, CT_RULE_ID, &id);
290 const char* app_id = request->get_app_id();
291 if (app_id == NULL) {
292 request->reply(ERR_OPERATION_FAILED);
297 error = rule_mgr->get_rule_by_id(app_id, id, &read_data);
300 request->reply(error, dummy, read_data);
303 void ctx::context_trigger::get_rule_ids(ctx::request_info* request)
307 const char* app_id = request->get_app_id();
308 if (app_id == NULL) {
309 request->reply(ERR_OPERATION_FAILED);
314 error = rule_mgr->get_rule_ids(app_id, &read_data);
317 request->reply(error, dummy, read_data);