blob: 376b656400eb1bef977df2ecdbcf84629b3f3c0f [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
18#ifndef LIBWEAVED_DEVICE_H_
19#define LIBWEAVED_DEVICE_H_
20
21#include <map>
22#include <memory>
23#include <string>
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -080024#include <vector>
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070025
26#include <base/callback.h>
27#include <base/macros.h>
28#include <base/memory/ref_counted.h>
Alex Vakulenko41705852015-10-13 10:12:06 -070029#include <brillo/any.h>
30#include <brillo/errors/error.h>
31#include <brillo/variant_dictionary.h>
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070032#include <libweaved/command.h>
33#include <libweaved/export.h>
34
35namespace com {
36namespace android {
37namespace Weave {
Alex Vakulenkode94eb52015-12-08 17:11:20 -080038class CommandProxyInterface;
39class ManagerProxyInterface;
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070040class ObjectManagerProxy;
41} // namespace Weave
42} // namespace android
43} // namespace com
44
45namespace dbus {
46class Bus;
47class ObjectPath;
48} // namespace dbus
49
50namespace weaved {
51
52class LIBWEAVED_EXPORT Device final {
53 public:
54 ~Device();
55
56 static std::unique_ptr<Device> CreateInstance(
57 const scoped_refptr<dbus::Bus>& bus,
58 const base::Closure& state_required_callback);
59
60 // Callback type for AddCommandHandler.
61 using CommandHandlerCallback =
62 base::Callback<void(const std::weak_ptr<Command>& command)>;
63
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -080064 void AddComponent(const std::string& component,
65 const std::vector<std::string>& traits);
66
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070067 // Sets handler for new commands added to the queue.
68 // |command_name| is the full command name of the command to handle. e.g.
69 // "base.reboot". Each command can have no more than one handler.
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -080070 void AddCommandHandler(const std::string& component,
71 const std::string& command_name,
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070072 const CommandHandlerCallback& callback);
73
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -080074 bool SetStateProperties(const std::string& component,
75 const brillo::VariantDictionary& dict,
Alex Vakulenko41705852015-10-13 10:12:06 -070076 brillo::ErrorPtr* error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070077
78 // Sets value of the single property.
79 // |name| is full property name, including package name. e.g. "base.network".
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -080080 bool SetStateProperty(const std::string& component,
81 const std::string& name,
Alex Vakulenko41705852015-10-13 10:12:06 -070082 const brillo::Any& value,
83 brillo::ErrorPtr* error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070084
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -080085 // Sets handler for new commands added to the queue.
86 // |command_name| is the full command name of the command to handle. e.g.
87 // "base.reboot". Each command can have no more than one handler.
88 LIBWEAVED_DEPRECATED void AddCommandHandler(
89 const std::string& command_name,
90 const CommandHandlerCallback& callback);
91
92 LIBWEAVED_DEPRECATED bool SetStateProperties(
93 const brillo::VariantDictionary& dict,
94 brillo::ErrorPtr* error);
95
96 // Sets value of the single property.
97 // |name| is full property name, including package name. e.g. "base.network".
98 LIBWEAVED_DEPRECATED bool SetStateProperty(const std::string& name,
99 const brillo::Any& value,
100 brillo::ErrorPtr* error);
101
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700102 private:
103 Device(const scoped_refptr<dbus::Bus>& bus,
104 const base::Closure& state_required_callback);
105
Alex Vakulenkode94eb52015-12-08 17:11:20 -0800106 void OnCommandAdded(com::android::Weave::CommandProxyInterface* proxy);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700107 void OnCommandRemoved(const dbus::ObjectPath& object_path);
108
Alex Vakulenkode94eb52015-12-08 17:11:20 -0800109 void OnManagerAdded(com::android::Weave::ManagerProxyInterface* proxy);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700110 void OnManagerRemoved(const dbus::ObjectPath& object_path);
111
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -0800112 const CommandHandlerCallback* FindHandlerForCommand(
113 com::android::Weave::CommandProxyInterface* proxy) const;
114
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700115 std::unique_ptr<com::android::Weave::ObjectManagerProxy> weaved_object_mgr_;
Alex Vakulenkode94eb52015-12-08 17:11:20 -0800116 com::android::Weave::ManagerProxyInterface* proxy_{nullptr};
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700117
Alex Vakulenkode94eb52015-12-08 17:11:20 -0800118 using CommandMap = std::map<com::android::Weave::CommandProxyInterface*,
119 std::shared_ptr<Command>>;
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700120 CommandMap command_map_;
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -0800121
122 struct CommandHandlerEntry {
123 std::string component;
124 std::string command_name;
125 CommandHandlerCallback callback;
126 };
127 std::vector<CommandHandlerEntry> command_handlers_;
128
129 struct ComponentEntry {
130 std::string component;
131 std::vector<std::string> traits;
132 };
133 std::vector<ComponentEntry> components_;
134
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700135 scoped_refptr<dbus::Bus> bus_;
136 base::Closure state_required_callback_;
137
138
139 DISALLOW_COPY_AND_ASSIGN(Device);
140};
141
142} // namespace weave
143
144#endif // LIBWEAVE_INCLUDE_WEAVE_DEVICE_H_