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