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 | |
| 21 | using com::android::Weave::CommandProxy; |
| 22 | using com::android::Weave::ManagerProxy; |
| 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 | |
| 49 | void Device::AddCommandHandler(const std::string& command_name, |
| 50 | const CommandHandlerCallback& callback) { |
| 51 | command_handler_map_.emplace(command_name, callback); |
| 52 | |
| 53 | // If there are any commands already received, call the handler immediately. |
| 54 | for (auto& pair : command_map_) { |
| 55 | if (pair.first->name() == command_name) { |
| 56 | if (!pair.second) |
| 57 | pair.second.reset(new Command{pair.first}); |
| 58 | callback.Run(pair.second); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 63 | bool Device::SetStateProperties(const brillo::VariantDictionary& dict, |
| 64 | brillo::ErrorPtr* error) { |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 65 | if (proxy_) |
| 66 | return proxy_->UpdateState(dict, error); |
| 67 | |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 68 | brillo::Error::AddTo(error, FROM_HERE, "weaved", "service_unavailable", |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 69 | "Process 'weaved' is unreachable"); |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | bool Device::SetStateProperty(const std::string& name, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 74 | const brillo::Any& value, |
| 75 | brillo::ErrorPtr* error) { |
| 76 | return SetStateProperties(brillo::VariantDictionary{{name, value}}, error); |
Alex Vakulenko | abbcdea | 2015-10-09 19:33:15 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void Device::OnCommandAdded(CommandProxy* proxy) { |
| 80 | std::shared_ptr<Command>& command = command_map_[proxy]; |
| 81 | auto iter = command_handler_map_.find(proxy->name()); |
| 82 | if (iter == command_handler_map_.end()) |
| 83 | return; |
| 84 | if (!command) |
| 85 | command.reset(new Command{proxy}); |
| 86 | iter->second.Run(command); |
| 87 | } |
| 88 | |
| 89 | void Device::OnCommandRemoved(const dbus::ObjectPath& object_path) { |
| 90 | auto proxy = weaved_object_mgr_->GetCommandProxy(object_path); |
| 91 | if (!proxy) |
| 92 | return; |
| 93 | command_map_.erase(proxy); |
| 94 | } |
| 95 | |
| 96 | void Device::OnManagerAdded(ManagerProxy* proxy) { |
| 97 | proxy_ = proxy; |
| 98 | state_required_callback_.Run(); |
| 99 | } |
| 100 | |
| 101 | void Device::OnManagerRemoved(const dbus::ObjectPath& object_path) { |
| 102 | proxy_ = nullptr; |
| 103 | } |
| 104 | |
| 105 | } // namespace weave |