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