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 };
59
GetStrategy()60 const std::unordered_map<UpdaterMode, UiStrategyCfg> &UiStrategy::GetStrategy()
61 {
62 return strategies_;
63 }
64
LoadStrategy(const JsonNode & node,UpdaterMode mode)65 bool UiStrategy::LoadStrategy(const JsonNode &node, UpdaterMode mode)
66 {
67 auto it = modeStr_.find(mode);
68 if (it == modeStr_.end()) {
69 return false;
70 }
71 const JsonNode &defaultNode = node[Traits<UiStrategyCfg>::STRUCT_KEY][DEFAULT_KEY];
72 const JsonNode &specificNode = node[Traits<UiStrategyCfg>::STRUCT_KEY][it->second];
73 if (!Visit<SETVAL>(specificNode, defaultNode, strategies_[mode])) {
74 LOG(ERROR) << "parse strategy config error";
75 return false;
76 }
77 LOG(DEBUG) << "mode " << modeStr_[mode] << ":\n" << strategies_[mode];
78 return true;
79 }
80
LoadStrategy(const JsonNode & node)81 bool UiStrategy::LoadStrategy(const JsonNode &node)
82 {
83 std::unordered_map<UpdaterMode, UiStrategyCfg>().swap(strategies_);
84 constexpr std::array strategies {UpdaterMode::OTA, UpdaterMode::FACTORYRST,
85 UpdaterMode::SDCARD, UpdaterMode::REBOOTFACTORYRST};
86 for (auto mode : strategies) {
87 if (!LoadStrategy(node, mode)) {
88 LOG(ERROR) << "load strategy " << modeStr_[mode] << " failed";
89 std::unordered_map<UpdaterMode, UiStrategyCfg>().swap(strategies_);
90 return false;
91 }
92 }
93 return true;
94 }
95 } // namespace Updater