blob: 3f01e4245cccb7b5634835eff8e3ac9e56211bd7 [file] [log] [blame]
Alex Deymofa78f142016-01-26 21:36:16 -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/binder_service_brillo.h"
18
19#include <base/bind.h>
20
21#include <binderwrapper/binder_wrapper.h>
22
23#include <utils/String16.h>
24#include <utils/StrongPointer.h>
25
26#include "update_engine/update_status_utils.h"
27
28using android::String16;
29using android::String8;
30using android::binder::Status;
31using android::brillo::IUpdateEngineStatusCallback;
32using android::brillo::ParcelableUpdateEngineStatus;
33using android::sp;
34using brillo::ErrorPtr;
35using std::string;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070036using update_engine::UpdateEngineStatus;
Alex Deymofa78f142016-01-26 21:36:16 -080037
38namespace chromeos_update_engine {
39
40namespace {
41string NormalString(const String16& in) {
42 return string{String8{in}.string()};
43}
44
45Status ToStatus(ErrorPtr* error) {
46 return Status::fromServiceSpecificError(
47 1, String8{error->get()->GetMessage().c_str()});
48}
49} // namespace
50
51template <typename... Parameters, typename... Arguments>
52Status BinderUpdateEngineBrilloService::CallCommonHandler(
53 bool (UpdateEngineService::*Handler)(ErrorPtr*, Parameters...),
54 Arguments... arguments) {
55 ErrorPtr error;
56 if (((common_.get())->*Handler)(&error, arguments...))
57 return Status::ok();
58 return ToStatus(&error);
59}
60
Aaron Woodbf5a2522017-10-04 10:58:36 -070061Status BinderUpdateEngineBrilloService::SetUpdateAttemptFlags(int flags) {
62 return CallCommonHandler(&UpdateEngineService::SetUpdateAttemptFlags, flags);
63}
64
Alex Deymofa78f142016-01-26 21:36:16 -080065Status BinderUpdateEngineBrilloService::AttemptUpdate(
Aaron Wood081c0232017-10-19 17:14:58 -070066 const String16& app_version,
67 const String16& omaha_url,
68 int flags,
69 bool* out_result) {
Alex Deymofa78f142016-01-26 21:36:16 -080070 return CallCommonHandler(&UpdateEngineService::AttemptUpdate,
71 NormalString(app_version),
72 NormalString(omaha_url),
Aaron Wood081c0232017-10-19 17:14:58 -070073 flags,
74 out_result);
Alex Deymofa78f142016-01-26 21:36:16 -080075}
76
77Status BinderUpdateEngineBrilloService::AttemptRollback(bool powerwash) {
78 return CallCommonHandler(&UpdateEngineService::AttemptRollback, powerwash);
79}
80
81Status BinderUpdateEngineBrilloService::CanRollback(bool* out_can_rollback) {
82 return CallCommonHandler(&UpdateEngineService::CanRollback, out_can_rollback);
83}
84
85Status BinderUpdateEngineBrilloService::ResetStatus() {
86 return CallCommonHandler(&UpdateEngineService::ResetStatus);
87}
88
89Status BinderUpdateEngineBrilloService::GetStatus(
90 ParcelableUpdateEngineStatus* status) {
Aaron Wood7f92e2b2017-08-28 14:51:21 -070091 UpdateEngineStatus update_engine_status;
92 auto ret =
93 CallCommonHandler(&UpdateEngineService::GetStatus, &update_engine_status);
Alex Deymofa78f142016-01-26 21:36:16 -080094
95 if (ret.isOk()) {
Aaron Wood7f92e2b2017-08-28 14:51:21 -070096 *status = ParcelableUpdateEngineStatus(update_engine_status);
Alex Deymofa78f142016-01-26 21:36:16 -080097 }
98
99 return ret;
100}
101
102Status BinderUpdateEngineBrilloService::RebootIfNeeded() {
103 return CallCommonHandler(&UpdateEngineService::RebootIfNeeded);
104}
105
106Status BinderUpdateEngineBrilloService::SetChannel(
107 const String16& target_channel, bool powerwash) {
108 return CallCommonHandler(&UpdateEngineService::SetChannel,
109 NormalString(target_channel),
110 powerwash);
111}
112
113Status BinderUpdateEngineBrilloService::GetChannel(bool get_current_channel,
114 String16* out_channel) {
115 string channel_string;
116 auto ret = CallCommonHandler(
117 &UpdateEngineService::GetChannel, get_current_channel, &channel_string);
118
119 *out_channel = String16(channel_string.c_str());
120 return ret;
121}
122
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700123Status BinderUpdateEngineBrilloService::SetCohortHint(
124 const String16& in_cohort_hint) {
125 return CallCommonHandler(&UpdateEngineService::SetCohortHint,
126 NormalString(in_cohort_hint));
127}
128
129Status BinderUpdateEngineBrilloService::GetCohortHint(
130 String16* out_cohort_hint) {
131 string cohort_hint;
132 auto ret =
133 CallCommonHandler(&UpdateEngineService::GetCohortHint, &cohort_hint);
134
135 *out_cohort_hint = String16(cohort_hint.c_str());
136 return ret;
137}
138
Alex Deymofa78f142016-01-26 21:36:16 -0800139Status BinderUpdateEngineBrilloService::SetP2PUpdatePermission(bool enabled) {
140 return CallCommonHandler(&UpdateEngineService::SetP2PUpdatePermission,
141 enabled);
142}
143
144Status BinderUpdateEngineBrilloService::GetP2PUpdatePermission(
145 bool* out_p2p_permission) {
146 return CallCommonHandler(&UpdateEngineService::GetP2PUpdatePermission,
147 out_p2p_permission);
148}
149
150Status BinderUpdateEngineBrilloService::SetUpdateOverCellularPermission(
151 bool enabled) {
152 return CallCommonHandler(
153 &UpdateEngineService::SetUpdateOverCellularPermission, enabled);
154}
155
156Status BinderUpdateEngineBrilloService::GetUpdateOverCellularPermission(
157 bool* out_cellular_permission) {
158 return CallCommonHandler(
159 &UpdateEngineService::GetUpdateOverCellularPermission,
160 out_cellular_permission);
161}
162
163Status BinderUpdateEngineBrilloService::GetDurationSinceUpdate(
164 int64_t* out_duration) {
165 return CallCommonHandler(&UpdateEngineService::GetDurationSinceUpdate,
166 out_duration);
167}
168
169Status BinderUpdateEngineBrilloService::GetPrevVersion(
170 String16* out_prev_version) {
171 string version_string;
172 auto ret =
173 CallCommonHandler(&UpdateEngineService::GetPrevVersion, &version_string);
174
175 *out_prev_version = String16(version_string.c_str());
176 return ret;
177}
178
179Status BinderUpdateEngineBrilloService::GetRollbackPartition(
180 String16* out_rollback_partition) {
181 string partition_string;
182 auto ret = CallCommonHandler(&UpdateEngineService::GetRollbackPartition,
183 &partition_string);
184
185 if (ret.isOk()) {
186 *out_rollback_partition = String16(partition_string.c_str());
187 }
188
189 return ret;
190}
191
192Status BinderUpdateEngineBrilloService::RegisterStatusCallback(
193 const sp<IUpdateEngineStatusCallback>& callback) {
194 callbacks_.emplace_back(callback);
195
196 auto binder_wrapper = android::BinderWrapper::Get();
197
198 binder_wrapper->RegisterForDeathNotifications(
199 IUpdateEngineStatusCallback::asBinder(callback),
200 base::Bind(&BinderUpdateEngineBrilloService::UnregisterStatusCallback,
201 base::Unretained(this),
202 base::Unretained(callback.get())));
203
204 return Status::ok();
205}
206
Shuqian Zhao29971732016-02-05 11:29:32 -0800207Status BinderUpdateEngineBrilloService::GetLastAttemptError(
208 int* out_last_attempt_error) {
209 return CallCommonHandler(&UpdateEngineService::GetLastAttemptError,
210 out_last_attempt_error);
211}
212
Alex Deymob3fa53b2016-04-18 19:57:58 -0700213Status BinderUpdateEngineBrilloService::GetEolStatus(int* out_eol_status) {
214 return CallCommonHandler(&UpdateEngineService::GetEolStatus, out_eol_status);
215}
216
Alex Deymofa78f142016-01-26 21:36:16 -0800217void BinderUpdateEngineBrilloService::UnregisterStatusCallback(
218 IUpdateEngineStatusCallback* callback) {
219 auto it = callbacks_.begin();
Alex Deymob3fa53b2016-04-18 19:57:58 -0700220 while (it != callbacks_.end() && it->get() != callback)
221 it++;
Alex Deymofa78f142016-01-26 21:36:16 -0800222
223 if (it == callbacks_.end()) {
224 LOG(ERROR) << "Got death notification for unknown callback.";
225 return;
226 }
227
228 LOG(INFO) << "Erasing orphan callback";
229 callbacks_.erase(it);
230}
231
232void BinderUpdateEngineBrilloService::SendStatusUpdate(
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700233 const UpdateEngineStatus& update_engine_status) {
234 ParcelableUpdateEngineStatus parcelable_status(update_engine_status);
Alex Deymofa78f142016-01-26 21:36:16 -0800235 for (auto& callback : callbacks_) {
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700236 callback->HandleStatusUpdate(parcelable_status);
Alex Deymofa78f142016-01-26 21:36:16 -0800237 }
238}
239
240} // namespace chromeos_update_engine