blob: 3faf39b94c5c71a030a831b934f593676f3ee1d1 [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
Matthew Barthc5736432017-04-17 12:22:50 -050014/**
15 * @brief Create a condition function object
16 *
17 * @param[in] condition - The condition being created
18 *
19 * @return - The created condition function object
20 */
Matthew Barthb86374d2017-04-12 10:57:19 -050021template <typename T>
22auto make_condition(T&& condition)
23{
24 return Condition(std::forward<T>(condition));
25}
26
Matthew Barthc5736432017-04-17 12:22:50 -050027/**
28 * @brief Create an action function object
29 *
30 * @param[in] action - The action being created
31 *
32 * @return - The created action function object
33 */
Matthew Barthb86374d2017-04-12 10:57:19 -050034template <typename T>
35auto make_action(T&& action)
36{
37 return Action(std::forward<T>(action));
38}
39
Matthew Barthc5736432017-04-17 12:22:50 -050040/**
41 * @struct Property Changed Condtion
42 * @brief A match filter functor to test Dbus property value changed signals
43 *
44 * @tparam T - The type of the property value
45 * @tparam U - The type of the condition
46 */
Matthew Barth240bf332017-04-12 13:48:29 -050047template <typename T, typename U>
48struct PropertyChangedCondition
49{
50 PropertyChangedCondition() = delete;
51 ~PropertyChangedCondition() = default;
52 PropertyChangedCondition(const PropertyChangedCondition&) = default;
53 PropertyChangedCondition& operator=(const PropertyChangedCondition&) =
54 default;
55 PropertyChangedCondition(PropertyChangedCondition&&) = default;
56 PropertyChangedCondition& operator=(PropertyChangedCondition&&) =
57 default;
58 PropertyChangedCondition(const char* iface, const char* property,
59 U&& condition) :
60 _iface(iface),
61 _property(property),
62 _condition(std::forward<U>(condition)) { }
63
64 /** @brief Test a property value.
65 *
66 * Extract the property from the PropertiesChanged
67 * message and run the condition test.
68 */
69 bool operator()(
70 sdbusplus::bus::bus&,
71 sdbusplus::message::message& msg,
72 Monitor&) const
73 {
74 std::map<std::string, sdbusplus::message::variant<T>> properties;
75 const char* iface = nullptr;
76
77 msg.read(iface);
78 if (!iface || strcmp(iface, _iface))
79 {
80 return false;
81 }
82
83 msg.read(properties);
84 auto it = properties.find(_property);
85 if (it == properties.cend())
86 {
87 return false;
88 }
89
90 return _condition(
91 std::forward<T>(it->second.template get<T>()));
92 }
93
94private:
95 const char* _iface;
96 const char* _property;
97 U _condition;
98};
99
Matthew Barthc5736432017-04-17 12:22:50 -0500100/**
101 * @struct Property Condition Base
102 * @brief A match filter functor to test property values
103 * @details The base property condition struct that retrieves the property value
104 * for a property condition
105 */
Matthew Barthe6af2332017-04-12 13:37:29 -0500106struct PropertyConditionBase
107{
108 PropertyConditionBase() = delete;
109 virtual ~PropertyConditionBase() = default;
110 PropertyConditionBase(const PropertyConditionBase&) = default;
111 PropertyConditionBase& operator=(const PropertyConditionBase&) = default;
112 PropertyConditionBase(PropertyConditionBase&&) = default;
113 PropertyConditionBase& operator=(PropertyConditionBase&&) = default;
114
115 /** @brief Constructor
116 *
117 * The service argument can be nullptr. If something
118 * else is provided the function will call the the
119 * service directly. If omitted, the function will
120 * look up the service in the ObjectMapper.
121 *
122 * @param path - The path of the object containing
123 * the property to be tested.
124 * @param iface - The interface hosting the property
125 * to be tested.
126 * @param property - The property to be tested.
127 * @param service - The DBus service hosting the object.
128 */
129 PropertyConditionBase(
130 const char* path,
131 const char* iface,
132 const char* property,
133 const char* service) :
134 _path(path ? path : std::string()),
135 _iface(iface),
136 _property(property),
137 _service(service) {}
138
139 /** @brief Forward comparison to type specific implementation. */
140 virtual bool eval(sdbusplus::message::message&) const = 0;
141
142 /** @brief Test a property value.
143 *
144 * Make a DBus call and test the value of any property.
145 */
146 bool operator()(
147 sdbusplus::bus::bus&,
148 sdbusplus::message::message&,
149 Monitor&) const;
150
151private:
152 std::string _path;
153 std::string _iface;
154 std::string _property;
155 const char* _service;
156};
157
Matthew Barthc5736432017-04-17 12:22:50 -0500158/**
159 * @struct Property Condtion
160 * @brief A match filter functor to test property values
161 *
162 * @tparam T - The type of the property value
163 * @tparam U - The type of the condition
164 */
Matthew Barthe6af2332017-04-12 13:37:29 -0500165template <typename T, typename U>
166struct PropertyCondition final : public PropertyConditionBase
167{
168 PropertyCondition() = delete;
169 ~PropertyCondition() = default;
170 PropertyCondition(const PropertyCondition&) = default;
171 PropertyCondition& operator=(const PropertyCondition&) = default;
172 PropertyCondition(PropertyCondition&&) = default;
173 PropertyCondition& operator=(PropertyCondition&&) = default;
174
175 /** @brief Constructor
176 *
177 * The service argument can be nullptr. If something
178 * else is provided the function will call the the
179 * service directly. If omitted, the function will
180 * look up the service in the ObjectMapper.
181 *
182 * @param path - The path of the object containing
183 * the property to be tested.
184 * @param iface - The interface hosting the property
185 * to be tested.
186 * @param property - The property to be tested.
187 * @param condition - The test to run on the property.
188 * @param service - The DBus service hosting the object.
189 */
190 PropertyCondition(
191 const char* path,
192 const char* iface,
193 const char* property,
194 U&& condition,
195 const char* service) :
196 PropertyConditionBase(path, iface, property, service),
197 _condition(std::forward<decltype(condition)>(condition)) {}
198
199 /** @brief Test a property value.
200 *
201 * Make a DBus call and test the value of any property.
202 */
203 bool eval(sdbusplus::message::message& msg) const override
204 {
205 sdbusplus::message::variant<T> value;
206 msg.read(value);
207 return _condition(std::forward<T>(value.template get<T>()));
208 }
209
210private:
211 U _condition;
212};
213
Matthew Barthc5736432017-04-17 12:22:50 -0500214/**
215 * @brief Used to process a Dbus property changed signal event
216 *
217 * @param[in] iface - Item value interface
218 * @param[in] property - Item value property
219 * @param[in] condition - Condition function to perform
220 *
221 * @tparam T - The type of the property
222 * @tparam U - The type of the condition
223 */
Matthew Barthe6af2332017-04-12 13:37:29 -0500224template <typename T, typename U>
Matthew Barth240bf332017-04-12 13:48:29 -0500225auto propertySignal(const char* iface,
226 const char* property,
227 U&& condition)
228{
229 return PropertyChangedCondition<T, U>(iface,
230 property,
231 std::move(condition));
232}
233
Matthew Barthc5736432017-04-17 12:22:50 -0500234/**
235 * @brief Used to process conditions on a start event
236 *
237 * @param[in] path - Item's Dbus path
238 * @param[in] iface - Item value interface
239 * @param[in] property - Item value property
240 * @param[in] condition - Condition function to perform
241 * @param[in] service - Service to lookup Dbus object
242 *
243 * @tparam T - The type of the property
244 * @tparam U - The type of the condition
245 */
Matthew Barth240bf332017-04-12 13:48:29 -0500246template <typename T, typename U>
Matthew Barthe6af2332017-04-12 13:37:29 -0500247auto propertyStart(const char* path,
248 const char* iface,
249 const char* property,
250 U&& condition,
251 const char* service = nullptr)
252{
253 return PropertyCondition<T, U>(path,
254 iface,
255 property,
256 std::move(condition),
257 service);
258}
259
Matthew Barthb86374d2017-04-12 10:57:19 -0500260} // namespace monitoring
261} // namespace dbus
262} // namespace phosphor