blob: 02bacb6297fae3c0d38a8173384d19fd470258f7 [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#include "device.h"
18
19#include "buffet/dbus-proxies.h"
20
21using com::android::Weave::CommandProxy;
22using com::android::Weave::ManagerProxy;
23
24namespace weaved {
25
26Device::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 Vakulenko55198ea2015-11-20 15:51:20 -080036 weaved_object_mgr_->SetManagerRemovedCallback(
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070037 base::Bind(&Device::OnManagerRemoved, base::Unretained(this)));
38}
39
40Device::~Device() {
41}
42
43std::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
49void 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 Vakulenko41705852015-10-13 10:12:06 -070063bool Device::SetStateProperties(const brillo::VariantDictionary& dict,
64 brillo::ErrorPtr* error) {
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070065 if (proxy_)
66 return proxy_->UpdateState(dict, error);
67
Alex Vakulenko41705852015-10-13 10:12:06 -070068 brillo::Error::AddTo(error, FROM_HERE, "weaved", "service_unavailable",
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070069 "Process 'weaved' is unreachable");
70 return false;
71}
72
73bool Device::SetStateProperty(const std::string& name,
Alex Vakulenko41705852015-10-13 10:12:06 -070074 const brillo::Any& value,
75 brillo::ErrorPtr* error) {
76 return SetStateProperties(brillo::VariantDictionary{{name, value}}, error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070077}
78
79void 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
89void 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
96void Device::OnManagerAdded(ManagerProxy* proxy) {
97 proxy_ = proxy;
98 state_required_callback_.Run();
99}
100
101void Device::OnManagerRemoved(const dbus::ObjectPath& object_path) {
102 proxy_ = nullptr;
103}
104
105} // namespace weave