blob: 5ad4f445c992a266d1b63c3558755debe15d350b [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 Bishop25d54b52018-11-21 12:57:51 -050012#include <any>
Patrick Venturea680d1e2018-10-14 13:34:26 -070013#include <map>
14#include <memory>
15#include <sdbusplus/server.hpp>
16#include <string>
17#include <vector>
18#include <xyz/openbmc_project/Inventory/Manager/server.hpp>
Brad Bishop49aefb32016-10-19 11:54:14 -040019
Brad Bishop02763c62018-12-12 22:02:07 -050020namespace sdbusplus
21{
22namespace bus
23{
24class bus;
25}
26} // namespace sdbusplus
Brad Bishop49aefb32016-10-19 11:54:14 -040027namespace phosphor
28{
29namespace inventory
30{
31namespace manager
32{
Brad Bishop451f8d92016-11-21 14:15:19 -050033
Patrick Venturea680d1e2018-10-14 13:34:26 -070034template <typename T>
35using ServerObject = T;
Brad Bishop451f8d92016-11-21 14:15:19 -050036
37using ManagerIface =
Brad Bishop9aa5e2f2017-01-15 19:45:40 -050038 sdbusplus::xyz::openbmc_project::Inventory::server::Manager;
Brad Bishop451f8d92016-11-21 14:15:19 -050039
Brad Bishop49aefb32016-10-19 11:54:14 -040040/** @class Manager
41 * @brief OpenBMC inventory manager implementation.
42 *
43 * A concrete implementation for the xyz.openbmc_project.Inventory.Manager
44 * DBus API.
45 */
Brad Bishop615b2a82018-03-29 10:32:41 -040046class Manager final : public ServerObject<ManagerIface>
Brad Bishop49aefb32016-10-19 11:54:14 -040047{
Brad Bishop615b2a82018-03-29 10:32:41 -040048 public:
49 Manager() = delete;
50 Manager(const Manager&) = delete;
51 Manager& operator=(const Manager&) = delete;
52 Manager(Manager&&) = default;
53 Manager& operator=(Manager&&) = default;
54 ~Manager() = default;
Brad Bishop49aefb32016-10-19 11:54:14 -040055
Brad Bishop615b2a82018-03-29 10:32:41 -040056 /** @brief Construct an inventory manager.
57 *
58 * @param[in] bus - An sdbusplus bus connection.
59 * @param[in] busname - The DBus busname to own.
60 * @param[in] root - The DBus path on which to implement
61 * an inventory manager.
62 * @param[in] iface - The DBus inventory interface to implement.
63 */
64 Manager(sdbusplus::bus::bus&&, const char*, const char*, const char*);
Brad Bishop49aefb32016-10-19 11:54:14 -040065
Brad Bishop615b2a82018-03-29 10:32:41 -040066 using EventInfo =
67 std::tuple<std::vector<EventBasePtr>, std::vector<Action>>;
Brad Bishop49aefb32016-10-19 11:54:14 -040068
Brad Bishop615b2a82018-03-29 10:32:41 -040069 /** @brief Start processing DBus messages. */
70 void run() noexcept;
Brad Bishop49aefb32016-10-19 11:54:14 -040071
Brad Bishop615b2a82018-03-29 10:32:41 -040072 /** @brief Provided for testing only. */
73 void shutdown() noexcept;
Brad Bishop49aefb32016-10-19 11:54:14 -040074
Brad Bishop615b2a82018-03-29 10:32:41 -040075 /** @brief sd_bus Notify method implementation callback. */
76 void
77 notify(std::map<sdbusplus::message::object_path, Object> objs) override;
Brad Bishop49aefb32016-10-19 11:54:14 -040078
Brad Bishop615b2a82018-03-29 10:32:41 -040079 /** @brief Event processing entry point. */
80 void handleEvent(sdbusplus::message::message&, const Event& event,
81 const EventInfo& info);
Brad Bishop49aefb32016-10-19 11:54:14 -040082
Brad Bishop615b2a82018-03-29 10:32:41 -040083 /** @brief Drop one or more objects from DBus. */
84 void destroyObjects(const std::vector<const char*>& paths);
Brad Bishop656a7d02016-10-19 22:20:02 -040085
Brad Bishop615b2a82018-03-29 10:32:41 -040086 /** @brief Add objects to DBus. */
87 void createObjects(
88 const std::map<sdbusplus::message::object_path, Object>& objs);
Brad Bishopeb68a682017-01-22 00:58:54 -050089
Brad Bishop615b2a82018-03-29 10:32:41 -040090 /** @brief Add or update objects on DBus. */
91 void updateObjects(
92 const std::map<sdbusplus::message::object_path, Object>& objs,
93 bool restoreFromCache = false);
Deepak Kodihallib28990f2017-08-08 07:19:34 -050094
Brad Bishop615b2a82018-03-29 10:32:41 -040095 /** @brief Restore persistent inventory items */
96 void restore();
Brad Bishop79ccaf72017-01-22 16:00:50 -050097
Brad Bishop615b2a82018-03-29 10:32:41 -040098 /** @brief Invoke an sdbusplus server binding method.
99 *
100 * Invoke the requested method with a reference to the requested
101 * sdbusplus server binding interface as a parameter.
102 *
103 * @tparam T - The sdbusplus server binding interface type.
104 * @tparam U - The type of the sdbusplus server binding member.
105 * @tparam Args - Argument types of the binding member.
106 *
107 * @param[in] path - The DBus path on which the method should
108 * be invoked.
109 * @param[in] interface - The DBus interface hosting the method.
110 * @param[in] member - Pointer to sdbusplus server binding member.
111 * @param[in] args - Arguments to forward to the binding member.
112 *
113 * @returns - The return/value type of the binding method being
114 * called.
115 */
116 template <typename T, typename U, typename... Args>
117 decltype(auto) invokeMethod(const char* path, const char* interface,
118 U&& member, Args&&... args)
119 {
120 auto& iface = getInterface<T>(path, interface);
121 return (iface.*member)(std::forward<Args>(args)...);
122 }
Brad Bishopda649b12016-11-30 14:35:02 -0500123
Brad Bishop615b2a82018-03-29 10:32:41 -0400124 using SigArgs = std::vector<std::unique_ptr<
125 std::tuple<Manager*, const DbusSignal*, const EventInfo*>>>;
126 using SigArg = SigArgs::value_type::element_type;
Brad Bishop49aefb32016-10-19 11:54:14 -0400127
Brad Bishop615b2a82018-03-29 10:32:41 -0400128 private:
Brad Bishop25d54b52018-11-21 12:57:51 -0500129 using InterfaceComposite = std::map<std::string, std::any>;
Brad Bishop615b2a82018-03-29 10:32:41 -0400130 using ObjectReferences = std::map<std::string, InterfaceComposite>;
131 using Events = std::vector<EventInfo>;
Brad Bishop90c30bc2017-01-22 16:40:47 -0500132
Brad Bishop615b2a82018-03-29 10:32:41 -0400133 // The int instantiations are safe since the signature of these
134 // functions don't change from one instantiation to the next.
Brad Bishop615b2a82018-03-29 10:32:41 -0400135 using Makers =
Brad Bishop02763c62018-12-12 22:02:07 -0500136 std::map<std::string, std::tuple<MakeInterfaceType, AssignInterfaceType,
137 SerializeInterfaceType<SerialOps>,
138 DeserializeInterfaceType<SerialOps>>>;
Brad Bishop49aefb32016-10-19 11:54:14 -0400139
Brad Bishop615b2a82018-03-29 10:32:41 -0400140 /** @brief Provides weak references to interface holders.
141 *
142 * Common code for all types for the templated getInterface
143 * methods.
144 *
145 * @param[in] path - The DBus path for which the interface
146 * holder instance should be provided.
147 * @param[in] interface - The DBus interface for which the
148 * holder instance should be provided.
149 *
150 * @returns A weak reference to the holder instance.
151 */
Brad Bishop25d54b52018-11-21 12:57:51 -0500152 const std::any& getInterfaceHolder(const char*, const char*) const;
153 std::any& getInterfaceHolder(const char*, const char*);
Brad Bishopb83a21e2016-11-30 13:43:37 -0500154
Brad Bishop615b2a82018-03-29 10:32:41 -0400155 /** @brief Provides weak references to interface holders.
156 *
157 * @tparam T - The sdbusplus server binding interface type.
158 *
159 * @param[in] path - The DBus path for which the interface
160 * should be provided.
161 * @param[in] interface - The DBus interface to obtain.
162 *
163 * @returns A weak reference to the interface holder.
164 */
165 template <typename T>
166 auto& getInterface(const char* path, const char* interface)
167 {
168 auto& holder = getInterfaceHolder(path, interface);
Brad Bishop25d54b52018-11-21 12:57:51 -0500169 return *std::any_cast<std::shared_ptr<T>&>(holder);
Brad Bishop615b2a82018-03-29 10:32:41 -0400170 }
171 template <typename T>
172 auto& getInterface(const char* path, const char* interface) const
173 {
174 auto& holder = getInterfaceHolder(path, interface);
Brad Bishop25d54b52018-11-21 12:57:51 -0500175 return *std::any_cast<T>(holder);
Brad Bishop615b2a82018-03-29 10:32:41 -0400176 }
Brad Bishopb83a21e2016-11-30 13:43:37 -0500177
Brad Bishop615b2a82018-03-29 10:32:41 -0400178 /** @brief Add or update interfaces on DBus. */
179 void updateInterfaces(const sdbusplus::message::object_path& path,
180 const Object& interfaces,
181 ObjectReferences::iterator pos,
182 bool emitSignals = true,
183 bool restoreFromCache = false);
Brad Bishop79ccaf72017-01-22 16:00:50 -0500184
Brad Bishop615b2a82018-03-29 10:32:41 -0400185 /** @brief Provided for testing only. */
186 volatile bool _shutdown;
Brad Bishop49aefb32016-10-19 11:54:14 -0400187
Brad Bishop615b2a82018-03-29 10:32:41 -0400188 /** @brief Path prefix applied to any relative paths. */
189 const char* _root;
Brad Bishop49aefb32016-10-19 11:54:14 -0400190
Brad Bishop615b2a82018-03-29 10:32:41 -0400191 /** @brief A container of sdbusplus server interface references. */
192 ObjectReferences _refs;
Brad Bishop49aefb32016-10-19 11:54:14 -0400193
Brad Bishop615b2a82018-03-29 10:32:41 -0400194 /** @brief A container contexts for signal callbacks. */
195 SigArgs _sigargs;
Brad Bishop49aefb32016-10-19 11:54:14 -0400196
Brad Bishop615b2a82018-03-29 10:32:41 -0400197 /** @brief A container of sdbusplus signal matches. */
198 std::vector<sdbusplus::bus::match_t> _matches;
Brad Bishop49aefb32016-10-19 11:54:14 -0400199
Brad Bishop615b2a82018-03-29 10:32:41 -0400200 /** @brief Persistent sdbusplus DBus bus connection. */
201 sdbusplus::bus::bus _bus;
Brad Bishop49aefb32016-10-19 11:54:14 -0400202
Brad Bishop615b2a82018-03-29 10:32:41 -0400203 /** @brief sdbusplus org.freedesktop.DBus.ObjectManager reference. */
204 sdbusplus::server::manager::manager _manager;
Brad Bishop49aefb32016-10-19 11:54:14 -0400205
Brad Bishop615b2a82018-03-29 10:32:41 -0400206 /** @brief A container of pimgen generated events and responses. */
207 static const Events _events;
Brad Bishop5fbaa7f2016-10-31 10:42:41 -0500208
Brad Bishop615b2a82018-03-29 10:32:41 -0400209 /** @brief A container of pimgen generated factory methods. */
210 static const Makers _makers;
Matt Spinler852db672019-03-06 13:46:14 -0600211
212 /** @brief Handles creating mapper associations for inventory objects */
213#ifdef CREATE_ASSOCIATIONS
214 associations::Manager _associations;
215#endif
Brad Bishop49aefb32016-10-19 11:54:14 -0400216};
217
218} // namespace manager
219} // namespace inventory
220} // namespace phosphor
221
222// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4