diff --git a/client/update_client.cpp b/client/update_client.cpp index dbce456d587e61cd89a204af3a8dc308ab938023..47a2c7f61dac29544cac82fb34bfc87dae792746 100644 --- a/client/update_client.cpp +++ b/client/update_client.cpp @@ -656,7 +656,7 @@ int32_t UpdateClient::GetInt32(napi_env env, napi_value arg, const std::string & bool result = false; napi_status status = napi_has_named_property(env, arg, attrName.c_str(), &result); if (result && (status == napi_ok)) { - napi_value value; + napi_value value = nullptr; napi_get_named_property(env, arg, attrName.c_str(), &value); napi_get_value_int32(env, value, &intValue); } @@ -668,7 +668,7 @@ int32_t UpdateClient::GetBool(napi_env env, napi_value arg, const std::string &a bool result = false; napi_status status = napi_has_named_property(env, arg, attrName.c_str(), &result); if (result && (status == napi_ok)) { - napi_value obj; + napi_value obj = nullptr; napi_get_named_property(env, arg, attrName.c_str(), &obj); napi_get_value_bool(env, obj, &value); } @@ -691,7 +691,7 @@ int32_t UpdateClient::GetStringValue(napi_env env, napi_value arg, std::string & int32_t UpdateClient::SetString(napi_env env, napi_value arg, const std::string &attrName, const std::string &string) { - napi_value value; + napi_value value = nullptr; napi_create_string_utf8(env, string.c_str(), string.length(), &value); napi_set_named_property(env, arg, attrName.c_str(), value); return CLIENT_SUCCESS; @@ -699,7 +699,7 @@ int32_t UpdateClient::SetString(napi_env env, napi_value arg, const std::string int32_t UpdateClient::SetInt32(napi_env env, napi_value arg, const std::string &attrName, int32_t intValue) { - napi_value infoStatus; + napi_value infoStatus = nullptr; napi_create_int32(env, intValue, &infoStatus); napi_set_named_property(env, arg, attrName.c_str(), infoStatus); return CLIENT_SUCCESS; @@ -707,7 +707,7 @@ int32_t UpdateClient::SetInt32(napi_env env, napi_value arg, const std::string & int32_t UpdateClient::SetBool(napi_env env, napi_value arg, const std::string &attrName, bool value) { - napi_value infoStatus; + napi_value infoStatus = nullptr; napi_create_int32(env, value, &infoStatus); napi_set_named_property(env, arg, attrName.c_str(), infoStatus); return CLIENT_SUCCESS; @@ -715,7 +715,7 @@ int32_t UpdateClient::SetBool(napi_env env, napi_value arg, const std::string &a int32_t UpdateClient::SetInt64(napi_env env, napi_value arg, const std::string &attrName, int64_t intValue) { - napi_value infoStatus; + napi_value infoStatus = nullptr; napi_create_int64(env, intValue, &infoStatus); napi_set_named_property(env, arg, attrName.c_str(), infoStatus); return CLIENT_SUCCESS; diff --git a/engine/include/update_service.h b/engine/include/update_service.h index 00e1ea3ecfc7b4312b2951eeafbac881ad5a06fa..950e3ef65f0acfbedb488d97dbc5f42c178231bc 100755 --- a/engine/include/update_service.h +++ b/engine/include/update_service.h @@ -63,7 +63,6 @@ public: static int32_t ParseJsonFile(const std::vector &buffer, VersionInfo &info); static int32_t ReadCheckVersionResult(const cJSON* results, VersionInfo &info); static int32_t ReadCheckVersiondescriptInfo(const cJSON *descriptInfo, VersionInfo &info); - static void GetServerIp(std::string &ip); #ifndef UPDATER_UT protected: #else @@ -85,7 +84,6 @@ public: bool VerifyDownloadPkg(const std::string &pkgName, Progress &progress); void ReadDataFromSSL(int32_t engineSocket); private: - std::string serverAddr_; UpdatePolicy policy_ = { 1, 1, INSTALLMODE_AUTO, AUTOUPGRADECONDITION_IDLE, { 10, 20 } }; diff --git a/engine/src/update_helper.cpp b/engine/src/update_helper.cpp index 9a2587a0e3746f80a97539e735971ea240e4d8ee..bbf474712ba4d47c27d80f49534d7ec4207f99d8 100755 --- a/engine/src/update_helper.cpp +++ b/engine/src/update_helper.cpp @@ -201,14 +201,17 @@ void UpdateHelper::Logger(const std::string &fileName, int32_t line, const char static std::vector buff(VECTOR_MAX_BUF_SIZE); va_list list; va_start(list, format); - int size = vsnprintf_s(reinterpret_cast(buff.data()), buff.capacity(), buff.capacity(), format, list); + int size = vsnprintf_s(reinterpret_cast(buff.data()), buff.capacity(), buff.capacity() - 1, format, list); ENGINE_CHECK(size != -1, return, ""); va_end(list); std::string str(buff.data(), size); - char realTime[MAX_TIME_SIZE]; + char realTime[MAX_TIME_SIZE] = {0}; auto sysTime = std::chrono::system_clock::now(); auto currentTime = std::chrono::system_clock::to_time_t(sysTime); - std::strftime(realTime, sizeof(realTime), "%Y-%m-%d %H:%M:%S", std::localtime(¤tTime)); + struct tm *localTime = std::localtime(¤tTime); + if (localTime != nullptr) { + std::strftime(realTime, sizeof(realTime), "%Y-%m-%d %H:%M:%S", localTime); + } logStream << realTime << "[" << LOG_LABEL << "]" << name << " " << line << " : " << str << std::endl; std::cout << realTime << "[" << LOG_LABEL << "]" << name << " " << line << " : " << str << std::endl; logStream.close(); @@ -286,4 +289,4 @@ std::vector UpdateHelper::HexToDegist(const std::string &str) return result; } } -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/engine/src/update_service.cpp b/engine/src/update_service.cpp index fa594dfb195e8ec0e5c1a8e9218e21deaba77e50..3d397c64604ba64ebe350f4578634083918b0f2e 100644 --- a/engine/src/update_service.cpp +++ b/engine/src/update_service.cpp @@ -61,12 +61,15 @@ const std::string SIGNING_CERT_NAME = "/data/update_sa/signing_cert.crt"; #else const std::string SIGNING_CERT_NAME = "/data/updater/src/signing_cert.crt"; #endif +const std::string PARAM_NAME_FOR_VERSION = "hw_sc.build.os.version"; +const std::string DEFAULT_VERSION = "2.2.0"; +const std::string PARAM_NAME_FOR_SEARCH = "update.serverip.search"; +const std::string PARAM_NAME_FOR_DOWNLOAD = "update.serverip.download"; +const std::string DEFAULT_SERVER_IP = "127.0.0.1"; UpdateService::UpdateService(int32_t systemAbilityId, bool runOnCreate) : SystemAbility(systemAbilityId, runOnCreate) { - GetServerIp(serverAddr_); - ENGINE_LOGI("UpdateService serverAddr: %s ", serverAddr_.c_str()); InitVersionInfo(versionInfo_); } @@ -127,10 +130,13 @@ int32_t UpdateService::CheckNewVersion() int32_t engineSocket = socket(AF_INET, SOCK_STREAM, 0); ENGINE_CHECK(engineSocket >= 0, SearchCallback("socket error !", SERVER_BUSY); return 1, "socket error !"); + std::string serverIp = OHOS::system::GetParameter(PARAM_NAME_FOR_SEARCH, DEFAULT_SERVER_IP); + ENGINE_LOGI("CheckNewVersion serverIp: %s ", serverIp.c_str()); + sockaddr_in engineSin {}; engineSin.sin_family = AF_INET; engineSin.sin_port = htons(PORT_NUMBER); - int32_t ret = inet_pton(AF_INET, serverAddr_.c_str(), &engineSin.sin_addr); + int32_t ret = inet_pton(AF_INET, serverIp.c_str(), &engineSin.sin_addr); ENGINE_CHECK(ret > 0, close(engineSocket); SearchCallback("Invalid ip!", SERVER_BUSY); return 1, "socket error"); struct timeval tv = {TIMEOUT_FOR_CONNECT, 0}; @@ -218,7 +224,7 @@ void UpdateService::SearchCallback(const std::string &msg, SearchStatus status) upgradeStatus_ = UPDATE_STATE_CHECK_VERSION_SUCCESS; // Compare the downloaded version with the local version. - std::string loadVersion = OHOS::system::GetParameter("hw_sc.build.os.version", ""); + std::string loadVersion = OHOS::system::GetParameter(PARAM_NAME_FOR_VERSION, DEFAULT_VERSION); int32_t ret = UpdateHelper::CompareVersion(versionInfo_.result[0].versionCode, loadVersion); if (ret <= 0) { versionInfo_.status = NO_NEW_VERSION; @@ -401,7 +407,7 @@ int32_t UpdateService::ReadCheckVersiondescriptInfo(const cJSON *descriptInfo, V bool UpdateService::VerifyDownloadPkg(const std::string &pkgName, Progress &progress) { // Compare the downloaded version with the local version. Only update is supported. - std::string loadVersion = OHOS::system::GetParameter("hw_sc.build.os.version", ""); + std::string loadVersion = OHOS::system::GetParameter(PARAM_NAME_FOR_VERSION, DEFAULT_VERSION); int32_t ret = UpdateHelper::CompareVersion(versionInfo_.result[0].versionCode, loadVersion); if (ret <= 0) { progress.endReason = "Update package version earlier than the local version"; @@ -424,40 +430,15 @@ bool UpdateService::VerifyDownloadPkg(const std::string &pkgName, Progress &prog std::string UpdateService::GetDownloadServerUrl() const { + std::string serverIp = OHOS::system::GetParameter(PARAM_NAME_FOR_DOWNLOAD, DEFAULT_SERVER_IP); + ENGINE_LOGI("GetDownloadServerUrl serverIp: %s ", serverIp.c_str()); std::string url = "http://"; - url += serverAddr_; + url += serverIp; url += "/"; url += versionInfo_.result[0].descriptPackageId; return url; } -void UpdateService::GetServerIp(std::string &ip) -{ - if (access("/data/update_sa", 0) == -1) { - mkdir("/data/update_sa", MKDIR_MODE); - } - - xmlDocPtr doc = xmlReadFile("/data/update_sa/update_config.xml", "UTF-8", XML_PARSE_NOBLANKS); - ENGINE_CHECK(doc != nullptr, return, "Open config Failed"); - - xmlNodePtr rootNode = xmlDocGetRootElement(doc); - ENGINE_CHECK(rootNode != nullptr, xmlFreeDoc(doc); return, "Get root node Failed"); - - xmlChar *nodeContent = nullptr; - for (xmlNodePtr node = rootNode->children; node; node = node->next) { - if ((!xmlStrcmp(node->name, (const xmlChar *)"serverIp"))) { - nodeContent = xmlNodeGetContent(node); - break; - } - } - if (nodeContent != nullptr) { - ip = (char*)nodeContent; - xmlFree(nodeContent); - } - xmlFreeDoc(doc); - return; -} - int32_t UpdateService::Cancel(int32_t service) { ENGINE_LOGI("Cancel %d", service); @@ -488,7 +469,7 @@ int32_t UpdateService::RebootAndInstall(const std::string &miscFile, const std:: void UpdateService::InitVersionInfo(VersionInfo &versionInfo) const { versionInfo.status = HAS_NEW_VERSION; - std::string versionName = OHOS::system::GetParameter("hw_sc.build.os.version", ""); + std::string versionName = OHOS::system::GetParameter(PARAM_NAME_FOR_VERSION, DEFAULT_VERSION); if (versionName.empty()) { versionInfo.status = SYSTEM_ERROR; versionInfo.errMsg = "Can not get local version"; diff --git a/interfaces/innerkits/engine/update_service_proxy.cpp b/interfaces/innerkits/engine/update_service_proxy.cpp index 28cbc1e286163c8007712e6cc28960c44d887736..392a172580d7c68a938f78e50ec6134b4b8a58b6 100755 --- a/interfaces/innerkits/engine/update_service_proxy.cpp +++ b/interfaces/innerkits/engine/update_service_proxy.cpp @@ -189,7 +189,10 @@ int32_t UpdateServiceProxy::RebootAndClean(const std::string &miscFile, const st data.WriteString16(Str8ToStr16(cmd)); MessageParcel reply; MessageOption option; - int32_t ret = remote->SendRequest(REBOOT_CLEAN, data, reply, option); + int32_t ret = ERR_OK; +#ifndef UPDATER_UT + ret = remote->SendRequest(REBOOT_CLEAN, data, reply, option); +#endif ENGINE_CHECK(ret == ERR_OK, return ERR_FLATTEN_OBJECT, "Transact error"); return reply.ReadInt32(); } @@ -205,7 +208,10 @@ int32_t UpdateServiceProxy::RebootAndInstall(const std::string &miscFile, const data.WriteString16(Str8ToStr16(packageName)); MessageParcel reply; MessageOption option; - int32_t ret = remote->SendRequest(REBOOT_INSTALL, data, reply, option); + int32_t ret = ERR_OK; +#ifndef UPDATER_UT + ret = remote->SendRequest(REBOOT_INSTALL, data, reply, option); +#endif ENGINE_CHECK(ret == ERR_OK, return ERR_FLATTEN_OBJECT, "Transact error"); return reply.ReadInt32(); }