Initialize Tizen 2.3
[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='/com/samsung/alarm/client'>"
78   "  <interface name='com.samsung.alarm.client'>"
79   "    <method name='alarm_expired'>"
80   "      <arg type='i' name='alarm_id' direction='in'/>"
81   "      <arg type='s' name='service_name' direction='in'/>"
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         g_thread_init(NULL);
257         g_type_init();
258
259         alarm_context.connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
260         if (alarm_context.connection == NULL) {
261                 ALARM_MGR_EXCEPTION_PRINT("g_bus_get_sync() is failed. error: %s", error->message);
262                 g_error_free(error);
263                 pthread_mutex_unlock(&init_lock);
264                 return ERR_ALARM_SYSTEM_FAIL;
265         }
266
267         alarm_context.proxy = g_dbus_proxy_new_sync(alarm_context.connection,
268                                                         G_DBUS_PROXY_FLAGS_NONE,
269                                                         NULL,
270                                                         "com.samsung.alarm.manager",
271                                                         "/com/samsung/alarm/manager",
272                                                         "com.samsung.alarm.manager",
273                                                         NULL,
274                                                         NULL);
275
276         if (alarm_context.proxy == NULL) {
277                 ALARM_MGR_EXCEPTION_PRINT("Creating a proxy is failed.");
278                 g_object_unref (alarm_context.connection);
279                 pthread_mutex_unlock(&init_lock);
280                 return ERR_ALARM_SYSTEM_FAIL;
281         }
282
283         // Only webapp which has a pid of WebProcess uses the sid. Otherwise, the pid is used.
284         snprintf(proc_file, MAX_LEN, "/proc/%d/cmdline", getpid());
285         fd = open(proc_file, O_RDONLY);
286         if (fd < 0) {
287                 SECURE_LOGE("Unable to get the proc file(%d).\n", getpid());
288                 g_object_unref(alarm_context.proxy);
289                 g_object_unref(alarm_context.connection);
290                 pthread_mutex_unlock(&init_lock);
291                 return ERR_ALARM_SYSTEM_FAIL;
292         }
293         else {
294                 ret = read(fd, process_name, MAX_LEN);
295                 close(fd);
296                 if (ret < 0) {
297                         ALARM_MGR_EXCEPTION_PRINT("Unable to read the proc file(%d).", getpid());
298                         g_object_unref(alarm_context.proxy);
299                         g_object_unref(alarm_context.connection);
300                         pthread_mutex_unlock(&init_lock);
301                         return ERR_ALARM_SYSTEM_FAIL;
302                 }
303                 else {
304                         if (strncmp(process_name, "/usr/bin/WebProcess", strlen("/usr/bin/WebProcess")) == 0) {
305                                 alarm_context.pid = getsid(getpid());
306                                 SECURE_LOGD("alarm_context.pid is set to sessionID, %d.", alarm_context.pid);
307                         }
308                         else {
309                                 alarm_context.pid = getpid();
310                                 SECURE_LOGD("alarm_context.pid is set to processID, %d.", alarm_context.pid);
311                         }
312                 }
313         }
314
315         sub_initialized = true;
316
317         pthread_mutex_unlock(&init_lock);
318
319         return ALARMMGR_RESULT_SUCCESS;
320 }
321
322 EXPORT_API int alarmmgr_init(const char *appid)
323 {
324         SECURE_LOGD("Enter");
325         int request_name_result = 0;
326         char service_name[MAX_SERVICE_NAME_LEN] = { 0 };
327         char service_name_mod[MAX_SERVICE_NAME_LEN]= { 0 };
328         int ret = ALARMMGR_RESULT_SUCCESS;
329         guint owner_id = 0;
330         int i = 0;
331         int j = 0;
332         int len = 0;
333
334         if (appid == NULL)
335                 return ERR_ALARM_INVALID_PARAM;
336
337         if (strlen(appid) >= MAX_PKG_NAME_LEN)
338                 return ERR_ALARM_INVALID_PARAM;
339
340         if (b_initialized) {
341                 SECURE_LOGD("alarm was already initialized. app_service_name=%s",
342                      g_quark_to_string(alarm_context.quark_app_service_name));
343                 return ALARMMGR_RESULT_SUCCESS;
344         }
345
346         ret = __sub_init();
347         if (ret < 0)
348                 return ret;
349
350         memset(service_name_mod, 'a', MAX_SERVICE_NAME_LEN - 1);
351
352         len = strlen("ALARM.");
353         strncpy(service_name, "ALARM.", len);
354         strncpy(service_name + len, appid, strlen(appid));
355
356         for(i = 0; i <= strlen(service_name); i++) {
357                 if (service_name[i] == '.') {
358                         service_name_mod[j] = service_name[i];
359                         j++;
360                 }
361                 else {
362                         service_name_mod[j] = service_name[i];
363                 }
364                 j++;
365         }
366
367         SECURE_LOGD("[alarm-lib]: dbus own name: %s", service_name_mod);
368         owner_id = g_bus_own_name_on_connection(alarm_context.connection, service_name_mod,
369                                                                                 G_BUS_NAME_OWNER_FLAGS_NONE, NULL, NULL, NULL, NULL);
370         if (owner_id == 0) {
371                 ALARM_MGR_EXCEPTION_PRINT("Acquiring the own name is failed. %s", service_name_mod);
372                 goto error;
373         }
374
375         introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
376         if (introspection_data == NULL) {
377                 ALARM_MGR_EXCEPTION_PRINT("g_dbus_node_info_new_for_xml() is failed.");
378                 goto error;
379         }
380
381         registration_id = g_dbus_connection_register_object(alarm_context.connection,
382                                                                                                 "/com/samsung/alarm/client",
383                                                                                                 introspection_data->interfaces[0],
384                                                                                                 &interface_vtable, NULL, NULL, NULL);
385         if (registration_id == 0) {
386                 ALARM_MGR_EXCEPTION_PRINT("Registering the callback is failed.");
387                 goto error;
388         }
389
390         alarm_context.quark_app_service_name = g_quark_from_string(service_name);
391         alarm_context.quark_app_service_name_mod= g_quark_from_string(service_name_mod);
392
393         b_initialized = true;
394
395         SECURE_LOGD("Leave");
396         return ALARMMGR_RESULT_SUCCESS;
397
398 error:
399         if (introspection_data) {
400                 g_dbus_node_info_unref(introspection_data);
401         }
402         if (registration_id != 0) {
403                 g_dbus_connection_unregister_object(alarm_context.connection, registration_id);
404         }
405         g_object_unref(alarm_context.proxy);
406         alarm_context.proxy = NULL;
407
408         g_object_unref(alarm_context.connection);
409         alarm_context.connection = NULL;
410
411         sub_initialized = false;
412         return ERR_ALARM_INVALID_PARAM;
413 }
414
415 EXPORT_API void alarmmgr_fini()
416 {
417         SECURE_LOGD("Enter");
418         if (introspection_data) {
419                 g_dbus_node_info_unref(introspection_data);
420         }
421
422         if (alarm_context.connection != NULL && registration_id != 0) {
423                 g_dbus_connection_unregister_object(alarm_context.connection, registration_id);
424         }
425
426         if (alarm_context.proxy) {
427                 g_object_unref(alarm_context.proxy);
428                 alarm_context.proxy = NULL;
429         }
430
431         if (alarm_context.connection) {
432                 g_object_unref(alarm_context.connection);
433                 alarm_context.connection = NULL;
434         }
435
436         b_initialized = false;
437         sub_initialized = false;
438
439         SECURE_LOGD("Leave");
440 }
441
442 EXPORT_API int alarmmgr_set_cb(alarm_cb_t handler, void *user_param)
443 {
444         SECURE_LOGD("Enter");
445
446         if (handler == NULL) {
447                 ALARM_MGR_EXCEPTION_PRINT("callback is NULL.");
448                 return ERR_ALARM_INVALID_PARAM;
449         }
450         alarm_context.alarm_handler = handler;
451         alarm_context.user_param = user_param;
452
453         SECURE_LOGD("Leave");
454         return ALARMMGR_RESULT_SUCCESS;
455 }
456
457 EXPORT_API alarm_entry_t *alarmmgr_create_alarm(void)
458 {
459         alarm_info_t *alarm = (alarm_info_t *) malloc(sizeof(alarm_info_t));
460
461         if (NULL == alarm)
462         {
463                 return NULL;
464         }
465
466         alarm->start.year = 0;
467         alarm->start.month = 0;
468         alarm->start.day = 0;
469         alarm->start.hour = 0;
470         alarm->start.min = 0;
471         alarm->start.sec = 0;
472
473         alarm->end.year = 0;
474         alarm->end.month = 0;
475         alarm->end.day = 0;
476         alarm->end.hour = 0;
477         alarm->end.min = 0;
478         alarm->end.sec = 0;
479
480         alarm->mode.repeat = ALARM_REPEAT_MODE_ONCE;
481         alarm->mode.u_interval.interval = 0;
482
483         alarm->alarm_type = ALARM_TYPE_DEFAULT;
484
485         alarm->reserved_info = 0;
486
487         return (alarm_entry_t *) alarm;
488 }
489
490 EXPORT_API int alarmmgr_free_alarm(alarm_entry_t *alarm)
491 {
492         if (alarm == NULL) {
493                 return ERR_ALARM_INVALID_PARAM;
494         }
495         free(alarm);
496
497         return ALARMMGR_RESULT_SUCCESS;
498 }
499
500 EXPORT_API int alarmmgr_set_time(alarm_entry_t *alarm, alarm_date_t time)
501 {
502         alarm_info_t *alarm_info;       /*= (alarm_info_t*)alarm;*/
503         int error_code;
504
505         if (alarm == NULL) {
506                 return ERR_ALARM_INVALID_PARAM;
507         }
508
509         alarm_info = (alarm_info_t *) alarm;
510
511         if (!__alarm_validate_date(&time, &error_code)) {
512                 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
513                 return error_code;
514         }
515
516         if (!__alarm_validate_time(&time, &error_code)) {
517                 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
518                 return error_code;
519         }
520
521         memcpy(&alarm_info->start, &time, sizeof(alarm_date_t));
522
523         return ALARMMGR_RESULT_SUCCESS;
524 }
525
526 EXPORT_API int alarmmgr_get_time(const alarm_entry_t *alarm,
527                                  alarm_date_t *time)
528 {
529         alarm_info_t *alarm_info = (alarm_info_t *) alarm;
530
531         if (alarm == NULL) {
532                 return ERR_ALARM_INVALID_PARAM;
533         }
534
535         if (time != NULL)
536                 memcpy(time, &alarm_info->start, sizeof(alarm_date_t));
537
538         return ALARMMGR_RESULT_SUCCESS;
539 }
540
541 EXPORT_API int alarmmgr_set_repeat_mode(alarm_entry_t *alarm,
542                                         alarm_repeat_mode_t repeat,
543                                         int interval)
544 {
545         alarm_info_t *alarm_info = (alarm_info_t *) alarm;
546
547         if (repeat >= ALARM_REPEAT_MODE_MAX) {
548                 return ERR_ALARM_INVALID_PARAM;
549         }
550
551         alarm_info->mode.repeat = repeat;
552
553         if (repeat == ALARM_REPEAT_MODE_REPEAT
554             || repeat == ALARM_REPEAT_MODE_WEEKLY) {
555                 alarm_info->mode.u_interval.interval = interval;
556         }
557
558         return ALARMMGR_RESULT_SUCCESS;
559 }
560
561 EXPORT_API int alarmmgr_get_repeat_mode(const alarm_entry_t *alarm,
562                                         alarm_repeat_mode_t *repeat,
563                                         int *interval)
564 {
565         alarm_info_t *alarm_info = (alarm_info_t *) alarm;
566
567         if (alarm == NULL) {
568                 return ERR_ALARM_INVALID_PARAM;
569         }
570
571         if (repeat != NULL)
572                 *repeat = alarm_info->mode.repeat;
573         if (interval != NULL)
574                 *interval = alarm_info->mode.u_interval.interval;
575
576         return ALARMMGR_RESULT_SUCCESS;
577 }
578
579 EXPORT_API int alarmmgr_set_type(alarm_entry_t *alarm, int alarm_type)
580 {
581         alarm_info_t *alarm_info;       /*= (alarm_info_t*)alarm;*/
582
583         if (alarm == NULL) {
584                 return ERR_ALARM_INVALID_PARAM;
585         }
586
587         alarm_info = (alarm_info_t *) alarm;
588
589         alarm_info->alarm_type = alarm_type;
590         alarm_info->alarm_type &= (~ALARM_TYPE_RELATIVE);
591
592         return ALARMMGR_RESULT_SUCCESS;
593 }
594
595 EXPORT_API int alarmmgr_get_type(const alarm_entry_t *alarm, int *alarm_type)
596 {
597         alarm_info_t *alarm_info = (alarm_info_t *) alarm;
598
599         if (alarm == NULL) {
600                 return ERR_ALARM_INVALID_PARAM;
601         }
602
603         if (alarm_type != NULL)
604                 *alarm_type = alarm_info->alarm_type;
605
606         return ALARMMGR_RESULT_SUCCESS;
607 }
608
609
610 static int __alarmmgr_init_appsvc(void)
611 {
612         int ret;
613
614         if (b_initialized) {
615                 ALARM_MGR_EXCEPTION_PRINT("alarm was already initialized\n");
616                 return ALARMMGR_RESULT_SUCCESS;
617         }
618
619         g_thread_init(NULL);
620
621         dbus_g_thread_init();
622
623         ret = __sub_init();
624         if (ret < 0)
625                 return ret;
626
627         b_initialized = true;
628
629         return ALARMMGR_RESULT_SUCCESS;
630
631 }
632
633 EXPORT_API void *alarmmgr_get_alarm_appsvc_info(alarm_id_t alarm_id, int *return_code){
634
635         int ret = 0;
636
637         ret = __sub_init();
638         if (ret < 0){
639                 if (return_code) {
640                         *return_code = ret;
641                 }
642                 return NULL;
643         }
644
645         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_get_alarm_appsvc_info() is called.");
646
647         if (alarm_id <= 0) {
648                 if (return_code) {
649                         *return_code = ERR_ALARM_INVALID_ID;
650                 }
651                 return NULL;
652         }
653
654         return _send_alarm_get_appsvc_info(alarm_context, alarm_id, return_code);
655
656 }
657
658 EXPORT_API int alarmmgr_set_rtc_time(alarm_date_t *time){
659
660         int ret = 0;
661         int error_code = 0;
662
663         if (!time){
664                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter time\n");
665                 return ERR_ALARM_INVALID_PARAM;
666         }
667
668         ret = __sub_init();
669         if (ret < 0){
670                 return ret;
671         }
672
673         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_set_rtc_time() is called\n");
674
675         if (!__alarm_validate_date(time, &error_code)) {
676                 ALARM_MGR_EXCEPTION_PRINT("RTC date error\n");
677                 return error_code;
678         }
679
680         if (!__alarm_validate_time(time, &error_code)) {
681                 ALARM_MGR_EXCEPTION_PRINT("RTC time error\n");
682                 return error_code;
683         }
684
685         time->year-=1900;
686         time->month-=1;
687
688         if (!_send_alarm_set_rtc_time
689                 (alarm_context, time, &error_code)){
690                         return error_code;
691         }
692
693         return ALARMMGR_RESULT_SUCCESS;
694
695 }
696
697 EXPORT_API int alarmmgr_add_alarm_appsvc_with_localtime(alarm_entry_t *alarm, void *bundle_data, alarm_id_t *alarm_id)
698 {
699         alarm_info_t *alarm_info = NULL;        /* = (alarm_info_t*)alarm; */
700         const char *operation = NULL;
701         int error_code = 0;
702         const char *appid = NULL;
703
704         bundle *b=(bundle *)bundle_data;
705
706         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
707
708         if (alarm == NULL) {
709                 return ERR_ALARM_INVALID_PARAM;
710         }
711
712         if (NULL == b)
713         {
714                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle\n");
715                 return ERR_ALARM_INVALID_PARAM;
716         }
717         operation = appsvc_get_operation(b);
718
719
720         if (NULL == operation)
721         {
722                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle [appsvc operation not present]\n");
723                 return ERR_ALARM_INVALID_PARAM;
724         }
725
726         if (__alarmmgr_init_appsvc() < 0)
727         {
728                 ALARM_MGR_EXCEPTION_PRINT("Unable to initialize dbus!!!\n");
729                 return ERR_ALARM_SYSTEM_FAIL;
730         }
731
732         alarm_info = (alarm_info_t *) alarm;
733
734         appid = appsvc_get_appid(b);
735
736         if (NULL == appid && (alarm_info->alarm_type & ALARM_TYPE_NOLAUNCH) )
737         {
738                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter\n");
739                 return ERR_ALARM_INVALID_PARAM;
740         }
741
742         if (alarm_info == NULL || alarm_id == NULL) {
743                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter\n");
744                 return ERR_ALARM_INVALID_PARAM;
745         }
746         alarm_mode_t *mode = &alarm_info->mode;
747
748         ALARM_MGR_EXCEPTION_PRINT("start(%d-%d-%d, %02d:%02d:%02d), end(%d-%d-%d), repeat(%d), interval(%d), type(%d)",
749                 alarm_info->start.day, alarm_info->start.month, alarm_info->start.year,
750                 alarm_info->start.hour, alarm_info->start.min, alarm_info->start.sec,
751                 alarm_info->end.year, alarm_info->end.month, alarm_info->end.day,
752                 alarm_info->mode.repeat, alarm_info->mode.u_interval, alarm_info->alarm_type);
753
754         /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
755         if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
756                 return ERR_ALARM_INVALID_PARAM;
757         }
758
759         if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
760                 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
761                 return error_code;
762         }
763
764         if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
765                 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
766                 return error_code;
767         }
768
769         if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
770                 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
771                 return error_code;
772         }
773
774
775         if (!_send_alarm_create_appsvc(alarm_context, alarm_info, alarm_id, b, &error_code)) {
776                 return error_code;
777         }
778
779         return ALARMMGR_RESULT_SUCCESS;
780 }
781
782
783
784
785 EXPORT_API int alarmmgr_add_alarm_with_localtime(alarm_entry_t *alarm,
786                                                  const char *destination,
787                                                  alarm_id_t *alarm_id)
788 {
789         char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
790         char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = { 0 };
791         alarm_info_t *alarm_info;       /* = (alarm_info_t*)alarm; */
792         int ret;
793         int i = 0;
794         int j = 0;
795
796         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
797
798         if (alarm == NULL) {
799                 return ERR_ALARM_INVALID_PARAM;
800         }
801
802         alarm_info = (alarm_info_t *) alarm;
803         if (alarm_info == NULL || alarm_id == NULL) {
804                 return ERR_ALARM_INVALID_PARAM;
805         }
806
807         int error_code;
808         alarm_mode_t *mode = &alarm_info->mode;
809
810         ret = __sub_init();
811         if (ret < 0)
812                 return ret;
813
814         ALARM_MGR_EXCEPTION_PRINT("start(%d-%d-%d, %02d:%02d:%02d), end(%d-%d-%d), repeat(%d), interval(%d), type(%d)",
815                 alarm_info->start.day, alarm_info->start.month, alarm_info->start.year,
816                 alarm_info->start.hour, alarm_info->start.min, alarm_info->start.sec,
817                 alarm_info->end.year, alarm_info->end.month, alarm_info->end.day,
818                 alarm_info->mode.repeat, alarm_info->mode.u_interval, alarm_info->alarm_type);
819
820         /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
821         if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
822                 return ERR_ALARM_INVALID_PARAM;
823         }
824
825         if (destination && strlen(destination) >= MAX_PKG_NAME_LEN){
826                 ALARM_MGR_EXCEPTION_PRINT("[alarm-lib]: destination name is too long!\n");
827                 return ERR_ALARM_INVALID_PARAM;
828         }
829
830
831         if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
832                 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
833                 return error_code;
834         }
835
836         if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
837                 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
838                 return error_code;
839         }
840
841         if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
842                 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
843                 return error_code;
844         }
845
846         if (destination != NULL) {
847                 memset(dst_service_name, 0, strlen(destination) + strlen("ALARM.") + 2);
848                 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "ALARM.%s", destination);
849                 memset(dst_service_name_mod, 'a', MAX_SERVICE_NAME_LEN-1);
850
851                 for (i=0; i<=strlen(dst_service_name); i++)
852                 {
853                         if (dst_service_name[i] == '.' )
854                         {
855                                 dst_service_name_mod[j] = dst_service_name[i];
856                                 j++;
857                         }
858                         else
859                         {
860                                 dst_service_name_mod[j] = dst_service_name[i];
861                         }
862                         j++;
863                 }
864
865                 if (!_send_alarm_create(alarm_context, alarm_info, alarm_id, dst_service_name, dst_service_name_mod, &error_code)) {
866                         return error_code;
867                 }
868         } else {
869                 if (!_send_alarm_create(alarm_context, alarm_info, alarm_id, "null", "null", &error_code)) {
870                         return error_code;
871                 }
872         }
873
874         return ALARMMGR_RESULT_SUCCESS;
875 }
876
877
878
879 EXPORT_API int alarmmgr_add_alarm_appsvc(int alarm_type, time_t trigger_at_time,
880                                   time_t interval, void *bundle_data,
881                                   alarm_id_t *alarm_id)
882 {
883         int error_code = 0;;
884         struct timeval current_time;
885         struct tm duetime_tm;
886         alarm_info_t alarm_info;
887         const char *operation = NULL;
888         char *appid = NULL;
889
890         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
891
892         bundle *b=(bundle *)bundle_data;
893
894         if (NULL == b)
895         {
896                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle\n");
897                 return ERR_ALARM_INVALID_PARAM;
898         }
899         operation = appsvc_get_operation(b);
900
901         if (NULL == operation)
902         {
903                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle [appsvc operation not present]\n");
904                 return ERR_ALARM_INVALID_PARAM;
905         }
906
907         appid = appsvc_get_appid(b);
908
909         if (NULL == appid && (alarm_type & ALARM_TYPE_NOLAUNCH) )
910         {
911                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter\n");
912                 return ERR_ALARM_INVALID_PARAM;
913         }
914
915         if (__alarmmgr_init_appsvc() < 0)
916         {
917                 ALARM_MGR_EXCEPTION_PRINT("Unable to initialize dbus!!!\n");
918                 return ERR_ALARM_SYSTEM_FAIL;
919         }
920
921         if (alarm_id == NULL) {
922                 return ERR_ALARM_INVALID_PARAM;
923         }
924
925         if (trigger_at_time < 0) {
926                 return ERR_ALARM_INVALID_PARAM;
927         }
928
929         alarm_info.alarm_type = alarm_type;
930         alarm_info.alarm_type |= ALARM_TYPE_RELATIVE;
931
932         gettimeofday(&current_time, NULL);
933
934         if (current_time.tv_usec > 500 * 1000)
935         {
936                 // When the millisecond part of the current_time is bigger than 500ms,
937                 // the duetime increases by extra 1sec.
938                 current_time.tv_sec += (trigger_at_time + 1);
939         }
940         else
941         {
942                 current_time.tv_sec += trigger_at_time;
943         }
944
945         tzset();        // Processes the TZ environment variable, and Set timezone, daylight, and tzname.
946         localtime_r(&current_time.tv_sec, &duetime_tm);
947
948         alarm_info.start.year = duetime_tm.tm_year + 1900;
949         alarm_info.start.month = duetime_tm.tm_mon + 1;
950         alarm_info.start.day = duetime_tm.tm_mday;
951
952         alarm_info.end.year = 0;
953         alarm_info.end.month = 0;
954         alarm_info.end.day = 0;
955
956         alarm_info.start.hour = duetime_tm.tm_hour;
957         alarm_info.start.min = duetime_tm.tm_min;
958         alarm_info.start.sec = duetime_tm.tm_sec;
959
960         if (interval <= 0) {
961                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_ONCE;
962                 alarm_info.mode.u_interval.interval = 0;
963         } else {
964                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_REPEAT;
965                 alarm_info.mode.u_interval.interval = interval;
966         }
967
968         ALARM_MGR_EXCEPTION_PRINT("trigger_at_time(%d), start(%d-%d-%d, %02d:%02d:%02d), repeat(%d), interval(%d), type(%d)",
969                 trigger_at_time, alarm_info.start.day, alarm_info.start.month, alarm_info.start.year,
970                 alarm_info.start.hour, alarm_info.start.min, alarm_info.start.sec,
971                 alarm_info.mode.repeat, alarm_info.mode.u_interval, alarm_info.alarm_type);
972
973         if (!_send_alarm_create_appsvc(alarm_context, &alarm_info, alarm_id, b, &error_code)) {
974                 return error_code;
975         }
976
977         return ALARMMGR_RESULT_SUCCESS;
978 }
979
980
981 EXPORT_API int alarmmgr_add_alarm(int alarm_type, time_t trigger_at_time,
982                                   time_t interval, const char *destination,
983                                   alarm_id_t *alarm_id)
984 {
985         char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
986         char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = { 0 };
987         int i = 0;
988         int j = 0;
989         int error_code;
990         struct timeval current_time;
991         struct tm duetime_tm;
992         alarm_info_t alarm_info;
993         int ret;
994
995         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
996
997         ret = __sub_init();
998         if (ret < 0)
999                 return ret;
1000
1001         if (alarm_id == NULL) {
1002                 return ERR_ALARM_INVALID_PARAM;
1003         }
1004
1005         if (trigger_at_time < 0) {
1006                 return ERR_ALARM_INVALID_PARAM;
1007         }
1008
1009         if (destination && strlen(destination) >= MAX_PKG_NAME_LEN){
1010                 ALARM_MGR_EXCEPTION_PRINT("[alarm-lib]: destination name is too long!\n");
1011                 return ERR_ALARM_INVALID_PARAM;
1012         }
1013
1014         alarm_info.alarm_type = alarm_type;
1015         alarm_info.alarm_type |= ALARM_TYPE_RELATIVE;
1016
1017         gettimeofday(&current_time, NULL);
1018
1019         if (current_time.tv_usec > 500 * 1000)
1020         {
1021                 // When the millisecond part of the current_time is bigger than 500ms,
1022                 // the duetime increases by extra 1sec.
1023                 current_time.tv_sec += (trigger_at_time + 1);
1024         }
1025         else
1026         {
1027                 current_time.tv_sec += trigger_at_time;
1028         }
1029
1030         tzset();        // Processes the TZ environment variable, and Set timezone, daylight, and tzname.
1031         localtime_r(&current_time.tv_sec, &duetime_tm);
1032
1033         alarm_info.start.year = duetime_tm.tm_year + 1900;
1034         alarm_info.start.month = duetime_tm.tm_mon + 1;
1035         alarm_info.start.day = duetime_tm.tm_mday;
1036
1037         alarm_info.end.year = 0;
1038         alarm_info.end.month = 0;
1039         alarm_info.end.day = 0;
1040
1041         alarm_info.start.hour = duetime_tm.tm_hour;
1042         alarm_info.start.min = duetime_tm.tm_min;
1043         alarm_info.start.sec = duetime_tm.tm_sec;
1044
1045         if (interval <= 0) {
1046                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_ONCE;
1047                 alarm_info.mode.u_interval.interval = 0;
1048         } else {
1049                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_REPEAT;
1050                 alarm_info.mode.u_interval.interval = interval;
1051         }
1052
1053         ALARM_MGR_EXCEPTION_PRINT("trigger_at_time(%d), start(%d-%d-%d, %02d:%02d:%02d), repeat(%d), interval(%d), type(%d)",
1054                 trigger_at_time, alarm_info.start.day, alarm_info.start.month, alarm_info.start.year,
1055                 alarm_info.start.hour, alarm_info.start.min, alarm_info.start.sec,
1056                 alarm_info.mode.repeat, alarm_info.mode.u_interval, alarm_info.alarm_type);
1057
1058         if (destination != NULL) {
1059                 memset(dst_service_name, 0,
1060                        strlen(destination) + strlen("ALARM.") + 2);
1061                 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "ALARM.%s",
1062                          destination);
1063                 memset(dst_service_name_mod,'a',MAX_SERVICE_NAME_LEN-1);
1064
1065                 j=0;
1066
1067                 for(i=0;i<=strlen(dst_service_name);i++)
1068                 {
1069                         if (dst_service_name[i] == '.')
1070                         {
1071                                 dst_service_name_mod[j]=dst_service_name[i];
1072                                 j++;
1073                         }
1074                         else
1075                         {
1076                                 dst_service_name_mod[j]=dst_service_name[i];
1077                         }
1078                         j++;
1079                 }
1080
1081                 if (!_send_alarm_create
1082                     (alarm_context, &alarm_info, alarm_id, dst_service_name,dst_service_name_mod,
1083                      &error_code)) {
1084                         return error_code;
1085                 }
1086         } else
1087             if (!_send_alarm_create
1088                 (alarm_context, &alarm_info, alarm_id, "null","null", &error_code)) {
1089                 return error_code;
1090         }
1091
1092         return ALARMMGR_RESULT_SUCCESS;
1093 }
1094
1095 EXPORT_API int alarmmgr_add_alarm_withcb(int alarm_type, time_t trigger_at_time,
1096                                   time_t interval, alarm_cb_t handler, void *user_param, alarm_id_t *alarm_id)
1097 {
1098         char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
1099         char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = { 0 };
1100         int i = 0;
1101         int j = 0;
1102         int error_code = 0;
1103         struct timeval current_time;
1104         struct tm duetime_tm;
1105         alarm_info_t alarm_info;
1106         int ret = 0;
1107         char appid[256] = {0,};
1108
1109         if (aul_app_get_appid_bypid(getpid(), appid, sizeof(appid)) != AUL_R_OK) {
1110                 ALARM_MGR_LOG_PRINT("aul_app_get_appid_bypid() is failed. PID %d may not be app.", getpid());
1111         }
1112
1113         ret = alarmmgr_init(appid);
1114         if (ret < 0)
1115                 return ret;
1116
1117         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_add_alarm_withcb() is called");
1118
1119         if (alarm_id == NULL) {
1120                 return ERR_ALARM_INVALID_PARAM;
1121         }
1122
1123         if (trigger_at_time < 0) {
1124                 return ERR_ALARM_INVALID_PARAM;
1125         }
1126
1127         alarm_info.alarm_type = alarm_type;
1128         alarm_info.alarm_type |= ALARM_TYPE_RELATIVE;
1129         alarm_info.alarm_type |= ALARM_TYPE_WITHCB;
1130
1131         gettimeofday(&current_time, NULL);
1132
1133         if (current_time.tv_usec > 500 * 1000)
1134         {
1135                 // When the millisecond part of the current_time is bigger than 500ms,
1136                 // the duetime increases by extra 1sec.
1137                 current_time.tv_sec += (trigger_at_time + 1);
1138         }
1139         else
1140         {
1141                 current_time.tv_sec += trigger_at_time;
1142         }
1143
1144         tzset();        // Processes the TZ environment variable, and Set timezone, daylight, and tzname.
1145         localtime_r(&current_time.tv_sec, &duetime_tm);
1146
1147         alarm_info.start.year = duetime_tm.tm_year + 1900;
1148         alarm_info.start.month = duetime_tm.tm_mon + 1;
1149         alarm_info.start.day = duetime_tm.tm_mday;
1150
1151         alarm_info.end.year = 0;
1152         alarm_info.end.month = 0;
1153         alarm_info.end.day = 0;
1154
1155         alarm_info.start.hour = duetime_tm.tm_hour;
1156         alarm_info.start.min = duetime_tm.tm_min;
1157         alarm_info.start.sec = duetime_tm.tm_sec;
1158
1159         if (interval <= 0) {
1160                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_ONCE;
1161                 alarm_info.mode.u_interval.interval = 0;
1162         } else {
1163                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_REPEAT;
1164                 alarm_info.mode.u_interval.interval = interval;
1165         }
1166
1167         ALARM_MGR_EXCEPTION_PRINT("trigger_at_time(%d), start(%d-%d-%d, %02d:%02d:%02d), repeat(%d), interval(%d), type(%d)",
1168                 trigger_at_time, alarm_info.start.day, alarm_info.start.month, alarm_info.start.year,
1169                 alarm_info.start.hour, alarm_info.start.min, alarm_info.start.sec,
1170                 alarm_info.mode.repeat, alarm_info.mode.u_interval.interval, alarm_info.alarm_type);
1171
1172         if (!_send_alarm_create(alarm_context, &alarm_info, alarm_id, "null","null", &error_code)) {
1173                 return error_code;
1174         }
1175         __add_resultcb(*alarm_id, handler, user_param);
1176
1177         return ALARMMGR_RESULT_SUCCESS;
1178 }
1179
1180 EXPORT_API int alarmmgr_remove_alarm(alarm_id_t alarm_id)
1181 {
1182         int error_code;
1183         int ret;
1184         alarm_cb_info_t *info;
1185
1186         ret = __sub_init();
1187         if (ret < 0)
1188                 return ret;
1189
1190         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_delete(%d) is called\n", alarm_id);
1191
1192         if (alarm_id <= 0) {
1193                 return ERR_ALARM_INVALID_ID;
1194         }
1195
1196         if (!_send_alarm_delete(alarm_context, alarm_id, &error_code))
1197                 return error_code;
1198
1199         info = __find_resultcb(alarm_id);
1200         __remove_resultcb(info);
1201
1202         return ALARMMGR_RESULT_SUCCESS;
1203 }
1204
1205 EXPORT_API int alarmmgr_remove_all(void)
1206 {
1207         int error_code;
1208         int return_code = ALARMMGR_RESULT_SUCCESS;
1209         int ret = __sub_init();
1210         if (ret < 0)
1211         {
1212                 return ret;
1213         }
1214
1215         if (!_send_alarm_delete_all(alarm_context, &error_code))
1216                 return error_code;
1217
1218         return return_code;
1219 }
1220
1221 EXPORT_API int alarmmgr_enum_alarm_ids(alarm_enum_fn_t fn, void *user_param)
1222 {
1223         SECURE_LOGD("Enter");
1224         GError *error = NULL;
1225         GVariant *alarm_array = NULL;
1226         int return_code = 0;
1227         int maxnum_of_ids = 0;
1228         int num_of_ids = 0;
1229         alarm_id_t alarm_id = -1;
1230         int ret = 0;
1231         char *e_cookie = NULL;
1232         char cookie[256] = {0,};
1233         int size = 0;
1234         GVariantIter *iter = NULL;
1235
1236         if (fn == NULL) {
1237                 return ERR_ALARM_INVALID_PARAM;
1238         }
1239
1240         size = security_server_get_cookie_size();
1241         ret = security_server_request_cookie(cookie, size);
1242         if (ret < 0) {
1243                 ALARM_MGR_EXCEPTION_PRINT("security_server_request_cookie() is failed.");
1244                 return ERR_ALARM_SYSTEM_FAIL;
1245         }
1246
1247         e_cookie = g_base64_encode((const guchar *)cookie, size);
1248         if (e_cookie == NULL) {
1249                 ALARM_MGR_EXCEPTION_PRINT("g_base64_encode() is failed.");
1250                 return ERR_ALARM_SYSTEM_FAIL;
1251         }
1252
1253         ret = __sub_init();
1254         if (ret < 0) {
1255                 ALARM_MGR_EXCEPTION_PRINT("__sub_init() is failed.");
1256                 g_free(e_cookie);
1257                 return ret;
1258         }
1259
1260         SECURE_LOGD("alarm_manager_call_alarm_get_number_of_ids_sync() is called");
1261         if (!alarm_manager_call_alarm_get_number_of_ids_sync(
1262             (AlarmManager*)alarm_context.proxy, alarm_context.pid, e_cookie, &maxnum_of_ids, &return_code, NULL, &error)) {
1263                 /* dbus-glib error */
1264                 /* error_code should be set */
1265                 ALARM_MGR_EXCEPTION_PRINT(
1266                     "alarm_manager_call_alarm_get_number_of_ids_sync() is failed by dbus. return_code[%d], err message[%s]",
1267                     return_code, error->message);
1268                 g_free(e_cookie);
1269                 return ERR_ALARM_SYSTEM_FAIL;
1270         }
1271
1272         g_free(e_cookie);
1273
1274         if (return_code != ALARMMGR_RESULT_SUCCESS) {
1275                 ALARM_MGR_EXCEPTION_PRINT("alarm_manager_call_alarm_get_number_of_ids_sync() is failed. return_code[%d]", return_code);
1276                 return return_code;
1277         } else {
1278                 ALARM_MGR_LOG_PRINT("maxnum_of_ids[%d]", maxnum_of_ids);
1279         }
1280
1281         SECURE_LOGD("alarm_manager_call_alarm_get_list_of_ids_sync() is called");
1282         if (!alarm_manager_call_alarm_get_list_of_ids_sync(
1283                      (AlarmManager*)alarm_context.proxy, alarm_context.pid, maxnum_of_ids, &alarm_array, &num_of_ids, &return_code, NULL, &error)) {
1284                 /*dbus-glib error */
1285                 /* error_code should be set */
1286                 ALARM_MGR_EXCEPTION_PRINT(
1287                     "alarm_manager_call_alarm_get_list_of_ids_sync() failed by dbus. num_of_ids[%d], return_code[%d].", num_of_ids, return_code);
1288                 return ERR_ALARM_SYSTEM_FAIL;
1289         }
1290
1291         if (return_code != ALARMMGR_RESULT_SUCCESS) {
1292                 return return_code;
1293         }
1294
1295         if (error != NULL) {
1296                 ALARM_MGR_LOG_PRINT("Alarm server is not ready dbus. error message %s.", error->message);
1297                 return ERR_ALARM_SYSTEM_FAIL;
1298         }
1299
1300         if (alarm_array == NULL) {
1301                 ALARM_MGR_LOG_PRINT("alarm server is not initilized.");
1302                 return ERR_ALARM_SYSTEM_FAIL;
1303         }
1304
1305         g_variant_get(alarm_array, "ai", &iter);
1306         while (g_variant_iter_loop(iter, "i", &alarm_id))
1307         {
1308                 (*fn) (alarm_id, user_param);
1309                 ALARM_MGR_LOG_PRINT("alarm_id (%d)", alarm_id);
1310         }
1311         g_variant_iter_free(iter);
1312         g_variant_unref(alarm_array);
1313
1314         SECURE_LOGD("Leave");
1315         return ALARMMGR_RESULT_SUCCESS;
1316 }
1317
1318 EXPORT_API int alarmmgr_get_info(alarm_id_t alarm_id, alarm_entry_t *alarm)
1319 {
1320         int error_code;
1321         alarm_info_t *alarm_info = (alarm_info_t *) alarm;
1322
1323         int ret;
1324
1325         ret = __sub_init();
1326         if (ret < 0)
1327                 return ret;
1328
1329         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_get_info() is called\n");
1330
1331         if (alarm_id < 0 || alarm_info == NULL) {
1332                 return ERR_ALARM_INVALID_PARAM;
1333         }
1334
1335         if (!_send_alarm_get_info(alarm_context, alarm_id, alarm_info, &error_code)) {
1336                 return error_code;
1337         }
1338
1339         return ALARMMGR_RESULT_SUCCESS;
1340 }
1341
1342 int alarmmgr_create(alarm_info_t *alarm_info, char *destination,
1343                     alarm_id_t *alarm_id)
1344 {
1345         char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
1346         alarm_mode_t *mode = &alarm_info->mode;
1347         int error_code;
1348
1349         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
1350
1351         if (alarm_info == NULL || alarm_id == NULL) {
1352                 return ERR_ALARM_INVALID_PARAM;
1353         }
1354
1355         ALARM_MGR_LOG_PRINT("alarm_info->start.year(%d), "
1356                             "alarm_info->start.month(%d), alarm_info->start.day(%d)",
1357                             alarm_info->start.year, alarm_info->start.month,
1358                             alarm_info->start.day);
1359
1360         /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
1361         if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
1362                 return ERR_ALARM_INVALID_PARAM;
1363         }
1364
1365         if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
1366                 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
1367                 return error_code;
1368         }
1369
1370         if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
1371                 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
1372                 return error_code;
1373         }
1374
1375         if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
1376                 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
1377                 return error_code;
1378         }
1379
1380         if (destination != NULL) {
1381                 memset(dst_service_name, 0,
1382                        strlen(destination) + strlen("ALARM.") + 2);
1383                 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "ALARM.%s",
1384                          destination);
1385                 if (!_send_alarm_create
1386                     (alarm_context, alarm_info, alarm_id, dst_service_name,"null",
1387                      &error_code)) {
1388                         return error_code;
1389                 }
1390         }
1391 /*TODO: Currently this API is not exported. Hence not modifying*/
1392         if (!_send_alarm_create
1393             (alarm_context, alarm_info, alarm_id, "null", "null", &error_code)) {
1394                 return error_code;
1395         }
1396
1397         return ALARMMGR_RESULT_SUCCESS;
1398
1399 }
1400
1401 int alarmmgr_get_number_of_ids(int *num_of_ids)
1402 {
1403         int error_code;
1404         ALARM_MGR_LOG_PRINT("[alarm-lib]: alarm_get_number_of_ids() is called.");
1405
1406         if (num_of_ids == NULL) {
1407                 return ERR_ALARM_INVALID_PARAM;
1408         }
1409         ALARM_MGR_LOG_PRINT("call alarm_get_number_of_ids\n");
1410         if (!_send_alarm_get_number_of_ids(alarm_context, num_of_ids, &error_code)) {
1411                 return error_code;
1412         }
1413
1414         return ALARMMGR_RESULT_SUCCESS;
1415 }
1416
1417 int alarmmgr_get_list_of_ids(int maxnum_of_ids, alarm_id_t *alarm_id,
1418                              int *num_of_ids)
1419 {
1420         int error_code;
1421         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_get_list_of_ids() is called.");
1422
1423         if (maxnum_of_ids < 0 || alarm_id == NULL || num_of_ids == NULL) {
1424                 return ERR_ALARM_INVALID_PARAM;
1425         }
1426
1427         if (maxnum_of_ids == 0) {
1428                 *num_of_ids = 0;
1429                 return ALARMMGR_RESULT_SUCCESS;
1430         }
1431
1432         if (!_send_alarm_get_list_of_ids
1433             (alarm_context, maxnum_of_ids, alarm_id, num_of_ids, &error_code)) {
1434                 return error_code;
1435         }
1436
1437         return ALARMMGR_RESULT_SUCCESS;
1438 }
1439
1440 EXPORT_API int alarmmgr_get_next_duetime(alarm_id_t alarm_id, time_t* duetime)
1441 {
1442         int error_code;
1443         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_get_next_duetime() is called.");
1444
1445         if (duetime == NULL) {
1446                 return ERR_ALARM_INVALID_PARAM;
1447         }
1448
1449         if (!_send_alarm_get_next_duetime(alarm_context, alarm_id, duetime, &error_code)) {
1450                 return error_code;
1451         }
1452
1453         return ALARMMGR_RESULT_SUCCESS;
1454 }
1455
1456 EXPORT_API int alarmmgr_get_all_info(char **db_path)
1457 {
1458         int error_code;
1459         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_get_all_info() is called.");
1460
1461         if (db_path == NULL) {
1462                 return ERR_ALARM_INVALID_PARAM;
1463         }
1464
1465         if (!_send_alarm_get_all_info(alarm_context, db_path, &error_code)) {
1466                 return error_code;
1467         }
1468
1469         ALARM_MGR_LOG_PRINT("[alarm-lib]: successfully save info in %s.", *db_path);
1470         return ALARMMGR_RESULT_SUCCESS;
1471 }