blob: 380f88821b9a0c277e8e325a6f768be91379b9be [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.
60 * @param[in] busname - The DBus busname to own.
61 * @param[in] root - The DBus path on which to implement
62 * an inventory manager.
63 * @param[in] iface - The DBus inventory interface to implement.
64 */
65 Manager(sdbusplus::bus::bus&&, const char*, const char*, const char*);
Brad Bishop49aefb32016-10-19 11:54:14 -040066
Brad Bishop615b2a82018-03-29 10:32:41 -040067 using EventInfo =
68 std::tuple<std::vector<EventBasePtr>, std::vector<Action>>;
Brad Bishop49aefb32016-10-19 11:54:14 -040069
Brad Bishop615b2a82018-03-29 10:32:41 -040070 /** @brief Start processing DBus messages. */
71 void run() noexcept;
Brad Bishop49aefb32016-10-19 11:54:14 -040072
Brad Bishop615b2a82018-03-29 10:32:41 -040073 /** @brief Provided for testing only. */
74 void shutdown() noexcept;
Brad Bishop49aefb32016-10-19 11:54:14 -040075
Brad Bishop615b2a82018-03-29 10:32:41 -040076 /** @brief sd_bus Notify method implementation callback. */
77 void
78 notify(std::map<sdbusplus::message::object_path, Object> objs) override;
Brad Bishop49aefb32016-10-19 11:54:14 -040079
Brad Bishop615b2a82018-03-29 10:32:41 -040080 /** @brief Event processing entry point. */
81 void handleEvent(sdbusplus::message::message&, const Event& event,
82 const EventInfo& info);
Brad Bishop49aefb32016-10-19 11:54:14 -040083
Brad Bishop615b2a82018-03-29 10:32:41 -040084 /** @brief Drop one or more objects from DBus. */
85 void destroyObjects(const std::vector<const char*>& paths);
Brad Bishop656a7d02016-10-19 22:20:02 -040086
Brad Bishop615b2a82018-03-29 10:32:41 -040087 /** @brief Add objects to DBus. */
88 void createObjects(
89 const std::map<sdbusplus::message::object_path, Object>& objs);
Brad Bishopeb68a682017-01-22 00:58:54 -050090
Brad Bishop615b2a82018-03-29 10:32:41 -040091 /** @brief Add or update objects on DBus. */
92 void updateObjects(
93 const std::map<sdbusplus::message::object_path, Object>& objs,
94 bool restoreFromCache = false);
Deepak Kodihallib28990f2017-08-08 07:19:34 -050095
Brad Bishop615b2a82018-03-29 10:32:41 -040096 /** @brief Restore persistent inventory items */
97 void restore();
Brad Bishop79ccaf72017-01-22 16:00:50 -050098
Brad Bishop615b2a82018-03-29 10:32:41 -040099 /** @brief Invoke an sdbusplus server binding method.
100 *
101 * Invoke the requested method with a reference to the requested
102 * sdbusplus server binding interface as a parameter.
103 *
104 * @tparam T - The sdbusplus server binding interface type.
105 * @tparam U - The type of the sdbusplus server binding member.
106 * @tparam Args - Argument types of the binding member.
107 *
108 * @param[in] path - The DBus path on which the method should
109 * be invoked.
110 * @param[in] interface - The DBus interface hosting the method.
111 * @param[in] member - Pointer to sdbusplus server binding member.
112 * @param[in] args - Arguments to forward to the binding member.
113 *
114 * @returns - The return/value type of the binding method being
115 * called.
116 */
117 template <typename T, typename U, typename... Args>
118 decltype(auto) invokeMethod(const char* path, const char* interface,
119 U&& member, Args&&... args)
120 {
121 auto& iface = getInterface<T>(path, interface);
122 return (iface.*member)(std::forward<Args>(args)...);
123 }
Brad Bishopda649b12016-11-30 14:35:02 -0500124
Brad Bishop615b2a82018-03-29 10:32:41 -0400125 using SigArgs = std::vector<std::unique_ptr<
126 std::tuple<Manager*, const DbusSignal*, const EventInfo*>>>;
127 using SigArg = SigArgs::value_type::element_type;
Brad Bishop49aefb32016-10-19 11:54:14 -0400128
Brad Bishop615b2a82018-03-29 10:32:41 -0400129 private:
Brad Bishop25d54b52018-11-21 12:57:51 -0500130 using InterfaceComposite = std::map<std::string, std::any>;
Brad Bishop615b2a82018-03-29 10:32:41 -0400131 using ObjectReferences = std::map<std::string, InterfaceComposite>;
132 using Events = std::vector<EventInfo>;
Brad Bishop90c30bc2017-01-22 16:40:47 -0500133
Brad Bishop615b2a82018-03-29 10:32:41 -0400134 // The int instantiations are safe since the signature of these
135 // functions don't change from one instantiation to the next.
Brad Bishop615b2a82018-03-29 10:32:41 -0400136 using Makers =
Brad Bishop02763c62018-12-12 22:02:07 -0500137 std::map<std::string, std::tuple<MakeInterfaceType, AssignInterfaceType,
138 SerializeInterfaceType<SerialOps>,
139 DeserializeInterfaceType<SerialOps>>>;
Brad Bishop49aefb32016-10-19 11:54:14 -0400140
Brad Bishop615b2a82018-03-29 10:32:41 -0400141 /** @brief Provides weak references to interface holders.
142 *
143 * Common code for all types for the templated getInterface
144 * methods.
145 *
146 * @param[in] path - The DBus path for which the interface
147 * holder instance should be provided.
148 * @param[in] interface - The DBus interface for which the
149 * holder instance should be provided.
150 *
151 * @returns A weak reference to the holder instance.
152 */
Brad Bishop25d54b52018-11-21 12:57:51 -0500153 const std::any& getInterfaceHolder(const char*, const char*) const;
154 std::any& getInterfaceHolder(const char*, const char*);
Brad Bishopb83a21e2016-11-30 13:43:37 -0500155
Brad Bishop615b2a82018-03-29 10:32:41 -0400156 /** @brief Provides weak references to interface holders.
157 *
158 * @tparam T - The sdbusplus server binding interface type.
159 *
160 * @param[in] path - The DBus path for which the interface
161 * should be provided.
162 * @param[in] interface - The DBus interface to obtain.
163 *
164 * @returns A weak reference to the interface holder.
165 */
166 template <typename T>
167 auto& getInterface(const char* path, const char* interface)
168 {
169 auto& holder = getInterfaceHolder(path, interface);
Brad Bishop25d54b52018-11-21 12:57:51 -0500170 return *std::any_cast<std::shared_ptr<T>&>(holder);
Brad Bishop615b2a82018-03-29 10:32:41 -0400171 }
172 template <typename T>
173 auto& getInterface(const char* path, const char* interface) const
174 {
175 auto& holder = getInterfaceHolder(path, interface);
Brad Bishop25d54b52018-11-21 12:57:51 -0500176 return *std::any_cast<T>(holder);
Brad Bishop615b2a82018-03-29 10:32:41 -0400177 }
Brad Bishopb83a21e2016-11-30 13:43:37 -0500178
Brad Bishop615b2a82018-03-29 10:32:41 -0400179 /** @brief Add or update interfaces on DBus. */
180 void updateInterfaces(const sdbusplus::message::object_path& path,
181 const Object& interfaces,
Brad Bishopae110f92020-12-06 11:43:16 -0500182 ObjectReferences::iterator pos, bool emitSignals,
183 bool restoreFromCache);
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