diff options
author | Brad Bishop <bradleyb@fuzziesquirrel.com> | 2016-10-19 21:19:04 -0400 |
---|---|---|
committer | Brad Bishop <bradleyb@fuzziesquirrel.com> | 2016-11-08 15:00:56 -0500 |
commit | bf5aa9cbf5de0642e26dd0c90cf73be1cc7182a5 (patch) | |
tree | f4e1f4e317a68a3ad14976e31d6ebf3a99de075b | |
parent | c038e016eac8a4bac9b4a06546fe975eb63ce9ff (diff) | |
download | phosphor-inventory-manager-bf5aa9cbf5de0642e26dd0c90cf73be1cc7182a5.tar.gz phosphor-inventory-manager-bf5aa9cbf5de0642e26dd0c90cf73be1cc7182a5.zip |
filters: Add propertiesChangedTo
Add a signal match filter for properties changing to a specific
value.
Change-Id: I10f20ba03ae7c629d2c338c7975e0d32d9008e01
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | examples/match2.yaml | 6 | ||||
-rw-r--r-- | filters.hpp | 67 | ||||
-rw-r--r-- | test/Makefile.am | 1 |
4 files changed, 75 insertions, 1 deletions
@@ -56,6 +56,8 @@ Supported filter tags are: The available filters provided by PIM are: * none - A non-filter. +* propertyChangedTo - Only match events when the specified property has +the specified value. ---- diff --git a/examples/match2.yaml b/examples/match2.yaml index 6fb9ff9..8cfa105 100644 --- a/examples/match2.yaml +++ b/examples/match2.yaml @@ -13,6 +13,10 @@ events: interface: org.freedesktop.DBus.Properties member: PropertiesChanged filter: - name: none + name: propertyChangedTo + args: + - value: xyz.openbmc_project.Testing + - value: TestProperty + - value: teststring # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/filters.hpp b/filters.hpp index eb843a7..309fff6 100644 --- a/filters.hpp +++ b/filters.hpp @@ -106,6 +106,60 @@ struct Wrapper std::shared_ptr<holder::Base> _ptr; }; +namespace property_condition +{ + +/** @struct PropertyCondition + * @brief Match filter functor that tests a property value. + * + * @tparam T - The type of the property being tested. + * @tparam U - The type of the condition checking functor. + */ +template <typename T, typename U> +struct PropertyCondition +{ + PropertyCondition() = delete; + ~PropertyCondition() = default; + PropertyCondition(const PropertyCondition&) = default; + PropertyCondition & operator=(const PropertyCondition&) = delete; + PropertyCondition(PropertyCondition&&) = default; + PropertyCondition& operator=(PropertyCondition&&) = default; + PropertyCondition(const char *iface, const char *property, U &&condition) : + _iface(iface), + _property(property), + _condition(std::forward<U>(condition)) { } + + /** @brief Test a property value. + * + * Extract the property from the PropertiesChanged + * message and run the condition test. + */ + bool operator()(sdbusplus::message::message &msg) const + { + std::map< + std::string, + sdbusplus::message::variant<T>> properties; + const char *iface = nullptr; + + msg.read(iface); + if(strcmp(iface, _iface)) + return false; + + msg.read(properties); + auto it = properties.find(_property); + if(it == properties.cend()) + return false; + + return _condition(it->second); + } + + private: + const char *_iface; + const char *_property; + U _condition; +}; + +} // namespace property_condition } // namespace details /** @brief The default filter. */ @@ -114,6 +168,19 @@ inline bool none(sdbusplus::message::message &) noexcept return true; } +/** @brief Implicit type deduction for constructing PropertyCondition. */ +template <typename T> +auto propertyChangedTo( + const char *iface, + const char *property, + T val) +{ + auto condition = [val = std::move(val)](auto arg){return arg == val;}; + using U = decltype(condition); + return details::property_condition::PropertyCondition<T, U>( + iface, property, std::move(condition)); +} + } // namespace filters } // namespace manager } // namespace inventory diff --git a/test/Makefile.am b/test/Makefile.am index 4026307..2c340ea 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -5,4 +5,5 @@ phosphor_inventory_test_SOURCES = \ phosphor_inventory_test_LDFLAGS = $(SYSTEMD_LIBS) phosphor_inventory_test_CFLAGS = $(SYSTEMD_CFLAGS) phosphor_inventory_test_LDADD = ${top_builddir}/manager.o \ + ${top_builddir}/filters.o \ ${top_builddir}/server.o |