tizen 2.4 release
[framework/appfw/alarm-manager.git] / src / alarm-lib.c
1 /*
2  *  alarm-manager
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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>
8  *
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
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <string.h>
30 #include <glib.h>
31 #include <fcntl.h>
32
33 #include "alarm.h"
34 #include "alarm-internal.h"
35 #include "alarm-mgr-stub.h"
36 #include <bundle.h>
37 #include <appsvc.h>
38 #include <aul.h>
39 #include <gio/gio.h>
40 #include <security-server.h>
41 #include <pkgmgr-info.h>
42
43 #define MAX_KEY_SIZE 256
44 #define MAX_PROC_NAME_LEN 512
45
46 static alarm_context_t alarm_context = { NULL, NULL, NULL, NULL, -1 };
47
48 static bool b_initialized = false;
49 static bool sub_initialized = false;
50
51 pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
52
53 static void __handle_expiry_method_call(GDBusConnection *conn,
54         const gchar *name, const gchar *path, const gchar *interface,
55         const gchar *method, GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data);
56
57 static int __alarm_validate_date(alarm_date_t *date, int *error_code);
58 static bool __alarm_validate_time(alarm_date_t *date, int *error_code);
59 static int __sub_init(void);
60 static int __alarmmgr_init_appsvc(void);
61
62 typedef struct _alarm_cb_info_t {
63         alarm_id_t alarm_id;
64         alarm_cb_t cb_func;
65         void *priv_data;
66         struct _alarm_cb_info_t *next;
67 } alarm_cb_info_t;
68
69 static alarm_cb_info_t *alarmcb_head = NULL;
70
71 guint registration_id = 0;
72
73 static GDBusNodeInfo *introspection_data = NULL;
74
75 static const gchar introspection_xml[] =
76   "<node name='/org/tizen/alarm/client'>"
77   "  <interface name='org.tizen.alarm.client'>"
78   "    <method name='alarm_expired'>"
79   "      <arg type='i' name='alarm_id' direction='in'/>"
80   "      <arg type='s' name='service_name' direction='in'/>"
81   "    </method>"
82   "  </interface>"
83   "</node>";
84
85 static const GDBusInterfaceVTable interface_vtable =
86 {
87         __handle_expiry_method_call,
88         NULL,
89         NULL
90 };
91
92 static int __bg_category_func(const char *name, void *user_data)
93 {
94         bg_category_cb_info_t *info = (bg_category_cb_info_t *)user_data;
95         ALARM_MGR_LOG_PRINT("appid[%s], bg name = %s", info->appid, name);
96         if (name &&
97                         strncmp("enable", name, strlen(name)) && strncmp("disable", name, strlen(name))) {
98                 info->has_bg = true;
99                 return -1;
100         }
101
102         return 0;
103 }
104
105 static void __add_resultcb(alarm_id_t alarm_id, alarm_cb_t cb_func, void *data)
106 {
107         alarm_cb_info_t *info;
108
109         info = (alarm_cb_info_t *) malloc(sizeof(alarm_cb_info_t));
110         if(info == NULL)
111                 return;
112         info->alarm_id = alarm_id;
113         info->cb_func = cb_func;
114         info->priv_data = data;
115
116         info->next = alarmcb_head;
117         alarmcb_head = info;
118 }
119
120 static alarm_cb_info_t *__find_resultcb(alarm_id_t alarm_id)
121 {
122         alarm_cb_info_t *tmp;
123
124         tmp = alarmcb_head;
125         while (tmp) {
126                 if (tmp->alarm_id == alarm_id) {
127                         ALARM_MGR_LOG_PRINT("matched alarm id =  %d", alarm_id);
128                         return tmp;
129                 }
130                 tmp = tmp->next;
131         }
132         return NULL;
133 }
134
135 static void __remove_resultcb(alarm_cb_info_t *info)
136 {
137         alarm_cb_info_t *tmp;
138
139         if (alarmcb_head == NULL || info == NULL)
140                 return;
141
142         if (alarmcb_head == info) {
143                 alarmcb_head = info->next;
144                 free(info);
145                 return;
146         }
147
148         tmp = alarmcb_head;
149         while (tmp) {
150                 if (tmp->next == info) {
151                         tmp->next = info->next;
152                         free(info);
153                         return;
154                 }
155                 tmp = tmp->next;
156         }
157 }
158
159 static void __handle_expiry_method_call(GDBusConnection *conn,
160                 const gchar *name, const gchar *path, const gchar *interface,
161                 const gchar *method, GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
162 {
163         if (method && strcmp(method, "alarm_expired") == 0) {
164                 gchar *package_name = NULL;
165                 alarm_id_t alarm_id = 0;
166                 alarm_cb_info_t *info = NULL;
167                 g_variant_get(param, "(is)", &alarm_id, &package_name);
168                 ALARM_MGR_EXCEPTION_PRINT("[alarm-lib] : Alarm expired for [%s] : Alarm id [%d]", package_name, alarm_id);
169
170                 if (alarm_context.alarm_handler != NULL) {
171                         alarm_context.alarm_handler(alarm_id, alarm_context.user_param);
172                 }
173
174                 info = __find_resultcb(alarm_id);
175                 if (info && info->cb_func) {
176                         info->cb_func(alarm_id, info->priv_data);
177                 }
178                 g_free(package_name);
179         }
180 }
181
182 static int __alarm_validate_date(alarm_date_t *date, int *error_code)
183 {
184
185         if (date->year == 0 && date->month == 0 && date->day == 0) {
186                 return true;
187         }
188
189         int year = date->year;
190         int month = date->month;
191         int day = date->day;
192
193         if (month < 1 || month > 12) {
194                 if (error_code)
195                         *error_code = ERR_ALARM_INVALID_DATE;
196                 return false;
197         }
198
199         if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8
200              || month == 10 || month == 12)
201             && (day < 1 || day > 31)) {
202                 if (error_code)
203                         *error_code = ERR_ALARM_INVALID_DATE;
204                 return false;
205         }
206
207         if ((month == 4 || month == 6 || month == 9 || month == 11)
208             && (day < 1 || day > 30)) {
209                 if (error_code)
210                         *error_code = ERR_ALARM_INVALID_DATE;
211                 return false;
212         }
213
214         if (month == 2) {
215                 if ((year % 100 != 0 && year % 4 == 0) || (year % 400 == 0)) {
216                         if (day < 1 || day > 29) {
217                                 if (error_code)
218                                         *error_code = ERR_ALARM_INVALID_DATE;
219                                 return false;
220                         }
221                 } else {
222                         if (day < 1 || day > 28) {
223                                 if (error_code)
224                                         *error_code = ERR_ALARM_INVALID_DATE;
225                                 return false;
226                         }
227                 }
228
229         }
230
231         return true;
232
233 }
234
235 static bool __alarm_validate_time(alarm_date_t *date, int *error_code)
236 {
237         if (date->hour < 0 || date->hour > 23) {
238                 if (error_code)
239                         *error_code = ERR_ALARM_INVALID_TIME;
240                 return false;
241         }
242
243         if (date->min < 0 || date->min > 59) {
244                 if (error_code)
245                         *error_code = ERR_ALARM_INVALID_TIME;
246                 return false;
247         }
248
249         return true;
250 }
251
252 static bool __is_permitted(const char *app_id, int alarm_type)
253 {
254         if (app_id == NULL) {
255                 ALARM_MGR_EXCEPTION_PRINT("app_id is NULL. Only expicit launch is permitted\n");
256                 return false;
257         }
258
259         pkgmgrinfo_appinfo_h handle = NULL;
260         int ret;
261         bool _return = false;
262
263         ret = pkgmgrinfo_appinfo_get_appinfo(app_id, &handle);
264         if (ret != PMINFO_R_OK) {
265                 ALARM_MGR_EXCEPTION_PRINT("Failed to get appinfo [%s]\n", app_id);
266         } else {
267                 char *app_type = NULL;
268                 ret = pkgmgrinfo_appinfo_get_component_type(handle, &app_type);
269                 if (app_type && strcmp("uiapp", app_type) == 0) {
270                         ALARM_MGR_LOG_PRINT("[%s] is ui application. It is allowed", app_id);
271                         _return = true;
272                         goto out;
273                 } else if (app_type && strcmp("svcapp", app_type) == 0) {
274                         ALARM_MGR_LOG_PRINT("[%s] is service application.", app_id);
275
276                         bg_category_cb_info_t info = {
277                                 .appid = app_id,
278                                 .has_bg = false
279                         };
280
281                         if (alarm_type & ALARM_TYPE_INEXACT) {
282                                 ret = pkgmgrinfo_appinfo_foreach_background_category(handle, __bg_category_func, &info);
283                                 if (ret == PMINFO_R_OK && info.has_bg) {
284                                         ALARM_MGR_LOG_PRINT("[%s] has background categories.", app_id);
285                                         _return = true;
286                                         goto out;
287                                 } else {
288                                         ALARM_MGR_EXCEPTION_PRINT("Failed to foreach background category. [%s] is not allowed\n", app_id);
289                                 }
290                         }
291                 }
292         }
293
294 out :
295         if (handle)
296                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
297         return _return;
298 }
299
300 static int __sub_init()
301 {
302         GError *error = NULL;
303         char proc_file[MAX_PROC_NAME_LEN] = {0, };
304         char process_name[MAX_PROC_NAME_LEN] = {0, };
305         int fd = 0;
306         int ret = 0;
307         const int MAX_LEN = MAX_PROC_NAME_LEN;
308
309         pthread_mutex_lock(&init_lock);
310
311         if (sub_initialized) {
312                 pthread_mutex_unlock(&init_lock);
313                 return ALARMMGR_RESULT_SUCCESS;
314         }
315
316 #if !GLIB_CHECK_VERSION(2,32,0)
317         g_thread_init(NULL);
318 #endif
319         g_type_init();
320
321         alarm_context.connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
322         if (alarm_context.connection == NULL) {
323                 ALARM_MGR_EXCEPTION_PRINT("g_bus_get_sync() is failed.");
324                 if (error) {
325                         ALARM_MGR_EXCEPTION_PRINT("dbus error message: %s", error->message);
326                         g_error_free(error);
327                 }
328                 pthread_mutex_unlock(&init_lock);
329                 return ERR_ALARM_SYSTEM_FAIL;
330         }
331
332         alarm_context.proxy = g_dbus_proxy_new_sync(alarm_context.connection,
333                                                         G_DBUS_PROXY_FLAGS_NONE,
334                                                         NULL,
335                                                         "org.tizen.alarm.manager",
336                                                         "/org/tizen/alarm/manager",
337                                                         "org.tizen.alarm.manager",
338                                                         NULL,
339                                                         NULL);
340
341         if (alarm_context.proxy == NULL) {
342                 ALARM_MGR_EXCEPTION_PRINT("Creating a proxy is failed.");
343                 g_object_unref (alarm_context.connection);
344                 pthread_mutex_unlock(&init_lock);
345                 return ERR_ALARM_SYSTEM_FAIL;
346         }
347
348         // Only webapp which has a pid of WebProcess uses the sid. Otherwise, the pid is used.
349         snprintf(proc_file, MAX_LEN, "/proc/%d/cmdline", getpid());
350         fd = open(proc_file, O_RDONLY);
351         if (fd < 0) {
352                 SECURE_LOGE("Unable to get the proc file(%d).\n", getpid());
353                 g_object_unref(alarm_context.proxy);
354                 g_object_unref(alarm_context.connection);
355                 pthread_mutex_unlock(&init_lock);
356                 return ERR_ALARM_SYSTEM_FAIL;
357         }
358         else {
359                 ret = read(fd, process_name, MAX_LEN - 1);
360                 close(fd);
361                 if (ret < 0) {
362                         ALARM_MGR_EXCEPTION_PRINT("Unable to read the proc file(%d).", getpid());
363                         g_object_unref(alarm_context.proxy);
364                         g_object_unref(alarm_context.connection);
365                         pthread_mutex_unlock(&init_lock);
366                         return ERR_ALARM_SYSTEM_FAIL;
367                 }
368                 else {
369                         if (strncmp(process_name, "/usr/bin/WebProcess", strlen("/usr/bin/WebProcess")) == 0) {
370                                 alarm_context.pid = getsid(getpid());
371                                 SECURE_LOGD("alarm_context.pid is set to sessionID, %d.", alarm_context.pid);
372                         }
373                         else {
374                                 alarm_context.pid = getpid();
375                                 SECURE_LOGD("alarm_context.pid is set to processID, %d.", alarm_context.pid);
376                         }
377                 }
378         }
379
380         sub_initialized = true;
381
382         pthread_mutex_unlock(&init_lock);
383
384         return ALARMMGR_RESULT_SUCCESS;
385 }
386
387 EXPORT_API int alarmmgr_init(const char *appid)
388 {
389         SECURE_LOGD("Enter");
390         char service_name[MAX_SERVICE_NAME_LEN] = { 0 };
391         char service_name_mod[MAX_SERVICE_NAME_LEN]= { 0 };
392         int ret = ALARMMGR_RESULT_SUCCESS;
393         guint owner_id = 0;
394         int i = 0;
395         int j = 0;
396         int len = 0;
397
398         if (appid == NULL)
399                 return ERR_ALARM_INVALID_PARAM;
400
401         if (strlen(appid) >= MAX_PKG_NAME_LEN)
402                 return ERR_ALARM_INVALID_PARAM;
403
404         if (b_initialized) {
405                 SECURE_LOGD("alarm was already initialized. app_service_name=%s",
406                      g_quark_to_string(alarm_context.quark_app_service_name));
407                 return ALARMMGR_RESULT_SUCCESS;
408         }
409
410         ret = __sub_init();
411         if (ret < 0)
412                 return ret;
413
414         memset(service_name_mod, 'a', MAX_SERVICE_NAME_LEN - 1);
415
416         len = strlen("ALARM.");
417         strncpy(service_name, "ALARM.", len);
418         strncpy(service_name + len, appid, strlen(appid));
419
420         for(i = 0; i <= strlen(service_name); i++) {
421                 if (service_name[i] == '.') {
422                         service_name_mod[j] = service_name[i];
423                         j++;
424                 }
425                 else {
426                         service_name_mod[j] = service_name[i];
427                 }
428                 j++;
429         }
430
431         SECURE_LOGD("[alarm-lib]: dbus own name: %s", service_name_mod);
432         owner_id = g_bus_own_name_on_connection(alarm_context.connection, service_name_mod,
433                                                                                 G_BUS_NAME_OWNER_FLAGS_NONE, NULL, NULL, NULL, NULL);
434         if (owner_id == 0) {
435                 ALARM_MGR_EXCEPTION_PRINT("Acquiring the own name is failed. %s", service_name_mod);
436                 goto error;
437         }
438
439         introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
440         if (introspection_data == NULL) {
441                 ALARM_MGR_EXCEPTION_PRINT("g_dbus_node_info_new_for_xml() is failed.");
442                 goto error;
443         }
444
445         registration_id = g_dbus_connection_register_object(alarm_context.connection,
446                                                                                                 "/org/tizen/alarm/client",
447                                                                                                 introspection_data->interfaces[0],
448                                                                                                 &interface_vtable, NULL, NULL, NULL);
449         if (registration_id == 0) {
450                 ALARM_MGR_EXCEPTION_PRINT("Registering the callback is failed.");
451                 goto error;
452         }
453
454         alarm_context.quark_app_service_name = g_quark_from_string(service_name);
455         alarm_context.quark_app_service_name_mod= g_quark_from_string(service_name_mod);
456
457         b_initialized = true;
458
459         SECURE_LOGD("Leave");
460         return ALARMMGR_RESULT_SUCCESS;
461
462 error:
463         if (introspection_data) {
464                 g_dbus_node_info_unref(introspection_data);
465         }
466         if (registration_id != 0) {
467                 g_dbus_connection_unregister_object(alarm_context.connection, registration_id);
468         }
469         g_object_unref(alarm_context.proxy);
470         alarm_context.proxy = NULL;
471
472         g_object_unref(alarm_context.connection);
473         alarm_context.connection = NULL;
474
475         sub_initialized = false;
476         return ERR_ALARM_INVALID_PARAM;
477 }
478
479 EXPORT_API void alarmmgr_fini()
480 {
481         SECURE_LOGD("Enter");
482         if (introspection_data) {
483                 g_dbus_node_info_unref(introspection_data);
484         }
485
486         if (alarm_context.connection != NULL && registration_id != 0) {
487                 g_dbus_connection_unregister_object(alarm_context.connection, registration_id);
488         }
489
490         if (alarm_context.proxy) {
491                 g_object_unref(alarm_context.proxy);
492                 alarm_context.proxy = NULL;
493         }
494
495         if (alarm_context.connection) {
496                 g_object_unref(alarm_context.connection);
497                 alarm_context.connection = NULL;
498         }
499
500         b_initialized = false;
501         sub_initialized = false;
502
503         SECURE_LOGD("Leave");
504 }
505
506 EXPORT_API int alarmmgr_set_cb(alarm_cb_t handler, void *user_param)
507 {
508         SECURE_LOGD("Enter");
509
510         if (handler == NULL) {
511                 ALARM_MGR_EXCEPTION_PRINT("callback is NULL.");
512                 return ERR_ALARM_INVALID_PARAM;
513         }
514         alarm_context.alarm_handler = handler;
515         alarm_context.user_param = user_param;
516
517         SECURE_LOGD("Leave");
518         return ALARMMGR_RESULT_SUCCESS;
519 }
520
521 EXPORT_API alarm_entry_t *alarmmgr_create_alarm(void)
522 {
523         alarm_info_t *alarm = (alarm_info_t *) malloc(sizeof(alarm_info_t));
524
525         if (NULL == alarm)
526         {
527                 return NULL;
528         }
529
530         alarm->start.year = 0;
531         alarm->start.month = 0;
532         alarm->start.day = 0;
533         alarm->start.hour = 0;
534         alarm->start.min = 0;
535         alarm->start.sec = 0;
536
537         alarm->end.year = 0;
538         alarm->end.month = 0;
539         alarm->end.day = 0;
540         alarm->end.hour = 0;
541         alarm->end.min = 0;
542         alarm->end.sec = 0;
543
544         alarm->mode.repeat = ALARM_REPEAT_MODE_ONCE;
545         alarm->mode.u_interval.interval = 0;
546
547         alarm->alarm_type = ALARM_TYPE_DEFAULT;
548
549         alarm->reserved_info = 0;
550
551         return (alarm_entry_t *) alarm;
552 }
553
554 EXPORT_API int alarmmgr_free_alarm(alarm_entry_t *alarm)
555 {
556         if (alarm == NULL) {
557                 return ERR_ALARM_INVALID_PARAM;
558         }
559         free(alarm);
560
561         return ALARMMGR_RESULT_SUCCESS;
562 }
563
564 EXPORT_API int alarmmgr_set_time(alarm_entry_t *alarm, alarm_date_t time)
565 {
566         alarm_info_t *alarm_info;       /*= (alarm_info_t*)alarm;*/
567         int error_code;
568
569         if (alarm == NULL) {
570                 return ERR_ALARM_INVALID_PARAM;
571         }
572
573         alarm_info = (alarm_info_t *) alarm;
574
575         if (!__alarm_validate_date(&time, &error_code)) {
576                 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
577                 return error_code;
578         }
579
580         if (!__alarm_validate_time(&time, &error_code)) {
581                 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
582                 return error_code;
583         }
584
585         memcpy(&alarm_info->start, &time, sizeof(alarm_date_t));
586
587         return ALARMMGR_RESULT_SUCCESS;
588 }
589
590 EXPORT_API int alarmmgr_get_time(const alarm_entry_t *alarm,
591                                  alarm_date_t *time)
592 {
593         alarm_info_t *alarm_info = (alarm_info_t *) alarm;
594
595         if (alarm == NULL) {
596                 return ERR_ALARM_INVALID_PARAM;
597         }
598
599         if (time != NULL)
600                 memcpy(time, &alarm_info->start, sizeof(alarm_date_t));
601
602         return ALARMMGR_RESULT_SUCCESS;
603 }
604
605 EXPORT_API int alarmmgr_set_repeat_mode(alarm_entry_t *alarm,
606                                         alarm_repeat_mode_t repeat,
607                                         int interval)
608 {
609         alarm_info_t *alarm_info = (alarm_info_t *) alarm;
610
611         if (repeat >= ALARM_REPEAT_MODE_MAX) {
612                 return ERR_ALARM_INVALID_PARAM;
613         }
614
615         alarm_info->mode.repeat = repeat;
616         if (repeat == ALARM_REPEAT_MODE_REPEAT || repeat == ALARM_REPEAT_MODE_WEEKLY) {
617                 if (interval <= 0) {
618                         return ERR_ALARM_INVALID_PARAM;
619                 }
620                 alarm_info->mode.u_interval.interval = interval;
621         }
622
623         return ALARMMGR_RESULT_SUCCESS;
624 }
625
626 EXPORT_API int alarmmgr_get_repeat_mode(const alarm_entry_t *alarm,
627                                         alarm_repeat_mode_t *repeat,
628                                         int *interval)
629 {
630         alarm_info_t *alarm_info = (alarm_info_t *) alarm;
631
632         if (alarm == NULL) {
633                 return ERR_ALARM_INVALID_PARAM;
634         }
635
636         if (repeat != NULL)
637                 *repeat = alarm_info->mode.repeat;
638         if (interval != NULL)
639                 *interval = alarm_info->mode.u_interval.interval;
640
641         return ALARMMGR_RESULT_SUCCESS;
642 }
643
644 EXPORT_API int alarmmgr_set_type(alarm_entry_t *alarm, int alarm_type)
645 {
646         alarm_info_t *alarm_info;       /*= (alarm_info_t*)alarm;*/
647
648         if (alarm == NULL) {
649                 return ERR_ALARM_INVALID_PARAM;
650         }
651
652         alarm_info = (alarm_info_t *) alarm;
653
654         alarm_info->alarm_type = alarm_type;
655         alarm_info->alarm_type &= (~ALARM_TYPE_RELATIVE);
656
657         return ALARMMGR_RESULT_SUCCESS;
658 }
659
660 EXPORT_API int alarmmgr_get_type(const alarm_entry_t *alarm, int *alarm_type)
661 {
662         alarm_info_t *alarm_info = (alarm_info_t *) alarm;
663
664         if (alarm == NULL) {
665                 return ERR_ALARM_INVALID_PARAM;
666         }
667
668         if (alarm_type != NULL)
669                 *alarm_type = alarm_info->alarm_type;
670
671         return ALARMMGR_RESULT_SUCCESS;
672 }
673
674
675 static int __alarmmgr_init_appsvc(void)
676 {
677         if (b_initialized) {
678                 ALARM_MGR_EXCEPTION_PRINT("alarm was already initialized.");
679                 return ALARMMGR_RESULT_SUCCESS;
680         }
681
682         int ret = __sub_init();
683         if (ret < 0)
684                 return ret;
685
686         b_initialized = true;
687         return ALARMMGR_RESULT_SUCCESS;
688 }
689
690 EXPORT_API void *alarmmgr_get_alarm_appsvc_info(alarm_id_t alarm_id, int *return_code){
691
692         int ret = 0;
693
694         ret = __sub_init();
695         if (ret < 0){
696                 if (return_code) {
697                         *return_code = ret;
698                 }
699                 return NULL;
700         }
701
702         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_get_alarm_appsvc_info() is called.");
703
704         if (alarm_id <= 0) {
705                 if (return_code) {
706                         *return_code = ERR_ALARM_INVALID_ID;
707                 }
708                 return NULL;
709         }
710
711         return _send_alarm_get_appsvc_info(alarm_context, alarm_id, return_code);
712
713 }
714
715 EXPORT_API int alarmmgr_set_rtc_time(alarm_date_t *time){
716
717         int ret = 0;
718         int error_code = 0;
719
720         if (!time){
721                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter time\n");
722                 return ERR_ALARM_INVALID_PARAM;
723         }
724
725         ret = __sub_init();
726         if (ret < 0){
727                 return ret;
728         }
729
730         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_set_rtc_time() is called\n");
731
732         if (!__alarm_validate_date(time, &error_code)) {
733                 ALARM_MGR_EXCEPTION_PRINT("RTC date error\n");
734                 return error_code;
735         }
736
737         if (!__alarm_validate_time(time, &error_code)) {
738                 ALARM_MGR_EXCEPTION_PRINT("RTC time error\n");
739                 return error_code;
740         }
741
742         time->year-=1900;
743         time->month-=1;
744
745         if (!_send_alarm_set_rtc_time
746                 (alarm_context, time, &error_code)){
747                         return error_code;
748         }
749
750         return ALARMMGR_RESULT_SUCCESS;
751
752 }
753
754 EXPORT_API int alarmmgr_add_alarm_appsvc_with_localtime(alarm_entry_t *alarm, void *bundle_data, alarm_id_t *alarm_id)
755 {
756         alarm_info_t *alarm_info = NULL;        /* = (alarm_info_t*)alarm; */
757         const char *operation = NULL;
758         int error_code = 0;
759         const char *appid = NULL;
760
761         bundle *b=(bundle *)bundle_data;
762
763         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
764
765         if (alarm == NULL) {
766                 return ERR_ALARM_INVALID_PARAM;
767         }
768
769         if (NULL == b)
770         {
771                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle\n");
772                 return ERR_ALARM_INVALID_PARAM;
773         }
774
775         operation = appsvc_get_operation(b);
776
777         if (NULL == operation)
778         {
779                 appsvc_set_operation(b, APPSVC_OPERATION_DEFAULT);
780         }
781
782         if (__alarmmgr_init_appsvc() < 0)
783         {
784                 ALARM_MGR_EXCEPTION_PRINT("Unable to initialize dbus!!!\n");
785                 return ERR_ALARM_SYSTEM_FAIL;
786         }
787
788         alarm_info = (alarm_info_t *) alarm;
789
790         appid = appsvc_get_appid(b);
791
792         if ( (NULL == appid && (alarm_info->alarm_type & ALARM_TYPE_NOLAUNCH)) ||
793                         (NULL == appid && operation && !strcmp(operation, APPSVC_OPERATION_DEFAULT)) )
794         {
795                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter\n");
796                 return ERR_ALARM_INVALID_PARAM;
797         }
798
799         // Checking api version
800         int ret;
801         int result = 0;
802         pkgmgrinfo_pkginfo_h pkginfo = NULL;
803         char pkgid[512] = {0, };
804         const char *api_version = "2.4";
805
806         if (aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid)) != AUL_R_OK) {
807                 ALARM_MGR_EXCEPTION_PRINT("aul_app_get_pkgid_bypid() is failed. PID %d may not be app.", getpid());
808         } else {
809                 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkginfo);
810                 if (ret != PMINFO_R_OK) {
811                         ALARM_MGR_EXCEPTION_PRINT("Failed to get pkginfo\n");
812                 }
813                 else {
814                         ret = pkgmgrinfo_pkginfo_check_api_version(pkginfo, api_version, &result);
815                         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
816                         if (ret) {
817                                 ALARM_MGR_EXCEPTION_PRINT("Failed to check api version [%d]\n", ret);
818                                 return ERR_ALARM_SYSTEM_FAIL;
819                         }
820                 }
821         }
822
823         if (result >= 0 && //Since 2.4
824                         !__is_permitted(appid, alarm_info->alarm_type)) {
825                 ALARM_MGR_EXCEPTION_PRINT("[%s] is not permitted \n", appid);
826                 return ERR_ALARM_NOT_PERMITTED_APP;
827         }
828
829         if (alarm_info == NULL || alarm_id == NULL) {
830                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter\n");
831                 return ERR_ALARM_INVALID_PARAM;
832         }
833         alarm_mode_t *mode = &alarm_info->mode;
834
835         ALARM_MGR_EXCEPTION_PRINT("start(%d-%d-%d, %02d:%02d:%02d), end(%d-%d-%d), repeat(%d), interval(%d), type(%d)",
836                 alarm_info->start.day, alarm_info->start.month, alarm_info->start.year,
837                 alarm_info->start.hour, alarm_info->start.min, alarm_info->start.sec,
838                 alarm_info->end.year, alarm_info->end.month, alarm_info->end.day,
839                 alarm_info->mode.repeat, alarm_info->mode.u_interval, alarm_info->alarm_type);
840
841         /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
842         if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
843                 return ERR_ALARM_INVALID_PARAM;
844         }
845
846         if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
847                 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
848                 return error_code;
849         }
850
851         if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
852                 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
853                 return error_code;
854         }
855
856         if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
857                 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
858                 return error_code;
859         }
860
861
862         if (!_send_alarm_create_appsvc(alarm_context, alarm_info, alarm_id, b, &error_code)) {
863                 return error_code;
864         }
865
866         return ALARMMGR_RESULT_SUCCESS;
867 }
868
869
870
871
872 EXPORT_API int alarmmgr_add_alarm_with_localtime(alarm_entry_t *alarm,
873                                                  const char *destination,
874                                                  alarm_id_t *alarm_id)
875 {
876         char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
877         char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = { 0 };
878         alarm_info_t *alarm_info;       /* = (alarm_info_t*)alarm; */
879         int ret;
880         int i = 0;
881         int j = 0;
882
883         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
884
885         if (alarm == NULL) {
886                 return ERR_ALARM_INVALID_PARAM;
887         }
888
889         alarm_info = (alarm_info_t *) alarm;
890         if (alarm_info == NULL || alarm_id == NULL) {
891                 return ERR_ALARM_INVALID_PARAM;
892         }
893
894         int error_code;
895         alarm_mode_t *mode = &alarm_info->mode;
896
897         ret = __sub_init();
898         if (ret < 0)
899                 return ret;
900
901         ALARM_MGR_EXCEPTION_PRINT("start(%d-%d-%d, %02d:%02d:%02d), end(%d-%d-%d), repeat(%d), interval(%d), type(%d)",
902                 alarm_info->start.day, alarm_info->start.month, alarm_info->start.year,
903                 alarm_info->start.hour, alarm_info->start.min, alarm_info->start.sec,
904                 alarm_info->end.year, alarm_info->end.month, alarm_info->end.day,
905                 alarm_info->mode.repeat, alarm_info->mode.u_interval, alarm_info->alarm_type);
906
907         /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
908         if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
909                 return ERR_ALARM_INVALID_PARAM;
910         }
911
912         if (destination && strlen(destination) >= MAX_PKG_NAME_LEN){
913                 ALARM_MGR_EXCEPTION_PRINT("[alarm-lib]: destination name is too long!\n");
914                 return ERR_ALARM_INVALID_PARAM;
915         }
916
917
918         if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
919                 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
920                 return error_code;
921         }
922
923         if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
924                 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
925                 return error_code;
926         }
927
928         if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
929                 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
930                 return error_code;
931         }
932
933         if (destination != NULL) {
934                 memset(dst_service_name, 0, strlen(destination) + strlen("ALARM.") + 2);
935                 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "ALARM.%s", destination);
936                 memset(dst_service_name_mod, 'a', MAX_SERVICE_NAME_LEN-1);
937
938                 for (i=0; i<=strlen(dst_service_name); i++)
939                 {
940                         if (dst_service_name[i] == '.' )
941                         {
942                                 dst_service_name_mod[j] = dst_service_name[i];
943                                 j++;
944                         }
945                         else
946                         {
947                                 dst_service_name_mod[j] = dst_service_name[i];
948                         }
949                         j++;
950                 }
951
952                 if (!_send_alarm_create(alarm_context, alarm_info, alarm_id, dst_service_name, dst_service_name_mod, &error_code)) {
953                         return error_code;
954                 }
955         } else {
956                 if (!_send_alarm_create(alarm_context, alarm_info, alarm_id, "null", "null", &error_code)) {
957                         return error_code;
958                 }
959         }
960
961         return ALARMMGR_RESULT_SUCCESS;
962 }
963
964
965
966 EXPORT_API int alarmmgr_add_alarm_appsvc(int alarm_type, time_t trigger_at_time,
967                                   time_t interval, void *bundle_data,
968                                   alarm_id_t *alarm_id)
969 {
970         int error_code = 0;;
971         struct timeval current_time;
972         struct tm duetime_tm;
973         alarm_info_t alarm_info;
974         const char *operation = NULL;
975         const char *appid = NULL;
976
977         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
978
979         bundle *b=(bundle *)bundle_data;
980
981         if (NULL == b)
982         {
983                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle\n");
984                 return ERR_ALARM_INVALID_PARAM;
985         }
986         operation = appsvc_get_operation(b);
987
988         if (NULL == operation)
989         {
990                 appsvc_set_operation(b, APPSVC_OPERATION_DEFAULT);
991         }
992
993         appid = appsvc_get_appid(b);
994
995         if ( (NULL == appid && (alarm_type & ALARM_TYPE_NOLAUNCH)) ||
996                         (NULL == appid && operation && !strcmp(operation, APPSVC_OPERATION_DEFAULT)) )
997         {
998                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter\n");
999                 return ERR_ALARM_INVALID_PARAM;
1000         }
1001
1002         if (__alarmmgr_init_appsvc() < 0)
1003         {
1004                 ALARM_MGR_EXCEPTION_PRINT("Unable to initialize dbus!!!\n");
1005                 return ERR_ALARM_SYSTEM_FAIL;
1006         }
1007
1008         if (alarm_id == NULL) {
1009                 return ERR_ALARM_INVALID_PARAM;
1010         }
1011
1012         if (trigger_at_time < 0) {
1013                 return ERR_ALARM_INVALID_PARAM;
1014         }
1015
1016         alarm_info.alarm_type = alarm_type;
1017         alarm_info.alarm_type |= ALARM_TYPE_RELATIVE;
1018
1019         // Checking api version
1020         int ret;
1021         int result = 0;
1022         pkgmgrinfo_pkginfo_h pkginfo = NULL;
1023         char pkgid[512] = {0, };
1024         const char *api_version = "2.4";
1025
1026         if (aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid)) != AUL_R_OK) {
1027                 ALARM_MGR_EXCEPTION_PRINT("aul_app_get_pkgid_bypid() is failed. PID %d may not be app.", getpid());
1028         } else {
1029                 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkginfo);
1030                 if (ret != PMINFO_R_OK) {
1031                         ALARM_MGR_EXCEPTION_PRINT("Failed to get pkginfo\n");
1032                 }
1033                 else {
1034                         ret = pkgmgrinfo_pkginfo_check_api_version(pkginfo, api_version, &result);
1035                         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
1036                         if (ret) {
1037                                 ALARM_MGR_EXCEPTION_PRINT("Failed to check api version [%d]\n", ret);
1038                                 return ERR_ALARM_SYSTEM_FAIL;
1039                         }
1040                 }
1041         }
1042
1043         if (result < 0) {
1044                 if (alarm_info.alarm_type & ALARM_TYPE_INEXACT) {
1045                         alarm_info.alarm_type ^= ALARM_TYPE_INEXACT;
1046                 }
1047         } else { //Since 2.4
1048                 if (!__is_permitted(appid, alarm_info.alarm_type)) {
1049                         ALARM_MGR_EXCEPTION_PRINT("[%s] is not permitted \n", appid);
1050                         return ERR_ALARM_NOT_PERMITTED_APP;
1051                 }
1052         }
1053
1054         gettimeofday(&current_time, NULL);
1055
1056         if (current_time.tv_usec > 500 * 1000)
1057         {
1058                 // When the millisecond part of the current_time is bigger than 500ms,
1059                 // the duetime increases by extra 1sec.
1060                 current_time.tv_sec += (trigger_at_time + 1);
1061         }
1062         else
1063         {
1064                 current_time.tv_sec += trigger_at_time;
1065         }
1066
1067         tzset();        // Processes the TZ environment variable, and Set timezone, daylight, and tzname.
1068         localtime_r(&current_time.tv_sec, &duetime_tm);
1069
1070         alarm_info.start.year = duetime_tm.tm_year + 1900;
1071         alarm_info.start.month = duetime_tm.tm_mon + 1;
1072         alarm_info.start.day = duetime_tm.tm_mday;
1073
1074         alarm_info.end.year = 0;
1075         alarm_info.end.month = 0;
1076         alarm_info.end.day = 0;
1077
1078         alarm_info.start.hour = duetime_tm.tm_hour;
1079         alarm_info.start.min = duetime_tm.tm_min;
1080         alarm_info.start.sec = duetime_tm.tm_sec;
1081
1082         if ((alarm_info.alarm_type & ALARM_TYPE_INEXACT) && interval < MIN_INEXACT_INTERVAL) {
1083                 interval = MIN_INEXACT_INTERVAL;
1084         }
1085
1086         if (interval <= 0) {
1087                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_ONCE;
1088                 alarm_info.mode.u_interval.interval = 0;
1089         } else {
1090                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_REPEAT;
1091                 alarm_info.mode.u_interval.interval = interval;
1092         }
1093
1094         ALARM_MGR_EXCEPTION_PRINT("trigger_at_time(%d), start(%d-%d-%d, %02d:%02d:%02d), repeat(%d), interval(%d), type(%d)",
1095                 trigger_at_time, alarm_info.start.day, alarm_info.start.month, alarm_info.start.year,
1096                 alarm_info.start.hour, alarm_info.start.min, alarm_info.start.sec,
1097                 alarm_info.mode.repeat, alarm_info.mode.u_interval.interval, alarm_info.alarm_type);
1098
1099         if (!_send_alarm_create_appsvc(alarm_context, &alarm_info, alarm_id, b, &error_code)) {
1100                 return error_code;
1101         }
1102
1103         return ALARMMGR_RESULT_SUCCESS;
1104 }
1105
1106
1107 EXPORT_API int alarmmgr_add_alarm(int alarm_type, time_t trigger_at_time,
1108                                   time_t interval, const char *destination,
1109                                   alarm_id_t *alarm_id)
1110 {
1111         char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
1112         char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = { 0 };
1113         int i = 0;
1114         int j = 0;
1115         int error_code;
1116         struct timeval current_time;
1117         struct tm duetime_tm;
1118         alarm_info_t alarm_info;
1119         int ret;
1120
1121         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
1122
1123         ret = __sub_init();
1124         if (ret < 0)
1125                 return ret;
1126
1127         if (alarm_id == NULL) {
1128                 return ERR_ALARM_INVALID_PARAM;
1129         }
1130
1131         if (trigger_at_time < 0) {
1132                 return ERR_ALARM_INVALID_PARAM;
1133         }
1134
1135         if (destination && strlen(destination) >= MAX_PKG_NAME_LEN){
1136                 ALARM_MGR_EXCEPTION_PRINT("[alarm-lib]: destination name is too long!\n");
1137                 return ERR_ALARM_INVALID_PARAM;
1138         }
1139
1140         alarm_info.alarm_type = alarm_type;
1141         alarm_info.alarm_type |= ALARM_TYPE_RELATIVE;
1142
1143         gettimeofday(&current_time, NULL);
1144
1145         if (current_time.tv_usec > 500 * 1000)
1146         {
1147                 // When the millisecond part of the current_time is bigger than 500ms,
1148                 // the duetime increases by extra 1sec.
1149                 current_time.tv_sec += (trigger_at_time + 1);
1150         }
1151         else
1152         {
1153                 current_time.tv_sec += trigger_at_time;
1154         }
1155
1156         tzset();        // Processes the TZ environment variable, and Set timezone, daylight, and tzname.
1157         localtime_r(&current_time.tv_sec, &duetime_tm);
1158
1159         alarm_info.start.year = duetime_tm.tm_year + 1900;
1160         alarm_info.start.month = duetime_tm.tm_mon + 1;
1161         alarm_info.start.day = duetime_tm.tm_mday;
1162
1163         alarm_info.end.year = 0;
1164         alarm_info.end.month = 0;
1165         alarm_info.end.day = 0;
1166
1167         alarm_info.start.hour = duetime_tm.tm_hour;
1168         alarm_info.start.min = duetime_tm.tm_min;
1169         alarm_info.start.sec = duetime_tm.tm_sec;
1170
1171         if (interval <= 0) {
1172                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_ONCE;
1173                 alarm_info.mode.u_interval.interval = 0;
1174         } else {
1175                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_REPEAT;
1176                 alarm_info.mode.u_interval.interval = interval;
1177         }
1178
1179         ALARM_MGR_EXCEPTION_PRINT("trigger_at_time(%d), start(%d-%d-%d, %02d:%02d:%02d), repeat(%d), interval(%d), type(%d)",
1180                 trigger_at_time, alarm_info.start.day, alarm_info.start.month, alarm_info.start.year,
1181                 alarm_info.start.hour, alarm_info.start.min, alarm_info.start.sec,
1182                 alarm_info.mode.repeat, alarm_info.mode.u_interval, alarm_info.alarm_type);
1183
1184         if (destination != NULL) {
1185                 memset(dst_service_name, 0,
1186                        strlen(destination) + strlen("ALARM.") + 2);
1187                 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "ALARM.%s",
1188                          destination);
1189                 memset(dst_service_name_mod,'a',MAX_SERVICE_NAME_LEN-1);
1190
1191                 j=0;
1192
1193                 for(i=0;i<=strlen(dst_service_name);i++)
1194                 {
1195                         if (dst_service_name[i] == '.')
1196                         {
1197                                 dst_service_name_mod[j]=dst_service_name[i];
1198                                 j++;
1199                         }
1200                         else
1201                         {
1202                                 dst_service_name_mod[j]=dst_service_name[i];
1203                         }
1204                         j++;
1205                 }
1206
1207                 if (!_send_alarm_create
1208                     (alarm_context, &alarm_info, alarm_id, dst_service_name,dst_service_name_mod,
1209                      &error_code)) {
1210                         return error_code;
1211                 }
1212         } else
1213             if (!_send_alarm_create
1214                 (alarm_context, &alarm_info, alarm_id, "null","null", &error_code)) {
1215                 return error_code;
1216         }
1217
1218         return ALARMMGR_RESULT_SUCCESS;
1219 }
1220
1221 EXPORT_API int alarmmgr_add_alarm_withcb(int alarm_type, time_t trigger_at_time,
1222                                   time_t interval, alarm_cb_t handler, void *user_param, alarm_id_t *alarm_id)
1223 {
1224         int error_code = 0;
1225         struct timeval current_time;
1226         struct tm duetime_tm;
1227         alarm_info_t alarm_info;
1228         int ret = 0;
1229         char appid[256] = {0,};
1230
1231         if (aul_app_get_appid_bypid(getpid(), appid, sizeof(appid)) != AUL_R_OK) {
1232                 ALARM_MGR_LOG_PRINT("aul_app_get_appid_bypid() is failed. PID %d may not be app.", getpid());
1233         }
1234
1235         ret = alarmmgr_init(appid);
1236         if (ret < 0)
1237                 return ret;
1238
1239         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_add_alarm_withcb() is called");
1240
1241         if (alarm_id == NULL) {
1242                 return ERR_ALARM_INVALID_PARAM;
1243         }
1244
1245         if (trigger_at_time < 0) {
1246                 return ERR_ALARM_INVALID_PARAM;
1247         }
1248
1249         alarm_info.alarm_type = alarm_type;
1250         alarm_info.alarm_type |= ALARM_TYPE_RELATIVE;
1251         alarm_info.alarm_type |= ALARM_TYPE_WITHCB;
1252
1253         gettimeofday(&current_time, NULL);
1254
1255         if (current_time.tv_usec > 500 * 1000)
1256         {
1257                 // When the millisecond part of the current_time is bigger than 500ms,
1258                 // the duetime increases by extra 1sec.
1259                 current_time.tv_sec += (trigger_at_time + 1);
1260         }
1261         else
1262         {
1263                 current_time.tv_sec += trigger_at_time;
1264         }
1265
1266         tzset();        // Processes the TZ environment variable, and Set timezone, daylight, and tzname.
1267         localtime_r(&current_time.tv_sec, &duetime_tm);
1268
1269         alarm_info.start.year = duetime_tm.tm_year + 1900;
1270         alarm_info.start.month = duetime_tm.tm_mon + 1;
1271         alarm_info.start.day = duetime_tm.tm_mday;
1272
1273         alarm_info.end.year = 0;
1274         alarm_info.end.month = 0;
1275         alarm_info.end.day = 0;
1276
1277         alarm_info.start.hour = duetime_tm.tm_hour;
1278         alarm_info.start.min = duetime_tm.tm_min;
1279         alarm_info.start.sec = duetime_tm.tm_sec;
1280
1281         if (interval <= 0) {
1282                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_ONCE;
1283                 alarm_info.mode.u_interval.interval = 0;
1284         } else {
1285                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_REPEAT;
1286                 alarm_info.mode.u_interval.interval = interval;
1287         }
1288
1289         ALARM_MGR_EXCEPTION_PRINT("trigger_at_time(%d), start(%d-%d-%d, %02d:%02d:%02d), repeat(%d), interval(%d), type(%d)",
1290                 trigger_at_time, alarm_info.start.day, alarm_info.start.month, alarm_info.start.year,
1291                 alarm_info.start.hour, alarm_info.start.min, alarm_info.start.sec,
1292                 alarm_info.mode.repeat, alarm_info.mode.u_interval.interval, alarm_info.alarm_type);
1293
1294         if (!_send_alarm_create(alarm_context, &alarm_info, alarm_id, "null","null", &error_code)) {
1295                 return error_code;
1296         }
1297         __add_resultcb(*alarm_id, handler, user_param);
1298
1299         return ALARMMGR_RESULT_SUCCESS;
1300 }
1301
1302 EXPORT_API int alarmmgr_remove_alarm(alarm_id_t alarm_id)
1303 {
1304         int error_code;
1305         int ret;
1306         alarm_cb_info_t *info;
1307
1308         ret = __sub_init();
1309         if (ret < 0)
1310                 return ret;
1311
1312         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_delete(%d) is called\n", alarm_id);
1313
1314         if (alarm_id <= 0) {
1315                 return ERR_ALARM_INVALID_ID;
1316         }
1317
1318         if (!_send_alarm_delete(alarm_context, alarm_id, &error_code))
1319                 return error_code;
1320
1321         info = __find_resultcb(alarm_id);
1322         __remove_resultcb(info);
1323
1324         return ALARMMGR_RESULT_SUCCESS;
1325 }
1326
1327 EXPORT_API int alarmmgr_remove_all(void)
1328 {
1329         int error_code;
1330         int return_code = ALARMMGR_RESULT_SUCCESS;
1331         int ret = __sub_init();
1332         if (ret < 0)
1333         {
1334                 return ret;
1335         }
1336
1337         if (!_send_alarm_delete_all(alarm_context, &error_code))
1338                 return error_code;
1339
1340         return return_code;
1341 }
1342
1343 EXPORT_API int alarmmgr_enum_alarm_ids(alarm_enum_fn_t fn, void *user_param)
1344 {
1345         SECURE_LOGD("Enter");
1346         GError *error = NULL;
1347         GVariant *alarm_array = NULL;
1348         int return_code = 0;
1349         int maxnum_of_ids = 0;
1350         int num_of_ids = 0;
1351         alarm_id_t alarm_id = -1;
1352         int ret = 0;
1353         char *e_cookie = NULL;
1354         char cookie[256] = {0,};
1355         int size = 0;
1356         GVariantIter *iter = NULL;
1357
1358         if (fn == NULL) {
1359                 return ERR_ALARM_INVALID_PARAM;
1360         }
1361
1362         size = security_server_get_cookie_size();
1363         ret = security_server_request_cookie(cookie, size);
1364         if (ret < 0) {
1365                 ALARM_MGR_EXCEPTION_PRINT("security_server_request_cookie() is failed.");
1366                 return ERR_ALARM_SYSTEM_FAIL;
1367         }
1368
1369         e_cookie = g_base64_encode((const guchar *)cookie, size);
1370         if (e_cookie == NULL) {
1371                 ALARM_MGR_EXCEPTION_PRINT("g_base64_encode() is failed.");
1372                 return ERR_ALARM_SYSTEM_FAIL;
1373         }
1374
1375         ret = __sub_init();
1376         if (ret < 0) {
1377                 ALARM_MGR_EXCEPTION_PRINT("__sub_init() is failed.");
1378                 g_free(e_cookie);
1379                 return ret;
1380         }
1381
1382         SECURE_LOGD("alarm_manager_call_alarm_get_number_of_ids_sync() is called");
1383         if (!alarm_manager_call_alarm_get_number_of_ids_sync(
1384             (AlarmManager*)alarm_context.proxy, alarm_context.pid, e_cookie, &maxnum_of_ids, &return_code, NULL, &error)) {
1385                 /* dbus error. error_code should be set */
1386                 ALARM_MGR_EXCEPTION_PRINT("alarm_manager_call_alarm_get_number_of_ids_sync() is failed by dbus. return_code[%d]", return_code);
1387                 if (error) {
1388                         ALARM_MGR_EXCEPTION_PRINT("dbus error message: %s", error->message);
1389                         g_error_free(error);
1390                 }
1391                 g_free(e_cookie);
1392                 return ERR_ALARM_SYSTEM_FAIL;
1393         }
1394
1395         g_free(e_cookie);
1396
1397         if (return_code != ALARMMGR_RESULT_SUCCESS) {
1398                 ALARM_MGR_EXCEPTION_PRINT("alarm_manager_call_alarm_get_number_of_ids_sync() is failed. return_code[%d]", return_code);
1399                 return return_code;
1400         } else {
1401                 ALARM_MGR_LOG_PRINT("maxnum_of_ids[%d]", maxnum_of_ids);
1402         }
1403
1404         SECURE_LOGD("alarm_manager_call_alarm_get_list_of_ids_sync() is called");
1405         if (!alarm_manager_call_alarm_get_list_of_ids_sync(
1406                      (AlarmManager*)alarm_context.proxy, alarm_context.pid, maxnum_of_ids, &alarm_array, &num_of_ids, &return_code, NULL, &error)) {
1407                 /* dbus error. error_code should be set */
1408                 ALARM_MGR_EXCEPTION_PRINT(
1409                     "alarm_manager_call_alarm_get_list_of_ids_sync() failed by dbus. num_of_ids[%d], return_code[%d].", num_of_ids, return_code);
1410                 if (error) {
1411                         ALARM_MGR_EXCEPTION_PRINT("dbus error message: %s.", error->message);
1412                         g_error_free(error);
1413                 }
1414                 return ERR_ALARM_SYSTEM_FAIL;
1415         }
1416
1417         if (return_code != ALARMMGR_RESULT_SUCCESS) {
1418                 return return_code;
1419         }
1420
1421         if (alarm_array == NULL) {
1422                 ALARM_MGR_LOG_PRINT("alarm server is not initilized.");
1423                 return ERR_ALARM_SYSTEM_FAIL;
1424         }
1425
1426         g_variant_get(alarm_array, "ai", &iter);
1427         while (g_variant_iter_loop(iter, "i", &alarm_id))
1428         {
1429                 (*fn) (alarm_id, user_param);
1430                 ALARM_MGR_LOG_PRINT("alarm_id (%d)", alarm_id);
1431         }
1432         g_variant_iter_free(iter);
1433         g_variant_unref(alarm_array);
1434
1435         SECURE_LOGD("Leave");
1436         return ALARMMGR_RESULT_SUCCESS;
1437 }
1438
1439 EXPORT_API int alarmmgr_get_info(alarm_id_t alarm_id, alarm_entry_t *alarm)
1440 {
1441         int error_code;
1442         alarm_info_t *alarm_info = (alarm_info_t *) alarm;
1443
1444         int ret;
1445
1446         ret = __sub_init();
1447         if (ret < 0)
1448                 return ret;
1449
1450         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_get_info() is called\n");
1451
1452         if (alarm_id < 0 || alarm_info == NULL) {
1453                 return ERR_ALARM_INVALID_PARAM;
1454         }
1455
1456         if (!_send_alarm_get_info(alarm_context, alarm_id, alarm_info, &error_code)) {
1457                 return error_code;
1458         }
1459
1460         return ALARMMGR_RESULT_SUCCESS;
1461 }
1462
1463 int alarmmgr_create(alarm_info_t *alarm_info, char *destination,
1464                     alarm_id_t *alarm_id)
1465 {
1466         char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
1467         alarm_mode_t *mode = &alarm_info->mode;
1468         int error_code;
1469
1470         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
1471
1472         if (alarm_info == NULL || alarm_id == NULL) {
1473                 return ERR_ALARM_INVALID_PARAM;
1474         }
1475
1476         ALARM_MGR_LOG_PRINT("alarm_info->start.year(%d), "
1477                             "alarm_info->start.month(%d), alarm_info->start.day(%d)",
1478                             alarm_info->start.year, alarm_info->start.month,
1479                             alarm_info->start.day);
1480
1481         /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
1482         if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
1483                 return ERR_ALARM_INVALID_PARAM;
1484         }
1485
1486         if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
1487                 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
1488                 return error_code;
1489         }
1490
1491         if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
1492                 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
1493                 return error_code;
1494         }
1495
1496         if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
1497                 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
1498                 return error_code;
1499         }
1500
1501         if (destination != NULL) {
1502                 memset(dst_service_name, 0,
1503                        strlen(destination) + strlen("ALARM.") + 2);
1504                 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "ALARM.%s",
1505                          destination);
1506                 if (!_send_alarm_create
1507                     (alarm_context, alarm_info, alarm_id, dst_service_name,"null",
1508                      &error_code)) {
1509                         return error_code;
1510                 }
1511         }
1512 /*TODO: Currently this API is not exported. Hence not modifying*/
1513         if (!_send_alarm_create
1514             (alarm_context, alarm_info, alarm_id, "null", "null", &error_code)) {
1515                 return error_code;
1516         }
1517
1518         return ALARMMGR_RESULT_SUCCESS;
1519
1520 }
1521
1522 int alarmmgr_get_number_of_ids(int *num_of_ids)
1523 {
1524         int error_code;
1525         ALARM_MGR_LOG_PRINT("[alarm-lib]: alarm_get_number_of_ids() is called.");
1526
1527         if (num_of_ids == NULL) {
1528                 return ERR_ALARM_INVALID_PARAM;
1529         }
1530         ALARM_MGR_LOG_PRINT("call alarm_get_number_of_ids\n");
1531         if (!_send_alarm_get_number_of_ids(alarm_context, num_of_ids, &error_code)) {
1532                 return error_code;
1533         }
1534
1535         return ALARMMGR_RESULT_SUCCESS;
1536 }
1537
1538 int alarmmgr_get_list_of_ids(int maxnum_of_ids, alarm_id_t *alarm_id,
1539                              int *num_of_ids)
1540 {
1541         int error_code;
1542         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_get_list_of_ids() is called.");
1543
1544         if (maxnum_of_ids < 0 || alarm_id == NULL || num_of_ids == NULL) {
1545                 return ERR_ALARM_INVALID_PARAM;
1546         }
1547
1548         if (maxnum_of_ids == 0) {
1549                 *num_of_ids = 0;
1550                 return ALARMMGR_RESULT_SUCCESS;
1551         }
1552
1553         if (!_send_alarm_get_list_of_ids
1554             (alarm_context, maxnum_of_ids, alarm_id, num_of_ids, &error_code)) {
1555                 return error_code;
1556         }
1557
1558         return ALARMMGR_RESULT_SUCCESS;
1559 }
1560
1561 EXPORT_API int alarmmgr_get_next_duetime(alarm_id_t alarm_id, time_t* duetime)
1562 {
1563         int error_code;
1564         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_get_next_duetime() is called.");
1565
1566         if (duetime == NULL) {
1567                 return ERR_ALARM_INVALID_PARAM;
1568         }
1569
1570         if (!_send_alarm_get_next_duetime(alarm_context, alarm_id, duetime, &error_code)) {
1571                 return error_code;
1572         }
1573
1574         return ALARMMGR_RESULT_SUCCESS;
1575 }
1576
1577 EXPORT_API int alarmmgr_get_all_info(char **db_path)
1578 {
1579         int error_code;
1580         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_get_all_info() is called.");
1581
1582         if (db_path == NULL) {
1583                 return ERR_ALARM_INVALID_PARAM;
1584         }
1585
1586         if (!_send_alarm_get_all_info(alarm_context, db_path, &error_code)) {
1587                 return error_code;
1588         }
1589
1590         ALARM_MGR_LOG_PRINT("[alarm-lib]: successfully save info in %s.", *db_path);
1591         return ALARMMGR_RESULT_SUCCESS;
1592 }
1593
1594 EXPORT_API int alarmmgr_add_periodic_alarm_withcb(int interval, periodic_method_e method,
1595         alarm_cb_t handler, void *user_param, alarm_id_t *alarm_id)
1596 {
1597         int error_code = 0;
1598         alarm_info_t alarm_info;
1599         int ret = 0;
1600         char appid[256] = {0,};
1601
1602         if (aul_app_get_appid_bypid(getpid(), appid, sizeof(appid)) != AUL_R_OK) {
1603                 ALARM_MGR_LOG_PRINT("aul_app_get_appid_bypid() is failed. PID %d may not be app.",
1604                         getpid());
1605         }
1606
1607         ret = alarmmgr_init(appid);
1608         if (ret < 0)
1609                 return ret;
1610
1611         if (alarm_id == NULL) {
1612                 return ERR_ALARM_INVALID_PARAM;
1613         }
1614
1615         if (!_send_alarm_create_periodic(alarm_context, interval, 0, (int)method, alarm_id,
1616                 &error_code)) {
1617                 return error_code;
1618         }
1619         __add_resultcb(*alarm_id, handler, user_param);
1620
1621         return ALARMMGR_RESULT_SUCCESS;
1622 }
1623
1624 EXPORT_API int alarmmgr_add_reference_periodic_alarm_withcb(int interval,
1625         alarm_cb_t handler, void *user_param, alarm_id_t *alarm_id)
1626 {
1627         int error_code = 0;
1628         alarm_info_t alarm_info;
1629         int ret = 0;
1630         char appid[256] = {0,};
1631
1632         if (aul_app_get_appid_bypid(getpid(), appid, sizeof(appid)) != AUL_R_OK) {
1633                 ALARM_MGR_LOG_PRINT("aul_app_get_appid_bypid() is failed. PID %d may not be app.",
1634                         getpid());
1635         }
1636
1637         ret = alarmmgr_init(appid);
1638         if (ret < 0)
1639                 return ret;
1640
1641         if (alarm_id == NULL) {
1642                 return ERR_ALARM_INVALID_PARAM;
1643         }
1644
1645         if (!_send_alarm_create_periodic(alarm_context, interval, 1, 0,
1646                 alarm_id, &error_code)) {
1647                 return error_code;
1648         }
1649
1650         __add_resultcb(*alarm_id, handler, user_param);
1651
1652         return ALARMMGR_RESULT_SUCCESS;
1653 }
1654
1655 EXPORT_API int alarmmgr_set_systime(int new_time)
1656 {
1657         int error_code;
1658         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_set_systime(%d) is called.", new_time);
1659
1660         if (__sub_init() < 0) {
1661                 return ERR_ALARM_SYSTEM_FAIL;
1662         }
1663
1664         if (!_send_alarm_set_time(alarm_context, new_time, &error_code)) {
1665                 ALARM_MGR_EXCEPTION_PRINT("Failed to set time. error: %d", error_code);
1666                 return error_code;
1667         }
1668
1669         ALARM_MGR_LOG_PRINT("[alarm-lib]: successfully set the time(%d) by pid(%d).", new_time, alarm_context.pid);
1670         return ALARMMGR_RESULT_SUCCESS;
1671 }
1672
1673 EXPORT_API int alarmmgr_set_systime_with_propagation_delay(struct timespec new_time, struct timespec req_time)
1674 {
1675         int error_code;
1676         ALARM_MGR_LOG_PRINT("[alarm-lib] New: %d(sec) %09d(nsec), Requested: %d(sec) %09d(nsec)",
1677                 new_time.tv_sec, new_time.tv_nsec, req_time.tv_sec, req_time.tv_nsec);
1678
1679         if (__sub_init() < 0) {
1680                 return ERR_ALARM_SYSTEM_FAIL;
1681         }
1682
1683         if (!_send_alarm_set_time_with_propagation_delay(alarm_context, new_time.tv_sec, new_time.tv_nsec, req_time.tv_sec, req_time.tv_nsec, &error_code)) {
1684                 ALARM_MGR_EXCEPTION_PRINT("Failed to set time with propagation delay. error: %d", error_code);
1685                 return error_code;
1686         }
1687
1688         ALARM_MGR_LOG_PRINT("[alarm-lib]: successfully set the time by pid(%d).", alarm_context.pid);
1689         return ALARMMGR_RESULT_SUCCESS;
1690 }
1691
1692 EXPORT_API int alarmmgr_set_timezone(char *tzpath_str)
1693 {
1694         int error_code;
1695         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_set_timezone() is called.");
1696
1697         if (tzpath_str == NULL) {
1698                 return ERR_ALARM_INVALID_PARAM;
1699         }
1700
1701         if (__sub_init() < 0) {
1702                 return ERR_ALARM_SYSTEM_FAIL;
1703         }
1704
1705         if (!_send_alarm_set_timezone(alarm_context, tzpath_str, &error_code)) {
1706                 return error_code;
1707         }
1708
1709         ALARM_MGR_LOG_PRINT("[alarm-lib]: successfully set the timezone(%s) by pid(%d)", tzpath_str, alarm_context.pid);
1710         return ALARMMGR_RESULT_SUCCESS;
1711 }