blob: 0acc8cd25ee48e3de64981c9cc466e35cf791518 [file] [log] [blame]
David Zeuthenfe225c12014-04-29 10:37:35 -07001// 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 Deymoaab50e32014-11-10 19:55:35 -08005#include "update_engine/update_manager/boxed_value.h"
6
Ben Chandcf5a1d2014-08-07 09:34:12 -07007#include <stdint.h>
8
David Zeuthenfe225c12014-04-29 10:37:35 -07009#include <set>
10#include <string>
11
12#include <base/strings/string_number_conversions.h>
13#include <base/time/time.h>
14
Alex Deymo63784a52014-05-28 10:46:14 -070015#include "update_engine/update_manager/shill_provider.h"
16#include "update_engine/update_manager/updater_provider.h"
David Zeuthenfe225c12014-04-29 10:37:35 -070017#include "update_engine/utils.h"
18
19using std::set;
20using std::string;
21
Alex Deymo63784a52014-05-28 10:46:14 -070022namespace chromeos_update_manager {
David Zeuthenfe225c12014-04-29 10:37:35 -070023
24// Template instantiation for common types; used in BoxedValue::ToString().
25// Keep in sync with boxed_value_unitttest.cc.
26
27template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070028string BoxedValue::ValuePrinter<string>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070029 const string* val = reinterpret_cast<const string*>(value);
30 return *val;
31}
32
33template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070034string BoxedValue::ValuePrinter<int>(const void* value) {
Alex Deymof967ebe2014-05-05 14:46:17 -070035 const int* val = reinterpret_cast<const int*>(value);
36 return base::IntToString(*val);
37}
38
39template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070040string BoxedValue::ValuePrinter<unsigned int>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070041 const unsigned int* val = reinterpret_cast<const unsigned int*>(value);
42 return base::UintToString(*val);
43}
44
45template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070046string BoxedValue::ValuePrinter<int64_t>(const void* value) {
Alex Deymof967ebe2014-05-05 14:46:17 -070047 const int64_t* val = reinterpret_cast<const int64_t*>(value);
48 return base::Int64ToString(*val);
David Zeuthenfe225c12014-04-29 10:37:35 -070049}
50
51template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070052string BoxedValue::ValuePrinter<uint64_t>(const void* value) {
Alex Deymof967ebe2014-05-05 14:46:17 -070053 const uint64_t* val =
54 reinterpret_cast<const uint64_t*>(value);
Ben Chandcf5a1d2014-08-07 09:34:12 -070055 return base::Uint64ToString(static_cast<uint64_t>(*val));
David Zeuthenfe225c12014-04-29 10:37:35 -070056}
57
58template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070059string BoxedValue::ValuePrinter<bool>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070060 const bool* val = reinterpret_cast<const bool*>(value);
61 return *val ? "true" : "false";
62}
63
64template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070065string BoxedValue::ValuePrinter<double>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070066 const double* val = reinterpret_cast<const double*>(value);
67 return base::DoubleToString(*val);
68}
69
70template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070071string BoxedValue::ValuePrinter<base::Time>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070072 const base::Time* val = reinterpret_cast<const base::Time*>(value);
73 return chromeos_update_engine::utils::ToString(*val);
74}
75
76template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070077string BoxedValue::ValuePrinter<base::TimeDelta>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070078 const base::TimeDelta* val = reinterpret_cast<const base::TimeDelta*>(value);
79 return chromeos_update_engine::utils::FormatTimeDelta(*val);
80}
81
Alex Deymof329b932014-10-30 01:37:48 -070082static string ConnectionTypeToString(ConnectionType type) {
David Zeuthenfe225c12014-04-29 10:37:35 -070083 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
101template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700102string BoxedValue::ValuePrinter<ConnectionType>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700103 const ConnectionType* val = reinterpret_cast<const ConnectionType*>(value);
104 return ConnectionTypeToString(*val);
105}
106
107template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700108string BoxedValue::ValuePrinter<set<ConnectionType>>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700109 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
121template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700122string BoxedValue::ValuePrinter<ConnectionTethering>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700123 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
139template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700140string BoxedValue::ValuePrinter<Stage>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700141 const Stage* val = reinterpret_cast<const Stage*>(value);
142 switch (*val) {
Alex Vakulenko072359c2014-07-18 11:41:07 -0700143 case Stage::kIdle:
David Zeuthenfe225c12014-04-29 10:37:35 -0700144 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 Arnoldec7f9162014-07-15 13:24:46 -0700166template<>
167string 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 Deymo63784a52014-05-28 10:46:14 -0700182} // namespace chromeos_update_manager