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