blob: cabbac9c038e4910ffab842b05a93bcfa1eb5561 [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"
8
Brad Bishop25d54b52018-11-21 12:57:51 -05009#include <any>
Patrick Venturea680d1e2018-10-14 13:34:26 -070010#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 Bishop49aefb32016-10-19 11:54:14 -040016
Brad Bishop02763c62018-12-12 22:02:07 -050017namespace sdbusplus
18{
19namespace bus
20{
21class bus;
22}
23} // namespace sdbusplus
Brad Bishop49aefb32016-10-19 11:54:14 -040024namespace phosphor
25{
26namespace inventory
27{
28namespace manager
29{
Brad Bishop451f8d92016-11-21 14:15:19 -050030
Patrick Venturea680d1e2018-10-14 13:34:26 -070031template <typename T>
32using ServerObject = T;
Brad Bishop451f8d92016-11-21 14:15:19 -050033
34using ManagerIface =
Brad Bishop9aa5e2f2017-01-15 19:45:40 -050035 sdbusplus::xyz::openbmc_project::Inventory::server::Manager;
Brad Bishop451f8d92016-11-21 14:15:19 -050036
Brad Bishop49aefb32016-10-19 11:54:14 -040037/** @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 Bishop615b2a82018-03-29 10:32:41 -040043class Manager final : public ServerObject<ManagerIface>
Brad Bishop49aefb32016-10-19 11:54:14 -040044{
Brad Bishop615b2a82018-03-29 10:32:41 -040045 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 Bishop49aefb32016-10-19 11:54:14 -040052
Brad Bishop615b2a82018-03-29 10:32:41 -040053 /** @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 Bishop49aefb32016-10-19 11:54:14 -040062
Brad Bishop615b2a82018-03-29 10:32:41 -040063 using EventInfo =
64 std::tuple<std::vector<EventBasePtr>, std::vector<Action>>;
Brad Bishop49aefb32016-10-19 11:54:14 -040065
Brad Bishop615b2a82018-03-29 10:32:41 -040066 /** @brief Start processing DBus messages. */
67 void run() noexcept;
Brad Bishop49aefb32016-10-19 11:54:14 -040068
Brad Bishop615b2a82018-03-29 10:32:41 -040069 /** @brief Provided for testing only. */
70 void shutdown() noexcept;
Brad Bishop49aefb32016-10-19 11:54:14 -040071
Brad Bishop615b2a82018-03-29 10:32:41 -040072 /** @brief sd_bus Notify method implementation callback. */
73 void
74 notify(std::map<sdbusplus::message::object_path, Object> objs) override;
Brad Bishop49aefb32016-10-19 11:54:14 -040075
Brad Bishop615b2a82018-03-29 10:32:41 -040076 /** @brief Event processing entry point. */
77 void handleEvent(sdbusplus::message::message&, const Event& event,
78 const EventInfo& info);
Brad Bishop49aefb32016-10-19 11:54:14 -040079
Brad Bishop615b2a82018-03-29 10:32:41 -040080 /** @brief Drop one or more objects from DBus. */
81 void destroyObjects(const std::vector<const char*>& paths);
Brad Bishop656a7d02016-10-19 22:20:02 -040082
Brad Bishop615b2a82018-03-29 10:32:41 -040083 /** @brief Add objects to DBus. */
84 void createObjects(
85 const std::map<sdbusplus::message::object_path, Object>& objs);
Brad Bishopeb68a682017-01-22 00:58:54 -050086
Brad Bishop615b2a82018-03-29 10:32:41 -040087 /** @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 Kodihallib28990f2017-08-08 07:19:34 -050091
Brad Bishop615b2a82018-03-29 10:32:41 -040092 /** @brief Restore persistent inventory items */
93 void restore();
Brad Bishop79ccaf72017-01-22 16:00:50 -050094
Brad Bishop615b2a82018-03-29 10:32:41 -040095 /** @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 Bishopda649b12016-11-30 14:35:02 -0500120
Brad Bishop615b2a82018-03-29 10:32:41 -0400121 using SigArgs = std::vector<std::unique_ptr<
122 std::tuple<Manager*, const DbusSignal*, const EventInfo*>>>;
123 using SigArg = SigArgs::value_type::element_type;
Brad Bishop49aefb32016-10-19 11:54:14 -0400124
Brad Bishop615b2a82018-03-29 10:32:41 -0400125 private:
Brad Bishop25d54b52018-11-21 12:57:51 -0500126 using InterfaceComposite = std::map<std::string, std::any>;
Brad Bishop615b2a82018-03-29 10:32:41 -0400127 using ObjectReferences = std::map<std::string, InterfaceComposite>;
128 using Events = std::vector<EventInfo>;
Brad Bishop90c30bc2017-01-22 16:40:47 -0500129
Brad Bishop615b2a82018-03-29 10:32:41 -0400130 // The int instantiations are safe since the signature of these
131 // functions don't change from one instantiation to the next.
Brad Bishop615b2a82018-03-29 10:32:41 -0400132 using Makers =
Brad Bishop02763c62018-12-12 22:02:07 -0500133 std::map<std::string, std::tuple<MakeInterfaceType, AssignInterfaceType,
134 SerializeInterfaceType<SerialOps>,
135 DeserializeInterfaceType<SerialOps>>>;
Brad Bishop49aefb32016-10-19 11:54:14 -0400136
Brad Bishop615b2a82018-03-29 10:32:41 -0400137 /** @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 Bishop25d54b52018-11-21 12:57:51 -0500149 const std::any& getInterfaceHolder(const char*, const char*) const;
150 std::any& getInterfaceHolder(const char*, const char*);
Brad Bishopb83a21e2016-11-30 13:43:37 -0500151
Brad Bishop615b2a82018-03-29 10:32:41 -0400152 /** @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 Bishop25d54b52018-11-21 12:57:51 -0500166 return *std::any_cast<std::shared_ptr<T>&>(holder);
Brad Bishop615b2a82018-03-29 10:32:41 -0400167 }
168 template <typename T>
169 auto& getInterface(const char* path, const char* interface) const
170 {
171 auto& holder = getInterfaceHolder(path, interface);
Brad Bishop25d54b52018-11-21 12:57:51 -0500172 return *std::any_cast<T>(holder);
Brad Bishop615b2a82018-03-29 10:32:41 -0400173 }
Brad Bishopb83a21e2016-11-30 13:43:37 -0500174
Brad Bishop615b2a82018-03-29 10:32:41 -0400175 /** @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 Bishop79ccaf72017-01-22 16:00:50 -0500181
Brad Bishop615b2a82018-03-29 10:32:41 -0400182 /** @brief Provided for testing only. */
183 volatile bool _shutdown;
Brad Bishop49aefb32016-10-19 11:54:14 -0400184
Brad Bishop615b2a82018-03-29 10:32:41 -0400185 /** @brief Path prefix applied to any relative paths. */
186 const char* _root;
Brad Bishop49aefb32016-10-19 11:54:14 -0400187
Brad Bishop615b2a82018-03-29 10:32:41 -0400188 /** @brief A container of sdbusplus server interface references. */
189 ObjectReferences _refs;
Brad Bishop49aefb32016-10-19 11:54:14 -0400190
Brad Bishop615b2a82018-03-29 10:32:41 -0400191 /** @brief A container contexts for signal callbacks. */
192 SigArgs _sigargs;
Brad Bishop49aefb32016-10-19 11:54:14 -0400193
Brad Bishop615b2a82018-03-29 10:32:41 -0400194 /** @brief A container of sdbusplus signal matches. */
195 std::vector<sdbusplus::bus::match_t> _matches;
Brad Bishop49aefb32016-10-19 11:54:14 -0400196
Brad Bishop615b2a82018-03-29 10:32:41 -0400197 /** @brief Persistent sdbusplus DBus bus connection. */
198 sdbusplus::bus::bus _bus;
Brad Bishop49aefb32016-10-19 11:54:14 -0400199
Brad Bishop615b2a82018-03-29 10:32:41 -0400200 /** @brief sdbusplus org.freedesktop.DBus.ObjectManager reference. */
201 sdbusplus::server::manager::manager _manager;
Brad Bishop49aefb32016-10-19 11:54:14 -0400202
Brad Bishop615b2a82018-03-29 10:32:41 -0400203 /** @brief A container of pimgen generated events and responses. */
204 static const Events _events;
Brad Bishop5fbaa7f2016-10-31 10:42:41 -0500205
Brad Bishop615b2a82018-03-29 10:32:41 -0400206 /** @brief A container of pimgen generated factory methods. */
207 static const Makers _makers;
Brad Bishop49aefb32016-10-19 11:54:14 -0400208};
209
210} // namespace manager
211} // namespace inventory
212} // namespace phosphor
213
214// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4