blob: a8332e56dbf9109904a58a78b914e5bae68be377 [file] [log] [blame]
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -07001/*
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 Vakulenko41705852015-10-13 10:12:06 -070023#include <brillo/errors/error.h>
24#include <brillo/variant_dictionary.h>
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070025#include <libweaved/export.h>
26
27namespace com {
28namespace android {
29namespace Weave {
Alex Vakulenkode94eb52015-12-08 17:11:20 -080030class CommandProxyInterface;
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070031} // namespace Weave
32} // namespace android
33} // namespace com
34
35
36namespace weaved {
37
38class Device;
39
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -080040namespace detail {
41
42// Helper function for Command::GetParameter<T>. Allows specialization.
43template <typename T>
44inline 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.
49template <>
50inline 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 Vakulenkoabbcdea2015-10-09 19:33:15 -070064class 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 Vakulenko3bd3ece2015-12-09 12:29:01 -080085 // Returns the name of the component this command was sent to.
86 const std::string& GetComponent() const;
87
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070088 // 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 Vakulenko41705852015-10-13 10:12:06 -070095 const brillo::VariantDictionary& GetParameters() const;
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070096
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 Vakulenko41705852015-10-13 10:12:06 -0700103 const brillo::VariantDictionary& parameters = GetParameters();
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700104 T value{};
105 auto p = parameters.find(name);
106 if (p != parameters.end())
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -0800107 detail::GetValue<T>(p->second, &value);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700108 return value;
109 }
110
111 // Updates the command progress. The |progress| should match the schema.
112 // Returns false if |progress| value is incorrect.
Alex Vakulenko41705852015-10-13 10:12:06 -0700113 bool SetProgress(const brillo::VariantDictionary& progress,
114 brillo::ErrorPtr* error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700115
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 Vakulenko41705852015-10-13 10:12:06 -0700119 bool Complete(const brillo::VariantDictionary& results,
120 brillo::ErrorPtr* error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700121
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 Vakulenko41705852015-10-13 10:12:06 -0700126 brillo::ErrorPtr* error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700127
128 // Cancels command execution.
129 // Sets command into terminal "canceled" state.
Alex Vakulenko41705852015-10-13 10:12:06 -0700130 bool Cancel(brillo::ErrorPtr* error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700131
132 protected:
Alex Vakulenkode94eb52015-12-08 17:11:20 -0800133 Command(com::android::Weave::CommandProxyInterface* proxy);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700134
135 private:
136 friend class Device;
Alex Vakulenkode94eb52015-12-08 17:11:20 -0800137 com::android::Weave::CommandProxyInterface* proxy_{nullptr};
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700138
139 DISALLOW_COPY_AND_ASSIGN(Command);
140};
141
142} // namespace weave
143
144#endif // LIBWEAVED_COMMAND_H_