Tizen 2.0 Release
[framework/system/sync-agent.git] / src / framework / event / oma_dm_noti_api.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 //#define COMPONENT_TAG APP
23 #ifndef SYNC_AGENT_LOG
24 #undef LOG_TAG
25 #define LOG_TAG "OMADM_CLIENT"
26 #endif
27
28 #include <sync_agent.h>
29
30 #include "oma_dm_noti_api.h"
31 #include "oma_dm_api_common_internal.h"
32
33 #ifndef EXPORT_API
34 #define EXPORT_API __attribute__ ((visibility("default")))
35 #endif
36
37 #define OMA_DM_PUSH_CONFIG_FILE "/usr/share/oma-dm-cfg/fw-init/omadmnotiui_fw_config.xml"
38
39 static int (*_get_push_noti_callback(int noti_id)) (sync_agent_event_data_s * request, void *data);
40
41 static int __download_noti_cb(sync_agent_event_data_s * request, void *data);
42 static int __install_noti_cb(sync_agent_event_data_s * request, void *data);
43 static int __download_info_noti_cb(sync_agent_event_data_s * request, void *data);
44 static int __engine_start_noti_cb(sync_agent_event_data_s * request, void *data);
45 static int __engine_fail_noti_cb(sync_agent_event_data_s * request, void *data);
46 static int __wifi_only_download_fail_noti_cb(sync_agent_event_data_s * request, void *data);
47 static int __memory_full_noti_cb(sync_agent_event_data_s * request, void *data);
48 static int __over_big_size_noti_cb(sync_agent_event_data_s * request, void *data);
49 static int __low_battery_noti_cb(sync_agent_event_data_s * request, void *data);
50 static int __default_noti_cb(sync_agent_event_data_s * request, void *data);
51
52 static int _push_event_interface(sync_agent_dm_event_e event_num, int user_data, char *server_id, char *session_id);
53
54 static int task_id = 0;
55
56 typedef enum {
57         PUSH_TYPE_NONE = -1,
58         PUSH_NOT_SPECIFIED = 0,
59         PUSH_BACKGROUND,
60         PUSH_INFORMATIVE,
61         PUSH_INTERACTION
62 } sync_agent_dm_push_type;
63
64 EXPORT_API sync_agent_dm_error_e sync_agent_dm_push_init()
65 {
66         _EXTERN_FUNC_ENTER;
67
68         sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
69
70         sync_agent_init_error_e init_error = sync_agent_init(OMA_DM_PUSH_CONFIG_FILE);
71         if (init_error != SYNC_AGENT_INIT_SUCCESS) {
72                 _DEBUG_ERROR("sync_agent_init() failed !! - err : %d", init_error);
73                 result = UI_SYNC_AGENT_DM_FAIL;
74         }
75
76         sync_agent_event_error_e err = sync_agent_run_noti_listener("omadm_noti_ui");
77         if (err != SYNC_AGENT_EVENT_SUCCESS) {
78                 _DEBUG_ERROR("sync_agent_run_noti_listener() failed !! - err : %d", err);
79                 result = UI_SYNC_AGENT_DM_FAIL;
80         }
81
82         int noti_id = NOTI_DOWNLOAD;
83         for (; noti_id < NOTI_ALERT_DISPLAY; noti_id++) {
84                 err = sync_agent_set_noti_callback(noti_id, _get_push_noti_callback(noti_id), NULL);
85                 if (err != SYNC_AGENT_EVENT_SUCCESS) {
86                         _DEBUG_ERROR("sync_agent_set_noti_callback(noti_id : %d) failed - err : %d !!", noti_id, err);
87                         result = UI_SYNC_AGENT_DM_FAIL;
88                         break;
89                 }
90         }
91
92         _EXTERN_FUNC_EXIT;
93
94         return result;
95 }
96
97 EXPORT_API sync_agent_dm_error_e sync_agent_dm_push_deinit()
98 {
99         _EXTERN_FUNC_ENTER;
100
101         sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
102
103         sync_agent_deinit_error_e deinit = SYNC_AGENT_DEINIT_SUCCESS;
104
105         deinit = sync_agent_deinit();
106         if (deinit != SYNC_AGENT_DEINIT_SUCCESS) {
107                 _DEBUG_ERROR("sync_agent_deinit() failed !!");
108                 result = UI_SYNC_AGENT_DM_FAIL;
109         }
110         /*
111            sync_agent_event_error_e err = sync_agent_stop_noti_listener();
112            if (err != SYNC_AGENT_EVENT_SUCCESS) {
113            _DEBUG_INFO("sync_agent_stop_noti_listener() failed !! - err : %d", err);
114            result = UI_SYNC_AGENT_DM_FAIL;
115            }
116
117            err = sync_agent_clean_event_handler();
118            if (err != SYNC_AGENT_EVENT_SUCCESS) {
119            _DEBUG_ERROR("sync_agent_clean_event_handler() failed !! - err : %d", err);
120            result = UI_SYNC_AGENT_DM_FAIL;
121            }
122          */
123
124         _EXTERN_FUNC_EXIT;
125
126         return result;
127 }
128
129 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_update_noti(char *server_id, char *session_id)
130 {
131         _EXTERN_FUNC_ENTER;
132
133         retvm_if((session_id == NULL || server_id == NULL), UI_SYNC_AGENT_DM_FAIL, "session id is null or server id is null ");
134
135         sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
136
137         if (_push_event_interface(EVENT_SOFTWARE_UPDATE, 0, server_id, session_id) != SYNC_AGENT_DM_API_RESULT_SUCCESS) {
138                 _DEBUG_ERROR("_push_event_interface(event num : %d) failed !!", EVENT_SOFTWARE_UPDATE);
139                 result = UI_SYNC_AGENT_DM_FAIL;
140         } else {
141                 _DEBUG_VERBOSE("_push_event_interface(event num : %d) success !!", EVENT_SOFTWARE_UPDATE);
142         }
143
144         _EXTERN_FUNC_EXIT;
145
146         return result;
147 }
148
149 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_cancel_noti()
150 {
151         _EXTERN_FUNC_ENTER;
152
153         sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
154
155         if (_push_event_interface(EVENT_CANCEL, 0, NULL, NULL) != SYNC_AGENT_DM_API_RESULT_SUCCESS) {
156                 _DEBUG_ERROR("_push_event_interface(event num : %d) failed !!", EVENT_CANCEL);
157                 result = UI_SYNC_AGENT_DM_FAIL;
158         } else {
159                 _DEBUG_VERBOSE("_push_event_interface(event num : %d) success !!", EVENT_CANCEL);
160         }
161
162         _EXTERN_FUNC_EXIT;
163
164         return result;
165 }
166
167 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_download_noti(int response_type)
168 {
169         _EXTERN_FUNC_ENTER;
170
171         sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
172
173         if (_push_event_interface(EVENT_DOWNLOAD, response_type, NULL, NULL) != SYNC_AGENT_DM_API_RESULT_SUCCESS) {
174                 _DEBUG_ERROR("_push_event_interface(event num : %d) failed !!", EVENT_DOWNLOAD);
175                 result = UI_SYNC_AGENT_DM_FAIL;
176         } else {
177                 _DEBUG_VERBOSE("_push_event_interface(event num : %d) success !!", EVENT_DOWNLOAD);
178         }
179
180         _EXTERN_FUNC_EXIT;
181
182         return result;
183 }
184
185 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_install_noti(int response_type)
186 {
187         _EXTERN_FUNC_ENTER;
188
189         sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
190
191         if (_push_event_interface(EVENT_INSTALL, response_type, NULL, NULL) != SYNC_AGENT_DM_API_RESULT_SUCCESS) {
192                 _DEBUG_ERROR("_push_event_interface(event num : %d) failed !!", EVENT_INSTALL);
193                 result = UI_SYNC_AGENT_DM_FAIL;
194         } else {
195                 _DEBUG_VERBOSE("_push_event_interface(event num : %d) success !!", EVENT_INSTALL);
196         }
197
198         _EXTERN_FUNC_EXIT;
199
200         return result;
201 }
202
203 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_set_interval_noti(int interval_type)
204 {
205         _EXTERN_FUNC_ENTER;
206
207         sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
208
209         if (_push_event_interface(EVENT_SET_INTERVAL, interval_type, NULL, NULL) != SYNC_AGENT_DM_API_RESULT_SUCCESS) {
210                 _DEBUG_ERROR("_push_event_interface(event num : %d) failed !!", EVENT_SET_INTERVAL);
211                 result = UI_SYNC_AGENT_DM_FAIL;
212         } else {
213                 _DEBUG_VERBOSE("_push_event_interface(event num : %d) success !!", EVENT_SET_INTERVAL);
214         }
215
216         _EXTERN_FUNC_EXIT;
217
218         return result;
219 }
220
221 static int (*_get_push_noti_callback(int noti_id)) (sync_agent_event_data_s * request, void *data) {
222         _EXTERN_FUNC_ENTER;
223
224         _DEBUG_INFO("start (noti_type : %d)", noti_id);
225
226         _EXTERN_FUNC_EXIT;
227
228         switch (noti_id) {
229         case NOTI_DOWNLOAD:
230                 return __download_noti_cb;
231                 break;
232         case NOTI_INSTALL:
233                 return __install_noti_cb;
234                 break;
235         case NOTI_DOWNLOAD_INFO:
236                 return __download_info_noti_cb;
237                 break;
238         case NOTI_ENGINE_START:
239                 return __engine_start_noti_cb;
240                 break;
241         case NOTI_ENGINE_FAIL:
242                 return __engine_fail_noti_cb;
243                 break;
244         case NOTI_WIFI_ONLY_DOWNLOAD_FAIL:
245                 return __wifi_only_download_fail_noti_cb;
246                 break;
247         case NOTI_MEMORY_FULL:
248                 return __memory_full_noti_cb;
249                 break;
250         case NOTI_OVER_BIG_SIZE:
251                 return __over_big_size_noti_cb;
252                 break;
253         case NOTI_LOW_BATTERY:
254                 return __low_battery_noti_cb;
255                 break;
256         default:
257                 return __default_noti_cb;
258                 break;
259         }
260
261         return 0;
262 }
263
264 static int __download_noti_cb(sync_agent_event_data_s * request, void *data)
265 {
266         _INNER_FUNC_ENTER;
267
268         _INNER_FUNC_EXIT;
269
270         return 0;
271 }
272
273 static int __install_noti_cb(sync_agent_event_data_s * request, void *data)
274 {
275         _INNER_FUNC_ENTER;
276
277         _INNER_FUNC_EXIT;
278
279         return 0;
280 }
281
282 static int __download_info_noti_cb(sync_agent_event_data_s * request, void *data)
283 {
284         _INNER_FUNC_ENTER;
285
286         int file_size = 0;
287         char *file_path = NULL;
288
289         sync_agent_get_event_data_param(request, &file_size);
290         sync_agent_get_event_data_param(request, &file_path);
291         sync_agent_get_event_data_param(request, &task_id);
292
293         _DEBUG_VERBOSE("file_size : %d", file_size);
294         _DEBUG_VERBOSE("file_path : %s", file_path);
295         _DEBUG_VERBOSE("task id : %d", task_id);
296
297         off_t return_stat;
298         struct stat file_info;
299         long int down_size = 0;
300         double percent = 0.0;
301
302         while (percent != 1.0) {
303                 if ((return_stat = stat(file_path, &file_info)) == -1) {
304                         _DEBUG_ERROR("Error: get file size !!");
305                         g_free(file_path);
306                         return 0;
307                 }
308                 down_size = (long int)(file_info.st_size);
309                 percent = ((double)down_size) / ((double)file_size);
310                 _DEBUG_VERBOSE("download percent : %d", (int)(percent * 100));
311                 usleep(100000);
312         }
313
314         _DEBUG_VERBOSE("download complete !!");
315
316         g_free(file_path);
317
318         if (request != NULL) {
319                 if (request->size != NULL)
320                         g_free(request->size);
321                 g_free(request);
322         }
323
324         _INNER_FUNC_EXIT;
325
326         return 0;
327 }
328
329 static int __engine_start_noti_cb(sync_agent_event_data_s * request, void *data)
330 {
331         _INNER_FUNC_ENTER;
332
333         sync_agent_get_event_data_param(request, &task_id);
334
335         _DEBUG_VERBOSE("task id : %d", task_id);
336
337         if (request != NULL) {
338                 if (request->size != NULL)
339                         g_free(request->size);
340                 g_free(request);
341         }
342
343         _INNER_FUNC_EXIT;
344
345         return 0;
346 }
347
348 static int __engine_fail_noti_cb(sync_agent_event_data_s * request, void *data)
349 {
350         _INNER_FUNC_ENTER;
351
352         int prev_noti_id = -1;
353         int fail_type = -1;
354
355         sync_agent_get_event_data_param(request, &prev_noti_id);
356         sync_agent_get_event_data_param(request, &fail_type);
357
358         _DEBUG_VERBOSE("previous noti id : %d", prev_noti_id);
359         _DEBUG_VERBOSE("fail type : %d", fail_type);
360
361         if (request != NULL) {
362                 if (request->size != NULL)
363                         g_free(request->size);
364                 g_free(request);
365         }
366
367         _INNER_FUNC_EXIT;
368
369         return 0;
370 }
371
372 static int __wifi_only_download_fail_noti_cb(sync_agent_event_data_s * request, void *data)
373 {
374         _INNER_FUNC_ENTER;
375
376         _INNER_FUNC_EXIT;
377
378         return 0;
379 }
380
381 static int __memory_full_noti_cb(sync_agent_event_data_s * request, void *data)
382 {
383         _INNER_FUNC_ENTER;
384
385         int file_size = 0;
386
387         sync_agent_get_event_data_param(request, &file_size);
388
389         _DEBUG_VERBOSE("file_size : %d", file_size);
390
391         if (request != NULL) {
392                 if (request->size != NULL)
393                         g_free(request->size);
394                 g_free(request);
395         }
396
397         _INNER_FUNC_EXIT;
398
399         return 0;
400 }
401
402 static int __over_big_size_noti_cb(sync_agent_event_data_s * request, void *data)
403 {
404         _INNER_FUNC_ENTER;
405
406         _INNER_FUNC_EXIT;
407
408         return 0;
409 }
410
411 static int __low_battery_noti_cb(sync_agent_event_data_s * request, void *data)
412 {
413         _INNER_FUNC_ENTER;
414
415         char *battery_level = NULL;
416
417         sync_agent_get_event_data_param(request, &battery_level);
418
419         _DEBUG_VERBOSE("battery level : %s", battery_level);
420
421         g_free(battery_level);
422
423         if (request != NULL) {
424                 if (request->size != NULL)
425                         g_free(request->size);
426                 g_free(request);
427         }
428
429         _INNER_FUNC_EXIT;
430
431         return 0;
432 }
433
434 static int __default_noti_cb(sync_agent_event_data_s * request, void *data)
435 {
436         _INNER_FUNC_ENTER;
437
438         _DEBUG_ERROR("noti_id flow is [%s]", "default");
439
440         _INNER_FUNC_EXIT;
441
442         return 0;
443 }
444
445 static int _push_event_interface(sync_agent_dm_event_e event_num, int user_data, char *server_id, char *session_id)
446 {
447         _EXTERN_FUNC_ENTER;
448
449         int api_result = SYNC_AGENT_DM_API_RESULT_SUCCESS;
450         sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
451         sync_agent_event_data_s *request_event = NULL;
452         sync_agent_event_data_s *response_event = NULL;
453         char *real_session_id = NULL;
454         char *real_server_id = NULL;
455
456         request_event = sync_agent_create_event(event_num);
457         if (request_event == NULL) {
458                 _DEBUG_ERROR("request_event is NULL !!");
459                 _EXTERN_FUNC_EXIT;
460                 return SYNC_AGENT_DM_API_RESULT_FAILURE;
461         }
462
463         _DEBUG_VERBOSE("event num : %d", event_num);
464
465         switch (event_num) {
466         case EVENT_SOFTWARE_UPDATE:
467                 {
468                         sync_agent_dm_ui_type_e ui_type = FOTA_NOTI_UI;
469                         real_session_id = strdup(session_id);
470                         real_server_id = strdup(server_id);
471                         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &ui_type);
472                         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, real_session_id);
473                         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, real_server_id);
474
475                         _DEBUG_VERBOSE("ui_type : FOTA_NOTI_UI");
476                         _DEBUG_VERBOSE("SESSION_ID : %s", real_session_id);
477                         _DEBUG_VERBOSE("SERVER_ID : %s", real_server_id);
478                 }
479                 break;
480         case EVENT_CANCEL:
481                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &task_id);
482
483                 _DEBUG_VERBOSE("task_id : %d", task_id);
484                 break;
485         case EVENT_DOWNLOAD:
486         case EVENT_INSTALL:
487         case EVENT_SET_INTERVAL:
488                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &user_data);
489
490                 _DEBUG_VERBOSE("user_data : %d", user_data);
491                 break;
492         default:
493                 break;
494         }
495
496         response_event = sync_agent_send_event(request_event, &error);
497         if (error != SYNC_AGENT_EVENT_SUCCESS) {
498                 _DEBUG_ERROR("sync_agent_send_event() failed !! - err = %d", error);
499                 sync_agent_free_event(request_event);
500                 _EXTERN_FUNC_EXIT;
501                 return SYNC_AGENT_DM_API_RESULT_FAILURE;
502         }
503
504         if (response_event == NULL) {
505                 _DEBUG_ERROR("response_event is NULL !!");
506                 sync_agent_free_event(request_event);
507                 _EXTERN_FUNC_EXIT;
508                 return SYNC_AGENT_DM_API_RESULT_FAILURE;
509         }
510
511         sync_agent_get_event_data_param(response_event, &api_result);
512         if (api_result != SYNC_AGENT_DM_API_RESULT_SUCCESS) {
513                 _DEBUG_ERROR("api_result : failed !!");
514                 sync_agent_free_event(request_event);
515                 sync_agent_free_event_data(response_event);
516                 _EXTERN_FUNC_EXIT;
517                 return SYNC_AGENT_DM_API_RESULT_FAILURE;
518         }
519
520         _DEBUG_VERBOSE("api_result : %d", api_result);
521
522         /* free request & response event */
523         if (event_num == EVENT_SOFTWARE_UPDATE) {
524                 free(real_session_id);
525                 free(real_server_id);
526         }
527         sync_agent_free_event(request_event);
528         sync_agent_free_event_data(response_event);
529
530         _EXTERN_FUNC_EXIT;
531
532         return api_result;
533 }