blob: 0758314883b9dbba1f0b1ab681122a115ab8ce57 [file] [log] [blame]
Christopher Wileycc8ce0e2015-10-01 16:48:47 -07001//
2// Copyright (C) 2015 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//
16#include "update_engine/update_status_utils.h"
17
18#include <base/logging.h>
Jae Hoon Kim2f78c1c2019-07-25 13:20:43 -070019#include <base/strings/string_number_conversions.h>
20#include <brillo/key_value_store.h>
Christopher Wileycc8ce0e2015-10-01 16:48:47 -070021#include <update_engine/dbus-constants.h>
22
Jae Hoon Kim2f78c1c2019-07-25 13:20:43 -070023using brillo::KeyValueStore;
24using std::string;
25using update_engine::UpdateEngineStatus;
Christopher Wileycc8ce0e2015-10-01 16:48:47 -070026using update_engine::UpdateStatus;
27
28namespace chromeos_update_engine {
29
Jae Hoon Kim916af852019-08-01 17:45:30 -070030namespace {
31
32// Note: Do not change these, autotest depends on these string variables being
33// exactly these matches.
34const char kCurrentOp[] = "CURRENT_OP";
35const char kIsInstall[] = "IS_INSTALL";
36const char kLastCheckedTime[] = "LAST_CHECKED_TIME";
37const char kNewSize[] = "NEW_SIZE";
38const char kNewVersion[] = "NEW_VERSION";
39const char kProgress[] = "PROGRESS";
40
41} // namespace
42
Christopher Wileycc8ce0e2015-10-01 16:48:47 -070043const char* UpdateStatusToString(const UpdateStatus& status) {
44 switch (status) {
45 case UpdateStatus::IDLE:
46 return update_engine::kUpdateStatusIdle;
47 case UpdateStatus::CHECKING_FOR_UPDATE:
48 return update_engine::kUpdateStatusCheckingForUpdate;
49 case UpdateStatus::UPDATE_AVAILABLE:
50 return update_engine::kUpdateStatusUpdateAvailable;
Weidong Guo421ff332017-04-17 10:08:38 -070051 case UpdateStatus::NEED_PERMISSION_TO_UPDATE:
52 return update_engine::kUpdateStatusNeedPermissionToUpdate;
Christopher Wileycc8ce0e2015-10-01 16:48:47 -070053 case UpdateStatus::DOWNLOADING:
54 return update_engine::kUpdateStatusDownloading;
55 case UpdateStatus::VERIFYING:
56 return update_engine::kUpdateStatusVerifying;
57 case UpdateStatus::FINALIZING:
58 return update_engine::kUpdateStatusFinalizing;
59 case UpdateStatus::UPDATED_NEED_REBOOT:
60 return update_engine::kUpdateStatusUpdatedNeedReboot;
61 case UpdateStatus::REPORTING_ERROR_EVENT:
62 return update_engine::kUpdateStatusReportingErrorEvent;
63 case UpdateStatus::ATTEMPTING_ROLLBACK:
64 return update_engine::kUpdateStatusAttemptingRollback;
65 case UpdateStatus::DISABLED:
66 return update_engine::kUpdateStatusDisabled;
67 }
68
69 NOTREACHED();
70 return nullptr;
71}
72
Jae Hoon Kim2f78c1c2019-07-25 13:20:43 -070073string UpdateEngineStatusToString(const UpdateEngineStatus& status) {
74 KeyValueStore key_value_store;
75
76#if BASE_VER < 576279
Jae Hoon Kim916af852019-08-01 17:45:30 -070077 key_value_store.SetString(kLastCheckedTime,
Jae Hoon Kim2f78c1c2019-07-25 13:20:43 -070078 base::Int64ToString(status.last_checked_time));
Jae Hoon Kim916af852019-08-01 17:45:30 -070079 key_value_store.SetString(kProgress, base::DoubleToString(status.progress));
80 key_value_store.SetString(kNewSize,
Jae Hoon Kim2f78c1c2019-07-25 13:20:43 -070081 base::Uint64ToString(status.new_size_bytes));
82#else
Jae Hoon Kim916af852019-08-01 17:45:30 -070083 key_value_store.SetString(kLastCheckedTime,
Jae Hoon Kim2f78c1c2019-07-25 13:20:43 -070084 base::NumberToString(status.last_checked_time));
Jae Hoon Kim916af852019-08-01 17:45:30 -070085 key_value_store.SetString(kProgress, base::NumberToString(status.progress));
86 key_value_store.SetString(kNewSize,
Jae Hoon Kim2f78c1c2019-07-25 13:20:43 -070087 base::NumberToString(status.new_size_bytes));
88#endif
Jae Hoon Kim916af852019-08-01 17:45:30 -070089 key_value_store.SetString(kCurrentOp, UpdateStatusToString(status.status));
90 key_value_store.SetString(kNewVersion, status.new_version);
91 key_value_store.SetBoolean(kIsInstall, status.is_install);
Jae Hoon Kim2f78c1c2019-07-25 13:20:43 -070092
93 return key_value_store.SaveToString();
94}
95
Christopher Wileycc8ce0e2015-10-01 16:48:47 -070096} // namespace chromeos_update_engine