blob: cee1ece86a02b48470cd41d9f5f92622617e419f [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2014 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
David Zeuthenfe225c12014-04-29 10:37:35 -070016
Alex Deymoaab50e32014-11-10 19:55:35 -080017#include "update_engine/update_manager/boxed_value.h"
18
Ben Chandcf5a1d2014-08-07 09:34:12 -070019#include <stdint.h>
20
David Zeuthenfe225c12014-04-29 10:37:35 -070021#include <set>
22#include <string>
23
24#include <base/strings/string_number_conversions.h>
25#include <base/time/time.h>
26
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/utils.h"
Sen Jiang255e22b2016-05-20 16:15:29 -070028#include "update_engine/connection_utils.h"
Marton Hunyady0e0e3542018-02-21 18:51:39 +010029#include "update_engine/update_manager/rollback_prefs.h"
Alex Deymo63784a52014-05-28 10:46:14 -070030#include "update_engine/update_manager/shill_provider.h"
31#include "update_engine/update_manager/updater_provider.h"
Adolfo Victoria94ffe132018-06-28 16:14:56 -070032#include "update_engine/update_manager/weekly_time.h"
David Zeuthenfe225c12014-04-29 10:37:35 -070033
Sen Jiang255e22b2016-05-20 16:15:29 -070034using chromeos_update_engine::ConnectionTethering;
35using chromeos_update_engine::ConnectionType;
36using chromeos_update_engine::connection_utils::StringForConnectionType;
David Zeuthenfe225c12014-04-29 10:37:35 -070037using std::set;
38using std::string;
39
Alex Deymo63784a52014-05-28 10:46:14 -070040namespace chromeos_update_manager {
David Zeuthenfe225c12014-04-29 10:37:35 -070041
42// Template instantiation for common types; used in BoxedValue::ToString().
43// Keep in sync with boxed_value_unitttest.cc.
44
Amin Hassani4b717432019-01-14 16:24:20 -080045template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070046string BoxedValue::ValuePrinter<string>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070047 const string* val = reinterpret_cast<const string*>(value);
48 return *val;
49}
50
Amin Hassani4b717432019-01-14 16:24:20 -080051template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070052string BoxedValue::ValuePrinter<int>(const void* value) {
Alex Deymof967ebe2014-05-05 14:46:17 -070053 const int* val = reinterpret_cast<const int*>(value);
Amin Hassani2e4eda52019-01-07 14:01:17 -080054#if BASE_VER < 576279
Alex Deymof967ebe2014-05-05 14:46:17 -070055 return base::IntToString(*val);
Amin Hassani2e4eda52019-01-07 14:01:17 -080056#else
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -070057 return base::NumberToString(*val);
Amin Hassani2e4eda52019-01-07 14:01:17 -080058#endif
Alex Deymof967ebe2014-05-05 14:46:17 -070059}
60
Amin Hassani4b717432019-01-14 16:24:20 -080061template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070062string BoxedValue::ValuePrinter<unsigned int>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070063 const unsigned int* val = reinterpret_cast<const unsigned int*>(value);
Amin Hassani2e4eda52019-01-07 14:01:17 -080064#if BASE_VER < 576279
David Zeuthenfe225c12014-04-29 10:37:35 -070065 return base::UintToString(*val);
Amin Hassani2e4eda52019-01-07 14:01:17 -080066#else
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -070067 return base::NumberToString(*val);
Amin Hassani2e4eda52019-01-07 14:01:17 -080068#endif
David Zeuthenfe225c12014-04-29 10:37:35 -070069}
70
Amin Hassani4b717432019-01-14 16:24:20 -080071template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070072string BoxedValue::ValuePrinter<int64_t>(const void* value) {
Alex Deymof967ebe2014-05-05 14:46:17 -070073 const int64_t* val = reinterpret_cast<const int64_t*>(value);
Amin Hassani2e4eda52019-01-07 14:01:17 -080074#if BASE_VER < 576279
Alex Deymof967ebe2014-05-05 14:46:17 -070075 return base::Int64ToString(*val);
Amin Hassani2e4eda52019-01-07 14:01:17 -080076#else
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -070077 return base::NumberToString(*val);
Amin Hassani2e4eda52019-01-07 14:01:17 -080078#endif
David Zeuthenfe225c12014-04-29 10:37:35 -070079}
80
Amin Hassani4b717432019-01-14 16:24:20 -080081template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070082string BoxedValue::ValuePrinter<uint64_t>(const void* value) {
Amin Hassani4b717432019-01-14 16:24:20 -080083 const uint64_t* val = reinterpret_cast<const uint64_t*>(value);
Amin Hassani2e4eda52019-01-07 14:01:17 -080084#if BASE_VER < 576279
85 return base::Uint64ToString(*val);
86#else
87 return base::NumberToString(*val);
88#endif
David Zeuthenfe225c12014-04-29 10:37:35 -070089}
90
Amin Hassani4b717432019-01-14 16:24:20 -080091template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070092string BoxedValue::ValuePrinter<bool>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070093 const bool* val = reinterpret_cast<const bool*>(value);
94 return *val ? "true" : "false";
95}
96
Amin Hassani4b717432019-01-14 16:24:20 -080097template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070098string BoxedValue::ValuePrinter<double>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070099 const double* val = reinterpret_cast<const double*>(value);
Amin Hassani2e4eda52019-01-07 14:01:17 -0800100#if BASE_VER < 576279
David Zeuthenfe225c12014-04-29 10:37:35 -0700101 return base::DoubleToString(*val);
Amin Hassani2e4eda52019-01-07 14:01:17 -0800102#else
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -0700103 return base::NumberToString(*val);
Amin Hassani2e4eda52019-01-07 14:01:17 -0800104#endif
David Zeuthenfe225c12014-04-29 10:37:35 -0700105}
106
Amin Hassani4b717432019-01-14 16:24:20 -0800107template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700108string BoxedValue::ValuePrinter<base::Time>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700109 const base::Time* val = reinterpret_cast<const base::Time*>(value);
110 return chromeos_update_engine::utils::ToString(*val);
111}
112
Amin Hassani4b717432019-01-14 16:24:20 -0800113template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700114string BoxedValue::ValuePrinter<base::TimeDelta>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700115 const base::TimeDelta* val = reinterpret_cast<const base::TimeDelta*>(value);
116 return chromeos_update_engine::utils::FormatTimeDelta(*val);
117}
118
Amin Hassani4b717432019-01-14 16:24:20 -0800119template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700120string BoxedValue::ValuePrinter<ConnectionType>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700121 const ConnectionType* val = reinterpret_cast<const ConnectionType*>(value);
Sen Jiang255e22b2016-05-20 16:15:29 -0700122 return StringForConnectionType(*val);
David Zeuthenfe225c12014-04-29 10:37:35 -0700123}
124
Amin Hassani4b717432019-01-14 16:24:20 -0800125template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700126string BoxedValue::ValuePrinter<set<ConnectionType>>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700127 string ret = "";
128 const set<ConnectionType>* val =
129 reinterpret_cast<const set<ConnectionType>*>(value);
130 for (auto& it : *val) {
131 ConnectionType type = it;
132 if (ret.size() > 0)
133 ret += ",";
Sen Jiang255e22b2016-05-20 16:15:29 -0700134 ret += StringForConnectionType(type);
David Zeuthenfe225c12014-04-29 10:37:35 -0700135 }
136 return ret;
137}
138
Amin Hassani4b717432019-01-14 16:24:20 -0800139template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700140string BoxedValue::ValuePrinter<ConnectionTethering>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700141 const ConnectionTethering* val =
142 reinterpret_cast<const ConnectionTethering*>(value);
143 switch (*val) {
144 case ConnectionTethering::kNotDetected:
145 return "Not Detected";
146 case ConnectionTethering::kSuspected:
147 return "Suspected";
148 case ConnectionTethering::kConfirmed:
149 return "Confirmed";
150 case ConnectionTethering::kUnknown:
151 return "Unknown";
152 }
153 NOTREACHED();
154 return "Unknown";
155}
156
Marton Hunyady0e0e3542018-02-21 18:51:39 +0100157template <>
158string BoxedValue::ValuePrinter<RollbackToTargetVersion>(const void* value) {
159 const RollbackToTargetVersion* val =
160 reinterpret_cast<const RollbackToTargetVersion*>(value);
161 switch (*val) {
162 case RollbackToTargetVersion::kUnspecified:
163 return "Unspecified";
164 case RollbackToTargetVersion::kDisabled:
165 return "Disabled";
Marton Hunyadyd4bc4622018-08-30 15:52:23 +0200166 case RollbackToTargetVersion::kRollbackAndPowerwash:
167 return "Rollback and powerwash";
Marton Hunyady31aefb92018-08-29 16:17:03 +0200168 case RollbackToTargetVersion::kRollbackAndRestoreIfPossible:
169 return "Rollback and restore if possible";
Marton Hunyadyd4bc4622018-08-30 15:52:23 +0200170 case RollbackToTargetVersion::kRollbackOnlyIfRestorePossible:
171 return "Rollback only if restore is possible";
Marton Hunyady0e0e3542018-02-21 18:51:39 +0100172 case RollbackToTargetVersion::kMaxValue:
173 NOTREACHED();
174 return "Max value";
175 }
176 NOTREACHED();
177 return "Unknown";
178}
179
Amin Hassani4b717432019-01-14 16:24:20 -0800180template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700181string BoxedValue::ValuePrinter<Stage>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700182 const Stage* val = reinterpret_cast<const Stage*>(value);
183 switch (*val) {
Alex Vakulenko072359c2014-07-18 11:41:07 -0700184 case Stage::kIdle:
David Zeuthenfe225c12014-04-29 10:37:35 -0700185 return "Idle";
186 case Stage::kCheckingForUpdate:
187 return "Checking For Update";
188 case Stage::kUpdateAvailable:
189 return "Update Available";
190 case Stage::kDownloading:
191 return "Downloading";
192 case Stage::kVerifying:
193 return "Verifying";
194 case Stage::kFinalizing:
195 return "Finalizing";
196 case Stage::kUpdatedNeedReboot:
197 return "Updated, Need Reboot";
198 case Stage::kReportingErrorEvent:
199 return "Reporting Error Event";
200 case Stage::kAttemptingRollback:
201 return "Attempting Rollback";
202 }
203 NOTREACHED();
204 return "Unknown";
205}
206
Amin Hassani4b717432019-01-14 16:24:20 -0800207template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700208string BoxedValue::ValuePrinter<UpdateRequestStatus>(const void* value) {
209 const UpdateRequestStatus* val =
210 reinterpret_cast<const UpdateRequestStatus*>(value);
211 switch (*val) {
212 case UpdateRequestStatus::kNone:
213 return "None";
214 case UpdateRequestStatus::kInteractive:
215 return "Interactive";
216 case UpdateRequestStatus::kPeriodic:
217 return "Periodic";
218 }
219 NOTREACHED();
220 return "Unknown";
221}
222
Aaron Woodbf5a2522017-10-04 10:58:36 -0700223template <>
224string BoxedValue::ValuePrinter<UpdateRestrictions>(const void* value) {
225 const UpdateRestrictions* val =
226 reinterpret_cast<const UpdateRestrictions*>(value);
227
228 if (*val == UpdateRestrictions::kNone) {
229 return "None";
230 }
231 string retval = "Flags:";
232 if (*val & kRestrictDownloading) {
233 retval += " RestrictDownloading";
234 }
235 return retval;
236}
237
Adolfo Victoria94ffe132018-06-28 16:14:56 -0700238template <>
239string BoxedValue::ValuePrinter<WeeklyTimeInterval>(const void* value) {
240 const WeeklyTimeInterval* val =
241 reinterpret_cast<const WeeklyTimeInterval*>(value);
242 return val->ToString();
243}
244
245template <>
246string BoxedValue::ValuePrinter<WeeklyTimeIntervalVector>(const void* value) {
247 const WeeklyTimeIntervalVector* val =
248 reinterpret_cast<const WeeklyTimeIntervalVector*>(value);
249
250 string retval = "Disallowed intervals:\n";
251 for (const auto& interval : *val) {
252 retval += interval.ToString() + "\n";
253 }
254 return retval;
255}
256
Alex Deymo63784a52014-05-28 10:46:14 -0700257} // namespace chromeos_update_manager