Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | #ifndef LIBWEAVED_COMMAND_H_ |
| 18 | #define LIBWEAVED_COMMAND_H_ |
| 19 | |
| 20 | #include <string> |
| 21 | |
| 22 | #include <base/macros.h> |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 23 | #include <brillo/errors/error.h> |
| 24 | #include <brillo/variant_dictionary.h> |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 25 | #include <libweaved/export.h> |
| 26 | |
| 27 | namespace com { |
| 28 | namespace android { |
| 29 | namespace Weave { |
| 30 | class CommandProxy; |
| 31 | } // namespace Weave |
| 32 | } // namespace android |
| 33 | } // namespace com |
| 34 | |
| 35 | |
| 36 | namespace weaved { |
| 37 | |
| 38 | class Device; |
| 39 | |
| 40 | class LIBWEAVED_EXPORT Command final { |
| 41 | public: |
| 42 | enum class State { |
| 43 | kQueued, |
| 44 | kInProgress, |
| 45 | kPaused, |
| 46 | kError, |
| 47 | kDone, |
| 48 | kCancelled, |
| 49 | kAborted, |
| 50 | kExpired, |
| 51 | }; |
| 52 | |
| 53 | enum class Origin { kLocal, kCloud }; |
| 54 | |
| 55 | // Returns the full command ID. |
| 56 | const std::string& GetID() const; |
| 57 | |
| 58 | // Returns the full name of the command. |
| 59 | const std::string& GetName() const; |
| 60 | |
| 61 | // Returns the command state. |
| 62 | Command::State GetState() const; |
| 63 | |
| 64 | // Returns the origin of the command. |
| 65 | Command::Origin GetOrigin() const; |
| 66 | |
| 67 | // Returns the command parameters. |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 68 | const brillo::VariantDictionary& GetParameters() const; |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 69 | |
| 70 | // Helper function to get a command parameter of particular type T from the |
| 71 | // command parameter list. Returns default value for type T (e.g. 0 for int or |
| 72 | // or "" for std::string) if the parameter with the given name is not found or |
| 73 | // is of incorrect type. |
| 74 | template <typename T> |
| 75 | T GetParameter(const std::string& name) const { |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 76 | const brillo::VariantDictionary& parameters = GetParameters(); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 77 | T value{}; |
| 78 | auto p = parameters.find(name); |
| 79 | if (p != parameters.end()) |
| 80 | p->second.GetValue<T>(&value); |
| 81 | return value; |
| 82 | } |
| 83 | |
| 84 | // Updates the command progress. The |progress| should match the schema. |
| 85 | // Returns false if |progress| value is incorrect. |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 86 | bool SetProgress(const brillo::VariantDictionary& progress, |
| 87 | brillo::ErrorPtr* error); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 88 | |
| 89 | // Sets command into terminal "done" state. |
| 90 | // Updates the command results. The |results| should match the schema. |
| 91 | // Returns false if |results| value is incorrect. |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 92 | bool Complete(const brillo::VariantDictionary& results, |
| 93 | brillo::ErrorPtr* error); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 94 | |
| 95 | // Aborts command execution. |
| 96 | // Sets command into terminal "aborted" state. |
| 97 | bool Abort(const std::string& error_code, |
| 98 | const std::string& error_message, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 99 | brillo::ErrorPtr* error); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 100 | |
| 101 | // Cancels command execution. |
| 102 | // Sets command into terminal "canceled" state. |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame^] | 103 | bool Cancel(brillo::ErrorPtr* error); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 104 | |
| 105 | protected: |
| 106 | Command(com::android::Weave::CommandProxy* proxy); |
| 107 | |
| 108 | private: |
| 109 | friend class Device; |
| 110 | com::android::Weave::CommandProxy* proxy_{nullptr}; |
| 111 | |
| 112 | DISALLOW_COPY_AND_ASSIGN(Command); |
| 113 | }; |
| 114 | |
| 115 | } // namespace weave |
| 116 | |
| 117 | #endif // LIBWEAVED_COMMAND_H_ |