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 | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame] | 45 | mgr.handleEvent( |
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 | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame] | 74 | for (auto pEvent : std::get<std::vector<details::EventBasePtr>>( |
| 75 | group)) |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 76 | { |
| 77 | if (pEvent->type != |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 78 | details::Event::Type::DBUS_SIGNAL) |
| 79 | { |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 80 | continue; |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 81 | } |
Brad Bishop | 4f20a3e | 2016-11-29 15:21:46 -0500 | [diff] [blame] | 82 | |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 83 | // Create a callback context for this event group. |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 84 | auto dbusEvent = static_cast<details::DbusSignal*>( |
| 85 | pEvent.get()); |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 86 | |
| 87 | // Go ahead and store an iterator pointing at |
| 88 | // the event data to avoid lookups later since |
| 89 | // additional signal callbacks aren't added |
| 90 | // after the manager is constructed. |
| 91 | _sigargs.emplace_back( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 92 | std::make_unique<SigArg>( |
| 93 | this, |
| 94 | dbusEvent, |
| 95 | &group)); |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 96 | |
| 97 | // Register our callback and the context for |
| 98 | // each signal event. |
| 99 | _matches.emplace_back( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 100 | _bus, |
Brad Bishop | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame] | 101 | dbusEvent->signature, |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 102 | details::_signal, |
| 103 | _sigargs.back().get()); |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 104 | } |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | _bus.request_name(busname); |
| 108 | } |
| 109 | |
| 110 | void Manager::shutdown() noexcept |
| 111 | { |
| 112 | _shutdown = true; |
| 113 | } |
| 114 | |
| 115 | void Manager::run() noexcept |
| 116 | { |
Brad Bishop | 3e4a19a | 2017-01-21 22:17:09 -0500 | [diff] [blame^] | 117 | sdbusplus::message::message unusedMsg{nullptr}; |
| 118 | |
| 119 | // Run startup events. |
| 120 | for (auto& group : _events) |
| 121 | { |
| 122 | for (auto pEvent : std::get<std::vector<details::EventBasePtr>>( |
| 123 | group)) |
| 124 | { |
| 125 | if (pEvent->type == |
| 126 | details::Event::Type::STARTUP) |
| 127 | { |
| 128 | handleEvent(unusedMsg, *pEvent, group); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 133 | while (!_shutdown) |
| 134 | { |
| 135 | try |
| 136 | { |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 137 | _bus.process_discard(); |
Brad Bishop | 2442498 | 2017-01-13 16:37:14 -0500 | [diff] [blame] | 138 | _bus.wait((5000000us).count()); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 139 | } |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 140 | catch (const std::exception& e) |
| 141 | { |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 142 | std::cerr << e.what() << std::endl; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
Brad Bishop | 9aa5e2f | 2017-01-15 19:45:40 -0500 | [diff] [blame] | 147 | void Manager::notify(sdbusplus::message::object_path path, Object object) |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 148 | { |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 149 | try |
| 150 | { |
Brad Bishop | eb68a68 | 2017-01-22 00:58:54 -0500 | [diff] [blame] | 151 | createObjects({std::make_pair(path, object)}); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 152 | } |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 153 | catch (const std::exception& e) |
| 154 | { |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 155 | std::cerr << e.what() << std::endl; |
| 156 | } |
| 157 | } |
| 158 | |
Brad Bishop | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame] | 159 | void Manager::handleEvent( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 160 | sdbusplus::message::message& msg, |
Brad Bishop | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame] | 161 | const details::Event& event, |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 162 | const EventInfo& info) |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 163 | { |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 164 | auto& actions = std::get<1>(info); |
Brad Bishop | 3d57f50 | 2016-10-19 12:18:41 -0400 | [diff] [blame] | 165 | |
Brad Bishop | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame] | 166 | for (auto& f : event) |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 167 | { |
Brad Bishop | 2371900 | 2017-01-24 21:08:46 -0500 | [diff] [blame] | 168 | if (!(*f)(_bus, msg, *this)) |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 169 | { |
Brad Bishop | 064c94a | 2017-01-21 21:33:30 -0500 | [diff] [blame] | 170 | return; |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 171 | } |
Brad Bishop | 3d57f50 | 2016-10-19 12:18:41 -0400 | [diff] [blame] | 172 | } |
Brad Bishop | 064c94a | 2017-01-21 21:33:30 -0500 | [diff] [blame] | 173 | for (auto& action : actions) |
| 174 | { |
Brad Bishop | 2371900 | 2017-01-24 21:08:46 -0500 | [diff] [blame] | 175 | (*action)(_bus, *this); |
Brad Bishop | 064c94a | 2017-01-21 21:33:30 -0500 | [diff] [blame] | 176 | } |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Brad Bishop | 7b7e712 | 2017-01-21 21:21:46 -0500 | [diff] [blame] | 179 | void Manager::destroyObjects( |
| 180 | const std::vector<const char*>& paths) |
Brad Bishop | 656a7d0 | 2016-10-19 22:20:02 -0400 | [diff] [blame] | 181 | { |
Brad Bishop | 7b7e712 | 2017-01-21 21:21:46 -0500 | [diff] [blame] | 182 | for (const auto& path : paths) |
| 183 | { |
| 184 | std::string p{path}; |
| 185 | _refs.erase(_root + p); |
| 186 | } |
Brad Bishop | 656a7d0 | 2016-10-19 22:20:02 -0400 | [diff] [blame] | 187 | } |
| 188 | |
Brad Bishop | eb68a68 | 2017-01-22 00:58:54 -0500 | [diff] [blame] | 189 | void Manager::createObjects( |
| 190 | const std::map<sdbusplus::message::object_path, Object>& objs) |
| 191 | { |
| 192 | std::string absPath; |
| 193 | |
| 194 | for (auto& o : objs) |
| 195 | { |
| 196 | auto& relPath = o.first; |
| 197 | auto& ifaces = o.second; |
| 198 | |
| 199 | absPath.assign(_root); |
| 200 | absPath.append(relPath); |
| 201 | |
| 202 | auto obj = _refs.find(absPath); |
| 203 | if (obj != _refs.end()) |
| 204 | { |
| 205 | // This object already exists...skip. |
| 206 | continue; |
| 207 | } |
| 208 | |
| 209 | // Create an interface holder for each interface |
| 210 | // provided by the client and group them into |
| 211 | // a container. |
| 212 | InterfaceComposite ref; |
| 213 | |
| 214 | auto i = ifaces.size(); |
| 215 | for (auto& iface : ifaces) |
| 216 | { |
| 217 | auto& props = iface.second; |
| 218 | |
| 219 | // Defer sending any signals until the last interface. |
| 220 | auto deferSignals = --i != 0; |
| 221 | auto pMakers = _makers.find(iface.first.c_str()); |
| 222 | |
| 223 | if (pMakers == _makers.end()) |
| 224 | { |
| 225 | // This interface is not known. |
| 226 | continue; |
| 227 | } |
| 228 | |
| 229 | auto& maker = std::get<MakerType>(pMakers->second); |
| 230 | |
| 231 | ref.emplace( |
| 232 | iface.first, |
| 233 | maker( |
| 234 | _bus, |
| 235 | absPath.c_str(), |
| 236 | props, |
| 237 | deferSignals)); |
| 238 | } |
| 239 | |
| 240 | if (!ref.empty()) |
| 241 | { |
| 242 | // Hang on to a reference to the object (interfaces) |
| 243 | // so it stays on the bus, and so we can make calls |
| 244 | // to it if needed. |
| 245 | _refs.emplace( |
| 246 | absPath, std::move(ref)); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 251 | details::holder::Base& Manager::getInterfaceHolder( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 252 | const char* path, const char* interface) |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 253 | { |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 254 | return const_cast<const Manager*>( |
| 255 | this)->getInterfaceHolder(path, interface); |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | details::holder::Base& Manager::getInterfaceHolder( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 259 | const char* path, const char* interface) const |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 260 | { |
| 261 | std::string p{path}; |
| 262 | auto oit = _refs.find(_root + p); |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 263 | if (oit == _refs.end()) |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 264 | throw std::runtime_error( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 265 | _root + p + " was not found"); |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 266 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 267 | auto& obj = oit->second; |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 268 | auto iit = obj.find(interface); |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 269 | if (iit == obj.end()) |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 270 | throw std::runtime_error( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 271 | "interface was not found"); |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 272 | |
| 273 | return *iit->second; |
| 274 | } |
| 275 | |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 276 | } // namespace manager |
| 277 | } // namespace inventory |
| 278 | } // namespace phosphor |
| 279 | |
| 280 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |