blob: 773431ce7c9efb3b19beb12ba2af9d408feeb263 [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
Ben Chandcf5a1d2014-08-07 09:34:12 -07005#include <stdint.h>
6
David Zeuthenfe225c12014-04-29 10:37:35 -07007#include <set>
8#include <string>
9
10#include <base/strings/string_number_conversions.h>
11#include <base/time/time.h>
12
Alex Deymo63784a52014-05-28 10:46:14 -070013#include "update_engine/update_manager/boxed_value.h"
14#include "update_engine/update_manager/shill_provider.h"
15#include "update_engine/update_manager/updater_provider.h"
David Zeuthenfe225c12014-04-29 10:37:35 -070016#include "update_engine/utils.h"
17
18using std::set;
19using std::string;
20
Alex Deymo63784a52014-05-28 10:46:14 -070021namespace chromeos_update_manager {
David Zeuthenfe225c12014-04-29 10:37:35 -070022
23// Template instantiation for common types; used in BoxedValue::ToString().
24// Keep in sync with boxed_value_unitttest.cc.
25
26template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070027string BoxedValue::ValuePrinter<string>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070028 const string* val = reinterpret_cast<const string*>(value);
29 return *val;
30}
31
32template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070033string BoxedValue::ValuePrinter<int>(const void* value) {
Alex Deymof967ebe2014-05-05 14:46:17 -070034 const int* val = reinterpret_cast<const int*>(value);
35 return base::IntToString(*val);
36}
37
38template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070039string BoxedValue::ValuePrinter<unsigned int>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070040 const unsigned int* val = reinterpret_cast<const unsigned int*>(value);
41 return base::UintToString(*val);
42}
43
44template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070045string BoxedValue::ValuePrinter<int64_t>(const void* value) {
Alex Deymof967ebe2014-05-05 14:46:17 -070046 const int64_t* val = reinterpret_cast<const int64_t*>(value);
47 return base::Int64ToString(*val);
David Zeuthenfe225c12014-04-29 10:37:35 -070048}
49
50template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070051string BoxedValue::ValuePrinter<uint64_t>(const void* value) {
Alex Deymof967ebe2014-05-05 14:46:17 -070052 const uint64_t* val =
53 reinterpret_cast<const uint64_t*>(value);
Ben Chandcf5a1d2014-08-07 09:34:12 -070054 return base::Uint64ToString(static_cast<uint64_t>(*val));
David Zeuthenfe225c12014-04-29 10:37:35 -070055}
56
57template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070058string BoxedValue::ValuePrinter<bool>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070059 const bool* val = reinterpret_cast<const bool*>(value);
60 return *val ? "true" : "false";
61}
62
63template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070064string BoxedValue::ValuePrinter<double>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070065 const double* val = reinterpret_cast<const double*>(value);
66 return base::DoubleToString(*val);
67}
68
69template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070070string BoxedValue::ValuePrinter<base::Time>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070071 const base::Time* val = reinterpret_cast<const base::Time*>(value);
72 return chromeos_update_engine::utils::ToString(*val);
73}
74
75template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070076string BoxedValue::ValuePrinter<base::TimeDelta>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070077 const base::TimeDelta* val = reinterpret_cast<const base::TimeDelta*>(value);
78 return chromeos_update_engine::utils::FormatTimeDelta(*val);
79}
80
81static std::string ConnectionTypeToString(ConnectionType type) {
82 switch (type) {
83 case ConnectionType::kEthernet:
84 return "Ethernet";
85 case ConnectionType::kWifi:
86 return "Wifi";
87 case ConnectionType::kWimax:
88 return "Wimax";
89 case ConnectionType::kBluetooth:
90 return "Bluetooth";
91 case ConnectionType::kCellular:
92 return "Cellular";
93 case ConnectionType::kUnknown:
94 return "Unknown";
95 }
96 NOTREACHED();
97 return "Unknown";
98}
99
100template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700101string BoxedValue::ValuePrinter<ConnectionType>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700102 const ConnectionType* val = reinterpret_cast<const ConnectionType*>(value);
103 return ConnectionTypeToString(*val);
104}
105
106template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700107string BoxedValue::ValuePrinter<set<ConnectionType>>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700108 string ret = "";
109 const set<ConnectionType>* val =
110 reinterpret_cast<const set<ConnectionType>*>(value);
111 for (auto& it : *val) {
112 ConnectionType type = it;
113 if (ret.size() > 0)
114 ret += ",";
115 ret += ConnectionTypeToString(type);
116 }
117 return ret;
118}
119
120template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700121string BoxedValue::ValuePrinter<ConnectionTethering>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700122 const ConnectionTethering* val =
123 reinterpret_cast<const ConnectionTethering*>(value);
124 switch (*val) {
125 case ConnectionTethering::kNotDetected:
126 return "Not Detected";
127 case ConnectionTethering::kSuspected:
128 return "Suspected";
129 case ConnectionTethering::kConfirmed:
130 return "Confirmed";
131 case ConnectionTethering::kUnknown:
132 return "Unknown";
133 }
134 NOTREACHED();
135 return "Unknown";
136}
137
138template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700139string BoxedValue::ValuePrinter<Stage>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700140 const Stage* val = reinterpret_cast<const Stage*>(value);
141 switch (*val) {
Alex Vakulenko072359c2014-07-18 11:41:07 -0700142 case Stage::kIdle:
David Zeuthenfe225c12014-04-29 10:37:35 -0700143 return "Idle";
144 case Stage::kCheckingForUpdate:
145 return "Checking For Update";
146 case Stage::kUpdateAvailable:
147 return "Update Available";
148 case Stage::kDownloading:
149 return "Downloading";
150 case Stage::kVerifying:
151 return "Verifying";
152 case Stage::kFinalizing:
153 return "Finalizing";
154 case Stage::kUpdatedNeedReboot:
155 return "Updated, Need Reboot";
156 case Stage::kReportingErrorEvent:
157 return "Reporting Error Event";
158 case Stage::kAttemptingRollback:
159 return "Attempting Rollback";
160 }
161 NOTREACHED();
162 return "Unknown";
163}
164
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700165template<>
166string BoxedValue::ValuePrinter<UpdateRequestStatus>(const void* value) {
167 const UpdateRequestStatus* val =
168 reinterpret_cast<const UpdateRequestStatus*>(value);
169 switch (*val) {
170 case UpdateRequestStatus::kNone:
171 return "None";
172 case UpdateRequestStatus::kInteractive:
173 return "Interactive";
174 case UpdateRequestStatus::kPeriodic:
175 return "Periodic";
176 }
177 NOTREACHED();
178 return "Unknown";
179}
180
Alex Deymo63784a52014-05-28 10:46:14 -0700181} // namespace chromeos_update_manager