blob: 2ffa47b0c0a5bbb5286e7d4982d7284074f61e50 [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 Bishop49aefb32016-10-19 11:54:14 -040011
12namespace phosphor
13{
14namespace inventory
15{
16namespace manager
17{
18namespace details
19{
Brad Bishop451f8d92016-11-21 14:15:19 -050020
21template <typename T>
22using ServerObject = typename sdbusplus::server::object::object<T>;
23
24using ManagerIface =
Brad Bishop9aa5e2f2017-01-15 19:45:40 -050025 sdbusplus::xyz::openbmc_project::Inventory::server::Manager;
Brad Bishop451f8d92016-11-21 14:15:19 -050026
Brad Bishop65ffffa2016-11-29 12:31:31 -050027/** @struct MakeInterface
28 * @brief Adapt an sdbusplus interface proxy.
29 *
30 * Template instances are builder functions that create
31 * adapted sdbusplus interface proxy interface objects.
32 *
33 * @tparam T - The type of the interface being adapted.
34 */
35template <typename T>
36struct MakeInterface
37{
Brad Bishop6676c122017-01-15 20:38:39 -050038 static auto make(sdbusplus::bus::bus& bus, const char* path, bool deferSignals)
Brad Bishop65ffffa2016-11-29 12:31:31 -050039 {
40 using HolderType = holder::Holder<std::unique_ptr<T>>;
41 return static_cast<std::unique_ptr<holder::Base>>(
Brad Bishop7b337772017-01-12 16:11:24 -050042 HolderType::template make_unique<HolderType>(
43 std::forward<std::unique_ptr<T>>(
44 std::make_unique<T>(
45 std::forward<decltype(bus)>(bus),
Brad Bishop6676c122017-01-15 20:38:39 -050046 std::forward<decltype(path)>(path),
47 std::forward<decltype(deferSignals)>(deferSignals)))));
Brad Bishop65ffffa2016-11-29 12:31:31 -050048 }
49};
Brad Bishop49aefb32016-10-19 11:54:14 -040050} // namespace details
51
52/** @class Manager
53 * @brief OpenBMC inventory manager implementation.
54 *
55 * A concrete implementation for the xyz.openbmc_project.Inventory.Manager
56 * DBus API.
57 */
58class Manager final :
Brad Bishop451f8d92016-11-21 14:15:19 -050059 public details::ServerObject<details::ManagerIface>
Brad Bishop49aefb32016-10-19 11:54:14 -040060{
61 public:
Brad Bishop7b337772017-01-12 16:11:24 -050062 Manager() = delete;
63 Manager(const Manager&) = delete;
64 Manager& operator=(const Manager&) = delete;
65 Manager(Manager&&) = default;
66 Manager& operator=(Manager&&) = default;
67 ~Manager() = default;
Brad Bishop49aefb32016-10-19 11:54:14 -040068
Brad Bishop7b337772017-01-12 16:11:24 -050069 /** @brief Construct an inventory manager.
70 *
71 * @param[in] bus - An sdbusplus bus connection.
72 * @param[in] busname - The DBus busname to own.
73 * @param[in] root - The DBus path on which to implement
74 * an inventory manager.
75 * @param[in] iface - The DBus inventory interface to implement.
76 */
77 Manager(sdbusplus::bus::bus&&, const char*, const char*, const char*);
Brad Bishop49aefb32016-10-19 11:54:14 -040078
Brad Bishop7b337772017-01-12 16:11:24 -050079 using Object = std::map <
80 std::string, std::map <
Brad Bishop9aa5e2f2017-01-15 19:45:40 -050081 std::string, sdbusplus::message::variant<int64_t, std::string >>>;
Brad Bishop7b337772017-01-12 16:11:24 -050082 using EventInfo = std::tuple <
83 std::vector<details::EventBasePtr>,
84 std::vector<details::ActionBasePtr >>;
Brad Bishop49aefb32016-10-19 11:54:14 -040085
Brad Bishop7b337772017-01-12 16:11:24 -050086 /** @brief Start processing DBus messages. */
87 void run() noexcept;
Brad Bishop49aefb32016-10-19 11:54:14 -040088
Brad Bishop7b337772017-01-12 16:11:24 -050089 /** @brief Provided for testing only. */
90 void shutdown() noexcept;
Brad Bishop49aefb32016-10-19 11:54:14 -040091
Brad Bishop7b337772017-01-12 16:11:24 -050092 /** @brief sd_bus Notify method implementation callback. */
Brad Bishop9aa5e2f2017-01-15 19:45:40 -050093 void notify(sdbusplus::message::object_path, Object) override;
Brad Bishop49aefb32016-10-19 11:54:14 -040094
Brad Bishop7b337772017-01-12 16:11:24 -050095 /** @brief sd_bus signal callback. */
96 void signal(sdbusplus::message::message&,
97 const details::DbusSignal& event,
98 const EventInfo& info);
Brad Bishop49aefb32016-10-19 11:54:14 -040099
Brad Bishop7b337772017-01-12 16:11:24 -0500100 /** @brief Drop an object from DBus. */
101 void destroyObject(const char*);
Brad Bishop656a7d02016-10-19 22:20:02 -0400102
Brad Bishop7b337772017-01-12 16:11:24 -0500103 /** @brief Invoke an sdbusplus server binding method.
104 *
105 * Invoke the requested method with a reference to the requested
106 * sdbusplus server binding interface as a parameter.
107 *
108 * @tparam T - The sdbusplus server binding interface type.
109 * @tparam U - The type of the sdbusplus server binding member.
110 * @tparam Args - Argument types of the binding member.
111 *
112 * @param[in] path - The DBus path on which the method should
113 * be invoked.
114 * @param[in] interface - The DBus interface hosting the method.
115 * @param[in] member - Pointer to sdbusplus server binding member.
116 * @param[in] args - Arguments to forward to the binding member.
117 *
118 * @returns - The return/value type of the binding method being
119 * called.
120 */
121 template<typename T, typename U, typename ...Args>
122 decltype(auto) invokeMethod(const char* path, const char* interface,
123 U&& member, Args&& ...args)
124 {
125 auto& holder = getInterface<std::unique_ptr<T>>(path, interface);
126 auto& iface = *holder.get();
127 return (iface.*member)(std::forward<Args>(args)...);
128 }
Brad Bishopda649b12016-11-30 14:35:02 -0500129
Brad Bishop7b337772017-01-12 16:11:24 -0500130 using SigArgs = std::vector <
131 std::unique_ptr <
132 std::tuple <
133 Manager*,
134 const details::DbusSignal*,
135 const EventInfo* >>>;
136 using SigArg = SigArgs::value_type::element_type;
Brad Bishop49aefb32016-10-19 11:54:14 -0400137
138 private:
Brad Bishop7b337772017-01-12 16:11:24 -0500139 using HolderPtr = std::unique_ptr<details::holder::Base>;
140 using InterfaceComposite = std::map<std::string, HolderPtr>;
141 using ObjectReferences = std::map<std::string, InterfaceComposite>;
142 using Events = std::vector<EventInfo>;
143 using MakerType = HolderPtr(*)(
Brad Bishop6676c122017-01-15 20:38:39 -0500144 sdbusplus::bus::bus&, const char*, bool);
Brad Bishop7b337772017-01-12 16:11:24 -0500145 using Makers = std::map<std::string, MakerType>;
Brad Bishop49aefb32016-10-19 11:54:14 -0400146
Brad Bishop7b337772017-01-12 16:11:24 -0500147 /** @brief Provides weak references to interface holders.
148 *
149 * Common code for all types for the templated getInterface
150 * methods.
151 *
152 * @param[in] path - The DBus path for which the interface
153 * holder instance should be provided.
154 * @param[in] interface - The DBus interface for which the
155 * holder instance should be provided.
156 *
157 * @returns A weak reference to the holder instance.
158 */
159 details::holder::Base& getInterfaceHolder(
160 const char*, const char*) const;
161 details::holder::Base& getInterfaceHolder(
162 const char*, const char*);
Brad Bishopb83a21e2016-11-30 13:43:37 -0500163
Brad Bishop7b337772017-01-12 16:11:24 -0500164 /** @brief Provides weak references to interface holders.
165 *
166 * @tparam T - The sdbusplus server binding interface type.
167 *
168 * @param[in] path - The DBus path for which the interface
169 * should be provided.
170 * @param[in] interface - The DBus interface to obtain.
171 *
172 * @returns A weak reference to the interface holder.
173 */
174 template<typename T>
175 auto& getInterface(const char* path, const char* interface)
176 {
177 auto& holder = getInterfaceHolder(path, interface);
178 return static_cast <
179 details::holder::Holder<T>& >(holder);
180 }
181 template<typename T>
182 auto& getInterface(const char* path, const char* interface) const
183 {
184 auto& holder = getInterfaceHolder(path, interface);
185 return static_cast <
186 const details::holder::Holder<T>& >(holder);
187 }
Brad Bishopb83a21e2016-11-30 13:43:37 -0500188
Brad Bishop7b337772017-01-12 16:11:24 -0500189 /** @brief Provided for testing only. */
Brad Bishopb9b929b2017-01-13 16:33:06 -0500190 volatile bool _shutdown;
Brad Bishop49aefb32016-10-19 11:54:14 -0400191
Brad Bishop7b337772017-01-12 16:11:24 -0500192 /** @brief Path prefix applied to any relative paths. */
193 const char* _root;
Brad Bishop49aefb32016-10-19 11:54:14 -0400194
Brad Bishop7b337772017-01-12 16:11:24 -0500195 /** @brief A container of sdbusplus server interface references. */
196 ObjectReferences _refs;
Brad Bishop49aefb32016-10-19 11:54:14 -0400197
Brad Bishop7b337772017-01-12 16:11:24 -0500198 /** @brief A container contexts for signal callbacks. */
199 SigArgs _sigargs;
Brad Bishop49aefb32016-10-19 11:54:14 -0400200
Brad Bishop7b337772017-01-12 16:11:24 -0500201 /** @brief A container of sdbusplus signal matches. */
202 std::vector<sdbusplus::server::match::match> _matches;
Brad Bishop49aefb32016-10-19 11:54:14 -0400203
Brad Bishop7b337772017-01-12 16:11:24 -0500204 /** @brief Persistent sdbusplus DBus bus connection. */
205 sdbusplus::bus::bus _bus;
Brad Bishop49aefb32016-10-19 11:54:14 -0400206
Brad Bishop7b337772017-01-12 16:11:24 -0500207 /** @brief sdbusplus org.freedesktop.DBus.ObjectManager reference. */
208 sdbusplus::server::manager::manager _manager;
Brad Bishop49aefb32016-10-19 11:54:14 -0400209
Brad Bishop7b337772017-01-12 16:11:24 -0500210 /** @brief A container of pimgen generated events and responses. */
211 static const Events _events;
Brad Bishop5fbaa7f2016-10-31 10:42:41 -0500212
Brad Bishop7b337772017-01-12 16:11:24 -0500213 /** @brief A container of pimgen generated factory methods. */
214 static const Makers _makers;
Brad Bishop49aefb32016-10-19 11:54:14 -0400215};
216
217} // namespace manager
218} // namespace inventory
219} // namespace phosphor
220
221// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4