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