blob: 5c22f8469e7e4f21d06e76f173debe55011ff136 [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 Wood3a9c9622017-10-19 17:14:58 -070051 bool started;
Aaron Wood224dfc22017-10-04 10:58:36 -070052 return service_
53 ->AttemptUpdate(
54 String16{in_app_version.c_str()},
55 String16{in_omaha_url.c_str()},
Aaron Wood3a9c9622017-10-19 17:14:58 -070056 at_user_request ? 0 : UpdateAttemptFlags::kFlagNonInteractive,
57 &started)
Aaron Wood224dfc22017-10-04 10:58:36 -070058 .isOk();
Casey Dahlina93cd532016-01-14 16:55:11 -080059}
60
Sen Jiang3ce4e522019-01-04 15:34:19 -080061bool BinderUpdateEngineClient::AttemptInstall(
62 const string& omaha_url, const std::vector<string>& dlc_module_ids) {
63 return false;
64}
65
Casey Dahlina93cd532016-01-14 16:55:11 -080066bool BinderUpdateEngineClient::GetStatus(int64_t* out_last_checked_time,
67 double* out_progress,
68 UpdateStatus* out_update_status,
69 string* out_new_version,
70 int64_t* out_new_size) const {
71 ParcelableUpdateEngineStatus status;
72
73 if (!service_->GetStatus(&status).isOk())
74 return false;
75
76 *out_last_checked_time = status.last_checked_time_;
77 *out_progress = status.progress_;
78 StringToUpdateStatus(String8{status.current_operation_}.string(),
79 out_update_status);
80 *out_new_version = String8{status.new_version_}.string();
81 *out_new_size = status.new_size_;
82 return true;
83}
84
Alex Deymo5b5fa8b2016-10-06 15:40:49 -070085bool BinderUpdateEngineClient::SetCohortHint(const string& in_cohort_hint) {
86 return service_->SetCohortHint(String16{in_cohort_hint.c_str()}).isOk();
87}
88
89bool BinderUpdateEngineClient::GetCohortHint(string* out_cohort_hint) const {
90 String16 out_as_string16;
91
92 if (!service_->GetCohortHint(&out_as_string16).isOk())
93 return false;
94
95 *out_cohort_hint = String8{out_as_string16}.string();
96 return true;
97}
98
Casey Dahlina93cd532016-01-14 16:55:11 -080099bool BinderUpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) {
100 return service_->SetUpdateOverCellularPermission(allowed).isOk();
101}
102
103bool BinderUpdateEngineClient::GetUpdateOverCellularPermission(
104 bool* allowed) const {
105 return service_->GetUpdateOverCellularPermission(allowed).isOk();
106}
107
108bool BinderUpdateEngineClient::SetP2PUpdatePermission(bool enabled) {
109 return service_->SetP2PUpdatePermission(enabled).isOk();
110}
111
112bool BinderUpdateEngineClient::GetP2PUpdatePermission(bool* enabled) const {
113 return service_->GetP2PUpdatePermission(enabled).isOk();
114}
115
116bool BinderUpdateEngineClient::Rollback(bool powerwash) {
117 return service_->AttemptRollback(powerwash).isOk();
118}
119
120bool BinderUpdateEngineClient::GetRollbackPartition(
121 string* rollback_partition) const {
122 String16 out_as_string16;
123
124 if (!service_->GetRollbackPartition(&out_as_string16).isOk())
125 return false;
126
127 *rollback_partition = String8{out_as_string16}.string();
128 return true;
129}
130
131bool BinderUpdateEngineClient::GetPrevVersion(string* prev_version) const {
132 String16 out_as_string16;
133
134 if (!service_->GetPrevVersion(&out_as_string16).isOk())
135 return false;
136
137 *prev_version = String8{out_as_string16}.string();
138 return true;
139}
140
141void BinderUpdateEngineClient::RebootIfNeeded() {
142 if (!service_->RebootIfNeeded().isOk()) {
143 // Reboot error code doesn't necessarily mean that a reboot
144 // failed. For example, D-Bus may be shutdown before we receive the
145 // result.
146 LOG(INFO) << "RebootIfNeeded() failure ignored.";
147 }
148}
149
150bool BinderUpdateEngineClient::ResetStatus() {
151 return service_->ResetStatus().isOk();
152}
153
Casey Dahlin40892492016-01-25 16:55:28 -0800154Status BinderUpdateEngineClient::StatusUpdateCallback::HandleStatusUpdate(
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700155 const ParcelableUpdateEngineStatus& status) {
Casey Dahlin40892492016-01-25 16:55:28 -0800156 UpdateStatus update_status;
157
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700158 StringToUpdateStatus(String8{status.current_operation_}.string(),
159 &update_status);
Casey Dahlin40892492016-01-25 16:55:28 -0800160
161 for (auto& handler : client_->handlers_) {
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700162 handler->HandleStatusUpdate(status.last_checked_time_,
163 status.progress_,
164 update_status,
165 String8{status.new_version_}.string(),
166 status.new_size_);
Casey Dahlin40892492016-01-25 16:55:28 -0800167 }
168
169 return Status::ok();
170}
171
172bool BinderUpdateEngineClient::RegisterStatusUpdateHandler(
Casey Dahlina93cd532016-01-14 16:55:11 -0800173 StatusUpdateHandler* handler) {
Casey Dahlin40892492016-01-25 16:55:28 -0800174 if (!status_callback_.get()) {
175 status_callback_ =
176 new BinderUpdateEngineClient::StatusUpdateCallback(this);
177 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
190 if (!GetStatus(&last_checked_time, &progress, &update_status,
191 &new_version, &new_size)) {
192 handler->IPCError("Could not get status from binder service");
193 }
194
195 handler->HandleStatusUpdate(last_checked_time, progress, update_status,
196 new_version, new_size);
197
Casey Dahlin40892492016-01-25 16:55:28 -0800198 return true;
Casey Dahlina93cd532016-01-14 16:55:11 -0800199}
200
Casey Dahlina715f7b2016-01-29 16:38:51 -0800201bool BinderUpdateEngineClient::UnregisterStatusUpdateHandler(
202 StatusUpdateHandler* handler) {
Alex Deymob3fa53b2016-04-18 19:57:58 -0700203 auto it = std::find(handlers_.begin(), handlers_.end(), handler);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800204 if (it != handlers_.end()) {
205 handlers_.erase(it);
206 return true;
207 }
208
209 return false;
210}
211
Casey Dahlina93cd532016-01-14 16:55:11 -0800212bool BinderUpdateEngineClient::SetTargetChannel(const string& in_target_channel,
213 bool allow_powerwash) {
214 return service_->SetChannel(String16{in_target_channel.c_str()},
215 allow_powerwash).isOk();
216}
217
218bool BinderUpdateEngineClient::GetTargetChannel(string* out_channel) const {
219 String16 out_as_string16;
220
221 if (!service_->GetChannel(false, &out_as_string16).isOk())
222 return false;
223
224 *out_channel = String8{out_as_string16}.string();
225 return true;
226}
227
228bool BinderUpdateEngineClient::GetChannel(string* out_channel) const {
229 String16 out_as_string16;
230
231 if (!service_->GetChannel(true, &out_as_string16).isOk())
232 return false;
233
234 *out_channel = String8{out_as_string16}.string();
235 return true;
236}
237
Shuqian Zhao29971732016-02-05 11:29:32 -0800238bool BinderUpdateEngineClient::GetLastAttemptError(
239 int32_t* last_attempt_error) const {
240 int out_as_int;
241
242 if (!service_->GetLastAttemptError(&out_as_int).isOk())
243 return false;
244
245 *last_attempt_error = out_as_int;
246 return true;
247}
248
Alex Deymob3fa53b2016-04-18 19:57:58 -0700249bool BinderUpdateEngineClient::GetEolStatus(int32_t* eol_status) const {
250 int out_as_int;
251
252 if (!service_->GetEolStatus(&out_as_int).isOk())
253 return false;
254
255 *eol_status = out_as_int;
256 return true;
257}
258
Casey Dahlina93cd532016-01-14 16:55:11 -0800259} // namespace internal
260} // namespace update_engine