blob: fa10955290fc444f6945c32fee908bd815043694 [file] [log] [blame]
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -08001// Copyright 2015 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070014
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080015#include "libweaved/command.h"
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070016
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080017#include "android/weave/IWeaveCommand.h"
18#include "common/binder_utils.h"
19#include "common/data_conversion.h"
20
21using weaved::binder_utils::ParseDictionary;
22using weaved::binder_utils::ToString;
23using weaved::binder_utils::ToString16;
24using weaved::binder_utils::StatusToError;
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070025
26namespace weaved {
27
Alex Vakulenko13d115c2016-01-07 10:47:57 -080028namespace {
29
30// Converts binder exception code into a weave error code string.
31std::string BinderExceptionString(int32_t exception_code) {
32 if (exception_code == android::binder::Status::EX_NONE)
33 return "_none";
34 else if (exception_code == android::binder::Status::EX_SECURITY)
35 return "_security";
36 else if (exception_code == android::binder::Status::EX_BAD_PARCELABLE)
37 return "_bad_parcelable";
38 else if (exception_code == android::binder::Status::EX_ILLEGAL_ARGUMENT)
39 return "_illegal_argument";
40 else if (exception_code == android::binder::Status::EX_NULL_POINTER)
41 return "_null_pointer";
42 else if (exception_code == android::binder::Status::EX_ILLEGAL_STATE)
43 return "_illegal_state";
44 else if (exception_code == android::binder::Status::EX_NETWORK_MAIN_THREAD)
45 return "_network_error";
46 else if (exception_code == android::binder::Status::EX_UNSUPPORTED_OPERATION)
47 return "_unsupported_operation";
48 else if (exception_code == android::binder::Status::EX_SERVICE_SPECIFIC)
49 return "_general_failure";
50
51 return "_unknown";
52}
53
54} // anonymous namespace
55
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080056Command::Command(const android::sp<android::weave::IWeaveCommand>& proxy)
57 : binder_proxy_{proxy} {}
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070058
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080059Command::~Command() {}
60
61std::string Command::GetID() const {
62 std::string id;
63 android::String16 id16;
64 if (binder_proxy_->getId(&id16).isOk())
65 id.assign(ToString(id16));
66 return id;
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070067}
68
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080069std::string Command::GetName() const {
70 std::string name;
71 android::String16 name16;
72 if (binder_proxy_->getId(&name16).isOk())
73 name.assign(ToString(name16));
74 return name;
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070075}
76
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080077std::string Command::GetComponent() const {
78 std::string component;
79 android::String16 component16;
80 if (binder_proxy_->getId(&component16).isOk())
81 component.assign(ToString(component16));
82 return component;
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -080083}
84
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070085Command::State Command::GetState() const {
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080086 std::string state;
87 android::String16 state16;
88 if (binder_proxy_->getState(&state16).isOk())
89 state.assign(ToString(state16));
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070090 if (state == "queued")
91 return Command::State::kQueued;
92 else if (state == "inProgress")
93 return Command::State::kInProgress;
94 else if (state == "paused")
95 return Command::State::kPaused;
96 else if (state == "error")
97 return Command::State::kError;
98 else if (state == "done")
99 return Command::State::kDone;
100 else if (state == "cancelled")
101 return Command::State::kCancelled;
102 else if (state == "aborted")
103 return Command::State::kAborted;
104 else if (state == "expired")
105 return Command::State::kExpired;
106 LOG(WARNING) << "Unknown command state: " << state;
107 return Command::State::kQueued;
108}
109
110Command::Origin Command::GetOrigin() const {
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -0800111 std::string origin;
112 android::String16 origin16;
113 if (binder_proxy_->getState(&origin16).isOk())
114 origin.assign(ToString(origin16));
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700115 if (origin == "local")
116 return Command::Origin::kLocal;
117 else if (origin == "cloud")
118 return Command::Origin::kCloud;
119 LOG(WARNING) << "Unknown command origin: " << origin;
120 return Command::Origin::kLocal;
121}
122
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -0800123brillo::VariantDictionary Command::GetParameters() const {
124 brillo::VariantDictionary params;
125 android::String16 params_string16;
126 if (binder_proxy_->getParameters(&params_string16).isOk()) {
127 std::unique_ptr<base::DictionaryValue> dict;
128 if (ParseDictionary(params_string16, &dict).isOk())
129 params = details::DictionaryValueToVariantDictionary(*dict);
130 }
131 return params;
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700132}
133
Alex Vakulenko41705852015-10-13 10:12:06 -0700134bool Command::SetProgress(const brillo::VariantDictionary& progress,
135 brillo::ErrorPtr* error) {
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -0800136 auto dict = details::VariantDictionaryToDictionaryValue(progress, error);
137 return dict && StatusToError(binder_proxy_->setProgress(ToString16(*dict)),
138 error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700139}
140
Alex Vakulenko41705852015-10-13 10:12:06 -0700141bool Command::Complete(const brillo::VariantDictionary& results,
142 brillo::ErrorPtr* error) {
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -0800143 auto dict = details::VariantDictionaryToDictionaryValue(results, error);
144 return dict && StatusToError(binder_proxy_->complete(ToString16(*dict)),
145 error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700146}
147
148bool Command::Abort(const std::string& error_code,
149 const std::string& error_message,
Alex Vakulenko41705852015-10-13 10:12:06 -0700150 brillo::ErrorPtr* error) {
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -0800151 return StatusToError(binder_proxy_->abort(ToString16(error_code),
152 ToString16(error_message)),
153 error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700154}
155
Alex Vakulenko13d115c2016-01-07 10:47:57 -0800156bool Command::AbortWithCustomError(const brillo::Error* command_error,
157 brillo::ErrorPtr* error) {
158 std::string error_code = "_" + command_error->GetCode();
159 return Abort(error_code, command_error->GetMessage(), error);
160}
161
162bool Command::AbortWithCustomError(android::binder::Status status,
163 brillo::ErrorPtr* error) {
164 std::string error_code = BinderExceptionString(status.exceptionCode());
165 return Abort(error_code, status.exceptionMessage().string(), error);
166}
167
Alex Vakulenko41705852015-10-13 10:12:06 -0700168bool Command::Cancel(brillo::ErrorPtr* error) {
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -0800169 return StatusToError(binder_proxy_->cancel(), error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700170}
171
Alex Vakulenko13d115c2016-01-07 10:47:57 -0800172bool Command::Pause(brillo::ErrorPtr* error) {
173 return StatusToError(binder_proxy_->pause(), error);
174}
175
176bool Command::SetError(const std::string& error_code,
177 const std::string& error_message,
178 brillo::ErrorPtr* error) {
179 return StatusToError(binder_proxy_->setError(ToString16(error_code),
180 ToString16(error_message)),
181 error);
182}
183
184bool Command::SetCustomError(const brillo::Error* command_error,
185 brillo::ErrorPtr* error) {
186 std::string error_code = "_" + command_error->GetCode();
187 return SetError(error_code, command_error->GetMessage(), error);
188}
189
190bool Command::SetCustomError(android::binder::Status status,
191 brillo::ErrorPtr* error) {
192 std::string error_code = BinderExceptionString(status.exceptionCode());
193 return SetError(error_code, status.exceptionMessage().string(), error);
194}
195
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700196} // namespace weave