Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 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 Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 16 | |
Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 17 | #include "update_engine/update_manager/boxed_value.h" |
| 18 | |
Ben Chan | dcf5a1d | 2014-08-07 09:34:12 -0700 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 21 | #include <set> |
| 22 | #include <string> |
| 23 | |
| 24 | #include <base/strings/string_number_conversions.h> |
| 25 | #include <base/time/time.h> |
| 26 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 27 | #include "update_engine/common/utils.h" |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 28 | #include "update_engine/connection_utils.h" |
Marton Hunyady | 0e0e354 | 2018-02-21 18:51:39 +0100 | [diff] [blame] | 29 | #include "update_engine/update_manager/rollback_prefs.h" |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 30 | #include "update_engine/update_manager/shill_provider.h" |
| 31 | #include "update_engine/update_manager/updater_provider.h" |
Adolfo Victoria | 94ffe13 | 2018-06-28 16:14:56 -0700 | [diff] [blame] | 32 | #include "update_engine/update_manager/weekly_time.h" |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 33 | |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 34 | using chromeos_update_engine::ConnectionTethering; |
| 35 | using chromeos_update_engine::ConnectionType; |
| 36 | using chromeos_update_engine::connection_utils::StringForConnectionType; |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 37 | using std::set; |
| 38 | using std::string; |
| 39 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 40 | namespace chromeos_update_manager { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 41 | |
| 42 | // Template instantiation for common types; used in BoxedValue::ToString(). |
| 43 | // Keep in sync with boxed_value_unitttest.cc. |
| 44 | |
| 45 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 46 | string BoxedValue::ValuePrinter<string>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 47 | const string* val = reinterpret_cast<const string*>(value); |
| 48 | return *val; |
| 49 | } |
| 50 | |
| 51 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 52 | string BoxedValue::ValuePrinter<int>(const void* value) { |
Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 53 | const int* val = reinterpret_cast<const int*>(value); |
| 54 | return base::IntToString(*val); |
| 55 | } |
| 56 | |
| 57 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 58 | string BoxedValue::ValuePrinter<unsigned int>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 59 | const unsigned int* val = reinterpret_cast<const unsigned int*>(value); |
| 60 | return base::UintToString(*val); |
| 61 | } |
| 62 | |
| 63 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 64 | string BoxedValue::ValuePrinter<int64_t>(const void* value) { |
Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 65 | const int64_t* val = reinterpret_cast<const int64_t*>(value); |
| 66 | return base::Int64ToString(*val); |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 70 | string BoxedValue::ValuePrinter<uint64_t>(const void* value) { |
Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 71 | const uint64_t* val = |
| 72 | reinterpret_cast<const uint64_t*>(value); |
Ben Chan | dcf5a1d | 2014-08-07 09:34:12 -0700 | [diff] [blame] | 73 | return base::Uint64ToString(static_cast<uint64_t>(*val)); |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 77 | string BoxedValue::ValuePrinter<bool>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 78 | const bool* val = reinterpret_cast<const bool*>(value); |
| 79 | return *val ? "true" : "false"; |
| 80 | } |
| 81 | |
| 82 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 83 | string BoxedValue::ValuePrinter<double>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 84 | const double* val = reinterpret_cast<const double*>(value); |
| 85 | return base::DoubleToString(*val); |
| 86 | } |
| 87 | |
| 88 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 89 | string BoxedValue::ValuePrinter<base::Time>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 90 | const base::Time* val = reinterpret_cast<const base::Time*>(value); |
| 91 | return chromeos_update_engine::utils::ToString(*val); |
| 92 | } |
| 93 | |
| 94 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 95 | string BoxedValue::ValuePrinter<base::TimeDelta>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 96 | const base::TimeDelta* val = reinterpret_cast<const base::TimeDelta*>(value); |
| 97 | return chromeos_update_engine::utils::FormatTimeDelta(*val); |
| 98 | } |
| 99 | |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 100 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 101 | string BoxedValue::ValuePrinter<ConnectionType>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 102 | const ConnectionType* val = reinterpret_cast<const ConnectionType*>(value); |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 103 | return StringForConnectionType(*val); |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 107 | string BoxedValue::ValuePrinter<set<ConnectionType>>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 108 | string ret = ""; |
| 109 | const set<ConnectionType>* val = |
| 110 | reinterpret_cast<const set<ConnectionType>*>(value); |
| 111 | for (auto& it : *val) { |
| 112 | ConnectionType type = it; |
| 113 | if (ret.size() > 0) |
| 114 | ret += ","; |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 115 | ret += StringForConnectionType(type); |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 116 | } |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 121 | string BoxedValue::ValuePrinter<ConnectionTethering>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 122 | const ConnectionTethering* val = |
| 123 | reinterpret_cast<const ConnectionTethering*>(value); |
| 124 | switch (*val) { |
| 125 | case ConnectionTethering::kNotDetected: |
| 126 | return "Not Detected"; |
| 127 | case ConnectionTethering::kSuspected: |
| 128 | return "Suspected"; |
| 129 | case ConnectionTethering::kConfirmed: |
| 130 | return "Confirmed"; |
| 131 | case ConnectionTethering::kUnknown: |
| 132 | return "Unknown"; |
| 133 | } |
| 134 | NOTREACHED(); |
| 135 | return "Unknown"; |
| 136 | } |
| 137 | |
Marton Hunyady | 0e0e354 | 2018-02-21 18:51:39 +0100 | [diff] [blame] | 138 | template <> |
| 139 | string BoxedValue::ValuePrinter<RollbackToTargetVersion>(const void* value) { |
| 140 | const RollbackToTargetVersion* val = |
| 141 | reinterpret_cast<const RollbackToTargetVersion*>(value); |
| 142 | switch (*val) { |
| 143 | case RollbackToTargetVersion::kUnspecified: |
| 144 | return "Unspecified"; |
| 145 | case RollbackToTargetVersion::kDisabled: |
| 146 | return "Disabled"; |
| 147 | case RollbackToTargetVersion::kRollbackWithFullPowerwash: |
| 148 | return "Rollback with full powerwash"; |
| 149 | case RollbackToTargetVersion::kMaxValue: |
| 150 | NOTREACHED(); |
| 151 | return "Max value"; |
| 152 | } |
| 153 | NOTREACHED(); |
| 154 | return "Unknown"; |
| 155 | } |
| 156 | |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 157 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 158 | string BoxedValue::ValuePrinter<Stage>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 159 | const Stage* val = reinterpret_cast<const Stage*>(value); |
| 160 | switch (*val) { |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 161 | case Stage::kIdle: |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 162 | return "Idle"; |
| 163 | case Stage::kCheckingForUpdate: |
| 164 | return "Checking For Update"; |
| 165 | case Stage::kUpdateAvailable: |
| 166 | return "Update Available"; |
| 167 | case Stage::kDownloading: |
| 168 | return "Downloading"; |
| 169 | case Stage::kVerifying: |
| 170 | return "Verifying"; |
| 171 | case Stage::kFinalizing: |
| 172 | return "Finalizing"; |
| 173 | case Stage::kUpdatedNeedReboot: |
| 174 | return "Updated, Need Reboot"; |
| 175 | case Stage::kReportingErrorEvent: |
| 176 | return "Reporting Error Event"; |
| 177 | case Stage::kAttemptingRollback: |
| 178 | return "Attempting Rollback"; |
| 179 | } |
| 180 | NOTREACHED(); |
| 181 | return "Unknown"; |
| 182 | } |
| 183 | |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 184 | template<> |
| 185 | string BoxedValue::ValuePrinter<UpdateRequestStatus>(const void* value) { |
| 186 | const UpdateRequestStatus* val = |
| 187 | reinterpret_cast<const UpdateRequestStatus*>(value); |
| 188 | switch (*val) { |
| 189 | case UpdateRequestStatus::kNone: |
| 190 | return "None"; |
| 191 | case UpdateRequestStatus::kInteractive: |
| 192 | return "Interactive"; |
| 193 | case UpdateRequestStatus::kPeriodic: |
| 194 | return "Periodic"; |
| 195 | } |
| 196 | NOTREACHED(); |
| 197 | return "Unknown"; |
| 198 | } |
| 199 | |
Aaron Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 200 | template <> |
| 201 | string BoxedValue::ValuePrinter<UpdateRestrictions>(const void* value) { |
| 202 | const UpdateRestrictions* val = |
| 203 | reinterpret_cast<const UpdateRestrictions*>(value); |
| 204 | |
| 205 | if (*val == UpdateRestrictions::kNone) { |
| 206 | return "None"; |
| 207 | } |
| 208 | string retval = "Flags:"; |
| 209 | if (*val & kRestrictDownloading) { |
| 210 | retval += " RestrictDownloading"; |
| 211 | } |
| 212 | return retval; |
| 213 | } |
| 214 | |
Adolfo Victoria | 94ffe13 | 2018-06-28 16:14:56 -0700 | [diff] [blame] | 215 | template <> |
| 216 | string BoxedValue::ValuePrinter<WeeklyTimeInterval>(const void* value) { |
| 217 | const WeeklyTimeInterval* val = |
| 218 | reinterpret_cast<const WeeklyTimeInterval*>(value); |
| 219 | return val->ToString(); |
| 220 | } |
| 221 | |
| 222 | template <> |
| 223 | string BoxedValue::ValuePrinter<WeeklyTimeIntervalVector>(const void* value) { |
| 224 | const WeeklyTimeIntervalVector* val = |
| 225 | reinterpret_cast<const WeeklyTimeIntervalVector*>(value); |
| 226 | |
| 227 | string retval = "Disallowed intervals:\n"; |
| 228 | for (const auto& interval : *val) { |
| 229 | retval += interval.ToString() + "\n"; |
| 230 | } |
| 231 | return retval; |
| 232 | } |
| 233 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 234 | } // namespace chromeos_update_manager |