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