Tizen 2.1 base
[platform/core/system/sync-agent.git] / src / framework / event / oma_dm_fota_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_fota_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_FOTA_CONFIG_FILE "/usr/share/oma-dm-cfg/fw-init/omadmfotaui_fw_config.xml"
38
39 static int (*_get_fota_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 _fota_event_interface(sync_agent_dm_event_e event_num, int user_data);
53
54 static int task_id = 0;
55
56 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_init()
57 {
58         _EXTERN_FUNC_ENTER;
59
60         sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
61
62         sync_agent_init_error_e init_error = sync_agent_init(OMA_DM_FOTA_CONFIG_FILE);
63         if (init_error != SYNC_AGENT_INIT_SUCCESS) {
64                 _DEBUG_ERROR("sync_agent_init() failed !! - err : %d", init_error);
65                 result = UI_SYNC_AGENT_DM_FAIL;
66         }
67
68         sync_agent_event_error_e err = sync_agent_run_noti_listener("omadm_fota_ui");
69         if (err != SYNC_AGENT_EVENT_SUCCESS) {
70                 _DEBUG_ERROR("sync_agent_run_noti_listener() failed !! - err : %d", err);
71                 result = UI_SYNC_AGENT_DM_FAIL;
72         }
73
74         int noti_id = NOTI_DOWNLOAD;
75         for (; noti_id < NOTI_ALERT_DISPLAY; noti_id++) {
76                 err = sync_agent_set_noti_callback(noti_id, _get_fota_noti_callback(noti_id), NULL);
77                 if (err != SYNC_AGENT_EVENT_SUCCESS) {
78                         _DEBUG_ERROR("sync_agent_set_noti_callback(noti_id : %d) failed - err : %d !!", noti_id, err);
79                         result = UI_SYNC_AGENT_DM_FAIL;
80                         break;
81                 }
82         }
83
84         _EXTERN_FUNC_EXIT;
85
86         return result;
87 }
88
89 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_deinit()
90 {
91         _EXTERN_FUNC_ENTER;
92
93         sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
94
95         sync_agent_deinit_error_e deinit = SYNC_AGENT_DEINIT_SUCCESS;
96
97         deinit = sync_agent_deinit();
98         if (deinit != SYNC_AGENT_DEINIT_SUCCESS) {
99                 _DEBUG_ERROR("sync_agent_deinit() failed !!");
100                 result = UI_SYNC_AGENT_DM_FAIL;
101         }
102         /*
103            sync_agent_event_error_e err = sync_agent_stop_noti_listener();
104            if (err != SYNC_AGENT_EVENT_SUCCESS) {
105            _DEBUG_INFO("sync_agent_stop_noti_listener() failed !! - err : %d", err);
106            result = UI_SYNC_AGENT_DM_FAIL;
107            }
108
109            err = sync_agent_clean_event_handler();
110            if (err != SYNC_AGENT_EVENT_SUCCESS) {
111            _DEBUG_ERROR("sync_agent_clean_event_handler() failed !! - err : %d", err);
112            result = UI_SYNC_AGENT_DM_FAIL;
113            }
114          */
115
116         _EXTERN_FUNC_EXIT;
117
118         return result;
119 }
120
121 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_software_update(sync_agent_dm_service_server_type_e server_type)
122 {
123         _EXTERN_FUNC_ENTER;
124
125         sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
126
127         if (_fota_event_interface(EVENT_SOFTWARE_UPDATE, server_type) != SYNC_AGENT_DM_API_RESULT_SUCCESS) {
128                 _DEBUG_ERROR("_fota_event_interface(event num : %d) failed !!", EVENT_SOFTWARE_UPDATE);
129                 result = UI_SYNC_AGENT_DM_FAIL;
130         } else {
131                 _DEBUG_VERBOSE("_fota_event_interface(event num : %d) success !!", EVENT_SOFTWARE_UPDATE);
132         }
133
134         _EXTERN_FUNC_EXIT;
135
136         return result;
137 }
138
139 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_cancel()
140 {
141         _EXTERN_FUNC_ENTER;
142
143         sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
144
145         if (_fota_event_interface(EVENT_CANCEL, 0) != SYNC_AGENT_DM_API_RESULT_SUCCESS) {
146                 _DEBUG_ERROR("_fota_event_interface(event num : %d) failed !!", EVENT_CANCEL);
147                 result = UI_SYNC_AGENT_DM_FAIL;
148         } else {
149                 _DEBUG_VERBOSE("_fota_event_interface(event num : %d) success !!", EVENT_CANCEL);
150         }
151
152         _EXTERN_FUNC_EXIT;
153
154         return result;
155 }
156
157 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_download(int response_type)
158 {
159         _EXTERN_FUNC_ENTER;
160
161         sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
162
163         if (_fota_event_interface(EVENT_DOWNLOAD, response_type) != SYNC_AGENT_DM_API_RESULT_SUCCESS) {
164                 _DEBUG_ERROR("_fota_event_interface(event num : %d) failed !!", EVENT_DOWNLOAD);
165                 result = UI_SYNC_AGENT_DM_FAIL;
166         } else {
167                 _DEBUG_VERBOSE("_fota_event_interface(event num : %d) success !!", EVENT_DOWNLOAD);
168         }
169
170         _EXTERN_FUNC_EXIT;
171
172         return result;
173 }
174
175 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_install(int response_type)
176 {
177         _EXTERN_FUNC_ENTER;
178
179         sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
180
181         if (_fota_event_interface(EVENT_INSTALL, response_type) != SYNC_AGENT_DM_API_RESULT_SUCCESS) {
182                 _DEBUG_ERROR("_fota_event_interface(event num : %d) failed !!", EVENT_INSTALL);
183                 result = UI_SYNC_AGENT_DM_FAIL;
184         } else {
185                 _DEBUG_VERBOSE("_fota_event_interface(event num : %d) success !!", EVENT_INSTALL);
186         }
187
188         _EXTERN_FUNC_EXIT;
189
190         return result;
191 }
192
193 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_set_interval(int interval_type)
194 {
195         _EXTERN_FUNC_ENTER;
196
197         sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
198
199         if (_fota_event_interface(EVENT_SET_INTERVAL, interval_type) != SYNC_AGENT_DM_API_RESULT_SUCCESS) {
200                 _DEBUG_ERROR("_fota_event_interface(event num : %d) failed !!", EVENT_SET_INTERVAL);
201                 result = UI_SYNC_AGENT_DM_FAIL;
202         } else {
203                 _DEBUG_VERBOSE("_fota_event_interface(event num : %d) success !!", EVENT_SET_INTERVAL);
204         }
205
206         _EXTERN_FUNC_EXIT;
207
208         return result;
209 }
210
211 static int (*_get_fota_noti_callback(int noti_id)) (sync_agent_event_data_s * request, void *data) {
212         _EXTERN_FUNC_ENTER;
213
214         _DEBUG_INFO("start (noti_type : %d)", noti_id);
215
216         _EXTERN_FUNC_EXIT;
217
218         switch (noti_id) {
219         case NOTI_DOWNLOAD:
220                 return __download_noti_cb;
221                 break;
222         case NOTI_INSTALL:
223                 return __install_noti_cb;
224                 break;
225         case NOTI_DOWNLOAD_INFO:
226                 return __download_info_noti_cb;
227                 break;
228         case NOTI_ENGINE_START:
229                 return __engine_start_noti_cb;
230                 break;
231         case NOTI_ENGINE_FAIL:
232                 return __engine_fail_noti_cb;
233                 break;
234         case NOTI_WIFI_ONLY_DOWNLOAD_FAIL:
235                 return __wifi_only_download_fail_noti_cb;
236                 break;
237         case NOTI_MEMORY_FULL:
238                 return __memory_full_noti_cb;
239                 break;
240         case NOTI_OVER_BIG_SIZE:
241                 return __over_big_size_noti_cb;
242                 break;
243         case NOTI_LOW_BATTERY:
244                 return __low_battery_noti_cb;
245                 break;
246         default:
247                 return __default_noti_cb;
248                 break;
249         }
250
251         return 0;
252 }
253
254 static int __download_noti_cb(sync_agent_event_data_s * request, void *data)
255 {
256         _INNER_FUNC_ENTER;
257
258         _INNER_FUNC_EXIT;
259
260         return 0;
261 }
262
263 static int __install_noti_cb(sync_agent_event_data_s * request, void *data)
264 {
265         _INNER_FUNC_ENTER;
266
267         _INNER_FUNC_EXIT;
268
269         return 0;
270 }
271
272 static int __download_info_noti_cb(sync_agent_event_data_s * request, void *data)
273 {
274         _INNER_FUNC_ENTER;
275
276         int file_size = 0;
277         char *file_path = NULL;
278
279         sync_agent_get_event_data_param(request, &file_size);
280         sync_agent_get_event_data_param(request, &file_path);
281         sync_agent_get_event_data_param(request, &task_id);
282
283         _DEBUG_VERBOSE("file_size : %d", file_size);
284         _DEBUG_VERBOSE("file_path : %s", file_path);
285         _DEBUG_VERBOSE("task id : %d", task_id);
286
287         off_t return_stat;
288         struct stat file_info;
289         long int down_size = 0;
290         double percent = 0.0;
291
292         while (percent != 1.0) {
293                 if ((return_stat = stat(file_path, &file_info)) == -1) {
294                         _DEBUG_ERROR("Error: get file size !!");
295                         g_free(file_path);
296                         return 0;
297                 }
298                 down_size = (long int)(file_info.st_size);
299                 percent = ((double)down_size) / ((double)file_size);
300                 _DEBUG_VERBOSE("download percent : %d", (int)(percent * 100));
301                 usleep(100000);
302         }
303
304         _DEBUG_VERBOSE("download complete !!");
305
306         g_free(file_path);
307
308         if (request != NULL) {
309                 if (request->size != NULL)
310                         g_free(request->size);
311                 g_free(request);
312         }
313
314         _INNER_FUNC_EXIT;
315
316         return 0;
317 }
318
319 static int __engine_start_noti_cb(sync_agent_event_data_s * request, void *data)
320 {
321         _INNER_FUNC_ENTER;
322
323         sync_agent_get_event_data_param(request, &task_id);
324
325         _DEBUG_VERBOSE("task id : %d", task_id);
326
327         if (request != NULL) {
328                 if (request->size != NULL)
329                         g_free(request->size);
330                 g_free(request);
331         }
332
333         _INNER_FUNC_EXIT;
334
335         return 0;
336 }
337
338 static int __engine_fail_noti_cb(sync_agent_event_data_s * request, void *data)
339 {
340         _INNER_FUNC_ENTER;
341
342         int prev_noti_id = -1;
343         int fail_type = -1;
344
345         sync_agent_get_event_data_param(request, &prev_noti_id);
346         sync_agent_get_event_data_param(request, &fail_type);
347
348         _DEBUG_VERBOSE("previous noti id : %d", prev_noti_id);
349         _DEBUG_VERBOSE("fail type : %d", fail_type);
350
351         if (request != NULL) {
352                 if (request->size != NULL)
353                         g_free(request->size);
354                 g_free(request);
355         }
356
357         _INNER_FUNC_EXIT;
358
359         return 0;
360 }
361
362 static int __wifi_only_download_fail_noti_cb(sync_agent_event_data_s * request, void *data)
363 {
364         _INNER_FUNC_ENTER;
365
366         _INNER_FUNC_EXIT;
367
368         return 0;
369 }
370
371 static int __memory_full_noti_cb(sync_agent_event_data_s * request, void *data)
372 {
373         _INNER_FUNC_ENTER;
374
375         int file_size = 0;
376
377         sync_agent_get_event_data_param(request, &file_size);
378
379         _DEBUG_VERBOSE("file_size : %d", file_size);
380
381         if (request != NULL) {
382                 if (request->size != NULL)
383                         g_free(request->size);
384                 g_free(request);
385         }
386
387         _INNER_FUNC_EXIT;
388
389         return 0;
390 }
391
392 static int __over_big_size_noti_cb(sync_agent_event_data_s * request, void *data)
393 {
394         _INNER_FUNC_ENTER;
395
396         _INNER_FUNC_EXIT;
397
398         return 0;
399 }
400
401 static int __low_battery_noti_cb(sync_agent_event_data_s * request, void *data)
402 {
403         _INNER_FUNC_ENTER;
404
405         char *battery_level = NULL;
406
407         sync_agent_get_event_data_param(request, &battery_level);
408
409         _DEBUG_VERBOSE("battery level : %s", battery_level);
410
411         g_free(battery_level);
412
413         if (request != NULL) {
414                 if (request->size != NULL)
415                         g_free(request->size);
416                 g_free(request);
417         }
418
419         _INNER_FUNC_EXIT;
420
421         return 0;
422 }
423
424 static int __default_noti_cb(sync_agent_event_data_s * request, void *data)
425 {
426         _INNER_FUNC_ENTER;
427
428         _DEBUG_ERROR("noti_id flow is [%s]", "default");
429
430         _INNER_FUNC_EXIT;
431
432         return 0;
433 }
434
435 static int _fota_event_interface(sync_agent_dm_event_e event_num, int user_data)
436 {
437         _EXTERN_FUNC_ENTER;
438
439         int api_result = SYNC_AGENT_DM_API_RESULT_SUCCESS;
440         sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
441         sync_agent_event_data_s *request_event = NULL;
442         sync_agent_event_data_s *response_event = NULL;
443
444         request_event = sync_agent_create_event(event_num);
445         if (request_event == NULL) {
446                 _DEBUG_ERROR("request_event is NULL !!");
447                 _EXTERN_FUNC_EXIT;
448                 return SYNC_AGENT_DM_API_RESULT_FAILURE;
449         }
450
451         _DEBUG_VERBOSE("event num : %d", event_num);
452
453         switch (event_num) {
454         case EVENT_SOFTWARE_UPDATE:
455                 {
456                         sync_agent_dm_ui_type_e ui_type = FOTA_COMMON_UI;
457                         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &ui_type);
458                         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &task_id);
459                         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &user_data);
460
461                         _DEBUG_VERBOSE("ui_type : FOTA_COMMON_UI");
462                         _DEBUG_VERBOSE("task_id : %d", task_id);
463                 }
464                 break;
465         case EVENT_CANCEL:
466                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &task_id);
467
468                 _DEBUG_VERBOSE("task_id : %d", task_id);
469                 break;
470         case EVENT_DOWNLOAD:
471         case EVENT_INSTALL:
472         case EVENT_SET_INTERVAL:
473                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &user_data);
474                 break;
475         default:
476                 break;
477         }
478
479         _DEBUG_VERBOSE("user_data : %d", user_data);
480
481         response_event = sync_agent_send_event(request_event, &error);
482         if (error != SYNC_AGENT_EVENT_SUCCESS) {
483                 _DEBUG_ERROR("sync_agent_send_event() failed !! - err = %d", error);
484                 sync_agent_free_event(request_event);
485                 _EXTERN_FUNC_EXIT;
486                 return SYNC_AGENT_DM_API_RESULT_FAILURE;
487         }
488
489         if (response_event == NULL) {
490                 _DEBUG_ERROR("response_event is NULL !!");
491                 sync_agent_free_event(request_event);
492                 _EXTERN_FUNC_EXIT;
493                 return SYNC_AGENT_DM_API_RESULT_FAILURE;
494         }
495
496         sync_agent_get_event_data_param(response_event, &api_result);
497         if (api_result != SYNC_AGENT_DM_API_RESULT_SUCCESS) {
498                 _DEBUG_ERROR("api_result : failed !!");
499                 sync_agent_free_event(request_event);
500                 sync_agent_free_event_data(response_event);
501                 _EXTERN_FUNC_EXIT;
502                 return SYNC_AGENT_DM_API_RESULT_FAILURE;
503         }
504
505         _DEBUG_VERBOSE("api_result : %d", api_result);
506
507         /* free request & response event */
508         sync_agent_free_event(request_event);
509         sync_agent_free_event_data(response_event);
510
511         _EXTERN_FUNC_EXIT;
512
513         return api_result;
514 }