add systemd unit files
[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
555 EXPORT_API int alarmmgr_add_alarm_appsvc_with_localtime(alarm_entry_t *alarm, void *bundle_data, alarm_id_t *alarm_id)
556 {
557         alarm_info_t *alarm_info = NULL;        /* = (alarm_info_t*)alarm; */
558         const char *operation = NULL;
559         int error_code = 0;
560
561         bundle *b=(bundle *)bundle_data;
562
563         if (alarm == NULL) {
564                 return ERR_ALARM_INVALID_PARAM;
565         }
566
567         if (NULL == b)
568         {
569                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle\n");
570                 return ERR_ALARM_INVALID_PARAM;
571         }
572         operation = appsvc_get_operation(b);
573         
574         if (NULL == operation)
575         {
576                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle [appsvc operation not present]\n");
577                 return ERR_ALARM_INVALID_PARAM;
578         }
579
580         if (__alarmmgr_init_appsvc() < 0)
581         {
582                 ALARM_MGR_EXCEPTION_PRINT("Unable to initialize dbus!!!\n");
583                 return ERR_ALARM_SYSTEM_FAIL;
584         }
585
586         alarm_info = (alarm_info_t *) alarm;
587
588         if (alarm_info == NULL || alarm_id == NULL) {
589                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter\n");
590                 return ERR_ALARM_INVALID_PARAM;
591         }
592         alarm_mode_t *mode = &alarm_info->mode;
593
594         ALARM_MGR_LOG_PRINT("alarm_info->start.year(%d), "
595                             "alarm_info->start.month(%d), alarm_info->start.day(%d)",
596                             alarm_info->start.year, alarm_info->start.month,
597                             alarm_info->start.day);
598
599         /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
600         if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
601                 return ERR_ALARM_INVALID_PARAM;
602         }
603
604         if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
605                 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
606                 return error_code;
607         }
608
609         if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
610                 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
611                 return error_code;
612         }
613
614         if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
615                 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
616                 return error_code;
617         }
618
619
620         if (!_send_alarm_create_appsvc
621             (alarm_context, alarm_info, alarm_id, b,
622              &error_code)) {
623                 return error_code;
624         }       
625
626         return ALARMMGR_RESULT_SUCCESS;
627 }
628
629
630
631
632 EXPORT_API int alarmmgr_add_alarm_with_localtime(alarm_entry_t *alarm,
633                                                  const char *destination,
634                                                  alarm_id_t *alarm_id)
635 {
636         char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
637         char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = { 0 };
638         alarm_info_t *alarm_info;       /* = (alarm_info_t*)alarm; */
639         int ret;
640         int i = 0;
641         int j = 0;
642
643         if (alarm == NULL) {
644                 return ERR_ALARM_INVALID_PARAM;
645         }
646
647         alarm_info = (alarm_info_t *) alarm;
648
649         int error_code;
650         alarm_mode_t *mode = &alarm_info->mode;
651
652         ret = __sub_init();
653         if (ret < 0)
654                 return ret;
655
656         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
657
658         ALARM_MGR_LOG_PRINT("alarm_info->start.year(%d), "
659                             "alarm_info->start.month(%d), alarm_info->start.day(%d)",
660                             alarm_info->start.year, alarm_info->start.month,
661                             alarm_info->start.day);
662
663         if (alarm_info == NULL || alarm_id == NULL) {
664                 return ERR_ALARM_INVALID_PARAM;
665         }
666         /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
667         if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
668                 return ERR_ALARM_INVALID_PARAM;
669         }
670
671         if (destination && strlen(destination) >= MAX_PKG_NAME_LEN){
672                 ALARM_MGR_EXCEPTION_PRINT("[alarm-lib]: destination name is too long!\n");
673                 return ERR_ALARM_INVALID_PARAM;
674         }
675
676
677         if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
678                 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
679                 return error_code;
680         }
681
682         if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
683                 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
684                 return error_code;
685         }
686
687         if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
688                 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
689                 return error_code;
690         }
691
692         if (destination != NULL) {
693                 memset(dst_service_name, 0,
694                        strlen(destination) + strlen(".ALARM") + 2);
695                 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "%s",
696                          destination);
697
698                 memset(dst_service_name_mod,'a',MAX_SERVICE_NAME_LEN-1);
699
700                 j=0;
701
702                 for(i=0; i<=strlen(dst_service_name); i++)
703                 {
704                         if (dst_service_name[i] == '.' )
705                         {
706                                 dst_service_name_mod[j] = dst_service_name[i];
707                                 j++;
708                         }
709                         else
710                         {
711                                 dst_service_name_mod[j] = dst_service_name[i];
712                         }
713                         j++;
714                 }
715
716                 strncat(dst_service_name, ".ALARM", strlen(".ALARM"));
717                 strncat(dst_service_name_mod, ".ALARM", strlen(".ALARM"));
718
719                 if (!_send_alarm_create
720                     (alarm_context, alarm_info, alarm_id, dst_service_name, dst_service_name_mod,
721                      &error_code)) {
722                         return error_code;
723                 }
724         } else
725             if (!_send_alarm_create
726                 (alarm_context, alarm_info, alarm_id, "null", "null", &error_code)) {
727                 return error_code;
728         }
729
730         return ALARMMGR_RESULT_SUCCESS;
731 }
732
733
734
735 EXPORT_API int alarmmgr_add_alarm_appsvc(int alarm_type, time_t trigger_at_time,
736                                   time_t interval, void *bundle_data,
737                                   alarm_id_t *alarm_id)
738 {
739         int error_code = 0;;
740         time_t current_time;
741         struct tm duetime_tm;
742         alarm_info_t alarm_info;
743         const char *operation = NULL;
744
745         bundle *b=(bundle *)bundle_data;
746
747         if (NULL == b)
748         {
749                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle\n");
750                 return ERR_ALARM_INVALID_PARAM;
751         }
752         operation = appsvc_get_operation(b);
753         
754         if (NULL == operation)
755         {
756                 ALARM_MGR_EXCEPTION_PRINT("Invalid parameter bundle [appsvc operation not present]\n");
757                 return ERR_ALARM_INVALID_PARAM;
758         }
759
760         if (__alarmmgr_init_appsvc() < 0)
761         {
762                 ALARM_MGR_EXCEPTION_PRINT("Unable to initialize dbus!!!\n");
763                 return ERR_ALARM_SYSTEM_FAIL;
764         }
765
766         ALARM_MGR_LOG_PRINT("interval(%d)", interval);
767
768         if (alarm_id == NULL) {
769                 return ERR_ALARM_INVALID_PARAM;
770         }
771
772         if (trigger_at_time < 0) {
773                 return ERR_ALARM_INVALID_PARAM;
774         }
775
776         alarm_info.alarm_type = alarm_type;
777         alarm_info.alarm_type |= ALARM_TYPE_RELATIVE;
778
779         time(&current_time);
780
781         current_time += trigger_at_time;
782
783         localtime_r(&current_time, &duetime_tm);
784
785         alarm_info.start.year = duetime_tm.tm_year + 1900;
786         alarm_info.start.month = duetime_tm.tm_mon + 1;
787         alarm_info.start.day = duetime_tm.tm_mday;
788
789         alarm_info.end.year = 0;
790         alarm_info.end.month = 0;
791         alarm_info.end.day = 0;
792
793         alarm_info.start.hour = duetime_tm.tm_hour;
794         alarm_info.start.min = duetime_tm.tm_min;
795         alarm_info.start.sec = duetime_tm.tm_sec;
796
797         if (interval <= 0) {
798                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_ONCE;
799                 alarm_info.mode.u_interval.interval = 0;
800         } else {
801                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_REPEAT;
802                 alarm_info.mode.u_interval.interval = interval;
803         }
804
805         if (!_send_alarm_create_appsvc
806             (alarm_context, &alarm_info, alarm_id, b,
807              &error_code)) {
808                 return error_code;
809         }       
810
811         return ALARMMGR_RESULT_SUCCESS;
812 }
813
814
815 EXPORT_API int alarmmgr_add_alarm(int alarm_type, time_t trigger_at_time,
816                                   time_t interval, const char *destination,
817                                   alarm_id_t *alarm_id)
818 {
819         char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
820         char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = { 0 };
821         int i = 0;
822         int j = 0;
823         int error_code;
824         time_t current_time;
825         struct tm duetime_tm;
826         alarm_info_t alarm_info;
827         int ret;
828
829         ret = __sub_init();
830         if (ret < 0)
831                 return ret;
832
833         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
834
835         ALARM_MGR_LOG_PRINT("interval(%d)", interval);
836
837         if (alarm_id == NULL) {
838                 return ERR_ALARM_INVALID_PARAM;
839         }
840
841         if (trigger_at_time < 0) {
842                 return ERR_ALARM_INVALID_PARAM;
843         }
844
845         if (destination && strlen(destination) >= MAX_PKG_NAME_LEN){
846                 ALARM_MGR_EXCEPTION_PRINT("[alarm-lib]: destination name is too long!\n");
847                 return ERR_ALARM_INVALID_PARAM;
848         }
849
850         alarm_info.alarm_type = alarm_type;
851         alarm_info.alarm_type |= ALARM_TYPE_RELATIVE;
852
853         time(&current_time);
854
855         current_time += trigger_at_time;
856
857         localtime_r(&current_time, &duetime_tm);
858
859         alarm_info.start.year = duetime_tm.tm_year + 1900;
860         alarm_info.start.month = duetime_tm.tm_mon + 1;
861         alarm_info.start.day = duetime_tm.tm_mday;
862
863         alarm_info.end.year = 0;
864         alarm_info.end.month = 0;
865         alarm_info.end.day = 0;
866
867         alarm_info.start.hour = duetime_tm.tm_hour;
868         alarm_info.start.min = duetime_tm.tm_min;
869         alarm_info.start.sec = duetime_tm.tm_sec;
870
871         if (interval <= 0) {
872                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_ONCE;
873                 alarm_info.mode.u_interval.interval = 0;
874         } else {
875                 alarm_info.mode.repeat = ALARM_REPEAT_MODE_REPEAT;
876                 alarm_info.mode.u_interval.interval = interval;
877         }
878
879         if (destination != NULL) {
880                 memset(dst_service_name, 0,
881                        strlen(destination) + strlen(".ALARM") + 2);
882                 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "%s",
883                          destination);
884                 memset(dst_service_name_mod,'a',MAX_SERVICE_NAME_LEN-1);
885
886                 j=0;
887
888                 for(i=0;i<=strlen(dst_service_name);i++)
889                 {
890                         if (dst_service_name[i] == '.')
891                         {
892                                 dst_service_name_mod[j]=dst_service_name[i];
893                                 j++;
894                         }
895                         else
896                         {
897                                 dst_service_name_mod[j]=dst_service_name[i];
898                         }
899                         j++;
900                 }
901
902                 strncat(dst_service_name, ".ALARM", strlen(".ALARM"));
903                 strncat(dst_service_name_mod, ".ALARM", strlen(".ALARM"));
904
905                 if (!_send_alarm_create
906                     (alarm_context, &alarm_info, alarm_id, dst_service_name,dst_service_name_mod,
907                      &error_code)) {
908                         return error_code;
909                 }
910         } else
911             if (!_send_alarm_create
912                 (alarm_context, &alarm_info, alarm_id, "null","null", &error_code)) {
913                 return error_code;
914         }
915
916         return ALARMMGR_RESULT_SUCCESS;
917 }
918
919 EXPORT_API int alarmmgr_remove_alarm(alarm_id_t alarm_id)
920 {
921         int error_code;
922         int ret;
923
924         ret = __sub_init();
925         if (ret < 0)
926                 return ret;
927
928         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_delete() is called\n");
929
930         if (alarm_id <= 0) {
931                 return ERR_ALARM_INVALID_ID;
932         }
933
934         if (!_send_alarm_delete(alarm_context, alarm_id, &error_code))
935                 return error_code;
936
937         return ALARMMGR_RESULT_SUCCESS;
938 }
939
940 EXPORT_API int alarmmgr_enum_alarm_ids(alarm_enum_fn_t fn, void *user_param)
941 {
942         GError *error = NULL;
943         GArray *alarm_array = NULL;
944         int return_code = 0;
945         int i = 0;
946         int maxnum_of_ids;
947         int num_of_ids;
948         int alarm_id = -1;
949         int ret;
950
951         if (fn == NULL)
952                 return ERR_ALARM_INVALID_PARAM;
953
954         ret = __sub_init();
955         if (ret < 0)
956                 return ret;
957
958         if (!org_tizen_alarm_manager_alarm_get_number_of_ids(
959             alarm_context.proxy, alarm_context.pid, &maxnum_of_ids,
960                &return_code, &error)) {
961                 /* dbus-glib error */
962                 /* error_code should be set */
963                 ALARM_MGR_EXCEPTION_PRINT(
964                     "org_tizen_alarm_manager_alarm_get_number_of_ids() "
965                     "failed. return_code[%d], return_code[%s]\n",
966                 return_code, error->message);
967         }
968
969         if (return_code != 0) {
970                 return return_code;
971         }
972
973         if (!org_tizen_alarm_manager_alarm_get_list_of_ids(
974                      alarm_context.proxy, alarm_context.pid, maxnum_of_ids,
975              &alarm_array, &num_of_ids, &return_code, &error)) {
976                 /*dbus-glib error */
977                 /* error_code should be set */
978                 ALARM_MGR_EXCEPTION_PRINT(
979                     "org_tizen_alarm_manager_alarm_get_list_of_ids() "
980                     "failed. alarm_id[%d], return_code[%d]\n",
981                      alarm_id, return_code);
982         }
983
984         if (return_code != 0) {
985                 return return_code;
986         } else {
987                 for (i = 0; i < alarm_array->len && i < maxnum_of_ids; i++) {
988                         alarm_id = g_array_index(alarm_array, alarm_id_t, i);
989                         (*fn) (alarm_id, user_param);
990                         ALARM_MGR_LOG_PRINT(" alarm_id(%d)\n", alarm_id);
991                 }
992
993                 g_array_free(alarm_array, true);
994         }
995
996         return ALARMMGR_RESULT_SUCCESS;
997 }
998
999 EXPORT_API int alarmmgr_get_info(alarm_id_t alarm_id, alarm_entry_t *alarm)
1000 {
1001         int error_code;
1002         alarm_info_t *alarm_info = (alarm_info_t *) alarm;
1003
1004         int ret;
1005
1006         ret = __sub_init();
1007         if (ret < 0)
1008                 return ret;
1009
1010         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_get_info() is called\n");
1011
1012         if (alarm_id < 0 || alarm_info == NULL) {
1013                 return ERR_ALARM_INVALID_PARAM;
1014         }
1015
1016         if (!_send_alarm_get_info(alarm_context, alarm_id, alarm_info,
1017                                   &error_code))
1018                 return error_code;
1019
1020         return ALARMMGR_RESULT_SUCCESS;
1021 }
1022
1023 EXPORT_API int alarmmgr_power_on(bool on_off)
1024 {
1025         int error_code;
1026         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_power_on() is called\n");
1027
1028 #ifdef __ALARM_BOOT
1029         if (!_send_alarm_power_on(alarm_context, on_off, &error_code))
1030                 return error_code;
1031 #else
1032         ALARM_MGR_LOG_PRINT("[alarm-lib]:ALARM_BOOT feature is not supported. "
1033                             "so we return false.\n");
1034         return ERR_ALARM_SYSTEM_FAIL;
1035 #endif
1036
1037         return ALARMMGR_RESULT_SUCCESS;
1038 }
1039
1040 int alarmmgr_create(alarm_info_t *alarm_info, char *destination,
1041                     alarm_id_t *alarm_id)
1042 {
1043         char dst_service_name[MAX_SERVICE_NAME_LEN] = { 0 };
1044         alarm_mode_t *mode = &alarm_info->mode;
1045         int error_code;
1046
1047         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_create() is called\n");
1048
1049         if (alarm_info == NULL || alarm_id == NULL) {
1050                 return ERR_ALARM_INVALID_PARAM;
1051         }
1052
1053         ALARM_MGR_LOG_PRINT("alarm_info->start.year(%d), "
1054                             "alarm_info->start.month(%d), alarm_info->start.day(%d)",
1055                             alarm_info->start.year, alarm_info->start.month,
1056                             alarm_info->start.day);
1057
1058         /* TODO: This should be changed to > ALARM_REPEAT_MODE_MAX ? */
1059         if (mode->repeat >= ALARM_REPEAT_MODE_MAX) {
1060                 return ERR_ALARM_INVALID_PARAM;
1061         }
1062
1063         if (!__alarm_validate_date(&alarm_info->start, &error_code)) {
1064                 ALARM_MGR_EXCEPTION_PRINT("start date error\n");
1065                 return error_code;
1066         }
1067
1068         if (!__alarm_validate_time(&alarm_info->start, &error_code)) {
1069                 ALARM_MGR_EXCEPTION_PRINT("start time error\n");
1070                 return error_code;
1071         }
1072
1073         if (!__alarm_validate_date(&alarm_info->end, &error_code)) {
1074                 ALARM_MGR_EXCEPTION_PRINT("end date error\n");
1075                 return error_code;
1076         }
1077
1078         if (destination != NULL) {
1079                 memset(dst_service_name, 0,
1080                        strlen(destination) + strlen(".ALARM") + 2);
1081                 snprintf(dst_service_name, MAX_SERVICE_NAME_LEN, "%s.ALARM",
1082                          destination);
1083                 if (!_send_alarm_create
1084                     (alarm_context, alarm_info, alarm_id, dst_service_name,"null",
1085                      &error_code)) {
1086                         return error_code;
1087                 }
1088         }
1089 /*TODO: Currently this API is not exported. Hence not modifying*/
1090         if (!_send_alarm_create
1091             (alarm_context, alarm_info, alarm_id, "null", "null", &error_code)) {
1092                 return error_code;
1093         }
1094
1095         return ALARMMGR_RESULT_SUCCESS;
1096
1097 }
1098
1099 int alarmmgr_get_number_of_ids(int *num_of_ids)
1100 {
1101         int error_code;
1102         ALARM_MGR_LOG_PRINT("[alarm-lib]:"
1103                             "alarm_get_number_of_ids() is called\n");
1104
1105         if (num_of_ids == NULL) {
1106                 return ERR_ALARM_INVALID_PARAM;
1107         }
1108         ALARM_MGR_LOG_PRINT("call alarm_get_number_of_ids\n");
1109         if (!_send_alarm_get_number_of_ids(alarm_context, num_of_ids,
1110                                            &error_code))
1111                 return error_code;
1112
1113         return ALARMMGR_RESULT_SUCCESS;
1114 }
1115
1116 int alarmmgr_get_list_of_ids(int maxnum_of_ids, alarm_id_t *alarm_id,
1117                              int *num_of_ids)
1118 {
1119         int error_code;
1120         ALARM_MGR_LOG_PRINT("[alarm-lib]:alarm_get_list_of_ids() is called\n");
1121
1122         if (maxnum_of_ids < 0 || alarm_id == NULL || num_of_ids == NULL) {
1123                 return ERR_ALARM_INVALID_PARAM;
1124         }
1125
1126         if (maxnum_of_ids == 0) {
1127                 *num_of_ids = 0;
1128                 return ALARMMGR_RESULT_SUCCESS;
1129         }
1130
1131         if (!_send_alarm_get_list_of_ids
1132             (alarm_context, maxnum_of_ids, alarm_id, num_of_ids, &error_code))
1133                 return error_code;
1134
1135         return ALARMMGR_RESULT_SUCCESS;
1136 }