diff options
Diffstat (limited to 'src/agent/common/util/util.c')
-rw-r--r-- | src/agent/common/util/util.c | 490 |
1 files changed, 490 insertions, 0 deletions
diff --git a/src/agent/common/util/util.c b/src/agent/common/util/util.c new file mode 100644 index 0000000..0623576 --- /dev/null +++ b/src/agent/common/util/util.c @@ -0,0 +1,490 @@ +/* + * oma-dm-agent + * Copyright (c) 2012 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/*lib*/ +#include <aul.h> +#include <unistd.h> +#include <appsvc.h> +#include <glib.h> + +/*sync-agent*/ +#include <sync_agent.h> + +/*dm-agent*/ +#include "common/dm_common.h" +#include "common/util/util.h" +#include "ipc_agent.h" +#include "ipc_common.h" + +#ifndef OMADM_AGENT_LOG +#undef LOG_TAG +#define LOG_TAG "OMA_DM_UTIL" +#endif + +static void _update_csc(); + +void str_free(char **str) +{ + _EXTERN_FUNC_ENTER; + + if (*str == NULL) { + _EXTERN_FUNC_EXIT; + return; + } else { + free(*str); + *str = NULL; + } + + _EXTERN_FUNC_EXIT; +} + +int chartoint(char *data, int *value) +{ + _EXTERN_FUNC_ENTER; + + retvm_if(data == NULL, 0, "data is NULL!!"); + + if (data == NULL) { + _EXTERN_FUNC_EXIT; + return 0; + } else { + (*value) = atoi(data); + _EXTERN_FUNC_EXIT; + return 1; + } + + _EXTERN_FUNC_EXIT; + return 0; +} + +int launch_oma_dm_fumo_ui() +{ + _EXTERN_FUNC_ENTER; + + int result = 0; + _DEBUG_INFO("\n---------------------------------------------------: %d\n", result); + _DEBUG_INFO("\n is launch oma fumo dm ui : %d ?\n", result); + result = aul_app_is_running(OMA_DM_FUMO_UI_PKG); + _DEBUG_INFO("\n Is launch oma fumo dm ui , result : %d\n", result); + _DEBUG_INFO("\n---------------------------------------------------: %d \n", result); + + if (result == 0) { + bundle *pBundle = NULL; + pBundle = bundle_create(); + if (pBundle == NULL) { + _DEBUG_INFO("bundle_create fail\n"); + _EXTERN_FUNC_EXIT; + return 0; + } + result = bundle_add(pBundle, "FOTA_UI", "3"); + if (result == -1) { + bundle_free(pBundle); + _DEBUG_INFO("bundle_add fail\n"); + _EXTERN_FUNC_EXIT; + return 0; + } + + result = aul_launch_app(OMA_DM_FUMO_UI_PKG, pBundle); + _DEBUG_INFO("result : %d\n", result); + if (result == -1) { + bundle_free(pBundle); + _EXTERN_FUNC_EXIT; + return 0; + } + bundle_free(pBundle); + sleep(2); + } else { + _DEBUG_INFO("---------------------------------------------------------------------------------------------------------------------------------"); + _DEBUG_INFO("existed oma dm ui\n"); + _DEBUG_INFO("---------------------------------------------------------------------------------------------------------------------------------"); + } + _EXTERN_FUNC_EXIT; + return 1; +} + +int launch_om_dm_fumo_noti_ui(int noti_data, char *session_id, char *server_id) +{ + _EXTERN_FUNC_ENTER; + + int result = 0; + _DEBUG_INFO("\n---------------------------------------------------: %d\n", result); + _DEBUG_INFO("\n is launch oma noti dm ui ? : %d\n", result); + result = aul_app_is_running(OMA_DM_FUMO_NOTI_UI_PKG); + _DEBUG_INFO("\n Is launch oma noti dm ui , result : %d\n", result); + _DEBUG_INFO("\n---------------------------------------------------: %d\n", result); + + if (result == 0) { + char *noti_type = NULL; + noti_type = g_strdup_printf("%d", noti_data); + + bundle *pBundle = NULL; + pBundle = bundle_create(); + if (pBundle == NULL) { + _DEBUG_INFO("bundle_create fail\n"); + _EXTERN_FUNC_EXIT; + return 0; + } + + result = bundle_add(pBundle, "FOTA_NOTI", noti_type); + result = bundle_add(pBundle, "FOTA_SESSIONID", session_id); + result = bundle_add(pBundle, "FOTA_SERVERID", server_id); + + if (session_id == NULL) { + /*if engine stoped by battery off or other exceptions */ + result = bundle_add(pBundle, "FOTA_SESSIONID", "1111"); + } + if (server_id == NULL) { + result = bundle_add(pBundle, "FOTA_SERVERID", "1111"); + } + if (result == -1) { + _DEBUG_INFO("bundle_add fail\n"); + bundle_free(pBundle); + _EXTERN_FUNC_EXIT; + return 0; + } + + result = aul_launch_app(OMA_DM_FUMO_NOTI_UI_PKG, pBundle); + _DEBUG_INFO("result : %d\n", result); + + if (result == -1) { + bundle_free(pBundle); + _EXTERN_FUNC_EXIT; + return 0; + } + + str_free(¬i_type); + bundle_free(pBundle); + sleep(2); + } else { + _DEBUG_INFO("---------------------------------------------------------------------------------------------------------------------------------"); + _DEBUG_INFO("existed oma dm noti ui\n"); + _DEBUG_INFO("---------------------------------------------------------------------------------------------------------------------------------"); + } + _EXTERN_FUNC_EXIT; + return 1; +} + +int launch_om_dm_fumo_alert_ui() +{ + _EXTERN_FUNC_ENTER; + + int ret = 0; + _DEBUG_INFO("OMA_DM_ALERT_UI_PKG : %s", OMA_DM_ALERT_UI_PKG); + ret = aul_app_is_running(OMA_DM_ALERT_UI_PKG); + _DEBUG_INFO("aul_app_is_running(%s) [%d] ", OMA_DM_ALERT_UI_PKG, ret); + if (ret == 0) { + bundle *b = bundle_create(); + //appsvc_set_operation(b, APPSVC_OPERATION_DEFAULT); + //appsvc_set_pkgname(b, OMA_DM_ALERT_UI_PKG); + + //ret = appsvc_run_service(b, 0, NULL, NULL); + ret = aul_launch_app(OMA_DM_ALERT_UI_PKG, b); + _DEBUG_INFO("result : %d\n", ret); + + _DEBUG_INFO("oma dm alert ui run : %d\n", ret); + bundle_free(b); + if (ret >= 0) { + sleep(2); + _EXTERN_FUNC_EXIT; + return 1; + } else { + _EXTERN_FUNC_EXIT; + return 0; + } + } else { + return 1; + } + _EXTERN_FUNC_EXIT; + return 0; +} + +int launch_oma_dm_cp_ui(int sec_type, int ext_id) +{ + _EXTERN_FUNC_ENTER; + + _DEBUG_INFO("Start !!"); + int result = 0; + _DEBUG_INFO("OMA_DM_CP_UI_PKG : %s", OMA_DM_CP_UI_PKG); + _DEBUG_INFO("\n---------------------------------------------------\n"); + _DEBUG_INFO("\n is launch oma dm cp ui ?\n"); + result = aul_app_is_running(OMA_DM_CP_UI_PKG); + _DEBUG_INFO("\n Is launch oma dm cp ui, result : %d\n", result); + _DEBUG_INFO("\n---------------------------------------------------\n"); + + if (result == 0) { + char *pin_type = NULL; + pin_type = g_strdup_printf("%d", sec_type); + char *extId = NULL; + extId = g_strdup_printf("%d", ext_id); + + bundle *pBundle = NULL; + pBundle = bundle_create(); + if (pBundle == NULL) { + _DEBUG_INFO("bundle_create fail\n"); + _EXTERN_FUNC_EXIT; + return 0; + } + + result = bundle_add(pBundle, "PIN_TYPE", pin_type); + if (result == -1) { + _DEBUG_INFO("bundle_add fail\n"); + bundle_free(pBundle); + _EXTERN_FUNC_EXIT; + return 0; + } + + result = bundle_add(pBundle, "EXT_ID", extId); + if (result == -1) { + _DEBUG_INFO("bundle_add fail\n"); + bundle_free(pBundle); + _EXTERN_FUNC_EXIT; + return 0; + } + + result = aul_launch_app(OMA_DM_CP_UI_PKG, pBundle); + _DEBUG_INFO("result : %d\n", result); + + if (result == -1) { + _EXTERN_FUNC_EXIT; + bundle_free(pBundle); + return 0; + } + + str_free(&pin_type); + bundle_free(pBundle); + sleep(2); + } else { + _DEBUG_INFO("---------------------------------------------------------------------------------------------------------------------------------"); + _DEBUG_INFO("existed oma dm cp ui\n"); + _DEBUG_INFO("---------------------------------------------------------------------------------------------------------------------------------"); + } + _EXTERN_FUNC_EXIT; + return 1; +} + +void terminate_oma_dm_ui(int result_status, ENGINE_ID service_engine_id) +{ + _EXTERN_FUNC_ENTER; + + if (result_status == DM_ERR_REMINDER_INTERVAL || result_status == DM_DOWNLOAD_POPUP || result_status == DM_INSTALL_POPUP || result_status == DM_RESUME_IDLE) { + _DEBUG_INFO("result status : %d", result_status); + return; + } + + _DEBUG_INFO("start"); + + DM_ERROR ret = DM_OK; + int isfumo_engine = 0; + engine_status *status = NULL; + int result = 0; + bool cancel_flag = 0; + + cancel_flag = sync_agent_check_cancel_flag(); + if (cancel_flag) { + _EXTERN_FUNC_EXIT; + return; + } + isfumo_engine = IsExist_Engine_id(FUMO_SERVICE_ENGINE); + + if (isfumo_engine == 1) { + if (service_engine_id == FUMO_SERVICE_ENGINE) { + _DEBUG_INFO("existed engine id in db"); + ret = Get_Engine_Status(FUMO_SERVICE_ENGINE, &status); + _DEBUG_INFO("get fumo service engine : %d", ret); + + if (status != NULL && status->ui_mode != NULL) { + result = aul_app_is_running(OMA_DM_FUMO_NOTI_UI_PKG); + if (result == 1) { + terminate_ui_status(status->ui_noti_type, status->ui_mode, result_status); + } + result = aul_app_is_running(OMA_DM_FUMO_UI_PKG); + if (result == 1) { + terminate_ui_status(status->ui_noti_type, status->ui_mode, result_status); + } + } else { + _DEBUG_INFO("fumo service ui mode not existed"); + } + + if (status != NULL) + Free_Memory_Engine_Status(&status, 1); + + ret = Delete_Engine_Status(FUMO_SERVICE_ENGINE); + _DEBUG_INFO("delete engine status : %d", ret); + } else { + _DEBUG_INFO("fumo service not existed"); + } + } else { + _DEBUG_INFO("fumo service not existed"); + } + _EXTERN_FUNC_EXIT; + +} + +void terminate_ui_status(int noti_type, char *ui_mode, int result_status) +{ + _EXTERN_FUNC_ENTER; + + int noti_res = 0; + + _DEBUG_INFO("noti type : %d", noti_type); + /*noti_res = noti_engine_fail(noti_type, fail_type); */ + + switch (noti_type) { + case NOTI_ENGINE_START: + _DEBUG_INFO("connect to sever ui "); + if (result_status != DM_OK) { + if (result_status == DM_ERR_UNAUTHORIZED || result_status == DM_ERR_AUTHENTICATION_REQUIRED) { + noti_res = noti_engine_fail(noti_type, CONNECTION_STATUS_FAILURE_AUTHENTICATION); + } else { + noti_res = noti_engine_fail(noti_type, CONNECTION_STATUS_FAILURE); + } + } else { + noti_res = noti_engine_fail(noti_type, CONNECTION_STATUS_UPTODATA); + } + _DEBUG_INFO("connect to sever ui : %d", noti_res); + break; + case NOTI_UNKNOWN: + case NOTI_DOWNLOAD: + case NOTI_DOWNLOAD_INFO: + case NOTI_INSTALL: + //case NOTI_WIFI_ONLY_DOWNLOAD_FAIL: + //case NOTI_MEMORY_FULL: + noti_res = noti_engine_fail(noti_type, 0); + break; + case NOTI_LOW_BATTERY: + case NOTI_ALERT_DISPLAY: + case NOTI_ALERT_CONFIRMATION: + case NOTI_ALERT_INPUTTEXT: + case NOTI_ALERT_SINGLE_CHOICE: + case NOTI_ALERT_MULTIPLE_CHOICE: + break; + default: + break; + } + + _DEBUG_INFO("cancel ui : %d", noti_res); + _EXTERN_FUNC_EXIT; + +} + +int check_csc() +{ + _EXTERN_FUNC_ENTER; + + int exist = 0; + + /*FIXME(temporary do not process csc for ui reason) + check csc_check file is existed + if false do csc update + if true check csc xml file has been changed */ + + /*exist = sync_agent_is_existing_fs(CSC_CHECK_PATH); */ + /*temp value */ + exist = 1; + +/*for prevent*/ + /*if(exist == 0 ) { + _update_csc(); + } */ + + _update_csc(); + + _DEBUG_INFO("update = %d", exist); + _EXTERN_FUNC_EXIT; + return exist; +} + +static void _update_csc() +{ + _INNER_FUNC_ENTER; + + //dmacc mo db update + /*sync_agent_write_whole_file(OMA_DS_CSC_CHECK_PATH, contents, strlen(contents), false); */ + + _INNER_FUNC_EXIT; +} + +int get_wifi_state() +{ + _EXTERN_FUNC_ENTER; + + sync_agent_na_result_e ret = SYNC_AGENT_NA_SUCCESS; + sync_agent_na_network_connection_type_e con_type = SYNC_AGENT_NA_NETWORK_CONNECTION_TYPE_UNKNOWN; + sync_agent_na_network_connection_state_e con_state = SYNC_AGENT_NA_NETWORK_CONNECTION_STATE_NONE; + + ret = sync_agent_get_connection_type(&con_type); + if (ret != SYNC_AGENT_NA_SUCCESS) { + _DEBUG_INFO("get connection type"); + ret = SYNC_AGENT_NA_DOWNLOAD_DATA_FAIL; + goto return_part; + } + _DEBUG_INFO("get connection type : %d", con_type); + + ret = sync_agent_get_connection_state(&con_state); + _DEBUG_INFO("get connection state : %d", con_state); + if (ret != SYNC_AGENT_NA_SUCCESS) { + _DEBUG_INFO("get connection state"); + ret = SYNC_AGENT_NA_NETWORK_UNAVAILABLE; + goto return_part; + } + + if (con_type != SYNC_AGENT_NA_NETWORK_CONNECTION_TYPE_WIFI || (con_type == SYNC_AGENT_NA_NETWORK_CONNECTION_TYPE_WIFI && con_state != SYNC_AGENT_NA_NETWORK_CONNECTION_STATE_WIFI_CONNECTED)) { + _DEBUG_INFO("wifi only on mode download fail"); + ret = SYNC_AGENT_NA_NETWORK_UNAVAILABLE; + goto return_part; + } else { + _DEBUG_INFO("wifi on mode download"); + } + + _EXTERN_FUNC_EXIT; + return ret; + + return_part: + _DEBUG_INFO("error end %d : ", ret); + _EXTERN_FUNC_EXIT; + + return ret; +} + +DM_ERROR nonce_decode(char *nextNonce, unsigned char **nextNonceDecode, unsigned int *out_len) +{ + _EXTERN_FUNC_ENTER; + + unsigned char *tempnextNonceDecode = NULL; + tempnextNonceDecode = g_base64_decode(nextNonce, (unsigned int *)(out_len)); + + if (tempnextNonceDecode == NULL && *out_len != 0) { + _DEBUG_INFO("tempnextNonceDecode = NULL , out len : %d", *out_len); + int i = 0; + char *zero = "0"; + *nextNonceDecode = (unsigned char *)calloc(1, (*out_len) + 1); + for (i = 0; i < (*out_len); ++i) { + g_strlcat((char *)(*nextNonceDecode), zero, *out_len); + } + + } else { + + _DEBUG_INFO("tempnextNonceDecode = %s, out_len = %d", tempnextNonceDecode, *out_len); + /**nextNonceDecode = (char *)strdup(tempnextNonceDecode); + str_free((char **)(&tempnextNonceDecode));*/ + *nextNonceDecode = tempnextNonceDecode; + } + + _EXTERN_FUNC_EXIT; + return DM_OK; + +} |