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