4 * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 /******************************************************************************
24 * File : email-core-alarm.c
25 * Desc : Alarm Management
30 * 2013.01.28 : created
31 *****************************************************************************/
36 #include <sys/types.h>
41 #include "email-convert.h"
42 #include "email-types.h"
43 #include "email-daemon.h"
44 #include "email-debug-log.h"
45 #include "email-storage.h"
46 #include "email-network.h"
47 #include "email-device.h"
48 #include "email-utilities.h"
49 #include "email-core-utils.h"
50 #include "email-core-global.h"
51 #include "email-core-alarm.h"
54 INTERNAL_FUNC GList *alarm_data_list = NULL;
55 static pthread_mutex_t alarm_data_lock = PTHREAD_MUTEX_INITIALIZER;
57 INTERNAL_FUNC int emcore_get_alarm_data_by_alarm_id(alarm_id_t input_alarm_id, email_alarm_data_t **output_alarm_data)
59 EM_DEBUG_FUNC_BEGIN("input_alarm_id [%d] output_alarm_data [%p]", input_alarm_id, output_alarm_data);
60 int err = EMAIL_ERROR_NONE;
62 email_alarm_data_t *alarm_data = NULL;
64 if (output_alarm_data == NULL) {
65 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
66 err = EMAIL_ERROR_INVALID_PARAM;
70 ENTER_CRITICAL_SECTION(alarm_data_lock);
71 index = g_list_first(alarm_data_list);
73 alarm_data = index->data;
74 if (alarm_data->alarm_id == input_alarm_id) {
77 index = g_list_next(index);
79 LEAVE_CRITICAL_SECTION(alarm_data_lock);
82 *output_alarm_data = alarm_data;
84 err = EMAIL_ERROR_ALARM_DATA_NOT_FOUND;
88 EM_DEBUG_FUNC_END("err [%d]", err);
92 INTERNAL_FUNC int emcore_get_alarm_data_by_reference_id(email_alarm_class_t input_class_id, int input_reference_id, email_alarm_data_t **output_alarm_data)
94 EM_DEBUG_FUNC_BEGIN("input_class_id [%d] input_reference_id [%d] output_alarm_data [%p]", input_class_id, input_reference_id, output_alarm_data);
95 int err = EMAIL_ERROR_NONE;
97 email_alarm_data_t *alarm_data = NULL;
99 if (output_alarm_data == NULL) {
100 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
101 err = EMAIL_ERROR_INVALID_PARAM;
105 ENTER_CRITICAL_SECTION(alarm_data_lock);
107 index = g_list_first(alarm_data_list);
110 alarm_data = index->data;
111 if (alarm_data->class_id == input_class_id && alarm_data->reference_id == input_reference_id) {
112 EM_DEBUG_LOG("found");
115 index = g_list_next(index);
118 LEAVE_CRITICAL_SECTION(alarm_data_lock);
121 *output_alarm_data = alarm_data;
123 err = EMAIL_ERROR_ALARM_DATA_NOT_FOUND;
127 EM_DEBUG_FUNC_END("err [%d]", err);
131 INTERNAL_FUNC int emcore_check_alarm_by_class_id(email_alarm_class_t input_class_id)
133 EM_DEBUG_FUNC_BEGIN("input_class_id [%d]", input_class_id);
134 int err = EMAIL_ERROR_NONE;
136 email_alarm_data_t *alarm_data = NULL;
138 ENTER_CRITICAL_SECTION(alarm_data_lock);
139 index = g_list_first(alarm_data_list);
141 alarm_data = index->data;
142 if (alarm_data->class_id == input_class_id) {
143 EM_DEBUG_LOG("found");
146 index = g_list_next(index);
148 LEAVE_CRITICAL_SECTION(alarm_data_lock);
151 err = EMAIL_ERROR_ALARM_DATA_NOT_FOUND;
153 EM_DEBUG_FUNC_END("err [%d]", err);
157 static int emcore_add_alarm_data_to_alarm_data_list(char *multi_user_name,
158 alarm_id_t input_alarm_id,
159 email_alarm_class_t input_class_id,
160 int input_reference_id,
161 time_t input_trigger_at_time,
162 int (*input_alarm_callback)(email_alarm_data_t*, void *),
165 EM_DEBUG_FUNC_BEGIN("input_alarm_id [%d] input_class_id[%d] input_reference_id[%d] input_trigger_at_time[%d] input_alarm_callback [%p] user_data[%p]", input_alarm_id, input_class_id, input_reference_id, input_trigger_at_time, input_alarm_callback, user_data);
166 int err = EMAIL_ERROR_NONE;
167 email_alarm_data_t *alarm_data = NULL;
169 if (input_alarm_id == 0 || input_trigger_at_time == 0 || input_alarm_callback == NULL) {
170 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
171 err = EMAIL_ERROR_INVALID_PARAM;
175 alarm_data = em_malloc(sizeof(email_alarm_data_t));
177 if (alarm_data == NULL) {
178 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
179 err = EMAIL_ERROR_OUT_OF_MEMORY;
183 alarm_data->alarm_id = input_alarm_id;
184 alarm_data->class_id = input_class_id;
185 alarm_data->reference_id = input_reference_id;
186 alarm_data->trigger_at_time = input_trigger_at_time;
187 alarm_data->alarm_callback = input_alarm_callback;
188 alarm_data->user_data = user_data;
189 alarm_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
191 ENTER_CRITICAL_SECTION(alarm_data_lock);
192 alarm_data_list = g_list_append(alarm_data_list, (gpointer)alarm_data);
193 LEAVE_CRITICAL_SECTION(alarm_data_lock);
197 EM_DEBUG_FUNC_END("err [%d]", err);
201 static int emcore_remove_alarm(email_alarm_data_t *input_alarm_data)
203 EM_DEBUG_FUNC_BEGIN("input_alarm_data [%p]", input_alarm_data);
205 int ret = ALARMMGR_RESULT_SUCCESS;
206 int err = EMAIL_ERROR_NONE;
208 ENTER_CRITICAL_SECTION(alarm_data_lock);
209 EM_DEBUG_LOG("alarm_id [%d]", input_alarm_data->alarm_id);
210 if ((ret = alarmmgr_remove_alarm(input_alarm_data->alarm_id)) != ALARMMGR_RESULT_SUCCESS) {
211 EM_DEBUG_EXCEPTION("alarmmgr_remove_alarm failed [%d]", ret);
212 err = EMAIL_ERROR_SYSTEM_FAILURE;
214 EM_SAFE_FREE(input_alarm_data->multi_user_name);
215 LEAVE_CRITICAL_SECTION(alarm_data_lock);
217 EM_DEBUG_FUNC_END("err [%d]", err);
221 INTERNAL_FUNC int emcore_delete_alram_data_from_alarm_data_list(email_alarm_data_t *input_alarm_data)
223 EM_DEBUG_FUNC_BEGIN("input_alarm_data [%p]", input_alarm_data);
224 int err = EMAIL_ERROR_NONE;
226 if (input_alarm_data == NULL) {
227 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
228 err = EMAIL_ERROR_INVALID_PARAM;
232 if ((err = emcore_remove_alarm(input_alarm_data)) != EMAIL_ERROR_NONE) {
233 EM_DEBUG_EXCEPTION("emcore_remove_alarm failed [%d]", err);
236 ENTER_CRITICAL_SECTION(alarm_data_lock);
237 alarm_data_list = g_list_remove_all(alarm_data_list, input_alarm_data);
239 if (g_list_length(alarm_data_list) == 0) {
240 g_list_free(alarm_data_list);
241 alarm_data_list = NULL;
243 LEAVE_CRITICAL_SECTION(alarm_data_lock);
247 EM_DEBUG_FUNC_END("err [%d]", err);
251 INTERNAL_FUNC int emcore_delete_alram_data_by_reference_id(email_alarm_class_t input_class_id, int input_reference_id)
253 EM_DEBUG_FUNC_BEGIN("input_class_id[%d] input_reference_id[%d]", input_class_id, input_reference_id);
254 int err = EMAIL_ERROR_NONE;
255 email_alarm_data_t *alarm_data = NULL;
257 if ((err = emcore_get_alarm_data_by_reference_id(input_class_id, input_reference_id, &alarm_data)) != EMAIL_ERROR_NONE) {
258 EM_DEBUG_LOG("emcore_get_alarm_data_by_reference_id return [%d]", err);
262 if ((err = emcore_delete_alram_data_from_alarm_data_list(alarm_data)) != EMAIL_ERROR_NONE) {
263 EM_DEBUG_LOG("emcore_delete_alram_data_from_alarm_data_list return [%d]", err);
267 EM_DEBUG_FUNC_END("err [%d]", err);
271 INTERNAL_FUNC int emcore_add_alarm(char *multi_user_name,
272 time_t input_trigger_at_time,
273 email_alarm_class_t input_class_id,
274 int input_reference_id,
275 int (*input_alarm_callback)(email_alarm_data_t*, void *),
276 void *input_user_data)
278 EM_DEBUG_FUNC_BEGIN("input_trigger_at_time[%d] input_class_id[%d] input_reference_id[%d] "
279 "input_alarm_callback[%p] input_user_data[%p]",
280 input_trigger_at_time, input_class_id, input_reference_id,
281 input_alarm_callback, input_user_data);
283 int err = EMAIL_ERROR_NONE;
285 time_t alarm_interval = 0;
286 time_t current_time = 0;
287 alarm_id_t alarm_id = 0;
291 alarm_interval = input_trigger_at_time - current_time;
293 EM_DEBUG_ALARM_LOG("alarm_interval [%d]", (int)alarm_interval);
295 if ((ret = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE,
297 ALARM_REPEAT_MODE_ONCE,
298 EMAIL_ALARM_DESTINATION,
299 &alarm_id)) != ALARMMGR_RESULT_SUCCESS) {
300 EM_DEBUG_EXCEPTION("alarmmgr_add_alarm failed [%d]", ret);
301 err = EMAIL_ERROR_SYSTEM_FAILURE;
305 if ((err = emcore_add_alarm_data_to_alarm_data_list(multi_user_name,
309 input_trigger_at_time,
310 input_alarm_callback,
311 input_user_data)) != EMAIL_ERROR_NONE) {
312 EM_DEBUG_EXCEPTION("emcore_add_alarm_data_to_alarm_data_list failed [%d]", err);
318 EM_DEBUG_FUNC_END("err [%d]", err);