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 | #include "device.h" |
| 18 | |
| 19 | #include "buffet/dbus-proxies.h" |
| 20 | |
Alex Vakulenko | de94eb5 | 2015-12-08 17:11:20 -0800 | [diff] [blame] | 21 | using com::android::Weave::CommandProxyInterface; |
| 22 | using com::android::Weave::ManagerProxyInterface; |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 23 | |
| 24 | namespace weaved { |
| 25 | |
| 26 | Device::Device(const scoped_refptr<dbus::Bus>& bus, |
| 27 | const base::Closure& state_required_callback) |
| 28 | : bus_{bus}, state_required_callback_{state_required_callback} { |
| 29 | weaved_object_mgr_.reset(new com::android::Weave::ObjectManagerProxy{bus_}); |
| 30 | weaved_object_mgr_->SetCommandAddedCallback( |
| 31 | base::Bind(&Device::OnCommandAdded, base::Unretained(this))); |
| 32 | weaved_object_mgr_->SetCommandRemovedCallback( |
| 33 | base::Bind(&Device::OnCommandRemoved, base::Unretained(this))); |
| 34 | weaved_object_mgr_->SetManagerAddedCallback( |
| 35 | base::Bind(&Device::OnManagerAdded, base::Unretained(this))); |
Alex Vakulenko | 55198ea | 2015-11-20 15:51:20 -0800 | [diff] [blame] | 36 | weaved_object_mgr_->SetManagerRemovedCallback( |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 37 | base::Bind(&Device::OnManagerRemoved, base::Unretained(this))); |
| 38 | } |
| 39 | |
| 40 | Device::~Device() { |
| 41 | } |
| 42 | |
| 43 | std::unique_ptr<Device> Device::CreateInstance( |
| 44 | const scoped_refptr<dbus::Bus>& bus, |
| 45 | const base::Closure& state_required_callback) { |
| 46 | return std::unique_ptr<Device>{new Device{bus, state_required_callback}}; |
| 47 | } |
| 48 | |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 49 | void Device::AddComponent(const std::string& component, |
| 50 | const std::vector<std::string>& traits) { |
| 51 | ComponentEntry entry; |
| 52 | entry.component = component; |
| 53 | entry.traits = traits; |
| 54 | components_.push_back(std::move(entry)); |
| 55 | if (proxy_) |
| 56 | proxy_->AddComponent(component, traits, nullptr); |
| 57 | } |
| 58 | |
| 59 | void Device::AddCommandHandler(const std::string& component, |
| 60 | const std::string& command_name, |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 61 | const CommandHandlerCallback& callback) { |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 62 | for (const auto& entry : command_handlers_) { |
| 63 | if (entry.command_name != command_name) |
| 64 | continue; |
| 65 | // The command names are the same, make sure we have different components. |
| 66 | // This means that both component names are not empty and are different. |
| 67 | CHECK(!component.empty() && !entry.component.empty() && |
| 68 | component != entry.component) |
| 69 | << "Handler for " << component << ":" << command_name << " already set"; |
| 70 | } |
| 71 | CommandHandlerEntry entry; |
| 72 | entry.component = component; |
| 73 | entry.command_name = command_name; |
| 74 | entry.callback = callback; |
| 75 | |
| 76 | command_handlers_.push_back(std::move(entry)); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 77 | |
| 78 | // If there are any commands already received, call the handler immediately. |
| 79 | for (auto& pair : command_map_) { |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 80 | if (pair.first->name() == command_name && |
| 81 | (component.empty() || pair.first->component() == component)) { |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 82 | if (!pair.second) |
| 83 | pair.second.reset(new Command{pair.first}); |
| 84 | callback.Run(pair.second); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 89 | bool Device::SetStateProperties(const std::string& component, |
| 90 | const brillo::VariantDictionary& dict, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 91 | brillo::ErrorPtr* error) { |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 92 | if (proxy_) |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 93 | return proxy_->UpdateState(component, dict, error); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 94 | |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 95 | brillo::Error::AddTo(error, FROM_HERE, "weaved", "service_unavailable", |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 96 | "Process 'weaved' is unreachable"); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 97 | return false; |
| 98 | } |
| 99 | |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 100 | bool Device::SetStateProperty(const std::string& component, |
| 101 | const std::string& name, |
| 102 | const brillo::Any& value, |
| 103 | brillo::ErrorPtr* error) { |
| 104 | return SetStateProperties(component, brillo::VariantDictionary{{name, value}}, |
| 105 | error); |
| 106 | } |
| 107 | |
| 108 | void Device::AddCommandHandler(const std::string& command_name, |
| 109 | const CommandHandlerCallback& callback) { |
| 110 | AddCommandHandler("", command_name, callback); |
| 111 | } |
| 112 | |
| 113 | bool Device::SetStateProperties(const brillo::VariantDictionary& dict, |
| 114 | brillo::ErrorPtr* error) { |
| 115 | return SetStateProperties("", dict, error); |
| 116 | } |
| 117 | |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 118 | bool Device::SetStateProperty(const std::string& name, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 119 | const brillo::Any& value, |
| 120 | brillo::ErrorPtr* error) { |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 121 | return SetStateProperty("", name, value, error); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Alex Vakulenko | de94eb5 | 2015-12-08 17:11:20 -0800 | [diff] [blame] | 124 | void Device::OnCommandAdded(CommandProxyInterface* proxy) { |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 125 | std::shared_ptr<Command>& command = command_map_[proxy]; |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 126 | const Device::CommandHandlerCallback* callback = FindHandlerForCommand(proxy); |
| 127 | if (!callback) |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 128 | return; |
| 129 | if (!command) |
| 130 | command.reset(new Command{proxy}); |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 131 | callback->Run(command); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void Device::OnCommandRemoved(const dbus::ObjectPath& object_path) { |
| 135 | auto proxy = weaved_object_mgr_->GetCommandProxy(object_path); |
| 136 | if (!proxy) |
| 137 | return; |
| 138 | command_map_.erase(proxy); |
| 139 | } |
| 140 | |
Alex Vakulenko | de94eb5 | 2015-12-08 17:11:20 -0800 | [diff] [blame] | 141 | void Device::OnManagerAdded(ManagerProxyInterface* proxy) { |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 142 | proxy_ = proxy; |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 143 | for (const auto& entry : components_) |
| 144 | proxy_->AddComponent(entry.component, entry.traits, nullptr); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 145 | state_required_callback_.Run(); |
| 146 | } |
| 147 | |
| 148 | void Device::OnManagerRemoved(const dbus::ObjectPath& object_path) { |
| 149 | proxy_ = nullptr; |
| 150 | } |
| 151 | |
Alex Vakulenko | 3bd3ece | 2015-12-09 12:29:01 -0800 | [diff] [blame] | 152 | const Device::CommandHandlerCallback* Device::FindHandlerForCommand( |
| 153 | com::android::Weave::CommandProxyInterface* proxy) const { |
| 154 | for (const auto& entry : command_handlers_) { |
| 155 | if (proxy->name() == entry.command_name && |
| 156 | (entry.component.empty() || proxy->component() == entry.component)) { |
| 157 | return &entry.callback; |
| 158 | } |
| 159 | } |
| 160 | return nullptr; |
| 161 | } |
| 162 | |
| 163 | |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 164 | } // namespace weave |