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