blob: 5f396a7a64453413dd53f8456cf6de18873134b7 [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
Alex Vakulenkode94eb52015-12-08 17:11:20 -080021using com::android::Weave::CommandProxyInterface;
22using com::android::Weave::ManagerProxyInterface;
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070023
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
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -080049void 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
59void Device::AddCommandHandler(const std::string& component,
60 const std::string& command_name,
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070061 const CommandHandlerCallback& callback) {
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -080062 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 Vakulenkoabbcdea2015-10-09 19:33:15 -070077
78 // If there are any commands already received, call the handler immediately.
79 for (auto& pair : command_map_) {
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -080080 if (pair.first->name() == command_name &&
81 (component.empty() || pair.first->component() == component)) {
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070082 if (!pair.second)
83 pair.second.reset(new Command{pair.first});
84 callback.Run(pair.second);
85 }
86 }
87}
88
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -080089bool Device::SetStateProperties(const std::string& component,
90 const brillo::VariantDictionary& dict,
Alex Vakulenko41705852015-10-13 10:12:06 -070091 brillo::ErrorPtr* error) {
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070092 if (proxy_)
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -080093 return proxy_->UpdateState(component, dict, error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070094
Alex Vakulenko41705852015-10-13 10:12:06 -070095 brillo::Error::AddTo(error, FROM_HERE, "weaved", "service_unavailable",
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -080096 "Process 'weaved' is unreachable");
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -070097 return false;
98}
99
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -0800100bool 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
108void Device::AddCommandHandler(const std::string& command_name,
109 const CommandHandlerCallback& callback) {
110 AddCommandHandler("", command_name, callback);
111}
112
113bool Device::SetStateProperties(const brillo::VariantDictionary& dict,
114 brillo::ErrorPtr* error) {
115 return SetStateProperties("", dict, error);
116}
117
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700118bool Device::SetStateProperty(const std::string& name,
Alex Vakulenko41705852015-10-13 10:12:06 -0700119 const brillo::Any& value,
120 brillo::ErrorPtr* error) {
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -0800121 return SetStateProperty("", name, value, error);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700122}
123
Alex Vakulenkode94eb52015-12-08 17:11:20 -0800124void Device::OnCommandAdded(CommandProxyInterface* proxy) {
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700125 std::shared_ptr<Command>& command = command_map_[proxy];
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -0800126 const Device::CommandHandlerCallback* callback = FindHandlerForCommand(proxy);
127 if (!callback)
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700128 return;
129 if (!command)
130 command.reset(new Command{proxy});
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -0800131 callback->Run(command);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700132}
133
134void 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 Vakulenkode94eb52015-12-08 17:11:20 -0800141void Device::OnManagerAdded(ManagerProxyInterface* proxy) {
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700142 proxy_ = proxy;
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -0800143 for (const auto& entry : components_)
144 proxy_->AddComponent(entry.component, entry.traits, nullptr);
Alex Vakulenkoabbcdea2015-10-09 19:33:15 -0700145 state_required_callback_.Run();
146}
147
148void Device::OnManagerRemoved(const dbus::ObjectPath& object_path) {
149 proxy_ = nullptr;
150}
151
Alex Vakulenko3bd3ece2015-12-09 12:29:01 -0800152const 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 Vakulenkoabbcdea2015-10-09 19:33:15 -0700164} // namespace weave