xref: /aosp_15_r20/system/update_engine/update_status_utils.cc (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
1 //
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>
19 #include <brillo/key_value_store.h>
20 
21 using brillo::KeyValueStore;
22 using std::string;
23 using update_engine::UpdateEngineStatus;
24 using update_engine::UpdateStatus;
25 
26 namespace chromeos_update_engine {
27 
28 namespace {
29 
30 // Note: Do not change these, autotest depends on these string variables being
31 // exactly these matches.
32 const char kCurrentOp[] = "CURRENT_OP";
33 const char kIsInstall[] = "IS_INSTALL";
34 const char kIsEnterpriseRollback[] = "IS_ENTERPRISE_ROLLBACK";
35 const char kLastCheckedTime[] = "LAST_CHECKED_TIME";
36 const char kNewSize[] = "NEW_SIZE";
37 const char kNewVersion[] = "NEW_VERSION";
38 const char kProgress[] = "PROGRESS";
39 const char kWillPowerwashAfterReboot[] = "WILL_POWERWASH_AFTER_REBOOT";
40 
41 namespace update_engine {
42 const char kUpdateStatusIdle[] = "UPDATE_STATUS_IDLE";
43 const char kUpdateStatusCheckingForUpdate[] =
44     "UPDATE_STATUS_CHECKING_FOR_UPDATE";
45 const char kUpdateStatusUpdateAvailable[] = "UPDATE_STATUS_UPDATE_AVAILABLE";
46 const char kUpdateStatusDownloading[] = "UPDATE_STATUS_DOWNLOADING";
47 const char kUpdateStatusVerifying[] = "UPDATE_STATUS_VERIFYING";
48 const char kUpdateStatusFinalizing[] = "UPDATE_STATUS_FINALIZING";
49 const char kUpdateStatusUpdatedNeedReboot[] =
50     "UPDATE_STATUS_UPDATED_NEED_REBOOT";
51 const char kUpdateStatusReportingErrorEvent[] =
52     "UPDATE_STATUS_REPORTING_ERROR_EVENT";
53 const char kUpdateStatusAttemptingRollback[] =
54     "UPDATE_STATUS_ATTEMPTING_ROLLBACK";
55 const char kUpdateStatusDisabled[] = "UPDATE_STATUS_DISABLED";
56 const char kUpdateStatusNeedPermissionToUpdate[] =
57     "UPDATE_STATUS_NEED_PERMISSION_TO_UPDATE";
58 const char kUpdateStatusCleanupPreviousUpdate[] =
59     "UPDATE_STATUS_CLEANUP_PREVIOUS_UPDATE";
60 }  // namespace update_engine
61 
62 }  // namespace
63 
UpdateStatusToString(const UpdateStatus & status)64 const char* UpdateStatusToString(const UpdateStatus& status) {
65   switch (status) {
66     case UpdateStatus::IDLE:
67       return update_engine::kUpdateStatusIdle;
68     case UpdateStatus::CHECKING_FOR_UPDATE:
69       return update_engine::kUpdateStatusCheckingForUpdate;
70     case UpdateStatus::UPDATE_AVAILABLE:
71       return update_engine::kUpdateStatusUpdateAvailable;
72     case UpdateStatus::NEED_PERMISSION_TO_UPDATE:
73       return update_engine::kUpdateStatusNeedPermissionToUpdate;
74     case UpdateStatus::DOWNLOADING:
75       return update_engine::kUpdateStatusDownloading;
76     case UpdateStatus::VERIFYING:
77       return update_engine::kUpdateStatusVerifying;
78     case UpdateStatus::FINALIZING:
79       return update_engine::kUpdateStatusFinalizing;
80     case UpdateStatus::UPDATED_NEED_REBOOT:
81       return update_engine::kUpdateStatusUpdatedNeedReboot;
82     case UpdateStatus::REPORTING_ERROR_EVENT:
83       return update_engine::kUpdateStatusReportingErrorEvent;
84     case UpdateStatus::ATTEMPTING_ROLLBACK:
85       return update_engine::kUpdateStatusAttemptingRollback;
86     case UpdateStatus::DISABLED:
87       return update_engine::kUpdateStatusDisabled;
88     case UpdateStatus::CLEANUP_PREVIOUS_UPDATE:
89       return update_engine::kUpdateStatusCleanupPreviousUpdate;
90   }
91 
92   NOTREACHED();
93   return nullptr;
94 }
95 
UpdateEngineStatusToString(const UpdateEngineStatus & status)96 string UpdateEngineStatusToString(const UpdateEngineStatus& status) {
97   KeyValueStore key_value_store;
98 
99   key_value_store.SetString(kLastCheckedTime,
100                             std::format("{}", status.last_checked_time));
101   key_value_store.SetString(kProgress, std::format("{}", status.progress));
102   key_value_store.SetString(kNewSize, std::format("{}", status.new_size_bytes));
103   key_value_store.SetString(kCurrentOp, UpdateStatusToString(status.status));
104   key_value_store.SetString(kNewVersion, status.new_version);
105   key_value_store.SetBoolean(kIsEnterpriseRollback,
106                              status.is_enterprise_rollback);
107   key_value_store.SetBoolean(kIsInstall, status.is_install);
108   key_value_store.SetBoolean(kWillPowerwashAfterReboot,
109                              status.will_powerwash_after_reboot);
110 
111   return key_value_store.SaveToString();
112 }
113 
114 }  // namespace chromeos_update_engine
115