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