blob: c2c586ece75119a3823c098d1a261c9cb165076a [file] [log] [blame]
Matthew Barthb86374d2017-04-12 10:57:19 -05001#pragma once
2
3#include "data_types.hpp"
4
5namespace phosphor
6{
7namespace dbus
8{
9namespace monitoring
10{
11
12class Monitor;
13
14template <typename T>
15auto make_condition(T&& condition)
16{
17 return Condition(std::forward<T>(condition));
18}
19
20template <typename T>
21auto make_action(T&& action)
22{
23 return Action(std::forward<T>(action));
24}
25
Matthew Barthe6af2332017-04-12 13:37:29 -050026struct 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
71private:
72 std::string _path;
73 std::string _iface;
74 std::string _property;
75 const char* _service;
76};
77
78template <typename T, typename U>
79struct 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
123private:
124 U _condition;
125};
126
127template <typename T, typename U>
128auto 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 Barthb86374d2017-04-12 10:57:19 -0500141} // namespace monitoring
142} // namespace dbus
143} // namespace phosphor