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.
19 #include <types_internal.h>
20 #include <scope_mutex.h>
21 #include <timer_mgr.h>
24 #include "timer_types.h"
29 using namespace ctx::timer_manager;
30 static GMutex timer_mutex;
32 ctx::trigger_timer::ref_count_array_s::ref_count_array_s()
34 memset(count, 0, sizeof(int) * MAX_DAY);
37 ctx::trigger_timer::trigger_timer(ctx::context_trigger* tr, std::string z)
43 ctx::trigger_timer::~trigger_timer()
48 int ctx::trigger_timer::merge_day_of_week(int* ref_cnt)
52 for (int d = 0; d < MAX_DAY; ++d) {
54 day_of_week |= (0x01 << d);
61 bool ctx::trigger_timer::add(int minute, int day_of_week)
63 IF_FAIL_RETURN_TAG(minute >=0 && minute < 1440 &&
64 day_of_week > 0 && day_of_week <= timer_manager::EVERYDAY,
65 false, _E, "Invalid parameter");
67 ctx::scope_mutex sm(&timer_mutex);
69 ref_count_array_s &ref = ref_count_map[minute];
71 for (int d = 0; d < MAX_DAY; ++d) {
72 if ((day_of_week & (0x01 << d)) != 0) {
77 return reset_timer(minute);
80 bool ctx::trigger_timer::remove(int minute, int day_of_week)
82 IF_FAIL_RETURN_TAG(minute >=0 && minute < 1440 &&
83 day_of_week > 0 && day_of_week <= timer_manager::EVERYDAY,
84 false, _E, "Invalid parameter");
86 ctx::scope_mutex sm(&timer_mutex);
88 ref_count_array_s &ref = ref_count_map[minute];
90 for (int d = 0; d < MAX_DAY; ++d) {
91 if ((day_of_week & (0x01 << d)) != 0 && ref.count[d] > 0) {
96 return reset_timer(minute);
99 bool ctx::trigger_timer::reset_timer(int minute)
101 int day_of_week = merge_day_of_week(ref_count_map[minute].count);
102 timer_state_s &timer = timer_state_map[minute];
104 if (day_of_week == timer.day_of_week) {
105 /* Necessary timers are already running... */
109 if (day_of_week == 0 && timer.timer_id > 0) {
110 /* Turn off the timer at hour, if it is not necessray anymore. */
111 timer_manager::remove(timer.timer_id);
112 timer_state_map.erase(minute);
113 ref_count_map.erase(minute);
117 if (timer.timer_id > 0) {
118 /* Turn off the current timer, to set a new one. */
119 timer_manager::remove(timer.timer_id);
121 timer.day_of_week = 0;
124 /* Create a new timer, w.r.t. the new day_of_week value. */
126 int m = minute - h * 60;
127 int tid = timer_manager::set_at(h, m, day_of_week, this);
128 IF_FAIL_RETURN_TAG(tid > 0, false, _E, "Timer setting failed");
130 timer.timer_id = tid;
131 timer.day_of_week = day_of_week;
136 void ctx::trigger_timer::clear()
138 ctx::scope_mutex sm(&timer_mutex);
140 for (timer_state_map_t::iterator it = timer_state_map.begin(); it != timer_state_map.end(); ++it) {
141 if (it->second.timer_id > 0) {
142 timer_manager::remove(it->second.timer_id);
146 timer_state_map.clear();
147 ref_count_map.clear();
150 bool ctx::trigger_timer::on_timer_expired(int timer_id, void* user_data)
157 localtime_r(&rawtime, &timeinfo);
159 int hour = timeinfo.tm_hour;
160 int min = timeinfo.tm_min;
161 int day_of_week = (0x01 << timeinfo.tm_wday);
163 on_timer_expired(hour, min, day_of_week);
168 void ctx::trigger_timer::on_timer_expired(int hour, int min, int day_of_week)
170 _I("[Timer-%s] Time: %02d:%02d, Day of Week: %#x", zone.c_str(), hour, min, day_of_week);
173 result.set(NULL, TIMER_RESPONSE_KEY_TIME_OF_DAY, hour * 60 + min);
174 result.set(NULL, TIMER_RESPONSE_KEY_DAY_OF_WEEK, convert_day_of_week_to_string(day_of_week));
176 ctx::json dummy = NULL;
177 trigger->push_fact(TIMER_EVENT_REQ_ID, ERR_NONE, TIMER_EVENT_SUBJECT, dummy, result, zone.c_str());
180 int ctx::trigger_timer::get_day_of_month()
187 localtime_r(&rawtime, &timeinfo);
189 int day_of_month = timeinfo.tm_mday;
194 std::string ctx::trigger_timer::get_day_of_week()
201 localtime_r(&rawtime, &timeinfo);
203 int day_of_week = (0x01 << timeinfo.tm_wday);
205 return convert_day_of_week_to_string(day_of_week);
208 int ctx::trigger_timer::get_minute_of_day()
215 localtime_r(&rawtime, &timeinfo);
217 int hour = timeinfo.tm_hour;
218 int minute = timeinfo.tm_min;
220 return hour * 60 + minute;
223 int ctx::trigger_timer::convert_string_to_day_of_week(std::string d)
226 d = d.substr(1, d.length() - 2);
228 if (d.compare(TIMER_SUN) == 0) {
230 } else if (d.compare(TIMER_MON) == 0) {
232 } else if (d.compare(TIMER_TUE) == 0) {
234 } else if (d.compare(TIMER_WED) == 0) {
236 } else if (d.compare(TIMER_THU) == 0) {
238 } else if (d.compare(TIMER_FRI) == 0) {
240 } else if (d.compare(TIMER_SAT) == 0) {
242 } else if (d.compare(TIMER_WEEKDAY) == 0) {
244 } else if (d.compare(TIMER_WEEKEND) == 0) {
246 } else if (d.compare(TIMER_EVERYDAY) == 0) {
253 std::string ctx::trigger_timer::convert_day_of_week_to_string(int d)
259 } else if (d == MON) {
261 } else if (d == TUE) {
263 } else if (d == WED) {
265 } else if (d == THU) {
267 } else if (d == FRI) {
269 } else if (d == SAT) {
271 } else if (d == WEEKDAY) {
273 } else if (d == WEEKEND) {
275 } else if (d == EVERYDAY) {
276 day = TIMER_EVERYDAY;
282 bool ctx::trigger_timer::empty()
284 return timer_state_map.empty();