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 // 16 17 #include "update_engine/update_manager/boxed_value.h" 18 19 #include <stdint.h> 20 21 #include <set> 22 #include <string> 23 24 #include <base/strings/string_number_conversions.h> 25 #include <base/time/time.h> 26 27 #include "update_engine/common/utils.h" 28 #include "update_engine/update_manager/shill_provider.h" 29 #include "update_engine/update_manager/updater_provider.h" 30 31 using std::set; 32 using std::string; 33 34 namespace chromeos_update_manager { 35 36 // Template instantiation for common types; used in BoxedValue::ToString(). 37 // Keep in sync with boxed_value_unitttest.cc. 38 39 template<> ValuePrinter(const void * value)40string BoxedValue::ValuePrinter<string>(const void* value) { 41 const string* val = reinterpret_cast<const string*>(value); 42 return *val; 43 } 44 45 template<> ValuePrinter(const void * value)46string BoxedValue::ValuePrinter<int>(const void* value) { 47 const int* val = reinterpret_cast<const int*>(value); 48 return base::IntToString(*val); 49 } 50 51 template<> ValuePrinter(const void * value)52string BoxedValue::ValuePrinter<unsigned int>(const void* value) { 53 const unsigned int* val = reinterpret_cast<const unsigned int*>(value); 54 return base::UintToString(*val); 55 } 56 57 template<> ValuePrinter(const void * value)58string BoxedValue::ValuePrinter<int64_t>(const void* value) { 59 const int64_t* val = reinterpret_cast<const int64_t*>(value); 60 return base::Int64ToString(*val); 61 } 62 63 template<> ValuePrinter(const void * value)64string BoxedValue::ValuePrinter<uint64_t>(const void* value) { 65 const uint64_t* val = 66 reinterpret_cast<const uint64_t*>(value); 67 return base::Uint64ToString(static_cast<uint64_t>(*val)); 68 } 69 70 template<> ValuePrinter(const void * value)71string BoxedValue::ValuePrinter<bool>(const void* value) { 72 const bool* val = reinterpret_cast<const bool*>(value); 73 return *val ? "true" : "false"; 74 } 75 76 template<> ValuePrinter(const void * value)77string BoxedValue::ValuePrinter<double>(const void* value) { 78 const double* val = reinterpret_cast<const double*>(value); 79 return base::DoubleToString(*val); 80 } 81 82 template<> ValuePrinter(const void * value)83string BoxedValue::ValuePrinter<base::Time>(const void* value) { 84 const base::Time* val = reinterpret_cast<const base::Time*>(value); 85 return chromeos_update_engine::utils::ToString(*val); 86 } 87 88 template<> ValuePrinter(const void * value)89string BoxedValue::ValuePrinter<base::TimeDelta>(const void* value) { 90 const base::TimeDelta* val = reinterpret_cast<const base::TimeDelta*>(value); 91 return chromeos_update_engine::utils::FormatTimeDelta(*val); 92 } 93 ConnectionTypeToString(ConnectionType type)94static string ConnectionTypeToString(ConnectionType type) { 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<> ValuePrinter(const void * value)114string BoxedValue::ValuePrinter<ConnectionType>(const void* value) { 115 const ConnectionType* val = reinterpret_cast<const ConnectionType*>(value); 116 return ConnectionTypeToString(*val); 117 } 118 119 template<> ValuePrinter(const void * value)120string BoxedValue::ValuePrinter<set<ConnectionType>>(const void* value) { 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<> ValuePrinter(const void * value)134string BoxedValue::ValuePrinter<ConnectionTethering>(const void* value) { 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<> ValuePrinter(const void * value)152string BoxedValue::ValuePrinter<Stage>(const void* value) { 153 const Stage* val = reinterpret_cast<const Stage*>(value); 154 switch (*val) { 155 case Stage::kIdle: 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 178 template<> ValuePrinter(const void * value)179string 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 194 } // namespace chromeos_update_manager 195