blob: 9b8338b901700a47ac1f0d8f7c627d89f1f3d9b8 [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>
8#include <xyz/openbmc_project/Inventory/Manager/server.hpp>
9
10namespace phosphor
11{
12namespace inventory
13{
14namespace manager
15{
16namespace details
17{
18namespace interface
19{
20namespace holder
21{
22
23/** @struct Base
24 * @brief sdbusplus server interface holder base.
25 *
26 * Provides a common type for assembling containers of sdbusplus server
27 * interfaces. Typically a multiple inheritance scheme (sdbusplus::object)
28 * would be used for this; however, for objects created by PIM the interfaces
29 * are not known at build time.
30 */
31struct Base
32{
33 Base() = default;
34 virtual ~Base() = default;
35 Base(const Base&) = delete;
36 Base& operator=(const Base&) = delete;
37 Base(Base&&) = default;
38 Base& operator=(Base&&) = default;
39};
40
41/** @struct Holder
42 * @brief sdbusplus server interface holder.
43 *
44 * Holds a pointer to an sdbusplus server interface instance.
45 *
46 * @tparam T - The sdbusplus server interface type to hold.
47 */
48template <typename T>
49struct Holder final : public Base
50{
51 Holder() = delete;
52 ~Holder() = default;
53 Holder(const Holder&) = delete;
54 Holder& operator=(const Holder&) = delete;
55 Holder(Holder&&) = default;
56 Holder& operator=(Holder&&) = default;
57 explicit Holder(auto &&ptr) noexcept : _ptr(std::move(ptr)) {}
58
59 /** @brief sdbusplus server interface holder factory method.
60 *
61 * @param bus[in] - An sdbusplus bus connection
62 * @param bus[in] - The path of the object for which
63 * an interface is to be held.
64 * @returns A held interface.
65 */
66 static auto make(sdbusplus::bus::bus &bus, const char *path)
67 {
68 return static_cast<std::unique_ptr<Base>>(
69 std::make_unique<Holder<T>>(
70 std::make_unique<T>(bus, path)));
71 }
72
73 private:
74 std::unique_ptr<T> _ptr;
75};
76
77} // namespace holder
78} // namespace interface
79} // namespace details
80
81/** @class Manager
82 * @brief OpenBMC inventory manager implementation.
83 *
84 * A concrete implementation for the xyz.openbmc_project.Inventory.Manager
85 * DBus API.
86 */
87class Manager final :
88 public sdbusplus::server::xyz::openbmc_project::Inventory::Manager
89{
90 public:
91 Manager() = delete;
92 Manager(const Manager&) = delete;
93 Manager& operator=(const Manager&) = delete;
94 Manager(Manager&&) = default;
95 Manager& operator=(Manager&&) = default;
96 ~Manager() = default;
97
98 /** @brief Construct an inventory manager.
99 *
100 * @param[in] bus - An sdbusplus bus connection.
101 * @param[in] busname - The DBus busname to own.
102 * @param[in] root - The DBus path on which to implement
103 * an inventory manager.
104 * @param[in] iface - The DBus inventory interface to implement.
105 */
106 Manager(sdbusplus::bus::bus &&, const char *, const char*, const char*);
107
108 using Object = std::map<
109 std::string, std::map<
110 std::string, sdbusplus::message::variant<std::string>>>;
111
112 /** @brief Start processing DBus messages. */
113 void run() noexcept;
114
115 /** @brief Provided for testing only. */
116 void shutdown() noexcept;
117
118 /** @brief sd_bus Notify method implementation callback. */
119 void notify(std::string path, Object) override;
120
121 /** @brief sd_bus signal callback. */
122 void signal(sdbusplus::message::message &, auto &);
123
124 using Event = std::tuple<const char *>;
125 using SigArgs = std::vector<
126 std::unique_ptr<
127 std::tuple<
128 Manager *,
129 const Event *>>>;
130 using SigArg = SigArgs::value_type::element_type;
131
132 private:
133 using Holder = details::interface::holder::Base;
134 using HolderPtr = std::unique_ptr<Holder>;
135 using InterfaceComposite = std::map<std::string, HolderPtr>;
136 using ObjectReferences = std::map<std::string, InterfaceComposite>;
137 using Events = std::map<const char *, Event>;
138
139 /** @brief Provided for testing only. */
140 bool _shutdown;
141
142 /** @brief Path prefix applied to any relative paths. */
143 const char * _root;
144
145 /** @brief A container of sdbusplus server interface references. */
146 ObjectReferences _refs;
147
148 /** @brief A container contexts for signal callbacks. */
149 SigArgs _sigargs;
150
151 /** @brief A container of sdbusplus signal matches. */
152 std::vector<sdbusplus::server::match::match> _matches;
153
154 /** @brief Persistent sdbusplus DBus bus connection. */
155 sdbusplus::bus::bus _bus;
156
157 /** @brief sdbusplus org.freedesktop.DBus.ObjectManager reference. */
158 sdbusplus::server::manager::manager _manager;
159
160 /** @brief A container of pimgen generated events and responses. */
161 static const Events _events;
162};
163
164} // namespace manager
165} // namespace inventory
166} // namespace phosphor
167
168// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4