Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 1 | /* |
| 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 Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 24 | #include <vector> |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 25 | |
| 26 | #include <base/callback.h> |
| 27 | #include <base/macros.h> |
| 28 | #include <base/memory/ref_counted.h> |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 29 | #include <brillo/any.h> |
| 30 | #include <brillo/errors/error.h> |
| 31 | #include <brillo/variant_dictionary.h> |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 32 | #include <libweaved/command.h> |
| 33 | #include <libweaved/export.h> |
| 34 | |
| 35 | namespace com { |
| 36 | namespace android { |
| 37 | namespace Weave { |
Alex Vakulenko | de94eb5 | 2015-12-08 17:11:20 -0800 | [diff] [blame] | 38 | class CommandProxyInterface; |
| 39 | class ManagerProxyInterface; |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 40 | class ObjectManagerProxy; |
| 41 | } // namespace Weave |
| 42 | } // namespace android |
| 43 | } // namespace com |
| 44 | |
| 45 | namespace dbus { |
| 46 | class Bus; |
| 47 | class ObjectPath; |
| 48 | } // namespace dbus |
| 49 | |
| 50 | namespace weaved { |
| 51 | |
| 52 | class 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 Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 64 | void AddComponent(const std::string& component, |
| 65 | const std::vector<std::string>& traits); |
| 66 | |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 67 | // 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 Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 70 | void AddCommandHandler(const std::string& component, |
| 71 | const std::string& command_name, |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 72 | const CommandHandlerCallback& callback); |
| 73 | |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 74 | bool SetStateProperties(const std::string& component, |
| 75 | const brillo::VariantDictionary& dict, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 76 | brillo::ErrorPtr* error); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 77 | |
| 78 | // Sets value of the single property. |
| 79 | // |name| is full property name, including package name. e.g. "base.network". |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 80 | bool SetStateProperty(const std::string& component, |
| 81 | const std::string& name, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 82 | const brillo::Any& value, |
| 83 | brillo::ErrorPtr* error); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 84 | |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 85 | // 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 Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 102 | private: |
| 103 | Device(const scoped_refptr<dbus::Bus>& bus, |
| 104 | const base::Closure& state_required_callback); |
| 105 | |
Alex Vakulenko | de94eb5 | 2015-12-08 17:11:20 -0800 | [diff] [blame] | 106 | void OnCommandAdded(com::android::Weave::CommandProxyInterface* proxy); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 107 | void OnCommandRemoved(const dbus::ObjectPath& object_path); |
| 108 | |
Alex Vakulenko | de94eb5 | 2015-12-08 17:11:20 -0800 | [diff] [blame] | 109 | void OnManagerAdded(com::android::Weave::ManagerProxyInterface* proxy); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 110 | void OnManagerRemoved(const dbus::ObjectPath& object_path); |
| 111 | |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 112 | const CommandHandlerCallback* FindHandlerForCommand( |
| 113 | com::android::Weave::CommandProxyInterface* proxy) const; |
| 114 | |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 115 | std::unique_ptr<com::android::Weave::ObjectManagerProxy> weaved_object_mgr_; |
Alex Vakulenko | de94eb5 | 2015-12-08 17:11:20 -0800 | [diff] [blame] | 116 | com::android::Weave::ManagerProxyInterface* proxy_{nullptr}; |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 117 | |
Alex Vakulenko | de94eb5 | 2015-12-08 17:11:20 -0800 | [diff] [blame] | 118 | using CommandMap = std::map<com::android::Weave::CommandProxyInterface*, |
| 119 | std::shared_ptr<Command>>; |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 120 | CommandMap command_map_; |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 121 | |
| 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 Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 135 | 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_ |