| 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 | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 117 | while (!_shutdown) | 
|  | 118 | { | 
|  | 119 | try | 
|  | 120 | { | 
| Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 121 | _bus.process_discard(); | 
| Brad Bishop | 2442498 | 2017-01-13 16:37:14 -0500 | [diff] [blame] | 122 | _bus.wait((5000000us).count()); | 
| Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 123 | } | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 124 | catch (const std::exception& e) | 
|  | 125 | { | 
| Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 126 | std::cerr << e.what() << std::endl; | 
|  | 127 | } | 
|  | 128 | } | 
|  | 129 | } | 
|  | 130 |  | 
| Brad Bishop | 9aa5e2f | 2017-01-15 19:45:40 -0500 | [diff] [blame] | 131 | void Manager::notify(sdbusplus::message::object_path path, Object object) | 
| Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 132 | { | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 133 | try | 
|  | 134 | { | 
| Brad Bishop | 9aa5e2f | 2017-01-15 19:45:40 -0500 | [diff] [blame] | 135 |  | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 136 | if (object.cbegin() == object.cend()) | 
| Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 137 | throw std::runtime_error( | 
| Brad Bishop | 9aa5e2f | 2017-01-15 19:45:40 -0500 | [diff] [blame] | 138 | "No interfaces in " + path.str); | 
| Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 139 |  | 
| Brad Bishop | 9aa5e2f | 2017-01-15 19:45:40 -0500 | [diff] [blame] | 140 | path.str.insert(0, _root); | 
| Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 141 |  | 
|  | 142 | auto obj = _refs.find(path); | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 143 | if (obj != _refs.end()) | 
| Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 144 | throw std::runtime_error( | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 145 | obj->first + " already exists"); | 
| Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 146 |  | 
|  | 147 | // Create an interface holder for each interface | 
|  | 148 | // provided by the client and group them into | 
|  | 149 | // a container. | 
|  | 150 | InterfaceComposite ref; | 
|  | 151 |  | 
| Brad Bishop | 6676c12 | 2017-01-15 20:38:39 -0500 | [diff] [blame] | 152 | auto i = object.size(); | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 153 | for (auto& x : object) | 
|  | 154 | { | 
| Brad Bishop | 6676c12 | 2017-01-15 20:38:39 -0500 | [diff] [blame] | 155 | // Defer sending any signals until the last interface. | 
|  | 156 | auto deferSignals = --i != 0; | 
| Brad Bishop | 5fbaa7f | 2016-10-31 10:42:41 -0500 | [diff] [blame] | 157 | auto maker = _makers.find(x.first.c_str()); | 
| Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 158 |  | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 159 | if (maker == _makers.end()) | 
| Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 160 | throw std::runtime_error( | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 161 | "Unimplemented interface: " + x.first); | 
| Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 162 |  | 
| Brad Bishop | 1ab880a | 2016-12-05 15:49:31 -0500 | [diff] [blame] | 163 | ref.emplace(x.first, | 
| Brad Bishop | 6676c12 | 2017-01-15 20:38:39 -0500 | [diff] [blame] | 164 | (maker->second)(_bus, path.str.c_str(), deferSignals)); | 
| Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 165 | } | 
|  | 166 |  | 
| Brad Bishop | 9d10fb2 | 2017-01-15 20:15:20 -0500 | [diff] [blame] | 167 | if (!ref.empty()) | 
|  | 168 | { | 
|  | 169 | // Hang on to a reference to the object (interfaces) | 
|  | 170 | // so it stays on the bus, and so we can make calls | 
|  | 171 | // to it if needed. | 
|  | 172 | _refs.emplace( | 
|  | 173 | path, std::move(ref)); | 
|  | 174 | } | 
| Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 175 | } | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 176 | catch (const std::exception& e) | 
|  | 177 | { | 
| Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 178 | std::cerr << e.what() << std::endl; | 
|  | 179 | } | 
|  | 180 | } | 
|  | 181 |  | 
| Brad Bishop | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame^] | 182 | void Manager::handleEvent( | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 183 | sdbusplus::message::message& msg, | 
| Brad Bishop | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame^] | 184 | const details::Event& event, | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 185 | const EventInfo& info) | 
| Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 186 | { | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 187 | auto& actions = std::get<1>(info); | 
| Brad Bishop | 3d57f50 | 2016-10-19 12:18:41 -0400 | [diff] [blame] | 188 |  | 
| Brad Bishop | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame^] | 189 | for (auto& f : event) | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 190 | { | 
| Brad Bishop | 2371900 | 2017-01-24 21:08:46 -0500 | [diff] [blame] | 191 | if (!(*f)(_bus, msg, *this)) | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 192 | { | 
| Brad Bishop | 064c94a | 2017-01-21 21:33:30 -0500 | [diff] [blame] | 193 | return; | 
| 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 | 064c94a | 2017-01-21 21:33:30 -0500 | [diff] [blame] | 196 | for (auto& action : actions) | 
|  | 197 | { | 
| Brad Bishop | 2371900 | 2017-01-24 21:08:46 -0500 | [diff] [blame] | 198 | (*action)(_bus, *this); | 
| Brad Bishop | 064c94a | 2017-01-21 21:33:30 -0500 | [diff] [blame] | 199 | } | 
| Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 200 | } | 
|  | 201 |  | 
| Brad Bishop | 7b7e712 | 2017-01-21 21:21:46 -0500 | [diff] [blame] | 202 | void Manager::destroyObjects( | 
|  | 203 | const std::vector<const char*>& paths) | 
| Brad Bishop | 656a7d0 | 2016-10-19 22:20:02 -0400 | [diff] [blame] | 204 | { | 
| Brad Bishop | 7b7e712 | 2017-01-21 21:21:46 -0500 | [diff] [blame] | 205 | for (const auto& path : paths) | 
|  | 206 | { | 
|  | 207 | std::string p{path}; | 
|  | 208 | _refs.erase(_root + p); | 
|  | 209 | } | 
| Brad Bishop | 656a7d0 | 2016-10-19 22:20:02 -0400 | [diff] [blame] | 210 | } | 
|  | 211 |  | 
| Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 212 | details::holder::Base& Manager::getInterfaceHolder( | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 213 | const char* path, const char* interface) | 
| Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 214 | { | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 215 | return const_cast<const Manager*>( | 
|  | 216 | this)->getInterfaceHolder(path, interface); | 
| Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 217 | } | 
|  | 218 |  | 
|  | 219 | details::holder::Base& Manager::getInterfaceHolder( | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 220 | const char* path, const char* interface) const | 
| Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 221 | { | 
|  | 222 | std::string p{path}; | 
|  | 223 | auto oit = _refs.find(_root + p); | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 224 | if (oit == _refs.end()) | 
| Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 225 | throw std::runtime_error( | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 226 | _root + p + " was not found"); | 
| Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 227 |  | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 228 | auto& obj = oit->second; | 
| Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 229 | auto iit = obj.find(interface); | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 230 | if (iit == obj.end()) | 
| Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 231 | throw std::runtime_error( | 
| Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 232 | "interface was not found"); | 
| Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 233 |  | 
|  | 234 | return *iit->second; | 
|  | 235 | } | 
|  | 236 |  | 
| Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 237 | } // namespace manager | 
|  | 238 | } // namespace inventory | 
|  | 239 | } // namespace phosphor | 
|  | 240 |  | 
|  | 241 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |