blob: 3168abe597f248605c43255a3d45cd373586253e [file] [log] [blame]
Brad Bishop4b916f12017-05-23 18:06:38 -04001/**
2 * @file propertywatch.hpp
3 * @brief PropertyWatch class declarations.
4 *
5 * In general class users should include propertywatchimpl.hpp instead to avoid
6 * link failures.
7 */
8#pragma once
9
10#include "data_types.hpp"
11#include "watch.hpp"
12
13namespace phosphor
14{
15namespace dbus
16{
17namespace monitoring
18{
19
Brad Bishopfccdc392017-05-22 21:11:09 -040020class Callback;
21
Brad Bishop4b916f12017-05-23 18:06:38 -040022/** @class PropertyWatch
23 * @brief Type agnostic, factored out logic for property watches.
24 *
25 * A property watch maintains the state of one or more DBus properties
26 * as specified by the supplied index.
27 */
Patrick Venture3d6d3182018-08-31 09:33:09 -070028template <typename DBusInterfaceType>
29class PropertyWatch : public Watch
Brad Bishop4b916f12017-05-23 18:06:38 -040030{
Brad Bishopd1eac882018-03-29 10:34:05 -040031 public:
32 PropertyWatch() = delete;
33 PropertyWatch(const PropertyWatch&) = delete;
34 PropertyWatch(PropertyWatch&&) = default;
35 PropertyWatch& operator=(const PropertyWatch&) = delete;
36 PropertyWatch& operator=(PropertyWatch&&) = default;
37 virtual ~PropertyWatch() = default;
38 PropertyWatch(const PropertyIndex& watchIndex,
39 Callback* callback = nullptr) :
40 Watch(),
41 index(watchIndex), cb(callback), alreadyRan(false)
42 {
43 }
Brad Bishop4b916f12017-05-23 18:06:38 -040044
Brad Bishopd1eac882018-03-29 10:34:05 -040045 /** @brief Start the watch.
46 *
47 * Watch start interface implementation for PropertyWatch.
48 */
49 void start() override;
Brad Bishop4b916f12017-05-23 18:06:38 -040050
Brad Bishopd1eac882018-03-29 10:34:05 -040051 /** @brief Run the watch callback method.
52 *
53 * Watch callback interface implementation for PropertyWatch.
54 */
55 void callback(Context ctx) override;
Brad Bishopce4fbe12017-06-06 23:58:09 -040056
Brad Bishopd1eac882018-03-29 10:34:05 -040057 /** @brief Update properties.
58 *
59 * Subclasses to query the properties specified by the index
60 * and update the cache.
61 *
62 * @param[in] busName - The busname hosting the interface to query.
63 * @param[in] path - The path of the interface to query.
64 * @param[in] interface - The interface to query.
65 */
66 virtual void updateProperties(const std::string& busName,
67 const std::string& path,
68 const std::string& interface) = 0;
Brad Bishop4b916f12017-05-23 18:06:38 -040069
Brad Bishopd1eac882018-03-29 10:34:05 -040070 /** @brief Dbus signal callback for PropertiesChanged.
71 *
72 * Subclasses to update the cache.
73 *
74 * @param[in] message - The org.freedesktop.DBus.PropertiesChanged
75 * message.
76 * @param[in] path - The path associated with the message.
77 * @param[in] interface - The interface associated with the message.
78 */
79 virtual void propertiesChanged(sdbusplus::message::message&,
80 const std::string& path,
81 const std::string& interface) = 0;
Brad Bishop4b916f12017-05-23 18:06:38 -040082
Brad Bishopd1eac882018-03-29 10:34:05 -040083 /** @brief Dbus signal callback for InterfacesAdded.
84 *
85 * Subclasses to update the cache.
86 *
87 * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
88 * message.
89 */
90 virtual void interfacesAdded(sdbusplus::message::message& msg) = 0;
Brad Bishop4b916f12017-05-23 18:06:38 -040091
Brad Bishopd1eac882018-03-29 10:34:05 -040092 protected:
93 /** @brief Property names and their associated storage. */
94 const PropertyIndex& index;
Brad Bishop4b916f12017-05-23 18:06:38 -040095
Brad Bishopd1eac882018-03-29 10:34:05 -040096 /** @brief Optional callback method. */
97 Callback* const cb;
Brad Bishop4b916f12017-05-23 18:06:38 -040098
Brad Bishopd1eac882018-03-29 10:34:05 -040099 /** @brief The start method should only be invoked once. */
100 bool alreadyRan;
Brad Bishop4b916f12017-05-23 18:06:38 -0400101};
102
103/** @class PropertyWatchOfType
104 * @brief Type specific logic for PropertyWatch.
105 *
106 * @tparam DBusInterfaceType - DBus access delegate.
107 * @tparam T - The type of the properties being watched.
108 */
109template <typename T, typename DBusInterfaceType>
110class PropertyWatchOfType : public PropertyWatch<DBusInterfaceType>
111{
Brad Bishopd1eac882018-03-29 10:34:05 -0400112 public:
113 PropertyWatchOfType() = default;
114 PropertyWatchOfType(const PropertyWatchOfType&) = delete;
115 PropertyWatchOfType(PropertyWatchOfType&&) = default;
116 PropertyWatchOfType& operator=(const PropertyWatchOfType&) = delete;
117 PropertyWatchOfType& operator=(PropertyWatchOfType&&) = default;
118 ~PropertyWatchOfType() = default;
Matthew Barthae786ef2019-09-04 15:46:13 -0500119 PropertyWatchOfType(const std::vector<std::function<bool(T)>>& filterOps,
120 const PropertyIndex& watchIndex, Callback& callback) :
121 PropertyWatch<DBusInterfaceType>(watchIndex, &callback),
122 filterOps(filterOps)
Brad Bishopd1eac882018-03-29 10:34:05 -0400123 {
124 }
Matthew Barthae786ef2019-09-04 15:46:13 -0500125 PropertyWatchOfType(const std::vector<std::function<bool(T)>>& filterOps,
126 const PropertyIndex& watchIndex) :
127 PropertyWatch<DBusInterfaceType>(watchIndex, nullptr),
128 filterOps(filterOps)
Brad Bishopd1eac882018-03-29 10:34:05 -0400129 {
130 }
Brad Bishopfccdc392017-05-22 21:11:09 -0400131
Brad Bishopd1eac882018-03-29 10:34:05 -0400132 /** @brief PropertyMatch implementation for PropertyWatchOfType.
133 *
134 * @param[in] busName - The busname hosting the interface to query.
135 * @param[in] path - The path of the interface to query.
136 * @param[in] interface - The interface to query.
137 */
138 void updateProperties(const std::string& busName, const std::string& path,
139 const std::string& interface) override;
Brad Bishop4b916f12017-05-23 18:06:38 -0400140
Brad Bishopd1eac882018-03-29 10:34:05 -0400141 /** @brief PropertyMatch implementation for PropertyWatchOfType.
142 *
143 * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
144 * message.
145 * @param[in] path - The path associated with the message.
146 * @param[in] interface - The interface associated with the message.
147 */
148 void propertiesChanged(sdbusplus::message::message& msg,
149 const std::string& path,
150 const std::string& interface) override;
Brad Bishop4b916f12017-05-23 18:06:38 -0400151
Brad Bishopd1eac882018-03-29 10:34:05 -0400152 /** @brief DBus agnostic implementation of interfacesAdded.
153 *
154 * @param[in] path - The path of the properties that changed.
155 * @param[in] interface - The interface of the properties that
156 * changed.
157 * @param[in] properites - The properties that changed.
158 */
159 void propertiesChanged(const std::string& path,
160 const std::string& interface,
161 const PropertiesChanged<T>& properties);
Brad Bishop4b916f12017-05-23 18:06:38 -0400162
Brad Bishopd1eac882018-03-29 10:34:05 -0400163 /** @brief PropertyMatch implementation for PropertyWatchOfType.
164 *
165 * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
166 * message.
167 */
168 void interfacesAdded(sdbusplus::message::message& msg) override;
Brad Bishop4b916f12017-05-23 18:06:38 -0400169
Brad Bishopd1eac882018-03-29 10:34:05 -0400170 /** @brief DBus agnostic implementation of interfacesAdded.
171 *
172 * @param[in] path - The path of the added interfaces.
173 * @param[in] interfaces - The added interfaces.
174 */
175 void interfacesAdded(const std::string& path,
176 const InterfacesAdded<T>& interfaces);
Matthew Barthae786ef2019-09-04 15:46:13 -0500177
178 private:
179 /** @brief List of filter operations to perform on property changes. */
180 const std::vector<std::function<bool(T)>> filterOps;
Brad Bishop4b916f12017-05-23 18:06:38 -0400181};
182
183} // namespace monitoring
184} // namespace dbus
185} // namespace phosphor