1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci *
7fb299fa2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci *
9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci * limitations under the License.
14fb299fa2Sopenharmony_ci */
15fb299fa2Sopenharmony_ci#include "updater/updater_preprocess.h"
16fb299fa2Sopenharmony_ci#include "log.h"
17fb299fa2Sopenharmony_ci#include "parameter.h"
18fb299fa2Sopenharmony_ci#include "utils.h"
19fb299fa2Sopenharmony_ci
20fb299fa2Sopenharmony_ciusing namespace std;
21fb299fa2Sopenharmony_ciusing namespace Hpackage;
22fb299fa2Sopenharmony_cinamespace Updater {
23fb299fa2Sopenharmony_ciextern "C" __attribute__((constructor)) void RegisterHelper(void)
24fb299fa2Sopenharmony_ci{
25fb299fa2Sopenharmony_ci    PreProcess::GetInstance().RegisterHelper(UpdatePreProcess);
26fb299fa2Sopenharmony_ci}
27fb299fa2Sopenharmony_ci
28fb299fa2Sopenharmony_ciextern "C" __attribute__((constructor)) void AuthHelper(void)
29fb299fa2Sopenharmony_ci{
30fb299fa2Sopenharmony_ci    PreProcess::GetInstance().AuthHelper(UpdateAuth);
31fb299fa2Sopenharmony_ci}
32fb299fa2Sopenharmony_ci
33fb299fa2Sopenharmony_ciextern "C" __attribute__((constructor)) void ClearHelper(void)
34fb299fa2Sopenharmony_ci{
35fb299fa2Sopenharmony_ci    PreProcess::GetInstance().ClearHelper(UpdateClear);
36fb299fa2Sopenharmony_ci}
37fb299fa2Sopenharmony_ci
38fb299fa2Sopenharmony_civoid PreProcess::RegisterHelper(PreProcessFunc ptr)
39fb299fa2Sopenharmony_ci{
40fb299fa2Sopenharmony_ci    helper_ = std::move(ptr);
41fb299fa2Sopenharmony_ci}
42fb299fa2Sopenharmony_ci
43fb299fa2Sopenharmony_civoid PreProcess::AuthHelper(AuthFunc ptr)
44fb299fa2Sopenharmony_ci{
45fb299fa2Sopenharmony_ci    authHelper_ = std::move(ptr);
46fb299fa2Sopenharmony_ci}
47fb299fa2Sopenharmony_ci
48fb299fa2Sopenharmony_civoid PreProcess::ClearHelper(ClearFunc ptr)
49fb299fa2Sopenharmony_ci{
50fb299fa2Sopenharmony_ci    clearHelper_ = std::move(ptr);
51fb299fa2Sopenharmony_ci}
52fb299fa2Sopenharmony_ci
53fb299fa2Sopenharmony_ciPreProcess &PreProcess::GetInstance()
54fb299fa2Sopenharmony_ci{
55fb299fa2Sopenharmony_ci    static PreProcess checkPackagesInfo;
56fb299fa2Sopenharmony_ci    return checkPackagesInfo;
57fb299fa2Sopenharmony_ci}
58fb299fa2Sopenharmony_ci
59fb299fa2Sopenharmony_ciint32_t PreProcess::DoUpdatePreProcess(UpdaterParams &upParams, PkgManager::PkgManagerPtr pkgManager)
60fb299fa2Sopenharmony_ci{
61fb299fa2Sopenharmony_ci    if (helper_ == nullptr) {
62fb299fa2Sopenharmony_ci        LOG(INFO) << "PreProcess helper_ is null";
63fb299fa2Sopenharmony_ci        return -1;
64fb299fa2Sopenharmony_ci    }
65fb299fa2Sopenharmony_ci    return helper_(upParams, pkgManager);
66fb299fa2Sopenharmony_ci}
67fb299fa2Sopenharmony_ci
68fb299fa2Sopenharmony_ciint32_t PreProcess::DoUpdateAuth(std::string path)
69fb299fa2Sopenharmony_ci{
70fb299fa2Sopenharmony_ci    return authHelper_(path);
71fb299fa2Sopenharmony_ci}
72fb299fa2Sopenharmony_ci
73fb299fa2Sopenharmony_ciint32_t PreProcess::DoUpdateClear(void)
74fb299fa2Sopenharmony_ci{
75fb299fa2Sopenharmony_ci    return clearHelper_();
76fb299fa2Sopenharmony_ci}
77fb299fa2Sopenharmony_ci
78fb299fa2Sopenharmony_ciint CheckVersion(PkgManager::PkgManagerPtr pkgManager, PackagesInfoPtr pkginfomanager)
79fb299fa2Sopenharmony_ci{
80fb299fa2Sopenharmony_ci    int ret = -1;
81fb299fa2Sopenharmony_ci    if (pkgManager == nullptr || pkginfomanager == nullptr) {
82fb299fa2Sopenharmony_ci        return PKG_INVALID_VERSION;
83fb299fa2Sopenharmony_ci    }
84fb299fa2Sopenharmony_ci    const char *verPtr = GetDisplayVersion();
85fb299fa2Sopenharmony_ci    if (verPtr == nullptr) {
86fb299fa2Sopenharmony_ci        LOG(ERROR) << "Fail to GetDisplayVersion";
87fb299fa2Sopenharmony_ci        return PKG_INVALID_VERSION;
88fb299fa2Sopenharmony_ci    }
89fb299fa2Sopenharmony_ci    std::string verStr(verPtr);
90fb299fa2Sopenharmony_ci    LOG(INFO) << "current version:" << verStr;
91fb299fa2Sopenharmony_ci    std::vector<std::string> targetVersions = pkginfomanager->GetOTAVersion(pkgManager, "version_list", "");
92fb299fa2Sopenharmony_ci    if (targetVersions.size() == 0) {
93fb299fa2Sopenharmony_ci        targetVersions = pkginfomanager->GetOTAVersion(pkgManager, "/version_list", "");
94fb299fa2Sopenharmony_ci    }
95fb299fa2Sopenharmony_ci    for (size_t i = 0; i < targetVersions.size(); i++) {
96fb299fa2Sopenharmony_ci        ret = verStr.compare(targetVersions[i]);
97fb299fa2Sopenharmony_ci        if (ret == 0) {
98fb299fa2Sopenharmony_ci            LOG(INFO) << "Check version success ";
99fb299fa2Sopenharmony_ci            break;
100fb299fa2Sopenharmony_ci        }
101fb299fa2Sopenharmony_ci    }
102fb299fa2Sopenharmony_ci    return ret;
103fb299fa2Sopenharmony_ci}
104fb299fa2Sopenharmony_ci
105fb299fa2Sopenharmony_ciint CheckBoardId(PkgManager::PkgManagerPtr pkgManager, PackagesInfoPtr pkginfomanager)
106fb299fa2Sopenharmony_ci{
107fb299fa2Sopenharmony_ci    int ret = -1;
108fb299fa2Sopenharmony_ci    if (pkgManager == nullptr || pkginfomanager == nullptr) {
109fb299fa2Sopenharmony_ci        return PKG_INVALID_VERSION;
110fb299fa2Sopenharmony_ci    }
111fb299fa2Sopenharmony_ci    std::string localBoardId = Utils::GetLocalBoardId();
112fb299fa2Sopenharmony_ci    if (localBoardId.empty()) {
113fb299fa2Sopenharmony_ci        return 0;
114fb299fa2Sopenharmony_ci    }
115fb299fa2Sopenharmony_ci    std::vector<std::string> boardIdList = pkginfomanager->GetBoardID(pkgManager, "board_list", "");
116fb299fa2Sopenharmony_ci    if (boardIdList.size() == 0) {
117fb299fa2Sopenharmony_ci        boardIdList = pkginfomanager->GetBoardID(pkgManager, "/board_list", "");
118fb299fa2Sopenharmony_ci    }
119fb299fa2Sopenharmony_ci    for (size_t i = 0; i < boardIdList.size(); i++) {
120fb299fa2Sopenharmony_ci        ret = localBoardId.compare(boardIdList[i]);
121fb299fa2Sopenharmony_ci        if (ret == 0) {
122fb299fa2Sopenharmony_ci            LOG(INFO) << "Check board list success ";
123fb299fa2Sopenharmony_ci            break;
124fb299fa2Sopenharmony_ci        }
125fb299fa2Sopenharmony_ci    }
126fb299fa2Sopenharmony_ci    return ret;
127fb299fa2Sopenharmony_ci}
128fb299fa2Sopenharmony_ci
129fb299fa2Sopenharmony_ciint32_t UpdatePreProcess(UpdaterParams &upParams, PkgManager::PkgManagerPtr pkgManager)
130fb299fa2Sopenharmony_ci{
131fb299fa2Sopenharmony_ci    int ret = -1;
132fb299fa2Sopenharmony_ci    if (pkgManager == nullptr) {
133fb299fa2Sopenharmony_ci        return PKG_INVALID_VERSION;
134fb299fa2Sopenharmony_ci    }
135fb299fa2Sopenharmony_ci    PackagesInfoPtr pkginfomanager = PackagesInfo::GetPackagesInfoInstance();
136fb299fa2Sopenharmony_ci    if (pkginfomanager == nullptr) {
137fb299fa2Sopenharmony_ci        return PKG_INVALID_VERSION;
138fb299fa2Sopenharmony_ci    }
139fb299fa2Sopenharmony_ci    ret = CheckBoardId(pkgManager, pkginfomanager);
140fb299fa2Sopenharmony_ci    if (ret != 0) {
141fb299fa2Sopenharmony_ci        PackagesInfo::ReleasePackagesInfoInstance(pkginfomanager);
142fb299fa2Sopenharmony_ci        return ret;
143fb299fa2Sopenharmony_ci    }
144fb299fa2Sopenharmony_ci    ret = CheckVersion(pkgManager, pkginfomanager);
145fb299fa2Sopenharmony_ci    PackagesInfo::ReleasePackagesInfoInstance(pkginfomanager);
146fb299fa2Sopenharmony_ci    return ret;
147fb299fa2Sopenharmony_ci}
148fb299fa2Sopenharmony_ci
149fb299fa2Sopenharmony_ciint32_t UpdateAuth(std::string &path)
150fb299fa2Sopenharmony_ci{
151fb299fa2Sopenharmony_ci    return 0;
152fb299fa2Sopenharmony_ci}
153fb299fa2Sopenharmony_ci
154fb299fa2Sopenharmony_ciint32_t UpdateClear(void)
155fb299fa2Sopenharmony_ci{
156fb299fa2Sopenharmony_ci    return 0;
157fb299fa2Sopenharmony_ci}
158fb299fa2Sopenharmony_ci} // namespace Updater
159