3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 //#define COMPONENT_TAG APP
23 #ifndef SYNC_AGENT_LOG
25 #define LOG_TAG "OMADM_CLIENT"
28 #include <sync_agent.h>
30 #include "oma_dm_fota_api.h"
31 #include "oma_dm_api_common_internal.h"
34 #define EXPORT_API __attribute__ ((visibility("default")))
37 #define OMA_DM_FOTA_CONFIG_FILE "/usr/share/oma-dm-cfg/fw-init/omadmfotaui_fw_config.xml"
39 static int (*_get_fota_noti_callback(int noti_id)) (sync_agent_event_data_s * request, void *data);
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);
52 static int _fota_event_interface(sync_agent_dm_event_e event_num, int user_data);
54 static int task_id = 0;
56 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_init()
60 sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
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;
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;
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;
89 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_deinit()
93 sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
95 sync_agent_deinit_error_e deinit = SYNC_AGENT_DEINIT_SUCCESS;
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;
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;
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;
121 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_software_update(sync_agent_dm_service_server_type_e server_type)
125 sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
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;
131 _DEBUG_VERBOSE("_fota_event_interface(event num : %d) success !!", EVENT_SOFTWARE_UPDATE);
139 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_cancel()
143 sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
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;
149 _DEBUG_VERBOSE("_fota_event_interface(event num : %d) success !!", EVENT_CANCEL);
157 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_download(int response_type)
161 sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
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;
167 _DEBUG_VERBOSE("_fota_event_interface(event num : %d) success !!", EVENT_DOWNLOAD);
175 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_install(int response_type)
179 sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
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;
185 _DEBUG_VERBOSE("_fota_event_interface(event num : %d) success !!", EVENT_INSTALL);
193 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_set_interval(int interval_type)
197 sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
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;
203 _DEBUG_VERBOSE("_fota_event_interface(event num : %d) success !!", EVENT_SET_INTERVAL);
211 static int (*_get_fota_noti_callback(int noti_id)) (sync_agent_event_data_s * request, void *data) {
214 _DEBUG_INFO("start (noti_type : %d)", noti_id);
220 return __download_noti_cb;
223 return __install_noti_cb;
225 case NOTI_DOWNLOAD_INFO:
226 return __download_info_noti_cb;
228 case NOTI_ENGINE_START:
229 return __engine_start_noti_cb;
231 case NOTI_ENGINE_FAIL:
232 return __engine_fail_noti_cb;
234 case NOTI_WIFI_ONLY_DOWNLOAD_FAIL:
235 return __wifi_only_download_fail_noti_cb;
237 case NOTI_MEMORY_FULL:
238 return __memory_full_noti_cb;
240 case NOTI_OVER_BIG_SIZE:
241 return __over_big_size_noti_cb;
243 case NOTI_LOW_BATTERY:
244 return __low_battery_noti_cb;
247 return __default_noti_cb;
254 static int __download_noti_cb(sync_agent_event_data_s * request, void *data)
263 static int __install_noti_cb(sync_agent_event_data_s * request, void *data)
272 static int __download_info_noti_cb(sync_agent_event_data_s * request, void *data)
277 char *file_path = NULL;
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);
283 _DEBUG_VERBOSE("file_size : %d", file_size);
284 _DEBUG_VERBOSE("file_path : %s", file_path);
285 _DEBUG_VERBOSE("task id : %d", task_id);
288 struct stat file_info;
289 long int down_size = 0;
290 double percent = 0.0;
292 while (percent != 1.0) {
293 if ((return_stat = stat(file_path, &file_info)) == -1) {
294 _DEBUG_ERROR("Error: get file size !!");
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));
304 _DEBUG_VERBOSE("download complete !!");
308 if (request != NULL) {
309 if (request->size != NULL)
310 g_free(request->size);
319 static int __engine_start_noti_cb(sync_agent_event_data_s * request, void *data)
323 sync_agent_get_event_data_param(request, &task_id);
325 _DEBUG_VERBOSE("task id : %d", task_id);
327 if (request != NULL) {
328 if (request->size != NULL)
329 g_free(request->size);
338 static int __engine_fail_noti_cb(sync_agent_event_data_s * request, void *data)
342 int prev_noti_id = -1;
345 sync_agent_get_event_data_param(request, &prev_noti_id);
346 sync_agent_get_event_data_param(request, &fail_type);
348 _DEBUG_VERBOSE("previous noti id : %d", prev_noti_id);
349 _DEBUG_VERBOSE("fail type : %d", fail_type);
351 if (request != NULL) {
352 if (request->size != NULL)
353 g_free(request->size);
362 static int __wifi_only_download_fail_noti_cb(sync_agent_event_data_s * request, void *data)
371 static int __memory_full_noti_cb(sync_agent_event_data_s * request, void *data)
377 sync_agent_get_event_data_param(request, &file_size);
379 _DEBUG_VERBOSE("file_size : %d", file_size);
381 if (request != NULL) {
382 if (request->size != NULL)
383 g_free(request->size);
392 static int __over_big_size_noti_cb(sync_agent_event_data_s * request, void *data)
401 static int __low_battery_noti_cb(sync_agent_event_data_s * request, void *data)
405 char *battery_level = NULL;
407 sync_agent_get_event_data_param(request, &battery_level);
409 _DEBUG_VERBOSE("battery level : %s", battery_level);
411 g_free(battery_level);
413 if (request != NULL) {
414 if (request->size != NULL)
415 g_free(request->size);
424 static int __default_noti_cb(sync_agent_event_data_s * request, void *data)
428 _DEBUG_ERROR("noti_id flow is [%s]", "default");
435 static int _fota_event_interface(sync_agent_dm_event_e event_num, int user_data)
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;
444 request_event = sync_agent_create_event(event_num);
445 if (request_event == NULL) {
446 _DEBUG_ERROR("request_event is NULL !!");
448 return SYNC_AGENT_DM_API_RESULT_FAILURE;
451 _DEBUG_VERBOSE("event num : %d", event_num);
454 case EVENT_SOFTWARE_UPDATE:
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);
461 _DEBUG_VERBOSE("ui_type : FOTA_COMMON_UI");
462 _DEBUG_VERBOSE("task_id : %d", task_id);
466 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &task_id);
468 _DEBUG_VERBOSE("task_id : %d", task_id);
472 case EVENT_SET_INTERVAL:
473 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &user_data);
479 _DEBUG_VERBOSE("user_data : %d", user_data);
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);
486 return SYNC_AGENT_DM_API_RESULT_FAILURE;
489 if (response_event == NULL) {
490 _DEBUG_ERROR("response_event is NULL !!");
491 sync_agent_free_event(request_event);
493 return SYNC_AGENT_DM_API_RESULT_FAILURE;
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);
502 return SYNC_AGENT_DM_API_RESULT_FAILURE;
505 _DEBUG_VERBOSE("api_result : %d", api_result);
507 /* free request & response event */
508 sync_agent_free_event(request_event);
509 sync_agent_free_event_data(response_event);