• 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 #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 
RegisterHelper(PreProcessFunc ptr)28 void PreProcess::RegisterHelper(PreProcessFunc ptr)
29 {
30     helper_ = std::move(ptr);
31 }
32 
GetInstance()33 PreProcess &PreProcess::GetInstance()
34 {
35     static PreProcess checkPackagesInfo;
36     return checkPackagesInfo;
37 }
38 
DoUpdatePreProcess(PkgManager::PkgManagerPtr pkgManager)39 int32_t PreProcess::DoUpdatePreProcess(PkgManager::PkgManagerPtr pkgManager)
40 {
41     if (helper_ == nullptr) {
42         LOG(INFO) << "PreProcess helper_ is null";
43         return -1;
44     }
45     return helper_(pkgManager);
46 }
47 
CheckVersion(PkgManager::PkgManagerPtr pkgManager,PackagesInfoPtr pkginfomanager)48 int CheckVersion(PkgManager::PkgManagerPtr pkgManager, PackagesInfoPtr pkginfomanager)
49 {
50     int ret = -1;
51     if (pkgManager == nullptr || pkginfomanager == nullptr) {
52         return PKG_INVALID_VERSION;
53     }
54     const char *verPtr = GetDisplayVersion();
55     if (verPtr == nullptr) {
56         LOG(ERROR) << "Fail to GetDisplayVersion";
57         return PKG_INVALID_VERSION;
58     }
59     std::string verStr(verPtr);
60     LOG(INFO) << "current version:" << verStr;
61     std::vector<std::string> targetVersions = pkginfomanager->GetOTAVersion(pkgManager, "/version_list", "");
62     for (size_t i = 0; i < targetVersions.size(); i++) {
63         ret = verStr.compare(targetVersions[i]);
64         if (ret == 0) {
65             LOG(INFO) << "Check version success ";
66             break;
67         }
68     }
69     return ret;
70 }
71 
CheckBoardId(PkgManager::PkgManagerPtr pkgManager,PackagesInfoPtr pkginfomanager)72 int CheckBoardId(PkgManager::PkgManagerPtr pkgManager, PackagesInfoPtr pkginfomanager)
73 {
74     int ret = -1;
75     if (pkgManager == nullptr || pkginfomanager == nullptr) {
76         return PKG_INVALID_VERSION;
77     }
78     std::string localBoardId = Utils::GetLocalBoardId();
79     if (localBoardId.empty()) {
80         return 0;
81     }
82     std::vector<std::string> boardIdList = pkginfomanager->GetBoardID(pkgManager, "/board_list", "");
83     for (size_t i = 0; i < boardIdList.size(); i++) {
84         ret = localBoardId.compare(boardIdList[i]);
85         if (ret == 0) {
86             LOG(INFO) << "Check board list success ";
87             break;
88         }
89     }
90     return ret;
91 }
92 
UpdatePreProcess(PkgManager::PkgManagerPtr pkgManager)93 int32_t UpdatePreProcess(PkgManager::PkgManagerPtr pkgManager)
94 {
95     int ret = -1;
96     if (pkgManager == nullptr) {
97         return PKG_INVALID_VERSION;
98     }
99     PackagesInfoPtr pkginfomanager = PackagesInfo::GetPackagesInfoInstance();
100     if (pkginfomanager == nullptr) {
101         return PKG_INVALID_VERSION;
102     }
103     ret = CheckBoardId(pkgManager, pkginfomanager);
104     if (ret != 0) {
105         PackagesInfo::ReleasePackagesInfoInstance(pkginfomanager);
106         return ret;
107     }
108     ret = CheckVersion(pkgManager, pkginfomanager);
109     PackagesInfo::ReleasePackagesInfoInstance(pkginfomanager);
110     return ret;
111 }
112 } // namespace Updater
113