tizen 2.3 release
[framework/appfw/alarm-manager.git] / src / alarm-lib-stub.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 #include <stdio.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <string.h>
30 #include <glib.h>
31 #include "alarm.h"
32 #include "alarm-internal.h"
33 #include "alarm-mgr-stub.h"
34 #include "security-server.h"
35
36 #define ALARM_SERVICE_NAME      "appframework.alarm"
37 #define ALARM_OBJECT_PATH       "/appframework/alarm"
38 #define ALARM_INTERFACE_NAME "appframework.alarm"
39
40
41 bool _send_alarm_create(alarm_context_t context, alarm_info_t *alarm_info,
42                         alarm_id_t *alarm_id, const char *dst_service_name, const char *dst_service_name_mod,
43                         int *error_code);
44 bool _send_alarm_create_appsvc(alarm_context_t context, alarm_info_t *alarm_info,
45                         alarm_id_t *alarm_id, bundle *b,int *error_code);
46 bool _send_alarm_delete(alarm_context_t context, alarm_id_t alarm_id,
47                         int *error_code);
48 bool _send_alarm_get_list_of_ids(alarm_context_t context, int maxnum_of_ids,
49                                  alarm_id_t *alarm_id, int *num_of_ids,
50                                  int *error_code);
51 bool _send_alarm_get_number_of_ids(alarm_context_t context, int *num_of_ids,
52                                    int *error_code);
53 bool _send_alarm_get_info(alarm_context_t context, alarm_id_t alarm_id,
54                           alarm_info_t *alarm_info, int *error_code);
55
56 char* __get_cookie(int *error_code)
57 {
58         char *e_cookie = NULL;
59         char cookie[256] = {0,};
60         int size = 0;
61         int retval = 0;
62
63         size = security_server_get_cookie_size();
64         retval = security_server_request_cookie(cookie, size);
65         if (retval < 0) {
66                 ALARM_MGR_EXCEPTION_PRINT("security_server_request_cookie() is failed. retval = %d", retval);
67                 if (error_code) {
68                         *error_code = ERR_ALARM_SYSTEM_FAIL;
69                 }
70                 return NULL;
71         }
72
73         e_cookie = g_base64_encode((const guchar *)cookie, size);
74         if (e_cookie == NULL) {
75                 ALARM_MGR_EXCEPTION_PRINT("g_base64_encode() is failed.");
76                 if (error_code) {
77                         *error_code = ERR_ALARM_SYSTEM_FAIL;
78                 }
79                 return NULL;
80         }
81
82         ALARM_MGR_LOG_PRINT("Gets the cookie successfully.");
83         return e_cookie;
84 }
85
86
87 bool _send_alarm_create_appsvc(alarm_context_t context, alarm_info_t *alarm_info,
88                         alarm_id_t *alarm_id, bundle *b,
89                         int *error_code)
90 {
91         GError *error = NULL;
92         int return_code = 0;
93         char *e_cookie = NULL;
94         bundle_raw *b_data = NULL;
95         int datalen = 0;
96
97         if (bundle_encode(b, &b_data, &datalen))
98         {
99                 ALARM_MGR_EXCEPTION_PRINT("Unable to encode the bundle data\n");
100                 if (error_code) {
101                         *error_code = ERR_ALARM_SYSTEM_FAIL;
102                 }
103                 return false;
104         }
105
106         e_cookie = __get_cookie(error_code);
107         if (e_cookie == NULL) {
108                 ALARM_MGR_EXCEPTION_PRINT("Getting the cookie is failed. error_code = %d", *error_code);
109                 return false;
110         }
111
112         if (!alarm_manager_call_alarm_create_appsvc_sync((AlarmManager*)context.proxy, context.pid,
113                                                     alarm_info->start.year,
114                                                     alarm_info->start.month,
115                                                     alarm_info->start.day,
116                                                     alarm_info->start.hour,
117                                                     alarm_info->start.min,
118                                                     alarm_info->start.sec,
119                                                     alarm_info->end.year,
120                                                     alarm_info->end.month,
121                                                     alarm_info->end.day,
122                                                     alarm_info->mode.u_interval.day_of_week,
123                                                     alarm_info->mode.repeat,
124                                                     alarm_info->alarm_type,
125                                                     alarm_info->reserved_info,
126                                                     (char *)b_data, e_cookie,
127                                                     alarm_id, &return_code,
128                                                     NULL, &error)) {
129                 /* g_dbus_proxy_call_sync error */
130                 /* error_code should be set */
131                 ALARM_MGR_EXCEPTION_PRINT(
132                 "alarm_manager_call_alarm_create_appsvc_sync()failed. alarm_id[%d], return_code[%d].", alarm_id, return_code);
133                 ALARM_MGR_EXCEPTION_PRINT("error->message is %s.", error->message);
134         }
135
136         g_free(e_cookie);
137
138         if (b_data) {
139                 free(b_data);
140                 b_data = NULL;
141         }
142
143         if (return_code != 0) {
144                 if (error_code) {
145                         *error_code = return_code;
146                 }
147                 return false;
148         }
149
150         return true;
151 }
152
153
154 bool _send_alarm_create(alarm_context_t context, alarm_info_t *alarm_info,
155                         alarm_id_t *alarm_id, const char *dst_service_name, const char *dst_service_name_mod,
156                         int *error_code)
157 {
158         GError *error = NULL;
159         int return_code = 0;
160         char *e_cookie = NULL;
161
162         /*TODO: Dbus bus name validation is must & will be added to avoid alarm-server crash*/
163         if (g_quark_to_string(context.quark_app_service_name) == NULL
164                 && strlen(dst_service_name) == 4
165                 && strncmp(dst_service_name, "null",4) == 0) {
166                         ALARM_MGR_EXCEPTION_PRINT("Invalid arg. Provide valid destination or call alarmmgr_init()\n");
167                 if (error_code) {
168                         *error_code = ERR_ALARM_INVALID_PARAM;
169                 }
170                 return false;
171         }
172
173         e_cookie = __get_cookie(error_code);
174         if (e_cookie == NULL) {
175                 ALARM_MGR_EXCEPTION_PRINT("Getting the cookie is failed. error_code = %d", *error_code);
176                 return false;
177         }
178
179         if (!alarm_manager_call_alarm_create_sync((AlarmManager*)context.proxy, context.pid,
180                         g_quark_to_string(context.quark_app_service_name),
181                         g_quark_to_string(context.quark_app_service_name_mod),
182                                                     alarm_info->start.year,
183                                                     alarm_info->start.month,
184                                                     alarm_info->start.day,
185                                                     alarm_info->start.hour,
186                                                     alarm_info->start.min,
187                                                     alarm_info->start.sec,
188                                                     alarm_info->end.year,
189                                                     alarm_info->end.month,
190                                                     alarm_info->end.day,
191                                                     alarm_info->mode.u_interval.day_of_week,
192                                                     alarm_info->mode.repeat,
193                                                     alarm_info->alarm_type,
194                                                     alarm_info->reserved_info,
195                                                     dst_service_name, dst_service_name_mod, e_cookie,
196                                                     alarm_id, &return_code,
197                                                     NULL, &error)) {
198                 /* g_dbus_proxy_call_sync error error */
199                 /* error_code should be set */
200                 ALARM_MGR_EXCEPTION_PRINT(
201                 "alarm_manager_call_alarm_create_sync()failed. alarm_id[%d], return_code[%d]", alarm_id, return_code);
202                 ALARM_MGR_EXCEPTION_PRINT("error->message is %s", error->message);
203                 if (error_code) {
204                         *error_code = ERR_ALARM_SYSTEM_FAIL;
205                 }
206                 g_free(e_cookie);
207                 return false;
208         }
209
210         g_free(e_cookie);
211
212         if (return_code != 0) {
213                 if (error_code) {
214                         *error_code = return_code;
215                 }
216                 return false;
217         }
218
219         return true;
220 }
221 bundle *_send_alarm_get_appsvc_info(alarm_context_t context, alarm_id_t alarm_id, int *error_code)
222 {
223         GError *error = NULL;
224         int return_code = 0;
225         bundle *b = NULL;
226         char *e_cookie = NULL;
227         gchar *b_data = NULL;
228         int len = 0;
229
230         e_cookie = __get_cookie(error_code);
231         if (e_cookie == NULL) {
232                 ALARM_MGR_EXCEPTION_PRINT("Getting the cookie is failed. error_code = %d", *error_code);
233                 return false;
234         }
235
236         if (!alarm_manager_call_alarm_get_appsvc_info_sync
237             ((AlarmManager*)context.proxy, context.pid, alarm_id, e_cookie, &b_data, &return_code, NULL, &error)) {
238                 /* g_dbus_proxy_call_sync error */
239                 /*error_code should be set */
240                 ALARM_MGR_EXCEPTION_PRINT("alarm_manager_call_alarm_get_appsvc_info_sync() failed. alarm_id[%d], return_code[%d].", alarm_id, return_code);
241
242                 if (error_code) {
243                         *error_code = ERR_ALARM_SYSTEM_FAIL;
244                 }
245
246                 g_free(e_cookie);
247
248                 if (b_data) {
249                         g_free(b_data);
250                 }
251
252                 return NULL;
253         }
254
255         if (return_code != 0) {
256                 if (error_code) {
257                         *error_code = return_code;
258                 }
259         } else {
260                 b = bundle_decode((bundle_raw *)b_data, len);
261         }
262
263         g_free(e_cookie);
264
265         if (b_data) {
266                 g_free(b_data);
267         }
268
269         return b;
270 }
271
272
273 bool _send_alarm_set_rtc_time(alarm_context_t context, alarm_date_t *time, int *error_code){
274
275         GError *error = NULL;
276         int return_code = 0;
277         char *e_cookie = NULL;
278
279         e_cookie = __get_cookie(error_code);
280         if (e_cookie == NULL) {
281                 ALARM_MGR_EXCEPTION_PRINT("Getting the cookie is failed. error_code = %d", *error_code);
282                 return false;
283         }
284
285         if (!alarm_manager_call_alarm_set_rtc_time_sync
286             ((AlarmManager*)context.proxy, context.pid,
287                 time->year, time->month, time->day,
288                  time->hour, time->min, time->sec,
289                   e_cookie, NULL, NULL, &error)) {
290                 /* g_dbus_proxy_call_sync error */
291                 /*error_code should be set */
292                 ALARM_MGR_EXCEPTION_PRINT("alarm_manager_call_alarm_set_rtc_time() failed. return_code[%d]\n", return_code);
293                 if (error_code) {
294                         *error_code = ERR_ALARM_SYSTEM_FAIL;
295                 }
296
297                 g_free(e_cookie);
298                 return false;
299         }
300
301         g_free(e_cookie);
302
303         if (return_code != 0) {
304                 if (error_code) {
305                         *error_code = return_code;
306                 }
307                 return false;
308         }
309
310         return true;
311 }
312
313 bool _send_alarm_delete(alarm_context_t context, alarm_id_t alarm_id, int *error_code)
314 {
315         GError *error = NULL;
316         int return_code = 0;
317         char *e_cookie = NULL;
318
319         e_cookie = __get_cookie(error_code);
320         if (e_cookie == NULL) {
321                 ALARM_MGR_EXCEPTION_PRINT("Getting the cookie is failed. error_code = %d", *error_code);
322                 return false;
323         }
324
325         if (!alarm_manager_call_alarm_delete_sync
326             ((AlarmManager*)context.proxy, context.pid, alarm_id, e_cookie, &return_code, NULL, &error)) {
327                 /* g_dbus_proxy_call_sync error */
328                 /*error_code should be set */
329                 ALARM_MGR_EXCEPTION_PRINT("alarm_manager_call_alarm_delete_sync() failed. alarm_id[%d], return_code[%d]", alarm_id, return_code);
330                 if (error_code) {
331                         *error_code = ERR_ALARM_SYSTEM_FAIL;
332                 }
333
334                 g_free(e_cookie);
335                 return false;
336         }
337
338         g_free(e_cookie);
339
340         if (return_code != 0) {
341                 if (error_code) {
342                         *error_code = return_code;
343                 }
344                 return false;
345         }
346
347         return true;
348 }
349
350 bool _send_alarm_delete_all(alarm_context_t context, int *error_code)
351 {
352         GError *error = NULL;
353         int return_code = 0;
354         char *e_cookie = NULL;
355
356         e_cookie = __get_cookie(error_code);
357         if (e_cookie == NULL) {
358                 ALARM_MGR_EXCEPTION_PRINT("Getting the cookie is failed. error_code = %d", *error_code);
359                 return false;
360         }
361
362         if (!alarm_manager_call_alarm_delete_all_sync
363             ((AlarmManager*)context.proxy, context.pid, e_cookie, &return_code, NULL, &error)) {
364                 /* g_dbus_proxy_call_sync error */
365                 /*error_code should be set */
366                 ALARM_MGR_EXCEPTION_PRINT("alarm_manager_call_alarm_delete_all_sync() failed. return_code[%d]", return_code);
367                 if (error_code) {
368                         *error_code = ERR_ALARM_SYSTEM_FAIL;
369                 }
370
371                 g_free(e_cookie);
372                 return false;
373         }
374
375         g_free(e_cookie);
376
377         if (return_code != 0) {
378                 if (error_code) {
379                         *error_code = return_code;
380                 }
381                 return false;
382         }
383
384         return true;
385 }
386
387 bool _send_alarm_get_list_of_ids(alarm_context_t context, int maxnum_of_ids,
388                                  alarm_id_t *alarm_id, int *num_of_ids,
389                                  int *error_code)
390 {
391         GError *error = NULL;
392         GVariant *alarm_array = NULL;
393         int return_code = 0;
394         int i = 0;
395
396         if (!alarm_manager_call_alarm_get_list_of_ids_sync((AlarmManager*)context.proxy,
397                              context.pid, maxnum_of_ids, &alarm_array,
398                              num_of_ids, &return_code, NULL, &error)) {
399                 /* g_dbus_proxy_call_sync error */
400                 /*error_code should be set */
401                 ALARM_MGR_EXCEPTION_PRINT(
402                 "alarm_manager_call_alarm_get_list_of_ids_sync() failed by dbus. alarm_id[%d], return_code[%d]\n", alarm_id, return_code);
403                 if (error_code) {
404                         *error_code = ERR_ALARM_SYSTEM_FAIL;
405                 }
406
407                 return false;
408         }
409
410         if (return_code != 0) {
411                 if (error_code) {
412                         *error_code = return_code;
413                 }
414                 return false;
415         }
416         else
417         {
418                 GVariantIter *iter = NULL;
419                 gint i = 0;
420                 g_variant_get (alarm_array, "ai", &iter);
421                 while (g_variant_iter_loop (iter, "i", &alarm_id[i]))
422                 {
423                         ALARM_MGR_LOG_PRINT("alarm_id (%d)", alarm_id[i]);
424                         i++;
425                 }
426                 g_variant_iter_free (iter);
427                 *num_of_ids = i;
428                 g_variant_unref(alarm_array);
429         }
430
431         return true;
432 }
433
434 bool _send_alarm_get_number_of_ids(alarm_context_t context, int *num_of_ids,
435                                    int *error_code)
436 {
437         GError *error = NULL;
438         gint return_code = 0;
439         char *e_cookie = NULL;
440
441         e_cookie = __get_cookie(error_code);
442         if (e_cookie == NULL) {
443                 ALARM_MGR_EXCEPTION_PRINT("Getting the cookie is failed. error_code = %d", *error_code);
444                 return false;
445         }
446
447         if (!alarm_manager_call_alarm_get_number_of_ids_sync((AlarmManager*)context.proxy, context.pid, e_cookie, num_of_ids, &return_code, NULL, &error)) {
448                 /* g_dbus_proxy_call_sync error */
449                 /* error_code should be set */
450                 ALARM_MGR_EXCEPTION_PRINT(
451                 "alarm_manager_call_alarm_get_number_of_ids_sync() failed by dbus. return_code[%d], return_code[%s].",
452                 return_code, error->message);
453
454                 if (error_code) {
455                         *error_code = ERR_ALARM_SYSTEM_FAIL;
456                 }
457                 return false;
458         }
459
460         if (return_code != 0) {
461                 if (error_code) {
462                         *error_code = return_code;
463                 }
464                 return false;
465         }
466
467         return true;
468 }
469
470 bool _send_alarm_get_info(alarm_context_t context, alarm_id_t alarm_id,
471                           alarm_info_t *alarm_info, int *error_code)
472 {
473         GError *error = NULL;
474         int return_code = 0;
475         char *e_cookie = NULL;
476
477         e_cookie = __get_cookie(error_code);
478         if (e_cookie == NULL) {
479                 ALARM_MGR_EXCEPTION_PRINT("Getting the cookie is failed. error_code = %d", *error_code);
480                 return false;
481         }
482
483         if (!alarm_manager_call_alarm_get_info_sync((AlarmManager*)context.proxy,
484                 context.pid, alarm_id, e_cookie, &alarm_info->start.year,
485                 &alarm_info->start.month, &alarm_info->start.day,
486                 &alarm_info->start.hour, &alarm_info->start.min,
487                 &alarm_info->start.sec, &alarm_info->end.year,
488                 &alarm_info->end.month, &alarm_info->end.day,
489                 &alarm_info->mode.u_interval.day_of_week,
490                 (gint *)&alarm_info->mode.repeat,
491                 &alarm_info->alarm_type, &alarm_info->reserved_info, &return_code, NULL, &error)) {
492                 /* g_dbus_proxy_call_sync error */
493                 /* error_code should be set */
494                 ALARM_MGR_EXCEPTION_PRINT(
495                 "alarm_manager_call_alarm_get_info_sync() failed by dbus. alarm_id[%d], return_code[%d]\n", alarm_id, return_code);
496                 if (error_code) {
497                         *error_code = ERR_ALARM_SYSTEM_FAIL;
498                 }
499                 g_free(e_cookie);
500                 return false;
501         }
502
503         g_free(e_cookie);
504
505         if (return_code != 0) {
506                 if (error_code) {
507                         *error_code = return_code;
508                 }
509                 return false;
510         }
511
512         return true;
513 }
514
515 bool _send_alarm_get_next_duetime(alarm_context_t context,
516                                  alarm_id_t alarm_id, time_t* duetime,
517                                  int *error_code)
518 {
519         GError *error = NULL;
520         int return_code = 0;
521         char *e_cookie = NULL;
522
523         e_cookie = __get_cookie(error_code);
524         if (e_cookie == NULL) {
525                 ALARM_MGR_EXCEPTION_PRINT("Getting the cookie is failed. error_code = %d", *error_code);
526                 return false;
527         }
528
529         if (!alarm_manager_call_alarm_get_next_duetime_sync((AlarmManager*)context.proxy,
530                              context.pid, alarm_id, e_cookie, duetime, &return_code, NULL, &error)) {
531                 /*g_dbus_proxy_call_sync error */
532                 /*error_code should be set */
533                 ALARM_MGR_EXCEPTION_PRINT(
534                 "alarm_manager_call_alarm_get_next_duetime_sync() failed by dbus. alarm_id[%d], return_code[%d]\n", alarm_id, return_code);
535                 if (error_code) {
536                         *error_code = ERR_ALARM_SYSTEM_FAIL;
537                 }
538                 g_free(e_cookie);
539                 return false;
540         }
541
542         g_free(e_cookie);
543
544         if (return_code != 0) {
545                 if (error_code) {
546                         *error_code = return_code;
547                 }
548                 return false;
549         }
550
551         return true;
552 }
553
554 bool _send_alarm_get_all_info(alarm_context_t context, char ** db_path, int *error_code)
555 {
556         GError *error = NULL;
557         int return_code = 0;
558         char *e_cookie = NULL;
559
560         e_cookie = __get_cookie(error_code);
561         if (e_cookie == NULL) {
562                 ALARM_MGR_EXCEPTION_PRINT("Getting the cookie is failed. error_code = %d", *error_code);
563                 return false;
564         }
565
566         if (!alarm_manager_call_alarm_get_all_info_sync((AlarmManager*)context.proxy, context.pid, e_cookie, db_path, &return_code, NULL, &error)) {
567                 /*g_dbus_proxy_call_sync error */
568                 /*error_code should be set */
569                 ALARM_MGR_EXCEPTION_PRINT("alarm_manager_call_alarm_get_all_info_sync() failed by dbus. return_code[%d][%s]", return_code, error->message);
570                 if (error_code) {
571                         *error_code = ERR_ALARM_SYSTEM_FAIL;
572                 }
573                 g_free(e_cookie);
574                 return false;
575         }
576
577         g_free(e_cookie);
578
579         if (return_code != ALARMMGR_RESULT_SUCCESS) {
580                 if (error_code) {
581                         *error_code = return_code;
582                 }
583                 return false;
584         }
585
586         return true;
587 }