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