David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 1 | // Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ben Chan | dcf5a1d | 2014-08-07 09:34:12 -0700 | [diff] [blame] | 5 | #include <stdint.h> |
| 6 | |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 7 | #include <set> |
| 8 | #include <string> |
| 9 | |
| 10 | #include <base/strings/string_number_conversions.h> |
| 11 | #include <base/time/time.h> |
| 12 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 13 | #include "update_engine/update_manager/boxed_value.h" |
| 14 | #include "update_engine/update_manager/shill_provider.h" |
| 15 | #include "update_engine/update_manager/updater_provider.h" |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 16 | #include "update_engine/utils.h" |
| 17 | |
| 18 | using std::set; |
| 19 | using std::string; |
| 20 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 21 | namespace chromeos_update_manager { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 22 | |
| 23 | // Template instantiation for common types; used in BoxedValue::ToString(). |
| 24 | // Keep in sync with boxed_value_unitttest.cc. |
| 25 | |
| 26 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame^] | 27 | string BoxedValue::ValuePrinter<string>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 28 | const string* val = reinterpret_cast<const string*>(value); |
| 29 | return *val; |
| 30 | } |
| 31 | |
| 32 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame^] | 33 | string BoxedValue::ValuePrinter<int>(const void* value) { |
Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 34 | const int* val = reinterpret_cast<const int*>(value); |
| 35 | return base::IntToString(*val); |
| 36 | } |
| 37 | |
| 38 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame^] | 39 | string BoxedValue::ValuePrinter<unsigned int>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 40 | const unsigned int* val = reinterpret_cast<const unsigned int*>(value); |
| 41 | return base::UintToString(*val); |
| 42 | } |
| 43 | |
| 44 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame^] | 45 | string BoxedValue::ValuePrinter<int64_t>(const void* value) { |
Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 46 | const int64_t* val = reinterpret_cast<const int64_t*>(value); |
| 47 | return base::Int64ToString(*val); |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame^] | 51 | string BoxedValue::ValuePrinter<uint64_t>(const void* value) { |
Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 52 | const uint64_t* val = |
| 53 | reinterpret_cast<const uint64_t*>(value); |
Ben Chan | dcf5a1d | 2014-08-07 09:34:12 -0700 | [diff] [blame] | 54 | return base::Uint64ToString(static_cast<uint64_t>(*val)); |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame^] | 58 | string BoxedValue::ValuePrinter<bool>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 59 | const bool* val = reinterpret_cast<const bool*>(value); |
| 60 | return *val ? "true" : "false"; |
| 61 | } |
| 62 | |
| 63 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame^] | 64 | string BoxedValue::ValuePrinter<double>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 65 | const double* val = reinterpret_cast<const double*>(value); |
| 66 | return base::DoubleToString(*val); |
| 67 | } |
| 68 | |
| 69 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame^] | 70 | string BoxedValue::ValuePrinter<base::Time>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 71 | const base::Time* val = reinterpret_cast<const base::Time*>(value); |
| 72 | return chromeos_update_engine::utils::ToString(*val); |
| 73 | } |
| 74 | |
| 75 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame^] | 76 | string BoxedValue::ValuePrinter<base::TimeDelta>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 77 | const base::TimeDelta* val = reinterpret_cast<const base::TimeDelta*>(value); |
| 78 | return chromeos_update_engine::utils::FormatTimeDelta(*val); |
| 79 | } |
| 80 | |
| 81 | static std::string ConnectionTypeToString(ConnectionType type) { |
| 82 | switch (type) { |
| 83 | case ConnectionType::kEthernet: |
| 84 | return "Ethernet"; |
| 85 | case ConnectionType::kWifi: |
| 86 | return "Wifi"; |
| 87 | case ConnectionType::kWimax: |
| 88 | return "Wimax"; |
| 89 | case ConnectionType::kBluetooth: |
| 90 | return "Bluetooth"; |
| 91 | case ConnectionType::kCellular: |
| 92 | return "Cellular"; |
| 93 | case ConnectionType::kUnknown: |
| 94 | return "Unknown"; |
| 95 | } |
| 96 | NOTREACHED(); |
| 97 | return "Unknown"; |
| 98 | } |
| 99 | |
| 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); |
| 103 | return ConnectionTypeToString(*val); |
| 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 += ","; |
| 115 | ret += ConnectionTypeToString(type); |
| 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 | |
| 138 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame^] | 139 | string BoxedValue::ValuePrinter<Stage>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 140 | const Stage* val = reinterpret_cast<const Stage*>(value); |
| 141 | switch (*val) { |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 142 | case Stage::kIdle: |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 143 | return "Idle"; |
| 144 | case Stage::kCheckingForUpdate: |
| 145 | return "Checking For Update"; |
| 146 | case Stage::kUpdateAvailable: |
| 147 | return "Update Available"; |
| 148 | case Stage::kDownloading: |
| 149 | return "Downloading"; |
| 150 | case Stage::kVerifying: |
| 151 | return "Verifying"; |
| 152 | case Stage::kFinalizing: |
| 153 | return "Finalizing"; |
| 154 | case Stage::kUpdatedNeedReboot: |
| 155 | return "Updated, Need Reboot"; |
| 156 | case Stage::kReportingErrorEvent: |
| 157 | return "Reporting Error Event"; |
| 158 | case Stage::kAttemptingRollback: |
| 159 | return "Attempting Rollback"; |
| 160 | } |
| 161 | NOTREACHED(); |
| 162 | return "Unknown"; |
| 163 | } |
| 164 | |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame^] | 165 | template<> |
| 166 | string BoxedValue::ValuePrinter<UpdateRequestStatus>(const void* value) { |
| 167 | const UpdateRequestStatus* val = |
| 168 | reinterpret_cast<const UpdateRequestStatus*>(value); |
| 169 | switch (*val) { |
| 170 | case UpdateRequestStatus::kNone: |
| 171 | return "None"; |
| 172 | case UpdateRequestStatus::kInteractive: |
| 173 | return "Interactive"; |
| 174 | case UpdateRequestStatus::kPeriodic: |
| 175 | return "Periodic"; |
| 176 | } |
| 177 | NOTREACHED(); |
| 178 | return "Unknown"; |
| 179 | } |
| 180 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 181 | } // namespace chromeos_update_manager |