1 /*
2 * Copyright (c) 2022-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 "dms_version_manager.h"
17
18 #include "distributed_device_profile_client.h"
19 #include "dtbschedmgr_log.h"
20 #include "string_ex.h"
21
22 namespace OHOS {
23 namespace DistributedSchedule {
24 namespace {
25 const std::string TAG = "DmsVersionManager";
26 const std::string DMS_SERVICE_ID = "appInfo";
27 const std::string DMS_SERVICE_TYPE = "appInfo";
28 const std::string PACKAGE_NAMES = "packageNames";
29 const std::string VERSIONS = "versions";
30 const std::string DMS_NAME = "dmsfwk";
31 const int32_t DMS_VERSION_LENGTH = 3;
32 const int32_t DMS_MAJOR_VERSION_INDEX = 0;
33 const int32_t DMS_MINOR_VERSION_INDEX = 1;
34 const int32_t DMS_FEATURE_VERSION_INDEX = 2;
35 }
36
IsRemoteDmsVersionLower(const std::string & remoteDeviceId,const DmsVersion & thresholdDmsVersion)37 bool DmsVersionManager::IsRemoteDmsVersionLower(const std::string& remoteDeviceId,
38 const DmsVersion& thresholdDmsVersion)
39 {
40 DmsVersion dmsVersion;
41 int32_t result = GetRemoteDmsVersion(remoteDeviceId, dmsVersion);
42 if (result != ERR_OK) {
43 return true;
44 }
45 return CompareDmsVersion(dmsVersion, thresholdDmsVersion);
46 }
47
GetRemoteDmsVersion(const std::string & deviceId,DmsVersion & dmsVersion)48 int32_t DmsVersionManager::GetRemoteDmsVersion(const std::string& deviceId, DmsVersion& dmsVersion)
49 {
50 std::string appInfoJsonData;
51 int32_t result = GetAppInfoFromDP(deviceId, appInfoJsonData);
52 if (result != ERR_OK) {
53 HILOGI("get app info failed, result: %{public}d!", result);
54 return result;
55 }
56 std::string packageNamesData;
57 std::string versionsData;
58 result = ParseAppInfo(appInfoJsonData, packageNamesData, versionsData);
59 if (result != ERR_OK) {
60 HILOGI("parse app info failed");
61 return result;
62 }
63
64 std::string dmsVersionData;
65 result = GetDmsVersionDataFromAppInfo(packageNamesData, versionsData, dmsVersionData);
66 if (result != ERR_OK) {
67 HILOGW("get dms version data failed, result: %{public}d!", result);
68 return result;
69 }
70 if (!ParseDmsVersion(dmsVersionData, dmsVersion)) {
71 HILOGE("parse dms version failed");
72 return DMS_VERSION_PARSE_EXCEPTION;
73 }
74 return ERR_OK;
75 }
76
GetAppInfoFromDP(const std::string & deviceId,std::string & appInfoJsonData)77 int32_t DmsVersionManager::GetAppInfoFromDP(const std::string& deviceId, std::string& appInfoJsonData)
78 {
79 DeviceProfile::ServiceCharacteristicProfile profile;
80 int32_t result = DeviceProfile::DistributedDeviceProfileClient::GetInstance().GetDeviceProfile(deviceId,
81 DMS_SERVICE_ID, profile);
82 if (result != ERR_OK) {
83 return result;
84 }
85 appInfoJsonData = profile.GetCharacteristicProfileJson();
86 return ERR_OK;
87 }
88
ParseAppInfo(const std::string & appInfoJsonData,std::string & packageNamesData,std::string & versionsData)89 int32_t DmsVersionManager::ParseAppInfo(const std::string& appInfoJsonData, std::string& packageNamesData,
90 std::string& versionsData)
91 {
92 if (appInfoJsonData.empty()) {
93 return DMS_VERSION_EMPTY;
94 }
95 nlohmann::json appInfoJson = nlohmann::json::parse(appInfoJsonData.c_str(), nullptr, false);
96 if (appInfoJson.is_discarded()) {
97 return DMS_VERSION_EMPTY;
98 }
99 if (appInfoJson.find(PACKAGE_NAMES) == appInfoJson.end() || !appInfoJson.at(PACKAGE_NAMES).is_string()) {
100 return DMS_VERSION_EMPTY;
101 }
102 appInfoJson.at(PACKAGE_NAMES).get_to(packageNamesData);
103
104 if (appInfoJson.find(VERSIONS) == appInfoJson.end() || !appInfoJson.at(VERSIONS).is_string()) {
105 return DMS_VERSION_EMPTY;
106 }
107 appInfoJson.at(VERSIONS).get_to(versionsData);
108
109 return ERR_OK;
110 }
111
GetDmsVersionDataFromAppInfo(const std::string & packageNamesData,const std::string & versionsData,std::string & dmsVersionData)112 int32_t DmsVersionManager::GetDmsVersionDataFromAppInfo(const std::string& packageNamesData,
113 const std::string& versionsData, std::string& dmsVersionData)
114 {
115 if (packageNamesData.empty() || versionsData.empty()) {
116 return DMS_VERSION_EMPTY;
117 }
118 std::vector<std::string> packageNameList;
119 std::vector<std::string> versionsList;
120 SplitStr(packageNamesData, ",", packageNameList);
121 SplitStr(versionsData, ",", versionsList);
122 if (packageNameList.size() != versionsList.size()) {
123 return DMS_VERSION_PARSE_EXCEPTION;
124 }
125 for (std::size_t i = 0; i < packageNameList.size(); i++) {
126 if (packageNameList[i] == DMS_NAME) {
127 dmsVersionData = versionsList[i];
128 return ERR_OK;
129 }
130 }
131 return DMS_VERSION_EMPTY;
132 }
133
ParseDmsVersion(const std::string & dmsVersionData,DmsVersion & dmsVersion)134 bool DmsVersionManager::ParseDmsVersion(const std::string& dmsVersionData, DmsVersion& dmsVersion)
135 {
136 std::vector<std::string> versionNumList;
137 SplitStr(dmsVersionData, ".", versionNumList);
138 if (versionNumList.size() != DMS_VERSION_LENGTH) {
139 return false;
140 }
141 int32_t majorVersionNum = -1;
142 if (!OHOS::StrToInt(versionNumList[DMS_MAJOR_VERSION_INDEX], majorVersionNum) || majorVersionNum < 0) {
143 return false;
144 }
145 dmsVersion.majorVersionNum = static_cast<uint32_t>(majorVersionNum);
146
147 int32_t minorVersionNum = -1;
148 if (!OHOS::StrToInt(versionNumList[DMS_MINOR_VERSION_INDEX], minorVersionNum) || minorVersionNum < 0) {
149 return false;
150 }
151 dmsVersion.minorVersionNum = static_cast<uint32_t>(minorVersionNum);
152
153 int32_t featureVersionNum = -1;
154 if (!OHOS::StrToInt(versionNumList[DMS_FEATURE_VERSION_INDEX], featureVersionNum) || featureVersionNum < 0) {
155 return false;
156 }
157 dmsVersion.featureVersionNum = static_cast<uint32_t>(featureVersionNum);
158 return true;
159 }
160
CompareDmsVersion(const DmsVersion & dmsVersion,const DmsVersion & thresholdDmsVersion)161 bool DmsVersionManager::CompareDmsVersion(const DmsVersion& dmsVersion, const DmsVersion& thresholdDmsVersion)
162 {
163 if (dmsVersion.majorVersionNum < thresholdDmsVersion.majorVersionNum) {
164 return true;
165 }
166 if (dmsVersion.majorVersionNum == thresholdDmsVersion.majorVersionNum &&
167 dmsVersion.minorVersionNum < thresholdDmsVersion.minorVersionNum) {
168 return true;
169 }
170 if (dmsVersion.majorVersionNum == thresholdDmsVersion.majorVersionNum &&
171 dmsVersion.minorVersionNum == thresholdDmsVersion.minorVersionNum &&
172 dmsVersion.featureVersionNum < thresholdDmsVersion.featureVersionNum) {
173 return true;
174 }
175 return false;
176 }
177 } // namespace DistributedSchedule
178 } // namespace OHOS
179