702faf59be4dd68fa4085ba29acb2e8e914f6361
[platform/core/appfw/alarm-manager.git] / lib / alarm-lib-dbus.c
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <sys/types.h>
19 #include <string.h>
20 #include <glib.h>
21 #include <notification.h>
22 #include <notification_ipc.h>
23
24 #include "alarm.h"
25 #include "alarm-internal.h"
26
27 bool _send_alarm_create_noti(alarm_context_t context, alarm_info_t *alarm_info,
28                 alarm_id_t *alarm_id, notification_h noti, int *error_code)
29 {
30         GError *error = NULL;
31         int return_code = -1;
32         GVariant *noti_gv = NULL;
33         char *noti_data;
34         guchar *data;
35         int datalen = 0;
36         GVariant *param = NULL;
37         GVariant *reply = NULL;
38
39         noti_gv = notification_ipc_make_gvariant_from_noti(noti, true);
40         if (!noti_gv) {
41                 if (error_code)
42                         *error_code = ERR_ALARM_SYSTEM_FAIL;
43                 return false;
44         }
45
46         datalen = g_variant_get_size(noti_gv);
47         if (datalen < 0)
48                 return false;
49
50         data = malloc(datalen);
51         if (!data)
52                 return false;
53
54         g_variant_store(noti_gv, data);
55         noti_data = g_base64_encode((guchar *)data, datalen);
56
57         param = g_variant_new("(iiiiiiiiiixiixs)",
58                         alarm_info->start.year,
59                         alarm_info->start.month,
60                         alarm_info->start.day,
61                         alarm_info->start.hour,
62                         alarm_info->start.min,
63                         alarm_info->start.sec,
64                         alarm_info->end.year,
65                         alarm_info->end.month,
66                         alarm_info->end.day,
67                         alarm_info->mode.u_interval.day_of_week,
68                         (gint64)alarm_info->mode.u_interval.interval,
69                         alarm_info->mode.repeat,
70                         alarm_info->alarm_type,
71                         (gint64)alarm_info->reserved_info,
72                         (char *)noti_data);
73
74         if (noti_data)
75                 free(noti_data);
76         if (data)
77                 free(data);
78         g_variant_unref(noti_gv);
79
80         reply = g_dbus_proxy_call_sync(context.proxy, "alarm_create_noti", param,
81                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
82         if (error) {
83                 if (error_code) {
84                         if (error && error->code == G_DBUS_ERROR_ACCESS_DENIED)
85                                 *error_code = ERR_ALARM_NO_PERMISSION;
86                         else
87                                 *error_code = ERR_ALARM_SYSTEM_FAIL;
88                 }
89
90                 LOGE("g_dbus_proxy_call_sync() failed. alarm_id[%d],\
91                                 return_code[%d].error->message is %s(%d)", *(int *)alarm_id,
92                                 return_code, error->message, error->code);
93                 g_error_free(error);
94                 return false;
95         }
96
97         g_variant_get(reply, "(ii)", alarm_id, &return_code);
98
99         LOGD("alarm_create_noti() success. alarm_id[%d], return_code[%d].",
100                         *alarm_id, return_code);
101
102         if (return_code != 0) {
103                 if (error_code)
104                         *error_code = return_code;
105
106                 g_variant_unref(reply);
107                 return false;
108         }
109
110         g_variant_unref(reply);
111         return true;
112 }
113
114 bool _send_alarm_create_appsvc(alarm_context_t context, alarm_info_t *alarm_info,
115                 alarm_id_t *alarm_id, bundle *b, int *error_code)
116 {
117         GError *error = NULL;
118         int return_code = -1;
119         bundle_raw *b_data = NULL;
120         int datalen = 0;
121         GVariant *param = NULL;
122         GVariant *reply = NULL;
123
124         if (bundle_encode(b, &b_data, &datalen)) {
125                 LOGE("Unable to encode the bundle data\n");
126                 if (error_code)
127                         *error_code = ERR_ALARM_SYSTEM_FAIL;
128                 return false;
129         }
130
131         param = g_variant_new("(iiiiiiiiiixiixs)",
132                         alarm_info->start.year,
133                         alarm_info->start.month,
134                         alarm_info->start.day,
135                         alarm_info->start.hour,
136                         alarm_info->start.min,
137                         alarm_info->start.sec,
138                         alarm_info->end.year,
139                         alarm_info->end.month,
140                         alarm_info->end.day,
141                         alarm_info->mode.u_interval.day_of_week,
142                         (gint64)alarm_info->mode.u_interval.interval,
143                         alarm_info->mode.repeat,
144                         alarm_info->alarm_type,
145                         (gint64)alarm_info->reserved_info,
146                         (char *)b_data);
147
148         if (b_data)
149                 free(b_data);
150
151         reply = g_dbus_proxy_call_sync(context.proxy, "alarm_create_appsvc", param,
152                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
153         if (error) {
154                 if (error_code) {
155                         if (error && error->code == G_DBUS_ERROR_ACCESS_DENIED)
156                                 *error_code = ERR_ALARM_NO_PERMISSION;
157                         else
158                                 *error_code = ERR_ALARM_SYSTEM_FAIL;
159                 }
160
161                 LOGE("g_dbus_proxy_call_sync() failed.\
162                                 alarm_id[%d], return_code[%d]. error->message is %s(%d)",
163                                 *alarm_id, return_code, error->message, error->code);
164                 g_error_free(error);
165                 return false;
166         }
167
168         g_variant_get(reply, "(ii)", alarm_id, &return_code);
169
170         LOGD("alarm_create_appsvc() success. alarm_id[%d], return_code[%d].",
171                         *alarm_id, return_code);
172
173         if (return_code != 0) {
174                 if (error_code)
175                         *error_code = return_code;
176
177                 g_variant_unref(reply);
178                 return false;
179         }
180
181         g_variant_unref(reply);
182         return true;
183 }
184
185 bool _send_alarm_create(alarm_context_t context, alarm_info_t *alarm_info,
186                 alarm_id_t *alarm_id, const char *dst_service_name, const char *dst_service_name_mod,
187                 int *error_code)
188 {
189         GError *error = NULL;
190         int return_code = -1;
191         GVariant *param = NULL;
192         GVariant *reply = NULL;
193
194         /*TODO: Dbus bus name validation is must & will be added to avoid alarm-server crash*/
195         if (context.app_service_name == NULL
196                         && strlen(dst_service_name) == 4
197                         && strncmp(dst_service_name, "null", 4) == 0) {
198                 LOGE("Invalid arg. Provide valid destination or call alarmmgr_init()\n");
199                 if (error_code)
200                         *error_code = ERR_ALARM_INVALID_PARAM;
201                 return false;
202         }
203
204         param = g_variant_new("(ssiiiiiiiiiiiiixss)",
205                         context.app_service_name,
206                         context.app_service_name_mod,
207                         alarm_info->start.year,
208                         alarm_info->start.month,
209                         alarm_info->start.day,
210                         alarm_info->start.hour,
211                         alarm_info->start.min,
212                         alarm_info->start.sec,
213                         alarm_info->msec,
214                         alarm_info->end.year,
215                         alarm_info->end.month,
216                         alarm_info->end.day,
217                         alarm_info->mode.u_interval.day_of_week,
218                         alarm_info->mode.repeat,
219                         alarm_info->alarm_type,
220                         (gint64)alarm_info->reserved_info,
221                         dst_service_name, dst_service_name_mod);
222
223         reply = g_dbus_proxy_call_sync(context.proxy, "alarm_create", param,
224                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
225         if (error) {
226                 if (error_code) {
227                         if (error && error->code == G_DBUS_ERROR_ACCESS_DENIED)
228                                 *error_code = ERR_ALARM_NO_PERMISSION;
229                         else
230                                 *error_code = ERR_ALARM_SYSTEM_FAIL;
231                 }
232
233                 LOGE("g_dbus_proxy_call_sync() failed.\
234                                 alarm_id[%d], return_code[%d]. error->message is %s(%d)",
235                                 *alarm_id, return_code, error->message, error->code);
236                 g_error_free(error);
237
238                 return false;
239         }
240
241         g_variant_get(reply, "(ii)", alarm_id, &return_code);
242
243         LOGD("alarm_create() dbus sync success. alarm_id[%d], return_code[%d].",
244                         *alarm_id, return_code);
245
246         if (return_code != 0) {
247                 if (error_code)
248                         *error_code = return_code;
249
250                 g_variant_unref(reply);
251                 return false;
252         }
253
254         g_variant_unref(reply);
255         return true;
256 }
257
258 bool _send_alarm_create_periodic(alarm_context_t context, int interval, int is_ref,
259                 int method, alarm_id_t *alarm_id, int *error_code)
260 {
261         GError *error = NULL;
262         int return_code = -1;
263         GVariant *param = NULL;
264         GVariant *reply = NULL;
265
266         if (context.app_service_name == NULL) {
267                 LOGE("Invalid arg. Provide valid destination or call alarmmgr_init()\n");
268                 if (error_code)
269                         *error_code = ERR_ALARM_INVALID_PARAM;
270                 return false;
271         }
272
273         param = g_variant_new("(ssiii)",
274                         context.app_service_name,
275                         context.app_service_name_mod,
276                         interval, is_ref, method);
277
278         reply = g_dbus_proxy_call_sync(context.proxy, "alarm_create_periodic", param,
279                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
280         if (error) {
281                 if (error_code) {
282                         if (error && error->code == G_DBUS_ERROR_ACCESS_DENIED)
283                                 *error_code = ERR_ALARM_NO_PERMISSION;
284                         else
285                                 *error_code = ERR_ALARM_SYSTEM_FAIL;
286                 }
287
288                 LOGE("g_dbus_proxy_call_sync() failed.\
289                                 alarm_id[%d], return_code[%d]. error->message is %s(%d)",
290                                 *(int *)alarm_id, return_code, error->message, error->code);
291
292                 g_error_free(error);
293
294                 return false;
295         }
296
297         g_variant_get(reply, "(ii)", alarm_id, &return_code);
298
299         LOGD("alarm_create_periodic() dbus sync success. alarm_id[%d], return_code[%d].",
300                         *alarm_id, return_code);
301
302         if (return_code != 0) {
303                 if (error_code)
304                         *error_code = return_code;
305
306                 g_variant_unref(reply);
307                 return false;
308         }
309
310         g_variant_unref(reply);
311         return true;
312 }
313
314 bundle *_send_alarm_get_appsvc_info(alarm_context_t context, alarm_id_t alarm_id, int *error_code)
315 {
316         GError *error = NULL;
317         int return_code = -1;
318         bundle *b = NULL;
319         gchar *b_data = NULL;
320         GVariant *param = NULL;
321         GVariant *reply = NULL;
322
323         param = g_variant_new("(i)", alarm_id);
324
325         reply = g_dbus_proxy_call_sync(context.proxy, "alarm_get_appsvc_info", param,
326                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
327         if (error) {
328                 if (error_code) {
329                         if (error && error->code == G_DBUS_ERROR_ACCESS_DENIED)
330                                 *error_code = ERR_ALARM_NO_PERMISSION;
331                         else
332                                 *error_code = ERR_ALARM_SYSTEM_FAIL;
333                 }
334
335                 LOGE("g_dbus_proxy_call_sync() failed.\
336                                 alarm_id[%d], return_code[%d]. error->message is %s(%d)",
337                                 alarm_id, return_code, error->message, error->code);
338
339                 g_error_free(error);
340
341                 return NULL;
342         }
343
344         g_variant_get(reply, "(si)", &b_data, &return_code);
345
346         LOGD("alarm_get_appsvc_info() dbus sync success. return_code[%d].",
347                         return_code);
348
349         if (return_code != 0) {
350                 if (error_code)
351                         *error_code = return_code;
352         } else {
353                 b = bundle_decode((bundle_raw *)b_data, strlen(b_data));
354         }
355
356         if (b_data)
357                 g_free(b_data);
358
359         g_variant_unref(reply);
360         return b;
361 }
362
363 notification_h _send_alarm_get_noti_info(alarm_context_t context, alarm_id_t alarm_id, int *error_code)
364 {
365         GError *error = NULL;
366         int return_code = -1;
367         int datalen;
368         GVariant *noti_gv = NULL;
369         GVariant *body = NULL;
370         notification_h noti = NULL;
371         gchar *noti_data = NULL;
372         guchar *data;
373         GVariant *param = NULL;
374         GVariant *reply = NULL;
375
376         param = g_variant_new("(i)", alarm_id);
377
378         reply = g_dbus_proxy_call_sync(context.proxy, "alarm_get_noti_info", param,
379                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
380         if (error) {
381                 if (error_code) {
382                         if (error && error->code == G_DBUS_ERROR_ACCESS_DENIED)
383                                 *error_code = ERR_ALARM_NO_PERMISSION;
384                         else
385                                 *error_code = ERR_ALARM_SYSTEM_FAIL;
386                 }
387
388                 LOGE("g_dbus_proxy_call_sync() failed.\
389                                 alarm_id[%d], return_code[%d]. error->message is %s(%d)",
390                                 alarm_id, return_code, error->message, error->code);
391
392                 g_error_free(error);
393
394                 return NULL;
395         }
396
397         g_variant_get(reply, "(si)", &noti_data, &return_code);
398
399         LOGD("alarm_get_noti_info() dbus sync success. return_code[%d].",
400                         return_code);
401
402         if (return_code != 0) {
403                 if (error_code)
404                         *error_code = return_code;
405         } else {
406                 data = g_base64_decode(noti_data, (gsize *)&datalen);
407
408                 noti_gv = g_variant_new_from_data(G_VARIANT_TYPE("(v)"),
409                                 data, datalen,
410                                 TRUE, NULL, NULL);
411
412                 g_variant_get(noti_gv, "(v)", &body);
413
414                 noti = notification_create(NOTIFICATION_TYPE_NOTI);
415                 notification_ipc_make_noti_from_gvariant(noti, body);
416
417                 g_free(data);
418                 g_variant_unref(noti_gv);
419         }
420
421         if (noti_data)
422                 g_free(noti_data);
423
424         g_variant_unref(reply);
425         return noti;
426 }
427
428 bool _send_alarm_set_rtc_time(alarm_context_t context, alarm_date_t *time, int *error_code)
429 {
430         GError *error = NULL;
431         int return_code = -1;
432         GVariant *param = NULL;
433         GVariant *reply = NULL;
434
435         param = g_variant_new("(iiiiii)", time->year, time->month, time->day,
436                         time->hour, time->min, time->sec);
437
438         reply = g_dbus_proxy_call_sync(context.proxy, "alarm_set_rtc_time", param,
439                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
440         if (error) {
441                 if (error_code) {
442                         if (error && error->code == G_DBUS_ERROR_ACCESS_DENIED)
443                                 *error_code = ERR_ALARM_NO_PERMISSION;
444                         else
445                                 *error_code = ERR_ALARM_SYSTEM_FAIL;
446                 }
447
448                 LOGE("g_dbus_proxy_call_sync() failed.\
449                                 return_code[%d]. error->message is %s(%d)",
450                                 return_code, error->message, error->code);
451
452                 g_error_free(error);
453
454                 return false;
455         }
456
457         g_variant_get(reply, "(i)", &return_code);
458
459         LOGD("alarm_set_rtc_time() dbus sync success. return_code[%d].",
460                         return_code);
461
462         if (return_code != 0) {
463                 if (error_code)
464                         *error_code = return_code;
465
466                 g_variant_unref(reply);
467                 return false;
468         }
469
470         g_variant_unref(reply);
471         return true;
472 }
473
474 bool _send_alarm_delete(alarm_context_t context, alarm_id_t alarm_id, int *error_code)
475 {
476         GError *error = NULL;
477         int return_code = -1;
478         GVariant *param = NULL;
479         GVariant *reply = NULL;
480
481         param = g_variant_new("(i)", alarm_id);
482
483         reply = g_dbus_proxy_call_sync(context.proxy, "alarm_delete", param,
484                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
485         if (error) {
486                 if (error_code) {
487                         if (error && error->code == G_DBUS_ERROR_ACCESS_DENIED)
488                                 *error_code = ERR_ALARM_NO_PERMISSION;
489                         else
490                                 *error_code = ERR_ALARM_SYSTEM_FAIL;
491                 }
492
493                 LOGE("g_dbus_proxy_call_sync() failed.\
494                                 alarm_id[%d], return_code[%d]. error->message is %s(%d)",
495                                 alarm_id, return_code, error->message, error->code);
496
497                 g_error_free(error);
498
499                 return false;
500         }
501
502         g_variant_get(reply, "(i)", &return_code);
503
504         LOGD("alarm_delete() dbus sync success. return_code[%d].",
505                         return_code);
506
507         if (return_code != 0) {
508                 if (error_code)
509                         *error_code = return_code;
510
511                 g_variant_unref(reply);
512                 return false;
513         }
514
515         g_variant_unref(reply);
516         return true;
517 }
518
519 bool _send_alarm_delete_all(alarm_context_t context, int *error_code)
520 {
521         GError *error = NULL;
522         int return_code = -1;
523         GVariant *reply = NULL;
524
525         reply = g_dbus_proxy_call_sync(context.proxy, "alarm_delete_all", NULL,
526                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
527         if (error) {
528                 if (error_code) {
529                         if (error && error->code == G_DBUS_ERROR_ACCESS_DENIED)
530                                 *error_code = ERR_ALARM_NO_PERMISSION;
531                         else
532                                 *error_code = ERR_ALARM_SYSTEM_FAIL;
533                 }
534
535                 LOGE("g_dbus_proxy_call_sync() failed.\
536                                 return_code[%d]. error->message is %s(%d)",
537                                 return_code, error->message, error->code);
538
539                 g_error_free(error);
540                 return false;
541         }
542
543         g_variant_get(reply, "(i)", &return_code);
544
545         LOGD("alarm_delete_all() dbus sync success. return_code[%d].",
546                         return_code);
547
548         if (return_code != 0) {
549                 if (error_code)
550                         *error_code = return_code;
551
552                 g_variant_unref(reply);
553                 return false;
554         }
555
556         g_variant_unref(reply);
557         return true;
558 }
559
560 bool _send_alarm_get_list_of_ids(alarm_context_t context, int maxnum_of_ids,
561                 GVariantIter **iter, int *num_of_ids,
562                 int *error_code)
563 {
564         GError *error = NULL;
565         int return_code = -1;
566         GVariantIter *iter_temp = NULL;
567         GVariant *arr = NULL;
568         GVariant *param = NULL;
569         GVariant *reply = NULL;
570
571         param = g_variant_new("(i)", maxnum_of_ids);
572
573         reply = g_dbus_proxy_call_sync(context.proxy, "alarm_get_list_of_ids", param,
574                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
575         if (error) {
576                 if (error_code) {
577                         if (error && error->code == G_DBUS_ERROR_ACCESS_DENIED)
578                                 *error_code = ERR_ALARM_NO_PERMISSION;
579                         else
580                                 *error_code = ERR_ALARM_SYSTEM_FAIL;
581                 }
582                 LOGE("g_dbus_proxy_call_sync() failed.\
583                                 return_code[%d]. error->message is %s(%d)",
584                                 return_code, error->message, error->code);
585
586                 g_error_free(error);
587
588                 return false;
589         }
590
591         g_variant_get(reply, "(@aiii)", &arr, num_of_ids, &return_code);
592         g_variant_get(arr, "ai", &iter_temp);
593
594         LOGD("alarm_get_list_of_ids() dbus sync success. return_code[%d].",
595                         return_code);
596
597         if (return_code != 0) {
598                 if (error_code)
599                         *error_code = return_code;
600
601                 g_variant_unref(reply);
602                 return false;
603         }
604
605         *iter = iter_temp;
606
607         g_variant_unref(reply);
608
609         return true;
610 }
611
612 bool _send_alarm_get_number_of_ids(alarm_context_t context, int *num_of_ids,
613                 int *error_code)
614 {
615         GError *error = NULL;
616         int return_code = -1;
617         GVariant *reply = NULL;
618
619         reply = g_dbus_proxy_call_sync(context.proxy, "alarm_get_number_of_ids", NULL,
620                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
621         if (error) {
622                 if (error_code) {
623                         if (error && error->code == G_DBUS_ERROR_ACCESS_DENIED)
624                                 *error_code = ERR_ALARM_NO_PERMISSION;
625                         else
626                                 *error_code = ERR_ALARM_SYSTEM_FAIL;
627                 }
628
629                 LOGE("g_dbus_proxy_call_sync() failed.\
630                                 return_code[%d]. error->message is %s(%d)",
631                                 return_code, error->message, error->code);
632
633                 g_error_free(error);
634                 return false;
635         }
636
637         g_variant_get(reply, "(ii)", num_of_ids, &return_code);
638
639         LOGD("alarm_get_number_of_ids() dbus sync success.\
640                         num_of_ids[%d] return_code[%d].",
641                         *num_of_ids, return_code);
642
643         if (return_code != 0) {
644                 if (error_code)
645                         *error_code = return_code;
646
647                 g_variant_unref(reply);
648                 return false;
649         }
650
651         g_variant_unref(reply);
652         return true;
653 }
654
655 bool _send_alarm_get_info(alarm_context_t context, alarm_id_t alarm_id,
656                 alarm_info_t *alarm_info, int *error_code)
657 {
658         GError *error = NULL;
659         int return_code = -1;
660         GVariant *param = NULL;
661         GVariant *reply = NULL;
662         gint64 tmp_reserved_info;
663         param = g_variant_new("(i)", alarm_id);
664
665         reply = g_dbus_proxy_call_sync(context.proxy, "alarm_get_info", param,
666                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
667         if (error) {
668                 if (error_code) {
669                         if (error && error->code == G_DBUS_ERROR_ACCESS_DENIED)
670                                 *error_code = ERR_ALARM_NO_PERMISSION;
671                         else
672                                 *error_code = ERR_ALARM_SYSTEM_FAIL;
673                 }
674
675                 LOGE("g_dbus_proxy_call_sync() failed.\
676                                 alarm_id [%d] return_code[%d]. error->message is %s(%d)",
677                                 alarm_id, return_code, error->message, error->code);
678
679                 g_error_free(error);
680                 return false;
681         }
682
683         g_variant_get(reply, "(iiiiiiiiiiiixi)",
684                         &alarm_info->start.year,
685                         &alarm_info->start.month,
686                         &alarm_info->start.day,
687                         &alarm_info->start.hour,
688                         &alarm_info->start.min,
689                         &alarm_info->start.sec,
690                         &alarm_info->end.year,
691                         &alarm_info->end.month,
692                         &alarm_info->end.day,
693                         &alarm_info->mode.u_interval.day_of_week,
694                         &alarm_info->mode.repeat,
695                         &alarm_info->alarm_type,
696                         &tmp_reserved_info,
697                         &return_code);
698         alarm_info->reserved_info = (time_t)tmp_reserved_info;
699
700         LOGD("alarm_get_info() dbus sync success. return_code[%d].",
701                         return_code);
702
703         if (return_code != 0) {
704                 if (error_code)
705                         *error_code = return_code;
706
707                 g_variant_unref(reply);
708                 return false;
709         }
710
711         g_variant_unref(reply);
712         return true;
713 }
714
715 bool _send_alarm_get_next_duetime(alarm_context_t context, alarm_id_t alarm_id,
716                 time_t *duetime, int *error_code)
717 {
718         GError *error = NULL;
719         int return_code = -1;
720         GVariant *param = NULL;
721         GVariant *reply = NULL;
722         gint64 _duetime = 0;
723
724         param = g_variant_new("(i)", alarm_id);
725
726         reply = g_dbus_proxy_call_sync(context.proxy, "alarm_get_next_duetime", param,
727                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
728         if (error) {
729                 if (error_code) {
730                         if (error && error->code == G_DBUS_ERROR_ACCESS_DENIED)
731                                 *error_code = ERR_ALARM_NO_PERMISSION;
732                         else
733                                 *error_code = ERR_ALARM_SYSTEM_FAIL;
734                 }
735
736                 LOGE("g_dbus_proxy_call_sync() failed.\
737                                 alarm_id [%d] return_code[%d]. error->message is %s(%d)",
738                                 alarm_id, return_code, error->message, error->code);
739
740                 g_error_free(error);
741                 return false;
742         }
743
744         g_variant_get(reply, "(xi)", &_duetime, &return_code);
745
746         LOGD("alarm_get_next_duetime() dbus sync success. return_code[%d].",
747                         return_code);
748
749         if (return_code != 0) {
750                 if (error_code)
751                         *error_code = return_code;
752
753                 g_variant_unref(reply);
754                 return false;
755         }
756
757         *duetime = (time_t)_duetime;
758         g_variant_unref(reply);
759         return true;
760 }
761
762 bool _send_alarm_get_all_info(alarm_context_t context, char **db_path, int *error_code)
763 {
764         GError *error = NULL;
765         int return_code = -1;
766         GVariant *reply = NULL;
767         char *_db_path = NULL;
768
769         reply = g_dbus_proxy_call_sync(context.proxy, "alarm_get_all_info", NULL,
770                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
771         if (error) {
772                 if (error_code) {
773                         if (error && error->code == G_DBUS_ERROR_ACCESS_DENIED)
774                                 *error_code = ERR_ALARM_NO_PERMISSION;
775                         else
776                                 *error_code = ERR_ALARM_SYSTEM_FAIL;
777                 }
778
779                 LOGE("g_dbus_proxy_call_sync() failed.\
780                                 return_code[%d]. error->message is %s(%d)",
781                                 return_code, error->message, error->code);
782
783                 g_error_free(error);
784                 return false;
785         }
786
787         g_variant_get(reply, "(si)", &_db_path, &return_code);
788
789         LOGD("alarm_get_all_info() dbus sync success. db_path[%s] return_code[%d].",
790                         *db_path, return_code);
791
792         if (return_code != ALARMMGR_RESULT_SUCCESS) {
793                 if (error_code)
794                         *error_code = return_code;
795
796                 g_variant_unref(reply);
797                 return false;
798         }
799
800         *db_path = _db_path;
801         g_variant_unref(reply);
802         return true;
803 }
804
805 bool _send_alarm_set_time(alarm_context_t context, time_t new_time, int *error_code)
806 {
807         GError *error = NULL;
808         int return_code = -1;
809         GVariant *param = NULL;
810         GVariant *reply = NULL;
811
812         param = g_variant_new("(x)", (gint64)new_time);
813
814         reply = g_dbus_proxy_call_sync(context.proxy, "alarm_set_time", param,
815                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
816         if (error) {
817                 if (error_code)
818                         *error_code = ERR_ALARM_SYSTEM_FAIL;
819
820                 LOGE("g_dbus_proxy_call_sync() failed.\
821                                 return_code[%d]. error->message is %s(%d)",
822                                 return_code, error->message, error->code);
823
824                 g_error_free(error);
825                 return false;
826         }
827
828         g_variant_get(reply, "(i)", &return_code);
829
830         LOGD("alarm_set_time() dbus sync success. return_code[%d].",
831                         return_code);
832
833         if (return_code != ALARMMGR_RESULT_SUCCESS) {
834                 if (error_code)
835                         *error_code = return_code;
836
837                 g_variant_unref(reply);
838                 return false;
839         }
840
841         g_variant_unref(reply);
842         return true;
843 }
844
845 static void _alarm_set_time_cb(GObject *source_object, GAsyncResult *res,
846                 gpointer user_data)
847 {
848         int return_code = -1;
849         GError *error = NULL;
850         GVariant *reply = NULL;
851         alarm_set_time_data_t *func_data = (alarm_set_time_data_t *)user_data;
852
853         reply = g_dbus_proxy_call_finish(func_data->proxy, res, &error);
854         if (error) {
855                 LOGE("dbus error message: %s", error->message);
856                 g_error_free(error);
857                 return_code = ERR_ALARM_SYSTEM_FAIL;
858         } else {
859                 g_variant_get(reply, "(i)", &return_code);
860                 LOGD("alarm_set_time_async() dbus success. return_code[%d].",
861                                 return_code);
862         }
863
864         if (func_data->callback != NULL)
865                 func_data->callback(return_code, func_data->user_data);
866
867         g_variant_unref(reply);
868         g_free(func_data);
869 }
870
871 bool _send_alarm_set_time_async(alarm_context_t context, time_t new_time, alarm_set_time_cb_t result_cb, void *user_data)
872 {
873         alarm_set_time_data_t *func_data;
874         GVariant *param;
875
876         func_data = g_try_new0(alarm_set_time_data_t, 1);
877
878         if (func_data == NULL)
879                 return false;
880
881         func_data->callback = result_cb;
882         func_data->user_data = user_data;
883         func_data->proxy = context.proxy;
884
885         param = g_variant_new("(x)", (gint64)new_time);
886
887         g_dbus_proxy_call(context.proxy, "alarm_set_time", param,
888                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, _alarm_set_time_cb, func_data);
889
890         return true;
891 }
892
893 bool _send_alarm_set_time_with_propagation_delay(alarm_context_t context, struct timespec new_time, struct timespec req_time, int *error_code)
894 {
895         GError *error = NULL;
896         int return_code = -1;
897         GVariant *param = NULL;
898         GVariant *reply = NULL;
899
900         param = g_variant_new("(xxxx)", (gint64)new_time.tv_sec, (gint64)new_time.tv_nsec,
901                                 (gint64)req_time.tv_sec, (gint64)req_time.tv_nsec);
902
903         reply = g_dbus_proxy_call_sync(context.proxy, "alarm_set_time_with_propagation_delay", param,
904                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
905         if (error) {
906                 if (error_code) {
907                         if (error && error->code == G_DBUS_ERROR_ACCESS_DENIED)
908                                 *error_code = ERR_ALARM_NO_PERMISSION;
909                         else
910                                 *error_code = ERR_ALARM_SYSTEM_FAIL;
911                 }
912
913                 LOGE("g_dbus_proxy_call_sync() failed.\
914                                 return_code[%d]. error->message is %s(%d)",
915                                 return_code, error->message, error->code);
916
917                 g_error_free(error);
918                 return false;
919         }
920
921         g_variant_get(reply, "(i)", &return_code);
922
923         LOGD("alarm_set_time_with_propagation_delay dbus sync() success.\
924                         return_code[%d]", return_code);
925
926         if (return_code != ALARMMGR_RESULT_SUCCESS) {
927                 if (error_code)
928                         *error_code = return_code;
929
930                 g_variant_unref(reply);
931                 return false;
932         }
933
934         g_variant_unref(reply);
935         return true;
936 }
937
938 static void _alarm_set_time_with_delay_cb(GObject *source_object, GAsyncResult *res,
939                 gpointer user_data)
940 {
941         int return_code = -1;
942         GError *error = NULL;
943         GVariant *reply = NULL;
944         alarm_set_time_data_t *func_data = (alarm_set_time_data_t *)user_data;
945
946         reply = g_dbus_proxy_call_finish(func_data->proxy, res, &error);
947         if (error) {
948                 LOGE("dbus error message: %s", error->message);
949                 g_error_free(error);
950                 return_code = ERR_ALARM_SYSTEM_FAIL;
951         } else {
952                 g_variant_get(reply, "(i)", &return_code);
953
954                 LOGD("alarm_set_time_with_propagation_delay_async() dbus success.\
955                                 return_code[%d].", return_code);
956         }
957
958         if (func_data->callback != NULL)
959                 func_data->callback(return_code, func_data->user_data);
960
961         g_variant_unref(reply);
962         g_free(func_data);
963 }
964
965 bool _send_alarm_set_time_with_propagation_delay_async(alarm_context_t context, struct timespec new_time, struct timespec req_time, alarm_set_time_cb_t result_cb, void *user_data)
966 {
967         alarm_set_time_data_t *func_data;
968         GVariant *param = NULL;
969
970         func_data = g_try_new0(alarm_set_time_data_t, 1);
971
972         if (func_data == NULL)
973                 return false;
974
975         func_data->callback = result_cb;
976         func_data->user_data = user_data;
977         func_data->proxy = context.proxy;
978
979         param = g_variant_new("(xxxx)", (gint64)new_time.tv_sec, (gint64)new_time.tv_nsec,
980                                 (gint64)req_time.tv_sec, (gint64)req_time.tv_nsec);
981
982         g_dbus_proxy_call(context.proxy, "alarm_set_time_with_propagation_delay", param,
983                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, _alarm_set_time_with_delay_cb, func_data);
984
985         return true;
986 }
987
988 bool _send_alarm_set_timezone(alarm_context_t context, char *tzpath_str, int *error_code)
989 {
990         GError *error = NULL;
991         int return_code = -1;
992         GVariant *param = NULL;
993         GVariant *reply = NULL;
994
995         param = g_variant_new("(s)", tzpath_str);
996
997         reply = g_dbus_proxy_call_sync(context.proxy, "alarm_set_timezone", param,
998                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
999         if (error) {
1000                 if (error_code) {
1001                         if (error && error->code == G_DBUS_ERROR_ACCESS_DENIED)
1002                                 *error_code = ERR_ALARM_NO_PERMISSION;
1003                         else
1004                                 *error_code = ERR_ALARM_SYSTEM_FAIL;
1005                 }
1006
1007                 LOGE("g_dbus_proxy_call_sync() failed.\
1008                                 return_code[%d]. error->message is %s(%d)",
1009                                 return_code, error->message, error->code);
1010
1011                 g_error_free(error);
1012                 return false;
1013         }
1014
1015         g_variant_get(reply, "(i)", &return_code);
1016
1017         LOGD("alarm_set_timezon dbus sync() success. tz_path[%s] return_code[%d]",
1018                         tzpath_str, return_code);
1019
1020         if (return_code != ALARMMGR_RESULT_SUCCESS) {
1021                 if (error_code)
1022                         *error_code = return_code;
1023
1024                 g_variant_unref(reply);
1025                 return false;
1026         }
1027
1028         g_variant_unref(reply);
1029         return true;
1030 }
1031
1032 bool _send_alarm_set_global(alarm_context_t context, const alarm_id_t alarm_id, bool global, int *error_code)
1033 {
1034         GError *error = NULL;
1035         int return_code = -1;
1036         GVariant *param = NULL;
1037         GVariant *reply = NULL;
1038
1039         param = g_variant_new("(ib)", alarm_id, (gboolean)global);
1040
1041         reply = g_dbus_proxy_call_sync(context.proxy, "alarm_set_global", param,
1042                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1043         if (error) {
1044                 if (error_code) {
1045                         if (error && error->code == G_DBUS_ERROR_ACCESS_DENIED)
1046                                 *error_code = ERR_ALARM_NO_PERMISSION;
1047                         else
1048                                 *error_code = ERR_ALARM_SYSTEM_FAIL;
1049                 }
1050
1051                 LOGE("g_dbus_proxy_call_sync() failed.\
1052                                 alarm_id[%d] return_code[%d]. error->message is %s(%d)",
1053                                 alarm_id, return_code, error->message, error->code);
1054
1055                 g_error_free(error);
1056                 return false;
1057         }
1058
1059         g_variant_get(reply, "(i)", &return_code);
1060
1061         LOGD("alarm_set_global dbus sync() success. alarm_id[%d], global[%d]\
1062                         return_code[%d]", alarm_id, global, return_code);
1063
1064         if (return_code != ALARMMGR_RESULT_SUCCESS) {
1065                 if (error_code)
1066                         *error_code = return_code;
1067
1068                 g_variant_unref(reply);
1069                 return false;
1070         }
1071
1072         g_variant_unref(reply);
1073         return true;
1074 }
1075
1076 bool _send_alarm_get_global(alarm_context_t context, const alarm_id_t alarm_id, bool *global, int *error_code)
1077 {
1078         GError *error = NULL;
1079         int return_code = -1;
1080         GVariant *param = NULL;
1081         GVariant *reply = NULL;
1082
1083         param = g_variant_new("(i)", alarm_id);
1084
1085         reply = g_dbus_proxy_call_sync(context.proxy, "alarm_get_global", param,
1086                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1087         if (error) {
1088                 if (error_code) {
1089                         if (error->code == G_DBUS_ERROR_ACCESS_DENIED)
1090                                 *error_code = ERR_ALARM_NO_PERMISSION;
1091                         else
1092                                 *error_code = ERR_ALARM_SYSTEM_FAIL;
1093                 }
1094
1095                 LOGE("g_dbus_proxy_call_sync() failed.\
1096                                 alarm_id[%d] return_code[%d]. error->message is %s(%d)",
1097                                 alarm_id, return_code, error->message, error->code);
1098
1099                 g_error_free(error);
1100                 return false;
1101         }
1102
1103         g_variant_get(reply, "(bi)", global, &return_code);
1104
1105         LOGD("alarm_get_global dbus sync() success. alarm_id[%d], global[%d]\
1106                         return_code[%d]", alarm_id, *global, return_code);
1107
1108         if (return_code != 0) {
1109                 if (error_code)
1110                         *error_code = return_code;
1111
1112                 g_variant_unref(reply);
1113                 return false;
1114         }
1115
1116         g_variant_unref(reply);
1117         return true;
1118 }
1119
1120 bool _send_alarm_update(alarm_context_t context, alarm_id_t alarm_id,
1121                 alarm_info_t *alarm_info, int update_flag, int *error_code)
1122 {
1123         GError *error = NULL;
1124         int return_code = -1;
1125         GVariant *param = NULL;
1126         GVariant *reply = NULL;
1127
1128         param = g_variant_new("(iiiiiiiiiixiixi)",
1129                         alarm_id,
1130                         alarm_info->start.year,
1131                         alarm_info->start.month,
1132                         alarm_info->start.day,
1133                         alarm_info->start.hour,
1134                         alarm_info->start.min,
1135                         alarm_info->start.sec,
1136                         alarm_info->end.year,
1137                         alarm_info->end.month,
1138                         alarm_info->end.day,
1139                         (gint64)alarm_info->mode.u_interval.interval,
1140                         alarm_info->mode.repeat,
1141                         alarm_info->alarm_type,
1142                         (gint64)alarm_info->reserved_info,
1143                         update_flag);
1144
1145         reply = g_dbus_proxy_call_sync(context.proxy, "alarm_update", param,
1146                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1147         if (error) {
1148                 if (error_code) {
1149                         if (error->code == G_DBUS_ERROR_ACCESS_DENIED)
1150                                 *error_code = ERR_ALARM_NO_PERMISSION;
1151                         else
1152                                 *error_code = ERR_ALARM_SYSTEM_FAIL;
1153                 }
1154
1155                 LOGE("g_dbus_proxy_call_sync() failed.\
1156                                 alarm_id[%d] return_code[%d]. error->message is %s(%d)",
1157                                 alarm_id, return_code, error->message, error->code);
1158
1159                 g_error_free(error);
1160                 return false;
1161         }
1162
1163         g_variant_get(reply, "(i)", &return_code);
1164
1165         LOGD("alarm_update dbus sync() success. alarm_id[%d], return_code[%d]",
1166                         alarm_id, return_code);
1167
1168         if (return_code != 0) {
1169                 if (error_code)
1170                         *error_code = return_code;
1171
1172                 g_variant_unref(reply);
1173                 return false;
1174         }
1175
1176         g_variant_unref(reply);
1177         return true;
1178 }