| 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 |  | 
|  | 24 | #include "update_engine/parcelable_update_engine_status.h" | 
|  | 25 | #include "update_engine/update_status_utils.h" | 
|  | 26 |  | 
|  | 27 | using android::OK; | 
|  | 28 | using android::String16; | 
|  | 29 | using android::String8; | 
|  | 30 | using android::brillo::ParcelableUpdateEngineStatus; | 
| Casey Dahlin | 4089249 | 2016-01-25 16:55:28 -0800 | [diff] [blame] | 31 | using android::binder::Status; | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 32 | using android::getService; | 
|  | 33 | using chromeos_update_engine::StringToUpdateStatus; | 
|  | 34 | using std::string; | 
|  | 35 |  | 
|  | 36 | namespace update_engine { | 
|  | 37 | namespace internal { | 
|  | 38 |  | 
|  | 39 | bool BinderUpdateEngineClient::Init() { | 
| Casey Dahlin | 4089249 | 2016-01-25 16:55:28 -0800 | [diff] [blame] | 40 | if (!binder_watcher_.Init()) return false; | 
|  | 41 |  | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 42 | return getService(String16{"android.brillo.UpdateEngineService"}, | 
|  | 43 | &service_) == OK; | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | bool BinderUpdateEngineClient::AttemptUpdate(const string& in_app_version, | 
|  | 47 | const string& in_omaha_url, | 
|  | 48 | bool at_user_request) { | 
|  | 49 | return service_->AttemptUpdate(String16{in_app_version.c_str()}, | 
|  | 50 | String16{in_omaha_url.c_str()}, | 
|  | 51 | at_user_request ? 1 : 0).isOk(); | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | bool BinderUpdateEngineClient::GetStatus(int64_t* out_last_checked_time, | 
|  | 55 | double* out_progress, | 
|  | 56 | UpdateStatus* out_update_status, | 
|  | 57 | string* out_new_version, | 
|  | 58 | int64_t* out_new_size) const { | 
|  | 59 | ParcelableUpdateEngineStatus status; | 
|  | 60 |  | 
|  | 61 | if (!service_->GetStatus(&status).isOk()) | 
|  | 62 | return false; | 
|  | 63 |  | 
|  | 64 | *out_last_checked_time = status.last_checked_time_; | 
|  | 65 | *out_progress = status.progress_; | 
|  | 66 | StringToUpdateStatus(String8{status.current_operation_}.string(), | 
|  | 67 | out_update_status); | 
|  | 68 | *out_new_version = String8{status.new_version_}.string(); | 
|  | 69 | *out_new_size = status.new_size_; | 
|  | 70 | return true; | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | bool BinderUpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) { | 
|  | 74 | return service_->SetUpdateOverCellularPermission(allowed).isOk(); | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | bool BinderUpdateEngineClient::GetUpdateOverCellularPermission( | 
|  | 78 | bool* allowed) const { | 
|  | 79 | return service_->GetUpdateOverCellularPermission(allowed).isOk(); | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | bool BinderUpdateEngineClient::SetP2PUpdatePermission(bool enabled) { | 
|  | 83 | return service_->SetP2PUpdatePermission(enabled).isOk(); | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | bool BinderUpdateEngineClient::GetP2PUpdatePermission(bool* enabled) const { | 
|  | 87 | return service_->GetP2PUpdatePermission(enabled).isOk(); | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | bool BinderUpdateEngineClient::Rollback(bool powerwash) { | 
|  | 91 | return service_->AttemptRollback(powerwash).isOk(); | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | bool BinderUpdateEngineClient::GetRollbackPartition( | 
|  | 95 | string* rollback_partition) const { | 
|  | 96 | String16 out_as_string16; | 
|  | 97 |  | 
|  | 98 | if (!service_->GetRollbackPartition(&out_as_string16).isOk()) | 
|  | 99 | return false; | 
|  | 100 |  | 
|  | 101 | *rollback_partition = String8{out_as_string16}.string(); | 
|  | 102 | return true; | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | bool BinderUpdateEngineClient::GetPrevVersion(string* prev_version) const { | 
|  | 106 | String16 out_as_string16; | 
|  | 107 |  | 
|  | 108 | if (!service_->GetPrevVersion(&out_as_string16).isOk()) | 
|  | 109 | return false; | 
|  | 110 |  | 
|  | 111 | *prev_version = String8{out_as_string16}.string(); | 
|  | 112 | return true; | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | void BinderUpdateEngineClient::RebootIfNeeded() { | 
|  | 116 | if (!service_->RebootIfNeeded().isOk()) { | 
|  | 117 | // Reboot error code doesn't necessarily mean that a reboot | 
|  | 118 | // failed. For example, D-Bus may be shutdown before we receive the | 
|  | 119 | // result. | 
|  | 120 | LOG(INFO) << "RebootIfNeeded() failure ignored."; | 
|  | 121 | } | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | bool BinderUpdateEngineClient::ResetStatus() { | 
|  | 125 | return service_->ResetStatus().isOk(); | 
|  | 126 | } | 
|  | 127 |  | 
| Casey Dahlin | 4089249 | 2016-01-25 16:55:28 -0800 | [diff] [blame] | 128 | Status BinderUpdateEngineClient::StatusUpdateCallback::HandleStatusUpdate( | 
|  | 129 | int64_t last_checked_time, | 
|  | 130 | double progress, | 
|  | 131 | const String16& current_operation, | 
|  | 132 | const String16& new_version, | 
|  | 133 | int64_t new_size) { | 
|  | 134 | UpdateStatus update_status; | 
|  | 135 |  | 
|  | 136 | StringToUpdateStatus(String8{current_operation}.string(), &update_status); | 
|  | 137 |  | 
|  | 138 | for (auto& handler : client_->handlers_) { | 
|  | 139 | handler->HandleStatusUpdate(last_checked_time, progress, update_status, | 
|  | 140 | String8{new_version}.string(), new_size); | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | return Status::ok(); | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | bool BinderUpdateEngineClient::RegisterStatusUpdateHandler( | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 147 | StatusUpdateHandler* handler) { | 
| Casey Dahlin | 4089249 | 2016-01-25 16:55:28 -0800 | [diff] [blame] | 148 | if (!status_callback_.get()) { | 
|  | 149 | status_callback_ = | 
|  | 150 | new BinderUpdateEngineClient::StatusUpdateCallback(this); | 
|  | 151 | if (!service_->RegisterStatusCallback(status_callback_).isOk()) { | 
|  | 152 | return false; | 
|  | 153 | } | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | handlers_.push_back(handler); | 
|  | 157 | return true; | 
| Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 158 | } | 
|  | 159 |  | 
|  | 160 | bool BinderUpdateEngineClient::SetTargetChannel(const string& in_target_channel, | 
|  | 161 | bool allow_powerwash) { | 
|  | 162 | return service_->SetChannel(String16{in_target_channel.c_str()}, | 
|  | 163 | allow_powerwash).isOk(); | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | bool BinderUpdateEngineClient::GetTargetChannel(string* out_channel) const { | 
|  | 167 | String16 out_as_string16; | 
|  | 168 |  | 
|  | 169 | if (!service_->GetChannel(false, &out_as_string16).isOk()) | 
|  | 170 | return false; | 
|  | 171 |  | 
|  | 172 | *out_channel = String8{out_as_string16}.string(); | 
|  | 173 | return true; | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | bool BinderUpdateEngineClient::GetChannel(string* out_channel) const { | 
|  | 177 | String16 out_as_string16; | 
|  | 178 |  | 
|  | 179 | if (!service_->GetChannel(true, &out_as_string16).isOk()) | 
|  | 180 | return false; | 
|  | 181 |  | 
|  | 182 | *out_channel = String8{out_as_string16}.string(); | 
|  | 183 | return true; | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | }  // namespace internal | 
|  | 187 | }  // namespace update_engine |