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