blob: b4625e457c0ccaeb9608ba585de7fc6a86938f55 [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 Dahlin40892492016-01-25 16:55:28 -080028using android::binder::Status;
Alex Deymob3fa53b2016-04-18 19:57:58 -070029using android::brillo::ParcelableUpdateEngineStatus;
Casey Dahlina93cd532016-01-14 16:55:11 -080030using android::getService;
Aaron Wood224dfc22017-10-04 10:58:36 -070031using android::OK;
32using android::String16;
33using android::String8;
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() {
Casey Dahlin40892492016-01-25 16:55:28 -080042 if (!binder_watcher_.Init()) return false;
43
Casey Dahlina93cd532016-01-14 16:55:11 -080044 return getService(String16{"android.brillo.UpdateEngineService"},
45 &service_) == OK;
46}
47
48bool BinderUpdateEngineClient::AttemptUpdate(const string& in_app_version,
49 const string& in_omaha_url,
50 bool at_user_request) {
Aaron Wood224dfc22017-10-04 10:58:36 -070051 return service_
52 ->AttemptUpdate(
53 String16{in_app_version.c_str()},
54 String16{in_omaha_url.c_str()},
55 at_user_request ? 0 : UpdateAttemptFlags::kFlagNonInteractive)
56 .isOk();
Casey Dahlina93cd532016-01-14 16:55:11 -080057}
58
59bool BinderUpdateEngineClient::GetStatus(int64_t* out_last_checked_time,
60 double* out_progress,
61 UpdateStatus* out_update_status,
62 string* out_new_version,
63 int64_t* out_new_size) const {
64 ParcelableUpdateEngineStatus status;
65
66 if (!service_->GetStatus(&status).isOk())
67 return false;
68
69 *out_last_checked_time = status.last_checked_time_;
70 *out_progress = status.progress_;
71 StringToUpdateStatus(String8{status.current_operation_}.string(),
72 out_update_status);
73 *out_new_version = String8{status.new_version_}.string();
74 *out_new_size = status.new_size_;
75 return true;
76}
77
Alex Deymo5b5fa8b2016-10-06 15:40:49 -070078bool BinderUpdateEngineClient::SetCohortHint(const string& in_cohort_hint) {
79 return service_->SetCohortHint(String16{in_cohort_hint.c_str()}).isOk();
80}
81
82bool BinderUpdateEngineClient::GetCohortHint(string* out_cohort_hint) const {
83 String16 out_as_string16;
84
85 if (!service_->GetCohortHint(&out_as_string16).isOk())
86 return false;
87
88 *out_cohort_hint = String8{out_as_string16}.string();
89 return true;
90}
91
Casey Dahlina93cd532016-01-14 16:55:11 -080092bool BinderUpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) {
93 return service_->SetUpdateOverCellularPermission(allowed).isOk();
94}
95
96bool BinderUpdateEngineClient::GetUpdateOverCellularPermission(
97 bool* allowed) const {
98 return service_->GetUpdateOverCellularPermission(allowed).isOk();
99}
100
101bool BinderUpdateEngineClient::SetP2PUpdatePermission(bool enabled) {
102 return service_->SetP2PUpdatePermission(enabled).isOk();
103}
104
105bool BinderUpdateEngineClient::GetP2PUpdatePermission(bool* enabled) const {
106 return service_->GetP2PUpdatePermission(enabled).isOk();
107}
108
109bool BinderUpdateEngineClient::Rollback(bool powerwash) {
110 return service_->AttemptRollback(powerwash).isOk();
111}
112
113bool BinderUpdateEngineClient::GetRollbackPartition(
114 string* rollback_partition) const {
115 String16 out_as_string16;
116
117 if (!service_->GetRollbackPartition(&out_as_string16).isOk())
118 return false;
119
120 *rollback_partition = String8{out_as_string16}.string();
121 return true;
122}
123
124bool BinderUpdateEngineClient::GetPrevVersion(string* prev_version) const {
125 String16 out_as_string16;
126
127 if (!service_->GetPrevVersion(&out_as_string16).isOk())
128 return false;
129
130 *prev_version = String8{out_as_string16}.string();
131 return true;
132}
133
134void BinderUpdateEngineClient::RebootIfNeeded() {
135 if (!service_->RebootIfNeeded().isOk()) {
136 // Reboot error code doesn't necessarily mean that a reboot
137 // failed. For example, D-Bus may be shutdown before we receive the
138 // result.
139 LOG(INFO) << "RebootIfNeeded() failure ignored.";
140 }
141}
142
143bool BinderUpdateEngineClient::ResetStatus() {
144 return service_->ResetStatus().isOk();
145}
146
Casey Dahlin40892492016-01-25 16:55:28 -0800147Status BinderUpdateEngineClient::StatusUpdateCallback::HandleStatusUpdate(
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700148 const ParcelableUpdateEngineStatus& status) {
Casey Dahlin40892492016-01-25 16:55:28 -0800149 UpdateStatus update_status;
150
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700151 StringToUpdateStatus(String8{status.current_operation_}.string(),
152 &update_status);
Casey Dahlin40892492016-01-25 16:55:28 -0800153
154 for (auto& handler : client_->handlers_) {
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700155 handler->HandleStatusUpdate(status.last_checked_time_,
156 status.progress_,
157 update_status,
158 String8{status.new_version_}.string(),
159 status.new_size_);
Casey Dahlin40892492016-01-25 16:55:28 -0800160 }
161
162 return Status::ok();
163}
164
165bool BinderUpdateEngineClient::RegisterStatusUpdateHandler(
Casey Dahlina93cd532016-01-14 16:55:11 -0800166 StatusUpdateHandler* handler) {
Casey Dahlin40892492016-01-25 16:55:28 -0800167 if (!status_callback_.get()) {
168 status_callback_ =
169 new BinderUpdateEngineClient::StatusUpdateCallback(this);
170 if (!service_->RegisterStatusCallback(status_callback_).isOk()) {
171 return false;
172 }
173 }
174
175 handlers_.push_back(handler);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800176
177 int64_t last_checked_time;
178 double progress;
179 UpdateStatus update_status;
180 string new_version;
181 int64_t new_size;
182
183 if (!GetStatus(&last_checked_time, &progress, &update_status,
184 &new_version, &new_size)) {
185 handler->IPCError("Could not get status from binder service");
186 }
187
188 handler->HandleStatusUpdate(last_checked_time, progress, update_status,
189 new_version, new_size);
190
Casey Dahlin40892492016-01-25 16:55:28 -0800191 return true;
Casey Dahlina93cd532016-01-14 16:55:11 -0800192}
193
Casey Dahlina715f7b2016-01-29 16:38:51 -0800194bool BinderUpdateEngineClient::UnregisterStatusUpdateHandler(
195 StatusUpdateHandler* handler) {
Alex Deymob3fa53b2016-04-18 19:57:58 -0700196 auto it = std::find(handlers_.begin(), handlers_.end(), handler);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800197 if (it != handlers_.end()) {
198 handlers_.erase(it);
199 return true;
200 }
201
202 return false;
203}
204
Casey Dahlina93cd532016-01-14 16:55:11 -0800205bool BinderUpdateEngineClient::SetTargetChannel(const string& in_target_channel,
206 bool allow_powerwash) {
207 return service_->SetChannel(String16{in_target_channel.c_str()},
208 allow_powerwash).isOk();
209}
210
211bool BinderUpdateEngineClient::GetTargetChannel(string* out_channel) const {
212 String16 out_as_string16;
213
214 if (!service_->GetChannel(false, &out_as_string16).isOk())
215 return false;
216
217 *out_channel = String8{out_as_string16}.string();
218 return true;
219}
220
221bool BinderUpdateEngineClient::GetChannel(string* out_channel) const {
222 String16 out_as_string16;
223
224 if (!service_->GetChannel(true, &out_as_string16).isOk())
225 return false;
226
227 *out_channel = String8{out_as_string16}.string();
228 return true;
229}
230
Shuqian Zhao29971732016-02-05 11:29:32 -0800231bool BinderUpdateEngineClient::GetLastAttemptError(
232 int32_t* last_attempt_error) const {
233 int out_as_int;
234
235 if (!service_->GetLastAttemptError(&out_as_int).isOk())
236 return false;
237
238 *last_attempt_error = out_as_int;
239 return true;
240}
241
Alex Deymob3fa53b2016-04-18 19:57:58 -0700242bool BinderUpdateEngineClient::GetEolStatus(int32_t* eol_status) const {
243 int out_as_int;
244
245 if (!service_->GetEolStatus(&out_as_int).isOk())
246 return false;
247
248 *eol_status = out_as_int;
249 return true;
250}
251
Casey Dahlina93cd532016-01-14 16:55:11 -0800252} // namespace internal
253} // namespace update_engine