1d8e1d76eec9e0a08f1bb003c606760188baa78d
[framework/appfw/alarm-manager.git] / 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<dbus/dbus.h>
34 #include<dbus/dbus-glib.h>
35 #include<glib.h>
36 #include <fcntl.h>
37 #include <dbus/dbus-glib-lowlevel.h>
38
39 #include "alarm.h"
40 #include "alarm-internal.h"
41 #include "alarm-stub.h"
42 #include <bundle.h>
43 #include <appsvc.h>
44 #include <aul.h>
45
46 #define MAX_KEY_SIZE 256
47
48 static alarm_context_t alarm_context = { NULL, NULL, NULL, NULL, -1 };
49
50 static bool b_initialized = false;
51 static bool sub_initialized = false;
52
53 #define MAX_OBJECT_PATH_LEN 256
54 #define DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT 0
55
56 static DBusHandlerResult __expire_alarm_filter(DBusConnection *connection,
57                                                DBusMessage *message,
58                                                void *user_data);
59 static int __alarm_validate_date(alarm_date_t *date, int *error_code);
60 static bool __alarm_validate_time(alarm_date_t *date, int *error_code);
61 static int __sub_init(void);
62 static int __alarmmgr_init_appsvc(void);
63 bool alarm_power_off(int *error_code);
64 int alarmmgr_check_next_duetime(void);
65
66 typedef struct _alarm_cb_info_t {
67         int alarm_id;
68         alarm_cb_t cb_func;
69         void *priv_data;
70         struct _alarm_cb_info_t *next;
71 } alarm_cb_info_t;
72
73 static alarm_cb_info_t *alarmcb_head = NULL;
74
75 static void __add_resultcb(int alarm_id, alarm_cb_t cb_func,
76                          void *data)
77 {
78         alarm_cb_info_t *info;
79
80         info = (alarm_cb_info_t *) malloc(sizeof(alarm_cb_info_t));
81         if(info == NULL)
82                 return;
83         info->alarm_id = alarm_id;
84         info->cb_func = cb_func;
85         info->priv_data = data;
86
87         info->next = alarmcb_head;
88         alarmcb_head = info;
89 }
90
91 static alarm_cb_info_t *__find_resultcb(int alarm_id)
92 {
93         alarm_cb_info_t *tmp;
94
95         tmp = alarmcb_head;
96         while (tmp) {
97                 if (tmp->alarm_id == alarm_id)
98                         return tmp;
99                 tmp = tmp->next;
100         }
101         return NULL;
102 }
103
104 static void __remove_resultcb(alarm_cb_info_t *info)
105 {
106         alarm_cb_info_t *tmp;
107
108         if (alarmcb_head == NULL || info == NULL)
109                 return;
110
111         if (alarmcb_head == info) {
112                 alarmcb_head = info->next;
113                 free(info);
114                 return;
115         }
116
117         tmp = alarmcb_head;
118         while (tmp) {
119                 if (tmp->next == info) {
120                         tmp->next = info->next;
121                         free(info);
122                         return;
123                 }
124                 tmp = tmp->next;
125         }
126 }
127
128 static DBusHandlerResult __expire_alarm_filter(DBusConnection *connection,
129                                                DBusMessage *message,
130                                                void *user_data)
131 {
132         alarm_cb_info_t *info;
133
134         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL) {
135                 const char *method_name = dbus_message_get_member(message);
136                 /*"alarm_expired" */
137
138                 if (strcmp(method_name, "alarm_expired") == 0) {
139                         DBusMessageIter iter;
140                         alarm_id_t alarm_id;
141                         const char *service_name =
142                             dbus_message_get_destination(message);
143                         const char *object_path =
144                             dbus_message_get_path(message);
145                         /* "/org/tizen/alarm/client" */
146                         const char *interface_name =
147                             dbus_message_get_interface(message);
148                         /* "org.tizen.alarm.client" */
149
150                         dbus_message_iter_init(message, &iter);
151                         dbus_message_iter_get_basic(&iter, &alarm_id);
152
153                         ALARM_MGR_LOG_PRINT("[alarm-lib]:service_name=%s, "
154                         "object_path=%s, interface_name=%s, method_name=%s, "
155                         "alarm_id=%d, handler=%s\n",
156                         service_name ? service_name : "no name",
157                         object_path ? object_path : "no path",
158                         interface_name ? interface_name : "no interface",
159                         method_name ? method_name : "no method", alarm_id,
160                         alarm_context.alarm_handler ? "ok" : "no handler");
161
162                         if (alarm_context.alarm_handler != NULL)
163                                 /* alarm_context.alarm_handler(alarm_id); */
164                                 alarm_context.alarm_handler(alarm_id,
165                                         alarm_context.user_param);
166                         info = __find_resultcb(alarm_id);
167
168                         if( info && info->cb_func ) {
169                                 info->cb_func(alarm_id, info->priv_data);
170                         //      __remove_resultcb(info);
171                         }
172
173                         return DBUS_HANDLER_RESULT_HANDLED;
174                 }
175         }
176
177         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
178 }
179
180 static int __alarm_validate_date(alarm_date_t *date, int *error_code)
181 {
182
183         if (date->year == 0 && date->month == 0 && date->day == 0) {
184                 return true;
185         }
186
187         int year = date->year;
188         int month = date->month;
189         int day = date->day;
190
191         if (month < 1 || month > 12) {
192                 if (error_code)
193                         *error_code = ERR_ALARM_INVALID_DATE;
194                 return false;
195         }
196
197         if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8
198              || month == 10 || month == 12)
199             && (day < 1 || day > 31)) {
200                 if (error_code)
201                         *error_code = ERR_ALARM_INVALID_DATE;
202                 return false;
203         }
204
205         if ((month == 4 || month == 6 || month == 9 || month == 11)
206             && (day < 1 || day > 30)) {
207                 if (error_code)
208                         *error_code = ERR_ALARM_INVALID_DATE;
209                 return false;
210         }
211
212         if (month == 2) {
213                 if ((year % 100 != 0 && year % 4 == 0) || (year % 400 == 0)) {
214                         if (day < 1 || day > 29) {
215                                 if (error_code)
216                                         *error_code = ERR_ALARM_INVALID_DATE;
217                                 return false;
218                         }
219                 } else {
220                         if (day < 1 || day > 28) {
221                                 if (error_code)
222                                         *error_code = ERR_ALARM_INVALID_DATE;
223                                 return false;
224                         }
225                 }
226
227         }
228
229         return true;
230
231 }
232
233 static bool __alarm_validate_time(alarm_date_t *date, int *error_code)
234 {
235         if (date->hour < 0 || date->hour > 23) {
236                 if (error_code)
237                         *error_code = ERR_ALARM_INVALID_TIME;
238                 return false;
239         }
240
241         if (date->min < 0 || date->min > 59) {
242                 if (error_code)
243                         *error_code = ERR_ALARM_INVALID_TIME;
244                 return false;
245         }
246
247         return true;
248 }
249
250 static int __sub_init()
251 {
252         GError *error = NULL;
253
254         if (sub_initialized) {
255                 ALARM_MGR_EXCEPTION_PRINT("__sub_init was already called.\n");
256                 return ALARMMGR_RESULT_SUCCESS;
257         }
258
259         g_thread_init(NULL);
260         dbus_g_thread_init();
261
262         alarm_context.bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
263         if (alarm_context.bus == NULL) {
264                 ALARM_MGR_EXCEPTION_PRINT("dbus bus get failed\n");
265
266                 return ERR_ALARM_SYSTEM_FAIL;
267         }
268
269         alarm_context.proxy = dbus_g_proxy_new_for_name(alarm_context.bus,
270                                                         "org.tizen.alarm.manager",
271                                                         "/org/tizen/alarm/manager",
272                                                         "org.tizen.alarm.manager");
273         if (alarm_context.proxy == NULL) {
274                 ALARM_MGR_EXCEPTION_PRINT("dbus bus proxy get failed\n");
275
276                 return ERR_ALARM_SYSTEM_FAIL;
277         }
278
279         alarm_context.pid = getpid();   /*this running appliction's process id*/
280
281         sub_initialized = true;
282
283         return ALARMMGR_RESULT_SUCCESS;
284 }
285
286 bool alarm_power_off(int *error_code)
287 {
288         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_power_off() is called\n");
289
290 #ifdef __ALARM_BOOT
291         return _send_alarm_power_off(alarm_context, error_code);
292 #else
293         ALARM_MGR_LOG_PRINT(
294                         "[alarm-lib]:ALARM_BOOT feature is not supported. "
295                             "so we return false.\n");
296         if (error_code)
297                 *error_code = -1;       /*-1 means that system failed 
298                                                         internally.*/
299         return false;
300 #endif
301 }
302
303 int alarmmgr_check_next_duetime()
304 {
305         int error_code;
306         ALARM_MGR_LOG_PRINT(
307             "[alarm-lib]:alarm_check_next_duetime() is called\n");
308
309 #ifdef __ALARM_BOOT
310         if (!_send_alarm_check_next_duetime(alarm_context, &error_code))
311                 return error_code;
312 #else
313         ALARM_MGR_LOG_PRINT(
314                     "[alarm-lib]:ALARM_BOOT feature is not supported. "
315                             "so we return false.\n");
316         return ERR_ALARM_SYSTEM_FAIL;
317 #endif
318
319         return ALARMMGR_RESULT_SUCCESS;
320 }
321
322 EXPORT_API int alarmmgr_init(const char *appid)
323 {
324         DBusError derror;
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
329         int ret;
330         int i = 0;
331         int j = 0;
332
333         if (appid == NULL)
334                 return ERR_ALARM_INVALID_PARAM;
335
336         if (strlen(appid) >= MAX_PKG_NAME_LEN)
337                 return ERR_ALARM_INVALID_PARAM;
338
339         if (b_initialized) {
340                 ALARM_MGR_EXCEPTION_PRINT(
341                      "alarm was already initialized. app_service_name=%s\n",
342                      g_quark_to_string(alarm_context.quark_app_service_name));
343                 return ALARMMGR_RESULT_SUCCESS;
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         strncpy(service_name, appid, MAX_PKG_NAME_LEN);
352
353         j=0;
354
355         for(i=0;i<=strlen(service_name);i++)
356         {
357                 if (service_name[i] == '.' )
358                 {
359                         service_name_mod[j] = service_name[i];
360                         j++;
361                 }
362                 else{
363                         service_name_mod[j] = service_name[i];
364                 }
365                 j++;
366         }
367
368         strncat(service_name, ".ALARM", strlen(".ALARM"));
369
370         strncat(service_name_mod, ".ALARM", strlen(".ALARM"));
371
372
373         dbus_error_init(&derror);
374
375         request_name_result = dbus_bus_request_name(
376                           dbus_g_connection_get_connection(alarm_context.bus),
377                           service_name_mod, 0, &derror);
378         if (dbus_error_is_set(&derror)) /*failure*/ {
379                 ALARM_MGR_EXCEPTION_PRINT(
380                      "Failed to dbus_bus_request_name(%s): %s\n", service_name,
381                      derror.message);
382                 dbus_error_free(&derror);
383
384                 return ERR_ALARM_SYSTEM_FAIL;
385         }
386         alarm_context.quark_app_service_name =
387             g_quark_from_string(service_name);
388         alarm_context.quark_app_service_name_mod=
389             g_quark_from_string(service_name_mod);
390
391
392         if (!dbus_connection_add_filter(
393              dbus_g_connection_get_connection(alarm_context.bus),
394              __expire_alarm_filter, NULL, NULL)) {
395                 ALARM_MGR_EXCEPTION_PRINT("add __expire_alarm_filter failed\n");
396
397                 return ERR_ALARM_SYSTEM_FAIL;
398         }
399
400         b_initialized = true;
401         return ALARMMGR_RESULT_SUCCESS;
402
403 }
404
405 EXPORT_API void alarmmgr_fini()
406 {
407         dbus_connection_remove_filter(dbus_g_connection_get_connection
408                                       (alarm_context.bus),
409                                       __expire_alarm_filter, NULL);
410 }
411
412 EXPORT_API int alarmmgr_set_cb(alarm_cb_t handler, void *user_param)
413 {
414         ALARM_MGR_LOG_PRINT("alarm_set_cb is called\n");
415
416         if (handler == NULL) {
417                 return ERR_ALARM_INVALID_PARAM;
418         }
419         alarm_context.alarm_handler = handler;
420         alarm_context.user_param = user_param;
421         return ALARMMGR_RESULT_SUCCESS;
422 }
423
424 EXPORT_API alarm_entry_t *alarmmgr_create_alarm(void)
425 {
426         alarm_info_t *alarm = (alarm_info_t *) malloc(sizeof(alarm_info_t));
427
428         if (NULL == alarm)
429         {
430                 return NULL;
431         }
432
433         alarm->start.year = 0;
434         alarm->start.month = 0;
435         alarm->start.day = 0;
436         alarm->start.hour = 0;
437         alarm->start.min = 0;
438         alarm->start.sec = 0;
439
440         alarm->end.year = 0;
441         alarm->end.month = 0;
442         alarm->end.day = 0;
443         alarm->end.hour = 0;
444         alarm->end.min = 0;
445         alarm->end.sec = 0;
446
447         alarm->mode.repeat = ALARM_REPEAT_MODE_ONCE;
448         alarm->mode.u_interval.interval = 0;
449
450         alarm->alarm_type = ALARM_TYPE_DEFAULT;
451
452         alarm->reserved_info = 0;
453
454         return (alarm_entry_t *) alarm;
455 }
456
457 EXPORT_API int alarmmgr_free_alarm(alarm_entry_t *alarm)
458 {
459         if (alarm == NULL) {
460                 return ERR_ALARM_INVALID_PARAM;
461         }
462         free(alarm);
463
464         return ALARMMGR_RESULT_SUCCESS;
465 }
466
467 EXPORT_API int alarmmgr_set_time(alarm_entry_t *alarm, alarm_date_t time)
468 {
469         alarm_info_t *alarm_info;       /*= (alarm_info_t*)alarm;*/
470         int error_code;
471
472         if (alarm == NULL) {
473                 return ERR_ALARM_INVALID_PARAM;
474         }
475
476         alarm_info = (alarm_info_t *) alarm;
477
478         if (!__alarm_validate_date(&time, &error_code)) {
479                 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
480                 return error_code;
481         }
482
483         if (!__alarm_validate_time(&time, &error_code)) {
484                 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
485                 return error_code;
486         }
487
488         memcpy(&alarm_info->start, &time, sizeof(alarm_date_t));
489
490         return ALARMMGR_RESULT_SUCCESS;
491 }
492
493 EXPORT_API int alarmmgr_get_time(const alarm_entry_t *alarm,
494                                  alarm_date_t *time)
495 {
496         alarm_info_t *alarm_info = (alarm_info_t *) alarm;
497
498         if (alarm == NULL) {
499                 return ERR_ALARM_INVALID_PARAM;
500         }
501
502         if (time != NULL)
503                 memcpy(time, &alarm_info->start, sizeof(alarm_date_t));
504
505         return ALARMMGR_RESULT_SUCCESS;
506 }
507
508 EXPORT_API int alarmmgr_set_repeat_mode(alarm_entry_t *alarm,
509                                         alarm_repeat_mode_t repeat,
510                                         int interval)
511 {
512         alarm_info_t *alarm_info = (alarm_info_t *) alarm;
513
514         if (repeat >= ALARM_REPEAT_MODE_MAX) {
515                 return ERR_ALARM_INVALID_PARAM;
516         }
517
518         alarm_info->mode.repeat = repeat;
519
520         if (repeat == ALARM_REPEAT_MODE_REPEAT
521             || repeat == ALARM_REPEAT_MODE_WEEKLY) {
522                 alarm_info->mode.u_interval.interval = interval;
523         }
524
525         return ALARMMGR_RESULT_SUCCESS;
526 }
527
528 EXPORT_API int alarmmgr_get_repeat_mode(const alarm_entry_t *alarm,
529                                         alarm_repeat_mode_t *repeat,
530                                         int *interval)
531 {
532         alarm_info_t *alarm_info = (alarm_info_t *) alarm;
533
534         if (alarm == NULL) {
535                 return ERR_ALARM_INVALID_PARAM;
536         }
537
538         if (repeat != NULL)
539                 *repeat = alarm_info->mode.repeat;
540         if (interval != NULL)
541                 *interval = alarm_info->mode.u_interval.interval;
542
543         return ALARMMGR_RESULT_SUCCESS;
544 }
545
546 EXPORT_API int alarmmgr_set_type(alarm_entry_t *alarm, int alarm_type)
547 {
548         alarm_info_t *alarm_info;       /*= (alarm_info_t*)alarm;*/
549
550         if (alarm == NULL) {
551                 return ERR_ALARM_INVALID_PARAM;
552         }
553
554         alarm_info = (alarm_info_t *) alarm;
555
556         alarm_info->alarm_type = alarm_type;
557         alarm_info->alarm_type &= (~ALARM_TYPE_RELATIVE);
558
559         return ALARMMGR_RESULT_SUCCESS;
560 }
561
562 EXPORT_API int alarmmgr_get_type(const alarm_entry_t *alarm, int *alarm_type)
563 {
564         alarm_info_t *alarm_info = (alarm_info_t *) alarm;
565
566         if (alarm == NULL) {
567                 return ERR_ALARM_INVALID_PARAM;
568         }
569
570         if (alarm_type != NULL)
571                 *alarm_type = alarm_info->alarm_type;
572
573         return ALARMMGR_RESULT_SUCCESS;
574 }
575
576
577 static int __alarmmgr_init_appsvc(void)
578 {
579         int ret;
580
581         if (b_initialized) {
582                 ALARM_MGR_EXCEPTION_PRINT("alarm was already initialized\n");
583                 return ALARMMGR_RESULT_SUCCESS;
584         }
585
586         g_thread_init(NULL);
587         
588         dbus_g_thread_init();
589
590         ret = __sub_init();
591         if (ret < 0)
592                 return ret;
593
594         b_initialized = true;
595         
596         return ALARMMGR_RESULT_SUCCESS;
597
598 }
599
600 EXPORT_API void *alarmmgr_get_alarm_appsvc_info(alarm_id_t alarm_id, int *return_code){
601
602         int ret = 0;
603
604         ret = __sub_init();
605         if (ret < 0){
606                 if (return_code)
607                         *return_code = ret;
608                 return NULL;
609         }
610
611         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_get_alarm_appsvc_info() is called\n");
612
613         if (alarm_id <= 0) {
614                 if (return_code)
615                         *return_code = ERR_ALARM_INVALID_ID;
616                 return NULL;
617         }
618
619         return _send_alarm_get_appsvc_info(alarm_context, alarm_id, return_code);
620
621 }
622
623 EXPORT_API int alarmmgr_set_rtc_time(alarm_date_t *time){
624
625         int ret = 0;
626         int error_code = 0;
627
628         if (!time){
629                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter time\n");
630                 return ERR_ALARM_INVALID_PARAM;
631         }
632
633         ret = __sub_init();
634         if (ret < 0){
635                 return ret;
636         }
637
638         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_set_rtc_time() is called\n");
639
640         if (!__alarm_validate_date(time, &error_code)) {
641                 ALARM_MGR_EXCEPTION_PRINT("RTC date error\n");
642                 return error_code;
643         }
644
645         if (!__alarm_validate_time(time, &error_code)) {
646                 ALARM_MGR_EXCEPTION_PRINT("RTC time error\n");
647                 return error_code;
648         }
649
650         time->year-=1900;
651         time->month-=1;
652
653         if (!_send_alarm_set_rtc_time
654                 (alarm_context, time, &error_code)){
655                         return error_code;
656         }
657
658         return ALARMMGR_RESULT_SUCCESS;
659
660 }
661
662 EXPORT_API int alarmmgr_add_alarm_appsvc_with_localtime(alarm_entry_t *alarm, void *bundle_data, alarm_id_t *alarm_id)
663 {
664         alarm_info_t *alarm_info = NULL;        /* = (alarm_info_t*)alarm; */
665         const char *operation = NULL;
666         int error_code = 0;
667         char *appid = NULL;
668
669         bundle *b=(bundle *)bundle_data;
670
671         if (alarm == NULL) {
672                 return ERR_ALARM_INVALID_PARAM;
673         }
674
675         if (NULL == b)
676         {
677                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle\n");
678                 return ERR_ALARM_INVALID_PARAM;
679         }
680         operation = appsvc_get_operation(b);
681         
682         if (NULL == operation)
683         {
684                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle [appsvc operation not present]\n");
685                 return ERR_ALARM_INVALID_PARAM;
686         }
687
688         if (__alarmmgr_init_appsvc() < 0)
689         {
690                 ALARM_MGR_EXCEPTION_PRINT("Unable to initialize dbus!!!\n");
691                 return ERR_ALARM_SYSTEM_FAIL;
692         }
693
694         alarm_info = (alarm_info_t *) alarm;
695
696         appid = appsvc_get_appid(b);
697
698         if (NULL == appid && (alarm_info->alarm_type & ALARM_TYPE_NOLAUNCH) )
699         {
700                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter\n");
701                 return ERR_ALARM_INVALID_PARAM;
702         }
703
704         if (alarm_info == NULL || alarm_id == NULL) {
705                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter\n");
706                 return ERR_ALARM_INVALID_PARAM;
707         }
708         alarm_mode_t *mode = &alarm_info->mode;
709
710         ALARM_MGR_LOG_PRINT("alarm_info->start.year(%d), "
711                             "alarm_info->start.month(%d), alarm_info->start.day(%d)",
712                             alarm_info->start.year, alarm_info->start.month,
713                             alarm_info->start.day);
714
715         /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
716         if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
717                 return ERR_ALARM_INVALID_PARAM;
718         }
719
720         if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
721                 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
722                 return error_code;
723         }
724
725         if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
726                 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
727                 return error_code;
728         }
729
730         if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
731                 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
732                 return error_code;
733         }
734
735
736         if (!_send_alarm_create_appsvc
737             (alarm_context, alarm_info, alarm_id, b,
738              &error_code)) {
739                 return error_code;
740         }       
741
742         return ALARMMGR_RESULT_SUCCESS;
743 }
744
745
746
747
748 EXPORT_API int alarmmgr_add_alarm_with_localtime(alarm_entry_t *alarm,
749                                                  const char *destination,
750                                                  alarm_id_t *alarm_id)
751 {
752         char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
753         char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = { 0 };
754         alarm_info_t *alarm_info;       /* = (alarm_info_t*)alarm; */
755         int ret;
756         int i = 0;
757         int j = 0;
758
759         if (alarm == NULL) {
760                 return ERR_ALARM_INVALID_PARAM;
761         }
762
763         alarm_info = (alarm_info_t *) alarm;
764         if (alarm_info == NULL || alarm_id == NULL) {
765                 return ERR_ALARM_INVALID_PARAM;
766         }
767
768         int error_code;
769         alarm_mode_t *mode = &alarm_info->mode;
770
771         ret = __sub_init();
772         if (ret < 0)
773                 return ret;
774
775         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
776
777         ALARM_MGR_LOG_PRINT("alarm_info->start.year(%d), "
778                             "alarm_info->start.month(%d), alarm_info->start.day(%d)",
779                             alarm_info->start.year, alarm_info->start.month,
780                             alarm_info->start.day);
781
782         /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
783         if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
784                 return ERR_ALARM_INVALID_PARAM;
785         }
786
787         if (destination && strlen(destination) >= MAX_PKG_NAME_LEN){
788                 ALARM_MGR_EXCEPTION_PRINT("[alarm-lib]: destination name is too long!\n");
789                 return ERR_ALARM_INVALID_PARAM;
790         }
791
792
793         if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
794                 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
795                 return error_code;
796         }
797
798         if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
799                 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
800                 return error_code;
801         }
802
803         if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
804                 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
805                 return error_code;
806         }
807
808         if (destination != NULL) {
809                 memset(dst_service_name, 0,
810                        strlen(destination) + strlen(".ALARM") + 2);
811                 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "%s",
812                          destination);
813
814                 memset(dst_service_name_mod,'a',MAX_SERVICE_NAME_LEN-1);
815
816                 j=0;
817
818                 for(i=0; i<=strlen(dst_service_name); i++)
819                 {
820                         if (dst_service_name[i] == '.' )
821                         {
822                                 dst_service_name_mod[j] = dst_service_name[i];
823                                 j++;
824                         }
825                         else
826                         {
827                                 dst_service_name_mod[j] = dst_service_name[i];
828                         }
829                         j++;
830                 }
831
832                 strncat(dst_service_name, ".ALARM", strlen(".ALARM"));
833                 strncat(dst_service_name_mod, ".ALARM", strlen(".ALARM"));
834
835                 if (!_send_alarm_create
836                     (alarm_context, alarm_info, alarm_id, dst_service_name, dst_service_name_mod,
837                      &error_code)) {
838                         return error_code;
839                 }
840         } else
841             if (!_send_alarm_create
842                 (alarm_context, alarm_info, alarm_id, "null", "null", &error_code)) {
843                 return error_code;
844         }
845
846         return ALARMMGR_RESULT_SUCCESS;
847 }
848
849
850
851 EXPORT_API int alarmmgr_add_alarm_appsvc(int alarm_type, time_t trigger_at_time,
852                                   time_t interval, void *bundle_data,
853                                   alarm_id_t *alarm_id)
854 {
855         int error_code = 0;;
856         time_t current_time;
857         struct tm duetime_tm;
858         alarm_info_t alarm_info;
859         const char *operation = NULL;
860         char *appid = NULL;
861
862         bundle *b=(bundle *)bundle_data;
863
864         if (NULL == b)
865         {
866                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle\n");
867                 return ERR_ALARM_INVALID_PARAM;
868         }
869         operation = appsvc_get_operation(b);
870         
871         if (NULL == operation)
872         {
873                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle [appsvc operation not present]\n");
874                 return ERR_ALARM_INVALID_PARAM;
875         }
876
877         appid = appsvc_get_appid(b);
878
879         if (NULL == appid && (alarm_type & ALARM_TYPE_NOLAUNCH) )
880         {
881                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter\n");
882                 return ERR_ALARM_INVALID_PARAM;
883         }
884
885         if (__alarmmgr_init_appsvc() < 0)
886         {
887                 ALARM_MGR_EXCEPTION_PRINT("Unable to initialize dbus!!!\n");
888                 return ERR_ALARM_SYSTEM_FAIL;
889         }
890
891         ALARM_MGR_LOG_PRINT("interval(%d)", interval);
892
893         if (alarm_id == NULL) {
894                 return ERR_ALARM_INVALID_PARAM;
895         }
896
897         if (trigger_at_time < 0) {
898                 return ERR_ALARM_INVALID_PARAM;
899         }
900
901         alarm_info.alarm_type = alarm_type;
902         alarm_info.alarm_type |= ALARM_TYPE_RELATIVE;
903
904         time(&current_time);
905
906         current_time += trigger_at_time;
907
908         localtime_r(&current_time, &duetime_tm);
909
910         alarm_info.start.year = duetime_tm.tm_year + 1900;
911         alarm_info.start.month = duetime_tm.tm_mon + 1;
912         alarm_info.start.day = duetime_tm.tm_mday;
913
914         alarm_info.end.year = 0;
915         alarm_info.end.month = 0;
916         alarm_info.end.day = 0;
917
918         alarm_info.start.hour = duetime_tm.tm_hour;
919         alarm_info.start.min = duetime_tm.tm_min;
920         alarm_info.start.sec = duetime_tm.tm_sec;
921
922         if (interval <= 0) {
923                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_ONCE;
924                 alarm_info.mode.u_interval.interval = 0;
925         } else {
926                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_REPEAT;
927                 alarm_info.mode.u_interval.interval = interval;
928         }
929
930         if (!_send_alarm_create_appsvc
931             (alarm_context, &alarm_info, alarm_id, b,
932              &error_code)) {
933                 return error_code;
934         }       
935
936         return ALARMMGR_RESULT_SUCCESS;
937 }
938
939
940 EXPORT_API int alarmmgr_add_alarm(int alarm_type, time_t trigger_at_time,
941                                   time_t interval, const char *destination,
942                                   alarm_id_t *alarm_id)
943 {
944         char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
945         char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = { 0 };
946         int i = 0;
947         int j = 0;
948         int error_code;
949         time_t current_time;
950         struct tm duetime_tm;
951         alarm_info_t alarm_info;
952         int ret;
953
954         ret = __sub_init();
955         if (ret < 0)
956                 return ret;
957
958         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
959
960         ALARM_MGR_LOG_PRINT("interval(%d)", interval);
961
962         if (alarm_id == NULL) {
963                 return ERR_ALARM_INVALID_PARAM;
964         }
965
966         if (trigger_at_time < 0) {
967                 return ERR_ALARM_INVALID_PARAM;
968         }
969
970         if (destination && strlen(destination) >= MAX_PKG_NAME_LEN){
971                 ALARM_MGR_EXCEPTION_PRINT("[alarm-lib]: destination name is too long!\n");
972                 return ERR_ALARM_INVALID_PARAM;
973         }
974
975         alarm_info.alarm_type = alarm_type;
976         alarm_info.alarm_type |= ALARM_TYPE_RELATIVE;
977
978         time(&current_time);
979
980         current_time += trigger_at_time;
981
982         localtime_r(&current_time, &duetime_tm);
983
984         alarm_info.start.year = duetime_tm.tm_year + 1900;
985         alarm_info.start.month = duetime_tm.tm_mon + 1;
986         alarm_info.start.day = duetime_tm.tm_mday;
987
988         alarm_info.end.year = 0;
989         alarm_info.end.month = 0;
990         alarm_info.end.day = 0;
991
992         alarm_info.start.hour = duetime_tm.tm_hour;
993         alarm_info.start.min = duetime_tm.tm_min;
994         alarm_info.start.sec = duetime_tm.tm_sec;
995
996         if (interval <= 0) {
997                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_ONCE;
998                 alarm_info.mode.u_interval.interval = 0;
999         } else {
1000                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_REPEAT;
1001                 alarm_info.mode.u_interval.interval = interval;
1002         }
1003
1004         if (destination != NULL) {
1005                 memset(dst_service_name, 0,
1006                        strlen(destination) + strlen(".ALARM") + 2);
1007                 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "%s",
1008                          destination);
1009                 memset(dst_service_name_mod,'a',MAX_SERVICE_NAME_LEN-1);
1010
1011                 j=0;
1012
1013                 for(i=0;i<=strlen(dst_service_name);i++)
1014                 {
1015                         if (dst_service_name[i] == '.')
1016                         {
1017                                 dst_service_name_mod[j]=dst_service_name[i];
1018                                 j++;
1019                         }
1020                         else
1021                         {
1022                                 dst_service_name_mod[j]=dst_service_name[i];
1023                         }
1024                         j++;
1025                 }
1026
1027                 strncat(dst_service_name, ".ALARM", strlen(".ALARM"));
1028                 strncat(dst_service_name_mod, ".ALARM", strlen(".ALARM"));
1029
1030                 if (!_send_alarm_create
1031                     (alarm_context, &alarm_info, alarm_id, dst_service_name,dst_service_name_mod,
1032                      &error_code)) {
1033                         return error_code;
1034                 }
1035         } else
1036             if (!_send_alarm_create
1037                 (alarm_context, &alarm_info, alarm_id, "null","null", &error_code)) {
1038                 return error_code;
1039         }
1040
1041         return ALARMMGR_RESULT_SUCCESS;
1042 }
1043
1044 EXPORT_API int alarmmgr_add_alarm_withcb(int alarm_type, time_t trigger_at_time,
1045                                   time_t interval, alarm_cb_t handler, void *user_param, alarm_id_t *alarm_id)
1046 {
1047         char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
1048         char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = { 0 };
1049         int i = 0;
1050         int j = 0;
1051         int error_code;
1052         time_t current_time;
1053         struct tm duetime_tm;
1054         alarm_info_t alarm_info;
1055         int ret;
1056         char appid[256];
1057
1058         aul_app_get_appid_bypid(getpid(), appid, sizeof(appid));
1059
1060         ret = alarmmgr_init(appid);
1061         if (ret < 0)
1062                 return ret;
1063
1064         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarmmgr_add_alarm_withcb() is called\n");
1065
1066         ALARM_MGR_LOG_PRINT("interval(%d)", interval);
1067
1068         if (alarm_id == NULL) {
1069                 return ERR_ALARM_INVALID_PARAM;
1070         }
1071
1072         if (trigger_at_time < 0) {
1073                 return ERR_ALARM_INVALID_PARAM;
1074         }
1075
1076         alarm_info.alarm_type = alarm_type;
1077         alarm_info.alarm_type |= ALARM_TYPE_RELATIVE;
1078         alarm_info.alarm_type |= ALARM_TYPE_WITHCB;
1079
1080         time(&current_time);
1081
1082         current_time += trigger_at_time;
1083
1084         localtime_r(&current_time, &duetime_tm);
1085
1086         alarm_info.start.year = duetime_tm.tm_year + 1900;
1087         alarm_info.start.month = duetime_tm.tm_mon + 1;
1088         alarm_info.start.day = duetime_tm.tm_mday;
1089
1090         alarm_info.end.year = 0;
1091         alarm_info.end.month = 0;
1092         alarm_info.end.day = 0;
1093
1094         alarm_info.start.hour = duetime_tm.tm_hour;
1095         alarm_info.start.min = duetime_tm.tm_min;
1096         alarm_info.start.sec = duetime_tm.tm_sec;
1097
1098         if (interval <= 0) {
1099                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_ONCE;
1100                 alarm_info.mode.u_interval.interval = 0;
1101         } else {
1102                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_REPEAT;
1103                 alarm_info.mode.u_interval.interval = interval;
1104         }
1105
1106         if (!_send_alarm_create(alarm_context, &alarm_info, alarm_id, "null","null", &error_code)) {
1107                 return error_code;
1108         }
1109         __add_resultcb(*alarm_id, handler, user_param);
1110
1111         return ALARMMGR_RESULT_SUCCESS;
1112 }
1113
1114 EXPORT_API int alarmmgr_remove_alarm(alarm_id_t alarm_id)
1115 {
1116         int error_code;
1117         int ret;
1118         alarm_cb_info_t *info;
1119
1120         ret = __sub_init();
1121         if (ret < 0)
1122                 return ret;
1123
1124         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_delete() is called\n");
1125
1126         if (alarm_id <= 0) {
1127                 return ERR_ALARM_INVALID_ID;
1128         }
1129
1130         if (!_send_alarm_delete(alarm_context, alarm_id, &error_code))
1131                 return error_code;
1132
1133         info = __find_resultcb(alarm_id);
1134         __remove_resultcb(info);
1135
1136         return ALARMMGR_RESULT_SUCCESS;
1137 }
1138
1139 EXPORT_API int alarmmgr_enum_alarm_ids(alarm_enum_fn_t fn, void *user_param)
1140 {
1141         GError *error = NULL;
1142         GArray *alarm_array = NULL;
1143         int return_code = 0;
1144         int i = 0;
1145         int maxnum_of_ids;
1146         int num_of_ids;
1147         int alarm_id = -1;
1148         int ret;
1149
1150         if (fn == NULL)
1151                 return ERR_ALARM_INVALID_PARAM;
1152
1153         ret = __sub_init();
1154         if (ret < 0)
1155                 return ret;
1156
1157         if (!org_tizen_alarm_manager_alarm_get_number_of_ids(
1158             alarm_context.proxy, alarm_context.pid, &maxnum_of_ids,
1159                &return_code, &error)) {
1160                 /* dbus-glib error */
1161                 /* error_code should be set */
1162                 ALARM_MGR_EXCEPTION_PRINT(
1163                     "org_tizen_alarm_manager_alarm_get_number_of_ids() "
1164                     "failed. return_code[%d], return_code[%s]\n",
1165                 return_code, error->message);
1166         }
1167
1168         if (return_code != 0) {
1169                 return return_code;
1170         }
1171
1172         if (!org_tizen_alarm_manager_alarm_get_list_of_ids(
1173                      alarm_context.proxy, alarm_context.pid, maxnum_of_ids,
1174              &alarm_array, &num_of_ids, &return_code, &error)) {
1175                 /*dbus-glib error */
1176                 /* error_code should be set */
1177                 ALARM_MGR_EXCEPTION_PRINT(
1178                     "org_tizen_alarm_manager_alarm_get_list_of_ids() "
1179                     "failed. alarm_id[%d], return_code[%d]\n",
1180                      alarm_id, return_code);
1181         }
1182
1183         if (return_code != 0) {
1184                 return return_code;
1185         } else {
1186                 if (error != NULL) {
1187                         ALARM_MGR_LOG_PRINT(
1188                                 "Alarm server not ready dbus error message %s\n", error->message);
1189                         return ERR_ALARM_SYSTEM_FAIL;
1190                 }
1191                 if (NULL == alarm_array) {
1192                         ALARM_MGR_LOG_PRINT(
1193                                 "alarm server not initilized\n");
1194                         return ERR_ALARM_SYSTEM_FAIL;
1195                 }
1196                 for (i = 0; i < alarm_array->len && i < maxnum_of_ids; i++) {
1197                         alarm_id = g_array_index(alarm_array, alarm_id_t, i);
1198                         (*fn) (alarm_id, user_param);
1199                         ALARM_MGR_LOG_PRINT(" alarm_id(%d)\n", alarm_id);
1200                 }
1201
1202                 g_array_free(alarm_array, true);
1203         }
1204
1205         return ALARMMGR_RESULT_SUCCESS;
1206 }
1207
1208 EXPORT_API int alarmmgr_get_info(alarm_id_t alarm_id, alarm_entry_t *alarm)
1209 {
1210         int error_code;
1211         alarm_info_t *alarm_info = (alarm_info_t *) alarm;
1212
1213         int ret;
1214
1215         ret = __sub_init();
1216         if (ret < 0)
1217                 return ret;
1218
1219         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_get_info() is called\n");
1220
1221         if (alarm_id < 0 || alarm_info == NULL) {
1222                 return ERR_ALARM_INVALID_PARAM;
1223         }
1224
1225         if (!_send_alarm_get_info(alarm_context, alarm_id, alarm_info,
1226                                   &error_code))
1227                 return error_code;
1228
1229         return ALARMMGR_RESULT_SUCCESS;
1230 }
1231
1232 EXPORT_API int alarmmgr_power_on(bool on_off)
1233 {
1234         int error_code;
1235         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_power_on() is called\n");
1236
1237 #ifdef __ALARM_BOOT
1238         if (!_send_alarm_power_on(alarm_context, on_off, &error_code))
1239                 return error_code;
1240 #else
1241         ALARM_MGR_LOG_PRINT("[alarm-lib]:ALARM_BOOT feature is not supported. "
1242                             "so we return false.\n");
1243         return ERR_ALARM_SYSTEM_FAIL;
1244 #endif
1245
1246         return ALARMMGR_RESULT_SUCCESS;
1247 }
1248
1249 int alarmmgr_create(alarm_info_t *alarm_info, char *destination,
1250                     alarm_id_t *alarm_id)
1251 {
1252         char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
1253         alarm_mode_t *mode = &alarm_info->mode;
1254         int error_code;
1255
1256         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
1257
1258         if (alarm_info == NULL || alarm_id == NULL) {
1259                 return ERR_ALARM_INVALID_PARAM;
1260         }
1261
1262         ALARM_MGR_LOG_PRINT("alarm_info->start.year(%d), "
1263                             "alarm_info->start.month(%d), alarm_info->start.day(%d)",
1264                             alarm_info->start.year, alarm_info->start.month,
1265                             alarm_info->start.day);
1266
1267         /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
1268         if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
1269                 return ERR_ALARM_INVALID_PARAM;
1270         }
1271
1272         if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
1273                 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
1274                 return error_code;
1275         }
1276
1277         if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
1278                 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
1279                 return error_code;
1280         }
1281
1282         if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
1283                 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
1284                 return error_code;
1285         }
1286
1287         if (destination != NULL) {
1288                 memset(dst_service_name, 0,
1289                        strlen(destination) + strlen(".ALARM") + 2);
1290                 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "%s.ALARM",
1291                          destination);
1292                 if (!_send_alarm_create
1293                     (alarm_context, alarm_info, alarm_id, dst_service_name,"null",
1294                      &error_code)) {
1295                         return error_code;
1296                 }
1297         }
1298 /*TODO: Currently this API is not exported. Hence not modifying*/
1299         if (!_send_alarm_create
1300             (alarm_context, alarm_info, alarm_id, "null", "null", &error_code)) {
1301                 return error_code;
1302         }
1303
1304         return ALARMMGR_RESULT_SUCCESS;
1305
1306 }
1307
1308 int alarmmgr_get_number_of_ids(int *num_of_ids)
1309 {
1310         int error_code;
1311         ALARM_MGR_LOG_PRINT("[alarm-lib]:"
1312                             "alarm_get_number_of_ids() is called\n");
1313
1314         if (num_of_ids == NULL) {
1315                 return ERR_ALARM_INVALID_PARAM;
1316         }
1317         ALARM_MGR_LOG_PRINT("call alarm_get_number_of_ids\n");
1318         if (!_send_alarm_get_number_of_ids(alarm_context, num_of_ids,
1319                                            &error_code))
1320                 return error_code;
1321
1322         return ALARMMGR_RESULT_SUCCESS;
1323 }
1324
1325 int alarmmgr_get_list_of_ids(int maxnum_of_ids, alarm_id_t *alarm_id,
1326                              int *num_of_ids)
1327 {
1328         int error_code;
1329         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_get_list_of_ids() is called\n");
1330
1331         if (maxnum_of_ids < 0 || alarm_id == NULL || num_of_ids == NULL) {
1332                 return ERR_ALARM_INVALID_PARAM;
1333         }
1334
1335         if (maxnum_of_ids == 0) {
1336                 *num_of_ids = 0;
1337                 return ALARMMGR_RESULT_SUCCESS;
1338         }
1339
1340         if (!_send_alarm_get_list_of_ids
1341             (alarm_context, maxnum_of_ids, alarm_id, num_of_ids, &error_code))
1342                 return error_code;
1343
1344         return ALARMMGR_RESULT_SUCCESS;
1345 }