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