blob: 33980b9b5ec86746f2854c99808e1985b4ef661a [file] [log] [blame]
Brad Bishop49aefb32016-10-19 11:54:14 -04001#pragma once
2
3#include <map>
4#include <memory>
5#include <string>
6#include <vector>
7#include <sdbusplus/server.hpp>
Brad Bishope30dd772016-11-12 21:39:28 -05008#include "xyz/openbmc_project/Inventory/Manager/server.hpp"
Brad Bishop67b788d2016-11-29 13:09:01 -05009#include "events.hpp"
Brad Bishopc038e012016-10-19 13:02:24 -040010#include "actions.hpp"
Brad Bishop1157af12017-01-22 01:03:02 -050011#include "types.hpp"
Brad Bishop49aefb32016-10-19 11:54:14 -040012
13namespace phosphor
14{
15namespace inventory
16{
17namespace manager
18{
19namespace details
20{
Brad Bishop451f8d92016-11-21 14:15:19 -050021
22template <typename T>
23using ServerObject = typename sdbusplus::server::object::object<T>;
24
25using ManagerIface =
Brad Bishop9aa5e2f2017-01-15 19:45:40 -050026 sdbusplus::xyz::openbmc_project::Inventory::server::Manager;
Brad Bishop451f8d92016-11-21 14:15:19 -050027
Brad Bishop65ffffa2016-11-29 12:31:31 -050028/** @struct MakeInterface
29 * @brief Adapt an sdbusplus interface proxy.
30 *
31 * Template instances are builder functions that create
32 * adapted sdbusplus interface proxy interface objects.
33 *
34 * @tparam T - The type of the interface being adapted.
35 */
36template <typename T>
37struct MakeInterface
38{
Brad Bishop6676c122017-01-15 20:38:39 -050039 static auto make(sdbusplus::bus::bus& bus, const char* path, bool deferSignals)
Brad Bishop65ffffa2016-11-29 12:31:31 -050040 {
41 using HolderType = holder::Holder<std::unique_ptr<T>>;
42 return static_cast<std::unique_ptr<holder::Base>>(
Brad Bishop7b337772017-01-12 16:11:24 -050043 HolderType::template make_unique<HolderType>(
44 std::forward<std::unique_ptr<T>>(
45 std::make_unique<T>(
46 std::forward<decltype(bus)>(bus),
Brad Bishop6676c122017-01-15 20:38:39 -050047 std::forward<decltype(path)>(path),
48 std::forward<decltype(deferSignals)>(deferSignals)))));
Brad Bishop65ffffa2016-11-29 12:31:31 -050049 }
50};
Brad Bishop49aefb32016-10-19 11:54:14 -040051} // namespace details
52
53/** @class Manager
54 * @brief OpenBMC inventory manager implementation.
55 *
56 * A concrete implementation for the xyz.openbmc_project.Inventory.Manager
57 * DBus API.
58 */
59class Manager final :
Brad Bishop451f8d92016-11-21 14:15:19 -050060 public details::ServerObject<details::ManagerIface>
Brad Bishop49aefb32016-10-19 11:54:14 -040061{
62 public:
Brad Bishop7b337772017-01-12 16:11:24 -050063 Manager() = delete;
64 Manager(const Manager&) = delete;
65 Manager& operator=(const Manager&) = delete;
66 Manager(Manager&&) = default;
67 Manager& operator=(Manager&&) = default;
68 ~Manager() = default;
Brad Bishop49aefb32016-10-19 11:54:14 -040069
Brad Bishop7b337772017-01-12 16:11:24 -050070 /** @brief Construct an inventory manager.
71 *
72 * @param[in] bus - An sdbusplus bus connection.
73 * @param[in] busname - The DBus busname to own.
74 * @param[in] root - The DBus path on which to implement
75 * an inventory manager.
76 * @param[in] iface - The DBus inventory interface to implement.
77 */
78 Manager(sdbusplus::bus::bus&&, const char*, const char*, const char*);
Brad Bishop49aefb32016-10-19 11:54:14 -040079
Brad Bishop7b337772017-01-12 16:11:24 -050080 using EventInfo = std::tuple <
81 std::vector<details::EventBasePtr>,
82 std::vector<details::ActionBasePtr >>;
Brad Bishop49aefb32016-10-19 11:54:14 -040083
Brad Bishop7b337772017-01-12 16:11:24 -050084 /** @brief Start processing DBus messages. */
85 void run() noexcept;
Brad Bishop49aefb32016-10-19 11:54:14 -040086
Brad Bishop7b337772017-01-12 16:11:24 -050087 /** @brief Provided for testing only. */
88 void shutdown() noexcept;
Brad Bishop49aefb32016-10-19 11:54:14 -040089
Brad Bishop7b337772017-01-12 16:11:24 -050090 /** @brief sd_bus Notify method implementation callback. */
Brad Bishop9aa5e2f2017-01-15 19:45:40 -050091 void notify(sdbusplus::message::object_path, Object) override;
Brad Bishop49aefb32016-10-19 11:54:14 -040092
Brad Bishop48547a82017-01-19 15:12:50 -050093 /** @brief Event processing entry point. */
94 void handleEvent(sdbusplus::message::message&,
95 const details::Event& event,
96 const EventInfo& info);
Brad Bishop49aefb32016-10-19 11:54:14 -040097
Brad Bishop7b7e7122017-01-21 21:21:46 -050098 /** @brief Drop one or more objects from DBus. */
99 void destroyObjects(
100 const std::vector<const char*>& paths);
Brad Bishop656a7d02016-10-19 22:20:02 -0400101
Brad Bishop7b337772017-01-12 16:11:24 -0500102 /** @brief Invoke an sdbusplus server binding method.
103 *
104 * Invoke the requested method with a reference to the requested
105 * sdbusplus server binding interface as a parameter.
106 *
107 * @tparam T - The sdbusplus server binding interface type.
108 * @tparam U - The type of the sdbusplus server binding member.
109 * @tparam Args - Argument types of the binding member.
110 *
111 * @param[in] path - The DBus path on which the method should
112 * be invoked.
113 * @param[in] interface - The DBus interface hosting the method.
114 * @param[in] member - Pointer to sdbusplus server binding member.
115 * @param[in] args - Arguments to forward to the binding member.
116 *
117 * @returns - The return/value type of the binding method being
118 * called.
119 */
120 template<typename T, typename U, typename ...Args>
121 decltype(auto) invokeMethod(const char* path, const char* interface,
122 U&& member, Args&& ...args)
123 {
124 auto& holder = getInterface<std::unique_ptr<T>>(path, interface);
125 auto& iface = *holder.get();
126 return (iface.*member)(std::forward<Args>(args)...);
127 }
Brad Bishopda649b12016-11-30 14:35:02 -0500128
Brad Bishop7b337772017-01-12 16:11:24 -0500129 using SigArgs = std::vector <
130 std::unique_ptr <
131 std::tuple <
132 Manager*,
133 const details::DbusSignal*,
134 const EventInfo* >>>;
135 using SigArg = SigArgs::value_type::element_type;
Brad Bishop49aefb32016-10-19 11:54:14 -0400136
137 private:
Brad Bishop7b337772017-01-12 16:11:24 -0500138 using HolderPtr = std::unique_ptr<details::holder::Base>;
139 using InterfaceComposite = std::map<std::string, HolderPtr>;
140 using ObjectReferences = std::map<std::string, InterfaceComposite>;
141 using Events = std::vector<EventInfo>;
142 using MakerType = HolderPtr(*)(
Brad Bishop6676c122017-01-15 20:38:39 -0500143 sdbusplus::bus::bus&, const char*, bool);
Brad Bishop7b337772017-01-12 16:11:24 -0500144 using Makers = std::map<std::string, MakerType>;
Brad Bishop49aefb32016-10-19 11:54:14 -0400145
Brad Bishop7b337772017-01-12 16:11:24 -0500146 /** @brief Provides weak references to interface holders.
147 *
148 * Common code for all types for the templated getInterface
149 * methods.
150 *
151 * @param[in] path - The DBus path for which the interface
152 * holder instance should be provided.
153 * @param[in] interface - The DBus interface for which the
154 * holder instance should be provided.
155 *
156 * @returns A weak reference to the holder instance.
157 */
158 details::holder::Base& getInterfaceHolder(
159 const char*, const char*) const;
160 details::holder::Base& getInterfaceHolder(
161 const char*, const char*);
Brad Bishopb83a21e2016-11-30 13:43:37 -0500162
Brad Bishop7b337772017-01-12 16:11:24 -0500163 /** @brief Provides weak references to interface holders.
164 *
165 * @tparam T - The sdbusplus server binding interface type.
166 *
167 * @param[in] path - The DBus path for which the interface
168 * should be provided.
169 * @param[in] interface - The DBus interface to obtain.
170 *
171 * @returns A weak reference to the interface holder.
172 */
173 template<typename T>
174 auto& getInterface(const char* path, const char* interface)
175 {
176 auto& holder = getInterfaceHolder(path, interface);
177 return static_cast <
178 details::holder::Holder<T>& >(holder);
179 }
180 template<typename T>
181 auto& getInterface(const char* path, const char* interface) const
182 {
183 auto& holder = getInterfaceHolder(path, interface);
184 return static_cast <
185 const details::holder::Holder<T>& >(holder);
186 }
Brad Bishopb83a21e2016-11-30 13:43:37 -0500187
Brad Bishop7b337772017-01-12 16:11:24 -0500188 /** @brief Provided for testing only. */
Brad Bishopb9b929b2017-01-13 16:33:06 -0500189 volatile bool _shutdown;
Brad Bishop49aefb32016-10-19 11:54:14 -0400190
Brad Bishop7b337772017-01-12 16:11:24 -0500191 /** @brief Path prefix applied to any relative paths. */
192 const char* _root;
Brad Bishop49aefb32016-10-19 11:54:14 -0400193
Brad Bishop7b337772017-01-12 16:11:24 -0500194 /** @brief A container of sdbusplus server interface references. */
195 ObjectReferences _refs;
Brad Bishop49aefb32016-10-19 11:54:14 -0400196
Brad Bishop7b337772017-01-12 16:11:24 -0500197 /** @brief A container contexts for signal callbacks. */
198 SigArgs _sigargs;
Brad Bishop49aefb32016-10-19 11:54:14 -0400199
Brad Bishop7b337772017-01-12 16:11:24 -0500200 /** @brief A container of sdbusplus signal matches. */
201 std::vector<sdbusplus::server::match::match> _matches;
Brad Bishop49aefb32016-10-19 11:54:14 -0400202
Brad Bishop7b337772017-01-12 16:11:24 -0500203 /** @brief Persistent sdbusplus DBus bus connection. */
204 sdbusplus::bus::bus _bus;
Brad Bishop49aefb32016-10-19 11:54:14 -0400205
Brad Bishop7b337772017-01-12 16:11:24 -0500206 /** @brief sdbusplus org.freedesktop.DBus.ObjectManager reference. */
207 sdbusplus::server::manager::manager _manager;
Brad Bishop49aefb32016-10-19 11:54:14 -0400208
Brad Bishop7b337772017-01-12 16:11:24 -0500209 /** @brief A container of pimgen generated events and responses. */
210 static const Events _events;
Brad Bishop5fbaa7f2016-10-31 10:42:41 -0500211
Brad Bishop7b337772017-01-12 16:11:24 -0500212 /** @brief A container of pimgen generated factory methods. */
213 static const Makers _makers;
Brad Bishop49aefb32016-10-19 11:54:14 -0400214};
215
216} // namespace manager
217} // namespace inventory
218} // namespace phosphor
219
220// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4