blob: 5e0930bad075f8d9ac37afb0bf9ae1784243d94d [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{
Brad Bishop451f8d92016-11-21 14:15:19 -050019
20template <typename T>
Brad Bishopa5cc34c2017-02-03 20:57:36 -050021using ServerObject = T;
Brad Bishop451f8d92016-11-21 14:15:19 -050022
23using ManagerIface =
Brad Bishop9aa5e2f2017-01-15 19:45:40 -050024 sdbusplus::xyz::openbmc_project::Inventory::server::Manager;
Brad Bishop451f8d92016-11-21 14:15:19 -050025
Brad Bishop65ffffa2016-11-29 12:31:31 -050026/** @struct MakeInterface
27 * @brief Adapt an sdbusplus interface proxy.
28 *
29 * Template instances are builder functions that create
30 * adapted sdbusplus interface proxy interface objects.
31 *
32 * @tparam T - The type of the interface being adapted.
33 */
34template <typename T>
35struct MakeInterface
36{
Brad Bishop150147a2017-02-08 23:57:46 -050037 static any_ns::any make(
Brad Bishop90c30bc2017-01-22 16:40:47 -050038 sdbusplus::bus::bus& bus,
39 const char* path,
Brad Bishopa5cc34c2017-02-03 20:57:36 -050040 const Interface& props)
Brad Bishop65ffffa2016-11-29 12:31:31 -050041 {
Brad Bishop90c30bc2017-01-22 16:40:47 -050042 // TODO: pass props to import constructor...
Brad Bishop150147a2017-02-08 23:57:46 -050043 return any_ns::any(std::make_shared<T>(bus, path));
Brad Bishop65ffffa2016-11-29 12:31:31 -050044 }
45};
Brad Bishop49aefb32016-10-19 11:54:14 -040046
47/** @class Manager
48 * @brief OpenBMC inventory manager implementation.
49 *
50 * A concrete implementation for the xyz.openbmc_project.Inventory.Manager
51 * DBus API.
52 */
53class Manager final :
Brad Bishop12f8a3c2017-02-09 00:02:00 -050054 public ServerObject<ManagerIface>
Brad Bishop49aefb32016-10-19 11:54:14 -040055{
56 public:
Brad Bishop7b337772017-01-12 16:11:24 -050057 Manager() = delete;
58 Manager(const Manager&) = delete;
59 Manager& operator=(const Manager&) = delete;
60 Manager(Manager&&) = default;
61 Manager& operator=(Manager&&) = default;
62 ~Manager() = default;
Brad Bishop49aefb32016-10-19 11:54:14 -040063
Brad Bishop7b337772017-01-12 16:11:24 -050064 /** @brief Construct an inventory manager.
65 *
66 * @param[in] bus - An sdbusplus bus connection.
67 * @param[in] busname - The DBus busname to own.
68 * @param[in] root - The DBus path on which to implement
69 * an inventory manager.
70 * @param[in] iface - The DBus inventory interface to implement.
71 */
72 Manager(sdbusplus::bus::bus&&, const char*, const char*, const char*);
Brad Bishop49aefb32016-10-19 11:54:14 -040073
Brad Bishop7b337772017-01-12 16:11:24 -050074 using EventInfo = std::tuple <
Brad Bishop12f8a3c2017-02-09 00:02:00 -050075 std::vector<EventBasePtr>,
76 std::vector<Action >>;
Brad Bishop49aefb32016-10-19 11:54:14 -040077
Brad Bishop7b337772017-01-12 16:11:24 -050078 /** @brief Start processing DBus messages. */
79 void run() noexcept;
Brad Bishop49aefb32016-10-19 11:54:14 -040080
Brad Bishop7b337772017-01-12 16:11:24 -050081 /** @brief Provided for testing only. */
82 void shutdown() noexcept;
Brad Bishop49aefb32016-10-19 11:54:14 -040083
Brad Bishop7b337772017-01-12 16:11:24 -050084 /** @brief sd_bus Notify method implementation callback. */
Brad Bishop03f4cd92017-02-03 15:17:21 -050085 void notify(
86 std::map<sdbusplus::message::object_path, Object> objs) override;
Brad Bishop49aefb32016-10-19 11:54:14 -040087
Brad Bishop48547a82017-01-19 15:12:50 -050088 /** @brief Event processing entry point. */
89 void handleEvent(sdbusplus::message::message&,
Brad Bishop12f8a3c2017-02-09 00:02:00 -050090 const Event& event,
Brad Bishop48547a82017-01-19 15:12:50 -050091 const EventInfo& info);
Brad Bishop49aefb32016-10-19 11:54:14 -040092
Brad Bishop7b7e7122017-01-21 21:21:46 -050093 /** @brief Drop one or more objects from DBus. */
94 void destroyObjects(
95 const std::vector<const char*>& paths);
Brad Bishop656a7d02016-10-19 22:20:02 -040096
Brad Bishopeb68a682017-01-22 00:58:54 -050097 /** @brief Add objects to DBus. */
98 void createObjects(
99 const std::map<sdbusplus::message::object_path, Object>& objs);
100
Brad Bishop7b337772017-01-12 16:11:24 -0500101 /** @brief Invoke an sdbusplus server binding method.
102 *
103 * Invoke the requested method with a reference to the requested
104 * sdbusplus server binding interface as a parameter.
105 *
106 * @tparam T - The sdbusplus server binding interface type.
107 * @tparam U - The type of the sdbusplus server binding member.
108 * @tparam Args - Argument types of the binding member.
109 *
110 * @param[in] path - The DBus path on which the method should
111 * be invoked.
112 * @param[in] interface - The DBus interface hosting the method.
113 * @param[in] member - Pointer to sdbusplus server binding member.
114 * @param[in] args - Arguments to forward to the binding member.
115 *
116 * @returns - The return/value type of the binding method being
117 * called.
118 */
119 template<typename T, typename U, typename ...Args>
120 decltype(auto) invokeMethod(const char* path, const char* interface,
121 U&& member, Args&& ...args)
122 {
Brad Bishop150147a2017-02-08 23:57:46 -0500123 auto& iface = getInterface<T>(path, interface);
Brad Bishop7b337772017-01-12 16:11:24 -0500124 return (iface.*member)(std::forward<Args>(args)...);
125 }
Brad Bishopda649b12016-11-30 14:35:02 -0500126
Brad Bishop7b337772017-01-12 16:11:24 -0500127 using SigArgs = std::vector <
128 std::unique_ptr <
129 std::tuple <
130 Manager*,
Brad Bishop12f8a3c2017-02-09 00:02:00 -0500131 const DbusSignal*,
Brad Bishop7b337772017-01-12 16:11:24 -0500132 const EventInfo* >>>;
133 using SigArg = SigArgs::value_type::element_type;
Brad Bishop49aefb32016-10-19 11:54:14 -0400134
135 private:
Brad Bishop150147a2017-02-08 23:57:46 -0500136 using InterfaceComposite = std::map<std::string, any_ns::any>;
Brad Bishop7b337772017-01-12 16:11:24 -0500137 using ObjectReferences = std::map<std::string, InterfaceComposite>;
138 using Events = std::vector<EventInfo>;
Brad Bishop90c30bc2017-01-22 16:40:47 -0500139
140 // The int instantiation is safe since the signature of these
141 // functions don't change from one instantiation to the next.
142 using MakerType = std::add_pointer_t <
Brad Bishop12f8a3c2017-02-09 00:02:00 -0500143 decltype(MakeInterface<int>::make) >;
Brad Bishop90c30bc2017-01-22 16:40:47 -0500144 using Makers = std::map<std::string, std::tuple<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 */
Brad Bishop150147a2017-02-08 23:57:46 -0500158 const any_ns::any& getInterfaceHolder(
Brad Bishop7b337772017-01-12 16:11:24 -0500159 const char*, const char*) const;
Brad Bishop150147a2017-02-08 23:57:46 -0500160 any_ns::any& getInterfaceHolder(
Brad Bishop7b337772017-01-12 16:11:24 -0500161 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);
Brad Bishop150147a2017-02-08 23:57:46 -0500177 return *any_ns::any_cast<std::shared_ptr<T> &>(holder);
Brad Bishop7b337772017-01-12 16:11:24 -0500178 }
179 template<typename T>
180 auto& getInterface(const char* path, const char* interface) const
181 {
182 auto& holder = getInterfaceHolder(path, interface);
Brad Bishop150147a2017-02-08 23:57:46 -0500183 return *any_ns::any_cast<T>(holder);
Brad Bishop7b337772017-01-12 16:11:24 -0500184 }
Brad Bishopb83a21e2016-11-30 13:43:37 -0500185
Brad Bishop7b337772017-01-12 16:11:24 -0500186 /** @brief Provided for testing only. */
Brad Bishopb9b929b2017-01-13 16:33:06 -0500187 volatile bool _shutdown;
Brad Bishop49aefb32016-10-19 11:54:14 -0400188
Brad Bishop7b337772017-01-12 16:11:24 -0500189 /** @brief Path prefix applied to any relative paths. */
190 const char* _root;
Brad Bishop49aefb32016-10-19 11:54:14 -0400191
Brad Bishop7b337772017-01-12 16:11:24 -0500192 /** @brief A container of sdbusplus server interface references. */
193 ObjectReferences _refs;
Brad Bishop49aefb32016-10-19 11:54:14 -0400194
Brad Bishop7b337772017-01-12 16:11:24 -0500195 /** @brief A container contexts for signal callbacks. */
196 SigArgs _sigargs;
Brad Bishop49aefb32016-10-19 11:54:14 -0400197
Brad Bishop7b337772017-01-12 16:11:24 -0500198 /** @brief A container of sdbusplus signal matches. */
199 std::vector<sdbusplus::server::match::match> _matches;
Brad Bishop49aefb32016-10-19 11:54:14 -0400200
Brad Bishop7b337772017-01-12 16:11:24 -0500201 /** @brief Persistent sdbusplus DBus bus connection. */
202 sdbusplus::bus::bus _bus;
Brad Bishop49aefb32016-10-19 11:54:14 -0400203
Brad Bishop7b337772017-01-12 16:11:24 -0500204 /** @brief sdbusplus org.freedesktop.DBus.ObjectManager reference. */
205 sdbusplus::server::manager::manager _manager;
Brad Bishop49aefb32016-10-19 11:54:14 -0400206
Brad Bishop7b337772017-01-12 16:11:24 -0500207 /** @brief A container of pimgen generated events and responses. */
208 static const Events _events;
Brad Bishop5fbaa7f2016-10-31 10:42:41 -0500209
Brad Bishop7b337772017-01-12 16:11:24 -0500210 /** @brief A container of pimgen generated factory methods. */
211 static const Makers _makers;
Brad Bishop49aefb32016-10-19 11:54:14 -0400212};
213
214} // namespace manager
215} // namespace inventory
216} // namespace phosphor
217
218// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4