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_EXCEPTION_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 };
334 return ERR_ALARM_INVALID_PARAM;
336 if (strlen(appid) >= MAX_PKG_NAME_LEN)
337 return ERR_ALARM_INVALID_PARAM;
340 ALARM_MGR_EXCEPTION_PRINT(
341 "alarm was already initialized. app_service_name=%s\n",
342 g_quark_to_string(alarm_context.quark_app_service_name));
343 return ALARMMGR_RESULT_SUCCESS;
350 memset(service_name_mod, 'a', MAX_SERVICE_NAME_LEN-1);
351 strncpy(service_name, appid, MAX_PKG_NAME_LEN);
355 for(i=0;i<=strlen(service_name);i++)
357 if (service_name[i] == '.' )
359 service_name_mod[j] = service_name[i];
363 service_name_mod[j] = service_name[i];
368 strncat(service_name, ".ALARM", strlen(".ALARM"));
370 strncat(service_name_mod, ".ALARM", strlen(".ALARM"));
373 dbus_error_init(&derror);
375 request_name_result = dbus_bus_request_name(
376 dbus_g_connection_get_connection(alarm_context.bus),
377 service_name_mod, 0, &derror);
378 if (dbus_error_is_set(&derror)) /*failure*/ {
379 ALARM_MGR_EXCEPTION_PRINT(
380 "Failed to dbus_bus_request_name(%s): %s\n", service_name,
382 dbus_error_free(&derror);
384 return ERR_ALARM_SYSTEM_FAIL;
386 alarm_context.quark_app_service_name =
387 g_quark_from_string(service_name);
388 alarm_context.quark_app_service_name_mod=
389 g_quark_from_string(service_name_mod);
392 if (!dbus_connection_add_filter(
393 dbus_g_connection_get_connection(alarm_context.bus),
394 __expire_alarm_filter, NULL, NULL)) {
395 ALARM_MGR_EXCEPTION_PRINT("add __expire_alarm_filter failed\n");
397 return ERR_ALARM_SYSTEM_FAIL;
400 b_initialized = true;
401 return ALARMMGR_RESULT_SUCCESS;
405 EXPORT_API void alarmmgr_fini()
407 dbus_connection_remove_filter(dbus_g_connection_get_connection
409 __expire_alarm_filter, NULL);
412 EXPORT_API int alarmmgr_set_cb(alarm_cb_t handler, void *user_param)
414 ALARM_MGR_LOG_PRINT("alarm_set_cb is called\n");
416 if (handler == NULL) {
417 return ERR_ALARM_INVALID_PARAM;
419 alarm_context.alarm_handler = handler;
420 alarm_context.user_param = user_param;
421 return ALARMMGR_RESULT_SUCCESS;
424 EXPORT_API alarm_entry_t *alarmmgr_create_alarm(void)
426 alarm_info_t *alarm = (alarm_info_t *) malloc(sizeof(alarm_info_t));
433 alarm->start.year = 0;
434 alarm->start.month = 0;
435 alarm->start.day = 0;
436 alarm->start.hour = 0;
437 alarm->start.min = 0;
438 alarm->start.sec = 0;
441 alarm->end.month = 0;
447 alarm->mode.repeat = ALARM_REPEAT_MODE_ONCE;
448 alarm->mode.u_interval.interval = 0;
450 alarm->alarm_type = ALARM_TYPE_DEFAULT;
452 alarm->reserved_info = 0;
454 return (alarm_entry_t *) alarm;
457 EXPORT_API int alarmmgr_free_alarm(alarm_entry_t *alarm)
460 return ERR_ALARM_INVALID_PARAM;
464 return ALARMMGR_RESULT_SUCCESS;
467 EXPORT_API int alarmmgr_set_time(alarm_entry_t *alarm, alarm_date_t time)
469 alarm_info_t *alarm_info; /*= (alarm_info_t*)alarm;*/
473 return ERR_ALARM_INVALID_PARAM;
476 alarm_info = (alarm_info_t *) alarm;
478 if (!__alarm_validate_date(&time, &error_code)) {
479 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
483 if (!__alarm_validate_time(&time, &error_code)) {
484 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
488 memcpy(&alarm_info->start, &time, sizeof(alarm_date_t));
490 return ALARMMGR_RESULT_SUCCESS;
493 EXPORT_API int alarmmgr_get_time(const alarm_entry_t *alarm,
496 alarm_info_t *alarm_info = (alarm_info_t *) alarm;
499 return ERR_ALARM_INVALID_PARAM;
503 memcpy(time, &alarm_info->start, sizeof(alarm_date_t));
505 return ALARMMGR_RESULT_SUCCESS;
508 EXPORT_API int alarmmgr_set_repeat_mode(alarm_entry_t *alarm,
509 alarm_repeat_mode_t repeat,
512 alarm_info_t *alarm_info = (alarm_info_t *) alarm;
514 if (repeat >= ALARM_REPEAT_MODE_MAX) {
515 return ERR_ALARM_INVALID_PARAM;
518 alarm_info->mode.repeat = repeat;
520 if (repeat == ALARM_REPEAT_MODE_REPEAT
521 || repeat == ALARM_REPEAT_MODE_WEEKLY) {
522 alarm_info->mode.u_interval.interval = interval;
525 return ALARMMGR_RESULT_SUCCESS;
528 EXPORT_API int alarmmgr_get_repeat_mode(const alarm_entry_t *alarm,
529 alarm_repeat_mode_t *repeat,
532 alarm_info_t *alarm_info = (alarm_info_t *) alarm;
535 return ERR_ALARM_INVALID_PARAM;
539 *repeat = alarm_info->mode.repeat;
540 if (interval != NULL)
541 *interval = alarm_info->mode.u_interval.interval;
543 return ALARMMGR_RESULT_SUCCESS;
546 EXPORT_API int alarmmgr_set_type(alarm_entry_t *alarm, int alarm_type)
548 alarm_info_t *alarm_info; /*= (alarm_info_t*)alarm;*/
551 return ERR_ALARM_INVALID_PARAM;
554 alarm_info = (alarm_info_t *) alarm;
556 alarm_info->alarm_type = alarm_type;
557 alarm_info->alarm_type &= (~ALARM_TYPE_RELATIVE);
559 return ALARMMGR_RESULT_SUCCESS;
562 EXPORT_API int alarmmgr_get_type(const alarm_entry_t *alarm, int *alarm_type)
564 alarm_info_t *alarm_info = (alarm_info_t *) alarm;
567 return ERR_ALARM_INVALID_PARAM;
570 if (alarm_type != NULL)
571 *alarm_type = alarm_info->alarm_type;
573 return ALARMMGR_RESULT_SUCCESS;
577 static int __alarmmgr_init_appsvc(void)
582 ALARM_MGR_EXCEPTION_PRINT("alarm was already initialized\n");
583 return ALARMMGR_RESULT_SUCCESS;
588 dbus_g_thread_init();
594 b_initialized = true;
596 return ALARMMGR_RESULT_SUCCESS;
600 EXPORT_API void *alarmmgr_get_alarm_appsvc_info(alarm_id_t alarm_id, int *return_code){
611 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_get_alarm_appsvc_info() is called\n");
615 *return_code = ERR_ALARM_INVALID_ID;
619 return _send_alarm_get_appsvc_info(alarm_context, alarm_id, return_code);
623 EXPORT_API int alarmmgr_set_rtc_time(alarm_date_t *time){
629 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter time\n");
630 return ERR_ALARM_INVALID_PARAM;
638 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_set_rtc_time() is called\n");
640 if (!__alarm_validate_date(time, &error_code)) {
641 ALARM_MGR_EXCEPTION_PRINT("RTC date error\n");
645 if (!__alarm_validate_time(time, &error_code)) {
646 ALARM_MGR_EXCEPTION_PRINT("RTC time error\n");
653 if (!_send_alarm_set_rtc_time
654 (alarm_context, time, &error_code)){
658 return ALARMMGR_RESULT_SUCCESS;
662 EXPORT_API int alarmmgr_add_alarm_appsvc_with_localtime(alarm_entry_t *alarm, void *bundle_data, alarm_id_t *alarm_id)
664 alarm_info_t *alarm_info = NULL; /* = (alarm_info_t*)alarm; */
665 const char *operation = NULL;
669 bundle *b=(bundle *)bundle_data;
672 return ERR_ALARM_INVALID_PARAM;
677 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle\n");
678 return ERR_ALARM_INVALID_PARAM;
680 operation = appsvc_get_operation(b);
682 if (NULL == operation)
684 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle [appsvc operation not present]\n");
685 return ERR_ALARM_INVALID_PARAM;
688 if (__alarmmgr_init_appsvc() < 0)
690 ALARM_MGR_EXCEPTION_PRINT("Unable to initialize dbus!!!\n");
691 return ERR_ALARM_SYSTEM_FAIL;
694 alarm_info = (alarm_info_t *) alarm;
696 appid = appsvc_get_appid(b);
698 if (NULL == appid && (alarm_info->alarm_type & ALARM_TYPE_NOLAUNCH) )
700 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter\n");
701 return ERR_ALARM_INVALID_PARAM;
704 if (alarm_info == NULL || alarm_id == NULL) {
705 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter\n");
706 return ERR_ALARM_INVALID_PARAM;
708 alarm_mode_t *mode = &alarm_info->mode;
710 ALARM_MGR_LOG_PRINT("alarm_info->start.year(%d), "
711 "alarm_info->start.month(%d), alarm_info->start.day(%d)",
712 alarm_info->start.year, alarm_info->start.month,
713 alarm_info->start.day);
715 /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
716 if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
717 return ERR_ALARM_INVALID_PARAM;
720 if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
721 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
725 if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
726 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
730 if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
731 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
736 if (!_send_alarm_create_appsvc
737 (alarm_context, alarm_info, alarm_id, b,
742 return ALARMMGR_RESULT_SUCCESS;
748 EXPORT_API int alarmmgr_add_alarm_with_localtime(alarm_entry_t *alarm,
749 const char *destination,
750 alarm_id_t *alarm_id)
752 char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
753 char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = { 0 };
754 alarm_info_t *alarm_info; /* = (alarm_info_t*)alarm; */
760 return ERR_ALARM_INVALID_PARAM;
763 alarm_info = (alarm_info_t *) alarm;
764 if (alarm_info == NULL || alarm_id == NULL) {
765 return ERR_ALARM_INVALID_PARAM;
769 alarm_mode_t *mode = &alarm_info->mode;
775 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
777 ALARM_MGR_LOG_PRINT("alarm_info->start.year(%d), "
778 "alarm_info->start.month(%d), alarm_info->start.day(%d)",
779 alarm_info->start.year, alarm_info->start.month,
780 alarm_info->start.day);
782 /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
783 if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
784 return ERR_ALARM_INVALID_PARAM;
787 if (destination && strlen(destination) >= MAX_PKG_NAME_LEN){
788 ALARM_MGR_EXCEPTION_PRINT("[alarm-lib]: destination name is too long!\n");
789 return ERR_ALARM_INVALID_PARAM;
793 if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
794 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
798 if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
799 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
803 if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
804 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
808 if (destination != NULL) {
809 memset(dst_service_name, 0,
810 strlen(destination) + strlen(".ALARM") + 2);
811 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "%s",
814 memset(dst_service_name_mod,'a',MAX_SERVICE_NAME_LEN-1);
818 for(i=0; i<=strlen(dst_service_name); i++)
820 if (dst_service_name[i] == '.' )
822 dst_service_name_mod[j] = dst_service_name[i];
827 dst_service_name_mod[j] = dst_service_name[i];
832 strncat(dst_service_name, ".ALARM", strlen(".ALARM"));
833 strncat(dst_service_name_mod, ".ALARM", strlen(".ALARM"));
835 if (!_send_alarm_create
836 (alarm_context, alarm_info, alarm_id, dst_service_name, dst_service_name_mod,
841 if (!_send_alarm_create
842 (alarm_context, alarm_info, alarm_id, "null", "null", &error_code)) {
846 return ALARMMGR_RESULT_SUCCESS;
851 EXPORT_API int alarmmgr_add_alarm_appsvc(int alarm_type, time_t trigger_at_time,
852 time_t interval, void *bundle_data,
853 alarm_id_t *alarm_id)
857 struct tm duetime_tm;
858 alarm_info_t alarm_info;
859 const char *operation = NULL;
862 bundle *b=(bundle *)bundle_data;
866 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle\n");
867 return ERR_ALARM_INVALID_PARAM;
869 operation = appsvc_get_operation(b);
871 if (NULL == operation)
873 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle [appsvc operation not present]\n");
874 return ERR_ALARM_INVALID_PARAM;
877 appid = appsvc_get_appid(b);
879 if (NULL == appid && (alarm_type & ALARM_TYPE_NOLAUNCH) )
881 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter\n");
882 return ERR_ALARM_INVALID_PARAM;
885 if (__alarmmgr_init_appsvc() < 0)
887 ALARM_MGR_EXCEPTION_PRINT("Unable to initialize dbus!!!\n");
888 return ERR_ALARM_SYSTEM_FAIL;
891 ALARM_MGR_LOG_PRINT("interval(%d)", interval);
893 if (alarm_id == NULL) {
894 return ERR_ALARM_INVALID_PARAM;
897 if (trigger_at_time < 0) {
898 return ERR_ALARM_INVALID_PARAM;
901 alarm_info.alarm_type = alarm_type;
902 alarm_info.alarm_type |= ALARM_TYPE_RELATIVE;
906 current_time += trigger_at_time;
908 localtime_r(¤t_time, &duetime_tm);
910 alarm_info.start.year = duetime_tm.tm_year + 1900;
911 alarm_info.start.month = duetime_tm.tm_mon + 1;
912 alarm_info.start.day = duetime_tm.tm_mday;
914 alarm_info.end.year = 0;
915 alarm_info.end.month = 0;
916 alarm_info.end.day = 0;
918 alarm_info.start.hour = duetime_tm.tm_hour;
919 alarm_info.start.min = duetime_tm.tm_min;
920 alarm_info.start.sec = duetime_tm.tm_sec;
923 alarm_info.mode.repeat = ALARM_REPEAT_MODE_ONCE;
924 alarm_info.mode.u_interval.interval = 0;
926 alarm_info.mode.repeat = ALARM_REPEAT_MODE_REPEAT;
927 alarm_info.mode.u_interval.interval = interval;
930 if (!_send_alarm_create_appsvc
931 (alarm_context, &alarm_info, alarm_id, b,
936 return ALARMMGR_RESULT_SUCCESS;
940 EXPORT_API int alarmmgr_add_alarm(int alarm_type, time_t trigger_at_time,
941 time_t interval, const char *destination,
942 alarm_id_t *alarm_id)
944 char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
945 char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = { 0 };
950 struct tm duetime_tm;
951 alarm_info_t alarm_info;
958 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
960 ALARM_MGR_LOG_PRINT("interval(%d)", interval);
962 if (alarm_id == NULL) {
963 return ERR_ALARM_INVALID_PARAM;
966 if (trigger_at_time < 0) {
967 return ERR_ALARM_INVALID_PARAM;
970 if (destination && strlen(destination) >= MAX_PKG_NAME_LEN){
971 ALARM_MGR_EXCEPTION_PRINT("[alarm-lib]: destination name is too long!\n");
972 return ERR_ALARM_INVALID_PARAM;
975 alarm_info.alarm_type = alarm_type;
976 alarm_info.alarm_type |= ALARM_TYPE_RELATIVE;
980 current_time += trigger_at_time;
982 localtime_r(¤t_time, &duetime_tm);
984 alarm_info.start.year = duetime_tm.tm_year + 1900;
985 alarm_info.start.month = duetime_tm.tm_mon + 1;
986 alarm_info.start.day = duetime_tm.tm_mday;
988 alarm_info.end.year = 0;
989 alarm_info.end.month = 0;
990 alarm_info.end.day = 0;
992 alarm_info.start.hour = duetime_tm.tm_hour;
993 alarm_info.start.min = duetime_tm.tm_min;
994 alarm_info.start.sec = duetime_tm.tm_sec;
997 alarm_info.mode.repeat = ALARM_REPEAT_MODE_ONCE;
998 alarm_info.mode.u_interval.interval = 0;
1000 alarm_info.mode.repeat = ALARM_REPEAT_MODE_REPEAT;
1001 alarm_info.mode.u_interval.interval = interval;
1004 if (destination != NULL) {
1005 memset(dst_service_name, 0,
1006 strlen(destination) + strlen(".ALARM") + 2);
1007 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "%s",
1009 memset(dst_service_name_mod,'a',MAX_SERVICE_NAME_LEN-1);
1013 for(i=0;i<=strlen(dst_service_name);i++)
1015 if (dst_service_name[i] == '.')
1017 dst_service_name_mod[j]=dst_service_name[i];
1022 dst_service_name_mod[j]=dst_service_name[i];
1027 strncat(dst_service_name, ".ALARM", strlen(".ALARM"));
1028 strncat(dst_service_name_mod, ".ALARM", strlen(".ALARM"));
1030 if (!_send_alarm_create
1031 (alarm_context, &alarm_info, alarm_id, dst_service_name,dst_service_name_mod,
1036 if (!_send_alarm_create
1037 (alarm_context, &alarm_info, alarm_id, "null","null", &error_code)) {
1041 return ALARMMGR_RESULT_SUCCESS;
1044 EXPORT_API int alarmmgr_add_alarm_withcb(int alarm_type, time_t trigger_at_time,
1045 time_t interval, alarm_cb_t handler, void *user_param, alarm_id_t *alarm_id)
1047 char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
1048 char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = { 0 };
1052 time_t current_time;
1053 struct tm duetime_tm;
1054 alarm_info_t alarm_info;
1058 aul_app_get_appid_bypid(getpid(), appid, sizeof(appid));
1060 ret = alarmmgr_init(appid);
1064 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_add_alarm_withcb() is called\n");
1066 ALARM_MGR_LOG_PRINT("interval(%d)", interval);
1068 if (alarm_id == NULL) {
1069 return ERR_ALARM_INVALID_PARAM;
1072 if (trigger_at_time < 0) {
1073 return ERR_ALARM_INVALID_PARAM;
1076 alarm_info.alarm_type = alarm_type;
1077 alarm_info.alarm_type |= ALARM_TYPE_RELATIVE;
1078 alarm_info.alarm_type |= ALARM_TYPE_WITHCB;
1080 time(¤t_time);
1082 current_time += trigger_at_time;
1084 localtime_r(¤t_time, &duetime_tm);
1086 alarm_info.start.year = duetime_tm.tm_year + 1900;
1087 alarm_info.start.month = duetime_tm.tm_mon + 1;
1088 alarm_info.start.day = duetime_tm.tm_mday;
1090 alarm_info.end.year = 0;
1091 alarm_info.end.month = 0;
1092 alarm_info.end.day = 0;
1094 alarm_info.start.hour = duetime_tm.tm_hour;
1095 alarm_info.start.min = duetime_tm.tm_min;
1096 alarm_info.start.sec = duetime_tm.tm_sec;
1098 if (interval <= 0) {
1099 alarm_info.mode.repeat = ALARM_REPEAT_MODE_ONCE;
1100 alarm_info.mode.u_interval.interval = 0;
1102 alarm_info.mode.repeat = ALARM_REPEAT_MODE_REPEAT;
1103 alarm_info.mode.u_interval.interval = interval;
1106 if (!_send_alarm_create(alarm_context, &alarm_info, alarm_id, "null","null", &error_code)) {
1109 __add_resultcb(*alarm_id, handler, user_param);
1111 return ALARMMGR_RESULT_SUCCESS;
1114 EXPORT_API int alarmmgr_remove_alarm(alarm_id_t alarm_id)
1118 alarm_cb_info_t *info;
1124 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_delete() is called\n");
1126 if (alarm_id <= 0) {
1127 return ERR_ALARM_INVALID_ID;
1130 if (!_send_alarm_delete(alarm_context, alarm_id, &error_code))
1133 info = __find_resultcb(alarm_id);
1134 __remove_resultcb(info);
1136 return ALARMMGR_RESULT_SUCCESS;
1139 EXPORT_API int alarmmgr_enum_alarm_ids(alarm_enum_fn_t fn, void *user_param)
1141 GError *error = NULL;
1142 GArray *alarm_array = NULL;
1143 int return_code = 0;
1151 return ERR_ALARM_INVALID_PARAM;
1157 if (!org_tizen_alarm_manager_alarm_get_number_of_ids(
1158 alarm_context.proxy, alarm_context.pid, &maxnum_of_ids,
1159 &return_code, &error)) {
1160 /* dbus-glib error */
1161 /* error_code should be set */
1162 ALARM_MGR_EXCEPTION_PRINT(
1163 "org_tizen_alarm_manager_alarm_get_number_of_ids() "
1164 "failed. return_code[%d], return_code[%s]\n",
1165 return_code, error->message);
1168 if (return_code != 0) {
1172 if (!org_tizen_alarm_manager_alarm_get_list_of_ids(
1173 alarm_context.proxy, alarm_context.pid, maxnum_of_ids,
1174 &alarm_array, &num_of_ids, &return_code, &error)) {
1175 /*dbus-glib error */
1176 /* error_code should be set */
1177 ALARM_MGR_EXCEPTION_PRINT(
1178 "org_tizen_alarm_manager_alarm_get_list_of_ids() "
1179 "failed. alarm_id[%d], return_code[%d]\n",
1180 alarm_id, return_code);
1183 if (return_code != 0) {
1186 if (error != NULL) {
1187 ALARM_MGR_LOG_PRINT(
1188 "Alarm server not ready dbus error message %s\n", error->message);
1189 return ERR_ALARM_SYSTEM_FAIL;
1191 if (NULL == alarm_array) {
1192 ALARM_MGR_LOG_PRINT(
1193 "alarm server not initilized\n");
1194 return ERR_ALARM_SYSTEM_FAIL;
1196 for (i = 0; i < alarm_array->len && i < maxnum_of_ids; i++) {
1197 alarm_id = g_array_index(alarm_array, alarm_id_t, i);
1198 (*fn) (alarm_id, user_param);
1199 ALARM_MGR_LOG_PRINT(" alarm_id(%d)\n", alarm_id);
1202 g_array_free(alarm_array, true);
1205 return ALARMMGR_RESULT_SUCCESS;
1208 EXPORT_API int alarmmgr_get_info(alarm_id_t alarm_id, alarm_entry_t *alarm)
1211 alarm_info_t *alarm_info = (alarm_info_t *) alarm;
1219 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_get_info() is called\n");
1221 if (alarm_id < 0 || alarm_info == NULL) {
1222 return ERR_ALARM_INVALID_PARAM;
1225 if (!_send_alarm_get_info(alarm_context, alarm_id, alarm_info,
1229 return ALARMMGR_RESULT_SUCCESS;
1232 EXPORT_API int alarmmgr_power_on(bool on_off)
1235 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_power_on() is called\n");
1238 if (!_send_alarm_power_on(alarm_context, on_off, &error_code))
1241 ALARM_MGR_LOG_PRINT("[alarm-lib]:ALARM_BOOT feature is not supported. "
1242 "so we return false.\n");
1243 return ERR_ALARM_SYSTEM_FAIL;
1246 return ALARMMGR_RESULT_SUCCESS;
1249 int alarmmgr_create(alarm_info_t *alarm_info, char *destination,
1250 alarm_id_t *alarm_id)
1252 char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
1253 alarm_mode_t *mode = &alarm_info->mode;
1256 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
1258 if (alarm_info == NULL || alarm_id == NULL) {
1259 return ERR_ALARM_INVALID_PARAM;
1262 ALARM_MGR_LOG_PRINT("alarm_info->start.year(%d), "
1263 "alarm_info->start.month(%d), alarm_info->start.day(%d)",
1264 alarm_info->start.year, alarm_info->start.month,
1265 alarm_info->start.day);
1267 /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
1268 if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
1269 return ERR_ALARM_INVALID_PARAM;
1272 if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
1273 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
1277 if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
1278 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
1282 if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
1283 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
1287 if (destination != NULL) {
1288 memset(dst_service_name, 0,
1289 strlen(destination) + strlen(".ALARM") + 2);
1290 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "%s.ALARM",
1292 if (!_send_alarm_create
1293 (alarm_context, alarm_info, alarm_id, dst_service_name,"null",
1298 /*TODO: Currently this API is not exported. Hence not modifying*/
1299 if (!_send_alarm_create
1300 (alarm_context, alarm_info, alarm_id, "null", "null", &error_code)) {
1304 return ALARMMGR_RESULT_SUCCESS;
1308 int alarmmgr_get_number_of_ids(int *num_of_ids)
1311 ALARM_MGR_LOG_PRINT("[alarm-lib]:"
1312 "alarm_get_number_of_ids() is called\n");
1314 if (num_of_ids == NULL) {
1315 return ERR_ALARM_INVALID_PARAM;
1317 ALARM_MGR_LOG_PRINT("call alarm_get_number_of_ids\n");
1318 if (!_send_alarm_get_number_of_ids(alarm_context, num_of_ids,
1322 return ALARMMGR_RESULT_SUCCESS;
1325 int alarmmgr_get_list_of_ids(int maxnum_of_ids, alarm_id_t *alarm_id,
1329 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_get_list_of_ids() is called\n");
1331 if (maxnum_of_ids < 0 || alarm_id == NULL || num_of_ids == NULL) {
1332 return ERR_ALARM_INVALID_PARAM;
1335 if (maxnum_of_ids == 0) {
1337 return ALARMMGR_RESULT_SUCCESS;
1340 if (!_send_alarm_get_list_of_ids
1341 (alarm_context, maxnum_of_ids, alarm_id, num_of_ids, &error_code))
1344 return ALARMMGR_RESULT_SUCCESS;