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