• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 = IN_PROCESS_CALL(
81         DeviceProfile::DistributedDeviceProfileClient::GetInstance().GetDeviceProfile(deviceId,
82             DMS_SERVICE_ID, profile)
83     );
84     if (result != ERR_OK) {
85         return result;
86     }
87     appInfoJsonData = profile.GetCharacteristicProfileJson();
88     return ERR_OK;
89 }
90 
ParseAppInfo(const std::string & appInfoJsonData,std::string & packageNamesData,std::string & versionsData)91 int32_t DmsVersionManager::ParseAppInfo(const std::string& appInfoJsonData, std::string& packageNamesData,
92     std::string& versionsData)
93 {
94     if (appInfoJsonData.empty()) {
95         return DMS_VERSION_EMPTY;
96     }
97     nlohmann::json appInfoJson = nlohmann::json::parse(appInfoJsonData.c_str(), nullptr, false);
98     if (appInfoJson.find(PACKAGE_NAMES) == appInfoJson.end() || !appInfoJson.at(PACKAGE_NAMES).is_string()) {
99         return DMS_VERSION_EMPTY;
100     }
101     appInfoJson.at(PACKAGE_NAMES).get_to(packageNamesData);
102 
103     if (appInfoJson.find(VERSIONS) == appInfoJson.end() || !appInfoJson.at(VERSIONS).is_string()) {
104         return DMS_VERSION_EMPTY;
105     }
106     appInfoJson.at(VERSIONS).get_to(versionsData);
107 
108     return ERR_OK;
109 }
110 
GetDmsVersionDataFromAppInfo(const std::string & packageNamesData,const std::string & versionsData,std::string & dmsVersionData)111 int32_t DmsVersionManager::GetDmsVersionDataFromAppInfo(const std::string& packageNamesData,
112     const std::string& versionsData, std::string& dmsVersionData)
113 {
114     if (packageNamesData.empty() || versionsData.empty()) {
115         return DMS_VERSION_EMPTY;
116     }
117     std::vector<std::string> packageNameList;
118     std::vector<std::string> versionsList;
119     SplitStr(packageNamesData, ",", packageNameList);
120     SplitStr(versionsData, ",", versionsList);
121     if (packageNameList.size() != versionsList.size()) {
122         return DMS_VERSION_PARSE_EXCEPTION;
123     }
124     for (std::size_t i = 0; i < packageNameList.size(); i++) {
125         if (packageNameList[i] == DMS_NAME) {
126             dmsVersionData = versionsList[i];
127             return ERR_OK;
128         }
129     }
130     return DMS_VERSION_EMPTY;
131 }
132 
ParseDmsVersion(const std::string & dmsVersionData,DmsVersion & dmsVersion)133 bool DmsVersionManager::ParseDmsVersion(const std::string& dmsVersionData, DmsVersion& dmsVersion)
134 {
135     std::vector<std::string> versionNumList;
136     SplitStr(dmsVersionData, ".", versionNumList);
137     if (versionNumList.size() != DMS_VERSION_LENGTH) {
138         return false;
139     }
140     int32_t majorVersionNum = -1;
141     if (!OHOS::StrToInt(versionNumList[DMS_MAJOR_VERSION_INDEX], majorVersionNum) || majorVersionNum < 0) {
142         return false;
143     }
144     dmsVersion.majorVersionNum = static_cast<uint32_t>(majorVersionNum);
145 
146     int32_t minorVersionNum = -1;
147     if (!OHOS::StrToInt(versionNumList[DMS_MINOR_VERSION_INDEX], minorVersionNum) || minorVersionNum < 0) {
148         return false;
149     }
150     dmsVersion.minorVersionNum = static_cast<uint32_t>(minorVersionNum);
151 
152     int32_t featureVersionNum = -1;
153     if (!OHOS::StrToInt(versionNumList[DMS_FEATURE_VERSION_INDEX], featureVersionNum) || featureVersionNum < 0) {
154         return false;
155     }
156     dmsVersion.featureVersionNum = static_cast<uint32_t>(featureVersionNum);
157     return true;
158 }
159 
CompareDmsVersion(const DmsVersion & dmsVersion,const DmsVersion & thresholdDmsVersion)160 bool DmsVersionManager::CompareDmsVersion(const DmsVersion& dmsVersion, const DmsVersion& thresholdDmsVersion)
161 {
162     if (dmsVersion.majorVersionNum < thresholdDmsVersion.majorVersionNum) {
163         return true;
164     }
165     if (dmsVersion.majorVersionNum == thresholdDmsVersion.majorVersionNum &&
166         dmsVersion.minorVersionNum < thresholdDmsVersion.minorVersionNum) {
167         return true;
168     }
169     if (dmsVersion.majorVersionNum == thresholdDmsVersion.majorVersionNum &&
170         dmsVersion.minorVersionNum == thresholdDmsVersion.minorVersionNum &&
171         dmsVersion.featureVersionNum < thresholdDmsVersion.featureVersionNum) {
172         return true;
173     }
174     return false;
175 }
176 } // namespace DistributedSchedule
177 } // namespace OHOS
178