blob: ab1133c251ae8c3c76c85556f34defbb1e853109 [file] [log] [blame]
Brad Bishop49aefb32016-10-19 11:54:14 -04001#pragma once
2
Brad Bishop67b788d2016-11-29 13:09:01 -05003#include "events.hpp"
Brad Bishopc1f47982017-02-09 01:27:38 -05004#include "functor.hpp"
Brad Bishop02763c62018-12-12 22:02:07 -05005#include "interface_ops.hpp"
Deepak Kodihalli6620e982017-08-05 13:09:54 -05006#include "serialize.hpp"
Patrick Venturea680d1e2018-10-14 13:34:26 -07007#include "types.hpp"
Matt Spinler852db672019-03-06 13:46:14 -06008#ifdef CREATE_ASSOCIATIONS
9#include "association_manager.hpp"
10#endif
Patrick Venturea680d1e2018-10-14 13:34:26 -070011
Brad Bishopa83db302020-12-06 14:51:23 -050012#include <sdbusplus/server.hpp>
13#include <xyz/openbmc_project/Inventory/Manager/server.hpp>
14
Brad Bishop25d54b52018-11-21 12:57:51 -050015#include <any>
Patrick Venturea680d1e2018-10-14 13:34:26 -070016#include <map>
17#include <memory>
Patrick Venturea680d1e2018-10-14 13:34:26 -070018#include <string>
19#include <vector>
Brad Bishop49aefb32016-10-19 11:54:14 -040020
Brad Bishop02763c62018-12-12 22:02:07 -050021namespace sdbusplus
22{
23namespace bus
24{
25class bus;
26}
27} // namespace sdbusplus
Brad Bishop49aefb32016-10-19 11:54:14 -040028namespace phosphor
29{
30namespace inventory
31{
32namespace manager
33{
Brad Bishop451f8d92016-11-21 14:15:19 -050034
Patrick Venturea680d1e2018-10-14 13:34:26 -070035template <typename T>
36using ServerObject = T;
Brad Bishop451f8d92016-11-21 14:15:19 -050037
38using ManagerIface =
Brad Bishop9aa5e2f2017-01-15 19:45:40 -050039 sdbusplus::xyz::openbmc_project::Inventory::server::Manager;
Brad Bishop451f8d92016-11-21 14:15:19 -050040
Brad Bishop49aefb32016-10-19 11:54:14 -040041/** @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 Bishop615b2a82018-03-29 10:32:41 -040047class Manager final : public ServerObject<ManagerIface>
Brad Bishop49aefb32016-10-19 11:54:14 -040048{
Brad Bishop615b2a82018-03-29 10:32:41 -040049 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 Bishop49aefb32016-10-19 11:54:14 -040056
Brad Bishop615b2a82018-03-29 10:32:41 -040057 /** @brief Construct an inventory manager.
58 *
59 * @param[in] bus - An sdbusplus bus connection.
Brad Bishop615b2a82018-03-29 10:32:41 -040060 * @param[in] root - The DBus path on which to implement
61 * an inventory manager.
Brad Bishop615b2a82018-03-29 10:32:41 -040062 */
Brad Bishop20c94352020-12-06 20:23:56 -050063 Manager(sdbusplus::bus::bus&&, const char*);
Brad Bishop49aefb32016-10-19 11:54:14 -040064
Brad Bishop615b2a82018-03-29 10:32:41 -040065 using EventInfo =
66 std::tuple<std::vector<EventBasePtr>, std::vector<Action>>;
Brad Bishop49aefb32016-10-19 11:54:14 -040067
Brad Bishop20c94352020-12-06 20:23:56 -050068 /** @brief Start processing DBus messages.
69 *
70 * @param[in] busname - The DBus busname to own.
71 */
72 void run(const char*);
Brad Bishop49aefb32016-10-19 11:54:14 -040073
Brad Bishop615b2a82018-03-29 10:32:41 -040074 /** @brief Provided for testing only. */
75 void shutdown() noexcept;
Brad Bishop49aefb32016-10-19 11:54:14 -040076
Brad Bishop615b2a82018-03-29 10:32:41 -040077 /** @brief sd_bus Notify method implementation callback. */
78 void
79 notify(std::map<sdbusplus::message::object_path, Object> objs) override;
Brad Bishop49aefb32016-10-19 11:54:14 -040080
Brad Bishop615b2a82018-03-29 10:32:41 -040081 /** @brief Event processing entry point. */
82 void handleEvent(sdbusplus::message::message&, const Event& event,
83 const EventInfo& info);
Brad Bishop49aefb32016-10-19 11:54:14 -040084
Brad Bishop615b2a82018-03-29 10:32:41 -040085 /** @brief Drop one or more objects from DBus. */
86 void destroyObjects(const std::vector<const char*>& paths);
Brad Bishop656a7d02016-10-19 22:20:02 -040087
Brad Bishop615b2a82018-03-29 10:32:41 -040088 /** @brief Add objects to DBus. */
89 void createObjects(
90 const std::map<sdbusplus::message::object_path, Object>& objs);
Brad Bishopeb68a682017-01-22 00:58:54 -050091
Brad Bishop615b2a82018-03-29 10:32:41 -040092 /** @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 Kodihallib28990f2017-08-08 07:19:34 -050096
Brad Bishop615b2a82018-03-29 10:32:41 -040097 /** @brief Restore persistent inventory items */
98 void restore();
Brad Bishop79ccaf72017-01-22 16:00:50 -050099
Brad Bishop615b2a82018-03-29 10:32:41 -0400100 /** @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 Bishopda649b12016-11-30 14:35:02 -0500125
Brad Bishop615b2a82018-03-29 10:32:41 -0400126 using SigArgs = std::vector<std::unique_ptr<
127 std::tuple<Manager*, const DbusSignal*, const EventInfo*>>>;
128 using SigArg = SigArgs::value_type::element_type;
Brad Bishop49aefb32016-10-19 11:54:14 -0400129
Brad Bishop615b2a82018-03-29 10:32:41 -0400130 private:
Brad Bishop25d54b52018-11-21 12:57:51 -0500131 using InterfaceComposite = std::map<std::string, std::any>;
Brad Bishop615b2a82018-03-29 10:32:41 -0400132 using ObjectReferences = std::map<std::string, InterfaceComposite>;
133 using Events = std::vector<EventInfo>;
Brad Bishop90c30bc2017-01-22 16:40:47 -0500134
Brad Bishop615b2a82018-03-29 10:32:41 -0400135 // The int instantiations are safe since the signature of these
136 // functions don't change from one instantiation to the next.
Brad Bishop615b2a82018-03-29 10:32:41 -0400137 using Makers =
Brad Bishop02763c62018-12-12 22:02:07 -0500138 std::map<std::string, std::tuple<MakeInterfaceType, AssignInterfaceType,
139 SerializeInterfaceType<SerialOps>,
140 DeserializeInterfaceType<SerialOps>>>;
Brad Bishop49aefb32016-10-19 11:54:14 -0400141
Brad Bishop615b2a82018-03-29 10:32:41 -0400142 /** @brief Provides weak references to interface holders.
143 *
144 * Common code for all types for the templated getInterface
145 * methods.
146 *
147 * @param[in] path - The DBus path for which the interface
148 * holder instance should be provided.
149 * @param[in] interface - The DBus interface for which the
150 * holder instance should be provided.
151 *
152 * @returns A weak reference to the holder instance.
153 */
Brad Bishop25d54b52018-11-21 12:57:51 -0500154 const std::any& getInterfaceHolder(const char*, const char*) const;
155 std::any& getInterfaceHolder(const char*, const char*);
Brad Bishopb83a21e2016-11-30 13:43:37 -0500156
Brad Bishop615b2a82018-03-29 10:32:41 -0400157 /** @brief Provides weak references to interface holders.
158 *
159 * @tparam T - The sdbusplus server binding interface type.
160 *
161 * @param[in] path - The DBus path for which the interface
162 * should be provided.
163 * @param[in] interface - The DBus interface to obtain.
164 *
165 * @returns A weak reference to the interface holder.
166 */
167 template <typename T>
168 auto& getInterface(const char* path, const char* interface)
169 {
170 auto& holder = getInterfaceHolder(path, interface);
Brad Bishop25d54b52018-11-21 12:57:51 -0500171 return *std::any_cast<std::shared_ptr<T>&>(holder);
Brad Bishop615b2a82018-03-29 10:32:41 -0400172 }
173 template <typename T>
174 auto& getInterface(const char* path, const char* interface) const
175 {
176 auto& holder = getInterfaceHolder(path, interface);
Brad Bishop25d54b52018-11-21 12:57:51 -0500177 return *std::any_cast<T>(holder);
Brad Bishop615b2a82018-03-29 10:32:41 -0400178 }
Brad Bishopb83a21e2016-11-30 13:43:37 -0500179
Brad Bishop615b2a82018-03-29 10:32:41 -0400180 /** @brief Add or update interfaces on DBus. */
181 void updateInterfaces(const sdbusplus::message::object_path& path,
182 const Object& interfaces,
Brad Bishopae110f92020-12-06 11:43:16 -0500183 ObjectReferences::iterator pos, bool emitSignals,
184 bool restoreFromCache);
Brad Bishop79ccaf72017-01-22 16:00:50 -0500185
Brad Bishop615b2a82018-03-29 10:32:41 -0400186 /** @brief Path prefix applied to any relative paths. */
187 const char* _root;
Brad Bishop49aefb32016-10-19 11:54:14 -0400188
Brad Bishop615b2a82018-03-29 10:32:41 -0400189 /** @brief A container of sdbusplus server interface references. */
190 ObjectReferences _refs;
Brad Bishop49aefb32016-10-19 11:54:14 -0400191
Brad Bishop615b2a82018-03-29 10:32:41 -0400192 /** @brief A container contexts for signal callbacks. */
193 SigArgs _sigargs;
Brad Bishop49aefb32016-10-19 11:54:14 -0400194
Brad Bishop615b2a82018-03-29 10:32:41 -0400195 /** @brief A container of sdbusplus signal matches. */
196 std::vector<sdbusplus::bus::match_t> _matches;
Brad Bishop49aefb32016-10-19 11:54:14 -0400197
Brad Bishop615b2a82018-03-29 10:32:41 -0400198 /** @brief Persistent sdbusplus DBus bus connection. */
199 sdbusplus::bus::bus _bus;
Brad Bishop49aefb32016-10-19 11:54:14 -0400200
Brad Bishop615b2a82018-03-29 10:32:41 -0400201 /** @brief sdbusplus org.freedesktop.DBus.ObjectManager reference. */
202 sdbusplus::server::manager::manager _manager;
Brad Bishop49aefb32016-10-19 11:54:14 -0400203
Brad Bishop615b2a82018-03-29 10:32:41 -0400204 /** @brief A container of pimgen generated events and responses. */
205 static const Events _events;
Brad Bishop5fbaa7f2016-10-31 10:42:41 -0500206
Brad Bishop615b2a82018-03-29 10:32:41 -0400207 /** @brief A container of pimgen generated factory methods. */
208 static const Makers _makers;
Matt Spinler852db672019-03-06 13:46:14 -0600209
210 /** @brief Handles creating mapper associations for inventory objects */
211#ifdef CREATE_ASSOCIATIONS
212 associations::Manager _associations;
213#endif
Brad Bishop4627a9c2020-12-06 19:26:57 -0500214
215 /** @brief Manager status indicator */
216 volatile enum class ManagerStatus { STARTING, RUNNING, STOPPING } _status;
Brad Bishop49aefb32016-10-19 11:54:14 -0400217};
218
219} // namespace manager
220} // namespace inventory
221} // namespace phosphor
222
223// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4