Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <utility> |
| 4 | #include <memory> |
| 5 | #include <sdbusplus/bus.hpp> |
| 6 | #include "utils.hpp" |
| 7 | #include "types.hpp" |
| 8 | |
| 9 | namespace phosphor |
| 10 | { |
| 11 | namespace inventory |
| 12 | { |
| 13 | namespace manager |
| 14 | { |
| 15 | |
| 16 | class Manager; |
| 17 | |
| 18 | /** @brief make_action |
| 19 | * |
| 20 | * Adapt an action function object. |
| 21 | * |
| 22 | * @param[in] action - The action being adapted. |
| 23 | * @returns - The adapted action. |
| 24 | * |
| 25 | * @tparam T - The type of the action being adapted. |
| 26 | */ |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 27 | template <typename T> auto make_action(T&& action) |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 28 | { |
| 29 | return Action(std::forward<T>(action)); |
| 30 | } |
| 31 | |
| 32 | /** @brief make_filter |
| 33 | * |
| 34 | * Adapt a filter function object. |
| 35 | * |
| 36 | * @param[in] filter - The filter being adapted. |
| 37 | * @returns - The adapted filter. |
| 38 | * |
| 39 | * @tparam T - The type of the filter being adapted. |
| 40 | */ |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 41 | template <typename T> auto make_filter(T&& filter) |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 42 | { |
| 43 | return Filter(std::forward<T>(filter)); |
| 44 | } |
| 45 | |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 46 | /** @brief make_path_condition |
| 47 | * |
| 48 | * Adapt a path_condition function object. |
| 49 | * |
| 50 | * @param[in] filter - The functor being adapted. |
| 51 | * @returns - The adapted functor. |
| 52 | * |
| 53 | * @tparam T - The type of the functor being adapted. |
| 54 | */ |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 55 | template <typename T> auto make_path_condition(T&& condition) |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 56 | { |
| 57 | return PathCondition(std::forward<T>(condition)); |
| 58 | } |
| 59 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 60 | template <typename T, typename... Args> |
| 61 | auto callArrayWithStatus(T&& container, Args&&... args) |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 62 | { |
| 63 | for (auto f : container) |
| 64 | { |
| 65 | if (!f(std::forward<Args>(args)...)) |
| 66 | { |
| 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | return true; |
| 71 | } |
| 72 | |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 73 | namespace functor |
| 74 | { |
| 75 | |
| 76 | /** @brief Destroy objects action. */ |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 77 | inline auto destroyObjects(std::vector<const char*>&& paths, |
| 78 | std::vector<PathCondition>&& conditions) |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 79 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 80 | return [=](auto& b, auto& m) { |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 81 | for (const auto& p : paths) |
| 82 | { |
| 83 | if (callArrayWithStatus(conditions, p, b, m)) |
| 84 | { |
| 85 | m.destroyObjects({p}); |
| 86 | } |
| 87 | } |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 88 | }; |
| 89 | } |
| 90 | |
| 91 | /** @brief Create objects action. */ |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 92 | inline auto |
| 93 | createObjects(std::map<sdbusplus::message::object_path, Object>&& objs) |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 94 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 95 | return [=](auto&, auto& m) { m.createObjects(objs); }; |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** @brief Set a property action. |
| 99 | * |
| 100 | * Invoke the requested method with a reference to the requested |
| 101 | * sdbusplus server binding interface as a parameter. |
| 102 | * |
| 103 | * @tparam T - The sdbusplus server binding interface type. |
| 104 | * @tparam U - The type of the sdbusplus server binding member |
| 105 | * function that sets the property. |
| 106 | * @tparam V - The property value type. |
| 107 | * |
| 108 | * @param[in] paths - The DBus paths on which the property should |
| 109 | * be set. |
| 110 | * @param[in] iface - The DBus interface hosting the property. |
| 111 | * @param[in] member - Pointer to sdbusplus server binding member. |
| 112 | * @param[in] value - The value the property should be set to. |
| 113 | * |
| 114 | * @returns - A function object that sets the requested property |
| 115 | * to the requested value. |
| 116 | */ |
| 117 | template <typename T, typename U, typename V> |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 118 | auto setProperty(std::vector<const char*>&& paths, |
| 119 | std::vector<PathCondition>&& conditions, const char* iface, |
| 120 | U&& member, V&& value) |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 121 | { |
| 122 | // The manager is the only parameter passed to actions. |
| 123 | // Bind the path, interface, interface member function pointer, |
| 124 | // and value to a lambda. When it is called, forward the |
| 125 | // path, interface and value on to the manager member function. |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 126 | return [ |
| 127 | paths, conditions = conditions, iface, member, |
| 128 | value = std::forward<V>(value) |
| 129 | ](auto& b, auto& m) |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 130 | { |
| 131 | for (auto p : paths) |
| 132 | { |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 133 | if (callArrayWithStatus(conditions, p, b, m)) |
| 134 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 135 | m.template invokeMethod<T>(p, iface, member, value); |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 136 | } |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 137 | } |
| 138 | }; |
| 139 | } |
| 140 | |
| 141 | /** @struct PropertyChangedCondition |
| 142 | * @brief Match filter functor that tests a property value. |
| 143 | * |
| 144 | * @tparam T - The type of the property being tested. |
| 145 | * @tparam U - The type of the condition checking functor. |
| 146 | */ |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 147 | template <typename T, typename U> struct PropertyChangedCondition |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 148 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 149 | PropertyChangedCondition() = delete; |
| 150 | ~PropertyChangedCondition() = default; |
| 151 | PropertyChangedCondition(const PropertyChangedCondition&) = default; |
| 152 | PropertyChangedCondition& |
| 153 | operator=(const PropertyChangedCondition&) = default; |
| 154 | PropertyChangedCondition(PropertyChangedCondition&&) = default; |
| 155 | PropertyChangedCondition& operator=(PropertyChangedCondition&&) = default; |
| 156 | PropertyChangedCondition(const char* iface, const char* property, |
| 157 | U&& condition) : |
| 158 | _iface(iface), |
| 159 | _property(property), _condition(std::forward<U>(condition)) |
| 160 | { |
| 161 | } |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 162 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 163 | /** @brief Test a property value. |
| 164 | * |
| 165 | * Extract the property from the PropertiesChanged |
| 166 | * message and run the condition test. |
| 167 | */ |
| 168 | bool operator()(sdbusplus::bus::bus&, sdbusplus::message::message& msg, |
| 169 | Manager&) const |
| 170 | { |
| 171 | std::map<std::string, sdbusplus::message::variant<T>> properties; |
| 172 | const char* iface = nullptr; |
| 173 | |
| 174 | msg.read(iface); |
| 175 | if (!iface || strcmp(iface, _iface)) |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 176 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 177 | return false; |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 178 | } |
| 179 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 180 | msg.read(properties); |
| 181 | auto it = properties.find(_property); |
| 182 | if (it == properties.cend()) |
| 183 | { |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | return _condition(std::forward<T>(it->second.template get<T>())); |
| 188 | } |
| 189 | |
| 190 | private: |
| 191 | const char* _iface; |
| 192 | const char* _property; |
| 193 | U _condition; |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | /** @struct PropertyConditionBase |
| 197 | * @brief Match filter functor that tests a property value. |
| 198 | * |
| 199 | * Base class for PropertyCondition - factored out code that |
| 200 | * doesn't need to be templated. |
| 201 | */ |
| 202 | struct PropertyConditionBase |
| 203 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 204 | PropertyConditionBase() = delete; |
| 205 | virtual ~PropertyConditionBase() = default; |
| 206 | PropertyConditionBase(const PropertyConditionBase&) = default; |
| 207 | PropertyConditionBase& operator=(const PropertyConditionBase&) = default; |
| 208 | PropertyConditionBase(PropertyConditionBase&&) = default; |
| 209 | PropertyConditionBase& operator=(PropertyConditionBase&&) = default; |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 210 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 211 | /** @brief Constructor |
| 212 | * |
| 213 | * The service argument can be nullptr. If something |
| 214 | * else is provided the function will call the the |
| 215 | * service directly. If omitted, the function will |
| 216 | * look up the service in the ObjectMapper. |
| 217 | * |
| 218 | * @param path - The path of the object containing |
| 219 | * the property to be tested. |
| 220 | * @param iface - The interface hosting the property |
| 221 | * to be tested. |
| 222 | * @param property - The property to be tested. |
| 223 | * @param service - The DBus service hosting the object. |
| 224 | */ |
| 225 | PropertyConditionBase(const char* path, const char* iface, |
| 226 | const char* property, const char* service) : |
| 227 | _path(path ? path : std::string()), |
| 228 | _iface(iface), _property(property), _service(service) |
| 229 | { |
| 230 | } |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 231 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 232 | /** @brief Forward comparison to type specific implementation. */ |
| 233 | virtual bool eval(sdbusplus::message::message&) const = 0; |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 234 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 235 | /** @brief Test a property value. |
| 236 | * |
| 237 | * Make a DBus call and test the value of any property. |
| 238 | */ |
| 239 | bool operator()(sdbusplus::bus::bus&, sdbusplus::message::message&, |
| 240 | Manager&) const; |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 241 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 242 | /** @brief Test a property value. |
| 243 | * |
| 244 | * Make a DBus call and test the value of any property. |
| 245 | */ |
| 246 | bool operator()(const std::string&, sdbusplus::bus::bus&, Manager&) const; |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 247 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 248 | private: |
| 249 | std::string _path; |
| 250 | std::string _iface; |
| 251 | std::string _property; |
| 252 | const char* _service; |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | /** @struct PropertyCondition |
| 256 | * @brief Match filter functor that tests a property value. |
| 257 | * |
| 258 | * @tparam T - The type of the property being tested. |
| 259 | * @tparam U - The type of the condition checking functor. |
| 260 | */ |
| 261 | template <typename T, typename U> |
| 262 | struct PropertyCondition final : public PropertyConditionBase |
| 263 | { |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 264 | PropertyCondition() = delete; |
| 265 | ~PropertyCondition() = default; |
| 266 | PropertyCondition(const PropertyCondition&) = default; |
| 267 | PropertyCondition& operator=(const PropertyCondition&) = default; |
| 268 | PropertyCondition(PropertyCondition&&) = default; |
| 269 | PropertyCondition& operator=(PropertyCondition&&) = default; |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 270 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 271 | /** @brief Constructor |
| 272 | * |
| 273 | * The service argument can be nullptr. If something |
| 274 | * else is provided the function will call the the |
| 275 | * service directly. If omitted, the function will |
| 276 | * look up the service in the ObjectMapper. |
| 277 | * |
| 278 | * @param path - The path of the object containing |
| 279 | * the property to be tested. |
| 280 | * @param iface - The interface hosting the property |
| 281 | * to be tested. |
| 282 | * @param property - The property to be tested. |
| 283 | * @param condition - The test to run on the property. |
| 284 | * @param service - The DBus service hosting the object. |
| 285 | */ |
| 286 | PropertyCondition(const char* path, const char* iface, const char* property, |
| 287 | U&& condition, const char* service) : |
| 288 | PropertyConditionBase(path, iface, property, service), |
| 289 | _condition(std::forward<decltype(condition)>(condition)) |
| 290 | { |
| 291 | } |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 292 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 293 | /** @brief Test a property value. |
| 294 | * |
| 295 | * Make a DBus call and test the value of any property. |
| 296 | */ |
| 297 | bool eval(sdbusplus::message::message& msg) const override |
| 298 | { |
| 299 | sdbusplus::message::variant<T> value; |
| 300 | msg.read(value); |
| 301 | return _condition(std::forward<T>(value.template get<T>())); |
| 302 | } |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 303 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 304 | private: |
| 305 | U _condition; |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 306 | }; |
| 307 | |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 308 | /** @brief Implicit type deduction for constructing PropertyChangedCondition. */ |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 309 | template <typename T> |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 310 | auto propertyChangedTo(const char* iface, const char* property, T&& val) |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 311 | { |
| 312 | auto condition = [val = std::forward<T>(val)](T && arg) |
| 313 | { |
| 314 | return arg == val; |
| 315 | }; |
| 316 | using U = decltype(condition); |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 317 | return PropertyChangedCondition<T, U>(iface, property, |
| 318 | std::move(condition)); |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /** @brief Implicit type deduction for constructing PropertyCondition. */ |
| 322 | template <typename T> |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 323 | auto propertyIs(const char* path, const char* iface, const char* property, |
| 324 | T&& val, const char* service = nullptr) |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 325 | { |
| 326 | auto condition = [val = std::forward<T>(val)](T && arg) |
| 327 | { |
| 328 | return arg == val; |
| 329 | }; |
| 330 | using U = decltype(condition); |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame^] | 331 | return PropertyCondition<T, U>(path, iface, property, std::move(condition), |
| 332 | service); |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 333 | } |
| 334 | } // namespace functor |
| 335 | } // namespace manager |
| 336 | } // namespace inventory |
| 337 | } // namespace phosphor |
| 338 | |
| 339 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |