blob: 34753fe2b0728fad1344bdfdea82d5519c88b193 [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 {
30class CommandProxy;
31} // namespace Weave
32} // namespace android
33} // namespace com
34
35
36namespace weaved {
37
38class Device;
39
40class 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 Vakulenko41705852015-10-13 10:12:06 -070068 const brillo::VariantDictionary& GetParameters() const;
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070069
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 Vakulenko41705852015-10-13 10:12:06 -070076 const brillo::VariantDictionary& parameters = GetParameters();
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070077 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 Vakulenko41705852015-10-13 10:12:06 -070086 bool SetProgress(const brillo::VariantDictionary& progress,
87 brillo::ErrorPtr* error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070088
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 Vakulenko41705852015-10-13 10:12:06 -070092 bool Complete(const brillo::VariantDictionary& results,
93 brillo::ErrorPtr* error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070094
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 Vakulenko41705852015-10-13 10:12:06 -070099 brillo::ErrorPtr* error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700100
101 // Cancels command execution.
102 // Sets command into terminal "canceled" state.
Alex Vakulenko41705852015-10-13 10:12:06 -0700103 bool Cancel(brillo::ErrorPtr* error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700104
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_