blob: 1d88eaea0bdd65d4d13525c4e4a6f589eefccd58 [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"
Matthew Barthefe01582019-09-09 15:22:37 -050011#include "filters.hpp"
Brad Bishop4b916f12017-05-23 18:06:38 -040012#include "watch.hpp"
13
Andrew Geisslerae4c95c2020-05-16 13:58:53 -050014#include <string>
15
Brad Bishop4b916f12017-05-23 18:06:38 -040016namespace phosphor
17{
18namespace dbus
19{
20namespace monitoring
21{
22
Brad Bishopfccdc392017-05-22 21:11:09 -040023class Callback;
24
Brad Bishop4b916f12017-05-23 18:06:38 -040025/** @class PropertyWatch
26 * @brief Type agnostic, factored out logic for property watches.
27 *
28 * A property watch maintains the state of one or more DBus properties
29 * as specified by the supplied index.
30 */
Patrick Venture3d6d3182018-08-31 09:33:09 -070031template <typename DBusInterfaceType>
32class PropertyWatch : public Watch
Brad Bishop4b916f12017-05-23 18:06:38 -040033{
Brad Bishopd1eac882018-03-29 10:34:05 -040034 public:
35 PropertyWatch() = delete;
36 PropertyWatch(const PropertyWatch&) = delete;
37 PropertyWatch(PropertyWatch&&) = default;
38 PropertyWatch& operator=(const PropertyWatch&) = delete;
39 PropertyWatch& operator=(PropertyWatch&&) = default;
40 virtual ~PropertyWatch() = default;
41 PropertyWatch(const PropertyIndex& watchIndex,
Lei YU98d64622022-05-24 19:11:23 +080042 bool ignoreStartCallback = false,
Brad Bishopd1eac882018-03-29 10:34:05 -040043 Callback* callback = nullptr) :
44 Watch(),
Lei YU98d64622022-05-24 19:11:23 +080045 index(watchIndex), cb(callback), alreadyRan(false),
46 ignoreStartCallback(ignoreStartCallback)
George Liu3fe976c2022-06-21 09:37:04 +080047 {}
Brad Bishop4b916f12017-05-23 18:06:38 -040048
Brad Bishopd1eac882018-03-29 10:34:05 -040049 /** @brief Start the watch.
50 *
51 * Watch start interface implementation for PropertyWatch.
52 */
53 void start() override;
Brad Bishop4b916f12017-05-23 18:06:38 -040054
Brad Bishopd1eac882018-03-29 10:34:05 -040055 /** @brief Run the watch callback method.
56 *
57 * Watch callback interface implementation for PropertyWatch.
58 */
59 void callback(Context ctx) override;
Brad Bishopce4fbe12017-06-06 23:58:09 -040060
Brad Bishopd1eac882018-03-29 10:34:05 -040061 /** @brief Update properties.
62 *
63 * Subclasses to query the properties specified by the index
64 * and update the cache.
65 *
66 * @param[in] busName - The busname hosting the interface to query.
67 * @param[in] path - The path of the interface to query.
68 * @param[in] interface - The interface to query.
69 */
70 virtual void updateProperties(const std::string& busName,
71 const std::string& path,
72 const std::string& interface) = 0;
Brad Bishop4b916f12017-05-23 18:06:38 -040073
Brad Bishopd1eac882018-03-29 10:34:05 -040074 /** @brief Dbus signal callback for PropertiesChanged.
75 *
76 * Subclasses to update the cache.
77 *
78 * @param[in] message - The org.freedesktop.DBus.PropertiesChanged
79 * message.
80 * @param[in] path - The path associated with the message.
81 * @param[in] interface - The interface associated with the message.
82 */
83 virtual void propertiesChanged(sdbusplus::message::message&,
84 const std::string& path,
85 const std::string& interface) = 0;
Brad Bishop4b916f12017-05-23 18:06:38 -040086
Brad Bishopd1eac882018-03-29 10:34:05 -040087 /** @brief Dbus signal callback for InterfacesAdded.
88 *
89 * Subclasses to update the cache.
90 *
91 * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
92 * message.
93 */
94 virtual void interfacesAdded(sdbusplus::message::message& msg) = 0;
Brad Bishop4b916f12017-05-23 18:06:38 -040095
Brad Bishopd1eac882018-03-29 10:34:05 -040096 protected:
97 /** @brief Property names and their associated storage. */
98 const PropertyIndex& index;
Brad Bishop4b916f12017-05-23 18:06:38 -040099
Brad Bishopd1eac882018-03-29 10:34:05 -0400100 /** @brief Optional callback method. */
101 Callback* const cb;
Brad Bishop4b916f12017-05-23 18:06:38 -0400102
Brad Bishopd1eac882018-03-29 10:34:05 -0400103 /** @brief The start method should only be invoked once. */
104 bool alreadyRan;
Lei YU98d64622022-05-24 19:11:23 +0800105
106 /** @brief Ignore callback on start */
107 bool ignoreStartCallback;
Brad Bishop4b916f12017-05-23 18:06:38 -0400108};
109
110/** @class PropertyWatchOfType
111 * @brief Type specific logic for PropertyWatch.
112 *
113 * @tparam DBusInterfaceType - DBus access delegate.
114 * @tparam T - The type of the properties being watched.
115 */
116template <typename T, typename DBusInterfaceType>
117class PropertyWatchOfType : public PropertyWatch<DBusInterfaceType>
118{
Brad Bishopd1eac882018-03-29 10:34:05 -0400119 public:
120 PropertyWatchOfType() = default;
121 PropertyWatchOfType(const PropertyWatchOfType&) = delete;
122 PropertyWatchOfType(PropertyWatchOfType&&) = default;
123 PropertyWatchOfType& operator=(const PropertyWatchOfType&) = delete;
124 PropertyWatchOfType& operator=(PropertyWatchOfType&&) = default;
125 ~PropertyWatchOfType() = default;
Matthew Barthefe01582019-09-09 15:22:37 -0500126 PropertyWatchOfType(const PropertyIndex& watchIndex, Callback& callback,
Lei YU98d64622022-05-24 19:11:23 +0800127 bool ignoreStartCallback = false,
Matthew Barthefe01582019-09-09 15:22:37 -0500128 Filters* filterOps = nullptr) :
Lei YU98d64622022-05-24 19:11:23 +0800129 PropertyWatch<DBusInterfaceType>(watchIndex, ignoreStartCallback,
130 &callback),
Matthew Barthae786ef2019-09-04 15:46:13 -0500131 filterOps(filterOps)
George Liu3fe976c2022-06-21 09:37:04 +0800132 {}
Matthew Barthefe01582019-09-09 15:22:37 -0500133 PropertyWatchOfType(const PropertyIndex& watchIndex,
Lei YU98d64622022-05-24 19:11:23 +0800134 bool ignoreStartCallback = false,
Matthew Barthefe01582019-09-09 15:22:37 -0500135 Filters* filterOps = nullptr) :
Lei YU98d64622022-05-24 19:11:23 +0800136 PropertyWatch<DBusInterfaceType>(watchIndex, ignoreStartCallback,
137 nullptr),
Matthew Barthae786ef2019-09-04 15:46:13 -0500138 filterOps(filterOps)
George Liu3fe976c2022-06-21 09:37:04 +0800139 {}
Brad Bishopfccdc392017-05-22 21:11:09 -0400140
Brad Bishopd1eac882018-03-29 10:34:05 -0400141 /** @brief PropertyMatch implementation for PropertyWatchOfType.
142 *
143 * @param[in] busName - The busname hosting the interface to query.
144 * @param[in] path - The path of the interface to query.
145 * @param[in] interface - The interface to query.
146 */
147 void updateProperties(const std::string& busName, const std::string& path,
148 const std::string& interface) override;
Brad Bishop4b916f12017-05-23 18:06:38 -0400149
Brad Bishopd1eac882018-03-29 10:34:05 -0400150 /** @brief PropertyMatch implementation for PropertyWatchOfType.
151 *
152 * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
153 * message.
154 * @param[in] path - The path associated with the message.
155 * @param[in] interface - The interface associated with the message.
156 */
157 void propertiesChanged(sdbusplus::message::message& msg,
158 const std::string& path,
159 const std::string& interface) override;
Brad Bishop4b916f12017-05-23 18:06:38 -0400160
Brad Bishopd1eac882018-03-29 10:34:05 -0400161 /** @brief DBus agnostic implementation of interfacesAdded.
162 *
163 * @param[in] path - The path of the properties that changed.
164 * @param[in] interface - The interface of the properties that
165 * changed.
166 * @param[in] properites - The properties that changed.
167 */
168 void propertiesChanged(const std::string& path,
169 const std::string& interface,
170 const PropertiesChanged<T>& properties);
Brad Bishop4b916f12017-05-23 18:06:38 -0400171
Brad Bishopd1eac882018-03-29 10:34:05 -0400172 /** @brief PropertyMatch implementation for PropertyWatchOfType.
173 *
174 * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
175 * message.
176 */
177 void interfacesAdded(sdbusplus::message::message& msg) override;
Brad Bishop4b916f12017-05-23 18:06:38 -0400178
Brad Bishopd1eac882018-03-29 10:34:05 -0400179 /** @brief DBus agnostic implementation of interfacesAdded.
180 *
181 * @param[in] path - The path of the added interfaces.
182 * @param[in] interfaces - The added interfaces.
183 */
184 void interfacesAdded(const std::string& path,
185 const InterfacesAdded<T>& interfaces);
Matthew Barthae786ef2019-09-04 15:46:13 -0500186
187 private:
Matthew Barthefe01582019-09-09 15:22:37 -0500188 /** @brief Optional filter operations to perform on property changes. */
189 Filters* const filterOps;
Brad Bishop4b916f12017-05-23 18:06:38 -0400190};
191
192} // namespace monitoring
193} // namespace dbus
194} // namespace phosphor