Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Brad Bishop | 67b788d | 2016-11-29 13:09:01 -0500 | [diff] [blame] | 3 | #include "events.hpp" |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 4 | #include "functor.hpp" |
Brad Bishop | 02763c6 | 2018-12-12 22:02:07 -0500 | [diff] [blame] | 5 | #include "interface_ops.hpp" |
Deepak Kodihalli | 6620e98 | 2017-08-05 13:09:54 -0500 | [diff] [blame] | 6 | #include "serialize.hpp" |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 7 | #include "types.hpp" |
Matt Spinler | 852db67 | 2019-03-06 13:46:14 -0600 | [diff] [blame] | 8 | #ifdef CREATE_ASSOCIATIONS |
| 9 | #include "association_manager.hpp" |
| 10 | #endif |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 11 | |
Brad Bishop | a83db30 | 2020-12-06 14:51:23 -0500 | [diff] [blame] | 12 | #include <sdbusplus/server.hpp> |
| 13 | #include <xyz/openbmc_project/Inventory/Manager/server.hpp> |
| 14 | |
Brad Bishop | 25d54b5 | 2018-11-21 12:57:51 -0500 | [diff] [blame] | 15 | #include <any> |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 16 | #include <map> |
| 17 | #include <memory> |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 18 | #include <string> |
| 19 | #include <vector> |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 20 | |
Brad Bishop | 02763c6 | 2018-12-12 22:02:07 -0500 | [diff] [blame] | 21 | namespace sdbusplus |
| 22 | { |
| 23 | namespace bus |
| 24 | { |
| 25 | class bus; |
| 26 | } |
| 27 | } // namespace sdbusplus |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 28 | namespace phosphor |
| 29 | { |
| 30 | namespace inventory |
| 31 | { |
| 32 | namespace manager |
| 33 | { |
Brad Bishop | 451f8d9 | 2016-11-21 14:15:19 -0500 | [diff] [blame] | 34 | |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 35 | template <typename T> |
| 36 | using ServerObject = T; |
Brad Bishop | 451f8d9 | 2016-11-21 14:15:19 -0500 | [diff] [blame] | 37 | |
| 38 | using ManagerIface = |
Brad Bishop | 9aa5e2f | 2017-01-15 19:45:40 -0500 | [diff] [blame] | 39 | sdbusplus::xyz::openbmc_project::Inventory::server::Manager; |
Brad Bishop | 451f8d9 | 2016-11-21 14:15:19 -0500 | [diff] [blame] | 40 | |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 41 | /** @class Manager |
| 42 | * @brief OpenBMC inventory manager implementation. |
| 43 | * |
| 44 | * A concrete implementation for the xyz.openbmc_project.Inventory.Manager |
| 45 | * DBus API. |
| 46 | */ |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 47 | class Manager final : public ServerObject<ManagerIface> |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 48 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 49 | public: |
| 50 | Manager() = delete; |
| 51 | Manager(const Manager&) = delete; |
| 52 | Manager& operator=(const Manager&) = delete; |
| 53 | Manager(Manager&&) = default; |
| 54 | Manager& operator=(Manager&&) = default; |
| 55 | ~Manager() = default; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 56 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 57 | /** @brief Construct an inventory manager. |
| 58 | * |
| 59 | * @param[in] bus - An sdbusplus bus connection. |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 60 | * @param[in] root - The DBus path on which to implement |
| 61 | * an inventory manager. |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 62 | */ |
Patrick Williams | 563306f | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 63 | Manager(sdbusplus::bus_t&&, const char*); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 64 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 65 | using EventInfo = |
| 66 | std::tuple<std::vector<EventBasePtr>, std::vector<Action>>; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 67 | |
Brad Bishop | 20c9435 | 2020-12-06 20:23:56 -0500 | [diff] [blame] | 68 | /** @brief Start processing DBus messages. |
| 69 | * |
| 70 | * @param[in] busname - The DBus busname to own. |
| 71 | */ |
| 72 | void run(const char*); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 73 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 74 | /** @brief Provided for testing only. */ |
| 75 | void shutdown() noexcept; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 76 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 77 | /** @brief sd_bus Notify method implementation callback. */ |
| 78 | void |
| 79 | notify(std::map<sdbusplus::message::object_path, Object> objs) override; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 80 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 81 | /** @brief Event processing entry point. */ |
Patrick Williams | 563306f | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 82 | void handleEvent(sdbusplus::message_t&, const Event& event, |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 83 | const EventInfo& info); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 84 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 85 | /** @brief Drop one or more objects from DBus. */ |
| 86 | void destroyObjects(const std::vector<const char*>& paths); |
Brad Bishop | 656a7d0 | 2016-10-19 22:20:02 -0400 | [diff] [blame] | 87 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 88 | /** @brief Add objects to DBus. */ |
| 89 | void createObjects( |
| 90 | const std::map<sdbusplus::message::object_path, Object>& objs); |
Brad Bishop | eb68a68 | 2017-01-22 00:58:54 -0500 | [diff] [blame] | 91 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 92 | /** @brief Add or update objects on DBus. */ |
| 93 | void updateObjects( |
| 94 | const std::map<sdbusplus::message::object_path, Object>& objs, |
| 95 | bool restoreFromCache = false); |
Deepak Kodihalli | b28990f | 2017-08-08 07:19:34 -0500 | [diff] [blame] | 96 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 97 | /** @brief Restore persistent inventory items */ |
| 98 | void restore(); |
Brad Bishop | 79ccaf7 | 2017-01-22 16:00:50 -0500 | [diff] [blame] | 99 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 100 | /** @brief Invoke an sdbusplus server binding method. |
| 101 | * |
| 102 | * Invoke the requested method with a reference to the requested |
| 103 | * sdbusplus server binding interface as a parameter. |
| 104 | * |
| 105 | * @tparam T - The sdbusplus server binding interface type. |
| 106 | * @tparam U - The type of the sdbusplus server binding member. |
| 107 | * @tparam Args - Argument types of the binding member. |
| 108 | * |
| 109 | * @param[in] path - The DBus path on which the method should |
| 110 | * be invoked. |
| 111 | * @param[in] interface - The DBus interface hosting the method. |
| 112 | * @param[in] member - Pointer to sdbusplus server binding member. |
| 113 | * @param[in] args - Arguments to forward to the binding member. |
| 114 | * |
| 115 | * @returns - The return/value type of the binding method being |
| 116 | * called. |
| 117 | */ |
| 118 | template <typename T, typename U, typename... Args> |
| 119 | decltype(auto) invokeMethod(const char* path, const char* interface, |
| 120 | U&& member, Args&&... args) |
| 121 | { |
| 122 | auto& iface = getInterface<T>(path, interface); |
| 123 | return (iface.*member)(std::forward<Args>(args)...); |
| 124 | } |
Brad Bishop | da649b1 | 2016-11-30 14:35:02 -0500 | [diff] [blame] | 125 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 126 | using SigArgs = std::vector<std::unique_ptr< |
| 127 | std::tuple<Manager*, const DbusSignal*, const EventInfo*>>>; |
| 128 | using SigArg = SigArgs::value_type::element_type; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 129 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 130 | private: |
Brad Bishop | 25d54b5 | 2018-11-21 12:57:51 -0500 | [diff] [blame] | 131 | using InterfaceComposite = std::map<std::string, std::any>; |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 132 | using ObjectReferences = std::map<std::string, InterfaceComposite>; |
| 133 | using Events = std::vector<EventInfo>; |
Brad Bishop | 90c30bc | 2017-01-22 16:40:47 -0500 | [diff] [blame] | 134 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 135 | // The int instantiations are safe since the signature of these |
| 136 | // functions don't change from one instantiation to the next. |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 137 | using Makers = |
Brad Bishop | 02763c6 | 2018-12-12 22:02:07 -0500 | [diff] [blame] | 138 | std::map<std::string, std::tuple<MakeInterfaceType, AssignInterfaceType, |
| 139 | SerializeInterfaceType<SerialOps>, |
Matt Spinler | 59521e8 | 2021-02-11 13:41:06 -0600 | [diff] [blame] | 140 | DeserializeInterfaceType<SerialOps> |
| 141 | #ifdef CREATE_ASSOCIATIONS |
| 142 | , |
| 143 | GetPropertyValueType |
| 144 | #endif |
| 145 | >>; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 146 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 147 | /** @brief Provides weak references to interface holders. |
| 148 | * |
| 149 | * Common code for all types for the templated getInterface |
| 150 | * methods. |
| 151 | * |
| 152 | * @param[in] path - The DBus path for which the interface |
| 153 | * holder instance should be provided. |
| 154 | * @param[in] interface - The DBus interface for which the |
| 155 | * holder instance should be provided. |
| 156 | * |
| 157 | * @returns A weak reference to the holder instance. |
| 158 | */ |
Brad Bishop | 25d54b5 | 2018-11-21 12:57:51 -0500 | [diff] [blame] | 159 | const std::any& getInterfaceHolder(const char*, const char*) const; |
| 160 | std::any& getInterfaceHolder(const char*, const char*); |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 161 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 162 | /** @brief Provides weak references to interface holders. |
| 163 | * |
| 164 | * @tparam T - The sdbusplus server binding interface type. |
| 165 | * |
| 166 | * @param[in] path - The DBus path for which the interface |
| 167 | * should be provided. |
| 168 | * @param[in] interface - The DBus interface to obtain. |
| 169 | * |
| 170 | * @returns A weak reference to the interface holder. |
| 171 | */ |
| 172 | template <typename T> |
| 173 | auto& getInterface(const char* path, const char* interface) |
| 174 | { |
| 175 | auto& holder = getInterfaceHolder(path, interface); |
Brad Bishop | 25d54b5 | 2018-11-21 12:57:51 -0500 | [diff] [blame] | 176 | return *std::any_cast<std::shared_ptr<T>&>(holder); |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 177 | } |
| 178 | template <typename T> |
| 179 | auto& getInterface(const char* path, const char* interface) const |
| 180 | { |
| 181 | auto& holder = getInterfaceHolder(path, interface); |
Brad Bishop | 25d54b5 | 2018-11-21 12:57:51 -0500 | [diff] [blame] | 182 | return *std::any_cast<T>(holder); |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 183 | } |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 184 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 185 | /** @brief Add or update interfaces on DBus. */ |
| 186 | void updateInterfaces(const sdbusplus::message::object_path& path, |
| 187 | const Object& interfaces, |
Brad Bishop | ae110f9 | 2020-12-06 11:43:16 -0500 | [diff] [blame] | 188 | ObjectReferences::iterator pos, bool emitSignals, |
| 189 | bool restoreFromCache); |
Brad Bishop | 79ccaf7 | 2017-01-22 16:00:50 -0500 | [diff] [blame] | 190 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 191 | /** @brief Path prefix applied to any relative paths. */ |
| 192 | const char* _root; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 193 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 194 | /** @brief A container of sdbusplus server interface references. */ |
| 195 | ObjectReferences _refs; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 196 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 197 | /** @brief A container contexts for signal callbacks. */ |
| 198 | SigArgs _sigargs; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 199 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 200 | /** @brief A container of sdbusplus signal matches. */ |
| 201 | std::vector<sdbusplus::bus::match_t> _matches; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 202 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 203 | /** @brief Persistent sdbusplus DBus bus connection. */ |
Patrick Williams | 563306f | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 204 | sdbusplus::bus_t _bus; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 205 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 206 | /** @brief sdbusplus org.freedesktop.DBus.ObjectManager reference. */ |
Patrick Williams | 563306f | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 207 | sdbusplus::server::manager_t _manager; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 208 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 209 | /** @brief A container of pimgen generated events and responses. */ |
| 210 | static const Events _events; |
Brad Bishop | 5fbaa7f | 2016-10-31 10:42:41 -0500 | [diff] [blame] | 211 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 212 | /** @brief A container of pimgen generated factory methods. */ |
| 213 | static const Makers _makers; |
Matt Spinler | 852db67 | 2019-03-06 13:46:14 -0600 | [diff] [blame] | 214 | |
| 215 | /** @brief Handles creating mapper associations for inventory objects */ |
| 216 | #ifdef CREATE_ASSOCIATIONS |
| 217 | associations::Manager _associations; |
| 218 | #endif |
Brad Bishop | 4627a9c | 2020-12-06 19:26:57 -0500 | [diff] [blame] | 219 | |
| 220 | /** @brief Manager status indicator */ |
| 221 | volatile enum class ManagerStatus { STARTING, RUNNING, STOPPING } _status; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | } // namespace manager |
| 225 | } // namespace inventory |
| 226 | } // namespace phosphor |
| 227 | |
| 228 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |