blob: 8b59c1b1a8fc79f124e39c021ed16c8929295f32 [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 Bishop03f4cd92017-02-03 15:17:21 -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 Bishop90c30bc2017-01-22 16:40:47 -050039 static std::unique_ptr<details::holder::Base> make(
40 sdbusplus::bus::bus& bus,
41 const char* path,
42 const Interface& props,
43 bool deferSignals)
Brad Bishop65ffffa2016-11-29 12:31:31 -050044 {
Brad Bishop90c30bc2017-01-22 16:40:47 -050045 // TODO: pass props to import constructor...
Brad Bishop65ffffa2016-11-29 12:31:31 -050046 using HolderType = holder::Holder<std::unique_ptr<T>>;
Brad Bishop90c30bc2017-01-22 16:40:47 -050047 return HolderType::template make_unique<HolderType>(
48 std::forward<std::unique_ptr<T>>(
49 std::make_unique<T>(
50 std::forward<decltype(bus)>(bus),
51 std::forward<decltype(path)>(path),
52 std::forward<decltype(deferSignals)>(deferSignals))));
Brad Bishop65ffffa2016-11-29 12:31:31 -050053 }
54};
Brad Bishop49aefb32016-10-19 11:54:14 -040055} // namespace details
56
57/** @class Manager
58 * @brief OpenBMC inventory manager implementation.
59 *
60 * A concrete implementation for the xyz.openbmc_project.Inventory.Manager
61 * DBus API.
62 */
63class Manager final :
Brad Bishop451f8d92016-11-21 14:15:19 -050064 public details::ServerObject<details::ManagerIface>
Brad Bishop49aefb32016-10-19 11:54:14 -040065{
66 public:
Brad Bishop7b337772017-01-12 16:11:24 -050067 Manager() = delete;
68 Manager(const Manager&) = delete;
69 Manager& operator=(const Manager&) = delete;
70 Manager(Manager&&) = default;
71 Manager& operator=(Manager&&) = default;
72 ~Manager() = default;
Brad Bishop49aefb32016-10-19 11:54:14 -040073
Brad Bishop7b337772017-01-12 16:11:24 -050074 /** @brief Construct an inventory manager.
75 *
76 * @param[in] bus - An sdbusplus bus connection.
77 * @param[in] busname - The DBus busname to own.
78 * @param[in] root - The DBus path on which to implement
79 * an inventory manager.
80 * @param[in] iface - The DBus inventory interface to implement.
81 */
82 Manager(sdbusplus::bus::bus&&, const char*, const char*, const char*);
Brad Bishop49aefb32016-10-19 11:54:14 -040083
Brad Bishop7b337772017-01-12 16:11:24 -050084 using EventInfo = std::tuple <
85 std::vector<details::EventBasePtr>,
86 std::vector<details::ActionBasePtr >>;
Brad Bishop49aefb32016-10-19 11:54:14 -040087
Brad Bishop7b337772017-01-12 16:11:24 -050088 /** @brief Start processing DBus messages. */
89 void run() noexcept;
Brad Bishop49aefb32016-10-19 11:54:14 -040090
Brad Bishop7b337772017-01-12 16:11:24 -050091 /** @brief Provided for testing only. */
92 void shutdown() noexcept;
Brad Bishop49aefb32016-10-19 11:54:14 -040093
Brad Bishop7b337772017-01-12 16:11:24 -050094 /** @brief sd_bus Notify method implementation callback. */
Brad Bishop03f4cd92017-02-03 15:17:21 -050095 void notify(
96 std::map<sdbusplus::message::object_path, Object> objs) override;
Brad Bishop49aefb32016-10-19 11:54:14 -040097
Brad Bishop48547a82017-01-19 15:12:50 -050098 /** @brief Event processing entry point. */
99 void handleEvent(sdbusplus::message::message&,
100 const details::Event& event,
101 const EventInfo& info);
Brad Bishop49aefb32016-10-19 11:54:14 -0400102
Brad Bishop7b7e7122017-01-21 21:21:46 -0500103 /** @brief Drop one or more objects from DBus. */
104 void destroyObjects(
105 const std::vector<const char*>& paths);
Brad Bishop656a7d02016-10-19 22:20:02 -0400106
Brad Bishopeb68a682017-01-22 00:58:54 -0500107 /** @brief Add objects to DBus. */
108 void createObjects(
109 const std::map<sdbusplus::message::object_path, Object>& objs);
110
Brad Bishop7b337772017-01-12 16:11:24 -0500111 /** @brief Invoke an sdbusplus server binding method.
112 *
113 * Invoke the requested method with a reference to the requested
114 * sdbusplus server binding interface as a parameter.
115 *
116 * @tparam T - The sdbusplus server binding interface type.
117 * @tparam U - The type of the sdbusplus server binding member.
118 * @tparam Args - Argument types of the binding member.
119 *
120 * @param[in] path - The DBus path on which the method should
121 * be invoked.
122 * @param[in] interface - The DBus interface hosting the method.
123 * @param[in] member - Pointer to sdbusplus server binding member.
124 * @param[in] args - Arguments to forward to the binding member.
125 *
126 * @returns - The return/value type of the binding method being
127 * called.
128 */
129 template<typename T, typename U, typename ...Args>
130 decltype(auto) invokeMethod(const char* path, const char* interface,
131 U&& member, Args&& ...args)
132 {
133 auto& holder = getInterface<std::unique_ptr<T>>(path, interface);
134 auto& iface = *holder.get();
135 return (iface.*member)(std::forward<Args>(args)...);
136 }
Brad Bishopda649b12016-11-30 14:35:02 -0500137
Brad Bishop7b337772017-01-12 16:11:24 -0500138 using SigArgs = std::vector <
139 std::unique_ptr <
140 std::tuple <
141 Manager*,
142 const details::DbusSignal*,
143 const EventInfo* >>>;
144 using SigArg = SigArgs::value_type::element_type;
Brad Bishop49aefb32016-10-19 11:54:14 -0400145
146 private:
Brad Bishop7b337772017-01-12 16:11:24 -0500147 using HolderPtr = std::unique_ptr<details::holder::Base>;
148 using InterfaceComposite = std::map<std::string, HolderPtr>;
149 using ObjectReferences = std::map<std::string, InterfaceComposite>;
150 using Events = std::vector<EventInfo>;
Brad Bishop90c30bc2017-01-22 16:40:47 -0500151
152 // The int instantiation is safe since the signature of these
153 // functions don't change from one instantiation to the next.
154 using MakerType = std::add_pointer_t <
155 decltype(details::MakeInterface<int>::make) >;
156 using Makers = std::map<std::string, std::tuple<MakerType>>;
Brad Bishop49aefb32016-10-19 11:54:14 -0400157
Brad Bishop7b337772017-01-12 16:11:24 -0500158 /** @brief Provides weak references to interface holders.
159 *
160 * Common code for all types for the templated getInterface
161 * methods.
162 *
163 * @param[in] path - The DBus path for which the interface
164 * holder instance should be provided.
165 * @param[in] interface - The DBus interface for which the
166 * holder instance should be provided.
167 *
168 * @returns A weak reference to the holder instance.
169 */
170 details::holder::Base& getInterfaceHolder(
171 const char*, const char*) const;
172 details::holder::Base& getInterfaceHolder(
173 const char*, const char*);
Brad Bishopb83a21e2016-11-30 13:43:37 -0500174
Brad Bishop7b337772017-01-12 16:11:24 -0500175 /** @brief Provides weak references to interface holders.
176 *
177 * @tparam T - The sdbusplus server binding interface type.
178 *
179 * @param[in] path - The DBus path for which the interface
180 * should be provided.
181 * @param[in] interface - The DBus interface to obtain.
182 *
183 * @returns A weak reference to the interface holder.
184 */
185 template<typename T>
186 auto& getInterface(const char* path, const char* interface)
187 {
188 auto& holder = getInterfaceHolder(path, interface);
189 return static_cast <
190 details::holder::Holder<T>& >(holder);
191 }
192 template<typename T>
193 auto& getInterface(const char* path, const char* interface) const
194 {
195 auto& holder = getInterfaceHolder(path, interface);
196 return static_cast <
197 const details::holder::Holder<T>& >(holder);
198 }
Brad Bishopb83a21e2016-11-30 13:43:37 -0500199
Brad Bishop7b337772017-01-12 16:11:24 -0500200 /** @brief Provided for testing only. */
Brad Bishopb9b929b2017-01-13 16:33:06 -0500201 volatile bool _shutdown;
Brad Bishop49aefb32016-10-19 11:54:14 -0400202
Brad Bishop7b337772017-01-12 16:11:24 -0500203 /** @brief Path prefix applied to any relative paths. */
204 const char* _root;
Brad Bishop49aefb32016-10-19 11:54:14 -0400205
Brad Bishop7b337772017-01-12 16:11:24 -0500206 /** @brief A container of sdbusplus server interface references. */
207 ObjectReferences _refs;
Brad Bishop49aefb32016-10-19 11:54:14 -0400208
Brad Bishop7b337772017-01-12 16:11:24 -0500209 /** @brief A container contexts for signal callbacks. */
210 SigArgs _sigargs;
Brad Bishop49aefb32016-10-19 11:54:14 -0400211
Brad Bishop7b337772017-01-12 16:11:24 -0500212 /** @brief A container of sdbusplus signal matches. */
213 std::vector<sdbusplus::server::match::match> _matches;
Brad Bishop49aefb32016-10-19 11:54:14 -0400214
Brad Bishop7b337772017-01-12 16:11:24 -0500215 /** @brief Persistent sdbusplus DBus bus connection. */
216 sdbusplus::bus::bus _bus;
Brad Bishop49aefb32016-10-19 11:54:14 -0400217
Brad Bishop7b337772017-01-12 16:11:24 -0500218 /** @brief sdbusplus org.freedesktop.DBus.ObjectManager reference. */
219 sdbusplus::server::manager::manager _manager;
Brad Bishop49aefb32016-10-19 11:54:14 -0400220
Brad Bishop7b337772017-01-12 16:11:24 -0500221 /** @brief A container of pimgen generated events and responses. */
222 static const Events _events;
Brad Bishop5fbaa7f2016-10-31 10:42:41 -0500223
Brad Bishop7b337772017-01-12 16:11:24 -0500224 /** @brief A container of pimgen generated factory methods. */
225 static const Makers _makers;
Brad Bishop49aefb32016-10-19 11:54:14 -0400226};
227
228} // namespace manager
229} // namespace inventory
230} // namespace phosphor
231
232// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4