blob: bd12ef60ab0ddb45ed87c6dc22de6ec0fe5abf83 [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 Bishop7b7e7122017-01-21 21:21:46 -0500100 /** @brief Drop one or more objects from DBus. */
101 void destroyObjects(
102 const std::vector<const char*>& paths);
Brad Bishop656a7d02016-10-19 22:20:02 -0400103
Brad Bishop7b337772017-01-12 16:11:24 -0500104 /** @brief Invoke an sdbusplus server binding method.
105 *
106 * Invoke the requested method with a reference to the requested
107 * sdbusplus server binding interface as a parameter.
108 *
109 * @tparam T - The sdbusplus server binding interface type.
110 * @tparam U - The type of the sdbusplus server binding member.
111 * @tparam Args - Argument types of the binding member.
112 *
113 * @param[in] path - The DBus path on which the method should
114 * be invoked.
115 * @param[in] interface - The DBus interface hosting the method.
116 * @param[in] member - Pointer to sdbusplus server binding member.
117 * @param[in] args - Arguments to forward to the binding member.
118 *
119 * @returns - The return/value type of the binding method being
120 * called.
121 */
122 template<typename T, typename U, typename ...Args>
123 decltype(auto) invokeMethod(const char* path, const char* interface,
124 U&& member, Args&& ...args)
125 {
126 auto& holder = getInterface<std::unique_ptr<T>>(path, interface);
127 auto& iface = *holder.get();
128 return (iface.*member)(std::forward<Args>(args)...);
129 }
Brad Bishopda649b12016-11-30 14:35:02 -0500130
Brad Bishop7b337772017-01-12 16:11:24 -0500131 using SigArgs = std::vector <
132 std::unique_ptr <
133 std::tuple <
134 Manager*,
135 const details::DbusSignal*,
136 const EventInfo* >>>;
137 using SigArg = SigArgs::value_type::element_type;
Brad Bishop49aefb32016-10-19 11:54:14 -0400138
139 private:
Brad Bishop7b337772017-01-12 16:11:24 -0500140 using HolderPtr = std::unique_ptr<details::holder::Base>;
141 using InterfaceComposite = std::map<std::string, HolderPtr>;
142 using ObjectReferences = std::map<std::string, InterfaceComposite>;
143 using Events = std::vector<EventInfo>;
144 using MakerType = HolderPtr(*)(
Brad Bishop6676c122017-01-15 20:38:39 -0500145 sdbusplus::bus::bus&, const char*, bool);
Brad Bishop7b337772017-01-12 16:11:24 -0500146 using Makers = std::map<std::string, MakerType>;
Brad Bishop49aefb32016-10-19 11:54:14 -0400147
Brad Bishop7b337772017-01-12 16:11:24 -0500148 /** @brief Provides weak references to interface holders.
149 *
150 * Common code for all types for the templated getInterface
151 * methods.
152 *
153 * @param[in] path - The DBus path for which the interface
154 * holder instance should be provided.
155 * @param[in] interface - The DBus interface for which the
156 * holder instance should be provided.
157 *
158 * @returns A weak reference to the holder instance.
159 */
160 details::holder::Base& getInterfaceHolder(
161 const char*, const char*) const;
162 details::holder::Base& getInterfaceHolder(
163 const char*, const char*);
Brad Bishopb83a21e2016-11-30 13:43:37 -0500164
Brad Bishop7b337772017-01-12 16:11:24 -0500165 /** @brief Provides weak references to interface holders.
166 *
167 * @tparam T - The sdbusplus server binding interface type.
168 *
169 * @param[in] path - The DBus path for which the interface
170 * should be provided.
171 * @param[in] interface - The DBus interface to obtain.
172 *
173 * @returns A weak reference to the interface holder.
174 */
175 template<typename T>
176 auto& getInterface(const char* path, const char* interface)
177 {
178 auto& holder = getInterfaceHolder(path, interface);
179 return static_cast <
180 details::holder::Holder<T>& >(holder);
181 }
182 template<typename T>
183 auto& getInterface(const char* path, const char* interface) const
184 {
185 auto& holder = getInterfaceHolder(path, interface);
186 return static_cast <
187 const details::holder::Holder<T>& >(holder);
188 }
Brad Bishopb83a21e2016-11-30 13:43:37 -0500189
Brad Bishop7b337772017-01-12 16:11:24 -0500190 /** @brief Provided for testing only. */
Brad Bishopb9b929b2017-01-13 16:33:06 -0500191 volatile bool _shutdown;
Brad Bishop49aefb32016-10-19 11:54:14 -0400192
Brad Bishop7b337772017-01-12 16:11:24 -0500193 /** @brief Path prefix applied to any relative paths. */
194 const char* _root;
Brad Bishop49aefb32016-10-19 11:54:14 -0400195
Brad Bishop7b337772017-01-12 16:11:24 -0500196 /** @brief A container of sdbusplus server interface references. */
197 ObjectReferences _refs;
Brad Bishop49aefb32016-10-19 11:54:14 -0400198
Brad Bishop7b337772017-01-12 16:11:24 -0500199 /** @brief A container contexts for signal callbacks. */
200 SigArgs _sigargs;
Brad Bishop49aefb32016-10-19 11:54:14 -0400201
Brad Bishop7b337772017-01-12 16:11:24 -0500202 /** @brief A container of sdbusplus signal matches. */
203 std::vector<sdbusplus::server::match::match> _matches;
Brad Bishop49aefb32016-10-19 11:54:14 -0400204
Brad Bishop7b337772017-01-12 16:11:24 -0500205 /** @brief Persistent sdbusplus DBus bus connection. */
206 sdbusplus::bus::bus _bus;
Brad Bishop49aefb32016-10-19 11:54:14 -0400207
Brad Bishop7b337772017-01-12 16:11:24 -0500208 /** @brief sdbusplus org.freedesktop.DBus.ObjectManager reference. */
209 sdbusplus::server::manager::manager _manager;
Brad Bishop49aefb32016-10-19 11:54:14 -0400210
Brad Bishop7b337772017-01-12 16:11:24 -0500211 /** @brief A container of pimgen generated events and responses. */
212 static const Events _events;
Brad Bishop5fbaa7f2016-10-31 10:42:41 -0500213
Brad Bishop7b337772017-01-12 16:11:24 -0500214 /** @brief A container of pimgen generated factory methods. */
215 static const Makers _makers;
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