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