blob: 18ae23b74583f2bbb80db652058e0874e6b9a3aa [file] [log] [blame]
Casey Dahlina93cd532016-01-14 16:55:11 -08001//
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
17#include "update_engine/client_library/client_dbus.h"
18
19#include <base/message_loop/message_loop.h>
20
Amin Hassanic80d2d82019-06-21 17:43:32 -070021#include <memory>
22
Casey Dahlina93cd532016-01-14 16:55:11 -080023#include <dbus/bus.h>
Amin Hassani9b66aa62019-03-14 14:13:30 -070024#include <dlcservice/proto_bindings/dlcservice.pb.h>
Casey Dahlina93cd532016-01-14 16:55:11 -080025#include <update_engine/dbus-constants.h>
26
27#include "update_engine/update_status_utils.h"
28
29using chromeos_update_engine::StringToUpdateStatus;
30using dbus::Bus;
31using org::chromium::UpdateEngineInterfaceProxy;
32using std::string;
Amin Hassanic80d2d82019-06-21 17:43:32 -070033using std::unique_ptr;
Xiaochu Liu88d90382018-08-29 16:09:11 -070034using std::vector;
Casey Dahlina93cd532016-01-14 16:55:11 -080035
36namespace update_engine {
Amin Hassanic80d2d82019-06-21 17:43:32 -070037
38unique_ptr<UpdateEngineClient> UpdateEngineClient::CreateInstance() {
39 auto ret = std::make_unique<internal::DBusUpdateEngineClient>();
40 if (!ret->Init()) {
41 ret.reset();
42 }
43 return ret;
44}
45
Casey Dahlina93cd532016-01-14 16:55:11 -080046namespace internal {
47
Amin Hassanieb463ee2019-06-20 19:23:03 -070048namespace {
49// This converts the status from Protobuf |StatusResult| to The internal
50// |UpdateEngineStatus| struct.
51bool ConvertToUpdateEngineStatus(const StatusResult& status,
52 UpdateEngineStatus* out_status) {
53 out_status->last_checked_time = status.last_checked_time();
54 out_status->progress = status.progress();
55 out_status->new_version = status.new_version();
56 out_status->new_size_bytes = status.new_size();
57 return StringToUpdateStatus(status.current_operation(), &out_status->status);
58}
59} // namespace
60
Casey Dahlina93cd532016-01-14 16:55:11 -080061bool DBusUpdateEngineClient::Init() {
62 Bus::Options options;
63 options.bus_type = Bus::SYSTEM;
64 scoped_refptr<Bus> bus{new Bus{options}};
65
66 if (!bus->Connect())
67 return false;
68
69 proxy_.reset(new UpdateEngineInterfaceProxy{bus});
70 return true;
71}
72
73bool DBusUpdateEngineClient::AttemptUpdate(const string& in_app_version,
74 const string& in_omaha_url,
75 bool at_user_request) {
76 return proxy_->AttemptUpdateWithFlags(
77 in_app_version,
78 in_omaha_url,
Amin Hassani6bb001f2018-02-26 14:33:02 -080079 (at_user_request)
80 ? 0
81 : update_engine::UpdateAttemptFlags::kFlagNonInteractive,
Casey Dahlina93cd532016-01-14 16:55:11 -080082 nullptr);
83}
84
Amin Hassani9b66aa62019-03-14 14:13:30 -070085bool DBusUpdateEngineClient::AttemptInstall(const string& omaha_url,
86 const vector<string>& dlc_ids) {
Xiaochu Liu88d90382018-08-29 16:09:11 -070087 // Convert parameters into protobuf.
Amin Hassani9b66aa62019-03-14 14:13:30 -070088 dlcservice::DlcModuleList dlc_parameters;
Xiaochu Liu88d90382018-08-29 16:09:11 -070089 dlc_parameters.set_omaha_url(omaha_url);
Amin Hassani9b66aa62019-03-14 14:13:30 -070090 for (const auto& dlc_id : dlc_ids) {
91 dlcservice::DlcModuleInfo* dlc_module_info =
92 dlc_parameters.add_dlc_module_infos();
93 dlc_module_info->set_dlc_id(dlc_id);
Xiaochu Liu88d90382018-08-29 16:09:11 -070094 }
Amin Hassani3bab1772019-06-21 14:58:25 -070095 return proxy_->AttemptInstall(dlc_parameters,
96 nullptr /* brillo::ErrorPtr* */);
Xiaochu Liu88d90382018-08-29 16:09:11 -070097}
98
Casey Dahlina93cd532016-01-14 16:55:11 -080099bool DBusUpdateEngineClient::GetStatus(int64_t* out_last_checked_time,
100 double* out_progress,
101 UpdateStatus* out_update_status,
102 string* out_new_version,
103 int64_t* out_new_size) const {
Amin Hassanieb463ee2019-06-20 19:23:03 -0700104 StatusResult status;
105 if (!proxy_->GetStatusAdvanced(&status, nullptr)) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800106 return false;
107 }
108
Amin Hassanieb463ee2019-06-20 19:23:03 -0700109 *out_last_checked_time = status.last_checked_time();
110 *out_progress = status.progress();
111 *out_new_version = status.new_version();
112 *out_new_size = status.new_size();
113 return StringToUpdateStatus(status.current_operation(), out_update_status);
114}
115
116bool DBusUpdateEngineClient::GetStatus(UpdateEngineStatus* out_status) const {
117 StatusResult status;
118 if (!proxy_->GetStatusAdvanced(&status, nullptr)) {
119 return false;
120 }
121
122 return ConvertToUpdateEngineStatus(status, out_status);
Casey Dahlina93cd532016-01-14 16:55:11 -0800123}
124
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700125bool DBusUpdateEngineClient::SetCohortHint(const string& cohort_hint) {
126 return proxy_->SetCohortHint(cohort_hint, nullptr);
127}
128
129bool DBusUpdateEngineClient::GetCohortHint(string* cohort_hint) const {
130 return proxy_->GetCohortHint(cohort_hint, nullptr);
131}
132
Casey Dahlina93cd532016-01-14 16:55:11 -0800133bool DBusUpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) {
134 return proxy_->SetUpdateOverCellularPermission(allowed, nullptr);
135}
136
137bool DBusUpdateEngineClient::GetUpdateOverCellularPermission(
138 bool* allowed) const {
139 return proxy_->GetUpdateOverCellularPermission(allowed, nullptr);
140}
141
142bool DBusUpdateEngineClient::SetP2PUpdatePermission(bool enabled) {
143 return proxy_->SetP2PUpdatePermission(enabled, nullptr);
144}
145
146bool DBusUpdateEngineClient::GetP2PUpdatePermission(bool* enabled) const {
147 return proxy_->GetP2PUpdatePermission(enabled, nullptr);
148}
149
150bool DBusUpdateEngineClient::Rollback(bool powerwash) {
151 return proxy_->AttemptRollback(powerwash, nullptr);
152}
153
154bool DBusUpdateEngineClient::GetRollbackPartition(
155 string* rollback_partition) const {
156 return proxy_->GetRollbackPartition(rollback_partition, nullptr);
157}
158
159bool DBusUpdateEngineClient::GetPrevVersion(string* prev_version) const {
160 return proxy_->GetPrevVersion(prev_version, nullptr);
161}
162
163void DBusUpdateEngineClient::RebootIfNeeded() {
164 bool ret = proxy_->RebootIfNeeded(nullptr);
165 if (!ret) {
166 // Reboot error code doesn't necessarily mean that a reboot
167 // failed. For example, D-Bus may be shutdown before we receive the
168 // result.
169 LOG(INFO) << "RebootIfNeeded() failure ignored.";
170 }
171}
172
173bool DBusUpdateEngineClient::ResetStatus() {
174 return proxy_->ResetStatus(nullptr);
175}
176
Alex Deymo492eaf52016-02-02 12:05:02 -0800177void DBusUpdateEngineClient::DBusStatusHandlersRegistered(
Amin Hassaniaefaab22019-01-14 16:20:16 -0800178 const string& interface, const string& signal_name, bool success) const {
Casey Dahlina93cd532016-01-14 16:55:11 -0800179 if (!success) {
Casey Dahlina715f7b2016-01-29 16:38:51 -0800180 for (auto handler : handlers_) {
Amin Hassaniaefaab22019-01-14 16:20:16 -0800181 handler->IPCError("Could not connect to" + signal_name + " on " +
182 interface);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800183 }
184 } else {
185 StatusUpdateHandlersRegistered(nullptr);
Casey Dahlina93cd532016-01-14 16:55:11 -0800186 }
Casey Dahlina715f7b2016-01-29 16:38:51 -0800187}
Casey Dahlina93cd532016-01-14 16:55:11 -0800188
Casey Dahlina715f7b2016-01-29 16:38:51 -0800189void DBusUpdateEngineClient::StatusUpdateHandlersRegistered(
190 StatusUpdateHandler* handler) const {
Amin Hassanieb463ee2019-06-20 19:23:03 -0700191 UpdateEngineStatus status;
192 if (!GetStatus(&status)) {
Casey Dahlina715f7b2016-01-29 16:38:51 -0800193 handler->IPCError("Could not query current status");
Casey Dahlina93cd532016-01-14 16:55:11 -0800194 return;
195 }
196
Alex Deymo492eaf52016-02-02 12:05:02 -0800197 std::vector<update_engine::StatusUpdateHandler*> just_handler = {handler};
198 for (auto h : handler ? just_handler : handlers_) {
Amin Hassanieb463ee2019-06-20 19:23:03 -0700199 h->HandleStatusUpdate(status);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800200 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800201}
202
Casey Dahlina715f7b2016-01-29 16:38:51 -0800203void DBusUpdateEngineClient::RunStatusUpdateHandlers(
Amin Hassanieb463ee2019-06-20 19:23:03 -0700204 const StatusResult& status) {
205 UpdateEngineStatus ue_status;
206 ConvertToUpdateEngineStatus(status, &ue_status);
Casey Dahlina93cd532016-01-14 16:55:11 -0800207
Casey Dahlina715f7b2016-01-29 16:38:51 -0800208 for (auto handler : handlers_) {
Amin Hassanieb463ee2019-06-20 19:23:03 -0700209 handler->HandleStatusUpdate(ue_status);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800210 }
211}
212
213bool DBusUpdateEngineClient::UnregisterStatusUpdateHandler(
214 StatusUpdateHandler* handler) {
Alex Deymob3fa53b2016-04-18 19:57:58 -0700215 auto it = std::find(handlers_.begin(), handlers_.end(), handler);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800216 if (it != handlers_.end()) {
217 handlers_.erase(it);
218 return true;
219 }
220
221 return false;
Casey Dahlina93cd532016-01-14 16:55:11 -0800222}
223
Casey Dahlin40892492016-01-25 16:55:28 -0800224bool DBusUpdateEngineClient::RegisterStatusUpdateHandler(
Casey Dahlina93cd532016-01-14 16:55:11 -0800225 StatusUpdateHandler* handler) {
226 if (!base::MessageLoopForIO::current()) {
227 LOG(FATAL) << "Cannot get UpdateEngineClient outside of message loop.";
Casey Dahlin40892492016-01-25 16:55:28 -0800228 return false;
Casey Dahlina93cd532016-01-14 16:55:11 -0800229 }
230
Casey Dahlina715f7b2016-01-29 16:38:51 -0800231 handlers_.push_back(handler);
232
233 if (dbus_handler_registered_) {
234 StatusUpdateHandlersRegistered(handler);
235 return true;
236 }
237
Amin Hassanieb463ee2019-06-20 19:23:03 -0700238 proxy_->RegisterStatusUpdateAdvancedSignalHandler(
Casey Dahlina715f7b2016-01-29 16:38:51 -0800239 base::Bind(&DBusUpdateEngineClient::RunStatusUpdateHandlers,
240 base::Unretained(this)),
Alex Deymo492eaf52016-02-02 12:05:02 -0800241 base::Bind(&DBusUpdateEngineClient::DBusStatusHandlersRegistered,
242 base::Unretained(this)));
Casey Dahlina715f7b2016-01-29 16:38:51 -0800243
244 dbus_handler_registered_ = true;
Casey Dahlin40892492016-01-25 16:55:28 -0800245
246 return true;
Casey Dahlina93cd532016-01-14 16:55:11 -0800247}
248
249bool DBusUpdateEngineClient::SetTargetChannel(const string& in_target_channel,
250 bool allow_powerwash) {
251 return proxy_->SetChannel(in_target_channel, allow_powerwash, nullptr);
252}
253
254bool DBusUpdateEngineClient::GetTargetChannel(string* out_channel) const {
255 return proxy_->GetChannel(false, // Get the target channel.
256 out_channel,
257 nullptr);
258}
259
260bool DBusUpdateEngineClient::GetChannel(string* out_channel) const {
261 return proxy_->GetChannel(true, // Get the current channel.
262 out_channel,
263 nullptr);
264}
265
Shuqian Zhao29971732016-02-05 11:29:32 -0800266bool DBusUpdateEngineClient::GetLastAttemptError(
267 int32_t* last_attempt_error) const {
268 return proxy_->GetLastAttemptError(last_attempt_error, nullptr);
269}
270
Alex Deymob3fa53b2016-04-18 19:57:58 -0700271bool DBusUpdateEngineClient::GetEolStatus(int32_t* eol_status) const {
272 return proxy_->GetEolStatus(eol_status, nullptr);
273}
274
Casey Dahlina93cd532016-01-14 16:55:11 -0800275} // namespace internal
276} // namespace update_engine