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 | 79ccaf7 | 2017-01-22 16:00:50 -0500 | [diff] [blame] | 19 | #include <algorithm> |
Saqib Khan | 101d837 | 2017-02-20 15:36:11 -0600 | [diff] [blame] | 20 | #include <phosphor-logging/log.hpp> |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 21 | #include "manager.hpp" |
Brad Bishop | 79ccaf7 | 2017-01-22 16:00:50 -0500 | [diff] [blame] | 22 | #include "errors.hpp" |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 23 | |
Brad Bishop | 2442498 | 2017-01-13 16:37:14 -0500 | [diff] [blame] | 24 | using namespace std::literals::chrono_literals; |
| 25 | |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 26 | namespace phosphor |
| 27 | { |
| 28 | namespace inventory |
| 29 | { |
| 30 | namespace manager |
| 31 | { |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 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, |
Brad Bishop | 12f8a3c | 2017-02-09 00:02:00 -0500 | [diff] [blame] | 47 | static_cast<const DbusSignal&>( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 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 | |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 59 | Manager::Manager( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 60 | sdbusplus::bus::bus&& bus, |
| 61 | const char* busname, |
| 62 | const char* root, |
| 63 | const char* iface) : |
Brad Bishop | 12f8a3c | 2017-02-09 00:02:00 -0500 | [diff] [blame] | 64 | ServerObject<ManagerIface>(bus, root), |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 65 | _shutdown(false), |
| 66 | _root(root), |
| 67 | _bus(std::move(bus)), |
Brad Bishop | 9b86483 | 2017-03-07 00:10:33 -0500 | [diff] [blame] | 68 | _manager(_bus, root) |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 69 | { |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 70 | for (auto& group : _events) |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 71 | { |
Brad Bishop | 12f8a3c | 2017-02-09 00:02:00 -0500 | [diff] [blame] | 72 | for (auto pEvent : std::get<std::vector<EventBasePtr>>( |
Brad Bishop | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame] | 73 | group)) |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 74 | { |
| 75 | if (pEvent->type != |
Brad Bishop | 12f8a3c | 2017-02-09 00:02:00 -0500 | [diff] [blame] | 76 | Event::Type::DBUS_SIGNAL) |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 77 | { |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 78 | continue; |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 79 | } |
Brad Bishop | 4f20a3e | 2016-11-29 15:21:46 -0500 | [diff] [blame] | 80 | |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 81 | // Create a callback context for this event group. |
Brad Bishop | 12f8a3c | 2017-02-09 00:02:00 -0500 | [diff] [blame] | 82 | auto dbusEvent = static_cast<DbusSignal*>( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 83 | pEvent.get()); |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 84 | |
| 85 | // Go ahead and store an iterator pointing at |
| 86 | // the event data to avoid lookups later since |
| 87 | // additional signal callbacks aren't added |
| 88 | // after the manager is constructed. |
| 89 | _sigargs.emplace_back( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 90 | std::make_unique<SigArg>( |
| 91 | this, |
| 92 | dbusEvent, |
| 93 | &group)); |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 94 | |
| 95 | // Register our callback and the context for |
| 96 | // each signal event. |
| 97 | _matches.emplace_back( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 98 | _bus, |
Brad Bishop | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame] | 99 | dbusEvent->signature, |
Brad Bishop | 12f8a3c | 2017-02-09 00:02:00 -0500 | [diff] [blame] | 100 | _signal, |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 101 | _sigargs.back().get()); |
Brad Bishop | 68c8083 | 2016-11-29 16:41:32 -0500 | [diff] [blame] | 102 | } |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | _bus.request_name(busname); |
| 106 | } |
| 107 | |
| 108 | void Manager::shutdown() noexcept |
| 109 | { |
| 110 | _shutdown = true; |
| 111 | } |
| 112 | |
| 113 | void Manager::run() noexcept |
| 114 | { |
Brad Bishop | 3e4a19a | 2017-01-21 22:17:09 -0500 | [diff] [blame] | 115 | sdbusplus::message::message unusedMsg{nullptr}; |
| 116 | |
| 117 | // Run startup events. |
| 118 | for (auto& group : _events) |
| 119 | { |
Brad Bishop | 12f8a3c | 2017-02-09 00:02:00 -0500 | [diff] [blame] | 120 | for (auto pEvent : std::get<std::vector<EventBasePtr>>( |
Brad Bishop | 3e4a19a | 2017-01-21 22:17:09 -0500 | [diff] [blame] | 121 | group)) |
| 122 | { |
| 123 | if (pEvent->type == |
Brad Bishop | 12f8a3c | 2017-02-09 00:02:00 -0500 | [diff] [blame] | 124 | Event::Type::STARTUP) |
Brad Bishop | 3e4a19a | 2017-01-21 22:17:09 -0500 | [diff] [blame] | 125 | { |
| 126 | handleEvent(unusedMsg, *pEvent, group); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 131 | while (!_shutdown) |
| 132 | { |
| 133 | try |
| 134 | { |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 135 | _bus.process_discard(); |
Brad Bishop | 2442498 | 2017-01-13 16:37:14 -0500 | [diff] [blame] | 136 | _bus.wait((5000000us).count()); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 137 | } |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 138 | catch (const std::exception& e) |
| 139 | { |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 140 | std::cerr << e.what() << std::endl; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
Brad Bishop | 79ccaf7 | 2017-01-22 16:00:50 -0500 | [diff] [blame] | 145 | void Manager::updateInterfaces( |
| 146 | const sdbusplus::message::object_path& path, |
| 147 | const Object& interfaces, |
| 148 | ObjectReferences::iterator pos, |
| 149 | bool newObject) |
| 150 | { |
| 151 | auto& refaces = pos->second; |
| 152 | auto ifaceit = interfaces.cbegin(); |
| 153 | auto opsit = _makers.cbegin(); |
| 154 | auto refaceit = refaces.begin(); |
| 155 | std::vector<std::string> signals; |
| 156 | |
| 157 | while (ifaceit != interfaces.cend()) |
| 158 | { |
| 159 | try |
| 160 | { |
| 161 | // Find the binding ops for this interface. |
| 162 | opsit = std::lower_bound( |
| 163 | opsit, |
| 164 | _makers.cend(), |
| 165 | ifaceit->first, |
| 166 | compareFirst(_makers.key_comp())); |
| 167 | |
| 168 | if (opsit == _makers.cend() || opsit->first != ifaceit->first) |
| 169 | { |
| 170 | // This interface is not supported. |
| 171 | throw InterfaceError( |
| 172 | "Encountered unsupported interface.", |
| 173 | ifaceit->first); |
| 174 | } |
| 175 | |
| 176 | // Find the binding insertion point or the binding to update. |
| 177 | refaceit = std::lower_bound( |
| 178 | refaceit, |
| 179 | refaces.end(), |
| 180 | ifaceit->first, |
| 181 | compareFirst(refaces.key_comp())); |
| 182 | |
| 183 | if (refaceit == refaces.end() || refaceit->first != ifaceit->first) |
| 184 | { |
| 185 | // Add the new interface. |
| 186 | auto& ctor = std::get<MakerType>(opsit->second); |
| 187 | refaceit = refaces.insert( |
| 188 | refaceit, |
| 189 | std::make_pair( |
| 190 | ifaceit->first, |
| 191 | ctor( |
| 192 | _bus, |
| 193 | path.str.c_str(), |
| 194 | ifaceit->second))); |
| 195 | signals.push_back(ifaceit->first); |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | // Set the new property values. |
| 200 | auto& assign = std::get<AssignerType>(opsit->second); |
| 201 | assign(ifaceit->second, refaceit->second); |
| 202 | } |
| 203 | } |
| 204 | catch (const InterfaceError& e) |
| 205 | { |
| 206 | // Reset the binding ops iterator since we are |
| 207 | // at the end. |
| 208 | opsit = _makers.cbegin(); |
| 209 | e.log(); |
| 210 | } |
| 211 | |
| 212 | ++ifaceit; |
| 213 | } |
| 214 | |
| 215 | if (newObject) |
| 216 | { |
| 217 | _bus.emit_object_added(path.str.c_str()); |
| 218 | } |
| 219 | else if (!signals.empty()) |
| 220 | { |
| 221 | // TODO: emit an interfaces added signal |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void Manager::updateObjects( |
| 226 | const std::map<sdbusplus::message::object_path, Object>& objs) |
| 227 | { |
| 228 | auto objit = objs.cbegin(); |
| 229 | auto refit = _refs.begin(); |
| 230 | std::string absPath; |
| 231 | bool newObj; |
| 232 | |
| 233 | while (objit != objs.cend()) |
| 234 | { |
| 235 | // Find the insertion point or the object to update. |
| 236 | refit = std::lower_bound( |
| 237 | refit, |
| 238 | _refs.end(), |
| 239 | objit->first, |
| 240 | compareFirst(RelPathCompare(_root))); |
| 241 | |
| 242 | absPath.assign(_root); |
| 243 | absPath.append(objit->first); |
| 244 | |
| 245 | newObj = false; |
| 246 | if (refit == _refs.end() || refit->first != absPath) |
| 247 | { |
| 248 | refit = _refs.insert( |
| 249 | refit, |
| 250 | std::make_pair( |
| 251 | absPath, |
| 252 | decltype(_refs)::mapped_type())); |
| 253 | newObj = true; |
| 254 | } |
| 255 | |
| 256 | updateInterfaces(absPath, objit->second, refit, newObj); |
| 257 | ++objit; |
| 258 | } |
| 259 | } |
| 260 | |
Brad Bishop | 03f4cd9 | 2017-02-03 15:17:21 -0500 | [diff] [blame] | 261 | void Manager::notify(std::map<sdbusplus::message::object_path, Object> objs) |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 262 | { |
Brad Bishop | cda036f | 2017-02-15 10:06:48 -0500 | [diff] [blame] | 263 | updateObjects(objs); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 264 | } |
| 265 | |
Brad Bishop | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame] | 266 | void Manager::handleEvent( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 267 | sdbusplus::message::message& msg, |
Brad Bishop | 12f8a3c | 2017-02-09 00:02:00 -0500 | [diff] [blame] | 268 | const Event& event, |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 269 | const EventInfo& info) |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 270 | { |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 271 | auto& actions = std::get<1>(info); |
Brad Bishop | 3d57f50 | 2016-10-19 12:18:41 -0400 | [diff] [blame] | 272 | |
Brad Bishop | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame] | 273 | for (auto& f : event) |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 274 | { |
Brad Bishop | 07934a6 | 2017-02-08 23:34:59 -0500 | [diff] [blame] | 275 | if (!f(_bus, msg, *this)) |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 276 | { |
Brad Bishop | 064c94a | 2017-01-21 21:33:30 -0500 | [diff] [blame] | 277 | return; |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 278 | } |
Brad Bishop | 3d57f50 | 2016-10-19 12:18:41 -0400 | [diff] [blame] | 279 | } |
Brad Bishop | 064c94a | 2017-01-21 21:33:30 -0500 | [diff] [blame] | 280 | for (auto& action : actions) |
| 281 | { |
Brad Bishop | 07934a6 | 2017-02-08 23:34:59 -0500 | [diff] [blame] | 282 | action(_bus, *this); |
Brad Bishop | 064c94a | 2017-01-21 21:33:30 -0500 | [diff] [blame] | 283 | } |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 284 | } |
| 285 | |
Brad Bishop | 7b7e712 | 2017-01-21 21:21:46 -0500 | [diff] [blame] | 286 | void Manager::destroyObjects( |
| 287 | const std::vector<const char*>& paths) |
Brad Bishop | 656a7d0 | 2016-10-19 22:20:02 -0400 | [diff] [blame] | 288 | { |
Brad Bishop | a5cc34c | 2017-02-03 20:57:36 -0500 | [diff] [blame] | 289 | std::string p; |
| 290 | |
Brad Bishop | 7b7e712 | 2017-01-21 21:21:46 -0500 | [diff] [blame] | 291 | for (const auto& path : paths) |
| 292 | { |
Brad Bishop | a5cc34c | 2017-02-03 20:57:36 -0500 | [diff] [blame] | 293 | p.assign(_root); |
| 294 | p.append(path); |
| 295 | _bus.emit_object_removed(p.c_str()); |
| 296 | _refs.erase(p); |
Brad Bishop | 7b7e712 | 2017-01-21 21:21:46 -0500 | [diff] [blame] | 297 | } |
Brad Bishop | 656a7d0 | 2016-10-19 22:20:02 -0400 | [diff] [blame] | 298 | } |
| 299 | |
Brad Bishop | eb68a68 | 2017-01-22 00:58:54 -0500 | [diff] [blame] | 300 | void Manager::createObjects( |
| 301 | const std::map<sdbusplus::message::object_path, Object>& objs) |
| 302 | { |
Brad Bishop | cda036f | 2017-02-15 10:06:48 -0500 | [diff] [blame] | 303 | updateObjects(objs); |
Brad Bishop | eb68a68 | 2017-01-22 00:58:54 -0500 | [diff] [blame] | 304 | } |
| 305 | |
Brad Bishop | 150147a | 2017-02-08 23:57:46 -0500 | [diff] [blame] | 306 | any_ns::any& Manager::getInterfaceHolder( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 307 | const char* path, const char* interface) |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 308 | { |
Brad Bishop | 150147a | 2017-02-08 23:57:46 -0500 | [diff] [blame] | 309 | return const_cast<any_ns::any&>( |
| 310 | const_cast<const Manager*>( |
| 311 | this)->getInterfaceHolder(path, interface)); |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 312 | } |
| 313 | |
Brad Bishop | 150147a | 2017-02-08 23:57:46 -0500 | [diff] [blame] | 314 | const any_ns::any& Manager::getInterfaceHolder( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 315 | const char* path, const char* interface) const |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 316 | { |
| 317 | std::string p{path}; |
| 318 | auto oit = _refs.find(_root + p); |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 319 | if (oit == _refs.end()) |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 320 | throw std::runtime_error( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 321 | _root + p + " was not found"); |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 322 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 323 | auto& obj = oit->second; |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 324 | auto iit = obj.find(interface); |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 325 | if (iit == obj.end()) |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 326 | throw std::runtime_error( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 327 | "interface was not found"); |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 328 | |
Brad Bishop | 150147a | 2017-02-08 23:57:46 -0500 | [diff] [blame] | 329 | return iit->second; |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 330 | } |
| 331 | |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 332 | } // namespace manager |
| 333 | } // namespace inventory |
| 334 | } // namespace phosphor |
| 335 | |
| 336 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |