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