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