changed operation string
[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
313 bool _send_alarm_set_rtc_time(alarm_context_t context, alarm_date_t *time, int *error_code){
314
315         GError *error = NULL;
316         int return_code = 0;
317
318         char cookie[256] = {0,};
319         char *e_cookie = NULL;
320         int size = 0;
321         int retval = 0;
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                 if (error_code)
330                         *error_code = -1; /*TODO: define error*/
331                 return false;
332         }
333
334         e_cookie = g_base64_encode((const guchar *)cookie, size);
335
336         if (NULL == e_cookie)
337         {
338                 ALARM_MGR_EXCEPTION_PRINT(
339                         "g_base64_encode failed\n");
340                 if (error_code)
341                         *error_code = -1; /*TODO: define error*/
342                 return false;
343         }
344
345         if (!org_tizen_alarm_manager_alarm_set_rtc_time
346             (context.proxy, context.pid,
347                 time->year, time->month, time->day,
348                  time->hour, time->min, time->sec,
349                   e_cookie, &return_code, &error)) {
350                 /* dbus-glib error */
351                 /*error_code should be set */
352                 ALARM_MGR_EXCEPTION_PRINT(
353                 "org_tizen_alarm_manager_alarm_set_rtc_time() failed. "
354                      "return_code[%d]\n", return_code);
355                 if (error_code)
356                         *error_code = ERR_ALARM_SYSTEM_FAIL; /*-1 means that system
357                                                                 failed internally.*/
358                 if (e_cookie)
359                         g_free(e_cookie);
360
361                 return false;
362         }
363         if (e_cookie)
364         {
365                 g_free(e_cookie);
366                 e_cookie = NULL;
367         }
368
369         if (return_code != 0) {
370                 if (error_code)
371                         *error_code = return_code;
372                 return false;
373         }
374
375         return true;
376
377 }
378
379 bool _send_alarm_delete(alarm_context_t context, alarm_id_t alarm_id,
380                         int *error_code)
381 {
382         GError *error = NULL;
383         int return_code = 0;
384
385         char cookie[256];
386         char *e_cookie;
387         int size;
388         int retval;
389
390         size = security_server_get_cookie_size();
391         retval = security_server_request_cookie(cookie, size);
392
393         if (retval < 0) {
394                 ALARM_MGR_EXCEPTION_PRINT(
395                         "security_server_request_cookie failed\n");
396                 return false;
397         }
398
399         e_cookie = g_base64_encode((const guchar *)cookie, size);
400
401         if (!org_tizen_alarm_manager_alarm_delete
402             (context.proxy, context.pid, alarm_id, e_cookie, &return_code,
403              &error)) {
404                 /* dbus-glib error */
405                 /*error_code should be set */
406                 ALARM_MGR_EXCEPTION_PRINT(
407                 "org_tizen_alarm_manager_alarm_delete() failed. " 
408                      "alarm_id[%d], return_code[%d]\n", alarm_id, return_code);
409                 if (error_code)
410                         *error_code = -1;       /*-1 means that system 
411                                                         failed internally.*/
412
413                 return false;
414         }
415
416         g_free(e_cookie);
417
418         if (return_code != 0) {
419                 if (error_code)
420                         *error_code = return_code;
421                 return false;
422         }
423
424         return true;
425
426 }
427
428 #ifdef __ALARM_BOOT
429 bool _send_alarm_power_on(alarm_context_t context, bool on_off,
430                            int *error_code)
431 {
432         GError *error = NULL;
433         int return_code = 0;
434
435         if (!org_tizen_alarm_manager_alarm_power_on
436             (context.proxy, context.pid, on_off, &return_code, &error)) {
437                 /* dbus-glib error */
438                 /* error_code should be set */
439                 ALARM_MGR_EXCEPTION_PRINT(
440                 "org_tizen_alarm_manager_alarm_power_on failed. "
441                      "return_code[%d]\n", return_code);
442                 if (error_code)
443                         *error_code = -1;       /*-1 means that system 
444                                                         failed internally*/
445                 return false;
446         }
447
448         if (return_code != 0) {
449                 if (error_code)
450                         *error_code = return_code;
451                 return false;
452         }
453
454         return true;
455
456 }
457
458 bool _send_alarm_power_off(alarm_context_t context, int *error_code)
459 {
460         GError *error = NULL;
461         int return_code = 0;
462
463         if (!org_tizen_alarm_manager_alarm_power_off(context.proxy,
464                                 context.pid, &return_code, &error)) {
465                 /* dbus-glib error */
466                 /* error_code should be set */
467                 ALARM_MGR_EXCEPTION_PRINT(
468                 "org_tizen_alarm_manager_alarm_power_off failed. "
469                      "return_code[%d]\n", return_code);
470                 if (error_code)
471                         *error_code = -1;       /*-1 means that system 
472                                                 failed internally.*/
473                 return false;
474         }
475
476         if (return_code != 0) {
477                 if (error_code)
478                         *error_code = return_code;
479                 return false;
480         }
481
482         return true;
483
484 }
485
486 bool _send_alarm_check_next_duetime(alarm_context_t context, int *error_code)
487 {
488         GError *error = NULL;
489         int return_code = 0;
490
491         if (!org_tizen_alarm_manager_alarm_check_next_duetime(context.proxy,
492                                         context.pid, &return_code, &error)) {
493                 /*dbus-glib error */
494                 /*error_code should be set */
495                 ALARM_MGR_EXCEPTION_PRINT(
496                 "org_tizen_alarm_manager_alarm_power_check_next_duetime's "
497                 "return value is false. return_code[%d]\n", return_code);
498                 if (error_code)
499                         *error_code = -1;       /*-1 means that system 
500                                                 failed internally*/
501                 return false;
502         }
503
504         if (return_code != 0) {
505                 if (error_code)
506                         *error_code = return_code;
507                 return false;
508         }
509
510         return true;
511
512 }
513
514 #endif                          /* __ALARM_BOOT */
515
516 bool _send_alarm_get_list_of_ids(alarm_context_t context, int maxnum_of_ids,
517                                  alarm_id_t *alarm_id, int *num_of_ids,
518                                  int *error_code)
519 {
520
521         GError *error = NULL;
522         GArray *alarm_array = NULL;
523         int return_code = 0;
524         int i = 0;
525
526         if (!org_tizen_alarm_manager_alarm_get_list_of_ids(context.proxy,
527                              context.pid, maxnum_of_ids, &alarm_array,
528                              num_of_ids, &return_code, &error)) {
529                 /*dbus-glib error */
530                 /*error_code should be set */
531                 ALARM_MGR_EXCEPTION_PRINT(
532                 "org_tizen_alarm_manager_alarm_get_list_of_ids() failed. "
533                      "alarm_id[%d], return_code[%d]\n", alarm_id, return_code);
534                 if (error_code)
535                         *error_code = -1;       /*-1 means that system 
536                                                         failed internally.*/
537
538                 return false;
539         }
540
541         if (return_code != 0) {
542                 if (error_code)
543                         *error_code = return_code;
544                 return false;
545         } else {
546                 for (i = 0; i < alarm_array->len && i < maxnum_of_ids; i++) {
547                         alarm_id[i] = g_array_index(alarm_array, alarm_id_t, i);
548                         ALARM_MGR_LOG_PRINT(" alarm_id(%d)\n", alarm_id[i]);
549                 }
550
551                 *num_of_ids = alarm_array->len;
552                 g_array_free(alarm_array, true);
553         }
554
555         return true;
556 }
557
558 bool _send_alarm_get_number_of_ids(alarm_context_t context, int *num_of_ids,
559                                    int *error_code)
560 {
561         GError *error = NULL;
562         gint return_code = 0;
563
564         if (!org_tizen_alarm_manager_alarm_get_number_of_ids(context.proxy,
565                              context.pid, num_of_ids, &return_code, &error)) {
566                 /* dbus-glib error */
567                 /* error_code should be set */
568                 ALARM_MGR_EXCEPTION_PRINT(
569                 "org_tizen_alarm_manager_alarm_get_number_of_ids() failed. "
570                 "return_code[%d], return_code[%s]\n", \
571                         return_code, error->message);
572                 if (error_code)
573                         *error_code = -1;       /*-1 means that system 
574                                                         failed internally*/
575                 return false;
576         }
577
578         if (return_code != 0) {
579                 if (error_code)
580                         *error_code = return_code;
581                 return false;
582         }
583
584         return true;
585
586 }
587
588 bool _send_alarm_get_info(alarm_context_t context, alarm_id_t alarm_id,
589                           alarm_info_t *alarm_info, int *error_code)
590 {
591         GError *error = NULL;
592         int return_code = 0;
593
594         if (!org_tizen_alarm_manager_alarm_get_info(context.proxy,
595                      context.pid, alarm_id, &alarm_info->start.year,
596                      &alarm_info->start.month, &alarm_info->start.day,
597                      &alarm_info->start.hour, &alarm_info->start.min,
598                      &alarm_info->start.sec, &alarm_info->end.year,
599                      &alarm_info->end.month, &alarm_info->end.day,
600                      &alarm_info->mode.u_interval.day_of_week,
601                      (gint *)&alarm_info->mode.repeat,
602                      &alarm_info->alarm_type, &alarm_info->reserved_info,
603                                                       &return_code, &error)) {
604                 /*dbus-glib error */
605                 /* error_code should be set */
606                 ALARM_MGR_EXCEPTION_PRINT(
607                 "org_tizen_alarm_manager_alarm_get_info() failed. "
608                      "alarm_id[%d], return_code[%d]\n", alarm_id, return_code);
609                 if (error_code)
610                         *error_code = -1;       /*-1 means that system failed 
611                                                                 internally.*/
612                 return false;
613         }
614
615         if (return_code != 0) {
616                 if (error_code)
617                         *error_code = return_code;
618                 return false;
619         }
620         return true;
621
622 }