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 #include "updater/updater_preprocess.h"
16 #include "log.h"
17 #include "parameter.h"
18 #include "utils.h"
19
20 using namespace std;
21 using namespace Hpackage;
22 namespace Updater {
RegisterHelper(void)23 extern "C" __attribute__((constructor)) void RegisterHelper(void)
24 {
25 PreProcess::GetInstance().RegisterHelper(UpdatePreProcess);
26 }
27
AuthHelper(void)28 extern "C" __attribute__((constructor)) void AuthHelper(void)
29 {
30 PreProcess::GetInstance().AuthHelper(UpdateAuth);
31 }
32
ClearHelper(void)33 extern "C" __attribute__((constructor)) void ClearHelper(void)
34 {
35 PreProcess::GetInstance().ClearHelper(UpdateClear);
36 }
37
RegisterHelper(PreProcessFunc ptr)38 void PreProcess::RegisterHelper(PreProcessFunc ptr)
39 {
40 helper_ = std::move(ptr);
41 }
42
AuthHelper(AuthFunc ptr)43 void PreProcess::AuthHelper(AuthFunc ptr)
44 {
45 authHelper_ = std::move(ptr);
46 }
47
ClearHelper(ClearFunc ptr)48 void PreProcess::ClearHelper(ClearFunc ptr)
49 {
50 clearHelper_ = std::move(ptr);
51 }
52
GetInstance()53 PreProcess &PreProcess::GetInstance()
54 {
55 static PreProcess checkPackagesInfo;
56 return checkPackagesInfo;
57 }
58
DoUpdatePreProcess(UpdaterParams & upParams,PkgManager::PkgManagerPtr pkgManager)59 int32_t PreProcess::DoUpdatePreProcess(UpdaterParams &upParams, PkgManager::PkgManagerPtr pkgManager)
60 {
61 if (helper_ == nullptr) {
62 LOG(INFO) << "PreProcess helper_ is null";
63 return -1;
64 }
65 return helper_(upParams, pkgManager);
66 }
67
DoUpdateAuth(std::string path)68 int32_t PreProcess::DoUpdateAuth(std::string path)
69 {
70 return authHelper_(path);
71 }
72
DoUpdateClear(void)73 int32_t PreProcess::DoUpdateClear(void)
74 {
75 return clearHelper_();
76 }
77
CheckVersion(PkgManager::PkgManagerPtr pkgManager,PackagesInfoPtr pkginfomanager)78 int CheckVersion(PkgManager::PkgManagerPtr pkgManager, PackagesInfoPtr pkginfomanager)
79 {
80 int ret = -1;
81 if (pkgManager == nullptr || pkginfomanager == nullptr) {
82 return PKG_INVALID_VERSION;
83 }
84 const char *verPtr = GetDisplayVersion();
85 if (verPtr == nullptr) {
86 LOG(ERROR) << "Fail to GetDisplayVersion";
87 return PKG_INVALID_VERSION;
88 }
89 std::string verStr(verPtr);
90 LOG(INFO) << "current version:" << verStr;
91 std::vector<std::string> targetVersions = pkginfomanager->GetOTAVersion(pkgManager, "version_list", "");
92 if (targetVersions.size() == 0) {
93 targetVersions = pkginfomanager->GetOTAVersion(pkgManager, "/version_list", "");
94 }
95 for (size_t i = 0; i < targetVersions.size(); i++) {
96 ret = verStr.compare(targetVersions[i]);
97 if (ret == 0) {
98 LOG(INFO) << "Check version success ";
99 break;
100 }
101 }
102 return ret;
103 }
104
CheckBoardId(PkgManager::PkgManagerPtr pkgManager,PackagesInfoPtr pkginfomanager)105 int CheckBoardId(PkgManager::PkgManagerPtr pkgManager, PackagesInfoPtr pkginfomanager)
106 {
107 int ret = -1;
108 if (pkgManager == nullptr || pkginfomanager == nullptr) {
109 return PKG_INVALID_VERSION;
110 }
111 std::string localBoardId = Utils::GetLocalBoardId();
112 if (localBoardId.empty()) {
113 return 0;
114 }
115 std::vector<std::string> boardIdList = pkginfomanager->GetBoardID(pkgManager, "board_list", "");
116 if (boardIdList.size() == 0) {
117 boardIdList = pkginfomanager->GetBoardID(pkgManager, "/board_list", "");
118 }
119 for (size_t i = 0; i < boardIdList.size(); i++) {
120 ret = localBoardId.compare(boardIdList[i]);
121 if (ret == 0) {
122 LOG(INFO) << "Check board list success ";
123 break;
124 }
125 }
126 return ret;
127 }
128
UpdatePreProcess(UpdaterParams & upParams,PkgManager::PkgManagerPtr pkgManager)129 int32_t UpdatePreProcess(UpdaterParams &upParams, PkgManager::PkgManagerPtr pkgManager)
130 {
131 int ret = -1;
132 if (pkgManager == nullptr) {
133 return PKG_INVALID_VERSION;
134 }
135 PackagesInfoPtr pkginfomanager = PackagesInfo::GetPackagesInfoInstance();
136 if (pkginfomanager == nullptr) {
137 return PKG_INVALID_VERSION;
138 }
139 ret = CheckBoardId(pkgManager, pkginfomanager);
140 if (ret != 0) {
141 PackagesInfo::ReleasePackagesInfoInstance(pkginfomanager);
142 return ret;
143 }
144 ret = CheckVersion(pkgManager, pkginfomanager);
145 PackagesInfo::ReleasePackagesInfoInstance(pkginfomanager);
146 return ret;
147 }
148
UpdateAuth(std::string & path)149 int32_t UpdateAuth(std::string &path)
150 {
151 return 0;
152 }
153
UpdateClear(void)154 int32_t UpdateClear(void)
155 {
156 return 0;
157 }
158 } // namespace Updater
159