4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Venkatesha Sarpangala <sarpangala.v@samsung.com>, Jayoun Lee <airjany@samsung.com>,
7 * Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
34 #include<dbus/dbus-glib.h>
37 #include <dbus/dbus-glib-lowlevel.h>
40 #include "alarm-internal.h"
41 #include "alarm-stub.h"
46 #define MAX_KEY_SIZE 256
48 static alarm_context_t alarm_context = { NULL, NULL, NULL, NULL, -1 };
50 static bool b_initialized = false;
51 static bool sub_initialized = false;
53 #define MAX_OBJECT_PATH_LEN 256
54 #define DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT 0
56 static DBusHandlerResult __expire_alarm_filter(DBusConnection *connection,
59 static int __alarm_validate_date(alarm_date_t *date, int *error_code);
60 static bool __alarm_validate_time(alarm_date_t *date, int *error_code);
61 static int __sub_init(void);
62 static int __alarmmgr_init_appsvc(void);
63 bool alarm_power_off(int *error_code);
64 int alarmmgr_check_next_duetime(void);
66 typedef struct _alarm_cb_info_t {
70 struct _alarm_cb_info_t *next;
73 static alarm_cb_info_t *alarmcb_head = NULL;
75 static void __add_resultcb(int alarm_id, alarm_cb_t cb_func,
78 alarm_cb_info_t *info;
80 info = (alarm_cb_info_t *) malloc(sizeof(alarm_cb_info_t));
83 info->alarm_id = alarm_id;
84 info->cb_func = cb_func;
85 info->priv_data = data;
87 info->next = alarmcb_head;
91 static alarm_cb_info_t *__find_resultcb(int alarm_id)
97 if (tmp->alarm_id == alarm_id)
104 static void __remove_resultcb(alarm_cb_info_t *info)
106 alarm_cb_info_t *tmp;
108 if (alarmcb_head == NULL || info == NULL)
111 if (alarmcb_head == info) {
112 alarmcb_head = info->next;
119 if (tmp->next == info) {
120 tmp->next = info->next;
128 static DBusHandlerResult __expire_alarm_filter(DBusConnection *connection,
129 DBusMessage *message,
132 alarm_cb_info_t *info;
134 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL) {
135 const char *method_name = dbus_message_get_member(message);
138 if (strcmp(method_name, "alarm_expired") == 0) {
139 DBusMessageIter iter;
141 const char *service_name =
142 dbus_message_get_destination(message);
143 const char *object_path =
144 dbus_message_get_path(message);
145 /* "/org/tizen/alarm/client" */
146 const char *interface_name =
147 dbus_message_get_interface(message);
148 /* "org.tizen.alarm.client" */
150 dbus_message_iter_init(message, &iter);
151 dbus_message_iter_get_basic(&iter, &alarm_id);
153 ALARM_MGR_LOG_PRINT("[alarm-lib]:service_name=%s, "
154 "object_path=%s, interface_name=%s, method_name=%s, "
155 "alarm_id=%d, handler=%s\n",
156 service_name ? service_name : "no name",
157 object_path ? object_path : "no path",
158 interface_name ? interface_name : "no interface",
159 method_name ? method_name : "no method", alarm_id,
160 alarm_context.alarm_handler ? "ok" : "no handler");
162 if (alarm_context.alarm_handler != NULL)
163 /* alarm_context.alarm_handler(alarm_id); */
164 alarm_context.alarm_handler(alarm_id,
165 alarm_context.user_param);
166 info = __find_resultcb(alarm_id);
168 if( info && info->cb_func ) {
169 info->cb_func(alarm_id, info->priv_data);
170 // __remove_resultcb(info);
173 return DBUS_HANDLER_RESULT_HANDLED;
177 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
180 static int __alarm_validate_date(alarm_date_t *date, int *error_code)
183 if (date->year == 0 && date->month == 0 && date->day == 0) {
187 int year = date->year;
188 int month = date->month;
191 if (month < 1 || month > 12) {
193 *error_code = ERR_ALARM_INVALID_DATE;
197 if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8
198 || month == 10 || month == 12)
199 && (day < 1 || day > 31)) {
201 *error_code = ERR_ALARM_INVALID_DATE;
205 if ((month == 4 || month == 6 || month == 9 || month == 11)
206 && (day < 1 || day > 30)) {
208 *error_code = ERR_ALARM_INVALID_DATE;
213 if ((year % 100 != 0 && year % 4 == 0) || (year % 400 == 0)) {
214 if (day < 1 || day > 29) {
216 *error_code = ERR_ALARM_INVALID_DATE;
220 if (day < 1 || day > 28) {
222 *error_code = ERR_ALARM_INVALID_DATE;
233 static bool __alarm_validate_time(alarm_date_t *date, int *error_code)
235 if (date->hour < 0 || date->hour > 23) {
237 *error_code = ERR_ALARM_INVALID_TIME;
241 if (date->min < 0 || date->min > 59) {
243 *error_code = ERR_ALARM_INVALID_TIME;
250 static int __sub_init()
252 GError *error = NULL;
254 if (sub_initialized) {
255 //ALARM_MGR_LOG_PRINT("__sub_init was already called.\n");
256 return ALARMMGR_RESULT_SUCCESS;
260 dbus_g_thread_init();
262 alarm_context.bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
263 if (alarm_context.bus == NULL) {
264 ALARM_MGR_EXCEPTION_PRINT("dbus bus get failed\n");
266 return ERR_ALARM_SYSTEM_FAIL;
269 alarm_context.proxy = dbus_g_proxy_new_for_name(alarm_context.bus,
270 "org.tizen.alarm.manager",
271 "/org/tizen/alarm/manager",
272 "org.tizen.alarm.manager");
273 if (alarm_context.proxy == NULL) {
274 ALARM_MGR_EXCEPTION_PRINT("dbus bus proxy get failed\n");
276 return ERR_ALARM_SYSTEM_FAIL;
279 alarm_context.pid = getpid(); /*this running appliction's process id*/
281 sub_initialized = true;
283 return ALARMMGR_RESULT_SUCCESS;
286 bool alarm_power_off(int *error_code)
288 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_power_off() is called\n");
291 return _send_alarm_power_off(alarm_context, error_code);
294 "[alarm-lib]:ALARM_BOOT feature is not supported. "
295 "so we return false.\n");
297 *error_code = -1; /*-1 means that system failed
303 int alarmmgr_check_next_duetime()
307 "[alarm-lib]:alarm_check_next_duetime() is called\n");
310 if (!_send_alarm_check_next_duetime(alarm_context, &error_code))
314 "[alarm-lib]:ALARM_BOOT feature is not supported. "
315 "so we return false.\n");
316 return ERR_ALARM_SYSTEM_FAIL;
319 return ALARMMGR_RESULT_SUCCESS;
322 EXPORT_API int alarmmgr_init(const char *appid)
325 int request_name_result = 0;
326 char service_name[MAX_SERVICE_NAME_LEN] = { 0 };
327 char service_name_mod[MAX_SERVICE_NAME_LEN]= { 0 };
335 return ERR_ALARM_INVALID_PARAM;
337 if (strlen(appid) >= MAX_PKG_NAME_LEN)
338 return ERR_ALARM_INVALID_PARAM;
341 ALARM_MGR_EXCEPTION_PRINT(
342 "alarm was already initialized. app_service_name=%s\n",
343 g_quark_to_string(alarm_context.quark_app_service_name));
344 return ALARMMGR_RESULT_SUCCESS;
351 memset(service_name_mod, 'a', MAX_SERVICE_NAME_LEN-1);
353 len = strlen("ALARM.");
354 strncpy(service_name, "ALARM.", len);
355 strncpy(service_name + len, appid, strlen(appid));
359 for(i=0;i<=strlen(service_name);i++)
361 if (service_name[i] == '.' )
363 service_name_mod[j] = service_name[i];
367 service_name_mod[j] = service_name[i];
372 ALARM_MGR_LOG_PRINT("[alarm-lib]: service_name %s\n", service_name);
373 ALARM_MGR_LOG_PRINT("[alarm-lib]: service_name_mod %s\n", service_name_mod);
375 dbus_error_init(&derror);
377 request_name_result = dbus_bus_request_name(
378 dbus_g_connection_get_connection(alarm_context.bus),
379 service_name_mod, 0, &derror);
380 if (dbus_error_is_set(&derror)) /*failure*/ {
381 ALARM_MGR_EXCEPTION_PRINT(
382 "Failed to dbus_bus_request_name(%s): %s\n", service_name,
384 dbus_error_free(&derror);
386 return ERR_ALARM_SYSTEM_FAIL;
388 alarm_context.quark_app_service_name =
389 g_quark_from_string(service_name);
390 alarm_context.quark_app_service_name_mod=
391 g_quark_from_string(service_name_mod);
394 if (!dbus_connection_add_filter(
395 dbus_g_connection_get_connection(alarm_context.bus),
396 __expire_alarm_filter, NULL, NULL)) {
397 ALARM_MGR_EXCEPTION_PRINT("add __expire_alarm_filter failed\n");
399 return ERR_ALARM_SYSTEM_FAIL;
402 b_initialized = true;
403 return ALARMMGR_RESULT_SUCCESS;
407 EXPORT_API void alarmmgr_fini()
409 dbus_connection_remove_filter(dbus_g_connection_get_connection
411 __expire_alarm_filter, NULL);
414 EXPORT_API int alarmmgr_set_cb(alarm_cb_t handler, void *user_param)
416 ALARM_MGR_LOG_PRINT("alarm_set_cb is called\n");
418 if (handler == NULL) {
419 return ERR_ALARM_INVALID_PARAM;
421 alarm_context.alarm_handler = handler;
422 alarm_context.user_param = user_param;
423 return ALARMMGR_RESULT_SUCCESS;
426 EXPORT_API alarm_entry_t *alarmmgr_create_alarm(void)
428 alarm_info_t *alarm = (alarm_info_t *) malloc(sizeof(alarm_info_t));
435 alarm->start.year = 0;
436 alarm->start.month = 0;
437 alarm->start.day = 0;
438 alarm->start.hour = 0;
439 alarm->start.min = 0;
440 alarm->start.sec = 0;
443 alarm->end.month = 0;
449 alarm->mode.repeat = ALARM_REPEAT_MODE_ONCE;
450 alarm->mode.u_interval.interval = 0;
452 alarm->alarm_type = ALARM_TYPE_DEFAULT;
454 alarm->reserved_info = 0;
456 return (alarm_entry_t *) alarm;
459 EXPORT_API int alarmmgr_free_alarm(alarm_entry_t *alarm)
462 return ERR_ALARM_INVALID_PARAM;
466 return ALARMMGR_RESULT_SUCCESS;
469 EXPORT_API int alarmmgr_set_time(alarm_entry_t *alarm, alarm_date_t time)
471 alarm_info_t *alarm_info; /*= (alarm_info_t*)alarm;*/
475 return ERR_ALARM_INVALID_PARAM;
478 alarm_info = (alarm_info_t *) alarm;
480 if (!__alarm_validate_date(&time, &error_code)) {
481 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
485 if (!__alarm_validate_time(&time, &error_code)) {
486 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
490 memcpy(&alarm_info->start, &time, sizeof(alarm_date_t));
492 return ALARMMGR_RESULT_SUCCESS;
495 EXPORT_API int alarmmgr_get_time(const alarm_entry_t *alarm,
498 alarm_info_t *alarm_info = (alarm_info_t *) alarm;
501 return ERR_ALARM_INVALID_PARAM;
505 memcpy(time, &alarm_info->start, sizeof(alarm_date_t));
507 return ALARMMGR_RESULT_SUCCESS;
510 EXPORT_API int alarmmgr_set_repeat_mode(alarm_entry_t *alarm,
511 alarm_repeat_mode_t repeat,
514 alarm_info_t *alarm_info = (alarm_info_t *) alarm;
516 if (repeat >= ALARM_REPEAT_MODE_MAX) {
517 return ERR_ALARM_INVALID_PARAM;
520 alarm_info->mode.repeat = repeat;
522 if (repeat == ALARM_REPEAT_MODE_REPEAT
523 || repeat == ALARM_REPEAT_MODE_WEEKLY) {
524 alarm_info->mode.u_interval.interval = interval;
527 return ALARMMGR_RESULT_SUCCESS;
530 EXPORT_API int alarmmgr_get_repeat_mode(const alarm_entry_t *alarm,
531 alarm_repeat_mode_t *repeat,
534 alarm_info_t *alarm_info = (alarm_info_t *) alarm;
537 return ERR_ALARM_INVALID_PARAM;
541 *repeat = alarm_info->mode.repeat;
542 if (interval != NULL)
543 *interval = alarm_info->mode.u_interval.interval;
545 return ALARMMGR_RESULT_SUCCESS;
548 EXPORT_API int alarmmgr_set_type(alarm_entry_t *alarm, int alarm_type)
550 alarm_info_t *alarm_info; /*= (alarm_info_t*)alarm;*/
553 return ERR_ALARM_INVALID_PARAM;
556 alarm_info = (alarm_info_t *) alarm;
558 alarm_info->alarm_type = alarm_type;
559 alarm_info->alarm_type &= (~ALARM_TYPE_RELATIVE);
561 return ALARMMGR_RESULT_SUCCESS;
564 EXPORT_API int alarmmgr_get_type(const alarm_entry_t *alarm, int *alarm_type)
566 alarm_info_t *alarm_info = (alarm_info_t *) alarm;
569 return ERR_ALARM_INVALID_PARAM;
572 if (alarm_type != NULL)
573 *alarm_type = alarm_info->alarm_type;
575 return ALARMMGR_RESULT_SUCCESS;
579 static int __alarmmgr_init_appsvc(void)
584 ALARM_MGR_EXCEPTION_PRINT("alarm was already initialized\n");
585 return ALARMMGR_RESULT_SUCCESS;
590 dbus_g_thread_init();
596 b_initialized = true;
598 return ALARMMGR_RESULT_SUCCESS;
602 EXPORT_API void *alarmmgr_get_alarm_appsvc_info(alarm_id_t alarm_id, int *return_code){
613 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_get_alarm_appsvc_info() is called\n");
617 *return_code = ERR_ALARM_INVALID_ID;
621 return _send_alarm_get_appsvc_info(alarm_context, alarm_id, return_code);
625 EXPORT_API int alarmmgr_set_rtc_time(alarm_date_t *time){
631 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter time\n");
632 return ERR_ALARM_INVALID_PARAM;
640 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_set_rtc_time() is called\n");
642 if (!__alarm_validate_date(time, &error_code)) {
643 ALARM_MGR_EXCEPTION_PRINT("RTC date error\n");
647 if (!__alarm_validate_time(time, &error_code)) {
648 ALARM_MGR_EXCEPTION_PRINT("RTC time error\n");
655 if (!_send_alarm_set_rtc_time
656 (alarm_context, time, &error_code)){
660 return ALARMMGR_RESULT_SUCCESS;
664 EXPORT_API int alarmmgr_add_alarm_appsvc_with_localtime(alarm_entry_t *alarm, void *bundle_data, alarm_id_t *alarm_id)
666 alarm_info_t *alarm_info = NULL; /* = (alarm_info_t*)alarm; */
667 const char *operation = NULL;
671 bundle *b=(bundle *)bundle_data;
673 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
676 return ERR_ALARM_INVALID_PARAM;
681 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle\n");
682 return ERR_ALARM_INVALID_PARAM;
684 operation = appsvc_get_operation(b);
686 if (NULL == operation)
688 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle [appsvc operation not present]\n");
689 return ERR_ALARM_INVALID_PARAM;
692 if (__alarmmgr_init_appsvc() < 0)
694 ALARM_MGR_EXCEPTION_PRINT("Unable to initialize dbus!!!\n");
695 return ERR_ALARM_SYSTEM_FAIL;
698 alarm_info = (alarm_info_t *) alarm;
700 appid = appsvc_get_appid(b);
702 if (NULL == appid && (alarm_info->alarm_type & ALARM_TYPE_NOLAUNCH) )
704 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter\n");
705 return ERR_ALARM_INVALID_PARAM;
708 if (alarm_info == NULL || alarm_id == NULL) {
709 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter\n");
710 return ERR_ALARM_INVALID_PARAM;
712 alarm_mode_t *mode = &alarm_info->mode;
714 ALARM_MGR_LOG_PRINT("start(%d-%d-%d, %02d:%02d:%02d), end(%d-%d-%d), repeat(%d), interval(%d), type(%d)",
715 alarm_info->start.day, alarm_info->start.month, alarm_info->start.year,
716 alarm_info->start.hour, alarm_info->start.min, alarm_info->start.sec,
717 alarm_info->end.year, alarm_info->end.month, alarm_info->end.day,
718 alarm_info->mode.repeat, alarm_info->mode.u_interval, alarm_info->alarm_type);
720 /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
721 if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
722 return ERR_ALARM_INVALID_PARAM;
725 if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
726 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
730 if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
731 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
735 if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
736 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
741 if (!_send_alarm_create_appsvc
742 (alarm_context, alarm_info, alarm_id, b,
747 return ALARMMGR_RESULT_SUCCESS;
753 EXPORT_API int alarmmgr_add_alarm_with_localtime(alarm_entry_t *alarm,
754 const char *destination,
755 alarm_id_t *alarm_id)
757 char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
758 char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = { 0 };
759 alarm_info_t *alarm_info; /* = (alarm_info_t*)alarm; */
764 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
767 return ERR_ALARM_INVALID_PARAM;
770 alarm_info = (alarm_info_t *) alarm;
771 if (alarm_info == NULL || alarm_id == NULL) {
772 return ERR_ALARM_INVALID_PARAM;
776 alarm_mode_t *mode = &alarm_info->mode;
782 ALARM_MGR_LOG_PRINT("start(%d-%d-%d, %02d:%02d:%02d), end(%d-%d-%d), repeat(%d), interval(%d), type(%d)",
783 alarm_info->start.day, alarm_info->start.month, alarm_info->start.year,
784 alarm_info->start.hour, alarm_info->start.min, alarm_info->start.sec,
785 alarm_info->end.year, alarm_info->end.month, alarm_info->end.day,
786 alarm_info->mode.repeat, alarm_info->mode.u_interval, alarm_info->alarm_type);
788 /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
789 if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
790 return ERR_ALARM_INVALID_PARAM;
793 if (destination && strlen(destination) >= MAX_PKG_NAME_LEN){
794 ALARM_MGR_EXCEPTION_PRINT("[alarm-lib]: destination name is too long!\n");
795 return ERR_ALARM_INVALID_PARAM;
799 if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
800 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
804 if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
805 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
809 if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
810 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
814 if (destination != NULL) {
815 memset(dst_service_name, 0,
816 strlen(destination) + strlen("ALARM.") + 2);
817 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "ALARM.%s",
820 memset(dst_service_name_mod,'a',MAX_SERVICE_NAME_LEN-1);
824 for(i=0; i<=strlen(dst_service_name); i++)
826 if (dst_service_name[i] == '.' )
828 dst_service_name_mod[j] = dst_service_name[i];
833 dst_service_name_mod[j] = dst_service_name[i];
838 if (!_send_alarm_create
839 (alarm_context, alarm_info, alarm_id, dst_service_name, dst_service_name_mod,
844 if (!_send_alarm_create
845 (alarm_context, alarm_info, alarm_id, "null", "null", &error_code)) {
849 return ALARMMGR_RESULT_SUCCESS;
854 EXPORT_API int alarmmgr_add_alarm_appsvc(int alarm_type, time_t trigger_at_time,
855 time_t interval, void *bundle_data,
856 alarm_id_t *alarm_id)
860 struct tm duetime_tm;
861 alarm_info_t alarm_info;
862 const char *operation = NULL;
865 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
867 bundle *b=(bundle *)bundle_data;
871 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle\n");
872 return ERR_ALARM_INVALID_PARAM;
874 operation = appsvc_get_operation(b);
876 if (NULL == operation)
878 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle [appsvc operation not present]\n");
879 return ERR_ALARM_INVALID_PARAM;
882 appid = appsvc_get_appid(b);
884 if (NULL == appid && (alarm_type & ALARM_TYPE_NOLAUNCH) )
886 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter\n");
887 return ERR_ALARM_INVALID_PARAM;
890 if (__alarmmgr_init_appsvc() < 0)
892 ALARM_MGR_EXCEPTION_PRINT("Unable to initialize dbus!!!\n");
893 return ERR_ALARM_SYSTEM_FAIL;
896 if (alarm_id == NULL) {
897 return ERR_ALARM_INVALID_PARAM;
900 if (trigger_at_time < 0) {
901 return ERR_ALARM_INVALID_PARAM;
904 alarm_info.alarm_type = alarm_type;
905 alarm_info.alarm_type |= ALARM_TYPE_RELATIVE;
909 current_time += trigger_at_time;
911 localtime_r(¤t_time, &duetime_tm);
913 alarm_info.start.year = duetime_tm.tm_year + 1900;
914 alarm_info.start.month = duetime_tm.tm_mon + 1;
915 alarm_info.start.day = duetime_tm.tm_mday;
917 alarm_info.end.year = 0;
918 alarm_info.end.month = 0;
919 alarm_info.end.day = 0;
921 alarm_info.start.hour = duetime_tm.tm_hour;
922 alarm_info.start.min = duetime_tm.tm_min;
923 alarm_info.start.sec = duetime_tm.tm_sec;
926 alarm_info.mode.repeat = ALARM_REPEAT_MODE_ONCE;
927 alarm_info.mode.u_interval.interval = 0;
929 alarm_info.mode.repeat = ALARM_REPEAT_MODE_REPEAT;
930 alarm_info.mode.u_interval.interval = interval;
933 ALARM_MGR_LOG_PRINT("trigger_at_time(%d), start(%d-%d-%d, %02d:%02d:%02d), repeat(%d), interval(%d), type(%d)",
934 trigger_at_time, alarm_info.start.day, alarm_info.start.month, alarm_info.start.year,
935 alarm_info.start.hour, alarm_info.start.min, alarm_info.start.sec,
936 alarm_info.mode.repeat, alarm_info.mode.u_interval, alarm_info.alarm_type);
938 if (!_send_alarm_create_appsvc
939 (alarm_context, &alarm_info, alarm_id, b,
944 return ALARMMGR_RESULT_SUCCESS;
948 EXPORT_API int alarmmgr_add_alarm(int alarm_type, time_t trigger_at_time,
949 time_t interval, const char *destination,
950 alarm_id_t *alarm_id)
952 char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
953 char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = { 0 };
958 struct tm duetime_tm;
959 alarm_info_t alarm_info;
962 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
968 if (alarm_id == NULL) {
969 return ERR_ALARM_INVALID_PARAM;
972 if (trigger_at_time < 0) {
973 return ERR_ALARM_INVALID_PARAM;
976 if (destination && strlen(destination) >= MAX_PKG_NAME_LEN){
977 ALARM_MGR_EXCEPTION_PRINT("[alarm-lib]: destination name is too long!\n");
978 return ERR_ALARM_INVALID_PARAM;
981 alarm_info.alarm_type = alarm_type;
982 alarm_info.alarm_type |= ALARM_TYPE_RELATIVE;
986 current_time += trigger_at_time;
988 localtime_r(¤t_time, &duetime_tm);
990 alarm_info.start.year = duetime_tm.tm_year + 1900;
991 alarm_info.start.month = duetime_tm.tm_mon + 1;
992 alarm_info.start.day = duetime_tm.tm_mday;
994 alarm_info.end.year = 0;
995 alarm_info.end.month = 0;
996 alarm_info.end.day = 0;
998 alarm_info.start.hour = duetime_tm.tm_hour;
999 alarm_info.start.min = duetime_tm.tm_min;
1000 alarm_info.start.sec = duetime_tm.tm_sec;
1002 if (interval <= 0) {
1003 alarm_info.mode.repeat = ALARM_REPEAT_MODE_ONCE;
1004 alarm_info.mode.u_interval.interval = 0;
1006 alarm_info.mode.repeat = ALARM_REPEAT_MODE_REPEAT;
1007 alarm_info.mode.u_interval.interval = interval;
1010 ALARM_MGR_LOG_PRINT("trigger_at_time(%d), start(%d-%d-%d, %02d:%02d:%02d), repeat(%d), interval(%d), type(%d)",
1011 trigger_at_time, alarm_info.start.day, alarm_info.start.month, alarm_info.start.year,
1012 alarm_info.start.hour, alarm_info.start.min, alarm_info.start.sec,
1013 alarm_info.mode.repeat, alarm_info.mode.u_interval, alarm_info.alarm_type);
1015 if (destination != NULL) {
1016 memset(dst_service_name, 0,
1017 strlen(destination) + strlen("ALARM.") + 2);
1018 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "ALARM.%s",
1020 memset(dst_service_name_mod,'a',MAX_SERVICE_NAME_LEN-1);
1024 for(i=0;i<=strlen(dst_service_name);i++)
1026 if (dst_service_name[i] == '.')
1028 dst_service_name_mod[j]=dst_service_name[i];
1033 dst_service_name_mod[j]=dst_service_name[i];
1038 if (!_send_alarm_create
1039 (alarm_context, &alarm_info, alarm_id, dst_service_name,dst_service_name_mod,
1044 if (!_send_alarm_create
1045 (alarm_context, &alarm_info, alarm_id, "null","null", &error_code)) {
1049 return ALARMMGR_RESULT_SUCCESS;
1052 EXPORT_API int alarmmgr_add_alarm_withcb(int alarm_type, time_t trigger_at_time,
1053 time_t interval, alarm_cb_t handler, void *user_param, alarm_id_t *alarm_id)
1055 char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
1056 char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = { 0 };
1060 time_t current_time;
1061 struct tm duetime_tm;
1062 alarm_info_t alarm_info;
1066 ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid));
1067 if (ret != AUL_R_OK)
1068 return ERR_ALARM_SYSTEM_FAIL;
1070 ret = alarmmgr_init(appid);
1074 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_add_alarm_withcb() is called\n");
1076 ALARM_MGR_LOG_PRINT("interval(%d)", interval);
1078 if (alarm_id == NULL) {
1079 return ERR_ALARM_INVALID_PARAM;
1082 if (trigger_at_time < 0) {
1083 return ERR_ALARM_INVALID_PARAM;
1086 alarm_info.alarm_type = alarm_type;
1087 alarm_info.alarm_type |= ALARM_TYPE_RELATIVE;
1088 alarm_info.alarm_type |= ALARM_TYPE_WITHCB;
1090 time(¤t_time);
1092 current_time += trigger_at_time;
1094 localtime_r(¤t_time, &duetime_tm);
1096 alarm_info.start.year = duetime_tm.tm_year + 1900;
1097 alarm_info.start.month = duetime_tm.tm_mon + 1;
1098 alarm_info.start.day = duetime_tm.tm_mday;
1100 alarm_info.end.year = 0;
1101 alarm_info.end.month = 0;
1102 alarm_info.end.day = 0;
1104 alarm_info.start.hour = duetime_tm.tm_hour;
1105 alarm_info.start.min = duetime_tm.tm_min;
1106 alarm_info.start.sec = duetime_tm.tm_sec;
1108 if (interval <= 0) {
1109 alarm_info.mode.repeat = ALARM_REPEAT_MODE_ONCE;
1110 alarm_info.mode.u_interval.interval = 0;
1112 alarm_info.mode.repeat = ALARM_REPEAT_MODE_REPEAT;
1113 alarm_info.mode.u_interval.interval = interval;
1116 if (!_send_alarm_create(alarm_context, &alarm_info, alarm_id, "null","null", &error_code)) {
1119 __add_resultcb(*alarm_id, handler, user_param);
1121 return ALARMMGR_RESULT_SUCCESS;
1124 EXPORT_API int alarmmgr_remove_alarm(alarm_id_t alarm_id)
1128 alarm_cb_info_t *info;
1134 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_delete(%d) is called\n", alarm_id);
1136 if (alarm_id <= 0) {
1137 return ERR_ALARM_INVALID_ID;
1140 if (!_send_alarm_delete(alarm_context, alarm_id, &error_code))
1143 info = __find_resultcb(alarm_id);
1144 __remove_resultcb(info);
1146 return ALARMMGR_RESULT_SUCCESS;
1149 EXPORT_API int alarmmgr_enum_alarm_ids(alarm_enum_fn_t fn, void *user_param)
1151 GError *error = NULL;
1152 GArray *alarm_array = NULL;
1153 int return_code = 0;
1161 return ERR_ALARM_INVALID_PARAM;
1167 if (!org_tizen_alarm_manager_alarm_get_number_of_ids(
1168 alarm_context.proxy, alarm_context.pid, &maxnum_of_ids,
1169 &return_code, &error)) {
1170 /* dbus-glib error */
1171 /* error_code should be set */
1172 ALARM_MGR_EXCEPTION_PRINT(
1173 "org_tizen_alarm_manager_alarm_get_number_of_ids() "
1174 "failed. return_code[%d], return_code[%s]\n",
1175 return_code, error->message);
1178 if (return_code != 0) {
1182 if (!org_tizen_alarm_manager_alarm_get_list_of_ids(
1183 alarm_context.proxy, alarm_context.pid, maxnum_of_ids,
1184 &alarm_array, &num_of_ids, &return_code, &error)) {
1185 /*dbus-glib error */
1186 /* error_code should be set */
1187 ALARM_MGR_EXCEPTION_PRINT(
1188 "org_tizen_alarm_manager_alarm_get_list_of_ids() "
1189 "failed. alarm_id[%d], return_code[%d]\n",
1190 alarm_id, return_code);
1193 if (return_code != 0) {
1196 if (error != NULL) {
1197 ALARM_MGR_LOG_PRINT(
1198 "Alarm server not ready dbus error message %s\n", error->message);
1199 return ERR_ALARM_SYSTEM_FAIL;
1201 if (NULL == alarm_array) {
1202 ALARM_MGR_LOG_PRINT(
1203 "alarm server not initilized\n");
1204 return ERR_ALARM_SYSTEM_FAIL;
1206 for (i = 0; i < alarm_array->len && i < maxnum_of_ids; i++) {
1207 alarm_id = g_array_index(alarm_array, alarm_id_t, i);
1208 (*fn) (alarm_id, user_param);
1209 ALARM_MGR_LOG_PRINT(" alarm_id(%d)\n", alarm_id);
1212 g_array_free(alarm_array, true);
1215 return ALARMMGR_RESULT_SUCCESS;
1218 EXPORT_API int alarmmgr_get_info(alarm_id_t alarm_id, alarm_entry_t *alarm)
1221 alarm_info_t *alarm_info = (alarm_info_t *) alarm;
1229 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_get_info() is called\n");
1231 if (alarm_id < 0 || alarm_info == NULL) {
1232 return ERR_ALARM_INVALID_PARAM;
1235 if (!_send_alarm_get_info(alarm_context, alarm_id, alarm_info,
1239 return ALARMMGR_RESULT_SUCCESS;
1242 EXPORT_API int alarmmgr_power_on(bool on_off)
1245 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_power_on() is called\n");
1248 if (!_send_alarm_power_on(alarm_context, on_off, &error_code))
1251 ALARM_MGR_LOG_PRINT("[alarm-lib]:ALARM_BOOT feature is not supported. "
1252 "so we return false.\n");
1253 return ERR_ALARM_SYSTEM_FAIL;
1256 return ALARMMGR_RESULT_SUCCESS;
1259 int alarmmgr_create(alarm_info_t *alarm_info, char *destination,
1260 alarm_id_t *alarm_id)
1262 char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
1263 alarm_mode_t *mode = &alarm_info->mode;
1266 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
1268 if (alarm_info == NULL || alarm_id == NULL) {
1269 return ERR_ALARM_INVALID_PARAM;
1272 ALARM_MGR_LOG_PRINT("alarm_info->start.year(%d), "
1273 "alarm_info->start.month(%d), alarm_info->start.day(%d)",
1274 alarm_info->start.year, alarm_info->start.month,
1275 alarm_info->start.day);
1277 /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
1278 if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
1279 return ERR_ALARM_INVALID_PARAM;
1282 if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
1283 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
1287 if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
1288 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
1292 if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
1293 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
1297 if (destination != NULL) {
1298 memset(dst_service_name, 0,
1299 strlen(destination) + strlen("ALARM.") + 2);
1300 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "ALARM.%s",
1302 if (!_send_alarm_create
1303 (alarm_context, alarm_info, alarm_id, dst_service_name,"null",
1308 /*TODO: Currently this API is not exported. Hence not modifying*/
1309 if (!_send_alarm_create
1310 (alarm_context, alarm_info, alarm_id, "null", "null", &error_code)) {
1314 return ALARMMGR_RESULT_SUCCESS;
1318 int alarmmgr_get_number_of_ids(int *num_of_ids)
1321 ALARM_MGR_LOG_PRINT("[alarm-lib]:"
1322 "alarm_get_number_of_ids() is called\n");
1324 if (num_of_ids == NULL) {
1325 return ERR_ALARM_INVALID_PARAM;
1327 ALARM_MGR_LOG_PRINT("call alarm_get_number_of_ids\n");
1328 if (!_send_alarm_get_number_of_ids(alarm_context, num_of_ids,
1332 return ALARMMGR_RESULT_SUCCESS;
1335 int alarmmgr_get_list_of_ids(int maxnum_of_ids, alarm_id_t *alarm_id,
1339 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_get_list_of_ids() is called\n");
1341 if (maxnum_of_ids < 0 || alarm_id == NULL || num_of_ids == NULL) {
1342 return ERR_ALARM_INVALID_PARAM;
1345 if (maxnum_of_ids == 0) {
1347 return ALARMMGR_RESULT_SUCCESS;
1350 if (!_send_alarm_get_list_of_ids
1351 (alarm_context, maxnum_of_ids, alarm_id, num_of_ids, &error_code))
1354 return ALARMMGR_RESULT_SUCCESS;
1357 EXPORT_API int alarmmgr_get_next_duetime(alarm_id_t alarm_id, time_t* duetime)
1360 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_get_next_duetime() is called\n");
1362 if (duetime == NULL) {
1363 return ERR_ALARM_INVALID_PARAM;
1366 if (!_send_alarm_get_next_duetime
1367 (alarm_context, alarm_id, duetime, &error_code))
1370 return ALARMMGR_RESULT_SUCCESS;