Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 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 | |
| 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 Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 22 | #include <utils/String8.h> |
| 23 | |
Casey Dahlin | 6ee50b9 | 2016-02-01 13:40:21 -0800 | [diff] [blame] | 24 | #include "update_engine/common_service.h" |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 25 | #include "update_engine/parcelable_update_engine_status.h" |
| 26 | #include "update_engine/update_status_utils.h" |
| 27 | |
Casey Dahlin | 4089249 | 2016-01-25 16:55:28 -0800 | [diff] [blame] | 28 | using android::binder::Status; |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 29 | using android::brillo::ParcelableUpdateEngineStatus; |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 30 | using android::getService; |
Aaron Wood | 224dfc2 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 31 | using android::OK; |
| 32 | using android::String16; |
| 33 | using android::String8; |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 34 | using chromeos_update_engine::StringToUpdateStatus; |
| 35 | using std::string; |
Aaron Wood | 224dfc2 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 36 | using update_engine::UpdateAttemptFlags; |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 37 | |
| 38 | namespace update_engine { |
| 39 | namespace internal { |
| 40 | |
| 41 | bool BinderUpdateEngineClient::Init() { |
Casey Dahlin | 4089249 | 2016-01-25 16:55:28 -0800 | [diff] [blame] | 42 | if (!binder_watcher_.Init()) return false; |
| 43 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 44 | return getService(String16{"android.brillo.UpdateEngineService"}, |
| 45 | &service_) == OK; |
| 46 | } |
| 47 | |
| 48 | bool BinderUpdateEngineClient::AttemptUpdate(const string& in_app_version, |
| 49 | const string& in_omaha_url, |
| 50 | bool at_user_request) { |
Aaron Wood | 3a9c962 | 2017-10-19 17:14:58 -0700 | [diff] [blame^] | 51 | bool started; |
Aaron Wood | 224dfc2 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 52 | return service_ |
| 53 | ->AttemptUpdate( |
| 54 | String16{in_app_version.c_str()}, |
| 55 | String16{in_omaha_url.c_str()}, |
Aaron Wood | 3a9c962 | 2017-10-19 17:14:58 -0700 | [diff] [blame^] | 56 | at_user_request ? 0 : UpdateAttemptFlags::kFlagNonInteractive, |
| 57 | &started) |
Aaron Wood | 224dfc2 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 58 | .isOk(); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | bool BinderUpdateEngineClient::GetStatus(int64_t* out_last_checked_time, |
| 62 | double* out_progress, |
| 63 | UpdateStatus* out_update_status, |
| 64 | string* out_new_version, |
| 65 | int64_t* out_new_size) const { |
| 66 | ParcelableUpdateEngineStatus status; |
| 67 | |
| 68 | if (!service_->GetStatus(&status).isOk()) |
| 69 | return false; |
| 70 | |
| 71 | *out_last_checked_time = status.last_checked_time_; |
| 72 | *out_progress = status.progress_; |
| 73 | StringToUpdateStatus(String8{status.current_operation_}.string(), |
| 74 | out_update_status); |
| 75 | *out_new_version = String8{status.new_version_}.string(); |
| 76 | *out_new_size = status.new_size_; |
| 77 | return true; |
| 78 | } |
| 79 | |
Alex Deymo | 5b5fa8b | 2016-10-06 15:40:49 -0700 | [diff] [blame] | 80 | bool BinderUpdateEngineClient::SetCohortHint(const string& in_cohort_hint) { |
| 81 | return service_->SetCohortHint(String16{in_cohort_hint.c_str()}).isOk(); |
| 82 | } |
| 83 | |
| 84 | bool BinderUpdateEngineClient::GetCohortHint(string* out_cohort_hint) const { |
| 85 | String16 out_as_string16; |
| 86 | |
| 87 | if (!service_->GetCohortHint(&out_as_string16).isOk()) |
| 88 | return false; |
| 89 | |
| 90 | *out_cohort_hint = String8{out_as_string16}.string(); |
| 91 | return true; |
| 92 | } |
| 93 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 94 | bool BinderUpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) { |
| 95 | return service_->SetUpdateOverCellularPermission(allowed).isOk(); |
| 96 | } |
| 97 | |
| 98 | bool BinderUpdateEngineClient::GetUpdateOverCellularPermission( |
| 99 | bool* allowed) const { |
| 100 | return service_->GetUpdateOverCellularPermission(allowed).isOk(); |
| 101 | } |
| 102 | |
| 103 | bool BinderUpdateEngineClient::SetP2PUpdatePermission(bool enabled) { |
| 104 | return service_->SetP2PUpdatePermission(enabled).isOk(); |
| 105 | } |
| 106 | |
| 107 | bool BinderUpdateEngineClient::GetP2PUpdatePermission(bool* enabled) const { |
| 108 | return service_->GetP2PUpdatePermission(enabled).isOk(); |
| 109 | } |
| 110 | |
| 111 | bool BinderUpdateEngineClient::Rollback(bool powerwash) { |
| 112 | return service_->AttemptRollback(powerwash).isOk(); |
| 113 | } |
| 114 | |
| 115 | bool BinderUpdateEngineClient::GetRollbackPartition( |
| 116 | string* rollback_partition) const { |
| 117 | String16 out_as_string16; |
| 118 | |
| 119 | if (!service_->GetRollbackPartition(&out_as_string16).isOk()) |
| 120 | return false; |
| 121 | |
| 122 | *rollback_partition = String8{out_as_string16}.string(); |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | bool BinderUpdateEngineClient::GetPrevVersion(string* prev_version) const { |
| 127 | String16 out_as_string16; |
| 128 | |
| 129 | if (!service_->GetPrevVersion(&out_as_string16).isOk()) |
| 130 | return false; |
| 131 | |
| 132 | *prev_version = String8{out_as_string16}.string(); |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | void BinderUpdateEngineClient::RebootIfNeeded() { |
| 137 | if (!service_->RebootIfNeeded().isOk()) { |
| 138 | // Reboot error code doesn't necessarily mean that a reboot |
| 139 | // failed. For example, D-Bus may be shutdown before we receive the |
| 140 | // result. |
| 141 | LOG(INFO) << "RebootIfNeeded() failure ignored."; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | bool BinderUpdateEngineClient::ResetStatus() { |
| 146 | return service_->ResetStatus().isOk(); |
| 147 | } |
| 148 | |
Casey Dahlin | 4089249 | 2016-01-25 16:55:28 -0800 | [diff] [blame] | 149 | Status BinderUpdateEngineClient::StatusUpdateCallback::HandleStatusUpdate( |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 150 | const ParcelableUpdateEngineStatus& status) { |
Casey Dahlin | 4089249 | 2016-01-25 16:55:28 -0800 | [diff] [blame] | 151 | UpdateStatus update_status; |
| 152 | |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 153 | StringToUpdateStatus(String8{status.current_operation_}.string(), |
| 154 | &update_status); |
Casey Dahlin | 4089249 | 2016-01-25 16:55:28 -0800 | [diff] [blame] | 155 | |
| 156 | for (auto& handler : client_->handlers_) { |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 157 | handler->HandleStatusUpdate(status.last_checked_time_, |
| 158 | status.progress_, |
| 159 | update_status, |
| 160 | String8{status.new_version_}.string(), |
| 161 | status.new_size_); |
Casey Dahlin | 4089249 | 2016-01-25 16:55:28 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | return Status::ok(); |
| 165 | } |
| 166 | |
| 167 | bool BinderUpdateEngineClient::RegisterStatusUpdateHandler( |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 168 | StatusUpdateHandler* handler) { |
Casey Dahlin | 4089249 | 2016-01-25 16:55:28 -0800 | [diff] [blame] | 169 | if (!status_callback_.get()) { |
| 170 | status_callback_ = |
| 171 | new BinderUpdateEngineClient::StatusUpdateCallback(this); |
| 172 | if (!service_->RegisterStatusCallback(status_callback_).isOk()) { |
| 173 | return false; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | handlers_.push_back(handler); |
Casey Dahlin | a715f7b | 2016-01-29 16:38:51 -0800 | [diff] [blame] | 178 | |
| 179 | int64_t last_checked_time; |
| 180 | double progress; |
| 181 | UpdateStatus update_status; |
| 182 | string new_version; |
| 183 | int64_t new_size; |
| 184 | |
| 185 | if (!GetStatus(&last_checked_time, &progress, &update_status, |
| 186 | &new_version, &new_size)) { |
| 187 | handler->IPCError("Could not get status from binder service"); |
| 188 | } |
| 189 | |
| 190 | handler->HandleStatusUpdate(last_checked_time, progress, update_status, |
| 191 | new_version, new_size); |
| 192 | |
Casey Dahlin | 4089249 | 2016-01-25 16:55:28 -0800 | [diff] [blame] | 193 | return true; |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Casey Dahlin | a715f7b | 2016-01-29 16:38:51 -0800 | [diff] [blame] | 196 | bool BinderUpdateEngineClient::UnregisterStatusUpdateHandler( |
| 197 | StatusUpdateHandler* handler) { |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 198 | auto it = std::find(handlers_.begin(), handlers_.end(), handler); |
Casey Dahlin | a715f7b | 2016-01-29 16:38:51 -0800 | [diff] [blame] | 199 | if (it != handlers_.end()) { |
| 200 | handlers_.erase(it); |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | return false; |
| 205 | } |
| 206 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 207 | bool BinderUpdateEngineClient::SetTargetChannel(const string& in_target_channel, |
| 208 | bool allow_powerwash) { |
| 209 | return service_->SetChannel(String16{in_target_channel.c_str()}, |
| 210 | allow_powerwash).isOk(); |
| 211 | } |
| 212 | |
| 213 | bool BinderUpdateEngineClient::GetTargetChannel(string* out_channel) const { |
| 214 | String16 out_as_string16; |
| 215 | |
| 216 | if (!service_->GetChannel(false, &out_as_string16).isOk()) |
| 217 | return false; |
| 218 | |
| 219 | *out_channel = String8{out_as_string16}.string(); |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | bool BinderUpdateEngineClient::GetChannel(string* out_channel) const { |
| 224 | String16 out_as_string16; |
| 225 | |
| 226 | if (!service_->GetChannel(true, &out_as_string16).isOk()) |
| 227 | return false; |
| 228 | |
| 229 | *out_channel = String8{out_as_string16}.string(); |
| 230 | return true; |
| 231 | } |
| 232 | |
Shuqian Zhao | 2997173 | 2016-02-05 11:29:32 -0800 | [diff] [blame] | 233 | bool BinderUpdateEngineClient::GetLastAttemptError( |
| 234 | int32_t* last_attempt_error) const { |
| 235 | int out_as_int; |
| 236 | |
| 237 | if (!service_->GetLastAttemptError(&out_as_int).isOk()) |
| 238 | return false; |
| 239 | |
| 240 | *last_attempt_error = out_as_int; |
| 241 | return true; |
| 242 | } |
| 243 | |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 244 | bool BinderUpdateEngineClient::GetEolStatus(int32_t* eol_status) const { |
| 245 | int out_as_int; |
| 246 | |
| 247 | if (!service_->GetEolStatus(&out_as_int).isOk()) |
| 248 | return false; |
| 249 | |
| 250 | *eol_status = out_as_int; |
| 251 | return true; |
| 252 | } |
| 253 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 254 | } // namespace internal |
| 255 | } // namespace update_engine |