Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 1 | // 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 Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 14 | |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 15 | #include "libweaved/command.h" |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 16 | |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 17 | #include "android/weave/IWeaveCommand.h" |
| 18 | #include "common/binder_utils.h" |
| 19 | #include "common/data_conversion.h" |
| 20 | |
| 21 | using weaved::binder_utils::ParseDictionary; |
| 22 | using weaved::binder_utils::ToString; |
| 23 | using weaved::binder_utils::ToString16; |
| 24 | using weaved::binder_utils::StatusToError; |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 25 | |
| 26 | namespace weaved { |
| 27 | |
Alex Vakulenko | 13d115c | 2016-01-07 10:47:57 -0800 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | // Converts binder exception code into a weave error code string. |
| 31 | std::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 Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 56 | Command::Command(const android::sp<android::weave::IWeaveCommand>& proxy) |
| 57 | : binder_proxy_{proxy} {} |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 58 | |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 59 | Command::~Command() {} |
| 60 | |
| 61 | std::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 Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 69 | std::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 Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 77 | std::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 Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 85 | Command::State Command::GetState() const { |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 86 | std::string state; |
| 87 | android::String16 state16; |
| 88 | if (binder_proxy_->getState(&state16).isOk()) |
| 89 | state.assign(ToString(state16)); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 90 | 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 | |
| 110 | Command::Origin Command::GetOrigin() const { |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 111 | std::string origin; |
| 112 | android::String16 origin16; |
| 113 | if (binder_proxy_->getState(&origin16).isOk()) |
| 114 | origin.assign(ToString(origin16)); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 115 | 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 Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 123 | brillo::VariantDictionary Command::GetParameters() const { |
| 124 | brillo::VariantDictionary params; |
| 125 | android::String16 params_string16; |
| 126 | if (binder_proxy_->getParameters(¶ms_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 Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 134 | bool Command::SetProgress(const brillo::VariantDictionary& progress, |
| 135 | brillo::ErrorPtr* error) { |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 136 | auto dict = details::VariantDictionaryToDictionaryValue(progress, error); |
| 137 | return dict && StatusToError(binder_proxy_->setProgress(ToString16(*dict)), |
| 138 | error); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 141 | bool Command::Complete(const brillo::VariantDictionary& results, |
| 142 | brillo::ErrorPtr* error) { |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 143 | auto dict = details::VariantDictionaryToDictionaryValue(results, error); |
| 144 | return dict && StatusToError(binder_proxy_->complete(ToString16(*dict)), |
| 145 | error); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | bool Command::Abort(const std::string& error_code, |
| 149 | const std::string& error_message, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 150 | brillo::ErrorPtr* error) { |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 151 | return StatusToError(binder_proxy_->abort(ToString16(error_code), |
| 152 | ToString16(error_message)), |
| 153 | error); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Alex Vakulenko | 13d115c | 2016-01-07 10:47:57 -0800 | [diff] [blame] | 156 | bool 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 | |
| 162 | bool 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 Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 168 | bool Command::Cancel(brillo::ErrorPtr* error) { |
Alex Vakulenko | ae29f7d | 2015-12-21 16:30:37 -0800 | [diff] [blame] | 169 | return StatusToError(binder_proxy_->cancel(), error); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Alex Vakulenko | 13d115c | 2016-01-07 10:47:57 -0800 | [diff] [blame] | 172 | bool Command::Pause(brillo::ErrorPtr* error) { |
| 173 | return StatusToError(binder_proxy_->pause(), error); |
| 174 | } |
| 175 | |
| 176 | bool 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 | |
| 184 | bool 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 | |
| 190 | bool 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 Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 196 | } // namespace weave |