• 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 "ui_strategy.h"
17 #include <array>
18 
19 namespace Updater {
operator <<(std::ostream & os,const UiStrategyCfg & info)20 std::ostream &operator<<(std::ostream &os, const UiStrategyCfg &info)
21 {
22     os << "confirmPageId: " << info.confirmPageId << std::endl;
23     os << "labelLogId: { " << info.labelLogId << " }" << std::endl;
24     os << "labelLogResId: { " << info.labelLogResId << " }" << std::endl;
25     os << "labelUpdId: { " << info.labelUpdId << " }" << std::endl;
26     os << info.progressPage << std::endl;
27     os << info.resPage;
28     return os;
29 }
30 
operator <<(std::ostream & os,const ResPage & info)31 std::ostream &operator<<(std::ostream &os, const ResPage &info)
32 {
33     os << "resPage: {" << std::endl;
34     os << "\tsucessPageId: " << info.successPageId << std::endl;
35     os << "\tfailPageId: " << info.failPageId << std::endl;
36     os << "}" << std::endl;
37     return os;
38 }
39 
operator <<(std::ostream & os,const ProgressPage & info)40 std::ostream &operator<<(std::ostream &os, const ProgressPage &info)
41 {
42     os << "progressPage: {" << std::endl;
43     os << "\tprocessPageId: " << info.progressPageId << std::endl;
44     os << "\tprgrsComId: " << info.progressComId << std::endl;
45     os << "\tprgrsType: " << info.progressType << std::endl;
46     os << "\tlogoComId: " << info.logoComId << std::endl;
47     os << "\tlogoType: " << info.logoType << std::endl;
48     os << "}";
49     return os;
50 }
51 
52 std::unordered_map<UpdaterMode, UiStrategyCfg> UiStrategy::strategies_;
53 std::unordered_map<UpdaterMode, std::string> UiStrategy::modeStr_ = {
54     {UpdaterMode::SDCARD, "sdcard"},
55     {UpdaterMode::FACTORYRST, "factoryRst"},
56     {UpdaterMode::REBOOTFACTORYRST, "rebootFactoryRst"},
57     {UpdaterMode::OTA, "ota"},
58     {UpdaterMode::RECOVER, "recover"},
59 };
60 
GetStrategy()61 const std::unordered_map<UpdaterMode, UiStrategyCfg> &UiStrategy::GetStrategy()
62 {
63     return strategies_;
64 }
65 
LoadStrategy(const JsonNode & node,UpdaterMode mode)66 bool UiStrategy::LoadStrategy(const JsonNode &node, UpdaterMode mode)
67 {
68     auto it = modeStr_.find(mode);
69     if (it == modeStr_.end()) {
70         return false;
71     }
72     const JsonNode &defaultNode = node[Traits<UiStrategyCfg>::STRUCT_KEY][DEFAULT_KEY];
73     const JsonNode &specificNode = node[Traits<UiStrategyCfg>::STRUCT_KEY][it->second];
74     if (!Visit<SETVAL>(specificNode, defaultNode, strategies_[mode])) {
75         LOG(ERROR) << "parse strategy config error";
76         return false;
77     }
78     LOG(DEBUG) << "mode " << modeStr_[mode] << ":\n" << strategies_[mode];
79     return true;
80 }
81 
LoadStrategy(const JsonNode & node)82 bool UiStrategy::LoadStrategy(const JsonNode &node)
83 {
84     std::unordered_map<UpdaterMode, UiStrategyCfg>().swap(strategies_);
85     constexpr std::array strategies {UpdaterMode::OTA, UpdaterMode::FACTORYRST,
86         UpdaterMode::SDCARD, UpdaterMode::REBOOTFACTORYRST, UpdaterMode::RECOVER};
87     for (auto mode : strategies) {
88         if (!LoadStrategy(node, mode)) {
89             LOG(ERROR) << "load strategy " << modeStr_[mode] << " failed";
90             std::unordered_map<UpdaterMode, UiStrategyCfg>().swap(strategies_);
91             return false;
92         }
93     }
94     return true;
95 }
96 } // namespace Updater