blob: 0cf7ed2fc1d6c43401d5ef0a3fbcfcb99a4e4caf [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
11#include "update_engine/policy_manager/boxed_value.h"
12#include "update_engine/policy_manager/shill_provider.h"
13#include "update_engine/policy_manager/updater_provider.h"
14#include "update_engine/utils.h"
15
16using std::set;
17using std::string;
18
19namespace chromeos_policy_manager {
20
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<>
31string BoxedValue::ValuePrinter<unsigned int>(const void *value) {
32 const unsigned int* val = reinterpret_cast<const unsigned int*>(value);
33 return base::UintToString(*val);
34}
35
36template<>
37string BoxedValue::ValuePrinter<unsigned long>(const void *value) {
38 const unsigned long* val = reinterpret_cast<const unsigned long*>(value);
39 return base::Uint64ToString(static_cast<uint64>(*val));
40}
41
42template<>
43string BoxedValue::ValuePrinter<unsigned long long>(const void *value) {
44 const unsigned long long* val =
45 reinterpret_cast<const unsigned long long*>(value);
46 return base::Uint64ToString(static_cast<uint64>(*val));
47}
48
49template<>
50string BoxedValue::ValuePrinter<int>(const void *value) {
51 const int* val = reinterpret_cast<const int*>(value);
52 return base::IntToString(*val);
53}
54
55template<>
56string 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
163} // namespace chromeos_policy_manager