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_noti_api.h"
31 #include "oma_dm_api_common_internal.h"
34 #define EXPORT_API __attribute__ ((visibility("default")))
37 #define OMA_DM_PUSH_CONFIG_FILE "/usr/share/oma-dm-cfg/fw-init/omadmnotiui_fw_config.xml"
39 static int (*_get_push_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 _push_event_interface(sync_agent_dm_event_e event_num, int user_data, char *server_id, char *session_id);
54 static int task_id = 0;
58 PUSH_NOT_SPECIFIED = 0,
62 } sync_agent_dm_push_type;
64 EXPORT_API sync_agent_dm_error_e sync_agent_dm_push_init()
68 sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
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;
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;
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;
97 EXPORT_API sync_agent_dm_error_e sync_agent_dm_push_deinit()
101 sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
103 sync_agent_deinit_error_e deinit = SYNC_AGENT_DEINIT_SUCCESS;
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;
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;
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;
129 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_update_noti(char *server_id, char *session_id)
133 retvm_if((session_id == NULL || server_id == NULL), UI_SYNC_AGENT_DM_FAIL, "session id is null or server id is null ");
135 sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
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;
141 _DEBUG_VERBOSE("_push_event_interface(event num : %d) success !!", EVENT_SOFTWARE_UPDATE);
149 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_cancel_noti()
153 sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
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;
159 _DEBUG_VERBOSE("_push_event_interface(event num : %d) success !!", EVENT_CANCEL);
167 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_download_noti(int response_type)
171 sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
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;
177 _DEBUG_VERBOSE("_push_event_interface(event num : %d) success !!", EVENT_DOWNLOAD);
185 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_install_noti(int response_type)
189 sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
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;
195 _DEBUG_VERBOSE("_push_event_interface(event num : %d) success !!", EVENT_INSTALL);
203 EXPORT_API sync_agent_dm_error_e sync_agent_dm_fota_set_interval_noti(int interval_type)
207 sync_agent_dm_error_e result = UI_SYNC_AGENT_DM_SUCCESS;
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;
213 _DEBUG_VERBOSE("_push_event_interface(event num : %d) success !!", EVENT_SET_INTERVAL);
221 static int (*_get_push_noti_callback(int noti_id)) (sync_agent_event_data_s * request, void *data) {
224 _DEBUG_INFO("start (noti_type : %d)", noti_id);
230 return __download_noti_cb;
233 return __install_noti_cb;
235 case NOTI_DOWNLOAD_INFO:
236 return __download_info_noti_cb;
238 case NOTI_ENGINE_START:
239 return __engine_start_noti_cb;
241 case NOTI_ENGINE_FAIL:
242 return __engine_fail_noti_cb;
244 case NOTI_WIFI_ONLY_DOWNLOAD_FAIL:
245 return __wifi_only_download_fail_noti_cb;
247 case NOTI_MEMORY_FULL:
248 return __memory_full_noti_cb;
250 case NOTI_OVER_BIG_SIZE:
251 return __over_big_size_noti_cb;
253 case NOTI_LOW_BATTERY:
254 return __low_battery_noti_cb;
257 return __default_noti_cb;
264 static int __download_noti_cb(sync_agent_event_data_s * request, void *data)
273 static int __install_noti_cb(sync_agent_event_data_s * request, void *data)
282 static int __download_info_noti_cb(sync_agent_event_data_s * request, void *data)
287 char *file_path = NULL;
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);
293 _DEBUG_VERBOSE("file_size : %d", file_size);
294 _DEBUG_VERBOSE("file_path : %s", file_path);
295 _DEBUG_VERBOSE("task id : %d", task_id);
298 struct stat file_info;
299 long int down_size = 0;
300 double percent = 0.0;
302 while (percent != 1.0) {
303 if ((return_stat = stat(file_path, &file_info)) == -1) {
304 _DEBUG_ERROR("Error: get file size !!");
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));
314 _DEBUG_VERBOSE("download complete !!");
318 if (request != NULL) {
319 if (request->size != NULL)
320 g_free(request->size);
329 static int __engine_start_noti_cb(sync_agent_event_data_s * request, void *data)
333 sync_agent_get_event_data_param(request, &task_id);
335 _DEBUG_VERBOSE("task id : %d", task_id);
337 if (request != NULL) {
338 if (request->size != NULL)
339 g_free(request->size);
348 static int __engine_fail_noti_cb(sync_agent_event_data_s * request, void *data)
352 int prev_noti_id = -1;
355 sync_agent_get_event_data_param(request, &prev_noti_id);
356 sync_agent_get_event_data_param(request, &fail_type);
358 _DEBUG_VERBOSE("previous noti id : %d", prev_noti_id);
359 _DEBUG_VERBOSE("fail type : %d", fail_type);
361 if (request != NULL) {
362 if (request->size != NULL)
363 g_free(request->size);
372 static int __wifi_only_download_fail_noti_cb(sync_agent_event_data_s * request, void *data)
381 static int __memory_full_noti_cb(sync_agent_event_data_s * request, void *data)
387 sync_agent_get_event_data_param(request, &file_size);
389 _DEBUG_VERBOSE("file_size : %d", file_size);
391 if (request != NULL) {
392 if (request->size != NULL)
393 g_free(request->size);
402 static int __over_big_size_noti_cb(sync_agent_event_data_s * request, void *data)
411 static int __low_battery_noti_cb(sync_agent_event_data_s * request, void *data)
415 char *battery_level = NULL;
417 sync_agent_get_event_data_param(request, &battery_level);
419 _DEBUG_VERBOSE("battery level : %s", battery_level);
421 g_free(battery_level);
423 if (request != NULL) {
424 if (request->size != NULL)
425 g_free(request->size);
434 static int __default_noti_cb(sync_agent_event_data_s * request, void *data)
438 _DEBUG_ERROR("noti_id flow is [%s]", "default");
445 static int _push_event_interface(sync_agent_dm_event_e event_num, int user_data, char *server_id, char *session_id)
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;
456 request_event = sync_agent_create_event(event_num);
457 if (request_event == NULL) {
458 _DEBUG_ERROR("request_event is NULL !!");
460 return SYNC_AGENT_DM_API_RESULT_FAILURE;
463 _DEBUG_VERBOSE("event num : %d", event_num);
466 case EVENT_SOFTWARE_UPDATE:
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);
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);
481 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &task_id);
483 _DEBUG_VERBOSE("task_id : %d", task_id);
487 case EVENT_SET_INTERVAL:
488 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &user_data);
490 _DEBUG_VERBOSE("user_data : %d", user_data);
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);
501 return SYNC_AGENT_DM_API_RESULT_FAILURE;
504 if (response_event == NULL) {
505 _DEBUG_ERROR("response_event is NULL !!");
506 sync_agent_free_event(request_event);
508 return SYNC_AGENT_DM_API_RESULT_FAILURE;
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);
517 return SYNC_AGENT_DM_API_RESULT_FAILURE;
520 _DEBUG_VERBOSE("api_result : %d", api_result);
522 /* free request & response event */
523 if (event_num == EVENT_SOFTWARE_UPDATE) {
524 free(real_session_id);
525 free(real_server_id);
527 sync_agent_free_event(request_event);
528 sync_agent_free_event_data(response_event);