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