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