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 | |
Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame^] | 5 | #include "update_engine/update_manager/boxed_value.h" |
| 6 | |
Ben Chan | dcf5a1d | 2014-08-07 09:34:12 -0700 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 9 | #include <set> |
| 10 | #include <string> |
| 11 | |
| 12 | #include <base/strings/string_number_conversions.h> |
| 13 | #include <base/time/time.h> |
| 14 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 15 | #include "update_engine/update_manager/shill_provider.h" |
| 16 | #include "update_engine/update_manager/updater_provider.h" |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 17 | #include "update_engine/utils.h" |
| 18 | |
| 19 | using std::set; |
| 20 | using std::string; |
| 21 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 22 | namespace chromeos_update_manager { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 23 | |
| 24 | // Template instantiation for common types; used in BoxedValue::ToString(). |
| 25 | // Keep in sync with boxed_value_unitttest.cc. |
| 26 | |
| 27 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 28 | string BoxedValue::ValuePrinter<string>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 29 | const string* val = reinterpret_cast<const string*>(value); |
| 30 | return *val; |
| 31 | } |
| 32 | |
| 33 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 34 | string BoxedValue::ValuePrinter<int>(const void* value) { |
Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 35 | const int* val = reinterpret_cast<const int*>(value); |
| 36 | return base::IntToString(*val); |
| 37 | } |
| 38 | |
| 39 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 40 | string BoxedValue::ValuePrinter<unsigned int>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 41 | const unsigned int* val = reinterpret_cast<const unsigned int*>(value); |
| 42 | return base::UintToString(*val); |
| 43 | } |
| 44 | |
| 45 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 46 | string BoxedValue::ValuePrinter<int64_t>(const void* value) { |
Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 47 | const int64_t* val = reinterpret_cast<const int64_t*>(value); |
| 48 | return base::Int64ToString(*val); |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 52 | string BoxedValue::ValuePrinter<uint64_t>(const void* value) { |
Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 53 | const uint64_t* val = |
| 54 | reinterpret_cast<const uint64_t*>(value); |
Ben Chan | dcf5a1d | 2014-08-07 09:34:12 -0700 | [diff] [blame] | 55 | return base::Uint64ToString(static_cast<uint64_t>(*val)); |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 59 | string BoxedValue::ValuePrinter<bool>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 60 | const bool* val = reinterpret_cast<const bool*>(value); |
| 61 | return *val ? "true" : "false"; |
| 62 | } |
| 63 | |
| 64 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 65 | string BoxedValue::ValuePrinter<double>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 66 | const double* val = reinterpret_cast<const double*>(value); |
| 67 | return base::DoubleToString(*val); |
| 68 | } |
| 69 | |
| 70 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 71 | string BoxedValue::ValuePrinter<base::Time>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 72 | const base::Time* val = reinterpret_cast<const base::Time*>(value); |
| 73 | return chromeos_update_engine::utils::ToString(*val); |
| 74 | } |
| 75 | |
| 76 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 77 | string BoxedValue::ValuePrinter<base::TimeDelta>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 78 | const base::TimeDelta* val = reinterpret_cast<const base::TimeDelta*>(value); |
| 79 | return chromeos_update_engine::utils::FormatTimeDelta(*val); |
| 80 | } |
| 81 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 82 | static string ConnectionTypeToString(ConnectionType type) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 83 | switch (type) { |
| 84 | case ConnectionType::kEthernet: |
| 85 | return "Ethernet"; |
| 86 | case ConnectionType::kWifi: |
| 87 | return "Wifi"; |
| 88 | case ConnectionType::kWimax: |
| 89 | return "Wimax"; |
| 90 | case ConnectionType::kBluetooth: |
| 91 | return "Bluetooth"; |
| 92 | case ConnectionType::kCellular: |
| 93 | return "Cellular"; |
| 94 | case ConnectionType::kUnknown: |
| 95 | return "Unknown"; |
| 96 | } |
| 97 | NOTREACHED(); |
| 98 | return "Unknown"; |
| 99 | } |
| 100 | |
| 101 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 102 | string BoxedValue::ValuePrinter<ConnectionType>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 103 | const ConnectionType* val = reinterpret_cast<const ConnectionType*>(value); |
| 104 | return ConnectionTypeToString(*val); |
| 105 | } |
| 106 | |
| 107 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 108 | string BoxedValue::ValuePrinter<set<ConnectionType>>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 109 | string ret = ""; |
| 110 | const set<ConnectionType>* val = |
| 111 | reinterpret_cast<const set<ConnectionType>*>(value); |
| 112 | for (auto& it : *val) { |
| 113 | ConnectionType type = it; |
| 114 | if (ret.size() > 0) |
| 115 | ret += ","; |
| 116 | ret += ConnectionTypeToString(type); |
| 117 | } |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 122 | string BoxedValue::ValuePrinter<ConnectionTethering>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 123 | const ConnectionTethering* val = |
| 124 | reinterpret_cast<const ConnectionTethering*>(value); |
| 125 | switch (*val) { |
| 126 | case ConnectionTethering::kNotDetected: |
| 127 | return "Not Detected"; |
| 128 | case ConnectionTethering::kSuspected: |
| 129 | return "Suspected"; |
| 130 | case ConnectionTethering::kConfirmed: |
| 131 | return "Confirmed"; |
| 132 | case ConnectionTethering::kUnknown: |
| 133 | return "Unknown"; |
| 134 | } |
| 135 | NOTREACHED(); |
| 136 | return "Unknown"; |
| 137 | } |
| 138 | |
| 139 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 140 | string BoxedValue::ValuePrinter<Stage>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 141 | const Stage* val = reinterpret_cast<const Stage*>(value); |
| 142 | switch (*val) { |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 143 | case Stage::kIdle: |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 144 | return "Idle"; |
| 145 | case Stage::kCheckingForUpdate: |
| 146 | return "Checking For Update"; |
| 147 | case Stage::kUpdateAvailable: |
| 148 | return "Update Available"; |
| 149 | case Stage::kDownloading: |
| 150 | return "Downloading"; |
| 151 | case Stage::kVerifying: |
| 152 | return "Verifying"; |
| 153 | case Stage::kFinalizing: |
| 154 | return "Finalizing"; |
| 155 | case Stage::kUpdatedNeedReboot: |
| 156 | return "Updated, Need Reboot"; |
| 157 | case Stage::kReportingErrorEvent: |
| 158 | return "Reporting Error Event"; |
| 159 | case Stage::kAttemptingRollback: |
| 160 | return "Attempting Rollback"; |
| 161 | } |
| 162 | NOTREACHED(); |
| 163 | return "Unknown"; |
| 164 | } |
| 165 | |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 166 | template<> |
| 167 | string BoxedValue::ValuePrinter<UpdateRequestStatus>(const void* value) { |
| 168 | const UpdateRequestStatus* val = |
| 169 | reinterpret_cast<const UpdateRequestStatus*>(value); |
| 170 | switch (*val) { |
| 171 | case UpdateRequestStatus::kNone: |
| 172 | return "None"; |
| 173 | case UpdateRequestStatus::kInteractive: |
| 174 | return "Interactive"; |
| 175 | case UpdateRequestStatus::kPeriodic: |
| 176 | return "Periodic"; |
| 177 | } |
| 178 | NOTREACHED(); |
| 179 | return "Unknown"; |
| 180 | } |
| 181 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 182 | } // namespace chromeos_update_manager |