blob: 512f7a84766bdc04c0f54414b519640d50a084bf [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
5#include <set>
6#include <string>
7
8#include <base/strings/string_number_conversions.h>
9#include <base/time/time.h>
10
Alex Deymo63784a52014-05-28 10:46:14 -070011#include "update_engine/update_manager/boxed_value.h"
12#include "update_engine/update_manager/shill_provider.h"
13#include "update_engine/update_manager/updater_provider.h"
David Zeuthenfe225c12014-04-29 10:37:35 -070014#include "update_engine/utils.h"
15
16using std::set;
17using std::string;
18
Alex Deymo63784a52014-05-28 10:46:14 -070019namespace chromeos_update_manager {
David Zeuthenfe225c12014-04-29 10:37:35 -070020
21// Template instantiation for common types; used in BoxedValue::ToString().
22// Keep in sync with boxed_value_unitttest.cc.
23
24template<>
25string BoxedValue::ValuePrinter<string>(const void *value) {
26 const string* val = reinterpret_cast<const string*>(value);
27 return *val;
28}
29
30template<>
Alex Deymof967ebe2014-05-05 14:46:17 -070031string BoxedValue::ValuePrinter<int>(const void *value) {
32 const int* val = reinterpret_cast<const int*>(value);
33 return base::IntToString(*val);
34}
35
36template<>
David Zeuthenfe225c12014-04-29 10:37:35 -070037string BoxedValue::ValuePrinter<unsigned int>(const void *value) {
38 const unsigned int* val = reinterpret_cast<const unsigned int*>(value);
39 return base::UintToString(*val);
40}
41
42template<>
Alex Deymof967ebe2014-05-05 14:46:17 -070043string BoxedValue::ValuePrinter<int64_t>(const void *value) {
44 const int64_t* val = reinterpret_cast<const int64_t*>(value);
45 return base::Int64ToString(*val);
David Zeuthenfe225c12014-04-29 10:37:35 -070046}
47
48template<>
Alex Deymof967ebe2014-05-05 14:46:17 -070049string BoxedValue::ValuePrinter<uint64_t>(const void *value) {
50 const uint64_t* val =
51 reinterpret_cast<const uint64_t*>(value);
David Zeuthenfe225c12014-04-29 10:37:35 -070052 return base::Uint64ToString(static_cast<uint64>(*val));
53}
54
55template<>
David Zeuthenfe225c12014-04-29 10:37:35 -070056string BoxedValue::ValuePrinter<bool>(const void *value) {
57 const bool* val = reinterpret_cast<const bool*>(value);
58 return *val ? "true" : "false";
59}
60
61template<>
62string BoxedValue::ValuePrinter<double>(const void *value) {
63 const double* val = reinterpret_cast<const double*>(value);
64 return base::DoubleToString(*val);
65}
66
67template<>
68string BoxedValue::ValuePrinter<base::Time>(const void *value) {
69 const base::Time* val = reinterpret_cast<const base::Time*>(value);
70 return chromeos_update_engine::utils::ToString(*val);
71}
72
73template<>
74string BoxedValue::ValuePrinter<base::TimeDelta>(const void *value) {
75 const base::TimeDelta* val = reinterpret_cast<const base::TimeDelta*>(value);
76 return chromeos_update_engine::utils::FormatTimeDelta(*val);
77}
78
79static std::string ConnectionTypeToString(ConnectionType type) {
80 switch (type) {
81 case ConnectionType::kEthernet:
82 return "Ethernet";
83 case ConnectionType::kWifi:
84 return "Wifi";
85 case ConnectionType::kWimax:
86 return "Wimax";
87 case ConnectionType::kBluetooth:
88 return "Bluetooth";
89 case ConnectionType::kCellular:
90 return "Cellular";
91 case ConnectionType::kUnknown:
92 return "Unknown";
93 }
94 NOTREACHED();
95 return "Unknown";
96}
97
98template<>
99string BoxedValue::ValuePrinter<ConnectionType>(const void *value) {
100 const ConnectionType* val = reinterpret_cast<const ConnectionType*>(value);
101 return ConnectionTypeToString(*val);
102}
103
104template<>
105string BoxedValue::ValuePrinter<set<ConnectionType>>(const void *value) {
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 += ",";
113 ret += ConnectionTypeToString(type);
114 }
115 return ret;
116}
117
118template<>
119string BoxedValue::ValuePrinter<ConnectionTethering>(const void *value) {
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
136template<>
137string BoxedValue::ValuePrinter<Stage>(const void *value) {
138 const Stage* val = reinterpret_cast<const Stage*>(value);
139 switch (*val) {
140 case Stage::kIdle:
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
Alex Deymo63784a52014-05-28 10:46:14 -0700163} // namespace chromeos_update_manager