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.
31 #include <sys/types.h>
37 #include "alarm-internal.h"
38 #include "alarm-mgr-stub.h"
44 #define MAX_KEY_SIZE 256
45 #define MAX_PROC_NAME_LEN 512
47 static alarm_context_t alarm_context = { NULL, NULL, NULL, NULL, -1 };
49 static bool b_initialized = false;
50 static bool sub_initialized = false;
52 pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
54 static void __handle_expiry_method_call(GDBusConnection *conn,
55 const gchar *name, const gchar *path, const gchar *interface,
56 const gchar *method, GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data);
58 static int __alarm_validate_date(alarm_date_t *date, int *error_code);
59 static bool __alarm_validate_time(alarm_date_t *date, int *error_code);
60 static int __sub_init(void);
61 static int __alarmmgr_init_appsvc(void);
63 typedef struct _alarm_cb_info_t {
67 struct _alarm_cb_info_t *next;
70 static alarm_cb_info_t *alarmcb_head = NULL;
72 guint registration_id = 0;
74 static GDBusNodeInfo *introspection_data = NULL;
76 static const gchar introspection_xml[] =
77 "<node name='/com/samsung/alarm/client'>"
78 " <interface name='com.samsung.alarm.client'>"
79 " <method name='alarm_expired'>"
80 " <arg type='i' name='alarm_id' direction='in'/>"
81 " <arg type='s' name='service_name' direction='in'/>"
86 static const GDBusInterfaceVTable interface_vtable =
88 __handle_expiry_method_call,
93 static void __add_resultcb(alarm_id_t alarm_id, alarm_cb_t cb_func, void *data)
95 alarm_cb_info_t *info;
97 info = (alarm_cb_info_t *) malloc(sizeof(alarm_cb_info_t));
100 info->alarm_id = alarm_id;
101 info->cb_func = cb_func;
102 info->priv_data = data;
104 info->next = alarmcb_head;
108 static alarm_cb_info_t *__find_resultcb(alarm_id_t alarm_id)
110 alarm_cb_info_t *tmp;
114 if (tmp->alarm_id == alarm_id) {
115 ALARM_MGR_LOG_PRINT("matched alarm id = %d", alarm_id);
123 static void __remove_resultcb(alarm_cb_info_t *info)
125 alarm_cb_info_t *tmp;
127 if (alarmcb_head == NULL || info == NULL)
130 if (alarmcb_head == info) {
131 alarmcb_head = info->next;
138 if (tmp->next == info) {
139 tmp->next = info->next;
147 static void __handle_expiry_method_call(GDBusConnection *conn,
148 const gchar *name, const gchar *path, const gchar *interface,
149 const gchar *method, GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
151 if (method && strcmp(method, "alarm_expired") == 0) {
152 const gchar *package_name = NULL;
153 alarm_id_t alarm_id = 0;
154 alarm_cb_info_t *info = NULL;
155 g_variant_get(param, "(is)", &alarm_id, &package_name);
156 ALARM_MGR_EXCEPTION_PRINT("[alarm-lib] : Alarm expired for [%s] : Alarm id [%d]", package_name, alarm_id);
158 if (alarm_context.alarm_handler != NULL) {
159 alarm_context.alarm_handler(alarm_id, alarm_context.user_param);
162 info = __find_resultcb(alarm_id);
163 if (info && info->cb_func) {
164 info->cb_func(alarm_id, info->priv_data);
166 g_free(package_name);
170 static int __alarm_validate_date(alarm_date_t *date, int *error_code)
173 if (date->year == 0 && date->month == 0 && date->day == 0) {
177 int year = date->year;
178 int month = date->month;
181 if (month < 1 || month > 12) {
183 *error_code = ERR_ALARM_INVALID_DATE;
187 if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8
188 || month == 10 || month == 12)
189 && (day < 1 || day > 31)) {
191 *error_code = ERR_ALARM_INVALID_DATE;
195 if ((month == 4 || month == 6 || month == 9 || month == 11)
196 && (day < 1 || day > 30)) {
198 *error_code = ERR_ALARM_INVALID_DATE;
203 if ((year % 100 != 0 && year % 4 == 0) || (year % 400 == 0)) {
204 if (day < 1 || day > 29) {
206 *error_code = ERR_ALARM_INVALID_DATE;
210 if (day < 1 || day > 28) {
212 *error_code = ERR_ALARM_INVALID_DATE;
223 static bool __alarm_validate_time(alarm_date_t *date, int *error_code)
225 if (date->hour < 0 || date->hour > 23) {
227 *error_code = ERR_ALARM_INVALID_TIME;
231 if (date->min < 0 || date->min > 59) {
233 *error_code = ERR_ALARM_INVALID_TIME;
240 static int __sub_init()
242 GError *error = NULL;
243 char proc_file[MAX_PROC_NAME_LEN] = {0, };
244 char process_name[MAX_PROC_NAME_LEN] = {0, };
247 const int MAX_LEN = MAX_PROC_NAME_LEN;
249 pthread_mutex_lock(&init_lock);
251 if (sub_initialized) {
252 pthread_mutex_unlock(&init_lock);
253 return ALARMMGR_RESULT_SUCCESS;
259 alarm_context.connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
260 if (alarm_context.connection == NULL) {
261 ALARM_MGR_EXCEPTION_PRINT("g_bus_get_sync() is failed. error: %s", error->message);
263 pthread_mutex_unlock(&init_lock);
264 return ERR_ALARM_SYSTEM_FAIL;
267 alarm_context.proxy = g_dbus_proxy_new_sync(alarm_context.connection,
268 G_DBUS_PROXY_FLAGS_NONE,
270 "com.samsung.alarm.manager",
271 "/com/samsung/alarm/manager",
272 "com.samsung.alarm.manager",
276 if (alarm_context.proxy == NULL) {
277 ALARM_MGR_EXCEPTION_PRINT("Creating a proxy is failed.");
278 g_object_unref (alarm_context.connection);
279 pthread_mutex_unlock(&init_lock);
280 return ERR_ALARM_SYSTEM_FAIL;
283 // Only webapp which has a pid of WebProcess uses the sid. Otherwise, the pid is used.
284 snprintf(proc_file, MAX_LEN, "/proc/%d/cmdline", getpid());
285 fd = open(proc_file, O_RDONLY);
287 SECURE_LOGE("Unable to get the proc file(%d).\n", getpid());
288 g_object_unref(alarm_context.proxy);
289 g_object_unref(alarm_context.connection);
290 pthread_mutex_unlock(&init_lock);
291 return ERR_ALARM_SYSTEM_FAIL;
294 ret = read(fd, process_name, MAX_LEN);
297 ALARM_MGR_EXCEPTION_PRINT("Unable to read the proc file(%d).", getpid());
298 g_object_unref(alarm_context.proxy);
299 g_object_unref(alarm_context.connection);
300 pthread_mutex_unlock(&init_lock);
301 return ERR_ALARM_SYSTEM_FAIL;
304 if (strncmp(process_name, "/usr/bin/WebProcess", strlen("/usr/bin/WebProcess")) == 0) {
305 alarm_context.pid = getsid(getpid());
306 SECURE_LOGD("alarm_context.pid is set to sessionID, %d.", alarm_context.pid);
309 alarm_context.pid = getpid();
310 SECURE_LOGD("alarm_context.pid is set to processID, %d.", alarm_context.pid);
315 sub_initialized = true;
317 pthread_mutex_unlock(&init_lock);
319 return ALARMMGR_RESULT_SUCCESS;
322 EXPORT_API int alarmmgr_init(const char *appid)
324 SECURE_LOGD("Enter");
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 };
328 int ret = ALARMMGR_RESULT_SUCCESS;
335 return ERR_ALARM_INVALID_PARAM;
337 if (strlen(appid) >= MAX_PKG_NAME_LEN)
338 return ERR_ALARM_INVALID_PARAM;
341 SECURE_LOGD("alarm was already initialized. app_service_name=%s",
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);
352 len = strlen("ALARM.");
353 strncpy(service_name, "ALARM.", len);
354 strncpy(service_name + len, appid, strlen(appid));
356 for(i = 0; i <= strlen(service_name); i++) {
357 if (service_name[i] == '.') {
358 service_name_mod[j] = service_name[i];
362 service_name_mod[j] = service_name[i];
367 SECURE_LOGD("[alarm-lib]: dbus own name: %s", service_name_mod);
368 owner_id = g_bus_own_name_on_connection(alarm_context.connection, service_name_mod,
369 G_BUS_NAME_OWNER_FLAGS_NONE, NULL, NULL, NULL, NULL);
371 ALARM_MGR_EXCEPTION_PRINT("Acquiring the own name is failed. %s", service_name_mod);
375 introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
376 if (introspection_data == NULL) {
377 ALARM_MGR_EXCEPTION_PRINT("g_dbus_node_info_new_for_xml() is failed.");
381 registration_id = g_dbus_connection_register_object(alarm_context.connection,
382 "/com/samsung/alarm/client",
383 introspection_data->interfaces[0],
384 &interface_vtable, NULL, NULL, NULL);
385 if (registration_id == 0) {
386 ALARM_MGR_EXCEPTION_PRINT("Registering the callback is failed.");
390 alarm_context.quark_app_service_name = g_quark_from_string(service_name);
391 alarm_context.quark_app_service_name_mod= g_quark_from_string(service_name_mod);
393 b_initialized = true;
395 SECURE_LOGD("Leave");
396 return ALARMMGR_RESULT_SUCCESS;
399 if (introspection_data) {
400 g_dbus_node_info_unref(introspection_data);
402 if (registration_id != 0) {
403 g_dbus_connection_unregister_object(alarm_context.connection, registration_id);
405 g_object_unref(alarm_context.proxy);
406 alarm_context.proxy = NULL;
408 g_object_unref(alarm_context.connection);
409 alarm_context.connection = NULL;
411 sub_initialized = false;
412 return ERR_ALARM_INVALID_PARAM;
415 EXPORT_API void alarmmgr_fini()
417 SECURE_LOGD("Enter");
418 if (introspection_data) {
419 g_dbus_node_info_unref(introspection_data);
422 if (alarm_context.connection != NULL && registration_id != 0) {
423 g_dbus_connection_unregister_object(alarm_context.connection, registration_id);
426 if (alarm_context.proxy) {
427 g_object_unref(alarm_context.proxy);
428 alarm_context.proxy = NULL;
431 if (alarm_context.connection) {
432 g_object_unref(alarm_context.connection);
433 alarm_context.connection = NULL;
436 b_initialized = false;
437 sub_initialized = false;
439 SECURE_LOGD("Leave");
442 EXPORT_API int alarmmgr_set_cb(alarm_cb_t handler, void *user_param)
444 SECURE_LOGD("Enter");
446 if (handler == NULL) {
447 ALARM_MGR_EXCEPTION_PRINT("callback is NULL.");
448 return ERR_ALARM_INVALID_PARAM;
450 alarm_context.alarm_handler = handler;
451 alarm_context.user_param = user_param;
453 SECURE_LOGD("Leave");
454 return ALARMMGR_RESULT_SUCCESS;
457 EXPORT_API alarm_entry_t *alarmmgr_create_alarm(void)
459 alarm_info_t *alarm = (alarm_info_t *) malloc(sizeof(alarm_info_t));
466 alarm->start.year = 0;
467 alarm->start.month = 0;
468 alarm->start.day = 0;
469 alarm->start.hour = 0;
470 alarm->start.min = 0;
471 alarm->start.sec = 0;
474 alarm->end.month = 0;
480 alarm->mode.repeat = ALARM_REPEAT_MODE_ONCE;
481 alarm->mode.u_interval.interval = 0;
483 alarm->alarm_type = ALARM_TYPE_DEFAULT;
485 alarm->reserved_info = 0;
487 return (alarm_entry_t *) alarm;
490 EXPORT_API int alarmmgr_free_alarm(alarm_entry_t *alarm)
493 return ERR_ALARM_INVALID_PARAM;
497 return ALARMMGR_RESULT_SUCCESS;
500 EXPORT_API int alarmmgr_set_time(alarm_entry_t *alarm, alarm_date_t time)
502 alarm_info_t *alarm_info; /*= (alarm_info_t*)alarm;*/
506 return ERR_ALARM_INVALID_PARAM;
509 alarm_info = (alarm_info_t *) alarm;
511 if (!__alarm_validate_date(&time, &error_code)) {
512 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
516 if (!__alarm_validate_time(&time, &error_code)) {
517 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
521 memcpy(&alarm_info->start, &time, sizeof(alarm_date_t));
523 return ALARMMGR_RESULT_SUCCESS;
526 EXPORT_API int alarmmgr_get_time(const alarm_entry_t *alarm,
529 alarm_info_t *alarm_info = (alarm_info_t *) alarm;
532 return ERR_ALARM_INVALID_PARAM;
536 memcpy(time, &alarm_info->start, sizeof(alarm_date_t));
538 return ALARMMGR_RESULT_SUCCESS;
541 EXPORT_API int alarmmgr_set_repeat_mode(alarm_entry_t *alarm,
542 alarm_repeat_mode_t repeat,
545 alarm_info_t *alarm_info = (alarm_info_t *) alarm;
547 if (repeat >= ALARM_REPEAT_MODE_MAX) {
548 return ERR_ALARM_INVALID_PARAM;
551 alarm_info->mode.repeat = repeat;
553 if (repeat == ALARM_REPEAT_MODE_REPEAT
554 || repeat == ALARM_REPEAT_MODE_WEEKLY) {
555 alarm_info->mode.u_interval.interval = interval;
558 return ALARMMGR_RESULT_SUCCESS;
561 EXPORT_API int alarmmgr_get_repeat_mode(const alarm_entry_t *alarm,
562 alarm_repeat_mode_t *repeat,
565 alarm_info_t *alarm_info = (alarm_info_t *) alarm;
568 return ERR_ALARM_INVALID_PARAM;
572 *repeat = alarm_info->mode.repeat;
573 if (interval != NULL)
574 *interval = alarm_info->mode.u_interval.interval;
576 return ALARMMGR_RESULT_SUCCESS;
579 EXPORT_API int alarmmgr_set_type(alarm_entry_t *alarm, int alarm_type)
581 alarm_info_t *alarm_info; /*= (alarm_info_t*)alarm;*/
584 return ERR_ALARM_INVALID_PARAM;
587 alarm_info = (alarm_info_t *) alarm;
589 alarm_info->alarm_type = alarm_type;
590 alarm_info->alarm_type &= (~ALARM_TYPE_RELATIVE);
592 return ALARMMGR_RESULT_SUCCESS;
595 EXPORT_API int alarmmgr_get_type(const alarm_entry_t *alarm, int *alarm_type)
597 alarm_info_t *alarm_info = (alarm_info_t *) alarm;
600 return ERR_ALARM_INVALID_PARAM;
603 if (alarm_type != NULL)
604 *alarm_type = alarm_info->alarm_type;
606 return ALARMMGR_RESULT_SUCCESS;
610 static int __alarmmgr_init_appsvc(void)
615 ALARM_MGR_EXCEPTION_PRINT("alarm was already initialized\n");
616 return ALARMMGR_RESULT_SUCCESS;
621 dbus_g_thread_init();
627 b_initialized = true;
629 return ALARMMGR_RESULT_SUCCESS;
633 EXPORT_API void *alarmmgr_get_alarm_appsvc_info(alarm_id_t alarm_id, int *return_code){
645 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_get_alarm_appsvc_info() is called.");
649 *return_code = ERR_ALARM_INVALID_ID;
654 return _send_alarm_get_appsvc_info(alarm_context, alarm_id, return_code);
658 EXPORT_API int alarmmgr_set_rtc_time(alarm_date_t *time){
664 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter time\n");
665 return ERR_ALARM_INVALID_PARAM;
673 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_set_rtc_time() is called\n");
675 if (!__alarm_validate_date(time, &error_code)) {
676 ALARM_MGR_EXCEPTION_PRINT("RTC date error\n");
680 if (!__alarm_validate_time(time, &error_code)) {
681 ALARM_MGR_EXCEPTION_PRINT("RTC time error\n");
688 if (!_send_alarm_set_rtc_time
689 (alarm_context, time, &error_code)){
693 return ALARMMGR_RESULT_SUCCESS;
697 EXPORT_API int alarmmgr_add_alarm_appsvc_with_localtime(alarm_entry_t *alarm, void *bundle_data, alarm_id_t *alarm_id)
699 alarm_info_t *alarm_info = NULL; /* = (alarm_info_t*)alarm; */
700 const char *operation = NULL;
702 const char *appid = NULL;
704 bundle *b=(bundle *)bundle_data;
706 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
709 return ERR_ALARM_INVALID_PARAM;
714 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle\n");
715 return ERR_ALARM_INVALID_PARAM;
717 operation = appsvc_get_operation(b);
720 if (NULL == operation)
722 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle [appsvc operation not present]\n");
723 return ERR_ALARM_INVALID_PARAM;
726 if (__alarmmgr_init_appsvc() < 0)
728 ALARM_MGR_EXCEPTION_PRINT("Unable to initialize dbus!!!\n");
729 return ERR_ALARM_SYSTEM_FAIL;
732 alarm_info = (alarm_info_t *) alarm;
734 appid = appsvc_get_appid(b);
736 if (NULL == appid && (alarm_info->alarm_type & ALARM_TYPE_NOLAUNCH) )
738 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter\n");
739 return ERR_ALARM_INVALID_PARAM;
742 if (alarm_info == NULL || alarm_id == NULL) {
743 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter\n");
744 return ERR_ALARM_INVALID_PARAM;
746 alarm_mode_t *mode = &alarm_info->mode;
748 ALARM_MGR_EXCEPTION_PRINT("start(%d-%d-%d, %02d:%02d:%02d), end(%d-%d-%d), repeat(%d), interval(%d), type(%d)",
749 alarm_info->start.day, alarm_info->start.month, alarm_info->start.year,
750 alarm_info->start.hour, alarm_info->start.min, alarm_info->start.sec,
751 alarm_info->end.year, alarm_info->end.month, alarm_info->end.day,
752 alarm_info->mode.repeat, alarm_info->mode.u_interval, alarm_info->alarm_type);
754 /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
755 if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
756 return ERR_ALARM_INVALID_PARAM;
759 if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
760 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
764 if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
765 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
769 if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
770 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
775 if (!_send_alarm_create_appsvc(alarm_context, alarm_info, alarm_id, b, &error_code)) {
779 return ALARMMGR_RESULT_SUCCESS;
785 EXPORT_API int alarmmgr_add_alarm_with_localtime(alarm_entry_t *alarm,
786 const char *destination,
787 alarm_id_t *alarm_id)
789 char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
790 char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = { 0 };
791 alarm_info_t *alarm_info; /* = (alarm_info_t*)alarm; */
796 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
799 return ERR_ALARM_INVALID_PARAM;
802 alarm_info = (alarm_info_t *) alarm;
803 if (alarm_info == NULL || alarm_id == NULL) {
804 return ERR_ALARM_INVALID_PARAM;
808 alarm_mode_t *mode = &alarm_info->mode;
814 ALARM_MGR_EXCEPTION_PRINT("start(%d-%d-%d, %02d:%02d:%02d), end(%d-%d-%d), repeat(%d), interval(%d), type(%d)",
815 alarm_info->start.day, alarm_info->start.month, alarm_info->start.year,
816 alarm_info->start.hour, alarm_info->start.min, alarm_info->start.sec,
817 alarm_info->end.year, alarm_info->end.month, alarm_info->end.day,
818 alarm_info->mode.repeat, alarm_info->mode.u_interval, alarm_info->alarm_type);
820 /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
821 if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
822 return ERR_ALARM_INVALID_PARAM;
825 if (destination && strlen(destination) >= MAX_PKG_NAME_LEN){
826 ALARM_MGR_EXCEPTION_PRINT("[alarm-lib]: destination name is too long!\n");
827 return ERR_ALARM_INVALID_PARAM;
831 if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
832 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
836 if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
837 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
841 if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
842 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
846 if (destination != NULL) {
847 memset(dst_service_name, 0, strlen(destination) + strlen("ALARM.") + 2);
848 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "ALARM.%s", destination);
849 memset(dst_service_name_mod, 'a', MAX_SERVICE_NAME_LEN-1);
851 for (i=0; i<=strlen(dst_service_name); i++)
853 if (dst_service_name[i] == '.' )
855 dst_service_name_mod[j] = dst_service_name[i];
860 dst_service_name_mod[j] = dst_service_name[i];
865 if (!_send_alarm_create(alarm_context, alarm_info, alarm_id, dst_service_name, dst_service_name_mod, &error_code)) {
869 if (!_send_alarm_create(alarm_context, alarm_info, alarm_id, "null", "null", &error_code)) {
874 return ALARMMGR_RESULT_SUCCESS;
879 EXPORT_API int alarmmgr_add_alarm_appsvc(int alarm_type, time_t trigger_at_time,
880 time_t interval, void *bundle_data,
881 alarm_id_t *alarm_id)
884 struct timeval current_time;
885 struct tm duetime_tm;
886 alarm_info_t alarm_info;
887 const char *operation = NULL;
890 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
892 bundle *b=(bundle *)bundle_data;
896 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle\n");
897 return ERR_ALARM_INVALID_PARAM;
899 operation = appsvc_get_operation(b);
901 if (NULL == operation)
903 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle [appsvc operation not present]\n");
904 return ERR_ALARM_INVALID_PARAM;
907 appid = appsvc_get_appid(b);
909 if (NULL == appid && (alarm_type & ALARM_TYPE_NOLAUNCH) )
911 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter\n");
912 return ERR_ALARM_INVALID_PARAM;
915 if (__alarmmgr_init_appsvc() < 0)
917 ALARM_MGR_EXCEPTION_PRINT("Unable to initialize dbus!!!\n");
918 return ERR_ALARM_SYSTEM_FAIL;
921 if (alarm_id == NULL) {
922 return ERR_ALARM_INVALID_PARAM;
925 if (trigger_at_time < 0) {
926 return ERR_ALARM_INVALID_PARAM;
929 alarm_info.alarm_type = alarm_type;
930 alarm_info.alarm_type |= ALARM_TYPE_RELATIVE;
932 gettimeofday(¤t_time, NULL);
934 if (current_time.tv_usec > 500 * 1000)
936 // When the millisecond part of the current_time is bigger than 500ms,
937 // the duetime increases by extra 1sec.
938 current_time.tv_sec += (trigger_at_time + 1);
942 current_time.tv_sec += trigger_at_time;
945 tzset(); // Processes the TZ environment variable, and Set timezone, daylight, and tzname.
946 localtime_r(¤t_time.tv_sec, &duetime_tm);
948 alarm_info.start.year = duetime_tm.tm_year + 1900;
949 alarm_info.start.month = duetime_tm.tm_mon + 1;
950 alarm_info.start.day = duetime_tm.tm_mday;
952 alarm_info.end.year = 0;
953 alarm_info.end.month = 0;
954 alarm_info.end.day = 0;
956 alarm_info.start.hour = duetime_tm.tm_hour;
957 alarm_info.start.min = duetime_tm.tm_min;
958 alarm_info.start.sec = duetime_tm.tm_sec;
961 alarm_info.mode.repeat = ALARM_REPEAT_MODE_ONCE;
962 alarm_info.mode.u_interval.interval = 0;
964 alarm_info.mode.repeat = ALARM_REPEAT_MODE_REPEAT;
965 alarm_info.mode.u_interval.interval = interval;
968 ALARM_MGR_EXCEPTION_PRINT("trigger_at_time(%d), start(%d-%d-%d, %02d:%02d:%02d), repeat(%d), interval(%d), type(%d)",
969 trigger_at_time, alarm_info.start.day, alarm_info.start.month, alarm_info.start.year,
970 alarm_info.start.hour, alarm_info.start.min, alarm_info.start.sec,
971 alarm_info.mode.repeat, alarm_info.mode.u_interval, alarm_info.alarm_type);
973 if (!_send_alarm_create_appsvc(alarm_context, &alarm_info, alarm_id, b, &error_code)) {
977 return ALARMMGR_RESULT_SUCCESS;
981 EXPORT_API int alarmmgr_add_alarm(int alarm_type, time_t trigger_at_time,
982 time_t interval, const char *destination,
983 alarm_id_t *alarm_id)
985 char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
986 char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = { 0 };
990 struct timeval current_time;
991 struct tm duetime_tm;
992 alarm_info_t alarm_info;
995 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
1001 if (alarm_id == NULL) {
1002 return ERR_ALARM_INVALID_PARAM;
1005 if (trigger_at_time < 0) {
1006 return ERR_ALARM_INVALID_PARAM;
1009 if (destination && strlen(destination) >= MAX_PKG_NAME_LEN){
1010 ALARM_MGR_EXCEPTION_PRINT("[alarm-lib]: destination name is too long!\n");
1011 return ERR_ALARM_INVALID_PARAM;
1014 alarm_info.alarm_type = alarm_type;
1015 alarm_info.alarm_type |= ALARM_TYPE_RELATIVE;
1017 gettimeofday(¤t_time, NULL);
1019 if (current_time.tv_usec > 500 * 1000)
1021 // When the millisecond part of the current_time is bigger than 500ms,
1022 // the duetime increases by extra 1sec.
1023 current_time.tv_sec += (trigger_at_time + 1);
1027 current_time.tv_sec += trigger_at_time;
1030 tzset(); // Processes the TZ environment variable, and Set timezone, daylight, and tzname.
1031 localtime_r(¤t_time.tv_sec, &duetime_tm);
1033 alarm_info.start.year = duetime_tm.tm_year + 1900;
1034 alarm_info.start.month = duetime_tm.tm_mon + 1;
1035 alarm_info.start.day = duetime_tm.tm_mday;
1037 alarm_info.end.year = 0;
1038 alarm_info.end.month = 0;
1039 alarm_info.end.day = 0;
1041 alarm_info.start.hour = duetime_tm.tm_hour;
1042 alarm_info.start.min = duetime_tm.tm_min;
1043 alarm_info.start.sec = duetime_tm.tm_sec;
1045 if (interval <= 0) {
1046 alarm_info.mode.repeat = ALARM_REPEAT_MODE_ONCE;
1047 alarm_info.mode.u_interval.interval = 0;
1049 alarm_info.mode.repeat = ALARM_REPEAT_MODE_REPEAT;
1050 alarm_info.mode.u_interval.interval = interval;
1053 ALARM_MGR_EXCEPTION_PRINT("trigger_at_time(%d), start(%d-%d-%d, %02d:%02d:%02d), repeat(%d), interval(%d), type(%d)",
1054 trigger_at_time, alarm_info.start.day, alarm_info.start.month, alarm_info.start.year,
1055 alarm_info.start.hour, alarm_info.start.min, alarm_info.start.sec,
1056 alarm_info.mode.repeat, alarm_info.mode.u_interval, alarm_info.alarm_type);
1058 if (destination != NULL) {
1059 memset(dst_service_name, 0,
1060 strlen(destination) + strlen("ALARM.") + 2);
1061 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "ALARM.%s",
1063 memset(dst_service_name_mod,'a',MAX_SERVICE_NAME_LEN-1);
1067 for(i=0;i<=strlen(dst_service_name);i++)
1069 if (dst_service_name[i] == '.')
1071 dst_service_name_mod[j]=dst_service_name[i];
1076 dst_service_name_mod[j]=dst_service_name[i];
1081 if (!_send_alarm_create
1082 (alarm_context, &alarm_info, alarm_id, dst_service_name,dst_service_name_mod,
1087 if (!_send_alarm_create
1088 (alarm_context, &alarm_info, alarm_id, "null","null", &error_code)) {
1092 return ALARMMGR_RESULT_SUCCESS;
1095 EXPORT_API int alarmmgr_add_alarm_withcb(int alarm_type, time_t trigger_at_time,
1096 time_t interval, alarm_cb_t handler, void *user_param, alarm_id_t *alarm_id)
1098 char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
1099 char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = { 0 };
1103 struct timeval current_time;
1104 struct tm duetime_tm;
1105 alarm_info_t alarm_info;
1107 char appid[256] = {0,};
1109 if (aul_app_get_appid_bypid(getpid(), appid, sizeof(appid)) != AUL_R_OK) {
1110 ALARM_MGR_LOG_PRINT("aul_app_get_appid_bypid() is failed. PID %d may not be app.", getpid());
1113 ret = alarmmgr_init(appid);
1117 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_add_alarm_withcb() is called");
1119 if (alarm_id == NULL) {
1120 return ERR_ALARM_INVALID_PARAM;
1123 if (trigger_at_time < 0) {
1124 return ERR_ALARM_INVALID_PARAM;
1127 alarm_info.alarm_type = alarm_type;
1128 alarm_info.alarm_type |= ALARM_TYPE_RELATIVE;
1129 alarm_info.alarm_type |= ALARM_TYPE_WITHCB;
1131 gettimeofday(¤t_time, NULL);
1133 if (current_time.tv_usec > 500 * 1000)
1135 // When the millisecond part of the current_time is bigger than 500ms,
1136 // the duetime increases by extra 1sec.
1137 current_time.tv_sec += (trigger_at_time + 1);
1141 current_time.tv_sec += trigger_at_time;
1144 tzset(); // Processes the TZ environment variable, and Set timezone, daylight, and tzname.
1145 localtime_r(¤t_time.tv_sec, &duetime_tm);
1147 alarm_info.start.year = duetime_tm.tm_year + 1900;
1148 alarm_info.start.month = duetime_tm.tm_mon + 1;
1149 alarm_info.start.day = duetime_tm.tm_mday;
1151 alarm_info.end.year = 0;
1152 alarm_info.end.month = 0;
1153 alarm_info.end.day = 0;
1155 alarm_info.start.hour = duetime_tm.tm_hour;
1156 alarm_info.start.min = duetime_tm.tm_min;
1157 alarm_info.start.sec = duetime_tm.tm_sec;
1159 if (interval <= 0) {
1160 alarm_info.mode.repeat = ALARM_REPEAT_MODE_ONCE;
1161 alarm_info.mode.u_interval.interval = 0;
1163 alarm_info.mode.repeat = ALARM_REPEAT_MODE_REPEAT;
1164 alarm_info.mode.u_interval.interval = interval;
1167 ALARM_MGR_EXCEPTION_PRINT("trigger_at_time(%d), start(%d-%d-%d, %02d:%02d:%02d), repeat(%d), interval(%d), type(%d)",
1168 trigger_at_time, alarm_info.start.day, alarm_info.start.month, alarm_info.start.year,
1169 alarm_info.start.hour, alarm_info.start.min, alarm_info.start.sec,
1170 alarm_info.mode.repeat, alarm_info.mode.u_interval.interval, alarm_info.alarm_type);
1172 if (!_send_alarm_create(alarm_context, &alarm_info, alarm_id, "null","null", &error_code)) {
1175 __add_resultcb(*alarm_id, handler, user_param);
1177 return ALARMMGR_RESULT_SUCCESS;
1180 EXPORT_API int alarmmgr_remove_alarm(alarm_id_t alarm_id)
1184 alarm_cb_info_t *info;
1190 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_delete(%d) is called\n", alarm_id);
1192 if (alarm_id <= 0) {
1193 return ERR_ALARM_INVALID_ID;
1196 if (!_send_alarm_delete(alarm_context, alarm_id, &error_code))
1199 info = __find_resultcb(alarm_id);
1200 __remove_resultcb(info);
1202 return ALARMMGR_RESULT_SUCCESS;
1205 EXPORT_API int alarmmgr_remove_all(void)
1208 int return_code = ALARMMGR_RESULT_SUCCESS;
1209 int ret = __sub_init();
1215 if (!_send_alarm_delete_all(alarm_context, &error_code))
1221 EXPORT_API int alarmmgr_enum_alarm_ids(alarm_enum_fn_t fn, void *user_param)
1223 SECURE_LOGD("Enter");
1224 GError *error = NULL;
1225 GVariant *alarm_array = NULL;
1226 int return_code = 0;
1227 int maxnum_of_ids = 0;
1229 alarm_id_t alarm_id = -1;
1231 char *e_cookie = NULL;
1232 char cookie[256] = {0,};
1234 GVariantIter *iter = NULL;
1237 return ERR_ALARM_INVALID_PARAM;
1240 size = security_server_get_cookie_size();
1241 ret = security_server_request_cookie(cookie, size);
1243 ALARM_MGR_EXCEPTION_PRINT("security_server_request_cookie() is failed.");
1244 return ERR_ALARM_SYSTEM_FAIL;
1247 e_cookie = g_base64_encode((const guchar *)cookie, size);
1248 if (e_cookie == NULL) {
1249 ALARM_MGR_EXCEPTION_PRINT("g_base64_encode() is failed.");
1250 return ERR_ALARM_SYSTEM_FAIL;
1255 ALARM_MGR_EXCEPTION_PRINT("__sub_init() is failed.");
1260 SECURE_LOGD("alarm_manager_call_alarm_get_number_of_ids_sync() is called");
1261 if (!alarm_manager_call_alarm_get_number_of_ids_sync(
1262 (AlarmManager*)alarm_context.proxy, alarm_context.pid, e_cookie, &maxnum_of_ids, &return_code, NULL, &error)) {
1263 /* dbus-glib error */
1264 /* error_code should be set */
1265 ALARM_MGR_EXCEPTION_PRINT(
1266 "alarm_manager_call_alarm_get_number_of_ids_sync() is failed by dbus. return_code[%d], err message[%s]",
1267 return_code, error->message);
1269 return ERR_ALARM_SYSTEM_FAIL;
1274 if (return_code != ALARMMGR_RESULT_SUCCESS) {
1275 ALARM_MGR_EXCEPTION_PRINT("alarm_manager_call_alarm_get_number_of_ids_sync() is failed. return_code[%d]", return_code);
1278 ALARM_MGR_LOG_PRINT("maxnum_of_ids[%d]", maxnum_of_ids);
1281 SECURE_LOGD("alarm_manager_call_alarm_get_list_of_ids_sync() is called");
1282 if (!alarm_manager_call_alarm_get_list_of_ids_sync(
1283 (AlarmManager*)alarm_context.proxy, alarm_context.pid, maxnum_of_ids, &alarm_array, &num_of_ids, &return_code, NULL, &error)) {
1284 /*dbus-glib error */
1285 /* error_code should be set */
1286 ALARM_MGR_EXCEPTION_PRINT(
1287 "alarm_manager_call_alarm_get_list_of_ids_sync() failed by dbus. num_of_ids[%d], return_code[%d].", num_of_ids, return_code);
1288 return ERR_ALARM_SYSTEM_FAIL;
1291 if (return_code != ALARMMGR_RESULT_SUCCESS) {
1295 if (error != NULL) {
1296 ALARM_MGR_LOG_PRINT("Alarm server is not ready dbus. error message %s.", error->message);
1297 return ERR_ALARM_SYSTEM_FAIL;
1300 if (alarm_array == NULL) {
1301 ALARM_MGR_LOG_PRINT("alarm server is not initilized.");
1302 return ERR_ALARM_SYSTEM_FAIL;
1305 g_variant_get(alarm_array, "ai", &iter);
1306 while (g_variant_iter_loop(iter, "i", &alarm_id))
1308 (*fn) (alarm_id, user_param);
1309 ALARM_MGR_LOG_PRINT("alarm_id (%d)", alarm_id);
1311 g_variant_iter_free(iter);
1312 g_variant_unref(alarm_array);
1314 SECURE_LOGD("Leave");
1315 return ALARMMGR_RESULT_SUCCESS;
1318 EXPORT_API int alarmmgr_get_info(alarm_id_t alarm_id, alarm_entry_t *alarm)
1321 alarm_info_t *alarm_info = (alarm_info_t *) alarm;
1329 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_get_info() is called\n");
1331 if (alarm_id < 0 || alarm_info == NULL) {
1332 return ERR_ALARM_INVALID_PARAM;
1335 if (!_send_alarm_get_info(alarm_context, alarm_id, alarm_info, &error_code)) {
1339 return ALARMMGR_RESULT_SUCCESS;
1342 int alarmmgr_create(alarm_info_t *alarm_info, char *destination,
1343 alarm_id_t *alarm_id)
1345 char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
1346 alarm_mode_t *mode = &alarm_info->mode;
1349 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
1351 if (alarm_info == NULL || alarm_id == NULL) {
1352 return ERR_ALARM_INVALID_PARAM;
1355 ALARM_MGR_LOG_PRINT("alarm_info->start.year(%d), "
1356 "alarm_info->start.month(%d), alarm_info->start.day(%d)",
1357 alarm_info->start.year, alarm_info->start.month,
1358 alarm_info->start.day);
1360 /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
1361 if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
1362 return ERR_ALARM_INVALID_PARAM;
1365 if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
1366 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
1370 if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
1371 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
1375 if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
1376 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
1380 if (destination != NULL) {
1381 memset(dst_service_name, 0,
1382 strlen(destination) + strlen("ALARM.") + 2);
1383 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "ALARM.%s",
1385 if (!_send_alarm_create
1386 (alarm_context, alarm_info, alarm_id, dst_service_name,"null",
1391 /*TODO: Currently this API is not exported. Hence not modifying*/
1392 if (!_send_alarm_create
1393 (alarm_context, alarm_info, alarm_id, "null", "null", &error_code)) {
1397 return ALARMMGR_RESULT_SUCCESS;
1401 int alarmmgr_get_number_of_ids(int *num_of_ids)
1404 ALARM_MGR_LOG_PRINT("[alarm-lib]: alarm_get_number_of_ids() is called.");
1406 if (num_of_ids == NULL) {
1407 return ERR_ALARM_INVALID_PARAM;
1409 ALARM_MGR_LOG_PRINT("call alarm_get_number_of_ids\n");
1410 if (!_send_alarm_get_number_of_ids(alarm_context, num_of_ids, &error_code)) {
1414 return ALARMMGR_RESULT_SUCCESS;
1417 int alarmmgr_get_list_of_ids(int maxnum_of_ids, alarm_id_t *alarm_id,
1421 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_get_list_of_ids() is called.");
1423 if (maxnum_of_ids < 0 || alarm_id == NULL || num_of_ids == NULL) {
1424 return ERR_ALARM_INVALID_PARAM;
1427 if (maxnum_of_ids == 0) {
1429 return ALARMMGR_RESULT_SUCCESS;
1432 if (!_send_alarm_get_list_of_ids
1433 (alarm_context, maxnum_of_ids, alarm_id, num_of_ids, &error_code)) {
1437 return ALARMMGR_RESULT_SUCCESS;
1440 EXPORT_API int alarmmgr_get_next_duetime(alarm_id_t alarm_id, time_t* duetime)
1443 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_get_next_duetime() is called.");
1445 if (duetime == NULL) {
1446 return ERR_ALARM_INVALID_PARAM;
1449 if (!_send_alarm_get_next_duetime(alarm_context, alarm_id, duetime, &error_code)) {
1453 return ALARMMGR_RESULT_SUCCESS;
1456 EXPORT_API int alarmmgr_get_all_info(char **db_path)
1459 ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_get_all_info() is called.");
1461 if (db_path == NULL) {
1462 return ERR_ALARM_INVALID_PARAM;
1465 if (!_send_alarm_get_all_info(alarm_context, db_path, &error_code)) {
1469 ALARM_MGR_LOG_PRINT("[alarm-lib]: successfully save info in %s.", *db_path);
1470 return ALARMMGR_RESULT_SUCCESS;