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