blob: a94d5ea0f266aa4e4d7721cd137188fc43994e11 [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 Bishopc1f47982017-02-09 01:27:38 -050010#include "functor.hpp"
Brad Bishop1157af12017-01-22 01:03:02 -050011#include "types.hpp"
Deepak Kodihalli6620e982017-08-05 13:09:54 -050012#include "serialize.hpp"
Brad Bishop49aefb32016-10-19 11:54:14 -040013
14namespace phosphor
15{
16namespace inventory
17{
18namespace manager
19{
Brad Bishop451f8d92016-11-21 14:15:19 -050020
21template <typename T>
Brad Bishopa5cc34c2017-02-03 20:57:36 -050022using ServerObject = T;
Brad Bishop451f8d92016-11-21 14:15:19 -050023
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 Bishop79ccaf72017-01-22 16:00:50 -050027/** @struct PropertiesVariant
28 * @brief Wrapper for sdbusplus PropertiesVariant.
29 *
30 * A wrapper is useful since MakeInterface is instantiated with 'int'
31 * to deduce the return type of its methods, which does not depend
32 * on T.
33 *
34 * @tparam T - The sdbusplus server binding type.
35 */
36template <typename T, typename Enable = void>
37struct PropertiesVariant {};
38
39template <typename T>
40struct PropertiesVariant<T, typename std::enable_if<std::is_object<T>::value>::type>
41{
42 using Type = typename T::PropertiesVariant;
43};
44
45template <typename T>
46using PropertiesVariantType = typename PropertiesVariant<T>::Type;
47
Brad Bishop65ffffa2016-11-29 12:31:31 -050048/** @struct MakeInterface
49 * @brief Adapt an sdbusplus interface proxy.
50 *
51 * Template instances are builder functions that create
52 * adapted sdbusplus interface proxy interface objects.
53 *
54 * @tparam T - The type of the interface being adapted.
55 */
Brad Bishop79ccaf72017-01-22 16:00:50 -050056
Brad Bishop65ffffa2016-11-29 12:31:31 -050057template <typename T>
58struct MakeInterface
59{
Brad Bishop150147a2017-02-08 23:57:46 -050060 static any_ns::any make(
Brad Bishop90c30bc2017-01-22 16:40:47 -050061 sdbusplus::bus::bus& bus,
62 const char* path,
Brad Bishopa5cc34c2017-02-03 20:57:36 -050063 const Interface& props)
Brad Bishop65ffffa2016-11-29 12:31:31 -050064 {
Brad Bishop9bbfcb12017-02-04 10:51:06 -050065 using InterfaceVariant =
Brad Bishop79ccaf72017-01-22 16:00:50 -050066 std::map<std::string, PropertiesVariantType<T>>;
Brad Bishop9bbfcb12017-02-04 10:51:06 -050067
68 InterfaceVariant v;
69
70 for (const auto& p : props)
71 {
72 v.emplace(
73 p.first,
Brad Bishop79ccaf72017-01-22 16:00:50 -050074 convertVariant<PropertiesVariantType<T>>(p.second));
Brad Bishop9bbfcb12017-02-04 10:51:06 -050075 }
76
77 return any_ns::any(std::make_shared<T>(bus, path, v));
Brad Bishop65ffffa2016-11-29 12:31:31 -050078 }
Brad Bishop79ccaf72017-01-22 16:00:50 -050079
80 static void assign(const Interface& props, any_ns::any& holder)
81 {
82 auto& iface = *any_ns::any_cast<std::shared_ptr<T> &>(holder);
83 for (const auto& p : props)
84 {
85 iface.setPropertyByName(
86 p.first, convertVariant<PropertiesVariantType<T>>(p.second));
87 }
88 }
Deepak Kodihalli6620e982017-08-05 13:09:54 -050089
90 static void serialize(const std::string& path, const std::string& iface,
91 const any_ns::any& holder)
92 {
93 const auto& object =
94 *any_ns::any_cast<const std::shared_ptr<T> &>(holder);
95 cereal::serialize(path, iface, object);
96 }
Brad Bishop65ffffa2016-11-29 12:31:31 -050097};
Brad Bishop49aefb32016-10-19 11:54:14 -040098
99/** @class Manager
100 * @brief OpenBMC inventory manager implementation.
101 *
102 * A concrete implementation for the xyz.openbmc_project.Inventory.Manager
103 * DBus API.
104 */
105class Manager final :
Brad Bishop12f8a3c2017-02-09 00:02:00 -0500106 public ServerObject<ManagerIface>
Brad Bishop49aefb32016-10-19 11:54:14 -0400107{
108 public:
Brad Bishop7b337772017-01-12 16:11:24 -0500109 Manager() = delete;
110 Manager(const Manager&) = delete;
111 Manager& operator=(const Manager&) = delete;
112 Manager(Manager&&) = default;
113 Manager& operator=(Manager&&) = default;
114 ~Manager() = default;
Brad Bishop49aefb32016-10-19 11:54:14 -0400115
Brad Bishop7b337772017-01-12 16:11:24 -0500116 /** @brief Construct an inventory manager.
117 *
118 * @param[in] bus - An sdbusplus bus connection.
119 * @param[in] busname - The DBus busname to own.
120 * @param[in] root - The DBus path on which to implement
121 * an inventory manager.
122 * @param[in] iface - The DBus inventory interface to implement.
123 */
124 Manager(sdbusplus::bus::bus&&, const char*, const char*, const char*);
Brad Bishop49aefb32016-10-19 11:54:14 -0400125
Brad Bishop7b337772017-01-12 16:11:24 -0500126 using EventInfo = std::tuple <
Brad Bishop12f8a3c2017-02-09 00:02:00 -0500127 std::vector<EventBasePtr>,
128 std::vector<Action >>;
Brad Bishop49aefb32016-10-19 11:54:14 -0400129
Brad Bishop7b337772017-01-12 16:11:24 -0500130 /** @brief Start processing DBus messages. */
131 void run() noexcept;
Brad Bishop49aefb32016-10-19 11:54:14 -0400132
Brad Bishop7b337772017-01-12 16:11:24 -0500133 /** @brief Provided for testing only. */
134 void shutdown() noexcept;
Brad Bishop49aefb32016-10-19 11:54:14 -0400135
Brad Bishop7b337772017-01-12 16:11:24 -0500136 /** @brief sd_bus Notify method implementation callback. */
Brad Bishop03f4cd92017-02-03 15:17:21 -0500137 void notify(
138 std::map<sdbusplus::message::object_path, Object> objs) override;
Brad Bishop49aefb32016-10-19 11:54:14 -0400139
Brad Bishop48547a82017-01-19 15:12:50 -0500140 /** @brief Event processing entry point. */
141 void handleEvent(sdbusplus::message::message&,
Brad Bishop12f8a3c2017-02-09 00:02:00 -0500142 const Event& event,
Brad Bishop48547a82017-01-19 15:12:50 -0500143 const EventInfo& info);
Brad Bishop49aefb32016-10-19 11:54:14 -0400144
Brad Bishop7b7e7122017-01-21 21:21:46 -0500145 /** @brief Drop one or more objects from DBus. */
146 void destroyObjects(
147 const std::vector<const char*>& paths);
Brad Bishop656a7d02016-10-19 22:20:02 -0400148
Brad Bishopeb68a682017-01-22 00:58:54 -0500149 /** @brief Add objects to DBus. */
150 void createObjects(
151 const std::map<sdbusplus::message::object_path, Object>& objs);
152
Brad Bishop79ccaf72017-01-22 16:00:50 -0500153 /** @brief Add or update objects on DBus. */
154 void updateObjects(
155 const std::map<sdbusplus::message::object_path, Object>& objs);
156
Brad Bishop7b337772017-01-12 16:11:24 -0500157 /** @brief Invoke an sdbusplus server binding method.
158 *
159 * Invoke the requested method with a reference to the requested
160 * sdbusplus server binding interface as a parameter.
161 *
162 * @tparam T - The sdbusplus server binding interface type.
163 * @tparam U - The type of the sdbusplus server binding member.
164 * @tparam Args - Argument types of the binding member.
165 *
166 * @param[in] path - The DBus path on which the method should
167 * be invoked.
168 * @param[in] interface - The DBus interface hosting the method.
169 * @param[in] member - Pointer to sdbusplus server binding member.
170 * @param[in] args - Arguments to forward to the binding member.
171 *
172 * @returns - The return/value type of the binding method being
173 * called.
174 */
175 template<typename T, typename U, typename ...Args>
176 decltype(auto) invokeMethod(const char* path, const char* interface,
177 U&& member, Args&& ...args)
178 {
Brad Bishop150147a2017-02-08 23:57:46 -0500179 auto& iface = getInterface<T>(path, interface);
Brad Bishop7b337772017-01-12 16:11:24 -0500180 return (iface.*member)(std::forward<Args>(args)...);
181 }
Brad Bishopda649b12016-11-30 14:35:02 -0500182
Brad Bishop7b337772017-01-12 16:11:24 -0500183 using SigArgs = std::vector <
184 std::unique_ptr <
185 std::tuple <
186 Manager*,
Brad Bishop12f8a3c2017-02-09 00:02:00 -0500187 const DbusSignal*,
Brad Bishop7b337772017-01-12 16:11:24 -0500188 const EventInfo* >>>;
189 using SigArg = SigArgs::value_type::element_type;
Brad Bishop49aefb32016-10-19 11:54:14 -0400190
191 private:
Brad Bishop150147a2017-02-08 23:57:46 -0500192 using InterfaceComposite = std::map<std::string, any_ns::any>;
Brad Bishop7b337772017-01-12 16:11:24 -0500193 using ObjectReferences = std::map<std::string, InterfaceComposite>;
194 using Events = std::vector<EventInfo>;
Brad Bishop90c30bc2017-01-22 16:40:47 -0500195
Brad Bishop79ccaf72017-01-22 16:00:50 -0500196 // The int instantiations are safe since the signature of these
Brad Bishop90c30bc2017-01-22 16:40:47 -0500197 // functions don't change from one instantiation to the next.
198 using MakerType = std::add_pointer_t <
Brad Bishop12f8a3c2017-02-09 00:02:00 -0500199 decltype(MakeInterface<int>::make) >;
Brad Bishop79ccaf72017-01-22 16:00:50 -0500200 using AssignerType = std::add_pointer_t <
201 decltype(MakeInterface<int>::assign) >;
Deepak Kodihalli6620e982017-08-05 13:09:54 -0500202 using SerializerType = std::add_pointer_t <
203 decltype(MakeInterface<int>::serialize) >;
204 using Makers = std::map<std::string,
205 std::tuple<MakerType, AssignerType, SerializerType>>;
Brad Bishop49aefb32016-10-19 11:54:14 -0400206
Brad Bishop7b337772017-01-12 16:11:24 -0500207 /** @brief Provides weak references to interface holders.
208 *
209 * Common code for all types for the templated getInterface
210 * methods.
211 *
212 * @param[in] path - The DBus path for which the interface
213 * holder instance should be provided.
214 * @param[in] interface - The DBus interface for which the
215 * holder instance should be provided.
216 *
217 * @returns A weak reference to the holder instance.
218 */
Brad Bishop150147a2017-02-08 23:57:46 -0500219 const any_ns::any& getInterfaceHolder(
Brad Bishop7b337772017-01-12 16:11:24 -0500220 const char*, const char*) const;
Brad Bishop150147a2017-02-08 23:57:46 -0500221 any_ns::any& getInterfaceHolder(
Brad Bishop7b337772017-01-12 16:11:24 -0500222 const char*, const char*);
Brad Bishopb83a21e2016-11-30 13:43:37 -0500223
Brad Bishop7b337772017-01-12 16:11:24 -0500224 /** @brief Provides weak references to interface holders.
225 *
226 * @tparam T - The sdbusplus server binding interface type.
227 *
228 * @param[in] path - The DBus path for which the interface
229 * should be provided.
230 * @param[in] interface - The DBus interface to obtain.
231 *
232 * @returns A weak reference to the interface holder.
233 */
234 template<typename T>
235 auto& getInterface(const char* path, const char* interface)
236 {
237 auto& holder = getInterfaceHolder(path, interface);
Brad Bishop150147a2017-02-08 23:57:46 -0500238 return *any_ns::any_cast<std::shared_ptr<T> &>(holder);
Brad Bishop7b337772017-01-12 16:11:24 -0500239 }
240 template<typename T>
241 auto& getInterface(const char* path, const char* interface) const
242 {
243 auto& holder = getInterfaceHolder(path, interface);
Brad Bishop150147a2017-02-08 23:57:46 -0500244 return *any_ns::any_cast<T>(holder);
Brad Bishop7b337772017-01-12 16:11:24 -0500245 }
Brad Bishopb83a21e2016-11-30 13:43:37 -0500246
Brad Bishop79ccaf72017-01-22 16:00:50 -0500247 /** @brief Add or update interfaces on DBus. */
248 void updateInterfaces(
249 const sdbusplus::message::object_path& path,
250 const Object& interfaces,
251 ObjectReferences::iterator pos,
252 bool emitSignals = true);
253
Brad Bishop7b337772017-01-12 16:11:24 -0500254 /** @brief Provided for testing only. */
Brad Bishopb9b929b2017-01-13 16:33:06 -0500255 volatile bool _shutdown;
Brad Bishop49aefb32016-10-19 11:54:14 -0400256
Brad Bishop7b337772017-01-12 16:11:24 -0500257 /** @brief Path prefix applied to any relative paths. */
258 const char* _root;
Brad Bishop49aefb32016-10-19 11:54:14 -0400259
Brad Bishop7b337772017-01-12 16:11:24 -0500260 /** @brief A container of sdbusplus server interface references. */
261 ObjectReferences _refs;
Brad Bishop49aefb32016-10-19 11:54:14 -0400262
Brad Bishop7b337772017-01-12 16:11:24 -0500263 /** @brief A container contexts for signal callbacks. */
264 SigArgs _sigargs;
Brad Bishop49aefb32016-10-19 11:54:14 -0400265
Brad Bishop7b337772017-01-12 16:11:24 -0500266 /** @brief A container of sdbusplus signal matches. */
Patrick Williams133ca132017-05-01 22:12:34 -0500267 std::vector<sdbusplus::bus::match_t> _matches;
Brad Bishop49aefb32016-10-19 11:54:14 -0400268
Brad Bishop7b337772017-01-12 16:11:24 -0500269 /** @brief Persistent sdbusplus DBus bus connection. */
270 sdbusplus::bus::bus _bus;
Brad Bishop49aefb32016-10-19 11:54:14 -0400271
Brad Bishop7b337772017-01-12 16:11:24 -0500272 /** @brief sdbusplus org.freedesktop.DBus.ObjectManager reference. */
273 sdbusplus::server::manager::manager _manager;
Brad Bishop49aefb32016-10-19 11:54:14 -0400274
Brad Bishop7b337772017-01-12 16:11:24 -0500275 /** @brief A container of pimgen generated events and responses. */
276 static const Events _events;
Brad Bishop5fbaa7f2016-10-31 10:42:41 -0500277
Brad Bishop7b337772017-01-12 16:11:24 -0500278 /** @brief A container of pimgen generated factory methods. */
279 static const Makers _makers;
Brad Bishop49aefb32016-10-19 11:54:14 -0400280};
281
282} // namespace manager
283} // namespace inventory
284} // namespace phosphor
285
286// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4