blob: 4dff9efaf13bde7bdf9e6eb9debeb605d62e8518 [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);
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -070054 return base::NumberToString(*val);
Alex Deymof967ebe2014-05-05 14:46:17 -070055}
56
Amin Hassani4b717432019-01-14 16:24:20 -080057template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070058string BoxedValue::ValuePrinter<unsigned int>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070059 const unsigned int* val = reinterpret_cast<const unsigned int*>(value);
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -070060 return base::NumberToString(*val);
David Zeuthenfe225c12014-04-29 10:37:35 -070061}
62
Amin Hassani4b717432019-01-14 16:24:20 -080063template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070064string BoxedValue::ValuePrinter<int64_t>(const void* value) {
Alex Deymof967ebe2014-05-05 14:46:17 -070065 const int64_t* val = reinterpret_cast<const int64_t*>(value);
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -070066 return base::NumberToString(*val);
David Zeuthenfe225c12014-04-29 10:37:35 -070067}
68
Amin Hassani4b717432019-01-14 16:24:20 -080069template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070070string BoxedValue::ValuePrinter<uint64_t>(const void* value) {
Amin Hassani4b717432019-01-14 16:24:20 -080071 const uint64_t* val = reinterpret_cast<const uint64_t*>(value);
Amin Hassani2e4eda52019-01-07 14:01:17 -080072 return base::NumberToString(*val);
David Zeuthenfe225c12014-04-29 10:37:35 -070073}
74
Amin Hassani4b717432019-01-14 16:24:20 -080075template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070076string BoxedValue::ValuePrinter<bool>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070077 const bool* val = reinterpret_cast<const bool*>(value);
78 return *val ? "true" : "false";
79}
80
Amin Hassani4b717432019-01-14 16:24:20 -080081template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070082string BoxedValue::ValuePrinter<double>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070083 const double* val = reinterpret_cast<const double*>(value);
Jakub Pawlowski7e1dcf72018-07-26 00:29:42 -070084 return base::NumberToString(*val);
David Zeuthenfe225c12014-04-29 10:37:35 -070085}
86
Amin Hassani4b717432019-01-14 16:24:20 -080087template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070088string BoxedValue::ValuePrinter<base::Time>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070089 const base::Time* val = reinterpret_cast<const base::Time*>(value);
90 return chromeos_update_engine::utils::ToString(*val);
91}
92
Amin Hassani4b717432019-01-14 16:24:20 -080093template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070094string BoxedValue::ValuePrinter<base::TimeDelta>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070095 const base::TimeDelta* val = reinterpret_cast<const base::TimeDelta*>(value);
96 return chromeos_update_engine::utils::FormatTimeDelta(*val);
97}
98
Amin Hassani4b717432019-01-14 16:24:20 -080099template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700100string BoxedValue::ValuePrinter<ConnectionType>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700101 const ConnectionType* val = reinterpret_cast<const ConnectionType*>(value);
Sen Jiang255e22b2016-05-20 16:15:29 -0700102 return StringForConnectionType(*val);
David Zeuthenfe225c12014-04-29 10:37:35 -0700103}
104
Amin Hassani4b717432019-01-14 16:24:20 -0800105template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700106string BoxedValue::ValuePrinter<set<ConnectionType>>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700107 string ret = "";
108 const set<ConnectionType>* val =
109 reinterpret_cast<const set<ConnectionType>*>(value);
110 for (auto& it : *val) {
111 ConnectionType type = it;
112 if (ret.size() > 0)
113 ret += ",";
Sen Jiang255e22b2016-05-20 16:15:29 -0700114 ret += StringForConnectionType(type);
David Zeuthenfe225c12014-04-29 10:37:35 -0700115 }
116 return ret;
117}
118
Amin Hassani4b717432019-01-14 16:24:20 -0800119template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700120string BoxedValue::ValuePrinter<ConnectionTethering>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700121 const ConnectionTethering* val =
122 reinterpret_cast<const ConnectionTethering*>(value);
123 switch (*val) {
124 case ConnectionTethering::kNotDetected:
125 return "Not Detected";
126 case ConnectionTethering::kSuspected:
127 return "Suspected";
128 case ConnectionTethering::kConfirmed:
129 return "Confirmed";
130 case ConnectionTethering::kUnknown:
131 return "Unknown";
132 }
133 NOTREACHED();
134 return "Unknown";
135}
136
Marton Hunyady0e0e3542018-02-21 18:51:39 +0100137template <>
138string BoxedValue::ValuePrinter<RollbackToTargetVersion>(const void* value) {
139 const RollbackToTargetVersion* val =
140 reinterpret_cast<const RollbackToTargetVersion*>(value);
141 switch (*val) {
142 case RollbackToTargetVersion::kUnspecified:
143 return "Unspecified";
144 case RollbackToTargetVersion::kDisabled:
145 return "Disabled";
Marton Hunyadyd4bc4622018-08-30 15:52:23 +0200146 case RollbackToTargetVersion::kRollbackAndPowerwash:
147 return "Rollback and powerwash";
Marton Hunyady31aefb92018-08-29 16:17:03 +0200148 case RollbackToTargetVersion::kRollbackAndRestoreIfPossible:
149 return "Rollback and restore if possible";
Marton Hunyady0e0e3542018-02-21 18:51:39 +0100150 case RollbackToTargetVersion::kMaxValue:
151 NOTREACHED();
152 return "Max value";
153 }
154 NOTREACHED();
155 return "Unknown";
156}
157
Amin Hassani4b717432019-01-14 16:24:20 -0800158template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700159string BoxedValue::ValuePrinter<Stage>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700160 const Stage* val = reinterpret_cast<const Stage*>(value);
161 switch (*val) {
Alex Vakulenko072359c2014-07-18 11:41:07 -0700162 case Stage::kIdle:
David Zeuthenfe225c12014-04-29 10:37:35 -0700163 return "Idle";
164 case Stage::kCheckingForUpdate:
165 return "Checking For Update";
166 case Stage::kUpdateAvailable:
167 return "Update Available";
168 case Stage::kDownloading:
169 return "Downloading";
170 case Stage::kVerifying:
171 return "Verifying";
172 case Stage::kFinalizing:
173 return "Finalizing";
174 case Stage::kUpdatedNeedReboot:
175 return "Updated, Need Reboot";
176 case Stage::kReportingErrorEvent:
177 return "Reporting Error Event";
178 case Stage::kAttemptingRollback:
179 return "Attempting Rollback";
180 }
181 NOTREACHED();
182 return "Unknown";
183}
184
Amin Hassani4b717432019-01-14 16:24:20 -0800185template <>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700186string BoxedValue::ValuePrinter<UpdateRequestStatus>(const void* value) {
187 const UpdateRequestStatus* val =
188 reinterpret_cast<const UpdateRequestStatus*>(value);
189 switch (*val) {
190 case UpdateRequestStatus::kNone:
191 return "None";
192 case UpdateRequestStatus::kInteractive:
193 return "Interactive";
194 case UpdateRequestStatus::kPeriodic:
195 return "Periodic";
196 }
197 NOTREACHED();
198 return "Unknown";
199}
200
Aaron Woodbf5a2522017-10-04 10:58:36 -0700201template <>
202string BoxedValue::ValuePrinter<UpdateRestrictions>(const void* value) {
203 const UpdateRestrictions* val =
204 reinterpret_cast<const UpdateRestrictions*>(value);
205
206 if (*val == UpdateRestrictions::kNone) {
207 return "None";
208 }
209 string retval = "Flags:";
210 if (*val & kRestrictDownloading) {
211 retval += " RestrictDownloading";
212 }
213 return retval;
214}
215
Adolfo Victoria94ffe132018-06-28 16:14:56 -0700216template <>
217string BoxedValue::ValuePrinter<WeeklyTimeInterval>(const void* value) {
218 const WeeklyTimeInterval* val =
219 reinterpret_cast<const WeeklyTimeInterval*>(value);
220 return val->ToString();
221}
222
223template <>
224string BoxedValue::ValuePrinter<WeeklyTimeIntervalVector>(const void* value) {
225 const WeeklyTimeIntervalVector* val =
226 reinterpret_cast<const WeeklyTimeIntervalVector*>(value);
227
228 string retval = "Disallowed intervals:\n";
229 for (const auto& interval : *val) {
230 retval += interval.ToString() + "\n";
231 }
232 return retval;
233}
234
Alex Deymo63784a52014-05-28 10:46:14 -0700235} // namespace chromeos_update_manager