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<std::string, UiStrategyCfg> UiStrategy::strategies_;
53 std::vector<std::string> UiStrategy::modeStr_ = {
54 {UPDATERMODE_SDCARD},
55 {UPDATERMODE_FACTORYRST},
56 {UPDATERMODE_REBOOTFACTORYRST},
57 {UPDATERMODE_OTA},
58 {UPDATERMODE_RECOVER},
59 {UPDATERMODE_NIGHTUPDATE},
60 };
61
RegisterUiMode(const std::string & mode)62 void UiStrategy::RegisterUiMode(const std::string &mode)
63 {
64 LOG(DEBUG) << "RegisterUiMode " << mode;
65 modeStr_.emplace_back(mode);
66 }
67
GetStrategy()68 const std::unordered_map<std::string, UiStrategyCfg> &UiStrategy::GetStrategy()
69 {
70 return strategies_;
71 }
72
LoadStrategy(const JsonNode & node,const std::string & mode)73 bool UiStrategy::LoadStrategy(const JsonNode &node, const std::string &mode)
74 {
75 auto it = std::find(modeStr_.begin(), modeStr_.end(), mode);
76 if (it == modeStr_.end()) {
77 return false;
78 }
79 const JsonNode &defaultNode = node[Traits<UiStrategyCfg>::STRUCT_KEY][DEFAULT_KEY];
80 const JsonNode &specificNode = node[Traits<UiStrategyCfg>::STRUCT_KEY][*it];
81 if (!Visit<SETVAL>(specificNode, defaultNode, strategies_[mode])) {
82 LOG(ERROR) << "parse strategy config error";
83 return false;
84 }
85 LOG(DEBUG) << "mode " << mode << "\n" << strategies_[mode];
86 return true;
87 }
88
LoadStrategy(const JsonNode & node)89 bool UiStrategy::LoadStrategy(const JsonNode &node)
90 {
91 std::unordered_map<std::string, UiStrategyCfg>().swap(strategies_);
92 for (auto mode : modeStr_) {
93 if (!LoadStrategy(node, mode)) {
94 LOG(ERROR) << "load strategy " << mode << " failed";
95 std::unordered_map<std::string, UiStrategyCfg>().swap(strategies_);
96 return false;
97 }
98 }
99 return true;
100 }
101 } // namespace Updater