Brad Bishop | 3d57f50 | 2016-10-19 12:18:41 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <utility> |
| 4 | #include <memory> |
Brad Bishop | 07934a6 | 2017-02-08 23:34:59 -0500 | [diff] [blame] | 5 | #include <functional> |
Brad Bishop | 3d57f50 | 2016-10-19 12:18:41 -0400 | [diff] [blame] | 6 | #include <sdbusplus/message.hpp> |
Brad Bishop | 65ffffa | 2016-11-29 12:31:31 -0500 | [diff] [blame] | 7 | #include "utils.hpp" |
Brad Bishop | 3d57f50 | 2016-10-19 12:18:41 -0400 | [diff] [blame] | 8 | |
| 9 | namespace phosphor |
| 10 | { |
| 11 | namespace inventory |
| 12 | { |
| 13 | namespace manager |
| 14 | { |
Brad Bishop | c0eae11 | 2016-10-19 21:59:47 -0400 | [diff] [blame] | 15 | |
| 16 | class Manager; |
Brad Bishop | 65ffffa | 2016-11-29 12:31:31 -0500 | [diff] [blame] | 17 | namespace details |
| 18 | { |
Brad Bishop | 07934a6 | 2017-02-08 23:34:59 -0500 | [diff] [blame] | 19 | using Filter = std::function < |
| 20 | bool (sdbusplus::bus::bus&, sdbusplus::message::message&, Manager&) >; |
Brad Bishop | 65ffffa | 2016-11-29 12:31:31 -0500 | [diff] [blame] | 21 | |
Brad Bishop | 4f20a3e | 2016-11-29 15:21:46 -0500 | [diff] [blame] | 22 | /** @struct Event |
| 23 | * @brief Event object interface. |
Brad Bishop | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame] | 24 | * |
| 25 | * The event base is an assocation of an event type |
| 26 | * and an array of filter callbacks. |
Brad Bishop | 4f20a3e | 2016-11-29 15:21:46 -0500 | [diff] [blame] | 27 | */ |
Brad Bishop | 07934a6 | 2017-02-08 23:34:59 -0500 | [diff] [blame] | 28 | struct Event : public std::vector<Filter> |
Brad Bishop | 4f20a3e | 2016-11-29 15:21:46 -0500 | [diff] [blame] | 29 | { |
| 30 | enum class Type |
| 31 | { |
| 32 | DBUS_SIGNAL, |
Brad Bishop | 3e4a19a | 2017-01-21 22:17:09 -0500 | [diff] [blame] | 33 | STARTUP, |
Brad Bishop | 4f20a3e | 2016-11-29 15:21:46 -0500 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | virtual ~Event() = default; |
Brad Bishop | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame] | 37 | Event(const Event&) = delete; |
| 38 | Event& operator=(const Event&) = delete; |
Brad Bishop | 4f20a3e | 2016-11-29 15:21:46 -0500 | [diff] [blame] | 39 | Event(Event&&) = default; |
| 40 | Event& operator=(Event&&) = default; |
Brad Bishop | 4f20a3e | 2016-11-29 15:21:46 -0500 | [diff] [blame] | 41 | |
Brad Bishop | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame] | 42 | /** @brief Event constructor. |
| 43 | * |
| 44 | * @param[in] filters - An array of filter callbacks. |
| 45 | * @param[in] t - The event type. |
| 46 | */ |
| 47 | explicit Event( |
Brad Bishop | 07934a6 | 2017-02-08 23:34:59 -0500 | [diff] [blame] | 48 | const std::vector<Filter>& filters, Type t = Type::STARTUP) : |
| 49 | std::vector<Filter>(filters), |
Brad Bishop | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame] | 50 | type(t) {} |
| 51 | |
| 52 | /** @brief event class enumeration. */ |
Brad Bishop | 4f20a3e | 2016-11-29 15:21:46 -0500 | [diff] [blame] | 53 | Type type; |
| 54 | }; |
| 55 | |
Brad Bishop | 3e4a19a | 2017-01-21 22:17:09 -0500 | [diff] [blame] | 56 | using StartupEvent = Event; |
| 57 | |
Brad Bishop | 4f20a3e | 2016-11-29 15:21:46 -0500 | [diff] [blame] | 58 | using EventBasePtr = std::shared_ptr<Event>; |
| 59 | |
| 60 | /** @struct DbusSignal |
| 61 | * @brief DBus signal event. |
| 62 | * |
| 63 | * DBus signal events are an association of a match signature |
| 64 | * and filtering function object. |
| 65 | */ |
Brad Bishop | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame] | 66 | struct DbusSignal final : public Event |
Brad Bishop | 4f20a3e | 2016-11-29 15:21:46 -0500 | [diff] [blame] | 67 | { |
Brad Bishop | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame] | 68 | ~DbusSignal() = default; |
| 69 | DbusSignal(const DbusSignal&) = delete; |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 70 | DbusSignal& operator=(const DbusSignal&) = delete; |
Brad Bishop | 4f20a3e | 2016-11-29 15:21:46 -0500 | [diff] [blame] | 71 | DbusSignal(DbusSignal&&) = default; |
| 72 | DbusSignal& operator=(DbusSignal&&) = default; |
| 73 | |
| 74 | /** @brief Import from signature and filter constructor. |
| 75 | * |
| 76 | * @param[in] sig - The DBus match signature. |
Brad Bishop | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame] | 77 | * @param[in] filter - An array of DBus signal |
| 78 | * match callback filtering functions. |
Brad Bishop | 4f20a3e | 2016-11-29 15:21:46 -0500 | [diff] [blame] | 79 | */ |
Brad Bishop | 07934a6 | 2017-02-08 23:34:59 -0500 | [diff] [blame] | 80 | DbusSignal(const char* sig, const std::vector<Filter>& filters) : |
Brad Bishop | 48547a8 | 2017-01-19 15:12:50 -0500 | [diff] [blame] | 81 | Event(filters, Type::DBUS_SIGNAL), |
| 82 | signature(sig) {} |
| 83 | |
| 84 | const char* signature; |
Brad Bishop | 4f20a3e | 2016-11-29 15:21:46 -0500 | [diff] [blame] | 85 | }; |
| 86 | |
Brad Bishop | 65ffffa | 2016-11-29 12:31:31 -0500 | [diff] [blame] | 87 | /** @brief make_filter |
| 88 | * |
| 89 | * Adapt a filter function object. |
| 90 | * |
| 91 | * @param[in] filter - The filter being adapted. |
| 92 | * @returns - The adapted filter. |
| 93 | * |
| 94 | * @tparam T - The type of the filter being adapted. |
| 95 | */ |
| 96 | template <typename T> |
| 97 | auto make_filter(T&& filter) |
| 98 | { |
Brad Bishop | 07934a6 | 2017-02-08 23:34:59 -0500 | [diff] [blame] | 99 | return Filter(std::forward<T>(filter)); |
Brad Bishop | 65ffffa | 2016-11-29 12:31:31 -0500 | [diff] [blame] | 100 | } |
| 101 | } // namespace details |
| 102 | |
Brad Bishop | 3d57f50 | 2016-10-19 12:18:41 -0400 | [diff] [blame] | 103 | namespace filters |
| 104 | { |
| 105 | namespace details |
| 106 | { |
Brad Bishop | bf5aa9c | 2016-10-19 21:19:04 -0400 | [diff] [blame] | 107 | namespace property_condition |
| 108 | { |
| 109 | |
Brad Bishop | 143522e | 2017-01-19 09:12:54 -0500 | [diff] [blame] | 110 | /** @struct PropertyChangedCondition |
Brad Bishop | bf5aa9c | 2016-10-19 21:19:04 -0400 | [diff] [blame] | 111 | * @brief Match filter functor that tests a property value. |
| 112 | * |
| 113 | * @tparam T - The type of the property being tested. |
| 114 | * @tparam U - The type of the condition checking functor. |
| 115 | */ |
| 116 | template <typename T, typename U> |
Brad Bishop | 143522e | 2017-01-19 09:12:54 -0500 | [diff] [blame] | 117 | struct PropertyChangedCondition |
Brad Bishop | bf5aa9c | 2016-10-19 21:19:04 -0400 | [diff] [blame] | 118 | { |
Brad Bishop | 143522e | 2017-01-19 09:12:54 -0500 | [diff] [blame] | 119 | PropertyChangedCondition() = delete; |
| 120 | ~PropertyChangedCondition() = default; |
| 121 | PropertyChangedCondition(const PropertyChangedCondition&) = default; |
Brad Bishop | 07934a6 | 2017-02-08 23:34:59 -0500 | [diff] [blame] | 122 | PropertyChangedCondition& operator=(const PropertyChangedCondition&) = default; |
Brad Bishop | 143522e | 2017-01-19 09:12:54 -0500 | [diff] [blame] | 123 | PropertyChangedCondition(PropertyChangedCondition&&) = default; |
| 124 | PropertyChangedCondition& operator=(PropertyChangedCondition&&) = default; |
| 125 | PropertyChangedCondition(const char* iface, const char* property, |
| 126 | U&& condition) : |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 127 | _iface(iface), |
| 128 | _property(property), |
| 129 | _condition(std::forward<U>(condition)) { } |
Brad Bishop | bf5aa9c | 2016-10-19 21:19:04 -0400 | [diff] [blame] | 130 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 131 | /** @brief Test a property value. |
| 132 | * |
| 133 | * Extract the property from the PropertiesChanged |
| 134 | * message and run the condition test. |
| 135 | */ |
Brad Bishop | 2371900 | 2017-01-24 21:08:46 -0500 | [diff] [blame] | 136 | bool operator()( |
| 137 | sdbusplus::bus::bus&, |
| 138 | sdbusplus::message::message& msg, |
| 139 | Manager&) const |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 140 | { |
| 141 | std::map < |
Brad Bishop | bf5aa9c | 2016-10-19 21:19:04 -0400 | [diff] [blame] | 142 | std::string, |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 143 | sdbusplus::message::variant<T >> properties; |
| 144 | const char* iface = nullptr; |
Brad Bishop | bf5aa9c | 2016-10-19 21:19:04 -0400 | [diff] [blame] | 145 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 146 | msg.read(iface); |
Brad Bishop | 064c94a | 2017-01-21 21:33:30 -0500 | [diff] [blame] | 147 | if (!iface || strcmp(iface, _iface)) |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 148 | { |
Brad Bishop | bf5aa9c | 2016-10-19 21:19:04 -0400 | [diff] [blame] | 149 | return false; |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 150 | } |
Brad Bishop | bf5aa9c | 2016-10-19 21:19:04 -0400 | [diff] [blame] | 151 | |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 152 | msg.read(properties); |
| 153 | auto it = properties.find(_property); |
| 154 | if (it == properties.cend()) |
| 155 | { |
| 156 | return false; |
| 157 | } |
Brad Bishop | bf5aa9c | 2016-10-19 21:19:04 -0400 | [diff] [blame] | 158 | |
Brad Bishop | fb083c2 | 2017-01-19 09:22:04 -0500 | [diff] [blame] | 159 | return _condition( |
| 160 | std::forward<T>(it->second.template get<T>())); |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 161 | } |
Brad Bishop | bf5aa9c | 2016-10-19 21:19:04 -0400 | [diff] [blame] | 162 | |
| 163 | private: |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 164 | const char* _iface; |
| 165 | const char* _property; |
| 166 | U _condition; |
Brad Bishop | bf5aa9c | 2016-10-19 21:19:04 -0400 | [diff] [blame] | 167 | }; |
| 168 | |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 169 | /** @struct PropertyConditionBase |
| 170 | * @brief Match filter functor that tests a property value. |
| 171 | * |
| 172 | * Base class for PropertyCondition - factored out code that |
| 173 | * doesn't need to be templated. |
| 174 | */ |
| 175 | struct PropertyConditionBase |
| 176 | { |
| 177 | PropertyConditionBase() = delete; |
| 178 | virtual ~PropertyConditionBase() = default; |
Brad Bishop | 07934a6 | 2017-02-08 23:34:59 -0500 | [diff] [blame] | 179 | PropertyConditionBase(const PropertyConditionBase&) = default; |
| 180 | PropertyConditionBase& operator=(const PropertyConditionBase&) = default; |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 181 | PropertyConditionBase(PropertyConditionBase&&) = default; |
| 182 | PropertyConditionBase& operator=(PropertyConditionBase&&) = default; |
| 183 | |
| 184 | /** @brief Constructor |
| 185 | * |
| 186 | * The service argument can be nullptr. If something |
| 187 | * else is provided the function will call the the |
| 188 | * service directly. If omitted, the function will |
| 189 | * look up the service in the ObjectMapper. |
| 190 | * |
| 191 | * @param path - The path of the object containing |
| 192 | * the property to be tested. |
| 193 | * @param iface - The interface hosting the property |
| 194 | * to be tested. |
| 195 | * @param property - The property to be tested. |
| 196 | * @param service - The DBus service hosting the object. |
| 197 | */ |
| 198 | PropertyConditionBase( |
| 199 | const char* path, |
| 200 | const char* iface, |
| 201 | const char* property, |
| 202 | const char* service) : |
| 203 | _path(path), |
| 204 | _iface(iface), |
| 205 | _property(property), |
| 206 | _service(service) {} |
| 207 | |
| 208 | /** @brief Forward comparison to type specific implementation. */ |
| 209 | virtual bool eval(sdbusplus::message::message&) const = 0; |
| 210 | |
| 211 | /** @brief Test a property value. |
| 212 | * |
| 213 | * Make a DBus call and test the value of any property. |
| 214 | */ |
| 215 | bool operator()( |
| 216 | sdbusplus::bus::bus&, |
| 217 | sdbusplus::message::message&, |
| 218 | Manager&) const; |
| 219 | |
| 220 | private: |
| 221 | std::string _path; |
| 222 | std::string _iface; |
| 223 | std::string _property; |
| 224 | const char* _service; |
| 225 | }; |
| 226 | |
| 227 | /** @struct PropertyCondition |
| 228 | * @brief Match filter functor that tests a property value. |
| 229 | * |
| 230 | * @tparam T - The type of the property being tested. |
| 231 | * @tparam U - The type of the condition checking functor. |
| 232 | */ |
| 233 | template <typename T, typename U> |
| 234 | struct PropertyCondition final : public PropertyConditionBase |
| 235 | { |
| 236 | PropertyCondition() = delete; |
| 237 | ~PropertyCondition() = default; |
Brad Bishop | 07934a6 | 2017-02-08 23:34:59 -0500 | [diff] [blame] | 238 | PropertyCondition(const PropertyCondition&) = default; |
| 239 | PropertyCondition& operator=(const PropertyCondition&) = default; |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 240 | PropertyCondition(PropertyCondition&&) = default; |
| 241 | PropertyCondition& operator=(PropertyCondition&&) = default; |
| 242 | |
| 243 | /** @brief Constructor |
| 244 | * |
| 245 | * The service argument can be nullptr. If something |
| 246 | * else is provided the function will call the the |
| 247 | * service directly. If omitted, the function will |
| 248 | * look up the service in the ObjectMapper. |
| 249 | * |
| 250 | * @param path - The path of the object containing |
| 251 | * the property to be tested. |
| 252 | * @param iface - The interface hosting the property |
| 253 | * to be tested. |
| 254 | * @param property - The property to be tested. |
| 255 | * @param condition - The test to run on the property. |
| 256 | * @param service - The DBus service hosting the object. |
| 257 | */ |
| 258 | PropertyCondition( |
| 259 | const char* path, |
| 260 | const char* iface, |
| 261 | const char* property, |
| 262 | U&& condition, |
| 263 | const char* service) : |
| 264 | PropertyConditionBase(path, iface, property, service), |
| 265 | _condition(std::forward<decltype(condition)>(condition)) {} |
| 266 | |
| 267 | /** @brief Test a property value. |
| 268 | * |
| 269 | * Make a DBus call and test the value of any property. |
| 270 | */ |
| 271 | bool eval(sdbusplus::message::message& msg) const override |
| 272 | { |
| 273 | sdbusplus::message::variant<T> value; |
| 274 | msg.read(value); |
| 275 | return _condition( |
| 276 | std::forward<T>(value.template get<T>())); |
| 277 | } |
| 278 | |
| 279 | private: |
| 280 | U _condition; |
| 281 | }; |
| 282 | |
Brad Bishop | bf5aa9c | 2016-10-19 21:19:04 -0400 | [diff] [blame] | 283 | } // namespace property_condition |
Brad Bishop | 3d57f50 | 2016-10-19 12:18:41 -0400 | [diff] [blame] | 284 | } // namespace details |
| 285 | |
Brad Bishop | 143522e | 2017-01-19 09:12:54 -0500 | [diff] [blame] | 286 | /** @brief Implicit type deduction for constructing PropertyChangedCondition. */ |
Brad Bishop | bf5aa9c | 2016-10-19 21:19:04 -0400 | [diff] [blame] | 287 | template <typename T> |
| 288 | auto propertyChangedTo( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 289 | const char* iface, |
| 290 | const char* property, |
Brad Bishop | fb083c2 | 2017-01-19 09:22:04 -0500 | [diff] [blame] | 291 | T&& val) |
Brad Bishop | bf5aa9c | 2016-10-19 21:19:04 -0400 | [diff] [blame] | 292 | { |
Brad Bishop | 76e2f6a | 2017-01-31 14:05:22 -0500 | [diff] [blame] | 293 | auto condition = [val = std::forward<T>(val)](T && arg) |
Brad Bishop | d1bbf3a | 2016-12-01 00:03:26 -0500 | [diff] [blame] | 294 | { |
| 295 | return arg == val; |
| 296 | }; |
Brad Bishop | bf5aa9c | 2016-10-19 21:19:04 -0400 | [diff] [blame] | 297 | using U = decltype(condition); |
Brad Bishop | 143522e | 2017-01-19 09:12:54 -0500 | [diff] [blame] | 298 | return details::property_condition::PropertyChangedCondition<T, U>( |
Brad Bishop | 7b33777 | 2017-01-12 16:11:24 -0500 | [diff] [blame] | 299 | iface, property, std::move(condition)); |
Brad Bishop | bf5aa9c | 2016-10-19 21:19:04 -0400 | [diff] [blame] | 300 | } |
| 301 | |
Brad Bishop | fa51da7 | 2017-01-19 11:06:51 -0500 | [diff] [blame] | 302 | /** @brief Implicit type deduction for constructing PropertyCondition. */ |
| 303 | template <typename T> |
| 304 | auto propertyIs( |
| 305 | const char* path, |
| 306 | const char* iface, |
| 307 | const char* property, |
| 308 | T&& val, |
| 309 | const char* service = nullptr) |
| 310 | { |
| 311 | auto condition = [val = std::forward<T>(val)](T && arg) |
| 312 | { |
| 313 | return arg == val; |
| 314 | }; |
| 315 | using U = decltype(condition); |
| 316 | return details::property_condition::PropertyCondition<T, U>( |
| 317 | path, iface, property, std::move(condition), service); |
| 318 | } |
| 319 | |
Brad Bishop | 3d57f50 | 2016-10-19 12:18:41 -0400 | [diff] [blame] | 320 | } // namespace filters |
| 321 | } // namespace manager |
| 322 | } // namespace inventory |
| 323 | } // namespace phosphor |
| 324 | |
| 325 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |