2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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.
23 #include <app_alarm.h>
24 #include <app_control_internal.h>
30 #define LOG_TAG "CAPI_APPFW_ALARM"
33 alarm_registered_alarm_cb cb;
36 } alarm_foreach_item_cb_context;
38 static int alarm_registered_alarm_cb_broker(int alarm_id, void *user_data)
40 alarm_foreach_item_cb_context* foreach_cb_context = NULL;
42 if (user_data == NULL)
47 foreach_cb_context = (alarm_foreach_item_cb_context*)user_data;
49 if (foreach_cb_context != NULL && *(foreach_cb_context->foreach_break) == false)
51 if (foreach_cb_context->cb(alarm_id, foreach_cb_context->user_data) == false)
53 *(foreach_cb_context->foreach_break) = true;
60 static int convert_error_code_to_alarm(const char* function, alarm_error_t alarm_error)
64 case ERR_ALARM_INVALID_PARAM:
65 case ERR_ALARM_INVALID_REPEAT:
66 LOGE("[%s] INVALID_PARAMETER(0x%08x)", function, ALARM_ERROR_INVALID_PARAMETER);
67 return ALARM_ERROR_INVALID_PARAMETER;
70 case ERR_ALARM_INVALID_ID:
71 LOGE("[%s] INVALID_PARAMETER(0x%08x)", function, ALARM_ERROR_INVALID_PARAMETER);
72 return ALARM_ERROR_INVALID_PARAMETER;
75 case ERR_ALARM_INVALID_TIME:
76 LOGE("[%s] INVALID_TIME(0x%08x)", function, ALARM_ERROR_INVALID_TIME);
77 return ALARM_ERROR_INVALID_TIME;
80 case ERR_ALARM_INVALID_DATE:
81 LOGE("[%s] INVALID_DATE(0x%08x)", function, ALARM_ERROR_INVALID_DATE);
82 return ALARM_ERROR_INVALID_DATE;
85 case ERR_ALARM_NO_SERVICE_NAME:
86 LOGE("[%s] INVALID_PARAMETER(0x%08x)", function, ALARM_ERROR_INVALID_PARAMETER);
87 return ALARM_ERROR_INVALID_PARAMETER;
90 case ERR_ALARM_SYSTEM_FAIL:
91 LOGE("[%s] CONNECTION_FAIL(0x%08x)", function, ALARM_ERROR_CONNECTION_FAIL);
92 return ALARM_ERROR_CONNECTION_FAIL;
95 case ERR_ALARM_NO_PERMISSION:
96 LOGE("[%s] PERMISSION_DENIED(0x%08x)", function, ALARM_ERROR_PERMISSION_DENIED);
97 return ALARM_ERROR_PERMISSION_DENIED;
100 case ALARMMGR_RESULT_SUCCESS:
101 return ALARM_ERROR_NONE;
105 return ALARM_ERROR_INVALID_PARAMETER;
109 int alarm_get_scheduled_date(int alarm_id, struct tm* date)
111 alarm_error_t result;
116 LOGE("INVALID_PARAMETER(0x%08x)", ALARM_ERROR_INVALID_PARAMETER);
117 return ALARM_ERROR_INVALID_PARAMETER;
120 result = alarmmgr_get_next_duetime(alarm_id, &due_time);
121 if (result != ALARMMGR_RESULT_SUCCESS)
123 return convert_error_code_to_alarm(__FUNCTION__, result);
126 localtime_r(&due_time, date);
128 return ALARM_ERROR_NONE;
131 int alarm_get_scheduled_period(int alarm_id, int* period)
133 alarm_error_t result;
134 alarm_entry_t *entry = NULL;
135 alarm_repeat_mode_t mode;
140 LOGE("INVALID_PARAMETER(0x%08x)", ALARM_ERROR_INVALID_PARAMETER);
141 return ALARM_ERROR_INVALID_PARAMETER;
144 entry = alarmmgr_create_alarm();
146 result = alarmmgr_get_info(alarm_id, entry);
147 if (result != ALARMMGR_RESULT_SUCCESS)
151 alarmmgr_free_alarm(entry);
153 return convert_error_code_to_alarm(__FUNCTION__, result);
156 result = alarmmgr_get_repeat_mode(entry, &mode, &value);
157 if (result != ALARMMGR_RESULT_SUCCESS)
161 alarmmgr_free_alarm(entry);
163 return convert_error_code_to_alarm(__FUNCTION__, result);
166 result = alarmmgr_free_alarm(entry);
167 if(result != ALARMMGR_RESULT_SUCCESS)
171 alarmmgr_free_alarm(entry);
173 return convert_error_code_to_alarm(__FUNCTION__, result);
178 return ALARM_ERROR_NONE;
181 int alarm_schedule_after_delay(app_control_h app_control, int delay, int period, int *alarm_id)
186 if (app_control == NULL)
188 LOGE("INVALID_PARAMETER(0x%08x)", ALARM_ERROR_INVALID_PARAMETER);
189 return ALARM_ERROR_INVALID_PARAMETER;
192 if (app_control_to_bundle(app_control, &bundle_data) != APP_CONTROL_ERROR_NONE)
194 LOGE("INVALID_PARAMETER(0x%08x)", ALARM_ERROR_INVALID_PARAMETER);
195 return ALARM_ERROR_INVALID_PARAMETER;
198 result = alarmmgr_add_alarm_appsvc(ALARM_TYPE_DEFAULT, delay, period, bundle_data, alarm_id);
200 return convert_error_code_to_alarm(__FUNCTION__, result);
203 int alarm_schedule_at_date(app_control_h app_control, struct tm *date, int period_in_second, int *alarm_id)
205 alarm_date_t internal_time;
206 alarm_entry_t* alarm_info;
210 if (app_control == NULL || date == NULL)
212 LOGE("INVALID_PARAMETER(0x%08x)", ALARM_ERROR_INVALID_PARAMETER);
213 return ALARM_ERROR_INVALID_PARAMETER;
216 if (app_control_to_bundle(app_control, &bundle_data) != APP_CONTROL_ERROR_NONE)
218 LOGE("INVALID_PARAMETER(0x%08x)", ALARM_ERROR_INVALID_PARAMETER);
219 return ALARM_ERROR_INVALID_PARAMETER;
222 alarm_info = alarmmgr_create_alarm();
224 internal_time.year = date->tm_year + 1900;
225 internal_time.month = date->tm_mon +1;
226 internal_time.day = date->tm_mday;
228 internal_time.hour = date->tm_hour;
229 internal_time.min = date->tm_min;
230 internal_time.sec = date->tm_sec;
232 result = alarmmgr_set_time(alarm_info,internal_time);
236 alarmmgr_free_alarm(alarm_info);
237 return convert_error_code_to_alarm(__FUNCTION__, result);
240 if (period_in_second > 0)
242 result = alarmmgr_set_repeat_mode(alarm_info, ALARM_REPEAT_MODE_REPEAT, period_in_second);
246 result = alarmmgr_set_repeat_mode(alarm_info, ALARM_REPEAT_MODE_ONCE, period_in_second);
251 alarmmgr_free_alarm(alarm_info);
252 return convert_error_code_to_alarm(__FUNCTION__, result);
255 result = alarmmgr_set_type(alarm_info, ALARM_TYPE_DEFAULT);
259 alarmmgr_free_alarm(alarm_info);
260 return convert_error_code_to_alarm(__FUNCTION__, result);
263 result = alarmmgr_add_alarm_appsvc_with_localtime(alarm_info, bundle_data, alarm_id);
267 alarmmgr_free_alarm(alarm_info);
268 return convert_error_code_to_alarm(__FUNCTION__, result);
271 alarmmgr_free_alarm(alarm_info);
272 return ALARM_ERROR_NONE;
275 int alarm_cancel(int alarm_id)
279 result = alarmmgr_remove_alarm(alarm_id);
281 return convert_error_code_to_alarm(__FUNCTION__, result);
284 int alarm_cancel_all()
286 LOGD("%s", __func__);
287 int result = alarmmgr_remove_all();
288 return convert_error_code_to_alarm(__FUNCTION__, result);
291 int alarm_foreach_registered_alarm(alarm_registered_alarm_cb callback, void* user_data)
294 bool foreach_break = false;
296 if (callback == NULL)
298 LOGE("INVALID_PARAMETER(0x%08x)", ALARM_ERROR_INVALID_PARAMETER);
299 return ALARM_ERROR_INVALID_PARAMETER;
302 //alarm_registered_alarm_cb_broker
303 alarm_foreach_item_cb_context foreach_cb_context = {
305 .user_data = user_data,
306 .foreach_break = &foreach_break
309 result = alarmmgr_enum_alarm_ids(alarm_registered_alarm_cb_broker, &foreach_cb_context);
311 return convert_error_code_to_alarm(__FUNCTION__, result);
314 int alarm_get_current_time(struct tm* date)
320 LOGE("INVALID_PARAMETER(0x%08x)", ALARM_ERROR_INVALID_PARAMETER);
321 return ALARM_ERROR_INVALID_PARAMETER;
325 localtime_r(&now, date);
326 return ALARM_ERROR_NONE;
330 int alarm_schedule_with_recurrence_week_flag(app_control_h app_control, struct tm *date, int week_flag,int *alarm_id)
332 alarm_date_t internal_time;
333 alarm_entry_t* alarm_info;
337 if (app_control == NULL || date == NULL)
339 LOGE("INVALID_PARAMETER(0x%08x)", ALARM_ERROR_INVALID_PARAMETER);
340 return ALARM_ERROR_INVALID_PARAMETER;
343 if (app_control_to_bundle(app_control, &bundle_data) != APP_CONTROL_ERROR_NONE)
345 LOGE("INVALID_PARAMETER(0x%08x)", ALARM_ERROR_INVALID_PARAMETER);
346 return ALARM_ERROR_INVALID_PARAMETER;
349 alarm_info = alarmmgr_create_alarm();
351 internal_time.year = date->tm_year + 1900;
352 internal_time.month = date->tm_mon +1;
353 internal_time.day = date->tm_mday;
355 internal_time.hour = date->tm_hour;
356 internal_time.min = date->tm_min;
357 internal_time.sec = date->tm_sec;
359 result = alarmmgr_set_time(alarm_info,internal_time);
363 alarmmgr_free_alarm(alarm_info);
364 return convert_error_code_to_alarm(__FUNCTION__, result);
369 result = alarmmgr_set_repeat_mode(alarm_info, ALARM_REPEAT_MODE_WEEKLY, week_flag);
374 alarmmgr_free_alarm(alarm_info);
375 return convert_error_code_to_alarm(__FUNCTION__, result);
378 result = alarmmgr_set_type(alarm_info, ALARM_TYPE_DEFAULT);
382 alarmmgr_free_alarm(alarm_info);
383 return convert_error_code_to_alarm(__FUNCTION__, result);
386 result = alarmmgr_add_alarm_appsvc_with_localtime(alarm_info, bundle_data, alarm_id);
388 alarmmgr_free_alarm(alarm_info);
389 return convert_error_code_to_alarm(__FUNCTION__, result);
392 int alarm_get_scheduled_recurrence_week_flag(int alarm_id, int *week_flag)
394 alarm_error_t result;
395 alarm_entry_t *entry = NULL;
396 alarm_repeat_mode_t mode;
399 if(week_flag == NULL)
401 LOGE("INVALID_PARAMETER(0x%08x)", ALARM_ERROR_INVALID_PARAMETER);
402 return ALARM_ERROR_INVALID_PARAMETER;
405 entry = alarmmgr_create_alarm();
407 result = alarmmgr_get_info(alarm_id, entry);
408 if(result != ALARMMGR_RESULT_SUCCESS)
412 alarmmgr_free_alarm(entry);
414 return convert_error_code_to_alarm(__FUNCTION__, result);
417 result = alarmmgr_get_repeat_mode(entry, &mode, &value);
419 if(mode != ALARM_REPEAT_MODE_WEEKLY)
423 alarmmgr_free_alarm(entry);
425 return ALARM_ERROR_INVALID_PARAMETER;
428 if(result != ALARMMGR_RESULT_SUCCESS)
432 alarmmgr_free_alarm(entry);
434 return convert_error_code_to_alarm(__FUNCTION__, result);
437 result = alarmmgr_free_alarm(entry);
438 if(result != ALARMMGR_RESULT_SUCCESS)
442 alarmmgr_free_alarm(entry);
444 return convert_error_code_to_alarm(__FUNCTION__, result);
447 (*week_flag) = value;
449 return ALARM_ERROR_NONE;
452 int alarm_get_app_control(int alarm_id, app_control_h *app_control)
457 b = alarmmgr_get_alarm_appsvc_info(alarm_id, &error_code);
459 if(error_code != ALARMMGR_RESULT_SUCCESS)
461 return convert_error_code_to_alarm(__FUNCTION__, error_code);
466 return ALARM_ERROR_INVALID_PARAMETER;
469 error_code = app_control_create_request(b, app_control);
471 if(error_code != APP_CONTROL_ERROR_NONE)
473 return ALARM_ERROR_OUT_OF_MEMORY;
478 return ALARM_ERROR_NONE;