blob: 588bc64b40e10b849307a42f4877fb87b6b8e8a2 [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_binder.h"
18
19#include <binder/IServiceManager.h>
20
21#include <base/message_loop/message_loop.h>
Casey Dahlina93cd532016-01-14 16:55:11 -080022#include <utils/String8.h>
23
Casey Dahlin6ee50b92016-02-01 13:40:21 -080024#include "update_engine/common_service.h"
Casey Dahlina93cd532016-01-14 16:55:11 -080025#include "update_engine/parcelable_update_engine_status.h"
26#include "update_engine/update_status_utils.h"
27
Casey Dahlina93cd532016-01-14 16:55:11 -080028using android::getService;
Aaron Wood224dfc22017-10-04 10:58:36 -070029using android::OK;
30using android::String16;
31using android::String8;
Amin Hassaniaefaab22019-01-14 16:20:16 -080032using android::binder::Status;
33using android::brillo::ParcelableUpdateEngineStatus;
Casey Dahlina93cd532016-01-14 16:55:11 -080034using chromeos_update_engine::StringToUpdateStatus;
35using std::string;
Aaron Wood224dfc22017-10-04 10:58:36 -070036using update_engine::UpdateAttemptFlags;
Casey Dahlina93cd532016-01-14 16:55:11 -080037
38namespace update_engine {
39namespace internal {
40
41bool BinderUpdateEngineClient::Init() {
Amin Hassaniaefaab22019-01-14 16:20:16 -080042 if (!binder_watcher_.Init())
43 return false;
Casey Dahlin40892492016-01-25 16:55:28 -080044
Casey Dahlina93cd532016-01-14 16:55:11 -080045 return getService(String16{"android.brillo.UpdateEngineService"},
Amin Hassaniaefaab22019-01-14 16:20:16 -080046 &service_) == OK;
Casey Dahlina93cd532016-01-14 16:55:11 -080047}
48
49bool BinderUpdateEngineClient::AttemptUpdate(const string& in_app_version,
50 const string& in_omaha_url,
51 bool at_user_request) {
Aaron Wood3a9c9622017-10-19 17:14:58 -070052 bool started;
Aaron Wood224dfc22017-10-04 10:58:36 -070053 return service_
54 ->AttemptUpdate(
55 String16{in_app_version.c_str()},
56 String16{in_omaha_url.c_str()},
Aaron Wood3a9c9622017-10-19 17:14:58 -070057 at_user_request ? 0 : UpdateAttemptFlags::kFlagNonInteractive,
58 &started)
Aaron Wood224dfc22017-10-04 10:58:36 -070059 .isOk();
Casey Dahlina93cd532016-01-14 16:55:11 -080060}
61
Sen Jiang3ce4e522019-01-04 15:34:19 -080062bool BinderUpdateEngineClient::AttemptInstall(
63 const string& omaha_url, const std::vector<string>& dlc_module_ids) {
64 return false;
65}
66
Casey Dahlina93cd532016-01-14 16:55:11 -080067bool BinderUpdateEngineClient::GetStatus(int64_t* out_last_checked_time,
68 double* out_progress,
69 UpdateStatus* out_update_status,
70 string* out_new_version,
71 int64_t* out_new_size) const {
72 ParcelableUpdateEngineStatus status;
73
74 if (!service_->GetStatus(&status).isOk())
75 return false;
76
77 *out_last_checked_time = status.last_checked_time_;
78 *out_progress = status.progress_;
79 StringToUpdateStatus(String8{status.current_operation_}.string(),
80 out_update_status);
81 *out_new_version = String8{status.new_version_}.string();
82 *out_new_size = status.new_size_;
83 return true;
84}
85
Alex Deymo5b5fa8b2016-10-06 15:40:49 -070086bool BinderUpdateEngineClient::SetCohortHint(const string& in_cohort_hint) {
87 return service_->SetCohortHint(String16{in_cohort_hint.c_str()}).isOk();
88}
89
90bool BinderUpdateEngineClient::GetCohortHint(string* out_cohort_hint) const {
91 String16 out_as_string16;
92
93 if (!service_->GetCohortHint(&out_as_string16).isOk())
94 return false;
95
96 *out_cohort_hint = String8{out_as_string16}.string();
97 return true;
98}
99
Casey Dahlina93cd532016-01-14 16:55:11 -0800100bool BinderUpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) {
101 return service_->SetUpdateOverCellularPermission(allowed).isOk();
102}
103
104bool BinderUpdateEngineClient::GetUpdateOverCellularPermission(
105 bool* allowed) const {
106 return service_->GetUpdateOverCellularPermission(allowed).isOk();
107}
108
109bool BinderUpdateEngineClient::SetP2PUpdatePermission(bool enabled) {
110 return service_->SetP2PUpdatePermission(enabled).isOk();
111}
112
113bool BinderUpdateEngineClient::GetP2PUpdatePermission(bool* enabled) const {
114 return service_->GetP2PUpdatePermission(enabled).isOk();
115}
116
117bool BinderUpdateEngineClient::Rollback(bool powerwash) {
118 return service_->AttemptRollback(powerwash).isOk();
119}
120
121bool BinderUpdateEngineClient::GetRollbackPartition(
122 string* rollback_partition) const {
123 String16 out_as_string16;
124
125 if (!service_->GetRollbackPartition(&out_as_string16).isOk())
126 return false;
127
128 *rollback_partition = String8{out_as_string16}.string();
129 return true;
130}
131
132bool BinderUpdateEngineClient::GetPrevVersion(string* prev_version) const {
133 String16 out_as_string16;
134
135 if (!service_->GetPrevVersion(&out_as_string16).isOk())
136 return false;
137
138 *prev_version = String8{out_as_string16}.string();
139 return true;
140}
141
142void BinderUpdateEngineClient::RebootIfNeeded() {
143 if (!service_->RebootIfNeeded().isOk()) {
144 // Reboot error code doesn't necessarily mean that a reboot
145 // failed. For example, D-Bus may be shutdown before we receive the
146 // result.
147 LOG(INFO) << "RebootIfNeeded() failure ignored.";
148 }
149}
150
151bool BinderUpdateEngineClient::ResetStatus() {
152 return service_->ResetStatus().isOk();
153}
154
Casey Dahlin40892492016-01-25 16:55:28 -0800155Status BinderUpdateEngineClient::StatusUpdateCallback::HandleStatusUpdate(
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700156 const ParcelableUpdateEngineStatus& status) {
Casey Dahlin40892492016-01-25 16:55:28 -0800157 UpdateStatus update_status;
158
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700159 StringToUpdateStatus(String8{status.current_operation_}.string(),
160 &update_status);
Casey Dahlin40892492016-01-25 16:55:28 -0800161
162 for (auto& handler : client_->handlers_) {
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700163 handler->HandleStatusUpdate(status.last_checked_time_,
164 status.progress_,
165 update_status,
166 String8{status.new_version_}.string(),
167 status.new_size_);
Casey Dahlin40892492016-01-25 16:55:28 -0800168 }
169
170 return Status::ok();
171}
172
173bool BinderUpdateEngineClient::RegisterStatusUpdateHandler(
Casey Dahlina93cd532016-01-14 16:55:11 -0800174 StatusUpdateHandler* handler) {
Casey Dahlin40892492016-01-25 16:55:28 -0800175 if (!status_callback_.get()) {
Amin Hassaniaefaab22019-01-14 16:20:16 -0800176 status_callback_ = new BinderUpdateEngineClient::StatusUpdateCallback(this);
Casey Dahlin40892492016-01-25 16:55:28 -0800177 if (!service_->RegisterStatusCallback(status_callback_).isOk()) {
178 return false;
179 }
180 }
181
182 handlers_.push_back(handler);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800183
184 int64_t last_checked_time;
185 double progress;
186 UpdateStatus update_status;
187 string new_version;
188 int64_t new_size;
189
Amin Hassaniaefaab22019-01-14 16:20:16 -0800190 if (!GetStatus(&last_checked_time,
191 &progress,
192 &update_status,
193 &new_version,
194 &new_size)) {
Casey Dahlina715f7b2016-01-29 16:38:51 -0800195 handler->IPCError("Could not get status from binder service");
196 }
197
Amin Hassaniaefaab22019-01-14 16:20:16 -0800198 handler->HandleStatusUpdate(
199 last_checked_time, progress, update_status, new_version, new_size);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800200
Casey Dahlin40892492016-01-25 16:55:28 -0800201 return true;
Casey Dahlina93cd532016-01-14 16:55:11 -0800202}
203
Casey Dahlina715f7b2016-01-29 16:38:51 -0800204bool BinderUpdateEngineClient::UnregisterStatusUpdateHandler(
205 StatusUpdateHandler* handler) {
Alex Deymob3fa53b2016-04-18 19:57:58 -0700206 auto it = std::find(handlers_.begin(), handlers_.end(), handler);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800207 if (it != handlers_.end()) {
208 handlers_.erase(it);
209 return true;
210 }
211
212 return false;
213}
214
Casey Dahlina93cd532016-01-14 16:55:11 -0800215bool BinderUpdateEngineClient::SetTargetChannel(const string& in_target_channel,
216 bool allow_powerwash) {
Amin Hassaniaefaab22019-01-14 16:20:16 -0800217 return service_
218 ->SetChannel(String16{in_target_channel.c_str()}, allow_powerwash)
219 .isOk();
Casey Dahlina93cd532016-01-14 16:55:11 -0800220}
221
222bool BinderUpdateEngineClient::GetTargetChannel(string* out_channel) const {
223 String16 out_as_string16;
224
225 if (!service_->GetChannel(false, &out_as_string16).isOk())
226 return false;
227
228 *out_channel = String8{out_as_string16}.string();
229 return true;
230}
231
232bool BinderUpdateEngineClient::GetChannel(string* out_channel) const {
233 String16 out_as_string16;
234
235 if (!service_->GetChannel(true, &out_as_string16).isOk())
236 return false;
237
238 *out_channel = String8{out_as_string16}.string();
239 return true;
240}
241
Shuqian Zhao29971732016-02-05 11:29:32 -0800242bool BinderUpdateEngineClient::GetLastAttemptError(
243 int32_t* last_attempt_error) const {
244 int out_as_int;
245
246 if (!service_->GetLastAttemptError(&out_as_int).isOk())
247 return false;
248
249 *last_attempt_error = out_as_int;
250 return true;
251}
252
Alex Deymob3fa53b2016-04-18 19:57:58 -0700253bool BinderUpdateEngineClient::GetEolStatus(int32_t* eol_status) const {
254 int out_as_int;
255
256 if (!service_->GetEolStatus(&out_as_int).isOk())
257 return false;
258
259 *eol_status = out_as_int;
260 return true;
261}
262
Casey Dahlina93cd532016-01-14 16:55:11 -0800263} // namespace internal
264} // namespace update_engine