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 { |
Alex Vakulenko | de94eb5 | 2015-12-08 17:11:20 -0800 | [diff] [blame] | 30 | class CommandProxyInterface; |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 31 | } // namespace Weave |
| 32 | } // namespace android |
| 33 | } // namespace com |
| 34 | |
| 35 | |
| 36 | namespace weaved { |
| 37 | |
| 38 | class Device; |
| 39 | |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame^] | 40 | namespace detail { |
| 41 | |
| 42 | // Helper function for Command::GetParameter<T>. Allows specialization. |
| 43 | template <typename T> |
| 44 | inline bool GetValue(const brillo::Any& any, T* value) { |
| 45 | return any.GetValue<T>(value); |
| 46 | } |
| 47 | |
| 48 | // Specialization for double, allow to extract a double from an int. |
| 49 | template <> |
| 50 | inline bool GetValue<double>(const brillo::Any& any, double* value) { |
| 51 | if (any.GetValue<double>(value)) |
| 52 | return true; |
| 53 | |
| 54 | int int_val = 0; |
| 55 | if (!any.GetValue<int>(&int_val)) |
| 56 | return false; |
| 57 | |
| 58 | *value = static_cast<double>(int_val); |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | } // namespace detail |
| 63 | |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 64 | class LIBWEAVED_EXPORT Command final { |
| 65 | public: |
| 66 | enum class State { |
| 67 | kQueued, |
| 68 | kInProgress, |
| 69 | kPaused, |
| 70 | kError, |
| 71 | kDone, |
| 72 | kCancelled, |
| 73 | kAborted, |
| 74 | kExpired, |
| 75 | }; |
| 76 | |
| 77 | enum class Origin { kLocal, kCloud }; |
| 78 | |
| 79 | // Returns the full command ID. |
| 80 | const std::string& GetID() const; |
| 81 | |
| 82 | // Returns the full name of the command. |
| 83 | const std::string& GetName() const; |
| 84 | |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame^] | 85 | // Returns the name of the component this command was sent to. |
| 86 | const std::string& GetComponent() const; |
| 87 | |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 88 | // Returns the command state. |
| 89 | Command::State GetState() const; |
| 90 | |
| 91 | // Returns the origin of the command. |
| 92 | Command::Origin GetOrigin() const; |
| 93 | |
| 94 | // Returns the command parameters. |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 95 | const brillo::VariantDictionary& GetParameters() const; |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 96 | |
| 97 | // Helper function to get a command parameter of particular type T from the |
| 98 | // command parameter list. Returns default value for type T (e.g. 0 for int or |
| 99 | // or "" for std::string) if the parameter with the given name is not found or |
| 100 | // is of incorrect type. |
| 101 | template <typename T> |
| 102 | T GetParameter(const std::string& name) const { |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 103 | const brillo::VariantDictionary& parameters = GetParameters(); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 104 | T value{}; |
| 105 | auto p = parameters.find(name); |
| 106 | if (p != parameters.end()) |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame^] | 107 | detail::GetValue<T>(p->second, &value); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 108 | return value; |
| 109 | } |
| 110 | |
| 111 | // Updates the command progress. The |progress| should match the schema. |
| 112 | // Returns false if |progress| value is incorrect. |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 113 | bool SetProgress(const brillo::VariantDictionary& progress, |
| 114 | brillo::ErrorPtr* error); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 115 | |
| 116 | // Sets command into terminal "done" state. |
| 117 | // Updates the command results. The |results| should match the schema. |
| 118 | // Returns false if |results| value is incorrect. |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 119 | bool Complete(const brillo::VariantDictionary& results, |
| 120 | brillo::ErrorPtr* error); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 121 | |
| 122 | // Aborts command execution. |
| 123 | // Sets command into terminal "aborted" state. |
| 124 | bool Abort(const std::string& error_code, |
| 125 | const std::string& error_message, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 126 | brillo::ErrorPtr* error); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 127 | |
| 128 | // Cancels command execution. |
| 129 | // Sets command into terminal "canceled" state. |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 130 | bool Cancel(brillo::ErrorPtr* error); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 131 | |
| 132 | protected: |
Alex Vakulenko | de94eb5 | 2015-12-08 17:11:20 -0800 | [diff] [blame] | 133 | Command(com::android::Weave::CommandProxyInterface* proxy); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 134 | |
| 135 | private: |
| 136 | friend class Device; |
Alex Vakulenko | de94eb5 | 2015-12-08 17:11:20 -0800 | [diff] [blame] | 137 | com::android::Weave::CommandProxyInterface* proxy_{nullptr}; |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 138 | |
| 139 | DISALLOW_COPY_AND_ASSIGN(Command); |
| 140 | }; |
| 141 | |
| 142 | } // namespace weave |
| 143 | |
| 144 | #endif // LIBWEAVED_COMMAND_H_ |