Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 IBM Corporation |
| 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 | #include <iostream> |
| 17 | #include <exception> |
Brad Bishop | 2442498 | 2017-01-13 16:37:14 -0500 | [diff] [blame] | 18 | #include <chrono> |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 19 | #include "manager.hpp" |
| 20 | |
Brad Bishop | 2442498 | 2017-01-13 16:37:14 -0500 | [diff] [blame] | 21 | using namespace std::literals::chrono_literals; |
| 22 | |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 23 | namespace phosphor |
| 24 | { |
| 25 | namespace inventory |
| 26 | { |
| 27 | namespace manager |
| 28 | { |
| 29 | namespace details |
| 30 | { |
| 31 | |
| 32 | /** @brief Fowrarding signal callback. |
| 33 | * |
| 34 | * Extracts per-signal specific context and forwards the call to the manager |
| 35 | * instance. |
| 36 | */ |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 37 | auto _signal(sd_bus_message* m, void* data, sd_bus_error* e) noexcept |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 38 | { |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 39 | try |
| 40 | { |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 41 | auto msg = sdbusplus::message::message(m); |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 42 | auto& args = *static_cast<Manager::SigArg*>(data); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 43 | sd_bus_message_ref(m); |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 44 | auto& mgr = *std::get<0>(args); |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 45 | mgr.signal( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 46 | msg, |
| 47 | static_cast<const details::DbusSignal&>( |
| 48 | *std::get<1>(args)), |
| 49 | *std::get<2>(args)); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 50 | } |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 51 | catch (const std::exception& e) |
| 52 | { |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 53 | std::cerr << e.what() << std::endl; |
| 54 | } |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | } // namespace details |
| 60 | |
| 61 | Manager::Manager( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 62 | sdbusplus::bus::bus&& bus, |
| 63 | const char* busname, |
| 64 | const char* root, |
| 65 | const char* iface) : |
Brad Bishop | 451f8d9 | 2016-11-21 14:15:19 -0500 | [diff] [blame] | 66 | details::ServerObject<details::ManagerIface>(bus, root), |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 67 | _shutdown(false), |
| 68 | _root(root), |
| 69 | _bus(std::move(bus)), |
| 70 | _manager(sdbusplus::server::manager::manager(_bus, root)) |
| 71 | { |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 72 | for (auto& group : _events) |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 73 | { |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 74 | for (auto pEvent : std::get<0>(group)) |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 75 | { |
| 76 | if (pEvent->type != |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 77 | details::Event::Type::DBUS_SIGNAL) |
| 78 | { |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 79 | continue; |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 80 | } |
Brad Bishop | 4f20a3e | 2016-11-29 15:21:46 -0500 | [diff] [blame] | 81 | |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 82 | // Create a callback context for this event group. |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 83 | auto dbusEvent = static_cast<details::DbusSignal*>( |
| 84 | pEvent.get()); |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 85 | |
| 86 | // Go ahead and store an iterator pointing at |
| 87 | // the event data to avoid lookups later since |
| 88 | // additional signal callbacks aren't added |
| 89 | // after the manager is constructed. |
| 90 | _sigargs.emplace_back( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 91 | std::make_unique<SigArg>( |
| 92 | this, |
| 93 | dbusEvent, |
| 94 | &group)); |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 95 | |
| 96 | // Register our callback and the context for |
| 97 | // each signal event. |
| 98 | _matches.emplace_back( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 99 | _bus, |
| 100 | std::get<0>(*dbusEvent), |
| 101 | details::_signal, |
| 102 | _sigargs.back().get()); |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 103 | } |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | _bus.request_name(busname); |
| 107 | } |
| 108 | |
| 109 | void Manager::shutdown() noexcept |
| 110 | { |
| 111 | _shutdown = true; |
| 112 | } |
| 113 | |
| 114 | void Manager::run() noexcept |
| 115 | { |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 116 | while (!_shutdown) |
| 117 | { |
| 118 | try |
| 119 | { |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 120 | _bus.process_discard(); |
Brad Bishop | 2442498 | 2017-01-13 16:37:14 -0500 | [diff] [blame] | 121 | _bus.wait((5000000us).count()); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 122 | } |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 123 | catch (const std::exception& e) |
| 124 | { |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 125 | std::cerr << e.what() << std::endl; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
Brad Bishop | 9aa5e2f | 2017-01-15 19:45:40 -0500 | [diff] [blame] | 130 | void Manager::notify(sdbusplus::message::object_path path, Object object) |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 131 | { |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 132 | try |
| 133 | { |
Brad Bishop | 9aa5e2f | 2017-01-15 19:45:40 -0500 | [diff] [blame] | 134 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 135 | if (object.cbegin() == object.cend()) |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 136 | throw std::runtime_error( |
Brad Bishop | 9aa5e2f | 2017-01-15 19:45:40 -0500 | [diff] [blame] | 137 | "No interfaces in " + path.str); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 138 | |
Brad Bishop | 9aa5e2f | 2017-01-15 19:45:40 -0500 | [diff] [blame] | 139 | path.str.insert(0, _root); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 140 | |
| 141 | auto obj = _refs.find(path); |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 142 | if (obj != _refs.end()) |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 143 | throw std::runtime_error( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 144 | obj->first + " already exists"); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 145 | |
| 146 | // Create an interface holder for each interface |
| 147 | // provided by the client and group them into |
| 148 | // a container. |
| 149 | InterfaceComposite ref; |
| 150 | |
Brad Bishop | 6676c12 | 2017-01-15 20:38:39 -0500 | [diff] [blame^] | 151 | auto i = object.size(); |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 152 | for (auto& x : object) |
| 153 | { |
Brad Bishop | 6676c12 | 2017-01-15 20:38:39 -0500 | [diff] [blame^] | 154 | // Defer sending any signals until the last interface. |
| 155 | auto deferSignals = --i != 0; |
Brad Bishop | 5fbaa7f | 2016-10-31 10:42:41 -0500 | [diff] [blame] | 156 | auto maker = _makers.find(x.first.c_str()); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 157 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 158 | if (maker == _makers.end()) |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 159 | throw std::runtime_error( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 160 | "Unimplemented interface: " + x.first); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 161 | |
Brad Bishop | 1ab880a | 2016-12-05 15:49:31 -0500 | [diff] [blame] | 162 | ref.emplace(x.first, |
Brad Bishop | 6676c12 | 2017-01-15 20:38:39 -0500 | [diff] [blame^] | 163 | (maker->second)(_bus, path.str.c_str(), deferSignals)); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Brad Bishop | 9d10fb2 | 2017-01-15 20:15:20 -0500 | [diff] [blame] | 166 | if (!ref.empty()) |
| 167 | { |
| 168 | // Hang on to a reference to the object (interfaces) |
| 169 | // so it stays on the bus, and so we can make calls |
| 170 | // to it if needed. |
| 171 | _refs.emplace( |
| 172 | path, std::move(ref)); |
| 173 | } |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 174 | } |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 175 | catch (const std::exception& e) |
| 176 | { |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 177 | std::cerr << e.what() << std::endl; |
| 178 | } |
| 179 | } |
| 180 | |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 181 | void Manager::signal( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 182 | sdbusplus::message::message& msg, |
| 183 | const details::DbusSignal& event, |
| 184 | const EventInfo& info) |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 185 | { |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 186 | auto& filter = *std::get<1>(event); |
| 187 | auto& actions = std::get<1>(info); |
Brad Bishop | 3d57f50 | 2016-10-19 12:18:41 -0400 | [diff] [blame] | 188 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 189 | if (filter(msg, *this)) |
| 190 | { |
| 191 | for (auto& action : actions) |
| 192 | { |
Brad Bishop | 9007432 | 2016-11-29 13:05:57 -0500 | [diff] [blame] | 193 | (*action)(*this); |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 194 | } |
Brad Bishop | 3d57f50 | 2016-10-19 12:18:41 -0400 | [diff] [blame] | 195 | } |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 198 | void Manager::destroyObject(const char* path) |
Brad Bishop | 656a7d0 | 2016-10-19 22:20:02 -0400 | [diff] [blame] | 199 | { |
| 200 | std::string p{path}; |
| 201 | _refs.erase(_root + p); |
| 202 | } |
| 203 | |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 204 | details::holder::Base& Manager::getInterfaceHolder( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 205 | const char* path, const char* interface) |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 206 | { |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 207 | return const_cast<const Manager*>( |
| 208 | this)->getInterfaceHolder(path, interface); |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | details::holder::Base& Manager::getInterfaceHolder( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 212 | const char* path, const char* interface) const |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 213 | { |
| 214 | std::string p{path}; |
| 215 | auto oit = _refs.find(_root + p); |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 216 | if (oit == _refs.end()) |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 217 | throw std::runtime_error( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 218 | _root + p + " was not found"); |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 219 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 220 | auto& obj = oit->second; |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 221 | auto iit = obj.find(interface); |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 222 | if (iit == obj.end()) |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 223 | throw std::runtime_error( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 224 | "interface was not found"); |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 225 | |
| 226 | return *iit->second; |
| 227 | } |
| 228 | |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 229 | } // namespace manager |
| 230 | } // namespace inventory |
| 231 | } // namespace phosphor |
| 232 | |
| 233 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |