Matthew Barth | b86374d | 2017-04-12 10:57:19 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "data_types.hpp" |
| 4 | |
| 5 | namespace phosphor |
| 6 | { |
| 7 | namespace dbus |
| 8 | { |
| 9 | namespace monitoring |
| 10 | { |
| 11 | |
| 12 | class Monitor; |
| 13 | |
| 14 | template <typename T> |
| 15 | auto make_condition(T&& condition) |
| 16 | { |
| 17 | return Condition(std::forward<T>(condition)); |
| 18 | } |
| 19 | |
| 20 | template <typename T> |
| 21 | auto make_action(T&& action) |
| 22 | { |
| 23 | return Action(std::forward<T>(action)); |
| 24 | } |
| 25 | |
Matthew Barth | e6af233 | 2017-04-12 13:37:29 -0500 | [diff] [blame^] | 26 | struct PropertyConditionBase |
| 27 | { |
| 28 | PropertyConditionBase() = delete; |
| 29 | virtual ~PropertyConditionBase() = default; |
| 30 | PropertyConditionBase(const PropertyConditionBase&) = default; |
| 31 | PropertyConditionBase& operator=(const PropertyConditionBase&) = default; |
| 32 | PropertyConditionBase(PropertyConditionBase&&) = default; |
| 33 | PropertyConditionBase& operator=(PropertyConditionBase&&) = default; |
| 34 | |
| 35 | /** @brief Constructor |
| 36 | * |
| 37 | * The service argument can be nullptr. If something |
| 38 | * else is provided the function will call the the |
| 39 | * service directly. If omitted, the function will |
| 40 | * look up the service in the ObjectMapper. |
| 41 | * |
| 42 | * @param path - The path of the object containing |
| 43 | * the property to be tested. |
| 44 | * @param iface - The interface hosting the property |
| 45 | * to be tested. |
| 46 | * @param property - The property to be tested. |
| 47 | * @param service - The DBus service hosting the object. |
| 48 | */ |
| 49 | PropertyConditionBase( |
| 50 | const char* path, |
| 51 | const char* iface, |
| 52 | const char* property, |
| 53 | const char* service) : |
| 54 | _path(path ? path : std::string()), |
| 55 | _iface(iface), |
| 56 | _property(property), |
| 57 | _service(service) {} |
| 58 | |
| 59 | /** @brief Forward comparison to type specific implementation. */ |
| 60 | virtual bool eval(sdbusplus::message::message&) const = 0; |
| 61 | |
| 62 | /** @brief Test a property value. |
| 63 | * |
| 64 | * Make a DBus call and test the value of any property. |
| 65 | */ |
| 66 | bool operator()( |
| 67 | sdbusplus::bus::bus&, |
| 68 | sdbusplus::message::message&, |
| 69 | Monitor&) const; |
| 70 | |
| 71 | private: |
| 72 | std::string _path; |
| 73 | std::string _iface; |
| 74 | std::string _property; |
| 75 | const char* _service; |
| 76 | }; |
| 77 | |
| 78 | template <typename T, typename U> |
| 79 | struct PropertyCondition final : public PropertyConditionBase |
| 80 | { |
| 81 | PropertyCondition() = delete; |
| 82 | ~PropertyCondition() = default; |
| 83 | PropertyCondition(const PropertyCondition&) = default; |
| 84 | PropertyCondition& operator=(const PropertyCondition&) = default; |
| 85 | PropertyCondition(PropertyCondition&&) = default; |
| 86 | PropertyCondition& operator=(PropertyCondition&&) = default; |
| 87 | |
| 88 | /** @brief Constructor |
| 89 | * |
| 90 | * The service argument can be nullptr. If something |
| 91 | * else is provided the function will call the the |
| 92 | * service directly. If omitted, the function will |
| 93 | * look up the service in the ObjectMapper. |
| 94 | * |
| 95 | * @param path - The path of the object containing |
| 96 | * the property to be tested. |
| 97 | * @param iface - The interface hosting the property |
| 98 | * to be tested. |
| 99 | * @param property - The property to be tested. |
| 100 | * @param condition - The test to run on the property. |
| 101 | * @param service - The DBus service hosting the object. |
| 102 | */ |
| 103 | PropertyCondition( |
| 104 | const char* path, |
| 105 | const char* iface, |
| 106 | const char* property, |
| 107 | U&& condition, |
| 108 | const char* service) : |
| 109 | PropertyConditionBase(path, iface, property, service), |
| 110 | _condition(std::forward<decltype(condition)>(condition)) {} |
| 111 | |
| 112 | /** @brief Test a property value. |
| 113 | * |
| 114 | * Make a DBus call and test the value of any property. |
| 115 | */ |
| 116 | bool eval(sdbusplus::message::message& msg) const override |
| 117 | { |
| 118 | sdbusplus::message::variant<T> value; |
| 119 | msg.read(value); |
| 120 | return _condition(std::forward<T>(value.template get<T>())); |
| 121 | } |
| 122 | |
| 123 | private: |
| 124 | U _condition; |
| 125 | }; |
| 126 | |
| 127 | template <typename T, typename U> |
| 128 | auto propertyStart(const char* path, |
| 129 | const char* iface, |
| 130 | const char* property, |
| 131 | U&& condition, |
| 132 | const char* service = nullptr) |
| 133 | { |
| 134 | return PropertyCondition<T, U>(path, |
| 135 | iface, |
| 136 | property, |
| 137 | std::move(condition), |
| 138 | service); |
| 139 | } |
| 140 | |
Matthew Barth | b86374d | 2017-04-12 10:57:19 -0500 | [diff] [blame] | 141 | } // namespace monitoring |
| 142 | } // namespace dbus |
| 143 | } // namespace phosphor |