blob: 2b69266c4fdefa4d053e90cbaa3a0d6f93f3dff9 [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
15#ifndef LIBWEAVED_COMMAND_H_
16#define LIBWEAVED_COMMAND_H_
17
18#include <string>
19
20#include <base/macros.h>
Alex Vakulenko41705852015-10-13 10:12:06 -070021#include <brillo/errors/error.h>
22#include <brillo/variant_dictionary.h>
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070023#include <libweaved/export.h>
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080024#include <utils/StrongPointer.h>
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070025
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070026namespace android {
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080027namespace weave {
28class IWeaveCommand;
29} // namespace weave
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070030} // namespace android
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070031
32namespace weaved {
33
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080034class ServiceImpl;
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070035
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -080036namespace detail {
37
38// Helper function for Command::GetParameter<T>. Allows specialization.
39template <typename T>
40inline 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.
45template <>
46inline 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 Vakulenkoabbcdea2015-10-09 19:33:15 -070060class 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 Vakulenkoae29f7d2015-12-21 16:30:37 -080075 ~Command();
76
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070077 // Returns the full command ID.
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080078 std::string GetID() const;
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070079
80 // Returns the full name of the command.
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080081 std::string GetName() const;
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070082
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -080083 // Returns the name of the component this command was sent to.
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -080084 std::string GetComponent() const;
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -080085
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070086 // 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 Vakulenkoae29f7d2015-12-21 16:30:37 -080093 brillo::VariantDictionary GetParameters() const;
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070094
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 Vakulenko41705852015-10-13 10:12:06 -0700101 const brillo::VariantDictionary& parameters = GetParameters();
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700102 T value{};
103 auto p = parameters.find(name);
104 if (p != parameters.end())
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -0800105 detail::GetValue<T>(p->second, &value);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700106 return value;
107 }
108
109 // Updates the command progress. The |progress| should match the schema.
110 // Returns false if |progress| value is incorrect.
Alex Vakulenko41705852015-10-13 10:12:06 -0700111 bool SetProgress(const brillo::VariantDictionary& progress,
112 brillo::ErrorPtr* error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700113
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 Vakulenko41705852015-10-13 10:12:06 -0700117 bool Complete(const brillo::VariantDictionary& results,
118 brillo::ErrorPtr* error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700119
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 Vakulenko41705852015-10-13 10:12:06 -0700124 brillo::ErrorPtr* error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700125
126 // Cancels command execution.
127 // Sets command into terminal "canceled" state.
Alex Vakulenko41705852015-10-13 10:12:06 -0700128 bool Cancel(brillo::ErrorPtr* error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700129
130 protected:
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -0800131 explicit Command(const android::sp<android::weave::IWeaveCommand>& proxy);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700132
133 private:
Alex Vakulenkoae29f7d2015-12-21 16:30:37 -0800134 friend class ServiceImpl;
135 android::sp<android::weave::IWeaveCommand> binder_proxy_;
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700136
137 DISALLOW_COPY_AND_ASSIGN(Command);
138};
139
140} // namespace weave
141
142#endif // LIBWEAVED_COMMAND_H_