• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "firmware_check_analyze_utils.h"
17 
18 #include <iostream>
19 #include <memory>
20 #include <map>
21 #include <ohos_types.h>
22 #include <string>
23 
24 #include "constant.h"
25 #include "file_utils.h"
26 #include "firmware_combine_version_utils.h"
27 #include "firmware_constant.h"
28 #include "firmware_log.h"
29 #include "firmware_preferences_utils.h"
30 #include "json_utils.h"
31 #include "string_utils.h"
32 #include "update_helper.h"
33 
34 namespace OHOS {
35 namespace UpdateEngine {
DoAnalyze(const std::string & rawJson,std::vector<FirmwareComponent> & components,Duration & duration,CheckAndAuthInfo & checkAndAuthInfo)36 void FirmwareCheckAnalyzeUtils::DoAnalyze(const std::string &rawJson, std::vector<FirmwareComponent> &components,
37     Duration &duration, CheckAndAuthInfo &checkAndAuthInfo)
38 {
39     BlCheckResponse response;
40     int32_t ret = CAST_INT(JsonParseError::ERR_OK);
41     nlohmann::json root;
42     if (!JsonUtils::ParseAndGetJsonObject(rawJson, root)) {
43         FIRMWARE_LOGE("fail to parse out a json object");
44         return;
45     }
46 
47     int32_t status = CAST_INT(CheckResultStatus::STATUS_SYSTEM_ERROR);
48     JsonUtils::GetValueAndSetTo(root, "searchStatus", status);
49 
50     checkAndAuthInfo.responseStatus = std::to_string(status);
51     if (!IsLegalStatus(status)) {
52         FIRMWARE_LOGI("not found new version!");
53         return;
54     }
55     if (status == CAST_INT(CheckResultStatus::STATUS_NEW_VERSION_AVAILABLE)) {
56         ret += AnalyzeBlVersionCheckResults(root, response);
57         ret += AnalyzeComponents(root);
58     }
59 
60     // 解析的都是必须字段,全部解析正常,才能给component赋值
61     if (ret == CAST_INT(JsonParseError::ERR_OK)) {
62         components = components_;
63     }
64 }
65 
AnalyzeBlVersionCheckResults(nlohmann::json & root,BlCheckResponse & response)66 int32_t FirmwareCheckAnalyzeUtils::AnalyzeBlVersionCheckResults(nlohmann::json &root, BlCheckResponse &response)
67 {
68     if (root.find("checkResults") == root.end()) {
69         FIRMWARE_LOGE("FirmwareCheckAnalyzeUtils::AnalyzeBlVersionCheckResults no key checkResults");
70         return CAST_INT(JsonParseError::MISSING_PROP);
71     }
72     FIRMWARE_LOGI("checkResults size is %{public}lu ", root["checkResults"].size());
73     int32_t ret = CAST_INT(JsonParseError::ERR_OK);
74     for (auto &result : root["checkResults"]) {
75         int32_t status = CAST_INT(CheckResultStatus::STATUS_SYSTEM_ERROR);
76         JsonUtils::GetValueAndSetTo(root, "searchStatus", status);
77         if (status == CAST_INT(CheckResultStatus::STATUS_NEW_VERSION_AVAILABLE)) {
78             BlVersionCheckResult checkResult;
79             ret += JsonUtils::GetValueAndSetTo(result, "descriptPackageId", checkResult.descriptPackageId);
80             checkResult.blVersionType = 1;
81             checkResult.status = std::to_string(status);
82             UpdatePackage package;
83             package.versionId = "1";
84             int32_t versionPackageType = CAST_INT(PackageType::DYNAMIC);
85             ret += JsonUtils::GetValueAndSetTo(result, "packageType", versionPackageType);
86             package.versionPackageType = static_cast<PackageType>(versionPackageType);
87             package.packageIndex = 0;
88             checkResult.updatePackages.push_back(package);
89             TargetBlComponent component;
90             component.versionPackageType = package.versionPackageType;
91             ret += JsonUtils::GetValueAndSetTo(result, "versionName", component.displayVersionNumber);
92             ret += JsonUtils::GetValueAndSetTo(result, "versionName", component.versionNumber);
93             checkResult.targetBlComponents.push_back(component);
94             checkResult.blVersionInfo = result["blVersionInfo"].dump();
95             response.blVersionCheckResults.push_back(checkResult);
96             Version version;
97             version.versionId = "1";
98             ret += JsonUtils::GetValueAndSetTo(result, "versionCode", version.versionNumber);
99             ret += JsonUtils::GetValueAndSetTo(result, "url", version.url);
100             response.versionList.push_back(version);
101         }
102     }
103     return ret;
104 }
105 
AnalyzeComponents(nlohmann::json & root)106 int32_t FirmwareCheckAnalyzeUtils::AnalyzeComponents(nlohmann::json &root)
107 {
108     if (root.find("checkResults") == root.end()) {
109         FIRMWARE_LOGE("FirmwareCheckAnalyzeUtils::AnalyzeComponents no key checkResults");
110         return CAST_INT(JsonParseError::MISSING_PROP);
111     }
112     FIRMWARE_LOGI("checkResults size is %{public}lu ", root["checkResults"].size());
113     int32_t ret = CAST_INT(JsonParseError::ERR_OK);
114     std::string componentId;
115     for (auto &result : root["checkResults"]) {
116         FirmwareComponent component;
117         int32_t componetSize;
118         ret += JsonUtils::GetValueAndSetTo(result, "descriptPackageId", component.descriptPackageId);
119         ret += JsonUtils::GetValueAndSetTo(result, "url", component.url);
120         ret += JsonUtils::GetValueAndSetTo(result, "size", componetSize);
121         component.size = static_cast<int64_t>(componetSize);
122         component.fileName = StringUtils::GetLastSplitString(component.url, "/");
123         ret += JsonUtils::GetValueAndSetTo(result, "verifyInfo", component.verifyInfo);
124         ret += JsonUtils::GetValueAndSetTo(result, "versionCode", component.versionNumber);
125         ret += JsonUtils::GetValueAndSetTo(result, "versionName", component.targetBlVersionNumber);
126         component.targetBlDisplayVersionNumber = component.targetBlVersionNumber;
127         component.blVersionType = 1;
128         component.targetBlVersionNumber = component.targetBlVersionNumber;
129         component.componentId = component.descriptPackageId;
130         component.versionPackageType = PackageType::NORMAL;
131         componentId = component.descriptPackageId;
132         components_.push_back(component);
133     }
134 
135     if (root.find("descriptInfo") == root.end()) {
136         FIRMWARE_LOGE("FirmwareCheckAnalyzeUtils::AnalyzeComponents no key descriptInfo");
137         return CAST_INT(JsonParseError::MISSING_PROP);
138     }
139     for (auto &descriptInfo : root["descriptInfo"]) {
140         int32_t descriptInfoType;
141         std::string descContent;
142         std::string subString = "quota";
143         std::string replString = "\"";
144         ret += JsonUtils::GetValueAndSetTo(descriptInfo, "descriptionType", descriptInfoType);
145         ret += JsonUtils::GetValueAndSetTo(descriptInfo, "content", descContent);
146         StringUtils::ReplaceStringAll(descContent, subString, replString);
147         std::string changelogFilePath = Firmware::CHANGELOG_PATH + "/" + componentId + ".xml";
148         FIRMWARE_LOGI("changelog file %{public}s", changelogFilePath.c_str());
149         std::string data = std::to_string(descriptInfoType) + "|" + descContent;
150         if (!FileUtils::SaveDataToFile(changelogFilePath, data)) {
151             FIRMWARE_LOGE("write data to description file error, %{public}s", changelogFilePath.c_str());
152         }
153     }
154 
155     return ret;
156 }
157 
IsLegalStatus(int32_t status)158 bool FirmwareCheckAnalyzeUtils::IsLegalStatus(int32_t status)
159 {
160     return status == CAST_INT(CheckResultStatus::STATUS_NEW_VERSION_AVAILABLE) ||
161            status == CAST_INT(CheckResultStatus::STATUS_NEW_VERSION_NOT_AVAILABLE);
162 }
163 } // namespace UpdateEngine
164 } // namespace OHOS