Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <map> |
| 4 | #include <memory> |
| 5 | #include <string> |
| 6 | #include <vector> |
| 7 | #include <sdbusplus/server.hpp> |
Brad Bishop | e30dd77 | 2016-11-12 21:39:28 -0500 | [diff] [blame] | 8 | #include "xyz/openbmc_project/Inventory/Manager/server.hpp" |
Brad Bishop | 67b788d | 2016-11-29 13:09:01 -0500 | [diff] [blame] | 9 | #include "events.hpp" |
Brad Bishop | c038e01 | 2016-10-19 13:02:24 -0400 | [diff] [blame] | 10 | #include "actions.hpp" |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 11 | |
| 12 | namespace phosphor |
| 13 | { |
| 14 | namespace inventory |
| 15 | { |
| 16 | namespace manager |
| 17 | { |
| 18 | namespace details |
| 19 | { |
Brad Bishop | 451f8d9 | 2016-11-21 14:15:19 -0500 | [diff] [blame] | 20 | |
| 21 | template <typename T> |
| 22 | using ServerObject = typename sdbusplus::server::object::object<T>; |
| 23 | |
| 24 | using ManagerIface = |
Brad Bishop | 9aa5e2f | 2017-01-15 19:45:40 -0500 | [diff] [blame^] | 25 | sdbusplus::xyz::openbmc_project::Inventory::server::Manager; |
Brad Bishop | 451f8d9 | 2016-11-21 14:15:19 -0500 | [diff] [blame] | 26 | |
Brad Bishop | 65ffffa | 2016-11-29 12:31:31 -0500 | [diff] [blame] | 27 | /** @struct MakeInterface |
| 28 | * @brief Adapt an sdbusplus interface proxy. |
| 29 | * |
| 30 | * Template instances are builder functions that create |
| 31 | * adapted sdbusplus interface proxy interface objects. |
| 32 | * |
| 33 | * @tparam T - The type of the interface being adapted. |
| 34 | */ |
| 35 | template <typename T> |
| 36 | struct MakeInterface |
| 37 | { |
Brad Bishop | 5dd1c1e | 2017-01-12 16:14:27 -0500 | [diff] [blame] | 38 | static auto make(sdbusplus::bus::bus& bus, const char* path) |
Brad Bishop | 65ffffa | 2016-11-29 12:31:31 -0500 | [diff] [blame] | 39 | { |
| 40 | using HolderType = holder::Holder<std::unique_ptr<T>>; |
| 41 | return static_cast<std::unique_ptr<holder::Base>>( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 42 | HolderType::template make_unique<HolderType>( |
| 43 | std::forward<std::unique_ptr<T>>( |
| 44 | std::make_unique<T>( |
| 45 | std::forward<decltype(bus)>(bus), |
| 46 | std::forward<decltype(path)>(path))))); |
Brad Bishop | 65ffffa | 2016-11-29 12:31:31 -0500 | [diff] [blame] | 47 | } |
| 48 | }; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 49 | } // namespace details |
| 50 | |
| 51 | /** @class Manager |
| 52 | * @brief OpenBMC inventory manager implementation. |
| 53 | * |
| 54 | * A concrete implementation for the xyz.openbmc_project.Inventory.Manager |
| 55 | * DBus API. |
| 56 | */ |
| 57 | class Manager final : |
Brad Bishop | 451f8d9 | 2016-11-21 14:15:19 -0500 | [diff] [blame] | 58 | public details::ServerObject<details::ManagerIface> |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 59 | { |
| 60 | public: |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 61 | Manager() = delete; |
| 62 | Manager(const Manager&) = delete; |
| 63 | Manager& operator=(const Manager&) = delete; |
| 64 | Manager(Manager&&) = default; |
| 65 | Manager& operator=(Manager&&) = default; |
| 66 | ~Manager() = default; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 67 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 68 | /** @brief Construct an inventory manager. |
| 69 | * |
| 70 | * @param[in] bus - An sdbusplus bus connection. |
| 71 | * @param[in] busname - The DBus busname to own. |
| 72 | * @param[in] root - The DBus path on which to implement |
| 73 | * an inventory manager. |
| 74 | * @param[in] iface - The DBus inventory interface to implement. |
| 75 | */ |
| 76 | Manager(sdbusplus::bus::bus&&, const char*, const char*, const char*); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 77 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 78 | using Object = std::map < |
| 79 | std::string, std::map < |
Brad Bishop | 9aa5e2f | 2017-01-15 19:45:40 -0500 | [diff] [blame^] | 80 | std::string, sdbusplus::message::variant<int64_t, std::string >>>; |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 81 | using EventInfo = std::tuple < |
| 82 | std::vector<details::EventBasePtr>, |
| 83 | std::vector<details::ActionBasePtr >>; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 84 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 85 | /** @brief Start processing DBus messages. */ |
| 86 | void run() noexcept; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 87 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 88 | /** @brief Provided for testing only. */ |
| 89 | void shutdown() noexcept; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 90 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 91 | /** @brief sd_bus Notify method implementation callback. */ |
Brad Bishop | 9aa5e2f | 2017-01-15 19:45:40 -0500 | [diff] [blame^] | 92 | void notify(sdbusplus::message::object_path, Object) override; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 93 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 94 | /** @brief sd_bus signal callback. */ |
| 95 | void signal(sdbusplus::message::message&, |
| 96 | const details::DbusSignal& event, |
| 97 | const EventInfo& info); |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 98 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 99 | /** @brief Drop an object from DBus. */ |
| 100 | void destroyObject(const char*); |
Brad Bishop | 656a7d0 | 2016-10-19 22:20:02 -0400 | [diff] [blame] | 101 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 102 | /** @brief Invoke an sdbusplus server binding method. |
| 103 | * |
| 104 | * Invoke the requested method with a reference to the requested |
| 105 | * sdbusplus server binding interface as a parameter. |
| 106 | * |
| 107 | * @tparam T - The sdbusplus server binding interface type. |
| 108 | * @tparam U - The type of the sdbusplus server binding member. |
| 109 | * @tparam Args - Argument types of the binding member. |
| 110 | * |
| 111 | * @param[in] path - The DBus path on which the method should |
| 112 | * be invoked. |
| 113 | * @param[in] interface - The DBus interface hosting the method. |
| 114 | * @param[in] member - Pointer to sdbusplus server binding member. |
| 115 | * @param[in] args - Arguments to forward to the binding member. |
| 116 | * |
| 117 | * @returns - The return/value type of the binding method being |
| 118 | * called. |
| 119 | */ |
| 120 | template<typename T, typename U, typename ...Args> |
| 121 | decltype(auto) invokeMethod(const char* path, const char* interface, |
| 122 | U&& member, Args&& ...args) |
| 123 | { |
| 124 | auto& holder = getInterface<std::unique_ptr<T>>(path, interface); |
| 125 | auto& iface = *holder.get(); |
| 126 | return (iface.*member)(std::forward<Args>(args)...); |
| 127 | } |
Brad Bishop | da649b1 | 2016-11-30 14:35:02 -0500 | [diff] [blame] | 128 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 129 | using SigArgs = std::vector < |
| 130 | std::unique_ptr < |
| 131 | std::tuple < |
| 132 | Manager*, |
| 133 | const details::DbusSignal*, |
| 134 | const EventInfo* >>>; |
| 135 | using SigArg = SigArgs::value_type::element_type; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 136 | |
| 137 | private: |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 138 | using HolderPtr = std::unique_ptr<details::holder::Base>; |
| 139 | using InterfaceComposite = std::map<std::string, HolderPtr>; |
| 140 | using ObjectReferences = std::map<std::string, InterfaceComposite>; |
| 141 | using Events = std::vector<EventInfo>; |
| 142 | using MakerType = HolderPtr(*)( |
| 143 | sdbusplus::bus::bus&, const char*); |
| 144 | using Makers = std::map<std::string, MakerType>; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 145 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 146 | /** @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 | */ |
| 158 | details::holder::Base& getInterfaceHolder( |
| 159 | const char*, const char*) const; |
| 160 | details::holder::Base& getInterfaceHolder( |
| 161 | const char*, const char*); |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 162 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 163 | /** @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); |
| 177 | return static_cast < |
| 178 | details::holder::Holder<T>& >(holder); |
| 179 | } |
| 180 | template<typename T> |
| 181 | auto& getInterface(const char* path, const char* interface) const |
| 182 | { |
| 183 | auto& holder = getInterfaceHolder(path, interface); |
| 184 | return static_cast < |
| 185 | const details::holder::Holder<T>& >(holder); |
| 186 | } |
Brad Bishop | b83a21e | 2016-11-30 13:43:37 -0500 | [diff] [blame] | 187 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 188 | /** @brief Provided for testing only. */ |
Brad Bishop | b9b929b | 2017-01-13 16:33:06 -0500 | [diff] [blame] | 189 | volatile bool _shutdown; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 190 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 191 | /** @brief Path prefix applied to any relative paths. */ |
| 192 | const char* _root; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 193 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 194 | /** @brief A container of sdbusplus server interface references. */ |
| 195 | ObjectReferences _refs; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 196 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 197 | /** @brief A container contexts for signal callbacks. */ |
| 198 | SigArgs _sigargs; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 199 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 200 | /** @brief A container of sdbusplus signal matches. */ |
| 201 | std::vector<sdbusplus::server::match::match> _matches; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 202 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 203 | /** @brief Persistent sdbusplus DBus bus connection. */ |
| 204 | sdbusplus::bus::bus _bus; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 205 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 206 | /** @brief sdbusplus org.freedesktop.DBus.ObjectManager reference. */ |
| 207 | sdbusplus::server::manager::manager _manager; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 208 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 209 | /** @brief A container of pimgen generated events and responses. */ |
| 210 | static const Events _events; |
Brad Bishop | 5fbaa7f | 2016-10-31 10:42:41 -0500 | [diff] [blame] | 211 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 212 | /** @brief A container of pimgen generated factory methods. */ |
| 213 | static const Makers _makers; |
Brad Bishop | 49aefb3 | 2016-10-19 11:54:14 -0400 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | } // namespace manager |
| 217 | } // namespace inventory |
| 218 | } // namespace phosphor |
| 219 | |
| 220 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |