blob: 6648e9993cc657a7d0de07a3237780a6e68c6376 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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//
David Zeuthenfe225c12014-04-29 10:37:35 -070016
Alex Deymoaab50e32014-11-10 19:55:35 -080017#include "update_engine/update_manager/boxed_value.h"
18
Ben Chandcf5a1d2014-08-07 09:34:12 -070019#include <stdint.h>
20
David Zeuthenfe225c12014-04-29 10:37:35 -070021#include <set>
22#include <string>
23
24#include <base/strings/string_number_conversions.h>
25#include <base/time/time.h>
26
Alex Deymo63784a52014-05-28 10:46:14 -070027#include "update_engine/update_manager/shill_provider.h"
28#include "update_engine/update_manager/updater_provider.h"
David Zeuthenfe225c12014-04-29 10:37:35 -070029#include "update_engine/utils.h"
30
31using std::set;
32using std::string;
33
Alex Deymo63784a52014-05-28 10:46:14 -070034namespace chromeos_update_manager {
David Zeuthenfe225c12014-04-29 10:37:35 -070035
36// Template instantiation for common types; used in BoxedValue::ToString().
37// Keep in sync with boxed_value_unitttest.cc.
38
39template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070040string BoxedValue::ValuePrinter<string>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070041 const string* val = reinterpret_cast<const string*>(value);
42 return *val;
43}
44
45template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070046string BoxedValue::ValuePrinter<int>(const void* value) {
Alex Deymof967ebe2014-05-05 14:46:17 -070047 const int* val = reinterpret_cast<const int*>(value);
48 return base::IntToString(*val);
49}
50
51template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070052string BoxedValue::ValuePrinter<unsigned int>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070053 const unsigned int* val = reinterpret_cast<const unsigned int*>(value);
54 return base::UintToString(*val);
55}
56
57template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070058string BoxedValue::ValuePrinter<int64_t>(const void* value) {
Alex Deymof967ebe2014-05-05 14:46:17 -070059 const int64_t* val = reinterpret_cast<const int64_t*>(value);
60 return base::Int64ToString(*val);
David Zeuthenfe225c12014-04-29 10:37:35 -070061}
62
63template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070064string BoxedValue::ValuePrinter<uint64_t>(const void* value) {
Alex Deymof967ebe2014-05-05 14:46:17 -070065 const uint64_t* val =
66 reinterpret_cast<const uint64_t*>(value);
Ben Chandcf5a1d2014-08-07 09:34:12 -070067 return base::Uint64ToString(static_cast<uint64_t>(*val));
David Zeuthenfe225c12014-04-29 10:37:35 -070068}
69
70template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070071string BoxedValue::ValuePrinter<bool>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070072 const bool* val = reinterpret_cast<const bool*>(value);
73 return *val ? "true" : "false";
74}
75
76template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070077string BoxedValue::ValuePrinter<double>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070078 const double* val = reinterpret_cast<const double*>(value);
79 return base::DoubleToString(*val);
80}
81
82template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070083string BoxedValue::ValuePrinter<base::Time>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070084 const base::Time* val = reinterpret_cast<const base::Time*>(value);
85 return chromeos_update_engine::utils::ToString(*val);
86}
87
88template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -070089string BoxedValue::ValuePrinter<base::TimeDelta>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -070090 const base::TimeDelta* val = reinterpret_cast<const base::TimeDelta*>(value);
91 return chromeos_update_engine::utils::FormatTimeDelta(*val);
92}
93
Alex Deymof329b932014-10-30 01:37:48 -070094static string ConnectionTypeToString(ConnectionType type) {
David Zeuthenfe225c12014-04-29 10:37:35 -070095 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
113template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700114string BoxedValue::ValuePrinter<ConnectionType>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700115 const ConnectionType* val = reinterpret_cast<const ConnectionType*>(value);
116 return ConnectionTypeToString(*val);
117}
118
119template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700120string BoxedValue::ValuePrinter<set<ConnectionType>>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700121 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
133template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700134string BoxedValue::ValuePrinter<ConnectionTethering>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700135 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
151template<>
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700152string BoxedValue::ValuePrinter<Stage>(const void* value) {
David Zeuthenfe225c12014-04-29 10:37:35 -0700153 const Stage* val = reinterpret_cast<const Stage*>(value);
154 switch (*val) {
Alex Vakulenko072359c2014-07-18 11:41:07 -0700155 case Stage::kIdle:
David Zeuthenfe225c12014-04-29 10:37:35 -0700156 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
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700178template<>
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
Alex Deymo63784a52014-05-28 10:46:14 -0700194} // namespace chromeos_update_manager