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