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" |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 29 | #include "update_engine/update_manager/shill_provider.h" |
| 30 | #include "update_engine/update_manager/updater_provider.h" |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 31 | |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 32 | using chromeos_update_engine::ConnectionTethering; |
| 33 | using chromeos_update_engine::ConnectionType; |
| 34 | using chromeos_update_engine::connection_utils::StringForConnectionType; |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 35 | using std::set; |
| 36 | using std::string; |
| 37 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 38 | namespace chromeos_update_manager { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 39 | |
| 40 | // Template instantiation for common types; used in BoxedValue::ToString(). |
| 41 | // Keep in sync with boxed_value_unitttest.cc. |
| 42 | |
| 43 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 44 | string BoxedValue::ValuePrinter<string>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 45 | const string* val = reinterpret_cast<const string*>(value); |
| 46 | return *val; |
| 47 | } |
| 48 | |
| 49 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 50 | string BoxedValue::ValuePrinter<int>(const void* value) { |
Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 51 | const int* val = reinterpret_cast<const int*>(value); |
| 52 | return base::IntToString(*val); |
| 53 | } |
| 54 | |
| 55 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 56 | string BoxedValue::ValuePrinter<unsigned int>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 57 | const unsigned int* val = reinterpret_cast<const unsigned int*>(value); |
| 58 | return base::UintToString(*val); |
| 59 | } |
| 60 | |
| 61 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 62 | string BoxedValue::ValuePrinter<int64_t>(const void* value) { |
Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 63 | const int64_t* val = reinterpret_cast<const int64_t*>(value); |
| 64 | return base::Int64ToString(*val); |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 68 | string BoxedValue::ValuePrinter<uint64_t>(const void* value) { |
Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 69 | const uint64_t* val = |
| 70 | reinterpret_cast<const uint64_t*>(value); |
Ben Chan | dcf5a1d | 2014-08-07 09:34:12 -0700 | [diff] [blame] | 71 | return base::Uint64ToString(static_cast<uint64_t>(*val)); |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 75 | string BoxedValue::ValuePrinter<bool>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 76 | const bool* val = reinterpret_cast<const bool*>(value); |
| 77 | return *val ? "true" : "false"; |
| 78 | } |
| 79 | |
| 80 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 81 | string BoxedValue::ValuePrinter<double>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 82 | const double* val = reinterpret_cast<const double*>(value); |
| 83 | return base::DoubleToString(*val); |
| 84 | } |
| 85 | |
| 86 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 87 | string BoxedValue::ValuePrinter<base::Time>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 88 | const base::Time* val = reinterpret_cast<const base::Time*>(value); |
| 89 | return chromeos_update_engine::utils::ToString(*val); |
| 90 | } |
| 91 | |
| 92 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 93 | string BoxedValue::ValuePrinter<base::TimeDelta>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 94 | const base::TimeDelta* val = reinterpret_cast<const base::TimeDelta*>(value); |
| 95 | return chromeos_update_engine::utils::FormatTimeDelta(*val); |
| 96 | } |
| 97 | |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 98 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 99 | string BoxedValue::ValuePrinter<ConnectionType>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 100 | const ConnectionType* val = reinterpret_cast<const ConnectionType*>(value); |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 101 | return StringForConnectionType(*val); |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 105 | string BoxedValue::ValuePrinter<set<ConnectionType>>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 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 += ","; |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 113 | ret += StringForConnectionType(type); |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 114 | } |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | template<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 119 | string BoxedValue::ValuePrinter<ConnectionTethering>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 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<> |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 137 | string BoxedValue::ValuePrinter<Stage>(const void* value) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 138 | const Stage* val = reinterpret_cast<const Stage*>(value); |
| 139 | switch (*val) { |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 140 | case Stage::kIdle: |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 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 | |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 163 | template<> |
| 164 | string BoxedValue::ValuePrinter<UpdateRequestStatus>(const void* value) { |
| 165 | const UpdateRequestStatus* val = |
| 166 | reinterpret_cast<const UpdateRequestStatus*>(value); |
| 167 | switch (*val) { |
| 168 | case UpdateRequestStatus::kNone: |
| 169 | return "None"; |
| 170 | case UpdateRequestStatus::kInteractive: |
| 171 | return "Interactive"; |
| 172 | case UpdateRequestStatus::kPeriodic: |
| 173 | return "Periodic"; |
| 174 | } |
| 175 | NOTREACHED(); |
| 176 | return "Unknown"; |
| 177 | } |
| 178 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 179 | } // namespace chromeos_update_manager |